From 439dfc68865a286c48c79672a350cd467da38799 Mon Sep 17 00:00:00 2001 From: Logan oos Even <46396513+Logan007@users.noreply.github.com> Date: Sun, 26 Jun 2022 17:17:22 +0200 Subject: [PATCH] generalized sockaddr usage in edge (#1026) --- include/n2n_wire.h | 2 +- src/edge_utils.c | 24 ++++++++++-------------- src/wire.c | 2 +- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/include/n2n_wire.h b/include/n2n_wire.h index f56c8d9..1edf5be 100644 --- a/include/n2n_wire.h +++ b/include/n2n_wire.h @@ -191,7 +191,7 @@ int fill_sockaddr (struct sockaddr * addr, const n2n_sock_t * sock); int fill_n2nsock (n2n_sock_t* sock, - struct sockaddr* sa); + const struct sockaddr* sa); int encode_PACKET (uint8_t * base, size_t * idx, diff --git a/src/edge_utils.c b/src/edge_utils.c index 962fc4e..06d71e7 100644 --- a/src/edge_utils.c +++ b/src/edge_utils.c @@ -2160,7 +2160,7 @@ void edge_read_from_tap (n2n_edge_t * eee) { /** handle a datagram from the main UDP socket to the internet. */ -void process_udp (n2n_edge_t *eee, const struct sockaddr_in *sender_sock, const SOCKET in_sock, +void process_udp (n2n_edge_t *eee, const struct sockaddr *sender_sock, const SOCKET in_sock, uint8_t *udp_buf, size_t udp_size, time_t now) { n2n_common_t cmn; /* common fields in the packet header */ @@ -2192,7 +2192,7 @@ void process_udp (n2n_edge_t *eee, const struct sockaddr_in *sender_sock, const else { // REVISIT: type conversion back and forth, choose a consistent approach throughout whole code, // i.e. stick with more general sockaddr as long as possible and narrow only if required - fill_n2nsock(&sender, (struct sockaddr*)sender_sock); + fill_n2nsock(&sender, sender_sock); } /* The packet may not have an orig_sender socket spec. So default to last * hop as sender. */ @@ -2703,18 +2703,18 @@ int fetch_and_eventually_process_data (n2n_edge_t *eee, SOCKET sock, ssize_t bread = 0; + struct sockaddr_storage sas; + struct sockaddr *sender_sock = (struct sockaddr*)&sas; + socklen_t ss_size = sizeof(sas); + if((!eee->conf.connect_tcp) #ifndef SKIP_MULTICAST_PEERS_DISCOVERY || (sock == eee->udp_multicast_sock) #endif ) { // udp - struct sockaddr_in sender_sock; - socklen_t i; - - i = sizeof(sender_sock); bread = recvfrom(sock, pktbuf, N2N_PKT_BUF_SIZE, 0 /*flags*/, - (struct sockaddr *)&sender_sock, (socklen_t *)&i); + sender_sock, &ss_size); if((bread < 0) #ifdef WIN32 @@ -2733,18 +2733,14 @@ int fetch_and_eventually_process_data (n2n_edge_t *eee, SOCKET sock, // we have a datagram to process... if(bread > 0) { // ...and the datagram has data (not just a header) - process_udp(eee, &sender_sock, sock, pktbuf, bread, now); + process_udp(eee, sender_sock, sock, pktbuf, bread, now); } } else { // tcp - struct sockaddr_in sender_sock; - socklen_t i; - - i = sizeof(sender_sock); bread = recvfrom(sock, pktbuf + *position, *expected - *position, 0 /*flags*/, - (struct sockaddr *)&sender_sock, (socklen_t *)&i); + sender_sock, &ss_size); if((bread <= 0) && (errno)) { traceEvent(TRACE_ERROR, "recvfrom() failed %d errno %d (%s)", bread, errno, strerror(errno)); #ifdef WIN32 @@ -2768,7 +2764,7 @@ int fetch_and_eventually_process_data (n2n_edge_t *eee, SOCKET sock, } } else { // full packet read, handle it - process_udp(eee, (struct sockaddr_in*)&sender_sock, sock, + process_udp(eee, sender_sock, sock, pktbuf + sizeof(uint16_t), *position - sizeof(uint16_t), now); // reset, await new prepended length *expected = sizeof(uint16_t); diff --git a/src/wire.c b/src/wire.c index 2901ad3..7f0dd58 100644 --- a/src/wire.c +++ b/src/wire.c @@ -615,7 +615,7 @@ int fill_sockaddr (struct sockaddr * addr, // fills struct sockaddr's data into n2n_sock -int fill_n2nsock (n2n_sock_t* sock, struct sockaddr* sa) { +int fill_n2nsock (n2n_sock_t* sock, const struct sockaddr* sa) { sock->family = *(sa_family_t*)sa; switch(sock->family) {