Move client and thread struct to header file and use debug flag instead of debug in config.h

This commit is contained in:
Night Kaly 2024-09-20 18:53:00 +01:00
parent c57ba8d2ec
commit 6b43464637
Signed by: night0721
GPG key ID: 957D67B8DB7A119B
5 changed files with 45 additions and 38 deletions

View file

@ -1,6 +1,4 @@
/* Server */ /* Server */
#define DEBUG 0
#define PORT 20247 #define PORT 20247
#define MAX_NAME 32 /* Max username length */ #define MAX_NAME 32 /* Max username length */
#define MAX_DATA_LENGTH 8192 #define MAX_DATA_LENGTH 8192

View file

@ -1,8 +1,23 @@
#ifndef SERVER_H_ #ifndef SERVER_H_
#define SERVER_H_ #define SERVER_H_
extern int debug;
#define CHALLENGE_SIZE 32 #define CHALLENGE_SIZE 32
#define MAX_CONNECTION_QUEUE 128 /* for listen() */ #define MAX_CONNECTION_QUEUE 128 /* for listen() */
#define MAX_EVENTS 64 /* Max events can be returned simulataneouly by epoll */ #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 #endif

View file

@ -1,6 +1,9 @@
#include "packet.h" #include "packet.h"
#include "key.h" #include "key.h"
#include "util.h" #include "util.h"
#include "server/server.h"
int debug;
/* /*
* Requires manually free packet data * 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->type, &header[sizeof(pkt->status)], sizeof(pkt->type));
memcpy(&pkt->length, &header[sizeof(pkt->status) + sizeof(pkt->type)], sizeof(pkt->length)); memcpy(&pkt->length, &header[sizeof(pkt->status) + sizeof(pkt->type)], sizeof(pkt->length));
#if DEBUG == 1 if (debug) {
printf("==========PACKET RECEIVED========\n"); printf("==========PACKET RECEIVED========\n");
printf("Status: %d\n", pkt->status); printf("Status: %d\n", pkt->status);
printf("Type: %d\n", pkt->type); printf("Type: %d\n", pkt->type);
printf("Length: %d\n", pkt->length); printf("Length: %d\n", pkt->length);
#endif }
/* Validate the packet type and length */ /* Validate the packet type and length */
if (pkt->type > 0xFF || pkt->type < 0x0 || pkt->type != required_type) { 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 */ /* Null terminate data so it can be print */
pkt->data[pkt->length] = '\0'; pkt->data[pkt->length] = '\0';
#if DEBUG == 1 if (debug) {
printf("Data:\n"); printf("Data:\n");
print_bin(pkt->data, pkt->length); print_bin(pkt->data, pkt->length);
printf("Signature:\n"); printf("Signature:\n");
print_bin(pkt->signature, SIGN_SIZE); print_bin(pkt->signature, SIGN_SIZE);
#endif
} }
#if DEBUG == 1 }
if (debug) {
printf("==========END RECEIVING==========\n"); printf("==========END RECEIVING==========\n");
#endif }
return status; return status;
@ -163,7 +166,7 @@ int send_packet(packet_t *pkt, int fd)
} }
} }
#if DEBUG == 1 if (debug) {
printf("==========PACKET SENT============\n"); printf("==========PACKET SENT============\n");
printf("Status: %d\n", pkt->status); printf("Status: %d\n", pkt->status);
printf("Type: %d\n", pkt->type); printf("Type: %d\n", pkt->type);
@ -175,7 +178,7 @@ int send_packet(packet_t *pkt, int fd)
print_bin(pkt->signature, SIGN_SIZE); print_bin(pkt->signature, SIGN_SIZE);
} }
printf("==========END SENT===============\n"); printf("==========END SENT===============\n");
#endif }
return status; return status;

View file

@ -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) void print_message(int flag, message_t *msg, int user_color)
{ {
struct tm *timeinfo = localtime(&msg->creation); struct tm *timeinfo = localtime(&msg->creation);
char buffer[21]; char timestr[21];
if (flag) { 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 { } 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, A_BOLD);
wattron(chat_content, COLOR_PAIR(user_color)); wattron(chat_content, COLOR_PAIR(user_color));

View file

@ -4,24 +4,10 @@
#include "notification.h" #include "notification.h"
#include "server/server.h" #include "server/server.h"
typedef struct client_t { int debug;
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;
thread_t threads[MAX_THREADS]; thread_t threads[MAX_THREADS];
int num_thread = 0; int num_thread = 0;
void *thread_worker(void *arg);
/* /*
* Authenticate client before starting communication * Authenticate client before starting communication
*/ */
@ -154,12 +140,17 @@ void *thread_worker(void *arg)
} }
} }
int main() int main(int argc, char **argv)
{ {
if (sodium_init() < 0) { if (sodium_init() < 0) {
error(1, "Error initializing libsodium"); error(1, "Error initializing libsodium");
} }
if (argc == 2 && strcmp(argv[1], "-d") == 0) {
/* Turns on debug flag */
debug = 1;
}
signal(SIGPIPE, signal_handler); signal(SIGPIPE, signal_handler);
signal(SIGABRT, signal_handler); signal(SIGABRT, signal_handler);
signal(SIGINT, signal_handler); signal(SIGINT, signal_handler);