+ // Ensure no other nodes know about this name
+ if(meshlink_get_node(mesh, name)) {
+ fprintf(stderr, "A node with name %s is already known!\n", name);
+ meshlink_errno = MESHLINK_EEXIST;
+ return NULL;
+ }
+
+ // Get the local address
+ char *address = get_my_hostname(mesh);
+ if(!address) {
+ fprintf(stderr, "No Address known for ourselves!\n");
+ meshlink_errno = MESHLINK_ERESOLV;
+ return NULL;
+ }
+
+ if(!refresh_invitation_key(mesh)) {
+ meshlink_errno = MESHLINK_EINTERNAL;
+ 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) {
+ fprintf(stderr, "Could not create invitation file %s: %s\n", filename, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ 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 {
+ fprintf(stderr, "Could not create %s: %s\n", filename, strerror(errno));
+ meshlink_errno = MESHLINK_ESTORAGE;
+ 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);
+
+ return url;
+}
+
+bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) {
+ if(!mesh || !invitation) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return false;
+ }
+
+ //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, '/');