]> git.meshlink.io Git - meshlink/commitdiff
Remember the address used when connecting to an inviting node.
authorGuus Sliepen <guus@meshlink.io>
Fri, 6 Dec 2019 21:58:29 +0000 (22:58 +0100)
committerGuus Sliepen <guus@meshlink.io>
Fri, 6 Dec 2019 21:58:29 +0000 (22:58 +0100)
The inviter sends us its own host config file, which should be populated
with its known addresses. However, if a symbolic hostname is in the
invitation URL and it can resolve to multiple IP addresses, or if the IP
address associated with it is currently different from when the invitation
was generated, the address used to connect to the inviter might not be
present in its host config file. This could cause the invitation to succeed,
but then the nodes would fail to make a regular MeshLink connection.

src/meshlink.c

index fdc6fed874460271a5fe810722871238a8352cc3..c4613a7019e6182929804a30a7963dc0e9602557 100644 (file)
@@ -612,7 +612,7 @@ static bool finalize_join(meshlink_handle_t *mesh, const void *buf, uint16_t len
        }
 
        // Write host config files
-       while(count--) {
+       for(uint32_t i = 0; i < count; i++) {
                const void *data;
                uint32_t len = packmsg_get_bin_raw(&in, &data);
 
@@ -655,11 +655,24 @@ static bool finalize_join(meshlink_handle_t *mesh, const void *buf, uint16_t len
                        return false;
                }
 
-               node_add(mesh, n);
+               if(i == 0) {
+                       /* The first host config file is of the inviter itself;
+                        * remember the address we are currently using for the invitation connection.
+                        */
+                       sockaddr_t sa;
+                       socklen_t salen = sizeof(sa);
 
-               if(!config_write(mesh, "current", n->name, &config, mesh->config_key)) {
+                       if(getpeername(mesh->sock, &sa.sa, &salen) == 0) {
+                               node_add_recent_address(mesh, n, &sa);
+                       }
+               }
+
+               if(!node_write_config(mesh, n)) {
+                       free_node(n);
                        return false;
                }
+
+               node_add(mesh, n);
        }
 
        /* Ensure the configuration directory metadata is on disk */