From 15b02e0766ce4fdbf604489fdb1f55c9a0cce850 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Mon, 9 Sep 2019 21:35:14 +0200 Subject: [PATCH] Correctly update our own host config file after meshlink_set_port(). We wrote the host config file before we updated mesh->self, causing the old port number to remain in the host config file. This would cause subsequent calls to meshlink_invite() to have the wrong port number in the invitation. --- src/meshlink.c | 14 +++++++++++--- test/invite-join.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/meshlink.c b/src/meshlink.c index 164c9937..a9e1b551 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -2159,9 +2159,6 @@ bool meshlink_set_port(meshlink_handle_t *mesh, int port) { free(mesh->myport); xasprintf(&mesh->myport, "%d", port); - /* Write meshlink.conf with the updated port number */ - write_main_config_files(mesh); - /* Close down the network. This also deletes mesh->self. */ close_network_connections(mesh); @@ -2182,6 +2179,17 @@ bool meshlink_set_port(meshlink_handle_t *mesh, int port) { rval = true; } + /* Rebuild our own list of recent addresses */ + memset(mesh->self->recent, 0, sizeof(mesh->self->recent)); + add_local_addresses(mesh); + + /* Write meshlink.conf with the updated port number */ + write_main_config_files(mesh); + + if(!config_sync(mesh, "current")) { + return false; + } + done: pthread_mutex_unlock(&(mesh->mesh_mutex)); diff --git a/test/invite-join.c b/test/invite-join.c index 52a922a8..f76dc536 100644 --- a/test/invite-join.c +++ b/test/invite-join.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "meshlink.h" @@ -183,6 +184,42 @@ int main() { free(corge_url); + // Check that invitations work correctly after changing ports + + meshlink_set_invitation_timeout(mesh1, 86400); + meshlink_stop(mesh1); + meshlink_stop(mesh3); + + int oldport = meshlink_get_port(mesh1); + bool success; + + for(int i = 0; i < 100; i++) { + success = meshlink_set_port(mesh1, 0x9000 + rand() % 0x1000); + } + + assert(success); + int newport = meshlink_get_port(mesh1); + assert(oldport != newport); + + assert(meshlink_start(mesh1)); + quux_url = meshlink_invite(mesh1, NULL, "quux"); + fprintf(stderr, "Invitation URL for quux: %s\n", quux_url); + + // The old port should not be in the invitation URL + + char portstr[10]; + snprintf(portstr, sizeof(portstr), ":%d", oldport); + assert(!strstr(quux_url, portstr)); + + // The new port should be in the invitation URL + + snprintf(portstr, sizeof(portstr), ":%d", newport); + assert(strstr(quux_url, portstr)); + + // The invitation should work + + assert(meshlink_join(mesh3, quux_url)); + // Clean up. meshlink_close(mesh3); -- 2.39.2