+bool send_id(connection_t *c) {
+ gettimeofday(&c->start, NULL);
+
+ int minor = 0;
+
+ if(experimental) {
+ if(c->outgoing && !read_ecdsa_public_key(c)) {
+ minor = 1;
+ } else {
+ minor = myself->connection->protocol_minor;
+ }
+ }
+
+ if(proxytype && c->outgoing)
+ if(!send_proxyrequest(c)) {
+ return false;
+ }
+
+ return send_request(c, "%d %s %d.%d", ID, myself->connection->name, myself->connection->protocol_major, minor);
+}
+
+static bool finalize_invitation(connection_t *c, const char *data, uint16_t len) {
+ if(strchr(data, '\n')) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Received invalid key from invited node %s (%s)!\n", c->name, c->hostname);
+ return false;
+ }
+
+ // Create a new host config file
+ char filename[PATH_MAX];
+ snprintf(filename, sizeof(filename), "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
+
+ if(!access(filename, F_OK)) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Host config file for %s (%s) already exists!\n", c->name, c->hostname);
+ return false;
+ }
+
+ FILE *f = fopen(filename, "w");
+
+ if(!f) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Error trying to create %s: %s\n", filename, strerror(errno));
+ return false;
+ }
+
+ fprintf(f, "Ed25519PublicKey = %s\n", data);
+ fclose(f);
+
+ logger(DEBUG_CONNECTIONS, LOG_INFO, "Key succesfully received from %s (%s)", c->name, c->hostname);
+
+ // Call invitation-accepted script
+ environment_t env;
+ char *address, *port;
+
+ environment_init(&env);
+ environment_add(&env, "NODE=%s", c->name);
+ sockaddr2str(&c->address, &address, &port);
+ environment_add(&env, "REMOTEADDRESS=%s", address);
+ environment_add(&env, "NAME=%s", myself->name);
+
+ execute_script("invitation-accepted", &env);
+
+ environment_exit(&env);
+
+ sptps_send_record(&c->sptps, 2, data, 0);
+ return true;
+}
+
+static bool receive_invitation_sptps(void *handle, uint8_t type, const void *data, uint16_t len) {
+ connection_t *c = handle;
+
+ if(type == 128) {
+ return true;
+ }
+
+ if(type == 1 && c->status.invitation_used) {
+ return finalize_invitation(c, data, len);
+ }
+
+ if(type != 0 || len != 18 || c->status.invitation_used) {
+ return false;
+ }
+
+ // Recover the filename from the cookie and the key
+ char *fingerprint = ecdsa_get_base64_public_key(invitation_key);
+ char hashbuf[18 + strlen(fingerprint)];
+ char cookie[64];
+ memcpy(hashbuf, data, 18);
+ memcpy(hashbuf + 18, fingerprint, sizeof(hashbuf) - 18);
+ sha512(hashbuf, sizeof(hashbuf), cookie);
+ b64encode_urlsafe(cookie, cookie, 18);
+ free(fingerprint);
+
+ char filename[PATH_MAX], usedname[PATH_MAX];
+ snprintf(filename, sizeof(filename), "%s" SLASH "invitations" SLASH "%s", confbase, cookie);
+ snprintf(usedname, sizeof(usedname), "%s" SLASH "invitations" SLASH "%s.used", confbase, cookie);
+
+ // Atomically rename the invitation file
+ if(rename(filename, usedname)) {
+ if(errno == ENOENT) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s tried to use non-existing invitation %s\n", c->hostname, cookie);
+ } else {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Error trying to rename invitation %s\n", cookie);
+ }
+
+ return false;
+ }
+
+ // Check the timestamp of the invitation
+ struct stat st;
+
+ if(stat(usedname, &st)) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Could not stat %s", usedname);
+ return false;
+ }
+
+ if(st.st_mtime + invitation_lifetime < now.tv_sec) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s tried to use expired invitation %s", c->hostname, cookie);
+ return false;
+ }
+
+ // Open the renamed file
+ FILE *f = fopen(usedname, "r");
+
+ if(!f) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Error trying to open invitation %s\n", cookie);
+ return false;
+ }
+
+ // Read the new node's Name from the file
+ char buf[1024];
+ fgets(buf, sizeof(buf), f);
+
+ if(*buf) {
+ buf[strlen(buf) - 1] = 0;
+ }
+
+ len = strcspn(buf, " \t=");
+ char *name = buf + len;
+ name += strspn(name, " \t");
+
+ if(*name == '=') {
+ name++;
+ name += strspn(name, " \t");
+ }
+
+ buf[len] = 0;
+
+ if(!*buf || !*name || strcasecmp(buf, "Name") || !check_id(name)) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Invalid invitation file %s\n", cookie);
+ fclose(f);
+ return false;
+ }
+
+ free(c->name);
+ c->name = xstrdup(name);
+
+ // Send the node the contents of the invitation file
+ rewind(f);
+ size_t result;
+
+ while((result = fread(buf, 1, sizeof(buf), f))) {
+ sptps_send_record(&c->sptps, 0, buf, result);
+ }