Compare commits
2 Commits
ace4e3c8e5
...
d46d250b84
Author | SHA1 | Date | |
---|---|---|---|
d46d250b84 | |||
70ecbae964 |
77
main.c
77
main.c
@@ -73,59 +73,50 @@ int main() {
|
||||
|
||||
for (int i=1; i < MAX_CONNECTIONS; i++) {
|
||||
if (FD_ISSET(conn_sockets[i],&read_fd_set)) {
|
||||
do {
|
||||
|
||||
while ( strcmp(buffer,"END_OF_DATA\r\n") != 0 ) {
|
||||
int num_bytes_read = recv(conn_sockets[i],buffer,sizeof(buffer),0);
|
||||
if (num_bytes_read <= 0) {
|
||||
close(conn_sockets[i]);
|
||||
conn_sockets[i] = 0;
|
||||
to_user[i] = NULL;
|
||||
GOTO continue_for_loop;
|
||||
}
|
||||
strcat(data, buffer);
|
||||
}
|
||||
|
||||
strcat(data,"\0");
|
||||
|
||||
if (num_bytes_read <= 0) {
|
||||
while ( strstr(buffer,"END_OF_DATA") == NULL ) {
|
||||
int num_bytes_read = recv(conn_sockets[i],buffer,sizeof(buffer),0);
|
||||
if (num_bytes_read <= 0) {
|
||||
close(conn_sockets[i]);
|
||||
conn_sockets[i] = 0;
|
||||
to_user[i] = NULL;
|
||||
break;
|
||||
goto continue_for_loop;
|
||||
}
|
||||
strcat(data, buffer);
|
||||
}
|
||||
strcat(data,"\0");
|
||||
if (from_user[i] == NULL) {
|
||||
from_user[i] = fetch_user(fetch_from_string(data,"IAM"));
|
||||
if (from_user[i] == NULL) {
|
||||
printf("Please identify yourself.\n");
|
||||
close(conn_sockets[i]);
|
||||
continue;
|
||||
} else {
|
||||
|
||||
if (from_user[i] == NULL) {
|
||||
from_user[i] = fetch_user(fetch_sender_user_string(buffer));
|
||||
if (from_user[i] == NULL) {
|
||||
printf("Please identify yourself.\n");
|
||||
raise(SIGINT);
|
||||
return 150;
|
||||
} else {
|
||||
printf("You are %s\n",from_user[i]->username);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (to_user[i] == NULL) {
|
||||
to_user[i] = fetch_user(fetch_dest_user_string(buffer));
|
||||
if (to_user[i] == NULL) {
|
||||
printf("Invalid message format or User\n");
|
||||
raise(SIGINT);
|
||||
} else {
|
||||
printf("Message intended for %s\n",to_user[i]->username);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
char* message = fetch_message_string(buffer);
|
||||
|
||||
printf("You are %s\n",from_user[i]->username);
|
||||
}
|
||||
}
|
||||
|
||||
} while (strcmp(buffer,"END_OF_DATA\r\n") != 0);
|
||||
if (to_user[i] == NULL) {
|
||||
to_user[i] = fetch_user(fetch_from_string(data,"TO"));
|
||||
if (to_user[i] == NULL) {
|
||||
printf("Invalid user or message format.\n");
|
||||
close(conn_sockets[i]);
|
||||
continue;
|
||||
} else {
|
||||
printf("Message intended for %s\n",to_user[i]->username);
|
||||
}
|
||||
}
|
||||
|
||||
char* message = fetch_message_string(data);
|
||||
|
||||
close(conn_sockets[i]);
|
||||
|
||||
}
|
||||
|
||||
|
||||
continue_for_loop:
|
||||
data[0] = '\0'; /* This effectively clears the string, since the first element is null. */
|
||||
to_user[i] = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -2,36 +2,44 @@
|
||||
|
||||
#include "message_helpers.h"
|
||||
|
||||
char* fetch_dest_user_string(char* message) {
|
||||
char* token = malloc (sizeof(char) * strlen(message));
|
||||
token = strtok(message," \r\n");
|
||||
if (strcmp(token,"TO:") == 0) {
|
||||
token = strtok(NULL," \r\n");
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
char* fetch_from_string(char* message, char* indicator) {
|
||||
|
||||
return token;
|
||||
}
|
||||
char* message_copy = malloc(strlen(message));
|
||||
strcpy(message_copy,message);
|
||||
|
||||
char* fetch_sender_user_string(char* message) {
|
||||
fetch_generic_string("IAM",message);
|
||||
}
|
||||
|
||||
char* fetch_generic_string(char* indicator, char* message) {
|
||||
char* token = malloc (sizeof(char) * strlen(message));
|
||||
char* token = malloc (sizeof(char) * strlen(message_copy));
|
||||
|
||||
char* string_to_search = malloc(strlen(indicator) + 1);
|
||||
strcpy(string_to_search,indicator);
|
||||
strcat(string_to_search,":");
|
||||
|
||||
token = strtok(message," r\n");
|
||||
if (strcmp(token,string_to_search) == 0) {
|
||||
token = strtok(NULL," \r\n");
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
token = strtok(message_copy," r\n");
|
||||
while (strcmp(token,string_to_search) != 0) {
|
||||
token = strtok(NULL," \r\n");
|
||||
if (token == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
token = strtok(NULL," \r\n");
|
||||
return token;
|
||||
|
||||
}
|
||||
|
||||
char* fetch_message_string(char* message) {
|
||||
char* start = strstr(message,"START_OF_MESSAGE");
|
||||
if (start == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
int start_index = start - message;
|
||||
|
||||
char* end = strstr(message,"END_OF_MESSAGE");
|
||||
if (end == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
int end_index = end - message;
|
||||
|
||||
int message_length = end_index - start_index;
|
||||
char* message_string = malloc(message_length + 1);
|
||||
|
||||
for (int i=0;i < message_length; i++) {
|
||||
*(message_string + i) = *(message_
|
||||
}
|
||||
|
@@ -1,21 +1,12 @@
|
||||
|
||||
|
||||
/* If the message contains a string of the form:
|
||||
TO: <Username>
|
||||
then return 'Username' */
|
||||
char* fetch_dest_user_string(char* message);
|
||||
|
||||
/* If the message contains a string of the form:
|
||||
IAM: <Username>
|
||||
then return 'Username' */
|
||||
char* fetch_sender_user_string(char* message);
|
||||
|
||||
/* If the message contains a string of the form:
|
||||
<Indicator>: <Value>
|
||||
Then return 'value' */
|
||||
char* fetch_generic_string(char* indicator, char* message);
|
||||
char* fetch_from_string(char* message, char* indicator);
|
||||
|
||||
/* If the message contains a string of the form:
|
||||
START_OF_MESSAGE
|
||||
<text>
|
||||
END_OF_MESSAGE
|
||||
Then return <text> */
|
||||
char* fetch_message_string(char* message);
|
||||
|
Reference in New Issue
Block a user