]> 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 31bc608f1d43fd56f346d9d2736de43c3b79e951..7e05ab9362b4ecd9d6d81b268a25ff75d8f25f93 100644 (file)
@@ -246,14 +246,18 @@ static void retry_outgoing_handler(event_loop_t *loop, void *data) {
 }
 
 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);
@@ -588,6 +592,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);
 
@@ -598,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;