]> git.meshlink.io Git - meshlink/commitdiff
Don't use static variables when choosing a broadcast address.
authorGuus Sliepen <guus@meshlink.io>
Mon, 7 Oct 2019 12:13:42 +0000 (14:13 +0200)
committerGuus Sliepen <guus@meshlink.io>
Mon, 7 Oct 2019 12:13:42 +0000 (14:13 +0200)
Found by ThreadSanitizer.

src/meshlink_internal.h
src/net_packet.c
src/net_setup.c

index f32e87e65b18a20959b26dcd1404fc4a46473437..cbb98c6fd07048994f0bf7cae1df7ca52e951690 100644 (file)
@@ -54,6 +54,7 @@ typedef struct listen_socket_t {
        struct io_t tcp;
        struct io_t udp;
        sockaddr_t sa;
+       sockaddr_t broadcast_sa;
        bool bindto;
 } listen_socket_t;
 
index ea363b7f98dfcaa0f423992d6c5386721b0b7597..e4f3295af941bdd92335298c30b9c776e9b9c8dd 100644 (file)
@@ -324,32 +324,16 @@ static void choose_udp_address(meshlink_handle_t *mesh, const node_t *n, const s
 }
 
 static void choose_broadcast_address(meshlink_handle_t *mesh, const node_t *n, const sockaddr_t **sa, int *sock) {
-       static sockaddr_t broadcast_ipv4 = {
-               .in = {
-                       .sin_family = AF_INET,
-                       .sin_addr.s_addr = -1,
-               }
-       };
-
-       static sockaddr_t broadcast_ipv6 = {
-               .in6 = {
-                       .sin6_family = AF_INET6,
-                       .sin6_addr.s6_addr[0x0] = 0xff,
-                       .sin6_addr.s6_addr[0x1] = 0x02,
-                       .sin6_addr.s6_addr[0xf] = 0x01,
-               }
-       };
-
        *sock = prng(mesh, mesh->listen_sockets);
+       sockaddr_t *broadcast_sa = &mesh->listen_socket[*sock].broadcast_sa;
 
-       if(mesh->listen_socket[*sock].sa.sa.sa_family == AF_INET6) {
-               broadcast_ipv6.in6.sin6_port = n->prevedge->address.in.sin_port;
-               broadcast_ipv6.in6.sin6_scope_id = mesh->listen_socket[*sock].sa.in6.sin6_scope_id;
-               *sa = &broadcast_ipv6;
+       if(broadcast_sa->sa.sa_family == AF_INET6) {
+               broadcast_sa->in6.sin6_port = n->prevedge->address.in.sin_port;
        } else {
-               broadcast_ipv4.in.sin_port = n->prevedge->address.in.sin_port;
-               *sa = &broadcast_ipv4;
+               broadcast_sa->in.sin_port = n->prevedge->address.in.sin_port;
        }
+
+       *sa = broadcast_sa;
 }
 
 static void send_udppacket(meshlink_handle_t *mesh, node_t *n, vpn_packet_t *origpkt) {
index e79550327d1ec5bf15cdc79a4f03a4bda6a3f5ad..367ccd293857f98162b50ba4a8a4afda5f3be217 100644 (file)
@@ -381,6 +381,16 @@ static bool add_listen_address(meshlink_handle_t *mesh, char *address, bool bind
 
                mesh->listen_socket[mesh->listen_sockets].bindto = bindto;
                memcpy(&mesh->listen_socket[mesh->listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
+               memcpy(&mesh->listen_socket[mesh->listen_sockets].broadcast_sa, aip->ai_addr, aip->ai_addrlen);
+
+               if(aip->ai_family == AF_INET6) {
+                       mesh->listen_socket[mesh->listen_sockets].broadcast_sa.in6.sin6_addr.s6_addr[0x0] = 0xff;
+                       mesh->listen_socket[mesh->listen_sockets].broadcast_sa.in6.sin6_addr.s6_addr[0x1] = 0x02;
+                       mesh->listen_socket[mesh->listen_sockets].broadcast_sa.in6.sin6_addr.s6_addr[0xf] = 0x01;
+               } else {
+                       mesh->listen_socket[mesh->listen_sockets].broadcast_sa.in.sin_addr.s_addr = 0xffffffff;
+               }
+
                mesh->listen_sockets++;
                success = true;
        }