Move client and thread struct to header file and use debug flag instead of debug in config.h
This commit is contained in:
parent
c57ba8d2ec
commit
6b43464637
5 changed files with 45 additions and 38 deletions
|
@ -1,6 +1,4 @@
|
|||
/* Server */
|
||||
#define DEBUG 0
|
||||
|
||||
#define PORT 20247
|
||||
#define MAX_NAME 32 /* Max username length */
|
||||
#define MAX_DATA_LENGTH 8192
|
||||
|
|
|
@ -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
|
||||
|
|
35
lib/packet.c
35
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;
|
||||
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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,12 +140,17 @@ 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);
|
||||
signal(SIGINT, signal_handler);
|
||||
|
|
Loading…
Reference in a new issue