diff --git a/.gitignore b/.gitignore index 8222304..1d00464 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,10 @@ server/build/ server/*.o server/*.out +*.json + +server/.cache/ + # Node dependencies. client/node_modules/ diff --git a/server/src/api.c b/server/src/api.c new file mode 100644 index 0000000..b233f8f --- /dev/null +++ b/server/src/api.c @@ -0,0 +1,28 @@ +#include "include/data.h" + +PacketType packet_type_parse(const char* type_str) { + if (!type_str) return PACKET_TYPE_INVALID; + if (!strcmp(type_str, "join")) return PACKET_TYPE_JOIN; + if (!strcmp(type_str, "JOIN_EV")) return PACKET_TYPE_JOIN_EV; + return PACKET_TYPE_INVALID; +} + +Packet* packet_init(PacketType type, void* data) { + Packet* packet = malloc(sizeof(Packet)); + if (!packet) return NULL; + + packet->type = type; + packet->data = data; + + return packet; +} + +Packet* packet_init_untrusted(PacketType type, void* data) { + Packet* packet = malloc(sizeof(Packet)); + if (!packet) return NULL; + + packet->type = type; + packet->data = data; + + return packet; +} diff --git a/server/src/data.c b/server/src/data.c new file mode 100644 index 0000000..cb5f459 --- /dev/null +++ b/server/src/data.c @@ -0,0 +1,13 @@ +#include "include/data.h" + +int name_verify(const char* name) { + if (!name) return 0; + if (strlen(name) > NAME_MAX_LENGTH) return 0; + return 1; +} + +int msg_verify(const char* content) { + if (!content) return 0; + if (strlen(content) > MSG_MAX_LENGTH) return 0; + return 1; +} diff --git a/server/src/include/api.h b/server/src/include/api.h new file mode 100644 index 0000000..1f4a48f --- /dev/null +++ b/server/src/include/api.h @@ -0,0 +1,41 @@ +#ifndef API__H +#define API__H + +#include "data.h" + +typedef enum { + PACKET_TYPE_JOIN, + PACKET_TYPE_WELCOME, + PACKET_TYPE_JOIN_EV, +} PacketType; + +static const char* packet_type_strings[] = { + [PACKET_TYPE_JOIN]= "join", + [PACKET_TYPE_WELCOME]= "welcome", + [PACKET_TYPE_JOIN_EV]= "join_ev", +}; + +PacketType packet_type_parse(const char* type_str); + +typedef struct { + PacketType type; + void* data; +} Packet; + +Packet* packet_init(PacketType type, void* data); +Packet* packet_init_untrusted(PacketType type, void* data); + +typedef struct { + Name name; +} PacketJoin; + +typedef struct { + UserData* online; + MsgData* history; +} PacketWelcome; + +typedef struct { + UserData newguy; +} PacketJoinEvent; + +#endif diff --git a/server/src/include/data.h b/server/src/include/data.h new file mode 100644 index 0000000..da37e91 --- /dev/null +++ b/server/src/include/data.h @@ -0,0 +1,31 @@ +#ifndef DATA__H +#define DATA__H + +#include + +#define NAME_MAX_LENGTH 16 +#define MSG_MAX_LENGTH 1024 + +typedef int UserID; +typedef int MsgID; + +typedef char Name[NAME_MAX_LENGTH]; +int name_verify(const char* name); + +typedef char MsgContent[MSG_MAX_LENGTH]; +int msg_verify(const char* content); + +typedef struct { + UserID id; + Name name; +} UserData; + +typedef struct { + MsgID id; + UserData author; + MsgID parent; + MsgContent content; + time_t timestamp; +} MsgData; + +#endif