X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fmeshlink.c;h=ef02580ea93d641059eb203c7e3a8a17b14c1ab9;hb=01eaeb3c9fa60ae9c6e5b866acd9baef79622d99;hp=65a82c97f192f65b48fdd6a125c5d7d7c36c42b7;hpb=ba799a101d57c55d4cbf602511aa24c32a5b735e;p=meshlink diff --git a/src/meshlink.c b/src/meshlink.c index 65a82c97..ef02580e 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -48,8 +48,6 @@ typedef struct { #define MSG_NOSIGNAL 0 #endif -static pthread_mutex_t global_mutex; - __thread meshlink_errno_t meshlink_errno; meshlink_log_cb_t global_log_cb; meshlink_log_level_t global_log_level; @@ -503,8 +501,10 @@ static bool finalize_join(meshlink_handle_t *mesh) { } char *b64key = ecdsa_get_base64_public_key(mesh->self->connection->ecdsa); - if(!b64key) + if(!b64key) { + fclose(fh); return false; + } fprintf(fh, "ECDSAPublicKey = %s\n", b64key); fprintf(fh, "Port = %s\n", mesh->myport); @@ -554,6 +554,7 @@ static bool invitation_receive(void *handle, uint8_t type, const void *msg, uint break; case 1: + mesh->thedatalen = 0; return finalize_join(mesh); case 2: @@ -742,14 +743,16 @@ static bool meshlink_setup(meshlink_handle_t *mesh) { return true; } -meshlink_handle_t *meshlink_open(const char *confbase, const char *name, const char* appname, dclass_t dclass) { - return meshlink_open_with_size(confbase, name, appname, dclass, sizeof(meshlink_handle_t)); +meshlink_handle_t *meshlink_open(const char *confbase, const char *name, const char* appname, dev_class_t devclass) { + return meshlink_open_with_size(confbase, name, appname, devclass, sizeof(meshlink_handle_t)); } -meshlink_handle_t *meshlink_open_with_size(const char *confbase, const char *name, const char* appname, dclass_t dclass, size_t size) { +meshlink_handle_t *meshlink_open_with_size(const char *confbase, const char *name, const char* appname, dev_class_t devclass, size_t size) { // 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"); @@ -776,12 +779,24 @@ meshlink_handle_t *meshlink_open_with_size(const char *confbase, const char *nam } 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(size); mesh->confbase = xstrdup(confbase); mesh->appname = xstrdup(appname); - mesh->dclass = dclass; + mesh->devclass = devclass; if (usingname) mesh->name = xstrdup(name); - pthread_mutex_init ( &(mesh->nodes_mutex), NULL); + + // 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; @@ -830,29 +845,35 @@ meshlink_handle_t *meshlink_open_with_size(const char *confbase, const char *nam return NULL; } + 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) { - - logger(mesh, MESHLINK_DEBUG, "meshlink_start called\n"); - if(!mesh) { meshlink_errno = MESHLINK_EINVAL; return false; } + pthread_mutex_lock(&(mesh->mesh_mutex)); + + logger(mesh, MESHLINK_DEBUG, "meshlink_start called\n"); + + mesh->thedatalen = 0; // TODO: open listening sockets first @@ -860,6 +881,7 @@ bool meshlink_start(meshlink_handle_t *mesh) { if(!mesh->name ) { logger(mesh, MESHLINK_DEBUG, "No name given!\n"); meshlink_errno = MESHLINK_EINVAL; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return false; } @@ -869,6 +891,7 @@ 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; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return false; } @@ -876,18 +899,19 @@ bool meshlink_start(meshlink_handle_t *mesh) { discovery_start(mesh); + pthread_mutex_unlock(&(mesh->mesh_mutex)); return true; } void meshlink_stop(meshlink_handle_t *mesh) { - - logger(mesh, MESHLINK_DEBUG, "meshlink_stop called\n"); - if(!mesh) { meshlink_errno = MESHLINK_EINVAL; return; } + pthread_mutex_lock(&(mesh->mesh_mutex)); + logger(mesh, MESHLINK_DEBUG, "meshlink_stop called\n"); + // Stop discovery discovery_stop(mesh); @@ -897,8 +921,10 @@ void meshlink_stop(meshlink_handle_t *mesh) { shutdown(s->tcp.fd, SHUT_RDWR); // 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; // Fix the socket @@ -910,6 +936,8 @@ void meshlink_stop(meshlink_handle_t *mesh) { logger(mesh, MESHLINK_ERROR, "Could not repair listenen socket!"); else io_add(&mesh->loop, &s->tcp, handle_new_meta_connection, s, s->tcp.fd, IO_READ); + + pthread_mutex_unlock(&(mesh->mesh_mutex)); } void meshlink_close(meshlink_handle_t *mesh) { @@ -918,6 +946,9 @@ void meshlink_close(meshlink_handle_t *mesh) { return; } + // lock is not released after this + pthread_mutex_lock(&(mesh->mesh_mutex)); + // Close and free all resources used. close_network_connections(mesh); @@ -937,6 +968,7 @@ void meshlink_close(meshlink_handle_t *mesh) { free(mesh->name); free(mesh->appname); free(mesh->confbase); + pthread_mutex_destroy(&(mesh->mesh_mutex)); memset(mesh, 0, sizeof *mesh); @@ -949,7 +981,9 @@ void meshlink_set_receive_cb(meshlink_handle_t *mesh, meshlink_receive_cb_t cb) 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) { @@ -958,13 +992,17 @@ void meshlink_set_node_status_cb(meshlink_handle_t *mesh, meshlink_node_status_c 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; @@ -985,6 +1023,8 @@ bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const return false; } + pthread_mutex_lock(&(mesh->mesh_mutex)); + //add packet to the queue outpacketqueue_t *packet_in_queue = xzalloc(sizeof *packet_in_queue); packet_in_queue->destination=destination; @@ -992,25 +1032,34 @@ bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const packet_in_queue->len=len; if(!meshlink_queue_push(&mesh->outpacketqueue, packet_in_queue)) { free(packet_in_queue); + pthread_mutex_unlock(&(mesh->mesh_mutex)); return false; } //notify event loop signal_trigger(&(mesh->loop),&(mesh->datafromapp)); + + pthread_mutex_unlock(&(mesh->mesh_mutex)); return true; } void meshlink_send_from_queue(event_loop_t* el,meshlink_handle_t *mesh) { + pthread_mutex_lock(&(mesh->mesh_mutex)); + vpn_packet_t packet; meshlink_packethdr_t *hdr = (meshlink_packethdr_t *)packet.data; outpacketqueue_t* p = meshlink_queue_pop(&mesh->outpacketqueue); if(!p) + { + pthread_mutex_unlock(&(mesh->mesh_mutex)); return; + } if (sizeof(meshlink_packethdr_t) + p->len > MAXSIZE) { + pthread_mutex_unlock(&(mesh->mesh_mutex)); //log something - return ; + return; } packet.probe = false; @@ -1024,6 +1073,8 @@ void meshlink_send_from_queue(event_loop_t* el,meshlink_handle_t *mesh) { mesh->self->in_packets++; mesh->self->in_bytes += packet.len; route(mesh, mesh->self, &packet); + + pthread_mutex_unlock(&(mesh->mesh_mutex)); return ; } @@ -1032,14 +1083,22 @@ ssize_t meshlink_get_pmtu(meshlink_handle_t *mesh, meshlink_node_t *destination) meshlink_errno = MESHLINK_EINVAL; return -1; } + pthread_mutex_lock(&(mesh->mesh_mutex)); node_t *n = (node_t *)destination; - if(!n->status.reachable) + if(!n->status.reachable) { + pthread_mutex_unlock(&(mesh->mesh_mutex)); return 0; - else if(n->mtuprobes > 30 && n->minmtu) + + } + else if(n->mtuprobes > 30 && n->minmtu) { + pthread_mutex_unlock(&(mesh->mesh_mutex)); return n->minmtu; - else + } + else { + pthread_mutex_unlock(&(mesh->mesh_mutex)); return MTU; + } } char *meshlink_get_fingerprint(meshlink_handle_t *mesh, meshlink_node_t *node) { @@ -1047,11 +1106,13 @@ char *meshlink_get_fingerprint(meshlink_handle_t *mesh, meshlink_node_t *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; } @@ -1060,6 +1121,7 @@ char *meshlink_get_fingerprint(meshlink_handle_t *mesh, meshlink_node_t *node) { if(!fingerprint) meshlink_errno = MESHLINK_EINTERNAL; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return fingerprint; } @@ -1069,7 +1131,12 @@ meshlink_node_t *meshlink_get_node(meshlink_handle_t *mesh, const char *name) { return NULL; } - return (meshlink_node_t *)lookup_node(mesh, (char *)name); // TODO: make lookup_node() use const + 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) { @@ -1081,7 +1148,7 @@ meshlink_node_t **meshlink_get_all_nodes(meshlink_handle_t *mesh, meshlink_node_ meshlink_node_t **result; //lock mesh->nodes - pthread_mutex_lock(&(mesh->nodes_mutex)); + pthread_mutex_lock(&(mesh->mesh_mutex)); *nmemb = mesh->nodes->count; result = realloc(nodes, *nmemb * sizeof *nodes); @@ -1096,7 +1163,7 @@ meshlink_node_t **meshlink_get_all_nodes(meshlink_handle_t *mesh, meshlink_node_ meshlink_errno = MESHLINK_ENOMEM; } - pthread_mutex_unlock(&(mesh->nodes_mutex)); + pthread_mutex_unlock(&(mesh->mesh_mutex)); return result; } @@ -1112,12 +1179,16 @@ bool meshlink_sign(meshlink_handle_t *mesh, const void *data, size_t len, void * 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; } @@ -1132,23 +1203,32 @@ bool meshlink_verify(meshlink_handle_t *mesh, meshlink_node_t *source, const voi 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; - return false; + rval = false; + } else { + rval = ecdsa_verify(((struct node_t *)source)->ecdsa, data, len, signature); } - - return 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; } @@ -1157,6 +1237,7 @@ static bool refresh_invitation_key(meshlink_handle_t *mesh) { 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; } @@ -1186,6 +1267,7 @@ static bool refresh_invitation_key(meshlink_handle_t *mesh) { 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; } @@ -1202,8 +1284,10 @@ static bool refresh_invitation_key(meshlink_handle_t *mesh) { } } - if(mesh->invitation_key) + if(mesh->invitation_key) { + pthread_mutex_unlock(&(mesh->mesh_mutex)); return true; + } // Create a new key if necessary. FILE *f = fopen(filename, "r"); @@ -1211,6 +1295,7 @@ static bool refresh_invitation_key(meshlink_handle_t *mesh) { 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; } @@ -1218,12 +1303,14 @@ static bool refresh_invitation_key(meshlink_handle_t *mesh) { 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, "w"); 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); @@ -1238,6 +1325,7 @@ static bool refresh_invitation_key(meshlink_handle_t *mesh) { } } + pthread_mutex_unlock(&(mesh->mesh_mutex)); return mesh->invitation_key; } @@ -1246,16 +1334,23 @@ bool meshlink_add_address(meshlink_handle_t *mesh, const char *address) { meshlink_errno = MESHLINK_EINVAL; return false; } + + bool rval = false; + + pthread_mutex_lock(&(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); meshlink_errno = MESHLINK_EINVAL; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return false; } - return append_config_file(mesh, mesh->self->name, "Address", address); + rval = append_config_file(mesh, mesh->self->name, "Address", address); + pthread_mutex_unlock(&(mesh->mesh_mutex)); + return rval; } char *meshlink_invite(meshlink_handle_t *mesh, const char *name) { @@ -1263,11 +1358,14 @@ char *meshlink_invite(meshlink_handle_t *mesh, const char *name) { meshlink_errno = MESHLINK_EINVAL; return NULL; } + + pthread_mutex_lock(&(mesh->mesh_mutex)); // Check validity of the new node's name if(!check_id(name)) { logger(mesh, MESHLINK_DEBUG, "Invalid name for node.\n"); meshlink_errno = MESHLINK_EINVAL; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return NULL; } @@ -1277,6 +1375,7 @@ char *meshlink_invite(meshlink_handle_t *mesh, const char *name) { if(!access(filename, F_OK)) { logger(mesh, MESHLINK_DEBUG, "A host config file for %s already exists!\n", name); meshlink_errno = MESHLINK_EEXIST; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return NULL; } @@ -1284,6 +1383,7 @@ char *meshlink_invite(meshlink_handle_t *mesh, const char *name) { if(meshlink_get_node(mesh, name)) { logger(mesh, MESHLINK_DEBUG, "A node with name %s is already known!\n", name); meshlink_errno = MESHLINK_EEXIST; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return NULL; } @@ -1292,11 +1392,13 @@ char *meshlink_invite(meshlink_handle_t *mesh, const char *name) { if(!address) { logger(mesh, MESHLINK_DEBUG, "No Address known for ourselves!\n"); meshlink_errno = MESHLINK_ERESOLV; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return NULL; } if(!refresh_invitation_key(mesh)) { meshlink_errno = MESHLINK_EINTERNAL; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return NULL; } @@ -1329,6 +1431,7 @@ char *meshlink_invite(meshlink_handle_t *mesh, const char *name) { if(!ifd) { logger(mesh, MESHLINK_DEBUG, "Could not create invitation file %s: %s\n", filename, strerror(errno)); meshlink_errno = MESHLINK_ESTORAGE; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return NULL; } FILE *f = fdopen(ifd, "w"); @@ -1359,6 +1462,7 @@ char *meshlink_invite(meshlink_handle_t *mesh, const char *name) { } else { logger(mesh, MESHLINK_DEBUG, "Could not create %s: %s\n", filename, strerror(errno)); meshlink_errno = MESHLINK_ESTORAGE; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return NULL; } @@ -1374,6 +1478,7 @@ char *meshlink_invite(meshlink_handle_t *mesh, const char *name) { xasprintf(&url, "%s/%s%s", address, hash, cookie); free(address); + pthread_mutex_unlock(&(mesh->mesh_mutex)); return url; } @@ -1382,6 +1487,8 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) { meshlink_errno = MESHLINK_EINVAL; return false; } + + pthread_mutex_lock(&(mesh->mesh_mutex)); //TODO: think of a better name for this variable, or of a different way to tokenize the invitation URL. char copy[strlen(invitation) + 1]; @@ -1424,6 +1531,7 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) { ecdsa_t *key = ecdsa_generate(); if(!key) { meshlink_errno = MESHLINK_EINTERNAL; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return false; } @@ -1438,6 +1546,7 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) { struct addrinfo *ai = str2addrinfo(address, port, SOCK_STREAM); if(!ai) { meshlink_errno = MESHLINK_ERESOLV; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return false; } @@ -1446,6 +1555,7 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) { logger(mesh, MESHLINK_DEBUG, "Could not open socket: %s\n", strerror(errno)); freeaddrinfo(ai); meshlink_errno = MESHLINK_ENETWORK; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return false; } @@ -1454,6 +1564,7 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) { closesocket(mesh->sock); freeaddrinfo(ai); meshlink_errno = MESHLINK_ENETWORK; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return false; } @@ -1469,6 +1580,7 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) { logger(mesh, MESHLINK_DEBUG, "Error sending request to %s port %s: %s\n", address, port, strerror(errno)); closesocket(mesh->sock); meshlink_errno = MESHLINK_ENETWORK; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return false; } @@ -1481,6 +1593,7 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) { logger(mesh, MESHLINK_DEBUG, "Cannot read greeting from peer\n"); closesocket(mesh->sock); meshlink_errno = MESHLINK_ENETWORK; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return false; } @@ -1490,11 +1603,13 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) { if(sha512(fingerprint, strlen(fingerprint), hishash)) { logger(mesh, MESHLINK_DEBUG, "Could not create hash\n%s\n", mesh->line + 2); meshlink_errno = MESHLINK_EINTERNAL; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return false; } if(memcmp(hishash, mesh->hash, 18)) { logger(mesh, MESHLINK_DEBUG, "Peer has an invalid key!\n%s\n", mesh->line + 2); meshlink_errno = MESHLINK_EPEER; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return false; } @@ -1502,18 +1617,21 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) { ecdsa_t *hiskey = ecdsa_set_base64_public_key(fingerprint); if(!hiskey) { meshlink_errno = MESHLINK_EINTERNAL; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return false; } // Start an SPTPS session if(!sptps_start(&mesh->sptps, mesh, true, false, key, hiskey, "meshlink invitation", 15, invitation_send, invitation_receive)) { meshlink_errno = MESHLINK_EINTERNAL; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return false; } // Feed rest of input buffer to SPTPS if(!sptps_receive_data(&mesh->sptps, mesh->buffer, mesh->blen)) { meshlink_errno = MESHLINK_EPEER; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return false; } @@ -1525,11 +1643,13 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) { continue; logger(mesh, MESHLINK_DEBUG, "Error reading data from %s port %s: %s\n", address, port, strerror(errno)); meshlink_errno = MESHLINK_ENETWORK; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return false; } if(!sptps_receive_data(&mesh->sptps, mesh->line, len)) { meshlink_errno = MESHLINK_EPEER; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return false; } } @@ -1542,14 +1662,17 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) { if(!mesh->success) { logger(mesh, MESHLINK_DEBUG, "Connection closed by peer, invitation cancelled.\n"); meshlink_errno = MESHLINK_EPEER; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return false; } + pthread_mutex_unlock(&(mesh->mesh_mutex)); return true; invalid: logger(mesh, MESHLINK_DEBUG, "Invalid invitation URL or you are already connected to a Mesh ?\n"); meshlink_errno = MESHLINK_EINVAL; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return false; } @@ -1559,12 +1682,15 @@ char *meshlink_export(meshlink_handle_t *mesh) { return NULL; } + pthread_mutex_lock(&(mesh->mesh_mutex)); + char filename[PATH_MAX]; snprintf(filename, sizeof filename, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, mesh->self->name); FILE *f = fopen(filename, "r"); if(!f) { logger(mesh, MESHLINK_DEBUG, "Could not open %s: %s\n", filename, strerror(errno)); meshlink_errno = MESHLINK_ESTORAGE; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return NULL; } @@ -1579,11 +1705,14 @@ char *meshlink_export(meshlink_handle_t *mesh) { logger(mesh, MESHLINK_DEBUG, "Error reading from %s: %s\n", filename, strerror(errno)); fclose(f); meshlink_errno = MESHLINK_ESTORAGE; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return NULL; } fclose(f); buf[len - 1] = 0; + + pthread_mutex_unlock(&(mesh->mesh_mutex)); return buf; } @@ -1592,10 +1721,13 @@ bool meshlink_import(meshlink_handle_t *mesh, const char *data) { meshlink_errno = MESHLINK_EINVAL; return false; } + + pthread_mutex_lock(&(mesh->mesh_mutex)); if(strncmp(data, "Name = ", 7)) { logger(mesh, MESHLINK_DEBUG, "Invalid data\n"); meshlink_errno = MESHLINK_EPEER; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return false; } @@ -1603,6 +1735,7 @@ bool meshlink_import(meshlink_handle_t *mesh, const char *data) { if(!end) { logger(mesh, MESHLINK_DEBUG, "Invalid data\n"); meshlink_errno = MESHLINK_EPEER; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return false; } @@ -1613,6 +1746,7 @@ bool meshlink_import(meshlink_handle_t *mesh, const char *data) { if(!check_id(name)) { logger(mesh, MESHLINK_DEBUG, "Invalid Name\n"); meshlink_errno = MESHLINK_EPEER; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return false; } @@ -1621,12 +1755,14 @@ bool meshlink_import(meshlink_handle_t *mesh, const char *data) { if(!access(filename, F_OK)) { logger(mesh, MESHLINK_DEBUG, "File %s already exists, not importing\n", filename); meshlink_errno = MESHLINK_EEXIST; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return false; } if(errno != ENOENT) { logger(mesh, MESHLINK_DEBUG, "Error accessing %s: %s\n", filename, strerror(errno)); meshlink_errno = MESHLINK_ESTORAGE; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return false; } @@ -1634,6 +1770,7 @@ bool meshlink_import(meshlink_handle_t *mesh, const char *data) { if(!f) { logger(mesh, MESHLINK_DEBUG, "Could not create %s: %s\n", filename, strerror(errno)); meshlink_errno = MESHLINK_ESTORAGE; + pthread_mutex_unlock(&(mesh->mesh_mutex)); return false; } @@ -1642,6 +1779,7 @@ bool meshlink_import(meshlink_handle_t *mesh, const char *data) { load_all_nodes(mesh); + pthread_mutex_unlock(&(mesh->mesh_mutex)); return true; } @@ -1651,6 +1789,8 @@ void meshlink_blacklist(meshlink_handle_t *mesh, meshlink_node_t *node) { return; } + pthread_mutex_lock(&(mesh->mesh_mutex)); + node_t *n; n = (node_t*)node; n->status.blacklisted=true; @@ -1659,6 +1799,7 @@ 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"); + pthread_mutex_unlock(&(mesh->mesh_mutex)); return; } @@ -1668,75 +1809,194 @@ void meshlink_whitelist(meshlink_handle_t *mesh, meshlink_node_t *node) { return; } + pthread_mutex_lock(&(mesh->mesh_mutex)); + node_t *n = (node_t *)node; n->status.blacklisted = false; //TODO: remove blacklisted = yes from the config file + pthread_mutex_unlock(&(mesh->mesh_mutex)); return; } /* Hint that a hostname may be found at an address * See header file for detailed comment. */ -extern void meshlink_hint_address(meshlink_handle_t *mesh, meshlink_node_t *node, struct sockaddr *addr) { +void meshlink_hint_address(meshlink_handle_t *mesh, meshlink_node_t *node, const struct sockaddr *addr) { if(!mesh || !node || !addr) return; - char *addr_str = malloc(MAX_ADDRESS_LENGTH*sizeof(char)); - memset(addr_str, 0, MAX_ADDRESS_LENGTH*sizeof(char)); - - char *port_str = malloc(MAX_PORT_LENGTH*sizeof(char)); - memset(port_str, 0, MAX_PORT_LENGTH*sizeof(char)); + pthread_mutex_lock(&(mesh->mesh_mutex)); - // extra byte for a space, and one to make sure string is null-terminated - int full_addr_len = MAX_ADDRESS_LENGTH + MAX_PORT_LENGTH + 2; + char *host = NULL, *port = NULL, *str = NULL; + sockaddr2str((const sockaddr_t *)addr, &host, &port); - char *full_addr_str = malloc(full_addr_len*sizeof(char)); - memset(full_addr_str, 0, full_addr_len*sizeof(char)); - - // get address and port number - if(!get_ip_str(addr, addr_str, MAX_ADDRESS_LENGTH)) - goto fail; - if(!get_port_str(addr, port_str, MAX_ADDRESS_LENGTH)) - goto fail; - - // append_config_file expects an address, a space, and then a port number - strcat(full_addr_str, addr_str); - strcat(full_addr_str, " "); - strcat(full_addr_str, port_str); - - append_config_file(mesh, node->name, "Address", full_addr_str); + if(host && port) { + xasprintf(&str, "%s %s", host, port); + if ( (strncmp ("fe80",host,4) != 0) && ( strncmp("127.",host,4) != 0 ) && ( strcmp("localhost",host) !=0 ) ) + append_config_file(mesh, node->name, "Address", str); + else + logger(mesh, MESHLINK_DEBUG, "Not adding Link Local IPv6 Address to config\n"); + } -fail: - free(addr_str); - free(port_str); - free(full_addr_str); + free(str); + free(host); + free(port); + pthread_mutex_unlock(&(mesh->mesh_mutex)); // @TODO do we want to fire off a connection attempt right away? } -static void __attribute__((constructor)) meshlink_init(void) { - crypto_init(); +/* Return an array of edges in the current network graph. + * Data captures the current state and will not be updated. + * Caller must deallocate data when done. + */ +meshlink_edge_t **meshlink_get_all_edges_state(meshlink_handle_t *mesh, meshlink_edge_t **edges, size_t *nmemb) { + if(!mesh || !nmemb || (*nmemb && !edges)) { + meshlink_errno = MESHLINK_EINVAL; + return NULL; + } + + pthread_mutex_lock(&(mesh->mesh_mutex)); + + meshlink_edge_t **result = NULL; + meshlink_edge_t *copy = NULL; + int result_size = 0; + + result_size = mesh->edges->count; + + // if result is smaller than edges, we have to dealloc all the excess meshlink_edge_t + if(result_size > *nmemb) { + result = realloc(edges, result_size * sizeof (meshlink_edge_t*)); + } else { + result = edges; + } + + if(result) { + meshlink_edge_t **p = result; + int n = 0; + for splay_each(edge_t, e, mesh->edges) { + // skip edges that do not represent a two-directional connection + if((!e->reverse) || (e->reverse->to != e->from)) { + result_size--; + continue; + } + n++; + // the first *nmemb members of result can be re-used + if(n > *nmemb) { + copy = xzalloc(sizeof *copy); + } + else { + copy = *p; + } + copy->from = (meshlink_node_t*)e->from; + copy->to = (meshlink_node_t*)e->to; + copy->address = e->address.storage; + copy->options = e->options; + copy->weight = e->weight; + *p++ = copy; + } + // shrink result to the actual amount of memory used + for(int i = *nmemb; i > result_size; i--) { + free(result[i - 1]); + } + result = realloc(result, result_size * sizeof (meshlink_edge_t*)); + *nmemb = result_size; + } else { + *nmemb = 0; + free(result); + meshlink_errno = MESHLINK_ENOMEM; + } + + pthread_mutex_unlock(&(mesh->mesh_mutex)); + + return result; } -static void __attribute__((destructor)) meshlink_exit(void) { - crypto_exit(); +static bool channel_pre_accept(struct utcp *utcp, uint16_t port) { + //TODO: implement + return false; } -int weight_from_dclass(dclass_t dclass) -{ - switch(dclass) - { - case BACKBONE: - return 1; +static void channel_accept(struct utcp_connection *utcp_connection, uint16_t port) { + //TODO: implement +} - case STATIONARY: - return 3; +static int channel_recv(struct utcp_connection *connection, const void *data, size_t len) { + meshlink_channel_t *channel = connection->priv; + node_t *n = channel->node; + meshlink_handle_t *mesh = n->mesh; + if(!channel->receive_cb) + return -1; + else { + channel->receive_cb(mesh, channel, data, len); + return 0; + } +} + +static int channel_send(struct utcp *utcp, const void *data, size_t len) { + node_t *n = utcp->priv; + meshlink_handle_t *mesh = n->mesh; + return meshlink_send(mesh, (meshlink_node_t *)n, data, len) ? len : -1; +} + +void meshlink_set_channel_accept_cb(meshlink_handle_t *mesh, meshlink_channel_accept_cb_t cb) { + mesh->channel_accept_cb = cb; +} - case PORTABLE: - return 6; +void meshlink_set_channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_channel_receive_cb_t cb) { + channel->receive_cb = cb; +} + +meshlink_channel_t *meshlink_channel_open(meshlink_handle_t *mesh, meshlink_node_t *node, uint16_t port, meshlink_channel_receive_cb_t cb, const void *data, size_t len) { + node_t *n = (node_t *)node; + if(!n->utcp) { + n->utcp = utcp_init(channel_accept, channel_pre_accept, channel_send, n); + if(!n->utcp) + return NULL; } + meshlink_channel_t *channel = xzalloc(sizeof *channel); + channel->node = n; + channel->receive_cb = cb; + channel->c = utcp_connect(n->utcp, port, channel_recv, channel); + if(!channel->c) { + free(channel); + return NULL; + } + return channel; +} - return 9; +void meshlink_channel_shutdown(meshlink_handle_t *mesh, meshlink_channel_t *channel, int direction) { + utcp_shutdown(channel->c, direction); } + +void meshlink_channel_close(meshlink_handle_t *mesh, meshlink_channel_t *channel) { + utcp_close(channel->c); + free(channel); +} + +ssize_t meshlink_channel_send(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) { + // TODO: locking. + // Ideally we want to put the data into the UTCP connection's send buffer. + // Then, preferrably only if there is room in the receiver window, + // kick the meshlink thread to go send packets. + return utcp_send(channel->c, data, len); +} + +static void __attribute__((constructor)) meshlink_init(void) { + crypto_init(); +} + +static void __attribute__((destructor)) meshlink_exit(void) { + crypto_exit(); +} + + +/// Device class traits +dev_class_traits_t dev_class_traits[_DEV_CLASS_MAX +1] = { + { .min_connects = 3, .max_connects = 10000, .edge_weight = 1 }, // DEV_CLASS_BACKBONE + { .min_connects = 3, .max_connects = 100, .edge_weight = 3 }, // DEV_CLASS_STATIONARY + { .min_connects = 3, .max_connects = 3, .edge_weight = 6 }, // DEV_CLASS_PORTABLE + { .min_connects = 1, .max_connects = 1, .edge_weight = 9 }, // DEV_CLASS_UNKNOWN +};