+ // Count the number of valid invitations, clean up old ones
+ DIR *dir = opendir(filename);
+ if(!dir) {
+ logger(mesh, MESHLINK_DEBUG, "Could not read directory %s: %s\n", filename, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return false;
+ }
+
+ errno = 0;
+ int count = 0;
+ struct dirent *ent;
+ time_t deadline = time(NULL) - 604800; // 1 week in the past
+
+ while((ent = readdir(dir))) {
+ if(strlen(ent->d_name) != 24)
+ continue;
+ char invname[PATH_MAX];
+ struct stat st;
+ snprintf(invname, sizeof invname, "%s" SLASH "%s", filename, ent->d_name);
+ if(!stat(invname, &st)) {
+ if(mesh->invitation_key && deadline < st.st_mtime)
+ count++;
+ else
+ unlink(invname);
+ } else {
+ logger(mesh, MESHLINK_DEBUG, "Could not stat %s: %s\n", invname, strerror(errno));
+ errno = 0;
+ }
+ }
+
+ if(errno) {
+ logger(mesh, MESHLINK_DEBUG, "Error while reading directory %s: %s\n", filename, strerror(errno));
+ closedir(dir);
+ meshlink_errno = MESHLINK_ESTORAGE;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return false;
+ }
+
+ closedir(dir);
+
+ snprintf(filename, sizeof filename, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", mesh->confbase);
+
+ // Remove the key if there are no outstanding invitations.
+ if(!count) {
+ unlink(filename);
+ if(mesh->invitation_key) {
+ ecdsa_free(mesh->invitation_key);
+ mesh->invitation_key = NULL;
+ }
+ }
+
+ if(mesh->invitation_key) {
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return true;
+ }
+
+ // Create a new key if necessary.
+ FILE *f = fopen(filename, "r");
+ if(!f) {
+ if(errno != ENOENT) {
+ logger(mesh, MESHLINK_DEBUG, "Could not read %s: %s\n", filename, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return false;
+ }
+
+ mesh->invitation_key = ecdsa_generate();
+ if(!mesh->invitation_key) {
+ logger(mesh, MESHLINK_DEBUG, "Could not generate a new key!\n");
+ meshlink_errno = MESHLINK_EINTERNAL;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return false;
+ }
+ f = fopen(filename, "w");
+ if(!f) {
+ logger(mesh, MESHLINK_DEBUG, "Could not write %s: %s\n", filename, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return false;
+ }
+ chmod(filename, 0600);
+ ecdsa_write_pem_private_key(mesh->invitation_key, f);
+ fclose(f);
+ } else {
+ mesh->invitation_key = ecdsa_read_pem_private_key(f);
+ fclose(f);
+ if(!mesh->invitation_key) {
+ logger(mesh, MESHLINK_DEBUG, "Could not read private key from %s\n", filename);
+ meshlink_errno = MESHLINK_ESTORAGE;
+ }
+ }
+
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return mesh->invitation_key;
+}
+
+bool meshlink_add_address(meshlink_handle_t *mesh, const char *address) {
+ if(!mesh || !address) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return false;
+ }
+
+ bool rval = false;
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
+ for(const char *p = address; *p; p++) {
+ if(isalnum(*p) || *p == '-' || *p == '.' || *p == ':')
+ continue;
+ logger(mesh, MESHLINK_DEBUG, "Invalid character in address: %s\n", address);
+ meshlink_errno = MESHLINK_EINVAL;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return false;
+ }
+
+ rval = append_config_file(mesh, mesh->self->name, "Address", address);
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return rval;
+}
+
+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;
+ }
+
+ char hash[64];
+
+ // Create a hash of the key.
+ char *fingerprint = ecdsa_get_base64_public_key(mesh->invitation_key);
+ sha512(fingerprint, strlen(fingerprint), hash);
+ b64encode_urlsafe(hash, hash, 18);
+
+ // Create a random cookie for this invitation.
+ char cookie[25];
+ randomize(cookie, 18);
+
+ // Create a filename that doesn't reveal the cookie itself
+ char buf[18 + strlen(fingerprint)];
+ char cookiehash[64];
+ memcpy(buf, cookie, 18);
+ memcpy(buf + 18, fingerprint, sizeof buf - 18);
+ sha512(buf, sizeof buf, cookiehash);
+ b64encode_urlsafe(cookiehash, cookiehash, 18);
+
+ b64encode_urlsafe(cookie, cookie, 18);
+
+ free(fingerprint);
+
+ // Create a file containing the details of the invitation.
+ snprintf(filename, sizeof filename, "%s" SLASH "invitations" SLASH "%s", mesh->confbase, cookiehash);
+ int ifd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600);
+ if(!ifd) {
+ logger(mesh, MESHLINK_DEBUG, "Could not create invitation file %s: %s\n", filename, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return NULL;
+ }
+ FILE *f = fdopen(ifd, "w");
+ if(!f)
+ abort();
+
+ // Fill in the details.
+ fprintf(f, "Name = %s\n", name);
+ //if(netname)
+ // fprintf(f, "NetName = %s\n", netname);
+ fprintf(f, "ConnectTo = %s\n", mesh->self->name);
+
+ // Copy Broadcast and Mode
+ snprintf(filename, sizeof filename, "%s" SLASH "meshlink.conf", mesh->confbase);
+ FILE *tc = fopen(filename, "r");
+ if(tc) {
+ char buf[1024];
+ while(fgets(buf, sizeof buf, tc)) {
+ if((!strncasecmp(buf, "Mode", 4) && strchr(" \t=", buf[4]))
+ || (!strncasecmp(buf, "Broadcast", 9) && strchr(" \t=", buf[9]))) {
+ fputs(buf, f);
+ // Make sure there is a newline character.
+ if(!strchr(buf, '\n'))
+ fputc('\n', f);
+ }
+ }
+ fclose(tc);
+ } else {
+ logger(mesh, MESHLINK_DEBUG, "Could not create %s: %s\n", filename, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return NULL;
+ }
+
+ fprintf(f, "#---------------------------------------------------------------#\n");
+ fprintf(f, "Name = %s\n", mesh->self->name);
+
+ snprintf(filename, sizeof filename, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, mesh->self->name);
+ fcopy(f, filename);
+ fclose(f);
+
+ // Create an URL from the local address, key hash and cookie
+ char *url;
+ xasprintf(&url, "%s/%s%s", address, hash, cookie);
+ free(address);
+
+ pthread_mutex_unlock(&(mesh->mesh_mutex));
+ return url;
+}
+
+bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
+ if(!mesh || !invitation) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return false;
+ }
+
+ pthread_mutex_lock(&(mesh->mesh_mutex));
+
+ //TODO: think of a better name for this variable, or of a different way to tokenize the invitation URL.
+ char copy[strlen(invitation) + 1];
+ strcpy(copy, invitation);
+
+ // Split the invitation URL into hostname, port, key hash and cookie.
+
+ char *slash = strchr(copy, '/');