diff --git a/tunnels/adapters/connector/connector.c b/tunnels/adapters/connector/connector.c index 9bee197b..cfdbe5da 100644 --- a/tunnels/adapters/connector/connector.c +++ b/tunnels/adapters/connector/connector.c @@ -3,6 +3,9 @@ #include "utils/sockutils.h" #include "loggers/network_logger.h" + + + tunnel_t *newConnector(node_instance_context_t *instance_info) { connector_state_t *state = malloc(sizeof(connector_state_t)); @@ -58,10 +61,8 @@ tunnel_t *newConnector(node_instance_context_t *instance_info) tunnel_t *t = newTunnel(); t->state = state; - t->upStream = &connectorUpStream; - t->packetUpStream = &connectorPacketUpStream; - t->downStream = &connectorDownStream; - t->packetDownStream = &connectorPacketDownStream; + t->upStream = &upStream; + t->downStream = &downStream; atomic_thread_fence(memory_order_release); diff --git a/tunnels/adapters/connector/connector.h b/tunnels/adapters/connector/connector.h index 54fced95..975590eb 100644 --- a/tunnels/adapters/connector/connector.h +++ b/tunnels/adapters/connector/connector.h @@ -2,9 +2,9 @@ #include "api.h" -// con <-----\ /-----> Resolve=> TCP Connect || Udp Associate -// con <------> Connector <------> Resolve=> TCP Connect || Udp Associate -// con <-----/ \-----> Resolve=> TCP Connect || Udp Associate +// con <-----\ /-----> TCP Connect || Udp Associate +// con <------> Connector <------> TCP Connect || Udp Associate +// con <-----/ \-----> TCP Connect || Udp Associate // diff --git a/tunnels/adapters/connector/resolve.c b/tunnels/adapters/connector/resolve.c deleted file mode 100644 index 5adff767..00000000 --- a/tunnels/adapters/connector/resolve.c +++ /dev/null @@ -1,34 +0,0 @@ -#include "types.h" -#include "loggers/dns_logger.h" - -bool connectorResolvedomain(socket_context_t *dest) -{ - uint16_t old_port = sockaddr_port(&(dest->addr)); -#ifdef PROFILE - struct timeval tv1, tv2; - gettimeofday(&tv1, NULL); -#endif - assert(dest->domain != NULL); - /* resolve domain */ - { - if (sockaddr_set_ipport(&(dest->addr), dest->domain, old_port) != 0) - { - LOGE("Connector: resolve failed %s", dest->domain); - return false; - } - else - { - char ip[60]; - sockaddr_str(&(dest->addr), ip, 60); - LOGI("Connector: %s resolved to %s", dest->domain, ip); - } - } -#ifdef PROFILE - gettimeofday(&tv2, NULL); - double time_spent = (double)(tv2.tv_usec - tv1.tv_usec) / 1000000 + (double)(tv2.tv_sec - tv1.tv_sec); - LOGD("Connector: dns resolve took %lf sec", time_spent); -#endif - - dest->resolved = true; - return true; -} diff --git a/tunnels/adapters/tcp_connector/CMakeLists.txt b/tunnels/adapters/connector/tcp/CMakeLists.txt similarity index 100% rename from tunnels/adapters/tcp_connector/CMakeLists.txt rename to tunnels/adapters/connector/tcp/CMakeLists.txt diff --git a/tunnels/adapters/tcp_connector/tcp_connector.c b/tunnels/adapters/connector/tcp/tcp_connector.c similarity index 64% rename from tunnels/adapters/tcp_connector/tcp_connector.c rename to tunnels/adapters/connector/tcp/tcp_connector.c index ec5791b6..2863f8f2 100644 --- a/tunnels/adapters/tcp_connector/tcp_connector.c +++ b/tunnels/adapters/connector/tcp/tcp_connector.c @@ -1,12 +1,14 @@ #include "tcp_connector.h" +#include "loggers/network_logger.h" #include "types.h" #include "utils/sockutils.h" -#include "loggers/network_logger.h" static void cleanup(tcp_connector_con_state_t *cstate) { if (cstate->io) + { hevent_set_userdata(cstate->io, NULL); + } hio_t *last_resumed_io = NULL; @@ -42,66 +44,73 @@ static void cleanup(tcp_connector_con_state_t *cstate) free(cstate); } -static bool resume_write_queue(tcp_connector_con_state_t *cstate) +static bool resumeWriteQueue(tcp_connector_con_state_t *cstate) { - context_queue_t *data_queue = (cstate)->data_queue; + context_queue_t *data_queue = (cstate)->data_queue; context_queue_t *finished_queue = (cstate)->finished_queue; - hio_t *io = cstate->io; + hio_t * io = cstate->io; while (contextQueueLen(data_queue) > 0) { context_t *cw = contextQueuePop(data_queue); - int bytes = bufLen(cw->payload); - int nwrite = hio_write(io, rawBuf(cw->payload), bytes); + unsigned int bytes = bufLen(cw->payload); + int nwrite = hio_write(io, rawBuf(cw->payload), bytes); reuseBuffer(cstate->buffer_pool, cw->payload); cw->payload = NULL; contextQueuePush(cstate->finished_queue, cw); if (nwrite >= 0 && nwrite < bytes) - return false; // write pending - } - // data data_queue is empty - hio_t *last_resumed_io = NULL; - while (contextQueueLen(finished_queue) > 0) - { - context_t *cw = contextQueuePop(finished_queue); - hio_t *upstream_io = cw->src_io; - if (upstream_io != NULL && (last_resumed_io != upstream_io)) { - last_resumed_io = upstream_io; - hio_read(upstream_io); + return false; // write pending} } - destroyContext(cw); + // data data_queue is empty + hio_t *last_resumed_io = NULL; + while (contextQueueLen(finished_queue) > 0) + { + context_t *cw = contextQueuePop(finished_queue); + hio_t * upstream_io = cw->src_io; + if (upstream_io != NULL && (last_resumed_io != upstream_io)) + { + last_resumed_io = upstream_io; + hio_read(upstream_io); + } + destroyContext(cw); + } + return true; } - return true; } - -static void on_write_complete(hio_t * restrict io, const void * restrict buf, int writebytes) +static void onWriteComplete(hio_t *restrict io, const void *restrict buf, int writebytes) { + (void) buf; + (void) writebytes; + // resume the read on other end of the connection - tcp_connector_con_state_t *cstate = (tcp_connector_con_state_t *)(hevent_userdata(io)); + tcp_connector_con_state_t *cstate = (tcp_connector_con_state_t *) (hevent_userdata(io)); if (cstate == NULL) + { return; + } if (hio_write_is_complete(io)) { hio_setcb_write(cstate->io, NULL); cstate->write_paused = false; - context_queue_t *data_queue = cstate->data_queue; + context_queue_t *data_queue = cstate->data_queue; context_queue_t *finished_queue = cstate->finished_queue; if (contextQueueLen(data_queue) > 0) - if (!resume_write_queue(cstate)) + { + if (! resumeWriteQueue(cstate)) { - hio_setcb_write(cstate->io, on_write_complete); + hio_setcb_write(cstate->io, onWriteComplete); cstate->write_paused = true; return; } - + } hio_t *last_resumed_io = NULL; while (contextQueueLen(finished_queue) > 0) { - context_t *cw = contextQueuePop(finished_queue); - hio_t *upstream_io = cw->src_io; + context_t *cw = contextQueuePop(finished_queue); + hio_t * upstream_io = cw->src_io; if (upstream_io != NULL && (last_resumed_io != upstream_io)) { last_resumed_io = upstream_io; @@ -112,39 +121,42 @@ static void on_write_complete(hio_t * restrict io, const void * restrict buf, in } } -static void on_recv(hio_t * restrict io, void * restrict buf, int readbytes) +static void onRecv(hio_t *restrict io, void *restrict buf, int readbytes) { - tcp_connector_con_state_t *cstate = (tcp_connector_con_state_t *)(hevent_userdata(io)); + tcp_connector_con_state_t *cstate = (tcp_connector_con_state_t *) (hevent_userdata(io)); if (cstate == NULL) + { return; + } shift_buffer_t *payload = popBuffer(cstate->buffer_pool); setLen(payload, readbytes); - writeRaw(payload,buf,readbytes); + writeRaw(payload, buf, readbytes); tunnel_t *self = (cstate)->tunnel; - line_t *line = (cstate)->line; + line_t * line = (cstate)->line; context_t *context = newContext(line); - context->src_io = io; - context->payload = payload; + context->src_io = io; + context->payload = payload; self->downStream(self, context); } -static void on_close(hio_t *io) +static void onClose(hio_t *io) { - tcp_connector_con_state_t *cstate = (tcp_connector_con_state_t *)(hevent_userdata(io)); + tcp_connector_con_state_t *cstate = (tcp_connector_con_state_t *) (hevent_userdata(io)); if (cstate != NULL) - LOGD("TcpConnector: received close for FD:%x ", - (int)hio_fd(io)); + { + LOGD("TcpConnector: received close for FD:%x ", (int) hio_fd(io)); + } else - LOGD("TcpConnector: sent close for FD:%x ", - (int)hio_fd(io)); - + { + LOGD("TcpConnector: sent close for FD:%x ", (int) hio_fd(io)); + } if (cstate != NULL) { - tunnel_t *self = (cstate)->tunnel; - line_t *line = (cstate)->line; + tunnel_t * self = (cstate)->tunnel; + line_t * line = (cstate)->line; context_t *context = newFinContext(line); self->downStream(self, context); } @@ -157,29 +169,28 @@ static void onOutBoundConnected(hio_t *upstream_io) struct timeval tv2; gettimeofday(&tv2, NULL); - double time_spent = (double)(tv2.tv_usec - (cstate->__profile_conenct).tv_usec) / 1000000 + (double)(tv2.tv_sec - (cstate->__profile_conenct).tv_sec); - LOGD("TcpConnector: tcp connect took %d ms", (int)(time_spent * 1000)); + double time_spent = (double) (tv2.tv_usec - (cstate->__profile_conenct).tv_usec) / 1000000 + + (double) (tv2.tv_sec - (cstate->__profile_conenct).tv_sec); + LOGD("TcpConnector: tcp connect took %d ms", (int) (time_spent * 1000)); #endif tunnel_t *self = cstate->tunnel; - line_t *line = cstate->line; - hio_setcb_read(upstream_io, on_recv); + line_t * line = cstate->line; + hio_setcb_read(upstream_io, onRecv); char localaddrstr[SOCKADDR_STRLEN] = {0}; - char peeraddrstr[SOCKADDR_STRLEN] = {0}; + char peeraddrstr[SOCKADDR_STRLEN] = {0}; - LOGD("TcpConnector: connection succeed FD:%x [%s] => [%s]", - (int)hio_fd(upstream_io), - SOCKADDR_STR(hio_localaddr(upstream_io), localaddrstr), - SOCKADDR_STR(hio_peeraddr(upstream_io), peeraddrstr)); + LOGD("TcpConnector: connection succeed FD:%x [%s] => [%s]", (int) hio_fd(upstream_io), + SOCKADDR_STR(hio_localaddr(upstream_io), localaddrstr), SOCKADDR_STR(hio_peeraddr(upstream_io), peeraddrstr)); context_t *est_context = newContext(line); - est_context->est = true; - est_context->src_io = upstream_io; + est_context->est = true; + est_context->src_io = upstream_io; self->downStream(self, est_context); } -void tcpConnectorUpStream(tunnel_t *self, context_t *c) +void upStream(tunnel_t *self, context_t *c) { tcp_connector_con_state_t *cstate = CSTATE(c); @@ -188,23 +199,27 @@ void tcpConnectorUpStream(tunnel_t *self, context_t *c) if (cstate->write_paused) { if (c->src_io) + { hio_read_stop(c->src_io); + } contextQueuePush(cstate->data_queue, c); } else { - int bytes = bufLen(c->payload); - int nwrite = hio_write(cstate->io, rawBuf(c->payload), bytes); + unsigned int bytes = bufLen(c->payload); + int nwrite = hio_write(cstate->io, rawBuf(c->payload), bytes); if (nwrite >= 0 && nwrite < bytes) { if (c->src_io) + { hio_read_stop(c->src_io); + } reuseBuffer(cstate->buffer_pool, c->payload); c->payload = NULL; contextQueuePush(cstate->finished_queue, c); cstate->write_paused = true; - hio_setcb_write(cstate->io, on_write_complete); + hio_setcb_write(cstate->io, onWriteComplete); } else { @@ -226,24 +241,28 @@ void tcpConnectorUpStream(tunnel_t *self, context_t *c) gettimeofday(&(cstate->__profile_conenct), NULL); #endif - cstate->buffer_pool = buffer_pools[c->line->tid]; - cstate->tunnel = self; - cstate->line = c->line; - cstate->data_queue = newContextQueue(cstate->buffer_pool); + cstate->buffer_pool = buffer_pools[c->line->tid]; + cstate->tunnel = self; + cstate->line = c->line; + cstate->data_queue = newContextQueue(cstate->buffer_pool); cstate->finished_queue = newContextQueue(cstate->buffer_pool); - cstate->write_paused = true; + cstate->write_paused = true; socket_context_t final_ctx = {0}; // fill the final_ctx address based on settings { - socket_context_t *src_ctx = &(c->line->src_ctx); - socket_context_t *dest_ctx = &(c->line->dest_ctx); - tcp_connector_state_t *state = STATE(self); + socket_context_t * src_ctx = &(c->line->src_ctx); + socket_context_t * dest_ctx = &(c->line->dest_ctx); + tcp_connector_state_t *state = STATE(self); - if (state->dest_addr.status == cdvs_from_source) + if (state->dest_addr.status == kCdvsFromSource) + { copySocketContextAddr(&final_ctx, &src_ctx); - else if (state->dest_addr.status == cdvs_from_dest) + } + else if (state->dest_addr.status == kCdvsFromDest) + { copySocketContextAddr(&final_ctx, &dest_ctx); + } else { final_ctx.atype = state->dest_atype; @@ -251,19 +270,27 @@ void tcpConnectorUpStream(tunnel_t *self, context_t *c) { final_ctx.domain = malloc(state->dest_domain_len + 1); memcpy(final_ctx.domain, state->dest_addr.value_ptr, state->dest_domain_len + 1); - final_ctx.resolved = false; + final_ctx.resolved = false; final_ctx.addr.sa.sa_family = AF_INET; // addr resolve will change this } else + { sockaddr_set_ip(&(final_ctx.addr), state->dest_addr.value_ptr); + } } - if (state->dest_port.status == cdvs_from_source) + if (state->dest_port.status == kCdvsFromSource) + { sockaddr_set_port(&(final_ctx.addr), sockaddr_port(&(src_ctx->addr))); - else if (state->dest_port.status == cdvs_from_dest) + } + else if (state->dest_port.status == kCdvsFromDest) + { sockaddr_set_port(&(final_ctx.addr), sockaddr_port(&(dest_ctx->addr))); + } else + { sockaddr_set_port(&(final_ctx.addr), state->dest_port.value); + } } // sockaddr_set_ipport(&(final_ctx.addr), "127.0.0.1", 443); @@ -271,9 +298,9 @@ void tcpConnectorUpStream(tunnel_t *self, context_t *c) LOGD("TcpConnector: initiating connection"); if (final_ctx.atype == kSatDomainName) { - if (!final_ctx.resolved) + if (! final_ctx.resolved) { - if (!tcpConnectorResolvedomain(&final_ctx)) + if (! tcpConnectorResolvedomain(&final_ctx)) { free(final_ctx.domain); cleanup(cstate); @@ -284,9 +311,10 @@ void tcpConnectorUpStream(tunnel_t *self, context_t *c) } free(final_ctx.domain); } + tcp_connector_state_t *state = STATE(self); - hloop_t *loop = loops[c->line->tid]; - int sockfd = socket(final_ctx.addr.sa.sa_family, SOCK_STREAM, 0); + hloop_t *loop = loops[c->line->tid]; + int sockfd = socket(final_ctx.addr.sa.sa_family, SOCK_STREAM, 0); if (sockfd < 0) { LOGE("Connector: socket fd < 0"); @@ -294,16 +322,16 @@ void tcpConnectorUpStream(tunnel_t *self, context_t *c) CSTATE_MUT(c) = NULL; goto fail; } - if (STATE(self)->tcp_no_delay) + if (state->tcp_no_delay) { tcp_nodelay(sockfd, 1); } - if (STATE(self)->reuse_addr) + if (state->reuse_addr) { so_reuseport(sockfd, 1); } - if (STATE(self)->tcp_fast_open) + if (state->tcp_fast_open) { const int yes = 1; setsockopt(sockfd, SOL_TCP, TCP_FASTOPEN, &yes, sizeof(yes)); @@ -316,13 +344,8 @@ void tcpConnectorUpStream(tunnel_t *self, context_t *c) cstate->io = upstream_io; hevent_set_userdata(upstream_io, cstate); - // io <=> upstream_io - // hio_setup_upstream(io, upstream_io); hio_setcb_connect(upstream_io, onOutBoundConnected); - hio_setcb_close(upstream_io, on_close); - - // printf("connect to "); - // SOCKADDR_PRINT(hio_peeraddr(upstream_io)); + hio_setcb_close(upstream_io, onClose); hio_connect(upstream_io); destroyContext(c); } @@ -340,7 +363,7 @@ fail:; self->dw->downStream(self->dw, newFinContext(c->line)); destroyContext(c); } -void tcpConnectorDownStream(tunnel_t *self, context_t *c) +void downStream(tunnel_t *self, context_t *c) { tcp_connector_con_state_t *cstate = CSTATE(c); @@ -353,8 +376,8 @@ void tcpConnectorDownStream(tunnel_t *self, context_t *c) self->dw->downStream(self->dw, c); } gettimeofday(&tv2, NULL); - double time_spent = (double)(tv2.tv_usec - tv1.tv_usec) / 1000000 + (double)(tv2.tv_sec - tv1.tv_sec); - LOGD("TcpConnector: tcp downstream took %d ms", (int)(time_spent * 1000)); + double time_spent = (double) (tv2.tv_usec - tv1.tv_usec) / 1000000 + (double) (tv2.tv_sec - tv1.tv_sec); + LOGD("TcpConnector: tcp downstream took %d ms", (int) (time_spent * 1000)); #else self->dw->downStream(self->dw, c); @@ -367,11 +390,14 @@ void tcpConnectorDownStream(tunnel_t *self, context_t *c) { cstate->established = true; hio_read(cstate->io); - if (resume_write_queue(cstate)) + if (resumeWriteQueue(cstate)) + { cstate->write_paused = false; + } else - hio_setcb_write(cstate->io, on_write_complete); - + { + hio_setcb_write(cstate->io, onWriteComplete); + } self->dw->downStream(self->dw, c); } else if (c->fin) @@ -383,29 +409,13 @@ void tcpConnectorDownStream(tunnel_t *self, context_t *c) } } -void tcpConnectorPacketUpStream(tunnel_t *self, context_t *c) -{ - LOGE("TcpConnector: this node dose not support udp packets"); - - if (c->payload != NULL) - { - reuseContextBuffer(c); - } - self->dw->downStream(self->dw, newFinContext(c->line)); - destroyContext(c); -} -void tcpConnectorPacketDownStream(tunnel_t *self, context_t *c) -{ - // unreachable -} - tunnel_t *newTcpConnector(node_instance_context_t *instance_info) { tcp_connector_state_t *state = malloc(sizeof(tcp_connector_state_t)); memset(state, 0, sizeof(tcp_connector_state_t)); const cJSON *settings = instance_info->node_settings_json; - if (!(cJSON_IsObject(settings) && settings->child != NULL)) + if (! (cJSON_IsObject(settings) && settings->child != NULL)) { LOGF("JSON Error: TcpConnector->settings (object field) : The object was empty or invalid"); return NULL; @@ -426,9 +436,8 @@ tunnel_t *newTcpConnector(node_instance_context_t *instance_info) // memset set everything to 0... } - state->dest_addr = parseDynamicStrValueFromJsonObject(settings, "address", 2, - "src_context->address", - "dest_context->address"); + state->dest_addr = + parseDynamicStrValueFromJsonObject(settings, "address", 2, "src_context->address", "dest_context->address"); if (state->dest_addr.status == kDvsEmpty) { @@ -436,9 +445,8 @@ tunnel_t *newTcpConnector(node_instance_context_t *instance_info) return NULL; } - state->dest_port = parseDynamicNumericValueFromJsonObject(settings, "port", 2, - "src_context->port", - "dest_context->port"); + state->dest_port = + parseDynamicNumericValueFromJsonObject(settings, "port", 2, "src_context->port", "dest_context->port"); if (state->dest_port.status == kDvsEmpty) { @@ -447,28 +455,28 @@ tunnel_t *newTcpConnector(node_instance_context_t *instance_info) } if (state->dest_addr.status == kDvsConstant) { - state->dest_atype = getHostAddrType(state->dest_addr.value_ptr); + state->dest_atype = getHostAddrType(state->dest_addr.value_ptr); state->dest_domain_len = strlen(state->dest_addr.value_ptr); } - tunnel_t *t = newTunnel(); - t->state = state; - - t->upStream = &tcpConnectorUpStream; - t->packetUpStream = &tcpConnectorPacketUpStream; - t->downStream = &tcpConnectorDownStream; - t->packetDownStream = &tcpConnectorPacketDownStream; + tunnel_t *t = newTunnel(); + t->state = state; + t->upStream = &upStream; + t->downStream = &downStream; atomic_thread_fence(memory_order_release); return t; } -api_result_t apiTcpConnector(tunnel_t *self, char *msg) +api_result_t apiTcpConnector(tunnel_t *self, const char *msg) { - (void)(self); (void)(msg); return (api_result_t){0}; // TODO + (void) (self); + (void) (msg); + return (api_result_t){0}; } tunnel_t *destroyTcpConnector(tunnel_t *self) { + (void) (self); return NULL; } tunnel_metadata_t getMetadataTcpConnector() diff --git a/tunnels/adapters/tcp_connector/tcp_connector.h b/tunnels/adapters/connector/tcp/tcp_connector.h similarity index 82% rename from tunnels/adapters/tcp_connector/tcp_connector.h rename to tunnels/adapters/connector/tcp/tcp_connector.h index 760b1c2c..d0c41c6e 100644 --- a/tunnels/adapters/tcp_connector/tcp_connector.h +++ b/tunnels/adapters/connector/tcp/tcp_connector.h @@ -8,9 +8,7 @@ // -#define NODE_TCP_LISTINER - tunnel_t *newTcpConnector(node_instance_context_t *instance_info); -api_result_t apiTcpConnector(tunnel_t *self, char *msg); +api_result_t apiTcpConnector(tunnel_t *self,const char *msg); tunnel_t *destroyTcpConnector(tunnel_t *self); tunnel_metadata_t getMetadataTcpConnector(); diff --git a/tunnels/adapters/connector/tcp/types.h b/tunnels/adapters/connector/tcp/types.h new file mode 100644 index 00000000..40ac9e48 --- /dev/null +++ b/tunnels/adapters/connector/tcp/types.h @@ -0,0 +1,46 @@ +#pragma once +#include "api.h" + +// enable profile to see how much it takes to connect and downstream write +// #define PROFILE 1 + +enum tcp_connector_dynamic_value_status +{ + kCdvsEmpty = 0x0, + kCdvsConstant, + kCdvsFromSource, + kCdvsFromDest, +}; + +typedef struct tcp_connector_state_s +{ + // settings + bool tcp_no_delay; + bool tcp_fast_open; + bool reuse_addr; + enum domain_strategy domain_strategy; + + size_t dest_domain_len; + enum socket_address_type dest_atype; + dynamic_value_t dest_addr; + dynamic_value_t dest_port; + +} tcp_connector_state_t; + +typedef struct tcp_connector_con_state_s +{ +#ifdef PROFILE + struct timeval __profile_conenct; +#endif + + tunnel_t *tunnel; + line_t * line; + hio_t * io; + + buffer_pool_t * buffer_pool; + context_queue_t *data_queue; + context_queue_t *finished_queue; + + bool write_paused; + bool established; +} tcp_connector_con_state_t; diff --git a/tunnels/adapters/connector/types.h b/tunnels/adapters/connector/types.h index 32b9ffa7..3e36f8f7 100644 --- a/tunnels/adapters/connector/types.h +++ b/tunnels/adapters/connector/types.h @@ -1,34 +1,33 @@ #pragma once #include "api.h" -#define STATE(x) ((connector_state_t *)((x)->state)) -#define CSTATE(x) ((connector_con_state_t *)((((x)->line->chains_state)[self->chain_index]))) +#define STATE(x) ((connector_state_t *) ((x)->state)) +#define CSTATE(x) ((connector_con_state_t *) ((((x)->line->chains_state)[self->chain_index]))) #define CSTATE_MUT(x) ((x)->line->chains_state)[self->chain_index] - // enable profile to see how much it takes to connect and downstream write, dns and etc... // #define PROFILE 1 enum connector_dynamic_value_status { - cdvs_empty = 0x0, - cdvs_constant, - cdvs_from_source, - cdvs_from_dest, + kCdvsEmpty = 0x0, + kCdvsConstant, + kCdvsFromSource, + kCdvsFromDest, }; typedef struct connector_state_s { // settings - bool tcp_no_delay; - bool tcp_fast_open; - bool reuse_addr; + bool tcp_no_delay; + bool tcp_fast_open; + bool reuse_addr; enum domain_strategy domain_strategy; - size_t dest_domain_len; + size_t dest_domain_len; enum socket_address_type dest_atype; - dynamic_value_t dest_addr; - dynamic_value_t dest_port; + dynamic_value_t dest_addr; + dynamic_value_t dest_port; } connector_state_t; @@ -37,13 +36,13 @@ typedef struct connector_con_state_s #ifdef PROFILE struct timeval __profile_conenct; #endif + TunnelFlowRoutine upstream; + TunnelFlowRoutine downstream; + tunnel_t * tunnel; + line_t * line; + hio_t * io; - tunnel_t *tunnel; - line_t *line; - hio_t *io; - hio_t *io_back; - - buffer_pool_t *buffer_pool; + buffer_pool_t * buffer_pool; context_queue_t *data_queue; context_queue_t *finished_queue; @@ -51,17 +50,10 @@ typedef struct connector_con_state_s bool established; } connector_con_state_t; - - bool connectorResolvedomain(socket_context_t *dest); - -void connectorUpStream(tunnel_t *self, context_t *c); -void connectorDownStream(tunnel_t *self, context_t *c); +void connectorTcpUpStream(tunnel_t *self, context_t *c); +void connectorTcpDownStream(tunnel_t *self, context_t *c); void connectorPacketUpStream(tunnel_t *self, context_t *c); void connectorPacketDownStream(tunnel_t *self, context_t *c); - - - - diff --git a/tunnels/adapters/connector/udp/CMakeLists.txt b/tunnels/adapters/connector/udp/CMakeLists.txt new file mode 100644 index 00000000..f8bff61b --- /dev/null +++ b/tunnels/adapters/connector/udp/CMakeLists.txt @@ -0,0 +1,29 @@ + + +add_library(Connector STATIC + connector.c + tcp.c + udp.c + resolve.c + +) + +# target_include_directories(Connector PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) + +#ww api +target_include_directories(Connector PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../../ww) +target_link_libraries(Connector ww) + + +# add dependencies +include(${CMAKE_BINARY_DIR}/cmake/CPM.cmake) + + + + +target_compile_definitions(Connector PRIVATE Connector_VERSION=0.1) + +if(CMAKE_BUILD_TYPE STREQUAL "Debug") +target_compile_definitions(Connector PRIVATE DEBUG=1) + +endif() diff --git a/tunnels/adapters/connector/udp/udp_connector.c b/tunnels/adapters/connector/udp/udp_connector.c new file mode 100644 index 00000000..77a9204b --- /dev/null +++ b/tunnels/adapters/connector/udp/udp_connector.c @@ -0,0 +1,299 @@ +#include "loggers/network_logger.h" +#include "types.h" +#include "utils/sockutils.h" + +static void cleanup(connector_con_state_t *cstate) +{ + connector_state_t *state = STATE(cstate->tunnel); + if (state->dest_addr.status == cdvs_constant) + { + + }else if (state->dest_addr.status > cdvs_constant){ + free(cstate->line->dest_ctx) + } + + free(cstate); +} +static void onUdpRecv(hio_t *io, void *buf, int readbytes) +{ + connector_con_state_t *cstate = (connector_con_state_t *) (hevent_userdata(io)); + + shift_buffer_t *payload = popBuffer(cstate->buffer_pool); + setLen(payload, readbytes); + writeRaw(payload, buf, readbytes); + + tunnel_t *self = (cstate)->tunnel; + line_t *line = (cstate)->line; + + struct sockaddr *destaddr = hio_peeraddr(io); + + enum socket_address_type atype; + + if (destaddr->sa_family == AF_INET6) + { + atype = kSatIPV6; + } + else + { + atype = kSatIPV4; + } + + if (! cstate->established) + { + cstate->established = true; + context_t *est_context = newContext(line); + est_context->est = true; + est_context->src_io = io; + self->packetDownStream(self, est_context); + if (hevent_userdata(io) == NULL) + { + return; + } + } + + context_t *context = newContext(line); + context->src_io = io; + context->payload = payload; + + self->packetDownStream(self, context); +} + +void connectorPacketUpStream(tunnel_t *self, context_t *c) +{ + connector_con_state_t *cstate = CSTATE(c); + + if (c->payload != NULL) + { + unsigned int bytes = bufLen(c->payload); + + if (hio_is_closed(cstate->io)) + { + cleanup(CSTATE(c)); + CSTATE_MUT(c) = NULL; + reuseContextBuffer(c); + goto fail; + } + + size_t nwrite = hio_write(cstate->io, rawBuf(c->payload), bytes); + if (nwrite >= 0 && nwrite < bytes) + { + assert(false); // should not happen + } + + reuseContextBuffer(c); + destroyContext(c); + } + else + { + if (c->init) + { + CSTATE_MUT(c) = malloc(sizeof(connector_con_state_t)); + memset(CSTATE(c), 0, sizeof(connector_con_state_t)); + connector_con_state_t *cstate = CSTATE(c); + + cstate->buffer_pool = buffer_pools[c->line->tid]; + cstate->tunnel = self; + cstate->line = c->line; + cstate->write_paused = false; + cstate->data_queue = NULL; + cstate->finished_queue = NULL; + + // sockaddr_set_ipport(&(dest->addr),"www.gstatic.com",80); + + hloop_t *loop = loops[c->line->tid]; + + sockaddr_u host_addr = {0}; + sockaddr_set_ipport(&host_addr, "0.0.0.0", 0); + + int sockfd = socket(host_addr.sa.sa_family, SOCK_DGRAM, 0); + if (sockfd < 0) + { + LOGE("Connector: socket fd < 0"); + cleanup(CSTATE(c)); + CSTATE_MUT(c) = NULL; + goto fail; + } + +#ifdef OS_UNIX + so_reuseaddr(sockfd, 1); +#endif + sockaddr_u addr; + + sockaddr_set_ipport(&addr, "0.0.0.0", 0); + + if (bind(sockfd, &addr.sa, sockaddr_len(&addr)) < 0) + { + LOGE("UDP bind failed;"); + closesocket(sockfd); + goto fail; + } + + hio_t *upstream_io = hio_get(loop, sockfd); + assert(upstream_io != NULL); + + cstate->io = upstream_io; + hevent_set_userdata(upstream_io, cstate); + hio_setcb_read(upstream_io, onUdpRecv); + hio_read(upstream_io); + + socket_context_t* resolved_dest_context; + // fill the resolved_dest_context address based on settings + { + socket_context_t *src_ctx = &(c->line->src_ctx); + socket_context_t *dest_ctx = &(c->line->dest_ctx); + connector_state_t *state = STATE(self); + + if (state->dest_addr.status == cdvs_from_source) + { + resolved_dest_context = src_ctx; + // copySocketContextAddr(&resolved_dest_context, &src_ctx); + } + else if (state->dest_addr.status == cdvs_from_dest) + { + resolved_dest_context = dest_ctx; + // copySocketContextAddr(&resolved_dest_context, &dest_ctx); + } + else + { + resolved_dest_context.atype = state->dest_atype; + if (state->dest_atype == kSatDomainName) + { + resolved_dest_context.domain = state->dest_addr.value_ptr; + resolved_dest_context.domain_len = state->dest_domain_len; + resolved_dest_context.resolved = false; + } + else + sockaddr_set_ip(&(resolved_dest_context.addr), state->dest_addr.value_ptr); + } + + if (state->dest_port.status == cdvs_from_source) + sockaddr_set_port(&(resolved_dest_context.addr), sockaddr_port(&(src_ctx->addr))); + else if (state->dest_port.status == cdvs_from_dest) + sockaddr_set_port(&(resolved_dest_context.addr), sockaddr_port(&(dest_ctx->addr))); + else + sockaddr_set_port(&(resolved_dest_context.addr), state->dest_port.value); + } + + if (resolved_dest_context.atype == kSatDomainName) + { + if (! resolved_dest_context.resolved) + { + if (! connectorResolvedomain(&(resolved_dest_context))) + { + free(resolved_dest_context.domain); + cleanup(CSTATE(c)); + CSTATE_MUT(c) = NULL; + reuseContextBuffer(c); + goto fail; + } + } + free(resolved_dest_context.domain); + } + hio_set_peeraddr(cstate->io, &(resolved_dest_context.addr.sa), sockaddr_len(&(resolved_dest_context.addr))); + + destroyContext(c); + } + else if (c->fin) + { + hio_t *io = cstate->io; + hevent_set_userdata(io, NULL); + cleanup(CSTATE(c)); + CSTATE_MUT(c) = NULL; + destroyContext(c); + hio_close(io); + } + } + return; +fail:; + self->dw->downStream(self->dw, newFinContext(c->line)); + destroyContext(c); +} + +void connectorPacketDownStream(tunnel_t *self, context_t *c) +{ + if (c->fin) + { + hio_t *io = CSTATE(c)->io; + hevent_set_userdata(io, NULL); + cleanup(CSTATE(c)); + CSTATE_MUT(c) = NULL; + } + self->dw->downStream(self->dw, c); +} + + + +tunnel_t *newTcpConnector(node_instance_context_t *instance_info) +{ + tcp_connector_state_t *state = malloc(sizeof(tcp_connector_state_t)); + memset(state, 0, sizeof(tcp_connector_state_t)); + const cJSON *settings = instance_info->node_settings_json; + + if (! (cJSON_IsObject(settings) && settings->child != NULL)) + { + LOGF("JSON Error: TcpConnector->settings (object field) : The object was empty or invalid"); + return NULL; + } + + const cJSON *tcp_settings = cJSON_GetObjectItemCaseSensitive(settings, "tcp"); + if ((cJSON_IsObject(tcp_settings) && settings->child != NULL)) + { + getBoolFromJsonObject(&(state->tcp_no_delay), tcp_settings, "nodelay"); + getBoolFromJsonObject(&(state->tcp_fast_open), tcp_settings, "fastopen"); + getBoolFromJsonObject(&(state->reuse_addr), tcp_settings, "reuseaddr"); + int ds = 0; + getIntFromJsonObject(&ds, tcp_settings, "domain-strategy"); + state->domain_strategy = ds; + } + else + { + // memset set everything to 0... + } + + state->dest_addr = + parseDynamicStrValueFromJsonObject(settings, "address", 2, "src_context->address", "dest_context->address"); + + if (state->dest_addr.status == kDvsEmpty) + { + LOGF("JSON Error: TcpConnector->settings->address (string field) : The vaule was empty or invalid"); + return NULL; + } + + state->dest_port = + parseDynamicNumericValueFromJsonObject(settings, "port", 2, "src_context->port", "dest_context->port"); + + if (state->dest_port.status == kDvsEmpty) + { + LOGF("JSON Error: TcpConnector->settings->port (number field) : The vaule was empty or invalid"); + return NULL; + } + if (state->dest_addr.status == kDvsConstant) + { + state->dest_atype = getHostAddrType(state->dest_addr.value_ptr); + state->dest_domain_len = strlen(state->dest_addr.value_ptr); + } + + tunnel_t *t = newTunnel(); + t->state = state; + t->upStream = &upStream; + t->downStream = &downStream; + + atomic_thread_fence(memory_order_release); + + return t; +} +api_result_t apiTcpConnector(tunnel_t *self, const char *msg) +{ + (void) (self); + (void) (msg); + return (api_result_t){0}; +} +tunnel_t *destroyTcpConnector(tunnel_t *self) +{ + (void) (self); + return NULL; +} +tunnel_metadata_t getMetadataTcpConnector() +{ + return (tunnel_metadata_t){.version = 0001, .flags = 0x0}; +} \ No newline at end of file diff --git a/tunnels/adapters/connector/udp/udp_connector.h b/tunnels/adapters/connector/udp/udp_connector.h new file mode 100644 index 00000000..da6f52f0 --- /dev/null +++ b/tunnels/adapters/connector/udp/udp_connector.h @@ -0,0 +1,12 @@ +#pragma once +#include "api.h" + +// con <-----\ /-----> Resolve=> Udp Associate +// con <------> Udponnector <------> Resolve=> Udp Associate +// con <-----/ \-----> Resolve=> Udp Associate +// + +tunnel_t * newUdponnector(node_instance_context_t *instance_info); +api_result_t apiUdponnector(tunnel_t *self, const char *msg); +tunnel_t * destroyUdponnector(tunnel_t *self); +tunnel_metadata_t getMetadataUdponnector(); diff --git a/tunnels/adapters/tcp_listener/CMakeLists.txt b/tunnels/adapters/listener/tcp/CMakeLists.txt similarity index 100% rename from tunnels/adapters/tcp_listener/CMakeLists.txt rename to tunnels/adapters/listener/tcp/CMakeLists.txt diff --git a/tunnels/adapters/tcp_listener/tcp_listener.c b/tunnels/adapters/listener/tcp/tcp_listener.c similarity index 96% rename from tunnels/adapters/tcp_listener/tcp_listener.c rename to tunnels/adapters/listener/tcp/tcp_listener.c index 79aad365..f8a4bdaf 100644 --- a/tunnels/adapters/tcp_listener/tcp_listener.c +++ b/tunnels/adapters/listener/tcp/tcp_listener.c @@ -245,22 +245,7 @@ static inline void downStream(tunnel_t *self, context_t *c) } } -static void tcpListenerUpStream(tunnel_t *self, context_t *c) -{ - upStream(self, c); -} -static void tcpListenerPacketUpStream(tunnel_t *self, context_t *c) -{ - upStream(self, c); -} -static void tcpListenerDownStream(tunnel_t *self, context_t *c) -{ - downStream(self, c); -} -static void tcpListenerPacketDownStream(tunnel_t *self, context_t *c) -{ - downStream(self, c); -} + static void on_recv(hio_t *io, void *buf, int readbytes) { @@ -508,10 +493,8 @@ tunnel_t *newTcpListener(node_instance_context_t *instance_info) registerSocketAcceptor(t, filter_opt, on_inbound_connected); - t->upStream = &tcpListenerUpStream; - t->packetUpStream = &tcpListenerPacketUpStream; - t->downStream = &tcpListenerDownStream; - t->packetDownStream = &tcpListenerPacketDownStream; + t->upStream = &upStream; + t->downStream = &downStream; atomic_thread_fence(memory_order_release); return t; diff --git a/tunnels/adapters/listener/tcp/tcp_listener.h b/tunnels/adapters/listener/tcp/tcp_listener.h new file mode 100644 index 00000000..edf95826 --- /dev/null +++ b/tunnels/adapters/listener/tcp/tcp_listener.h @@ -0,0 +1,14 @@ +#pragma once +#include "api.h" + +// user <-----\ /-----> con 1 +// user <------> TcpListener <------> con 2 +// user <-----/ \-----> con 3 +// + +#define NODE_TCP_LISTINER + +tunnel_t * newTcpListener(node_instance_context_t *instance_info); +api_result_t apiTcpListener(tunnel_t *self, char *msg); +tunnel_t * destroyTcpListener(tunnel_t *self); +tunnel_metadata_t getMetadataTcpListener(); diff --git a/tunnels/adapters/tcp_connector/resolve.c b/tunnels/adapters/tcp_connector/resolve.c deleted file mode 100644 index 36eef709..00000000 --- a/tunnels/adapters/tcp_connector/resolve.c +++ /dev/null @@ -1,30 +0,0 @@ -#include "types.h" -#include "loggers/dns_logger.h" - -bool tcpConnectorResolvedomain(socket_context_t *dest) -{ - uint16_t old_port = sockaddr_port(&(dest->addr)); - struct timeval tv1, tv2; - gettimeofday(&tv1, NULL); - /* resolve domain */ - { - if (sockaddr_set_ipport(&(dest->addr), dest->domain, old_port) != 0) - { - LOGE("TcpConnector: resolve failed %s", dest->domain); - return false; - } - else - { - char ip[60]; - sockaddr_str(&(dest->addr), ip, 60); - LOGI("Connector: %s resolved to %s", dest->domain, ip); - } - } - gettimeofday(&tv2, NULL); - - double time_spent = (double)(tv2.tv_usec - tv1.tv_usec) / 1000000 + (double)(tv2.tv_sec - tv1.tv_sec); - LOGD("Connector: dns resolve took %lf sec", time_spent); - dest->resolved = true; - - return true; -} diff --git a/tunnels/adapters/tcp_connector/types.h b/tunnels/adapters/tcp_connector/types.h deleted file mode 100644 index 1767ca3b..00000000 --- a/tunnels/adapters/tcp_connector/types.h +++ /dev/null @@ -1,60 +0,0 @@ -#pragma once -#include "api.h" - -#define STATE(x) ((tcp_connector_state_t *)((x)->state)) -#define CSTATE(x) ((tcp_connector_con_state_t *)((((x)->line->chains_state)[self->chain_index]))) -#define CSTATE_MUT(x) ((x)->line->chains_state)[self->chain_index] - - -// enable profile to see how much it takes to connect and downstream write -// #define PROFILE 1 - -enum tcp_connector_dynamic_value_status -{ - cdvs_empty = 0x0, - cdvs_constant, - cdvs_from_source, - cdvs_from_dest, -}; - -typedef struct tcp_connector_state_s -{ - // settings - bool tcp_no_delay; - bool tcp_fast_open; - bool reuse_addr; - enum domain_strategy domain_strategy; - - size_t dest_domain_len; - enum socket_address_type dest_atype; - dynamic_value_t dest_addr; - dynamic_value_t dest_port; - -} tcp_connector_state_t; - -typedef struct tcp_connector_con_state_s -{ -#ifdef PROFILE - struct timeval __profile_conenct; -#endif - - tunnel_t *tunnel; - line_t *line; - hio_t *io; - hio_t *io_back; - - buffer_pool_t *buffer_pool; - context_queue_t *data_queue; - context_queue_t *finished_queue; - - bool write_paused; - bool established; -} tcp_connector_con_state_t; - - - -bool tcpConnectorResolvedomain(socket_context_t *dest); - - - - diff --git a/tunnels/adapters/tcp_listener/tcp_listener.h b/tunnels/adapters/tcp_listener/tcp_listener.h deleted file mode 100644 index 31a9c603..00000000 --- a/tunnels/adapters/tcp_listener/tcp_listener.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once -#include "api.h" - - -// user <-----\ /-----> con 1 -// user <------> TcpListener <------> con 2 -// user <-----/ \-----> con 3 -// - -#define NODE_TCP_LISTINER - -tunnel_t *newTcpListener(node_instance_context_t *instance_info); -api_result_t apiTcpListener(tunnel_t *self, char *msg); -tunnel_t *destroyTcpListener(tunnel_t *self); -tunnel_metadata_t getMetadataTcpListener(); diff --git a/tunnels/client/header/header_client.c b/tunnels/client/header/header_client.c index 91913cb2..1dfbc21c 100644 --- a/tunnels/client/header/header_client.c +++ b/tunnels/client/header/header_client.c @@ -12,9 +12,9 @@ enum header_dynamic_value_status { - hdvs_empty = 0x0, - hdvs_constant, - hdvs_source_port, + kHdvsEmpty = 0x0, + kHdvsConstant, + kHdvsSourcePort, }; typedef struct header_client_state_s @@ -37,7 +37,7 @@ static void upStream(tunnel_t *self, context_t *c) switch ((enum header_dynamic_value_status)state->data.status) { - case hdvs_source_port: + case kHdvsSourcePort: shiftl(c->payload, sizeof(uint16_t)); writeUI16(c->payload, sockaddr_port(&(c->line->src_ctx.addr))); break; @@ -56,22 +56,7 @@ static inline void downStream(tunnel_t *self, context_t *c) self->dw->downStream(self->dw, c); } -static void headerClientUpStream(tunnel_t *self, context_t *c) -{ - upStream(self, c); -} -static void headerClientPacketUpStream(tunnel_t *self, context_t *c) -{ - upStream(self, c); -} -static void headerClientDownStream(tunnel_t *self, context_t *c) -{ - downStream(self, c); -} -static void headerClientPacketDownStream(tunnel_t *self, context_t *c) -{ - downStream(self, c); -} + tunnel_t *newHeaderClient(node_instance_context_t *instance_info) { @@ -83,18 +68,16 @@ tunnel_t *newHeaderClient(node_instance_context_t *instance_info) "src_context->port"); tunnel_t *t = newTunnel(); t->state = state; - t->upStream = &headerClientUpStream; - t->packetUpStream = &headerClientPacketUpStream; - t->downStream = &headerClientDownStream; - t->packetDownStream = &headerClientPacketDownStream; + t->upStream = &upStream; + t->downStream = &downStream; atomic_thread_fence(memory_order_release); return t; } -api_result_t apiHeaderClient(tunnel_t *self, char *msg) +api_result_t apiHeaderClient(tunnel_t *self, const char *msg) { - (void)(self); (void)(msg); return (api_result_t){0}; // TODO + (void)(self); (void)(msg); return (api_result_t){0}; // TODO(root): } tunnel_t *destroyHeaderClient(tunnel_t *self) diff --git a/tunnels/client/header/header_client.h b/tunnels/client/header/header_client.h index cfa88acd..cb45277f 100644 --- a/tunnels/client/header/header_client.h +++ b/tunnels/client/header/header_client.h @@ -7,7 +7,7 @@ // tunnel_t *newHeaderClient(node_instance_context_t *instance_info); -api_result_t apiHeaderClient(tunnel_t *self, char *msg); +api_result_t apiHeaderClient(tunnel_t *self, const char *msg); tunnel_t *destroyHeaderClient(tunnel_t *self); tunnel_metadata_t getMetadataHeaderClient(); diff --git a/tunnels/client/http2/http2_client.c b/tunnels/client/http2/http2_client.c index 7286811d..fea5d553 100644 --- a/tunnels/client/http2/http2_client.c +++ b/tunnels/client/http2/http2_client.c @@ -318,7 +318,7 @@ static inline void upStream(tunnel_t *self, context_t *c) con->state = H2_SEND_HEADERS; // consumes payload while (trySendRequest(self, con, stream->stream_id, stream->io, c->payload)) - if (!ISALIVE(c)) + if (!isAlive(c->line)) { destroyContext(c); return; @@ -339,7 +339,7 @@ static inline void upStream(tunnel_t *self, context_t *c) { con->init_sent = true; self->up->upStream(self->up, newInitContext(con->line)); - if (!ISALIVE(c)) + if (!isAlive(c->line)) { destroyContext(c); return; @@ -347,7 +347,7 @@ static inline void upStream(tunnel_t *self, context_t *c) } while (trySendRequest(self, con, 0, NULL, NULL)) - if (!ISALIVE(c)) + if (!isAlive(c->line)) { destroyContext(c); return; @@ -366,7 +366,7 @@ static inline void upStream(tunnel_t *self, context_t *c) nghttp2_session_set_stream_user_data(con->session, stream->stream_id, NULL); remove_stream(con, stream); - if (con->root.next == NULL && con->childs_added >= state->concurrency && ISALIVE(c)) + if (con->root.next == NULL && con->childs_added >= state->concurrency && isAlive(c->line)) { context_t *con_fc = newFinContext(con->line); tunnel_t *con_dest = con->tunnel->up; @@ -397,7 +397,7 @@ static inline void downStream(tunnel_t *self, context_t *c) assert(ret == len); reuseContextBuffer(c); - if (!ISALIVE(c)) + if (!isAlive(c->line)) { destroyContext(c); return; @@ -412,13 +412,13 @@ static inline void downStream(tunnel_t *self, context_t *c) } while (trySendRequest(self, con, 0, NULL, NULL)) - if (!ISALIVE(c)) + if (!isAlive(c->line)) { destroyContext(c); return; } - if (con->root.next == NULL && con->childs_added >= state->concurrency && ISALIVE(c)) + if (con->root.next == NULL && con->childs_added >= state->concurrency && isAlive(c->line)) { context_t *con_fc = newFinContext(con->line); tunnel_t *con_dest = con->tunnel->up; @@ -439,26 +439,7 @@ static inline void downStream(tunnel_t *self, context_t *c) destroyContext(c); } } - -static void http2ClientUpStream(tunnel_t *self, context_t *c) -{ - upStream(self, c); -} -static void http2ClientPacketUpStream(tunnel_t *self, context_t *c) -{ - LOGF("Http2Client: Http2 protocol dose not run on udp"); - exit(1); -} -static void http2ClientDownStream(tunnel_t *self, context_t *c) -{ - downStream(self, c); -} -static void http2ClientPacketDownStream(tunnel_t *self, context_t *c) -{ - LOGF("Http2Client: Http2 protocol dose not run on udp"); - exit(1); -} - + tunnel_t *newHttp2Client(node_instance_context_t *instance_info) { http2_client_state_t *state = malloc(sizeof(http2_client_state_t) + (workers_count * sizeof(thread_connection_pool_t))); @@ -507,18 +488,15 @@ tunnel_t *newHttp2Client(node_instance_context_t *instance_info) tunnel_t *t = newTunnel(); t->state = state; - t->upStream = &http2ClientUpStream; - t->packetUpStream = &http2ClientPacketUpStream; - t->downStream = &http2ClientDownStream; - t->packetDownStream = &http2ClientPacketDownStream; - + t->upStream = &upStream; + t->downStream = &downStream; atomic_thread_fence(memory_order_release); return t; } api_result_t apiHttp2Client(tunnel_t *self, char *msg) { - (void)(self); (void)(msg); return (api_result_t){0}; // TODO + (void)(self); (void)(msg); return (api_result_t){0}; } tunnel_t *destroyHttp2Client(tunnel_t *self) diff --git a/tunnels/client/openssl/openssl_client.c b/tunnels/client/openssl/openssl_client.c index c20cf29a..9e4760aa 100644 --- a/tunnels/client/openssl/openssl_client.c +++ b/tunnels/client/openssl/openssl_client.c @@ -80,7 +80,7 @@ static void flush_write_queue(tunnel_t *self, context_t *c) { self->upStream(self, contextQueuePop(cstate->queue)); - if (!ISALIVE(c)) + if (!isAlive(c->line)) return; } } @@ -124,7 +124,7 @@ static inline void upStream(tunnel_t *self, context_t *c) context_t *send_context = newContextFrom(c); send_context->payload = buf; self->up->upStream(self->up, send_context); - if (!ISALIVE(c)) + if (!isAlive(c->line)) { reuseContextBuffer(c); destroyContext(c); @@ -281,7 +281,7 @@ static inline void downStream(tunnel_t *self, context_t *c) context_t *req_cont = newContextFrom(c); req_cont->payload = buf; self->up->upStream(self->up, req_cont); - if (!ISALIVE(c)) + if (!isAlive(c->line)) { reuseContextBuffer(c); destroyContext(c); @@ -338,7 +338,7 @@ static inline void downStream(tunnel_t *self, context_t *c) context_t *dw_est_ctx = newContextFrom(c); dw_est_ctx->est = true; self->dw->downStream(self->dw, dw_est_ctx); - if (!ISALIVE(c)) + if (!isAlive(c->line)) { LOGW("OpensslClient: prev node instantly closed the est with fin"); reuseContextBuffer(c); @@ -377,7 +377,7 @@ static inline void downStream(tunnel_t *self, context_t *c) context_t *data_ctx = newContextFrom(c); data_ctx->payload = buf; self->dw->downStream(self->dw, data_ctx); - if (!ISALIVE(c)) + if (!isAlive(c->line)) { reuseContextBuffer(c); destroyContext(c); @@ -428,22 +428,7 @@ failed:; return; } -static void openSSLUpStream(tunnel_t *self, context_t *c) -{ - upStream(self, c); -} -static void openSSLPacketUpStream(tunnel_t *self, context_t *c) -{ - upStream(self, c); // TODO : DTLS -} -static void openSSLDownStream(tunnel_t *self, context_t *c) -{ - downStream(self, c); -} -static void openSSLPacketDownStream(tunnel_t *self, context_t *c) -{ - downStream(self, c); -} + tunnel_t *newOpenSSLClient(node_instance_context_t *instance_info) { @@ -513,11 +498,8 @@ tunnel_t *newOpenSSLClient(node_instance_context_t *instance_info) tunnel_t *t = newTunnel(); t->state = state; - - t->upStream = &openSSLUpStream; - t->packetUpStream = &openSSLPacketUpStream; - t->downStream = &openSSLDownStream; - t->packetDownStream = &openSSLPacketDownStream; + t->upStream = &upStream; + t->downStream = &downStream; atomic_thread_fence(memory_order_release); return t; } diff --git a/tunnels/client/preconnect/helpers.h b/tunnels/client/preconnect/helpers.h index 17fdb1a9..dcf54906 100644 --- a/tunnels/client/preconnect/helpers.h +++ b/tunnels/client/preconnect/helpers.h @@ -1,5 +1,6 @@ #pragma once #include "types.h" +#include "utils/mathutils.h" #include "loggers/network_logger.h" #define STATE(x) ((preconnect_client_state_t *)((x)->state)) @@ -7,10 +8,6 @@ #define CSTATE_MUT(x) ((x)->line->chains_state)[self->chain_index] #define ISALIVE(x) (((((x)->line->chains_state)[self->chain_index])) != NULL) #define PRECONNECT_DELAY 100 -#undef max -#undef min -static inline size_t min(size_t x, size_t y) { return (((x) < (y)) ? (x) : (y)); } -static inline size_t max(size_t x, size_t y) { return (((x) < (y)) ? (y) : (x)); } static void add_connection(thread_box_t *box, preconnect_client_con_state_t *con) diff --git a/tunnels/client/preconnect/preconnect_client.c b/tunnels/client/preconnect/preconnect_client.c index 1f1f36a6..0a22f51e 100644 --- a/tunnels/client/preconnect/preconnect_client.c +++ b/tunnels/client/preconnect/preconnect_client.c @@ -1,8 +1,8 @@ #include "preconnect_client.h" -#include "managers/node_manager.h" +#include "helpers.h" #include "loggers/network_logger.h" +#include "managers/node_manager.h" #include "types.h" -#include "helpers.h" static inline void upStream(tunnel_t *self, context_t *c) { @@ -31,8 +31,8 @@ static inline void upStream(tunnel_t *self, context_t *c) } else { - const unsigned int tid = c->line->tid; - thread_box_t *this_tb = &(state->workers[tid]); + const unsigned int tid = c->line->tid; + thread_box_t * this_tb = &(state->workers[tid]); if (c->init) { @@ -43,8 +43,8 @@ static inline void upStream(tunnel_t *self, context_t *c) preconnect_client_con_state_t *ucon = this_tb->root.next; remove_connection(this_tb, ucon); - ucon->d = c->line; - ucon->mode = connected_pair; + ucon->d = c->line; + ucon->mode = connected_pair; CSTATE_MUT(c) = ucon; self->dw->downStream(self->dw, newEstContext(c->line)); initiateConnect(self); @@ -53,8 +53,8 @@ static inline void upStream(tunnel_t *self, context_t *c) { atomic_fetch_add_explicit(&(state->active_cons), 1, memory_order_relaxed); preconnect_client_con_state_t *dcon = create_cstate(c->line->tid); - CSTATE_MUT(c) = dcon; - dcon->mode = connected_direct; + CSTATE_MUT(c) = dcon; + dcon->mode = connected_direct; self->up->upStream(self->up, c); return; } @@ -63,7 +63,7 @@ static inline void upStream(tunnel_t *self, context_t *c) else if (c->fin) { preconnect_client_con_state_t *dcon = CSTATE(c); - CSTATE_MUT(c) = NULL; + CSTATE_MUT(c) = NULL; atomic_fetch_add_explicit(&(state->active_cons), -1, memory_order_relaxed); switch (dcon->mode) @@ -74,11 +74,11 @@ static inline void upStream(tunnel_t *self, context_t *c) break; case connected_pair:; - line_t *u_line = dcon->u; + line_t *u_line = dcon->u; (dcon->u->chains_state)[self->chain_index] = NULL; - context_t * fctx = switchLine(c, u_line); // created here to prevent destruction of line + context_t *fctx = switchLine(c, u_line); // created here to prevent destruction of line destroy_cstate(dcon); - self->up->upStream(self->up,fctx); + self->up->upStream(self->up, fctx); break; case notconnected: default: @@ -120,9 +120,9 @@ static inline void downStream(tunnel_t *self, context_t *c) } else { - const unsigned int tid = c->line->tid; - thread_box_t *this_tb = &(state->workers[tid]); - preconnect_client_con_state_t *ucon = CSTATE(c); + const unsigned int tid = c->line->tid; + thread_box_t * this_tb = &(state->workers[tid]); + preconnect_client_con_state_t *ucon = CSTATE(c); if (c->fin) { @@ -138,7 +138,7 @@ static inline void downStream(tunnel_t *self, context_t *c) case connected_pair:; atomic_fetch_add_explicit(&(state->active_cons), -1, memory_order_relaxed); - line_t *d_line = ucon->d; + line_t *d_line = ucon->d; (ucon->d->chains_state)[self->chain_index] = NULL; destroy_cstate(ucon); self->dw->downStream(self->dw, switchLine(c, d_line)); @@ -179,22 +179,7 @@ static inline void downStream(tunnel_t *self, context_t *c) } } } -static void preConnectClientUpStream(tunnel_t *self, context_t *c) -{ - upStream(self, c); -} -static void preConnectClientPacketUpStream(tunnel_t *self, context_t *c) -{ - upStream(self, c); -} -static void preConnectClientDownStream(tunnel_t *self, context_t *c) -{ - downStream(self, c); -} -static void preConnectClientPacketDownStream(tunnel_t *self, context_t *c) -{ - downStream(self, c); -} + static void start_preconnect(htimer_t *timer) { tunnel_t *t = hevent_userdata(timer); @@ -214,21 +199,20 @@ tunnel_t *newPreConnectClient(node_instance_context_t *instance_info) { const size_t start_delay_ms = 150; - preconnect_client_state_t *state = malloc(sizeof(preconnect_client_state_t) + (workers_count * sizeof(thread_box_t))); + preconnect_client_state_t *state = + malloc(sizeof(preconnect_client_state_t) + (workers_count * sizeof(thread_box_t))); memset(state, 0, sizeof(preconnect_client_state_t) + (workers_count * sizeof(thread_box_t))); const cJSON *settings = instance_info->node_settings_json; getIntFromJsonObject(&(state->min_unused_cons), settings, "minimum-unused"); - state->min_unused_cons = min(max(workers_count * 2, state->min_unused_cons), 9999999); - state->connection_per_thread = min(4,state->min_unused_cons / workers_count); + state->min_unused_cons = min(max(workers_count * 2, state->min_unused_cons), 9999999); + state->connection_per_thread = min(4, state->min_unused_cons / workers_count); - tunnel_t *t = newTunnel(); - t->state = state; - t->upStream = &preConnectClientUpStream; - t->packetUpStream = &preConnectClientPacketUpStream; - t->downStream = &preConnectClientDownStream; - t->packetDownStream = &preConnectClientPacketDownStream; + tunnel_t *t = newTunnel(); + t->state = state; + t->upStream = &upStream; + t->downStream = &downStream; htimer_t *start_timer = htimer_add(loops[0], start_preconnect, start_delay_ms, 1); hevent_set_userdata(start_timer, t); @@ -240,7 +224,9 @@ tunnel_t *newPreConnectClient(node_instance_context_t *instance_info) api_result_t apiPreConnectClient(tunnel_t *self, char *msg) { - (void)(self); (void)(msg); return (api_result_t){0}; // TODO + (void) (self); + (void) (msg); + return (api_result_t){0}; // TODO } tunnel_t *destroyPreConnectClient(tunnel_t *self) diff --git a/tunnels/client/preconnect/preconnect_client.h b/tunnels/client/preconnect/preconnect_client.h index 2babb67c..e85d6d33 100644 --- a/tunnels/client/preconnect/preconnect_client.h +++ b/tunnels/client/preconnect/preconnect_client.h @@ -7,7 +7,7 @@ // tunnel_t *newPreConnectClient(node_instance_context_t *instance_info); -api_result_t apiPreConnectClient(tunnel_t *self, char *msg); +api_result_t apiPreConnectClient(tunnel_t *self, const char *msg); tunnel_t *destroyPreConnectClient(tunnel_t *self); tunnel_metadata_t getMetadataPreConnectClient(); diff --git a/tunnels/client/protobuf/protobuf_client.c b/tunnels/client/protobuf/protobuf_client.c index bcb02cce..882eaf8d 100644 --- a/tunnels/client/protobuf/protobuf_client.c +++ b/tunnels/client/protobuf/protobuf_client.c @@ -11,7 +11,7 @@ // #include "packet.pb.h" #include "uleb128.h" -#define MAX_PACKET_SIZE 65536 * 16 +#define MAX_PACKET_SIZE (65536 * 16) #define STATE(x) ((protobuf_client_state_t *)((x)->state)) #define CSTATE(x) ((protobuf_client_con_state_t *)((((x)->line->chains_state)[self->chain_index]))) @@ -41,8 +41,9 @@ static void process(tunnel_t *self, context_t *cin) { protobuf_client_con_state_t *cstate = CSTATE(cin); buffer_stream_t *bstream = cstate->stream_buf; - if (bufferStreamLen(bstream) < cstate->wanted || cstate->wanted <= 0) + if (bufferStreamLen(bstream) < cstate->wanted || cstate->wanted <= 0) { return; +} context_t *c = newContextFrom(cin); c->payload = bufferStreamRead(bstream, cstate->wanted); @@ -103,9 +104,8 @@ static inline void downStream(tunnel_t *self, context_t *c) destroyContext(c); return; } - else - { - + + shift_buffer_t *buf = c->payload; if (bufLen(buf) < 2) { @@ -140,11 +140,12 @@ static inline void downStream(tunnel_t *self, context_t *c) bufferStreamPush(cstate->stream_buf, c->payload); c->payload = NULL; if (data_len >= bufLen(buf)) - process(self, c); + { process(self, c); +} destroyContext(c); return; } - } + } else { @@ -158,40 +159,22 @@ static inline void downStream(tunnel_t *self, context_t *c) self->dw->downStream(self->dw, c); } -static void protoBufClientUpStream(tunnel_t *self, context_t *c) -{ - upStream(self, c); -} -static void protoBufClientPacketUpStream(tunnel_t *self, context_t *c) -{ - upStream(self, c); -} -static void protoBufClientDownStream(tunnel_t *self, context_t *c) -{ - downStream(self, c); -} -static void protoBufClientPacketDownStream(tunnel_t *self, context_t *c) -{ - downStream(self, c); -} tunnel_t *newProtoBufClient(node_instance_context_t *instance_info) { tunnel_t *t = newTunnel(); - t->upStream = &protoBufClientUpStream; - t->packetUpStream = &protoBufClientPacketUpStream; - t->downStream = &protoBufClientDownStream; - t->packetDownStream = &protoBufClientPacketDownStream; + t->upStream = &upStream; + t->downStream = &downStream; atomic_thread_fence(memory_order_release); return t; } -api_result_t apiProtoBufClient(tunnel_t *self, char *msg) +api_result_t apiProtoBufClient(tunnel_t *self, const char *msg) { - (void)(self); (void)(msg); return (api_result_t){0}; // TODO + (void)(self); (void)(msg); return (api_result_t){0}; // TODO(root): } tunnel_t *destroyProtoBufClient(tunnel_t *self) diff --git a/tunnels/client/protobuf/protobuf_client.h b/tunnels/client/protobuf/protobuf_client.h index a786dfec..dbe5a5bb 100644 --- a/tunnels/client/protobuf/protobuf_client.h +++ b/tunnels/client/protobuf/protobuf_client.h @@ -6,7 +6,7 @@ // <---- decode <---- tunnel_t *newProtoBufClient(node_instance_context_t *instance_info); -api_result_t apiProtoBufClient(tunnel_t *self, char *msg); +api_result_t apiProtoBufClient(tunnel_t *self, const char *msg); tunnel_t *destroyProtoBufClient(tunnel_t *self); tunnel_metadata_t getMetadataProtoBufClient(); diff --git a/tunnels/client/reverse/helpers.h b/tunnels/client/reverse/helpers.h index 02e0325c..18064bd9 100644 --- a/tunnels/client/reverse/helpers.h +++ b/tunnels/client/reverse/helpers.h @@ -1,5 +1,6 @@ #pragma once #include "types.h" +#include "utils/mathutils.h" #include "loggers/network_logger.h" #define STATE(x) ((reverse_client_state_t *)((x)->state)) @@ -10,12 +11,8 @@ #define ISALIVE(x) (((((x)->line->chains_state)[state->chain_index_pi])) != NULL) #define PRECONNECT_DELAY_SHORT 10 #define PRECONNECT_DELAY_HIGH 1500 -#undef max -#undef min -static inline size_t min(size_t x, size_t y) { return (((x) < (y)) ? (x) : (y)); } -static inline size_t max(size_t x, size_t y) { return (((x) < (y)) ? (y) : (x)); } -static reverse_client_con_state_t *create_cstate(int tid) +static reverse_client_con_state_t *createCstate(int tid) { reverse_client_con_state_t *cstate = malloc(sizeof(reverse_client_con_state_t)); memset(cstate, 0, sizeof(reverse_client_con_state_t)); @@ -28,40 +25,41 @@ static reverse_client_con_state_t *create_cstate(int tid) return cstate; } -static void destroy_cstate(reverse_client_con_state_t *cstate) +static void destroyCstate(reverse_client_con_state_t *cstate) { destroyLine(cstate->u); destroyLine(cstate->d); free(cstate); } -static void do_connect(struct connect_arg *cg) +static void doConnect(struct connect_arg *cg) { tunnel_t *self = cg->t; reverse_client_state_t *state = STATE(self); - reverse_client_con_state_t *cstate = create_cstate(cg->tid); + reverse_client_con_state_t *cstate = createCstate(cg->tid); free(cg); (cstate->u->chains_state)[state->chain_index_pi] = cstate; (cstate->d->chains_state)[state->chain_index_pi] = cstate; self->up->upStream(self->up, newInitContext(cstate->u)); } -static void connect_timer_finished(htimer_t *timer) +static void connectTimerFinished(htimer_t *timer) { - do_connect(hevent_userdata(timer)); + doConnect(hevent_userdata(timer)); htimer_del(timer); } -static void before_connect(hevent_t *ev) +static void beforeConnect(hevent_t *ev) { struct connect_arg *cg = hevent_userdata(ev); - htimer_t *connect_timer = htimer_add(loops[cg->tid], connect_timer_finished, cg->delay, 1); + htimer_t *connect_timer = htimer_add(loops[cg->tid], connectTimerFinished, cg->delay, 1); hevent_set_userdata(connect_timer, cg); } static void initiateConnect(tunnel_t *t, int tid) { - if (STATE(t)->unused_cons[tid] >= STATE(t)->connection_per_thread) + if (STATE(t)->unused_cons[tid] >= STATE(t)->connection_per_thread) { return; +} bool more_delay = STATE(t)->unused_cons[tid] <= 0; // STATE(t)->unused_cons[tid] += 1; @@ -81,7 +79,7 @@ static void initiateConnect(tunnel_t *t, int tid) hevent_t ev; memset(&ev, 0, sizeof(ev)); ev.loop = worker_loop; - ev.cb = before_connect; + ev.cb = beforeConnect; struct connect_arg *cg = malloc(sizeof(struct connect_arg)); cg->t = t; cg->tid = tid; diff --git a/tunnels/client/reverse/reverse_client.c b/tunnels/client/reverse/reverse_client.c index a9c1c756..ab8cd8a9 100644 --- a/tunnels/client/reverse/reverse_client.c +++ b/tunnels/client/reverse/reverse_client.c @@ -61,14 +61,16 @@ static inline void downStream(tunnel_t *self, context_t *c) turned->first = true; self->dw->downStream(self->dw, turned); } - else + else { self->dw->downStream(self->dw, switchLine(c, ucstate->d)); +} } else { ucstate->pair_connected = true; - if (state->unused_cons[tid] > 0) + if (state->unused_cons[tid] > 0) { state->unused_cons[tid] -= 1; +} atomic_fetch_add_explicit(&(state->reverse_cons), 1, memory_order_relaxed); self->dw->downStream(self->dw, newInitContext(ucstate->d)); @@ -93,13 +95,12 @@ static inline void downStream(tunnel_t *self, context_t *c) destroyContext(c); return; } - else - { - ucstate->first_sent_d = true; + + ucstate->first_sent_d = true; c->first = true; self->dw->downStream(self->dw, switchLine(c, ucstate->d)); initiateConnect(self, tid); - } + } } else @@ -122,8 +123,9 @@ static inline void downStream(tunnel_t *self, context_t *c) else { destroy_cstate(ucstate); - if (state->unused_cons[tid] > 0) + if (state->unused_cons[tid] > 0) { state->unused_cons[tid] -= 1; +} LOGD("ReverseClient: disconnected, tid: %d unused: %u active: %d", tid, state->unused_cons[tid], atomic_load_explicit(&(state->reverse_cons), memory_order_relaxed)); initiateConnect(self, tid); @@ -146,24 +148,9 @@ static inline void downStream(tunnel_t *self, context_t *c) } } } -static void reverseClientUpStream(tunnel_t *self, context_t *c) -{ - upStream(self, c); -} -static void reverseClientPacketUpStream(tunnel_t *self, context_t *c) -{ - upStream(self, c); -} -static void reverseClientDownStream(tunnel_t *self, context_t *c) -{ - downStream(self, c); -} -static void reverseClientPacketDownStream(tunnel_t *self, context_t *c) -{ - downStream(self, c); -} -static void start_reverse_celint(htimer_t *timer) + +static void startReverseCelint(htimer_t *timer) { tunnel_t *t = hevent_userdata(timer); for (int i = 0; i < workers_count; i++) @@ -202,12 +189,9 @@ tunnel_t *newReverseClient(node_instance_context_t *instance_info) tunnel_t *t = newTunnel(); t->state = state; - t->upStream = &reverseClientUpStream; - t->packetUpStream = &reverseClientPacketUpStream; - t->downStream = &reverseClientDownStream; - t->packetDownStream = &reverseClientPacketDownStream; - - htimer_t *start_timer = htimer_add(loops[0], start_reverse_celint, start_delay_ms, 1); + t->upStream = &upStream; + t->downStream = &downStream; + htimer_t *start_timer = htimer_add(loops[0], startReverseCelint, start_delay_ms, 1); hevent_set_userdata(start_timer, t); atomic_thread_fence(memory_order_release); @@ -215,9 +199,9 @@ tunnel_t *newReverseClient(node_instance_context_t *instance_info) return t; } -api_result_t apiReverseClient(tunnel_t *self, char *msg) +api_result_t apiReverseClient(tunnel_t *self, const char *msg) { - (void)(self); (void)(msg); return (api_result_t){0}; // TODO + (void)(self); (void)(msg); return (api_result_t){0}; // TODO(root): } tunnel_t *destroyReverseClient(tunnel_t *self) diff --git a/tunnels/client/reverse/reverse_client.h b/tunnels/client/reverse/reverse_client.h index aab2afbc..9482ec12 100644 --- a/tunnels/client/reverse/reverse_client.h +++ b/tunnels/client/reverse/reverse_client.h @@ -7,6 +7,6 @@ tunnel_t *newReverseClient(node_instance_context_t *instance_info); -api_result_t apiReverseClient(tunnel_t *self, char *msg); +api_result_t apiReverseClient(tunnel_t *self, const char *msg); tunnel_t *destroyReverseClient(tunnel_t *self); tunnel_metadata_t getMetadataReverseClient(); diff --git a/tunnels/client/wolfssl/wolfssl_client.c b/tunnels/client/wolfssl/wolfssl_client.c index 9e43a03c..9ec6fbd3 100644 --- a/tunnels/client/wolfssl/wolfssl_client.c +++ b/tunnels/client/wolfssl/wolfssl_client.c @@ -83,7 +83,7 @@ static void flush_write_queue(tunnel_t *self, context_t *c) { self->upStream(self, contextQueuePop(cstate->queue)); - if (!ISALIVE(c)) + if (!isAlive(c->line)) return; } } @@ -127,7 +127,7 @@ static inline void upStream(tunnel_t *self, context_t *c) context_t *send_context = newContextFrom(c); send_context->payload = buf; self->up->upStream(self->up, send_context); - if (!ISALIVE(c)) + if (!isAlive(c->line)) { reuseContextBuffer(c); destroyContext(c); @@ -284,7 +284,7 @@ static inline void downStream(tunnel_t *self, context_t *c) context_t *req_cont = newContextFrom(c); req_cont->payload = buf; self->up->upStream(self->up, req_cont); - if (!ISALIVE(c)) + if (!isAlive(c->line)) { reuseContextBuffer(c); destroyContext(c); @@ -341,7 +341,7 @@ static inline void downStream(tunnel_t *self, context_t *c) context_t *dw_est_ctx = newContextFrom(c); dw_est_ctx->est = true; self->dw->downStream(self->dw, dw_est_ctx); - if (!ISALIVE(c)) + if (!isAlive(c->line)) { LOGW("OpensslClient: prev node instantly closed the est with fin"); reuseContextBuffer(c); @@ -380,7 +380,7 @@ static inline void downStream(tunnel_t *self, context_t *c) context_t *data_ctx = newContextFrom(c); data_ctx->payload = buf; self->dw->downStream(self->dw, data_ctx); - if (!ISALIVE(c)) + if (!isAlive(c->line)) { reuseContextBuffer(c); destroyContext(c); diff --git a/tunnels/logger/logger_tunnel.c b/tunnels/logger/logger_tunnel.c index 55648fb3..318b2dec 100644 --- a/tunnels/logger/logger_tunnel.c +++ b/tunnels/logger/logger_tunnel.c @@ -2,13 +2,16 @@ #include "buffer_pool.h" #include "loggers/network_logger.h" -#define STATE(x) ((logger_tunnel_state_t *)((x)->state)) -#define CSTATE(x) ((logger_tunnel_con_state_t *)((((x)->line->chains_state)[self->chain_index]))) +#define STATE(x) ((logger_tunnel_state_t *) ((x)->state)) +#define CSTATE(x) ((logger_tunnel_con_state_t *) ((((x)->line->chains_state)[self->chain_index]))) #define CSTATE_MUT(x) ((x)->line->chains_state)[self->chain_index] -#define ISALIVE(x) (CSTATE(x) != NULL) +#define ISALIVE(x) (CSTATE(x) != NULL) #undef min -static inline size_t min(size_t x, size_t y) { return (((x) < (y)) ? (x) : (y)); } +static inline size_t min(size_t x, size_t y) +{ + return (((x) < (y)) ? (x) : (y)); +} typedef struct logger_tunnel_state_s { @@ -37,10 +40,10 @@ static inline void upStream(tunnel_t *self, context_t *c) // send back something { context_t *reply = newContextFrom(c); - reply->payload = popBuffer(buffer_pools[c->line->tid]); + reply->payload = popBuffer(buffer_pools[c->line->tid]); reuseContextBuffer(c); destroyContext(c); - sprintf((char*)rawBuf(reply->payload), "%s", "salam"); + sprintf((char *) rawBuf(reply->payload), "%s", "salam"); setLen(reply->payload, strlen("salam")); self->dw->downStream(self->dw, reply); } @@ -96,8 +99,8 @@ static inline void downStream(tunnel_t *self, context_t *c) // send back something { context_t *reply = newContextFrom(c); - reply->payload = popBuffer(buffer_pools[c->line->tid]); - sprintf((char*)rawBuf(reply->payload), "%s", "salam"); + reply->payload = popBuffer(buffer_pools[c->line->tid]); + sprintf((char *) rawBuf(reply->payload), "%s", "salam"); setLen(reply->payload, strlen("salam")); self->up->upStream(self->up, reply); } @@ -118,7 +121,7 @@ static inline void downStream(tunnel_t *self, context_t *c) else { context_t *reply = newContextFrom(c); - reply->est = true; + reply->est = true; destroyContext(c); self->up->upStream(self->up, reply); } @@ -142,38 +145,20 @@ static inline void downStream(tunnel_t *self, context_t *c) } } -static void loggerTunnelUpStream(tunnel_t *self, context_t *c) -{ - upStream(self, c); -} -static void loggerTunnelPacketUpStream(tunnel_t *self, context_t *c) -{ - upStream(self, c); -} -static void loggerTunnelDownStream(tunnel_t *self, context_t *c) -{ - downStream(self, c); -} -static void loggerTunnelPacketDownStream(tunnel_t *self, context_t *c) -{ - downStream(self, c); -} - tunnel_t *newLoggerTunnel(node_instance_context_t *instance_info) { - tunnel_t *t = newTunnel(); - - t->upStream = &loggerTunnelUpStream; - t->packetUpStream = &loggerTunnelPacketUpStream; - t->downStream = &loggerTunnelDownStream; - t->packetDownStream = &loggerTunnelPacketDownStream; + tunnel_t *t = newTunnel(); + t->upStream = &upStream; + t->downStream = &downStream; return t; } api_result_t apiLoggerTunnel(tunnel_t *self, char *msg) { - (void)(self); (void)(msg); return (api_result_t){0}; // TODO + (void) (self); + (void) (msg); + return (api_result_t){0}; // TODO } tunnel_t *destroyLoggerTunnel(tunnel_t *self) diff --git a/tunnels/logger/logger_tunnel.h b/tunnels/logger/logger_tunnel.h index 52b7de58..9cd96cb9 100644 --- a/tunnels/logger/logger_tunnel.h +++ b/tunnels/logger/logger_tunnel.h @@ -7,6 +7,6 @@ // tunnel_t *newLoggerTunnel(node_instance_context_t *instance_info); -api_result_t apiLoggerTunnel(tunnel_t *self, char *msg); +api_result_t apiLoggerTunnel(tunnel_t *self, const char *msg); tunnel_t *destroyLoggerTunnel(tunnel_t *self); tunnel_metadata_t getMetadataLoggerTunnel(); diff --git a/tunnels/server/boringssl/boringssl_server.c b/tunnels/server/boringssl/boringssl_server.c index cde64819..e6bd5d34 100644 --- a/tunnels/server/boringssl/boringssl_server.c +++ b/tunnels/server/boringssl/boringssl_server.c @@ -141,7 +141,7 @@ // context_t *answer = newContext(c->line); // answer->payload = buf; // self->dw->downStream(self->dw, answer); -// if (!ISALIVE(c)) +// if (!isAlive(c->line)) // { // reuseContextBuffer(c); // destroyContext(c); @@ -182,7 +182,7 @@ // up_init_ctx->src_io = c->src_io; // self->up->upStream(self->up, up_init_ctx); -// if (!ISALIVE(c)) +// if (!isAlive(c->line)) // { // LOGW("Openssl server: next node instantly closed the init with fin"); // reuseContextBuffer(c); @@ -221,7 +221,7 @@ // cstate->first_sent = true; // } // self->up->upStream(self->up, up_ctx); -// if (!ISALIVE(c)) +// if (!isAlive(c->line)) // { // reuseContextBuffer(c); // destroyContext(c); @@ -250,7 +250,7 @@ // context_t *answer = newContext(c->line); // answer->payload = buf; // self->dw->downStream(self->dw, answer); -// if (!ISALIVE(c)) +// if (!isAlive(c->line)) // { // reuseContextBuffer(c); // destroyContext(c); @@ -372,7 +372,7 @@ // dw_context->payload = buf; // dw_context->src_io = c->src_io; // self->dw->downStream(self->dw, dw_context); -// if (!ISALIVE(c)) +// if (!isAlive(c->line)) // { // reuseContextBuffer(c); // destroyContext(c); diff --git a/tunnels/server/header/header_server.c b/tunnels/server/header/header_server.c index f7e43d24..4cdb25a9 100644 --- a/tunnels/server/header/header_server.c +++ b/tunnels/server/header/header_server.c @@ -3,18 +3,11 @@ #include "hv/hsocket.h" #include "loggers/network_logger.h" -#define MAX_PACKET_SIZE 65535 - -#define STATE(x) ((header_server_state_t *)((x)->state)) -#define CSTATE(x) ((header_server_con_state_t *)((((x)->line->chains_state)[self->chain_index]))) -#define CSTATE_MUT(x) ((x)->line->chains_state)[self->chain_index] -#define ISALIVE(x) (CSTATE(x) != NULL) - enum header_dynamic_value_status { - hdvs_empty = 0x0, - hdvs_constant, - hdvs_dest_port, + kHdvsEmpty = 0x0, + kHdvsConstant, + kHdvsDestPort, }; typedef struct header_server_state_s @@ -31,8 +24,8 @@ typedef struct header_server_con_state_s static void upStream(tunnel_t *self, context_t *c) { - header_server_state_t *state = STATE(self); - + header_server_state_t * state = STATE(self); + header_server_con_state_t *cstate = CSTATE(c); if (c->payload != NULL) { if (c->first) @@ -48,9 +41,9 @@ static void upStream(tunnel_t *self, context_t *c) } uint16_t port = 0; - switch ((enum header_dynamic_value_status)state->data.status) + switch ((enum header_dynamic_value_status) state->data.status) { - case hdvs_dest_port: + case kHdvsConstant: readUI16(buf, &port); sockaddr_set_port(&(c->line->dest_ctx.addr), port); @@ -66,13 +59,12 @@ static void upStream(tunnel_t *self, context_t *c) break; default: - (void)(0); break; } - CSTATE(c)->init_sent = true; + cstate->init_sent = true; self->up->upStream(self->up, newInitContext(c->line)); - if (!ISALIVE(c)) + if (! isAlive(c->line)) { reuseContextBuffer(c); destroyContext(c); @@ -82,21 +74,25 @@ static void upStream(tunnel_t *self, context_t *c) } else if (c->init) { - header_server_con_state_t *cstate = malloc(sizeof(header_server_con_state_t)); + cstate = malloc(sizeof(header_server_con_state_t)); cstate->init_sent = false; - CSTATE_MUT(c) = cstate; + CSTATE_MUT(c) = cstate; destroyContext(c); return; } else if (c->fin) { - bool send_fin = CSTATE(c)->init_sent; - free(CSTATE(c)); + bool send_fin = cstate->init_sent; + free(cstate); CSTATE_MUT(c) = NULL; if (send_fin) + { self->up->upStream(self->up, c); + } else + { destroyContext(c); + } return; } @@ -115,49 +111,32 @@ static inline void downStream(tunnel_t *self, context_t *c) self->dw->downStream(self->dw, c); } -static void headerServerUpStream(tunnel_t *self, context_t *c) -{ - upStream(self, c); -} -static void headerServerPacketUpStream(tunnel_t *self, context_t *c) -{ - upStream(self, c); -} -static void headerServerDownStream(tunnel_t *self, context_t *c) -{ - downStream(self, c); -} -static void headerServerPacketDownStream(tunnel_t *self, context_t *c) -{ - downStream(self, c); -} - tunnel_t *newHeaderServer(node_instance_context_t *instance_info) { header_server_state_t *state = malloc(sizeof(header_server_state_t)); memset(state, 0, sizeof(header_server_state_t)); const cJSON *settings = instance_info->node_settings_json; - state->data = parseDynamicNumericValueFromJsonObject(settings, "override", 1, - "dest_context->port"); - tunnel_t *t = newTunnel(); - t->state = state; - t->upStream = &headerServerUpStream; - t->packetUpStream = &headerServerPacketUpStream; - t->downStream = &headerServerDownStream; - t->packetDownStream = &headerServerPacketDownStream; + state->data = parseDynamicNumericValueFromJsonObject(settings, "override", 1, "dest_context->port"); + tunnel_t *t = newTunnel(); + t->state = state; + t->upStream = &upStream; + t->downStream = &downStream; atomic_thread_fence(memory_order_release); return t; } -api_result_t apiHeaderServer(tunnel_t *self, char *msg) +api_result_t apiHeaderServer(tunnel_t *self, const char *msg) { - (void)(self); (void)(msg); return (api_result_t){0}; // TODO + (void) (self); + (void) (msg); + return (api_result_t){0}; // TODO(root): } tunnel_t *destroyHeaderServer(tunnel_t *self) { + (void) (self); return NULL; } tunnel_metadata_t getMetadataHeaderServer() diff --git a/tunnels/server/header/header_server.h b/tunnels/server/header/header_server.h index 680beb57..6ded0d5e 100644 --- a/tunnels/server/header/header_server.h +++ b/tunnels/server/header/header_server.h @@ -1,15 +1,11 @@ #pragma once #include "api.h" -// +// // con <------> HeaderServer (decapsulate(data)) <-------> con -// // -tunnel_t *newHeaderServer(node_instance_context_t *instance_info); -api_result_t apiHeaderServer(tunnel_t *self, char *msg); -tunnel_t *destroyHeaderServer(tunnel_t *self); +tunnel_t * newHeaderServer(node_instance_context_t *instance_info); +api_result_t apiHeaderServer(tunnel_t *self,const char *msg); +tunnel_t * destroyHeaderServer(tunnel_t *self); tunnel_metadata_t getMetadataHeaderServer(); - - - diff --git a/tunnels/server/http2/helpers.h b/tunnels/server/http2/helpers.h index 2622f60e..492e2e77 100644 --- a/tunnels/server/http2/helpers.h +++ b/tunnels/server/http2/helpers.h @@ -3,112 +3,109 @@ #define MAX_CONCURRENT_STREAMS 0xffffffffu -static nghttp2_nv make_nv(const char *name, const char *value) +static nghttp2_nv makeNv(const char *name, const char *value) { nghttp2_nv nv; - nv.name = (uint8_t *)name; - nv.value = (uint8_t *)value; - nv.namelen = strlen(name); + nv.name = (uint8_t *) name; + nv.value = (uint8_t *) value; + nv.namelen = strlen(name); nv.valuelen = strlen(value); - nv.flags = NGHTTP2_NV_FLAG_NONE; + nv.flags = NGHTTP2_NV_FLAG_NONE; return nv; } -static nghttp2_nv make_nv2(const char *name, const char *value, - int namelen, int valuelen) +static nghttp2_nv makeNv2(const char *name, const char *value, int namelen, int valuelen) { nghttp2_nv nv; - nv.name = (uint8_t *)name; - nv.value = (uint8_t *)value; - nv.namelen = namelen; + nv.name = (uint8_t *) name; + nv.value = (uint8_t *) value; + nv.namelen = namelen; nv.valuelen = valuelen; - nv.flags = NGHTTP2_NV_FLAG_NONE; + nv.flags = NGHTTP2_NV_FLAG_NONE; return nv; } -static void print_frame_hd(const nghttp2_frame_hd *hd) +static void printFrameHd(const nghttp2_frame_hd *hd) { - printd("[frame] length=%d type=%x flags=%x stream_id=%d\n", - (int)hd->length, (int)hd->type, (int)hd->flags, hd->stream_id); + LOGD("[frame] length=%d type=%x flags=%x stream_id=%d\n", (int) hd->length, (int) hd->type, (int) hd->flags, + hd->stream_id); } -static void add_stream(http2_server_con_state_t *con, - http2_server_child_con_state_t *stream) +static void addStream(http2_server_con_state_t *con, http2_server_child_con_state_t *stream) { - stream->next = con->root.next; + stream->next = con->root.next; con->root.next = stream; - stream->prev = &con->root; + stream->prev = &con->root; if (stream->next) { stream->next->prev = stream; } } -static void remove_stream(http2_server_con_state_t *con, - http2_server_child_con_state_t *stream) +static void removeStream(http2_server_con_state_t *con, http2_server_child_con_state_t *stream) { - + (void) con; stream->prev->next = stream->next; if (stream->next) { stream->next->prev = stream->prev; } } -http2_server_child_con_state_t * -create_http2_stream(http2_server_con_state_t *con, line_t *this_line, tunnel_t *target_tun, int32_t stream_id) +http2_server_child_con_state_t *createHttp2Stream(http2_server_con_state_t *con, line_t *this_line, + tunnel_t *target_tun, int32_t stream_id) { http2_server_child_con_state_t *stream; stream = malloc(sizeof(http2_server_child_con_state_t)); memset(stream, 0, sizeof(http2_server_child_con_state_t)); - stream->stream_id = stream_id; - stream->chunkbs = newBufferStream(buffer_pools[this_line->tid]); - stream->parent = this_line; - stream->line = newLine(this_line->tid); + stream->stream_id = stream_id; + stream->chunkbs = newBufferStream(buffer_pools[this_line->tid]); + stream->parent = this_line; + stream->line = newLine(this_line->tid); stream->line->chains_state[target_tun->chain_index - 1] = stream; - stream->io = NULL; - stream->tunnel = target_tun; + stream->io = NULL; + stream->tunnel = target_tun; nghttp2_session_set_stream_user_data(con->session, stream_id, stream); return stream; } -static void delete_http2_stream(http2_server_child_con_state_t *stream) +static void deleteHttp2Stream(http2_server_child_con_state_t *stream) { stream->line->chains_state[stream->tunnel->chain_index - 1] = NULL; destroyBufferStream(stream->chunkbs); destroyLine(stream->line); - if (stream->request_path) + if (stream->request_path) { free(stream->request_path); +} free(stream); } -static http2_server_con_state_t *create_http2_connection(tunnel_t *self, line_t *line, hio_t *io) +static http2_server_con_state_t *createHttp2Connection(tunnel_t *self, line_t *line, hio_t *io) { - http2_server_state_t *state = STATE(self); - http2_server_con_state_t *con = malloc(sizeof(http2_server_con_state_t)); + http2_server_state_t * state = STATE(self); + http2_server_con_state_t *con = malloc(sizeof(http2_server_con_state_t)); memset(con, 0, sizeof(http2_server_con_state_t)); nghttp2_session_server_new2(&con->session, state->cbs, con, state->ngoptions); - con->state = H2_WANT_RECV; + con->state = kH2WantRecv; con->tunnel = self; - con->line = line; - con->io = io; + con->line = line; + con->io = io; - nghttp2_settings_entry settings[] = { - {NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, MAX_CONCURRENT_STREAMS}}; + nghttp2_settings_entry settings[] = {{NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, MAX_CONCURRENT_STREAMS}}; nghttp2_submit_settings(con->session, NGHTTP2_FLAG_NONE, settings, ARRAY_SIZE(settings)); - con->state = H2_SEND_SETTINGS; + con->state = kH2SendSettings; return con; } -static void delete_http2_connection(http2_server_con_state_t *con) +static void deleteHttp2Connection(http2_server_con_state_t *con) { - tunnel_t *self = con->tunnel; + tunnel_t * self = con->tunnel; http2_server_child_con_state_t *stream_i; for (stream_i = con->root.next; stream_i;) { - context_t *fin_ctx = newFinContext(stream_i->line); - tunnel_t *dest = stream_i->tunnel; - http2_server_child_con_state_t *next = stream_i->next; - delete_http2_stream(stream_i); + context_t * fin_ctx = newFinContext(stream_i->line); + tunnel_t * dest = stream_i->tunnel; + http2_server_child_con_state_t *next = stream_i->next; + deleteHttp2Stream(stream_i); CSTATE_MUT(fin_ctx) = NULL; dest->upStream(dest, fin_ctx); stream_i = next; @@ -119,4 +116,4 @@ static void delete_http2_connection(http2_server_con_state_t *con) con->line->chains_state[self->chain_index] = NULL; free(con); } -static inline size_t min(size_t x, size_t y) { return (((x) < (y)) ? (x) : (y)); } + diff --git a/tunnels/server/http2/http2_server.c b/tunnels/server/http2/http2_server.c index b0b764c2..29d84183 100644 --- a/tunnels/server/http2/http2_server.c +++ b/tunnels/server/http2/http2_server.c @@ -1,83 +1,90 @@ #include "http2_server.h" -#include "types.h" #include "helpers.h" #include "loggers/network_logger.h" +#include "types.h" -static int on_header_callback(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) +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) { + (void) session; + (void) namelen; + (void) valuelen; + (void) flags; if (userdata == NULL) + { return 0; + } - // LOGD("on_header_callback\n"); + // LOGD("onHeaderCallback\n"); print_frame_hd(&frame->hd); - const char *name = (const char *)_name; - const char *value = (const char *)_value; + const char *name = (const char *) _name; + const char *value = (const char *) _value; // LOGD("%s: %s\n", name, value); - http2_server_con_state_t *con = (http2_server_con_state_t *)userdata; - tunnel_t *self = con->tunnel; + http2_server_con_state_t *con = (http2_server_con_state_t *) userdata; + tunnel_t * self = con->tunnel; if (*name == ':') { - if (strcmp(name, ":method") == 0) - { - // req->method = http_method_enum(value); - } - else if (strcmp(name, ":path") == 0) - { - // req->url = value; - } - else if (strcmp(name, ":scheme") == 0) - { - // req->headers["Scheme"] = value; - } - else if (strcmp(name, ":authority") == 0) - { - // req->headers["Host"] = value; - } + // todo (http2headers) these should be saved somewhere + // if (strcmp(name, ":method") == 0) + // { + // // req->method = http_method_enum(value); + // } + // else if (strcmp(name, ":path") == 0) + // { + // // req->url = value; + // } + // else if (strcmp(name, ":scheme") == 0) + // { + // // req->headers["Scheme"] = value; + // } + // else if (strcmp(name, ":authority") == 0) + // { + // // req->headers["Host"] = value; + // } } else { // hp->parsed->headers[name] = value; if (strcmp(name, "content-type") == 0) { - con->content_type = http_content_type_enum(value); + con->content_type = httpContentTypeEnum(value); } } return 0; } -static int on_data_chunk_recv_callback(nghttp2_session *session, - uint8_t flags, int32_t stream_id, const uint8_t *data, - size_t len, void *userdata) +static int onDataChunkRecvCallback(nghttp2_session *session, uint8_t flags, int32_t stream_id, const uint8_t *data, + size_t len, void *userdata) { + (void) flags; if (userdata == NULL || len <= 0) + { return 0; - http2_server_con_state_t *con = (http2_server_con_state_t *)userdata; - tunnel_t *self = con->tunnel; + } + http2_server_con_state_t *con = (http2_server_con_state_t *) userdata; + tunnel_t * self = con->tunnel; - http2_server_child_con_state_t *stream = - nghttp2_session_get_stream_user_data(session, stream_id); - if (!stream) + http2_server_child_con_state_t *stream = nghttp2_session_get_stream_user_data(session, stream_id); + if (! stream) + { return 0; + } - // LOGD("on_data_chunk_recv_callback\n"); + // LOGD("onDataChunkRecvCallback\n"); // LOGD("stream_id=%d length=%d\n", stream_id, (int)len); // LOGD("up: %d\n", (int)len); - if (con->content_type == APPLICATION_GRPC) + if (con->content_type == kApplicationGrpc) { shift_buffer_t *buf = popBuffer(buffer_pools[con->line->tid]); - shiftl(buf, lCap(buf) / 1.25); // use some unused space + shiftl(buf, lCap(buf) / 2); // use some unused space setLen(buf, len); - writeRaw(buf,data,len); + writeRaw(buf, data, len); bufferStreamPush(stream->chunkbs, buf); while (true) @@ -86,18 +93,18 @@ static int on_data_chunk_recv_callback(nghttp2_session *session, { shift_buffer_t *gheader_buf = bufferStreamRead(stream->chunkbs, GRPC_MESSAGE_HDLEN); grpc_message_hd msghd; - grpc_message_hd_unpack(&msghd, rawBuf(gheader_buf)); + grpcMessageHdUnpack(&msghd, rawBuf(gheader_buf)); stream->bytes_needed = msghd.length; reuseBuffer(buffer_pools[con->line->tid], gheader_buf); } if (stream->bytes_needed > 0 && bufferStreamLen(stream->chunkbs) >= stream->bytes_needed) { shift_buffer_t *gdata_buf = bufferStreamRead(stream->chunkbs, stream->bytes_needed); - stream->bytes_needed = 0; - context_t *stream_data = newContext(stream->line); - stream_data->payload = gdata_buf; - stream_data->src_io = con->io; - if (!stream->first_sent) + stream->bytes_needed = 0; + context_t *stream_data = newContext(stream->line); + stream_data->payload = gdata_buf; + stream_data->src_io = con->io; + if (! stream->first_sent) { stream->first_sent = true; stream_data->first = true; @@ -105,7 +112,9 @@ static int on_data_chunk_recv_callback(nghttp2_session *session, stream->tunnel->upStream(stream->tunnel, stream_data); if (nghttp2_session_get_stream_user_data(session, stream_id)) + { continue; + } } break; } @@ -113,13 +122,13 @@ static int on_data_chunk_recv_callback(nghttp2_session *session, else { shift_buffer_t *buf = popBuffer(buffer_pools[con->line->tid]); - shiftl(buf, lCap(buf) / 1.25); // use some unused space + shiftl(buf, lCap(buf) / 2); // use some unused space setLen(buf, len); - writeRaw(buf,data,len); + writeRaw(buf, data, len); context_t *stream_data = newContext(stream->line); - stream_data->payload = buf; - stream_data->src_io = con->io; - if (!stream->first_sent) + stream_data->payload = buf; + stream_data->src_io = con->io; + if (! stream->first_sent) { stream->first_sent = true; stream_data->first = true; @@ -130,30 +139,30 @@ static int on_data_chunk_recv_callback(nghttp2_session *session, return 0; } -static int on_frame_recv_callback(nghttp2_session *session, - const nghttp2_frame *frame, void *userdata) +static int onFrameRecvCallback(nghttp2_session *session, const nghttp2_frame *frame, void *userdata) { if (userdata == NULL) + { return 0; - - // LOGD("on_frame_recv_callback\n"); + } + // LOGD("onFrameRecvCallback\n"); print_frame_hd(&frame->hd); - http2_server_con_state_t *con = (http2_server_con_state_t *)userdata; - tunnel_t *self = con->tunnel; + http2_server_con_state_t *con = (http2_server_con_state_t *) userdata; + tunnel_t * self = con->tunnel; switch (frame->hd.type) { - case NGHTTP2_DATA: - con->state = H2_RECV_DATA; + case kHttP2Data: + con->state = kH2RecvData; break; - case NGHTTP2_HEADERS: - con->state = H2_RECV_HEADERS; + case kHttP2Headers: + con->state = kH2RecvHeaders; break; - case NGHTTP2_SETTINGS: - con->state = H2_RECV_SETTINGS; + case kHttP2Settings: + con->state = kH2RecvSettings; break; - case NGHTTP2_PING: - con->state = H2_RECV_PING; + case kHttP2Ping: + con->state = kH2RecvPing; break; case NGHTTP2_RST_STREAM: case NGHTTP2_WINDOW_UPDATE: @@ -167,16 +176,17 @@ static int on_frame_recv_callback(nghttp2_session *session, // con->parsed->http_cb(con->parsed, HP_HEADERS_COMPLETE, NULL, 0); // } - if ((frame->hd.flags & HTTP2_FLAG_END_STREAM) == HTTP2_FLAG_END_STREAM) + if ((frame->hd.flags & kHttP2FlagEndStream) == kHttP2FlagEndStream) { - http2_server_child_con_state_t *stream = - nghttp2_session_get_stream_user_data(session, frame->hd.stream_id); - if (!stream) + http2_server_child_con_state_t *stream = nghttp2_session_get_stream_user_data(session, frame->hd.stream_id); + if (! stream) + { return 0; + } nghttp2_session_set_stream_user_data(con->session, stream->stream_id, NULL); - context_t *fc = newFinContext(stream->line); - tunnel_t *dest = stream->tunnel; - remove_stream(con, stream); + context_t *fc = newFinContext(stream->line); + tunnel_t * dest = stream->tunnel; + removeStream(con, stream); delete_http2_stream(stream); CSTATE_MUT(fc) = NULL; @@ -184,118 +194,108 @@ static int on_frame_recv_callback(nghttp2_session *session, return 0; } - if (frame->hd.type != NGHTTP2_HEADERS || - frame->headers.cat != NGHTTP2_HCAT_REQUEST) + if (frame->hd.type != NGHTTP2_HEADERS || frame->headers.cat != NGHTTP2_HCAT_REQUEST) { return 0; } nghttp2_nv nvs[10]; - int nvlen = 0; - nvs[nvlen++] = make_nv(":status", "200"); - if (con->content_type == APPLICATION_GRPC) + int nvlen = 0; + nvs[nvlen++] = makeNv(":status", "200"); + if (con->content_type == kApplicationGrpc) { // correct content_type: application/grpc - nvs[nvlen++] = make_nv("content-type", http_content_type_str(APPLICATION_GRPC)); - nvs[nvlen++] = make_nv("accept-encoding", "identity"); + nvs[nvlen++] = makeNv("content-type", httpContentTypeStr(kApplicationGrpc)); + nvs[nvlen++] = makeNv("accept-encoding", "identity"); } int flags = NGHTTP2_FLAG_END_HEADERS; nghttp2_submit_headers(con->session, flags, frame->hd.stream_id, NULL, &nvs[0], nvlen, NULL); - con->state = H2_SEND_HEADERS; + con->state = kH2SendHeaders; - http2_server_child_con_state_t *stream = create_http2_stream(con, con->line, self->up, frame->hd.stream_id); - add_stream(con, stream); + http2_server_child_con_state_t *stream = createHttp2Stream(con, con->line, self->up, frame->hd.stream_id); + addStream(con, stream); stream->tunnel->upStream(stream->tunnel, newInitContext(stream->line)); return 0; } -static bool trySendResponse(tunnel_t *self, http2_server_con_state_t *con, size_t stream_id, hio_t *stream_io, shift_buffer_t *buf) +static bool trySendResponse(tunnel_t *self, http2_server_con_state_t *con, size_t stream_id, hio_t *stream_io, + 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]))); if (con == NULL) + { return false; + } - char *data = NULL; + char * data = NULL; size_t len; - len = nghttp2_session_mem_send(con->session, (const uint8_t **)&data); + len = nghttp2_session_mem_send(con->session, (const uint8_t **) &data); // LOGD("nghttp2_session_mem_send %d\n", len); if (len > 0) { shift_buffer_t *send_buf = popBuffer(buffer_pools[line->tid]); - shiftl(send_buf, lCap(send_buf) / 1.25); // use some unused space + shiftl(send_buf, lCap(send_buf) / 2); // use some unused space setLen(send_buf, len); - writeRaw(send_buf,data,len); + writeRaw(send_buf, data, len); context_t *response_data = newContext(line); - response_data->payload = send_buf; - response_data->src_io = stream_io; + response_data->payload = send_buf; + response_data->src_io = stream_io; self->dw->downStream(self->dw, response_data); - // if (nghttp2_session_want_read(con->session) == 0 && - // nghttp2_session_want_write(con->session) == 0) - // { - // if (buf != NULL) - // { - // reuseBuffer(buffer_pools[line->tid], buf); - // } - // context_t *fin_ctx = newFinContext(line); - // delete_http2_connection(con); - // self->dw->downStream(self->dw, fin_ctx); - // return false; - // } - return true; } if (buf == NULL || bufLen(buf) <= 0) + { return false; + } // HTTP2_DATA - if (con->state == H2_SEND_HEADERS) + if (con->state == kH2SendHeaders) { - // http2_flag flags = HTTP2_FLAG_END_STREAM; - http2_flag flags = HTTP2_FLAG_NONE; + http2_flag flags = kHttP2FlagNone; // HTTP2 DATA framehd - con->state = H2_SEND_DATA; + con->state = kH2SendData; // LOGD("HTTP2 SEND_DATA_FRAME_HD...\n"); - if (con->content_type == APPLICATION_GRPC) + if (con->content_type == kApplicationGrpc) { grpc_message_hd msghd; - msghd.flags = 0; + msghd.flags = 0; msghd.length = bufLen(buf); // LOGD("grpc_message_hd: flags=%d length=%d\n", msghd.flags, msghd.length); // grpc server send grpc-status in HTTP2 header frame - flags = HTTP2_FLAG_NONE; + flags = kHttP2FlagNone; shiftl(buf, GRPC_MESSAGE_HDLEN); - grpc_message_hd_pack(&msghd, rawBufMut(buf)); + grpcMessageHdUnpack(&msghd, rawBufMut(buf)); } http2_frame_hd framehd; - framehd.length = bufLen(buf); - framehd.type = HTTP2_DATA; - framehd.flags = flags; + framehd.length = bufLen(buf); + framehd.type = kHttP2Data; + framehd.flags = flags; framehd.stream_id = stream_id; shiftl(buf, HTTP2_FRAME_HDLEN); - http2_frame_hd_pack(&framehd, rawBufMut(buf)); + http2FrameHdPack(&framehd, rawBufMut(buf)); context_t *response_data = newContext(line); - response_data->payload = buf; - response_data->src_io = stream_io; + response_data->payload = buf; + response_data->src_io = stream_io; self->dw->downStream(self->dw, response_data); goto send_done; } - else if (con->state == H2_SEND_DATA) + else if (con->state == kH2SendData) { send_done:; - con->state = H2_SEND_DONE; + con->state = kH2SendDone; } // LOGD("GetSendData %d\n", len); @@ -308,48 +308,48 @@ static inline void upStream(tunnel_t *self, context_t *c) if (c->payload != NULL) { http2_server_con_state_t *con = CSTATE(c); - con->io = c->src_io; - con->state = H2_WANT_RECV; - size_t len = bufLen(c->payload); - size_t ret = nghttp2_session_mem_recv2(con->session, (const uint8_t *)rawBuf(c->payload), len); + con->io = c->src_io; + 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); - if (!ISALIVE(c)) + if (! isAlive(c->line)) { destroyContext(c); return; } - + if (ret != len) { - delete_http2_connection(con); - self->dw->downStream(self->dw,newFinContext(c->line)); + deleteHttp2Connection(con); + self->dw->downStream(self->dw, newFinContext(c->line)); destroyContext(c); return; } - - while (trySendResponse(self, con, 0, NULL, NULL)) - if (!ISALIVE(c)) + { + if (! isAlive(c->line)) { destroyContext(c); return; } + } destroyContext(c); } else { if (c->init) { - CSTATE_MUT(c) = create_http2_connection(self, c->line, c->src_io); + CSTATE_MUT(c) = createHttp2Connection(self, c->line, c->src_io); self->dw->downStream(self->dw, newEstContext(c->line)); destroyContext(c); } else if (c->fin) { - delete_http2_connection(CSTATE(c)); + deleteHttp2Connection(CSTATE(c)); destroyContext(c); } } @@ -357,19 +357,21 @@ static inline void upStream(tunnel_t *self, context_t *c) static inline void downStream(tunnel_t *self, context_t *c) { - http2_server_child_con_state_t *stream = (http2_server_child_con_state_t *)CSTATE(c); - http2_server_con_state_t *con = stream->parent->chains_state[self->chain_index]; - stream->io = c->src_io; + http2_server_child_con_state_t *stream = CSTATE(c); + http2_server_con_state_t * con = stream->parent->chains_state[self->chain_index]; + stream->io = c->src_io; if (c->payload != NULL) { - con->state = H2_SEND_HEADERS; + con->state = kH2SendHeaders; while (trySendResponse(self, con, stream->stream_id, stream->io, c->payload)) - if (!ISALIVE(c)) + { + if (! isAlive(c->line)) { destroyContext(c); return; } + } c->payload = NULL; destroyContext(c); } @@ -378,39 +380,43 @@ static inline void downStream(tunnel_t *self, context_t *c) if (c->fin) { int flags = NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_END_HEADERS; - if (con->content_type == APPLICATION_GRPC) + if (con->content_type == kApplicationGrpc) { - nghttp2_nv nv = make_nv("grpc-status", "0"); + nghttp2_nv nv = makeNv("grpc-status", "0"); nghttp2_submit_headers(con->session, flags, stream->stream_id, NULL, &nv, 1, NULL); } else + { nghttp2_submit_headers(con->session, flags, stream->stream_id, NULL, NULL, 0, NULL); + } nghttp2_session_set_stream_user_data(con->session, stream->stream_id, NULL); - remove_stream(con, stream); + removeStream(con, stream); delete_http2_stream(stream); CSTATE_MUT(c) = NULL; while (trySendResponse(self, con, 0, NULL, NULL)) - if (!ISALIVE(c)) + { + if (! isAlive(c->line)) { destroyContext(c); return; } + } - if (nghttp2_session_want_read(con->session) == 0 && - nghttp2_session_want_write(con->session) == 0) + if (nghttp2_session_want_read(con->session) == 0 && nghttp2_session_want_write(con->session) == 0) { context_t *fin_ctx = newFinContext(con->line); - delete_http2_connection(con); + deleteHttp2Connection(con); self->dw->downStream(self->dw, fin_ctx); } destroyContext(c); return; } - else + { destroyContext(c); + } } } @@ -420,8 +426,8 @@ static void http2ServerUpStream(tunnel_t *self, context_t *c) } static void http2ServerPacketUpStream(tunnel_t *self, context_t *c) { - LOGF("Http2Server: Http2 protocol dose not run on udp"); - exit(1); + // LOGF("Http2Server: Http2 protocol dose not run on udp"); + upStream(self, c); } static void http2ServerDownStream(tunnel_t *self, context_t *c) { @@ -429,8 +435,8 @@ static void http2ServerDownStream(tunnel_t *self, context_t *c) } static void http2ServerPacketDownStream(tunnel_t *self, context_t *c) { - LOGF("Http2Server: Http2 protocol dose not run on udp"); - exit(1); + // LOGF("Http2Server: Http2 protocol dose not run on udp"); + downStream(self, c); } tunnel_t *newHttp2Server(node_instance_context_t *instance_info) @@ -440,33 +446,34 @@ tunnel_t *newHttp2Server(node_instance_context_t *instance_info) cJSON *settings = instance_info->node_settings_json; nghttp2_session_callbacks_new(&(state->cbs)); - nghttp2_session_callbacks_set_on_header_callback(state->cbs, on_header_callback); - nghttp2_session_callbacks_set_on_data_chunk_recv_callback(state->cbs, on_data_chunk_recv_callback); - nghttp2_session_callbacks_set_on_frame_recv_callback(state->cbs, on_frame_recv_callback); + nghttp2_session_callbacks_set_on_header_callback(state->cbs, onHeaderCallback); + nghttp2_session_callbacks_set_on_data_chunk_recv_callback(state->cbs, onDataChunkRecvCallback); + nghttp2_session_callbacks_set_on_frame_recv_callback(state->cbs, onFrameRecvCallback); nghttp2_option_new(&(state->ngoptions)); - nghttp2_option_set_peer_max_concurrent_streams(state->ngoptions, 0xffffffffu); + nghttp2_option_set_peer_max_concurrent_streams(state->ngoptions, 0xffffffffU); 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 = &http2ServerUpStream; - t->packetUpStream = &http2ServerPacketUpStream; - t->downStream = &http2ServerDownStream; - t->packetDownStream = &http2ServerPacketDownStream; - + tunnel_t *t = newTunnel(); + t->state = state; + t->upStream = &upStream; + t->downStream = &downStream; atomic_thread_fence(memory_order_release); return t; } -api_result_t apiHttp2Server(tunnel_t *self, char *msg) +api_result_t apiHttp2Server(tunnel_t *self, const char *msg) { - (void)(self); (void)(msg); return (api_result_t){0}; // TODO + (void) (self); + (void) (msg); + return (api_result_t){0}; // TODO(root): } tunnel_t *destroyHttp2Server(tunnel_t *self) { + (void) (self); + return NULL; } diff --git a/tunnels/server/http2/http2_server.h b/tunnels/server/http2/http2_server.h index 9d4b06c2..db1054a2 100644 --- a/tunnels/server/http2/http2_server.h +++ b/tunnels/server/http2/http2_server.h @@ -1,15 +1,11 @@ #pragma once #include "api.h" -// +// // con <------> http2-server <------> http2 stream (con) -// // -tunnel_t *newHttp2Server(node_instance_context_t *instance_info); -api_result_t apiHttp2Server(tunnel_t *self, char *msg); -tunnel_t *destroyHttp2Server(tunnel_t *self); +tunnel_t * newHttp2Server(node_instance_context_t *instance_info); +api_result_t apiHttp2Server(tunnel_t *self, const char *msg); +tunnel_t * destroyHttp2Server(tunnel_t *self); tunnel_metadata_t getMetadataHttp2Server(); - - - diff --git a/tunnels/server/http2/types.h b/tunnels/server/http2/types.h index 910d6440..dc97e8d4 100644 --- a/tunnels/server/http2/types.h +++ b/tunnels/server/http2/types.h @@ -1,75 +1,65 @@ #pragma once #include "api.h" -#include "nghttp2/nghttp2.h" #include "buffer_stream.h" -#include "http_def.h" -#include "http2_def.h" #include "grpc_def.h" - -#define STATE(x) ((http2_server_state_t *)((x)->state)) -#define CSTATE(x) ((void *)((((x)->line->chains_state)[self->chain_index]))) -#define CSTATE_MUT(x) ((x)->line->chains_state)[self->chain_index] -#define ISALIVE(x) (CSTATE(x) != NULL) - +#include "http2_def.h" +#include "http_def.h" +#include "nghttp2/nghttp2.h" typedef enum { - H2_SEND_MAGIC, - H2_SEND_SETTINGS, - H2_SEND_PING, - H2_SEND_HEADERS, - H2_SEND_DATA_FRAME_HD, - H2_SEND_DATA, - H2_SEND_DONE, - - H2_WANT_SEND, - H2_WANT_RECV, - - H2_RECV_SETTINGS, - H2_RECV_PING, - H2_RECV_HEADERS, - H2_RECV_DATA, + kH2SendMagic, + kH2SendSettings, + kH2SendPing, + kH2SendHeaders, + kH2SendDataFrameHd, + kH2SendData, + kH2SendDone, + + kH2WantSend, + kH2WantRecv, + + kH2RecvSettings, + kH2RecvPing, + kH2RecvHeaders, + kH2RecvData, } http2_session_state; - typedef struct http2_server_child_con_state_s { struct http2_server_child_con_state_s *prev, *next; - char *request_path; - int32_t stream_id; - bool first_sent; - - buffer_stream_t *chunkbs; //used for grpc - size_t bytes_needed; - line_t *parent; - line_t *line; - hio_t* io; - tunnel_t *tunnel; + char * request_path; + int32_t stream_id; + bool first_sent; + + buffer_stream_t *chunkbs; // used for grpc + size_t bytes_needed; + line_t * parent; + line_t * line; + hio_t * io; + tunnel_t * tunnel; } http2_server_child_con_state_t; typedef struct http2_server_con_state_s { - nghttp2_session *session; - http2_session_state state; - int error; - int frame_type_when_stream_closed; + nghttp2_session * session; + http2_session_state state; + int error; + int frame_type_when_stream_closed; enum http_content_type content_type; - - tunnel_t *tunnel; - line_t *line; - hio_t* io; + tunnel_t * tunnel; + line_t * line; + hio_t * io; http2_server_child_con_state_t root; } http2_server_con_state_t; - - typedef struct http2_server_state_s { nghttp2_session_callbacks *cbs; - tunnel_t *fallback; - nghttp2_option * ngoptions; + tunnel_t * fallback; + nghttp2_option * ngoptions; } http2_server_state_t; diff --git a/tunnels/server/openssl/openssl_server.c b/tunnels/server/openssl/openssl_server.c index f8a3c046..4c64e58e 100644 --- a/tunnels/server/openssl/openssl_server.c +++ b/tunnels/server/openssl/openssl_server.c @@ -60,9 +60,10 @@ struct timer_eventdata context_t *c; }; -static int onAlpnSelect(const unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, +static int onAlpnSelect(SSL *ssl,const unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg) { + (void)ssl; assert(inlen != 0); oss_server_state_t *state = arg; unsigned int offset = 0; @@ -108,8 +109,12 @@ static enum sslstatus getSslstatus(SSL *ssl, int n) } } -static size_t paddingDecisionCb(void *arg) +static size_t paddingDecisionCb(SSL *ssl, int type, size_t len, void *arg) { + (void) ssl; + (void) type; + (void) len; + oss_server_con_state_t *cstate = arg; return cstate->reply_sent_tit < 10 ? (16 * 200) : 0; } @@ -136,7 +141,7 @@ static struct timer_eventdata *newTimerData(tunnel_t *self, context_t *c) static void fallbackWrite(tunnel_t *self, context_t *c) { - if (! ISALIVE(c)) + if (! isAlive(c->line)) { destroyContext(c); return; @@ -153,7 +158,7 @@ static void fallbackWrite(tunnel_t *self, context_t *c) init_ctx->src_io = c->src_io; cstate->init_sent = true; state->fallback->upStream(state->fallback, init_ctx); - if (! ISALIVE(c)) + if (! isAlive(c->line)) { destroyContext(c); return; @@ -176,7 +181,7 @@ static void fallbackWrite(tunnel_t *self, context_t *c) static void onFallbackTimer(htimer_t *timer) { struct timer_eventdata *data = hevent_userdata(timer); - fallback_write(data->self, data->c); + fallbackWrite(data->self, data->c); htimer_del(timer); free(data); } @@ -198,7 +203,7 @@ static inline void upStream(tunnel_t *self, context_t *c) reuseContextBuffer(c); if (state->fallback_delay <= 0) { - fallback_write(self, c); + fallbackWrite(self, c); } else { @@ -228,7 +233,7 @@ static inline void upStream(tunnel_t *self, context_t *c) if (! SSL_is_init_finished(cstate->ssl)) { n = SSL_accept(cstate->ssl); - status = get_sslstatus(cstate->ssl, n); + status = getSslstatus(cstate->ssl, n); /* Did SSL request to write bytes? */ if (status == kSslstatusWantIo) @@ -245,7 +250,7 @@ static inline void upStream(tunnel_t *self, context_t *c) context_t *answer = newContextFrom(c); answer->payload = buf; self->dw->downStream(self->dw, answer); - if (! ISALIVE(c)) + if (! isAlive(c->line)) { reuseContextBuffer(c); destroyContext(c); @@ -275,7 +280,7 @@ static inline void upStream(tunnel_t *self, context_t *c) cstate->fallback = true; if (state->fallback_delay <= 0) { - fallback_write(self, c); + fallbackWrite(self, c); } else { @@ -301,7 +306,7 @@ static inline void upStream(tunnel_t *self, context_t *c) context_t *up_init_ctx = newInitContext(c->line); up_init_ctx->src_io = c->src_io; self->up->upStream(self->up, up_init_ctx); - if (! ISALIVE(c)) + if (! isAlive(c->line)) { LOGW("OpensslServer: next node instantly closed the init with fin"); reuseContextBuffer(c); @@ -334,7 +339,7 @@ static inline void upStream(tunnel_t *self, context_t *c) cstate->first_sent = true; } self->up->upStream(self->up, data_ctx); - if (! ISALIVE(c)) + if (! isAlive(c->line)) { reuseContextBuffer(c); destroyContext(c); @@ -348,7 +353,7 @@ static inline void upStream(tunnel_t *self, context_t *c) } while (n > 0); - status = get_sslstatus(cstate->ssl, n); + status = getSslstatus(cstate->ssl, n); /* Did SSL request to write bytes? This can happen if peer has requested SSL * renegotiation. */ @@ -366,7 +371,7 @@ static inline void upStream(tunnel_t *self, context_t *c) context_t *answer = newContextFrom(c); answer->payload = buf; self->dw->downStream(self->dw, answer); - if (! ISALIVE(c)) + if (! isAlive(c->line)) { reuseContextBuffer(c); destroyContext(c); @@ -498,7 +503,7 @@ static inline void downStream(tunnel_t *self, context_t *c) while (len) { int n = SSL_write(cstate->ssl, rawBuf(c->payload), len); - status = get_sslstatus(cstate->ssl, n); + status = getSslstatus(cstate->ssl, n); if (n > 0) { @@ -518,7 +523,7 @@ static inline void downStream(tunnel_t *self, context_t *c) context_t *dw_context = newContextFrom(c); dw_context->payload = buf; self->dw->downStream(self->dw, dw_context); - if (! ISALIVE(c)) + if (! isAlive(c->line)) { reuseContextBuffer(c); destroyContext(c); @@ -581,22 +586,6 @@ failed_after_establishment:; self->dw->downStream(self->dw, fail_context); } -static void openSSLUpStream(tunnel_t *self, context_t *c) -{ - upStream(self, c); -} -static void openSSLPacketUpStream(tunnel_t *self, context_t *c) -{ - upStream(self, c); // TODO(root): DTLS -} -static void openSSLDownStream(tunnel_t *self, context_t *c) -{ - downStream(self, c); -} -static void openSSLPacketDownStream(tunnel_t *self, context_t *c) -{ - downStream(self, c); -} tunnel_t *newOpenSSLServer(node_instance_context_t *instance_info) { @@ -699,7 +688,7 @@ tunnel_t *newOpenSSLServer(node_instance_context_t *instance_info) ssl_param->verify_peer = 0; // no mtls ssl_param->endpoint = kSslServer; - state->ssl_context = ssl_ctx_new(ssl_param); + state->ssl_context = sslCtxNew(ssl_param); // int brotli_alg = TLSEXT_comp_cert_brotli; // SSL_set1_cert_comp_preference(state->ssl_context,&brotli_alg,1); // SSL_compress_certs(state->ssl_context,TLSEXT_comp_cert_brotli); @@ -722,15 +711,13 @@ tunnel_t *newOpenSSLServer(node_instance_context_t *instance_info) { state->fallback->dw = t; } - t->upStream = &openSSLUpStream; - t->packetUpStream = &openSSLPacketUpStream; - t->downStream = &openSSLDownStream; - t->packetDownStream = &openSSLPacketDownStream; + t->upStream = &upStream; + t->downStream = &downStream; atomic_thread_fence(memory_order_release); return t; } -api_result_t apiOpenSSLServer(tunnel_t *self,const char *msg) +api_result_t apiOpenSSLServer(tunnel_t *self, const char *msg) { (void) (self); (void) (msg); diff --git a/tunnels/server/openssl/openssl_server.h b/tunnels/server/openssl/openssl_server.h index fa103427..01d39e75 100644 --- a/tunnels/server/openssl/openssl_server.h +++ b/tunnels/server/openssl/openssl_server.h @@ -6,6 +6,6 @@ // tunnel_t * newOpenSSLServer(node_instance_context_t *instance_info); -api_result_t apiOpenSSLServer(tunnel_t *self, char *msg); +api_result_t apiOpenSSLServer(tunnel_t *self, const char *msg); tunnel_t * destroyOpenSSLServer(tunnel_t *self); tunnel_metadata_t getMetadataOpenSSLServer(); diff --git a/tunnels/server/preconnect/preconnect_server.c b/tunnels/server/preconnect/preconnect_server.c index 89f56b29..ce7bb4e1 100644 --- a/tunnels/server/preconnect/preconnect_server.c +++ b/tunnels/server/preconnect/preconnect_server.c @@ -97,10 +97,8 @@ tunnel_t *newPreConnectServer(node_instance_context_t *instance_info) tunnel_t *t = newTunnel(); t->state = state; - t->upStream = &preConnectServerUpStream; - t->packetUpStream = &preConnectServerPacketUpStream; - t->downStream = &preConnectServerDownStream; - t->packetDownStream = &preConnectServerPacketDownStream; + t->upStream = &upStream; + t->downStream = &downStream; atomic_thread_fence(memory_order_release); return t; @@ -110,7 +108,7 @@ api_result_t apiPreConnectServer(tunnel_t *self, const char *msg) { (void) (self); (void) (msg); - return (api_result_t){0}; + return (api_result_t){0}; } tunnel_t *destroyPreConnectServer(tunnel_t *self) diff --git a/tunnels/server/protobuf/protobuf_server.c b/tunnels/server/protobuf/protobuf_server.c index ae154265..a65f5e57 100644 --- a/tunnels/server/protobuf/protobuf_server.c +++ b/tunnels/server/protobuf/protobuf_server.c @@ -144,33 +144,15 @@ static inline void downStream(tunnel_t *self, context_t *c, TunnelFlowRoutine do downstream(self->dw, c); } -static void protoBufServerUpStream(tunnel_t *self, context_t *c) -{ - upStream(self, c, self->up->upStream, self->dw->downStream); -} -static void protoBufServerPacketUpStream(tunnel_t *self, context_t *c) -{ - upStream(self, c, self->up->packetUpStream, self->dw->packetDownStream); -} -static void protoBufServerDownStream(tunnel_t *self, context_t *c) -{ - downStream(self, c, self->dw->downStream); -} -static void protoBufServerPacketDownStream(tunnel_t *self, context_t *c) -{ - downStream(self, c, self->dw->packetDownStream); -} + tunnel_t *newProtoBufServer(node_instance_context_t *instance_info) { (void) instance_info; - tunnel_t *t = newTunnel(); - - t->upStream = &protoBufServerUpStream; - t->packetUpStream = &protoBufServerPacketUpStream; - t->downStream = &protoBufServerDownStream; - t->packetDownStream = &protoBufServerPacketDownStream; + tunnel_t *t = newTunnel(); + t->upStream = &upStream; + t->downStream = &downStream; atomic_thread_fence(memory_order_release); return t; diff --git a/tunnels/server/reverse/reverse_server.c b/tunnels/server/reverse/reverse_server.c index 7c870036..ee451f50 100644 --- a/tunnels/server/reverse/reverse_server.c +++ b/tunnels/server/reverse/reverse_server.c @@ -65,7 +65,7 @@ static inline void upStream(tunnel_t *self, context_t *c) else { const unsigned int tid = c->line->tid; - thread_box_t * this_tb = &(state->workers[tid]); + thread_box_t * this_tb = &(state->threadlocal_pool[tid]); if (c->init) { if (state->chain_index_d == 0) @@ -131,7 +131,7 @@ static inline void downStream(tunnel_t *self, context_t *c) else { const unsigned int tid = c->line->tid; - thread_box_t * this_tb = &(state->workers[tid]); + thread_box_t * this_tb = &(state->threadlocal_pool[tid]); if (c->init) { if (state->chain_index_u == 0) @@ -200,22 +200,6 @@ static inline void downStream(tunnel_t *self, context_t *c) } } } -static void reverseServerUpStream(tunnel_t *self, context_t *c) -{ - upStream(self, c); -} -static void reverseServerPacketUpStream(tunnel_t *self, context_t *c) -{ - upStream(self, c); -} -static void reverseServerDownStream(tunnel_t *self, context_t *c) -{ - downStream(self, c); -} -static void reverseServerPacketDownStream(tunnel_t *self, context_t *c) -{ - downStream(self, c); -} tunnel_t *newReverseServer(node_instance_context_t *instance_info) { @@ -225,10 +209,8 @@ tunnel_t *newReverseServer(node_instance_context_t *instance_info) tunnel_t *t = newTunnel(); t->state = state; - t->upStream = &reverseServerUpStream; - t->packetUpStream = &reverseServerPacketUpStream; - t->downStream = &reverseServerDownStream; - t->packetDownStream = &reverseServerPacketDownStream; + t->upStream = &upStream; + t->downStream = &downStream; atomic_thread_fence(memory_order_release); return t; diff --git a/tunnels/server/reverse/types.h b/tunnels/server/reverse/types.h index 0c7becff..4dab4a14 100644 --- a/tunnels/server/reverse/types.h +++ b/tunnels/server/reverse/types.h @@ -6,20 +6,18 @@ typedef struct reverse_server_con_state_s { struct reverse_server_con_state_s *prev, *next; - bool paired; - bool signal_sent; - context_queue_t *uqueue; - line_t *u; - line_t *d; + bool paired; + bool signal_sent; + context_queue_t * uqueue; + line_t * u; + line_t * d; } reverse_server_con_state_t; - - typedef struct thread_box_s { - size_t d_count; - size_t u_count; + size_t d_count; + size_t u_count; reverse_server_con_state_t d_cons_root; reverse_server_con_state_t u_cons_root; @@ -27,12 +25,9 @@ typedef struct thread_box_s typedef struct reverse_server_state_s { - atomic_size_t u_available; - atomic_size_t d_available; - - size_t chain_index_u; - size_t chain_index_d; - thread_box_t workers[]; + uint8_t chain_index_u; + uint8_t chain_index_d; + thread_box_t threadlocal_pool[]; } reverse_server_state_t; diff --git a/tunnels/server/trojan/auth/trojan_auth_server.c b/tunnels/server/trojan/auth/trojan_auth_server.c index ff2f508e..6a69cdba 100644 --- a/tunnels/server/trojan/auth/trojan_auth_server.c +++ b/tunnels/server/trojan/auth/trojan_auth_server.c @@ -5,7 +5,6 @@ #include "utils/stringutils.h" #include "utils/userutils.h" - #define i_type hmap_users_t // NOLINT #define i_key hash_t // NOLINT #define i_val trojan_user_t * // NOLINT @@ -54,7 +53,7 @@ static void onFallbackTimer(htimer_t *timer) free(data); htimer_del(timer); - if (! ISALIVE(c)) + if (! isAlive(c->line)) { if (c->payload != NULL) { @@ -68,10 +67,11 @@ static void onFallbackTimer(htimer_t *timer) static inline void upStream(tunnel_t *self, context_t *c) { - trojan_auth_server_state_t *state = STATE(self); + trojan_auth_server_state_t * state = STATE(self); + trojan_auth_server_con_state_t *cstate = CSTATE(c); + if (c->payload != NULL) { - trojan_auth_server_con_state_t *cstate = CSTATE(c); if (cstate->authenticated) { self->up->upStream(self->up, c); @@ -125,7 +125,7 @@ static inline void upStream(tunnel_t *self, context_t *c) init_ctx->src_io = c->src_io; cstate->init_sent = true; self->up->upStream(self->up, init_ctx); - if (! ISALIVE(c)) + if (! isAlive(c->line)) { reuseContextBuffer(c); destroyContext(c); @@ -148,16 +148,16 @@ static inline void upStream(tunnel_t *self, context_t *c) { if (c->init) { - CSTATE_MUT(c) = malloc(sizeof(trojan_auth_server_con_state_t)); - memset(CSTATE(c), 0, sizeof(trojan_auth_server_con_state_t)); - trojan_auth_server_con_state_t *cstate = CSTATE(c); + cstate = malloc(sizeof(trojan_auth_server_con_state_t)); + memset(cstate, 0, sizeof(trojan_auth_server_con_state_t)); + CSTATE_MUT(c) = cstate; markAuthenticationNodePresence(c->line); destroyContext(c); } else if (c->fin) { - bool init_sent = CSTATE(c)->init_sent; - bool auth = CSTATE(c)->authenticated; + bool init_sent = cstate->init_sent; + bool auth = cstate->authenticated; free(CSTATE(c)); CSTATE_MUT(c) = NULL; if (init_sent) @@ -194,7 +194,6 @@ disconnect:; self->dw->downStream(self->dw, reply); return; fallback:; - trojan_auth_server_con_state_t *cstate = CSTATE(c); if (! cstate->init_sent) { context_t *init_ctx = newInitContext(c->line); @@ -202,7 +201,7 @@ fallback:; cstate->init_sent = true; state->fallback->upStream(state->fallback, init_ctx); - if (! ISALIVE(c)) + if (! isAlive(c->line)) { reuseContextBuffer(c); destroyContext(c); @@ -352,10 +351,8 @@ tunnel_t *newTrojanAuthServer(node_instance_context_t *instance_info) tunnel_t *t = newTunnel(); t->state = state; - t->upStream = &trojanAuthServerUpStream; - t->packetUpStream = &trojanAuthServerPacketUpStream; - t->downStream = &trojanAuthServerDownStream; - t->packetDownStream = &trojanAuthServerPacketDownStream; + t->upStream = &upStream; + t->downStream = &downStream; parse(t, settings, instance_info->chain_index); atomic_thread_fence(memory_order_release); diff --git a/tunnels/server/trojan/socks/trojan_socks_server.c b/tunnels/server/trojan/socks/trojan_socks_server.c index d65002bf..46c86081 100644 --- a/tunnels/server/trojan/socks/trojan_socks_server.c +++ b/tunnels/server/trojan/socks/trojan_socks_server.c @@ -377,7 +377,7 @@ static bool processUdp(tunnel_t *self, trojan_socks_server_con_state_t *cstate, context_t *up_init_ctx = newContextFrom(c); up_init_ctx->init = true; self->up->packetUpStream(self->up, up_init_ctx); - if (! isAlive(c)) + if (! isAlive(c->line)) { LOGW("TrojanSocksServer: next node instantly closed the init with fin"); return true; @@ -616,10 +616,8 @@ tunnel_t *newTrojanSocksServer(node_instance_context_t *instance_info) tunnel_t *t = newTunnel(); t->state = state; - t->upStream = &trojanSocksServerUpStream; - t->packetUpStream = &trojanSocksServerPacketUpStream; - t->downStream = &trojanSocksServerDownStream; - t->packetDownStream = &trojanSocksServerPacketDownStream; + t->upStream = &upStream; + t->downStream = &downStream; atomic_thread_fence(memory_order_release); return t; } diff --git a/tunnels/server/wolfssl/wolfssl_server.c b/tunnels/server/wolfssl/wolfssl_server.c index f0a7b014..c54103b8 100644 --- a/tunnels/server/wolfssl/wolfssl_server.c +++ b/tunnels/server/wolfssl/wolfssl_server.c @@ -13,11 +13,6 @@ #include #include -#define STATE(x) ((wssl_server_state_t *) ((x)->state)) -#define CSTATE(x) ((wssl_server_con_state_t *) ((((x)->line->chains_state)[self->chain_index]))) -#define CSTATE_MUT(x) ((x)->line->chains_state)[self->chain_index] -#define ISALIVE(x) (CSTATE(x) != NULL) - typedef struct { char * name; @@ -66,10 +61,10 @@ struct timer_eventdata tunnel_t * self; context_t *c; }; - -static int onAlpnSelect(const unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, - void *arg) +static int onAlpnSelect(SSL *ssl, const unsigned char **out, unsigned char *outlen, const unsigned char *in, + unsigned int inlen, void *arg) { + (void) ssl; assert(inlen != 0); wssl_server_state_t *state = arg; unsigned int offset = 0; @@ -104,17 +99,27 @@ static enum sslstatus getSslstatus(SSL *ssl, int n) switch (SSL_get_error(ssl, n)) { case SSL_ERROR_NONE: - return SSLSTATUS_OK; + return kSslstatusOk; case SSL_ERROR_WANT_WRITE: case SSL_ERROR_WANT_READ: - return SSLSTATUS_WANT_IO; + return kSslstatusWantIo; case SSL_ERROR_ZERO_RETURN: case SSL_ERROR_SYSCALL: default: - return SSLSTATUS_FAIL; + return kSslstatusFail; } } +// todo (tls in tls padding) wolfssl dose not support it but since its standard in tls 1.3, there must be a way +// static size_t paddingDecisionCb(SSL *ssl, int type, size_t len, void *arg) +// { +// (void) ssl; +// (void) type; +// (void) len; +// wssl_server_con_state_t *cstate = arg; +// return cstate->reply_sent_tit < 10 ? (16 * 200) : 0; +// } + static void cleanup(tunnel_t *self, context_t *c) { wssl_server_con_state_t *cstate = CSTATE(c); @@ -137,7 +142,7 @@ static struct timer_eventdata *newTimerData(tunnel_t *self, context_t *c) static void fallbackWrite(tunnel_t *self, context_t *c) { - if (! ISALIVE(c)) + if (! isAlive(c->line)) { destroyContext(c); return; @@ -154,7 +159,7 @@ static void fallbackWrite(tunnel_t *self, context_t *c) init_ctx->src_io = c->src_io; cstate->init_sent = true; state->fallback->upStream(state->fallback, init_ctx); - if (! ISALIVE(c)) + if (! isAlive(c->line)) { destroyContext(c); return; @@ -174,21 +179,21 @@ static void fallbackWrite(tunnel_t *self, context_t *c) c->payload = bufferStreamRead(cstate->fallback_buf, record_len); state->fallback->upStream(state->fallback, c); } -static void onFallbackTimer() +static void onFallbackTimer(htimer_t *timer) { struct timer_eventdata *data = hevent_userdata(timer); - fallback_write(data->self, data->c); + fallbackWrite(data->self, data->c); htimer_del(timer); free(data); } static inline void upStream(tunnel_t *self, context_t *c) { - wssl_server_state_t *state = STATE(self); + wssl_server_state_t * state = STATE(self); + wssl_server_con_state_t *cstate = CSTATE(c); if (c->payload != NULL) { - wssl_server_con_state_t *cstate = CSTATE(c); if (! cstate->handshake_completed) { @@ -199,11 +204,11 @@ static inline void upStream(tunnel_t *self, context_t *c) reuseContextBuffer(c); if (state->fallback_delay <= 0) { - fallback_write(self, c); + fallbackWrite(self, c); } else { - htimer_t *fallback_timer = htimer_add(c->line->loop, on_fallback_timer, state->fallback_delay, 1); + htimer_t *fallback_timer = htimer_add(c->line->loop, onFallbackTimer, state->fallback_delay, 1); hevent_set_userdata(fallback_timer, newTimerData(self, c)); } @@ -229,10 +234,11 @@ static inline void upStream(tunnel_t *self, context_t *c) if (! SSL_is_init_finished(cstate->ssl)) { n = SSL_accept(cstate->ssl); - status = get_sslstatus(cstate->ssl, n); + status = getSslstatus(cstate->ssl, n); /* Did SSL request to write bytes? */ - if (status == SSLSTATUS_WANT_IO) + if (status == kSslstatusWantIo) + { do { shift_buffer_t *buf = popBuffer(buffer_pools[c->line->tid]); @@ -245,7 +251,7 @@ static inline void upStream(tunnel_t *self, context_t *c) context_t *answer = newContextFrom(c); answer->payload = buf; self->dw->downStream(self->dw, answer); - if (! ISALIVE(c)) + if (! isAlive(c->line)) { reuseContextBuffer(c); destroyContext(c); @@ -264,8 +270,9 @@ static inline void upStream(tunnel_t *self, context_t *c) reuseBuffer(buffer_pools[c->line->tid], buf); } } while (n > 0); + } - if (status == SSLSTATUS_FAIL) + if (status == kSslstatusFail) { reuseContextBuffer(c); // payload already buffered printSSLError(); @@ -274,16 +281,17 @@ static inline void upStream(tunnel_t *self, context_t *c) cstate->fallback = true; if (state->fallback_delay <= 0) { - fallback_write(self, c); + fallbackWrite(self, c); } else { htimer_t *fallback_timer = - htimer_add(c->line->loop, on_fallback_timer, state->fallback_delay, 1); + htimer_add(c->line->loop, onFallbackTimer, state->fallback_delay, 1); hevent_set_userdata(fallback_timer, newTimerData(self, c)); } return; } + goto disconnect; } @@ -294,14 +302,14 @@ static inline void upStream(tunnel_t *self, context_t *c) return; } - LOGD("WolfsslServer: Tls handshake complete"); + LOGD("OpensslServer: Tls handshake complete"); cstate->handshake_completed = true; context_t *up_init_ctx = newInitContext(c->line); up_init_ctx->src_io = c->src_io; self->up->upStream(self->up, up_init_ctx); - if (! ISALIVE(c)) + if (! isAlive(c->line)) { - LOGW("WolfsslServer: next node instantly closed the init with fin"); + LOGW("OpensslServer: next node instantly closed the init with fin"); reuseContextBuffer(c); destroyContext(c); @@ -326,14 +334,13 @@ static inline void upStream(tunnel_t *self, context_t *c) setLen(buf, n); context_t *data_ctx = newContextFrom(c); data_ctx->payload = buf; - data_ctx->src_io = c->src_io; if (! (cstate->first_sent)) { data_ctx->first = true; cstate->first_sent = true; } self->up->upStream(self->up, data_ctx); - if (! ISALIVE(c)) + if (! isAlive(c->line)) { reuseContextBuffer(c); destroyContext(c); @@ -347,11 +354,12 @@ static inline void upStream(tunnel_t *self, context_t *c) } while (n > 0); - status = get_sslstatus(cstate->ssl, n); + status = getSslstatus(cstate->ssl, n); /* Did SSL request to write bytes? This can happen if peer has requested SSL * renegotiation. */ - if (status == SSLSTATUS_WANT_IO) + if (status == kSslstatusWantIo) + { do { shift_buffer_t *buf = popBuffer(buffer_pools[c->line->tid]); @@ -364,7 +372,7 @@ static inline void upStream(tunnel_t *self, context_t *c) context_t *answer = newContextFrom(c); answer->payload = buf; self->dw->downStream(self->dw, answer); - if (! ISALIVE(c)) + if (! isAlive(c->line)) { reuseContextBuffer(c); destroyContext(c); @@ -377,7 +385,6 @@ static inline void upStream(tunnel_t *self, context_t *c) // If BIO_should_retry() is false then the cause is an error condition. reuseBuffer(buffer_pools[c->line->tid], buf); reuseContextBuffer(c); - goto failed_after_establishment; } else @@ -385,8 +392,9 @@ static inline void upStream(tunnel_t *self, context_t *c) reuseBuffer(buffer_pools[c->line->tid], buf); } } while (n > 0); + } - if (status == SSLSTATUS_FAIL) + if (status == kSslstatusFail) { reuseContextBuffer(c); goto failed_after_establishment; @@ -410,14 +418,22 @@ static inline void upStream(tunnel_t *self, context_t *c) cstate->fallback_buf = newBufferStream(buffer_pools[c->line->tid]); SSL_set_accept_state(cstate->ssl); /* sets ssl to work in server mode. */ SSL_set_bio(cstate->ssl, cstate->rbio, cstate->wbio); + // if (state->anti_tit) + // { + // if (1 != SSL_set_record_padding_callback(cstate->ssl, paddingDecisionCb)) + // { + // LOGW("OpensslServer: Could not set ssl padding"); + // } + // SSL_set_record_padding_callback_arg(cstate->ssl, cstate); + // } destroyContext(c); } else if (c->fin) { - if (CSTATE(c)->fallback) + if (cstate->fallback) { - if (CSTATE(c)->fallback_init_sent) + if (cstate->fallback_init_sent) { cleanup(self, c); state->fallback->upStream(state->fallback, c); @@ -428,7 +444,7 @@ static inline void upStream(tunnel_t *self, context_t *c) destroyContext(c); } } - else if (CSTATE(c)->init_sent) + else if (cstate->init_sent) { cleanup(self, c); self->up->upStream(self->up, c); @@ -462,6 +478,11 @@ static inline void downStream(tunnel_t *self, context_t *c) if (c->payload != NULL) { + // not supported by wolfssl + // if (state->anti_tit && isAuthenticated(c->line)) + // { + // cstate->reply_sent_tit += 1; + // } enum sslstatus status; @@ -480,7 +501,7 @@ static inline void downStream(tunnel_t *self, context_t *c) while (len) { int n = SSL_write(cstate->ssl, rawBuf(c->payload), len); - status = get_sslstatus(cstate->ssl, n); + status = getSslstatus(cstate->ssl, n); if (n > 0) { @@ -499,9 +520,8 @@ static inline void downStream(tunnel_t *self, context_t *c) setLen(buf, n); context_t *dw_context = newContextFrom(c); dw_context->payload = buf; - dw_context->src_io = c->src_io; self->dw->downStream(self->dw, dw_context); - if (! ISALIVE(c)) + if (! isAlive(c->line)) { reuseContextBuffer(c); destroyContext(c); @@ -523,7 +543,7 @@ static inline void downStream(tunnel_t *self, context_t *c) } while (n > 0); } - if (status == SSLSTATUS_FAIL) + if (status == kSslstatusFail) { reuseContextBuffer(c); goto failed_after_establishment; @@ -564,22 +584,6 @@ failed_after_establishment:; self->dw->downStream(self->dw, fail_context); } -static void wolfSSLUpStream(tunnel_t *self, context_t *c) -{ - upStream(self, c); -} -static void wolfSSLPacketUpStream(tunnel_t *self, context_t *c) -{ - upStream(self, c); // TODO(root): DTLS -} -static void wolfSSLDownStream(tunnel_t *self, context_t *c) -{ - downStream(self, c); -} -static void wolfSSLPacketDownStream(tunnel_t *self, context_t *c) -{ - downStream(self, c); -} tunnel_t *newWolfSSLServer(node_instance_context_t *instance_info) { @@ -686,7 +690,7 @@ tunnel_t *newWolfSSLServer(node_instance_context_t *instance_info) } ssl_param->verify_peer = 0; // no mtls - ssl_param->endpoint = SSL_SERVER; + ssl_param->endpoint = kSslServer; state->ssl_context = ssl_ctx_new(ssl_param); // int brotli_alg = TLSEXT_comp_cert_brotli; // SSL_set1_cert_comp_preference(state->ssl_context,&brotli_alg,1); @@ -710,10 +714,8 @@ tunnel_t *newWolfSSLServer(node_instance_context_t *instance_info) { state->fallback->dw = t; } - t->upStream = &wolfSSLUpStream; - t->packetUpStream = &wolfSSLPacketUpStream; - t->downStream = &wolfSSLDownStream; - t->packetDownStream = &wolfSSLPacketDownStream; + t->upStream = &upStream; + t->downStream = &downStream; atomic_thread_fence(memory_order_release); return t; } @@ -723,7 +725,9 @@ api_result_t apiWolfSSLServer(tunnel_t *self, const char *msg) (void) self; (void) msg; - (void)(self); (void)(msg); return (api_result_t){0}; + (void) (self); + (void) (msg); + return (api_result_t){0}; } tunnel_t *destroyWolfSSLServer(tunnel_t *self) diff --git a/tunnels/shared/http2/http2_def.h b/tunnels/shared/http2/http2_def.h index 49f58eba..50644f8a 100644 --- a/tunnels/shared/http2/http2_def.h +++ b/tunnels/shared/http2/http2_def.h @@ -42,17 +42,17 @@ extern "C" typedef struct { - int length; + unsigned int length; http2_frame_type type; http2_flag flags; - int stream_id; + unsigned int stream_id; } http2_frame_hd; static inline void http2FrameHdPack(const http2_frame_hd *restrict hd, unsigned char *restrict buf) { // hton - int length = hd->length; - int stream_id = hd->stream_id; + unsigned int length = hd->length; + unsigned int stream_id = hd->stream_id; unsigned char *p = buf; *p++ = (length >> 16) & 0xFF; *p++ = (length >> 8) & 0xFF; diff --git a/tunnels/shared/openssl/openssl_globals.h b/tunnels/shared/openssl/openssl_globals.h index 6b413928..e51c150d 100644 --- a/tunnels/shared/openssl/openssl_globals.h +++ b/tunnels/shared/openssl/openssl_globals.h @@ -149,9 +149,9 @@ static ssl_ctx_t sslCtxNew(ssl_ctx_opt_t *param) return NULL; } -static void printSSLState() +static void printSSLState(const SSL *ssl) // NOLINT (ssl in unused problem) { - const char *current_state = SSL_state_string_long(s); + const char *current_state = SSL_state_string_long(ssl); LOGD("%s", current_state); } diff --git a/tunnels/shared/wolfssl/wolfssl_globals.h b/tunnels/shared/wolfssl/wolfssl_globals.h index 82234137..1bd2b2ff 100644 --- a/tunnels/shared/wolfssl/wolfssl_globals.h +++ b/tunnels/shared/wolfssl/wolfssl_globals.h @@ -150,7 +150,7 @@ static ssl_ctx_t sslCtxNew(ssl_ctx_opt_t *param) return NULL; } -static void printSSLState() +static void printSSLState(const SSL *ssl) // NOLINT (ssl in unused problem) { const char *current_state = SSL_state_string_long(s); LOGD("%s", current_state); diff --git a/ww/basic_types.h b/ww/basic_types.h index 9d679a4e..329b5f17 100644 --- a/ww/basic_types.h +++ b/ww/basic_types.h @@ -92,15 +92,8 @@ typedef struct dynamic_value_s enum dynamic_value_status status; size_t value; void * value_ptr; - } dynamic_value_t; -enum socket_address_cmd -{ - kSacConnect = 0X1, - kSacAssociate = 0X3, -}; - enum socket_address_type { kSatIPV4 = 0X1, @@ -108,15 +101,20 @@ enum socket_address_type kSatIPV6 = 0X4, }; +enum socket_address_protocol +{ + kSapTcp = 0X1, + kSapUdp = 0X3, +}; + // all data we need to connect to somewhere typedef struct socket_context_s { - uint8_t protocol; // IPPROTO_X - enum socket_address_cmd acmd; - enum socket_address_type atype; - char * domain; - unsigned int domain_len; - bool resolved; - sockaddr_u addr; + enum socket_address_protocol aproto; + enum socket_address_type atype; + char * domain; + unsigned int domain_len; + bool resolved; + sockaddr_u addr; } socket_context_t; diff --git a/ww/buffer_pool.c b/ww/buffer_pool.c index 34c9a449..2ed667e1 100644 --- a/ww/buffer_pool.c +++ b/ww/buffer_pool.c @@ -1,4 +1,5 @@ #include "buffer_pool.h" +#include "utils/mathutils.h" #include // for assert #include #include @@ -17,17 +18,6 @@ #define BUFFERPOOL_CONTAINER_LEN (50 + (250 * MEMORY_PROFILE)) #define BUFFER_SIZE ((MEMORY_PROFILE < MED2_MEMORY) ? 0 : EVP_READ_BUFSIZE) -#undef max -#undef min -static inline size_t max(size_t x, size_t y) -{ - return (((x) < (y)) ? (y) : (x)); -} -static inline size_t min(size_t x, size_t y) -{ - return (((x) < (y)) ? (x) : (y)); -} - static void firstCharge(buffer_pool_t *state) { // state->chunks = 1; diff --git a/ww/buffer_stream.c b/ww/buffer_stream.c index 0ad1360b..e1ca49de 100644 --- a/ww/buffer_stream.c +++ b/ww/buffer_stream.c @@ -1,15 +1,5 @@ #include "buffer_stream.h" - -#undef max -#undef min -static inline size_t min(size_t x, size_t y) -{ - return (((x) < (y)) ? (x) : (y)); -} -static inline size_t max(size_t x, size_t y) -{ - return (((x) > (y)) ? (x) : (y)); -} +#include "utils/mathutils.h" buffer_stream_t *newBufferStream(struct buffer_pool_s *pool) { diff --git a/ww/tunnel.c b/ww/tunnel.c index a01400d7..76e26768 100644 --- a/ww/tunnel.c +++ b/ww/tunnel.c @@ -29,9 +29,7 @@ tunnel_t *newTunnel() t->up = NULL; t->upStream = &defaultUpStream; - t->packetUpStream = &defaultPacketUpStream; t->downStream = &defaultDownStream; - t->packetDownStream = &defaultPacketDownStream; return t; } @@ -42,13 +40,6 @@ void defaultUpStream(tunnel_t *self, context_t *c) self->up->upStream(self->up, c); } } -void defaultPacketUpStream(tunnel_t *self, context_t *c) -{ - if (self->up != NULL) - { - self->up->packetUpStream(self->up, c); - } -} void defaultDownStream(tunnel_t *self, context_t *c) { @@ -57,10 +48,4 @@ void defaultDownStream(tunnel_t *self, context_t *c) self->dw->downStream(self->dw, c); } } -void defaultPacketDownStream(tunnel_t *self, context_t *c) -{ - if (self->dw != NULL) - { - self->dw->packetDownStream(self->dw, c); - } -} + diff --git a/ww/tunnel.h b/ww/tunnel.h index 9453843d..020d9b66 100644 --- a/ww/tunnel.h +++ b/ww/tunnel.h @@ -40,31 +40,30 @@ typedef struct context_s bool fin; } context_t; -typedef void (*TunnelFlowRoutine)(struct tunnel_s *self, context_t *c); +struct tunnel_s; -typedef struct tunnel_s +typedef void(*TunnelFlowRoutine)(struct tunnel_s *, struct context_s *); + +struct tunnel_s { void * state; hloop_t ** loops; struct tunnel_s *dw, *up; TunnelFlowRoutine upStream; - TunnelFlowRoutine packetUpStream; TunnelFlowRoutine downStream; - TunnelFlowRoutine packetDownStream; - size_t chain_index; + uint8_t chain_index; +}; -} tunnel_t; +typedef struct tunnel_s tunnel_t; tunnel_t *newTunnel(); void destroyTunnel(tunnel_t *self); -void chain(tunnel_t *self, tunnel_t *next); +void chain(tunnel_t *from, tunnel_t *to); void defaultUpStream(tunnel_t *self, context_t *c); -void defaultPacketUpStream(tunnel_t *self, context_t *c); void defaultDownStream(tunnel_t *self, context_t *c); -void defaultPacketDownStream(tunnel_t *self, context_t *c); inline line_t *newLine(uint16_t tid) { @@ -107,10 +106,6 @@ inline void destroyLine(line_t *l) assert(l->chains_state[i] == NULL); } - // if (l->dest_ctx.domain != NULL && ! l->dest_ctx.domain_is_constant_memory) - // { - // assert(l->dest_ctx.domain == NULL); - // } #endif assert(l->src_ctx.domain == NULL); // impossible (hopefully) @@ -119,8 +114,6 @@ inline void destroyLine(line_t *l) free(l->dest_ctx.domain); } - // if (l->dest_ctx.domain != NULL && !l->dest_ctx.domain_is_constant_memory) - // free(l->dest_ctx.domain); free(l); } inline void destroyContext(context_t *c) diff --git a/ww/utils/mathutils.h b/ww/utils/mathutils.h new file mode 100644 index 00000000..b1c203c2 --- /dev/null +++ b/ww/utils/mathutils.h @@ -0,0 +1,6 @@ +#pragma once +#include +#undef max +#undef min +static inline size_t min(size_t x, size_t y) { return (((x) < (y)) ? (x) : (y)); } +static inline size_t max(size_t x, size_t y) { return (((x) < (y)) ? (y) : (x)); }