]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
Lock meshlink.conf to ensure only one instance can run at a time.
[meshlink] / src / meshlink.c
index 4ff26ac16f8847fd3b3ad0522b0d274ef78ec4ce..6df7c419261fc30f175ef016d00538c295d77f29 100644 (file)
@@ -1,6 +1,6 @@
 /*
     meshlink.c -- Implementation of the MeshLink API.
-    Copyright (C) 2014, 2017 Guus Sliepen <guus@meshlink.io>
+    Copyright (C) 2014-2018 Guus Sliepen <guus@meshlink.io>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -59,6 +59,7 @@ const var_t variables[] = {
        {"ConnectTo", VAR_SERVER | VAR_MULTIPLE | VAR_SAFE},
        {"Name", VAR_SERVER},
        /* Host configuration */
+       {"CanonicalAddress", VAR_HOST},
        {"Address", VAR_HOST | VAR_MULTIPLE},
        {"ECDSAPublicKey", VAR_HOST},
        {"Port", VAR_HOST},
@@ -96,6 +97,7 @@ static int rstrip(char *value) {
 
 static void scan_for_hostname(const char *filename, char **hostname, char **port) {
        char line[4096];
+       bool canonical = false;
 
        if(!filename || (*hostname && *port)) {
                return;
@@ -135,25 +137,33 @@ static void scan_for_hostname(const char *filename, char **hostname, char **port
 
                p += strspn(p, "\t ");
                p[strcspn(p, "\t ")] = 0;
-               // p is now pointing to the port
-
-               // Check that the hostname is a symbolic name (it's not a numeric IPv4 or IPv6 address)
-               if(!q[strspn(q, "0123456789.")] || strchr(q, ':')) {
-                       continue;
-               }
+               // p is now pointing to the port, if present
 
                if(!*port && !strcasecmp(line, "Port")) {
                        *port = xstrdup(q);
-               } else if(!*hostname && !strcasecmp(line, "Address")) {
+               } else if(!canonical && !*hostname && !strcasecmp(line, "Address")) {
+                       // Check that the hostname is a symbolic name (it's not a numeric IPv4 or IPv6 address)
+                       if(!q[strspn(q, "0123456789.")] || strchr(q, ':')) {
+                               continue;
+                       }
+
                        *hostname = xstrdup(q);
 
+                       if(*p) {
+                               free(*port);
+                               *port = xstrdup(p);
+                       }
+               } else if(!strcasecmp(line, "CanonicalAddress")) {
+                       *hostname = xstrdup(q);
+                       canonical = true;
+
                        if(*p) {
                                free(*port);
                                *port = xstrdup(p);
                        }
                }
 
-               if(*hostname && *port) {
+               if(canonical && *hostname && *port) {
                        break;
                }
        }
@@ -162,6 +172,10 @@ static void scan_for_hostname(const char *filename, char **hostname, char **port
 }
 
 static bool is_valid_hostname(const char *hostname) {
+       if(!*hostname) {
+               return false;
+       }
+
        for(const char *p = hostname; *p; p++) {
                if(!(isalnum(*p) || *p == '-' || *p == '.' || *p == ':')) {
                        return false;
@@ -171,6 +185,26 @@ static bool is_valid_hostname(const char *hostname) {
        return true;
 }
 
+static bool is_valid_port(const char *port) {
+       if(!*port) {
+               return false;
+       }
+
+       if(isdigit(*port)) {
+               char *end;
+               unsigned long int result = strtoul(port, &end, 10);
+               return result && result < 65536 && !*end;
+       }
+
+       for(const char *p = port; *p; p++) {
+               if(!(isalnum(*p) || *p == '-')) {
+                       return false;
+               }
+       }
+
+       return true;
+}
+
 static void set_timeout(int sock, int timeout) {
 #ifdef _WIN32
        DWORD tv = timeout;
@@ -313,63 +347,62 @@ char *meshlink_get_external_address_for_family(meshlink_handle_t *mesh, int fami
        return hostname;
 }
 
+// String comparison which handles NULL arguments
+static bool safe_streq(const char *a, const char *b) {
+       if (!a || !b)
+               return a == b;
+       else
+               return !strcmp(a, b);
+}
+
 // This gets the hostname part for use in invitation URLs
 static char *get_my_hostname(meshlink_handle_t *mesh) {
-       char *hostname[2] = {NULL};
-       char *port = NULL;
+       char *hostname[3] = {NULL};
+       char *port[3] = {NULL};
        char *hostport = NULL;
-       char *name = mesh->self->name;
-       char filename[PATH_MAX] = "";
-
-       // 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[0], &port);
 
-       if(hostname[0]) {
-               goto done;
-       }
+       // Use the best Address statement in our own host config file
+       char filename[PATH_MAX] = "";
+       snprintf(filename, sizeof(filename), "%s" SLASH "hosts" SLASH "%s", mesh->confbase, mesh->self->name);
+       scan_for_hostname(filename, &hostname[0], &port[0]);
 
-       hostname[0] = meshlink_get_external_address_for_family(mesh, AF_INET);
-       hostname[1] = meshlink_get_external_address_for_family(mesh, AF_INET6);
+       hostname[1] = meshlink_get_external_address_for_family(mesh, AF_INET);
+       hostname[2] = meshlink_get_external_address_for_family(mesh, AF_INET6);
 
-       if(!hostname[0] && !hostname[1]) {
-               return NULL;
-       }
-
-       if(hostname[0] && hostname[1] && !strcmp(hostname[0], hostname[1])) {
-               free(hostname[1]);
-               hostname[1] = NULL;
-       }
+       // Concatenate all unique address to the hostport string
+       for (int i = 0; i < 3; i++) {
+               if (!hostname[i])
+                       continue;
 
-       port = xstrdup(mesh->myport);
+               // Ignore duplicate hostnames
+               bool found = false;
 
-       for(int i = 0; i < 2; i++) {
-               if(hostname[i]) {
-                       char *tmphostport;
-                       xasprintf(&tmphostport, "%s %s", hostname[i], port);
-                       append_config_file(mesh, mesh->self->name, "Address", tmphostport);
-                       free(tmphostport);
+               for (int j = 0; i < j; j++) {
+                       if (safe_streq(hostname[i], hostname[j]) && safe_streq(port[i], port[j])) {
+                               found = true;
+                               break;
+                       }
                }
-       }
-
-done:
 
-       for(int i = 0; i < 2; i++) {
-               if(!hostname[i]) {
+               if (found) {
+                       free(hostname[i]);
+                       free(port[i]);
+                       hostname[i] = NULL;
+                       port[i] = NULL;
                        continue;
                }
 
-               char *newhostport;
-               xasprintf(&newhostport, (strchr(hostname[i], ':') ? "%s%s[%s]" : "%s%s%s"), hostport ? hostport : "", hostport ? "," : "", hostname[i]);
-               free(hostname[i]);
-               free(hostport);
-               hostport = newhostport;
-       }
+               // Ensure we have the same addresses in our own host config file.
+               char *tmphostport;
+               xasprintf(&tmphostport, "%s %s", hostname[i], port[i] ? port[i] : mesh->myport);
+               append_config_file(mesh, mesh->self->name, "Address", tmphostport);
+               free(tmphostport);
 
-       if(port) {
+               // Append the address to the hostport string
                char *newhostport;
-               xasprintf(&newhostport, "%s:%s", hostport, port);
-               free(port);
+               xasprintf(&newhostport, (strchr(hostname[i], ':') ? "%s%s[%s]:%s" : "%s%s%s:%s"), hostport ? hostport : "", hostport ? "," : "", hostname[i], port[i] ? port[i] : mesh->myport);
+               free(hostname[i]);
+               free(port[i]);
                free(hostport);
                hostport = newhostport;
        }
@@ -500,6 +533,31 @@ int check_port(meshlink_handle_t *mesh) {
        return 0;
 }
 
+static void deltree(const char *dirname) {
+       DIR *d = opendir(dirname);
+
+       if(d) {
+               struct dirent *ent;
+
+               while((ent = readdir(d))) {
+                       if(ent->d_name[0] == '.') {
+                               continue;
+                       }
+
+                       char filename[PATH_MAX];
+                       snprintf(filename, sizeof(filename), "%s" SLASH "%s", dirname, ent->d_name);
+
+                       if(unlink(filename)) {
+                               deltree(filename);
+                       }
+               }
+
+               closedir(d);
+       }
+
+       rmdir(dirname);
+}
+
 static bool finalize_join(meshlink_handle_t *mesh) {
        char *name = xstrdup(get_value(mesh->data, "Name"));
 
@@ -525,6 +583,19 @@ static bool finalize_join(meshlink_handle_t *mesh) {
 
        fprintf(f, "Name = %s\n", name);
 
+       // Wipe all old host config files and invitations
+       snprintf(filename, sizeof(filename), "%s" SLASH "hosts", mesh->confbase);
+       deltree(filename);
+
+       if(mkdir(filename, 0777) && errno != EEXIST) {
+               logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", filename, strerror(errno));
+               return false;
+       }
+
+       snprintf(filename, sizeof(filename), "%s" SLASH "invitations", mesh->confbase);
+       deltree(filename);
+
+       // Create a new host config file for ourself
        snprintf(filename, sizeof(filename), "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
        FILE *fh = fopen(filename, "w");
 
@@ -656,8 +727,10 @@ static bool finalize_join(meshlink_handle_t *mesh) {
        sptps_send_record(&(mesh->sptps), 1, b64key, strlen(b64key));
        free(b64key);
 
+       free(mesh->name);
        free(mesh->self->name);
        free(mesh->self->connection->name);
+       mesh->name = xstrdup(name);
        mesh->self->name = xstrdup(name);
        mesh->self->connection->name = name;
 
@@ -796,6 +869,8 @@ static const char *errstr[] = {
        [MESHLINK_ESTORAGE] = "Storage error",
        [MESHLINK_ENETWORK] = "Network error",
        [MESHLINK_EPEER] = "Error communicating with peer",
+       [MESHLINK_ENOTSUP] = "Operation not supported",
+       [MESHLINK_EBUSY] = "MeshLink instance already in use",
 };
 
 const char *meshlink_strerror(meshlink_errno_t err) {
@@ -974,6 +1049,12 @@ meshlink_handle_t *meshlink_open(const char *confbase, const char *name, const c
                return NULL;
        }
 
+       if(strchr(appname, ' ')) {
+               logger(NULL, MESHLINK_ERROR, "Invalid appname given!\n");
+               meshlink_errno = MESHLINK_EINVAL;
+               return NULL;
+       }
+
        if(!name || !*name) {
                logger(NULL, MESHLINK_ERROR, "No name given!\n");
                //return NULL;
@@ -999,6 +1080,7 @@ meshlink_handle_t *meshlink_open(const char *confbase, const char *name, const c
        mesh->appname = xstrdup(appname);
        mesh->devclass = devclass;
        mesh->discovery = true;
+       mesh->invitation_timeout = 604800; // 1 week
 
        if(usingname) {
                mesh->name = xstrdup(name);
@@ -1036,6 +1118,32 @@ meshlink_handle_t *meshlink_open(const char *confbase, const char *name, const c
                }
        }
 
+       // Open the configuration file and lock it
+
+       mesh->conffile = fopen(filename, "r");
+
+       if(!mesh->conffile) {
+               logger(NULL, MESHLINK_ERROR, "Cannot not open %s: %s\n", filename, strerror(errno));
+               meshlink_close(mesh);
+               meshlink_errno = MESHLINK_ESTORAGE;
+               return NULL;
+       }
+
+#ifdef FD_CLOEXEC
+       fcntl(fileno(mesh->conffile), F_SETFD, FD_CLOEXEC);
+#endif
+
+#ifdef HAVE_MINGW
+       // TODO: use _locking()?
+#else
+       if(flock(fileno(mesh->conffile), LOCK_EX | LOCK_NB) != 0) {
+               logger(NULL, MESHLINK_ERROR, "Cannot lock %s: %s\n", filename, strerror(errno));
+               meshlink_close(mesh);
+               meshlink_errno = MESHLINK_EBUSY;
+               return NULL;
+       }
+#endif
+
        // Read the configuration
 
        init_configuration(&mesh->config);
@@ -1127,16 +1235,21 @@ bool meshlink_start(meshlink_handle_t *mesh) {
                logger(mesh, MESHLINK_DEBUG, "Could not start thread: %s\n", strerror(errno));
                memset(&mesh->thread, 0, sizeof(mesh)->thread);
                meshlink_errno = MESHLINK_EINTERNAL;
+               event_loop_stop(&mesh->loop);
                pthread_mutex_unlock(&(mesh->mesh_mutex));
                return false;
        }
 
        mesh->threadstarted = true;
 
+#if HAVE_CATTA
+
        if(mesh->discovery) {
                discovery_start(mesh);
        }
 
+#endif
+
        pthread_mutex_unlock(&(mesh->mesh_mutex));
        return true;
 }
@@ -1150,11 +1263,15 @@ void meshlink_stop(meshlink_handle_t *mesh) {
        pthread_mutex_lock(&(mesh->mesh_mutex));
        logger(mesh, MESHLINK_DEBUG, "meshlink_stop called\n");
 
+#if HAVE_CATTA
+
        // Stop discovery
        if(mesh->discovery) {
                discovery_stop(mesh);
        }
 
+#endif
+
        // Shut down the main thread
        event_loop_stop(&mesh->loop);
 
@@ -1173,12 +1290,14 @@ void meshlink_stop(meshlink_handle_t *mesh) {
                }
        }
 
-       // Wait for the main thread to finish
-       pthread_mutex_unlock(&(mesh->mesh_mutex));
-       pthread_join(mesh->thread, NULL);
-       pthread_mutex_lock(&(mesh->mesh_mutex));
+       if(mesh->threadstarted) {
+               // Wait for the main thread to finish
+               pthread_mutex_unlock(&(mesh->mesh_mutex));
+               pthread_join(mesh->thread, NULL);
+               pthread_mutex_lock(&(mesh->mesh_mutex));
 
-       mesh->threadstarted = false;
+               mesh->threadstarted = false;
+       }
 
        // Close all metaconnections
        if(mesh->connections) {
@@ -1192,10 +1311,9 @@ void meshlink_stop(meshlink_handle_t *mesh) {
 
        if(mesh->outgoings) {
                list_delete_list(mesh->outgoings);
+               mesh->outgoings = NULL;
        }
 
-       mesh->outgoings = NULL;
-
        pthread_mutex_unlock(&(mesh->mesh_mutex));
 }
 
@@ -1235,37 +1353,15 @@ void meshlink_close(meshlink_handle_t *mesh) {
        free(mesh->confbase);
        pthread_mutex_destroy(&(mesh->mesh_mutex));
 
+       if(mesh->conffile) {
+               fclose(mesh->conffile);
+        }
+
        memset(mesh, 0, sizeof(*mesh));
 
        free(mesh);
 }
 
-static void deltree(const char *dirname) {
-       DIR *d = opendir(dirname);
-
-       if(d) {
-               struct dirent *ent;
-
-               while((ent = readdir(d))) {
-                       if(ent->d_name[0] == '.') {
-                               continue;
-                       }
-
-                       char filename[PATH_MAX];
-                       snprintf(filename, sizeof(filename), "%s" SLASH "%s", dirname, ent->d_name);
-
-                       if(unlink(filename)) {
-                               deltree(filename);
-                       }
-               }
-
-               closedir(d);
-       }
-
-       rmdir(dirname);
-       return;
-}
-
 bool meshlink_destroy(const char *confbase) {
        if(!confbase) {
                meshlink_errno = MESHLINK_EINVAL;
@@ -1313,6 +1409,17 @@ void meshlink_set_node_status_cb(meshlink_handle_t *mesh, meshlink_node_status_c
        pthread_mutex_unlock(&(mesh->mesh_mutex));
 }
 
+void meshlink_set_node_duplicate_cb(meshlink_handle_t *mesh, meshlink_node_duplicate_cb_t cb) {
+       if(!mesh) {
+               meshlink_errno = MESHLINK_EINVAL;
+               return;
+       }
+
+       pthread_mutex_lock(&(mesh->mesh_mutex));
+       mesh->node_duplicate_cb = cb;
+       pthread_mutex_unlock(&(mesh->mesh_mutex));
+}
+
 void meshlink_set_log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, meshlink_log_cb_t cb) {
        if(mesh) {
                pthread_mutex_lock(&(mesh->mesh_mutex));
@@ -1673,8 +1780,8 @@ static bool refresh_invitation_key(meshlink_handle_t *mesh) {
        return mesh->invitation_key;
 }
 
-bool meshlink_add_address(meshlink_handle_t *mesh, const char *address) {
-       if(!mesh || !address) {
+bool meshlink_set_canonical_address(meshlink_handle_t *mesh, meshlink_node_t *node, const char *address, const char *port) {
+       if(!mesh || !node || !address) {
                meshlink_errno = MESHLINK_EINVAL;
                return false;
        }
@@ -1685,15 +1792,32 @@ bool meshlink_add_address(meshlink_handle_t *mesh, const char *address) {
                return false;
        }
 
-       bool rval = false;
+       if(port && !is_valid_port(port)) {
+               logger(mesh, MESHLINK_DEBUG, "Invalid character in port: %s\n", address);
+               meshlink_errno = MESHLINK_EINVAL;
+               return false;
+       }
+
+       char *canonical_address;
+
+       if(port) {
+               xasprintf(&canonical_address, "%s %s", address, port);
+       } else {
+               canonical_address = xstrdup(address);
+       }
 
        pthread_mutex_lock(&(mesh->mesh_mutex));
-       rval = append_config_file(mesh, mesh->self->name, "Address", address);
+       bool rval = modify_config_file(mesh, node->name, "CanonicalAddress", canonical_address, 1);
        pthread_mutex_unlock(&(mesh->mesh_mutex));
 
+       free(canonical_address);
        return rval;
 }
 
+bool meshlink_add_address(meshlink_handle_t *mesh, const char *address) {
+       return meshlink_set_canonical_address(mesh, (meshlink_node_t *)mesh->self, address, NULL);
+}
+
 bool meshlink_add_external_address(meshlink_handle_t *mesh) {
        if(!mesh) {
                meshlink_errno = MESHLINK_EINVAL;
@@ -1779,6 +1903,10 @@ done:
        return rval;
 }
 
+void meshlink_set_invitation_timeout(meshlink_handle_t *mesh, int timeout) {
+       mesh->invitation_timeout = timeout;
+}
+
 char *meshlink_invite(meshlink_handle_t *mesh, const char *name) {
        if(!mesh) {
                meshlink_errno = MESHLINK_EINVAL;
@@ -1937,7 +2065,7 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
        char copy[strlen(invitation) + 1];
        strcpy(copy, invitation);
 
-       // Split the invitation URL into hostname, port, key hash and cookie.
+       // Split the invitation URL into a list of hostname/port tuples, a key hash and a cookie.
 
        char *slash = strchr(copy, '/');
 
@@ -1952,13 +2080,7 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
        }
 
        char *address = copy;
-       char *port = strrchr(address, ':');
-
-       if(!port) {
-               goto invalid;
-       }
-
-       *port++ = 0;
+       char *port = NULL;
 
        if(!b64decode(slash, mesh->hash, 18) || !b64decode(slash + 24, mesh->cookie, 18)) {
                goto invalid;
@@ -1985,6 +2107,15 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
                        *comma++ = 0;
                }
 
+               // Split of the port
+               port = strrchr(address, ':');
+
+               if(!port) {
+                       goto invalid;
+               }
+
+               *port++ = 0;
+
                // IPv6 address are enclosed in brackets, per RFC 3986
                if(*address == '[') {
                        address++;
@@ -1996,7 +2127,7 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
 
                        *bracket++ = 0;
 
-                       if(comma && bracket != comma) {
+                       if(*bracket) {
                                goto invalid;
                        }
                }
@@ -2048,7 +2179,7 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
 
        mesh->blen = 0;
 
-       if(!sendline(mesh->sock, "0 ?%s %d.%d", b64key, PROT_MAJOR, 1)) {
+       if(!sendline(mesh->sock, "0 ?%s %d.%d %s", b64key, PROT_MAJOR, 1, mesh->appname)) {
                logger(mesh, MESHLINK_DEBUG, "Error sending request to %s port %s: %s\n", address, port, strerror(errno));
                closesocket(mesh->sock);
                meshlink_errno = MESHLINK_ENETWORK;
@@ -2283,8 +2414,14 @@ void meshlink_blacklist(meshlink_handle_t *mesh, meshlink_node_t *node) {
        //Make blacklisting persistent in the config file
        append_config_file(mesh, n->name, "blacklisted", "yes");
 
+       //Immediately terminate any connections we have with the blacklisted node
+       for list_each(connection_t, c, mesh->connections) {
+               if(c->node == n) {
+                       terminate_connection(mesh, c, c->status.active);
+               }
+       }
+
        pthread_mutex_unlock(&(mesh->mesh_mutex));
-       return;
 }
 
 void meshlink_whitelist(meshlink_handle_t *mesh, meshlink_node_t *node) {
@@ -2396,6 +2533,11 @@ 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;
+
+       if(n->status.destroyed) {
+               return -1;
+       }
+
        meshlink_handle_t *mesh = n->mesh;
        return meshlink_send(mesh, (meshlink_node_t *)n, data, len) ? (ssize_t)len : -1;
 }
@@ -2569,7 +2711,18 @@ void update_node_status(meshlink_handle_t *mesh, node_t *n) {
        }
 }
 
+void handle_duplicate_node(meshlink_handle_t *mesh, node_t *n) {
+       if(!mesh->node_duplicate_cb || n->status.duplicate) {
+               return;
+       }
+
+       n->status.duplicate = true;
+       mesh->node_duplicate_cb(mesh, (meshlink_node_t *)n);
+}
+
 void meshlink_enable_discovery(meshlink_handle_t *mesh, bool enable) {
+#if HAVE_CATTA
+
        if(!mesh) {
                meshlink_errno = MESHLINK_EINVAL;
                return;
@@ -2593,6 +2746,11 @@ void meshlink_enable_discovery(meshlink_handle_t *mesh, bool enable) {
 
 end:
        pthread_mutex_unlock(&mesh->mesh_mutex);
+#else
+       (void)mesh;
+       (void)enable;
+       meshlink_errno = MESHLINK_ENOTSUP;
+#endif
 }
 
 static void __attribute__((constructor)) meshlink_init(void) {