Skip to content

Commit

Permalink
finish udp listener
Browse files Browse the repository at this point in the history
  • Loading branch information
radkesvat committed May 2, 2024
1 parent e83698e commit 026d1f3
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 144 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ cacert.pem

/core/texts
/cmake
/.cache

.env.local
.env.*.local
Expand Down
4 changes: 2 additions & 2 deletions tunnels/adapters/connector/connector.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ tunnel_t *newConnector(node_instance_context_t *instance_info)
udp_outbound_node->version = instance_info->node->version;
registerNode(tcp_outbound_node, settings);
registerNode(udp_outbound_node, settings);
runNode(tcp_outbound_node, instance_info->chain_index + 1);
runNode(udp_outbound_node, instance_info->chain_index + 1);
runNode(tcp_outbound_node, instance_info->chain_index);
runNode(udp_outbound_node, instance_info->chain_index);
state->tcp_connector = tcp_outbound_node->instance;
state->udp_connector = udp_outbound_node->instance;

Expand Down
18 changes: 18 additions & 0 deletions tunnels/adapters/listener/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@


add_library(Listener STATIC
listener.c

)

# target_include_directories(Listener PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
#ww api
target_include_directories(Listener PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../../ww)
target_link_libraries(Listener ww)
# add dependencies
include(${CMAKE_BINARY_DIR}/cmake/CPM.cmake)

target_compile_definitions(Listener PRIVATE Listener_VERSION=0.1)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
target_compile_definitions(Listener PRIVATE DEBUG=1)
endif()
90 changes: 90 additions & 0 deletions tunnels/adapters/listener/listener.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include "listener.h"
#include "loggers/network_logger.h"
#include "managers/node_manager.h"
#include "utils/stringutils.h"

typedef struct listener_state_s
{
tunnel_t *tcp_listener;
tunnel_t *udp_listener;

} listener_state_t;

typedef struct listener_con_state_s
{

} listener_con_state_t;

static void upStream(tunnel_t *self, context_t *c)
{
self->up->upStream(self->up, c);
}
static void downStream(tunnel_t *self, context_t *c)
{
listener_state_t *state = STATE(self);

switch ((c->line->src_ctx.address_protocol))
{
default:
case kSapTcp:
state->tcp_listener->downStream(state->tcp_listener, c);
break;

case kSapUdp:
state->udp_listener->downStream(state->udp_listener, c);
break;
}
}

tunnel_t *newListener(node_instance_context_t *instance_info)
{
listener_state_t *state = malloc(sizeof(listener_state_t));
memset(state, 0, sizeof(listener_state_t));
cJSON *settings = instance_info->node_settings_json;

if (! (cJSON_IsObject(settings) && settings->child != NULL))
{
LOGF("JSON Error: Listener->settings (object field) : The object was empty or invalid");
return NULL;
}
node_t *tcp_outbound_node = newNode();
node_t *udp_outbound_node = newNode();
tcp_outbound_node->name = concat(instance_info->node->name, "_tcp_inbound");
tcp_outbound_node->type = "TcpListener";
tcp_outbound_node->version = instance_info->node->version;
udp_outbound_node->name = concat(instance_info->node->name, "_tcp_inbound");
udp_outbound_node->type = "UdpListener";
udp_outbound_node->version = instance_info->node->version;
registerNode(tcp_outbound_node, settings);
registerNode(udp_outbound_node, settings);
runNode(tcp_outbound_node, instance_info->chain_index);
runNode(udp_outbound_node, instance_info->chain_index);
state->tcp_listener = tcp_outbound_node->instance;
state->udp_listener = udp_outbound_node->instance;

tunnel_t *t = newTunnel();
t->state = state;
t->upStream = &upStream;
t->downStream = &downStream;

chainDown(t, state->tcp_listener);
chainDown(t, state->udp_listener);

atomic_thread_fence(memory_order_release);
return t;
}
api_result_t apiListener(tunnel_t *self, const char *msg)
{
(void) (self);
(void) (msg);
return (api_result_t){0};
}
tunnel_t *destroyListener(tunnel_t *self)
{
(void) (self);
return NULL;
}
tunnel_metadata_t getMetadataListener()
{
return (tunnel_metadata_t){.version = 0001, .flags = 0x0};
}
12 changes: 12 additions & 0 deletions tunnels/adapters/listener/listener.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once
#include "api.h"

// user <-----\ /-----> (Tcp|Udp) listener
// user <------> Listener <------> (Tcp|Udp) listener
// user <-----/ \-----> (Tcp|Udp) listener
//

tunnel_t * newListener(node_instance_context_t *instance_info);
api_result_t apiListener(tunnel_t *self, const char *msg);
tunnel_t * destroyListener(tunnel_t *self);
tunnel_metadata_t getMetadataListener();
17 changes: 11 additions & 6 deletions tunnels/adapters/listener/tcp/tcp_listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,19 @@ static void onInboundConnected(hevent_t *ev)
}

// send the init packet
context_t *context = newInitContext(line);
context->src_io = io;
self->upStream(self, context);
if (! isAlive(line))
lockLine(line);
{
LOGW("TcpListener: socket just got closed by upstream before anything happend");
return;
context_t *context = newInitContext(line);
context->src_io = io;
self->upStream(self, context);
if (! isAlive(line))
{
LOGW("TcpListener: socket just got closed by upstream before anything happend");
unLockLine(line);
return;
}
}
unLockLine(line);
hio_read(io);
}

Expand Down
7 changes: 3 additions & 4 deletions tunnels/adapters/listener/tcp/tcp_listener.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#pragma once
#include "api.h"

// user <-----\ /-----> con 1
// user <------> TcpListener <------> con 2
// user <-----/ \-----> con 3
//
// user <-----\ /-----> Tcp con 1
// user <------> TcpListener <------> Tcp con 2
// user <-----/ \-----> Tcp con 3


tunnel_t *newTcpListener(node_instance_context_t *instance_info);
Expand Down
14 changes: 7 additions & 7 deletions tunnels/adapters/listener/udp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@


add_library(TcpListener STATIC
tcp_listener.c
add_library(UdpListener STATIC
udp_listener.c

)

# target_include_directories(TcpListener PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
# target_include_directories(UdpListener PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

#ww api
target_include_directories(TcpListener PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../../ww)
target_link_libraries(TcpListener ww)
target_include_directories(UdpListener PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../../ww)
target_link_libraries(UdpListener ww)


# add dependencies
Expand All @@ -19,9 +19,9 @@ include(${CMAKE_BINARY_DIR}/cmake/CPM.cmake)



target_compile_definitions(TcpListener PRIVATE TcpListener_VERSION=0.1)
target_compile_definitions(UdpListener PRIVATE UdpListener_VERSION=0.1)

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
target_compile_definitions(TcpListener PRIVATE DEBUG=1)
target_compile_definitions(UdpListener PRIVATE DEBUG=1)

endif()
Loading

0 comments on commit 026d1f3

Please sign in to comment.