+/* REQ_KEY is overloaded to allow arbitrary requests to be routed between two nodes. */
+
+static bool req_key_ext_h(meshlink_handle_t *mesh, connection_t *c, const char *request, node_t *from, int reqno) {
+ (void)c;
+
+ switch(reqno) {
+ case REQ_PUBKEY: {
+ char *pubkey = ecdsa_get_base64_public_key(mesh->private_key);
+
+ if(!from->nexthop || !from->nexthop->connection) {
+ return false;
+ }
+
+ send_request(mesh, from->nexthop->connection, NULL, "%d %s %s %d %s", REQ_KEY, mesh->self->name, from->name, ANS_PUBKEY, pubkey);
+ free(pubkey);
+ return true;
+ }
+
+ case ANS_PUBKEY: {
+ if(node_read_public_key(mesh, from)) {
+ logger(mesh, MESHLINK_WARNING, "Got ANS_PUBKEY from %s even though we already have his pubkey", from->name);
+ return true;
+ }
+
+ char pubkey[MAX_STRING_SIZE];
+
+ if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, pubkey) != 1 || !(from->ecdsa = ecdsa_set_base64_public_key(pubkey))) {
+ logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ANS_PUBKEY", from->name, "invalid pubkey");
+ return true;
+ }
+
+ logger(mesh, MESHLINK_INFO, "Learned ECDSA public key from %s", from->name);
+ from->status.dirty = true;
+
+ /* If we are trying to form an outgoing connection to this node, retry immediately */
+ for list_each(outgoing_t, outgoing, mesh->outgoings) {
+ if(outgoing->node == from && outgoing->ev.cb) {
+ outgoing->timeout = 0;
+ timeout_set(&mesh->loop, &outgoing->ev, &(struct timeval) {
+ 0, 0
+ });
+ }
+ }
+
+ /* Also reset any UTCP timers */
+ utcp_reset_timers(from->utcp);
+
+ return true;
+ }
+
+ case REQ_KEY: {
+ if(!node_read_public_key(mesh, from)) {
+ logger(mesh, MESHLINK_DEBUG, "No ECDSA key known for %s", from->name);
+ send_request(mesh, from->nexthop->connection, NULL, "%d %s %s %d", REQ_KEY, mesh->self->name, from->name, REQ_PUBKEY);
+ return true;
+ }
+
+ if(from->sptps.label) {
+ logger(mesh, MESHLINK_DEBUG, "Got REQ_KEY from %s while we already started a SPTPS session!", from->name);
+
+ if(mesh->loop.now.tv_sec < from->last_req_key + req_key_timeout && strcmp(mesh->self->name, from->name) < 0) {
+ logger(mesh, MESHLINK_DEBUG, "Ignoring REQ_KEY from %s.", from->name);
+ return true;
+ }
+ }
+
+ char buf[MAX_STRING_SIZE];
+ int len;
+
+ if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, buf) != 1 || !(len = b64decode(buf, buf, strlen(buf)))) {
+ logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "REQ_SPTPS_START", from->name, "invalid SPTPS data");
+ return true;
+ }
+
+ char label[sizeof(meshlink_udp_label) + strlen(from->name) + strlen(mesh->self->name) + 2];
+ snprintf(label, sizeof(label), "%s %s %s", meshlink_udp_label, from->name, mesh->self->name);
+ sptps_stop(&from->sptps);
+ from->status.validkey = false;
+ from->status.waitingforkey = true;
+ from->last_req_key = mesh->loop.now.tv_sec;
+
+ if(!sptps_start(&from->sptps, from, false, true, mesh->private_key, from->ecdsa, label, sizeof(label) - 1, send_sptps_data, receive_sptps_record)) {
+ logger(mesh, MESHLINK_ERROR, "Could not start SPTPS session with %s: %s", from->name, strerror(errno));
+ return true;
+ }
+
+ if(!sptps_receive_data(&from->sptps, buf, len)) {
+ logger(mesh, MESHLINK_ERROR, "Could not process SPTPS data from %s: %s", from->name, strerror(errno));
+ return true;
+ }
+
+ return true;
+ }
+
+ case REQ_SPTPS: {
+ if(!from->status.validkey) {
+ logger(mesh, MESHLINK_ERROR, "Got REQ_SPTPS from %s but we don't have a valid key yet", from->name);
+ return true;
+ }
+
+ char buf[MAX_STRING_SIZE];
+ int len;
+
+ if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, buf) != 1 || !(len = b64decode(buf, buf, strlen(buf)))) {
+ logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "REQ_SPTPS", from->name, "invalid SPTPS data");
+ return true;
+ }
+
+ if(!sptps_receive_data(&from->sptps, buf, len)) {
+ logger(mesh, MESHLINK_ERROR, "Could not process SPTPS data from %s: %s", from->name, strerror(errno));
+ return true;
+ }
+
+ return true;
+ }
+
+ default:
+ logger(mesh, MESHLINK_ERROR, "Unknown extended REQ_KEY request from %s: %s", from->name, request);
+ return true;
+ }
+}
+
+bool req_key_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
+ assert(request);
+ assert(*request);
+