+ if(!try_bind(port)) {
+ meshlink_errno = MESHLINK_ENETWORK;
+ return false;
+ }
+
+ bool rval = false;
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
+ if(mesh->threadstarted) {
+ meshlink_errno = MESHLINK_EINVAL;
+ goto done;
+ }
+
+ close_network_connections(mesh);
+ exit_configuration(&mesh->config);
+
+ char portstr[10];
+ snprintf(portstr, sizeof(portstr), "%d", port);
+ portstr[sizeof(portstr) - 1] = 0;
+
+ modify_config_file(mesh, mesh->name, "Port", portstr, true);
+
+ init_configuration(&mesh->config);
+
+ if(!read_server_config(mesh)) {
+ meshlink_errno = MESHLINK_ESTORAGE;
+ } else if(!setup_network(mesh)) {
+ meshlink_errno = MESHLINK_ENETWORK;
+ } else {
+ rval = true;
+ }
+
+done:
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+
+ return rval;
+}
+
+void meshlink_set_invitation_timeout(meshlink_handle_t *mesh, int timeout) {
+ mesh->invitation_timeout = timeout;
+}
+
+char *meshlink_invite(meshlink_handle_t *mesh, const char *name) {
+ if(!mesh) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return NULL;
+ }
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
+ // Check validity of the new node's name
+ if(!check_id(name)) {
+ logger(mesh, MESHLINK_DEBUG, "Invalid name for node.\n");
+ meshlink_errno = MESHLINK_EINVAL;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return NULL;
+ }
+
+ // Ensure no host configuration file with that name exists
+ char filename[PATH_MAX];
+ snprintf(filename, sizeof(filename), "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name);
+
+ if(!access(filename, F_OK)) {
+ logger(mesh, MESHLINK_DEBUG, "A host config file for %s already exists!\n", name);
+ meshlink_errno = MESHLINK_EEXIST;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return NULL;
+ }
+
+ // Ensure no other nodes know about this name
+ if(meshlink_get_node(mesh, name)) {
+ logger(mesh, MESHLINK_DEBUG, "A node with name %s is already known!\n", name);
+ meshlink_errno = MESHLINK_EEXIST;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return NULL;
+ }
+
+ // Get the local address
+ char *address = get_my_hostname(mesh);
+
+ if(!address) {
+ logger(mesh, MESHLINK_DEBUG, "No Address known for ourselves!\n");
+ meshlink_errno = MESHLINK_ERESOLV;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return NULL;
+ }
+
+ if(!refresh_invitation_key(mesh)) {
+ meshlink_errno = MESHLINK_EINTERNAL;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return NULL;