]> git.meshlink.io Git - meshlink/blobdiff - src/net_setup.c
Remove global variables from the event loop code.
[meshlink] / src / net_setup.c
index 484346faaf8260c1407bfb86935986df07f473f5..896df4327269c73bd9696a891ecbca3c65d054c7 100644 (file)
 
 #include "system.h"
 
-#include "cipher.h"
 #include "conf.h"
 #include "connection.h"
-#include "digest.h"
 #include "ecdsa.h"
 #include "graph.h"
 #include "logger.h"
@@ -84,11 +82,10 @@ bool read_ecdsa_public_key(connection_t *c) {
 
 static bool read_ecdsa_private_key(void) {
        FILE *fp;
-       char *fname;
+       char filename[PATH_MAX];
 
-       xasprintf(&fname, "%s" SLASH "ecdsa_key.priv", mesh->confbase);
-       fp = fopen(fname, "r");
-       free(fname);
+       snprintf(filename,PATH_MAX, "%s" SLASH "ecdsa_key.priv", mesh->confbase);
+       fp = fopen(filename, "r");
 
        if(!fp) {
                logger(DEBUG_ALWAYS, LOG_ERR, "Error reading ECDSA private key file: %s", strerror(errno));
@@ -106,38 +103,36 @@ static bool read_ecdsa_private_key(void) {
 
 static bool read_invitation_key(void) {
        FILE *fp;
-       char *fname;
+       char filename[PATH_MAX];
 
-       if(invitation_key) {
-               ecdsa_free(invitation_key);
-               invitation_key = NULL;
+       if(mesh->invitation_key) {
+               ecdsa_free(mesh->invitation_key);
+               mesh->invitation_key = NULL;
        }
 
-       xasprintf(&fname, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", mesh->confbase);
+       snprintf(filename,PATH_MAX, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", mesh->confbase);
 
-       fp = fopen(fname, "r");
+       fp = fopen(filename, "r");
 
        if(fp) {
-               invitation_key = ecdsa_read_pem_private_key(fp);
+               mesh->invitation_key = ecdsa_read_pem_private_key(fp);
                fclose(fp);
-               if(!invitation_key)
-                       logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno));
+               if(!mesh->invitation_key)
+                       logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", filename, strerror(errno));
        }
 
-       free(fname);
-       return invitation_key;
+       return mesh->invitation_key;
 }
 
 void load_all_nodes(void) {
        DIR *dir;
        struct dirent *ent;
-       char *dname;
+       char dname[PATH_MAX];
 
-       xasprintf(&dname, "%s" SLASH "hosts", mesh->confbase);
+       snprintf(dname,PATH_MAX, "%s" SLASH "hosts", mesh->confbase);
        dir = opendir(dname);
        if(!dir) {
                logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", dname, strerror(errno));
-               free(dname);
                return;
        }
 
@@ -176,9 +171,9 @@ char *get_name(void) {
 }
 
 bool setup_myself_reloadable(void) {
-       localdiscovery = true;
+       mesh->localdiscovery = true;
        keylifetime = 3600; // TODO: check if this can be removed as well
-       maxtimeout = 900;
+       mesh->maxtimeout = 900;
        autoconnect = 3;
        mesh->self->options |= OPTION_PMTU_DISCOVERY;
 
@@ -222,8 +217,8 @@ static bool add_listen_address(char *address, bool bindto) {
                // Ignore duplicate addresses
                bool found = false;
 
-               for(int i = 0; i < listen_sockets; i++)
-                       if(!memcmp(&listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
+               for(int i = 0; i < mesh->listen_sockets; i++)
+                       if(!memcmp(&mesh->listen_socket[i].sa, aip->ai_addr, aip->ai_addrlen)) {
                                found = true;
                                break;
                        }
@@ -231,7 +226,7 @@ static bool add_listen_address(char *address, bool bindto) {
                if(found)
                        continue;
 
-               if(listen_sockets >= MAXSOCKETS) {
+               if(mesh->listen_sockets >= MAXSOCKETS) {
                        logger(DEBUG_ALWAYS, LOG_ERR, "Too many listening sockets");
                        return false;
                }
@@ -248,18 +243,18 @@ static bool add_listen_address(char *address, bool bindto) {
                        continue;
                }
 
-               io_add(&listen_socket[listen_sockets].tcp, handle_new_meta_connection, &listen_socket[listen_sockets], tcp_fd, IO_READ);
-               io_add(&listen_socket[listen_sockets].udp, handle_incoming_vpn_data, &listen_socket[listen_sockets], udp_fd, IO_READ);
+               io_add(&mesh->loop, &mesh->listen_socket[mesh->listen_sockets].tcp, handle_new_meta_connection, &mesh->listen_socket[mesh->listen_sockets], tcp_fd, IO_READ);
+               io_add(&mesh->loop, &mesh->listen_socket[mesh->listen_sockets].udp, handle_incoming_vpn_data, &mesh->listen_socket[mesh->listen_sockets], udp_fd, IO_READ);
 
-               if(debug_level >= DEBUG_CONNECTIONS) {
+               if(mesh->debug_level >= DEBUG_CONNECTIONS) {
                        char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr);
                        logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Listening on %s", hostname);
                        free(hostname);
                }
 
-               listen_socket[listen_sockets].bindto = bindto;
-               memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
-               listen_sockets++;
+               mesh->listen_socket[mesh->listen_sockets].bindto = bindto;
+               memcpy(&mesh->listen_socket[mesh->listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
+               mesh->listen_sockets++;
        }
 
        freeaddrinfo(ai);
@@ -337,13 +332,13 @@ bool setup_myself(void) {
 
        /* Open sockets */
 
-       listen_sockets = 0;
+       mesh->listen_sockets = 0;
        int cfgs = 0;
 
        if(!add_listen_address(address, NULL))
                return false;
 
-       if(!listen_sockets) {
+       if(!mesh->listen_sockets) {
                logger(DEBUG_ALWAYS, LOG_ERR, "Unable to create any listening socket!");
                return false;
        }
@@ -372,8 +367,8 @@ bool setup_network(void) {
        init_edges();
        init_requests();
 
-       pinginterval = 60;
-       pingtimeout = 5;
+       mesh->pinginterval = 60;
+       mesh->pingtimeout = 5;
        maxoutbufsize = 10 * MTU;
 
        if(!setup_myself())
@@ -401,11 +396,11 @@ void close_network_connections(void) {
                free_connection(mesh->self->connection);
        }
 
-       for(int i = 0; i < listen_sockets; i++) {
-               io_del(&listen_socket[i].tcp);
-               io_del(&listen_socket[i].udp);
-               close(listen_socket[i].tcp.fd);
-               close(listen_socket[i].udp.fd);
+       for(int i = 0; i < mesh->listen_sockets; i++) {
+               io_del(&mesh->loop, &mesh->listen_socket[i].tcp);
+               io_del(&mesh->loop, &mesh->listen_socket[i].udp);
+               close(mesh->listen_socket[i].tcp.fd);
+               close(mesh->listen_socket[i].udp.fd);
        }
 
        exit_requests();