+ // 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)) {
+ fprintf(stderr, "A host config file for %s already exists!\n", name);
+ meshlink_errno = MESHLINK_EEXIST;
+ return NULL;
+ }
+
+ // 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);