Compare commits
2 Commits
3125dd48d6
...
ace4e3c8e5
Author | SHA1 | Date | |
---|---|---|---|
ace4e3c8e5 | |||
3e07370f31 |
83
main.c
83
main.c
@@ -14,6 +14,7 @@
|
|||||||
|
|
||||||
#define BUFFER_SIZE 10000
|
#define BUFFER_SIZE 10000
|
||||||
#define MAX_CONNECTIONS 100
|
#define MAX_CONNECTIONS 100
|
||||||
|
#define DATA_SIZE 50000
|
||||||
|
|
||||||
User** create_user_list(char* filename);
|
User** create_user_list(char* filename);
|
||||||
void sigint_handler(int dummy);
|
void sigint_handler(int dummy);
|
||||||
@@ -22,9 +23,10 @@ User* fetch_user(char* username);
|
|||||||
User** users;
|
User** users;
|
||||||
int num_users;
|
int num_users;
|
||||||
|
|
||||||
bool stop_running = false;
|
bool stop_running;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
stop_running = false;
|
||||||
|
|
||||||
signal(SIGINT,sigint_handler);
|
signal(SIGINT,sigint_handler);
|
||||||
|
|
||||||
@@ -34,8 +36,10 @@ int main() {
|
|||||||
fd_set read_fd_set;
|
fd_set read_fd_set;
|
||||||
int conn_sockets[MAX_CONNECTIONS] = {-1};
|
int conn_sockets[MAX_CONNECTIONS] = {-1};
|
||||||
User* to_user[MAX_CONNECTIONS] = {NULL};
|
User* to_user[MAX_CONNECTIONS] = {NULL};
|
||||||
|
User* from_user[MAX_CONNECTIONS] = {NULL};
|
||||||
FD_ZERO(&read_fd_set);
|
FD_ZERO(&read_fd_set);
|
||||||
char buffer[BUFFER_SIZE];
|
char buffer[BUFFER_SIZE];
|
||||||
|
char data[DATA_SIZE];
|
||||||
|
|
||||||
num_users = num_of_lines("user_file.txt");
|
num_users = num_of_lines("user_file.txt");
|
||||||
users = create_user_list("user_file.txt");
|
users = create_user_list("user_file.txt");
|
||||||
@@ -69,43 +73,70 @@ int main() {
|
|||||||
|
|
||||||
for (int i=1; i < MAX_CONNECTIONS; i++) {
|
for (int i=1; i < MAX_CONNECTIONS; i++) {
|
||||||
if (FD_ISSET(conn_sockets[i],&read_fd_set)) {
|
if (FD_ISSET(conn_sockets[i],&read_fd_set)) {
|
||||||
int num_bytes_read = recv(conn_sockets[i],buffer,sizeof(buffer),0);
|
do {
|
||||||
if (num_bytes_read <= 0) {
|
|
||||||
close(conn_sockets[i]);
|
while ( strcmp(buffer,"END_OF_DATA\r\n") != 0 ) {
|
||||||
conn_sockets[i] = 0;
|
int num_bytes_read = recv(conn_sockets[i],buffer,sizeof(buffer),0);
|
||||||
to_user[i] = NULL;
|
if (num_bytes_read <= 0) {
|
||||||
} else {
|
close(conn_sockets[i]);
|
||||||
if (to_user[i] == NULL) {
|
conn_sockets[i] = 0;
|
||||||
to_user[i] = fetch_user(fetch_dest_user_string(buffer));
|
to_user[i] = NULL;
|
||||||
if (to_user[i] == NULL) {
|
GOTO continue_for_loop;
|
||||||
printf("Invalid message format or User\n");
|
}
|
||||||
exit(241);
|
strcat(data, buffer);
|
||||||
} else {
|
}
|
||||||
printf("Message intended for %s\n",to_user[i]->username);
|
|
||||||
exit(0);
|
strcat(data,"\0");
|
||||||
|
|
||||||
|
if (num_bytes_read <= 0) {
|
||||||
|
close(conn_sockets[i]);
|
||||||
|
conn_sockets[i] = 0;
|
||||||
|
to_user[i] = NULL;
|
||||||
|
break;
|
||||||
|
} 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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} while (strcmp(buffer,"END_OF_DATA\r\n") != 0);
|
||||||
// for (int i=0;i<num_bytes_read;i++) {
|
|
||||||
// printf("%c",buffer[i]);
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
continue_for_loop:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (stop_running) {
|
if (stop_running == true) {
|
||||||
for (int i=0; i< MAX_CONNECTIONS; i++) {
|
printf("Stopping...\n");
|
||||||
|
for (int i=MAX_CONNECTIONS-1; i>= 0; i--) {
|
||||||
|
close(i);
|
||||||
close(conn_sockets[i]);
|
close(conn_sockets[i]);
|
||||||
}
|
}
|
||||||
exit(130);
|
return 130;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -13,3 +13,25 @@ char* fetch_dest_user_string(char* message) {
|
|||||||
|
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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* 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
return token;
|
||||||
|
|
||||||
|
}
|
||||||
|
@@ -4,3 +4,18 @@
|
|||||||
TO: <Username>
|
TO: <Username>
|
||||||
then return 'Username' */
|
then return 'Username' */
|
||||||
char* fetch_dest_user_string(char* message);
|
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);
|
||||||
|
|
||||||
|
/* If the message contains a string of the form:
|
||||||
|
START_OF_MESSAGE
|
||||||
|
<text>
|
||||||
|
END_OF_MESSAGE
|
||||||
|
Reference in New Issue
Block a user