/*
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
{"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},
static void scan_for_hostname(const char *filename, char **hostname, char **port) {
char line[4096];
+ bool canonical = false;
if(!filename || (*hostname && *port)) {
return;
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);
}
}
- if(*hostname && *port) {
+ if(canonical && *hostname && *port) {
break;
}
}
}
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;
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;
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"));
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");
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;
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;
mesh->appname = xstrdup(appname);
mesh->devclass = devclass;
mesh->discovery = true;
+ mesh->invitation_timeout = 604800; // 1 week
if(usingname) {
mesh->name = xstrdup(name);
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;
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);
}
}
- // 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) {
if(mesh->outgoings) {
list_delete_list(mesh->outgoings);
+ mesh->outgoings = NULL;
}
- mesh->outgoings = NULL;
-
pthread_mutex_unlock(&(mesh->mesh_mutex));
}
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;
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;
}
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;
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;
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;
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;
}
}
void meshlink_enable_discovery(meshlink_handle_t *mesh, bool enable) {
+#if HAVE_CATTA
if(!mesh) {
meshlink_errno = MESHLINK_EINVAL;
return;
end:
pthread_mutex_unlock(&mesh->mesh_mutex);
+#else
+ (void)mesh;
+ (void)enable;
+ meshlink_errno = MESHLINK_ENOTSUP;
+#endif
}
static void __attribute__((constructor)) meshlink_init(void) {