+ if(!params->appname || !*params->appname) {
+ logger(NULL, MESHLINK_ERROR, "No appname given!\n");
+ meshlink_errno = MESHLINK_EINVAL;
+ return NULL;
+ }
+
+ if(strchr(params->appname, ' ')) {
+ logger(NULL, MESHLINK_ERROR, "Invalid appname given!\n");
+ meshlink_errno = MESHLINK_EINVAL;
+ return NULL;
+ }
+
+ if(!params->name || !*params->name) {
+ logger(NULL, MESHLINK_ERROR, "No name given!\n");
+ //return NULL;
+ } else { //check name only if there is a name != NULL
+
+ if(!check_id(params->name)) {
+ logger(NULL, MESHLINK_ERROR, "Invalid name given!\n");
+ meshlink_errno = MESHLINK_EINVAL;
+ return NULL;
+ } else {
+ usingname = true;
+ }
+ }
+
+ if(params->devclass < 0 || params->devclass >= DEV_CLASS_COUNT) {
+ logger(NULL, MESHLINK_ERROR, "Invalid devclass given!\n");
+ meshlink_errno = MESHLINK_EINVAL;
+ return NULL;
+ }
+
+ if((params->key && !params->keylen) || (!params->key && params->keylen)) {
+ logger(NULL, MESHLINK_ERROR, "Invalid key length!\n");
+ meshlink_errno = MESHLINK_EINVAL;
+ return NULL;
+ }
+
+ meshlink_handle_t *mesh = xzalloc(sizeof(meshlink_handle_t));
+
+ if(params->confbase) {
+ mesh->confbase = xstrdup(params->confbase);
+ }
+
+ mesh->appname = xstrdup(params->appname);
+ mesh->devclass = params->devclass;
+ mesh->discovery = true;
+ mesh->invitation_timeout = 604800; // 1 week
+ mesh->netns = params->netns;
+ mesh->submeshes = NULL;
+ mesh->log_cb = global_log_cb;
+ mesh->log_level = global_log_level;
+
+ memcpy(mesh->dev_class_traits, default_class_traits, sizeof(default_class_traits));
+
+ if(usingname) {
+ mesh->name = xstrdup(params->name);
+ }
+
+ // Hash the key
+ if(params->key) {
+ mesh->config_key = xmalloc(CHACHA_POLY1305_KEYLEN);
+
+ if(!prf(params->key, params->keylen, "MeshLink configuration key", 26, mesh->config_key, CHACHA_POLY1305_KEYLEN)) {
+ logger(NULL, MESHLINK_ERROR, "Error creating configuration key!\n");
+ meshlink_close(mesh);
+ meshlink_errno = MESHLINK_EINTERNAL;
+ return NULL;
+ }
+ }
+
+ // initialize mutex
+ pthread_mutexattr_t attr;
+ pthread_mutexattr_init(&attr);
+ pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
+ pthread_mutex_init(&(mesh->mesh_mutex), &attr);
+
+ mesh->threadstarted = false;
+ event_loop_init(&mesh->loop);
+ mesh->loop.data = mesh;
+
+ meshlink_queue_init(&mesh->outpacketqueue);
+
+ // If no configuration exists yet, create it.
+
+ if(!meshlink_confbase_exists(mesh)) {
+ if(!meshlink_setup(mesh)) {
+ logger(NULL, MESHLINK_ERROR, "Cannot create initial configuration\n");
+ meshlink_close(mesh);
+ return NULL;
+ }
+ } else {
+ if(!meshlink_read_config(mesh)) {
+ logger(NULL, MESHLINK_ERROR, "Cannot read main configuration\n");
+ meshlink_close(mesh);
+ return NULL;
+ }
+ }
+
+#ifdef HAVE_MINGW
+ struct WSAData wsa_state;
+ WSAStartup(MAKEWORD(2, 2), &wsa_state);
+#endif
+
+ // Setup up everything
+ // TODO: we should not open listening sockets yet
+
+ bool success = false;
+
+ if(mesh->netns != -1) {
+#ifdef HAVE_SETNS
+ pthread_t thr;
+
+ if(pthread_create(&thr, NULL, setup_network_in_netns_thread, mesh) == 0) {
+ void *retval = NULL;
+ success = pthread_join(thr, &retval) == 0 && retval;
+ }
+
+#else
+ meshlink_errno = MESHLINK_EINTERNAL;
+ return NULL;
+
+#endif // HAVE_SETNS
+ } else {
+ success = setup_network(mesh);
+ add_local_addresses(mesh);
+ }
+
+ if(!success) {
+ meshlink_close(mesh);
+ meshlink_errno = MESHLINK_ENETWORK;
+ return NULL;
+ }
+
+ add_local_addresses(mesh);
+ node_write_config(mesh, mesh->self);
+
+ idle_set(&mesh->loop, idle, mesh);
+
+ logger(NULL, MESHLINK_DEBUG, "meshlink_open returning\n");
+ return mesh;
+}
+
+meshlink_submesh_t *meshlink_submesh_open(meshlink_handle_t *mesh, const char *submesh) {
+ meshlink_submesh_t *s = NULL;
+
+ if(!mesh) {
+ logger(NULL, MESHLINK_ERROR, "No mesh handle given!\n");
+ meshlink_errno = MESHLINK_EINVAL;
+ return NULL;
+ }
+
+ if(!submesh || !*submesh) {
+ logger(NULL, MESHLINK_ERROR, "No submesh name given!\n");
+ meshlink_errno = MESHLINK_EINVAL;
+ return NULL;
+ }
+
+ //lock mesh->nodes
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
+ s = (meshlink_submesh_t *)create_submesh(mesh, submesh);
+
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+
+ return s;
+}
+
+static void *meshlink_main_loop(void *arg) {
+ meshlink_handle_t *mesh = arg;
+
+ if(mesh->netns != -1) {
+#ifdef HAVE_SETNS
+
+ if(setns(mesh->netns, CLONE_NEWNET) != 0) {
+ pthread_cond_signal(&mesh->cond);
+ return NULL;
+ }
+
+#else
+ pthread_cond_signal(&mesh->cond);
+ return NULL;
+#endif // HAVE_SETNS
+ }
+
+#if HAVE_CATTA
+
+ if(mesh->discovery) {
+ discovery_start(mesh);
+ }
+
+#endif
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
+ logger(mesh, MESHLINK_DEBUG, "Starting main_loop...\n");
+ pthread_cond_broadcast(&mesh->cond);
+ main_loop(mesh);
+ logger(mesh, MESHLINK_DEBUG, "main_loop returned.\n");
+
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+
+#if HAVE_CATTA
+
+ // Stop discovery
+ if(mesh->discovery) {
+ discovery_stop(mesh);
+ }
+
+#endif
+
+ return NULL;
+}
+
+bool meshlink_start(meshlink_handle_t *mesh) {
+ assert(mesh->self);
+ assert(mesh->private_key);
+
+ if(!mesh) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return false;
+ }
+
+ logger(mesh, MESHLINK_DEBUG, "meshlink_start called\n");
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
+ assert(mesh->self->ecdsa);
+ assert(!memcmp((uint8_t *)mesh->self->ecdsa + 64, (uint8_t *)mesh->private_key + 64, 32));
+
+ if(mesh->threadstarted) {
+ logger(mesh, MESHLINK_DEBUG, "thread was already running\n");
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return true;
+ }
+
+ if(mesh->listen_socket[0].tcp.fd < 0) {
+ logger(mesh, MESHLINK_ERROR, "Listening socket not open\n");
+ meshlink_errno = MESHLINK_ENETWORK;
+ return false;
+ }
+
+ mesh->thedatalen = 0;
+
+ // TODO: open listening sockets first
+
+ //Check that a valid name is set
+ if(!mesh->name) {
+ logger(mesh, MESHLINK_DEBUG, "No name given!\n");
+ meshlink_errno = MESHLINK_EINVAL;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return false;
+ }
+
+ init_outgoings(mesh);
+
+ // Start the main thread
+
+ event_loop_start(&mesh->loop);
+
+ if(pthread_create(&mesh->thread, NULL, meshlink_main_loop, mesh) != 0) {
+ logger(mesh, MESHLINK_DEBUG, "Could not start thread: %s\n", strerror(errno));
+ memset(&mesh->thread, 0, sizeof(mesh)->thread);
+ meshlink_errno = MESHLINK_EINTERNAL;
+ event_loop_stop(&mesh->loop);
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return false;
+ }
+
+ pthread_cond_wait(&mesh->cond, &mesh->mesh_mutex);
+ mesh->threadstarted = true;
+
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return true;
+}
+
+void meshlink_stop(meshlink_handle_t *mesh) {
+ if(!mesh) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return;
+ }
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+ logger(mesh, MESHLINK_DEBUG, "meshlink_stop called\n");
+
+ // Shut down the main thread
+ event_loop_stop(&mesh->loop);
+
+ // Send ourselves a UDP packet to kick the event loop
+ for(int i = 0; i < mesh->listen_sockets; i++) {
+ sockaddr_t sa;
+ socklen_t salen = sizeof(sa.sa);
+
+ if(getsockname(mesh->listen_socket[i].udp.fd, &sa.sa, &salen) == -1) {
+ logger(mesh, MESHLINK_ERROR, "System call `%s' failed: %s", "getsockname", sockstrerror(sockerrno));
+ continue;
+ }
+
+ if(sendto(mesh->listen_socket[i].udp.fd, "", 1, MSG_NOSIGNAL, &sa.sa, salen) == -1) {
+ logger(mesh, MESHLINK_ERROR, "Could not send a UDP packet to ourself: %s", sockstrerror(sockerrno));
+ }
+ }
+
+ if(mesh->threadstarted) {
+ // Wait for the main thread to finish
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ pthread_join(mesh->thread, NULL);
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
+ mesh->threadstarted = false;
+ }
+
+ // Close all metaconnections
+ if(mesh->connections) {
+ for(list_node_t *node = mesh->connections->head, *next; node; node = next) {
+ next = node->next;
+ connection_t *c = node->data;
+ c->outgoing = NULL;
+ terminate_connection(mesh, c, false);
+ }
+ }
+
+ exit_outgoings(mesh);
+
+ // Write out any changed node config files
+ if(mesh->nodes) {
+ for splay_each(node_t, n, mesh->nodes) {
+ if(n->status.dirty) {
+ node_write_config(mesh, n);
+ n->status.dirty = false;
+ }
+ }
+ }
+
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+}
+
+void meshlink_close(meshlink_handle_t *mesh) {
+ if(!mesh) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return;
+ }
+
+ // stop can be called even if mesh has not been started
+ meshlink_stop(mesh);
+
+ // lock is not released after this
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
+ // Close and free all resources used.
+
+ close_network_connections(mesh);
+
+ logger(mesh, MESHLINK_INFO, "Terminating");
+
+ event_loop_exit(&mesh->loop);
+
+#ifdef HAVE_MINGW
+
+ if(mesh->confbase) {
+ WSACleanup();
+ }
+
+#endif
+
+ ecdsa_free(mesh->invitation_key);
+
+ if(mesh->netns != -1) {
+ close(mesh->netns);
+ }
+
+ free(mesh->name);
+ free(mesh->appname);
+ free(mesh->confbase);
+ free(mesh->config_key);
+ ecdsa_free(mesh->private_key);
+ pthread_mutex_destroy(&(mesh->mesh_mutex));
+
+ main_config_unlock(mesh);
+
+ memset(mesh, 0, sizeof(*mesh));
+
+ free(mesh);
+}
+
+bool meshlink_destroy(const char *confbase) {
+ if(!confbase) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return false;
+ }
+
+ if(!config_destroy(confbase, "current")) {
+ logger(NULL, MESHLINK_ERROR, "Cannot remove confbase sub-directories %s: %s\n", confbase, strerror(errno));
+ return false;
+ }
+
+ config_destroy(confbase, "new");
+ config_destroy(confbase, "old");
+
+ if(rmdir(confbase) && errno != ENOENT) {
+ logger(NULL, MESHLINK_ERROR, "Cannot remove directory %s: %s\n", confbase, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ return false;
+ }
+
+ return true;
+}