Permalink
Browse files

fake list command

  • Loading branch information...
1 parent 96dbbd3 commit 8fb3a27ce0826c63872e039e746c942dd070701c @grobie committed Jun 10, 2009
Showing with 92 additions and 34 deletions.
  1. +92 −34 duoit.c
View
126 duoit.c
@@ -15,7 +15,7 @@
#define DPORT "3020" // the default port for data transfering
#define BACKLOG 10 // how many pending connections queue will hold
#define PATHLENGTH 256 //default pathlength for pwd-command - may be dynamically increased later
-#define IMGAGE 1 //type image/binary
+#define IMAGE 1 //type image/binary
#define ASCII 2 //type ascii
//struct for command and parameter pointers
typedef struct {
@@ -25,8 +25,8 @@ typedef struct {
// user struct
typedef struct usr {
- int csocket;
- int dsocket;
+ int c_socket;
+ int d_socket;
char d_address[INET6_ADDRSTRLEN];
int d_port;
int passv;
@@ -111,12 +111,12 @@ int ini_user_list(int newfd, usr **user) {
get_ip(newfd, ipstr, INET6_ADDRSTRLEN);
*user = calloc(1, sizeof(struct usr));
- (*user)->csocket = newfd;
- (*user)->dsocket = 0;
+ (*user)->c_socket = newfd;
+ (*user)->d_socket = 0;
strcpy((*user)->d_address, ipstr);
(*user)->d_port = get_port(newfd);
(*user)->passv = 0;
- (*user)->type = 1;
+ (*user)->type = IMAGE;
(*user)->pwd = calloc(1, PATHLENGTH);
(*user)->pwd_len = PATHLENGTH;
(*user)->next = NULL;
@@ -136,24 +136,24 @@ int append_user(int newfd, usr *user) {
//append new usr-struct
user->next = calloc(1, sizeof(struct usr));
user = user->next;
- user->csocket = newfd;
- user->dsocket = 0;
+ user->c_socket = newfd;
+ user->d_socket = 0;
strcpy(user->d_address, ipstr);
user->d_port = get_port(newfd);
user->passv = 0;
- user->type = 1;
+ user->type = IMAGE;
user->pwd = calloc(1, PATHLENGTH);
user->pwd_len = PATHLENGTH;
user->next = NULL;
return 0;
}
/*
- * find user by csocket number
+ * find user by c_socket number
*/
int find_user(int socket, usr *head, usr **result) {
while (head) {
- if (head->csocket == socket) {
+ if (head->c_socket == socket) {
*result = head;
return 0;
}
@@ -164,17 +164,17 @@ int find_user(int socket, usr *head, usr **result) {
}
/*
- * find ancestor of user with csocket number socket
+ * find ancestor of user with c_socket number socket
*/
int find_pre_user(int socket, usr *head, usr **result) {
usr *pre;
-
- if (head->csocket == socket) {
+
+ if (head->c_socket == socket) {
//only one user in user-list
return 1;
}
while (head) {
- if (head->csocket == socket) {
+ if (head->c_socket == socket) {
*result = pre;
return 0;
}
@@ -307,22 +307,22 @@ int send_message(int socket, char *message) {
*/
int user_command(instruc *instruction, usr *user) {
if (!instruction->params) {
- send_message(user->csocket, "501 Syntax error in parameters or arguments.");
+ send_message(user->c_socket, "501 Syntax error in parameters or arguments.");
return 1;
}
//remove redundant whitespaces from params
instruction->params = trim(instruction->params);
//only one parameter allowed
if (strchr(instruction->params, ' ')) {
- send_message(user->csocket, "501 Syntax error in parameters or arguments.");
+ send_message(user->c_socket, "501 Syntax error in parameters or arguments.");
return 1;
}
-
+
if (!(strcmp(instruction->params, "anonymous"))) {
- send_message(user->csocket, "230 User logged in.");
+ send_message(user->c_socket, "230 User logged in.");
} else {
- send_message(user->csocket, "530 Not logged in.");
+ send_message(user->c_socket, "530 Not logged in.");
}
return 0;
}
@@ -339,7 +339,7 @@ int port_command(instruc *instruction, usr *user) {
memset(ip, '\0', sizeof ip);
param = instruction->params;
if (!param) {
- send_message(user->csocket, "501 Syntax error in parameters or arguments.");
+ send_message(user->c_socket, "501 Syntax error in parameters or arguments.");
return 1;
}
//remove redundant whitespaces from params
@@ -358,15 +358,15 @@ int port_command(instruc *instruction, usr *user) {
} else if (i == 5) {
port += atoi(ptr);
} else {
- send_message(user->csocket, "501 Syntax error in parameters or arguments.");
+ send_message(user->c_socket, "501 Syntax error in parameters or arguments.");
return 1;
}
i++;
ptr = strtok(NULL, ",");
}
strcpy(user->d_address, ip);
user->d_port = port;
- send_message(user->csocket, "200 PORT command successful.");
+ send_message(user->c_socket, "200 PORT command successful.");
return 0;
}
@@ -377,42 +377,96 @@ int set_user_type(instruc *instruction, usr *user) {
int i = 0;
if (!instruction->params) {
- send_message(user->csocket, "501 Syntax error in parameters or arguments.");
+ send_message(user->c_socket, "501 Syntax error in parameters or arguments.");
return 1;
}
//demand one parameter - make its chars upper-case
while(instruction->params[i] != '\0') {
i++;
}
if (upcase(instruction->params, i)) {
- send_message(user->csocket, "501 Syntax error in parameters or arguments.");
+ send_message(user->c_socket, "501 Syntax error in parameters or arguments.");
return 1;
}
if (((i==1) && instruction->params[0] == 'A') || !strcmp(instruction->params, "ASCII")) {
//ascii type demanded
user->type = 2;
- send_message(user->csocket, "200 Type set to A");
+ send_message(user->c_socket, "200 Type set to A");
return 0;
} else if (((i==1) && (instruction->params[0] == 'I' || instruction->params[0] == 'B')) \
|| !strcmp(instruction->params, "IMAGE") || !strcmp(instruction->params, "BINARY")) {
//binary type demanded
user->type = 1;
- send_message(user->csocket, "200 Type set to I");
+ send_message(user->c_socket, "200 Type set to I");
return 0;
}
- send_message(user->csocket, "501 Syntax error in parameters or arguments.");
+ send_message(user->c_socket, "501 Syntax error in parameters or arguments.");
return 1;
}
/*
+ * send content of pwd
+ * TODO: check for null params
+ */
+int list_command(instruc *instruction, usr *user) {
+ int sock_fd;
+ struct addrinfo hints, *servinfo, *p;
+ char port[5];
+ int yes=1, rv;
+
+ if (user->d_socket != 0) {
+ send_message(user->c_socket, "125 Data connection already open; transfer starting.");
+ } else {
+ memset(&hints, 0, sizeof hints);
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+ sprintf(port, "%d", user->d_port);
+
+ if ((rv = getaddrinfo(user->d_address, port, &hints, &servinfo)) != 0) {
+ send_message(user->c_socket, "425 Unable to build data connection: Invalid arguments.");
+ return 1;
+ }
+
+ // loop through all the results
+ for (p = servinfo; p != NULL; p = p->ai_next) {
+ if ((sock_fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1){
+ perror("server: socket");
+ continue;
+ }
+
+ if (connect(sock_fd, p->ai_addr, p->ai_addrlen) == -1) {
+ close(sock_fd);
+ perror("connect");
+ continue;
+ }
+
+ //got one
+ user->d_socket = sock_fd;
+ send_message(user->c_socket, "150 Opening ASCII mode data connection for file list");
+ break;
+ }
+
+ if (user->d_socket == 0) {
+ send_message(user->c_socket, "425 Can't open data connection.");
+ return 1;
+ }
+ }
+
+ send_message(user->d_socket, "total 0\ndrwx------+ 24 Grobie staff 816 10 Jun 00:46 Desktop");
+
+ send_message(user->c_socket, "226 Transfer complete.");
+ return 0;
+}
+
+/*
* quits the connection with client
*/
int quit_command(instruc *instruction, usr *user, fd_set *master, usr **head) {
- send_message(user->csocket, "221 Goodbye.");
- close_connection(user->csocket, master, head);
+ send_message(user->c_socket, "221 Goodbye.");
+ close_connection(user->c_socket, master, head);
return 0;
}
@@ -474,11 +528,11 @@ int log_action(usr *user, char *message) {
char ip[INET6_ADDRSTRLEN];
curtime = time(NULL);
- if (get_ip(user->csocket, ip, sizeof ip)) {
+ if (get_ip(user->c_socket, ip, sizeof ip)) {
return 1;
}
strftime(log_time, 256, "%H:%M", localtime(&curtime));
- printf("*LOG* %s - %s[%d] - %s\n", log_time, ip, get_port(user->csocket), message);
+ printf("*LOG* %s - %s[%d] - %s\n", log_time, ip, get_port(user->c_socket), message);
return 0;
}
@@ -492,7 +546,7 @@ int dispatch(usr *user, char *received, int received_len, fd_set *master, usr **
instruc instruction;
if (parse_command(received, received_len, &instruction)) {
- send_message(user->csocket, "500 Syntax Error");
+ send_message(user->c_socket, "500 Syntax Error");
return 1;
}
@@ -508,10 +562,14 @@ int dispatch(usr *user, char *received, int received_len, fd_set *master, usr **
if (set_user_type(&instruction, user)) {
return 1;
}
+ } else if (!strcmp(instruction.command, "LIST")) {
+ if (list_command(&instruction, user)) {
+ return 1;
+ }
} else if (!strcmp(instruction.command, "QUIT")) {
quit_command(&instruction, user, master, head);
} else {
- send_message(user->csocket, "502 Command not implemented.");
+ send_message(user->c_socket, "502 Command not implemented.");
}
return 0;
}

0 comments on commit 8fb3a27

Please sign in to comment.