From c4deb6c20dceb73c0ac6baa2eb901434584e6191 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Mon, 7 Sep 2020 21:12:28 +0200 Subject: [PATCH] Use the canonical address exclusively for making outgoing meta-connections. If we have a node's canonical address, we now always use that as a source for addresses for outgoing meta-connection attempts. This commit also adds the function meshlink_clear_canonical_address() to ensure the canonical address can be removed if it is no longer valid. --- src/meshlink++.h | 12 ++++++++++++ src/meshlink.c | 24 ++++++++++++++++++++++++ src/meshlink.h | 13 +++++++++++++ src/meshlink.sym | 1 + src/net_socket.c | 2 +- 5 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/meshlink++.h b/src/meshlink++.h index e0a51066..183544ac 100644 --- a/src/meshlink++.h +++ b/src/meshlink++.h @@ -515,6 +515,18 @@ public: return meshlink_set_canonical_address(handle, node, address, port); } + /// Clear the canonical Address for a node. + /** This function clears the canonical Address for a node. + * + * @param mesh A handle which represents an instance of MeshLink. + * @param node A pointer to a struct meshlink_node describing the node. + * + * @return This function returns true if the address was removed, false otherwise. + */ + bool clear_canonical_address(node *node) { + return meshlink_clear_canonical_address(handle, node); + } + /// Add an invitation address for the local node. /** This function adds an address for the local node, which will be used only for invitation URLs. * This address is not stored permanently. diff --git a/src/meshlink.c b/src/meshlink.c index 7492c46b..56da2a04 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -2603,6 +2603,30 @@ bool meshlink_set_canonical_address(meshlink_handle_t *mesh, meshlink_node_t *no return config_sync(mesh, "current"); } +bool meshlink_clear_canonical_address(meshlink_handle_t *mesh, meshlink_node_t *node) { + if(!mesh || !node) { + meshlink_errno = MESHLINK_EINVAL; + return false; + } + + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + + node_t *n = (node_t *)node; + free(n->canonical_address); + n->canonical_address = NULL; + + if(!node_write_config(mesh, n)) { + pthread_mutex_unlock(&mesh->mutex); + return false; + } + + pthread_mutex_unlock(&mesh->mutex); + + return config_sync(mesh, "current"); +} + bool meshlink_add_invitation_address(struct meshlink_handle *mesh, const char *address, const char *port) { if(!mesh || !address) { meshlink_errno = MESHLINK_EINVAL; diff --git a/src/meshlink.h b/src/meshlink.h index 3a23eb08..738f8647 100644 --- a/src/meshlink.h +++ b/src/meshlink.h @@ -801,6 +801,8 @@ bool meshlink_verify(struct meshlink_handle *mesh, struct meshlink_node *source, * * If a canonical Address is set for the local node, * it will be used for the hostname part of generated invitation URLs. + * If a canonical Address is set for a remote node, + * it is used exclusively for creating outgoing connections to that node. * * \memberof meshlink_node * @param mesh A handle which represents an instance of MeshLink. @@ -813,6 +815,17 @@ bool meshlink_verify(struct meshlink_handle *mesh, struct meshlink_node *source, */ bool meshlink_set_canonical_address(struct meshlink_handle *mesh, struct meshlink_node *node, const char *address, const char *port) __attribute__((__warn_unused_result__)); +/// Clear the canonical Address for a node. +/** This function clears the canonical Address for a node. + * + * \memberof meshlink_node + * @param mesh A handle which represents an instance of MeshLink. + * @param node A pointer to a struct meshlink_node describing the node. + * + * @return This function returns true if the address was removed, false otherwise. + */ +bool meshlink_clear_canonical_address(struct meshlink_handle *mesh, struct meshlink_node *node) __attribute__((__warn_unused_result__)); + /// Add an invitation address for the local node. /** This function adds an address for the local node, which will be used only for invitation URLs. * This address is not stored permanently. diff --git a/src/meshlink.sym b/src/meshlink.sym index ba79ce3f..08dff729 100644 --- a/src/meshlink.sym +++ b/src/meshlink.sym @@ -26,6 +26,7 @@ meshlink_channel_open meshlink_channel_open_ex meshlink_channel_send meshlink_channel_shutdown +meshlink_clear_canonical_address meshlink_clear_invitation_addresses meshlink_close meshlink_destroy diff --git a/src/net_socket.c b/src/net_socket.c index 7ac6dbbd..d8462372 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -320,7 +320,7 @@ static bool get_next_outgoing_address(meshlink_handle_t *mesh, outgoing_t *outgo outgoing->ai = NULL; outgoing->aip = NULL; - outgoing->state = OUTGOING_RECENT; + outgoing->state = OUTGOING_END; } if(outgoing->state == OUTGOING_RECENT) { -- 2.39.2