From fd14f6fbf8206589d2a0ad8f0793845cb8faf1d7 Mon Sep 17 00:00:00 2001 From: Preston Pan Date: Thu, 26 Dec 2024 21:18:05 -0800 Subject: stuff --- .gitignore | 1 + Makefile | 3 ++- flake.nix | 6 +++--- include/array.h | 4 ++++ include/better_string.h | 2 +- include/hash_table.h | 35 +++++++++++++++++++++++++++++--- include/helpers.h | 3 +++ include/ht.h | 3 --- include/opcodes.h | 2 -- include/protocol.h | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ include/tsv.h | 16 +++++++++++++++ src/hash_table.c | 11 ++++++++++ src/helpers.c | 6 ++++++ src/main.c | 4 ++++ src/opcodes.c | 6 ------ src/tsv.c | 32 +++++++++++++++++++++++++++++ 16 files changed, 169 insertions(+), 19 deletions(-) delete mode 100644 include/ht.h create mode 100644 include/tsv.h create mode 100644 src/hash_table.c create mode 100644 src/tsv.c diff --git a/.gitignore b/.gitignore index 720c57f..119911e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ bin/** .cache/** result/** compile_commands.json +\#*# diff --git a/Makefile b/Makefile index cafa911..0460512 100644 --- a/Makefile +++ b/Makefile @@ -6,10 +6,11 @@ EXE := $(BIN_DIR)/umami SRC := $(wildcard $(SRC_DIR)/*.c) OBJ := $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o) -CFLAGS := -Wall -Iinclude +CFLAGS := -Wall -Iinclude -lcrypto -lssl LDFLAGS := -Llib LDLIBS := -lm +CFLAGS += $(NIX_CFLAGS_COMPILE) .PHONY: all clean all: $(EXE) diff --git a/flake.nix b/flake.nix index 7d8c456..5072164 100644 --- a/flake.nix +++ b/flake.nix @@ -8,7 +8,7 @@ outputs = { self, nixpkgs }: { packages.x86_64-linux = let pkgs = import nixpkgs { - system = "x86_64-linux"; # Adjust for your system, e.g., aarch64-darwin + system = "x86_64-linux"; }; in { @@ -18,7 +18,7 @@ src = ./.; - buildInputs = with pkgs; [ clang gnumake valgrind bear ]; + buildInputs = with pkgs; [ clang gnumake valgrind bear openssl ]; buildPhase = "make"; @@ -35,7 +35,7 @@ }; in pkgs.mkShell { - buildInputs = with pkgs; [ clang gnumake valgrind bear ]; + buildInputs = with pkgs; [ clang gnumake valgrind bear openssl ]; }; }; } diff --git a/include/array.h b/include/array.h index 48d82db..0b53f95 100644 --- a/include/array.h +++ b/include/array.h @@ -1,7 +1,9 @@ #ifndef ARRAY_H #define ARRAY_H + #include #define DEFAULT_ARR_LEN 10 + typedef struct { void **items; size_t size; @@ -14,5 +16,7 @@ void array_push(array_t *a, void *item); void *array_pop(array_t *a); +void *array_del(array_t *a, unsigned int ind); + void array_free(void *a, void (*freefunc)(void *)); #endif diff --git a/include/better_string.h b/include/better_string.h index b23010e..09fd928 100644 --- a/include/better_string.h +++ b/include/better_string.h @@ -10,7 +10,7 @@ typedef struct { string_t *init_string(const char *source); -void string_append(string_t *s, char c); +void string_push(string_t *s, char c); void string_concat(string_t *s1, string_t *s2); diff --git a/include/hash_table.h b/include/hash_table.h index 16d8fe9..357e38a 100644 --- a/include/hash_table.h +++ b/include/hash_table.h @@ -1,14 +1,43 @@ #ifndef HASH_TABLE_H #define HASH_TABLE_H +#include + +#define DEFAULT_HT_SIZE 500 + +typedef struct PAIR_STRUCT { + char *key; + void *v; +} pair_t; + typedef struct NODE_STRUCT { - + char *key; + void *v; + struct NODE_STRUCT *next; } node_t; typedef struct { - + node_t *head; + node_t *tail; } sll_t; typedef struct { - + sll_t **buckets; + size_t size; } ht_t; + +node_t *init_node(char *key, void *v); + +void sll_push(sll_t *sll, char *key, void *v); + +void sll_delete(sll_t *sll, char *key); + +sll_t *sll_free(void *x, void (*freefunc)(void *)); + +void ht_insert(ht_t *ht, char *key, void *value); + +void ht_delete(ht_t *ht, char *key); + +void *ht_get(ht_t *ht, char *key); + +ht_t *init_ht(size_t size); #endif diff --git a/include/helpers.h b/include/helpers.h index c685e09..276b590 100644 --- a/include/helpers.h +++ b/include/helpers.h @@ -1,7 +1,10 @@ #ifndef HELPERS_H #define HELPERS_H + #include +void die_lz(int code, const char *msg); + void die(const char *msg); void *safe_calloc(unsigned int i, size_t size); diff --git a/include/ht.h b/include/ht.h deleted file mode 100644 index 9172961..0000000 --- a/include/ht.h +++ /dev/null @@ -1,3 +0,0 @@ -#ifndef HT_H -#define HT_H -#endif diff --git a/include/opcodes.h b/include/opcodes.h index 3d5baf8..ff8400f 100644 --- a/include/opcodes.h +++ b/include/opcodes.h @@ -36,8 +36,6 @@ typedef enum { SO_BYE, } sopcode_t; -void die_lz(int code, const char *msg); - char *decode_client_opcode(int op); char *decode_server_opcode(int op); diff --git a/include/protocol.h b/include/protocol.h index 7b5eb07..16ba818 100644 --- a/include/protocol.h +++ b/include/protocol.h @@ -1,6 +1,14 @@ #ifndef PROTOCOL_H #define PROTOCOL_H +#include +#include + +#include +#include +#include +#include + #define MAX_OP_LEN 10 #define MAX_ARG_LEN 50 #define MAX_ARGS 5 @@ -11,4 +19,50 @@ #define USERNAME_SIZE 30 #define KEYLEN 512 +typedef struct { + string_t *nick; + bool is_guest; + int fd; + + /* list of channels where user is in */ + array_t *channels; + array_t *dms; + array_t *autojoin; + unsigned char passhash[SHA256_DIGEST_LENGTH]; +} user_t; + +typedef struct { + string_t *nick; + string_t *msg; + time_t time; +} message_t; + +typedef struct { + array_t *messages; + + user_t *admin; + array_t *mods; + + bool registered_only; + + bool invite_only; + array_t *allowed_users; + + array_t *users; + + string_t *chan_name; + string_t *desc; +} channel_t; + +typedef struct { + user_t *user1; + user_t *user2; + + array_t *messages; +} dm_t; + +string_t *encode_chanstate(ht_t *chans); + +string_t *encode_usersstate(ht_t *u); + #endif diff --git a/include/tsv.h b/include/tsv.h new file mode 100644 index 0000000..5f6a9f6 --- /dev/null +++ b/include/tsv.h @@ -0,0 +1,16 @@ +#ifndef TSV_H +#define TSV_H + +#include + +typedef struct { + char *source; + unsigned int i; + char c; +} tsv_t; + +tsv_t *init_tsv(char *source); + +string_t *next_tsv(tsv_t *tsv); + +#endif diff --git a/src/hash_table.c b/src/hash_table.c new file mode 100644 index 0000000..cced194 --- /dev/null +++ b/src/hash_table.c @@ -0,0 +1,11 @@ +#include +#include +#include + +ht_t *init_ht(size_t size) { + ht_t *ht = safe_calloc(1, sizeof(size)); + size_t realsize = size == 0 ? DEFAULT_HT_SIZE : size; + ht->buckets = safe_calloc(realsize, sizeof(sll_t *)); + ht->size = realsize; + return ht; +} diff --git a/src/helpers.c b/src/helpers.c index ad7228e..8485dc6 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -3,6 +3,12 @@ #include #include +void die_lz(int code, const char *msg) { + if (code < 0) { + die(msg); + } +} + void die(const char *msg) { fprintf(stderr, "panic: "); perror(msg); diff --git a/src/main.c b/src/main.c index 8555da1..7a17966 100644 --- a/src/main.c +++ b/src/main.c @@ -14,6 +14,7 @@ #include #include +#include #include #include #include @@ -22,6 +23,9 @@ int PORT = DEFAULT_PORT; int nfds = 1; struct pollfd fds[MAX_CONNECTIONS * 2]; +ht_t *USERS; +ht_t *CHAN; + void handle_sigint(int sig) { for (int i = 0; i < nfds; i++) { if (fds[i].fd >= 0) { diff --git a/src/opcodes.c b/src/opcodes.c index 66742db..8fc51f2 100644 --- a/src/opcodes.c +++ b/src/opcodes.c @@ -3,12 +3,6 @@ #include -void die_lz(int code, const char *msg) { - if (code < 0) { - die(msg); - } -} - char *decode_client_opcode(int op) { return "implement me"; } diff --git a/src/tsv.c b/src/tsv.c new file mode 100644 index 0000000..9b1c2be --- /dev/null +++ b/src/tsv.c @@ -0,0 +1,32 @@ +#include +#include + +#include +#include +#include + +tsv_t *init_tsv(char *source) { + tsv_t *tsv = safe_calloc(1, sizeof(tsv_t *)); + tsv->source = source; + tsv->i = 0; + tsv->c = tsv->source[tsv->i]; + return tsv; +} + +void tsv_move(tsv_t *tsv) { + if (tsv->c != '\0') { + tsv->i++; + tsv->c = tsv->source[tsv->i]; + } +} + +string_t *tsv_next(tsv_t *tsv) { + string_t *s = init_string(NULL); + bool escape = false; + while (tsv->c != '\t' && !escape) { + if (tsv->c == '\0') break; + string_push(s, tsv->c); + tsv_move(tsv); + } + return s; +} -- cgit