From e13c9a01a57f3fd0fec83999e8b4c960b8c110b5 Mon Sep 17 00:00:00 2001 From: Logan oos Even <46396513+Logan007@users.noreply.github.com> Date: Mon, 30 Aug 2021 14:23:31 +0545 Subject: [PATCH] rearranged thread-related code (#761) --- include/n2n.h | 1 + src/edge.c | 5 ----- src/edge_utils.c | 5 +++++ src/example_sn_embed.c | 4 +++- src/sn.c | 6 ++---- src/sn_utils.c | 12 +++++++++++- 6 files changed, 22 insertions(+), 11 deletions(-) diff --git a/include/n2n.h b/include/n2n.h index 396c8b6..11abe13 100644 --- a/include/n2n.h +++ b/include/n2n.h @@ -269,6 +269,7 @@ int quick_edge_init (char *device_name, char *community_name, char *supernode_ip_address_port, int *keep_on_running); int comm_init (struct sn_community *comm, char *cmn); +int sn_init_defaults (n2n_sn_t *sss); int sn_init (n2n_sn_t *sss); void sn_term (n2n_sn_t *sss); int supernode2sock (n2n_sock_t * sn, const n2n_sn_name_t addrIn); diff --git a/src/edge.c b/src/edge.c index 5e139a8..1fdb8a5 100644 --- a/src/edge.c +++ b/src/edge.c @@ -50,7 +50,6 @@ int supernode_disconnect (n2n_edge_t *eee); int fetch_and_eventually_process_data (n2n_edge_t *eee, SOCKET sock, uint8_t *pktbuf, uint16_t *expected, uint16_t *position, time_t now); -int resolve_create_thread (n2n_resolve_parameter_t **param, struct peer_info *sn_list); int resolve_check (n2n_resolve_parameter_t *param, uint8_t resolution_request, time_t now); int edge_init_routes (n2n_edge_t *eee, n2n_route_t *routes, uint16_t num_routes); @@ -1041,10 +1040,6 @@ int main (int argc, char* argv[]) { exit(1); } - if(resolve_create_thread(&(eee->resolve_parameter), eee->conf.supernodes) == 0) { - traceEvent(TRACE_NORMAL, "successfully created resolver thread"); - } - memcpy(&(eee->tuntap_priv_conf), &ec, sizeof(ec)); if((0 == strcmp("static", eee->tuntap_priv_conf.ip_mode)) || diff --git a/src/edge_utils.c b/src/edge_utils.c index 037f755..d3cc457 100644 --- a/src/edge_utils.c +++ b/src/edge_utils.c @@ -26,6 +26,7 @@ static HEAP_ALLOC (wrkmem, LZO1X_1_MEM_COMPRESS); /* ************************************** */ +int resolve_create_thread (n2n_resolve_parameter_t **param, struct peer_info *sn_list); int resolve_check (n2n_resolve_parameter_t *param, uint8_t resolution_request, time_t now); int resolve_cancel_thread (n2n_resolve_parameter_t *param); @@ -418,6 +419,10 @@ n2n_edge_t* edge_init (const n2n_edge_conf_t *conf, int *rv) { goto edge_init_error; } + if(resolve_create_thread(&(eee->resolve_parameter), eee->conf.supernodes) == 0) { + traceEvent(TRACE_NORMAL, "successfully created resolver thread"); + } + eee->network_traffic_filter = create_network_traffic_filter(); network_traffic_filter_add_rule(eee->network_traffic_filter, eee->conf.network_traffic_filter_rules); diff --git a/src/example_sn_embed.c b/src/example_sn_embed.c index f09d06c..f6478b6 100644 --- a/src/example_sn_embed.c +++ b/src/example_sn_embed.c @@ -25,7 +25,7 @@ int main () { n2n_sn_t sss_node; int rc; - sn_init(&sss_node); + sn_init_defaults(&sss_node); sss_node.daemon = 0; // Whether to daemonize sss_node.lport = 1234; // Main UDP listen port @@ -39,6 +39,8 @@ int main () { exit(-2); } + sn_init(&sss_node); + keep_running = 1; rc = run_sn_loop(&sss_node, &keep_running); diff --git a/src/sn.c b/src/sn.c index cb240dd..5c3af68 100644 --- a/src/sn.c +++ b/src/sn.c @@ -527,7 +527,7 @@ int main (int argc, char * const argv[]) { struct peer_info *scan, *tmp; - sn_init(&sss_node); + sn_init_defaults(&sss_node); add_federation_to_communities(&sss_node); if((argc >= 2) && (argv[1][0] != '-')) { @@ -635,9 +635,7 @@ int main (int argc, char * const argv[]) { } #endif - if(resolve_create_thread(&(sss_node.resolve_parameter), sss_node.federation->edges) == 0) { - traceEvent(TRACE_NORMAL, "successfully created resolver thread"); - } + sn_init(&sss_node); traceEvent(TRACE_NORMAL, "supernode started"); diff --git a/src/sn_utils.c b/src/sn_utils.c index a086c35..3d062bb 100644 --- a/src/sn_utils.c +++ b/src/sn_utils.c @@ -20,6 +20,7 @@ #define HASH_FIND_COMMUNITY(head, name, out) HASH_FIND_STR(head, name, out) +int resolve_create_thread (n2n_resolve_parameter_t **param, struct peer_info *sn_list); int resolve_check (n2n_resolve_parameter_t *param, uint8_t resolution_request, time_t now); int resolve_cancel_thread (n2n_resolve_parameter_t *param); @@ -727,7 +728,7 @@ int comm_init (struct sn_community *comm, char *cmn) { /** Initialise the supernode structure */ -int sn_init(n2n_sn_t *sss) { +int sn_init_defaults (n2n_sn_t *sss) { int i; size_t idx; @@ -787,6 +788,15 @@ int sn_init(n2n_sn_t *sss) { } +/** Initialise the supernode */ +int sn_init (n2n_sn_t *sss) { + + if(resolve_create_thread(&(sss->resolve_parameter), sss->federation->edges) == 0) { + traceEvent(TRACE_NORMAL, "successfully created resolver thread"); + } +} + + /** Deinitialise the supernode structure and deallocate any memory owned by * it. */ void sn_term (n2n_sn_t *sss) {