From 403ababa549ff6c29446a7e448de9e4c2c426eab Mon Sep 17 00:00:00 2001 From: Radkesvat <134321679+radkesvat@users.noreply.github.com> Date: Tue, 18 Jun 2024 22:21:45 +0000 Subject: [PATCH] 32 bit support --- CMakeLists.txt | 6 ++- tunnels/client/reverse/helpers.h | 8 ++- tunnels/client/reverse/reverse_client.c | 4 +- ww/basic_types.h | 2 +- ww/eventloop/base/hbase.c | 70 ++++++++++++++++--------- ww/eventloop/base/hlsem.c | 17 +----- 6 files changed, 58 insertions(+), 49 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d77140f..4f8c175d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,8 @@ set(CMAKE_CXX_STANDARD 11 CACHE INTERNAL "CXX standard version" FORCE) set(CMAKE_C_STANDARD_REQUIRED TRUE CACHE BOOL "request not to use lower versions" FORCE) set(CMAKE_CXX_STANDARD_REQUIRED TRUE CACHE BOOL "request not to use lower versions" FORCE) +# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32") for 32 bit build, note that ssl libs need tweaks +# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32") if(CMAKE_BUILD_TYPE STREQUAL "Release") cmake_policy(SET CMP0069 NEW) @@ -50,8 +52,8 @@ option(INCLUDE_OPENSSL_SERVER "link OpenSSlServer staticly to the core" TRUE) option(INCLUDE_OPENSSL_CLIENT "link OpenSSLClient staticly to the core" TRUE) option(INCLUDE_TROJAN_AUTH_SERVER "link TrojanAuthServer staticly to the core" TRUE) option(INCLUDE_TROJAN_SOCKS_SERVER "link TrojanSocksServer staticly to the core" TRUE) -option(INCLUDE_WOLFSSL_SERVER "link WolfSSLServer staticly to the core" TRUE) # temporarely disabled for compile speed -option(INCLUDE_WOLFSSL_CLIENT "link WolfSSLClient staticly to the core" TRUE) # temporarely disabled for compile speed +option(INCLUDE_WOLFSSL_SERVER "link WolfSSLServer staticly to the core" FALSE) # temporarely disabled for compile speed +option(INCLUDE_WOLFSSL_CLIENT "link WolfSSLClient staticly to the core" FALSE) # temporarely disabled for compile speed option(INCLUDE_BORINGSSL_SERVER "link BoringSSLServer staticly to the core" FALSE) #conflicts with openssl/noprefix ? option(INCLUDE_HTTP2_SERVER "link Http2Server staticly to the core" TRUE) option(INCLUDE_HTTP2_CLIENT "link Http2Client staticly to the core" TRUE) diff --git a/tunnels/client/reverse/helpers.h b/tunnels/client/reverse/helpers.h index e7691879..5d226232 100644 --- a/tunnels/client/reverse/helpers.h +++ b/tunnels/client/reverse/helpers.h @@ -5,7 +5,6 @@ #include "types.h" #include - enum { kPreconnectDelayShort = 10, @@ -49,7 +48,7 @@ static void cleanup(reverse_client_con_state_t *cstate) if (cstate->idle_handle) { reverse_client_state_t *state = STATE(cstate->self); - removeIdleItemByHash(cstate->u->tid, state->starved_connections, (hash_t) (cstate)); + removeIdleItemByHash(cstate->u->tid, state->starved_connections, (hash_t) (size_t) (cstate)); } doneLineDownSide(cstate->u); doneLineDownSide(cstate->d); @@ -60,11 +59,11 @@ static void cleanup(reverse_client_con_state_t *cstate) } static void doConnect(struct connect_arg *cg) { - tunnel_t *self = cg->t; + tunnel_t *self = cg->t; // reverse_client_state_t *state = STATE(self); reverse_client_con_state_t *cstate = createCstate(self, cg->tid); free(cg); - context_t *hello_data_ctx = newContext(cstate->u); + context_t *hello_data_ctx = newContext(cstate->u); self->up->upStream(self->up, newInitContext(cstate->u)); if (! isAlive(cstate->u)) @@ -151,7 +150,6 @@ static void onStarvedConnectionExpire(idle_item_t *idle_con) assert(! cstate->pair_connected); - state->threadlocal_pool[cstate->u->tid].unused_cons_count -= 1; LOGW("ReverseClient: a idle connection detected and closed"); diff --git a/tunnels/client/reverse/reverse_client.c b/tunnels/client/reverse/reverse_client.c index 89faa896..5e7867d3 100644 --- a/tunnels/client/reverse/reverse_client.c +++ b/tunnels/client/reverse/reverse_client.c @@ -68,7 +68,7 @@ static void downStream(tunnel_t *self, context_t *c) { ucstate->idle_handle = NULL; reverse_client_state_t *state = STATE(ucstate->self); - removeIdleItemByHash(ucstate->u->tid, state->starved_connections, (hash_t) (ucstate)); + removeIdleItemByHash(ucstate->u->tid, state->starved_connections, (hash_t)(size_t) (ucstate)); } ucstate->pair_connected = true; @@ -136,7 +136,7 @@ static void downStream(tunnel_t *self, context_t *c) initiateConnect(self, tid, false); - ucstate->idle_handle = newIdleItem(state->starved_connections, (hash_t) (ucstate), ucstate, + ucstate->idle_handle = newIdleItem(state->starved_connections, (hash_t) (size_t)(ucstate), ucstate, onStarvedConnectionExpire, c->line->tid, kConnectionStarvationTimeOut); diff --git a/ww/basic_types.h b/ww/basic_types.h index e9e9ead2..4fe513ab 100644 --- a/ww/basic_types.h +++ b/ww/basic_types.h @@ -10,7 +10,7 @@ #if defined(OS_UNIX) #include #elif ! defined(ssize_t) -typedef int64_t ssize_t; +typedef long ssize_t; #endif typedef uint64_t hash_t; diff --git a/ww/eventloop/base/hbase.c b/ww/eventloop/base/hbase.c index c7c36a2f..50cf43e2 100644 --- a/ww/eventloop/base/hbase.c +++ b/ww/eventloop/base/hbase.c @@ -100,7 +100,9 @@ char* hv_strreverse(char* str) { if (str == NULL) return NULL; char* b = str; char* e = str; - while(*e) {++e;} + while (*e) { + ++e; + } --e; char tmp; while (e > b) { @@ -128,7 +130,10 @@ char* hv_strncpy(char* dest, const char* src, size_t n) { char* hv_strncat(char* dest, const char* src, size_t n) { assert(dest != NULL && src != NULL); char* ret = dest; - while (*dest) {++dest;--n;} + while (*dest) { + ++dest; + --n; + } while (*src != '\0' && --n > 0) { *dest++ = *src++; } @@ -149,8 +154,14 @@ bool hv_strendswith(const char* str, const char* end) { assert(str != NULL && end != NULL); int len1 = 0; int len2 = 0; - while (*str) {++str; ++len1;} - while (*end) {++end; ++len2;} + while (*str) { + ++str; + ++len1; + } + while (*end) { + ++end; + ++len2; + } if (len1 < len2) return false; while (len2-- > 0) { --str; @@ -174,10 +185,12 @@ bool hv_wildcard_match(const char* str, const char* pattern) { if (*pattern == '*') { match = hv_strendswith(str, pattern + 1); break; - } else if (*str != *pattern) { + } + else if (*str != *pattern) { match = false; break; - } else { + } + else { ++str; ++pattern; } @@ -211,12 +224,12 @@ char* hv_strrchr_dir(const char* filepath) { const char* hv_basename(const char* filepath) { const char* pos = hv_strrchr_dir(filepath); - return pos ? pos+1 : filepath; + return pos ? pos + 1 : filepath; } const char* hv_suffixname(const char* filename) { const char* pos = hv_strrchr_dot(filename); - return pos ? pos+1 : ""; + return pos ? pos + 1 : ""; } int hv_mkdir_p(const char* dir) { @@ -402,19 +415,25 @@ bool hv_getboolean(const char* str) { } size_t hv_parse_size(const char* str) { - size_t size = 0, n = 0; + size_t size = 0; + uint64_t n = 0; const char* p = str; char c; while ((c = *p) != '\0') { if (c >= '0' && c <= '9') { n = n * 10 + c - '0'; - } else { + } + else { switch (c) { - case 'K': case 'k': n <<= 10; break; - case 'M': case 'm': n <<= 20; break; - case 'G': case 'g': n <<= 30; break; - case 'T': case 't': n <<= 40; break; - default: break; + case 'K': + case 'k': n <<= 10; break; + case 'M': + case 'm': n <<= 20; break; + case 'G': + case 'g': n <<= 30; break; + case 'T': + case 't': n <<= 40; break; + default: break; } size += n; n = 0; @@ -431,14 +450,15 @@ time_t hv_parse_time(const char* str) { while ((c = *p) != '\0') { if (c >= '0' && c <= '9') { n = n * 10 + c - '0'; - } else { + } + else { switch (c) { - case 's': break; - case 'm': n *= 60; break; - case 'h': n *= 60 * 60; break; - case 'd': n *= 24 * 60 * 60; break; - case 'w': n *= 7 * 24 * 60 * 60; break; - default: break; + case 's': break; + case 'm': n *= 60; break; + case 'h': n *= 60 * 60; break; + case 'd': n *= 24 * 60 * 60; break; + case 'w': n *= 7 * 24 * 60 * 60; break; + default: break; } time += n; n = 0; @@ -475,7 +495,8 @@ int hv_parse_url(hurl_t* stURL, const char* strURL) { if (pswd) { stURL->fields[HV_URL_PASSWORD].off = pswd + 1 - begin; stURL->fields[HV_URL_PASSWORD].len = pos - pswd - 1; - } else { + } + else { pswd = pos; } stURL->fields[HV_URL_USERNAME].off = user - begin; @@ -492,7 +513,8 @@ int hv_parse_url(hurl_t* stURL, const char* strURL) { for (unsigned short i = 1; i <= stURL->fields[HV_URL_PORT].len; ++i) { stURL->port = stURL->port * 10 + (port[i] - '0'); } - } else { + } + else { port = ep; // set default port stURL->port = 80; diff --git a/ww/eventloop/base/hlsem.c b/ww/eventloop/base/hlsem.c index e7b27ab2..ab056095 100644 --- a/ww/eventloop/base/hlsem.c +++ b/ww/eventloop/base/hlsem.c @@ -100,8 +100,6 @@ static bool SemaWait(hsem_t* sp) { } } - - static bool SemaTimedWait(hsem_t* sp, uint64_t timeout_usecs) { mach_timespec_t ts; ts.tv_sec = (uint32_t)(timeout_usecs / USECS_IN_1_SEC); @@ -215,11 +213,7 @@ static bool SemaSignal(hsem_t* sp, uint32_t count) { #define LSEMA_MAX_SPINS 10000 static bool _LSemaWaitPartialSpin(hlsem_t* s, uint64_t timeout_usecs) { -#ifdef OS_UNIX - ssize_t oldCount; -#else long oldCount; -#endif int spin = LSEMA_MAX_SPINS; while (--spin >= 0) { oldCount = atomic_load_explicit(&s->count, memory_order_relaxed); @@ -264,11 +258,8 @@ bool LSemaWait(hlsem_t* s) { } bool LSemaTryWait(hlsem_t* s) { -#ifdef OS_UNIX - ssize_t oldCount = atomic_load_explicit(&s->count, memory_order_relaxed); -#else long oldCount = atomic_load_explicit(&s->count, memory_order_relaxed); -#endif + while (oldCount > 0) { if (atomic_compare_exchange_weak_explicit(&s->count, &oldCount, oldCount - 1, memory_order_acquire, memory_order_relaxed)) { return true; @@ -283,13 +274,9 @@ bool LSemaTimedWait(hlsem_t* s, uint64_t timeout_usecs) { void LSemaSignal(hlsem_t* s, uint32_t count) { assert(count > 0); -#ifdef OS_UNIX - ssize_t oldCount = atomic_fetch_add_explicit(&s->count, (ssize_t)count, memory_order_release); - ssize_t toRelease = -oldCount < count ? -oldCount : (ssize_t)count; -#else + long oldCount = atomic_fetch_add_explicit(&s->count, (long)count, memory_order_release); long toRelease = -oldCount < (long)count ? -oldCount : (long)count; -#endif if (toRelease > 0) SemaSignal(&s->sema, (uint32_t)toRelease); }