From: Guus Sliepen Date: Fri, 11 Mar 2016 15:10:55 +0000 (+0100) Subject: Add the meshlink_get_port() and meshlink_set_port() functions. X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=0a02c50ca8c9d6e66629c0354d56517125411af7;p=meshlink Add the meshlink_get_port() and meshlink_set_port() functions. The latter can be used to change the port on which MeshLink listens. It may only be called when the mesh is not running. --- diff --git a/src/meshlink.c b/src/meshlink.c index 418be9e8..446e8d24 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -1512,6 +1512,66 @@ bool meshlink_add_external_address(meshlink_handle_t *mesh) { return rval; } +int meshlink_get_port(meshlink_handle_t *mesh) { + if(!mesh) { + meshlink_errno = MESHLINK_EINVAL; + return -1; + } + + if(!mesh->myport) { + meshlink_errno = MESHLINK_EINTERNAL; + return -1; + } + + return atoi(mesh->myport); +} + +bool meshlink_set_port(meshlink_handle_t *mesh, int port) { + if(!mesh || port < 0 || port >= 65536 || mesh->threadstarted) { + meshlink_errno = MESHLINK_EINVAL; + return false; + } + + if(mesh->myport && port == atoi(mesh->myport)) + return true; + + if(!try_bind(port)) { + meshlink_errno = MESHLINK_ENETWORK; + return false; + } + + bool rval = false; + + pthread_mutex_lock(&(mesh->mesh_mutex)); + if(mesh->threadstarted) { + meshlink_errno = MESHLINK_EINVAL; + goto done; + } + + close_network_connections(mesh); + exit_configuration(&mesh->config); + + char portstr[10]; + snprintf(portstr, sizeof portstr, "%d", port); + portstr[sizeof portstr - 1] = 0; + + modify_config_file(mesh, mesh->name, "Port", portstr, true); + + init_configuration(&mesh->config); + + if(!read_server_config(mesh)) + meshlink_errno = MESHLINK_ESTORAGE; + else if(!setup_network(mesh)) + meshlink_errno = MESHLINK_ENETWORK; + else + rval = true; + +done: + pthread_mutex_unlock(&(mesh->mesh_mutex)); + + return rval; +} + char *meshlink_invite(meshlink_handle_t *mesh, const char *name) { if(!mesh) { meshlink_errno = MESHLINK_EINVAL; diff --git a/src/meshlink.h b/src/meshlink.h index 1c1e7af4..26c7edb5 100644 --- a/src/meshlink.h +++ b/src/meshlink.h @@ -457,6 +457,33 @@ extern char *meshlink_get_external_address(meshlink_handle_t *mesh); */ extern bool meshlink_add_external_address(meshlink_handle_t *mesh); +/// Get the network port used by the local node. +/** This function returns the network port that the local node is listening on. + * + * @param mesh A handle which represents an instance of MeshLink. + * + * @return This function returns the port number, or -1 in case of an error. + */ +extern int meshlink_get_port(meshlink_handle_t *mesh); + +/// Set the network port used by the local node. +/** This function sets the network port that the local node is listening on. + * It may only be called when the mesh is not running. + * If unsure, call meshlink_stop() before calling this function. + * Also note that if your node is already part of a mesh with other nodes, + * that the other nodes may no longer be able to initiate connections to the local node, + * since they will try to connect to the previously configured port. + * + * @param mesh A handle which represents an instance of MeshLink. + * @param port The port number to listen on. This must be between 0 and 65535. + * If the port is set to 0, then MeshLink will listen on a port + * that is randomly assigned by the operating system every time meshlink_open() is called. + * + * @return This function returns true if the port was succesfully changed, false otherwise. + */ + +extern bool meshlink_set_port(meshlink_handle_t *mesh, int port); + /// Invite another node into the mesh. /** This function generates an invitation that can be used by another node to join the same mesh as the local node. * The generated invitation is a string containing a URL. diff --git a/src/utcp b/src/utcp index b570e123..a7ec88e6 160000 --- a/src/utcp +++ b/src/utcp @@ -1 +1 @@ -Subproject commit b570e123128f3fafa583bae186fd61429dcb49a7 +Subproject commit a7ec88e641e14ca115fece6d1ea0e6c43144853f