From e4b5445b3ca844e568a84abbf931a026a6ca6226 Mon Sep 17 00:00:00 2001 From: Test_User Date: Wed, 3 May 2023 22:57:53 -0400 Subject: C HaxServ --- main.c | 189 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 main.c (limited to 'main.c') diff --git a/main.c b/main.c new file mode 100644 index 0000000..6bebabb --- /dev/null +++ b/main.c @@ -0,0 +1,189 @@ +#include +#include + +#include "network.h" +#include "config.h" +#include "types.h" +#include "tls.h" +#include "types.h" + +int main(void) { + initservernetwork(); + + struct string full_msg = {malloc(0), 0}; + while (1) { + uint8_t data[512]; + uint64_t new_len = SSL_read(ssl, data, 512); + + if (new_len == 0) { + puts("Disconnected."); + return 0; + } + + 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) { + puts("OOM... currently just exiting bc there's no automatic reconnect in here yet, and the only sane solution to this is resyncing."); + return 1; + } + 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 + + uint64_t offset = 0; + while (offset < msg_len && full_msg.data[offset] == ' ') + offset++; + + if (msg_len == offset) { + puts("Protocol violation: Empty message."); + return 2; + } + + struct string source; + if (full_msg.data[0] == ':') { + source.data = full_msg.data + 1; + found = 0; + for (uint64_t i = offset + 1; i < msg_len; i++) { + if (full_msg.data[i] == ' ') { + found = 1; + offset = i + 1; + source.len = i - 1; + break; + } + } + if (!found || source.len + 1 == msg_len) { + puts("Protocol violation: Sender but no command."); + return 2; + } + } else { + source = (struct string){0}; + offset = 0; + } + + while (offset < msg_len && full_msg.data[offset] == ' ') + offset++; + + if (offset == msg_len) { + puts("Protocol violation: No command."); + return 2; + } + + 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)(struct string source, uint64_t argc, struct string *argv) = get_table_index(network_commands, command); + + if (func == 0) { + WRITES(1, STRING("WARNING: Command is unknown, ignoring...\n")); + } else { + write(1, "\n", 1); + func(source, argc, argv); + } + write(1, "\n", 1); + + 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?)"); + return 1; + } + full_msg.data = tmp; + + break; + } + } + } + + return 0; +} -- cgit v1.2.3