Permalink
Browse files

Userverwaltung in structs: append_user, find_user

  • Loading branch information...
1 parent 2676514 commit a25e709299d42e53bea6f803dc77ab11dc0021f2 @grobie committed Jun 6, 2009
Showing with 44 additions and 22 deletions.
  1. +44 −22 duoit.c
View
66 duoit.c
@@ -17,7 +17,6 @@
//struct for command and parameter pointers
typedef struct {
- int socket;
char *command;
char *params;
} instruc;
@@ -106,11 +105,40 @@ int ini_user_list(int newfd, usr **user) {
(*user)->csocket = newfd;
(*user)->pwd = calloc(1, PATHLENGTH);
(*user)->pwd_len = PATHLENGTH;
+ (*user)->next = NULL;
return 0;
}
-int append_user(){
+/*
+ * append user to user-list
+ */
+int append_user(int newfd, usr *user){
+
+ while (user->next) {
+ user = user->next;
+ }
+ //append new usr-struct
+ user->next = calloc(1, sizeof(struct usr));
+ user = user->next;
+ user->csocket = newfd;
+ user->pwd = calloc(1, PATHLENGTH);
+ user->pwd_len = PATHLENGTH;
+ user->next = NULL;
+ return 0;
}
+
+int find_user(int socket, usr *head, usr **result) {
+ while (head) {
+ if (head->csocket == socket) {
+ *result = head;
+ return 0;
+ }
+ head = head->next;
+ }
+
+ return 1;
+}
+
/*
* Handle new connection
*
@@ -132,21 +160,13 @@ void handle_new_connection(int *listener, fd_set *master, int *fdmax, usr **user
if (newfd > *fdmax) { // keep track of the max
*fdmax = newfd;
}
-
//adapt the user list
if (!(*user)) {
//initialize head of user-list
ini_user_list(newfd, user);
} else {
- while (user->next) {
- user = user->next;
- }
//append user to user-list
- user->next = calloc(1, sizeof(struct usr));
- user = user->next;
- user->csocket = newfd;
- user->pwd = calloc(1, PATHLENGTH);
- user->pwd_len = PATHLENGTH;
+ append_user(newfd, *user);
}
send_message(newfd, "220 Welcome to duoit FTP-Server. Service ready.");
inet_ntop(remoteaddr.ss_family, get_in_addr((struct sockaddr*)&remoteaddr), remoteIP, INET6_ADDRSTRLEN);
@@ -197,10 +217,10 @@ int send_message(int socket, char *message) {
/*
* quits the connection with client
*/
-int quit_command(instruc *instruction, fd_set *master) {
+int quit_command(instruc *instruction, usr *user, fd_set *master) {
- send_message(instruction->socket, "221 Goodbye.");
- close_connection(instruction->socket, master);
+ send_message(user->csocket, "221 Goodbye.");
+ close_connection(user->csocket, master);
return 0;
}
@@ -209,19 +229,18 @@ int quit_command(instruc *instruction, fd_set *master) {
*
* TODO
*/
-int dispatch(int socket, char *received, int received_len, fd_set *master) {
+int dispatch(usr *user, char *received, int received_len, fd_set *master) {
instruc instruction;
- instruction.socket = socket;
if (parse_command(received, received_len, &instruction)) {
- send_message(socket, "500 Syntax Error");
+ send_message(user->csocket, "500 Syntax Error");
return 1;
}
if (!strcmp(instruction.command, "QUIT")) {
- quit_command(&instruction, master);
+ quit_command(&instruction, user, master);
} else {
- send_message(socket, "502 Command not implemented.");
+ send_message(user->csocket, "502 Command not implemented.");
}
return 0;
}
@@ -286,7 +305,8 @@ int main(int argc, char *argv[]) {
fd_set read_fds; // temp file descriptor list for select()
int fdmax; // maximum file descriptor number
int listener; // listening socket descriptor
- usr *user;
+ usr *user=NULL;
+ usr *current = NULL;
int i, nbytes;
char buf[256];
@@ -313,7 +333,7 @@ int main(int argc, char *argv[]) {
for(i = 0; i <= fdmax; i++) {
if (FD_ISSET(i, &read_fds)) {
if (i == listener) {
- handle_new_connection(&listener, &master, &fdmax, user);
+ handle_new_connection(&listener, &master, &fdmax, &user);
} else {
if ((nbytes = recv(i, buf, sizeof buf, 0)) <= 0) {
@@ -326,7 +346,9 @@ int main(int argc, char *argv[]) {
}
close_connection(i, &master);
} else {
- dispatch(i, buf, nbytes, &master);
+ find_user(i, user, &current);
+ printf("current socket: %d\n", current->csocket);
+ dispatch(current, buf, nbytes, &master);
}
}

0 comments on commit a25e709

Please sign in to comment.