From f12d4a3e6dfc9b5eb9d48bc2aabc72f98f1b5a79 Mon Sep 17 00:00:00 2001 From: thorkill Date: Thu, 23 Jul 2015 18:33:57 +0200 Subject: [PATCH] Merged load_all_subnets and load_all_nodes to make autoconnect and strictsubnets work faster When AutoConnect is on tinc needs to know if nodes have Address to defined in thier hosts files. Currently tinc parsed node's host files if StrictSubnet was enabled. To reduce the parsing overhead I have merged load_all_subnets with load_all_nodes, such that load_all_subnets has been removed and load_all_nodes has if-statement extracting Subnet information from node's host file. --- src/net.c | 2 +- src/net.h | 1 - src/net_setup.c | 98 ++++++++++++++++--------------------------------- 3 files changed, 32 insertions(+), 69 deletions(-) diff --git a/src/net.c b/src/net.c index d1c3569a..fe89fb20 100644 --- a/src/net.c +++ b/src/net.c @@ -350,7 +350,7 @@ int reload_configuration(void) { if (subnet->owner) subnet->expires = 1; - load_all_subnets(); + load_all_nodes(); for splay_each(subnet_t, subnet, subnet_tree) { if (!subnet->owner) diff --git a/src/net.h b/src/net.h index c892d9e2..3f9bc40c 100644 --- a/src/net.h +++ b/src/net.h @@ -219,7 +219,6 @@ extern void update_edge_weight(void); extern void purge(void); extern void retry(void); extern int reload_configuration(void); -extern void load_all_subnets(void); extern void load_all_nodes(void); extern void try_tx(struct node_t *n, bool); diff --git a/src/net_setup.c b/src/net_setup.c index 63853de5..fe8cc675 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -347,66 +347,9 @@ void update_edge_weight(void) { } } - /* Read Subnets from all host config files */ -void load_all_subnets(void) { - DIR *dir; - struct dirent *ent; - char dname[PATH_MAX]; - - snprintf(dname, sizeof dname, "%s" SLASH "hosts", confbase); - dir = opendir(dname); - if(!dir) { - logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno)); - return; - } - - while((ent = readdir(dir))) { - if(!check_id(ent->d_name)) - continue; - - node_t *n = lookup_node(ent->d_name); - #ifdef _DIRENT_HAVE_D_TYPE - //if(ent->d_type != DT_REG) - // continue; - #endif - - 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); - - if(!n) { - n = new_node(); - n->name = xstrdup(ent->d_name); - node_add(n); - } - - for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) { - subnet_t *s, *s2; - - if(!get_config_subnet(cfg, &s)) - continue; - - if((s2 = lookup_subnet(n, s))) { - s2->expires = -1; - free(s); - } else { - subnet_add(n, s); - } - } - - if (lookup_config(config_tree, "Address")) - n->status.has_known_address = true; - - exit_configuration(&config_tree); - } - - closedir(dir); -} - void load_all_nodes(void) { DIR *dir; struct dirent *ent; @@ -424,18 +367,41 @@ void load_all_nodes(void) { continue; node_t *n = lookup_node(ent->d_name); - if(n) - continue; + if(!n) { + n = new_node(); + n->name = xstrdup(ent->d_name); + node_add(n); + } - n = new_node(); - n->name = xstrdup(ent->d_name); - node_add(n); + 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); + + if (strictsubnets) { + for(config_t *cfg = lookup_config(config_tree, "Subnet"); cfg; cfg = lookup_config_next(config_tree, cfg)) { + subnet_t *s, *s2; + + if(!get_config_subnet(cfg, &s)) + continue; + + if((s2 = lookup_subnet(n, s))) { + s2->expires = -1; + free(s); + } else { + subnet_add(n, s); + } + } + } + + if (lookup_config(config_tree, "Address")) + n->status.has_known_address = true; + + exit_configuration(&config_tree); } - closedir(dir); } - char *get_name(void) { char *name = NULL; char *returned_name; @@ -997,9 +963,7 @@ static bool setup_myself(void) { graph(); - if(strictsubnets) - load_all_subnets(); - else if(autoconnect) + if(autoconnect) load_all_nodes(); /* Open device */