From 2a18ac20efd8ed3e1fd76f11f6f816e77c099e47 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Mon, 21 Apr 2014 22:26:41 +0200 Subject: [PATCH] Move several variables to mesh. In particular: confbase, contradicting_add_edge, contradicting_del_edge, sleeptime, last_config_check, pingtimer, periodictimer. --- src/conf.c | 6 ++--- src/meshlink_internal.h | 8 +++++++ src/net.c | 50 +++++++++++++++++------------------------ src/net.h | 9 -------- src/net_setup.c | 8 +++---- src/protocol_auth.c | 6 ++--- src/protocol_edge.c | 4 ++-- 7 files changed, 40 insertions(+), 51 deletions(-) diff --git a/src/conf.c b/src/conf.c index 360053f9..a1c0a3df 100644 --- a/src/conf.c +++ b/src/conf.c @@ -297,7 +297,7 @@ bool read_server_config(void) { char *fname; bool x; - xasprintf(&fname, "%s" SLASH "tinc.conf", confbase); + xasprintf(&fname, "%s" SLASH "tinc.conf", mesh->confbase); errno = 0; x = read_config_file(mesh->config, fname); @@ -313,7 +313,7 @@ bool read_host_config(splay_tree_t *config_tree, const char *name) { char *fname; bool x; - xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, name); + xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name); x = read_config_file(config_tree, fname); free(fname); @@ -322,7 +322,7 @@ bool read_host_config(splay_tree_t *config_tree, const char *name) { bool append_config_file(const char *name, const char *key, const char *value) { char *fname; - xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, name); + xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name); FILE *fp = fopen(fname, "a"); diff --git a/src/meshlink_internal.h b/src/meshlink_internal.h index 04db92fc..c21608fe 100644 --- a/src/meshlink_internal.h +++ b/src/meshlink_internal.h @@ -22,6 +22,7 @@ #include "system.h" +#include "event.h" #include "meshlink.h" /// A handle for an instance of MeshLink. @@ -45,6 +46,13 @@ struct meshlink_handle { struct list_t *connections; struct list_t *outgoings; + + int contradicting_add_edge; + int contradicting_del_edge; + int sleeptime; + time_t last_config_check; + timeout_t pingtimer; + timeout_t periodictimer; }; /// A handle for a MeshLink node. diff --git a/src/net.c b/src/net.c index 7a176ef2..c2cbadf0 100644 --- a/src/net.c +++ b/src/net.c @@ -31,16 +31,6 @@ #include "protocol.h" #include "xalloc.h" -int contradicting_add_edge = 0; -int contradicting_del_edge = 0; -static int sleeptime = 10; -time_t last_config_check = 0; -static timeout_t pingtimer; -static timeout_t periodictimer; - -//TODO: move this to a better place -char *confbase; - /* Purge edges of unreachable nodes. Use carefully. */ // TODO: remove @@ -163,20 +153,20 @@ static void periodic_handler(void *data) { If so, sleep for a short while to prevent a storm of contradicting messages. */ - if(contradicting_del_edge > 100 && contradicting_add_edge > 100) { - logger(DEBUG_ALWAYS, LOG_WARNING, "Possible node with same Name as us! Sleeping %d seconds.", sleeptime); - usleep(sleeptime * 1000000LL); - sleeptime *= 2; - if(sleeptime < 0) - sleeptime = 3600; + if(mesh->contradicting_del_edge > 100 && mesh->contradicting_add_edge > 100) { + logger(DEBUG_ALWAYS, LOG_WARNING, "Possible node with same Name as us! Sleeping %d seconds.", mesh->sleeptime); + usleep(mesh->sleeptime * 1000000LL); + mesh->sleeptime *= 2; + if(mesh->sleeptime < 0) + mesh->sleeptime = 3600; } else { - sleeptime /= 2; - if(sleeptime < 10) - sleeptime = 10; + mesh->sleeptime /= 2; + if(mesh->sleeptime < 10) + mesh->sleeptime = 10; } - contradicting_add_edge = 0; - contradicting_del_edge = 0; + mesh->contradicting_add_edge = 0; + mesh->contradicting_del_edge = 0; /* If AutoConnect is set, check if we need to make or break connections. */ @@ -291,7 +281,7 @@ int reload_configuration(void) { return EINVAL; } - xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, mesh->self->name); + xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, mesh->self->name); read_config_file(mesh->config, fname); free(fname); @@ -306,16 +296,16 @@ int reload_configuration(void) { /* Close connections to hosts that have a changed or deleted host config file */ for list_each(connection_t, c, mesh->connections) { - xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", confbase, c->name); + xasprintf(&fname, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, c->name); struct stat s; - if(stat(fname, &s) || s.st_mtime > last_config_check) { + if(stat(fname, &s) || s.st_mtime > mesh->last_config_check) { logger(DEBUG_CONNECTIONS, LOG_INFO, "Host config file of %s has been changed", c->name); terminate_connection(c, c->status.active); } free(fname); } - last_config_check = now.tv_sec; + mesh->last_config_check = now.tv_sec; return 0; } @@ -335,23 +325,23 @@ void retry(void) { } /* Kick the ping timeout handler */ - timeout_set(&pingtimer, &(struct timeval){0, 0}); + timeout_set(&mesh->pingtimer, &(struct timeval){0, 0}); } /* this is where it all happens... */ int main_loop(void) { - timeout_add(&pingtimer, timeout_handler, &pingtimer, &(struct timeval){pingtimeout, rand() % 100000}); - timeout_add(&periodictimer, periodic_handler, &periodictimer, &(struct timeval){pingtimeout, rand() % 100000}); + timeout_add(&mesh->pingtimer, timeout_handler, &mesh->pingtimer, &(struct timeval){pingtimeout, rand() % 100000}); + timeout_add(&mesh->periodictimer, periodic_handler, &mesh->periodictimer, &(struct timeval){pingtimeout, rand() % 100000}); if(!event_loop()) { logger(DEBUG_ALWAYS, LOG_ERR, "Error while waiting for input: %s", strerror(errno)); return 1; } - timeout_del(&periodictimer); - timeout_del(&pingtimer); + timeout_del(&mesh->periodictimer); + timeout_del(&mesh->pingtimer); return 0; } diff --git a/src/net.h b/src/net.h index 706f5690..c87bbf36 100644 --- a/src/net.h +++ b/src/net.h @@ -94,9 +94,6 @@ extern bool do_prune; extern char *myport; extern int autoconnect; extern bool disablebuggypeers; -extern int contradicting_add_edge; -extern int contradicting_del_edge; -extern time_t last_config_check; extern char *proxyhost; extern char *proxyport; @@ -112,9 +109,6 @@ typedef enum proxytype_t { } proxytype_t; extern proxytype_t proxytype; -extern char *scriptinterpreter; -extern char *scriptextension; - /* Yes, very strange placement indeed, but otherwise the typedefs get all tangled up */ #include "connection.h" #include "node.h" @@ -157,7 +151,4 @@ extern void load_all_nodes(void); extern CRITICAL_SECTION mutex; #endif -//TODO: move this to a better place -extern char *confbase; - #endif /* __TINC_NET_H__ */ diff --git a/src/net_setup.c b/src/net_setup.c index 4c19e268..d966f163 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -94,7 +94,7 @@ static bool read_ecdsa_private_key(void) { FILE *fp; char *fname; - xasprintf(&fname, "%s" SLASH "ecdsa_key.priv", confbase); + xasprintf(&fname, "%s" SLASH "ecdsa_key.priv", mesh->confbase); fp = fopen(fname, "r"); free(fname); @@ -121,7 +121,7 @@ static bool read_invitation_key(void) { invitation_key = NULL; } - xasprintf(&fname, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", confbase); + xasprintf(&fname, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", mesh->confbase); fp = fopen(fname, "r"); @@ -153,7 +153,7 @@ void load_all_nodes(void) { struct dirent *ent; char *dname; - xasprintf(&dname, "%s" SLASH "hosts", confbase); + xasprintf(&dname, "%s" SLASH "hosts", mesh->confbase); dir = opendir(dname); if(!dir) { logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno)); @@ -381,7 +381,7 @@ bool setup_myself(void) { /* Done. */ - last_config_check = now.tv_sec; + mesh->last_config_check = now.tv_sec; return true; } diff --git a/src/protocol_auth.c b/src/protocol_auth.c index 2d5990e3..8b419bad 100644 --- a/src/protocol_auth.c +++ b/src/protocol_auth.c @@ -148,7 +148,7 @@ static bool finalize_invitation(connection_t *c, const char *data, uint16_t len) // Create a new host config file char filename[PATH_MAX]; - snprintf(filename, sizeof filename, "%s" SLASH "hosts" SLASH "%s", confbase, c->name); + snprintf(filename, sizeof filename, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, c->name); if(!access(filename, F_OK)) { logger(DEBUG_ALWAYS, LOG_ERR, "Host config file for %s (%s) already exists!\n", c->name, c->hostname); return false; @@ -195,8 +195,8 @@ static bool receive_invitation_sptps(void *handle, uint8_t type, const char *dat free(fingerprint); char filename[PATH_MAX], usedname[PATH_MAX]; - snprintf(filename, sizeof filename, "%s" SLASH "invitations" SLASH "%s", confbase, cookie); - snprintf(usedname, sizeof usedname, "%s" SLASH "invitations" SLASH "%s.used", confbase, cookie); + snprintf(filename, sizeof filename, "%s" SLASH "invitations" SLASH "%s", mesh->confbase, cookie); + snprintf(usedname, sizeof usedname, "%s" SLASH "invitations" SLASH "%s.used", mesh->confbase, cookie); // Atomically rename the invitation file if(rename(filename, usedname)) { diff --git a/src/protocol_edge.c b/src/protocol_edge.c index ecad9c6e..641011cf 100644 --- a/src/protocol_edge.c +++ b/src/protocol_edge.c @@ -121,7 +121,7 @@ bool add_edge_h(connection_t *c, const char *request) { } else if(from == mesh->self) { logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself which does not exist", "ADD_EDGE", c->name, c->hostname); - contradicting_add_edge++; + mesh->contradicting_add_edge++; e = new_edge(); e->from = from; e->to = to; @@ -207,7 +207,7 @@ bool del_edge_h(connection_t *c, const char *request) { if(e->from == mesh->self) { logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) for ourself", "DEL_EDGE", c->name, c->hostname); - contradicting_del_edge++; + mesh->contradicting_del_edge++; send_add_edge(c, e); /* Send back a correction */ return true; } -- 2.39.2