X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fmeshlink.c;h=a8f8c928876b034fb169a4c92821faebd851226f;hb=HEAD;hp=d0b8bef5d7c6ee46533069baf865939610070887;hpb=a036178f1a7b4484e7159c4a857632bb10e06739;p=meshlink diff --git a/src/meshlink.c b/src/meshlink.c index d0b8bef5..a1a2ec40 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -1,6 +1,6 @@ /* meshlink.c -- Implementation of the MeshLink API. - Copyright (C) 2014-2018 Guus Sliepen + Copyright (C) 2014-2021 Guus Sliepen 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 @@ -154,7 +154,9 @@ static int socket_in_netns(int domain, int type, int protocol, int netns) { pthread_t thr; if(pthread_create(&thr, NULL, socket_in_netns_thread, ¶ms) == 0) { - assert(pthread_join(thr, NULL) == 0); + if(pthread_join(thr, NULL) != 0) { + abort(); + } } return params.fd; @@ -224,6 +226,7 @@ char *meshlink_get_external_address(meshlink_handle_t *mesh) { } char *meshlink_get_external_address_for_family(meshlink_handle_t *mesh, int family) { + logger(mesh, MESHLINK_DEBUG, "meshlink_get_external_address_for_family(%d)", family); const char *url = mesh->external_address_url; if(!url) { @@ -342,8 +345,59 @@ static bool is_localaddr(sockaddr_t *sa) { } } +#ifdef HAVE_GETIFADDRS +struct getifaddrs_in_netns_params { + struct ifaddrs **ifa; + int netns; +}; + +#ifdef HAVE_SETNS +static void *getifaddrs_in_netns_thread(void *arg) { + struct getifaddrs_in_netns_params *params = arg; + + if(setns(params->netns, CLONE_NEWNET) == -1) { + meshlink_errno = MESHLINK_EINVAL; + return NULL; + } + + if(getifaddrs(params->ifa) != 0) { + *params->ifa = NULL; + } + + return NULL; +} +#endif // HAVE_SETNS + +static int getifaddrs_in_netns(struct ifaddrs **ifa, int netns) { + if(netns == -1) { + return getifaddrs(ifa); + } + +#ifdef HAVE_SETNS + struct getifaddrs_in_netns_params params = {ifa, netns}; + pthread_t thr; + + if(pthread_create(&thr, NULL, getifaddrs_in_netns_thread, ¶ms) == 0) { + if(pthread_join(thr, NULL) != 0) { + abort(); + } + } + + return *params.ifa ? 0 : -1; +#else + return -1; +#endif // HAVE_SETNS + +} +#endif + char *meshlink_get_local_address_for_family(meshlink_handle_t *mesh, int family) { - (void)mesh; + logger(mesh, MESHLINK_DEBUG, "meshlink_get_local_address_for_family(%d)", family); + + if(!mesh) { + meshlink_errno = MESHLINK_EINVAL; + return NULL; + } // Determine address of the local interface used for outgoing connections. char localaddr[NI_MAXHOST]; @@ -359,12 +413,12 @@ char *meshlink_get_local_address_for_family(meshlink_handle_t *mesh, int family) if(!success) { struct ifaddrs *ifa = NULL; - getifaddrs(&ifa); + getifaddrs_in_netns(&ifa, mesh->netns); for(struct ifaddrs *ifap = ifa; ifap; ifap = ifap->ifa_next) { sockaddr_t *sa = (sockaddr_t *)ifap->ifa_addr; - if(sa->sa.sa_family != family) { + if(!sa || sa->sa.sa_family != family) { continue; } @@ -597,7 +651,7 @@ static bool try_bind(meshlink_handle_t *mesh, int port) { int check_port(meshlink_handle_t *mesh) { for(int i = 0; i < 1000; i++) { - int port = 0x1000 + prng(mesh, 0x8000); + int port = 0x1000 + prng(mesh, 0x7000); if(try_bind(mesh, port)) { free(mesh->myport); @@ -638,7 +692,7 @@ static bool write_main_config_files(meshlink_handle_t *mesh) { } /* Write our own host config file */ - if(!node_write_config(mesh, mesh->self)) { + if(!node_write_config(mesh, mesh->self, true)) { return false; } @@ -738,7 +792,7 @@ static bool finalize_join(join_state_t *state, const void *buf, uint16_t len) { } if(!strcmp(name2, mesh->name)) { - logger(mesh, MESHLINK_DEBUG, "Secondary chunk would overwrite our own host config file.\n"); + logger(mesh, MESHLINK_ERROR, "Secondary chunk would overwrite our own host config file.\n"); free(name2); meshlink_errno = MESHLINK_EPEER; return false; @@ -772,7 +826,7 @@ static bool finalize_join(join_state_t *state, const void *buf, uint16_t len) { n->last_reachable = 0; n->last_unreachable = 0; - if(!node_write_config(mesh, n)) { + if(!node_write_config(mesh, n, true)) { free_node(n); return false; } @@ -781,7 +835,7 @@ static bool finalize_join(join_state_t *state, const void *buf, uint16_t len) { } /* Ensure the configuration directory metadata is on disk */ - if(!config_sync(mesh, "current") || !sync_path(mesh->confbase)) { + if(!config_sync(mesh, "current") || (mesh->confbase && !sync_path(mesh->confbase))) { return false; } @@ -957,7 +1011,7 @@ static bool ecdsa_keygen(meshlink_handle_t *mesh) { mesh->invitation_key = ecdsa_generate(); if(!mesh->private_key || !mesh->invitation_key) { - logger(mesh, MESHLINK_DEBUG, "Error during key generation!\n"); + logger(mesh, MESHLINK_ERROR, "Error during key generation!\n"); meshlink_errno = MESHLINK_EINTERNAL; return false; } @@ -1141,6 +1195,8 @@ static void *setup_network_in_netns_thread(void *arg) { #endif // HAVE_SETNS meshlink_open_params_t *meshlink_open_params_init(const char *confbase, const char *name, const char *appname, dev_class_t devclass) { + logger(NULL, MESHLINK_DEBUG, "meshlink_open_params_init(%s, %s, %s, %d)", confbase, name, appname, devclass); + if(!confbase || !*confbase) { logger(NULL, MESHLINK_ERROR, "No confbase given!\n"); meshlink_errno = MESHLINK_EINVAL; @@ -1179,10 +1235,14 @@ meshlink_open_params_t *meshlink_open_params_init(const char *confbase, const ch params->devclass = devclass; params->netns = -1; + xasprintf(¶ms->lock_filename, "%s" SLASH "meshlink.lock", confbase); + return params; } bool meshlink_open_params_set_netns(meshlink_open_params_t *params, int netns) { + logger(NULL, MESHLINK_DEBUG, "meshlink_open_params_set_netnst(%d)", netns); + if(!params) { meshlink_errno = MESHLINK_EINVAL; return false; @@ -1194,6 +1254,8 @@ bool meshlink_open_params_set_netns(meshlink_open_params_t *params, int netns) { } bool meshlink_open_params_set_storage_key(meshlink_open_params_t *params, const void *key, size_t keylen) { + logger(NULL, MESHLINK_DEBUG, "meshlink_open_params_set_storage_key(%p, %zu)", key, keylen); + if(!params) { meshlink_errno = MESHLINK_EINVAL; return false; @@ -1211,14 +1273,45 @@ bool meshlink_open_params_set_storage_key(meshlink_open_params_t *params, const return true; } +bool meshlink_open_params_set_storage_policy(meshlink_open_params_t *params, meshlink_storage_policy_t policy) { + logger(NULL, MESHLINK_DEBUG, "meshlink_open_params_set_storage_policy(%d)", policy); + + if(!params) { + meshlink_errno = MESHLINK_EINVAL; + return false; + } + + params->storage_policy = policy; + + return true; +} + +bool meshlink_open_params_set_lock_filename(meshlink_open_params_t *params, const char *filename) { + logger(NULL, MESHLINK_DEBUG, "meshlink_open_params_set_lock_filename(%s)", filename); + + if(!params || !filename) { + meshlink_errno = MESHLINK_EINVAL; + return false; + } + + free(params->lock_filename); + params->lock_filename = xstrdup(filename); + + return true; +} + bool meshlink_encrypted_key_rotate(meshlink_handle_t *mesh, const void *new_key, size_t new_keylen) { + logger(NULL, MESHLINK_DEBUG, "meshlink_encrypted_key_rotate(%p, %zu)", new_key, new_keylen); + if(!mesh || !new_key || !new_keylen) { logger(mesh, MESHLINK_ERROR, "Invalid arguments given!\n"); meshlink_errno = MESHLINK_EINVAL; return false; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } // Create hash for the new key void *new_config_key; @@ -1227,7 +1320,7 @@ bool meshlink_encrypted_key_rotate(meshlink_handle_t *mesh, const void *new_key, if(!prf(new_key, new_keylen, "MeshLink configuration key", 26, new_config_key, CHACHA_POLY1305_KEYLEN)) { logger(mesh, MESHLINK_ERROR, "Error creating new configuration key!\n"); meshlink_errno = MESHLINK_EINTERNAL; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return false; } @@ -1236,7 +1329,7 @@ bool meshlink_encrypted_key_rotate(meshlink_handle_t *mesh, const void *new_key, if(!config_copy(mesh, "current", mesh->config_key, "new", new_config_key)) { logger(mesh, MESHLINK_ERROR, "Could not set up configuration in %s/old: %s\n", mesh->confbase, strerror(errno)); meshlink_errno = MESHLINK_ESTORAGE; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return false; } @@ -1247,7 +1340,7 @@ bool meshlink_encrypted_key_rotate(meshlink_handle_t *mesh, const void *new_key, if(!config_rename(mesh, "current", "old")) { logger(mesh, MESHLINK_ERROR, "Cannot rename %s/current to %s/old\n", mesh->confbase, mesh->confbase); meshlink_errno = MESHLINK_ESTORAGE; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return false; } @@ -1258,7 +1351,7 @@ bool meshlink_encrypted_key_rotate(meshlink_handle_t *mesh, const void *new_key, if(!config_rename(mesh, "new", "current")) { logger(mesh, MESHLINK_ERROR, "Cannot rename %s/new to %s/current\n", mesh->confbase, mesh->confbase); meshlink_errno = MESHLINK_ESTORAGE; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return false; } @@ -1267,7 +1360,7 @@ bool meshlink_encrypted_key_rotate(meshlink_handle_t *mesh, const void *new_key, // Cleanup the "old" confbase sub-directory if(!config_destroy(mesh->confbase, "old")) { - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return false; } @@ -1276,12 +1369,14 @@ bool meshlink_encrypted_key_rotate(meshlink_handle_t *mesh, const void *new_key, free(mesh->config_key); mesh->config_key = new_config_key; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return true; } void meshlink_open_params_free(meshlink_open_params_t *params) { + logger(NULL, MESHLINK_DEBUG, "meshlink_open_params_free()"); + if(!params) { meshlink_errno = MESHLINK_EINVAL; return; @@ -1290,54 +1385,65 @@ void meshlink_open_params_free(meshlink_open_params_t *params) { free(params->confbase); free(params->name); free(params->appname); + free(params->lock_filename); free(params); } /// Device class traits static const dev_class_traits_t default_class_traits[DEV_CLASS_COUNT] = { - { .pingtimeout = 5, .pinginterval = 60, .min_connects = 3, .max_connects = 10000, .edge_weight = 1 }, // DEV_CLASS_BACKBONE - { .pingtimeout = 5, .pinginterval = 60, .min_connects = 3, .max_connects = 100, .edge_weight = 3 }, // DEV_CLASS_STATIONARY - { .pingtimeout = 5, .pinginterval = 60, .min_connects = 3, .max_connects = 3, .edge_weight = 6 }, // DEV_CLASS_PORTABLE - { .pingtimeout = 5, .pinginterval = 60, .min_connects = 1, .max_connects = 1, .edge_weight = 9 }, // DEV_CLASS_UNKNOWN + { .pingtimeout = 5, .pinginterval = 60, .maxtimeout = 900, .min_connects = 3, .max_connects = 10000, .edge_weight = 1 }, // DEV_CLASS_BACKBONE + { .pingtimeout = 5, .pinginterval = 60, .maxtimeout = 900, .min_connects = 3, .max_connects = 100, .edge_weight = 3 }, // DEV_CLASS_STATIONARY + { .pingtimeout = 5, .pinginterval = 60, .maxtimeout = 900, .min_connects = 3, .max_connects = 3, .edge_weight = 6 }, // DEV_CLASS_PORTABLE + { .pingtimeout = 5, .pinginterval = 60, .maxtimeout = 900, .min_connects = 1, .max_connects = 1, .edge_weight = 9 }, // DEV_CLASS_UNKNOWN }; meshlink_handle_t *meshlink_open(const char *confbase, const char *name, const char *appname, dev_class_t devclass) { + logger(NULL, MESHLINK_DEBUG, "meshlink_open(%s, %s, %s, %d)", confbase, name, appname, devclass); + if(!confbase || !*confbase) { logger(NULL, MESHLINK_ERROR, "No confbase given!\n"); meshlink_errno = MESHLINK_EINVAL; return NULL; } - /* Create a temporary struct on the stack, to avoid allocating and freeing one. */ - meshlink_open_params_t params; - memset(¶ms, 0, sizeof(params)); + char lock_filename[PATH_MAX]; + snprintf(lock_filename, sizeof(lock_filename), "%s" SLASH "meshlink.lock", confbase); - params.confbase = (char *)confbase; - params.name = (char *)name; - params.appname = (char *)appname; - params.devclass = devclass; - params.netns = -1; + /* Create a temporary struct on the stack, to avoid allocating and freeing one. */ + meshlink_open_params_t params = { + .confbase = (char *)confbase, + .lock_filename = lock_filename, + .name = (char *)name, + .appname = (char *)appname, + .devclass = devclass, + .netns = -1, + }; return meshlink_open_ex(¶ms); } meshlink_handle_t *meshlink_open_encrypted(const char *confbase, const char *name, const char *appname, dev_class_t devclass, const void *key, size_t keylen) { + logger(NULL, MESHLINK_DEBUG, "meshlink_open_encrypted(%s, %s, %s, %d, %p, %zu)", confbase, name, appname, devclass, key, keylen); + if(!confbase || !*confbase) { logger(NULL, MESHLINK_ERROR, "No confbase given!\n"); meshlink_errno = MESHLINK_EINVAL; return NULL; } - /* Create a temporary struct on the stack, to avoid allocating and freeing one. */ - meshlink_open_params_t params; - memset(¶ms, 0, sizeof(params)); + char lock_filename[PATH_MAX]; + snprintf(lock_filename, sizeof(lock_filename), "%s" SLASH "meshlink.lock", confbase); - params.confbase = (char *)confbase; - params.name = (char *)name; - params.appname = (char *)appname; - params.devclass = devclass; - params.netns = -1; + /* Create a temporary struct on the stack, to avoid allocating and freeing one. */ + meshlink_open_params_t params = { + .confbase = (char *)confbase, + .lock_filename = lock_filename, + .name = (char *)name, + .appname = (char *)appname, + .devclass = devclass, + .netns = -1, + }; if(!meshlink_open_params_set_storage_key(¶ms, key, keylen)) { return false; @@ -1347,6 +1453,8 @@ meshlink_handle_t *meshlink_open_encrypted(const char *confbase, const char *nam } meshlink_handle_t *meshlink_open_ephemeral(const char *name, const char *appname, dev_class_t devclass) { + logger(NULL, MESHLINK_DEBUG, "meshlink_open_ephemeral(%s, %s, %d)", name, appname, devclass); + if(!name) { logger(NULL, MESHLINK_ERROR, "No name given!\n"); meshlink_errno = MESHLINK_EINVAL; @@ -1378,19 +1486,18 @@ meshlink_handle_t *meshlink_open_ephemeral(const char *name, const char *appname } /* Create a temporary struct on the stack, to avoid allocating and freeing one. */ - meshlink_open_params_t params; - memset(¶ms, 0, sizeof(params)); - - params.name = (char *)name; - params.appname = (char *)appname; - params.devclass = devclass; - params.netns = -1; + meshlink_open_params_t params = { + .name = (char *)name, + .appname = (char *)appname, + .devclass = devclass, + .netns = -1, + }; return meshlink_open_ex(¶ms); } meshlink_handle_t *meshlink_open_ex(const meshlink_open_params_t *params) { - logger(NULL, MESHLINK_DEBUG, "meshlink_open called\n"); + logger(NULL, MESHLINK_DEBUG, "meshlink_open_ex()"); // Validate arguments provided by the application if(!params->appname || !*params->appname) { @@ -1431,7 +1538,7 @@ meshlink_handle_t *meshlink_open_ex(const meshlink_open_params_t *params) { mesh->appname = xstrdup(params->appname); mesh->devclass = params->devclass; - mesh->discovery = true; + mesh->discovery.enabled = true; mesh->invitation_timeout = 604800; // 1 week mesh->netns = params->netns; mesh->submeshes = NULL; @@ -1463,15 +1570,16 @@ meshlink_handle_t *meshlink_open_ex(const meshlink_open_params_t *params) { // initialize mutexes and conds pthread_mutexattr_t attr; - assert(pthread_mutexattr_init(&attr) == 0); - assert(pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) == 0); - assert(pthread_mutex_init(&mesh->mutex, &attr) == 0); - assert(pthread_cond_init(&mesh->cond, NULL) == 0); + pthread_mutexattr_init(&attr); + + if(pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0) { + abort(); + } - assert(pthread_mutex_init(&mesh->discovery_mutex, NULL) == 0); - assert(pthread_cond_init(&mesh->discovery_cond, NULL) == 0); + pthread_mutex_init(&mesh->mutex, &attr); + pthread_cond_init(&mesh->cond, NULL); - assert(pthread_cond_init(&mesh->adns_cond, NULL) == 0); + pthread_cond_init(&mesh->adns_cond, NULL); mesh->threadstarted = false; event_loop_init(&mesh->loop); @@ -1480,13 +1588,15 @@ meshlink_handle_t *meshlink_open_ex(const meshlink_open_params_t *params) { meshlink_queue_init(&mesh->outpacketqueue); // Atomically lock the configuration directory. - if(!main_config_lock(mesh)) { + if(!main_config_lock(mesh, params->lock_filename)) { meshlink_close(mesh); return NULL; } // If no configuration exists yet, create it. + bool new_configuration = false; + if(!meshlink_confbase_exists(mesh)) { if(!mesh->name) { logger(NULL, MESHLINK_ERROR, "No configuration files found!\n"); @@ -1500,6 +1610,8 @@ meshlink_handle_t *meshlink_open_ex(const meshlink_open_params_t *params) { meshlink_close(mesh); return NULL; } + + new_configuration = true; } else { if(!meshlink_read_config(mesh)) { logger(NULL, MESHLINK_ERROR, "Cannot read main configuration\n"); @@ -1508,6 +1620,8 @@ meshlink_handle_t *meshlink_open_ex(const meshlink_open_params_t *params) { } } + mesh->storage_policy = params->storage_policy; + #ifdef HAVE_MINGW struct WSAData wsa_state; WSAStartup(MAKEWORD(2, 2), &wsa_state); @@ -1544,7 +1658,7 @@ meshlink_handle_t *meshlink_open_ex(const meshlink_open_params_t *params) { add_local_addresses(mesh); - if(!node_write_config(mesh, mesh->self)) { + if(!node_write_config(mesh, mesh->self, new_configuration)) { logger(NULL, MESHLINK_ERROR, "Cannot update configuration\n"); return NULL; } @@ -1555,7 +1669,9 @@ meshlink_handle_t *meshlink_open_ex(const meshlink_open_params_t *params) { return mesh; } -meshlink_submesh_t *meshlink_submesh_open(meshlink_handle_t *mesh, const char *submesh) { +meshlink_submesh_t *meshlink_submesh_open(meshlink_handle_t *mesh, const char *submesh) { + logger(NULL, MESHLINK_DEBUG, "meshlink_submesh_open(%s)", submesh); + meshlink_submesh_t *s = NULL; if(!mesh) { @@ -1571,11 +1687,13 @@ meshlink_submesh_t *meshlink_submesh_open(meshlink_handle_t *mesh, const char * } //lock mesh->nodes - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } s = (meshlink_submesh_t *)create_submesh(mesh, submesh); - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return s; } @@ -1587,42 +1705,44 @@ static void *meshlink_main_loop(void *arg) { #ifdef HAVE_SETNS if(setns(mesh->netns, CLONE_NEWNET) != 0) { - assert(pthread_cond_signal(&mesh->cond) == 0); + pthread_cond_signal(&mesh->cond); return NULL; } #else - assert(pthread_cond_signal(&mesh->cond) == 0); + pthread_cond_signal(&mesh->cond); return NULL; #endif // HAVE_SETNS } -#if HAVE_CATTA - - if(mesh->discovery) { + if(mesh->discovery.enabled) { discovery_start(mesh); } -#endif + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(mesh->thread_status_cb) { + mesh->thread_status_cb(mesh, true); + } logger(mesh, MESHLINK_DEBUG, "Starting main_loop...\n"); - assert(pthread_cond_broadcast(&mesh->cond) == 0); + pthread_cond_broadcast(&mesh->cond); main_loop(mesh); logger(mesh, MESHLINK_DEBUG, "main_loop returned.\n"); - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + if(mesh->thread_status_cb) { + mesh->thread_status_cb(mesh, false); + } -#if HAVE_CATTA + pthread_mutex_unlock(&mesh->mutex); // Stop discovery - if(mesh->discovery) { + if(mesh->discovery.enabled) { discovery_stop(mesh); } -#endif - return NULL; } @@ -1634,7 +1754,9 @@ bool meshlink_start(meshlink_handle_t *mesh) { logger(mesh, MESHLINK_DEBUG, "meshlink_start called\n"); - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } assert(mesh->self); assert(mesh->private_key); @@ -1643,7 +1765,7 @@ bool meshlink_start(meshlink_handle_t *mesh) { if(mesh->threadstarted) { logger(mesh, MESHLINK_DEBUG, "thread was already running\n"); - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return true; } @@ -1653,13 +1775,18 @@ bool meshlink_start(meshlink_handle_t *mesh) { return false; } + // Reset node connection timers + for splay_each(node_t, n, mesh->nodes) { + n->last_connect_try = 0; + } + // TODO: open listening sockets first //Check that a valid name is set if(!mesh->name) { - logger(mesh, MESHLINK_DEBUG, "No name given!\n"); + logger(mesh, MESHLINK_ERROR, "No name given!\n"); meshlink_errno = MESHLINK_EINVAL; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return false; } @@ -1672,36 +1799,39 @@ bool meshlink_start(meshlink_handle_t *mesh) { // Ensure we have a decent amount of stack space. Musl's default of 80 kB is too small. pthread_attr_t attr; - assert(pthread_attr_init(&attr) == 0); - assert(pthread_attr_setstacksize(&attr, 1024 * 1024) == 0); + pthread_attr_init(&attr); + pthread_attr_setstacksize(&attr, 1024 * 1024); if(pthread_create(&mesh->thread, &attr, meshlink_main_loop, mesh) != 0) { - logger(mesh, MESHLINK_DEBUG, "Could not start thread: %s\n", strerror(errno)); + logger(mesh, MESHLINK_ERROR, "Could not start thread: %s\n", strerror(errno)); memset(&mesh->thread, 0, sizeof(mesh)->thread); meshlink_errno = MESHLINK_EINTERNAL; event_loop_stop(&mesh->loop); - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return false; } - assert(pthread_cond_wait(&mesh->cond, &mesh->mutex) == 0); + pthread_cond_wait(&mesh->cond, &mesh->mutex); mesh->threadstarted = true; // Ensure we are considered reachable graph(mesh); - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return true; } void meshlink_stop(meshlink_handle_t *mesh) { + logger(mesh, MESHLINK_DEBUG, "meshlink_stop()\n"); + if(!mesh) { meshlink_errno = MESHLINK_EINVAL; return; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); - logger(mesh, MESHLINK_DEBUG, "meshlink_stop called\n"); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } // Shut down the main thread event_loop_stop(&mesh->loop); @@ -1723,9 +1853,15 @@ void meshlink_stop(meshlink_handle_t *mesh) { if(mesh->threadstarted) { // Wait for the main thread to finish - assert(pthread_mutex_unlock(&mesh->mutex) == 0); - assert(pthread_join(mesh->thread, NULL) == 0); - assert(pthread_mutex_lock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); + + if(pthread_join(mesh->thread, NULL) != 0) { + abort(); + } + + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } mesh->threadstarted = false; } @@ -1752,15 +1888,19 @@ void meshlink_stop(meshlink_handle_t *mesh) { if(mesh->nodes) { for splay_each(node_t, n, mesh->nodes) { if(n->status.dirty) { - n->status.dirty = !node_write_config(mesh, n); + if(!node_write_config(mesh, n, false)) { + // ignore + } } } } - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); } void meshlink_close(meshlink_handle_t *mesh) { + logger(mesh, MESHLINK_DEBUG, "meshlink_close()\n"); + if(!mesh) { meshlink_errno = MESHLINK_EINVAL; return; @@ -1770,7 +1910,9 @@ void meshlink_close(meshlink_handle_t *mesh) { meshlink_stop(mesh); // lock is not released after this - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } // Close and free all resources used. @@ -1814,33 +1956,37 @@ void meshlink_close(meshlink_handle_t *mesh) { main_config_unlock(mesh); - assert(pthread_mutex_unlock(&mesh->mutex) == 0); - assert(pthread_mutex_destroy(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); + pthread_mutex_destroy(&mesh->mutex); memset(mesh, 0, sizeof(*mesh)); free(mesh); } -bool meshlink_destroy(const char *confbase) { - if(!confbase) { +bool meshlink_destroy_ex(const meshlink_open_params_t *params) { + logger(NULL, MESHLINK_DEBUG, "meshlink_destroy_ex()\n"); + + if(!params) { meshlink_errno = MESHLINK_EINVAL; return false; } + if(!params->confbase) { + /* Ephemeral instances */ + return true; + } + /* Exit early if the confbase directory itself doesn't exist */ - if(access(confbase, F_OK) && errno == ENOENT) { + if(access(params->confbase, F_OK) && errno == ENOENT) { return true; } /* Take the lock the same way meshlink_open() would. */ - char lockfilename[PATH_MAX]; - snprintf(lockfilename, sizeof(lockfilename), "%s" SLASH "meshlink.lock", confbase); - - FILE *lockfile = fopen(lockfilename, "w+"); + FILE *lockfile = fopen(params->lock_filename, "w+"); if(!lockfile) { - logger(NULL, MESHLINK_ERROR, "Could not open lock file %s: %s", lockfilename, strerror(errno)); + logger(NULL, MESHLINK_ERROR, "Could not open lock file %s: %s", params->lock_filename, strerror(errno)); meshlink_errno = MESHLINK_ESTORAGE; return false; } @@ -1854,7 +2000,7 @@ bool meshlink_destroy(const char *confbase) { #else if(flock(fileno(lockfile), LOCK_EX | LOCK_NB) != 0) { - logger(NULL, MESHLINK_ERROR, "Configuration directory %s still in use\n", lockfilename); + logger(NULL, MESHLINK_ERROR, "Configuration directory %s still in use\n", params->lock_filename); fclose(lockfile); meshlink_errno = MESHLINK_EBUSY; return false; @@ -1862,13 +2008,13 @@ bool meshlink_destroy(const char *confbase) { #endif - if(!config_destroy(confbase, "current") || !config_destroy(confbase, "new") || !config_destroy(confbase, "old")) { - logger(NULL, MESHLINK_ERROR, "Cannot remove sub-directories in %s: %s\n", confbase, strerror(errno)); + if(!config_destroy(params->confbase, "current") || !config_destroy(params->confbase, "new") || !config_destroy(params->confbase, "old")) { + logger(NULL, MESHLINK_ERROR, "Cannot remove sub-directories in %s: %s\n", params->confbase, strerror(errno)); return false; } - if(unlink(lockfilename)) { - logger(NULL, MESHLINK_ERROR, "Cannot remove lock file %s: %s\n", lockfilename, strerror(errno)); + if(unlink(params->lock_filename)) { + logger(NULL, MESHLINK_ERROR, "Cannot remove lock file %s: %s\n", params->lock_filename, strerror(errno)); fclose(lockfile); meshlink_errno = MESHLINK_ESTORAGE; return false; @@ -1876,8 +2022,8 @@ bool meshlink_destroy(const char *confbase) { fclose(lockfile); - if(!sync_path(confbase)) { - logger(NULL, MESHLINK_ERROR, "Cannot sync directory %s: %s\n", confbase, strerror(errno)); + if(!sync_path(params->confbase)) { + logger(NULL, MESHLINK_ERROR, "Cannot sync directory %s: %s\n", params->confbase, strerror(errno)); meshlink_errno = MESHLINK_ESTORAGE; return false; } @@ -1885,67 +2031,111 @@ bool meshlink_destroy(const char *confbase) { return true; } +bool meshlink_destroy(const char *confbase) { + logger(NULL, MESHLINK_DEBUG, "meshlink_destroy(%s)", confbase); + + char lock_filename[PATH_MAX]; + snprintf(lock_filename, sizeof(lock_filename), "%s" SLASH "meshlink.lock", confbase); + + meshlink_open_params_t params = { + .confbase = (char *)confbase, + .lock_filename = lock_filename, + }; + + return meshlink_destroy_ex(¶ms); +} + void meshlink_set_receive_cb(meshlink_handle_t *mesh, meshlink_receive_cb_t cb) { + logger(mesh, MESHLINK_DEBUG, "meshlink_set_receive_cb(%p)", (void *)(intptr_t)cb); + if(!mesh) { meshlink_errno = MESHLINK_EINVAL; return; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + mesh->receive_cb = cb; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); } void meshlink_set_connection_try_cb(meshlink_handle_t *mesh, meshlink_connection_try_cb_t cb) { + logger(mesh, MESHLINK_DEBUG, "meshlink_set_connection_try_cb(%p)", (void *)(intptr_t)cb); + if(!mesh) { meshlink_errno = MESHLINK_EINVAL; return; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + mesh->connection_try_cb = cb; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); } void meshlink_set_node_status_cb(meshlink_handle_t *mesh, meshlink_node_status_cb_t cb) { + logger(mesh, MESHLINK_DEBUG, "meshlink_set_node_status_cb(%p)", (void *)(intptr_t)cb); + if(!mesh) { meshlink_errno = MESHLINK_EINVAL; return; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + mesh->node_status_cb = cb; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); } void meshlink_set_node_pmtu_cb(meshlink_handle_t *mesh, meshlink_node_pmtu_cb_t cb) { + logger(mesh, MESHLINK_DEBUG, "meshlink_set_node_pmtu_cb(%p)", (void *)(intptr_t)cb); + if(!mesh) { meshlink_errno = MESHLINK_EINVAL; return; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + mesh->node_pmtu_cb = cb; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); } void meshlink_set_node_duplicate_cb(meshlink_handle_t *mesh, meshlink_node_duplicate_cb_t cb) { + logger(mesh, MESHLINK_DEBUG, "meshlink_set_node_duplicate_cb(%p)", (void *)(intptr_t)cb); + if(!mesh) { meshlink_errno = MESHLINK_EINVAL; return; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + mesh->node_duplicate_cb = cb; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); } void meshlink_set_log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, meshlink_log_cb_t cb) { + logger(mesh, MESHLINK_DEBUG, "meshlink_set_log_cb(%p)", (void *)(intptr_t)cb); + if(mesh) { - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + mesh->log_cb = cb; mesh->log_level = cb ? level : 0; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); } else { global_log_cb = cb; global_log_level = cb ? level : 0; @@ -1953,14 +2143,51 @@ void meshlink_set_log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, me } void meshlink_set_error_cb(struct meshlink_handle *mesh, meshlink_error_cb_t cb) { + logger(mesh, MESHLINK_DEBUG, "meshlink_set_error_cb(%p)", (void *)(intptr_t)cb); + if(!mesh) { meshlink_errno = MESHLINK_EINVAL; return; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + mesh->error_cb = cb; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); +} + +void meshlink_set_blacklisted_cb(struct meshlink_handle *mesh, meshlink_blacklisted_cb_t cb) { + logger(mesh, MESHLINK_DEBUG, "meshlink_set_blacklisted_cb(%p)", (void *)(intptr_t)cb); + + if(!mesh) { + meshlink_errno = MESHLINK_EINVAL; + return; + } + + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + + mesh->blacklisted_cb = cb; + pthread_mutex_unlock(&mesh->mutex); +} + +void meshlink_set_thread_status_cb(struct meshlink_handle *mesh, meshlink_thread_status_cb_t cb) { + logger(mesh, MESHLINK_DEBUG, "meshlink_set_thread_status_cb(%p)", (void *)(intptr_t)cb); + + if(!mesh) { + meshlink_errno = MESHLINK_EINVAL; + return; + } + + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + + mesh->thread_status_cb = cb; + pthread_mutex_unlock(&mesh->mutex); } static bool prepare_packet(meshlink_handle_t *mesh, meshlink_node_t *destination, const void *data, size_t len, vpn_packet_t *packet) { @@ -2014,6 +2241,8 @@ static bool meshlink_send_immediate(meshlink_handle_t *mesh, meshlink_node_t *de } bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const void *data, size_t len) { + logger(mesh, MESHLINK_DEBUG, "meshlink_send(%s, %p, %zu)", destination ? destination->name : "(null)", data, len); + // Validate arguments if(!mesh || !destination) { meshlink_errno = MESHLINK_EINVAL; @@ -2065,8 +2294,6 @@ void meshlink_send_from_queue(event_loop_t *loop, void *data) { for(vpn_packet_t *packet; (packet = meshlink_queue_pop(&mesh->outpacketqueue));) { logger(mesh, MESHLINK_DEBUG, "Removing packet of %d bytes from packet queue", packet->len); - mesh->self->in_packets++; - mesh->self->in_bytes += packet->len; route(mesh, mesh->self, packet); free(packet); } @@ -2078,19 +2305,21 @@ ssize_t meshlink_get_pmtu(meshlink_handle_t *mesh, meshlink_node_t *destination) return -1; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } node_t *n = (node_t *)destination; if(!n->status.reachable) { - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return 0; } else if(n->mtuprobes > 30 && n->minmtu) { - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return n->minmtu; } else { - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return MTU; } } @@ -2101,13 +2330,15 @@ char *meshlink_get_fingerprint(meshlink_handle_t *mesh, meshlink_node_t *node) { return NULL; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } node_t *n = (node_t *)node; if(!node_read_public_key(mesh, n) || !n->ecdsa) { meshlink_errno = MESHLINK_EINTERNAL; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return false; } @@ -2117,7 +2348,7 @@ char *meshlink_get_fingerprint(meshlink_handle_t *mesh, meshlink_node_t *node) { meshlink_errno = MESHLINK_EINTERNAL; } - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return fingerprint; } @@ -2138,9 +2369,12 @@ meshlink_node_t *meshlink_get_node(meshlink_handle_t *mesh, const char *name) { node_t *n = NULL; - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + n = lookup_node(mesh, (char *)name); // TODO: make lookup_node() use const - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); if(!n) { meshlink_errno = MESHLINK_ENOENT; @@ -2157,9 +2391,12 @@ meshlink_submesh_t *meshlink_get_submesh(meshlink_handle_t *mesh, const char *na meshlink_submesh_t *submesh = NULL; - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + submesh = (meshlink_submesh_t *)lookup_submesh(mesh, name); - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); if(!submesh) { meshlink_errno = MESHLINK_ENOENT; @@ -2177,7 +2414,9 @@ meshlink_node_t **meshlink_get_all_nodes(meshlink_handle_t *mesh, meshlink_node_ meshlink_node_t **result; //lock mesh->nodes - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } *nmemb = mesh->nodes->count; result = realloc(nodes, *nmemb * sizeof(*nodes)); @@ -2194,7 +2433,7 @@ meshlink_node_t **meshlink_get_all_nodes(meshlink_handle_t *mesh, meshlink_node_ meshlink_errno = MESHLINK_ENOMEM; } - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return result; } @@ -2202,7 +2441,9 @@ meshlink_node_t **meshlink_get_all_nodes(meshlink_handle_t *mesh, meshlink_node_ static meshlink_node_t **meshlink_get_all_nodes_by_condition(meshlink_handle_t *mesh, const void *condition, meshlink_node_t **nodes, size_t *nmemb, search_node_by_condition_t search_node) { meshlink_node_t **result; - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } *nmemb = 0; @@ -2214,7 +2455,7 @@ static meshlink_node_t **meshlink_get_all_nodes_by_condition(meshlink_handle_t * if(*nmemb == 0) { free(nodes); - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return NULL; } @@ -2234,7 +2475,7 @@ static meshlink_node_t **meshlink_get_all_nodes_by_condition(meshlink_handle_t * meshlink_errno = MESHLINK_ENOMEM; } - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return result; } @@ -2249,6 +2490,10 @@ static bool search_node_by_dev_class(const node_t *node, const void *condition) return false; } +static bool search_node_by_blacklisted(const node_t *node, const void *condition) { + return *(bool *)condition == node->status.blacklisted; +} + static bool search_node_by_submesh(const node_t *node, const void *condition) { if(condition == node->submesh) { return true; @@ -2311,6 +2556,15 @@ meshlink_node_t **meshlink_get_all_nodes_by_last_reachable(meshlink_handle_t *me return meshlink_get_all_nodes_by_condition(mesh, &range, nodes, nmemb, search_node_by_last_reachable); } +meshlink_node_t **meshlink_get_all_nodes_by_blacklisted(meshlink_handle_t *mesh, bool blacklisted, meshlink_node_t **nodes, size_t *nmemb) { + if(!mesh || !nmemb) { + meshlink_errno = MESHLINK_EINVAL; + return NULL; + } + + return meshlink_get_all_nodes_by_condition(mesh, &blacklisted, nodes, nmemb, search_node_by_blacklisted); +} + dev_class_t meshlink_get_node_dev_class(meshlink_handle_t *mesh, meshlink_node_t *node) { if(!mesh || !node) { meshlink_errno = MESHLINK_EINVAL; @@ -2319,15 +2573,58 @@ dev_class_t meshlink_get_node_dev_class(meshlink_handle_t *mesh, meshlink_node_t dev_class_t devclass; - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } devclass = ((node_t *)node)->devclass; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return devclass; } +bool meshlink_get_node_tiny(meshlink_handle_t *mesh, meshlink_node_t *node) { + if(!mesh || !node) { + meshlink_errno = MESHLINK_EINVAL; + return -1; + } + + bool tiny; + + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + + tiny = ((node_t *)node)->status.tiny; + + pthread_mutex_unlock(&mesh->mutex); + + return tiny; +} + +bool meshlink_get_node_blacklisted(meshlink_handle_t *mesh, meshlink_node_t *node) { + if(!mesh) { + meshlink_errno = MESHLINK_EINVAL; + } + + if(!node) { + return mesh->default_blacklist; + } + + bool blacklisted; + + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + + blacklisted = ((node_t *)node)->status.blacklisted; + + pthread_mutex_unlock(&mesh->mutex); + + return blacklisted; +} + meshlink_submesh_t *meshlink_get_node_submesh(meshlink_handle_t *mesh, meshlink_node_t *node) { if(!mesh || !node) { meshlink_errno = MESHLINK_EINVAL; @@ -2352,7 +2649,10 @@ bool meshlink_get_node_reachability(struct meshlink_handle *mesh, struct meshlin node_t *n = (node_t *)node; bool reachable; - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + reachable = n->status.reachable && !n->status.blacklisted; if(last_reachable) { @@ -2363,12 +2663,14 @@ bool meshlink_get_node_reachability(struct meshlink_handle *mesh, struct meshlin *last_unreachable = n->last_unreachable; } - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return reachable; } bool meshlink_sign(meshlink_handle_t *mesh, const void *data, size_t len, void *signature, size_t *siglen) { + logger(mesh, MESHLINK_DEBUG, "meshlink_sign(%p, %zu, %p, %p)", data, len, signature, (void *)siglen); + if(!mesh || !data || !len || !signature || !siglen) { meshlink_errno = MESHLINK_EINVAL; return false; @@ -2379,20 +2681,24 @@ bool meshlink_sign(meshlink_handle_t *mesh, const void *data, size_t len, void * return false; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } if(!ecdsa_sign(mesh->private_key, data, len, signature)) { meshlink_errno = MESHLINK_EINTERNAL; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return false; } *siglen = MESHLINK_SIGLEN; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&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) { + logger(mesh, MESHLINK_DEBUG, "meshlink_verify(%p, %zu, %p, %zu)", data, len, signature, siglen); + if(!mesh || !source || !data || !len || !signature) { meshlink_errno = MESHLINK_EINVAL; return false; @@ -2403,7 +2709,9 @@ bool meshlink_verify(meshlink_handle_t *mesh, meshlink_node_t *source, const voi return false; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } bool rval = false; @@ -2416,12 +2724,14 @@ bool meshlink_verify(meshlink_handle_t *mesh, meshlink_node_t *source, const voi rval = ecdsa_verify(((struct node_t *)source)->ecdsa, data, len, signature); } - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return rval; } static bool refresh_invitation_key(meshlink_handle_t *mesh) { - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } size_t count = invitation_purge_old(mesh, time(NULL) - mesh->invitation_timeout); @@ -2429,74 +2739,102 @@ static bool refresh_invitation_key(meshlink_handle_t *mesh) { // TODO: Update invitation key if necessary? } - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return mesh->invitation_key; } bool meshlink_set_canonical_address(meshlink_handle_t *mesh, meshlink_node_t *node, const char *address, const char *port) { + logger(mesh, MESHLINK_DEBUG, "meshlink_set_canonical_address(%s, %s, %s)", node ? node->name : "(null)", address ? address : "(null)", port ? port : "(null)"); + if(!mesh || !node || !address) { meshlink_errno = MESHLINK_EINVAL; return false; } if(!is_valid_hostname(address)) { - logger(mesh, MESHLINK_DEBUG, "Invalid character in address: %s", address); + logger(mesh, MESHLINK_ERROR, "Invalid character in address: %s", address); meshlink_errno = MESHLINK_EINVAL; return false; } if((node_t *)node != mesh->self && !port) { - logger(mesh, MESHLINK_DEBUG, "Missing port number!"); + logger(mesh, MESHLINK_ERROR, "Missing port number!"); meshlink_errno = MESHLINK_EINVAL; return false; } if(port && !is_valid_port(port)) { - logger(mesh, MESHLINK_DEBUG, "Invalid character in port: %s", address); + logger(mesh, MESHLINK_ERROR, "Invalid character in port: %s", address); meshlink_errno = MESHLINK_EINVAL; return false; } char *canonical_address; - if(port) { - xasprintf(&canonical_address, "%s %s", address, port); - } else { - canonical_address = xstrdup(address); - } + xasprintf(&canonical_address, "%s %s", address, port ? port : mesh->myport); - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } node_t *n = (node_t *)node; free(n->canonical_address); n->canonical_address = canonical_address; - if(!node_write_config(mesh, n)) { - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + if(!node_write_config(mesh, n, false)) { + pthread_mutex_unlock(&mesh->mutex); + return false; + } + + pthread_mutex_unlock(&mesh->mutex); + + return config_sync(mesh, "current"); +} + +bool meshlink_clear_canonical_address(meshlink_handle_t *mesh, meshlink_node_t *node) { + logger(mesh, MESHLINK_DEBUG, "meshlink_clear_canonical_address(%s)", node ? node->name : "(null)"); + + if(!mesh || !node) { + meshlink_errno = MESHLINK_EINVAL; + return false; + } + + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + + node_t *n = (node_t *)node; + free(n->canonical_address); + n->canonical_address = NULL; + + if(!node_write_config(mesh, n, false)) { + pthread_mutex_unlock(&mesh->mutex); return false; } - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return config_sync(mesh, "current"); } bool meshlink_add_invitation_address(struct meshlink_handle *mesh, const char *address, const char *port) { + logger(mesh, MESHLINK_DEBUG, "meshlink_add_invitation_address(%s, %s)", address ? address : "(null)", port ? port : "(null)"); + 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); + logger(mesh, MESHLINK_ERROR, "Invalid character in address: %s\n", address); meshlink_errno = MESHLINK_EINVAL; return false; } if(port && !is_valid_port(port)) { - logger(mesh, MESHLINK_DEBUG, "Invalid character in port: %s\n", address); + logger(mesh, MESHLINK_ERROR, "Invalid character in port: %s\n", address); meshlink_errno = MESHLINK_EINVAL; return false; } @@ -2509,39 +2847,49 @@ bool meshlink_add_invitation_address(struct meshlink_handle *mesh, const char *a combo = xstrdup(address); } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } if(!mesh->invitation_addresses) { mesh->invitation_addresses = list_alloc((list_action_t)free); } list_insert_tail(mesh->invitation_addresses, combo); - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return true; } void meshlink_clear_invitation_addresses(struct meshlink_handle *mesh) { + logger(mesh, MESHLINK_DEBUG, "meshlink_clear_invitation_addresses()"); + if(!mesh) { meshlink_errno = MESHLINK_EINVAL; return; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } if(mesh->invitation_addresses) { list_delete_list(mesh->invitation_addresses); mesh->invitation_addresses = NULL; } - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); } bool meshlink_add_address(meshlink_handle_t *mesh, const char *address) { + logger(mesh, MESHLINK_DEBUG, "meshlink_add_address(%s)", address ? address : "(null)"); + return meshlink_set_canonical_address(mesh, (meshlink_node_t *)mesh->self, address, NULL); } bool meshlink_add_external_address(meshlink_handle_t *mesh) { + logger(mesh, MESHLINK_DEBUG, "meshlink_add_external_address()"); + if(!mesh) { meshlink_errno = MESHLINK_EINVAL; return false; @@ -2572,14 +2920,19 @@ int meshlink_get_port(meshlink_handle_t *mesh) { int port; - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + port = atoi(mesh->myport); - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return port; } bool meshlink_set_port(meshlink_handle_t *mesh, int port) { + logger(mesh, MESHLINK_DEBUG, "meshlink_set_port(%d)", port); + if(!mesh || port < 0 || port >= 65536 || mesh->threadstarted) { meshlink_errno = MESHLINK_EINVAL; return false; @@ -2598,7 +2951,9 @@ bool meshlink_set_port(meshlink_handle_t *mesh, int port) { bool rval = false; - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } if(mesh->threadstarted) { meshlink_errno = MESHLINK_EINVAL; @@ -2639,16 +2994,20 @@ bool meshlink_set_port(meshlink_handle_t *mesh, int port) { rval = config_sync(mesh, "current"); done: - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return rval && meshlink_get_port(mesh) == port; } void meshlink_set_invitation_timeout(meshlink_handle_t *mesh, int timeout) { + logger(mesh, MESHLINK_DEBUG, "meshlink_invitation_timeout(%d)", timeout); + mesh->invitation_timeout = timeout; } char *meshlink_invite_ex(meshlink_handle_t *mesh, meshlink_submesh_t *submesh, const char *name, uint32_t flags) { + logger(mesh, MESHLINK_DEBUG, "meshlink_invite_ex(%s, %s, %u)", submesh ? submesh->name : "(null)", name ? name : "(null)", flags); + meshlink_submesh_t *s = NULL; if(!mesh) { @@ -2660,7 +3019,7 @@ char *meshlink_invite_ex(meshlink_handle_t *mesh, meshlink_submesh_t *submesh, c s = (meshlink_submesh_t *)lookup_submesh(mesh, submesh->name); if(s != submesh) { - logger(mesh, MESHLINK_DEBUG, "Invalid SubMesh Handle.\n"); + logger(mesh, MESHLINK_ERROR, "Invalid submesh handle.\n"); meshlink_errno = MESHLINK_EINVAL; return NULL; } @@ -2668,13 +3027,15 @@ char *meshlink_invite_ex(meshlink_handle_t *mesh, meshlink_submesh_t *submesh, c s = (meshlink_submesh_t *)mesh->self->submesh; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } // Check validity of the new node's name if(!check_id(name)) { logger(mesh, MESHLINK_ERROR, "Invalid name for node.\n"); meshlink_errno = MESHLINK_EINVAL; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return NULL; } @@ -2682,7 +3043,7 @@ char *meshlink_invite_ex(meshlink_handle_t *mesh, meshlink_submesh_t *submesh, c if(config_exists(mesh, "current", name)) { logger(mesh, MESHLINK_ERROR, "A host config file for %s already exists!\n", name); meshlink_errno = MESHLINK_EEXIST; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return NULL; } @@ -2690,7 +3051,7 @@ char *meshlink_invite_ex(meshlink_handle_t *mesh, meshlink_submesh_t *submesh, c if(lookup_node(mesh, name)) { logger(mesh, MESHLINK_ERROR, "A node with name %s is already known!\n", name); meshlink_errno = MESHLINK_EEXIST; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return NULL; } @@ -2700,21 +3061,21 @@ char *meshlink_invite_ex(meshlink_handle_t *mesh, meshlink_submesh_t *submesh, c if(!address) { logger(mesh, MESHLINK_ERROR, "No Address known for ourselves!\n"); meshlink_errno = MESHLINK_ERESOLV; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return NULL; } if(!refresh_invitation_key(mesh)) { meshlink_errno = MESHLINK_EINTERNAL; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return NULL; } // If we changed our own host config file, write it out now if(mesh->self->status.dirty) { - if(!node_write_config(mesh, mesh->self)) { + if(!node_write_config(mesh, mesh->self, false)) { logger(mesh, MESHLINK_ERROR, "Could not write our own host config file!\n"); - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return NULL; } } @@ -2776,7 +3137,7 @@ char *meshlink_invite_ex(meshlink_handle_t *mesh, meshlink_submesh_t *submesh, c if(!invitation_write(mesh, "current", cookiehash, &config, mesh->config_key)) { logger(mesh, MESHLINK_DEBUG, "Could not create invitation file %s: %s\n", cookiehash, strerror(errno)); meshlink_errno = MESHLINK_ESTORAGE; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return NULL; } @@ -2785,20 +3146,29 @@ char *meshlink_invite_ex(meshlink_handle_t *mesh, meshlink_submesh_t *submesh, c xasprintf(&url, "%s/%s%s", address, hash, cookie); free(address); - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return url; } char *meshlink_invite(meshlink_handle_t *mesh, meshlink_submesh_t *submesh, const char *name) { + logger(mesh, MESHLINK_DEBUG, "meshlink_invite_ex(%s, %s)", submesh ? submesh->name : "(null)", name ? name : "(null)"); + return meshlink_invite_ex(mesh, submesh, name, 0); } bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) { + logger(mesh, MESHLINK_DEBUG, "meshlink_join(%s)", invitation ? invitation : "(null)"); + if(!mesh || !invitation) { meshlink_errno = MESHLINK_EINVAL; return false; } + if(mesh->storage_policy == MESHLINK_STORAGE_DISABLED) { + meshlink_errno = MESHLINK_EINVAL; + return false; + } + join_state_t state = { .mesh = mesh, .sock = -1, @@ -2810,7 +3180,9 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) { //TODO: think of a better name for this variable, or of a different way to tokenize the invitation URL. char copy[strlen(invitation) + 1]; - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } //Before doing meshlink_join make sure we are not connected to another mesh if(mesh->threadstarted) { @@ -2898,7 +3270,7 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) { } // Connect to the meshlink daemon mentioned in the URL. - struct addrinfo *ai = adns_blocking_request(mesh, xstrdup(address), xstrdup(port), SOCK_STREAM, 5); + struct addrinfo *ai = adns_blocking_request(mesh, xstrdup(address), xstrdup(port), SOCK_STREAM, 30); if(ai) { for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) { @@ -2951,7 +3323,7 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) { state.blen = 0; if(!sendline(state.sock, "0 ?%s %d.%d %s", b64key, PROT_MAJOR, PROT_MINOR, mesh->appname)) { - logger(mesh, MESHLINK_DEBUG, "Error sending request to %s port %s: %s\n", address, port, strerror(errno)); + logger(mesh, MESHLINK_ERROR, "Error sending request to %s port %s: %s\n", address, port, strerror(errno)); meshlink_errno = MESHLINK_ENETWORK; goto exit; } @@ -2962,7 +3334,7 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) { int code, hismajor, hisminor = 0; if(!recvline(&state) || sscanf(state.line, "%d %s %d.%d", &code, hisname, &hismajor, &hisminor) < 3 || code != 0 || hismajor != PROT_MAJOR || !check_id(hisname) || !recvline(&state) || !rstrip(state.line) || sscanf(state.line, "%d ", &code) != 1 || code != ACK || strlen(state.line) < 3) { - logger(mesh, MESHLINK_DEBUG, "Cannot read greeting from peer\n"); + logger(mesh, MESHLINK_ERROR, "Cannot read greeting from peer\n"); meshlink_errno = MESHLINK_ENETWORK; goto exit; } @@ -2972,13 +3344,13 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) { char hishash[64]; if(sha512(fingerprint, strlen(fingerprint), hishash)) { - logger(mesh, MESHLINK_DEBUG, "Could not create hash\n%s\n", state.line + 2); + logger(mesh, MESHLINK_ERROR, "Could not create hash\n%s\n", state.line + 2); meshlink_errno = MESHLINK_EINTERNAL; goto exit; } if(memcmp(hishash, state.hash, 18)) { - logger(mesh, MESHLINK_DEBUG, "Peer has an invalid key!\n%s\n", state.line + 2); + logger(mesh, MESHLINK_ERROR, "Peer has an invalid key!\n%s\n", state.line + 2); meshlink_errno = MESHLINK_EPEER; goto exit; } @@ -3011,7 +3383,7 @@ 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)); + logger(mesh, MESHLINK_ERROR, "Error reading data from %s port %s: %s\n", address, port, strerror(errno)); meshlink_errno = MESHLINK_ENETWORK; goto exit; } @@ -3023,7 +3395,7 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) { } if(!state.success) { - logger(mesh, MESHLINK_DEBUG, "Connection closed by peer, invitation cancelled.\n"); + logger(mesh, MESHLINK_ERROR, "Connection closed by peer, invitation cancelled.\n"); meshlink_errno = MESHLINK_EPEER; goto exit; } @@ -3033,11 +3405,11 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) { ecdsa_free(key); closesocket(state.sock); - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return true; invalid: - logger(mesh, MESHLINK_DEBUG, "Invalid invitation URL\n"); + logger(mesh, MESHLINK_ERROR, "Invalid invitation URL\n"); meshlink_errno = MESHLINK_EINVAL; exit: sptps_stop(&state.sptps); @@ -3048,7 +3420,7 @@ exit: closesocket(state.sock); } - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return false; } @@ -3066,7 +3438,9 @@ char *meshlink_export(meshlink_handle_t *mesh) { packmsg_add_str(&out, mesh->name); packmsg_add_str(&out, CORE_MESH); - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } packmsg_add_int32(&out, mesh->self->devclass); packmsg_add_bool(&out, mesh->self->status.blacklisted); @@ -3100,10 +3474,10 @@ char *meshlink_export(meshlink_handle_t *mesh) { packmsg_add_int64(&out, 0); packmsg_add_int64(&out, 0); - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); if(!packmsg_output_ok(&out)) { - logger(mesh, MESHLINK_DEBUG, "Error creating export data\n"); + logger(mesh, MESHLINK_ERROR, "Error creating export data\n"); meshlink_errno = MESHLINK_EINTERNAL; return NULL; } @@ -3118,7 +3492,7 @@ char *meshlink_export(meshlink_handle_t *mesh) { packmsg_add_bin(&out2, buf, packmsg_output_size(&out, buf)); if(!packmsg_output_ok(&out2)) { - logger(mesh, MESHLINK_DEBUG, "Error creating export data\n"); + logger(mesh, MESHLINK_ERROR, "Error creating export data\n"); meshlink_errno = MESHLINK_EINTERNAL; free(buf2); return NULL; @@ -3130,6 +3504,8 @@ char *meshlink_export(meshlink_handle_t *mesh) { } bool meshlink_import(meshlink_handle_t *mesh, const char *data) { + logger(mesh, MESHLINK_DEBUG, "meshlink_import(%p)", (const void *)data); + if(!mesh || !data) { meshlink_errno = MESHLINK_EINVAL; return false; @@ -3140,7 +3516,8 @@ bool meshlink_import(meshlink_handle_t *mesh, const char *data) { int buflen = b64decode(data, buf, datalen); if(!buflen) { - logger(mesh, MESHLINK_DEBUG, "Invalid data\n"); + logger(mesh, MESHLINK_ERROR, "Invalid data\n"); + free(buf); meshlink_errno = MESHLINK_EPEER; return false; } @@ -3149,12 +3526,15 @@ bool meshlink_import(meshlink_handle_t *mesh, const char *data) { uint32_t count = packmsg_get_array(&in); if(!count) { - logger(mesh, MESHLINK_DEBUG, "Invalid data\n"); + logger(mesh, MESHLINK_ERROR, "Invalid data\n"); + free(buf); meshlink_errno = MESHLINK_EPEER; return false; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } while(count--) { const void *data2; @@ -3202,15 +3582,16 @@ bool meshlink_import(meshlink_handle_t *mesh, const char *data) { n->last_reachable = 0; n->last_unreachable = 0; - if(!node_write_config(mesh, n)) { + if(!node_write_config(mesh, n, true)) { free_node(n); + free(buf); return false; } node_add(mesh, n); } - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); free(buf); @@ -3246,11 +3627,15 @@ static bool blacklist(meshlink_handle_t *mesh, node_t *n) { */ for list_each(connection_t, c, mesh->connections) { if(c->node == n) { + if(c->status.active) { + send_error(mesh, c, BLACKLISTED, "blacklisted"); + } + shutdown(c->socket, SHUT_RDWR); } } - utcp_abort_all_connections(n->utcp); + utcp_reset_all_connections(n->utcp); n->mtu = 0; n->minmtu = 0; @@ -3269,35 +3654,46 @@ static bool blacklist(meshlink_handle_t *mesh, node_t *n) { mesh->node_status_cb(mesh, (meshlink_node_t *)n, false); } - return node_write_config(mesh, n) && config_sync(mesh, "current"); + /* Remove any outstanding invitations */ + invitation_purge_node(mesh, n->name); + + return node_write_config(mesh, n, true) && config_sync(mesh, "current"); } bool meshlink_blacklist(meshlink_handle_t *mesh, meshlink_node_t *node) { + logger(mesh, MESHLINK_DEBUG, "meshlink_blacklist(%s)", node ? node->name : "(null)"); + if(!mesh || !node) { meshlink_errno = MESHLINK_EINVAL; return false; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } if(!blacklist(mesh, (node_t *)node)) { - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return false; } - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); logger(mesh, MESHLINK_DEBUG, "Blacklisted %s.\n", node->name); return true; } bool meshlink_blacklist_by_name(meshlink_handle_t *mesh, const char *name) { + logger(mesh, MESHLINK_DEBUG, "meshlink_blacklist_by_name(%s)", name ? name : "(null)"); + if(!mesh || !name) { meshlink_errno = MESHLINK_EINVAL; return false; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } node_t *n = lookup_node(mesh, (char *)name); @@ -3308,11 +3704,11 @@ bool meshlink_blacklist_by_name(meshlink_handle_t *mesh, const char *name) { } if(!blacklist(mesh, (node_t *)n)) { - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return false; } - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); logger(mesh, MESHLINK_DEBUG, "Blacklisted %s.\n", name); return true; @@ -3337,35 +3733,43 @@ static bool whitelist(meshlink_handle_t *mesh, node_t *n) { update_node_status(mesh, n); } - return node_write_config(mesh, n) && config_sync(mesh, "current"); + return node_write_config(mesh, n, true) && config_sync(mesh, "current"); } bool meshlink_whitelist(meshlink_handle_t *mesh, meshlink_node_t *node) { + logger(mesh, MESHLINK_DEBUG, "meshlink_whitelist(%s)", node ? node->name : "(null)"); + if(!mesh || !node) { meshlink_errno = MESHLINK_EINVAL; return false; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } if(!whitelist(mesh, (node_t *)node)) { - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return false; } - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); logger(mesh, MESHLINK_DEBUG, "Whitelisted %s.\n", node->name); return true; } bool meshlink_whitelist_by_name(meshlink_handle_t *mesh, const char *name) { + logger(mesh, MESHLINK_DEBUG, "meshlink_whitelist_by_name(%s)", name ? name : "(null)"); + if(!mesh || !name) { meshlink_errno = MESHLINK_EINVAL; return false; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } node_t *n = lookup_node(mesh, (char *)name); @@ -3376,11 +3780,11 @@ bool meshlink_whitelist_by_name(meshlink_handle_t *mesh, const char *name) { } if(!whitelist(mesh, (node_t *)n)) { - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return false; } - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); logger(mesh, MESHLINK_DEBUG, "Whitelisted %s.\n", name); return true; @@ -3391,6 +3795,8 @@ void meshlink_set_default_blacklist(meshlink_handle_t *mesh, bool blacklist) { } bool meshlink_forget_node(meshlink_handle_t *mesh, meshlink_node_t *node) { + logger(mesh, MESHLINK_DEBUG, "meshlink_forget_node(%s)", node ? node->name : "(null)"); + if(!mesh || !node) { meshlink_errno = MESHLINK_EINVAL; return false; @@ -3398,18 +3804,20 @@ bool meshlink_forget_node(meshlink_handle_t *mesh, meshlink_node_t *node) { node_t *n = (node_t *)node; - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } /* Check that the node is not reachable */ if(n->status.reachable || n->connection) { - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); logger(mesh, MESHLINK_WARNING, "Could not forget %s: still reachable", n->name); return false; } /* Check that we don't have any active UTCP connections */ if(n->utcp && utcp_is_active(n->utcp)) { - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); logger(mesh, MESHLINK_WARNING, "Could not forget %s: active UTCP connections", n->name); return false; } @@ -3417,7 +3825,7 @@ bool meshlink_forget_node(meshlink_handle_t *mesh, meshlink_node_t *node) { /* Check that we have no active connections to this node */ for list_each(connection_t, c, mesh->connections) { if(c->node == n) { - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); logger(mesh, MESHLINK_WARNING, "Could not forget %s: active connection", n->name); return false; } @@ -3434,14 +3842,17 @@ bool meshlink_forget_node(meshlink_handle_t *mesh, meshlink_node_t *node) { /* Delete the config file for this node */ if(!config_delete(mesh, "current", n->name)) { - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return false; } + /* Delete any pending invitations */ + invitation_purge_node(mesh, n->name); + /* Delete the node struct and any remaining edges referencing this node */ node_del(mesh, n); - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return config_sync(mesh, "current"); } @@ -3450,22 +3861,26 @@ bool meshlink_forget_node(meshlink_handle_t *mesh, meshlink_node_t *node) { * See header file for detailed comment. */ void meshlink_hint_address(meshlink_handle_t *mesh, meshlink_node_t *node, const struct sockaddr *addr) { + logger(mesh, MESHLINK_DEBUG, "meshlink_hint_address(%s, %p)", node ? node->name : "(null)", (const void *)addr); + if(!mesh || !node || !addr) { meshlink_errno = EINVAL; return; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } node_t *n = (node_t *)node; if(node_add_recent_address(mesh, n, (sockaddr_t *)addr)) { - if(!node_write_config(mesh, n)) { + if(!node_write_config(mesh, n, false)) { logger(mesh, MESHLINK_DEBUG, "Could not update %s\n", n->name); } } - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); // @TODO do we want to fire off a connection attempt right away? } @@ -3473,7 +3888,12 @@ static bool channel_pre_accept(struct utcp *utcp, uint16_t port) { (void)port; node_t *n = utcp->priv; meshlink_handle_t *mesh = n->mesh; - return mesh->channel_accept_cb; + + if(mesh->channel_accept_cb && mesh->channel_listen_cb) { + return mesh->channel_listen_cb(mesh, (meshlink_node_t *)n, port); + } else { + return mesh->channel_accept_cb; + } } /* Finish one AIO buffer, return true if the channel is still open. */ @@ -3626,7 +4046,7 @@ static void channel_retransmit(struct utcp_connection *utcp_connection) { node_t *n = utcp_connection->utcp->priv; meshlink_handle_t *mesh = n->mesh; - if(n->mtuprobes == 31) { + if(n->mtuprobes == 31 && n->mtutimeout.cb) { timeout_set(&mesh->loop, &n->mtutimeout, &(struct timespec) { 0, 0 }); @@ -3645,6 +4065,8 @@ static ssize_t channel_send(struct utcp *utcp, const void *data, size_t len) { } void meshlink_set_channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_channel_receive_cb_t cb) { + logger(mesh, MESHLINK_DEBUG, "meshlink_set_channel_receive_cb(%p, %p)", (void *)channel, (void *)(intptr_t)cb); + if(!mesh || !channel) { meshlink_errno = MESHLINK_EINVAL; return; @@ -3653,7 +4075,7 @@ void meshlink_set_channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t channel->receive_cb = cb; } -static void channel_receive(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) { +void channel_receive(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) { (void)mesh; node_t *n = (node_t *)source; @@ -3726,9 +4148,6 @@ static void channel_poll(struct utcp_connection *connection, size_t len) { } if(sent != (ssize_t)todo) { - /* We should never get a partial send at this point */ - assert(sent <= 0); - /* Sending failed, abort all outstanding AIO buffers and send a poll callback. */ if(!aio_abort(mesh, channel, &channel->aio_send)) { return; @@ -3764,26 +4183,52 @@ static void channel_poll(struct utcp_connection *connection, size_t len) { } void meshlink_set_channel_poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_channel_poll_cb_t cb) { + logger(mesh, MESHLINK_DEBUG, "meshlink_set_channel_poll_cb(%p, %p)", (void *)channel, (void *)(intptr_t)cb); + if(!mesh || !channel) { meshlink_errno = MESHLINK_EINVAL; return; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + channel->poll_cb = cb; utcp_set_poll_cb(channel->c, (cb || channel->aio_send) ? channel_poll : NULL); - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); +} + +void meshlink_set_channel_listen_cb(meshlink_handle_t *mesh, meshlink_channel_listen_cb_t cb) { + logger(mesh, MESHLINK_DEBUG, "meshlink_set_channel_listen_cb(%p)", (void *)(intptr_t)cb); + + if(!mesh) { + meshlink_errno = MESHLINK_EINVAL; + return; + } + + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + + mesh->channel_listen_cb = cb; + + pthread_mutex_unlock(&mesh->mutex); } void meshlink_set_channel_accept_cb(meshlink_handle_t *mesh, meshlink_channel_accept_cb_t cb) { + logger(mesh, MESHLINK_DEBUG, "meshlink_set_channel_accept_cb(%p)", (void *)(intptr_t)cb); + if(!mesh) { meshlink_errno = MESHLINK_EINVAL; return; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + mesh->channel_accept_cb = cb; - mesh->receive_cb = channel_receive; for splay_each(node_t, n, mesh->nodes) { if(!n->utcp && n != mesh->self) { @@ -3793,36 +4238,72 @@ void meshlink_set_channel_accept_cb(meshlink_handle_t *mesh, meshlink_channel_ac } } - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); } void meshlink_set_channel_sndbuf(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t size) { - (void)mesh; + logger(mesh, MESHLINK_DEBUG, "meshlink_set_channel_sndbuf(%p, %zu)", (void *)channel, size); - if(!channel) { + meshlink_set_channel_sndbuf_storage(mesh, channel, NULL, size); +} + +void meshlink_set_channel_rcvbuf(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t size) { + logger(mesh, MESHLINK_DEBUG, "meshlink_set_channel_rcvbuf(%p, %zu)", (void *)channel, size); + + meshlink_set_channel_rcvbuf_storage(mesh, channel, NULL, size); +} + +void meshlink_set_channel_sndbuf_storage(meshlink_handle_t *mesh, meshlink_channel_t *channel, void *buf, size_t size) { + logger(mesh, MESHLINK_DEBUG, "meshlink_set_channel_sndbuf_storage(%p, %p, %zu)", (void *)channel, buf, size); + + if(!mesh || !channel) { meshlink_errno = MESHLINK_EINVAL; return; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); - utcp_set_sndbuf(channel->c, size); - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + + utcp_set_sndbuf(channel->c, buf, size); + pthread_mutex_unlock(&mesh->mutex); } -void meshlink_set_channel_rcvbuf(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t size) { - (void)mesh; +void meshlink_set_channel_rcvbuf_storage(meshlink_handle_t *mesh, meshlink_channel_t *channel, void *buf, size_t size) { + logger(mesh, MESHLINK_DEBUG, "meshlink_set_channel_rcvbuf_storage(%p, %p, %zu)", (void *)channel, buf, size); - if(!channel) { + if(!mesh || !channel) { meshlink_errno = MESHLINK_EINVAL; return; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); - utcp_set_rcvbuf(channel->c, size); - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + + utcp_set_rcvbuf(channel->c, buf, size); + pthread_mutex_unlock(&mesh->mutex); +} + +void meshlink_set_channel_flags(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint32_t flags) { + logger(mesh, MESHLINK_DEBUG, "meshlink_set_channel_flags(%p, %u)", (void *)channel, flags); + + if(!mesh || !channel) { + meshlink_errno = MESHLINK_EINVAL; + return; + } + + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + + utcp_set_flags(channel->c, flags); + pthread_mutex_unlock(&mesh->mutex); } meshlink_channel_t *meshlink_channel_open_ex(meshlink_handle_t *mesh, meshlink_node_t *node, uint16_t port, meshlink_channel_receive_cb_t cb, const void *data, size_t len, uint32_t flags) { + logger(mesh, MESHLINK_DEBUG, "meshlink_channel_open_ex(%s, %u, %p, %p, %zu, %u)", node ? node->name : "(null)", port, (void *)(intptr_t)cb, data, len, flags); + if(data && len) { abort(); // TODO: handle non-NULL data } @@ -3832,7 +4313,9 @@ meshlink_channel_t *meshlink_channel_open_ex(meshlink_handle_t *mesh, meshlink_n return NULL; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } node_t *n = (node_t *)node; @@ -3840,11 +4323,10 @@ meshlink_channel_t *meshlink_channel_open_ex(meshlink_handle_t *mesh, meshlink_n n->utcp = utcp_init(channel_accept, channel_pre_accept, channel_send, n); utcp_set_mtu(n->utcp, n->mtu - sizeof(meshlink_packethdr_t)); utcp_set_retransmit_cb(n->utcp, channel_retransmit); - mesh->receive_cb = channel_receive; if(!n->utcp) { meshlink_errno = errno == ENOMEM ? MESHLINK_ENOMEM : MESHLINK_EINTERNAL; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return NULL; } } @@ -3852,7 +4334,7 @@ meshlink_channel_t *meshlink_channel_open_ex(meshlink_handle_t *mesh, meshlink_n if(n->status.blacklisted) { logger(mesh, MESHLINK_ERROR, "Cannot open a channel with blacklisted node\n"); meshlink_errno = MESHLINK_EBLACKLISTED; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return NULL; } @@ -3866,7 +4348,7 @@ meshlink_channel_t *meshlink_channel_open_ex(meshlink_handle_t *mesh, meshlink_n channel->c = utcp_connect_ex(n->utcp, port, channel_recv, channel, flags); - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); if(!channel->c) { meshlink_errno = errno == ENOMEM ? MESHLINK_ENOMEM : MESHLINK_EINTERNAL; @@ -3878,27 +4360,38 @@ meshlink_channel_t *meshlink_channel_open_ex(meshlink_handle_t *mesh, meshlink_n } 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) { + logger(mesh, MESHLINK_DEBUG, "meshlink_channel_open_ex(%s, %u, %p, %p, %zu)", node ? node->name : "(null)", port, (void *)(intptr_t)cb, data, len); + return meshlink_channel_open_ex(mesh, node, port, cb, data, len, MESHLINK_CHANNEL_TCP); } void meshlink_channel_shutdown(meshlink_handle_t *mesh, meshlink_channel_t *channel, int direction) { + logger(mesh, MESHLINK_DEBUG, "meshlink_channel_shutdown(%p, %d)", (void *)channel, direction); + if(!mesh || !channel) { meshlink_errno = MESHLINK_EINVAL; return; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + utcp_shutdown(channel->c, direction); - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); } void meshlink_channel_close(meshlink_handle_t *mesh, meshlink_channel_t *channel) { + logger(mesh, MESHLINK_DEBUG, "meshlink_channel_close(%p)", (void *)channel); + if(!mesh || !channel) { meshlink_errno = MESHLINK_EINVAL; return; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } if(channel->c) { utcp_close(channel->c); @@ -3913,10 +4406,40 @@ void meshlink_channel_close(meshlink_handle_t *mesh, meshlink_channel_t *channel free(channel); } - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); +} + +void meshlink_channel_abort(meshlink_handle_t *mesh, meshlink_channel_t *channel) { + logger(mesh, MESHLINK_DEBUG, "meshlink_channel_abort(%p)", (void *)channel); + + if(!mesh || !channel) { + meshlink_errno = MESHLINK_EINVAL; + return; + } + + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + + if(channel->c) { + utcp_abort(channel->c); + channel->c = NULL; + + /* Clean up any outstanding AIO buffers. */ + aio_abort(mesh, channel, &channel->aio_send); + aio_abort(mesh, channel, &channel->aio_receive); + } + + if(!channel->in_callback) { + free(channel); + } + + pthread_mutex_unlock(&mesh->mutex); } ssize_t meshlink_channel_send(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) { + logger(mesh, MESHLINK_DEBUG, "meshlink_channel_send(%p, %p, %zu)", (void *)channel, data, len); + if(!mesh || !channel) { meshlink_errno = MESHLINK_EINVAL; return -1; @@ -3938,7 +4461,9 @@ ssize_t meshlink_channel_send(meshlink_handle_t *mesh, meshlink_channel_t *chann ssize_t retval; - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } /* Disallow direct calls to utcp_send() while we still have AIO active. */ if(channel->aio_send) { @@ -3947,7 +4472,7 @@ ssize_t meshlink_channel_send(meshlink_handle_t *mesh, meshlink_channel_t *chann retval = utcp_send(channel->c, data, len); } - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); if(retval < 0) { meshlink_errno = MESHLINK_ENETWORK; @@ -3957,6 +4482,8 @@ ssize_t meshlink_channel_send(meshlink_handle_t *mesh, meshlink_channel_t *chann } bool meshlink_channel_aio_send(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len, meshlink_aio_cb_t cb, void *priv) { + logger(mesh, MESHLINK_DEBUG, "meshlink_channel_aio_send(%p, %p, %zu, %p, %p)", (void *)channel, data, len, (void *)(intptr_t)cb, priv); + if(!mesh || !channel) { meshlink_errno = MESHLINK_EINVAL; return false; @@ -3973,7 +4500,9 @@ bool meshlink_channel_aio_send(meshlink_handle_t *mesh, meshlink_channel_t *chan aio->cb.buffer = cb; aio->priv = priv; - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } /* Append the AIO buffer descriptor to the end of the chain */ meshlink_aio_buffer_t **p = &channel->aio_send; @@ -3986,18 +4515,20 @@ bool meshlink_channel_aio_send(meshlink_handle_t *mesh, meshlink_channel_t *chan /* Ensure the poll callback is set, and call it right now to push data if possible */ utcp_set_poll_cb(channel->c, channel_poll); - size_t todo = MIN(len, utcp_get_rcvbuf_free(channel->c)); + size_t todo = MIN(len, utcp_get_sndbuf_free(channel->c)); if(todo) { channel_poll(channel->c, todo); } - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return true; } bool meshlink_channel_aio_fd_send(meshlink_handle_t *mesh, meshlink_channel_t *channel, int fd, size_t len, meshlink_aio_fd_cb_t cb, void *priv) { + logger(mesh, MESHLINK_DEBUG, "meshlink_channel_aio_fd_send(%p, %d, %zu, %p, %p)", (void *)channel, fd, len, (void *)(intptr_t)cb, priv); + if(!mesh || !channel) { meshlink_errno = MESHLINK_EINVAL; return false; @@ -4014,7 +4545,9 @@ bool meshlink_channel_aio_fd_send(meshlink_handle_t *mesh, meshlink_channel_t *c aio->cb.fd = cb; aio->priv = priv; - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } /* Append the AIO buffer descriptor to the end of the chain */ meshlink_aio_buffer_t **p = &channel->aio_send; @@ -4027,18 +4560,20 @@ bool meshlink_channel_aio_fd_send(meshlink_handle_t *mesh, meshlink_channel_t *c /* Ensure the poll callback is set, and call it right now to push data if possible */ utcp_set_poll_cb(channel->c, channel_poll); - size_t left = utcp_get_rcvbuf_free(channel->c); + size_t left = utcp_get_sndbuf_free(channel->c); if(left) { channel_poll(channel->c, left); } - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return true; } bool meshlink_channel_aio_receive(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len, meshlink_aio_cb_t cb, void *priv) { + logger(mesh, MESHLINK_DEBUG, "meshlink_channel_aio_receive(%p, %p, %zu, %p, %p)", (void *)channel, data, len, (void *)(intptr_t)cb, priv); + if(!mesh || !channel) { meshlink_errno = MESHLINK_EINVAL; return false; @@ -4055,7 +4590,9 @@ bool meshlink_channel_aio_receive(meshlink_handle_t *mesh, meshlink_channel_t *c aio->cb.buffer = cb; aio->priv = priv; - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } /* Append the AIO buffer descriptor to the end of the chain */ meshlink_aio_buffer_t **p = &channel->aio_receive; @@ -4066,12 +4603,14 @@ bool meshlink_channel_aio_receive(meshlink_handle_t *mesh, meshlink_channel_t *c *p = aio; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return true; } bool meshlink_channel_aio_fd_receive(meshlink_handle_t *mesh, meshlink_channel_t *channel, int fd, size_t len, meshlink_aio_fd_cb_t cb, void *priv) { + logger(mesh, MESHLINK_DEBUG, "meshlink_channel_aio_fd_receive(%p, %d, %zu, %p, %p)", (void *)channel, fd, len, (void *)(intptr_t)cb, priv); + if(!mesh || !channel) { meshlink_errno = MESHLINK_EINVAL; return false; @@ -4088,7 +4627,9 @@ bool meshlink_channel_aio_fd_receive(meshlink_handle_t *mesh, meshlink_channel_t aio->cb.fd = cb; aio->priv = priv; - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } /* Append the AIO buffer descriptor to the end of the chain */ meshlink_aio_buffer_t **p = &channel->aio_receive; @@ -4099,7 +4640,7 @@ bool meshlink_channel_aio_fd_receive(meshlink_handle_t *mesh, meshlink_channel_t *p = aio; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); return true; } @@ -4141,6 +4682,8 @@ size_t meshlink_channel_get_mss(meshlink_handle_t *mesh, meshlink_channel_t *cha } void meshlink_set_node_channel_timeout(meshlink_handle_t *mesh, meshlink_node_t *node, int timeout) { + logger(mesh, MESHLINK_DEBUG, "meshlink_set_node_channel_timeout(%s, %d)", node ? node->name : "(null)", timeout); + if(!mesh || !node) { meshlink_errno = MESHLINK_EINVAL; return; @@ -4148,7 +4691,9 @@ void meshlink_set_node_channel_timeout(meshlink_handle_t *mesh, meshlink_node_t node_t *n = (node_t *)node; - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } if(!n->utcp) { n->utcp = utcp_init(channel_accept, channel_pre_accept, channel_send, n); @@ -4158,7 +4703,7 @@ void meshlink_set_node_channel_timeout(meshlink_handle_t *mesh, meshlink_node_t utcp_set_user_timeout(n->utcp, timeout); - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); } void update_node_status(meshlink_handle_t *mesh, node_t *n) { @@ -4195,16 +4740,18 @@ void handle_duplicate_node(meshlink_handle_t *mesh, node_t *n) { } void meshlink_enable_discovery(meshlink_handle_t *mesh, bool enable) { -#if HAVE_CATTA + logger(mesh, MESHLINK_DEBUG, "meshlink_enable_discovery(%d)", enable); if(!mesh) { meshlink_errno = MESHLINK_EINVAL; return; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } - if(mesh->discovery == enable) { + if(mesh->discovery.enabled == enable) { goto end; } @@ -4216,18 +4763,39 @@ void meshlink_enable_discovery(meshlink_handle_t *mesh, bool enable) { } } - mesh->discovery = enable; + mesh->discovery.enabled = enable; end: - assert(pthread_mutex_unlock(&mesh->mutex) == 0); -#else - (void)mesh; - (void)enable; - meshlink_errno = MESHLINK_ENOTSUP; -#endif + pthread_mutex_unlock(&mesh->mutex); +} + +void meshlink_hint_network_change(struct meshlink_handle *mesh) { + logger(mesh, MESHLINK_DEBUG, "meshlink_hint_network_change()"); + + if(!mesh) { + meshlink_errno = MESHLINK_EINVAL; + return; + } + + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + + if(mesh->discovery.enabled) { + scan_ifaddrs(mesh); + } + + if(mesh->loop.now.tv_sec > mesh->discovery.last_update + 5) { + mesh->discovery.last_update = mesh->loop.now.tv_sec; + handle_network_change(mesh, 1); + } + + pthread_mutex_unlock(&mesh->mutex); } void meshlink_set_dev_class_timeouts(meshlink_handle_t *mesh, dev_class_t devclass, int pinginterval, int pingtimeout) { + logger(mesh, MESHLINK_DEBUG, "meshlink_set_dev_class_timeouts(%d, %d, %d)", devclass, pinginterval, pingtimeout); + if(!mesh || devclass < 0 || devclass >= DEV_CLASS_COUNT) { meshlink_errno = EINVAL; return; @@ -4238,13 +4806,18 @@ void meshlink_set_dev_class_timeouts(meshlink_handle_t *mesh, dev_class_t devcla return; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + mesh->dev_class_traits[devclass].pinginterval = pinginterval; mesh->dev_class_traits[devclass].pingtimeout = pingtimeout; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); } void meshlink_set_dev_class_fast_retry_period(meshlink_handle_t *mesh, dev_class_t devclass, int fast_retry_period) { + logger(mesh, MESHLINK_DEBUG, "meshlink_set_dev_class_fast_retry_period(%d, %d)", devclass, fast_retry_period); + if(!mesh || devclass < 0 || devclass >= DEV_CLASS_COUNT) { meshlink_errno = EINVAL; return; @@ -4255,23 +4828,74 @@ void meshlink_set_dev_class_fast_retry_period(meshlink_handle_t *mesh, dev_class return; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + mesh->dev_class_traits[devclass].fast_retry_period = fast_retry_period; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); +} + +void meshlink_set_dev_class_maxtimeout(struct meshlink_handle *mesh, dev_class_t devclass, int maxtimeout) { + logger(mesh, MESHLINK_DEBUG, "meshlink_set_dev_class_fast_maxtimeout(%d, %d)", devclass, maxtimeout); + + if(!mesh || devclass < 0 || devclass >= DEV_CLASS_COUNT) { + meshlink_errno = EINVAL; + return; + } + + if(maxtimeout < 0) { + meshlink_errno = EINVAL; + return; + } + + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + + mesh->dev_class_traits[devclass].maxtimeout = maxtimeout; + pthread_mutex_unlock(&mesh->mutex); +} + +void meshlink_reset_timers(struct meshlink_handle *mesh) { + logger(mesh, MESHLINK_DEBUG, "meshlink_reset_timers()"); + + if(!mesh) { + return; + } + + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + + handle_network_change(mesh, true); + + if(mesh->discovery.enabled) { + discovery_refresh(mesh); + } + + pthread_mutex_unlock(&mesh->mutex); } -extern void meshlink_set_inviter_commits_first(struct meshlink_handle *mesh, bool inviter_commits_first) { +void meshlink_set_inviter_commits_first(struct meshlink_handle *mesh, bool inviter_commits_first) { + logger(mesh, MESHLINK_DEBUG, "meshlink_set_inviter_commits_first(%d)", inviter_commits_first); + if(!mesh) { meshlink_errno = EINVAL; return; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + mesh->inviter_commits_first = inviter_commits_first; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); } void meshlink_set_external_address_discovery_url(struct meshlink_handle *mesh, const char *url) { + logger(mesh, MESHLINK_DEBUG, "meshlink_set_external_address_discovery_url(%s)", url ? url : "(null)"); + if(!mesh) { meshlink_errno = EINVAL; return; @@ -4282,13 +4906,18 @@ void meshlink_set_external_address_discovery_url(struct meshlink_handle *mesh, c return; } - assert(pthread_mutex_lock(&mesh->mutex) == 0); + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + free(mesh->external_address_url); mesh->external_address_url = url ? xstrdup(url) : NULL; - assert(pthread_mutex_unlock(&mesh->mutex) == 0); + pthread_mutex_unlock(&mesh->mutex); } void meshlink_set_scheduling_granularity(struct meshlink_handle *mesh, long granularity) { + logger(mesh, MESHLINK_DEBUG, "meshlink_set_scheduling_granularity(%ld)", granularity); + if(!mesh || granularity < 0) { meshlink_errno = EINVAL; return; @@ -4297,6 +4926,22 @@ void meshlink_set_scheduling_granularity(struct meshlink_handle *mesh, long gran utcp_set_clock_granularity(granularity); } +void meshlink_set_storage_policy(struct meshlink_handle *mesh, meshlink_storage_policy_t policy) { + logger(mesh, MESHLINK_DEBUG, "meshlink_set_storage_policy(%d)", policy); + + if(!mesh) { + meshlink_errno = EINVAL; + return; + } + + if(pthread_mutex_lock(&mesh->mutex) != 0) { + abort(); + } + + mesh->storage_policy = policy; + pthread_mutex_unlock(&mesh->mutex); +} + void handle_network_change(meshlink_handle_t *mesh, bool online) { (void)online;