]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
Stop using the global variable mesh in most of the rest of the code.
[meshlink] / src / meshlink.c
index 4abacb1433afd18b35c2027d7aef64af1ad932a6..7552c9f0be6d1bf064a90512b87facb9f198d186 100644 (file)
@@ -28,8 +28,6 @@
 #include "route.h"
 #include "xalloc.h"
 
-meshlink_handle_t *mesh;
-
 static const char *errstr[] = {
        [MESHLINK_OK] = "No error",
        [MESHLINK_ENOMEM] = "Out of memory",
@@ -40,11 +38,6 @@ const char *meshlink_strerror(meshlink_errno_t errno) {
        return errstr[errno];
 }
 
-// TODO: hack, remove once all global variables are gone.
-static void set_mesh(meshlink_handle_t *localmesh) {
-       mesh = localmesh;
-}
-
 static bool ecdsa_keygen(meshlink_handle_t *mesh) {
        ecdsa_t *key;
        FILE *f;
@@ -212,7 +205,8 @@ meshlink_handle_t *meshlink_open(const char *confbase, const char *name) {
        meshlink_handle_t *mesh = xzalloc(sizeof *mesh);
        mesh->confbase = xstrdup(confbase);
        mesh->name = xstrdup(name);
-       set_mesh(mesh);
+       event_loop_init(&mesh->loop);
+       mesh->loop.data = mesh;
 
        // TODO: should be set by a function.
        mesh->debug_level = 5;
@@ -236,13 +230,13 @@ meshlink_handle_t *meshlink_open(const char *confbase, const char *name) {
 
        init_configuration(&mesh->config);
 
-       if(!read_server_config())
+       if(!read_server_config(mesh))
                return meshlink_close(mesh), NULL;
 
        // Setup up everything
        // TODO: we should not open listening sockets yet
 
-       if(!setup_network())
+       if(!setup_network(mesh))
                return meshlink_close(mesh), NULL;
 
        return mesh;
@@ -251,9 +245,9 @@ meshlink_handle_t *meshlink_open(const char *confbase, const char *name) {
 void *meshlink_main_loop(void *arg) {
        meshlink_handle_t *mesh = arg;
 
-       try_outgoing_connections();
+       try_outgoing_connections(mesh);
 
-       main_loop();
+       main_loop(mesh);
 
        return NULL;
 }
@@ -283,12 +277,12 @@ void meshlink_stop(meshlink_handle_t *mesh) {
 void meshlink_close(meshlink_handle_t *mesh) {
        // Close and free all resources used.
 
-       close_network_connections();
+       close_network_connections(mesh);
 
        logger(DEBUG_ALWAYS, LOG_NOTICE, "Terminating");
 
        exit_configuration(&mesh->config);
-
+       event_loop_exit(&mesh->loop);
 }
 
 void meshlink_set_receive_cb(meshlink_handle_t *mesh, meshlink_receive_cb_t cb) {
@@ -322,12 +316,12 @@ bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const
 
         mesh->self->in_packets++;
         mesh->self->in_bytes += packet.len;
-        route(mesh->self, &packet);
+        route(mesh, mesh->self, &packet);
        return false;
 }
 
 meshlink_node_t *meshlink_get_node(meshlink_handle_t *mesh, const char *name) {
-       return (meshlink_node_t *)lookup_node(name);
+       return (meshlink_node_t *)lookup_node(mesh, name);
        return NULL;
 }
 
@@ -363,8 +357,6 @@ void meshlink_blacklist(meshlink_handle_t *mesh, meshlink_node_t *node) {
 }
 
 static void __attribute__((constructor)) meshlink_init(void) {
-       gettimeofday(&now, NULL);
-       srand(now.tv_sec + now.tv_usec);
        crypto_init();
 }