Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

User struct hält Daten-Port/IP. Mit port-command begonnen. Falsche ch…

…ar-array Größen in get_ip korrigiert
  • Loading branch information...
commit 65235d8639d2264d03c2dcdcbae4c0658fb4e61d 1 parent a28f999
Robert A authored
Showing with 42 additions and 13 deletions.
  1. +42 −13 duoit.c
55 duoit.c
View
@@ -27,7 +27,8 @@ typedef struct {
typedef struct usr {
int csocket;
int dsocket;
- int dport;
+ char d_address[INET6_ADDRSTRLEN];
+ int d_port;
int passv;
int type;
char *pwd;
@@ -106,10 +107,14 @@ int initialize_server() {
* initialize head of user-list
*/
int ini_user_list(int newfd, usr **user) {
+ char ipstr[INET6_ADDRSTRLEN];
+
+ get_ip(newfd, ipstr, INET6_ADDRSTRLEN);
*user = calloc(1, sizeof(struct usr));
(*user)->csocket = newfd;
(*user)->dsocket = 0;
- (*user)->dport = get_port(newfd);
+ strcpy((*user)->d_address, ipstr);
+ (*user)->d_port = get_port(newfd);
(*user)->passv = 0;
(*user)->type = 1;
(*user)->pwd = calloc(1, PATHLENGTH);
@@ -121,8 +126,10 @@ int ini_user_list(int newfd, usr **user) {
/*
* append user to user-list
*/
-int append_user(int newfd, usr *user){
+int append_user(int newfd, usr *user) {
+ char ipstr[INET6_ADDRSTRLEN];
+ get_ip(newfd, ipstr, INET6_ADDRSTRLEN);
while (user->next) {
user = user->next;
}
@@ -131,7 +138,8 @@ int append_user(int newfd, usr *user){
user = user->next;
user->csocket = newfd;
user->dsocket = 0;
- user->dport = get_port(newfd);
+ strcpy(user->d_address, ipstr);
+ user->d_port = get_port(newfd);
user->passv = 0;
user->type = 1;
user->pwd = calloc(1, PATHLENGTH);
@@ -311,7 +319,7 @@ int user_command(instruc *instruction, usr *user) {
return 1;
}
- if (!(strcmp(instruction->params, "duoit"))) {
+ if (!(strcmp(instruction->params, "anonymous"))) {
send_message(user->csocket, "230 User logged in.");
} else {
send_message(user->csocket, "530 Not logged in.");
@@ -320,6 +328,23 @@ int user_command(instruc *instruction, usr *user) {
}
/*
+ * save non-default data-connection infos
+ */
+int port_command(instruc *instruction, usr *user) {
+
+
+ if (!instruction->params) {
+ send_message(user->csocket, "501 Syntax error in parameters or arguments.");
+ return 1;
+ }
+ //remove redundant whitespaces from params
+ instruction->params = trim(instruction->params);
+
+ send_message(user->csocket, "200 PORT command successful.");
+ return 0;
+}
+
+/*
* sets the mode of file transfer
*/
int set_user_type(instruc *instruction, usr *user) {
@@ -390,20 +415,20 @@ int get_port(int sockfd) {
}
/*
- * fills the ip-string with the formatted IP[PORT] of the corresponding user
+ * fills the ip-string with the formatted IP of socket
*/
-int get_ip(usr *user, char *ip, int size) {
+int get_ip(int socket, char *ip, int size) {
socklen_t len;
struct sockaddr_storage addr;
- char ipstr[INET6_ADDRSTRLEN + 5];
+ char ipstr[INET6_ADDRSTRLEN];
//check whether buffer is big enough
- if(size < (INET6_ADDRSTRLEN + 5)) {
+ if(size < (INET6_ADDRSTRLEN)) {
return 1;
}
len = sizeof(struct sockaddr_storage);
- getpeername(user->csocket, (struct sockaddr*)&addr, &len);
+ getpeername(socket, (struct sockaddr*)&addr, &len);
//deal with both IPv4 and IPv6
if (addr.ss_family == AF_INET) {
struct sockaddr_in *sock = (struct sockaddr_in *)&addr;
@@ -420,10 +445,10 @@ int get_ip(usr *user, char *ip, int size) {
int log_action(usr *user, char *message) {
char log_time[256];
time_t curtime;
- char ip[INET6_ADDRSTRLEN + 5];
+ char ip[INET6_ADDRSTRLEN];
curtime = time(NULL);
- if (get_ip(user, ip, sizeof ip)) {
+ if (get_ip(user->csocket, ip, sizeof ip)) {
return 1;
}
strftime(log_time, 256, "%H:%M", localtime(&curtime));
@@ -449,6 +474,11 @@ int dispatch(usr *user, char *received, int received_len, fd_set *master, usr **
if (user_command(&instruction, user)) {
return 1;
}
+ } else if (!strcmp(instruction.command, "PORT")) {
+ printf("+++params: %s\n", instruction.params);
+ if (port_command(&instruction, user)) {
+ return 1;
+ }
} else if (!strcmp(instruction.command, "TYPE")) {
if (set_user_type(&instruction, user)) {
return 1;
@@ -499,7 +529,6 @@ int parse_command(char *received, int received_len, instruc *instruction) {
return 1;
}
}
-
printf("command: %s\n", received);
instruction->command = received;
Please sign in to comment.
Something went wrong with that request. Please try again.