diff --git a/include/config.h b/include/config.h index 22599a1..24688a8 100644 --- a/include/config.h +++ b/include/config.h @@ -1,6 +1,4 @@ /* Server */ -#define DEBUG 0 - #define PORT 20247 #define MAX_NAME 32 /* Max username length */ #define MAX_DATA_LENGTH 8192 diff --git a/include/server/server.h b/include/server/server.h index 03cf135..86f5cde 100644 --- a/include/server/server.h +++ b/include/server/server.h @@ -1,8 +1,23 @@ #ifndef SERVER_H_ #define SERVER_H_ +extern int debug; + #define CHALLENGE_SIZE 32 #define MAX_CONNECTION_QUEUE 128 /* for listen() */ #define MAX_EVENTS 64 /* Max events can be returned simulataneouly by epoll */ +typedef struct client_t { + int fd; /* File descriptor for client socket */ + uint8_t *shared_key; + char username[MAX_NAME]; /* Username of client */ +} client_t; + +typedef struct thread_t { + int epoll_fd; /* epoll instance for each thread */ + pthread_t thread; /* POSIX thread */ + int num_clients; /* Number of active clients in thread */ + client_t clients[MAX_CLIENTS_PER_THREAD]; /* Active clients */ +} thread_t; + #endif diff --git a/lib/packet.c b/lib/packet.c index 8aa3be3..4f642ea 100644 --- a/lib/packet.c +++ b/lib/packet.c @@ -1,6 +1,9 @@ #include "packet.h" #include "key.h" #include "util.h" +#include "server/server.h" + +int debug; /* * Requires manually free packet data @@ -30,12 +33,12 @@ int recv_packet(packet_t *pkt, int fd, uint8_t required_type) memcpy(&pkt->type, &header[sizeof(pkt->status)], sizeof(pkt->type)); memcpy(&pkt->length, &header[sizeof(pkt->status) + sizeof(pkt->type)], sizeof(pkt->length)); - #if DEBUG == 1 - printf("==========PACKET RECEIVED========\n"); - printf("Status: %d\n", pkt->status); - printf("Type: %d\n", pkt->type); - printf("Length: %d\n", pkt->length); - #endif + if (debug) { + printf("==========PACKET RECEIVED========\n"); + printf("Status: %d\n", pkt->status); + printf("Type: %d\n", pkt->type); + printf("Length: %d\n", pkt->length); + } /* Validate the packet type and length */ if (pkt->type > 0xFF || pkt->type < 0x0 || pkt->type != required_type) { @@ -78,16 +81,16 @@ int recv_packet(packet_t *pkt, int fd, uint8_t required_type) /* Null terminate data so it can be print */ pkt->data[pkt->length] = '\0'; - #if DEBUG == 1 + if (debug) { printf("Data:\n"); print_bin(pkt->data, pkt->length); printf("Signature:\n"); print_bin(pkt->signature, SIGN_SIZE); - #endif + } + } + if (debug) { + printf("==========END RECEIVING==========\n"); } - #if DEBUG == 1 - printf("==========END RECEIVING==========\n"); - #endif return status; @@ -163,9 +166,9 @@ int send_packet(packet_t *pkt, int fd) } } - #if DEBUG == 1 - printf("==========PACKET SENT============\n"); - printf("Status: %d\n", pkt->status); + if (debug) { + printf("==========PACKET SENT============\n"); + printf("Status: %d\n", pkt->status); printf("Type: %d\n", pkt->type); printf("Length: %d\n", pkt->length); if (pkt->length > 0) { @@ -174,8 +177,8 @@ int send_packet(packet_t *pkt, int fd) printf("Signature:\n"); print_bin(pkt->signature, SIGN_SIZE); } - printf("==========END SENT===============\n"); - #endif + printf("==========END SENT===============\n"); + } return status; diff --git a/src/client/ui.c b/src/client/ui.c index 515b50e..6d54e24 100644 --- a/src/client/ui.c +++ b/src/client/ui.c @@ -261,13 +261,13 @@ void add_message(uint8_t *author, uint8_t *recipient, uint8_t *content, uint32_t void print_message(int flag, message_t *msg, int user_color) { struct tm *timeinfo = localtime(&msg->creation); - char buffer[21]; + char timestr[21]; if (flag) { - strftime(buffer, sizeof(buffer), "%b %d %Y %H:%M:%S", timeinfo); + strftime(timestr, sizeof(timestr), "%b %d %Y %H:%M:%S", timeinfo); } else { - strftime(buffer, sizeof(buffer), "%H:%M:%S", timeinfo); + strftime(timestr, sizeof(timestr), "%H:%M:%S", timeinfo); } - wprintw(chat_content, "%s ", buffer); + wprintw(chat_content, "%s ", timestr); wattron(chat_content, A_BOLD); wattron(chat_content, COLOR_PAIR(user_color)); diff --git a/src/server/server.c b/src/server/server.c index fe77e7d..95d2fd1 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -4,24 +4,10 @@ #include "notification.h" #include "server/server.h" -typedef struct client_t { - int fd; /* File descriptor for client socket */ - uint8_t *shared_key; - char username[MAX_NAME]; /* Username of client */ -} client_t; - -typedef struct thread_t { - int epoll_fd; /* epoll instance for each thread */ - pthread_t thread; /* POSIX thread */ - int num_clients; /* Number of active clients in thread */ - client_t clients[MAX_CLIENTS_PER_THREAD]; /* Active clients */ -} thread_t; - +int debug; thread_t threads[MAX_THREADS]; int num_thread = 0; -void *thread_worker(void *arg); - /* * Authenticate client before starting communication */ @@ -154,11 +140,16 @@ void *thread_worker(void *arg) } } -int main() +int main(int argc, char **argv) { if (sodium_init() < 0) { error(1, "Error initializing libsodium"); } + + if (argc == 2 && strcmp(argv[1], "-d") == 0) { + /* Turns on debug flag */ + debug = 1; + } signal(SIGPIPE, signal_handler); signal(SIGABRT, signal_handler);