+ free(params->confbase);
+ free(params->name);
+ free(params->appname);
+
+ 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
+};
+
+meshlink_handle_t *meshlink_open(const char *confbase, const char *name, const char *appname, dev_class_t 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));
+
+ params.confbase = (char *)confbase;
+ params.name = (char *)name;
+ params.appname = (char *)appname;
+ params.devclass = devclass;
+ params.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) {
+ 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));
+
+ params.confbase = (char *)confbase;
+ params.name = (char *)name;
+ params.appname = (char *)appname;
+ params.devclass = devclass;
+ params.netns = -1;
+
+ if(!meshlink_open_params_set_storage_key(¶ms, key, keylen)) {
+ return false;
+ }
+
+ return meshlink_open_ex(¶ms);
+}
+
+meshlink_handle_t *meshlink_open_ephemeral(const char *name, const char *appname, dev_class_t devclass) {
+ /* 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;
+
+ return meshlink_open_ex(¶ms);
+}
+
+meshlink_handle_t *meshlink_open_ex(const meshlink_open_params_t *params) {
+ // Validate arguments provided by the application
+ bool usingname = false;
+
+ logger(NULL, MESHLINK_DEBUG, "meshlink_open called\n");
+
+ 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