Permalink
Browse files

USER implementiert (bisher nur duoi zugelassen). Bugfix bei parse_com…

…mand (Pointer zeigt nicht mehr auf ungültigen Speicher)
  • Loading branch information...
1 parent 807461f commit 5e3d7145e5b9466643811a680fed0d72ae2acf4d Robert A committed Jun 7, 2009
Showing with 46 additions and 22 deletions.
  1. +46 −22 duoit.c
View
@@ -128,6 +128,9 @@ int append_user(int newfd, usr *user){
return 0;
}
+/*
+ * find user by socket number
+ */
int find_user(int socket, usr *head, usr **result) {
while (head) {
if (head->csocket == socket) {
@@ -177,9 +180,7 @@ void handle_new_connection(int *listener, fd_set *master, int *fdmax, usr **user
/*
- * close connection
- *
- * closes connection and removes it from the file discriptors list
+ * close connection and remove it from the file discriptors list
*/
void close_connection(int socket, fd_set *master) {
close(socket);
@@ -216,6 +217,22 @@ int send_message(int socket, char *message) {
}
/*
+ * authenticate user
+ */
+int user_command(instruc *instruction, usr *user) {
+ if (!instruction->params) {
+ send_message(user->csocket, "501 Syntax error in parameters or arguments.");
+ return 1;
+ }
+ if (!(strcmp(instruction->params, "duoit"))) {
+ send_message(user->csocket, "230 User logged in.");
+ } else {
+ send_message(user->csocket, "530 Not logged in.");
+ }
+ return 0;
+}
+
+/*
* quits the connection with client
* TODO: usr struct freigeben
*/
@@ -290,7 +307,11 @@ int dispatch(usr *user, char *received, int received_len, fd_set *master) {
return 1;
}
- if (!strcmp(instruction.command, "QUIT")) {
+ if (!strcmp(instruction.command, "USER")) {
+ if (user_command(&instruction, user)) {
+ return 1;
+ }
+ } else if (!strcmp(instruction.command, "QUIT")) {
quit_command(&instruction, user, master);
} else {
send_message(user->csocket, "502 Command not implemented.");
@@ -299,15 +320,13 @@ int dispatch(usr *user, char *received, int received_len, fd_set *master) {
}
int parse_command(char *received, int received_len, instruc *instruction) {
- char *parameter;
- char command[5];
+ char *parameter = NULL;
int input_length;
int space_pos;
received[received_len] = '\0';
- printf("got message: %s", received);
+ printf("got message: %s\n", received);
- memset(command, '\0', sizeof command);
if (received[received_len - 2] == '\r' && received[received_len - 1] == '\n'){
received[received_len -2] = '\0';
input_length = received_len - 2;
@@ -324,23 +343,24 @@ int parse_command(char *received, int received_len, instruc *instruction) {
if (input_length > 4) {
return 1;
}
- strncpy(command, received, input_length);
+ if (format_command(received, input_length)) {
+ return 1;
+ }
} else {
space_pos = parameter - received;
if (space_pos > 4) {
return 1;
}
- strncpy(command, received, space_pos);
+ received[space_pos] = '\0'; //null terminate command
parameter++; //parameters begin after <SP>
+ if (format_command(received, space_pos)) {
+ return 1;
+ }
}
- if (format_command(command, strlen(command))) {
- return 1;
- }
-
- printf("command: %s\n", command);
+ printf("command: %s\n", received);
- instruction->command = command;
+ instruction->command = received;
instruction->params = parameter;
return 0;
@@ -358,8 +378,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=NULL;
- usr *current = NULL;
+ usr *head=NULL;
+ usr *current_user = NULL;
int i, nbytes;
char buf[256];
@@ -386,7 +406,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, &head);
} else {
if ((nbytes = recv(i, buf, sizeof buf, 0)) <= 0) {
@@ -399,11 +419,15 @@ int main(int argc, char *argv[]) {
}
close_connection(i, &master);
} else {
- find_user(i, user, &current);
- if (log_action(current, "Zauberei!")) {
+ // client sends message
+ if (find_user(i, head, &current_user)) {
+ printf("*ERR* corresponding user to socket %d not found!\n", i);
+ continue;
+ }
+ if (log_action(current_user, "Zauberei!")) {
printf("*ERR* logging error\n");
}
- dispatch(current, buf, nbytes, &master);
+ dispatch(current_user, buf, nbytes, &master);
}
}

0 comments on commit 5e3d714

Please sign in to comment.