From: Kirill Isakov Date: Wed, 11 Aug 2021 14:17:12 +0000 (+0600) Subject: Replace pointers to global splay trees with structs. X-Git-Url: https://tinc-vpn.org/git/browse?a=commitdiff_plain;h=3a149f7521dfff67e6a790c1a830afc649ae083e;p=tinc Replace pointers to global splay trees with structs. re #294 --- diff --git a/src/address_cache.c b/src/address_cache.c index df465da3..e17f8964 100644 --- a/src/address_cache.c +++ b/src/address_cache.c @@ -147,7 +147,7 @@ const sockaddr_t *get_recent_address(address_cache_t *cache) { // Otherwise, check if there are any known Address statements if(!cache->config_tree) { - init_configuration(&cache->config_tree); + cache->config_tree = create_configuration(); read_host_config(cache->config_tree, cache->node->name, false); cache->cfg = lookup_config(cache->config_tree, "Address"); } diff --git a/src/autoconnect.c b/src/autoconnect.c index 5651f54d..a5f433cf 100644 --- a/src/autoconnect.c +++ b/src/autoconnect.c @@ -28,7 +28,7 @@ static void make_new_connection() { /* Select a random node we haven't connected to yet. */ int count = 0; - for splay_each(node_t, n, node_tree) { + for splay_each(node_t, n, &node_tree) { if(n == myself || n->connection || !(n->status.has_address || n->status.reachable)) { continue; } @@ -42,7 +42,7 @@ static void make_new_connection() { int r = rand() % count; - for splay_each(node_t, n, node_tree) { + for splay_each(node_t, n, &node_tree) { if(n == myself || n->connection || !(n->status.has_address || n->status.reachable)) { continue; } @@ -80,9 +80,9 @@ static void connect_to_unreachable() { * are only a few reachable nodes, and many unreachable ones, we're * going to try harder to connect to them. */ - unsigned int r = rand() % node_tree->count; + unsigned int r = rand() % node_tree.count; - for splay_each(node_t, n, node_tree) { + for splay_each(node_t, n, &node_tree) { if(r--) { continue; } diff --git a/src/bsd/device.c b/src/bsd/device.c index 57029d5d..fe2d6c52 100644 --- a/src/bsd/device.c +++ b/src/bsd/device.c @@ -125,13 +125,13 @@ static bool setup_utun(void) { #endif static bool setup_device(void) { - get_config_string(lookup_config(config_tree, "Device"), &device); + get_config_string(lookup_config(&config_tree, "Device"), &device); // Find out if it's supposed to be a tun or a tap device char *type; - if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) { + if(get_config_string(lookup_config(&config_tree, "DeviceType"), &type)) { if(!strcasecmp(type, "tun")) /* use default */; @@ -233,7 +233,7 @@ static bool setup_device(void) { realname = device; } - if(!get_config_string(lookup_config(config_tree, "Interface"), &iface)) { + if(!get_config_string(lookup_config(&config_tree, "Interface"), &iface)) { iface = xstrdup(strrchr(realname, '/') ? strrchr(realname, '/') + 1 : realname); } else if(strcmp(iface, strrchr(realname, '/') ? strrchr(realname, '/') + 1 : realname)) { logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: Interface does not match Device. $INTERFACE might be set incorrectly."); diff --git a/src/conf.c b/src/conf.c index 90bd3697..4dd8fe7a 100644 --- a/src/conf.c +++ b/src/conf.c @@ -34,8 +34,6 @@ #include "protocol.h" #include "xalloc.h" -splay_tree_t *config_tree = NULL; - int pinginterval = 0; /* seconds between pings */ int pingtimeout = 0; /* seconds to wait for response */ @@ -72,8 +70,21 @@ static int config_compare(const config_t *a, const config_t *b) { } } -void init_configuration(splay_tree_t **config_tree) { - *config_tree = splay_alloc_tree((splay_compare_t) config_compare, (splay_action_t) free_config); +splay_tree_t config_tree = { + .compare = (splay_compare_t) config_compare, + .delete = (splay_action_t) free_config, +}; + +splay_tree_t *create_configuration() { + splay_tree_t *tree = splay_alloc_tree(NULL, NULL); + init_configuration(tree); + return tree; +} + +void init_configuration(splay_tree_t *tree) { + memset(tree, 0, sizeof(*tree)); + tree->compare = (splay_compare_t) config_compare; + tree->delete = (splay_action_t) free_config; } void exit_configuration(splay_tree_t **config_tree) { diff --git a/src/conf.h b/src/conf.h index 07db631c..8672ad79 100644 --- a/src/conf.h +++ b/src/conf.h @@ -32,8 +32,7 @@ typedef struct config_t { int line; } config_t; - -extern splay_tree_t *config_tree; +extern splay_tree_t config_tree; extern int pinginterval; extern int pingtimeout; @@ -41,7 +40,8 @@ extern int maxtimeout; extern bool bypass_security; extern list_t cmdline_conf; -extern void init_configuration(splay_tree_t **config_tree); +extern splay_tree_t *create_configuration(); +extern void init_configuration(splay_tree_t *); extern void exit_configuration(splay_tree_t **config_tree); extern config_t *new_config(void) __attribute__((__malloc__)); extern void free_config(config_t *config); diff --git a/src/edge.c b/src/edge.c index a19b9d33..15d3150d 100644 --- a/src/edge.c +++ b/src/edge.c @@ -28,12 +28,6 @@ #include "node.h" #include "xalloc.h" -splay_tree_t *edge_weight_tree; - -static int edge_compare(const edge_t *a, const edge_t *b) { - return strcmp(a->to->name, b->to->name); -} - static int edge_weight_compare(const edge_t *a, const edge_t *b) { int result; @@ -52,8 +46,12 @@ static int edge_weight_compare(const edge_t *a, const edge_t *b) { return strcmp(a->to->name, b->to->name); } -void init_edges(void) { - edge_weight_tree = splay_alloc_tree((splay_compare_t) edge_weight_compare, NULL); +splay_tree_t edge_weight_tree = { + .compare = (splay_compare_t) edge_weight_compare, +}; + +static int edge_compare(const edge_t *a, const edge_t *b) { + return strcmp(a->to->name, b->to->name); } splay_tree_t *new_edge_tree(void) { @@ -65,7 +63,7 @@ void free_edge_tree(splay_tree_t *edge_tree) { } void exit_edges(void) { - splay_delete_tree(edge_weight_tree); + splay_empty_tree(&edge_weight_tree); } /* Creation and deletion of connection elements */ @@ -96,7 +94,7 @@ void edge_add(edge_t *e) { e->reverse->reverse = e; } - node = splay_insert(edge_weight_tree, e); + node = splay_insert(&edge_weight_tree, e); if(!node) { logger(DEBUG_ALWAYS, LOG_ERR, "Edge from %s to %s already exists in edge_weight_tree\n", e->from->name, e->to->name); @@ -109,7 +107,7 @@ void edge_del(edge_t *e) { e->reverse->reverse = NULL; } - splay_delete(edge_weight_tree, e); + splay_delete(&edge_weight_tree, e); splay_delete(e->from->edge_tree, e); } @@ -123,7 +121,7 @@ edge_t *lookup_edge(node_t *from, node_t *to) { } bool dump_edges(connection_t *c) { - for splay_each(node_t, n, node_tree) { + for splay_each(node_t, n, &node_tree) { for splay_each(edge_t, e, n->edge_tree) { char *address = sockaddr2hostname(&e->address); char *local_address = sockaddr2hostname(&e->local_address); diff --git a/src/edge.h b/src/edge.h index 032acbc5..670801bb 100644 --- a/src/edge.h +++ b/src/edge.h @@ -39,9 +39,8 @@ typedef struct edge_t { struct edge_t *reverse; /* edge in the opposite direction, if available */ } edge_t; -extern splay_tree_t *edge_weight_tree; /* Tree with all known edges sorted on weight */ +extern splay_tree_t edge_weight_tree; /* Tree with all known edges sorted on weight */ -extern void init_edges(void); extern void exit_edges(void); extern edge_t *new_edge(void) __attribute__((__malloc__)); extern void free_edge(edge_t *e); diff --git a/src/fd_device.c b/src/fd_device.c index 7360d17e..f21e475c 100644 --- a/src/fd_device.c +++ b/src/fd_device.c @@ -150,7 +150,7 @@ static bool setup_device(void) { return false; } - if(!get_config_string(lookup_config(config_tree, "Device"), &device)) { + if(!get_config_string(lookup_config(&config_tree, "Device"), &device)) { logger(DEBUG_ALWAYS, LOG_ERR, "Could not read device from configuration!"); return false; } diff --git a/src/fsck.c b/src/fsck.c index 7b18c19a..c1247e17 100644 --- a/src/fsck.c +++ b/src/fsck.c @@ -119,19 +119,19 @@ static int strtailcmp(const char *str, const char *tail) { } static void check_conffile(const char *nodename, bool server) { - splay_tree_t *config = NULL; + splay_tree_t config; init_configuration(&config); bool read; if(server) { - read = read_server_config(config); + read = read_server_config(&config); } else { - read = read_host_config(config, nodename, true); + read = read_host_config(&config, nodename, true); } if(!read) { - exit_configuration(&config); + splay_empty_tree(&config); return; } @@ -144,7 +144,7 @@ static void check_conffile(const char *nodename, bool server) { int count[total_vars]; memset(count, 0, sizeof(count)); - for splay_each(config_t, conf, config) { + for splay_each(config_t, conf, &config) { int var_type = 0; for(size_t i = 0; variables[i].name; ++i) { @@ -181,7 +181,7 @@ static void check_conffile(const char *nodename, bool server) { } } - exit_configuration(&config); + splay_empty_tree(&config); } #ifdef HAVE_MINGW @@ -649,17 +649,17 @@ int fsck(const char *argv0) { // Avoid touching global configuration here. Read the config files into // a temporary configuration tree, then throw it away after fsck is done. - splay_tree_t *config = NULL; + splay_tree_t config; init_configuration(&config); // Read the server configuration file and append host configuration for our node. - bool success = read_server_config(config) && - read_host_config(config, name, true); + bool success = read_server_config(&config) && + read_host_config(&config, name, true); // Check both RSA and EC key pairs. // We need working configuration to run this check. if(success) { - success = check_keypairs(config, name); + success = check_keypairs(&config, name); } // Check that scripts are executable and check the config for invalid variables. @@ -667,7 +667,7 @@ int fsck(const char *argv0) { // This way, we can diagnose more issues on the first run. success = success & check_scripts_and_configs(); - exit_configuration(&config); + splay_empty_tree(&config); free(name); exe_name = NULL; diff --git a/src/graph.c b/src/graph.c index 3a843063..bf3e75c7 100644 --- a/src/graph.c +++ b/src/graph.c @@ -72,13 +72,13 @@ static void mst_kruskal(void) { /* Clear visited status on nodes */ - for splay_each(node_t, n, node_tree) { + for splay_each(node_t, n, &node_tree) { n->status.visited = false; } /* Starting point */ - for splay_each(edge_t, e, edge_weight_tree) { + for splay_each(edge_t, e, &edge_weight_tree) { if(e->from->status.reachable) { e->from->status.visited = true; break; @@ -89,7 +89,7 @@ static void mst_kruskal(void) { bool skipped = false; - for splay_each(edge_t, e, edge_weight_tree) { + for splay_each(edge_t, e, &edge_weight_tree) { if(!e->reverse || (e->from->status.visited == e->to->status.visited)) { skipped = true; continue; @@ -110,7 +110,7 @@ static void mst_kruskal(void) { if(skipped) { skipped = false; - next = edge_weight_tree->head; + next = edge_weight_tree.head; } } } @@ -124,7 +124,7 @@ static void sssp_bfs(void) { /* Clear visited status on nodes */ - for splay_each(node_t, n, node_tree) { + for splay_each(node_t, n, &node_tree) { n->status.visited = false; n->status.indirect = true; n->distance = -1; @@ -213,7 +213,7 @@ static void check_reachability(void) { int became_reachable_count = 0; int became_unreachable_count = 0; - for splay_each(node_t, n, node_tree) { + for splay_each(node_t, n, &node_tree) { if(n->status.visited != n->status.reachable) { n->status.reachable = !n->status.reachable; n->last_state_change = now.tv_sec; diff --git a/src/keys.c b/src/keys.c index dacb87b3..5f93a5a9 100644 --- a/src/keys.c +++ b/src/keys.c @@ -171,7 +171,7 @@ bool read_ecdsa_public_key(ecdsa_t **ecdsa, splay_tree_t **config_tree, const ch char *p; if(!*config_tree) { - init_configuration(config_tree); + *config_tree = create_configuration(); if(!read_host_config(*config_tree, name, true)) { return false; diff --git a/src/linux/device.c b/src/linux/device.c index 3050fe2f..8e239591 100644 --- a/src/linux/device.c +++ b/src/linux/device.c @@ -44,11 +44,11 @@ static char ifrname[IFNAMSIZ]; static const char *device_info; static bool setup_device(void) { - if(!get_config_string(lookup_config(config_tree, "Device"), &device)) { + if(!get_config_string(lookup_config(&config_tree, "Device"), &device)) { device = xstrdup(DEFAULT_DEVICE); } - if(!get_config_string(lookup_config(config_tree, "Interface"), &iface)) + if(!get_config_string(lookup_config(&config_tree, "Interface"), &iface)) if(netname) { iface = xstrdup(netname); } @@ -66,7 +66,7 @@ static bool setup_device(void) { struct ifreq ifr = {0}; - get_config_string(lookup_config(config_tree, "DeviceType"), &type); + get_config_string(lookup_config(&config_tree, "DeviceType"), &type); if(type && strcasecmp(type, "tun") && strcasecmp(type, "tap")) { logger(DEBUG_ALWAYS, LOG_ERR, "Unknown device type %s!", type); @@ -92,7 +92,7 @@ static bool setup_device(void) { bool t1q = false; - if(get_config_bool(lookup_config(config_tree, "IffOneQueue"), &t1q) && t1q) { + if(get_config_bool(lookup_config(&config_tree, "IffOneQueue"), &t1q) && t1q) { ifr.ifr_flags |= IFF_ONE_QUEUE; } diff --git a/src/mingw/device.c b/src/mingw/device.c index bf8ae137..b7a191f4 100644 --- a/src/mingw/device.c +++ b/src/mingw/device.c @@ -108,8 +108,8 @@ static bool setup_device(void) { int err; - get_config_string(lookup_config(config_tree, "Device"), &device); - get_config_string(lookup_config(config_tree, "Interface"), &iface); + get_config_string(lookup_config(&config_tree, "Device"), &device); + get_config_string(lookup_config(&config_tree, "Interface"), &iface); if(device && iface) { logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: both Device and Interface specified, results may not be as expected"); diff --git a/src/multicast_device.c b/src/multicast_device.c index 82f7fec0..907eb100 100644 --- a/src/multicast_device.c +++ b/src/multicast_device.c @@ -39,9 +39,9 @@ static bool setup_device(void) { char *space; int ttl = 1; - get_config_string(lookup_config(config_tree, "Interface"), &iface); + get_config_string(lookup_config(&config_tree, "Interface"), &iface); - if(!get_config_string(lookup_config(config_tree, "Device"), &device)) { + if(!get_config_string(lookup_config(&config_tree, "Device"), &device)) { logger(DEBUG_ALWAYS, LOG_ERR, "Device variable required for %s", device_info); goto error; } diff --git a/src/net.c b/src/net.c index dffe0b47..78d4b76b 100644 --- a/src/net.c +++ b/src/net.c @@ -50,7 +50,7 @@ void purge(void) { /* Remove all edges and subnets owned by unreachable nodes. */ - for splay_each(node_t, n, node_tree) { + for splay_each(node_t, n, &node_tree) { if(!n->status.reachable) { logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Purging node %s (%s)", n->name, n->hostname); @@ -74,9 +74,9 @@ void purge(void) { /* Check if anyone else claims to have an edge to an unreachable node. If not, delete node. */ - for splay_each(node_t, n, node_tree) { + for splay_each(node_t, n, &node_tree) { if(!n->status.reachable) { - for splay_each(edge_t, e, edge_weight_tree) + for splay_each(edge_t, e, &edge_weight_tree) if(e->to == n) { return; } @@ -289,7 +289,7 @@ static void periodic_handler(void *data) { /* If AutoConnect is set, check if we need to make or break connections. */ - if(autoconnect && node_tree->count > 1) { + if(autoconnect && node_tree.count > 1) { do_autoconnect(); } @@ -335,18 +335,17 @@ int reload_configuration(void) { /* Reread our own configuration file */ - exit_configuration(&config_tree); - init_configuration(&config_tree); + splay_empty_tree(&config_tree); - if(!read_server_config(config_tree)) { + if(!read_server_config(&config_tree)) { logger(DEBUG_ALWAYS, LOG_ERR, "Unable to reread configuration file."); return EINVAL; } - read_config_options(config_tree, NULL); + read_config_options(&config_tree, NULL); snprintf(fname, sizeof(fname), "%s" SLASH "hosts" SLASH "%s", confbase, myself->name); - read_config_file(config_tree, fname, true); + read_config_file(&config_tree, fname, true); /* Parse some options that are allowed to be changed while tinc is running */ @@ -355,20 +354,20 @@ int reload_configuration(void) { /* If StrictSubnet is set, expire deleted Subnets and read new ones in */ if(strictsubnets) { - for splay_each(subnet_t, subnet, subnet_tree) + for splay_each(subnet_t, subnet, &subnet_tree) if(subnet->owner) { subnet->expires = 1; } } - for splay_each(node_t, n, node_tree) { + for splay_each(node_t, n, &node_tree) { n->status.has_address = false; } load_all_nodes(); if(strictsubnets) { - for splay_each(subnet_t, subnet, subnet_tree) { + for splay_each(subnet_t, subnet, &subnet_tree) { if(!subnet->owner) { continue; } @@ -397,7 +396,7 @@ int reload_configuration(void) { subnet->expires = 1; } - config_t *cfg = lookup_config(config_tree, "Subnet"); + config_t *cfg = lookup_config(&config_tree, "Subnet"); while(cfg) { subnet_t *subnet, *s2; @@ -416,7 +415,7 @@ int reload_configuration(void) { } } - cfg = lookup_config_next(config_tree, cfg); + cfg = lookup_config_next(&config_tree, cfg); } for splay_each(subnet_t, subnet, myself->subnet_tree) { diff --git a/src/net_packet.c b/src/net_packet.c index 3f2fb489..290f743f 100644 --- a/src/net_packet.c +++ b/src/net_packet.c @@ -1638,7 +1638,7 @@ void broadcast_packet(const node_t *from, vpn_packet_t *packet) { break; } - for splay_each(node_t, n, node_tree) + for splay_each(node_t, n, &node_tree) if(n->status.reachable && n != myself && ((n->via == myself && n->nexthop == n) || n->via == n)) { send_packet(n, packet); } @@ -1661,7 +1661,7 @@ static node_t *try_harder(const sockaddr_t *from, const vpn_packet_t *pkt) { bool hard = false; static time_t last_hard_try = 0; - for splay_each(node_t, n, node_tree) { + for splay_each(node_t, n, &node_tree) { if(!n->status.reachable || n == myself) { continue; } diff --git a/src/net_setup.c b/src/net_setup.c index 4e1f2b81..d4993fa9 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -71,20 +71,20 @@ bool node_read_ecdsa_public_key(node_t *n) { return true; } - splay_tree_t *config_tree; FILE *fp; char *pubname = NULL; char *p; - init_configuration(&config_tree); + splay_tree_t config; + init_configuration(&config); - if(!read_host_config(config_tree, n->name, true)) { + if(!read_host_config(&config, n->name, true)) { goto exit; } /* First, check for simple Ed25519PublicKey statement */ - if(get_config_string(lookup_config(config_tree, "Ed25519PublicKey"), &p)) { + if(get_config_string(lookup_config(&config, "Ed25519PublicKey"), &p)) { n->ecdsa = ecdsa_set_base64_public_key(p); free(p); goto exit; @@ -92,7 +92,7 @@ bool node_read_ecdsa_public_key(node_t *n) { /* Else, check for Ed25519PublicKeyFile statement and read it */ - if(!get_config_string(lookup_config(config_tree, "Ed25519PublicKeyFile"), &pubname)) { + if(!get_config_string(lookup_config(&config, "Ed25519PublicKeyFile"), &pubname)) { xasprintf(&pubname, "%s" SLASH "hosts" SLASH "%s", confbase, n->name); } @@ -106,7 +106,7 @@ bool node_read_ecdsa_public_key(node_t *n) { fclose(fp); exit: - exit_configuration(&config_tree); + splay_empty_tree(&config); free(pubname); return n->ecdsa; } @@ -151,7 +151,7 @@ void regenerate_key(void) { logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys"); send_key_changed(); - for splay_each(node_t, n, node_tree) { + for splay_each(node_t, n, &node_tree) { n->status.validkey_in = false; } } @@ -176,10 +176,10 @@ void load_all_nodes(void) { node_t *n = lookup_node(ent->d_name); - splay_tree_t *config_tree; - init_configuration(&config_tree); - read_config_options(config_tree, ent->d_name); - read_host_config(config_tree, ent->d_name, true); + splay_tree_t config; + init_configuration(&config); + read_config_options(&config, ent->d_name); + read_host_config(&config, ent->d_name, true); if(!n) { n = new_node(); @@ -188,7 +188,7 @@ void load_all_nodes(void) { } if(strictsubnets) { - for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) { + for(config_t *cfg = lookup_config(&config, "Subnet"); cfg; cfg = lookup_config_next(&config, cfg)) { subnet_t *s, *s2; if(!get_config_subnet(cfg, &s)) { @@ -204,11 +204,11 @@ void load_all_nodes(void) { } } - if(lookup_config(config_tree, "Address")) { + if(lookup_config(&config, "Address")) { n->status.has_address = true; } - exit_configuration(&config_tree); + splay_empty_tree(&config); } closedir(dir); @@ -218,7 +218,7 @@ char *get_name(void) { char *name = NULL; char *returned_name; - get_config_string(lookup_config(config_tree, "Name"), &name); + get_config_string(lookup_config(&config_tree, "Name"), &name); if(!name) { return NULL; @@ -233,17 +233,17 @@ bool setup_myself_reloadable(void) { free(scriptinterpreter); scriptinterpreter = NULL; - get_config_string(lookup_config(config_tree, "ScriptsInterpreter"), &scriptinterpreter); + get_config_string(lookup_config(&config_tree, "ScriptsInterpreter"), &scriptinterpreter); free(scriptextension); - if(!get_config_string(lookup_config(config_tree, "ScriptsExtension"), &scriptextension)) { + if(!get_config_string(lookup_config(&config_tree, "ScriptsExtension"), &scriptextension)) { scriptextension = xstrdup(""); } char *proxy = NULL; - get_config_string(lookup_config(config_tree, "Proxy"), &proxy); + get_config_string(lookup_config(&config_tree, "Proxy"), &proxy); if(proxy) { char *space; @@ -342,11 +342,11 @@ bool setup_myself_reloadable(void) { bool choice; - if(get_config_bool(lookup_config(config_tree, "IndirectData"), &choice) && choice) { + if(get_config_bool(lookup_config(&config_tree, "IndirectData"), &choice) && choice) { myself->options |= OPTION_INDIRECT; } - if(get_config_bool(lookup_config(config_tree, "TCPOnly"), &choice) && choice) { + if(get_config_bool(lookup_config(&config_tree, "TCPOnly"), &choice) && choice) { myself->options |= OPTION_TCPONLY; } @@ -354,20 +354,20 @@ bool setup_myself_reloadable(void) { myself->options |= OPTION_INDIRECT; } - get_config_bool(lookup_config(config_tree, "UDPDiscovery"), &udp_discovery); - get_config_int(lookup_config(config_tree, "UDPDiscoveryKeepaliveInterval"), &udp_discovery_keepalive_interval); - get_config_int(lookup_config(config_tree, "UDPDiscoveryInterval"), &udp_discovery_interval); - get_config_int(lookup_config(config_tree, "UDPDiscoveryTimeout"), &udp_discovery_timeout); + get_config_bool(lookup_config(&config_tree, "UDPDiscovery"), &udp_discovery); + get_config_int(lookup_config(&config_tree, "UDPDiscoveryKeepaliveInterval"), &udp_discovery_keepalive_interval); + get_config_int(lookup_config(&config_tree, "UDPDiscoveryInterval"), &udp_discovery_interval); + get_config_int(lookup_config(&config_tree, "UDPDiscoveryTimeout"), &udp_discovery_timeout); - get_config_int(lookup_config(config_tree, "MTUInfoInterval"), &mtu_info_interval); - get_config_int(lookup_config(config_tree, "UDPInfoInterval"), &udp_info_interval); + get_config_int(lookup_config(&config_tree, "MTUInfoInterval"), &mtu_info_interval); + get_config_int(lookup_config(&config_tree, "UDPInfoInterval"), &udp_info_interval); - get_config_bool(lookup_config(config_tree, "DirectOnly"), &directonly); - get_config_bool(lookup_config(config_tree, "LocalDiscovery"), &localdiscovery); + get_config_bool(lookup_config(&config_tree, "DirectOnly"), &directonly); + get_config_bool(lookup_config(&config_tree, "LocalDiscovery"), &localdiscovery); char *rmode = NULL; - if(get_config_string(lookup_config(config_tree, "Mode"), &rmode)) { + if(get_config_string(lookup_config(&config_tree, "Mode"), &rmode)) { if(!strcasecmp(rmode, "router")) { routing_mode = RMODE_ROUTER; } else if(!strcasecmp(rmode, "switch")) { @@ -385,7 +385,7 @@ bool setup_myself_reloadable(void) { char *fmode = NULL; - if(get_config_string(lookup_config(config_tree, "Forwarding"), &fmode)) { + if(get_config_string(lookup_config(&config_tree, "Forwarding"), &fmode)) { if(!strcasecmp(fmode, "off")) { forwarding_mode = FMODE_OFF; } else if(!strcasecmp(fmode, "internal")) { @@ -402,25 +402,25 @@ bool setup_myself_reloadable(void) { } choice = !(myself->options & OPTION_TCPONLY); - get_config_bool(lookup_config(config_tree, "PMTUDiscovery"), &choice); + get_config_bool(lookup_config(&config_tree, "PMTUDiscovery"), &choice); if(choice) { myself->options |= OPTION_PMTU_DISCOVERY; } choice = true; - get_config_bool(lookup_config(config_tree, "ClampMSS"), &choice); + get_config_bool(lookup_config(&config_tree, "ClampMSS"), &choice); if(choice) { myself->options |= OPTION_CLAMP_MSS; } - get_config_bool(lookup_config(config_tree, "PriorityInheritance"), &priorityinheritance); - get_config_bool(lookup_config(config_tree, "DecrementTTL"), &decrement_ttl); + get_config_bool(lookup_config(&config_tree, "PriorityInheritance"), &priorityinheritance); + get_config_bool(lookup_config(&config_tree, "DecrementTTL"), &decrement_ttl); char *bmode = NULL; - if(get_config_string(lookup_config(config_tree, "Broadcast"), &bmode)) { + if(get_config_string(lookup_config(&config_tree, "Broadcast"), &bmode)) { if(!strcasecmp(bmode, "no")) { broadcast_mode = BMODE_NONE; } else if(!strcasecmp(bmode, "yes") || !strcasecmp(bmode, "mst")) { @@ -438,9 +438,9 @@ bool setup_myself_reloadable(void) { /* Delete all broadcast subnets before re-adding them */ - for splay_each(subnet_t, s, subnet_tree) { + for splay_each(subnet_t, s, &subnet_tree) { if(!s->owner) { - splay_delete_node(subnet_tree, node); + splay_delete_node(&subnet_tree, node); } } @@ -456,7 +456,7 @@ bool setup_myself_reloadable(void) { subnet_add(NULL, s); } - for(config_t *cfg = lookup_config(config_tree, "BroadcastSubnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) { + for(config_t *cfg = lookup_config(&config_tree, "BroadcastSubnet"); cfg; cfg = lookup_config_next(&config_tree, cfg)) { subnet_t *s; if(!get_config_subnet(cfg, &s)) { @@ -482,11 +482,11 @@ bool setup_myself_reloadable(void) { #endif - if(!get_config_int(lookup_config(config_tree, "MACExpire"), &macexpire)) { + if(!get_config_int(lookup_config(&config_tree, "MACExpire"), &macexpire)) { macexpire = 600; } - if(get_config_int(lookup_config(config_tree, "MaxTimeout"), &maxtimeout)) { + if(get_config_int(lookup_config(&config_tree, "MaxTimeout"), &maxtimeout)) { if(maxtimeout <= 0) { logger(DEBUG_ALWAYS, LOG_ERR, "Bogus maximum timeout!"); return false; @@ -497,7 +497,7 @@ bool setup_myself_reloadable(void) { char *afname = NULL; - if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) { + if(get_config_string(lookup_config(&config_tree, "AddressFamily"), &afname)) { if(!strcasecmp(afname, "IPv4")) { addressfamily = AF_INET; } else if(!strcasecmp(afname, "IPv6")) { @@ -513,19 +513,19 @@ bool setup_myself_reloadable(void) { free(afname); } - get_config_bool(lookup_config(config_tree, "Hostnames"), &hostnames); + get_config_bool(lookup_config(&config_tree, "Hostnames"), &hostnames); - if(!get_config_int(lookup_config(config_tree, "KeyExpire"), &keylifetime)) { + if(!get_config_int(lookup_config(&config_tree, "KeyExpire"), &keylifetime)) { keylifetime = 3600; } - if(!get_config_bool(lookup_config(config_tree, "AutoConnect"), &autoconnect)) { + if(!get_config_bool(lookup_config(&config_tree, "AutoConnect"), &autoconnect)) { autoconnect = true; } - get_config_bool(lookup_config(config_tree, "DisableBuggyPeers"), &disablebuggypeers); + get_config_bool(lookup_config(&config_tree, "DisableBuggyPeers"), &disablebuggypeers); - if(!get_config_int(lookup_config(config_tree, "InvitationExpire"), &invitation_lifetime)) { + if(!get_config_int(lookup_config(&config_tree, "InvitationExpire"), &invitation_lifetime)) { invitation_lifetime = 604800; // 1 week } @@ -669,9 +669,9 @@ static bool setup_myself(void) { myself->connection = new_connection(); myself->name = name; myself->connection->name = xstrdup(name); - read_host_config(config_tree, name, true); + read_host_config(&config_tree, name, true); - if(!get_config_string(lookup_config(config_tree, "Port"), &myport)) { + if(!get_config_string(lookup_config(&config_tree, "Port"), &myport)) { myport = xstrdup("655"); } else { port_specified = true; @@ -684,7 +684,7 @@ static bool setup_myself(void) { myself->options |= PROT_MINOR << 24; #ifdef DISABLE_LEGACY - myself->connection->ecdsa = read_ecdsa_private_key(config_tree, NULL); + myself->connection->ecdsa = read_ecdsa_private_key(&config_tree, NULL); experimental = myself->connection->ecdsa != NULL; if(!experimental) { @@ -694,8 +694,8 @@ static bool setup_myself(void) { #else - if(!get_config_bool(lookup_config(config_tree, "ExperimentalProtocol"), &experimental)) { - myself->connection->ecdsa = read_ecdsa_private_key(config_tree, NULL); + if(!get_config_bool(lookup_config(&config_tree, "ExperimentalProtocol"), &experimental)) { + myself->connection->ecdsa = read_ecdsa_private_key(&config_tree, NULL); experimental = myself->connection->ecdsa != NULL; if(!experimental) { @@ -703,7 +703,7 @@ static bool setup_myself(void) { } } else { if(experimental) { - myself->connection->ecdsa = read_ecdsa_private_key(config_tree, NULL); + myself->connection->ecdsa = read_ecdsa_private_key(&config_tree, NULL); if(!myself->connection->ecdsa) { return false; @@ -711,7 +711,7 @@ static bool setup_myself(void) { } } - myself->connection->rsa = read_rsa_private_key(config_tree, NULL); + myself->connection->rsa = read_rsa_private_key(&config_tree, NULL); if(!myself->connection->rsa) { if(experimental) { @@ -742,7 +742,7 @@ static bool setup_myself(void) { /* Read in all the subnets specified in the host configuration file */ - for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) { + for(config_t *cfg = lookup_config(&config_tree, "Subnet"); cfg; cfg = lookup_config_next(&config_tree, cfg)) { subnet_t *subnet; if(!get_config_subnet(cfg, &subnet)) { @@ -758,18 +758,18 @@ static bool setup_myself(void) { return false; } - get_config_bool(lookup_config(config_tree, "StrictSubnets"), &strictsubnets); - get_config_bool(lookup_config(config_tree, "TunnelServer"), &tunnelserver); + get_config_bool(lookup_config(&config_tree, "StrictSubnets"), &strictsubnets); + get_config_bool(lookup_config(&config_tree, "TunnelServer"), &tunnelserver); strictsubnets |= tunnelserver; - if(get_config_int(lookup_config(config_tree, "MaxConnectionBurst"), &max_connection_burst)) { + if(get_config_int(lookup_config(&config_tree, "MaxConnectionBurst"), &max_connection_burst)) { if(max_connection_burst <= 0) { logger(DEBUG_ALWAYS, LOG_ERR, "MaxConnectionBurst cannot be negative!"); return false; } } - if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) { + if(get_config_int(lookup_config(&config_tree, "UDPRcvBuf"), &udp_rcvbuf)) { if(udp_rcvbuf < 0) { logger(DEBUG_ALWAYS, LOG_ERR, "UDPRcvBuf cannot be negative!"); return false; @@ -778,7 +778,7 @@ static bool setup_myself(void) { udp_rcvbuf_warnings = true; } - if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) { + if(get_config_int(lookup_config(&config_tree, "UDPSndBuf"), &udp_sndbuf)) { if(udp_sndbuf < 0) { logger(DEBUG_ALWAYS, LOG_ERR, "UDPSndBuf cannot be negative!"); return false; @@ -787,7 +787,7 @@ static bool setup_myself(void) { udp_sndbuf_warnings = true; } - get_config_int(lookup_config(config_tree, "FWMark"), &fwmark); + get_config_int(lookup_config(&config_tree, "FWMark"), &fwmark); #ifndef SO_MARK if(fwmark) { @@ -799,7 +799,7 @@ static bool setup_myself(void) { int replaywin_int; - if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) { + if(get_config_int(lookup_config(&config_tree, "ReplayWindow"), &replaywin_int)) { if(replaywin_int < 0) { logger(DEBUG_ALWAYS, LOG_ERR, "ReplayWindow cannot be negative!"); return false; @@ -814,7 +814,7 @@ static bool setup_myself(void) { char *cipher; - if(!get_config_string(lookup_config(config_tree, "Cipher"), &cipher)) { + if(!get_config_string(lookup_config(&config_tree, "Cipher"), &cipher)) { cipher = xstrdup("aes-256-cbc"); } @@ -835,7 +835,7 @@ static bool setup_myself(void) { /* Check if we want to use message authentication codes... */ int maclength = 4; - get_config_int(lookup_config(config_tree, "MACLength"), &maclength); + get_config_int(lookup_config(&config_tree, "MACLength"), &maclength); if(maclength < 0) { logger(DEBUG_ALWAYS, LOG_ERR, "Bogus MAC length!"); @@ -844,7 +844,7 @@ static bool setup_myself(void) { char *digest; - if(!get_config_string(lookup_config(config_tree, "Digest"), &digest)) { + if(!get_config_string(lookup_config(&config_tree, "Digest"), &digest)) { digest = xstrdup("sha256"); } @@ -860,7 +860,7 @@ static bool setup_myself(void) { #endif /* Compression */ - if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) { + if(get_config_int(lookup_config(&config_tree, "Compression"), &myself->incompression)) { switch(myself->incompression) { case COMPRESS_LZ4: #ifdef HAVE_LZ4 @@ -929,7 +929,7 @@ static bool setup_myself(void) { devops = os_devops; - if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) { + if(get_config_string(lookup_config(&config_tree, "DeviceType"), &type)) { if(!strcasecmp(type, "dummy")) { devops = dummy_devops; } else if(!strcasecmp(type, "raw_socket")) { @@ -959,7 +959,7 @@ static bool setup_myself(void) { free(type); } - get_config_bool(lookup_config(config_tree, "DeviceStandby"), &device_standby); + get_config_bool(lookup_config(&config_tree, "DeviceStandby"), &device_standby); if(!devops.setup()) { return false; @@ -1018,7 +1018,7 @@ static bool setup_myself(void) { listen_sockets = 0; int cfgs = 0; - for(config_t *cfg = lookup_config(config_tree, "BindToAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) { + for(config_t *cfg = lookup_config(&config_tree, "BindToAddress"); cfg; cfg = lookup_config_next(&config_tree, cfg)) { cfgs++; get_config_string(cfg, &address); @@ -1027,7 +1027,7 @@ static bool setup_myself(void) { } } - for(config_t *cfg = lookup_config(config_tree, "ListenAddress"); cfg; cfg = lookup_config_next(config_tree, cfg)) { + for(config_t *cfg = lookup_config(&config_tree, "ListenAddress"); cfg; cfg = lookup_config_next(&config_tree, cfg)) { cfgs++; get_config_string(cfg, &address); @@ -1067,7 +1067,7 @@ static bool setup_myself(void) { myself->connection->hostname = xstrdup(myself->hostname); char *upnp = NULL; - get_config_string(lookup_config(config_tree, "UPnP"), &upnp); + get_config_string(lookup_config(&config_tree, "UPnP"), &upnp); bool upnp_tcp = false; bool upnp_udp = false; @@ -1102,11 +1102,8 @@ static bool setup_myself(void) { bool setup_network(void) { init_connections(); init_subnets(); - init_nodes(); - init_edges(); - init_requests(); - if(get_config_int(lookup_config(config_tree, "PingInterval"), &pinginterval)) { + if(get_config_int(lookup_config(&config_tree, "PingInterval"), &pinginterval)) { if(pinginterval < 1) { pinginterval = 86400; } @@ -1114,7 +1111,7 @@ bool setup_network(void) { pinginterval = 60; } - if(!get_config_int(lookup_config(config_tree, "PingTimeout"), &pingtimeout)) { + if(!get_config_int(lookup_config(&config_tree, "PingTimeout"), &pingtimeout)) { pingtimeout = 5; } @@ -1122,7 +1119,7 @@ bool setup_network(void) { pingtimeout = pinginterval; } - if(!get_config_int(lookup_config(config_tree, "MaxOutputBufferSize"), &maxoutbufsize)) { + if(!get_config_int(lookup_config(&config_tree, "MaxOutputBufferSize"), &maxoutbufsize)) { maxoutbufsize = 10 * MTU; } diff --git a/src/net_socket.c b/src/net_socket.c index a29398d3..6439a743 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -115,7 +115,7 @@ static bool bind_to_interface(int sd) { int status; #endif /* defined(SOL_SOCKET) && defined(SO_BINDTODEVICE) */ - if(!get_config_string(lookup_config(config_tree, "BindToInterface"), &iface)) { + if(!get_config_string(lookup_config(&config_tree, "BindToInterface"), &iface)) { return true; } @@ -216,7 +216,7 @@ int setup_listen_socket(const sockaddr_t *sa) { #endif if(get_config_string - (lookup_config(config_tree, "BindToInterface"), &iface)) { + (lookup_config(&config_tree, "BindToInterface"), &iface)) { #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE) struct ifreq ifr; @@ -829,7 +829,7 @@ void try_outgoing_connections(void) { /* Make sure there is one outgoing_t in the list for each ConnectTo. */ - for(config_t *cfg = lookup_config(config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(config_tree, cfg)) { + for(config_t *cfg = lookup_config(&config_tree, "ConnectTo"); cfg; cfg = lookup_config_next(&config_tree, cfg)) { char *name; get_config_string(cfg, &name); diff --git a/src/node.c b/src/node.c index b9897e89..850f2dae 100644 --- a/src/node.c +++ b/src/node.c @@ -32,10 +32,6 @@ #include "ed25519/sha512.h" -splay_tree_t *node_tree; -static splay_tree_t *node_id_tree; -static splay_tree_t *node_udp_tree; - node_t *myself; static int node_compare(const node_t *a, const node_t *b) { @@ -56,16 +52,23 @@ static int node_udp_compare(const node_t *a, const node_t *b) { return (a->name && b->name) ? strcmp(a->name, b->name) : 0; } -void init_nodes(void) { - node_tree = splay_alloc_tree((splay_compare_t) node_compare, (splay_action_t) free_node); - node_id_tree = splay_alloc_tree((splay_compare_t) node_id_compare, NULL); - node_udp_tree = splay_alloc_tree((splay_compare_t) node_udp_compare, NULL); -} +splay_tree_t node_tree = { + .compare = (splay_compare_t) node_compare, + .delete = (splay_action_t) free_node, +}; + +static splay_tree_t node_id_tree = { + .compare = (splay_compare_t) node_id_compare, +}; + +static splay_tree_t node_udp_tree = { + .compare = (splay_compare_t) node_udp_compare, +}; void exit_nodes(void) { - splay_delete_tree(node_udp_tree); - splay_delete_tree(node_id_tree); - splay_delete_tree(node_tree); + splay_empty_tree(&node_udp_tree); + splay_empty_tree(&node_id_tree); + splay_empty_tree(&node_tree); } node_t *new_node(void) { @@ -123,12 +126,12 @@ void node_add(node_t *n) { sha512(n->name, strlen(n->name), buf); memcpy(&n->id, buf, sizeof(n->id)); - splay_insert(node_tree, n); - splay_insert(node_id_tree, n); + splay_insert(&node_tree, n); + splay_insert(&node_id_tree, n); } void node_del(node_t *n) { - splay_delete(node_udp_tree, n); + splay_delete(&node_udp_tree, n); for splay_each(subnet_t, s, n->subnet_tree) { subnet_del(n, s); @@ -138,8 +141,8 @@ void node_del(node_t *n) { edge_del(e); } - splay_delete(node_id_tree, n); - splay_delete(node_tree, n); + splay_delete(&node_id_tree, n); + splay_delete(&node_tree, n); } node_t *lookup_node(char *name) { @@ -147,17 +150,17 @@ node_t *lookup_node(char *name) { n.name = name; - return splay_search(node_tree, &n); + return splay_search(&node_tree, &n); } node_t *lookup_node_id(const node_id_t *id) { node_t n = {.id = *id}; - return splay_search(node_id_tree, &n); + return splay_search(&node_id_tree, &n); } node_t *lookup_node_udp(const sockaddr_t *sa) { node_t tmp = {.address = *sa}; - return splay_search(node_udp_tree, &tmp); + return splay_search(&node_udp_tree, &tmp); } void update_node_udp(node_t *n, const sockaddr_t *sa) { @@ -166,7 +169,7 @@ void update_node_udp(node_t *n, const sockaddr_t *sa) { return; } - splay_delete(node_udp_tree, n); + splay_delete(&node_udp_tree, n); if(sa) { n->address = *sa; @@ -179,7 +182,7 @@ void update_node_udp(node_t *n, const sockaddr_t *sa) { } } - splay_insert(node_udp_tree, n); + splay_insert(&node_udp_tree, n); free(n->hostname); n->hostname = sockaddr2hostname(&n->address); logger(DEBUG_PROTOCOL, LOG_DEBUG, "UDP address of %s set to %s", n->name, n->hostname); @@ -195,7 +198,7 @@ void update_node_udp(node_t *n, const sockaddr_t *sa) { } bool dump_nodes(connection_t *c) { - for splay_each(node_t, n, node_tree) { + for splay_each(node_t, n, &node_tree) { char id[2 * sizeof(n->id) + 1]; for(size_t c = 0; c < sizeof(n->id); ++c) { @@ -220,7 +223,7 @@ bool dump_nodes(connection_t *c) { } bool dump_traffic(connection_t *c) { - for splay_each(node_t, n, node_tree) + for splay_each(node_t, n, &node_tree) send_request(c, "%d %d %s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, CONTROL, REQ_DUMP_TRAFFIC, n->name, n->in_packets, n->in_bytes, n->out_packets, n->out_bytes); diff --git a/src/node.h b/src/node.h index edb3408b..b6039684 100644 --- a/src/node.h +++ b/src/node.h @@ -115,9 +115,8 @@ typedef struct node_t { } node_t; extern struct node_t *myself; -extern splay_tree_t *node_tree; +extern splay_tree_t node_tree; -extern void init_nodes(void); extern void exit_nodes(void); extern node_t *new_node(void) __attribute__((__malloc__)); extern void free_node(node_t *n); diff --git a/src/protocol.c b/src/protocol.c index ed34f523..7de75b93 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -56,7 +56,19 @@ static char (*request_name[]) = { "REQ_PUBKEY", "ANS_PUBKEY", "SPTPS_PACKET", "UDP_INFO", "MTU_INFO", }; -static splay_tree_t *past_request_tree; +static int past_request_compare(const past_request_t *a, const past_request_t *b) { + return strcmp(a->request, b->request); +} + +static void free_past_request(past_request_t *r) { + free((char *)r->request); + free(r); +} + +static splay_tree_t past_request_tree = { + .compare = (splay_compare_t) past_request_compare, + .delete = (splay_action_t) free_past_request, +}; /* Generic request routines - takes care of logging and error detection as well */ @@ -159,24 +171,15 @@ bool receive_request(connection_t *c, const char *request) { return true; } -static int past_request_compare(const past_request_t *a, const past_request_t *b) { - return strcmp(a->request, b->request); -} - -static void free_past_request(past_request_t *r) { - free((char *)r->request); - free(r); -} - static timeout_t past_request_timeout; static void age_past_requests(void *data) { (void)data; int left = 0, deleted = 0; - for splay_each(past_request_t, p, past_request_tree) { + for splay_each(past_request_t, p, &past_request_tree) { if(p->firstseen + pinginterval <= now.tv_sec) { - splay_delete_node(past_request_tree, node), deleted++; + splay_delete_node(&past_request_tree, node), deleted++; } else { left++; } @@ -197,14 +200,14 @@ bool seen_request(const char *request) { p.request = request; - if(splay_search(past_request_tree, &p)) { + if(splay_search(&past_request_tree, &p)) { logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Already seen request"); return true; } else { new = xmalloc(sizeof(*new)); new->request = xstrdup(request); new->firstseen = now.tv_sec; - splay_insert(past_request_tree, new); + splay_insert(&past_request_tree, new); timeout_add(&past_request_timeout, age_past_requests, NULL, &(struct timeval) { 10, rand() % 100000 }); @@ -212,12 +215,8 @@ bool seen_request(const char *request) { } } -void init_requests(void) { - past_request_tree = splay_alloc_tree((splay_compare_t) past_request_compare, (splay_action_t) free_past_request); -} - void exit_requests(void) { - splay_delete_tree(past_request_tree); + splay_empty_tree(&past_request_tree); timeout_del(&past_request_timeout); } diff --git a/src/protocol.h b/src/protocol.h index 4ea0c8c7..fd1676cb 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -84,7 +84,6 @@ extern bool send_request(struct connection_t *c, const char *format, ...) __attr extern void forward_request(struct connection_t *c, const char *request); extern bool receive_request(struct connection_t *c, const char *request); -extern void init_requests(void); extern void exit_requests(void); extern bool seen_request(const char *request); diff --git a/src/protocol_auth.c b/src/protocol_auth.c index e16fe4fc..33341296 100644 --- a/src/protocol_auth.c +++ b/src/protocol_auth.c @@ -427,7 +427,7 @@ bool id_h(connection_t *c, const char *request) { if(bypass_security) { if(!c->config_tree) { - init_configuration(&c->config_tree); + c->config_tree = create_configuration(); } c->allow_request = ACK; @@ -444,7 +444,7 @@ bool id_h(connection_t *c, const char *request) { } if(!c->config_tree) { - init_configuration(&c->config_tree); + c->config_tree = create_configuration(); if(!read_host_config(c->config_tree, c->name, false)) { logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s had unknown identity (%s)", c->hostname, c->name); @@ -865,7 +865,7 @@ bool send_ack(connection_t *c) { } if(!get_config_int(lookup_config(c->config_tree, "Weight"), &c->estimated_weight)) { - get_config_int(lookup_config(config_tree, "Weight"), &c->estimated_weight); + get_config_int(lookup_config(&config_tree, "Weight"), &c->estimated_weight); } return send_request(c, "%d %s %d %x", ACK, myport, c->estimated_weight, (c->options & 0xffffff) | (experimental ? (PROT_MINOR << 24) : 0)); @@ -893,7 +893,7 @@ static void send_everything(connection_t *c) { return; } - for splay_each(node_t, n, node_tree) { + for splay_each(node_t, n, &node_tree) { for splay_each(subnet_t, s, n->subnet_tree) { send_add_subnet(c, s); } @@ -1005,7 +1005,7 @@ bool ack_h(connection_t *c, const char *request) { n->mtu = mtu; } - if(get_config_int(lookup_config(config_tree, "PMTU"), &mtu) && mtu < n->mtu) { + if(get_config_int(lookup_config(&config_tree, "PMTU"), &mtu) && mtu < n->mtu) { n->mtu = mtu; } diff --git a/src/protocol_edge.c b/src/protocol_edge.c index daceb2e0..524566f0 100644 --- a/src/protocol_edge.c +++ b/src/protocol_edge.c @@ -177,9 +177,9 @@ bool add_edge_h(connection_t *c, const char *request) { } if(e->weight != weight) { - splay_node_t *node = splay_unlink(edge_weight_tree, e); + splay_node_t *node = splay_unlink(&edge_weight_tree, e); e->weight = weight; - splay_insert_node(edge_weight_tree, node); + splay_insert_node(&edge_weight_tree, node); } } else if(from == myself) { logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself which does not exist", diff --git a/src/protocol_key.c b/src/protocol_key.c index b69d5adc..a957780b 100644 --- a/src/protocol_key.c +++ b/src/protocol_key.c @@ -50,7 +50,7 @@ void send_key_changed(void) { /* Force key exchange for connections using SPTPS */ if(experimental) { - for splay_each(node_t, n, node_tree) { + for splay_each(node_t, n, &node_tree) { if(n->status.reachable && n->status.validkey && n->status.sptps) { sptps_force_kex(&n->sptps); } diff --git a/src/raw_socket_device.c b/src/raw_socket_device.c index 4dd18e2b..8463502d 100644 --- a/src/raw_socket_device.c +++ b/src/raw_socket_device.c @@ -37,11 +37,11 @@ static bool setup_device(void) { struct ifreq ifr; struct sockaddr_ll sa; - if(!get_config_string(lookup_config(config_tree, "Interface"), &iface)) { + if(!get_config_string(lookup_config(&config_tree, "Interface"), &iface)) { iface = xstrdup("eth0"); } - if(!get_config_string(lookup_config(config_tree, "Device"), &device)) { + if(!get_config_string(lookup_config(&config_tree, "Device"), &device)) { device = xstrdup(iface); } diff --git a/src/solaris/device.c b/src/solaris/device.c index d1e114ae..78bb4370 100644 --- a/src/solaris/device.c +++ b/src/solaris/device.c @@ -58,7 +58,7 @@ static const char *device_info = NULL; static bool setup_device(void) { char *type; - if(!get_config_string(lookup_config(config_tree, "Device"), &device)) { + if(!get_config_string(lookup_config(&config_tree, "Device"), &device)) { if(routing_mode == RMODE_ROUTER) { device = xstrdup(DEFAULT_TUN_DEVICE); } else { @@ -66,7 +66,7 @@ static bool setup_device(void) { } } - if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) { + if(get_config_string(lookup_config(&config_tree, "DeviceType"), &type)) { if(!strcasecmp(type, "tun")) /* use default */; else if(!strcasecmp(type, "tap")) { @@ -102,7 +102,7 @@ static bool setup_device(void) { /* Get unit number. */ char *ptr = device; - get_config_string(lookup_config(config_tree, "Interface"), &ptr); + get_config_string(lookup_config(&config_tree, "Interface"), &ptr); while(*ptr && !isdigit((uint8_t) *ptr)) { ptr++; diff --git a/src/splay_tree.c b/src/splay_tree.c index 4d973732..50d701c9 100644 --- a/src/splay_tree.c +++ b/src/splay_tree.c @@ -604,12 +604,21 @@ void splay_delete(splay_tree_t *tree, void *data) { /* Fast tree cleanup */ -void splay_delete_tree(splay_tree_t *tree) { +void splay_empty_tree(splay_tree_t *tree) { for(splay_node_t *node = tree->head, *next; node; node = next) { next = node->next; splay_free_node(tree, node); } + tree->head = NULL; + tree->tail = NULL; + tree->root = NULL; + tree->count = 0; + tree->generation++; +} + +void splay_delete_tree(splay_tree_t *tree) { + splay_empty_tree(tree); splay_free_tree(tree); } diff --git a/src/splay_tree.h b/src/splay_tree.h index 86d2b067..da1658a1 100644 --- a/src/splay_tree.h +++ b/src/splay_tree.h @@ -85,6 +85,7 @@ extern void splay_delete_node(splay_tree_t *tree, splay_node_t *node); /* Fast tree cleanup */ +extern void splay_empty_tree(splay_tree_t *tree); extern void splay_delete_tree(splay_tree_t *tree); /* Searching */ diff --git a/src/subnet.c b/src/subnet.c index 13c46030..e0d1acbc 100644 --- a/src/subnet.c +++ b/src/subnet.c @@ -33,7 +33,10 @@ /* lists type of subnet */ -splay_tree_t *subnet_tree; +splay_tree_t subnet_tree = { + .compare = (splay_compare_t) subnet_compare, + .delete = (splay_action_t) free_subnet, +}; /* Subnet lookup cache */ @@ -50,15 +53,13 @@ void subnet_cache_flush(void) { /* Initialising trees */ void init_subnets(void) { - subnet_tree = splay_alloc_tree((splay_compare_t) subnet_compare, (splay_action_t) free_subnet); - ipv4_cache = hash_alloc(0x100, sizeof(ipv4_t)); ipv6_cache = hash_alloc(0x100, sizeof(ipv6_t)); mac_cache = hash_alloc(0x100, sizeof(mac_t)); } void exit_subnets(void) { - splay_delete_tree(subnet_tree); + splay_empty_tree(&subnet_tree); hash_free(ipv4_cache); hash_free(ipv6_cache); @@ -88,7 +89,7 @@ void free_subnet(subnet_t *subnet) { void subnet_add(node_t *n, subnet_t *subnet) { subnet->owner = n; - splay_insert(subnet_tree, subnet); + splay_insert(&subnet_tree, subnet); if(n) { splay_insert(n->subnet_tree, subnet); @@ -102,7 +103,7 @@ void subnet_del(node_t *n, subnet_t *subnet) { splay_delete(n->subnet_tree, subnet); } - splay_delete(subnet_tree, subnet); + splay_delete(&subnet_tree, subnet); subnet_cache_flush(); } @@ -124,7 +125,7 @@ subnet_t *lookup_subnet_mac(const node_t *owner, const mac_t *address) { // Search all subnets for a matching one - for splay_each(subnet_t, p, owner ? owner->subnet_tree : subnet_tree) { + for splay_each(subnet_t, p, owner ? owner->subnet_tree : &subnet_tree) { if(!p || p->type != SUBNET_MAC) { continue; } @@ -158,7 +159,7 @@ subnet_t *lookup_subnet_ipv4(const ipv4_t *address) { // Search all subnets for a matching one - for splay_each(subnet_t, p, subnet_tree) { + for splay_each(subnet_t, p, &subnet_tree) { if(!p || p->type != SUBNET_IPV4) { continue; } @@ -192,7 +193,7 @@ subnet_t *lookup_subnet_ipv6(const ipv6_t *address) { // Search all subnets for a matching one - for splay_each(subnet_t, p, subnet_tree) { + for splay_each(subnet_t, p, &subnet_tree) { if(!p || p->type != SUBNET_IPV6) { continue; } @@ -283,7 +284,7 @@ void subnet_update(node_t *owner, subnet_t *subnet, bool up) { } bool dump_subnets(connection_t *c) { - for splay_each(subnet_t, subnet, subnet_tree) { + for splay_each(subnet_t, subnet, &subnet_tree) { char netstr[MAXNETSTR]; if(!net2str(netstr, sizeof(netstr), subnet)) { diff --git a/src/subnet.h b/src/subnet.h index f50b13ba..2e27f68f 100644 --- a/src/subnet.h +++ b/src/subnet.h @@ -63,7 +63,7 @@ typedef struct subnet_t { #define MAXNETSTR 64 -extern splay_tree_t *subnet_tree; +extern splay_tree_t subnet_tree; extern int subnet_compare(const struct subnet_t *a, const struct subnet_t *b); extern subnet_t *new_subnet(void) __attribute__((__malloc__)); diff --git a/src/tincd.c b/src/tincd.c index dee38ef1..dff420a4 100644 --- a/src/tincd.c +++ b/src/tincd.c @@ -379,10 +379,7 @@ static BOOL WINAPI console_ctrl_handler(DWORD type) { #endif static void cleanup() { - if(config_tree) { - exit_configuration(&config_tree); - } - + splay_empty_tree(&config_tree); list_empty_list(&cmdline_conf); free_names(); } @@ -492,22 +489,20 @@ int main(int argc, char **argv) { unsetenv("LISTEN_PID"); #endif - init_configuration(&config_tree); - /* Slllluuuuuuurrrrp! */ gettimeofday(&now, NULL); srand(now.tv_sec + now.tv_usec); crypto_init(); - if(!read_server_config(config_tree)) { + if(!read_server_config(&config_tree)) { return 1; } if(debug_level == DEBUG_NOTHING) { int level = 0; - if(get_config_int(lookup_config(config_tree, "LogLevel"), &level)) { + if(get_config_int(lookup_config(&config_tree, "LogLevel"), &level)) { debug_level = level; } } @@ -576,7 +571,7 @@ int main2(int argc, char **argv) { /* Change process priority */ - if(get_config_string(lookup_config(config_tree, "ProcessPriority"), &priority)) { + if(get_config_string(lookup_config(&config_tree, "ProcessPriority"), &priority)) { if(!strcasecmp(priority, "Normal")) { if(setpriority(NORMAL_PRIORITY_CLASS) != 0) { logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "setpriority", strerror(errno)); diff --git a/src/uml_device.c b/src/uml_device.c index a8bedc09..970e462a 100644 --- a/src/uml_device.c +++ b/src/uml_device.c @@ -63,11 +63,11 @@ static bool setup_device(void) { } name; struct timeval tv; - if(!get_config_string(lookup_config(config_tree, "Device"), &device)) { + if(!get_config_string(lookup_config(&config_tree, "Device"), &device)) { xasprintf(&device, RUNSTATEDIR "/%s.umlsocket", identname); } - get_config_string(lookup_config(config_tree, "Interface"), &iface); + get_config_string(lookup_config(&config_tree, "Interface"), &iface); if((write_fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) { logger(DEBUG_ALWAYS, LOG_ERR, "Could not open write %s: %s", device_info, strerror(errno)); diff --git a/src/upnp.c b/src/upnp.c index 553630e8..93c207fa 100644 --- a/src/upnp.c +++ b/src/upnp.c @@ -177,8 +177,8 @@ void upnp_init(bool tcp, bool udp) { upnp_tcp = tcp; upnp_udp = udp; - get_config_int(lookup_config(config_tree, "UPnPDiscoverWait"), &upnp_discover_wait); - get_config_int(lookup_config(config_tree, "UPnPRefreshPeriod"), &upnp_refresh_period); + get_config_int(lookup_config(&config_tree, "UPnPDiscoverWait"), &upnp_discover_wait); + get_config_int(lookup_config(&config_tree, "UPnPRefreshPeriod"), &upnp_refresh_period); #ifdef HAVE_MINGW HANDLE handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)upnp_thread, NULL, 0, NULL); diff --git a/src/vde_device.c b/src/vde_device.c index f30c3e32..40173213 100644 --- a/src/vde_device.c +++ b/src/vde_device.c @@ -35,15 +35,15 @@ static char *group = NULL; static const char *device_info = "VDE socket"; static bool setup_device(void) { - if(!get_config_string(lookup_config(config_tree, "Device"), &device)) { + if(!get_config_string(lookup_config(&config_tree, "Device"), &device)) { xasprintf(&device, RUNSTATEDIR "/vde.ctl"); } - get_config_string(lookup_config(config_tree, "Interface"), &iface); + get_config_string(lookup_config(&config_tree, "Interface"), &iface); - get_config_int(lookup_config(config_tree, "VDEPort"), &port); + get_config_int(lookup_config(&config_tree, "VDEPort"), &port); - get_config_string(lookup_config(config_tree, "VDEGroup"), &group); + get_config_string(lookup_config(&config_tree, "VDEGroup"), &group); struct vde_open_args args = { .port = port,