]> git.meshlink.io Git - meshlink/commitdiff
Send our canonical address to nodes we want to communicate with.
authorGuus Sliepen <guus@meshlink.io>
Wed, 30 Dec 2020 13:55:35 +0000 (14:55 +0100)
committerGuus Sliepen <guus@meshlink.io>
Wed, 30 Dec 2020 13:55:35 +0000 (14:55 +0100)
This ensures canonical addresses are propagated automatically, and can be
used when making meta and UDP connections.

src/protocol.h
src/protocol_key.c

index f72d42d8999869c8f2baa27daf2dbad868a3660a..fb1e10960cc25539971a4250f8c9f358a1e87556 100644 (file)
@@ -48,6 +48,7 @@ typedef enum request_t {
        CONTROL,
        REQ_PUBKEY, ANS_PUBKEY,
        REQ_SPTPS,
        CONTROL,
        REQ_PUBKEY, ANS_PUBKEY,
        REQ_SPTPS,
+       REQ_CANONICAL,
        NUM_REQUESTS
 } request_t;
 
        NUM_REQUESTS
 } request_t;
 
@@ -94,6 +95,7 @@ bool send_pong(struct meshlink_handle *mesh, struct connection_t *);
 bool send_add_edge(struct meshlink_handle *mesh, struct connection_t *, const struct edge_t *, int contradictions);
 bool send_del_edge(struct meshlink_handle *mesh, struct connection_t *, const struct edge_t *, int contradictions);
 bool send_req_key(struct meshlink_handle *mesh, struct node_t *);
 bool send_add_edge(struct meshlink_handle *mesh, struct connection_t *, const struct edge_t *, int contradictions);
 bool send_del_edge(struct meshlink_handle *mesh, struct connection_t *, const struct edge_t *, int contradictions);
 bool send_req_key(struct meshlink_handle *mesh, struct node_t *);
+bool send_canonical_address(struct meshlink_handle *mesh, struct node_t *);
 
 /* Request handlers  */
 
 
 /* Request handlers  */
 
index 2dcb5e8d66debc9d8e814e53d3e624598e01fbbf..dbe3d917b779b87126bed3ca23ffcd5324c781a3 100644 (file)
@@ -83,6 +83,14 @@ static bool send_initial_sptps_data(void *handle, uint8_t type, const void *data
        return send_request(mesh, to->nexthop->connection, NULL, "%d %s %s %d %s", REQ_KEY, mesh->self->name, to->name, REQ_KEY, buf);
 }
 
        return send_request(mesh, to->nexthop->connection, NULL, "%d %s %s %d %s", REQ_KEY, mesh->self->name, to->name, REQ_KEY, buf);
 }
 
+bool send_canonical_address(meshlink_handle_t *mesh, node_t *to) {
+       if(!mesh->self->canonical_address) {
+               return true;
+       }
+
+       return send_request(mesh, to->nexthop->connection, NULL, "%d %s %s %d %s", REQ_KEY, mesh->self->name, to->name, REQ_CANONICAL, mesh->self->canonical_address);
+}
+
 bool send_req_key(meshlink_handle_t *mesh, node_t *to) {
        if(!node_read_public_key(mesh, to)) {
                logger(mesh, MESHLINK_DEBUG, "No ECDSA key known for %s", to->name);
 bool send_req_key(meshlink_handle_t *mesh, node_t *to) {
        if(!node_read_public_key(mesh, to)) {
                logger(mesh, MESHLINK_DEBUG, "No ECDSA key known for %s", to->name);
@@ -102,6 +110,9 @@ bool send_req_key(meshlink_handle_t *mesh, node_t *to) {
                logger(mesh, MESHLINK_DEBUG, "send_req_key(%s) called while sptps->label != NULL!", to->name);
        }
 
                logger(mesh, MESHLINK_DEBUG, "send_req_key(%s) called while sptps->label != NULL!", to->name);
        }
 
+       /* Send our canonical address to help with UDP hole punching */
+       send_canonical_address(mesh, to);
+
        char label[sizeof(meshlink_udp_label) + strlen(mesh->self->name) + strlen(to->name) + 2];
        snprintf(label, sizeof(label), "%s %s %s", meshlink_udp_label, mesh->self->name, to->name);
        sptps_stop(&to->sptps);
        char label[sizeof(meshlink_udp_label) + strlen(mesh->self->name) + strlen(to->name) + 2];
        snprintf(label, sizeof(label), "%s %s %s", meshlink_udp_label, mesh->self->name, to->name);
        sptps_stop(&to->sptps);
@@ -206,6 +217,9 @@ static bool req_key_ext_h(meshlink_handle_t *mesh, connection_t *c, const char *
                from->status.waitingforkey = true;
                from->last_req_key = mesh->loop.now.tv_sec;
 
                from->status.waitingforkey = true;
                from->last_req_key = mesh->loop.now.tv_sec;
 
+               /* Send our canonical address to help with UDP hole punching */
+               send_canonical_address(mesh, from);
+
                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_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;
@@ -241,6 +255,28 @@ static bool req_key_ext_h(meshlink_handle_t *mesh, connection_t *c, const char *
                return true;
        }
 
                return true;
        }
 
+       case REQ_CANONICAL: {
+               char host[MAX_STRING_SIZE];
+               char port[MAX_STRING_SIZE];
+
+               if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING " " MAX_STRING, host, port) != 2) {
+                       logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "REQ_CANONICAL", from->name, "invalid canonical address");
+                       return true;
+               }
+
+               strncat(host, " ", MAX_STRING_SIZE - 1);
+               strncat(host, port, MAX_STRING_SIZE - 1);
+
+               if(from->canonical_address && !strcmp(from->canonical_address, host)) {
+                       return true;
+               }
+
+               logger(mesh, MESHLINK_DEBUG, "Updating canonical address of %s to %s", from->name, host);
+               free(from->canonical_address);
+               from->canonical_address = xstrdup(host);
+               return true;
+       }
+
        default:
                logger(mesh, MESHLINK_ERROR, "Unknown extended REQ_KEY request from %s: %s", from->name, request);
                return true;
        default:
                logger(mesh, MESHLINK_ERROR, "Unknown extended REQ_KEY request from %s: %s", from->name, request);
                return true;