Compare commits

..

3 Commits

3 changed files with 40 additions and 12 deletions

14
README.md Normal file
View File

@@ -0,0 +1,14 @@
## Chat server
This is a simple, barely-working chat server (which uses a custom protocol). It is written in C, and relies on BSD sockets.
##Usage
To use it, compile the program with `make`.
TODO:
- Finish README
- Implement password authentication
- Fix bugs with message string manipulation (the actual message isn't being stored correctly, and contains extra characters).
- Do additional testing to ensure that everything works correctly (it probably doesn't).

10
main.c
View File

@@ -16,6 +16,7 @@
#define BUFFER_SIZE 10000 #define BUFFER_SIZE 10000
#define MAX_CONNECTIONS 100 #define MAX_CONNECTIONS 100
#define DATA_SIZE 50000 #define DATA_SIZE 50000
#define MESSAGE_SIZE 50000
User** create_user_list(char* filename); User** create_user_list(char* filename);
void sigint_handler(int dummy); void sigint_handler(int dummy);
@@ -80,6 +81,9 @@ int main() {
} }
for (int i=1; i < MAX_CONNECTIONS; i++) { for (int i=1; i < MAX_CONNECTIONS; i++) {
memset(data,0x00,DATA_SIZE);
if (FD_ISSET(conn_sockets[i],&read_fd_set)) { if (FD_ISSET(conn_sockets[i],&read_fd_set)) {
while ( strstr(buffer,"END_OF_DATA") == NULL ) { while ( strstr(buffer,"END_OF_DATA") == NULL ) {
@@ -125,8 +129,10 @@ int main() {
printf("Message intended for %s\n",to_user[i]->username); printf("Message intended for %s\n",to_user[i]->username);
} }
} }
char* message_string = fetch_message_string(data); char* message_string = malloc(sizeof(char) * MESSAGE_SIZE);
memset(message_string, 0x00, MESSAGE_SIZE);
message_string = fetch_message_string(data);
if (message_string == NULL) { if (message_string == NULL) {
printf("Invalid message.\n"); printf("Invalid message.\n");
return -10; return -10;

View File

@@ -25,26 +25,34 @@ char* fetch_from_string(char* message, char* indicator) {
} }
char* fetch_message_string(char* message) { char* fetch_message_string(char* message) {
int num_of_terminators;
char* start = strstr(message,"START_OF_MESSAGE"); char* message_copy = malloc(strlen(message));
strcpy(message_copy,message);
int num_of_terminators = 0;
int start_index = 0;
int end_index = 0;
int message_length = 0;
char* start = strstr(message_copy,"START_OF_MESSAGE");
if (start == NULL) { if (start == NULL) {
return NULL; return NULL;
} }
int start_index = start - message; start_index = start - message_copy;
start_index += strlen("START_OF_MESSAGE"); start_index += strlen("START_OF_MESSAGE");
while (*(message + start_index) == '\n' || *(message + start_index) == '\r') { while (*(message_copy + start_index) == '\n' || *(message_copy + start_index) == '\r') {
start_index++; start_index++;
} }
char* end = strstr(message,"END_OF_MESSAGE"); char* end = strstr(message_copy,"END_OF_MESSAGE");
if (end == NULL) { if (end == NULL) {
return NULL; return NULL;
} }
int end_index = end - message; end_index = end - message_copy;
if (*(message + end_index-2) = '\r') { if (*(message_copy + end_index-2) == '\r') {
num_of_terminators = 2; num_of_terminators = 2;
} else { } else {
num_of_terminators = 1; num_of_terminators = 1;
@@ -52,15 +60,15 @@ char* fetch_message_string(char* message) {
end_index -= num_of_terminators; end_index -= num_of_terminators;
int message_length = end_index - start_index; message_length = end_index - start_index;
char* message_string = malloc(message_length + 2); char* message_string = malloc(message_length + 2);
printf("Message length is %d\n",message_length); printf("Message goes from %d to %d\n",start_index,end_index);
for (int i=0; i<message_length; i++) { /* The reason the upper-bound is message_length-1 is because the last for (int i=0; i<message_length; i++) { /* The reason the upper-bound is message_length-1 is because the last
character is a new-line, which the user would not have typed. */ character is a new-line, which the user would not have typed. */
*(message_string + i) = *(message + start_index + i); *(message_string + i) = *(message_copy + start_index + i);
printf("%c",*(message_string + i)); printf("%c",*(message_string + i));
} }