]> git.meshlink.io Git - meshlink/commitdiff
If binding to the configured port fails, ask the OS to provide a port.
authorGuus Sliepen <guus@meshlink.io>
Thu, 27 Jul 2017 08:50:23 +0000 (10:50 +0200)
committerGuus Sliepen <guus@meshlink.io>
Thu, 27 Jul 2017 08:50:23 +0000 (10:50 +0200)
This will still allow MeshLink to make outgoing connections to other
nodes.

src/net_setup.c

index 8cfacc3a89188c4660f24a7b458bf23e721ba465..fb0cf8d0acffb4789b598d1e510c223dc2ec6e55 100644 (file)
@@ -390,8 +390,19 @@ bool setup_myself(meshlink_handle_t *mesh) {
 
        mesh->listen_sockets = 0;
 
 
        mesh->listen_sockets = 0;
 
-       if(!add_listen_address(mesh, address, NULL))
-               return false;
+       if(!add_listen_address(mesh, address, NULL)) {
+               if(!strcmp(mesh->myport, "0")) {
+                       logger(mesh, MESHLINK_WARNING, "Could not bind to port %s, asking OS to choose one for us", mesh->myport);
+                       free(mesh->myport);
+                       mesh->myport = strdup("0");
+                       if(!mesh->myport)
+                               return false;
+                       if(!add_listen_address(mesh, address, NULL))
+                               return false;
+               } else {
+                       return false;
+               }
+       }
 
        if(!mesh->listen_sockets) {
                logger(mesh, MESHLINK_ERROR, "Unable to create any listening socket!");
 
        if(!mesh->listen_sockets) {
                logger(mesh, MESHLINK_ERROR, "Unable to create any listening socket!");