X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fnet_setup.c;h=b6d88da3c3c42f9629959cf5cbcd77bede667b3f;hb=eb049936ffce049cf6b53de8ca0f39c98a360363;hp=d966f1632b9a406fca8149ac7f48c06ae88cb414;hpb=2a18ac20efd8ed3e1fd76f11f6f816e77c099e47;p=meshlink diff --git a/src/net_setup.c b/src/net_setup.c index d966f163..b6d88da3 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -34,15 +34,7 @@ #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)) @@ -116,9 +108,9 @@ static bool read_invitation_key(void) { FILE *fp; char *fname; - 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); @@ -126,26 +118,14 @@ static bool read_invitation_key(void) { fp = fopen(fname, "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) + if(!mesh->invitation_key) logger(DEBUG_ALWAYS, LOG_ERR, "Reading ECDSA private key file `%s' failed: %s", fname, strerror(errno)); } free(fname); - return invitation_key; -} - -static timeout_t keyexpire_timeout; - -static void keyexpire_handler(void *data) { - regenerate_key(); - timeout_set(data, &(struct timeval){keylifetime, rand() % 100000}); -} - -void regenerate_key(void) { - logger(DEBUG_STATUS, LOG_INFO, "Expiring symmetric keys"); - send_key_changed(); + return mesh->invitation_key; } void load_all_nodes(void) { @@ -196,7 +176,7 @@ 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; autoconnect = 3; @@ -211,7 +191,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, ' '); @@ -242,8 +222,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; } @@ -251,7 +231,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; } @@ -268,8 +248,8 @@ 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->listen_socket[mesh->listen_sockets].tcp, handle_new_meta_connection, &mesh->listen_socket[mesh->listen_sockets], tcp_fd, IO_READ); + io_add(&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) { char *hostname = sockaddr2hostname((sockaddr_t *) aip->ai_addr); @@ -277,9 +257,9 @@ static bool add_listen_address(char *address, bool bindto) { 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); @@ -305,8 +285,8 @@ bool setup_myself(void) { mesh->self->connection->name = xstrdup(name); read_host_config(mesh->config, name); - if(!get_config_string(lookup_config(mesh->config, "Port"), &myport)) - myport = xstrdup("655"); + if(!get_config_string(lookup_config(mesh->config, "Port"), &mesh->myport)) + mesh->myport = xstrdup("655"); else port_specified = true; @@ -319,16 +299,16 @@ bool setup_myself(void) { 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 */ @@ -336,9 +316,6 @@ bool setup_myself(void) { if(!setup_myself_reloadable()) return false; - // TODO: check whether this is used at all - timeout_add(&keyexpire_timeout, keyexpire_handler, &keyexpire_timeout, &(struct timeval){keylifetime, rand() % 100000}); - /* Compression */ // TODO: drop compression in the packet layer? @@ -360,23 +337,23 @@ 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; } // 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(&mesh->self->hostname, "MYSELF port %s", myport); + xasprintf(&mesh->self->hostname, "MYSELF port %s", mesh->myport); mesh->self->connection->hostname = xstrdup(mesh->self->hostname); /* Done. */ @@ -424,11 +401,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->listen_socket[i].tcp); + io_del(&mesh->listen_socket[i].udp); + close(mesh->listen_socket[i].tcp.fd); + close(mesh->listen_socket[i].udp.fd); } exit_requests(); @@ -436,7 +413,7 @@ void close_network_connections(void) { exit_nodes(); exit_connections(); - if(myport) free(myport); + if(mesh->myport) free(mesh->myport); return; }