Skip to content

Commit

Permalink
Finish general reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
radkesvat committed Apr 21, 2024
1 parent b0cab43 commit 93f45c2
Show file tree
Hide file tree
Showing 72 changed files with 1,599 additions and 1,619 deletions.
25 changes: 17 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ option(INCLUDE_TCP_LISTENER "link TcpListener staticly to the core" TRUE)
option(INCLUDE_LOGGER_TUNNEL "link LoggerTunnel staticly to the core" TRUE)
option(INCLUDE_CONNECTOR "link Connector staticly to the core" TRUE)
option(INCLUDE_TCPCONNECTOR "link TcpConnector staticly to the core" TRUE)
option(INCLUDE_UDPCONNECTOR "link UdpConnector staticly to the core" TRUE)
option(INCLUDE_BRIDGE "link Bridge staticly to the core" TRUE)

option(INCLUDE_OPENSSL_SERVER "link OpenSSlServer staticly to the core" TRUE)
Expand Down Expand Up @@ -96,8 +97,8 @@ target_link_libraries(Waterwall ww)
#tcp listener
if (INCLUDE_TCP_LISTENER)
target_compile_definitions(Waterwall PUBLIC INCLUDE_TCP_LISTENER=1)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tunnels/adapters/tcp_listener)
target_link_directories(Waterwall PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/tunnels/adapters/tcp_listener)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tunnels/adapters/listener/tcp)
target_link_directories(Waterwall PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/tunnels/adapters/listener/tcp)
target_link_libraries(Waterwall TcpListener)
endif()

Expand All @@ -112,17 +113,25 @@ endif()
#tcp connector
if (INCLUDE_TCPCONNECTOR)
target_compile_definitions(Waterwall PUBLIC INCLUDE_TCPCONNECTOR=1)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tunnels/adapters/bridge)
target_link_directories(Waterwall PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/tunnels/adapters/bridge)
target_link_libraries(Waterwall Bridge)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tunnels/adapters/connector/tcp)
target_link_directories(Waterwall PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/tunnels/adapters/connector/tcp)
target_link_libraries(Waterwall TcpConnector)
endif()

#udp connector
if (INCLUDE_UDPCONNECTOR)
target_compile_definitions(Waterwall PUBLIC INCLUDE_UDPCONNECTOR=1)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tunnels/adapters/connector/udp)
target_link_directories(Waterwall PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/tunnels/adapters/connector/udp)
target_link_libraries(Waterwall UdpConnector)
endif()

#bridge
if (INCLUDE_BRIDGE)
target_compile_definitions(Waterwall PUBLIC INCLUDE_BRIDGE=1)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tunnels/adapters/tcp_connector)
target_link_directories(Waterwall PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/tunnels/adapters/tcp_connector)
target_link_libraries(Waterwall TcpConnector)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tunnels/adapters/bridge)
target_link_directories(Waterwall PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/tunnels/adapters/bridge)
target_link_libraries(Waterwall Bridge)
endif()

#http2 server
Expand Down
50 changes: 8 additions & 42 deletions core/core_settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,45 +23,7 @@

static struct core_settings_s *settings = NULL;

#ifdef OS_UNIX
#include <sys/resource.h>
void increaseFileLimit()
{

struct rlimit rlim;
// Get the current limit
if (getrlimit(RLIMIT_NOFILE, &rlim) == -1)
{
LOGF("Core: getrlimit failed");
exit(EXIT_FAILURE);
}
if ((unsigned long) rlim.rlim_max < 8192)
{
LOGW(
"Core: Maximum allowed open files limit is %lu which is below 8192 !\n if you are running as a server then \
you might experince time-outs if this limits gets reached, depends on how many clients are connected to the server simultaneously\
",
(unsigned long) rlim.rlim_max);
}
else
{
LOGD("Core: File limit %lu -> %lu", (unsigned long) rlim.rlim_cur, (unsigned long) rlim.rlim_max);
}
// Set the hard limit to the maximum allowed value
rlim.rlim_cur = rlim.rlim_max;
// Apply the new limit
if (setrlimit(RLIMIT_NOFILE, &rlim) == -1)
{
LOGF("Core: setrlimit failed");
exit(EXIT_FAILURE);
}
}
#else
void increaseFileLimit()
{
(void) (0);
}
#endif

static void parseLogPartOfJsonNoCheck(const cJSON *log_obj)
{
Expand Down Expand Up @@ -129,8 +91,6 @@ static void parseLogPartOfJsonNoCheck(const cJSON *log_obj)
}
}



static void parseLogPartOfJson(cJSON *log_obj)
{
if (cJSON_IsObject(log_obj) && (log_obj->child != NULL))
Expand Down Expand Up @@ -229,11 +189,17 @@ void parseCoreSettings(char *data_json)
parseConfigPartOfJson(cJSON_GetObjectItemCaseSensitive(json, "configs"));
parseMiscPartOfJson(cJSON_GetObjectItemCaseSensitive(json, "misc"));

if ((settings->workers_count < 0) || (settings->workers_count > 200))
if (settings->workers_count <= 0)
{
LOGF("CoreSettings: the workers count is invalid");
fprintf(stderr, "CoreSettings: the workers count is invalid");
exit(1);
}

if (settings->workers_count > 255)
{
fprintf(stderr, "CoreSettings: workers count is shrinked to maximum supported value -> 255");
settings->workers_count = 255;
}
cJSON_Delete(json);
// TODO (DNS) Implement dns settings and backend
}
Expand Down
1 change: 0 additions & 1 deletion core/core_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ struct core_settings_s
};

void parseCoreSettings(char *data_json);
void increaseFileLimit();

char *getCoreLoggerFullPath();
char *getNetworkLoggerFullPath();
Expand Down
13 changes: 9 additions & 4 deletions core/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
#include "loggers/network_logger.h"
#include "managers/node_manager.h"
#include "managers/socket_manager.h"
#include "os_helpers.h"
#include "static_tunnels.h"
#include "utils/fileutils.h"
#include "utils/stringutils.h"

#undef hlog
#define hlog getCoreLogger() // NOLINT

#define CORE_FILE "core.json"

Expand Down Expand Up @@ -38,14 +43,14 @@ int main(void)
ww_construction_data_t runtime_data = {
.workers_count = getCoreSettings()->workers_count,
.core_logger_data = (logger_construction_data_t){.log_file_path = getCoreLoggerFullPath(),
.log_level = getCoreSettings()->core_log_level,
.log_console = getCoreSettings()->core_log_console},
.log_level = getCoreSettings()->core_log_level,
.log_console = getCoreSettings()->core_log_console},
.network_logger_data = (logger_construction_data_t){.log_file_path = getNetworkLoggerFullPath(),
.log_level = getCoreSettings()->network_log_level,
.log_console = getCoreSettings()->network_log_level},
.dns_logger_data = (logger_construction_data_t){.log_file_path = getDnsLoggerFullPath(),
.log_level = getCoreSettings()->dns_log_level,
.log_console = getCoreSettings()->dns_log_console},
.log_level = getCoreSettings()->dns_log_level,
.log_console = getCoreSettings()->dns_log_console},
};

createWW(runtime_data);
Expand Down
43 changes: 43 additions & 0 deletions core/os_helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

#include "loggers/core_logger.h"

#ifdef OS_UNIX
#include <sys/resource.h>
static void increaseFileLimit()
{

struct rlimit rlim;
// Get the current limit
if (getrlimit(RLIMIT_NOFILE, &rlim) == -1)
{
LOGF("Core: getrlimit failed");
exit(EXIT_FAILURE);
}
if ((unsigned long) rlim.rlim_max < 8192)
{
LOGW(
"Core: Maximum allowed open files limit is %lu which is below 8192 !\n if you are running as a server then \
you might experince time-outs if this limits gets reached, depends on how many clients are connected to the server simultaneously\
",
(unsigned long) rlim.rlim_max);
}
else
{
LOGD("Core: File limit %lu -> %lu", (unsigned long) rlim.rlim_cur, (unsigned long) rlim.rlim_max);
}
// Set the hard limit to the maximum allowed value
rlim.rlim_cur = rlim.rlim_max;
// Apply the new limit
if (setrlimit(RLIMIT_NOFILE, &rlim) == -1)
{
LOGF("Core: setrlimit failed");
exit(EXIT_FAILURE);
}
}
#else
static void increaseFileLimit()
{
(void) (0);
}
#endif
4 changes: 2 additions & 2 deletions core/static_tunnels.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
} while (0)

#ifdef INCLUDE_TCP_LISTENER
#include "tunnels/adapters/tcp_listener/tcp_listener.h"
#include "tunnels/adapters/listener/tcp/tcp_listener.h"
#endif

#ifdef INCLUDE_OPENSSL_SERVER
Expand Down Expand Up @@ -40,7 +40,7 @@
#endif

#ifdef INCLUDE_TCPCONNECTOR
#include "tunnels/adapters/tcp_connector/tcp_connector.h"
#include "tunnels/adapters/connector/tcp/tcp_connector.h"
#endif

#ifdef INCLUDE_BRIDGE
Expand Down
104 changes: 36 additions & 68 deletions tunnels/adapters/bridge/bridge.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
#include "bridge.h"
#include "managers/node_manager.h"
#include "loggers/network_logger.h"


#define STATE(x) ((bridge_state_t *)((x)->state))
#define CSTATE(x) ((bridge_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)
#include "managers/node_manager.h"

typedef struct bridge_state_s
{
bool initialized;
bool mode_upside; // this node is last node of upstream
node_t *pair_node;
bool mode_upside; // this node is last node of upstream
node_t * pair_node;
tunnel_t *pair;

} bridge_state_t;
Expand All @@ -22,66 +15,33 @@ typedef struct bridge_con_state_s

} bridge_con_state_t;

static void secondary_init(tunnel_t *self, bridge_state_t *state)
{
if (state->pair_node->instance == NULL)
{
LOGF("Bridge: the pair node is not running");
exit(1);
}
if (!state->mode_upside && self->dw != NULL)
{
LOGF("Bridge: misconfiguration, bridge only exists at the start or end of a chain");
exit(1);
}
state->pair = state->pair_node->instance;
state->initialized = true;
}
static void upStream(tunnel_t *self, context_t *c)
{
bridge_state_t *state = STATE(self);
if (!state->initialized)
secondary_init(self,state);
if (state->mode_upside)
state->pair->downStream(state->pair, c);
else
self->up->upStream(self->up, c);
{
self->dw->upStream = state->pair->dw->downStream;
state->pair->dw->downStream(state->pair, c);
}
}

static inline void downStream(tunnel_t *self, context_t *c)
{

bridge_state_t *state = STATE(self);
if (!state->initialized)
secondary_init(self,state);
if (state->mode_upside)
self->dw->downStream(self->dw, c);
else
state->pair->upStream(state->pair, c);
}

static void BridgeUpStream(tunnel_t *self, context_t *c)
{
upStream(self, c);
}
static void BridgePacketUpStream(tunnel_t *self, context_t *c)
{
upStream(self, c);
}
static void BridgeDownStream(tunnel_t *self, context_t *c)
{
downStream(self, c);
}
static void BridgePacketDownStream(tunnel_t *self, context_t *c)
{
downStream(self, c);
if (! state->mode_upside)
{
self->up->downStream = state->pair->up->upStream;
state->pair->up->upStream(state->pair, c);
}
}

tunnel_t *newBridge(node_instance_context_t *instance_info)
{
const cJSON *settings = instance_info->node_settings_json;
char *pair_node_name = NULL;
if (!getStringFromJsonObject(&pair_node_name, settings, "pair"))
const cJSON *settings = instance_info->node_settings_json;
char * pair_node_name = NULL;
if (! getStringFromJsonObject(&pair_node_name, settings, "pair"))
{
LOGF("Bridge: \"pair\" is not provided in json");
exit(1);
Expand All @@ -90,38 +50,46 @@ tunnel_t *newBridge(node_instance_context_t *instance_info)
bridge_state_t *state = malloc(sizeof(bridge_state_t));
memset(state, 0, sizeof(bridge_state_t));

hash_t hash_pairname = CALC_HASH_BYTES(pair_node_name, strlen(pair_node_name));
node_t *pair_node = getNode(hash_pairname);
hash_t hash_pairname = CALC_HASH_BYTES(pair_node_name, strlen(pair_node_name));
node_t *pair_node = getNode(hash_pairname);
if (pair_node == NULL)
{
LOGF("Bridge: pair node \"%s\" not found", pair_node_name);
exit(1);
}
free(pair_node_name);
state->pair_node = pair_node;

state->mode_upside = instance_info->self_node_handle->next == NULL;
state->pair_node = pair_node;
state->mode_upside = instance_info->node->next == NULL;
tunnel_t *t = newTunnel();

if (pair_node->instance)
{
state->pair = pair_node->instance;
bridge_state_t *pair_state = STATE(pair_node->instance);
pair_state->pair = t;
}

tunnel_t *t = newTunnel();
t->state = state;
t->upStream = &BridgeUpStream;
t->packetUpStream = &BridgePacketUpStream;
t->downStream = &BridgeDownStream;
t->packetDownStream = &BridgePacketDownStream;
t->state = state;
t->upStream = &upStream;
t->downStream = &downStream;
return t;
}

api_result_t apiBridge(tunnel_t *self, char *msg)
api_result_t apiBridge(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 *destroyBridge(tunnel_t *self)
{
(void) (self);

return NULL;
}
tunnel_metadata_t getMetadataBridge()
{
return (tunnel_metadata_t){.version = 0001, .flags = TFLAG_ROUTE_STARTER};
return (tunnel_metadata_t){.version = 0001, .flags = kNodeFlagChainHead};
}
Loading

0 comments on commit 93f45c2

Please sign in to comment.