/*
meshlink.c -- Implementation of the MeshLink API.
- Copyright (C) 2014-2018 Guus Sliepen <guus@meshlink.io>
+ Copyright (C) 2014-2021 Guus Sliepen <guus@meshlink.io>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
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;
}
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) {
int s = socket_in_netns(aip->ai_family, aip->ai_socktype, aip->ai_protocol, mesh->netns);
+#ifdef SO_NOSIGPIPE
+ int nosigpipe = 1;
+ setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, &nosigpipe, sizeof(nosigpipe));
+#endif
+
if(s >= 0) {
set_timeout(s, 5000);
}
}
+#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];
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;
}
}
/* Write our own host config file */
- if(!node_write_config(mesh, mesh->self)) {
+ if(!node_write_config(mesh, mesh->self, true)) {
return false;
}
}
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;
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;
}
}
/* 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;
}
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;
}
#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;
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;
}
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;
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;
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;
}
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;
}
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;
}
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;
}
// 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;
}
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;
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;
}
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;
}
/* 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) {
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;
// 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);
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");
meshlink_close(mesh);
return NULL;
}
+
+ new_configuration = true;
} else {
if(!meshlink_read_config(mesh)) {
logger(NULL, MESHLINK_ERROR, "Cannot read main configuration\n");
}
}
+ mesh->storage_policy = params->storage_policy;
+
#ifdef HAVE_MINGW
struct WSAData wsa_state;
WSAStartup(MAKEWORD(2, 2), &wsa_state);
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;
}
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) {
}
//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;
}
#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
-
- assert(pthread_mutex_lock(&mesh->mutex) == 0);
+ if(pthread_mutex_lock(&mesh->mutex) != 0) {
+ abort();
+ }
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 HAVE_CATTA
+ pthread_mutex_unlock(&mesh->mutex);
// Stop discovery
- if(mesh->discovery) {
+ if(mesh->discovery.enabled) {
discovery_stop(mesh);
}
-#endif
-
return NULL;
}
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);
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;
}
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;
}
// 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);
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;
}
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;
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.
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;
}
#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;
#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;
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;
}
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;
}
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);
}
static bool prepare_packet(meshlink_handle_t *mesh, meshlink_node_t *destination, const void *data, size_t len, vpn_packet_t *packet) {
}
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;
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);
}
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;
}
}
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;
}
meshlink_errno = MESHLINK_EINTERNAL;
}
- assert(pthread_mutex_unlock(&mesh->mutex) == 0);
+ pthread_mutex_unlock(&mesh->mutex);
return fingerprint;
}
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;
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;
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));
meshlink_errno = MESHLINK_ENOMEM;
}
- assert(pthread_mutex_unlock(&mesh->mutex) == 0);
+ pthread_mutex_unlock(&mesh->mutex);
return result;
}
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;
if(*nmemb == 0) {
free(nodes);
- assert(pthread_mutex_unlock(&mesh->mutex) == 0);
+ pthread_mutex_unlock(&mesh->mutex);
return NULL;
}
meshlink_errno = MESHLINK_ENOMEM;
}
- assert(pthread_mutex_unlock(&mesh->mutex) == 0);
+ pthread_mutex_unlock(&mesh->mutex);
return result;
}
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;
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;
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;
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) {
*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;
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;
return false;
}
- assert(pthread_mutex_lock(&mesh->mutex) == 0);
+ if(pthread_mutex_lock(&mesh->mutex) != 0) {
+ abort();
+ }
bool rval = false;
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);
// 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;
}
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;
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;
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;
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) {
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;
}
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;
}
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;
}
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;
}
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;
}
}
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;
}
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,
//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) {
}
// 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) {
continue;
}
+#ifdef SO_NOSIGPIPE
+ int nosigpipe = 1;
+ setsockopt(state.sock, SOL_SOCKET, SO_NOSIGPIPE, &nosigpipe, sizeof(nosigpipe));
+#endif
+
set_timeout(state.sock, 5000);
if(connect(state.sock, aip->ai_addr, aip->ai_addrlen)) {
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;
}
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;
}
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;
}
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;
}
}
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;
}
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);
closesocket(state.sock);
}
- assert(pthread_mutex_unlock(&mesh->mutex) == 0);
+ pthread_mutex_unlock(&mesh->mutex);
return false;
}
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);
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;
}
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;
}
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;
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;
}
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;
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);
*/
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;
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);
}
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;
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);
}
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;
}
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;
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;
}
/* 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;
}
/* 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");
}
* 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?
}
(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. */
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
});
}
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;
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;
}
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;
}
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) {
}
}
- 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
}
return NULL;
}
- assert(pthread_mutex_lock(&mesh->mutex) == 0);
+ if(pthread_mutex_lock(&mesh->mutex) != 0) {
+ abort();
+ }
node_t *n = (node_t *)node;
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;
}
}
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;
}
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;
}
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);
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;
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) {
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;
}
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;
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;
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;
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;
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;
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;
*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;
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;
*p = aio;
- assert(pthread_mutex_unlock(&mesh->mutex) == 0);
+ pthread_mutex_unlock(&mesh->mutex);
return true;
}
}
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;
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);
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) {
}
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;
}
}
}
- 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;
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;
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;
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;
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;
}
retry(mesh);
+ signal_trigger(&mesh->loop, &mesh->datafromapp);
}
void call_error_cb(meshlink_handle_t *mesh, meshlink_errno_t cb_errno) {