]> git.meshlink.io Git - meshlink/blobdiff - src/net_packet.c
Handle raw packets from tiny peers.
[meshlink] / src / net_packet.c
index b32c78f7d467456166e9551ca211242a651361f7..67fadfc5a8c6817d740ec5a33aba029ea4417994 100644 (file)
@@ -279,6 +279,11 @@ static void send_sptps_packet(meshlink_handle_t *mesh, node_t *n, vpn_packet_t *
        }
 
        if(!n->status.validkey) {
+               if(n->connection && (n->connection->flags & PROTOCOL_TINY) & n->connection->status.active) {
+                       send_raw_packet(mesh, n->connection, origpkt);
+                       return;
+               }
+
                logger(mesh, MESHLINK_INFO, "No valid key known yet for %s", n->name);
 
                if(!n->status.waitingforkey) {
@@ -305,7 +310,7 @@ static void send_sptps_packet(meshlink_handle_t *mesh, node_t *n, vpn_packet_t *
        return;
 }
 
-static void choose_udp_address(meshlink_handle_t *mesh, const node_t *n, const sockaddr_t **sa, int *sock) {
+static void choose_udp_address(meshlink_handle_t *mesh, const node_t *n, const sockaddr_t **sa, int *sock, sockaddr_t *sa_buf) {
        /* Latest guess */
        *sa = &n->address;
        *sock = n->sock;
@@ -330,6 +335,25 @@ static void choose_udp_address(meshlink_handle_t *mesh, const node_t *n, const s
                goto check_socket;
        }
 
+       /* Else, if we have a canonical address, try this once every batch */
+       if(mesh->udp_choice == 1 && n->canonical_address) {
+               char *host = xstrdup(n->canonical_address);
+               char *port = strchr(host, ' ');
+
+               if(port) {
+                       *port++ = 0;
+                       *sa_buf = str2sockaddr_random(mesh, host, port);
+                       *sa = sa_buf;
+
+                       if(sa_buf->sa.sa_family != AF_UNKNOWN) {
+                               free(host);
+                               goto check_socket;
+                       }
+               }
+
+               free(host);
+       }
+
        /* Otherwise, address are found in edges to this node.
           So we pick a random edge and a random socket. */
 
@@ -422,13 +446,14 @@ bool send_sptps_data(void *handle, uint8_t type, const void *data, size_t len) {
 
        /* Otherwise, send the packet via UDP */
 
+       sockaddr_t sa_buf;
        const sockaddr_t *sa;
        int sock;
 
        if(to->status.broadcast) {
                choose_broadcast_address(mesh, to, &sa, &sock);
        } else {
-               choose_udp_address(mesh, to, &sa, &sock);
+               choose_udp_address(mesh, to, &sa, &sock, &sa_buf);
        }
 
        if(sendto(mesh->listen_socket[sock].udp.fd, data, len, 0, &sa->sa, SALEN(sa->sa)) < 0 && !sockwouldblock(sockerrno)) {