]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
Ensure consistent use of SPTPS session labels.
[meshlink] / src / meshlink.c
index 68286d29046aa9e6477e012f5075f0a643f9e2d3..0f1e2738762e6a93ed898d2951b363ac721029b3 100644 (file)
@@ -181,27 +181,24 @@ static void scan_for_hostname(const char *filename, char **hostname, char **port
 
        fclose(f);
 }
-static char *get_my_hostname(meshlink_handle_t* mesh) {
-       char *hostname = NULL;
-       char *port = NULL;
-       char *hostport = NULL;
-       char *name = mesh->self->name;
-       char filename[PATH_MAX] = "";
-       char line[4096];
-       FILE *f;
 
-       // Use first Address statement in own host config file
-       snprintf(filename, sizeof filename, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
-       scan_for_hostname(filename, &hostname, &port);
+static bool is_valid_hostname(const char *hostname) {
+       for(const char *p = hostname; *p; p++) {
+               if(!(isalnum(*p) || *p == '-' || *p == '.' || *p == ':'))
+                       return false;
+       }
 
-       if(hostname)
-               goto done;
+       return true;
+}
+
+char *meshlink_get_external_address(meshlink_handle_t *mesh) {
+       char *hostname = NULL;
 
-       // If that doesn't work, guess externally visible hostname
        logger(mesh, MESHLINK_DEBUG, "Trying to discover externally visible hostname...\n");
        struct addrinfo *ai = str2addrinfo("meshlink.io", "80", SOCK_STREAM);
        struct addrinfo *aip = ai;
        static const char request[] = "GET http://www.meshlink.io/host.cgi HTTP/1.0\r\n\r\n";
+       char line[256];
 
        while(aip) {
                int s = socket(aip->ai_family, aip->ai_socktype, aip->ai_protocol);
@@ -234,17 +231,33 @@ static char *get_my_hostname(meshlink_handle_t* mesh) {
                freeaddrinfo(ai);
 
        // Check that the hostname is reasonable
-       if(hostname) {
-               for(char *p = hostname; *p; p++) {
-                       if(isalnum(*p) || *p == '-' || *p == '.' || *p == ':')
-                               continue;
-                       // If not, forget it.
-                       free(hostname);
-                       hostname = NULL;
-                       break;
-               }
+       if(hostname && !is_valid_hostname(hostname)) {
+               free(hostname);
+               hostname = NULL;
        }
 
+       if(!hostname)
+               meshlink_errno = MESHLINK_ERESOLV;
+
+       return hostname;
+}
+
+static char *get_my_hostname(meshlink_handle_t* mesh) {
+       char *hostname = NULL;
+       char *port = NULL;
+       char *hostport = NULL;
+       char *name = mesh->self->name;
+       char filename[PATH_MAX] = "";
+       FILE *f;
+
+       // Use first Address statement in own host config file
+       snprintf(filename, sizeof filename, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
+       scan_for_hostname(filename, &hostname, &port);
+
+       if(hostname)
+               goto done;
+
+       hostname = meshlink_get_external_address(mesh);
        if(!hostname)
                return NULL;
 
@@ -1002,9 +1015,6 @@ void meshlink_stop(meshlink_handle_t *mesh) {
        // Stop discovery
        discovery_stop(mesh);
 
-       pthread_mutex_lock(&(mesh->mesh_mutex));
-       logger(mesh, MESHLINK_DEBUG, "meshlink_stop called\n");
-
        // Shut down the main thread
        event_loop_stop(&mesh->loop);
 
@@ -1463,22 +1473,99 @@ bool meshlink_add_address(meshlink_handle_t *mesh, const char *address) {
                meshlink_errno = MESHLINK_EINVAL;
                return false;
        }
-       
+
+       if(!is_valid_hostname(address)) {
+               logger(mesh, MESHLINK_DEBUG, "Invalid character in address: %s\n", address);
+               meshlink_errno = MESHLINK_EINVAL;
+               return false;
+       }
+
        bool rval = false;
 
        pthread_mutex_lock(&(mesh->mesh_mutex));
+       rval = append_config_file(mesh, mesh->self->name, "Address", address);
+       pthread_mutex_unlock(&(mesh->mesh_mutex));
 
-       for(const char *p = address; *p; p++) {
-               if(isalnum(*p) || *p == '-' || *p == '.' || *p == ':')
-                       continue;
-               logger(mesh, MESHLINK_DEBUG, "Invalid character in address: %s\n", address);
+       return rval;
+}
+
+bool meshlink_add_external_address(meshlink_handle_t *mesh) {
+       if(!mesh) {
                meshlink_errno = MESHLINK_EINVAL;
-               pthread_mutex_unlock(&(mesh->mesh_mutex));
                return false;
        }
 
+       char *address = meshlink_get_external_address(mesh);
+       if(!address)
+               return false;
+
+       bool rval = false;
+
+       pthread_mutex_lock(&(mesh->mesh_mutex));
        rval = append_config_file(mesh, mesh->self->name, "Address", address);
        pthread_mutex_unlock(&(mesh->mesh_mutex));
+
+       free(address);
+       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;
 }
 
@@ -1751,7 +1838,7 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
        }
 
        // Start an SPTPS session
-       if(!sptps_start(&mesh->sptps, mesh, true, false, key, hiskey, "meshlink invitation", 15, invitation_send, invitation_receive)) {
+       if(!sptps_start(&mesh->sptps, mesh, true, false, key, hiskey, meshlink_invitation_label, sizeof meshlink_invitation_label, invitation_send, invitation_receive)) {
                meshlink_errno = MESHLINK_EINTERNAL;
                pthread_mutex_unlock(&(mesh->mesh_mutex));
                return false;
@@ -2088,9 +2175,6 @@ static void channel_accept(struct utcp_connection *utcp_connection, uint16_t por
 static ssize_t channel_send(struct utcp *utcp, const void *data, size_t len) {
        node_t *n = utcp->priv;
        meshlink_handle_t *mesh = n->mesh;
-       char hex[len * 2 + 1];
-       bin2hex(data, hex, len);
-       logger(mesh, MESHLINK_WARNING, "channel_send(%p, %p, %zu): %s\n", utcp, data, len, hex);
        return meshlink_send(mesh, (meshlink_node_t *)n, data, len) ? len : -1;
 }
 
@@ -2107,9 +2191,6 @@ static void channel_receive(meshlink_handle_t *mesh, meshlink_node_t *source, co
        node_t *n = (node_t *)source;
        if(!n->utcp)
                abort();
-       char hex[len * 2 + 1];
-       bin2hex(data, hex, len);
-       logger(mesh, MESHLINK_WARNING, "channel_receive(%p, %p, %zu): %s\n", n->utcp, data, len, hex);
        utcp_recv(n->utcp, data, len);
 }
 
@@ -2139,7 +2220,6 @@ void meshlink_set_channel_accept_cb(meshlink_handle_t *mesh, meshlink_channel_ac
        mesh->receive_cb = channel_receive;
        for splay_each(node_t, n, mesh->nodes) {
                if(!n->utcp && n != mesh->self) {
-                       logger(mesh, MESHLINK_WARNING, "utcp_init on node %s", n->name);
                        n->utcp = utcp_init(channel_accept, channel_pre_accept, channel_send, n);
                }
        }
@@ -2152,7 +2232,6 @@ meshlink_channel_t *meshlink_channel_open(meshlink_handle_t *mesh, meshlink_node
                return NULL;
        }
 
-       logger(mesh, MESHLINK_WARNING, "meshlink_channel_open(%p, %s, %u, %p, %p, %zu)\n", mesh, node->name, port, cb, data, len);
        node_t *n = (node_t *)node;
        if(!n->utcp) {
                n->utcp = utcp_init(channel_accept, channel_pre_accept, channel_send, n);