diff options
author | Preston Pan <ret2pop@gmail.com> | 2024-12-28 16:47:43 -0800 |
---|---|---|
committer | Preston Pan <ret2pop@gmail.com> | 2024-12-28 16:47:43 -0800 |
commit | 1fd608288ee47c2c560817f12f14b21069fed2f6 (patch) | |
tree | e6460b92dba5bb0d089c8c2a4e794e3504098359 /server | |
parent | 63f11aaec8d21844a07fd27003a992c102a3a297 (diff) |
makefile and directory structure change completely to build client and server
Diffstat (limited to 'server')
-rw-r--r-- | server/array.c | 37 | ||||
-rw-r--r-- | server/better_string.c | 51 | ||||
-rw-r--r-- | server/hash_table.c | 11 | ||||
-rw-r--r-- | server/helpers.c | 34 | ||||
-rw-r--r-- | server/main.c | 215 | ||||
-rw-r--r-- | server/opcodes.c | 20 | ||||
-rw-r--r-- | server/tsv.c | 32 |
7 files changed, 0 insertions, 400 deletions
diff --git a/server/array.c b/server/array.c deleted file mode 100644 index 84ed938..0000000 --- a/server/array.c +++ /dev/null @@ -1,37 +0,0 @@ -#include <array.h> -#include <helpers.h> -#include <stdlib.h> - -array_t *init_array() { - array_t *a = safe_calloc(1, sizeof(array_t)); - a->capacity = DEFAULT_ARR_LEN; - a->size = 0; - a->items = safe_calloc(a->capacity, sizeof(void *)); - return a; -} - -void array_push(array_t *a, void *item) { - if (a->size >= a->capacity - 2) { - a->capacity *= 2; - a->items = realloc(a->items, a->capacity); - } - a->items[a->size] = item; - a->size++; -} - -void *array_pop(array_t *a) { - if (a->size <= 0) - return NULL; - void *retval = a->items[a->size]; - a->size--; - return retval; -} - -void array_free(void *x, void (*freefunc)(void *)) { - array_t *a = (array_t *)x; - for (int i = 0; i < a->size; i++) { - freefunc(a->items[i]); - } - free(a->items); - free(a); -} diff --git a/server/better_string.c b/server/better_string.c deleted file mode 100644 index 4413f77..0000000 --- a/server/better_string.c +++ /dev/null @@ -1,51 +0,0 @@ -#include <better_string.h> -#include <helpers.h> -#include <stdlib.h> -#include <string.h> - -string_t *init_string(const char *src) { - string_t *s = safe_calloc(1, sizeof(string_t)); - size_t len = src ? strlen(src) : DEFAULT_STR_SIZE; - size_t size = len * 2; - s->buf = safe_calloc(size, sizeof(char)); - s->buf[0] = '\0'; - - if (src) - strcpy(s->buf, src); - s->len = len; - s->size = size; - return s; -} - -void string_push(string_t *s, char c) { - if (s->len >= s->size - 2) { - s->size *= 2; - s->buf = safe_realloc(s->buf, s->size); - } - s->buf[s->len] = c; - s->len++; -} - -char string_pop(string_t *s) { - char c = s->buf[s->len]; - s->len--; - return c; -} - -void string_concat_const(string_t *s1, const char *s2) { - for (int i = 0; i < strlen(s2); i++) { - string_push(s1, s2[i]); - } -} - -void string_concat(string_t *s1, string_t *s2) { - for (int i = 0; i < s2->len; i++) { - string_push(s1, s2->buf[i]); - } -} - -void string_free(void *x) { - string_t *s = x; - free(s->buf); - free(s); -} diff --git a/server/hash_table.c b/server/hash_table.c deleted file mode 100644 index cced194..0000000 --- a/server/hash_table.c +++ /dev/null @@ -1,11 +0,0 @@ -#include <hash_table.h> -#include <stdlib.h> -#include <helpers.h> - -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/server/helpers.c b/server/helpers.c deleted file mode 100644 index 8485dc6..0000000 --- a/server/helpers.c +++ /dev/null @@ -1,34 +0,0 @@ -#include <helpers.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -void die_lz(int code, const char *msg) { - if (code < 0) { - die(msg); - } -} - -void die(const char *msg) { - fprintf(stderr, "panic: "); - perror(msg); - exit(EXIT_FAILURE); -} - -void *safe_calloc(unsigned int i, size_t size) { - void *x = calloc(i, size); - if (x == NULL) { - die("abort: calloc()"); - } - return x; -} - -void *safe_realloc(void *x, size_t size) { - void *p = realloc(x, size); - if (x == NULL) { - die("abort: realloc()"); - } - return p; -} - -void alloc_zero(void *ptr, size_t size) { memset(ptr, 0, size); } diff --git a/server/main.c b/server/main.c deleted file mode 100644 index 7a17966..0000000 --- a/server/main.c +++ /dev/null @@ -1,215 +0,0 @@ -#include <errno.h> -#include <fcntl.h> -#include <netinet/in.h> -#include <signal.h> -#include <stdbool.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <sys/ioctl.h> -#include <sys/poll.h> -#include <sys/socket.h> -#include <sys/time.h> -#include <unistd.h> - -#include <array.h> -#include <better_string.h> -#include <hash_table.h> -#include <helpers.h> -#include <opcodes.h> -#include <protocol.h> - -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) { - close(fds[i].fd); - } - } - - exit(0); -} - -/* modifying function: modifies `in` to point to new location after opcode */ -int parse_op(char *in) { - char buf[MAX_OP_LEN]; - buf[0] = '\0'; - - char cur; - cur = in[0]; - - bool found = false; - for (int i = 0; i < 11; i++) { - if (cur == '\t') { - found = true; - in++; - break; - } else if (cur == '\0') { - break; - } - - buf[i] = in[0]; - in++; - cur = in[0]; - } - - return found ? encode_client_opcode(buf) : CO_UNRECOGNIZED; -} - -array_t *parse_args(char *buf) { return NULL; } - -void set_non_blocking(int sock) { - int flags = fcntl(sock, F_GETFL, 0); - int code = fcntl(sock, F_SETFL, flags | O_NONBLOCK); - die_lz(code, "fcntl()"); -} - -int main(int argc, char **argv) { - char buffer[MAX_BUFSIZE]; - char res_buffer[MAX_BUFSIZE]; - struct pollfd *local_fds1 = fds; - struct pollfd *local_fds2 = fds + MAX_CONNECTIONS; - struct pollfd *local_fds = local_fds1; - - /* We start by initializing our net-related structs */ - signal(SIGINT, handle_sigint); - - int listen_sd = socket(AF_INET6, SOCK_STREAM, 0); - int optval = 1; - die_lz(listen_sd, "socket()"); - - int reuse = setsockopt(listen_sd, SOL_SOCKET, SO_REUSEADDR, (char *)&optval, - sizeof(optval)); - die_lz(reuse, "setsockopt()"); - - int nonblock = ioctl(listen_sd, FIONBIO, (char *)&optval); - die_lz(nonblock, "ioctl()"); - - set_non_blocking(listen_sd); - - /* Make new address */ - struct sockaddr_in6 addr; - alloc_zero(&addr, sizeof(addr)); - - addr.sin6_family = AF_INET6; - memcpy(&addr.sin6_addr, &in6addr_any, sizeof(in6addr_any)); - addr.sin6_port = htons(DEFAULT_PORT); - - int sock_bind = bind(listen_sd, (struct sockaddr *)&addr, sizeof(addr)); - die_lz(sock_bind, "bind()"); - - int sock_listen = listen(listen_sd, 32); - die_lz(sock_listen, "listen()"); - - /* - The first fd entry is the listening socket for new connections; - all the rest are sockets we read from and write to. Whenever a client - connects, we know from the listening socket. - */ - - alloc_zero(fds, sizeof(fds)); - fds[0].fd = listen_sd; - fds[0].events = POLLIN; - - int timeout = 6000; - int sock_poll; - bool end_server = false; - - /* mainloop */ - do { - bool compress_array = false; - /* - if an fd loses connection, we want to remove all -1's from the fds array - */ - - sock_poll = poll(local_fds, nfds, timeout); - die_lz(sock_poll, "poll()"); - - for (int i = 0; i < nfds; i++) { - if (local_fds[i].revents == 0) - continue; - - if (local_fds[i].revents != POLLIN) { - end_server = true; - break; - } - - if (local_fds[i].fd == listen_sd) { - printf("socket is readable\n"); - int new_sd; - - do { - new_sd = accept(listen_sd, NULL, NULL); - - if (new_sd < 0 && errno != EWOULDBLOCK) { - perror("accept() failed"); - end_server = true; - break; - } - - local_fds[nfds].fd = new_sd; - local_fds[nfds].events = POLLIN; - nfds++; - } while (new_sd >= 0); - } else { - bool close_conn = false; - int fd_recv; - int fd_send; - fd_recv = recv(local_fds[i].fd, buffer, sizeof(buffer), 0); - - if (fd_recv < 0 && errno != EWOULDBLOCK) { - perror("recv() failed"); - close_conn = true; - } else if (fd_recv == 0) { - printf("Connection closed\n"); - close_conn = true; - } else { - /* echo server -- replace this with buffer parsing */ - /* TODO: reply to client based on user input */ - fd_send = send(local_fds[i].fd, buffer, fd_recv, 0); - if (fd_send < 0) { - perror("send()"); - close_conn = true; - } - } - - if (close_conn) { - close(local_fds[i].fd); - local_fds[i].fd = 0; - compress_array = true; - } - } - } - - if (compress_array) { - printf("switching...\n"); - int cur_nfds = nfds; - nfds = 0; - for (int i = 0; i < cur_nfds; i++) { - if (local_fds[i].fd != 0) { - local_fds2[nfds] = local_fds[i]; - nfds ++; - } - } - - local_fds1 = local_fds2; - local_fds2 = local_fds; - local_fds = local_fds1; - alloc_zero(local_fds2, MAX_CONNECTIONS); - } - } while (!end_server); - - for (int i = 0; i < nfds; i++) { - if (fds[i].fd >= 0) { - close(fds[i].fd); - } - } - - return 0; -} diff --git a/server/opcodes.c b/server/opcodes.c deleted file mode 100644 index 8fc51f2..0000000 --- a/server/opcodes.c +++ /dev/null @@ -1,20 +0,0 @@ -#include <stdlib.h> -#include <opcodes.h> -#include <helpers.h> - - -char *decode_client_opcode(int op) { - return "implement me"; -} - -char *decode_server_opcode(int op) { - return "implement me"; -} - -int encode_client_opcode(char *c) { - return -1; -} - -int encode_server_opcode(char *c) { - return -1; -} diff --git a/server/tsv.c b/server/tsv.c deleted file mode 100644 index 9b1c2be..0000000 --- a/server/tsv.c +++ /dev/null @@ -1,32 +0,0 @@ -#include <stdlib.h> -#include <stdbool.h> - -#include <tsv.h> -#include <helpers.h> -#include <better_string.h> - -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; -} |