From 590135ce9a1cb629c9cc31c9c528feac3885f7fc Mon Sep 17 00:00:00 2001 From: ame Date: Tue, 11 Feb 2025 02:36:44 -0600 Subject: fixes and asan slopgit add . --- makefile | 17 ++++++++++++++++- src/net.c | 48 ++++++++++++++++++++++++++++++++---------------- src/net/util.c | 6 +++--- tests/wss.lua | 8 +++++--- 4 files changed, 56 insertions(+), 23 deletions(-) diff --git a/makefile b/makefile index 991b84f..8f5d9a5 100644 --- a/makefile +++ b/makefile @@ -24,7 +24,22 @@ all: $(TARGET) release: CFLAGS += -O3 release: all -debug: CFLAGS += -g +# ok so im pretty sure asan should be linked too, however dlclose needs to be masked anyways +# and since libasan needs to be the first thing to load, you'll have to add it anyways +# run with something like 'LD_PRELOAD="/usr/lib/gcc/x86_64-pc-linux-gnu/14/libasan.so ./fakedlclose.so" lua5.4 ...' +# fakedlclose.so should be something as simple as the following: +# +# " +# #include +# int dlclose(void *handle) {;} +# " +# +# code (& fix) courtesy of +# https://github.com/google/sanitizers/issues/89#issuecomment-406316683 +# +# this also requires lua to be built with asan + +debug: CFLAGS += -ggdb3 -static-libasan -fsanitize=address -fsanitize=undefined -fno-omit-frame-pointer -fno-optimize-sibling-calls debug: all reg: diff --git a/src/net.c b/src/net.c index ba853e2..4163394 100644 --- a/src/net.c +++ b/src/net.c @@ -20,6 +20,16 @@ #define pab(M) {printf(M);abort();} +_Atomic int has_ssl_init = 0; + +void ssl_init(){ + if(has_ssl_init == 0){ + has_ssl_init = 1; + SSL_library_init(); + SSL_load_error_strings(); + } +} + SSL* ssl_connect(SSL_CTX* ctx, int sockfd, const char* hostname){ SSL* ssl = SSL_new(ctx); if(hostname != NULL) @@ -276,21 +286,23 @@ int i_ws_write(lua_State* L){ } int i_ws_close(lua_State* L){ - printf("free\n"); lua_pushstring(L, "_"); lua_gettable(L, 1); struct wss_data* data = lua_touserdata(L, -1); + if(data != NULL){ + str_free(data->buffer); - str_free(data->buffer); + SSL_set_shutdown(data->ssl, SSL_RECEIVED_SHUTDOWN | SSL_SENT_SHUTDOWN); + SSL_shutdown(data->ssl); + SSL_free(data->ssl); + SSL_CTX_free(data->ctx); - SSL_set_shutdown(data->ssl, SSL_RECEIVED_SHUTDOWN | SSL_SENT_SHUTDOWN); - SSL_shutdown(data->ssl); - SSL_free(data->ssl); - SSL_CTX_free(data->ctx); + close(data->sock); - close(data->sock); + free(data); + } - free(data); + luaI_tsetlud(L, 1, "_", NULL); return 0; } @@ -309,8 +321,7 @@ int l_wss(lua_State* L){ int set = 1; signal(SIGPIPE, SIG_IGN); - SSL_library_init(); - SSL_load_error_strings(); + ssl_init(); SSL_CTX* ctx = SSL_CTX_new(SSLv23_client_method()); SSL* ssl = ssl_connect(ctx, sock, awa.domain->c); @@ -385,13 +396,15 @@ int l_srequest(lua_State* L){ } int sock = get_host((char*)host, (char*)port); - if(sock == -1) abort(); + if(sock == -1){ + p_fatal("could not resolve address"); + abort(); + } - SSL_library_init(); - SSL_load_error_strings(); + ssl_init(); SSL_CTX* ctx = SSL_CTX_new(SSLv23_client_method()); SSL* ssl = ssl_connect(ctx, sock, host); - + char* path = "/"; if(params >= check + 1){ check++; @@ -466,7 +479,9 @@ int l_srequest(lua_State* L){ int idx = lua_gettop(L); parray_t* owo = NULL; + //handle errors int err = parse_header(a->c, header_eof - a->c, &owo); + assert(err == 0); for(int i = 0; i != owo->len; i++){ luaI_tsets(L, idx, (owo->P[i].key)->c, ((str*)owo->P[i].value)->c); @@ -497,6 +512,7 @@ int l_srequest(lua_State* L){ content = state.content; } } else { + str_pushl(content, header_eof + 4, extra_len - 4); memset(buffer, 0, BUFFER_LEN); for(; (len = SSL_read(ssl, buffer, BUFFER_LEN)) > 0;){ @@ -598,14 +614,14 @@ void* handle_client(void *_arg){ int64_t bite = recv_header(client_fd, &buffer, &header); header_eof = header - buffer; - printf("%x = %p - %p\n", header_eof, header, buffer); + /*printf("%x = %p - %p\n", header_eof, header, buffer); if(bite == -2) net_error(client_fd, 431); printf("'"); for(int i = bite - 20; i != bite; i++){ putchar(buffer[i]); } - printf("'\n"); + printf("'\n");*/ /* return NULL; diff --git a/src/net/util.c b/src/net/util.c index b54c253..b7e0b9d 100644 --- a/src/net/util.c +++ b/src/net/util.c @@ -100,7 +100,7 @@ int64_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof, int* st return len; } -#define max_uri_len 2048 +#define max_uri_len 4096 /** * @brief converts the request buffer into a parray_t * @@ -118,7 +118,7 @@ int parse_header(char* buffer, int header_eof, parray_t** _table){ for(; oi != header_eof; oi++){ if(buffer[oi] == ' ' || buffer[oi] == '\n'){ if(buffer[oi] == '\n') current->c[current->len - 1] = 0; - parray_set(table, item == 0 ? "Request" : + if(item < 3) parray_set(table, item == 0 ? "Request" : item == 1 ? "Path" : "Version", (void*)str_init(current->c)); str_clear(current); item++; @@ -133,7 +133,7 @@ int parse_header(char* buffer, int header_eof, parray_t** _table){ } } - if(item != 3){ + if(item < 3){ str_free(current); *_table = table; return -1; diff --git a/tests/wss.lua b/tests/wss.lua index 1344e0b..d6383a1 100644 --- a/tests/wss.lua +++ b/tests/wss.lua @@ -1,12 +1,14 @@ llby = require("lullaby") -local ws = llby.net.wss("echo.websocket.org", 443) +local ws = llby.net.wss("echo.websocket.org:443") -for i=1,500 do - local c = ws:read() +for i=1,10 do + local c = ws:read().content print(c) ws:write(c.."a") end + +ws:close() ws:close() --ws:ping() -- cgit v1.2.3