diff --git a/tunnels/client/http2/helpers.h b/tunnels/client/http2/helpers.h index 28e5e6ac..c80528c2 100644 --- a/tunnels/client/http2/helpers.h +++ b/tunnels/client/http2/helpers.h @@ -181,6 +181,8 @@ static http2_client_con_state_t *createHttp2Connection(tunnel_t *self, int tid) nghttp2_settings_entry settings[] = { {NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, kMaxConcurrentStreams}, {NGHTTP2_SETTINGS_MAX_FRAME_SIZE, (1U << 18)}, + {NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, (1U << 18) } + }; nghttp2_submit_settings(con->session, NGHTTP2_FLAG_NONE, settings, ARRAY_SIZE(settings)); diff --git a/tunnels/client/http2/http2_client.c b/tunnels/client/http2/http2_client.c index 05e3cabb..3169c9dd 100644 --- a/tunnels/client/http2/http2_client.c +++ b/tunnels/client/http2/http2_client.c @@ -4,6 +4,7 @@ #include "tunnel.h" #include "types.h" #include "utils/jsonutils.h" +#include "utils/mathutils.h" enum { @@ -424,10 +425,10 @@ static void downStream(tunnel_t *self, context_t *c) if (c->payload != NULL) { - con->state = kH2WantRecv; - size_t len = bufLen(c->payload); - size_t ret = nghttp2_session_mem_recv2(con->session, (const uint8_t *) rawBuf(c->payload), len); - assert(ret == len); + con->state = kH2WantRecv; + size_t len = bufLen(c->payload); + ssize_t ret = nghttp2_session_mem_recv2(con->session, (const uint8_t *) rawBuf(c->payload), len); + assert(ret == (ssize_t) len); reuseContextBuffer(c); if (! isAlive(c->line)) @@ -436,7 +437,7 @@ static void downStream(tunnel_t *self, context_t *c) return; } - if (ret != len) + if (ret != (ssize_t) len) { deleteHttp2Connection(con); self->dw->downStream(self->dw, newFinContext(c->line)); @@ -528,7 +529,7 @@ tunnel_t *newHttp2Client(node_instance_context_t *instance_info) t->state = state; t->upStream = &upStream; t->downStream = &downStream; - + return t; } diff --git a/tunnels/client/reality/reality_client.c b/tunnels/client/reality/reality_client.c index de3e8ff6..04c54e02 100644 --- a/tunnels/client/reality/reality_client.c +++ b/tunnels/client/reality/reality_client.c @@ -117,7 +117,7 @@ static void upStream(tunnel_t *self, context_t *c) shift_buffer_t *buf = c->payload; c->payload = NULL; - const unsigned int chunk_size = ((1 << 16) - (kSignLen + (2 * kEncryptionBlockSize) + kIVlen)); + const unsigned int chunk_size = ((1 << 15) - (kSignLen + (2 * kEncryptionBlockSize) + kIVlen)); if (bufLen(buf) < chunk_size) { diff --git a/tunnels/client/reverse/reverse_client.c b/tunnels/client/reverse/reverse_client.c index 44af0dba..656777b8 100644 --- a/tunnels/client/reverse/reverse_client.c +++ b/tunnels/client/reverse/reverse_client.c @@ -2,7 +2,6 @@ #include "helpers.h" #include "idle_table.h" #include "loggers/network_logger.h" -#include "shiftbuffer.h" #include "tunnel.h" #include "types.h" #include "utils/jsonutils.h" @@ -130,8 +129,8 @@ static void downStream(tunnel_t *self, context_t *c) initiateConnect(self, tid, false); - ucstate->idle_handle = newIdleItem(state->starved_connections, (hash_t) (ucstate), ucstate, - onStarvedConnectionExpire, c->line->tid, kConnectionStarvationTimeOut); + // ucstate->idle_handle = newIdleItem(state->starved_connections, (hash_t) (ucstate), ucstate, + // onStarvedConnectionExpire, c->line->tid, kConnectionStarvationTimeOut); destroyContext(c); } diff --git a/tunnels/server/halfduplex/halfduplex_server.c b/tunnels/server/halfduplex/halfduplex_server.c index 796575d1..905bcb8d 100644 --- a/tunnels/server/halfduplex/halfduplex_server.c +++ b/tunnels/server/halfduplex/halfduplex_server.c @@ -551,8 +551,6 @@ static void upStream(tunnel_t *self, context_t *c) assert(upload_line_cstate->buffering); - - shiftr(upload_line_cstate->buffering, sizeof(uint64_t)); if (bufLen(upload_line_cstate->buffering) > 0) { context_t *buf_ctx = newContext(main_line); diff --git a/tunnels/server/http2/helpers.h b/tunnels/server/http2/helpers.h index 64557bc9..0e8226ac 100644 --- a/tunnels/server/http2/helpers.h +++ b/tunnels/server/http2/helpers.h @@ -127,7 +127,11 @@ static http2_server_con_state_t *createHttp2Connection(tunnel_t *self, line_t *l nghttp2_settings_entry settings[] = { {NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, kMaxConcurrentStreams}, - {NGHTTP2_SETTINGS_MAX_FRAME_SIZE, (1U << 18)}, + {NGHTTP2_SETTINGS_MAX_FRAME_SIZE, (1U << 18) }, + {NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, (1U << 18) }, + + + }; nghttp2_submit_settings(con->session, NGHTTP2_FLAG_NONE, settings, ARRAY_SIZE(settings)); con->state = kH2SendSettings; diff --git a/tunnels/server/http2/http2_server.c b/tunnels/server/http2/http2_server.c index ffebfc6b..fed9ca7e 100644 --- a/tunnels/server/http2/http2_server.c +++ b/tunnels/server/http2/http2_server.c @@ -1,10 +1,11 @@ #include "http2_server.h" #include "grpc_def.h" -#include "http2_def.h" #include "helpers.h" +#include "http2_def.h" #include "loggers/network_logger.h" #include "nghttp2/nghttp2.h" #include "types.h" +#include "utils/mathutils.h" static int onHeaderCallback(nghttp2_session *session, const nghttp2_frame *frame, const uint8_t *_name, size_t namelen, const uint8_t *_value, size_t valuelen, uint8_t flags, void *userdata) @@ -24,7 +25,7 @@ static int onHeaderCallback(nghttp2_session *session, const nghttp2_frame *frame const char *value = (const char *) _value; // LOGD("%s: %s\n", name, value); - http2_server_con_state_t *con = (http2_server_con_state_t *) userdata; + http2_server_con_state_t *con = (http2_server_con_state_t *) userdata; if (*name == ':') { @@ -66,7 +67,7 @@ static int onDataChunkRecvCallback(nghttp2_session *session, uint8_t flags, int3 { return 0; } - http2_server_con_state_t *con = (http2_server_con_state_t *) userdata; + http2_server_con_state_t *con = (http2_server_con_state_t *) userdata; http2_server_child_con_state_t *stream = nghttp2_session_get_stream_user_data(session, stream_id); if (! stream) @@ -146,7 +147,7 @@ static int onFrameRecvCallback(nghttp2_session *session, const nghttp2_frame *fr // LOGD("onFrameRecvCallback\n"); printFrameHd(&frame->hd); http2_server_con_state_t *con = (http2_server_con_state_t *) userdata; - tunnel_t * self = con->tunnel; + tunnel_t *self = con->tunnel; switch (frame->hd.type) { @@ -184,7 +185,7 @@ static int onFrameRecvCallback(nghttp2_session *session, const nghttp2_frame *fr } nghttp2_session_set_stream_user_data(con->session, stream->stream_id, NULL); context_t *fc = newFinContext(stream->line); - tunnel_t * dest = stream->tunnel; + tunnel_t *dest = stream->tunnel; removeStream(con, stream); deleteHttp2Stream(stream); CSTATE_MUT(fc) = NULL; @@ -220,8 +221,7 @@ static int onFrameRecvCallback(nghttp2_session *session, const nghttp2_frame *fr return 0; } -static bool trySendResponse(tunnel_t *self, http2_server_con_state_t *con, size_t stream_id, - shift_buffer_t *buf) +static bool trySendResponse(tunnel_t *self, http2_server_con_state_t *con, size_t stream_id, shift_buffer_t *buf) { line_t *line = con->line; // http2_server_con_state_t *con = ((http2_server_con_state_t *)(((line->chains_state)[self->chain_index]))); @@ -230,7 +230,7 @@ static bool trySendResponse(tunnel_t *self, http2_server_con_state_t *con, size_ return false; } - char * data = NULL; + char *data = NULL; size_t len; len = nghttp2_session_mem_send(con->session, (const uint8_t **) &data); // LOGD("nghttp2_session_mem_send %d\n", len); @@ -306,8 +306,16 @@ static void upStream(tunnel_t *self, context_t *c) http2_server_con_state_t *con = CSTATE(c); con->state = kH2WantRecv; size_t len = bufLen(c->payload); - size_t ret = nghttp2_session_mem_recv2(con->session, (const uint8_t *) rawBuf(c->payload), len); - reuseContextBuffer(c); + + while (trySendResponse(self, con, 0, NULL)) + { + if (! isAlive(c->line)) + { + destroyContext(c); + return; + } + } + ssize_t ret = nghttp2_session_mem_recv2(con->session, (const uint8_t *) rawBuf(c->payload), len); if (! isAlive(c->line)) { @@ -315,7 +323,7 @@ static void upStream(tunnel_t *self, context_t *c) return; } - if (ret != len) + if (ret != (ssize_t) len) { deleteHttp2Connection(con); self->dw->downStream(self->dw, newFinContext(c->line)); @@ -323,14 +331,14 @@ static void upStream(tunnel_t *self, context_t *c) return; } - while (trySendResponse(self, con, 0, NULL)) + if (nghttp2_session_want_read(con->session) == 0 && nghttp2_session_want_write(con->session) == 0) { - if (! isAlive(c->line)) - { - destroyContext(c); - return; - } + context_t *fin_ctx = newFinContext(con->line); + deleteHttp2Connection(con); + self->dw->downStream(self->dw, fin_ctx); } + + reuseContextBuffer(c); destroyContext(c); } else @@ -353,7 +361,7 @@ static void upStream(tunnel_t *self, context_t *c) static void downStream(tunnel_t *self, context_t *c) { http2_server_child_con_state_t *stream = CSTATE(c); - http2_server_con_state_t * con = stream->parent->chains_state[self->chain_index]; + http2_server_con_state_t *con = stream->parent->chains_state[self->chain_index]; if (c->payload != NULL) { @@ -389,7 +397,7 @@ static void downStream(tunnel_t *self, context_t *c) deleteHttp2Stream(stream); CSTATE_MUT(c) = NULL; - while (trySendResponse(self, con, 0, NULL)) + while (trySendResponse(self, con, 0, NULL)) { if (! isAlive(c->line)) { @@ -414,11 +422,9 @@ static void downStream(tunnel_t *self, context_t *c) } } - - tunnel_t *newHttp2Server(node_instance_context_t *instance_info) { - (void)instance_info; + (void) instance_info; http2_server_state_t *state = malloc(sizeof(http2_server_state_t)); memset(state, 0, sizeof(http2_server_state_t)); @@ -432,11 +438,11 @@ tunnel_t *newHttp2Server(node_instance_context_t *instance_info) nghttp2_option_set_no_closed_streams(state->ngoptions, 1); nghttp2_option_set_no_http_messaging(state->ngoptions, 1); - tunnel_t *t = newTunnel(); - t->state = state; - t->upStream = &upStream; - t->downStream = &downStream; - + tunnel_t *t = newTunnel(); + t->state = state; + t->upStream = &upStream; + t->downStream = &downStream; + return t; } diff --git a/tunnels/server/reality/reality_server.c b/tunnels/server/reality/reality_server.c index ace22a83..5f7ad67a 100644 --- a/tunnels/server/reality/reality_server.c +++ b/tunnels/server/reality/reality_server.c @@ -265,7 +265,7 @@ static void downStream(tunnel_t *self, context_t *c) case kConAuthorized:; shift_buffer_t *buf = c->payload; c->payload = NULL; - const unsigned int chunk_size = ((1 << 16) - (kSignLen + (2 * kEncryptionBlockSize) + kIVlen)); + const unsigned int chunk_size = ((1 << 15) - (kSignLen + (2 * kEncryptionBlockSize) + kIVlen)); if (bufLen(buf) < chunk_size) { diff --git a/ww/ww.c b/ww/ww.c index f2f37795..e5b8318d 100644 --- a/ww/ww.c +++ b/ww/ww.c @@ -92,7 +92,7 @@ struct ww_runtime_state_s *getWW(void) } // trimming should not be necessary, using it for test purposes -// todo (remove) should be removed ? (status: disabled) +// todo (remove) should be removed ? (status: enabled) #ifdef OS_LINUX void idleFreeMem(htimer_t *timer) { @@ -107,7 +107,7 @@ htimer_t *trim_timer = NULL; _Noreturn void runMainThread(void) { -#if defined(OS_LINUX) && false +#if defined(OS_LINUX) && true trim_timer = htimer_add_period(loops[0], idleFreeMem, 2, 0, 0, 0, 0, INFINITE); #endif