+const char *meshlink_strerror(meshlink_errno_t err) {
+ if(err < 0 || err >= sizeof(errstr) / sizeof(*errstr))
+ return "Invalid error code";
+ return errstr[err];
+}
+
+static bool ecdsa_keygen(meshlink_handle_t *mesh) {
+ ecdsa_t *key;
+ FILE *f;
+ char pubname[PATH_MAX], privname[PATH_MAX];
+
+ logger(mesh, MESHLINK_DEBUG, "Generating ECDSA keypair:\n");
+
+ if(!(key = ecdsa_generate())) {
+ logger(mesh, MESHLINK_DEBUG, "Error during key generation!\n");
+ meshlink_errno = MESHLINK_EINTERNAL;
+ return false;
+ } else
+ logger(mesh, MESHLINK_DEBUG, "Done.\n");
+
+ snprintf(privname, sizeof(privname), "%s" SLASH "ecdsa_key.priv", mesh->confbase);
+ f = fopen(privname, "wb");
+
+ if(!f) {
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+
+#ifdef HAVE_FCHMOD
+ fchmod(fileno(f), 0600);
+#endif
+
+ if(!ecdsa_write_pem_private_key(key, f)) {
+ logger(mesh, MESHLINK_DEBUG, "Error writing private key!\n");
+ ecdsa_free(key);
+ fclose(f);
+ meshlink_errno = MESHLINK_EINTERNAL;
+ return false;
+ }
+
+ fclose(f);
+
+ snprintf(pubname, sizeof(pubname), "%s" SLASH "hosts" SLASH "%s", mesh->confbase, mesh->name);
+ f = fopen(pubname, "a");
+
+ if(!f) {
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+
+ char *pubkey = ecdsa_get_base64_public_key(key);
+ fprintf(f, "ECDSAPublicKey = %s\n", pubkey);
+ free(pubkey);
+
+ fclose(f);
+ ecdsa_free(key);
+
+ return true;
+}
+
+static struct timeval idle(event_loop_t *loop, void *data) {
+ meshlink_handle_t *mesh = data;
+ struct timeval t, tmin = {3600, 0};
+ for splay_each(node_t, n, mesh->nodes) {
+ if(!n->utcp)
+ continue;
+ t = utcp_timeout(n->utcp);
+ if(timercmp(&t, &tmin, <))
+ tmin = t;
+ }
+ return tmin;
+}
+
+// Find out what local address a socket would use if we connect to the given address.
+// We do this using connect() on a UDP socket, so the kernel has to resolve the address
+// of both endpoints, but this will actually not send any UDP packet.
+static bool getlocaladdrname(char *destaddr, char *host, socklen_t hostlen) {
+ struct addrinfo *rai = NULL;
+ const struct addrinfo hint = {
+ .ai_family = AF_UNSPEC,
+ .ai_socktype = SOCK_DGRAM,
+ .ai_protocol = IPPROTO_UDP,
+ };
+
+ if(getaddrinfo(destaddr, "80", &hint, &rai) || !rai)
+ return false;
+
+ int sock = socket(rai->ai_family, rai->ai_socktype, rai->ai_protocol);
+ if(sock == -1) {
+ freeaddrinfo(rai);
+ return false;
+ }
+
+ if(connect(sock, rai->ai_addr, rai->ai_addrlen) && !sockwouldblock(errno)) {
+ freeaddrinfo(rai);
+ return false;
+ }
+
+ freeaddrinfo(rai);
+
+ struct sockaddr_storage sn;
+ socklen_t sl = sizeof(sn);
+
+ if(getsockname(sock, (struct sockaddr *)&sn, &sl))
+ return false;
+
+ if(getnameinfo((struct sockaddr *)&sn, sl, host, hostlen, NULL, 0, NI_NUMERICHOST | NI_NUMERICSERV))
+ return false;
+
+ return true;
+}
+
+// Get our local address(es) by simulating connecting to an Internet host.
+static void add_local_addresses(meshlink_handle_t *mesh) {
+ char host[NI_MAXHOST];
+ char entry[MAX_STRING_SIZE];
+
+ // IPv4 example.org
+
+ if(getlocaladdrname("93.184.216.34", host, sizeof(host))) {
+ snprintf(entry, sizeof(entry), "%s %s", host, mesh->myport);
+ append_config_file(mesh, mesh->name, "Address", entry);
+ }
+
+ // IPv6 example.org
+
+ if(getlocaladdrname("2606:2800:220:1:248:1893:25c8:1946", host, sizeof(host))) {
+ snprintf(entry, sizeof(entry), "%s %s", host, mesh->myport);
+ append_config_file(mesh, mesh->name, "Address", entry);
+ }
+}
+
+static bool meshlink_setup(meshlink_handle_t *mesh) {
+ if(mkdir(mesh->confbase, 0777) && errno != EEXIST) {
+ logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", mesh->confbase, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+
+ char filename[PATH_MAX];
+ snprintf(filename, sizeof(filename), "%s" SLASH "hosts", mesh->confbase);
+
+ if(mkdir(filename, 0777) && errno != EEXIST) {
+ logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", filename, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+
+ snprintf(filename, sizeof(filename), "%s" SLASH "meshlink.conf", mesh->confbase);
+
+ if(!access(filename, F_OK)) {
+ logger(mesh, MESHLINK_DEBUG, "Configuration file %s already exists!\n", filename);
+ meshlink_errno = MESHLINK_EEXIST;
+ return false;
+ }
+
+ FILE *f = fopen(filename, "w");
+ if(!f) {
+ logger(mesh, MESHLINK_DEBUG, "Could not create file %s: %s\n", filename, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+
+ fprintf(f, "Name = %s\n", mesh->name);
+ fclose(f);
+
+ if(!ecdsa_keygen(mesh)) {
+ meshlink_errno = MESHLINK_EINTERNAL;
+ return false;
+ }
+
+ check_port(mesh);
+
+ return true;
+}
+
+meshlink_handle_t *meshlink_open(const char *confbase, const char *name, const char *appname, dev_class_t devclass) {
+ // Validate arguments provided by the application
+ bool usingname = false;
+
+ logger(NULL, MESHLINK_DEBUG, "meshlink_open called\n");
+
+ if(!confbase || !*confbase) {
+ logger(NULL, MESHLINK_ERROR, "No confbase given!\n");
+ meshlink_errno = MESHLINK_EINVAL;
+ return NULL;
+ }
+
+ if(!appname || !*appname) {
+ logger(NULL, MESHLINK_ERROR, "No appname given!\n");
+ meshlink_errno = MESHLINK_EINVAL;
+ return NULL;
+ }
+
+ if(!name || !*name) {
+ logger(NULL, MESHLINK_ERROR, "No name given!\n");
+ //return NULL;
+ } else { //check name only if there is a name != NULL
+
+ if(!check_id(name)) {
+ logger(NULL, MESHLINK_ERROR, "Invalid name given!\n");
+ meshlink_errno = MESHLINK_EINVAL;
+ return NULL;
+ } else
+ usingname = true;
+ }
+
+ if(devclass < 0 || devclass > _DEV_CLASS_MAX) {
+ logger(NULL, MESHLINK_ERROR, "Invalid devclass given!\n");
+ meshlink_errno = MESHLINK_EINVAL;
+ return NULL;
+ }
+
+ meshlink_handle_t *mesh = xzalloc(sizeof(meshlink_handle_t));
+ mesh->confbase = xstrdup(confbase);
+ mesh->appname = xstrdup(appname);
+ mesh->devclass = devclass;
+ if(usingname) mesh->name = xstrdup(name);
+
+ // initialize mutex
+ pthread_mutexattr_t attr;
+ pthread_mutexattr_init(&attr);
+ pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
+ pthread_mutex_init(&(mesh->mesh_mutex), &attr);
+
+ mesh->threadstarted = false;
+ event_loop_init(&mesh->loop);
+ mesh->loop.data = mesh;
+
+ // Check whether meshlink.conf already exists
+
+ char filename[PATH_MAX];
+ snprintf(filename, sizeof(filename), "%s" SLASH "meshlink.conf", confbase);
+
+ if(access(filename, R_OK)) {
+ if(errno == ENOENT) {
+ // If not, create it
+ if(!meshlink_setup(mesh)) {
+ // meshlink_errno is set by meshlink_setup()
+ return NULL;
+ }
+ } else {
+ logger(NULL, MESHLINK_ERROR, "Cannot not read from %s: %s\n", filename, strerror(errno));
+ meshlink_close(mesh);
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return NULL;
+ }
+ }
+
+ // Read the configuration
+
+ init_configuration(&mesh->config);
+
+ if(!read_server_config(mesh)) {
+ meshlink_close(mesh);
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return NULL;
+ };
+
+#ifdef HAVE_MINGW
+ struct WSAData wsa_state;
+ WSAStartup(MAKEWORD(2, 2), &wsa_state);
+#endif
+
+ // Setup up everything
+ // TODO: we should not open listening sockets yet
+
+ if(!setup_network(mesh)) {
+ meshlink_close(mesh);
+ meshlink_errno = MESHLINK_ENETWORK;
+ return NULL;
+ }
+
+ add_local_addresses(mesh);
+
+ idle_set(&mesh->loop, idle, mesh);
+
+ logger(NULL, MESHLINK_DEBUG, "meshlink_open returning\n");
+ return mesh;
+}
+
+static void *meshlink_main_loop(void *arg) {
+ meshlink_handle_t *mesh = arg;
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
+ try_outgoing_connections(mesh);
+
+ logger(mesh, MESHLINK_DEBUG, "Starting main_loop...\n");
+ main_loop(mesh);
+ logger(mesh, MESHLINK_DEBUG, "main_loop returned.\n");
+
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return NULL;
+}
+
+bool meshlink_start(meshlink_handle_t *mesh) {
+ if(!mesh) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return false;
+ }
+
+ logger(mesh, MESHLINK_DEBUG, "meshlink_start called\n");
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
+ if(mesh->threadstarted) {
+ logger(mesh, MESHLINK_DEBUG, "thread was already running\n");
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return true;
+ }
+
+ if(mesh->listen_socket[0].tcp.fd < 0) {
+ logger(mesh, MESHLINK_ERROR, "Listening socket not open\n");
+ meshlink_errno = MESHLINK_ENETWORK;
+ return false;
+ }
+
+ mesh->thedatalen = 0;
+
+ // TODO: open listening sockets first
+
+ //Check that a valid name is set
+ if(!mesh->name) {
+ logger(mesh, MESHLINK_DEBUG, "No name given!\n");
+ meshlink_errno = MESHLINK_EINVAL;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return false;
+ }
+
+ // Start the main thread
+
+ event_loop_start(&mesh->loop);
+
+ if(pthread_create(&mesh->thread, NULL, meshlink_main_loop, mesh) != 0) {
+ logger(mesh, MESHLINK_DEBUG, "Could not start thread: %s\n", strerror(errno));
+ memset(&mesh->thread, 0, sizeof(mesh)->thread);
+ meshlink_errno = MESHLINK_EINTERNAL;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return false;
+ }
+
+ mesh->threadstarted = true;
+
+ if(mesh->discovery)
+ discovery_start(mesh);
+
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return true;
+}
+
+void meshlink_stop(meshlink_handle_t *mesh) {
+ if(!mesh) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return;
+ }
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+ logger(mesh, MESHLINK_DEBUG, "meshlink_stop called\n");
+
+ // Stop discovery
+ if(mesh->discovery)
+ discovery_stop(mesh);
+
+ // Shut down the main thread
+ event_loop_stop(&mesh->loop);
+
+ // Send ourselves a UDP packet to kick the event loop
+ listen_socket_t *s = &mesh->listen_socket[0];
+ if(sendto(s->udp.fd, "", 1, MSG_NOSIGNAL, &s->sa.sa, SALEN(s->sa.sa)) == -1)
+ logger(mesh, MESHLINK_ERROR, "Could not send a UDP packet to ourself");
+
+ // 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;
+
+ // Close all metaconnections
+ if(mesh->connections) {
+ for(list_node_t *node = mesh->connections->head, *next; node; node = next) {
+ next = node->next;
+ connection_t *c = node->data;
+ c->outgoing = NULL;
+ terminate_connection(mesh, c, false);
+ }
+ }
+
+ if(mesh->outgoings)
+ list_delete_list(mesh->outgoings);
+ mesh->outgoings = NULL;
+
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+}
+
+void meshlink_close(meshlink_handle_t *mesh) {
+ if(!mesh || !mesh->confbase) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return;
+ }
+
+ // stop can be called even if mesh has not been started
+ meshlink_stop(mesh);
+
+ // lock is not released after this
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
+ // Close and free all resources used.
+
+ close_network_connections(mesh);
+
+ logger(mesh, MESHLINK_INFO, "Terminating");
+
+ exit_configuration(&mesh->config);
+ event_loop_exit(&mesh->loop);
+
+#ifdef HAVE_MINGW
+ if(mesh->confbase)
+ WSACleanup();
+#endif
+
+ ecdsa_free(mesh->invitation_key);
+
+ free(mesh->name);
+ free(mesh->appname);
+ free(mesh->confbase);
+ pthread_mutex_destroy(&(mesh->mesh_mutex));
+
+ 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;
+ return false;
+ }
+
+ char filename[PATH_MAX];
+ snprintf(filename, sizeof(filename), "%s" SLASH "meshlink.conf", confbase);
+
+ if(unlink(filename)) {
+ if(errno == ENOENT) {
+ meshlink_errno = MESHLINK_ENOENT;
+ return false;
+ } else {
+ logger(NULL, MESHLINK_ERROR, "Cannot delete %s: %s\n", filename, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+ }
+
+ deltree(confbase);
+
+ return true;
+}
+
+void meshlink_set_receive_cb(meshlink_handle_t *mesh, meshlink_receive_cb_t cb) {
+ if(!mesh) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return;
+ }
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+ mesh->receive_cb = cb;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+}
+
+void meshlink_set_node_status_cb(meshlink_handle_t *mesh, meshlink_node_status_cb_t cb) {
+ if(!mesh) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return;
+ }
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+ mesh->node_status_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));
+ mesh->log_cb = cb;
+ mesh->log_level = cb ? level : 0;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ } else {
+ global_log_cb = cb;
+ global_log_level = cb ? level : 0;
+ }
+}
+
+bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const void *data, size_t len) {
+ meshlink_packethdr_t *hdr;
+
+ // Validate arguments
+ if(!mesh || !destination || len >= MAXSIZE - sizeof(*hdr)) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return false;
+ }
+
+ if(!len)
+ return true;
+
+ if(!data) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return false;
+ }
+
+ // Prepare the packet
+ vpn_packet_t *packet = malloc(sizeof(*packet));
+ if(!packet) {
+ meshlink_errno = MESHLINK_ENOMEM;
+ return false;
+ }
+
+ packet->probe = false;
+ packet->tcp = false;
+ packet->len = len + sizeof(*hdr);
+
+ hdr = (meshlink_packethdr_t *)packet->data;
+ memset(hdr, 0, sizeof(*hdr));
+ // leave the last byte as 0 to make sure strings are always
+ // null-terminated if they are longer than the buffer
+ strncpy(hdr->destination, destination->name, (sizeof(hdr)->destination) - 1);
+ strncpy(hdr->source, mesh->self->name, (sizeof(hdr)->source) - 1);
+
+ memcpy(packet->data + sizeof(*hdr), data, len);
+
+ // Queue it
+ if(!meshlink_queue_push(&mesh->outpacketqueue, packet)) {
+ free(packet);
+ meshlink_errno = MESHLINK_ENOMEM;
+ return false;
+ }
+
+ // Notify event loop
+ signal_trigger(&(mesh->loop), &(mesh->datafromapp));
+
+ return true;
+}
+
+void meshlink_send_from_queue(event_loop_t *loop, meshlink_handle_t *mesh) {
+ vpn_packet_t *packet = meshlink_queue_pop(&mesh->outpacketqueue);
+ if(!packet)
+ return;
+
+ mesh->self->in_packets++;
+ mesh->self->in_bytes += packet->len;
+ route(mesh, mesh->self, packet);
+}
+
+ssize_t meshlink_get_pmtu(meshlink_handle_t *mesh, meshlink_node_t *destination) {
+ if(!mesh || !destination) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return -1;
+ }
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
+ node_t *n = (node_t *)destination;
+ if(!n->status.reachable) {
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return 0;
+
+ } else if(n->mtuprobes > 30 && n->minmtu) {
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return n->minmtu;
+ } else {
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return MTU;
+ }
+}
+
+char *meshlink_get_fingerprint(meshlink_handle_t *mesh, meshlink_node_t *node) {
+ if(!mesh || !node) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return NULL;
+ }
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
+ node_t *n = (node_t *)node;
+
+ if(!node_read_ecdsa_public_key(mesh, n) || !n->ecdsa) {
+ meshlink_errno = MESHLINK_EINTERNAL;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return false;
+ }
+
+ char *fingerprint = ecdsa_get_base64_public_key(n->ecdsa);
+
+ if(!fingerprint)
+ meshlink_errno = MESHLINK_EINTERNAL;
+
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return fingerprint;
+}
+
+meshlink_node_t *meshlink_get_self(meshlink_handle_t *mesh) {
+ if(!mesh) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return NULL;
+ }
+
+ return (meshlink_node_t *)mesh->self;
+}
+
+meshlink_node_t *meshlink_get_node(meshlink_handle_t *mesh, const char *name) {
+ if(!mesh || !name) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return NULL;
+ }
+
+ meshlink_node_t *node = NULL;
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+ node = (meshlink_node_t *)lookup_node(mesh, (char *)name); // TODO: make lookup_node() use const
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return node;
+}
+
+meshlink_node_t **meshlink_get_all_nodes(meshlink_handle_t *mesh, meshlink_node_t **nodes, size_t *nmemb) {
+ if(!mesh || !nmemb || (*nmemb && !nodes)) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return NULL;
+ }
+
+ meshlink_node_t **result;
+
+ //lock mesh->nodes
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
+ *nmemb = mesh->nodes->count;
+ result = realloc(nodes, *nmemb * sizeof(*nodes));
+
+ if(result) {
+ meshlink_node_t **p = result;
+ for splay_each(node_t, n, mesh->nodes)
+ *p++ = (meshlink_node_t *)n;
+ } else {
+ *nmemb = 0;
+ free(nodes);
+ meshlink_errno = MESHLINK_ENOMEM;
+ }
+
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+
+ return result;
+}
+
+bool meshlink_sign(meshlink_handle_t *mesh, const void *data, size_t len, void *signature, size_t *siglen) {
+ if(!mesh || !data || !len || !signature || !siglen) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return false;
+ }
+
+ if(*siglen < MESHLINK_SIGLEN) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return false;
+ }
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
+ if(!ecdsa_sign(mesh->self->connection->ecdsa, data, len, signature)) {
+ meshlink_errno = MESHLINK_EINTERNAL;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return false;
+ }
+
+ *siglen = MESHLINK_SIGLEN;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return true;
+}
+
+bool meshlink_verify(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len, const void *signature, size_t siglen) {
+ if(!mesh || !data || !len || !signature) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return false;
+ }
+
+ if(siglen != MESHLINK_SIGLEN) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return false;
+ }
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
+ bool rval = false;
+
+ struct node_t *n = (struct node_t *)source;
+ node_read_ecdsa_public_key(mesh, n);
+ if(!n->ecdsa) {
+ meshlink_errno = MESHLINK_EINTERNAL;
+ rval = false;
+ } else
+ rval = ecdsa_verify(((struct node_t *)source)->ecdsa, data, len, signature);
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return rval;
+}
+
+static bool refresh_invitation_key(meshlink_handle_t *mesh) {
+ char filename[PATH_MAX];
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
+ snprintf(filename, sizeof(filename), "%s" SLASH "invitations", mesh->confbase);
+ if(mkdir(filename, 0700) && errno != EEXIST) {
+ logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", filename, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return false;
+ }
+
+ // Count the number of valid invitations, clean up old ones
+ DIR *dir = opendir(filename);
+ if(!dir) {
+ logger(mesh, MESHLINK_DEBUG, "Could not read directory %s: %s\n", filename, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return false;
+ }
+
+ errno = 0;
+ int count = 0;
+ struct dirent *ent;
+ time_t deadline = time(NULL) - 604800; // 1 week in the past
+
+ while((ent = readdir(dir))) {
+ if(strlen(ent->d_name) != 24)
+ continue;
+ char invname[PATH_MAX];
+ struct stat st;
+ snprintf(invname, sizeof(invname), "%s" SLASH "%s", filename, ent->d_name);
+ if(!stat(invname, &st)) {
+ if(mesh->invitation_key && deadline < st.st_mtime)
+ count++;
+ else
+ unlink(invname);
+ } else {
+ logger(mesh, MESHLINK_DEBUG, "Could not stat %s: %s\n", invname, strerror(errno));
+ errno = 0;
+ }
+ }
+
+ if(errno) {
+ logger(mesh, MESHLINK_DEBUG, "Error while reading directory %s: %s\n", filename, strerror(errno));
+ closedir(dir);
+ meshlink_errno = MESHLINK_ESTORAGE;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return false;
+ }
+
+ closedir(dir);
+
+ snprintf(filename, sizeof(filename), "%s" SLASH "invitations" SLASH "ecdsa_key.priv", mesh->confbase);
+
+ // Remove the key if there are no outstanding invitations.
+ if(!count) {
+ unlink(filename);
+ if(mesh->invitation_key) {
+ ecdsa_free(mesh->invitation_key);
+ mesh->invitation_key = NULL;
+ }
+ }
+
+ if(mesh->invitation_key) {
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return true;
+ }
+
+ // Create a new key if necessary.
+ FILE *f = fopen(filename, "rb");
+ if(!f) {
+ if(errno != ENOENT) {
+ logger(mesh, MESHLINK_DEBUG, "Could not read %s: %s\n", filename, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return false;
+ }
+
+ mesh->invitation_key = ecdsa_generate();
+ if(!mesh->invitation_key) {
+ logger(mesh, MESHLINK_DEBUG, "Could not generate a new key!\n");
+ meshlink_errno = MESHLINK_EINTERNAL;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return false;
+ }
+ f = fopen(filename, "wb");
+ if(!f) {
+ logger(mesh, MESHLINK_DEBUG, "Could not write %s: %s\n", filename, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return false;
+ }
+ chmod(filename, 0600);
+ ecdsa_write_pem_private_key(mesh->invitation_key, f);
+ fclose(f);
+ } else {
+ mesh->invitation_key = ecdsa_read_pem_private_key(f);
+ fclose(f);
+ if(!mesh->invitation_key) {
+ logger(mesh, MESHLINK_DEBUG, "Could not read private key from %s\n", filename);
+ meshlink_errno = MESHLINK_ESTORAGE;
+ }
+ }
+
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return mesh->invitation_key;
+}
+
+bool meshlink_add_address(meshlink_handle_t *mesh, const char *address) {
+ if(!mesh || !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));
+
+ return rval;