]> git.meshlink.io Git - meshlink/blobdiff - src/net_socket.c
Ensure the maxtimeout value is taken from the destination node's device class.
[meshlink] / src / net_socket.c
index eb9e3fc41f98fca0fe15bf8d0b998425e69553fd..a692313958850a0f1b87db3bb313930684cb59b9 100644 (file)
@@ -19,6 +19,7 @@
 
 #include "system.h"
 
+#include "adns.h"
 #include "conf.h"
 #include "connection.h"
 #include "list.h"
@@ -70,173 +71,13 @@ static void configure_tcp(connection_t *c) {
        int lowdelay = IPTOS_LOWDELAY;
        setsockopt(c->socket, IPPROTO_IP, IP_TOS, (void *)&lowdelay, sizeof(lowdelay));
 #endif
-}
-
-static bool bind_to_address(meshlink_handle_t *mesh, connection_t *c) {
-       int s = -1;
-
-       for(int i = 0; i < mesh->listen_sockets && mesh->listen_socket[i].bindto; i++) {
-               if(mesh->listen_socket[i].sa.sa.sa_family != c->address.sa.sa_family) {
-                       continue;
-               }
-
-               if(s >= 0) {
-                       return false;
-               }
-
-               s = i;
-       }
 
-       if(s < 0) {
-               return false;
-       }
-
-       sockaddr_t sa = mesh->listen_socket[s].sa;
-
-       if(sa.sa.sa_family == AF_INET) {
-               sa.in.sin_port = 0;
-       } else if(sa.sa.sa_family == AF_INET6) {
-               sa.in6.sin6_port = 0;
-       }
-
-       if(bind(c->socket, &sa.sa, SALEN(sa.sa))) {
-               logger(mesh, MESHLINK_WARNING, "Can't bind outgoing socket: %s", strerror(errno));
-               return false;
-       }
-
-       return true;
-}
-
-int setup_listen_socket(const sockaddr_t *sa) {
-       int nfd;
-       char *addrstr;
-       int option;
-
-       nfd = socket(sa->sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
-
-       if(nfd < 0) {
-               logger(NULL, MESHLINK_ERROR, "Creating metasocket failed: %s", sockstrerror(sockerrno));
-               return -1;
-       }
-
-#ifdef FD_CLOEXEC
-       fcntl(nfd, F_SETFD, FD_CLOEXEC);
-#endif
-
-       /* Optimize TCP settings */
-
-       option = 1;
-       setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof(option));
-
-#if defined(IPV6_V6ONLY)
-
-       if(sa->sa.sa_family == AF_INET6) {
-               setsockopt(nfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
-       }
-
-#else
-#warning IPV6_V6ONLY not defined
+#if defined(SO_NOSIGPIPE)
+       int nosigpipe = 1;
+       setsockopt(c->socket, SOL_SOCKET, SO_NOSIGPIPE, (void *)&nosigpipe, sizeof(nosigpipe));
 #endif
-
-       if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
-               closesocket(nfd);
-               addrstr = sockaddr2hostname(sa);
-               logger(NULL, MESHLINK_ERROR, "Can't bind to %s/tcp: %s", addrstr, sockstrerror(sockerrno));
-               free(addrstr);
-               return -1;
-       }
-
-       if(listen(nfd, 3)) {
-               closesocket(nfd);
-               logger(NULL, MESHLINK_ERROR, "System call `%s' failed: %s", "listen", sockstrerror(sockerrno));
-               return -1;
-       }
-
-       return nfd;
 }
 
-int setup_vpn_in_socket(meshlink_handle_t *mesh, const sockaddr_t *sa) {
-       int nfd;
-       char *addrstr;
-       int option;
-
-       nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP);
-
-       if(nfd < 0) {
-               logger(mesh, MESHLINK_ERROR, "Creating UDP socket failed: %s", sockstrerror(sockerrno));
-               return -1;
-       }
-
-#ifdef FD_CLOEXEC
-       fcntl(nfd, F_SETFD, FD_CLOEXEC);
-#endif
-
-#ifdef O_NONBLOCK
-       {
-               int flags = fcntl(nfd, F_GETFL);
-
-               if(fcntl(nfd, F_SETFL, flags | O_NONBLOCK) < 0) {
-                       closesocket(nfd);
-                       logger(mesh, MESHLINK_ERROR, "System call `%s' failed: %s", "fcntl",
-                              strerror(errno));
-                       return -1;
-               }
-       }
-#elif defined(WIN32)
-       {
-               unsigned long arg = 1;
-
-               if(ioctlsocket(nfd, FIONBIO, &arg) != 0) {
-                       closesocket(nfd);
-                       logger(mesh, MESHLINK_ERROR, "Call to `%s' failed: %s", "ioctlsocket", sockstrerror(sockerrno));
-                       return -1;
-               }
-       }
-#endif
-
-       option = 1;
-       setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof(option));
-       setsockopt(nfd, SOL_SOCKET, SO_BROADCAST, (void *)&option, sizeof(option));
-
-#if defined(IPV6_V6ONLY)
-
-       if(sa->sa.sa_family == AF_INET6) {
-               setsockopt(nfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option));
-       }
-
-#endif
-
-#if defined(IP_DONTFRAG) && !defined(IP_DONTFRAGMENT)
-#define IP_DONTFRAGMENT IP_DONTFRAG
-#endif
-
-#if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
-       option = IP_PMTUDISC_DO;
-       setsockopt(nfd, IPPROTO_IP, IP_MTU_DISCOVER, (void *)&option, sizeof(option));
-#elif defined(IP_DONTFRAGMENT)
-       option = 1;
-       setsockopt(nfd, IPPROTO_IP, IP_DONTFRAGMENT, (void *)&option, sizeof(option));
-#endif
-
-#if defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
-       option = IPV6_PMTUDISC_DO;
-       setsockopt(nfd, IPPROTO_IPV6, IPV6_MTU_DISCOVER, (void *)&option, sizeof(option));
-#elif defined(IPV6_DONTFRAG)
-       option = 1;
-       setsockopt(nfd, IPPROTO_IPV6, IPV6_DONTFRAG, (void *)&option, sizeof(option));
-#endif
-
-       if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
-               closesocket(nfd);
-               addrstr = sockaddr2hostname(sa);
-               logger(mesh, MESHLINK_ERROR, "Can't bind to %s/udp: %s", addrstr, sockstrerror(sockerrno));
-               free(addrstr);
-               return -1;
-       }
-
-       return nfd;
-} /* int setup_vpn_in_socket */
-
 static void retry_outgoing_handler(event_loop_t *loop, void *data) {
        assert(data);
 
@@ -252,11 +93,13 @@ void retry_outgoing(meshlink_handle_t *mesh, outgoing_t *outgoing) {
                outgoing->timeout += 5;
        }
 
-       if(outgoing->timeout > mesh->maxtimeout) {
-               outgoing->timeout = mesh->maxtimeout;
+       int maxtimeout = mesh->dev_class_traits[outgoing->node->devclass].maxtimeout;
+
+       if(outgoing->timeout > maxtimeout) {
+               outgoing->timeout = maxtimeout;
        }
 
-       timeout_add(&mesh->loop, &outgoing->ev, retry_outgoing_handler, outgoing, &(struct timeval) {
+       timeout_add(&mesh->loop, &outgoing->ev, retry_outgoing_handler, outgoing, &(struct timespec) {
                outgoing->timeout, prng(mesh, TIMER_FUDGE)
        });
 
@@ -405,25 +248,26 @@ static void free_known_addresses(struct addrinfo *ai) {
        }
 }
 
-static struct addrinfo *get_canonical_address(node_t *n) {
-       if(!n->canonical_address) {
-               return false;
-       }
-
-       char *address = xstrdup(n->canonical_address);
-       char *port = strchr(address, ' ');
+static void canonical_resolve_cb(meshlink_handle_t *mesh, char *host, char *serv, void *data, struct addrinfo *ai, int err) {
+       (void)serv;
+       (void)err;
+       node_t *n = data;
+
+       free(host);
+       free(serv);
+
+       for list_each(outgoing_t, outgoing, mesh->outgoings) {
+               if(outgoing->node == n) {
+                       if(outgoing->state == OUTGOING_CANONICAL_RESOLVE) {
+                               outgoing->ai = ai;
+                               outgoing->aip = NULL;
+                               outgoing->state = OUTGOING_CANONICAL;
+                               do_outgoing_connection(mesh, outgoing);
+                       }
 
-       if(!port) {
-               free(address);
-               return false;
+                       return;
+               }
        }
-
-       *port++ = 0;
-
-       struct addrinfo *ai = str2addrinfo(address, port, SOCK_STREAM);
-       free(address);
-
-       return ai;
 }
 
 static bool get_next_outgoing_address(meshlink_handle_t *mesh, outgoing_t *outgoing) {
@@ -433,12 +277,34 @@ static bool get_next_outgoing_address(meshlink_handle_t *mesh, outgoing_t *outgo
 
        if(outgoing->state == OUTGOING_START) {
                start = true;
-               outgoing->state = OUTGOING_CANONICAL;
+               outgoing->state = OUTGOING_CANONICAL_RESOLVE;
+       }
+
+       if(outgoing->state == OUTGOING_CANONICAL_RESOLVE) {
+               node_t *n = outgoing->node;
+
+               if(n->canonical_address) {
+                       char *address = xstrdup(n->canonical_address);
+                       char *port = strchr(address, ' ');
+
+                       if(port) {
+                               *port++ = 0;
+                               port = xstrdup(port);
+                               adns_queue(mesh, address, port, canonical_resolve_cb, outgoing->node, 2);
+                               return false;
+                       } else {
+                               logger(mesh, MESHLINK_ERROR, "Canonical address for %s is missing port number", n->name);
+                               free(address);
+                               outgoing->state = OUTGOING_RECENT;
+                       }
+
+               } else {
+                       outgoing->state = OUTGOING_RECENT;
+               }
        }
 
        if(outgoing->state == OUTGOING_CANONICAL) {
                if(!outgoing->aip) {
-                       outgoing->ai = get_canonical_address(outgoing->node);
                        outgoing->aip = outgoing->ai;
                } else {
                        outgoing->aip = outgoing->aip->ai_next;
@@ -448,7 +314,10 @@ static bool get_next_outgoing_address(meshlink_handle_t *mesh, outgoing_t *outgo
                        return true;
                }
 
-               freeaddrinfo(outgoing->ai);
+               if(outgoing->ai) {
+                       freeaddrinfo(outgoing->ai);
+               }
+
                outgoing->ai = NULL;
                outgoing->aip = NULL;
                outgoing->state = OUTGOING_RECENT;
@@ -498,13 +367,12 @@ static bool get_next_outgoing_address(meshlink_handle_t *mesh, outgoing_t *outgo
 }
 
 void do_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) {
-       struct addrinfo *proxyai = NULL;
-       int result;
-
 begin:
 
        if(!get_next_outgoing_address(mesh, outgoing)) {
-               if(outgoing->state == OUTGOING_NO_KNOWN_ADDRESSES) {
+               if(outgoing->state == OUTGOING_CANONICAL_RESOLVE) {
+                       /* We are waiting for a callback from the ADNS thread */
+               } else if(outgoing->state == OUTGOING_NO_KNOWN_ADDRESSES) {
                        logger(mesh, MESHLINK_ERROR, "No known addresses for %s", outgoing->node->name);
                } else {
                        logger(mesh, MESHLINK_ERROR, "Could not set up a meta connection to %s", outgoing->node->name);
@@ -519,35 +387,26 @@ begin:
 
        memcpy(&c->address, outgoing->aip->ai_addr, outgoing->aip->ai_addrlen);
 
-       char *hostname = sockaddr2hostname(&c->address);
-
-       logger(mesh, MESHLINK_INFO, "Trying to connect to %s at %s", outgoing->node->name, hostname);
+       if(mesh->log_level <= MESHLINK_INFO) {
+               char *hostname = sockaddr2hostname(&c->address);
+               logger(mesh, MESHLINK_INFO, "Trying to connect to %s at %s", outgoing->node->name, hostname);
+               free(hostname);
+       }
 
-       if(!mesh->proxytype) {
-               c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
-               configure_tcp(c);
-       } else {
-               proxyai = str2addrinfo(mesh->proxyhost, mesh->proxyport, SOCK_STREAM);
+       c->socket = socket(c->address.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
 
-               if(!proxyai) {
-                       free_connection(c);
+       if(c->socket == -1) {
+               if(mesh->log_level <= MESHLINK_ERROR) {
+                       char *hostname = sockaddr2hostname(&c->address);
+                       logger(mesh, MESHLINK_ERROR, "Creating socket for %s at %s failed: %s", c->name, hostname, sockstrerror(sockerrno));
                        free(hostname);
-                       goto begin;
                }
 
-               logger(mesh, MESHLINK_INFO, "Using proxy at %s port %s", mesh->proxyhost, mesh->proxyport);
-               c->socket = socket(proxyai->ai_family, SOCK_STREAM, IPPROTO_TCP);
-               configure_tcp(c);
-       }
-
-       if(c->socket == -1) {
-               logger(mesh, MESHLINK_ERROR, "Creating socket for %s at %s failed: %s", c->name, hostname, sockstrerror(sockerrno));
                free_connection(c);
-               free(hostname);
                goto begin;
        }
 
-       free(hostname);
+       configure_tcp(c);
 
 #ifdef FD_CLOEXEC
        fcntl(c->socket, F_SETFD, FD_CLOEXEC);
@@ -562,21 +421,18 @@ begin:
 
 #endif
 
-       bind_to_address(mesh, c);
-
        /* Connect */
 
-       if(!mesh->proxytype) {
-               result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
-       } else {
-               result = connect(c->socket, proxyai->ai_addr, proxyai->ai_addrlen);
-               freeaddrinfo(proxyai);
-       }
+       int result = connect(c->socket, &c->address.sa, SALEN(c->address.sa));
 
        if(result == -1 && !sockinprogress(sockerrno)) {
-               logger(mesh, MESHLINK_ERROR, "Could not connect to %s: %s", outgoing->node->name, sockstrerror(sockerrno));
-               free_connection(c);
+               if(mesh->log_level <= MESHLINK_ERROR) {
+                       char *hostname = sockaddr2hostname(&c->address);
+                       logger(mesh, MESHLINK_ERROR, "Could not connect to %s: %s", outgoing->node->name, sockstrerror(sockerrno));
+                       free(hostname);
+               }
 
+               free_connection(c);
                goto begin;
        }
 
@@ -592,6 +448,20 @@ begin:
        io_add(&mesh->loop, &c->io, handle_meta_io, c, c->socket, IO_READ | IO_WRITE);
 }
 
+void reset_outgoing(outgoing_t *outgoing) {
+       if(outgoing->ai) {
+               if(outgoing->state == OUTGOING_RECENT || outgoing->state == OUTGOING_KNOWN) {
+                       free_known_addresses(outgoing->ai);
+               } else {
+                       freeaddrinfo(outgoing->ai);
+               }
+       }
+
+       outgoing->ai = NULL;
+       outgoing->aip = NULL;
+       outgoing->state = OUTGOING_START;
+}
+
 void setup_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) {
        timeout_del(&mesh->loop, &outgoing->ev);
 
@@ -602,16 +472,7 @@ void setup_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) {
                return;
        }
 
-
-       if(outgoing->ai) {
-               if(outgoing->state == OUTGOING_RECENT || outgoing->state == OUTGOING_KNOWN) {
-                       free_known_addresses(outgoing->ai);
-               } else {
-                       freeaddrinfo(outgoing->ai);
-               }
-       }
-
-       outgoing->state = OUTGOING_START;
+       reset_outgoing(outgoing);
 
        if(outgoing->node->status.blacklisted) {
                return;