From 32d75fadbf193218d0be42ca91b7688f1854f6e4 Mon Sep 17 00:00:00 2001 From: Test_User Date: Sat, 6 May 2023 03:03:58 -0400 Subject: Start adding client support --- main.c | 174 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 172 insertions(+), 2 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index e95d750..76b22e6 100644 --- a/main.c +++ b/main.c @@ -30,6 +30,7 @@ #include #include #include +#include #include "network.h" #include "config.h" @@ -37,12 +38,181 @@ #include "tls.h" #include "types.h" +void *client_loop(void *ign) { + while (1) { + struct string full_msg = {.data = malloc(0), .len = 0}; + WRITES(1, STRING("Yay\n")); + client_fd = accept(client_listen_fd, NULL, NULL); + listen(client_listen_fd, 0); + client_connected = 0; + client_nick.data = malloc(0); + while (1) { + char data[512]; + uint64_t new_len = read(client_fd, data, 512); + + if (new_len == 0) { + goto disconnect_client; + } + + uint8_t found = 0; + uint64_t msg_len; + for (uint64_t i = 0; i < new_len; i++) { + if (data[i] == '\n') { + found = 1; + msg_len = i + full_msg.len; + break; + } + } + + void *tmp = realloc(full_msg.data, full_msg.len+new_len); + if (tmp == 0 && full_msg.len+new_len != 0) { + WRITES(2, STRING("OOM... disconnect client.\n")); + goto disconnect_client; + } + full_msg.data = tmp; + + memcpy(full_msg.data+full_msg.len, data, new_len); + + full_msg.len += new_len; + + if (!found) + continue; + + while (1) { + WRITES(1, STRING("Recvd: ")); + write(1, full_msg.data, msg_len+1); // +1: \n + if (full_msg.data[msg_len - 1] == '\r') + msg_len--; + + uint64_t offset = 0; + while (offset < msg_len && full_msg.data[offset] == ' ') + offset++; + + if (offset == msg_len) { + puts("Protocol violation: No command."); + goto disconnect_client; + } + + struct string command; + command.data = full_msg.data+offset; + found = 0; + for (uint64_t i = offset; i < msg_len; i++) { + if (full_msg.data[i] == ' ') { + found = 1; + command.len = i - offset; + offset = i; + break; + } + } + if (!found) { + command.len = msg_len - offset; + offset = msg_len; + } + + while (offset < msg_len && full_msg.data[offset] == ' ') + offset++; + + uint64_t argc = 0; + uint64_t old_offset = offset; + if (offset < msg_len) { + while (offset < msg_len) { + if (full_msg.data[offset] == ':') { + argc++; + break; + } + + while (offset < msg_len && full_msg.data[offset] != ' ') + offset++; + + argc++; + + while (offset < msg_len && full_msg.data[offset] == ' ') + offset++; + } + } + offset = old_offset; + + struct string argv[argc]; + if (offset < msg_len) { + uint64_t i = 0; + while (offset < msg_len) { + if (full_msg.data[offset] == ':') { + argv[i].data = full_msg.data+offset+1; + argv[i].len = msg_len - offset - 1; + break; + } + + argv[i].data = full_msg.data+offset; + uint64_t start = offset; + + while (offset < msg_len && full_msg.data[offset] != ' ') + offset++; + + argv[i].len = offset - start; + + while (offset < msg_len && full_msg.data[offset] == ' ') + offset++; + + i++; + } + } + + int (*func)(uint64_t argc, struct string *argv) = get_table_index(client_network_commands, command); + + if (func == 0) { + WRITES(2, STRING("WARNING: Command is unknown, ignoring...\n")); + } else { + int err = func(argc, argv); + if (err) { + WRITES(1, STRING("Disconnecting client by result of the network command handler...\n")); + goto disconnect_client; + } + } + write(1, "\n", 1); + + if (full_msg.data[msg_len] == '\r') + msg_len++; + memmove(full_msg.data, full_msg.data+msg_len+1, full_msg.len - msg_len - 1); + full_msg.len -= msg_len+1; + + found = 0; + for (uint64_t i = 0; i < full_msg.len; i++) { + if (full_msg.data[i] == '\n') { + found = 1; + msg_len = i; + break; + } + } + + if (found == 0) { + void *tmp = realloc(full_msg.data, full_msg.len); + if (tmp == 0 && full_msg.len != 0) { + puts("AAAAAAAAA (OOM shrinking allocated data?)"); + goto disconnect_client; + } + full_msg.data = tmp; + + break; + } + } + } + disconnect_client: + close(client_fd); + free(full_msg.data); + listen(client_listen_fd, 1); + } +} + +pthread_t client_thread_id; int main(void) { initservernetwork(); + initclientnetwork(); + + pthread_create(&client_thread_id, NULL, client_loop, NULL); struct string full_msg = {malloc(0), 0}; while (1) { - uint8_t data[512]; + char data[512]; uint64_t new_len; { int len; @@ -190,7 +360,7 @@ int main(void) { } } - int (*func)(struct string source, uint64_t argc, struct string *argv) = get_table_index(network_commands, command); + int (*func)(struct string source, uint64_t argc, struct string *argv) = get_table_index(server_network_commands, command); if (func == 0) { WRITES(2, STRING("WARNING: Command is unknown, ignoring...\n")); -- cgit v1.2.3