From 46e373d99b4559245885c94930a6979d03a6f12b Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Mon, 7 Oct 2019 14:13:42 +0200 Subject: [PATCH] Don't use static variables when choosing a broadcast address. Found by ThreadSanitizer. --- src/meshlink_internal.h | 1 + src/net_packet.c | 28 ++++++---------------------- src/net_setup.c | 10 ++++++++++ 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/meshlink_internal.h b/src/meshlink_internal.h index f32e87e6..cbb98c6f 100644 --- a/src/meshlink_internal.h +++ b/src/meshlink_internal.h @@ -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; diff --git a/src/net_packet.c b/src/net_packet.c index ea363b7f..e4f3295a 100644 --- a/src/net_packet.c +++ b/src/net_packet.c @@ -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) { diff --git a/src/net_setup.c b/src/net_setup.c index e7955032..367ccd29 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -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; } -- 2.39.5