]> git.meshlink.io Git - meshlink/commitdiff
Move several variables to mesh.
authorGuus Sliepen <guus@sliepen.org>
Mon, 21 Apr 2014 20:26:41 +0000 (22:26 +0200)
committerGuus Sliepen <guus@sliepen.org>
Mon, 21 Apr 2014 20:26:41 +0000 (22:26 +0200)
In particular: confbase, contradicting_add_edge, contradicting_del_edge,
sleeptime, last_config_check, pingtimer, periodictimer.

src/conf.c
src/meshlink_internal.h
src/net.c
src/net.h
src/net_setup.c
src/protocol_auth.c
src/protocol_edge.c

index 360053f94b0181fe94ed9f5131df16c0ca75b599..a1c0a3df600f80ecbaee2df8807b98396583495f 100644 (file)
@@ -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");
 
index 04db92fcb2ceaa5f47cacaa7252c5780e1f58ae2..c21608fe344f562dcb02e07153acded3eb805d25 100644 (file)
@@ -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.
index 7a176ef2262a370e53a06bf885f5784985f9ae66..c2cbadf005e29cf39423e7d84b46cce85f8dca07 100644 (file)
--- a/src/net.c
+++ b/src/net.c
 #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;
 }
index 706f56904f79cb46907662f4c918e3c60638aa8b..c87bbf3684bfd0ede2bff6fe36133e1deb2b8f4e 100644 (file)
--- 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__ */
index 4c19e268a524fe38508873b2d8bd11e042129b03..d966f1632b9a406fca8149ac7f48c06ae88cb414 100644 (file)
@@ -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;
 }
index 2d5990e32c32e053093e590e38d9e0dfe2ca1280..8b419bad8863c3e18bcf5a56afc2acb05a2a4f17 100644 (file)
@@ -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)) {
index ecad9c6e2c08a562faffff195cea1e559e37b8b6..641011cffd859f1391f57a1cf64456012542af66 100644 (file)
@@ -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;
        }