]> git.meshlink.io Git - meshlink/blobdiff - src/net_socket.c
Move assert()s that dereference a pointer to after the pointer NULL check.
[meshlink] / src / net_socket.c
index 36fa57044d0fe0fe167be36e9bd5e0207addc509..7e05ab9362b4ecd9d6d81b268a25ff75d8f25f93 100644 (file)
@@ -40,9 +40,7 @@
 #define MSG_NOSIGNAL 0
 #endif
 
-int addressfamily = AF_UNSPEC;
-int seconds_till_retry = 5;
-int max_connection_burst = 100;
+static const int max_connection_burst = 100;
 
 /* Setup sockets */
 
@@ -213,39 +211,19 @@ int setup_vpn_in_socket(meshlink_handle_t *mesh, const sockaddr_t *sa) {
 #endif
 
 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
-
-       if(mesh->self->options & OPTION_PMTU_DISCOVERY) {
-               option = IP_PMTUDISC_DO;
-               setsockopt(nfd, IPPROTO_IP, IP_MTU_DISCOVER, (void *)&option, sizeof(option));
-       }
-
+       option = IP_PMTUDISC_DO;
+       setsockopt(nfd, IPPROTO_IP, IP_MTU_DISCOVER, (void *)&option, sizeof(option));
 #elif defined(IP_DONTFRAGMENT)
-
-       if(mesh->self->options & OPTION_PMTU_DISCOVERY) {
-               option = 1;
-               setsockopt(nfd, IPPROTO_IP, IP_DONTFRAGMENT, (void *)&option, sizeof(option));
-       }
-
-#else
-#warning No way to disable IPv4 fragmentation
+       option = 1;
+       setsockopt(nfd, IPPROTO_IP, IP_DONTFRAGMENT, (void *)&option, sizeof(option));
 #endif
 
 #if defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
-
-       if(mesh->self->options & OPTION_PMTU_DISCOVERY) {
-               option = IPV6_PMTUDISC_DO;
-               setsockopt(nfd, IPPROTO_IPV6, IPV6_MTU_DISCOVER, (void *)&option, sizeof(option));
-       }
-
+       option = IPV6_PMTUDISC_DO;
+       setsockopt(nfd, IPPROTO_IPV6, IPV6_MTU_DISCOVER, (void *)&option, sizeof(option));
 #elif defined(IPV6_DONTFRAG)
-
-       if(mesh->self->options & OPTION_PMTU_DISCOVERY) {
-               option = 1;
-               setsockopt(nfd, IPPROTO_IPV6, IPV6_DONTFRAG, (void *)&option, sizeof(option));
-       }
-
-#else
-#warning No way to disable IPv6 fragmentation
+       option = 1;
+       setsockopt(nfd, IPPROTO_IPV6, IPV6_DONTFRAG, (void *)&option, sizeof(option));
 #endif
 
        if(bind(nfd, &sa->sa, SALEN(sa->sa))) {
@@ -260,20 +238,26 @@ int setup_vpn_in_socket(meshlink_handle_t *mesh, const sockaddr_t *sa) {
 } /* int setup_vpn_in_socket */
 
 static void retry_outgoing_handler(event_loop_t *loop, void *data) {
+       assert(data);
+
        meshlink_handle_t *mesh = loop->data;
        outgoing_t *outgoing = data;
        setup_outgoing_connection(mesh, outgoing);
 }
 
 void retry_outgoing(meshlink_handle_t *mesh, outgoing_t *outgoing) {
-       outgoing->timeout += 5;
+       if(!mesh->reachable && mesh->loop.now.tv_sec < mesh->last_unreachable + mesh->dev_class_traits[mesh->devclass].fast_retry_period) {
+               outgoing->timeout = 1;
+       } else {
+               outgoing->timeout += 5;
+       }
 
        if(outgoing->timeout > mesh->maxtimeout) {
                outgoing->timeout = mesh->maxtimeout;
        }
 
        timeout_add(&mesh->loop, &outgoing->ev, retry_outgoing_handler, outgoing, &(struct timeval) {
-               outgoing->timeout, rand() % 100000
+               outgoing->timeout, prng(mesh, TIMER_FUDGE)
        });
 
        logger(mesh, MESHLINK_INFO, "Trying to re-establish outgoing connection in %d seconds", outgoing->timeout);
@@ -513,7 +497,7 @@ static bool get_next_outgoing_address(meshlink_handle_t *mesh, outgoing_t *outgo
        return false;
 }
 
-bool do_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) {
+void do_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) {
        struct addrinfo *proxyai = NULL;
        int result;
 
@@ -527,7 +511,7 @@ begin:
                        retry_outgoing(mesh, outgoing);
                }
 
-               return false;
+               return;
        }
 
        connection_t *c = new_connection();
@@ -599,14 +583,27 @@ begin:
        /* Now that there is a working socket, fill in the rest and register this connection. */
 
        c->status.connecting = true;
+       c->status.initiator = true;
        c->name = xstrdup(outgoing->node->name);
        c->last_ping_time = mesh->loop.now.tv_sec;
 
        connection_add(mesh, c);
 
        io_add(&mesh->loop, &c->io, handle_meta_io, c, c->socket, IO_READ | IO_WRITE);
+}
 
-       return true;
+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) {
@@ -619,16 +616,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;
@@ -642,18 +630,19 @@ void setup_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) {
 }
 
 /// Delayed close of a filedescriptor.
-static void tarpit(int fd) {
-       static int pits[10] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
-       static int next_pit = 0;
+static void tarpit(meshlink_handle_t *mesh, int fd) {
+       if(!fd) {
+               return;
+       }
 
-       if(pits[next_pit] != -1) {
-               closesocket(pits[next_pit]);
+       if(mesh->pits[mesh->next_pit]) {
+               closesocket(mesh->pits[mesh->next_pit]);
        }
 
-       pits[next_pit++] = fd;
+       mesh->pits[mesh->next_pit++] = fd;
 
-       if(next_pit >= (int)(sizeof pits / sizeof pits[0])) {
-               next_pit = 0;
+       if(mesh->next_pit >= (int)(sizeof mesh->pits / sizeof mesh->pits[0])) {
+               mesh->next_pit = 0;
        }
 }
 
@@ -688,20 +677,17 @@ void handle_new_meta_connection(event_loop_t *loop, void *data, int flags) {
 
        /* Rate limit incoming connections to max_connection_burst/second. */
 
-       static int connection_burst;
-       static int connection_burst_time;
-
-       if(mesh->loop.now.tv_sec != connection_burst_time) {
-               connection_burst_time = mesh->loop.now.tv_sec;
-               connection_burst = 0;
+       if(mesh->loop.now.tv_sec != mesh->connection_burst_time) {
+               mesh->connection_burst_time = mesh->loop.now.tv_sec;
+               mesh->connection_burst = 0;
        }
 
-       if(connection_burst >= max_connection_burst) {
-               tarpit(fd);
+       if(mesh->connection_burst >= max_connection_burst) {
+               tarpit(mesh, fd);
                return;
        }
 
-       connection_burst++;
+       mesh->connection_burst++;
 
        // Accept the new connection