]> git.meshlink.io Git - meshlink/blobdiff - src/net_setup.c
Move proxy variables to mesh.
[meshlink] / src / net_setup.c
index 2cc7ddf55a46c5555cd5a93ba0ecf54e1e214705..c48c0d5b66f458ec9e764a6ce917f28cadd359b1 100644 (file)
@@ -26,6 +26,7 @@
 #include "ecdsa.h"
 #include "graph.h"
 #include "logger.h"
+#include "meshlink_internal.h"
 #include "net.h"
 #include "netutl.h"
 #include "protocol.h"
 #include "utils.h"
 #include "xalloc.h"
 
-char *myport;
-
-char *proxyhost;
-char *proxyport;
-char *proxyuser;
-char *proxypass;
-proxytype_t proxytype;
-int autoconnect;
-bool disablebuggypeers;
+int autoconnect = 3;
 
 bool node_read_ecdsa_public_key(node_t *n) {
        if(ecdsa_active(n->ecdsa))
@@ -93,7 +86,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);
 
@@ -102,13 +95,13 @@ static bool read_ecdsa_private_key(void) {
                return false;
        }
 
-       myself->connection->ecdsa = ecdsa_read_pem_private_key(fp);
+       mesh->self->connection->ecdsa = ecdsa_read_pem_private_key(fp);
        fclose(fp);
 
-       if(!myself->connection->ecdsa)
+       if(!mesh->self->connection->ecdsa)
                logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file failed: %s", strerror(errno));
 
-       return myself->connection->ecdsa;
+       return mesh->self->connection->ecdsa;
 }
 
 static bool read_invitation_key(void) {
@@ -120,7 +113,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");
 
@@ -152,7 +145,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));
@@ -180,13 +173,13 @@ void load_all_nodes(void) {
 char *get_name(void) {
        char *name = NULL;
 
-       get_config_string(lookup_config(config_tree, "Name"), &name);
+       get_config_string(lookup_config(mesh->config, "Name"), &name);
 
        if(!name)
                return NULL;
 
        if(!check_id(name)) {
-               logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for myself!");
+               logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for mesh->self!");
                free(name);
                return NULL;
        }
@@ -199,7 +192,7 @@ bool setup_myself_reloadable(void) {
        keylifetime = 3600; // TODO: check if this can be removed as well
        maxtimeout = 900;
        autoconnect = 3;
-       myself->options |= OPTION_PMTU_DISCOVERY;
+       mesh->self->options |= OPTION_PMTU_DISCOVERY;
 
        read_invitation_key();
 
@@ -210,7 +203,7 @@ bool setup_myself_reloadable(void) {
   Add listening sockets.
 */
 static bool add_listen_address(char *address, bool bindto) {
-       char *port = myport;
+       char *port = mesh->myport;
 
        if(address) {
                char *space = strchr(address, ' ');
@@ -286,7 +279,7 @@ static bool add_listen_address(char *address, bool bindto) {
 }
 
 /*
-  Configure node_t myself and set up the local sockets (listen only)
+  Configure node_t mesh->self and set up the local sockets (listen only)
 */
 bool setup_myself(void) {
        char *name, *hostname, *cipher, *digest, *type;
@@ -298,36 +291,36 @@ bool setup_myself(void) {
                return false;
        }
 
-       myself = new_node();
-       myself->connection = new_connection();
-       myself->name = name;
-       myself->connection->name = xstrdup(name);
-       read_host_config(config_tree, name);
+       mesh->self = new_node();
+       mesh->self->connection = new_connection();
+       mesh->self->name = name;
+       mesh->self->connection->name = xstrdup(name);
+       read_host_config(mesh->config, name);
 
-       if(!get_config_string(lookup_config(config_tree, "Port"), &myport))
-               myport = xstrdup("655");
+       if(!get_config_string(lookup_config(mesh->config, "Port"), &mesh->myport))
+               mesh->myport = xstrdup("655");
        else
                port_specified = true;
 
-       myself->connection->options = 0;
-       myself->connection->protocol_major = PROT_MAJOR;
-       myself->connection->protocol_minor = PROT_MINOR;
+       mesh->self->connection->options = 0;
+       mesh->self->connection->protocol_major = PROT_MAJOR;
+       mesh->self->connection->protocol_minor = PROT_MINOR;
 
-       myself->options |= PROT_MINOR << 24;
+       mesh->self->options |= PROT_MINOR << 24;
 
        if(!read_ecdsa_private_key())
                return false;
 
-       /* Ensure myport is numeric */
+       /* Ensure mesh->myport is numeric */
 
-       if(!atoi(myport)) {
-               struct addrinfo *ai = str2addrinfo("localhost", myport, SOCK_DGRAM);
+       if(!atoi(mesh->myport)) {
+               struct addrinfo *ai = str2addrinfo("localhost", mesh->myport, SOCK_DGRAM);
                sockaddr_t sa;
                if(!ai || !ai->ai_addr)
                        return false;
-               free(myport);
+               free(mesh->myport);
                memcpy(&sa, ai->ai_addr, ai->ai_addrlen);
-               sockaddr2str(&sa, NULL, &myport);
+               sockaddr2str(&sa, NULL, &mesh->myport);
        }
 
        /* Check some options */
@@ -341,16 +334,16 @@ bool setup_myself(void) {
        /* Compression */
 
        // TODO: drop compression in the packet layer?
-       myself->incompression = 0;
-       myself->connection->outcompression = 0;
+       mesh->self->incompression = 0;
+       mesh->self->connection->outcompression = 0;
 
        /* Done */
 
-       myself->nexthop = myself;
-       myself->via = myself;
-       myself->status.reachable = true;
-       myself->last_state_change = now.tv_sec;
-       node_add(myself);
+       mesh->self->nexthop = mesh->self;
+       mesh->self->via = mesh->self;
+       mesh->self->status.reachable = true;
+       mesh->self->last_state_change = now.tv_sec;
+       node_add(mesh->self);
 
        graph();
 
@@ -372,15 +365,15 @@ bool setup_myself(void) {
 
        // TODO: require Port to be set? Or use "0" and use getsockname()?
 
-       if(!myport)
-               myport = xstrdup("655");
+       if(!mesh->myport)
+               mesh->myport = xstrdup("655");
 
-       xasprintf(&myself->hostname, "MYSELF port %s", myport);
-       myself->connection->hostname = xstrdup(myself->hostname);
+       xasprintf(&mesh->self->hostname, "MYSELF port %s", mesh->myport);
+       mesh->self->connection->hostname = xstrdup(mesh->self->hostname);
 
        /* Done. */
 
-       last_config_check = now.tv_sec;
+       mesh->last_config_check = now.tv_sec;
 
        return true;
 }
@@ -408,19 +401,19 @@ bool setup_network(void) {
   close all open network connections
 */
 void close_network_connections(void) {
-       for(list_node_t *node = connection_list->head, *next; node; node = next) {
+       for(list_node_t *node = mesh->connections->head, *next; node; node = next) {
                next = node->next;
                connection_t *c = node->data;
                c->outgoing = NULL;
                terminate_connection(c, false);
        }
 
-       if(outgoing_list)
-               list_delete_list(outgoing_list);
+       if(mesh->outgoings)
+               list_delete_list(mesh->outgoings);
 
-       if(myself && myself->connection) {
-               terminate_connection(myself->connection, false);
-               free_connection(myself->connection);
+       if(mesh->self && mesh->self->connection) {
+               terminate_connection(mesh->self->connection, false);
+               free_connection(mesh->self->connection);
        }
 
        for(int i = 0; i < listen_sockets; i++) {
@@ -435,7 +428,7 @@ void close_network_connections(void) {
        exit_nodes();
        exit_connections();
 
-       if(myport) free(myport);
+       if(mesh->myport) free(mesh->myport);
 
        return;
 }