+ 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;
+
+ char cookie[25];
+ b64encode_urlsafe(data, cookie, 18);
+
+ 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;
+ }
+
+ // 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);
+ sptps_send_record(&c->sptps, 1, buf, 0);
+ fclose(f);
+ unlink(usedname);
+
+ c->status.invitation_used = true;
+
+ logger(DEBUG_CONNECTIONS, LOG_INFO, "Invitation %s succesfully sent to %s (%s)", cookie, c->name, c->hostname);
+ return true;
+}
+
+bool id_h(connection_t *c, const char *request) {
+ char name[MAX_STRING_SIZE];
+
+ if(sscanf(request, "%*d " MAX_STRING " %d.%d", name, &c->protocol_major, &c->protocol_minor) < 2) {
+ logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ID", c->name,