From d41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb Mon Sep 17 00:00:00 2001 From: ng0 Date: Sun, 8 Sep 2019 12:33:09 +0000 Subject: uncrustify as demanded. --- src/nat/gnunet-helper-nat-client-windows.c | 325 ++--- src/nat/gnunet-helper-nat-client.c | 341 +++-- src/nat/gnunet-helper-nat-server-windows.c | 433 +++--- src/nat/gnunet-helper-nat-server.c | 583 ++++---- src/nat/gnunet-nat.c | 432 +++--- src/nat/gnunet-service-nat.c | 2190 ++++++++++++++-------------- src/nat/gnunet-service-nat.h | 26 +- src/nat/gnunet-service-nat_externalip.c | 181 ++- src/nat/gnunet-service-nat_externalip.h | 18 +- src/nat/gnunet-service-nat_helper.c | 368 +++-- src/nat/gnunet-service-nat_helper.h | 28 +- src/nat/gnunet-service-nat_mini.c | 594 ++++---- src/nat/gnunet-service-nat_mini.h | 20 +- src/nat/gnunet-service-nat_stun.c | 178 +-- src/nat/gnunet-service-nat_stun.h | 10 +- src/nat/nat.h | 34 +- src/nat/nat_api.c | 548 +++---- src/nat/nat_api_stun.c | 155 +- src/nat/nat_stun.h | 67 +- src/nat/test_nat.c | 124 +- src/nat/test_nat_mini.c | 84 +- src/nat/test_nat_test.c | 107 +- src/nat/test_stun.c | 278 ++-- 23 files changed, 3549 insertions(+), 3575 deletions(-) (limited to 'src/nat') diff --git a/src/nat/gnunet-helper-nat-client-windows.c b/src/nat/gnunet-helper-nat-client-windows.c index dda6552d8..2dac5d48e 100644 --- a/src/nat/gnunet-helper-nat-client-windows.c +++ b/src/nat/gnunet-helper-nat-client-windows.c @@ -11,12 +11,12 @@ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . SPDX-License-Identifier: AGPL3.0-or-later -*/ + */ /** * @file src/nat/gnunet-helper-nat-client-windows.c @@ -43,7 +43,7 @@ */ #define _GNU_SOURCE /* Instead of including gnunet_common.h */ -#define GNUNET_memcpy(dst,src,n) do { if (0 != n) { (void) memcpy (dst,src,n); } } while (0) +#define GNUNET_memcpy(dst, src, n) do { if (0 != n) { (void)memcpy(dst, src, n); } } while (0) #define FD_SETSIZE 1024 #include @@ -73,9 +73,7 @@ /** * IPv4 header. */ -struct ip_header -{ - +struct ip_header { /** * Version (4 bits) + Internet header length (4 bits) */ @@ -131,8 +129,7 @@ struct ip_header /** * Format of ICMP packet. */ -struct icmp_ttl_exceeded_header -{ +struct icmp_ttl_exceeded_header { uint8_t type; uint8_t code; @@ -144,8 +141,7 @@ struct icmp_ttl_exceeded_header /* followed by original payload */ }; -struct icmp_echo_header -{ +struct icmp_echo_header { uint8_t type; uint8_t code; @@ -158,8 +154,7 @@ struct icmp_echo_header /** * Beginning of UDP packet. */ -struct udp_header -{ +struct udp_header { uint16_t src_port; uint16_t dst_port; @@ -200,14 +195,14 @@ static uint16_t port; * @return 1 on success */ static int -inet_pton (int af, const char *cp, struct in_addr *buf) +inet_pton(int af, const char *cp, struct in_addr *buf) { - buf->s_addr = inet_addr (cp); + buf->s_addr = inet_addr(cp); if (buf->s_addr == INADDR_NONE) - { - fprintf (stderr, "Error %d handling address %s", WSAGetLastError (), cp); - return 0; - } + { + fprintf(stderr, "Error %d handling address %s", WSAGetLastError(), cp); + return 0; + } return 1; } @@ -220,7 +215,7 @@ inet_pton (int af, const char *cp, struct in_addr *buf) * @return the CRC 16. */ static uint16_t -calc_checksum (const uint16_t * data, unsigned int bytes) +calc_checksum(const uint16_t * data, unsigned int bytes) { uint32_t sum; unsigned int i; @@ -229,7 +224,7 @@ calc_checksum (const uint16_t * data, unsigned int bytes) for (i = 0; i < bytes / 2; i++) sum += data[i]; sum = (sum & 0xffff) + (sum >> 16); - sum = htons (0xffff - sum); + sum = htons(0xffff - sum); return sum; } @@ -241,11 +236,11 @@ calc_checksum (const uint16_t * data, unsigned int bytes) * @param other target address */ static void -send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other) +send_icmp_udp(const struct in_addr *my_ip, const struct in_addr *other) { - char packet[sizeof (struct ip_header) * 2 + - sizeof (struct icmp_ttl_exceeded_header) + - sizeof (struct udp_header)]; + char packet[sizeof(struct ip_header) * 2 + + sizeof(struct icmp_ttl_exceeded_header) + + sizeof(struct udp_header)]; struct ip_header ip_pkt; struct icmp_ttl_exceeded_header icmp_pkt; struct udp_header udp_pkt; @@ -257,8 +252,8 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other) off = 0; ip_pkt.vers_ihl = 0x45; ip_pkt.tos = 0; - ip_pkt.pkt_len = htons (sizeof (packet)); - ip_pkt.id = htons (256); + ip_pkt.pkt_len = htons(sizeof(packet)); + ip_pkt.id = htons(256); ip_pkt.flags_frag_offset = 0; ip_pkt.ttl = 128; ip_pkt.proto = IPPROTO_ICMP; @@ -266,23 +261,23 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other) ip_pkt.src_ip = my_ip->s_addr; ip_pkt.dst_ip = other->s_addr; ip_pkt.checksum = - htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); - GNUNET_memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); - off += sizeof (struct ip_header); + htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header))); + GNUNET_memcpy(&packet[off], &ip_pkt, sizeof(struct ip_header)); + off += sizeof(struct ip_header); icmp_pkt.type = ICMP_TIME_EXCEEDED; icmp_pkt.code = 0; icmp_pkt.checksum = 0; icmp_pkt.unused = 0; - GNUNET_memcpy (&packet[off], &icmp_pkt, sizeof (struct icmp_ttl_exceeded_header)); - off += sizeof (struct icmp_ttl_exceeded_header); + GNUNET_memcpy(&packet[off], &icmp_pkt, sizeof(struct icmp_ttl_exceeded_header)); + off += sizeof(struct icmp_ttl_exceeded_header); /* ip header of the presumably 'lost' udp packet */ ip_pkt.vers_ihl = 0x45; ip_pkt.tos = 0; ip_pkt.pkt_len = - htons (sizeof (struct ip_header) + sizeof (struct udp_header)); - ip_pkt.id = htons (0); + htons(sizeof(struct ip_header) + sizeof(struct udp_header)); + ip_pkt.id = htons(0); ip_pkt.flags_frag_offset = 0; ip_pkt.ttl = 128; ip_pkt.proto = IPPROTO_UDP; @@ -290,41 +285,41 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other) ip_pkt.src_ip = other->s_addr; ip_pkt.dst_ip = dummy.s_addr; ip_pkt.checksum = - htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); - GNUNET_memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); - off += sizeof (struct ip_header); + htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header))); + GNUNET_memcpy(&packet[off], &ip_pkt, sizeof(struct ip_header)); + off += sizeof(struct ip_header); /* build UDP header */ - udp_pkt.src_port = htons (NAT_TRAV_PORT); - udp_pkt.dst_port = htons (NAT_TRAV_PORT); - udp_pkt.length = htons (port); + udp_pkt.src_port = htons(NAT_TRAV_PORT); + udp_pkt.dst_port = htons(NAT_TRAV_PORT); + udp_pkt.length = htons(port); udp_pkt.crc = 0; - GNUNET_memcpy (&packet[off], &udp_pkt, sizeof (struct udp_header)); - off += sizeof (struct udp_header); + GNUNET_memcpy(&packet[off], &udp_pkt, sizeof(struct udp_header)); + off += sizeof(struct udp_header); /* no go back to calculate ICMP packet checksum */ icmp_pkt.checksum = - htons (calc_checksum - ((uint16_t *) & packet[off], - sizeof (struct icmp_ttl_exceeded_header) + - sizeof (struct ip_header) + sizeof (struct udp_header))); - GNUNET_memcpy (&packet[sizeof (struct ip_header)], &icmp_pkt, - sizeof (struct icmp_ttl_exceeded_header)); - - memset (&dst, 0, sizeof (dst)); + htons(calc_checksum + ((uint16_t *)&packet[off], + sizeof(struct icmp_ttl_exceeded_header) + + sizeof(struct ip_header) + sizeof(struct udp_header))); + GNUNET_memcpy(&packet[sizeof(struct ip_header)], &icmp_pkt, + sizeof(struct icmp_ttl_exceeded_header)); + + memset(&dst, 0, sizeof(dst)); dst.sin_family = AF_INET; dst.sin_addr = *other; err = - sendto (rawsock, packet, sizeof (packet), 0, (struct sockaddr *) &dst, - sizeof (dst)); + sendto(rawsock, packet, sizeof(packet), 0, (struct sockaddr *)&dst, + sizeof(dst)); if (err < 0) - { - fprintf (stderr, "sendto failed: %s\n", strerror (errno)); - } - else if (sizeof (packet) != (size_t) err) - { - fprintf (stderr, "Error: partial send of ICMP message\n"); - } + { + fprintf(stderr, "sendto failed: %s\n", strerror(errno)); + } + else if (sizeof(packet) != (size_t)err) + { + fprintf(stderr, "Error: partial send of ICMP message\n"); + } } @@ -335,15 +330,15 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other) * @param other target address */ static void -send_icmp (const struct in_addr *my_ip, const struct in_addr *other) +send_icmp(const struct in_addr *my_ip, const struct in_addr *other) { struct ip_header ip_pkt; struct icmp_ttl_exceeded_header icmp_ttl; struct icmp_echo_header icmp_echo; struct sockaddr_in dst; - char packet[sizeof (struct ip_header) * 2 + - sizeof (struct icmp_ttl_exceeded_header) + - sizeof (struct icmp_echo_header)]; + char packet[sizeof(struct ip_header) * 2 + + sizeof(struct icmp_ttl_exceeded_header) + + sizeof(struct icmp_echo_header)]; size_t off; int err; @@ -351,8 +346,8 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other) off = 0; ip_pkt.vers_ihl = 0x45; ip_pkt.tos = 0; - ip_pkt.pkt_len = htons (sizeof (packet)); - ip_pkt.id = htons (256); + ip_pkt.pkt_len = htons(sizeof(packet)); + ip_pkt.id = htons(256); ip_pkt.flags_frag_offset = 0; ip_pkt.ttl = IPDEFTTL; ip_pkt.proto = IPPROTO_ICMP; @@ -360,24 +355,24 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other) ip_pkt.src_ip = my_ip->s_addr; ip_pkt.dst_ip = other->s_addr; ip_pkt.checksum = - htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); - GNUNET_memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); - off += sizeof (ip_pkt); + htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header))); + GNUNET_memcpy(&packet[off], &ip_pkt, sizeof(struct ip_header)); + off += sizeof(ip_pkt); /* icmp reply: time exceeded */ icmp_ttl.type = ICMP_TIME_EXCEEDED; icmp_ttl.code = 0; icmp_ttl.checksum = 0; icmp_ttl.unused = 0; - GNUNET_memcpy (&packet[off], &icmp_ttl, sizeof (struct icmp_ttl_exceeded_header)); - off += sizeof (struct icmp_ttl_exceeded_header); + GNUNET_memcpy(&packet[off], &icmp_ttl, sizeof(struct icmp_ttl_exceeded_header)); + off += sizeof(struct icmp_ttl_exceeded_header); /* ip header of the presumably 'lost' udp packet */ ip_pkt.vers_ihl = 0x45; ip_pkt.tos = 0; ip_pkt.pkt_len = - htons (sizeof (struct ip_header) + sizeof (struct icmp_echo_header)); - ip_pkt.id = htons (256); + htons(sizeof(struct ip_header) + sizeof(struct icmp_echo_header)); + ip_pkt.id = htons(256); ip_pkt.flags_frag_offset = 0; ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */ ip_pkt.proto = IPPROTO_ICMP; @@ -385,44 +380,44 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other) ip_pkt.dst_ip = dummy.s_addr; ip_pkt.checksum = 0; ip_pkt.checksum = - htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); - GNUNET_memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); - off += sizeof (struct ip_header); + htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header))); + GNUNET_memcpy(&packet[off], &ip_pkt, sizeof(struct ip_header)); + off += sizeof(struct ip_header); icmp_echo.type = ICMP_ECHO; icmp_echo.code = 0; - icmp_echo.reserved = htonl (port); + icmp_echo.reserved = htonl(port); icmp_echo.checksum = 0; icmp_echo.checksum = - htons (calc_checksum - ((uint16_t *) & icmp_echo, sizeof (struct icmp_echo_header))); - GNUNET_memcpy (&packet[off], &icmp_echo, sizeof (struct icmp_echo_header)); + htons(calc_checksum + ((uint16_t *)&icmp_echo, sizeof(struct icmp_echo_header))); + GNUNET_memcpy(&packet[off], &icmp_echo, sizeof(struct icmp_echo_header)); /* no go back to calculate ICMP packet checksum */ - off = sizeof (struct ip_header); + off = sizeof(struct ip_header); icmp_ttl.checksum = - htons (calc_checksum - ((uint16_t *) & packet[off], - sizeof (struct icmp_ttl_exceeded_header) + - sizeof (struct ip_header) + sizeof (struct icmp_echo_header))); - GNUNET_memcpy (&packet[off], &icmp_ttl, sizeof (struct icmp_ttl_exceeded_header)); + htons(calc_checksum + ((uint16_t *)&packet[off], + sizeof(struct icmp_ttl_exceeded_header) + + sizeof(struct ip_header) + sizeof(struct icmp_echo_header))); + GNUNET_memcpy(&packet[off], &icmp_ttl, sizeof(struct icmp_ttl_exceeded_header)); - memset (&dst, 0, sizeof (dst)); + memset(&dst, 0, sizeof(dst)); dst.sin_family = AF_INET; dst.sin_addr = *other; err = - sendto (rawsock, packet, sizeof (packet), 0, (struct sockaddr *) &dst, - sizeof (dst)); + sendto(rawsock, packet, sizeof(packet), 0, (struct sockaddr *)&dst, + sizeof(dst)); if (err < 0) - { - fprintf (stderr, "sendto failed: %s\n", strerror (errno)); - } - else if (sizeof (packet) != (size_t) err) - { - fprintf (stderr, "Error: partial send of ICMP message\n"); - } + { + fprintf(stderr, "sendto failed: %s\n", strerror(errno)); + } + else if (sizeof(packet) != (size_t)err) + { + fprintf(stderr, "Error: partial send of ICMP message\n"); + } } @@ -432,100 +427,102 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other) * @return INVALID_SOCKET on error */ static SOCKET -make_raw_socket () +make_raw_socket() { DWORD bOptVal = TRUE; - int bOptLen = sizeof (bOptVal); + int bOptLen = sizeof(bOptVal); SOCKET ret; - ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW); + ret = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); if (INVALID_SOCKET == ret) - { - fprintf (stderr, "Error opening RAW socket: %s\n", strerror (errno)); - return INVALID_SOCKET; - } + { + fprintf(stderr, "Error opening RAW socket: %s\n", strerror(errno)); + return INVALID_SOCKET; + } if (0 != - setsockopt (ret, SOL_SOCKET, SO_BROADCAST, (char *) &bOptVal, bOptLen)) - { - fprintf (stderr, "Error setting SO_BROADCAST to ON: %s\n", - strerror (errno)); - closesocket (rawsock); - return INVALID_SOCKET; - } - - if (0 != setsockopt (ret, IPPROTO_IP, IP_HDRINCL, (char *) &bOptVal, bOptLen)) - { - fprintf (stderr, "Error setting IP_HDRINCL to ON: %s\n", strerror (errno)); - closesocket (rawsock); - return INVALID_SOCKET; - } + setsockopt(ret, SOL_SOCKET, SO_BROADCAST, (char *)&bOptVal, bOptLen)) + { + fprintf(stderr, "Error setting SO_BROADCAST to ON: %s\n", + strerror(errno)); + closesocket(rawsock); + return INVALID_SOCKET; + } + + if (0 != setsockopt(ret, IPPROTO_IP, IP_HDRINCL, (char *)&bOptVal, bOptLen)) + { + fprintf(stderr, "Error setting IP_HDRINCL to ON: %s\n", strerror(errno)); + closesocket(rawsock); + return INVALID_SOCKET; + } return ret; } int -main (int argc, char *const *argv) +main(int argc, char *const *argv) { struct in_addr external; struct in_addr target; WSADATA wsaData; unsigned int p; - if (argc > 1 && 0 != strcmp (argv[1], "-d")){ + if (argc > 1 && 0 != strcmp(argv[1], "-d")) + { privilege_testing = TRUE; - fprintf (stderr, - "%s", - "DEBUG: Running binary in privilege testing mode."); + fprintf(stderr, + "%s", + "DEBUG: Running binary in privilege testing mode."); argv++; argc--; } if (argc != 4) - { - fprintf (stderr, - "%s", - "This program must be started with our IP, the targets external IP, and our port as arguments.\n"); - return 1; - } - if ((1 != inet_pton (AF_INET, argv[1], &external)) || - (1 != inet_pton (AF_INET, argv[2], &target))) - { - fprintf (stderr, - "Error parsing IPv4 address: %s\n", - strerror (errno)); - return 1; - } - if ((1 != sscanf (argv[3], "%u", &p)) || (0 == p) || (0xFFFF < p)) - { - fprintf (stderr, - "Error parsing port value `%s'\n", - argv[3]); - return 1; - } - port = (uint16_t) p; - - if (0 != WSAStartup (MAKEWORD (2, 1), &wsaData)) - { - fprintf (stderr, - "%s", - "Failed to find Winsock 2.1 or better.\n"); - return 2; - } - if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy)) - { - fprintf (stderr, - "%s", - "Internal error converting dummy IP to binary.\n"); - return 2; - } - if (-1 == (rawsock = make_raw_socket ())) + { + fprintf(stderr, + "%s", + "This program must be started with our IP, the targets external IP, and our port as arguments.\n"); + return 1; + } + if ((1 != inet_pton(AF_INET, argv[1], &external)) || + (1 != inet_pton(AF_INET, argv[2], &target))) + { + fprintf(stderr, + "Error parsing IPv4 address: %s\n", + strerror(errno)); + return 1; + } + if ((1 != sscanf(argv[3], "%u", &p)) || (0 == p) || (0xFFFF < p)) + { + fprintf(stderr, + "Error parsing port value `%s'\n", + argv[3]); + return 1; + } + port = (uint16_t)p; + + if (0 != WSAStartup(MAKEWORD(2, 1), &wsaData)) + { + fprintf(stderr, + "%s", + "Failed to find Winsock 2.1 or better.\n"); + return 2; + } + if (1 != inet_pton(AF_INET, DUMMY_IP, &dummy)) + { + fprintf(stderr, + "%s", + "Internal error converting dummy IP to binary.\n"); + return 2; + } + if (-1 == (rawsock = make_raw_socket())) return 3; - if (!privilege_testing){ - send_icmp (&external, &target); - send_icmp_udp (&external, &target); - } - closesocket (rawsock); - WSACleanup (); + if (!privilege_testing) + { + send_icmp(&external, &target); + send_icmp_udp(&external, &target); + } + closesocket(rawsock); + WSACleanup(); return 0; } diff --git a/src/nat/gnunet-helper-nat-client.c b/src/nat/gnunet-helper-nat-client.c index 54a993452..d5d774964 100644 --- a/src/nat/gnunet-helper-nat-client.c +++ b/src/nat/gnunet-helper-nat-client.c @@ -11,12 +11,12 @@ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . SPDX-License-Identifier: AGPL3.0-or-later -*/ + */ /** * @file src/nat/gnunet-helper-nat-client.c @@ -75,7 +75,7 @@ * @param src source of the copy, may be NULL if @a n is zero * @param n number of bytes to copy */ -#define GNUNET_memcpy(dst,src,n) do { if (0 != n) { (void) memcpy (dst,src,n); } } while (0) +#define GNUNET_memcpy(dst, src, n) do { if (0 != n) { (void)memcpy(dst, src, n); } } while (0) /** * Must match IP given in the server. @@ -92,9 +92,7 @@ /** * IPv4 header. */ -struct ip_header -{ - +struct ip_header { /** * Version (4 bits) + Internet header length (4 bits) */ @@ -149,8 +147,7 @@ struct ip_header /** * Format of ICMP packet. */ -struct icmp_ttl_exceeded_header -{ +struct icmp_ttl_exceeded_header { uint8_t type; uint8_t code; @@ -162,8 +159,7 @@ struct icmp_ttl_exceeded_header /* followed by original payload */ }; -struct icmp_echo_header -{ +struct icmp_echo_header { uint8_t type; uint8_t code; @@ -176,8 +172,7 @@ struct icmp_echo_header /** * Beginning of UDP packet. */ -struct udp_header -{ +struct udp_header { uint16_t src_port; uint16_t dst_port; @@ -211,7 +206,7 @@ static uint16_t port; * @return the CRC 16. */ static uint16_t -calc_checksum (const uint16_t * data, unsigned int bytes) +calc_checksum(const uint16_t * data, unsigned int bytes) { uint32_t sum; unsigned int i; @@ -220,7 +215,7 @@ calc_checksum (const uint16_t * data, unsigned int bytes) for (i = 0; i < bytes / 2; i++) sum += data[i]; sum = (sum & 0xffff) + (sum >> 16); - sum = htons (0xffff - sum); + sum = htons(0xffff - sum); return sum; } @@ -232,11 +227,11 @@ calc_checksum (const uint16_t * data, unsigned int bytes) * @param other target address */ static void -send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other) +send_icmp_udp(const struct in_addr *my_ip, const struct in_addr *other) { - char packet[sizeof (struct ip_header) * 2 + - sizeof (struct icmp_ttl_exceeded_header) + - sizeof (struct udp_header)]; + char packet[sizeof(struct ip_header) * 2 + + sizeof(struct icmp_ttl_exceeded_header) + + sizeof(struct udp_header)]; struct ip_header ip_pkt; struct icmp_ttl_exceeded_header icmp_pkt; struct udp_header udp_pkt; @@ -249,11 +244,11 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other) ip_pkt.vers_ihl = 0x45; ip_pkt.tos = 0; #ifdef FREEBSD - ip_pkt.pkt_len = sizeof (packet); /* Workaround PR kern/21737 */ + ip_pkt.pkt_len = sizeof(packet); /* Workaround PR kern/21737 */ #else - ip_pkt.pkt_len = htons (sizeof (packet)); + ip_pkt.pkt_len = htons(sizeof(packet)); #endif - ip_pkt.id = htons (PACKET_ID); + ip_pkt.id = htons(PACKET_ID); ip_pkt.flags_frag_offset = 0; ip_pkt.ttl = 128; ip_pkt.proto = IPPROTO_ICMP; @@ -261,27 +256,27 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other) ip_pkt.src_ip = my_ip->s_addr; ip_pkt.dst_ip = other->s_addr; ip_pkt.checksum = - htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); - GNUNET_memcpy (&packet[off], - &ip_pkt, - sizeof (struct ip_header)); - off += sizeof (struct ip_header); + htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header))); + GNUNET_memcpy(&packet[off], + &ip_pkt, + sizeof(struct ip_header)); + off += sizeof(struct ip_header); icmp_pkt.type = ICMP_TIME_EXCEEDED; icmp_pkt.code = 0; icmp_pkt.checksum = 0; icmp_pkt.unused = 0; - GNUNET_memcpy (&packet[off], - &icmp_pkt, - sizeof (struct icmp_ttl_exceeded_header)); - off += sizeof (struct icmp_ttl_exceeded_header); + GNUNET_memcpy(&packet[off], + &icmp_pkt, + sizeof(struct icmp_ttl_exceeded_header)); + off += sizeof(struct icmp_ttl_exceeded_header); /* ip header of the presumably 'lost' udp packet */ ip_pkt.vers_ihl = 0x45; ip_pkt.tos = 0; ip_pkt.pkt_len = - htons (sizeof (struct ip_header) + sizeof (struct udp_header)); - ip_pkt.id = htons (0); + htons(sizeof(struct ip_header) + sizeof(struct udp_header)); + ip_pkt.id = htons(0); ip_pkt.flags_frag_offset = 0; ip_pkt.ttl = 128; ip_pkt.proto = IPPROTO_UDP; @@ -289,49 +284,49 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other) ip_pkt.src_ip = other->s_addr; ip_pkt.dst_ip = dummy.s_addr; ip_pkt.checksum = - htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); - GNUNET_memcpy (&packet[off], - &ip_pkt, - sizeof (struct ip_header)); - off += sizeof (struct ip_header); + htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header))); + GNUNET_memcpy(&packet[off], + &ip_pkt, + sizeof(struct ip_header)); + off += sizeof(struct ip_header); /* build UDP header */ - udp_pkt.src_port = htons (NAT_TRAV_PORT); - udp_pkt.dst_port = htons (NAT_TRAV_PORT); - udp_pkt.length = htons (port); + udp_pkt.src_port = htons(NAT_TRAV_PORT); + udp_pkt.dst_port = htons(NAT_TRAV_PORT); + udp_pkt.length = htons(port); udp_pkt.crc = 0; - GNUNET_memcpy (&packet[off], - &udp_pkt, - sizeof (struct udp_header)); - off += sizeof (struct udp_header); + GNUNET_memcpy(&packet[off], + &udp_pkt, + sizeof(struct udp_header)); + off += sizeof(struct udp_header); /* set ICMP checksum */ icmp_pkt.checksum = - htons (calc_checksum - ((uint16_t *) & packet[sizeof (struct ip_header)], - sizeof (struct icmp_ttl_exceeded_header) + - sizeof (struct ip_header) + sizeof (struct udp_header))); - GNUNET_memcpy (&packet[sizeof (struct ip_header)], - &icmp_pkt, - sizeof (struct icmp_ttl_exceeded_header)); - - memset (&dst, 0, sizeof (dst)); + htons(calc_checksum + ((uint16_t *)&packet[sizeof(struct ip_header)], + sizeof(struct icmp_ttl_exceeded_header) + + sizeof(struct ip_header) + sizeof(struct udp_header))); + GNUNET_memcpy(&packet[sizeof(struct ip_header)], + &icmp_pkt, + sizeof(struct icmp_ttl_exceeded_header)); + + memset(&dst, 0, sizeof(dst)); dst.sin_family = AF_INET; #if HAVE_SOCKADDR_IN_SIN_LEN - dst.sin_len = sizeof (struct sockaddr_in); + dst.sin_len = sizeof(struct sockaddr_in); #endif dst.sin_addr = *other; err = - sendto (rawsock, packet, sizeof (packet), 0, (struct sockaddr *) &dst, - sizeof (dst)); + sendto(rawsock, packet, sizeof(packet), 0, (struct sockaddr *)&dst, + sizeof(dst)); if (err < 0) - { - fprintf (stderr, "sendto failed: %s\n", strerror (errno)); - } - else if (sizeof (packet) != (size_t) err) - { - fprintf (stderr, "Error: partial send of ICMP message with size %lu\n", (unsigned long) off); - } + { + fprintf(stderr, "sendto failed: %s\n", strerror(errno)); + } + else if (sizeof(packet) != (size_t)err) + { + fprintf(stderr, "Error: partial send of ICMP message with size %lu\n", (unsigned long)off); + } } @@ -342,15 +337,15 @@ send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other) * @param other target address */ static void -send_icmp (const struct in_addr *my_ip, const struct in_addr *other) +send_icmp(const struct in_addr *my_ip, const struct in_addr *other) { struct ip_header ip_pkt; struct icmp_ttl_exceeded_header icmp_ttl; struct icmp_echo_header icmp_echo; struct sockaddr_in dst; - char packet[sizeof (struct ip_header) * 2 + - sizeof (struct icmp_ttl_exceeded_header) + - sizeof (struct icmp_echo_header)]; + char packet[sizeof(struct ip_header) * 2 + + sizeof(struct icmp_ttl_exceeded_header) + + sizeof(struct icmp_echo_header)]; size_t off; int err; @@ -359,11 +354,11 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other) ip_pkt.vers_ihl = 0x45; ip_pkt.tos = 0; #ifdef FREEBSD - ip_pkt.pkt_len = sizeof (packet); /* Workaround PR kern/21737 */ + ip_pkt.pkt_len = sizeof(packet); /* Workaround PR kern/21737 */ #else - ip_pkt.pkt_len = htons (sizeof (packet)); + ip_pkt.pkt_len = htons(sizeof(packet)); #endif - ip_pkt.id = htons (PACKET_ID); + ip_pkt.id = htons(PACKET_ID); ip_pkt.flags_frag_offset = 0; ip_pkt.ttl = IPDEFTTL; ip_pkt.proto = IPPROTO_ICMP; @@ -371,28 +366,28 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other) ip_pkt.src_ip = my_ip->s_addr; ip_pkt.dst_ip = other->s_addr; ip_pkt.checksum = - htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); - GNUNET_memcpy (&packet[off], - &ip_pkt, - sizeof (struct ip_header)); - off = sizeof (ip_pkt); + htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header))); + GNUNET_memcpy(&packet[off], + &ip_pkt, + sizeof(struct ip_header)); + off = sizeof(ip_pkt); /* icmp reply: time exceeded */ icmp_ttl.type = ICMP_TIME_EXCEEDED; icmp_ttl.code = 0; icmp_ttl.checksum = 0; icmp_ttl.unused = 0; - GNUNET_memcpy (&packet[off], - &icmp_ttl, - sizeof (struct icmp_ttl_exceeded_header)); - off += sizeof (struct icmp_ttl_exceeded_header); + GNUNET_memcpy(&packet[off], + &icmp_ttl, + sizeof(struct icmp_ttl_exceeded_header)); + off += sizeof(struct icmp_ttl_exceeded_header); /* ip header of the presumably 'lost' udp packet */ ip_pkt.vers_ihl = 0x45; ip_pkt.tos = 0; ip_pkt.pkt_len = - htons (sizeof (struct ip_header) + sizeof (struct icmp_echo_header)); - ip_pkt.id = htons (PACKET_ID); + htons(sizeof(struct ip_header) + sizeof(struct icmp_echo_header)); + ip_pkt.id = htons(PACKET_ID); ip_pkt.flags_frag_offset = 0; ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */ ip_pkt.proto = IPPROTO_ICMP; @@ -400,57 +395,57 @@ send_icmp (const struct in_addr *my_ip, const struct in_addr *other) ip_pkt.dst_ip = dummy.s_addr; ip_pkt.checksum = 0; ip_pkt.checksum = - htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); - GNUNET_memcpy (&packet[off], - &ip_pkt, - sizeof (struct ip_header)); - off += sizeof (struct ip_header); + htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header))); + GNUNET_memcpy(&packet[off], + &ip_pkt, + sizeof(struct ip_header)); + off += sizeof(struct ip_header); icmp_echo.type = ICMP_ECHO; icmp_echo.code = 0; - icmp_echo.reserved = htonl (port); + icmp_echo.reserved = htonl(port); icmp_echo.checksum = 0; icmp_echo.checksum = - htons (calc_checksum - ((uint16_t *) &icmp_echo, sizeof (struct icmp_echo_header))); - GNUNET_memcpy (&packet[off], - &icmp_echo, - sizeof (struct icmp_echo_header)); + htons(calc_checksum + ((uint16_t *)&icmp_echo, sizeof(struct icmp_echo_header))); + GNUNET_memcpy(&packet[off], + &icmp_echo, + sizeof(struct icmp_echo_header)); /* no go back to calculate ICMP packet checksum */ - off = sizeof (struct ip_header); + off = sizeof(struct ip_header); icmp_ttl.checksum = - htons (calc_checksum - ((uint16_t *) & packet[off], - sizeof (struct icmp_ttl_exceeded_header) + - sizeof (struct ip_header) + sizeof (struct icmp_echo_header))); - GNUNET_memcpy (&packet[off], - &icmp_ttl, - sizeof (struct icmp_ttl_exceeded_header)); + htons(calc_checksum + ((uint16_t *)&packet[off], + sizeof(struct icmp_ttl_exceeded_header) + + sizeof(struct ip_header) + sizeof(struct icmp_echo_header))); + GNUNET_memcpy(&packet[off], + &icmp_ttl, + sizeof(struct icmp_ttl_exceeded_header)); /* prepare for transmission */ - memset (&dst, 0, sizeof (dst)); + memset(&dst, 0, sizeof(dst)); dst.sin_family = AF_INET; #if HAVE_SOCKADDR_IN_SIN_LEN - dst.sin_len = sizeof (struct sockaddr_in); + dst.sin_len = sizeof(struct sockaddr_in); #endif dst.sin_addr = *other; err = - sendto (rawsock, packet, sizeof (packet), 0, (struct sockaddr *) &dst, - sizeof (dst)); + sendto(rawsock, packet, sizeof(packet), 0, (struct sockaddr *)&dst, + sizeof(dst)); if (err < 0) - { - fprintf (stderr, "sendto failed: %s\n", strerror (errno)); - } - else if (sizeof (packet) != (size_t) err) - { - fprintf (stderr, "Error: partial send of ICMP message\n"); - } + { + fprintf(stderr, "sendto failed: %s\n", strerror(errno)); + } + else if (sizeof(packet) != (size_t)err) + { + fprintf(stderr, "Error: partial send of ICMP message\n"); + } } int -main (int argc, char *const *argv) +main(int argc, char *const *argv) { const int one = 1; struct in_addr external; @@ -461,80 +456,80 @@ main (int argc, char *const *argv) int global_ret; /* Create an ICMP raw socket for writing (only operation that requires root) */ - rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_RAW); + rawsock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); raw_eno = errno; /* for later error checking */ /* now drop root privileges */ - uid = getuid (); + uid = getuid(); #ifdef HAVE_SETRESUID - if (0 != setresuid (uid, uid, uid)) - { - fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno)); - global_ret = 1; - goto cleanup; - } + if (0 != setresuid(uid, uid, uid)) + { + fprintf(stderr, "Failed to setresuid: %s\n", strerror(errno)); + global_ret = 1; + goto cleanup; + } #else - if (0 != (setuid (uid) | seteuid (uid))) - { - fprintf (stderr, "Failed to setuid: %s\n", strerror (errno)); - global_ret = 2; - goto cleanup; - } + if (0 != (setuid(uid) | seteuid(uid))) + { + fprintf(stderr, "Failed to setuid: %s\n", strerror(errno)); + global_ret = 2; + goto cleanup; + } #endif if (-1 == rawsock) - { - fprintf (stderr, "Error opening RAW socket: %s\n", strerror (raw_eno)); - global_ret = 3; - goto cleanup; - } + { + fprintf(stderr, "Error opening RAW socket: %s\n", strerror(raw_eno)); + global_ret = 3; + goto cleanup; + } if (0 != - setsockopt (rawsock, SOL_SOCKET, SO_BROADCAST, (char *) &one, sizeof (one))) - { - fprintf (stderr, "setsockopt failed: %s\n", strerror (errno)); - global_ret = 4; - goto cleanup; - } + setsockopt(rawsock, SOL_SOCKET, SO_BROADCAST, (char *)&one, sizeof(one))) + { + fprintf(stderr, "setsockopt failed: %s\n", strerror(errno)); + global_ret = 4; + goto cleanup; + } if (0 != - setsockopt (rawsock, IPPROTO_IP, IP_HDRINCL, (char *) &one, sizeof (one))) - { - fprintf (stderr, "setsockopt failed: %s\n", strerror (errno)); - global_ret = 5; - goto cleanup; - } + setsockopt(rawsock, IPPROTO_IP, IP_HDRINCL, (char *)&one, sizeof(one))) + { + fprintf(stderr, "setsockopt failed: %s\n", strerror(errno)); + global_ret = 5; + goto cleanup; + } if (4 != argc) - { - fprintf (stderr, - "This program must be started with our IP, the targets external IP, and our port as arguments.\n"); - global_ret = 6; - goto cleanup; - } - if ((1 != inet_pton (AF_INET, argv[1], &external)) || - (1 != inet_pton (AF_INET, argv[2], &target))) - { - fprintf (stderr, "Error parsing IPv4 address: %s\n", strerror (errno)); - global_ret = 7; - goto cleanup; - } - if ((1 != sscanf (argv[3], "%u", &p)) || (0 == p) || (0xFFFF < p)) - { - fprintf (stderr, "Error parsing port value `%s'\n", argv[3]); - global_ret = 8; - goto cleanup; - } - port = (uint16_t) p; - if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy)) - { - fprintf (stderr, "Internal error converting dummy IP to binary.\n"); - global_ret = 9; - goto cleanup; - } - send_icmp (&external, &target); - send_icmp_udp (&external, &target); + { + fprintf(stderr, + "This program must be started with our IP, the targets external IP, and our port as arguments.\n"); + global_ret = 6; + goto cleanup; + } + if ((1 != inet_pton(AF_INET, argv[1], &external)) || + (1 != inet_pton(AF_INET, argv[2], &target))) + { + fprintf(stderr, "Error parsing IPv4 address: %s\n", strerror(errno)); + global_ret = 7; + goto cleanup; + } + if ((1 != sscanf(argv[3], "%u", &p)) || (0 == p) || (0xFFFF < p)) + { + fprintf(stderr, "Error parsing port value `%s'\n", argv[3]); + global_ret = 8; + goto cleanup; + } + port = (uint16_t)p; + if (1 != inet_pton(AF_INET, DUMMY_IP, &dummy)) + { + fprintf(stderr, "Internal error converting dummy IP to binary.\n"); + global_ret = 9; + goto cleanup; + } + send_icmp(&external, &target); + send_icmp_udp(&external, &target); global_ret = 0; - cleanup: +cleanup: if (-1 != rawsock) - (void) close (rawsock); + (void)close(rawsock); return global_ret; } diff --git a/src/nat/gnunet-helper-nat-server-windows.c b/src/nat/gnunet-helper-nat-server-windows.c index f1b4896e8..13a4d8e6f 100644 --- a/src/nat/gnunet-helper-nat-server-windows.c +++ b/src/nat/gnunet-helper-nat-server-windows.c @@ -11,12 +11,12 @@ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . SPDX-License-Identifier: AGPL3.0-or-later -*/ + */ /** * @file src/nat/gnunet-helper-nat-server-windows.c @@ -42,7 +42,7 @@ */ #define _GNU_SOURCE /* Instead of including gnunet_common.h */ -#define GNUNET_memcpy(dst,src,n) do { if (0 != n) { (void) memcpy (dst,src,n); } } while (0) +#define GNUNET_memcpy(dst, src, n) do { if (0 != n) { (void)memcpy(dst, src, n); } } while (0) #define FD_SETSIZE 1024 #include @@ -94,9 +94,7 @@ /** * IPv4 header. */ -struct ip_header -{ - +struct ip_header { /** * Version (4 bits) + Internet header length (4 bits) */ @@ -151,8 +149,7 @@ struct ip_header /** * Format of ICMP packet. */ -struct icmp_ttl_exceeded_header -{ +struct icmp_ttl_exceeded_header { uint8_t type; uint8_t code; @@ -164,8 +161,7 @@ struct icmp_ttl_exceeded_header /* followed by original payload */ }; -struct icmp_echo_header -{ +struct icmp_echo_header { uint8_t type; uint8_t code; @@ -178,8 +174,7 @@ struct icmp_echo_header /** * Beginning of UDP packet. */ -struct udp_header -{ +struct udp_header { uint16_t src_port; uint16_t dst_port; @@ -223,7 +218,7 @@ static struct in_addr dummy; * @return the CRC 16. */ static uint16_t -calc_checksum (const uint16_t * data, unsigned int bytes) +calc_checksum(const uint16_t * data, unsigned int bytes) { uint32_t sum; unsigned int i; @@ -232,7 +227,7 @@ calc_checksum (const uint16_t * data, unsigned int bytes) for (i = 0; i < bytes / 2; i++) sum += data[i]; sum = (sum & 0xffff) + (sum >> 16); - sum = htons (0xffff - sum); + sum = htons(0xffff - sum); return sum; } @@ -246,14 +241,14 @@ calc_checksum (const uint16_t * data, unsigned int bytes) * @return 1 on success */ static int -inet_pton (int af, const char *cp, struct in_addr *buf) +inet_pton(int af, const char *cp, struct in_addr *buf) { - buf->s_addr = inet_addr (cp); + buf->s_addr = inet_addr(cp); if (buf->s_addr == INADDR_NONE) - { - fprintf (stderr, "Error %d handling address %s", WSAGetLastError (), cp); - return 0; - } + { + fprintf(stderr, "Error %d handling address %s", WSAGetLastError(), cp); + return 0; + } return 1; } @@ -264,9 +259,9 @@ inet_pton (int af, const char *cp, struct in_addr *buf) * @param my_ip source address (our ip address) */ static void -send_icmp_echo (const struct in_addr *my_ip) +send_icmp_echo(const struct in_addr *my_ip) { - char packet[sizeof (struct ip_header) + sizeof (struct icmp_echo_header)]; + char packet[sizeof(struct ip_header) + sizeof(struct icmp_echo_header)]; struct icmp_echo_header icmp_echo; struct ip_header ip_pkt; struct sockaddr_in dst; @@ -276,8 +271,8 @@ send_icmp_echo (const struct in_addr *my_ip) off = 0; ip_pkt.vers_ihl = 0x45; ip_pkt.tos = 0; - ip_pkt.pkt_len = htons (sizeof (packet)); - ip_pkt.id = htons (PACKET_ID); + ip_pkt.pkt_len = htons(sizeof(packet)); + ip_pkt.id = htons(PACKET_ID); ip_pkt.flags_frag_offset = 0; ip_pkt.ttl = IPDEFTTL; ip_pkt.proto = IPPROTO_ICMP; @@ -285,35 +280,35 @@ send_icmp_echo (const struct in_addr *my_ip) ip_pkt.src_ip = my_ip->s_addr; ip_pkt.dst_ip = dummy.s_addr; ip_pkt.checksum = - htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header))); - GNUNET_memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header)); - off += sizeof (struct ip_header); + htons(calc_checksum((uint16_t *)&ip_pkt, sizeof(struct ip_header))); + GNUNET_memcpy(&packet[off], &ip_pkt, sizeof(struct ip_header)); + off += sizeof(struct ip_header); icmp_echo.type = ICMP_ECHO; icmp_echo.code = 0; icmp_echo.reserved = 0; icmp_echo.checksum = 0; icmp_echo.checksum = - htons (calc_checksum - ((uint16_t *) & icmp_echo, sizeof (struct icmp_echo_header))); - GNUNET_memcpy (&packet[off], &icmp_echo, sizeof (struct icmp_echo_header)); - off += sizeof (struct icmp_echo_header); + htons(calc_checksum + ((uint16_t *)&icmp_echo, sizeof(struct icmp_echo_header))); + GNUNET_memcpy(&packet[off], &icmp_echo, sizeof(struct icmp_echo_header)); + off += sizeof(struct icmp_echo_header); - memset (&dst, 0, sizeof (dst)); + memset(&dst, 0, sizeof(dst)); dst.sin_family = AF_INET; dst.sin_addr = dummy; err = - sendto (rawsock, packet, off, 0, (struct sockaddr *) &dst, sizeof (dst)); + sendto(rawsock, packet, off, 0, (struct sockaddr *)&dst, sizeof(dst)); if (err < 0) - { + { #if VERBOSE - fprintf (stderr, "sendto failed: %s\n", strerror (errno)); + fprintf(stderr, "sendto failed: %s\n", strerror(errno)); #endif - } + } else if (err != off) - { - fprintf (stderr, "Error: partial send of ICMP message\n"); - } + { + fprintf(stderr, "Error: partial send of ICMP message\n"); + } } @@ -321,26 +316,26 @@ send_icmp_echo (const struct in_addr *my_ip) * Send a UDP message to the dummy IP. */ static void -send_udp () +send_udp() { struct sockaddr_in dst; ssize_t err; - memset (&dst, 0, sizeof (dst)); + memset(&dst, 0, sizeof(dst)); dst.sin_family = AF_INET; dst.sin_addr = dummy; - dst.sin_port = htons (NAT_TRAV_PORT); - err = sendto (udpsock, NULL, 0, 0, (struct sockaddr *) &dst, sizeof (dst)); + dst.sin_port = htons(NAT_TRAV_PORT); + err = sendto(udpsock, NULL, 0, 0, (struct sockaddr *)&dst, sizeof(dst)); if (err < 0) - { + { #if VERBOSE - fprintf (stderr, "sendto failed: %s\n", strerror (errno)); + fprintf(stderr, "sendto failed: %s\n", strerror(errno)); #endif - } + } else if (0 != err) - { - fprintf (stderr, "Error: partial send of ICMP message\n"); - } + { + fprintf(stderr, "Error: partial send of ICMP message\n"); + } } @@ -348,7 +343,7 @@ send_udp () * We've received an ICMP response. Process it. */ static void -process_icmp_response () +process_icmp_response() { char buf[65536]; ssize_t have; @@ -361,78 +356,80 @@ process_icmp_response () uint16_t port; DWORD ssize; - have = read (icmpsock, buf, sizeof (buf)); + have = read(icmpsock, buf, sizeof(buf)); if (have == -1) - { - fprintf (stderr, "Error reading raw socket: %s\n", strerror (errno)); - return; - } + { + fprintf(stderr, "Error reading raw socket: %s\n", strerror(errno)); + return; + } #if VERBOSE - fprintf (stderr, "Received message of %u bytes\n", (unsigned int) have); + fprintf(stderr, "Received message of %u bytes\n", (unsigned int)have); #endif if (have < - (ssize_t) (sizeof (struct ip_header) + - sizeof (struct icmp_ttl_exceeded_header) + - sizeof (struct ip_header))) - { - /* malformed */ - return; - } + (ssize_t)(sizeof(struct ip_header) + + sizeof(struct icmp_ttl_exceeded_header) + + sizeof(struct ip_header))) + { + /* malformed */ + return; + } off = 0; - GNUNET_memcpy (&ip_pkt, &buf[off], sizeof (struct ip_header)); - off += sizeof (struct ip_header); - GNUNET_memcpy (&source_ip, &ip_pkt.src_ip, sizeof (source_ip)); - GNUNET_memcpy (&icmp_ttl, &buf[off], sizeof (struct icmp_ttl_exceeded_header)); - off += sizeof (struct icmp_ttl_exceeded_header); + GNUNET_memcpy(&ip_pkt, &buf[off], sizeof(struct ip_header)); + off += sizeof(struct ip_header); + GNUNET_memcpy(&source_ip, &ip_pkt.src_ip, sizeof(source_ip)); + GNUNET_memcpy(&icmp_ttl, &buf[off], sizeof(struct icmp_ttl_exceeded_header)); + off += sizeof(struct icmp_ttl_exceeded_header); if ((ICMP_TIME_EXCEEDED != icmp_ttl.type) || (0 != icmp_ttl.code)) - { - /* different type than what we want */ - return; - } - /* skip 2nd IP header */ - GNUNET_memcpy (&ip_pkt, &buf[off], sizeof (struct ip_header)); - off += sizeof (struct ip_header); - - switch (ip_pkt.proto) - { - case IPPROTO_ICMP: - if (have != - (sizeof (struct ip_header) * 2 + - sizeof (struct icmp_ttl_exceeded_header) + - sizeof (struct icmp_echo_header))) { - /* malformed */ + /* different type than what we want */ return; } - /* grab ICMP ECHO content */ - GNUNET_memcpy (&icmp_echo, &buf[off], sizeof (struct icmp_echo_header)); - port = (uint16_t) ntohl (icmp_echo.reserved); - break; - case IPPROTO_UDP: - if (have != - (sizeof (struct ip_header) * 2 + - sizeof (struct icmp_ttl_exceeded_header) + sizeof (struct udp_header))) + /* skip 2nd IP header */ + GNUNET_memcpy(&ip_pkt, &buf[off], sizeof(struct ip_header)); + off += sizeof(struct ip_header); + + switch (ip_pkt.proto) { - /* malformed */ + case IPPROTO_ICMP: + if (have != + (sizeof(struct ip_header) * 2 + + sizeof(struct icmp_ttl_exceeded_header) + + sizeof(struct icmp_echo_header))) + { + /* malformed */ + return; + } + /* grab ICMP ECHO content */ + GNUNET_memcpy(&icmp_echo, &buf[off], sizeof(struct icmp_echo_header)); + port = (uint16_t)ntohl(icmp_echo.reserved); + break; + + case IPPROTO_UDP: + if (have != + (sizeof(struct ip_header) * 2 + + sizeof(struct icmp_ttl_exceeded_header) + sizeof(struct udp_header))) + { + /* malformed */ + return; + } + /* grab UDP content */ + GNUNET_memcpy(&udp_pkt, &buf[off], sizeof(struct udp_header)); + port = ntohs(udp_pkt.length); + break; + + default: + /* different type than what we want */ return; } - /* grab UDP content */ - GNUNET_memcpy (&udp_pkt, &buf[off], sizeof (struct udp_header)); - port = ntohs (udp_pkt.length); - break; - default: - /* different type than what we want */ - return; - } - - ssize = sizeof (buf); - WSAAddressToString ((LPSOCKADDR) & source_ip, sizeof (source_ip), NULL, buf, - &ssize); + + ssize = sizeof(buf); + WSAAddressToString((LPSOCKADDR)&source_ip, sizeof(source_ip), NULL, buf, + &ssize); if (port == 0) - fprintf (stdout, "%s\n", buf); + fprintf(stdout, "%s\n", buf); else - fprintf (stdout, "%s:%u\n", buf, (unsigned int) port); - fflush (stdout); + fprintf(stdout, "%s:%u\n", buf, (unsigned int)port); + fflush(stdout); } @@ -442,16 +439,16 @@ process_icmp_response () * @return INVALID_SOCKET on error */ static SOCKET -make_icmp_socket () +make_icmp_socket() { SOCKET ret; - ret = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP); + ret = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); if (INVALID_SOCKET == ret) - { - fprintf (stderr, "Error opening RAW socket: %s\n", strerror (errno)); - return INVALID_SOCKET; - } + { + fprintf(stderr, "Error opening RAW socket: %s\n", strerror(errno)); + return INVALID_SOCKET; + } return ret; } @@ -462,34 +459,34 @@ make_icmp_socket () * @return INVALID_SOCKET on error */ static SOCKET -make_raw_socket () +make_raw_socket() { DWORD bOptVal = TRUE; - int bOptLen = sizeof (bOptVal); + int bOptLen = sizeof(bOptVal); - rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP); + rawsock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); if (INVALID_SOCKET == rawsock) - { - fprintf (stderr, "Error opening RAW socket: %s\n", strerror (errno)); - return INVALID_SOCKET; - } + { + fprintf(stderr, "Error opening RAW socket: %s\n", strerror(errno)); + return INVALID_SOCKET; + } if (0 != - setsockopt (rawsock, SOL_SOCKET, SO_BROADCAST, (char *) &bOptVal, - bOptLen)) - { - fprintf (stderr, "Error setting SO_BROADCAST to ON: %s\n", - strerror (errno)); - closesocket (rawsock); - return INVALID_SOCKET; - } + setsockopt(rawsock, SOL_SOCKET, SO_BROADCAST, (char *)&bOptVal, + bOptLen)) + { + fprintf(stderr, "Error setting SO_BROADCAST to ON: %s\n", + strerror(errno)); + closesocket(rawsock); + return INVALID_SOCKET; + } if (0 != - setsockopt (rawsock, IPPROTO_IP, IP_HDRINCL, (char *) &bOptVal, bOptLen)) - { - fprintf (stderr, "Error setting IP_HDRINCL to ON: %s\n", strerror (errno)); - closesocket (rawsock); - return INVALID_SOCKET; - } + setsockopt(rawsock, IPPROTO_IP, IP_HDRINCL, (char *)&bOptVal, bOptLen)) + { + fprintf(stderr, "Error setting IP_HDRINCL to ON: %s\n", strerror(errno)); + closesocket(rawsock); + return INVALID_SOCKET; + } return rawsock; } @@ -501,33 +498,33 @@ make_raw_socket () * @return INVALID_SOCKET on error */ static SOCKET -make_udp_socket (const struct in_addr *my_ip) +make_udp_socket(const struct in_addr *my_ip) { SOCKET ret; struct sockaddr_in addr; - ret = socket (AF_INET, SOCK_DGRAM, 0); + ret = socket(AF_INET, SOCK_DGRAM, 0); if (INVALID_SOCKET == ret) - { - fprintf (stderr, "Error opening UDP socket: %s\n", strerror (errno)); - return INVALID_SOCKET; - } - memset (&addr, 0, sizeof (addr)); + { + fprintf(stderr, "Error opening UDP socket: %s\n", strerror(errno)); + return INVALID_SOCKET; + } + memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr = *my_ip; - addr.sin_port = htons (NAT_TRAV_PORT); - if (0 != bind (ret, (struct sockaddr *) &addr, sizeof (addr))) - { - fprintf (stderr, "Error binding UDP socket to port %u: %s\n", NAT_TRAV_PORT, - strerror (errno)); - /* likely problematic, but not certain, try to continue */ - } + addr.sin_port = htons(NAT_TRAV_PORT); + if (0 != bind(ret, (struct sockaddr *)&addr, sizeof(addr))) + { + fprintf(stderr, "Error binding UDP socket to port %u: %s\n", NAT_TRAV_PORT, + strerror(errno)); + /* likely problematic, but not certain, try to continue */ + } return ret; } int -main (int argc, char *const *argv) +main(int argc, char *const *argv) { struct in_addr external; fd_set rs; @@ -535,79 +532,79 @@ main (int argc, char *const *argv) WSADATA wsaData; unsigned int alt = 0; - if ( (argc > 1) && (0 != strcmp (argv[1], "-d"))) - { - privilege_testing = TRUE; - fprintf (stderr, - "%s", - "DEBUG: Running binary in privilege testing mode."); - argv++; - argc--; - } + if ((argc > 1) && (0 != strcmp(argv[1], "-d"))) + { + privilege_testing = TRUE; + fprintf(stderr, + "%s", + "DEBUG: Running binary in privilege testing mode."); + argv++; + argc--; + } if (2 != argc) - { - fprintf (stderr, - "This program must be started with our (internal NAT) IP as the only argument.\n"); - return 1; - } - if (1 != inet_pton (AF_INET, argv[1], &external)) - { - fprintf (stderr, "Error parsing IPv4 address: %s, error %s\n", argv[1], - strerror (errno)); - return 1; - } - if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy)) - { - fprintf (stderr, "Internal error converting dummy IP to binary.\n"); - return 2; - } - if (WSAStartup (MAKEWORD (2, 1), &wsaData) != 0) - { - fprintf (stderr, "Failed to find Winsock 2.1 or better.\n"); - return 2; - } - if (INVALID_SOCKET == (icmpsock = make_icmp_socket ())) - { - return 3; - } - if (INVALID_SOCKET == (make_raw_socket ())) - { - closesocket (icmpsock); - return 3; - } - if (INVALID_SOCKET == (udpsock = make_udp_socket (&external))) - { - closesocket (icmpsock); - closesocket (rawsock); - return 3; - } - - while ( ! privilege_testing) - { - FD_ZERO (&rs); - FD_SET (icmpsock, &rs); - tv.tv_sec = 0; - tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000; - if (-1 == select (icmpsock + 1, &rs, NULL, NULL, &tv)) { - if (errno == EINTR) - continue; - fprintf (stderr, "select failed: %s\n", strerror (errno)); - break; + fprintf(stderr, + "This program must be started with our (internal NAT) IP as the only argument.\n"); + return 1; + } + if (1 != inet_pton(AF_INET, argv[1], &external)) + { + fprintf(stderr, "Error parsing IPv4 address: %s, error %s\n", argv[1], + strerror(errno)); + return 1; + } + if (1 != inet_pton(AF_INET, DUMMY_IP, &dummy)) + { + fprintf(stderr, "Internal error converting dummy IP to binary.\n"); + return 2; + } + if (WSAStartup(MAKEWORD(2, 1), &wsaData) != 0) + { + fprintf(stderr, "Failed to find Winsock 2.1 or better.\n"); + return 2; + } + if (INVALID_SOCKET == (icmpsock = make_icmp_socket())) + { + return 3; + } + if (INVALID_SOCKET == (make_raw_socket())) + { + closesocket(icmpsock); + return 3; + } + if (INVALID_SOCKET == (udpsock = make_udp_socket(&external))) + { + closesocket(icmpsock); + closesocket(rawsock); + return 3; + } + + while (!privilege_testing) + { + FD_ZERO(&rs); + FD_SET(icmpsock, &rs); + tv.tv_sec = 0; + tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000; + if (-1 == select(icmpsock + 1, &rs, NULL, NULL, &tv)) + { + if (errno == EINTR) + continue; + fprintf(stderr, "select failed: %s\n", strerror(errno)); + break; + } + if (FD_ISSET(icmpsock, &rs)) + process_icmp_response(); + if (0 == (++alt % 2)) + send_icmp_echo(&external); + else + send_udp(); } - if (FD_ISSET (icmpsock, &rs)) - process_icmp_response (); - if (0 == (++alt % 2)) - send_icmp_echo (&external); - else - send_udp (); - } /* select failed (internal error or OS out of resources) */ - closesocket (icmpsock); - closesocket (rawsock); - closesocket (udpsock); - WSACleanup (); + closesocket(icmpsock); + closesocket(rawsock); + closesocket(udpsock); + WSACleanup(); if (privilege_testing) return 0; return 4; diff --git a/src/nat/gnunet-helper-nat-server.c b/src/nat/gnunet-helper-nat-server.c index 78b926037..f087590c8 100644 --- a/src/nat/gnunet-helper-nat-server.c +++ b/src/nat/gnunet-helper-nat-server.c @@ -11,12 +11,12 @@ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . SPDX-License-Identifier: AGPL3.0-or-later -*/ + */ /** * @file src/nat/gnunet-helper-nat-server.c @@ -79,7 +79,7 @@ * @param src source of the copy, may be NULL if @a n is zero * @param n number of bytes to copy */ -#define GNUNET_memcpy(dst,src,n) do { if (0 != n) { (void) memcpy (dst,src,n); } } while (0) +#define GNUNET_memcpy(dst, src, n) do { if (0 != n) { (void)memcpy(dst, src, n); } } while (0) /** * Should we print some debug output? @@ -109,9 +109,7 @@ /** * IPv4 header. */ -struct ip_header -{ - +struct ip_header { /** * Version (4 bits) + Internet header length (4 bits) */ @@ -166,8 +164,7 @@ struct ip_header /** * Format of ICMP packet. */ -struct icmp_ttl_exceeded_header -{ +struct icmp_ttl_exceeded_header { uint8_t type; uint8_t code; @@ -179,8 +176,7 @@ struct icmp_ttl_exceeded_header /* followed by original payload */ }; -struct icmp_echo_header -{ +struct icmp_echo_header { uint8_t type; uint8_t code; @@ -194,8 +190,7 @@ struct icmp_echo_header /** * Beginning of UDP packet. */ -struct udp_header -{ +struct udp_header { uint16_t src_port; uint16_t dst_port; @@ -234,7 +229,7 @@ static struct in_addr dummy; * @return the CRC 16. */ static uint16_t -calc_checksum (const uint16_t * data, unsigned int bytes) +calc_checksum(const uint16_t * data, unsigned int bytes) { uint32_t sum; unsigned int i; @@ -243,7 +238,7 @@ calc_checksum (const uint16_t * data, unsigned int bytes) for (i = 0; i < bytes / 2; i++) sum += data[i]; sum = (sum & 0xffff) + (sum >> 16); - sum = htons (0xffff - sum); + sum = htons(0xffff - sum); return sum; } @@ -254,9 +249,9 @@ calc_checksum (const uint16_t * data, unsigned int bytes) * @param my_ip source address (our ip address) */ static void -send_icmp_echo (const struct in_addr *my_ip) +send_icmp_echo(const struct in_addr *my_ip) { - char packet[sizeof (struct ip_header) + sizeof (struct icmp_echo_header)]; + char packet[sizeof(struct ip_header) + sizeof(struct icmp_echo_header)]; struct icmp_echo_header icmp_echo; struct ip_header ip_pkt; struct sockaddr_in dst; @@ -266,8 +261,8 @@ send_icmp_echo (const struct in_addr *my_ip) off = 0; ip_pkt.vers_ihl = 0x45; ip_pkt.tos = 0; - ip_pkt.pkt_len = htons (sizeof (packet)); - ip_pkt.id = htons (PACKET_ID); + ip_pkt.pkt_len = htons(sizeof(packet)); + ip_pkt.id = htons(PACKET_ID); ip_pkt.flags_frag_offset = 0; ip_pkt.ttl = IPDEFTTL; ip_pkt.proto = IPPROTO_ICMP; @@ -275,51 +270,51 @@ send_icmp_echo (const struct in_addr *my_ip) ip_pkt.src_ip = my_ip->s_addr; ip_pkt.dst_ip = dummy.s_addr; ip_pkt.checksum = - htons (calc_checksum ((uint16_t *) & ip_pkt, - sizeof (struct ip_header))); - GNUNET_memcpy (&packet[off], - &ip_pkt, - sizeof (struct ip_header)); - off += sizeof (struct ip_header); + htons(calc_checksum((uint16_t *)&ip_pkt, + sizeof(struct ip_header))); + GNUNET_memcpy(&packet[off], + &ip_pkt, + sizeof(struct ip_header)); + off += sizeof(struct ip_header); icmp_echo.type = ICMP_ECHO; icmp_echo.code = 0; icmp_echo.checksum = 0; icmp_echo.reserved = 0; icmp_echo.checksum = - htons (calc_checksum - ((uint16_t *) & icmp_echo, - sizeof (struct icmp_echo_header))); - GNUNET_memcpy (&packet[off], - &icmp_echo, - sizeof (struct icmp_echo_header)); - off += sizeof (struct icmp_echo_header); - - memset (&dst, 0, sizeof (dst)); + htons(calc_checksum + ((uint16_t *)&icmp_echo, + sizeof(struct icmp_echo_header))); + GNUNET_memcpy(&packet[off], + &icmp_echo, + sizeof(struct icmp_echo_header)); + off += sizeof(struct icmp_echo_header); + + memset(&dst, 0, sizeof(dst)); dst.sin_family = AF_INET; #if HAVE_SOCKADDR_IN_SIN_LEN - dst.sin_len = sizeof (struct sockaddr_in); + dst.sin_len = sizeof(struct sockaddr_in); #endif dst.sin_addr = dummy; - err = sendto (rawsock, - packet, - off, - 0, - (struct sockaddr *) &dst, - sizeof (dst)); + err = sendto(rawsock, + packet, + off, + 0, + (struct sockaddr *)&dst, + sizeof(dst)); if (err < 0) - { + { #if VERBOSE - fprintf (stderr, - "sendto failed: %s\n", - strerror (errno)); + fprintf(stderr, + "sendto failed: %s\n", + strerror(errno)); #endif - } - else if (sizeof (packet) != err) - { - fprintf (stderr, - "Error: partial send of ICMP message\n"); - } + } + else if (sizeof(packet) != err) + { + fprintf(stderr, + "Error: partial send of ICMP message\n"); + } } @@ -327,37 +322,37 @@ send_icmp_echo (const struct in_addr *my_ip) * Send a UDP message to the dummy IP. */ static void -send_udp () +send_udp() { struct sockaddr_in dst; ssize_t err; - memset (&dst, 0, sizeof (dst)); + memset(&dst, 0, sizeof(dst)); dst.sin_family = AF_INET; #if HAVE_SOCKADDR_IN_SIN_LEN - dst.sin_len = sizeof (struct sockaddr_in); + dst.sin_len = sizeof(struct sockaddr_in); #endif dst.sin_addr = dummy; - dst.sin_port = htons (NAT_TRAV_PORT); - err = sendto (udpsock, - NULL, - 0, - 0, - (struct sockaddr *) &dst, - sizeof (dst)); + dst.sin_port = htons(NAT_TRAV_PORT); + err = sendto(udpsock, + NULL, + 0, + 0, + (struct sockaddr *)&dst, + sizeof(dst)); if (err < 0) - { + { #if VERBOSE - fprintf (stderr, - "sendto failed: %s\n", - strerror (errno)); + fprintf(stderr, + "sendto failed: %s\n", + strerror(errno)); #endif - } + } else if (0 != err) - { - fprintf (stderr, - "Error: partial send of ICMP message\n"); - } + { + fprintf(stderr, + "Error: partial send of ICMP message\n"); + } } @@ -365,7 +360,7 @@ send_udp () * We've received an ICMP response. Process it. */ static void -process_icmp_response () +process_icmp_response() { char buf[65536]; ssize_t have; @@ -377,94 +372,96 @@ process_icmp_response () size_t off; uint16_t port; - have = read (icmpsock, buf, sizeof (buf)); + have = read(icmpsock, buf, sizeof(buf)); if (-1 == have) - { - fprintf (stderr, - "Error reading raw socket: %s\n", - strerror (errno)); - return; - } + { + fprintf(stderr, + "Error reading raw socket: %s\n", + strerror(errno)); + return; + } #if VERBOSE - fprintf (stderr, - "Received message of %u bytes\n", - (unsigned int) have); + fprintf(stderr, + "Received message of %u bytes\n", + (unsigned int)have); #endif if (have < - (ssize_t) (sizeof (struct ip_header) + - sizeof (struct icmp_ttl_exceeded_header) + - sizeof (struct ip_header))) - { - /* malformed */ - return; - } + (ssize_t)(sizeof(struct ip_header) + + sizeof(struct icmp_ttl_exceeded_header) + + sizeof(struct ip_header))) + { + /* malformed */ + return; + } off = 0; - GNUNET_memcpy (&ip_pkt, - &buf[off], - sizeof (struct ip_header)); - off += sizeof (struct ip_header); - GNUNET_memcpy (&icmp_ttl, - &buf[off], - sizeof (struct icmp_ttl_exceeded_header)); - off += sizeof (struct icmp_ttl_exceeded_header); + GNUNET_memcpy(&ip_pkt, + &buf[off], + sizeof(struct ip_header)); + off += sizeof(struct ip_header); + GNUNET_memcpy(&icmp_ttl, + &buf[off], + sizeof(struct icmp_ttl_exceeded_header)); + off += sizeof(struct icmp_ttl_exceeded_header); if ((ICMP_TIME_EXCEEDED != icmp_ttl.type) || (0 != icmp_ttl.code)) - { - /* different type than what we want */ - return; - } + { + /* different type than what we want */ + return; + } /* grab source IP of 1st IP header */ source_ip.s_addr = ip_pkt.src_ip; /* skip 2nd IP header */ - GNUNET_memcpy (&ip_pkt, - &buf[off], - sizeof (struct ip_header)); - off += sizeof (struct ip_header); + GNUNET_memcpy(&ip_pkt, + &buf[off], + sizeof(struct ip_header)); + off += sizeof(struct ip_header); switch (ip_pkt.proto) - { - case IPPROTO_ICMP: - if (have != - (sizeof (struct ip_header) * 2 + - sizeof (struct icmp_ttl_exceeded_header) + - sizeof (struct icmp_echo_header))) { - /* malformed */ - return; - } - /* grab ICMP ECHO content */ - GNUNET_memcpy (&icmp_echo, - &buf[off], - sizeof (struct icmp_echo_header)); - port = (uint16_t) ntohl (icmp_echo.reserved); - break; - case IPPROTO_UDP: - if (have != - (sizeof (struct ip_header) * 2 + - sizeof (struct icmp_ttl_exceeded_header) + sizeof (struct udp_header))) - { - /* malformed */ + case IPPROTO_ICMP: + if (have != + (sizeof(struct ip_header) * 2 + + sizeof(struct icmp_ttl_exceeded_header) + + sizeof(struct icmp_echo_header))) + { + /* malformed */ + return; + } + /* grab ICMP ECHO content */ + GNUNET_memcpy(&icmp_echo, + &buf[off], + sizeof(struct icmp_echo_header)); + port = (uint16_t)ntohl(icmp_echo.reserved); + break; + + case IPPROTO_UDP: + if (have != + (sizeof(struct ip_header) * 2 + + sizeof(struct icmp_ttl_exceeded_header) + sizeof(struct udp_header))) + { + /* malformed */ + return; + } + /* grab UDP content */ + GNUNET_memcpy(&udp_pkt, + &buf[off], + sizeof(struct udp_header)); + port = ntohs(udp_pkt.length); + break; + + default: + /* different type than what we want */ return; } - /* grab UDP content */ - GNUNET_memcpy (&udp_pkt, - &buf[off], - sizeof (struct udp_header)); - port = ntohs (udp_pkt.length); - break; - default: - /* different type than what we want */ - return; - } if (port == 0) - fprintf (stdout, "%s\n", - inet_ntop (AF_INET, &source_ip, buf, sizeof (buf))); + fprintf(stdout, "%s\n", + inet_ntop(AF_INET, &source_ip, buf, sizeof(buf))); else - fprintf (stdout, "%s:%u\n", - inet_ntop (AF_INET, &source_ip, buf, sizeof (buf)), - (unsigned int) port); - fflush (stdout); + fprintf(stdout, "%s:%u\n", + inet_ntop(AF_INET, &source_ip, buf, sizeof(buf)), + (unsigned int)port); + fflush(stdout); } @@ -474,34 +471,34 @@ process_icmp_response () * @return -1 on error, 0 on success */ static int -setup_raw_socket () +setup_raw_socket() { const int one = 1; if (-1 == - setsockopt (rawsock, - SOL_SOCKET, - SO_BROADCAST, - (char *) &one, - sizeof (one))) - { - fprintf (stderr, - "setsockopt failed: %s\n", - strerror (errno)); - return -1; - } + setsockopt(rawsock, + SOL_SOCKET, + SO_BROADCAST, + (char *)&one, + sizeof(one))) + { + fprintf(stderr, + "setsockopt failed: %s\n", + strerror(errno)); + return -1; + } if (-1 == - setsockopt (rawsock, - IPPROTO_IP, - IP_HDRINCL, - (char *) &one, - sizeof (one))) - { - fprintf (stderr, - "setsockopt failed: %s\n", - strerror (errno)); - return -1; - } + setsockopt(rawsock, + IPPROTO_IP, + IP_HDRINCL, + (char *)&one, + sizeof(one))) + { + fprintf(stderr, + "setsockopt failed: %s\n", + strerror(errno)); + return -1; + } return 0; } @@ -513,45 +510,45 @@ setup_raw_socket () * @return -1 on error */ static int -make_udp_socket (const struct in_addr *my_ip) +make_udp_socket(const struct in_addr *my_ip) { int ret; struct sockaddr_in addr; - ret = socket (AF_INET, SOCK_DGRAM, 0); + ret = socket(AF_INET, SOCK_DGRAM, 0); if (-1 == ret) - { - fprintf (stderr, - "Error opening UDP socket: %s\n", - strerror (errno)); - return -1; - } - memset (&addr, 0, sizeof (addr)); + { + fprintf(stderr, + "Error opening UDP socket: %s\n", + strerror(errno)); + return -1; + } + memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; #if HAVE_SOCKADDR_IN_SIN_LEN - addr.sin_len = sizeof (struct sockaddr_in); + addr.sin_len = sizeof(struct sockaddr_in); #endif addr.sin_addr = *my_ip; - addr.sin_port = htons (NAT_TRAV_PORT); - - if (0 != bind (ret, - (struct sockaddr *) &addr, - sizeof (addr))) - { - fprintf (stderr, - "Error binding UDP socket to port %u: %s\n", - NAT_TRAV_PORT, - strerror (errno)); - (void) close (ret); - return -1; - } + addr.sin_port = htons(NAT_TRAV_PORT); + + if (0 != bind(ret, + (struct sockaddr *)&addr, + sizeof(addr))) + { + fprintf(stderr, + "Error binding UDP socket to port %u: %s\n", + NAT_TRAV_PORT, + strerror(errno)); + (void)close(ret); + return -1; + } return ret; } int -main (int argc, - char *const *argv) +main(int argc, + char *const *argv) { struct in_addr external; fd_set rs; @@ -563,147 +560,147 @@ main (int argc, int global_ret; /* Create an ICMP raw socket for reading (we'll check errors later) */ - icmpsock = socket (AF_INET, - SOCK_RAW, - IPPROTO_ICMP); + icmpsock = socket(AF_INET, + SOCK_RAW, + IPPROTO_ICMP); icmp_eno = errno; /* Create an (ICMP) raw socket for writing (we'll check errors later) */ - rawsock = socket (AF_INET, - SOCK_RAW, - IPPROTO_RAW); + rawsock = socket(AF_INET, + SOCK_RAW, + IPPROTO_RAW); raw_eno = errno; udpsock = -1; /* drop root rights */ - uid = getuid (); + uid = getuid(); #ifdef HAVE_SETRESUID - if (0 != setresuid (uid, uid, uid)) - { - fprintf (stderr, - "Failed to setresuid: %s\n", - strerror (errno)); - global_ret = 1; - goto error_exit; - } + if (0 != setresuid(uid, uid, uid)) + { + fprintf(stderr, + "Failed to setresuid: %s\n", + strerror(errno)); + global_ret = 1; + goto error_exit; + } #else - if (0 != (setuid (uid) | seteuid (uid))) - { - fprintf (stderr, - "Failed to setuid: %s\n", - strerror (errno)); - global_ret = 2; - goto error_exit; - } + if (0 != (setuid(uid) | seteuid(uid))) + { + fprintf(stderr, + "Failed to setuid: %s\n", + strerror(errno)); + global_ret = 2; + goto error_exit; + } #endif /* Now that we run without root rights, we can do error checking... */ if (2 != argc) - { - fprintf (stderr, - "This program must be started with our (internal NAT) IP as the only argument.\n"); - global_ret = 3; - goto error_exit; - } - if (1 != inet_pton (AF_INET, argv[1], &external)) - { - fprintf (stderr, - "Error parsing IPv4 address: %s\n", - strerror (errno)); - global_ret = 4; - goto error_exit; - } - if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy)) - { - fprintf (stderr, - "Internal error converting dummy IP to binary.\n"); - global_ret = 5; - goto error_exit; - } + { + fprintf(stderr, + "This program must be started with our (internal NAT) IP as the only argument.\n"); + global_ret = 3; + goto error_exit; + } + if (1 != inet_pton(AF_INET, argv[1], &external)) + { + fprintf(stderr, + "Error parsing IPv4 address: %s\n", + strerror(errno)); + global_ret = 4; + goto error_exit; + } + if (1 != inet_pton(AF_INET, DUMMY_IP, &dummy)) + { + fprintf(stderr, + "Internal error converting dummy IP to binary.\n"); + global_ret = 5; + goto error_exit; + } /* error checking icmpsock */ if (-1 == icmpsock) - { - fprintf (stderr, - "Error opening RAW socket: %s\n", - strerror (icmp_eno)); - global_ret = 6; - goto error_exit; - } + { + fprintf(stderr, + "Error opening RAW socket: %s\n", + strerror(icmp_eno)); + global_ret = 6; + goto error_exit; + } if (icmpsock >= FD_SETSIZE) - { - /* this could happen if we were started with a large number of already-open - file descriptors... */ - fprintf (stderr, - "Socket number too large (%d > %u)\n", - icmpsock, - (unsigned int) FD_SETSIZE); - global_ret = 7; - goto error_exit; - } + { + /* this could happen if we were started with a large number of already-open + file descriptors... */ + fprintf(stderr, + "Socket number too large (%d > %u)\n", + icmpsock, + (unsigned int)FD_SETSIZE); + global_ret = 7; + goto error_exit; + } /* error checking rawsock */ if (-1 == rawsock) - { - fprintf (stderr, - "Error opening RAW socket: %s\n", - strerror (raw_eno)); - global_ret = 8; - goto error_exit; - } + { + fprintf(stderr, + "Error opening RAW socket: %s\n", + strerror(raw_eno)); + global_ret = 8; + goto error_exit; + } /* no need to check 'rawsock' against FD_SETSIZE as it is never used with 'select' */ - if (0 != setup_raw_socket ()) - { - global_ret = 9; - goto error_exit; - } + if (0 != setup_raw_socket()) + { + global_ret = 9; + goto error_exit; + } - if (-1 == (udpsock = make_udp_socket (&external))) - { - global_ret = 10; - goto error_exit; - } + if (-1 == (udpsock = make_udp_socket(&external))) + { + global_ret = 10; + goto error_exit; + } alt = 0; while (1) - { - FD_ZERO (&rs); - FD_SET (icmpsock, &rs); - tv.tv_sec = 0; - tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000; - if (-1 == select (icmpsock + 1, &rs, NULL, NULL, &tv)) - { - if (errno == EINTR) - continue; - fprintf (stderr, - "select failed: %s\n", - strerror (errno)); - break; - } - if (1 == getppid ()) /* Check the parent process id, if 1 the parent has died, so we should die too */ - break; - if (FD_ISSET (icmpsock, &rs)) { - process_icmp_response (); - continue; + FD_ZERO(&rs); + FD_SET(icmpsock, &rs); + tv.tv_sec = 0; + tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000; + if (-1 == select(icmpsock + 1, &rs, NULL, NULL, &tv)) + { + if (errno == EINTR) + continue; + fprintf(stderr, + "select failed: %s\n", + strerror(errno)); + break; + } + if (1 == getppid()) /* Check the parent process id, if 1 the parent has died, so we should die too */ + break; + if (FD_ISSET(icmpsock, &rs)) + { + process_icmp_response(); + continue; + } + if (0 == (++alt % 2)) + send_icmp_echo(&external); + else + send_udp(); } - if (0 == (++alt % 2)) - send_icmp_echo (&external); - else - send_udp (); - } /* select failed (internal error or OS out of resources) */ global_ret = 11; error_exit: if (-1 != icmpsock) - (void) close (icmpsock); + (void)close(icmpsock); if (-1 != rawsock) - (void) close (rawsock); + (void)close(rawsock); if (-1 != udpsock) - (void) close (udpsock); + (void)close(udpsock); return global_ret; } diff --git a/src/nat/gnunet-nat.c b/src/nat/gnunet-nat.c index 6c533374e..d82898f56 100644 --- a/src/nat/gnunet-nat.c +++ b/src/nat/gnunet-nat.c @@ -11,12 +11,12 @@ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . SPDX-License-Identifier: AGPL3.0-or-later -*/ + */ /** * @file src/nat/gnunet-nat.c @@ -95,13 +95,13 @@ static struct GNUNET_SCHEDULER_Task *rtask; * terminate. */ static void -test_finished () +test_finished() { if (NULL != nh) return; if (NULL != rtask) return; - GNUNET_SCHEDULER_shutdown (); + GNUNET_SCHEDULER_shutdown(); } @@ -119,21 +119,21 @@ test_finished () * @param addrlen actual length of the @a addr */ static void -address_cb (void *cls, - void **app_ctx, - int add_remove, - enum GNUNET_NAT_AddressClass ac, - const struct sockaddr *addr, - socklen_t addrlen) +address_cb(void *cls, + void **app_ctx, + int add_remove, + enum GNUNET_NAT_AddressClass ac, + const struct sockaddr *addr, + socklen_t addrlen) { - (void) cls; - (void) app_ctx; - - fprintf (stdout, - "%s %s (%d)\n", - add_remove ? "+" : "-", - GNUNET_a2s (addr, addrlen), - (int) ac); + (void)cls; + (void)app_ctx; + + fprintf(stdout, + "%s %s (%d)\n", + add_remove ? "+" : "-", + GNUNET_a2s(addr, addrlen), + (int)ac); } @@ -147,13 +147,13 @@ address_cb (void *cls, * @param remote_addrlen actual length of the @a remote_addr */ static void -reversal_cb (void *cls, - const struct sockaddr *remote_addr, - socklen_t remote_addrlen) +reversal_cb(void *cls, + const struct sockaddr *remote_addr, + socklen_t remote_addrlen) { - GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, - "Connection reversal requested by %s\n", - GNUNET_a2s (remote_addr, remote_addrlen)); + GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, + "Connection reversal requested by %s\n", + GNUNET_a2s(remote_addr, remote_addrlen)); } @@ -163,23 +163,23 @@ reversal_cb (void *cls, * @param cls NULL */ static void -do_shutdown (void *cls) +do_shutdown(void *cls) { if (NULL != nh) - { - GNUNET_NAT_unregister (nh); - nh = NULL; - } + { + GNUNET_NAT_unregister(nh); + nh = NULL; + } if (NULL != ls) - { - GNUNET_NETWORK_socket_close (ls); - ls = NULL; - } + { + GNUNET_NETWORK_socket_close(ls); + ls = NULL; + } if (NULL != rtask) - { - GNUNET_SCHEDULER_cancel (rtask); - rtask = NULL; - } + { + GNUNET_SCHEDULER_cancel(rtask); + rtask = NULL; + } } @@ -187,45 +187,45 @@ do_shutdown (void *cls) * Task to receive incoming packets for STUN processing. */ static void -stun_read_task (void *cls) +stun_read_task(void *cls) { ssize_t size; - rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, - ls, - &stun_read_task, - NULL); - size = GNUNET_NETWORK_socket_recvfrom_amount (ls); + rtask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL, + ls, + &stun_read_task, + NULL); + size = GNUNET_NETWORK_socket_recvfrom_amount(ls); if (size > 0) - { - GNUNET_break (0); - GNUNET_SCHEDULER_shutdown (); - global_ret = 1; - return; - } + { + GNUNET_break(0); + GNUNET_SCHEDULER_shutdown(); + global_ret = 1; + return; + } { char buf[size + 1]; struct sockaddr_storage sa; - socklen_t salen = sizeof (sa); + socklen_t salen = sizeof(sa); ssize_t ret; - ret = GNUNET_NETWORK_socket_recvfrom (ls, - buf, - size + 1, - (struct sockaddr *) &sa, - &salen); + ret = GNUNET_NETWORK_socket_recvfrom(ls, + buf, + size + 1, + (struct sockaddr *)&sa, + &salen); if (ret != size) - { - GNUNET_break (0); - GNUNET_SCHEDULER_shutdown (); - global_ret = 1; - return; - } - (void) GNUNET_NAT_stun_handle_packet (nh, - (const struct sockaddr *) &sa, - salen, - buf, - ret); + { + GNUNET_break(0); + GNUNET_SCHEDULER_shutdown(); + global_ret = 1; + return; + } + (void)GNUNET_NAT_stun_handle_packet(nh, + (const struct sockaddr *)&sa, + salen, + buf, + ret); } } @@ -239,10 +239,10 @@ stun_read_task (void *cls) * @param c configuration */ static void -run (void *cls, - char *const *args, - const char *cfgfile, - const struct GNUNET_CONFIGURATION_Handle *c) +run(void *cls, + char *const *args, + const char *cfgfile, + const struct GNUNET_CONFIGURATION_Handle *c) { uint8_t af; struct sockaddr *local_sa; @@ -251,154 +251,156 @@ run (void *cls, size_t remote_len; if (use_tcp && use_udp) - { - GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "Cannot use TCP and UDP\n"); - global_ret = 1; - return; - } + { + GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, "Cannot use TCP and UDP\n"); + global_ret = 1; + return; + } proto = 0; if (use_tcp) proto = IPPROTO_TCP; if (use_udp) proto = IPPROTO_UDP; - GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL); + GNUNET_SCHEDULER_add_shutdown(&do_shutdown, NULL); if (0 == proto) - { - GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "Must specify either TCP or UDP\n"); - global_ret = 1; - return; - } + { + GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, "Must specify either TCP or UDP\n"); + global_ret = 1; + return; + } local_len = 0; local_sa = NULL; remote_len = 0; remote_sa = NULL; if (NULL != local_addr) - { - local_len = - (socklen_t) GNUNET_STRINGS_parse_socket_addr (local_addr, &af, &local_sa); - if (0 == local_len) { - GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, - "Invalid socket address `%s'\n", - local_addr); - goto fail_and_shutdown; + local_len = + (socklen_t)GNUNET_STRINGS_parse_socket_addr(local_addr, &af, &local_sa); + if (0 == local_len) + { + GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, + "Invalid socket address `%s'\n", + local_addr); + goto fail_and_shutdown; + } } - } if (NULL != remote_addr) - { - remote_len = - GNUNET_STRINGS_parse_socket_addr (remote_addr, &af, &remote_sa); - if (0 == remote_len) { - GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, - "Invalid socket address `%s'\n", - remote_addr); - goto fail_and_shutdown; + remote_len = + GNUNET_STRINGS_parse_socket_addr(remote_addr, &af, &remote_sa); + if (0 == remote_len) + { + GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, + "Invalid socket address `%s'\n", + remote_addr); + goto fail_and_shutdown; + } } - } if (NULL != local_addr) - { - if (NULL == section_name) - section_name = GNUNET_strdup ("undefined"); - nh = GNUNET_NAT_register (c, - section_name, - proto, - 1, - (const struct sockaddr **) &local_sa, - &local_len, - &address_cb, - (listen_reversal) ? &reversal_cb : NULL, - NULL); - } - else if (listen_reversal) - { - GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, - "Use of `-W` only effective in combination with `-i`\n"); - goto fail_and_shutdown; - } - - if (NULL != remote_addr) - { - int ret; - - if ((NULL == nh) || (sizeof (struct sockaddr_in) != local_len)) { - GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, - "Require IPv4 local address to initiate connection reversal\n"); - goto fail_and_shutdown; + if (NULL == section_name) + section_name = GNUNET_strdup("undefined"); + nh = GNUNET_NAT_register(c, + section_name, + proto, + 1, + (const struct sockaddr **)&local_sa, + &local_len, + &address_cb, + (listen_reversal) ? &reversal_cb : NULL, + NULL); } - if (sizeof (struct sockaddr_in) != remote_len) + else if (listen_reversal) { - GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, - "Require IPv4 reversal target address\n"); + GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, + "Use of `-W` only effective in combination with `-i`\n"); goto fail_and_shutdown; } - GNUNET_assert (AF_INET == local_sa->sa_family); - GNUNET_assert (AF_INET == remote_sa->sa_family); - ret = GNUNET_NAT_request_reversal (nh, - (const struct sockaddr_in *) local_sa, - (const struct sockaddr_in *) remote_sa); - switch (ret) + + if (NULL != remote_addr) { - case GNUNET_SYSERR: - GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, - "Connection reversal internal error\n"); - break; - case GNUNET_NO: - GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, - "Connection reversal unavailable\n"); - break; - case GNUNET_OK: - /* operation in progress */ - break; + int ret; + + if ((NULL == nh) || (sizeof(struct sockaddr_in) != local_len)) + { + GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, + "Require IPv4 local address to initiate connection reversal\n"); + goto fail_and_shutdown; + } + if (sizeof(struct sockaddr_in) != remote_len) + { + GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, + "Require IPv4 reversal target address\n"); + goto fail_and_shutdown; + } + GNUNET_assert(AF_INET == local_sa->sa_family); + GNUNET_assert(AF_INET == remote_sa->sa_family); + ret = GNUNET_NAT_request_reversal(nh, + (const struct sockaddr_in *)local_sa, + (const struct sockaddr_in *)remote_sa); + switch (ret) + { + case GNUNET_SYSERR: + GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, + "Connection reversal internal error\n"); + break; + + case GNUNET_NO: + GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, + "Connection reversal unavailable\n"); + break; + + case GNUNET_OK: + /* operation in progress */ + break; + } } - } if (do_stun) - { - if (NULL == local_addr) - { - GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, - "Require local address to support STUN requests\n"); - goto fail_and_shutdown; - } - if (IPPROTO_UDP != proto) { - GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "STUN only supported over UDP\n"); - goto fail_and_shutdown; + if (NULL == local_addr) + { + GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, + "Require local address to support STUN requests\n"); + goto fail_and_shutdown; + } + if (IPPROTO_UDP != proto) + { + GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, "STUN only supported over UDP\n"); + goto fail_and_shutdown; + } + ls = GNUNET_NETWORK_socket_create(af, SOCK_DGRAM, IPPROTO_UDP); + if (NULL == ls) + { + GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, "Failed to create socket\n"); + goto fail_and_shutdown; + } + if (GNUNET_OK != GNUNET_NETWORK_socket_bind(ls, local_sa, local_len)) + { + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, + "Failed to bind to %s: %s\n", + GNUNET_a2s(local_sa, local_len), + strerror(errno)); + goto fail_and_shutdown; + } + rtask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL, + ls, + &stun_read_task, + NULL); } - ls = GNUNET_NETWORK_socket_create (af, SOCK_DGRAM, IPPROTO_UDP); - if (NULL == ls) - { - GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "Failed to create socket\n"); - goto fail_and_shutdown; - } - if (GNUNET_OK != GNUNET_NETWORK_socket_bind (ls, local_sa, local_len)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Failed to bind to %s: %s\n", - GNUNET_a2s (local_sa, local_len), - strerror (errno)); - goto fail_and_shutdown; - } - rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, - ls, - &stun_read_task, - NULL); - } - GNUNET_free_non_null (remote_sa); - GNUNET_free_non_null (local_sa); - test_finished (); + GNUNET_free_non_null(remote_sa); + GNUNET_free_non_null(local_sa); + test_finished(); return; fail_and_shutdown: global_ret = 1; - GNUNET_SCHEDULER_shutdown (); - GNUNET_free_non_null (remote_sa); - GNUNET_free_non_null (local_sa); + GNUNET_SCHEDULER_shutdown(); + GNUNET_free_non_null(remote_sa); + GNUNET_free_non_null(local_sa); } @@ -410,63 +412,63 @@ fail_and_shutdown: * @return 0 on success, -1 on error */ int -main (int argc, char *const argv[]) +main(int argc, char *const argv[]) { struct GNUNET_GETOPT_CommandLineOption options[] = { - - GNUNET_GETOPT_option_string ( + GNUNET_GETOPT_option_string( 'i', "in", "ADDRESS", - gettext_noop ("which IP and port are we locally using to bind/listen to"), + gettext_noop("which IP and port are we locally using to bind/listen to"), &local_addr), - GNUNET_GETOPT_option_string ( + GNUNET_GETOPT_option_string( 'r', "remote", "ADDRESS", - gettext_noop ( + gettext_noop( "which remote IP and port should be asked for connection reversal"), &remote_addr), - GNUNET_GETOPT_option_string ( + GNUNET_GETOPT_option_string( 'S', "section", NULL, - gettext_noop ( + gettext_noop( "name of configuration section to find additional options, such as manual host punching data"), §ion_name), - GNUNET_GETOPT_option_flag ('s', - "stun", - gettext_noop ("enable STUN processing"), - &do_stun), + GNUNET_GETOPT_option_flag('s', + "stun", + gettext_noop("enable STUN processing"), + &do_stun), - GNUNET_GETOPT_option_flag ('t', "tcp", gettext_noop ("use TCP"), &use_tcp), + GNUNET_GETOPT_option_flag('t', "tcp", gettext_noop("use TCP"), &use_tcp), - GNUNET_GETOPT_option_flag ('u', "udp", gettext_noop ("use UDP"), &use_udp), + GNUNET_GETOPT_option_flag('u', "udp", gettext_noop("use UDP"), &use_udp), - GNUNET_GETOPT_option_flag ('W', - "watch", - gettext_noop ( - "watch for connection reversal requests"), - &listen_reversal), - GNUNET_GETOPT_OPTION_END}; + GNUNET_GETOPT_option_flag('W', + "watch", + gettext_noop( + "watch for connection reversal requests"), + &listen_reversal), + GNUNET_GETOPT_OPTION_END + }; - if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) + if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv)) return 2; if (GNUNET_OK != - GNUNET_PROGRAM_run (argc, - argv, - "gnunet-nat [options]", - _ ("GNUnet NAT traversal autoconfigure daemon"), - options, - &run, - NULL)) - { - global_ret = 1; - } - GNUNET_free ((void *) argv); + GNUNET_PROGRAM_run(argc, + argv, + "gnunet-nat [options]", + _("GNUnet NAT traversal autoconfigure daemon"), + options, + &run, + NULL)) + { + global_ret = 1; + } + GNUNET_free((void *)argv); return global_ret; } diff --git a/src/nat/gnunet-service-nat.c b/src/nat/gnunet-service-nat.c index 2498a990a..6fd09a7d9 100644 --- a/src/nat/gnunet-service-nat.c +++ b/src/nat/gnunet-service-nat.c @@ -1,19 +1,19 @@ /* - This file is part of GNUnet. - Copyright (C) 2016, 2017 GNUnet e.V. - - GNUnet is free software: you can redistribute it and/or modify it - under the terms of the GNU Affero General Public License as published - by the Free Software Foundation, either version 3 of the License, - or (at your option) any later version. - - GNUnet is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . + This file is part of GNUnet. + Copyright (C) 2016, 2017 GNUnet e.V. + + GNUnet is free software: you can redistribute it and/or modify it + under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . SPDX-License-Identifier: AGPL3.0-or-later */ @@ -55,24 +55,23 @@ * How often should we ask the OS about a list of active * network interfaces? */ -#define SCAN_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15) +#define SCAN_FREQ GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 15) /** * How long do we wait until we forcefully terminate autoconfiguration? */ -#define AUTOCONFIG_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5) +#define AUTOCONFIG_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5) /** * How often do we scan for changes in how our external (dyndns) hostname resolves? */ -#define DYNDNS_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 7) +#define DYNDNS_FREQUENCY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 7) /** * Information we track per client address. */ -struct ClientAddress -{ +struct ClientAddress { /** * Network address used by the client. */ @@ -84,15 +83,13 @@ struct ClientAddress * pending. */ struct GNUNET_NAT_MiniHandle *mh; - }; /** * List of local addresses this system has. */ -struct LocalAddressList -{ +struct LocalAddressList { /** * This is a linked list. */ @@ -131,16 +128,13 @@ struct LocalAddressList * What type of address is this? */ enum GNUNET_NAT_AddressClass ac; - }; /** * Internal data structure we track for each of our clients. */ -struct ClientHandle -{ - +struct ClientHandle { /** * Kept in a DLL. */ @@ -232,15 +226,13 @@ struct ClientHandle * Client's IPPROTO, e.g. IPPROTO_UDP or IPPROTO_TCP. */ uint8_t proto; - }; /** * External IP address as given to us via some STUN server. */ -struct StunExternalIP -{ +struct StunExternalIP { /** * Kept in a DLL. */ @@ -344,22 +336,22 @@ int enable_upnp; * @param lal entry to free */ static void -free_lal (struct LocalAddressList *lal) +free_lal(struct LocalAddressList *lal) { - GNUNET_CONTAINER_DLL_remove (lal_head, - lal_tail, - lal); + GNUNET_CONTAINER_DLL_remove(lal_head, + lal_tail, + lal); if (NULL != lal->hc) - { - GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, - "Lost NATed local address %s, stopping NAT server\n", - GNUNET_a2s ((const struct sockaddr *) &lal->addr, - sizeof (struct sockaddr_in))); - - GN_stop_gnunet_nat_server_ (lal->hc); - lal->hc = NULL; - } - GNUNET_free (lal); + { + GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, + "Lost NATed local address %s, stopping NAT server\n", + GNUNET_a2s((const struct sockaddr *)&lal->addr, + sizeof(struct sockaddr_in))); + + GN_stop_gnunet_nat_server_(lal->hc); + lal->hc = NULL; + } + GNUNET_free(lal); } @@ -367,12 +359,12 @@ free_lal (struct LocalAddressList *lal) * Free the DLL starting at #lal_head. */ static void -destroy_lal () +destroy_lal() { struct LocalAddressList *lal; while (NULL != (lal = lal_head)) - free_lal (lal); + free_lal(lal); } @@ -385,53 +377,55 @@ destroy_lal () * @return #GNUNET_OK if message is well-formed */ static int -check_register (void *cls, - const struct GNUNET_NAT_RegisterMessage *message) +check_register(void *cls, + const struct GNUNET_NAT_RegisterMessage *message) { - uint16_t num_addrs = ntohs (message->num_addrs); - const char *off = (const char *) &message[1]; - size_t left = ntohs (message->header.size) - sizeof (*message); - - for (unsigned int i=0;inum_addrs); + const char *off = (const char *)&message[1]; + size_t left = ntohs(message->header.size) - sizeof(*message); - if (sizeof (sa_family_t) > left) + for (unsigned int i = 0; i < num_addrs; i++) { - GNUNET_break (0); - return GNUNET_SYSERR; - } - switch (sa->sa_family) - { - case AF_INET: - alen = sizeof (struct sockaddr_in); - break; - case AF_INET6: - alen = sizeof (struct sockaddr_in6); - break; + size_t alen; + const struct sockaddr *sa = (const struct sockaddr *)off; + + if (sizeof(sa_family_t) > left) + { + GNUNET_break(0); + return GNUNET_SYSERR; + } + switch (sa->sa_family) + { + case AF_INET: + alen = sizeof(struct sockaddr_in); + break; + + case AF_INET6: + alen = sizeof(struct sockaddr_in6); + break; + #if AF_UNIX - case AF_UNIX: - alen = sizeof (struct sockaddr_un); - break; + case AF_UNIX: + alen = sizeof(struct sockaddr_un); + break; #endif - default: - GNUNET_break (0); - return GNUNET_SYSERR; + default: + GNUNET_break(0); + return GNUNET_SYSERR; + } + if (alen > left) + { + GNUNET_break(0); + return GNUNET_SYSERR; + } + off += alen; + left -= alen; } - if (alen > left) + if (left != ntohs(message->str_len)) { - GNUNET_break (0); + GNUNET_break(0); return GNUNET_SYSERR; } - off += alen; - left -= alen; - } - if (left != ntohs (message->str_len)) - { - GNUNET_break (0); - return GNUNET_SYSERR; - } return GNUNET_OK; } @@ -445,9 +439,9 @@ check_register (void *cls, * @return #GNUNET_YES if @a ip is in @a network */ static int -match_ipv4 (const char *network, - const struct in_addr *ip, - uint8_t bits) +match_ipv4(const char *network, + const struct in_addr *ip, + uint8_t bits) { struct in_addr net; @@ -455,10 +449,10 @@ match_ipv4 (const char *network, return GNUNET_YES; if (0 == bits) return GNUNET_YES; - GNUNET_assert (1 == inet_pton (AF_INET, - network, - &net)); - return ! ((ip->s_addr ^ net.s_addr) & htonl (0xFFFFFFFFu << (32 - bits))); + GNUNET_assert(1 == inet_pton(AF_INET, + network, + &net)); + return !((ip->s_addr ^ net.s_addr) & htonl(0xFFFFFFFFu << (32 - bits))); } @@ -471,9 +465,9 @@ match_ipv4 (const char *network, * @return #GNUNET_YES if @a ip is in @a network */ static int -match_ipv6 (const char *network, - const struct in6_addr *ip, - uint8_t bits) +match_ipv6(const char *network, + const struct in6_addr *ip, + uint8_t bits) { struct in6_addr net; struct in6_addr mask; @@ -481,27 +475,27 @@ match_ipv6 (const char *network, if (0 == bits) return GNUNET_YES; - GNUNET_assert (1 == inet_pton (AF_INET6, - network, - &net)); - memset (&mask, 0, sizeof (mask)); - if (0 == GNUNET_memcmp (&mask, - ip)) + GNUNET_assert(1 == inet_pton(AF_INET6, + network, + &net)); + memset(&mask, 0, sizeof(mask)); + if (0 == GNUNET_memcmp(&mask, + ip)) return GNUNET_YES; off = 0; while (bits > 8) - { - mask.s6_addr[off++] = 0xFF; - bits -= 8; - } + { + mask.s6_addr[off++] = 0xFF; + bits -= 8; + } while (bits > 0) - { - mask.s6_addr[off] = (mask.s6_addr[off] >> 1) + 0x80; - bits--; - } - for (unsigned j = 0; j < sizeof (struct in6_addr) / sizeof (uint32_t); j++) - if (((((uint32_t *) ip)[j] & ((uint32_t *) &mask)[j])) != - (((uint32_t *) &net)[j] & ((int *) &mask)[j])) + { + mask.s6_addr[off] = (mask.s6_addr[off] >> 1) + 0x80; + bits--; + } + for (unsigned j = 0; j < sizeof(struct in6_addr) / sizeof(uint32_t); j++) + if (((((uint32_t *)ip)[j] & ((uint32_t *)&mask)[j])) != + (((uint32_t *)&net)[j] & ((int *)&mask)[j])) return GNUNET_NO; return GNUNET_YES; } @@ -515,14 +509,14 @@ match_ipv6 (const char *network, * @return #GNUNET_YES if @a ip is in a NAT range */ static int -is_nat_v4 (const struct in_addr *ip) +is_nat_v4(const struct in_addr *ip) { return - match_ipv4 ("10.0.0.0", ip, 8) || /* RFC 1918 */ - match_ipv4 ("100.64.0.0", ip, 10) || /* CG-NAT, RFC 6598 */ - match_ipv4 ("192.168.0.0", ip, 12) || /* RFC 1918 */ - match_ipv4 ("169.254.0.0", ip, 16) || /* AUTO, RFC 3927 */ - match_ipv4 ("172.16.0.0", ip, 16); /* RFC 1918 */ + match_ipv4("10.0.0.0", ip, 8) || /* RFC 1918 */ + match_ipv4("100.64.0.0", ip, 10) || /* CG-NAT, RFC 6598 */ + match_ipv4("192.168.0.0", ip, 12) || /* RFC 1918 */ + match_ipv4("169.254.0.0", ip, 16) || /* AUTO, RFC 3927 */ + match_ipv4("172.16.0.0", ip, 16); /* RFC 1918 */ } @@ -534,21 +528,19 @@ is_nat_v4 (const struct in_addr *ip) * @return #GNUNET_YES if @a ip is in a NAT range */ static int -is_nat_v6 (const struct in6_addr *ip) +is_nat_v6(const struct in6_addr *ip) { return - match_ipv6 ("fc00::", ip, 7) || /* RFC 4193 */ - match_ipv6 ("fec0::", ip, 10) || /* RFC 3879 */ - match_ipv6 ("fe80::", ip, 10); /* RFC 4291, link-local */ + match_ipv6("fc00::", ip, 7) || /* RFC 4193 */ + match_ipv6("fec0::", ip, 10) || /* RFC 3879 */ + match_ipv6("fe80::", ip, 10); /* RFC 4291, link-local */ } /** * Closure for #ifc_proc. */ -struct IfcProcContext -{ - +struct IfcProcContext { /** * Head of DLL of local addresses. */ @@ -558,7 +550,6 @@ struct IfcProcContext * Tail of DLL of local addresses. */ struct LocalAddressList *lal_tail; - }; @@ -576,13 +567,13 @@ struct IfcProcContext * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort */ static int -ifc_proc (void *cls, - const char *name, - int isDefault, - const struct sockaddr *addr, - const struct sockaddr *broadcast_addr, - const struct sockaddr *netmask, - socklen_t addrlen) +ifc_proc(void *cls, + const char *name, + int isDefault, + const struct sockaddr *addr, + const struct sockaddr *broadcast_addr, + const struct sockaddr *netmask, + socklen_t addrlen) { struct IfcProcContext *ifc_ctx = cls; struct LocalAddressList *lal; @@ -592,51 +583,53 @@ ifc_proc (void *cls, enum GNUNET_NAT_AddressClass ac; switch (addr->sa_family) - { - case AF_INET: - alen = sizeof (struct sockaddr_in); - ip4 = &((const struct sockaddr_in *) addr)->sin_addr; - if (match_ipv4 ("127.0.0.0", ip4, 8)) - ac = GNUNET_NAT_AC_LOOPBACK; - else if (is_nat_v4 (ip4)) - ac = GNUNET_NAT_AC_LAN; - else - ac = GNUNET_NAT_AC_GLOBAL; - break; - case AF_INET6: - alen = sizeof (struct sockaddr_in6); - ip6 = &((const struct sockaddr_in6 *) addr)->sin6_addr; - if (match_ipv6 ("::1", ip6, 128)) - ac = GNUNET_NAT_AC_LOOPBACK; - else if (is_nat_v6 (ip6)) - ac = GNUNET_NAT_AC_LAN; - else - ac = GNUNET_NAT_AC_GLOBAL; - if ( (ip6->s6_addr[11] == 0xFF) && - (ip6->s6_addr[12] == 0xFE) ) { - /* contains a MAC, be extra careful! */ - ac |= GNUNET_NAT_AC_PRIVATE; - } - break; + case AF_INET: + alen = sizeof(struct sockaddr_in); + ip4 = &((const struct sockaddr_in *)addr)->sin_addr; + if (match_ipv4("127.0.0.0", ip4, 8)) + ac = GNUNET_NAT_AC_LOOPBACK; + else if (is_nat_v4(ip4)) + ac = GNUNET_NAT_AC_LAN; + else + ac = GNUNET_NAT_AC_GLOBAL; + break; + + case AF_INET6: + alen = sizeof(struct sockaddr_in6); + ip6 = &((const struct sockaddr_in6 *)addr)->sin6_addr; + if (match_ipv6("::1", ip6, 128)) + ac = GNUNET_NAT_AC_LOOPBACK; + else if (is_nat_v6(ip6)) + ac = GNUNET_NAT_AC_LAN; + else + ac = GNUNET_NAT_AC_GLOBAL; + if ((ip6->s6_addr[11] == 0xFF) && + (ip6->s6_addr[12] == 0xFE)) + { + /* contains a MAC, be extra careful! */ + ac |= GNUNET_NAT_AC_PRIVATE; + } + break; + #if AF_UNIX - case AF_UNIX: - GNUNET_break (0); - return GNUNET_OK; + case AF_UNIX: + GNUNET_break(0); + return GNUNET_OK; #endif - default: - GNUNET_break (0); - return GNUNET_OK; - } - lal = GNUNET_malloc (sizeof (*lal)); + default: + GNUNET_break(0); + return GNUNET_OK; + } + lal = GNUNET_malloc(sizeof(*lal)); lal->af = addr->sa_family; lal->ac = ac; - GNUNET_memcpy (&lal->addr, - addr, - alen); - GNUNET_CONTAINER_DLL_insert (ifc_ctx->lal_head, - ifc_ctx->lal_tail, - lal); + GNUNET_memcpy(&lal->addr, + addr, + alen); + GNUNET_CONTAINER_DLL_insert(ifc_ctx->lal_head, + ifc_ctx->lal_tail, + lal); return GNUNET_OK; } @@ -652,30 +645,30 @@ ifc_proc (void *cls, * @param addr_len number of bytes in @a addr */ static void -notify_client (enum GNUNET_NAT_AddressClass ac, - struct ClientHandle *ch, - int add, - const void *addr, - size_t addr_len) +notify_client(enum GNUNET_NAT_AddressClass ac, + struct ClientHandle *ch, + int add, + const void *addr, + size_t addr_len) { struct GNUNET_MQ_Envelope *env; struct GNUNET_NAT_AddressChangeNotificationMessage *msg; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Notifying client about %s of IP %s\n", - add ? "addition" : "removal", - GNUNET_a2s (addr, - addr_len)); - env = GNUNET_MQ_msg_extra (msg, - addr_len, - GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE); - msg->add_remove = htonl (add); - msg->addr_class = htonl (ac); - GNUNET_memcpy (&msg[1], - addr, - addr_len); - GNUNET_MQ_send (ch->mq, - env); + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "Notifying client about %s of IP %s\n", + add ? "addition" : "removal", + GNUNET_a2s(addr, + addr_len)); + env = GNUNET_MQ_msg_extra(msg, + addr_len, + GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE); + msg->add_remove = htonl(add); + msg->addr_class = htonl(ac); + GNUNET_memcpy(&msg[1], + addr, + addr_len); + GNUNET_MQ_send(ch->mq, + env); } @@ -688,121 +681,123 @@ notify_client (enum GNUNET_NAT_AddressClass ac, * @param add #GNUNET_YES to add, #GNUNET_NO to remove */ static void -check_notify_client (struct LocalAddressList *delta, - struct ClientHandle *ch, - int add) +check_notify_client(struct LocalAddressList *delta, + struct ClientHandle *ch, + int add) { size_t alen; struct sockaddr_in v4; struct sockaddr_in6 v6; if (0 == (ch->flags & GNUNET_NAT_RF_ADDRESSES)) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Not notifying client as it does not care about addresses\n"); - return; - } - switch (delta->af) - { - case AF_INET: - alen = sizeof (struct sockaddr_in); - GNUNET_memcpy (&v4, - &delta->addr, - alen); - - /* Check for client notifications */ - for (unsigned int i=0;inum_caddrs;i++) { - const struct sockaddr_in *c4; - - if (AF_INET != ch->caddrs[i].ss.ss_family) - continue; /* IPv4 not relevant */ - c4 = (const struct sockaddr_in *) &ch->caddrs[i].ss; - if ( match_ipv4 ("127.0.0.1", &c4->sin_addr, 8) && - (0 != c4->sin_addr.s_addr) && - (! match_ipv4 ("127.0.0.1", &v4.sin_addr, 8)) ) - continue; /* bound to loopback, but this is not loopback */ - if ( (! match_ipv4 ("127.0.0.1", &c4->sin_addr, 8) ) && - match_ipv4 ("127.0.0.1", &v4.sin_addr, 8) ) - continue; /* bound to non-loopback, but this is loopback */ - if ( (0 != (delta->ac & GNUNET_NAT_AC_EXTERN)) && - (0 != c4->sin_addr.s_addr) && - (! is_nat_v4 (&v4.sin_addr)) ) - continue; /* based on external-IP, but this IP is not - from private address range. */ - if ( (0 != GNUNET_memcmp (&v4.sin_addr, - &c4->sin_addr)) && - (0 != c4->sin_addr.s_addr) && - (! is_nat_v4 (&c4->sin_addr)) ) - continue; /* this IP is not from private address range, - and IP does not match. */ - - /* OK, IP seems relevant, notify client */ - if (0 == htons (v4.sin_port)) - v4.sin_port = c4->sin_port; - notify_client (delta->ac, - ch, - add, - &v4, - alen); + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "Not notifying client as it does not care about addresses\n"); + return; } - break; - case AF_INET6: - alen = sizeof (struct sockaddr_in6); - GNUNET_memcpy (&v6, - &delta->addr, - alen); - for (unsigned int i=0;inum_caddrs;i++) + switch (delta->af) { - const struct sockaddr_in6 *c6; - - if (AF_INET6 != ch->caddrs[i].ss.ss_family) - continue; /* IPv4 not relevant */ - c6 = (const struct sockaddr_in6 *) &ch->caddrs[i].ss; - if ( match_ipv6 ("::1", &c6->sin6_addr, 128) && - (0 != GNUNET_memcmp (&c6->sin6_addr, - &in6addr_any)) && - (! match_ipv6 ("::1", &v6.sin6_addr, 128)) ) - continue; /* bound to loopback, but this is not loopback */ - if ( (! match_ipv6 ("::1", &c6->sin6_addr, 128) ) && - match_ipv6 ("::1", &v6.sin6_addr, 128) ) - continue; /* bound to non-loopback, but this is loopback */ - if ( (0 != (delta->ac & GNUNET_NAT_AC_EXTERN)) && - (0 != GNUNET_memcmp (&c6->sin6_addr, - &in6addr_any)) && - (! is_nat_v6 (&v6.sin6_addr)) ) - continue; /* based on external-IP, but this IP is not - from private address range. */ - if ( (0 != GNUNET_memcmp (&v6.sin6_addr, - &c6->sin6_addr)) && - (0 != GNUNET_memcmp (&c6->sin6_addr, - &in6addr_any)) && - (! is_nat_v6 (&c6->sin6_addr)) ) - continue; /* this IP is not from private address range, - and IP does not match. */ - if ( (match_ipv6 ("fe80::", &c6->sin6_addr, 10)) && - (0 != GNUNET_memcmp (&c6->sin6_addr, - &in6addr_any)) && - (0 != GNUNET_memcmp (&v6.sin6_addr, - &c6->sin6_addr)) && - (0 == (delta->ac & GNUNET_NAT_AC_EXTERN)) ) - continue; /* client bound to link-local, and the other address - does not match and is not an external IP */ - - /* OK, IP seems relevant, notify client */ - if (0 == htons (v6.sin6_port)) - v6.sin6_port = c6->sin6_port; - notify_client (delta->ac, - ch, - add, - &v6, - alen); + case AF_INET: + alen = sizeof(struct sockaddr_in); + GNUNET_memcpy(&v4, + &delta->addr, + alen); + + /* Check for client notifications */ + for (unsigned int i = 0; i < ch->num_caddrs; i++) + { + const struct sockaddr_in *c4; + + if (AF_INET != ch->caddrs[i].ss.ss_family) + continue; /* IPv4 not relevant */ + c4 = (const struct sockaddr_in *)&ch->caddrs[i].ss; + if (match_ipv4("127.0.0.1", &c4->sin_addr, 8) && + (0 != c4->sin_addr.s_addr) && + (!match_ipv4("127.0.0.1", &v4.sin_addr, 8))) + continue; /* bound to loopback, but this is not loopback */ + if ((!match_ipv4("127.0.0.1", &c4->sin_addr, 8)) && + match_ipv4("127.0.0.1", &v4.sin_addr, 8)) + continue; /* bound to non-loopback, but this is loopback */ + if ((0 != (delta->ac & GNUNET_NAT_AC_EXTERN)) && + (0 != c4->sin_addr.s_addr) && + (!is_nat_v4(&v4.sin_addr))) + continue; /* based on external-IP, but this IP is not + from private address range. */ + if ((0 != GNUNET_memcmp(&v4.sin_addr, + &c4->sin_addr)) && + (0 != c4->sin_addr.s_addr) && + (!is_nat_v4(&c4->sin_addr))) + continue; /* this IP is not from private address range, + and IP does not match. */ + + /* OK, IP seems relevant, notify client */ + if (0 == htons(v4.sin_port)) + v4.sin_port = c4->sin_port; + notify_client(delta->ac, + ch, + add, + &v4, + alen); + } + break; + + case AF_INET6: + alen = sizeof(struct sockaddr_in6); + GNUNET_memcpy(&v6, + &delta->addr, + alen); + for (unsigned int i = 0; i < ch->num_caddrs; i++) + { + const struct sockaddr_in6 *c6; + + if (AF_INET6 != ch->caddrs[i].ss.ss_family) + continue; /* IPv4 not relevant */ + c6 = (const struct sockaddr_in6 *)&ch->caddrs[i].ss; + if (match_ipv6("::1", &c6->sin6_addr, 128) && + (0 != GNUNET_memcmp(&c6->sin6_addr, + &in6addr_any)) && + (!match_ipv6("::1", &v6.sin6_addr, 128))) + continue; /* bound to loopback, but this is not loopback */ + if ((!match_ipv6("::1", &c6->sin6_addr, 128)) && + match_ipv6("::1", &v6.sin6_addr, 128)) + continue; /* bound to non-loopback, but this is loopback */ + if ((0 != (delta->ac & GNUNET_NAT_AC_EXTERN)) && + (0 != GNUNET_memcmp(&c6->sin6_addr, + &in6addr_any)) && + (!is_nat_v6(&v6.sin6_addr))) + continue; /* based on external-IP, but this IP is not + from private address range. */ + if ((0 != GNUNET_memcmp(&v6.sin6_addr, + &c6->sin6_addr)) && + (0 != GNUNET_memcmp(&c6->sin6_addr, + &in6addr_any)) && + (!is_nat_v6(&c6->sin6_addr))) + continue; /* this IP is not from private address range, + and IP does not match. */ + if ((match_ipv6("fe80::", &c6->sin6_addr, 10)) && + (0 != GNUNET_memcmp(&c6->sin6_addr, + &in6addr_any)) && + (0 != GNUNET_memcmp(&v6.sin6_addr, + &c6->sin6_addr)) && + (0 == (delta->ac & GNUNET_NAT_AC_EXTERN))) + continue; /* client bound to link-local, and the other address + does not match and is not an external IP */ + + /* OK, IP seems relevant, notify client */ + if (0 == htons(v6.sin6_port)) + v6.sin6_port = c6->sin6_port; + notify_client(delta->ac, + ch, + add, + &v6, + alen); + } + break; + + default: + GNUNET_break(0); + return; } - break; - default: - GNUNET_break (0); - return; - } } @@ -814,15 +809,15 @@ check_notify_client (struct LocalAddressList *delta, * @param add #GNUNET_YES to add, #GNUNET_NO to remove */ static void -notify_clients (struct LocalAddressList *delta, - int add) +notify_clients(struct LocalAddressList *delta, + int add) { for (struct ClientHandle *ch = ch_head; NULL != ch; ch = ch->next) - check_notify_client (delta, - ch, - add); + check_notify_client(delta, + ch, + add); } @@ -835,74 +830,74 @@ notify_clients (struct LocalAddressList *delta, * @param add #GNUNET_YES to add, #GNUNET_NO to remove */ static void -notify_client_external_ipv4_change (void *cls, - const struct in_addr *v4, - int add) +notify_client_external_ipv4_change(void *cls, + const struct in_addr *v4, + int add) { struct ClientHandle *ch = cls; struct sockaddr_in sa; int have_v4; /* (0) check if this impacts 'hole_external' */ - if ( (NULL != ch->hole_external) && - (0 == strcasecmp (ch->hole_external, - "AUTO")) ) - { - struct LocalAddressList lal; - struct sockaddr_in *s4; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Detected eternal IP, can now back-fill AUTO:%u in hole punching specification of `%s'\n", - (unsigned int) ch->ext_dns_port, - ch->section_name); - memset (&lal, 0, sizeof (lal)); - s4 = (struct sockaddr_in *) &lal.addr; - s4->sin_family = AF_INET; - s4->sin_port = htons (ch->ext_dns_port); - s4->sin_addr = *v4; - lal.af = AF_INET; - lal.ac = GNUNET_NAT_AC_GLOBAL | GNUNET_NAT_AC_MANUAL; - check_notify_client (&lal, - ch, - add); - } + if ((NULL != ch->hole_external) && + (0 == strcasecmp(ch->hole_external, + "AUTO"))) + { + struct LocalAddressList lal; + struct sockaddr_in *s4; + + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "Detected eternal IP, can now back-fill AUTO:%u in hole punching specification of `%s'\n", + (unsigned int)ch->ext_dns_port, + ch->section_name); + memset(&lal, 0, sizeof(lal)); + s4 = (struct sockaddr_in *)&lal.addr; + s4->sin_family = AF_INET; + s4->sin_port = htons(ch->ext_dns_port); + s4->sin_addr = *v4; + lal.af = AF_INET; + lal.ac = GNUNET_NAT_AC_GLOBAL | GNUNET_NAT_AC_MANUAL; + check_notify_client(&lal, + ch, + add); + } /* (1) check if client cares. */ - if (! ch->natted_address) + if (!ch->natted_address) return; have_v4 = GNUNET_NO; - for (unsigned int i=0;inum_caddrs;i++) - { - const struct sockaddr_storage *ss = &ch->caddrs[i].ss; - - if (AF_INET != ss->ss_family) - continue; - have_v4 = GNUNET_YES; - break; - } + for (unsigned int i = 0; i < ch->num_caddrs; i++) + { + const struct sockaddr_storage *ss = &ch->caddrs[i].ss; + + if (AF_INET != ss->ss_family) + continue; + have_v4 = GNUNET_YES; + break; + } if (GNUNET_NO == have_v4) return; /* IPv6-only */ /* (2) build address info */ - memset (&sa, - 0, - sizeof (sa)); + memset(&sa, + 0, + sizeof(sa)); sa.sin_family = AF_INET; sa.sin_addr = *v4; - sa.sin_port = htons (0); + sa.sin_port = htons(0); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Detected eternal IP %s, notifying client of external IP (without port)\n", - GNUNET_a2s ((const struct sockaddr *) &sa, - sizeof (sa))); + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "Detected eternal IP %s, notifying client of external IP (without port)\n", + GNUNET_a2s((const struct sockaddr *)&sa, + sizeof(sa))); /* (3) notify client of change */ - notify_client (is_nat_v4 (v4) - ? GNUNET_NAT_AC_EXTERN | GNUNET_NAT_AC_LAN - : GNUNET_NAT_AC_EXTERN | GNUNET_NAT_AC_GLOBAL, - ch, - add, - &sa, - sizeof (sa)); + notify_client(is_nat_v4(v4) + ? GNUNET_NAT_AC_EXTERN | GNUNET_NAT_AC_LAN + : GNUNET_NAT_AC_EXTERN | GNUNET_NAT_AC_GLOBAL, + ch, + add, + &sa, + sizeof(sa)); } @@ -914,54 +909,54 @@ notify_client_external_ipv4_change (void *cls, * @param ra IP address of the peer who wants us to connect to it */ static void -reversal_callback (void *cls, - const struct sockaddr_in *ra) +reversal_callback(void *cls, + const struct sockaddr_in *ra) { struct LocalAddressList *lal = cls; const struct sockaddr_in *l4; - GNUNET_assert (AF_INET == lal->af); - l4 = (const struct sockaddr_in *) &lal->addr; + GNUNET_assert(AF_INET == lal->af); + l4 = (const struct sockaddr_in *)&lal->addr; for (struct ClientHandle *ch = ch_head; NULL != ch; ch = ch->next) - { - struct GNUNET_NAT_ConnectionReversalRequestedMessage *crrm; - struct GNUNET_MQ_Envelope *env; - int match; - - /* Check if client is in applicable range for ICMP NAT traversal - for this local address */ - if (! ch->natted_address) - continue; - match = GNUNET_NO; - for (unsigned int i=0;inum_caddrs;i++) { - struct ClientAddress *ca = &ch->caddrs[i]; - const struct sockaddr_in *c4; - - if (AF_INET != ca->ss.ss_family) - continue; - c4 = (const struct sockaddr_in *) &ca->ss; - if ( (0 != c4->sin_addr.s_addr) && - (l4->sin_addr.s_addr != c4->sin_addr.s_addr) ) - continue; - match = GNUNET_YES; - break; + struct GNUNET_NAT_ConnectionReversalRequestedMessage *crrm; + struct GNUNET_MQ_Envelope *env; + int match; + + /* Check if client is in applicable range for ICMP NAT traversal + for this local address */ + if (!ch->natted_address) + continue; + match = GNUNET_NO; + for (unsigned int i = 0; i < ch->num_caddrs; i++) + { + struct ClientAddress *ca = &ch->caddrs[i]; + const struct sockaddr_in *c4; + + if (AF_INET != ca->ss.ss_family) + continue; + c4 = (const struct sockaddr_in *)&ca->ss; + if ((0 != c4->sin_addr.s_addr) && + (l4->sin_addr.s_addr != c4->sin_addr.s_addr)) + continue; + match = GNUNET_YES; + break; + } + if (!match) + continue; + + /* Notify applicable client about connection reversal request */ + env = GNUNET_MQ_msg_extra(crrm, + sizeof(struct sockaddr_in), + GNUNET_MESSAGE_TYPE_NAT_CONNECTION_REVERSAL_REQUESTED); + GNUNET_memcpy(&crrm[1], + ra, + sizeof(struct sockaddr_in)); + GNUNET_MQ_send(ch->mq, + env); } - if (! match) - continue; - - /* Notify applicable client about connection reversal request */ - env = GNUNET_MQ_msg_extra (crrm, - sizeof (struct sockaddr_in), - GNUNET_MESSAGE_TYPE_NAT_CONNECTION_REVERSAL_REQUESTED); - GNUNET_memcpy (&crrm[1], - ra, - sizeof (struct sockaddr_in)); - GNUNET_MQ_send (ch->mq, - env); - } } @@ -971,104 +966,104 @@ reversal_callback (void *cls, * @param cls NULL */ static void -run_scan (void *cls) +run_scan(void *cls) { struct IfcProcContext ifc_ctx; int found; int have_nat; struct LocalAddressList *lnext; - scan_task = GNUNET_SCHEDULER_add_delayed (SCAN_FREQ, - &run_scan, - NULL); - memset (&ifc_ctx, - 0, - sizeof (ifc_ctx)); - GNUNET_OS_network_interfaces_list (&ifc_proc, - &ifc_ctx); + scan_task = GNUNET_SCHEDULER_add_delayed(SCAN_FREQ, + &run_scan, + NULL); + memset(&ifc_ctx, + 0, + sizeof(ifc_ctx)); + GNUNET_OS_network_interfaces_list(&ifc_proc, + &ifc_ctx); /* remove addresses that disappeared */ for (struct LocalAddressList *lal = lal_head; NULL != lal; lal = lnext) - { - lnext = lal->next; - found = GNUNET_NO; - for (struct LocalAddressList *pos = ifc_ctx.lal_head; - NULL != pos; - pos = pos->next) { - if ( (pos->af == lal->af) && - (0 == memcmp (&lal->addr, - &pos->addr, - (AF_INET == lal->af) - ? sizeof (struct sockaddr_in) - : sizeof (struct sockaddr_in6))) ) - { - found = GNUNET_YES; - } + lnext = lal->next; + found = GNUNET_NO; + for (struct LocalAddressList *pos = ifc_ctx.lal_head; + NULL != pos; + pos = pos->next) + { + if ((pos->af == lal->af) && + (0 == memcmp(&lal->addr, + &pos->addr, + (AF_INET == lal->af) + ? sizeof(struct sockaddr_in) + : sizeof(struct sockaddr_in6)))) + { + found = GNUNET_YES; + } + } + if (GNUNET_NO == found) + { + notify_clients(lal, + GNUNET_NO); + free_lal(lal); + } } - if (GNUNET_NO == found) - { - notify_clients (lal, - GNUNET_NO); - free_lal (lal); - } - } /* add addresses that appeared */ have_nat = GNUNET_NO; for (struct LocalAddressList *pos = ifc_ctx.lal_head; NULL != pos; pos = ifc_ctx.lal_head) - { - found = GNUNET_NO; - if (GNUNET_NAT_AC_LAN == (GNUNET_NAT_AC_LAN & pos->ac)) - have_nat = GNUNET_YES; - for (struct LocalAddressList *lal = lal_head; - NULL != lal; - lal = lal->next) - { - if ( (pos->af == lal->af) && - (0 == memcmp (&lal->addr, - &pos->addr, - (AF_INET == lal->af) - ? sizeof (struct sockaddr_in) - : sizeof (struct sockaddr_in6))) ) - found = GNUNET_YES; - } - GNUNET_CONTAINER_DLL_remove (ifc_ctx.lal_head, - ifc_ctx.lal_tail, - pos); - if (GNUNET_YES == found) - { - GNUNET_free (pos); - } - else { - notify_clients (pos, - GNUNET_YES); - GNUNET_CONTAINER_DLL_insert (lal_head, - lal_tail, - pos); - if ( (AF_INET == pos->af) && - (NULL == pos->hc) && - (0 != (GNUNET_NAT_AC_LAN & pos->ac)) ) - { - const struct sockaddr_in *s4 - = (const struct sockaddr_in *) &pos->addr; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Found NATed local address %s, starting NAT server\n", - GNUNET_a2s ((const struct sockaddr *) &pos->addr, - sizeof (*s4))); - pos->hc = GN_start_gnunet_nat_server_ (&s4->sin_addr, - &reversal_callback, - pos, - cfg); - } + found = GNUNET_NO; + if (GNUNET_NAT_AC_LAN == (GNUNET_NAT_AC_LAN & pos->ac)) + have_nat = GNUNET_YES; + for (struct LocalAddressList *lal = lal_head; + NULL != lal; + lal = lal->next) + { + if ((pos->af == lal->af) && + (0 == memcmp(&lal->addr, + &pos->addr, + (AF_INET == lal->af) + ? sizeof(struct sockaddr_in) + : sizeof(struct sockaddr_in6)))) + found = GNUNET_YES; + } + GNUNET_CONTAINER_DLL_remove(ifc_ctx.lal_head, + ifc_ctx.lal_tail, + pos); + if (GNUNET_YES == found) + { + GNUNET_free(pos); + } + else + { + notify_clients(pos, + GNUNET_YES); + GNUNET_CONTAINER_DLL_insert(lal_head, + lal_tail, + pos); + if ((AF_INET == pos->af) && + (NULL == pos->hc) && + (0 != (GNUNET_NAT_AC_LAN & pos->ac))) + { + const struct sockaddr_in *s4 + = (const struct sockaddr_in *)&pos->addr; + + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "Found NATed local address %s, starting NAT server\n", + GNUNET_a2s((const struct sockaddr *)&pos->addr, + sizeof(*s4))); + pos->hc = GN_start_gnunet_nat_server_(&s4->sin_addr, + &reversal_callback, + pos, + cfg); + } + } } - } - GN_nat_status_changed (have_nat); + GN_nat_status_changed(have_nat); } @@ -1084,81 +1079,91 @@ run_scan (void *cls) * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code */ static void -upnp_addr_change_cb (void *cls, - int add_remove, - const struct sockaddr *addr, - socklen_t addrlen, - enum GNUNET_NAT_StatusCode result) +upnp_addr_change_cb(void *cls, + int add_remove, + const struct sockaddr *addr, + socklen_t addrlen, + enum GNUNET_NAT_StatusCode result) { struct ClientHandle *ch = cls; enum GNUNET_NAT_AddressClass ac; switch (result) - { - case GNUNET_NAT_ERROR_SUCCESS: - GNUNET_assert (NULL != addr); - break; - case GNUNET_NAT_ERROR_UPNPC_FAILED: - case GNUNET_NAT_ERROR_UPNPC_TIMEOUT: - case GNUNET_NAT_ERROR_IPC_FAILURE: - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Running upnpc failed: %d\n", - result); - return; - case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND: - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "external-ip binary not found\n"); - return; - case GNUNET_NAT_ERROR_UPNPC_NOT_FOUND: - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "upnpc binary not found\n"); - return; - case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED: - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "external-ip binary could not be run\n"); - return; - case GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED: - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "upnpc failed to create port mapping\n"); - return; - case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID: - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Invalid output from upnpc\n"); - return; - case GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID: - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Invalid address returned by upnpc\n"); - return; - default: - GNUNET_break (0); /* should not be possible */ - return; - } + { + case GNUNET_NAT_ERROR_SUCCESS: + GNUNET_assert(NULL != addr); + break; + + case GNUNET_NAT_ERROR_UPNPC_FAILED: + case GNUNET_NAT_ERROR_UPNPC_TIMEOUT: + case GNUNET_NAT_ERROR_IPC_FAILURE: + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "Running upnpc failed: %d\n", + result); + return; + + case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND: + GNUNET_log(GNUNET_ERROR_TYPE_INFO, + "external-ip binary not found\n"); + return; + + case GNUNET_NAT_ERROR_UPNPC_NOT_FOUND: + GNUNET_log(GNUNET_ERROR_TYPE_INFO, + "upnpc binary not found\n"); + return; + + case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED: + GNUNET_log(GNUNET_ERROR_TYPE_WARNING, + "external-ip binary could not be run\n"); + return; + + case GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED: + GNUNET_log(GNUNET_ERROR_TYPE_WARNING, + "upnpc failed to create port mapping\n"); + return; + + case GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID: + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "Invalid output from upnpc\n"); + return; + + case GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID: + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "Invalid address returned by upnpc\n"); + return; + + default: + GNUNET_break(0); /* should not be possible */ + return; + } switch (addr->sa_family) - { - case AF_INET: - ac = is_nat_v4 (&((const struct sockaddr_in *) addr)->sin_addr) - ? GNUNET_NAT_AC_LAN - : GNUNET_NAT_AC_EXTERN; - break; - case AF_INET6: - ac = is_nat_v6 (&((const struct sockaddr_in6 *) addr)->sin6_addr) - ? GNUNET_NAT_AC_LAN - : GNUNET_NAT_AC_EXTERN; - break; - default: - GNUNET_break (0); - return; - } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "upnp external address %s: %s\n", - add_remove ? "added" : "removed", - GNUNET_a2s (addr, - addrlen)); - notify_client (ac, - ch, - add_remove, - addr, - addrlen); + { + case AF_INET: + ac = is_nat_v4(&((const struct sockaddr_in *)addr)->sin_addr) + ? GNUNET_NAT_AC_LAN + : GNUNET_NAT_AC_EXTERN; + break; + + case AF_INET6: + ac = is_nat_v6(&((const struct sockaddr_in6 *)addr)->sin6_addr) + ? GNUNET_NAT_AC_LAN + : GNUNET_NAT_AC_EXTERN; + break; + + default: + GNUNET_break(0); + return; + } + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "upnp external address %s: %s\n", + add_remove ? "added" : "removed", + GNUNET_a2s(addr, + addrlen)); + notify_client(ac, + ch, + add_remove, + addr, + addrlen); } @@ -1171,7 +1176,7 @@ upnp_addr_change_cb (void *cls, * @param ch client handle to act upon */ static void -dyndns_lookup (void *cls); +dyndns_lookup(void *cls); /** @@ -1184,9 +1189,9 @@ dyndns_lookup (void *cls); * @param addrlen number of bytes in @a addr */ static void -process_external_ip (void *cls, - const struct sockaddr *addr, - socklen_t addrlen) +process_external_ip(void *cls, + const struct sockaddr *addr, + socklen_t addrlen) { struct ClientHandle *ch = cls; struct LocalAddressList *lal; @@ -1195,84 +1200,86 @@ process_external_ip (void *cls, struct sockaddr_in6 *v6; if (NULL == addr) - { - struct LocalAddressList *laln; - - ch->ext_dns = NULL; - ch->ext_dns_task - = GNUNET_SCHEDULER_add_delayed (dyndns_frequency, - &dyndns_lookup, - ch); - /* Current iteration is over, remove 'old' IPs now */ - for (lal = ch->ext_addr_head; NULL != lal; lal = laln) { - laln = lal->next; - if (GNUNET_YES == lal->old) - { - GNUNET_CONTAINER_DLL_remove (ch->ext_addr_head, - ch->ext_addr_tail, - lal); - check_notify_client (lal, - ch, - GNUNET_NO); - GNUNET_free (lal); - } + struct LocalAddressList *laln; + + ch->ext_dns = NULL; + ch->ext_dns_task + = GNUNET_SCHEDULER_add_delayed(dyndns_frequency, + &dyndns_lookup, + ch); + /* Current iteration is over, remove 'old' IPs now */ + for (lal = ch->ext_addr_head; NULL != lal; lal = laln) + { + laln = lal->next; + if (GNUNET_YES == lal->old) + { + GNUNET_CONTAINER_DLL_remove(ch->ext_addr_head, + ch->ext_addr_tail, + lal); + check_notify_client(lal, + ch, + GNUNET_NO); + GNUNET_free(lal); + } + } + return; } - return; - } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Got IP `%s' for external address `%s'\n", - GNUNET_a2s (addr, - addrlen), - ch->hole_external); + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "Got IP `%s' for external address `%s'\n", + GNUNET_a2s(addr, + addrlen), + ch->hole_external); /* build sockaddr storage with port number */ - memset (&ss, - 0, - sizeof (ss)); - GNUNET_memcpy (&ss, - addr, - addrlen); + memset(&ss, + 0, + sizeof(ss)); + GNUNET_memcpy(&ss, + addr, + addrlen); switch (addr->sa_family) - { - case AF_INET: - v4 = (struct sockaddr_in *) &ss; - v4->sin_port = htons (ch->ext_dns_port); - break; - case AF_INET6: - v6 = (struct sockaddr_in6 *) &ss; - v6->sin6_port = htons (ch->ext_dns_port); - break; - default: - GNUNET_break (0); - return; - } + { + case AF_INET: + v4 = (struct sockaddr_in *)&ss; + v4->sin_port = htons(ch->ext_dns_port); + break; + + case AF_INET6: + v6 = (struct sockaddr_in6 *)&ss; + v6->sin6_port = htons(ch->ext_dns_port); + break; + + default: + GNUNET_break(0); + return; + } /* See if 'ss' matches any of our known addresses */ for (lal = ch->ext_addr_head; NULL != lal; lal = lal->next) - { - if (GNUNET_NO == lal->old) - continue; /* already processed, skip */ - if ( (addr->sa_family == lal->addr.ss_family) && - (0 == memcmp (&ss, - &lal->addr, - addrlen)) ) { - /* Address unchanged, remember so we do not remove */ - lal->old = GNUNET_NO; - return; /* done here */ + if (GNUNET_NO == lal->old) + continue; /* already processed, skip */ + if ((addr->sa_family == lal->addr.ss_family) && + (0 == memcmp(&ss, + &lal->addr, + addrlen))) + { + /* Address unchanged, remember so we do not remove */ + lal->old = GNUNET_NO; + return; /* done here */ + } } - } /* notify client, and remember IP for later removal! */ - lal = GNUNET_new (struct LocalAddressList); + lal = GNUNET_new(struct LocalAddressList); lal->addr = ss; lal->af = ss.ss_family; lal->ac = GNUNET_NAT_AC_GLOBAL | GNUNET_NAT_AC_MANUAL; - GNUNET_CONTAINER_DLL_insert (ch->ext_addr_head, - ch->ext_addr_tail, - lal); - check_notify_client (lal, - ch, - GNUNET_YES); + GNUNET_CONTAINER_DLL_insert(ch->ext_addr_head, + ch->ext_addr_tail, + lal); + check_notify_client(lal, + ch, + GNUNET_YES); } @@ -1285,24 +1292,24 @@ process_external_ip (void *cls, * @param ch client handle to act upon */ static void -dyndns_lookup (void *cls) +dyndns_lookup(void *cls) { struct ClientHandle *ch = cls; struct LocalAddressList *lal; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Performing DNS lookup for punched hole given for `%s' as `%s:%u'\n", - ch->section_name, - ch->hole_external, - (unsigned int) ch->ext_dns_port); + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "Performing DNS lookup for punched hole given for `%s' as `%s:%u'\n", + ch->section_name, + ch->hole_external, + (unsigned int)ch->ext_dns_port); for (lal = ch->ext_addr_head; NULL != lal; lal = lal->next) lal->old = GNUNET_YES; ch->ext_dns_task = NULL; - ch->ext_dns = GNUNET_RESOLVER_ip_get (ch->hole_external, - AF_UNSPEC, - GNUNET_TIME_UNIT_MINUTES, - &process_external_ip, - ch); + ch->ext_dns = GNUNET_RESOLVER_ip_get(ch->hole_external, + AF_UNSPEC, + GNUNET_TIME_UNIT_MINUTES, + &process_external_ip, + ch); } @@ -1318,105 +1325,105 @@ dyndns_lookup (void *cls) * @param ch client handle to act upon */ static void -lookup_hole_external (struct ClientHandle *ch) +lookup_hole_external(struct ClientHandle *ch) { char *port; unsigned int pnum; struct sockaddr_in *s4; struct LocalAddressList *lal; - port = strrchr (ch->hole_external, ':'); + port = strrchr(ch->hole_external, ':'); if (NULL == port) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Malformed punched hole specification `%s' (lacks port)\n"), - ch->hole_external); - return; - } - if ( (1 != sscanf (port + 1, - "%u", - &pnum)) || - (pnum > 65535) ) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Invalid port number in punched hole specification `%s' (lacks port)\n"), - port + 1); - return; - } - ch->ext_dns_port = (uint16_t) pnum; + { + GNUNET_log(GNUNET_ERROR_TYPE_WARNING, + _("Malformed punched hole specification `%s' (lacks port)\n"), + ch->hole_external); + return; + } + if ((1 != sscanf(port + 1, + "%u", + &pnum)) || + (pnum > 65535)) + { + GNUNET_log(GNUNET_ERROR_TYPE_WARNING, + _("Invalid port number in punched hole specification `%s' (lacks port)\n"), + port + 1); + return; + } + ch->ext_dns_port = (uint16_t)pnum; *port = '\0'; - lal = GNUNET_new (struct LocalAddressList); + lal = GNUNET_new(struct LocalAddressList); if ('[' == *ch->hole_external) - { - struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) &lal->addr; + { + struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)&lal->addr; + + s6->sin6_family = AF_INET6; + if (']' != (ch->hole_external[strlen(ch->hole_external) - 1])) + { + GNUNET_log(GNUNET_ERROR_TYPE_WARNING, + _("Malformed punched hole specification `%s' (lacks `]')\n"), + ch->hole_external); + GNUNET_free(lal); + return; + } + ch->hole_external[strlen(ch->hole_external) - 1] = '\0'; + if (1 != inet_pton(AF_INET6, + ch->hole_external + 1, + &s6->sin6_addr)) + { + GNUNET_log(GNUNET_ERROR_TYPE_WARNING, + _("Malformed punched hole specification `%s' (IPv6 address invalid)"), + ch->hole_external + 1); + GNUNET_free(lal); + return; + } + s6->sin6_port = htons(ch->ext_dns_port); + lal->af = AF_INET6; + lal->ac = GNUNET_NAT_AC_GLOBAL | GNUNET_NAT_AC_MANUAL; + GNUNET_CONTAINER_DLL_insert(ch->ext_addr_head, + ch->ext_addr_tail, + lal); + check_notify_client(lal, + ch, + GNUNET_YES); + return; + } - s6->sin6_family = AF_INET6; - if (']' != (ch->hole_external[strlen(ch->hole_external)-1])) + s4 = (struct sockaddr_in *)&lal->addr; + s4->sin_family = AF_INET; + if (1 == inet_pton(AF_INET, + ch->hole_external, + &s4->sin_addr)) { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Malformed punched hole specification `%s' (lacks `]')\n"), - ch->hole_external); - GNUNET_free (lal); + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "IPv4 punched hole given for `%s' via `%s:%u'\n", + ch->section_name, + ch->hole_external, + (unsigned int)ch->ext_dns_port); + s4->sin_port = htons(ch->ext_dns_port); + lal->af = AF_INET; + lal->ac = GNUNET_NAT_AC_GLOBAL | GNUNET_NAT_AC_MANUAL; + GNUNET_CONTAINER_DLL_insert(ch->ext_addr_head, + ch->ext_addr_tail, + lal); + check_notify_client(lal, + ch, + GNUNET_YES); return; } - ch->hole_external[strlen(ch->hole_external)-1] = '\0'; - if (1 != inet_pton (AF_INET6, - ch->hole_external + 1, - &s6->sin6_addr)) + if (0 == strcasecmp(ch->hole_external, + "AUTO")) { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Malformed punched hole specification `%s' (IPv6 address invalid)"), - ch->hole_external + 1); - GNUNET_free (lal); + /* handled in #notify_client_external_ipv4_change() */ + GNUNET_free(lal); return; } - s6->sin6_port = htons (ch->ext_dns_port); - lal->af = AF_INET6; - lal->ac = GNUNET_NAT_AC_GLOBAL | GNUNET_NAT_AC_MANUAL; - GNUNET_CONTAINER_DLL_insert (ch->ext_addr_head, - ch->ext_addr_tail, - lal); - check_notify_client (lal, - ch, - GNUNET_YES); - return; - } - - s4 = (struct sockaddr_in *) &lal->addr; - s4->sin_family = AF_INET; - if (1 == inet_pton (AF_INET, - ch->hole_external, - &s4->sin_addr)) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "IPv4 punched hole given for `%s' via `%s:%u'\n", - ch->section_name, - ch->hole_external, - (unsigned int) ch->ext_dns_port); - s4->sin_port = htons (ch->ext_dns_port); - lal->af = AF_INET; - lal->ac = GNUNET_NAT_AC_GLOBAL | GNUNET_NAT_AC_MANUAL; - GNUNET_CONTAINER_DLL_insert (ch->ext_addr_head, - ch->ext_addr_tail, - lal); - check_notify_client (lal, - ch, - GNUNET_YES); - return; - } - if (0 == strcasecmp (ch->hole_external, - "AUTO")) - { - /* handled in #notify_client_external_ipv4_change() */ - GNUNET_free (lal); - return; - } /* got a DNS name, trigger lookup! */ - GNUNET_free (lal); + GNUNET_free(lal); ch->ext_dns_task - = GNUNET_SCHEDULER_add_now (&dyndns_lookup, - ch); + = GNUNET_SCHEDULER_add_now(&dyndns_lookup, + ch); } @@ -1428,132 +1435,134 @@ lookup_hole_external (struct ClientHandle *ch) * @param message the message received */ static void -handle_register (void *cls, - const struct GNUNET_NAT_RegisterMessage *message) +handle_register(void *cls, + const struct GNUNET_NAT_RegisterMessage *message) { struct ClientHandle *ch = cls; const char *off; size_t left; - if ( (0 != ch->proto) || - (NULL != ch->caddrs) ) - { - /* double registration not allowed */ - GNUNET_break (0); - GNUNET_SERVICE_client_drop (ch->client); - return; - } - ch->flags = message->flags; - ch->proto = message->proto; - ch->num_caddrs = ntohs (message->num_addrs); - ch->caddrs = GNUNET_new_array (ch->num_caddrs, - struct ClientAddress); - left = ntohs (message->header.size) - sizeof (*message); - off = (const char *) &message[1]; - for (unsigned int i=0;inum_caddrs;i++) - { - const struct sockaddr *sa = (const struct sockaddr *) off; - size_t alen; - uint16_t port; - int is_nat; - - if (sizeof (sa_family_t) > left) + if ((0 != ch->proto) || + (NULL != ch->caddrs)) { - GNUNET_break (0); - GNUNET_SERVICE_client_drop (ch->client); + /* double registration not allowed */ + GNUNET_break(0); + GNUNET_SERVICE_client_drop(ch->client); return; } - is_nat = GNUNET_NO; - switch (sa->sa_family) + ch->flags = message->flags; + ch->proto = message->proto; + ch->num_caddrs = ntohs(message->num_addrs); + ch->caddrs = GNUNET_new_array(ch->num_caddrs, + struct ClientAddress); + left = ntohs(message->header.size) - sizeof(*message); + off = (const char *)&message[1]; + for (unsigned int i = 0; i < ch->num_caddrs; i++) { - case AF_INET: - { - struct sockaddr_in s4; - - GNUNET_memcpy (&s4, - off, - sizeof (struct sockaddr_in)); - alen = sizeof (struct sockaddr_in); - if (is_nat_v4 (&s4.sin_addr)) - is_nat = GNUNET_YES; - port = ntohs (s4.sin_port); - } - break; - case AF_INET6: - { - struct sockaddr_in6 s6; - - GNUNET_memcpy (&s6, - off, - sizeof (struct sockaddr_in6)); - alen = sizeof (struct sockaddr_in6); - if (is_nat_v6 (&s6.sin6_addr)) - is_nat = GNUNET_YES; - port = ntohs (s6.sin6_port); - } - break; + const struct sockaddr *sa = (const struct sockaddr *)off; + size_t alen; + uint16_t port; + int is_nat; + + if (sizeof(sa_family_t) > left) + { + GNUNET_break(0); + GNUNET_SERVICE_client_drop(ch->client); + return; + } + is_nat = GNUNET_NO; + switch (sa->sa_family) + { + case AF_INET: + { + struct sockaddr_in s4; + + GNUNET_memcpy(&s4, + off, + sizeof(struct sockaddr_in)); + alen = sizeof(struct sockaddr_in); + if (is_nat_v4(&s4.sin_addr)) + is_nat = GNUNET_YES; + port = ntohs(s4.sin_port); + } + break; + + case AF_INET6: + { + struct sockaddr_in6 s6; + + GNUNET_memcpy(&s6, + off, + sizeof(struct sockaddr_in6)); + alen = sizeof(struct sockaddr_in6); + if (is_nat_v6(&s6.sin6_addr)) + is_nat = GNUNET_YES; + port = ntohs(s6.sin6_port); + } + break; + #if AF_UNIX - case AF_UNIX: - alen = sizeof (struct sockaddr_un); - port = 0; - break; + case AF_UNIX: + alen = sizeof(struct sockaddr_un); + port = 0; + break; #endif - default: - GNUNET_break (0); - GNUNET_SERVICE_client_drop (ch->client); - return; - } - /* store address */ - GNUNET_assert (alen <= left); - GNUNET_assert (alen <= sizeof (struct sockaddr_storage)); - GNUNET_memcpy (&ch->caddrs[i].ss, - off, - alen); - - /* If applicable, try UPNPC NAT punching */ - if ( (is_nat) && - (enable_upnp) && - ( (IPPROTO_TCP == ch->proto) || - (IPPROTO_UDP == ch->proto) ) ) - { - ch->natted_address = GNUNET_YES; - ch->caddrs[i].mh - = GNUNET_NAT_mini_map_start (port, - IPPROTO_TCP == ch->proto, - &upnp_addr_change_cb, - ch); + default: + GNUNET_break(0); + GNUNET_SERVICE_client_drop(ch->client); + return; + } + /* store address */ + GNUNET_assert(alen <= left); + GNUNET_assert(alen <= sizeof(struct sockaddr_storage)); + GNUNET_memcpy(&ch->caddrs[i].ss, + off, + alen); + + /* If applicable, try UPNPC NAT punching */ + if ((is_nat) && + (enable_upnp) && + ((IPPROTO_TCP == ch->proto) || + (IPPROTO_UDP == ch->proto))) + { + ch->natted_address = GNUNET_YES; + ch->caddrs[i].mh + = GNUNET_NAT_mini_map_start(port, + IPPROTO_TCP == ch->proto, + &upnp_addr_change_cb, + ch); + } + + off += alen; } - off += alen; - } - ch->section_name - = GNUNET_strndup (off, - ntohs (message->str_len)); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Received REGISTER message from client for subsystem `%s'\n", - ch->section_name); + = GNUNET_strndup(off, + ntohs(message->str_len)); + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "Received REGISTER message from client for subsystem `%s'\n", + ch->section_name); if (GNUNET_OK == - GNUNET_CONFIGURATION_get_value_string (cfg, - ch->section_name, - "HOLE_EXTERNAL", - &ch->hole_external)) - lookup_hole_external (ch); + GNUNET_CONFIGURATION_get_value_string(cfg, + ch->section_name, + "HOLE_EXTERNAL", + &ch->hole_external)) + lookup_hole_external(ch); /* Actually send IP address list to client */ for (struct LocalAddressList *lal = lal_head; NULL != lal; lal = lal->next) - { - check_notify_client (lal, - ch, - GNUNET_YES); - } + { + check_notify_client(lal, + ch, + GNUNET_YES); + } /* Also consider IPv4 determined by `external-ip` */ ch->external_monitor - = GN_external_ipv4_monitor_start (¬ify_client_external_ipv4_change, - ch); - GNUNET_SERVICE_client_continue (ch->client); + = GN_external_ipv4_monitor_start(¬ify_client_external_ipv4_change, + ch); + GNUNET_SERVICE_client_continue(ch->client); } @@ -1566,22 +1575,22 @@ handle_register (void *cls, * @return #GNUNET_OK if message is well-formed */ static int -check_stun (void *cls, - const struct GNUNET_NAT_HandleStunMessage *message) +check_stun(void *cls, + const struct GNUNET_NAT_HandleStunMessage *message) { - size_t sa_len = ntohs (message->sender_addr_size); - size_t expect = sa_len + ntohs (message->payload_size); - - if (ntohs (message->header.size) - sizeof (*message) != expect) - { - GNUNET_break (0); - return GNUNET_SYSERR; - } - if (sa_len < sizeof (sa_family_t)) - { - GNUNET_break (0); - return GNUNET_SYSERR; - } + size_t sa_len = ntohs(message->sender_addr_size); + size_t expect = sa_len + ntohs(message->payload_size); + + if (ntohs(message->header.size) - sizeof(*message) != expect) + { + GNUNET_break(0); + return GNUNET_SYSERR; + } + if (sa_len < sizeof(sa_family_t)) + { + GNUNET_break(0); + return GNUNET_SYSERR; + } return GNUNET_OK; } @@ -1594,33 +1603,33 @@ check_stun (void *cls, * @param add #GNUNET_YES to add, #GNUNET_NO to remove */ static void -notify_clients_stun_change (const struct sockaddr_in *ip, - int add) +notify_clients_stun_change(const struct sockaddr_in *ip, + int add) { for (struct ClientHandle *ch = ch_head; NULL != ch; ch = ch->next) - { - struct sockaddr_in v4; - struct GNUNET_NAT_AddressChangeNotificationMessage *msg; - struct GNUNET_MQ_Envelope *env; - - if (! ch->natted_address) - continue; - v4 = *ip; - v4.sin_port = htons (0); - env = GNUNET_MQ_msg_extra (msg, - sizeof (v4), - GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE); - msg->add_remove = htonl ((int32_t) add); - msg->addr_class = htonl (GNUNET_NAT_AC_EXTERN | - GNUNET_NAT_AC_GLOBAL); - GNUNET_memcpy (&msg[1], - &v4, - sizeof (v4)); - GNUNET_MQ_send (ch->mq, - env); - } + { + struct sockaddr_in v4; + struct GNUNET_NAT_AddressChangeNotificationMessage *msg; + struct GNUNET_MQ_Envelope *env; + + if (!ch->natted_address) + continue; + v4 = *ip; + v4.sin_port = htons(0); + env = GNUNET_MQ_msg_extra(msg, + sizeof(v4), + GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE); + msg->add_remove = htonl((int32_t)add); + msg->addr_class = htonl(GNUNET_NAT_AC_EXTERN | + GNUNET_NAT_AC_GLOBAL); + GNUNET_memcpy(&msg[1], + &v4, + sizeof(v4)); + GNUNET_MQ_send(ch->mq, + env); + } } @@ -1632,17 +1641,17 @@ notify_clients_stun_change (const struct sockaddr_in *ip, * @param cls the `struct StunExternalIP` to drop */ static void -stun_ip_timeout (void *cls) +stun_ip_timeout(void *cls) { struct StunExternalIP *se = cls; se->timeout_task = NULL; - notify_clients_stun_change (&se->external_addr, - GNUNET_NO); - GNUNET_CONTAINER_DLL_remove (se_head, - se_tail, - se); - GNUNET_free (se); + notify_clients_stun_change(&se->external_addr, + GNUNET_NO); + GNUNET_CONTAINER_DLL_remove(se_head, + se_tail, + se); + GNUNET_free(se); } @@ -1654,106 +1663,107 @@ stun_ip_timeout (void *cls) * @param message the message received */ static void -handle_stun (void *cls, - const struct GNUNET_NAT_HandleStunMessage *message) +handle_stun(void *cls, + const struct GNUNET_NAT_HandleStunMessage *message) { struct ClientHandle *ch = cls; - const char *buf = (const char *) &message[1]; + const char *buf = (const char *)&message[1]; const struct sockaddr *sa; const void *payload; size_t sa_len; size_t payload_size; struct sockaddr_in external_addr; - sa_len = ntohs (message->sender_addr_size); - payload_size = ntohs (message->payload_size); - sa = (const struct sockaddr *) &buf[0]; - payload = (const struct sockaddr *) &buf[sa_len]; + sa_len = ntohs(message->sender_addr_size); + payload_size = ntohs(message->payload_size); + sa = (const struct sockaddr *)&buf[0]; + payload = (const struct sockaddr *)&buf[sa_len]; switch (sa->sa_family) - { - case AF_INET: - if (sa_len != sizeof (struct sockaddr_in)) - { - GNUNET_break (0); - GNUNET_SERVICE_client_drop (ch->client); - return; - } - break; - case AF_INET6: - if (sa_len != sizeof (struct sockaddr_in6)) { - GNUNET_break (0); - GNUNET_SERVICE_client_drop (ch->client); - return; + case AF_INET: + if (sa_len != sizeof(struct sockaddr_in)) + { + GNUNET_break(0); + GNUNET_SERVICE_client_drop(ch->client); + return; + } + break; + + case AF_INET6: + if (sa_len != sizeof(struct sockaddr_in6)) + { + GNUNET_break(0); + GNUNET_SERVICE_client_drop(ch->client); + return; + } + break; } - break; - } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Received HANDLE_STUN message from client\n"); + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "Received HANDLE_STUN message from client\n"); if (GNUNET_OK == - GNUNET_NAT_stun_handle_packet_ (payload, - payload_size, - &external_addr)) - { - /* We now know that a server at "sa" claims that - we are visible at IP "external_addr". - - We should (for some fixed period of time) tell - all of our clients that listen to a NAT'ed address - that they might want to consider the given 'external_ip' - as their public IP address (this includes TCP and UDP - clients, even if only UDP sends STUN requests). - - If we do not get a renewal, the "external_addr" should be - removed again. The timeout frequency should be configurable - (with a sane default), so that the UDP plugin can tell how - often to re-request STUN. - */ - struct StunExternalIP *se; - - /* Check if we had a prior response from this STUN server */ - for (se = se_head; NULL != se; se = se->next) + GNUNET_NAT_stun_handle_packet_(payload, + payload_size, + &external_addr)) { - if ( (se->stun_server_addr_len != sa_len) || - (0 != memcmp (sa, - &se->stun_server_addr, - sa_len)) ) - continue; /* different STUN server */ - if (0 != GNUNET_memcmp (&external_addr, - &se->external_addr)) - { - /* external IP changed, update! */ - notify_clients_stun_change (&se->external_addr, - GNUNET_NO); - se->external_addr = external_addr; - notify_clients_stun_change (&se->external_addr, - GNUNET_YES); - } - /* update timeout */ - GNUNET_SCHEDULER_cancel (se->timeout_task); - se->timeout_task - = GNUNET_SCHEDULER_add_delayed (stun_stale_timeout, - &stun_ip_timeout, - se); - return; + /* We now know that a server at "sa" claims that + we are visible at IP "external_addr". + + We should (for some fixed period of time) tell + all of our clients that listen to a NAT'ed address + that they might want to consider the given 'external_ip' + as their public IP address (this includes TCP and UDP + clients, even if only UDP sends STUN requests). + + If we do not get a renewal, the "external_addr" should be + removed again. The timeout frequency should be configurable + (with a sane default), so that the UDP plugin can tell how + often to re-request STUN. + */ + struct StunExternalIP *se; + + /* Check if we had a prior response from this STUN server */ + for (se = se_head; NULL != se; se = se->next) + { + if ((se->stun_server_addr_len != sa_len) || + (0 != memcmp(sa, + &se->stun_server_addr, + sa_len))) + continue; /* different STUN server */ + if (0 != GNUNET_memcmp(&external_addr, + &se->external_addr)) + { + /* external IP changed, update! */ + notify_clients_stun_change(&se->external_addr, + GNUNET_NO); + se->external_addr = external_addr; + notify_clients_stun_change(&se->external_addr, + GNUNET_YES); + } + /* update timeout */ + GNUNET_SCHEDULER_cancel(se->timeout_task); + se->timeout_task + = GNUNET_SCHEDULER_add_delayed(stun_stale_timeout, + &stun_ip_timeout, + se); + return; + } + /* STUN server is completely new, create fresh entry */ + se = GNUNET_new(struct StunExternalIP); + se->external_addr = external_addr; + GNUNET_memcpy(&se->stun_server_addr, + sa, + sa_len); + se->stun_server_addr_len = sa_len; + se->timeout_task = GNUNET_SCHEDULER_add_delayed(stun_stale_timeout, + &stun_ip_timeout, + se); + GNUNET_CONTAINER_DLL_insert(se_head, + se_tail, + se); + notify_clients_stun_change(&se->external_addr, + GNUNET_NO); } - /* STUN server is completely new, create fresh entry */ - se = GNUNET_new (struct StunExternalIP); - se->external_addr = external_addr; - GNUNET_memcpy (&se->stun_server_addr, - sa, - sa_len); - se->stun_server_addr_len = sa_len; - se->timeout_task = GNUNET_SCHEDULER_add_delayed (stun_stale_timeout, - &stun_ip_timeout, - se); - GNUNET_CONTAINER_DLL_insert (se_head, - se_tail, - se); - notify_clients_stun_change (&se->external_addr, - GNUNET_NO); - } - GNUNET_SERVICE_client_continue (ch->client); + GNUNET_SERVICE_client_continue(ch->client); } @@ -1767,18 +1777,18 @@ handle_stun (void *cls, * @return #GNUNET_OK if message is well-formed */ static int -check_request_connection_reversal (void *cls, - const struct GNUNET_NAT_RequestConnectionReversalMessage *message) +check_request_connection_reversal(void *cls, + const struct GNUNET_NAT_RequestConnectionReversalMessage *message) { size_t expect; - expect = ntohs (message->local_addr_size) - + ntohs (message->remote_addr_size); - if (ntohs (message->header.size) - sizeof (*message) != expect) - { - GNUNET_break (0); - return GNUNET_SYSERR; - } + expect = ntohs(message->local_addr_size) + + ntohs(message->remote_addr_size); + if (ntohs(message->header.size) - sizeof(*message) != expect) + { + GNUNET_break(0); + return GNUNET_SYSERR; + } return GNUNET_OK; } @@ -1791,48 +1801,48 @@ check_request_connection_reversal (void *cls, * @param message the message received */ static void -handle_request_connection_reversal (void *cls, - const struct GNUNET_NAT_RequestConnectionReversalMessage *message) +handle_request_connection_reversal(void *cls, + const struct GNUNET_NAT_RequestConnectionReversalMessage *message) { struct ClientHandle *ch = cls; - const char *buf = (const char *) &message[1]; - size_t local_sa_len = ntohs (message->local_addr_size); - size_t remote_sa_len = ntohs (message->remote_addr_size); + const char *buf = (const char *)&message[1]; + size_t local_sa_len = ntohs(message->local_addr_size); + size_t remote_sa_len = ntohs(message->remote_addr_size); struct sockaddr_in l4; struct sockaddr_in r4; int ret; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Received REQUEST CONNECTION REVERSAL message from client\n"); - if (local_sa_len != sizeof (struct sockaddr_in)) - { - GNUNET_break_op (0); - GNUNET_SERVICE_client_drop (ch->client); - return; - } - if (remote_sa_len != sizeof (struct sockaddr_in)) - { - GNUNET_break_op (0); - GNUNET_SERVICE_client_drop (ch->client); - return; - } - GNUNET_memcpy (&l4, - buf, - sizeof (struct sockaddr_in)); - GNUNET_break_op (AF_INET == l4.sin_family); - buf += sizeof (struct sockaddr_in); - GNUNET_memcpy (&r4, - buf, - sizeof (struct sockaddr_in)); - GNUNET_break_op (AF_INET == r4.sin_family); - ret = GN_request_connection_reversal (&l4.sin_addr, - ntohs (l4.sin_port), - &r4.sin_addr, - cfg); + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "Received REQUEST CONNECTION REVERSAL message from client\n"); + if (local_sa_len != sizeof(struct sockaddr_in)) + { + GNUNET_break_op(0); + GNUNET_SERVICE_client_drop(ch->client); + return; + } + if (remote_sa_len != sizeof(struct sockaddr_in)) + { + GNUNET_break_op(0); + GNUNET_SERVICE_client_drop(ch->client); + return; + } + GNUNET_memcpy(&l4, + buf, + sizeof(struct sockaddr_in)); + GNUNET_break_op(AF_INET == l4.sin_family); + buf += sizeof(struct sockaddr_in); + GNUNET_memcpy(&r4, + buf, + sizeof(struct sockaddr_in)); + GNUNET_break_op(AF_INET == r4.sin_family); + ret = GN_request_connection_reversal(&l4.sin_addr, + ntohs(l4.sin_port), + &r4.sin_addr, + cfg); if (GNUNET_OK != ret) - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _("Connection reversal request failed\n")); - GNUNET_SERVICE_client_continue (ch->client); + GNUNET_log(GNUNET_ERROR_TYPE_WARNING, + _("Connection reversal request failed\n")); + GNUNET_SERVICE_client_continue(ch->client); } @@ -1842,31 +1852,31 @@ handle_request_connection_reversal (void *cls, * @param cls unused */ static void -shutdown_task (void *cls) +shutdown_task(void *cls) { struct StunExternalIP *se; while (NULL != (se = se_head)) - { - GNUNET_CONTAINER_DLL_remove (se_head, - se_tail, - se); - GNUNET_SCHEDULER_cancel (se->timeout_task); - GNUNET_free (se); - } - GN_nat_status_changed (GNUNET_NO); + { + GNUNET_CONTAINER_DLL_remove(se_head, + se_tail, + se); + GNUNET_SCHEDULER_cancel(se->timeout_task); + GNUNET_free(se); + } + GN_nat_status_changed(GNUNET_NO); if (NULL != scan_task) - { - GNUNET_SCHEDULER_cancel (scan_task); - scan_task = NULL; - } + { + GNUNET_SCHEDULER_cancel(scan_task); + scan_task = NULL; + } if (NULL != stats) - { - GNUNET_STATISTICS_destroy (stats, - GNUNET_NO); - stats = NULL; - } - destroy_lal (); + { + GNUNET_STATISTICS_destroy(stats, + GNUNET_NO); + stats = NULL; + } + destroy_lal(); } @@ -1878,49 +1888,49 @@ shutdown_task (void *cls) * @param service the initialized service */ static void -run (void *cls, - const struct GNUNET_CONFIGURATION_Handle *c, - struct GNUNET_SERVICE_Handle *service) +run(void *cls, + const struct GNUNET_CONFIGURATION_Handle *c, + struct GNUNET_SERVICE_Handle *service) { cfg = c; if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_time (cfg, - "NAT", - "STUN_STALE", - &stun_stale_timeout)) + GNUNET_CONFIGURATION_get_value_time(cfg, + "NAT", + "STUN_STALE", + &stun_stale_timeout)) stun_stale_timeout = GNUNET_TIME_UNIT_HOURS; /* Check for UPnP */ enable_upnp - = GNUNET_CONFIGURATION_get_value_yesno (cfg, - "NAT", - "ENABLE_UPNP"); + = GNUNET_CONFIGURATION_get_value_yesno(cfg, + "NAT", + "ENABLE_UPNP"); if (GNUNET_YES == enable_upnp) - { - /* check if it works */ - if (GNUNET_SYSERR == - GNUNET_OS_check_helper_binary ("upnpc", - GNUNET_NO, - NULL)) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("UPnP enabled in configuration, but UPnP client `upnpc` command not found, disabling UPnP\n")); - enable_upnp = GNUNET_SYSERR; + /* check if it works */ + if (GNUNET_SYSERR == + GNUNET_OS_check_helper_binary("upnpc", + GNUNET_NO, + NULL)) + { + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, + _("UPnP enabled in configuration, but UPnP client `upnpc` command not found, disabling UPnP\n")); + enable_upnp = GNUNET_SYSERR; + } } - } if (GNUNET_OK != - GNUNET_CONFIGURATION_get_value_time (cfg, - "nat", - "DYNDNS_FREQUENCY", - &dyndns_frequency)) + GNUNET_CONFIGURATION_get_value_time(cfg, + "nat", + "DYNDNS_FREQUENCY", + &dyndns_frequency)) dyndns_frequency = DYNDNS_FREQUENCY; - GNUNET_SCHEDULER_add_shutdown (&shutdown_task, - NULL); - stats = GNUNET_STATISTICS_create ("nat", - cfg); - scan_task = GNUNET_SCHEDULER_add_now (&run_scan, - NULL); + GNUNET_SCHEDULER_add_shutdown(&shutdown_task, + NULL); + stats = GNUNET_STATISTICS_create("nat", + cfg); + scan_task = GNUNET_SCHEDULER_add_now(&run_scan, + NULL); } @@ -1933,18 +1943,18 @@ run (void *cls, * @return a `struct ClientHandle` */ static void * -client_connect_cb (void *cls, - struct GNUNET_SERVICE_Client *c, - struct GNUNET_MQ_Handle *mq) +client_connect_cb(void *cls, + struct GNUNET_SERVICE_Client *c, + struct GNUNET_MQ_Handle *mq) { struct ClientHandle *ch; - ch = GNUNET_new (struct ClientHandle); + ch = GNUNET_new(struct ClientHandle); ch->mq = mq; ch->client = c; - GNUNET_CONTAINER_DLL_insert (ch_head, - ch_tail, - ch); + GNUNET_CONTAINER_DLL_insert(ch_head, + ch_tail, + ch); return ch; } @@ -1957,50 +1967,50 @@ client_connect_cb (void *cls, * @param internal_cls a `struct ClientHandle *` */ static void -client_disconnect_cb (void *cls, - struct GNUNET_SERVICE_Client *c, - void *internal_cls) +client_disconnect_cb(void *cls, + struct GNUNET_SERVICE_Client *c, + void *internal_cls) { struct ClientHandle *ch = internal_cls; struct LocalAddressList *lal; - GNUNET_CONTAINER_DLL_remove (ch_head, - ch_tail, - ch); - for (unsigned int i=0;inum_caddrs;i++) - { - if (NULL != ch->caddrs[i].mh) + GNUNET_CONTAINER_DLL_remove(ch_head, + ch_tail, + ch); + for (unsigned int i = 0; i < ch->num_caddrs; i++) { - GNUNET_NAT_mini_map_stop (ch->caddrs[i].mh); - ch->caddrs[i].mh = NULL; + if (NULL != ch->caddrs[i].mh) + { + GNUNET_NAT_mini_map_stop(ch->caddrs[i].mh); + ch->caddrs[i].mh = NULL; + } } - } - GNUNET_free_non_null (ch->caddrs); + GNUNET_free_non_null(ch->caddrs); while (NULL != (lal = ch->ext_addr_head)) - { - GNUNET_CONTAINER_DLL_remove (ch->ext_addr_head, - ch->ext_addr_tail, - lal); - GNUNET_free (lal); - } + { + GNUNET_CONTAINER_DLL_remove(ch->ext_addr_head, + ch->ext_addr_tail, + lal); + GNUNET_free(lal); + } if (NULL != ch->ext_dns_task) - { - GNUNET_SCHEDULER_cancel (ch->ext_dns_task); - ch->ext_dns_task = NULL; - } + { + GNUNET_SCHEDULER_cancel(ch->ext_dns_task); + ch->ext_dns_task = NULL; + } if (NULL != ch->external_monitor) - { - GN_external_ipv4_monitor_stop (ch->external_monitor); - ch->external_monitor = NULL; - } + { + GN_external_ipv4_monitor_stop(ch->external_monitor); + ch->external_monitor = NULL; + } if (NULL != ch->ext_dns) - { - GNUNET_RESOLVER_request_cancel (ch->ext_dns); - ch->ext_dns = NULL; - } - GNUNET_free_non_null (ch->hole_external); - GNUNET_free_non_null (ch->section_name); - GNUNET_free (ch); + { + GNUNET_RESOLVER_request_cancel(ch->ext_dns); + ch->ext_dns = NULL; + } + GNUNET_free_non_null(ch->hole_external); + GNUNET_free_non_null(ch->section_name); + GNUNET_free(ch); } @@ -2008,25 +2018,25 @@ client_disconnect_cb (void *cls, * Define "main" method using service macro. */ GNUNET_SERVICE_MAIN -("nat", - GNUNET_SERVICE_OPTION_NONE, - &run, - &client_connect_cb, - &client_disconnect_cb, - NULL, - GNUNET_MQ_hd_var_size (register, - GNUNET_MESSAGE_TYPE_NAT_REGISTER, - struct GNUNET_NAT_RegisterMessage, - NULL), - GNUNET_MQ_hd_var_size (stun, - GNUNET_MESSAGE_TYPE_NAT_HANDLE_STUN, - struct GNUNET_NAT_HandleStunMessage, - NULL), - GNUNET_MQ_hd_var_size (request_connection_reversal, - GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL, - struct GNUNET_NAT_RequestConnectionReversalMessage, - NULL), - GNUNET_MQ_handler_end ()); + ("nat", + GNUNET_SERVICE_OPTION_NONE, + &run, + &client_connect_cb, + &client_disconnect_cb, + NULL, + GNUNET_MQ_hd_var_size(register, + GNUNET_MESSAGE_TYPE_NAT_REGISTER, + struct GNUNET_NAT_RegisterMessage, + NULL), + GNUNET_MQ_hd_var_size(stun, + GNUNET_MESSAGE_TYPE_NAT_HANDLE_STUN, + struct GNUNET_NAT_HandleStunMessage, + NULL), + GNUNET_MQ_hd_var_size(request_connection_reversal, + GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL, + struct GNUNET_NAT_RequestConnectionReversalMessage, + NULL), + GNUNET_MQ_handler_end()); #if defined(LINUX) && defined(__GLIBC__) @@ -2036,11 +2046,11 @@ GNUNET_SERVICE_MAIN * MINIMIZE heap size (way below 128k) since this process doesn't need much. */ void __attribute__ ((constructor)) -GNUNET_ARM_memory_init () +GNUNET_ARM_memory_init() { - mallopt (M_TRIM_THRESHOLD, 4 * 1024); - mallopt (M_TOP_PAD, 1 * 1024); - malloc_trim (0); + mallopt(M_TRIM_THRESHOLD, 4 * 1024); + mallopt(M_TOP_PAD, 1 * 1024); + malloc_trim(0); } #endif diff --git a/src/nat/gnunet-service-nat.h b/src/nat/gnunet-service-nat.h index ec642f6e5..5717306bb 100644 --- a/src/nat/gnunet-service-nat.h +++ b/src/nat/gnunet-service-nat.h @@ -1,19 +1,19 @@ /* - This file is part of GNUnet. - Copyright (C) 2016, 2017 GNUnet e.V. + This file is part of GNUnet. + Copyright (C) 2016, 2017 GNUnet e.V. - GNUnet is free software: you can redistribute it and/or modify it - under the terms of the GNU Affero General Public License as published - by the Free Software Foundation, either version 3 of the License, - or (at your option) any later version. + GNUnet is free software: you can redistribute it and/or modify it + under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. - GNUnet is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . SPDX-License-Identifier: AGPL3.0-or-later */ diff --git a/src/nat/gnunet-service-nat_externalip.c b/src/nat/gnunet-service-nat_externalip.c index 282b070d3..c97f7f58b 100644 --- a/src/nat/gnunet-service-nat_externalip.c +++ b/src/nat/gnunet-service-nat_externalip.c @@ -11,12 +11,12 @@ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . SPDX-License-Identifier: AGPL3.0-or-later -*/ + */ /** * Code to figure out what our external IPv4 address(es) might * be (external IPv4s are what is seen on the rest of the Internet). @@ -54,26 +54,25 @@ * How long do we wait until we re-try running `external-ip` if the * command failed to terminate nicely? */ -#define EXTERN_IP_RETRY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15) +#define EXTERN_IP_RETRY_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 15) /** * How long do we wait until we re-try running `external-ip` if the * command failed (but terminated)? */ -#define EXTERN_IP_RETRY_FAILURE GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30) +#define EXTERN_IP_RETRY_FAILURE GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 30) /** * How long do we wait until we re-try running `external-ip` if the * command succeeded? */ -#define EXTERN_IP_RETRY_SUCCESS GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5) +#define EXTERN_IP_RETRY_SUCCESS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5) /** * Handle to monitor for external IP changes. */ -struct GN_ExternalIPMonitor -{ +struct GN_ExternalIPMonitor { /** * Kept in DLL. */ @@ -93,7 +92,6 @@ struct GN_ExternalIPMonitor * Closure for @e cb. */ void *cb_cls; - }; @@ -133,15 +131,15 @@ static struct in_addr mini_external_ipv4; * @param v4 the external address that changed */ static void -notify_monitors_external_ipv4_change (int add, - const struct in_addr *v4) +notify_monitors_external_ipv4_change(int add, + const struct in_addr *v4) { for (struct GN_ExternalIPMonitor *mon = mon_head; NULL != mon; mon = mon->next) - mon->cb (mon->cb_cls, - v4, - add); + mon->cb(mon->cb_cls, + v4, + add); } @@ -152,7 +150,7 @@ notify_monitors_external_ipv4_change (int add, * @param cls NULL */ static void -run_external_ip (void *cls); +run_external_ip(void *cls); /** @@ -165,46 +163,47 @@ run_external_ip (void *cls); * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code */ static void -handle_external_ip (void *cls, - const struct in_addr *addr, - enum GNUNET_NAT_StatusCode result) +handle_external_ip(void *cls, + const struct in_addr *addr, + enum GNUNET_NAT_StatusCode result) { char buf[INET_ADDRSTRLEN]; probe_external_ip_op = NULL; - GNUNET_SCHEDULER_cancel (probe_external_ip_task); + GNUNET_SCHEDULER_cancel(probe_external_ip_task); probe_external_ip_task - = GNUNET_SCHEDULER_add_delayed ((NULL == addr) - ? EXTERN_IP_RETRY_FAILURE - : EXTERN_IP_RETRY_SUCCESS, - &run_external_ip, - NULL); + = GNUNET_SCHEDULER_add_delayed((NULL == addr) + ? EXTERN_IP_RETRY_FAILURE + : EXTERN_IP_RETRY_SUCCESS, + &run_external_ip, + NULL); switch (result) - { - case GNUNET_NAT_ERROR_SUCCESS: - GNUNET_assert (NULL != addr); - if (addr->s_addr == mini_external_ipv4.s_addr) - return; /* not change */ - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Our external IP is now %s\n", - inet_ntop (AF_INET, - addr, - buf, - sizeof (buf))); - if (0 != mini_external_ipv4.s_addr) - notify_monitors_external_ipv4_change (GNUNET_NO, - &mini_external_ipv4); - mini_external_ipv4 = *addr; - notify_monitors_external_ipv4_change (GNUNET_YES, - &mini_external_ipv4); - break; - default: - if (0 != mini_external_ipv4.s_addr) - notify_monitors_external_ipv4_change (GNUNET_NO, - &mini_external_ipv4); - mini_external_ipv4.s_addr = 0; - break; - } + { + case GNUNET_NAT_ERROR_SUCCESS: + GNUNET_assert(NULL != addr); + if (addr->s_addr == mini_external_ipv4.s_addr) + return; /* not change */ + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "Our external IP is now %s\n", + inet_ntop(AF_INET, + addr, + buf, + sizeof(buf))); + if (0 != mini_external_ipv4.s_addr) + notify_monitors_external_ipv4_change(GNUNET_NO, + &mini_external_ipv4); + mini_external_ipv4 = *addr; + notify_monitors_external_ipv4_change(GNUNET_YES, + &mini_external_ipv4); + break; + + default: + if (0 != mini_external_ipv4.s_addr) + notify_monitors_external_ipv4_change(GNUNET_NO, + &mini_external_ipv4); + mini_external_ipv4.s_addr = 0; + break; + } } @@ -215,20 +214,20 @@ handle_external_ip (void *cls, * @param cls NULL */ static void -run_external_ip (void *cls) +run_external_ip(void *cls) { probe_external_ip_task - = GNUNET_SCHEDULER_add_delayed (EXTERN_IP_RETRY_TIMEOUT, - &run_external_ip, - NULL); + = GNUNET_SCHEDULER_add_delayed(EXTERN_IP_RETRY_TIMEOUT, + &run_external_ip, + NULL); if (NULL != probe_external_ip_op) - { - GNUNET_NAT_mini_get_external_ipv4_cancel_ (probe_external_ip_op); - probe_external_ip_op = NULL; - } + { + GNUNET_NAT_mini_get_external_ipv4_cancel_(probe_external_ip_op); + probe_external_ip_op = NULL; + } probe_external_ip_op - = GNUNET_NAT_mini_get_external_ipv4_ (&handle_external_ip, - NULL); + = GNUNET_NAT_mini_get_external_ipv4_(&handle_external_ip, + NULL); } @@ -239,32 +238,32 @@ run_external_ip (void *cls) * @param have_nat #GNUNET_YES if we believe we are behind NAT */ void -GN_nat_status_changed (int have_nat) +GN_nat_status_changed(int have_nat) { if (GNUNET_YES != enable_upnp) return; - if ( (GNUNET_YES == have_nat) && - (NULL == probe_external_ip_task) && - (NULL == probe_external_ip_op) ) - { - probe_external_ip_task - = GNUNET_SCHEDULER_add_now (&run_external_ip, - NULL); - return; - } - if (GNUNET_NO == have_nat) - { - if (NULL != probe_external_ip_task) + if ((GNUNET_YES == have_nat) && + (NULL == probe_external_ip_task) && + (NULL == probe_external_ip_op)) { - GNUNET_SCHEDULER_cancel (probe_external_ip_task); - probe_external_ip_task = NULL; + probe_external_ip_task + = GNUNET_SCHEDULER_add_now(&run_external_ip, + NULL); + return; } - if (NULL != probe_external_ip_op) + if (GNUNET_NO == have_nat) { - GNUNET_NAT_mini_get_external_ipv4_cancel_ (probe_external_ip_op); - probe_external_ip_op = NULL; + if (NULL != probe_external_ip_task) + { + GNUNET_SCHEDULER_cancel(probe_external_ip_task); + probe_external_ip_task = NULL; + } + if (NULL != probe_external_ip_op) + { + GNUNET_NAT_mini_get_external_ipv4_cancel_(probe_external_ip_op); + probe_external_ip_op = NULL; + } } - } } @@ -276,21 +275,21 @@ GN_nat_status_changed (int have_nat) * @return handle to cancel */ struct GN_ExternalIPMonitor * -GN_external_ipv4_monitor_start (GN_NotifyExternalIPv4Change cb, - void *cb_cls) +GN_external_ipv4_monitor_start(GN_NotifyExternalIPv4Change cb, + void *cb_cls) { struct GN_ExternalIPMonitor *mon; - mon = GNUNET_new (struct GN_ExternalIPMonitor); + mon = GNUNET_new(struct GN_ExternalIPMonitor); mon->cb = cb; mon->cb_cls = cb_cls; - GNUNET_CONTAINER_DLL_insert (mon_head, - mon_tail, - mon); + GNUNET_CONTAINER_DLL_insert(mon_head, + mon_tail, + mon); if (0 != mini_external_ipv4.s_addr) - cb (cb_cls, - &mini_external_ipv4, - GNUNET_YES); + cb(cb_cls, + &mini_external_ipv4, + GNUNET_YES); return mon; } @@ -301,12 +300,12 @@ GN_external_ipv4_monitor_start (GN_NotifyExternalIPv4Change cb, * @param mon monitor to call */ void -GN_external_ipv4_monitor_stop (struct GN_ExternalIPMonitor *mon) +GN_external_ipv4_monitor_stop(struct GN_ExternalIPMonitor *mon) { - GNUNET_CONTAINER_DLL_remove (mon_head, - mon_tail, - mon); - GNUNET_free (mon); + GNUNET_CONTAINER_DLL_remove(mon_head, + mon_tail, + mon); + GNUNET_free(mon); } /* end of gnunet-service-nat_externalip.c */ diff --git a/src/nat/gnunet-service-nat_externalip.h b/src/nat/gnunet-service-nat_externalip.h index f93b6793d..41ab49e2f 100644 --- a/src/nat/gnunet-service-nat_externalip.h +++ b/src/nat/gnunet-service-nat_externalip.h @@ -11,19 +11,19 @@ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . SPDX-License-Identifier: AGPL3.0-or-later -*/ + */ /** * Code to figure out what our external IPv4 address(es) might * be (external IPv4s are what is seen on the rest of the Internet). * * This can be implemented using different methods, and we allow * the main service to be notified about changes to what we believe - * is our external IPv4 address. + * is our external IPv4 address. * * Note that this is explicitly only about NATed systems; if one * of our network interfaces has a global IP address this does @@ -46,7 +46,7 @@ * @param have_nat #GNUNET_YES if we believe we are behind NAT */ void -GN_nat_status_changed (int have_nat); +GN_nat_status_changed(int have_nat); /** @@ -58,8 +58,8 @@ GN_nat_status_changed (int have_nat); */ typedef void (*GN_NotifyExternalIPv4Change)(void *cls, - const struct in_addr *ip, - int add_remove); + const struct in_addr *ip, + int add_remove); /** @@ -76,8 +76,8 @@ struct GN_ExternalIPMonitor; * @return handle to cancel */ struct GN_ExternalIPMonitor * -GN_external_ipv4_monitor_start (GN_NotifyExternalIPv4Change cb, - void *cb_cls); +GN_external_ipv4_monitor_start(GN_NotifyExternalIPv4Change cb, + void *cb_cls); /** @@ -86,7 +86,7 @@ GN_external_ipv4_monitor_start (GN_NotifyExternalIPv4Change cb, * @param mon monitor to call */ void -GN_external_ipv4_monitor_stop (struct GN_ExternalIPMonitor *mon); +GN_external_ipv4_monitor_stop(struct GN_ExternalIPMonitor *mon); #endif diff --git a/src/nat/gnunet-service-nat_helper.c b/src/nat/gnunet-service-nat_helper.c index cf5038dd7..42612751d 100644 --- a/src/nat/gnunet-service-nat_helper.c +++ b/src/nat/gnunet-service-nat_helper.c @@ -11,12 +11,12 @@ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . SPDX-License-Identifier: AGPL3.0-or-later -*/ + */ /** * @file nat/gnunet-service-nat_helper.c @@ -32,9 +32,7 @@ /** * Information we keep per NAT helper process. */ -struct HelperContext -{ - +struct HelperContext { /** * IP address we pass to the NAT helper. */ @@ -89,7 +87,7 @@ struct HelperContext * @param cls a `struct HelperContext` */ static void -restart_nat_server (void *cls); +restart_nat_server(void *cls); /** @@ -98,13 +96,13 @@ restart_nat_server (void *cls); * @param h context of the helper */ static void -try_again (struct HelperContext *h) +try_again(struct HelperContext *h) { - GNUNET_assert (NULL == h->server_read_task); - h->server_retry_delay = GNUNET_TIME_STD_BACKOFF (h->server_retry_delay); - h->server_read_task = GNUNET_SCHEDULER_add_delayed (h->server_retry_delay, - &restart_nat_server, - h); + GNUNET_assert(NULL == h->server_read_task); + h->server_retry_delay = GNUNET_TIME_STD_BACKOFF(h->server_retry_delay); + h->server_read_task = GNUNET_SCHEDULER_add_delayed(h->server_retry_delay, + &restart_nat_server, + h); } @@ -116,7 +114,7 @@ try_again (struct HelperContext *h) * @param cls the `struct HelperContext` */ static void -nat_server_read (void *cls) +nat_server_read(void *cls) { struct HelperContext *h = cls; char mybuf[40]; @@ -126,73 +124,73 @@ nat_server_read (void *cls) struct sockaddr_in sin_addr; h->server_read_task = NULL; - memset (mybuf, 0, sizeof (mybuf)); + memset(mybuf, 0, sizeof(mybuf)); bytes = - GNUNET_DISK_file_read (h->server_stdout_handle, mybuf, sizeof (mybuf)); + GNUNET_DISK_file_read(h->server_stdout_handle, mybuf, sizeof(mybuf)); if (bytes < 1) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Finished reading from server stdout with code: %d\n", - (int) bytes); - if (0 != GNUNET_OS_process_kill (h->server_proc, GNUNET_TERM_SIG)) - GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING, "nat", "kill"); - GNUNET_OS_process_wait (h->server_proc); - GNUNET_OS_process_destroy (h->server_proc); - h->server_proc = NULL; - GNUNET_DISK_pipe_close (h->server_stdout); - h->server_stdout = NULL; - h->server_stdout_handle = NULL; - try_again (h); - return; - } - - port_start = NULL; - for (size_t i = 0; i < sizeof (mybuf); i++) - { - if (mybuf[i] == '\n') { - mybuf[i] = '\0'; - break; + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "Finished reading from server stdout with code: %d\n", + (int)bytes); + if (0 != GNUNET_OS_process_kill(h->server_proc, GNUNET_TERM_SIG)) + GNUNET_log_from_strerror(GNUNET_ERROR_TYPE_WARNING, "nat", "kill"); + GNUNET_OS_process_wait(h->server_proc); + GNUNET_OS_process_destroy(h->server_proc); + h->server_proc = NULL; + GNUNET_DISK_pipe_close(h->server_stdout); + h->server_stdout = NULL; + h->server_stdout_handle = NULL; + try_again(h); + return; } - if ((mybuf[i] == ':') && (i + 1 < sizeof (mybuf))) + + port_start = NULL; + for (size_t i = 0; i < sizeof(mybuf); i++) { - mybuf[i] = '\0'; - port_start = &mybuf[i + 1]; + if (mybuf[i] == '\n') + { + mybuf[i] = '\0'; + break; + } + if ((mybuf[i] == ':') && (i + 1 < sizeof(mybuf))) + { + mybuf[i] = '\0'; + port_start = &mybuf[i + 1]; + } } - } /* construct socket address of sender */ - memset (&sin_addr, 0, sizeof (sin_addr)); + memset(&sin_addr, 0, sizeof(sin_addr)); sin_addr.sin_family = AF_INET; #if HAVE_SOCKADDR_IN_SIN_LEN - sin_addr.sin_len = sizeof (sin_addr); + sin_addr.sin_len = sizeof(sin_addr); #endif - if ((NULL == port_start) || (1 != sscanf (port_start, "%d", &port)) || - (-1 == inet_pton (AF_INET, mybuf, &sin_addr.sin_addr))) - { - /* should we restart gnunet-helper-nat-server? */ - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _ ( - "gnunet-helper-nat-server generated malformed address `%s'\n"), - mybuf); - h->server_read_task = - GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, - h->server_stdout_handle, - &nat_server_read, - h); - return; - } - sin_addr.sin_port = htons ((uint16_t) port); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "gnunet-helper-nat-server read: %s:%d\n", - mybuf, - port); - h->cb (h->cb_cls, &sin_addr); + if ((NULL == port_start) || (1 != sscanf(port_start, "%d", &port)) || + (-1 == inet_pton(AF_INET, mybuf, &sin_addr.sin_addr))) + { + /* should we restart gnunet-helper-nat-server? */ + GNUNET_log(GNUNET_ERROR_TYPE_WARNING, + _( + "gnunet-helper-nat-server generated malformed address `%s'\n"), + mybuf); + h->server_read_task = + GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL, + h->server_stdout_handle, + &nat_server_read, + h); + return; + } + sin_addr.sin_port = htons((uint16_t)port); + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "gnunet-helper-nat-server read: %s:%d\n", + mybuf, + port); + h->cb(h->cb_cls, &sin_addr); h->server_read_task = - GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, - h->server_stdout_handle, - &nat_server_read, - h); + GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL, + h->server_stdout_handle, + &nat_server_read, + h); } @@ -203,67 +201,67 @@ nat_server_read (void *cls) * @param cls a `struct HelperContext` */ static void -restart_nat_server (void *cls) +restart_nat_server(void *cls) { struct HelperContext *h = cls; char *binary; char ia[INET_ADDRSTRLEN]; h->server_read_task = NULL; - GNUNET_assert (NULL != - inet_ntop (AF_INET, &h->internal_address, ia, sizeof (ia))); + GNUNET_assert(NULL != + inet_ntop(AF_INET, &h->internal_address, ia, sizeof(ia))); /* Start the server process */ - binary = GNUNET_OS_get_suid_binary_path (h->cfg, "gnunet-helper-nat-server"); - if (GNUNET_YES != GNUNET_OS_check_helper_binary (binary, GNUNET_YES, ia)) - { - /* move instantly to max delay, as this is unlikely to be fixed */ - h->server_retry_delay = GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD; - GNUNET_free (binary); - try_again (h); - return; - } + binary = GNUNET_OS_get_suid_binary_path(h->cfg, "gnunet-helper-nat-server"); + if (GNUNET_YES != GNUNET_OS_check_helper_binary(binary, GNUNET_YES, ia)) + { + /* move instantly to max delay, as this is unlikely to be fixed */ + h->server_retry_delay = GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD; + GNUNET_free(binary); + try_again(h); + return; + } h->server_stdout = - GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES); + GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES); if (NULL == h->server_stdout) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "pipe"); - GNUNET_free (binary); - try_again (h); - return; - } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Starting `%s' at `%s'\n", - "gnunet-helper-nat-server", - ia); - h->server_proc = GNUNET_OS_start_process (GNUNET_NO, - 0, - NULL, - h->server_stdout, - NULL, - binary, - "gnunet-helper-nat-server", - ia, - NULL); - GNUNET_free (binary); + { + GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "pipe"); + GNUNET_free(binary); + try_again(h); + return; + } + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "Starting `%s' at `%s'\n", + "gnunet-helper-nat-server", + ia); + h->server_proc = GNUNET_OS_start_process(GNUNET_NO, + 0, + NULL, + h->server_stdout, + NULL, + binary, + "gnunet-helper-nat-server", + ia, + NULL); + GNUNET_free(binary); if (NULL == h->server_proc) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - _ ("Failed to start %s\n"), - "gnunet-helper-nat-server"); - GNUNET_DISK_pipe_close (h->server_stdout); - h->server_stdout = NULL; - try_again (h); - return; - } + { + GNUNET_log(GNUNET_ERROR_TYPE_WARNING, + _("Failed to start %s\n"), + "gnunet-helper-nat-server"); + GNUNET_DISK_pipe_close(h->server_stdout); + h->server_stdout = NULL; + try_again(h); + return; + } /* Close the write end of the read pipe */ - GNUNET_DISK_pipe_close_end (h->server_stdout, GNUNET_DISK_PIPE_END_WRITE); + GNUNET_DISK_pipe_close_end(h->server_stdout, GNUNET_DISK_PIPE_END_WRITE); h->server_stdout_handle = - GNUNET_DISK_pipe_handle (h->server_stdout, GNUNET_DISK_PIPE_END_READ); + GNUNET_DISK_pipe_handle(h->server_stdout, GNUNET_DISK_PIPE_END_READ); h->server_read_task = - GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, - h->server_stdout_handle, - &nat_server_read, - h); + GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL, + h->server_stdout_handle, + &nat_server_read, + h); } @@ -278,24 +276,24 @@ restart_nat_server (void *cls) * @return NULL on error */ struct HelperContext * -GN_start_gnunet_nat_server_ (const struct in_addr *internal_address, - GN_ReversalCallback cb, - void *cb_cls, - const struct GNUNET_CONFIGURATION_Handle *cfg) +GN_start_gnunet_nat_server_(const struct in_addr *internal_address, + GN_ReversalCallback cb, + void *cb_cls, + const struct GNUNET_CONFIGURATION_Handle *cfg) { struct HelperContext *h; - h = GNUNET_new (struct HelperContext); + h = GNUNET_new(struct HelperContext); h->cb = cb; h->cb_cls = cb_cls; h->internal_address = *internal_address; h->cfg = cfg; - restart_nat_server (h); + restart_nat_server(h); if (NULL == h->server_stdout) - { - GN_stop_gnunet_nat_server_ (h); - return NULL; - } + { + GN_stop_gnunet_nat_server_(h); + return NULL; + } return h; } @@ -307,31 +305,31 @@ GN_start_gnunet_nat_server_ (const struct in_addr *internal_address, * @param h helper context to stop */ void -GN_stop_gnunet_nat_server_ (struct HelperContext *h) +GN_stop_gnunet_nat_server_(struct HelperContext *h) { if (NULL != h->server_read_task) - { - GNUNET_SCHEDULER_cancel (h->server_read_task); - h->server_read_task = NULL; - } + { + GNUNET_SCHEDULER_cancel(h->server_read_task); + h->server_read_task = NULL; + } if (NULL != h->server_proc) - { - if (0 != GNUNET_OS_process_kill (h->server_proc, GNUNET_TERM_SIG)) - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); - GNUNET_OS_process_wait (h->server_proc); - GNUNET_OS_process_destroy (h->server_proc); - h->server_proc = NULL; - GNUNET_DISK_pipe_close (h->server_stdout); - h->server_stdout = NULL; - h->server_stdout_handle = NULL; - } + { + if (0 != GNUNET_OS_process_kill(h->server_proc, GNUNET_TERM_SIG)) + GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill"); + GNUNET_OS_process_wait(h->server_proc); + GNUNET_OS_process_destroy(h->server_proc); + h->server_proc = NULL; + GNUNET_DISK_pipe_close(h->server_stdout); + h->server_stdout = NULL; + h->server_stdout_handle = NULL; + } if (NULL != h->server_stdout) - { - GNUNET_DISK_pipe_close (h->server_stdout); - h->server_stdout = NULL; - h->server_stdout_handle = NULL; - } - GNUNET_free (h); + { + GNUNET_DISK_pipe_close(h->server_stdout); + h->server_stdout = NULL; + h->server_stdout_handle = NULL; + } + GNUNET_free(h); } @@ -348,10 +346,10 @@ GN_stop_gnunet_nat_server_ (struct HelperContext *h) * #GNUNET_OK otherwise */ int -GN_request_connection_reversal (const struct in_addr *internal_address, - uint16_t internal_port, - const struct in_addr *remote_v4, - const struct GNUNET_CONFIGURATION_Handle *cfg) +GN_request_connection_reversal(const struct in_addr *internal_address, + uint16_t internal_port, + const struct in_addr *remote_v4, + const struct GNUNET_CONFIGURATION_Handle *cfg) { char intv4[INET_ADDRSTRLEN]; char remv4[INET_ADDRSTRLEN]; @@ -359,44 +357,44 @@ GN_request_connection_reversal (const struct in_addr *internal_address, struct GNUNET_OS_Process *proc; char *binary; - if (NULL == inet_ntop (AF_INET, internal_address, intv4, INET_ADDRSTRLEN)) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop"); - return GNUNET_SYSERR; - } - if (NULL == inet_ntop (AF_INET, remote_v4, remv4, INET_ADDRSTRLEN)) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop"); - return GNUNET_SYSERR; - } - GNUNET_snprintf (port_as_string, - sizeof (port_as_string), - "%d", - internal_port); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Running gnunet-helper-nat-client %s %s %u\n", - intv4, - remv4, - internal_port); - binary = GNUNET_OS_get_suid_binary_path (cfg, "gnunet-helper-nat-client"); - proc = GNUNET_OS_start_process (GNUNET_NO, - 0, - NULL, - NULL, - NULL, - binary, - "gnunet-helper-nat-client", - intv4, - remv4, - port_as_string, - NULL); - GNUNET_free (binary); + if (NULL == inet_ntop(AF_INET, internal_address, intv4, INET_ADDRSTRLEN)) + { + GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "inet_ntop"); + return GNUNET_SYSERR; + } + if (NULL == inet_ntop(AF_INET, remote_v4, remv4, INET_ADDRSTRLEN)) + { + GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "inet_ntop"); + return GNUNET_SYSERR; + } + GNUNET_snprintf(port_as_string, + sizeof(port_as_string), + "%d", + internal_port); + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "Running gnunet-helper-nat-client %s %s %u\n", + intv4, + remv4, + internal_port); + binary = GNUNET_OS_get_suid_binary_path(cfg, "gnunet-helper-nat-client"); + proc = GNUNET_OS_start_process(GNUNET_NO, + 0, + NULL, + NULL, + NULL, + binary, + "gnunet-helper-nat-client", + intv4, + remv4, + port_as_string, + NULL); + GNUNET_free(binary); if (NULL == proc) return GNUNET_SYSERR; /* we know that the gnunet-helper-nat-client will terminate virtually * instantly */ - GNUNET_OS_process_wait (proc); - GNUNET_OS_process_destroy (proc); + GNUNET_OS_process_wait(proc); + GNUNET_OS_process_destroy(proc); return GNUNET_OK; } diff --git a/src/nat/gnunet-service-nat_helper.h b/src/nat/gnunet-service-nat_helper.h index 4c39a374c..e655149bc 100644 --- a/src/nat/gnunet-service-nat_helper.h +++ b/src/nat/gnunet-service-nat_helper.h @@ -11,12 +11,12 @@ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . SPDX-License-Identifier: AGPL3.0-or-later -*/ + */ /** * @file nat/gnunet-service-nat_helper.h @@ -39,11 +39,11 @@ struct HelperContext; * request from another peer. * * @param cls closure - * @param ra IP address of the peer who wants us to connect to it + * @param ra IP address of the peer who wants us to connect to it */ typedef void (*GN_ReversalCallback) (void *cls, - const struct sockaddr_in *ra); + const struct sockaddr_in *ra); /** @@ -57,12 +57,12 @@ typedef void * @return NULL on error */ struct HelperContext * -GN_start_gnunet_nat_server_ (const struct in_addr *internal_address, - GN_ReversalCallback cb, - void *cb_cls, - const struct GNUNET_CONFIGURATION_Handle *cfg); +GN_start_gnunet_nat_server_(const struct in_addr *internal_address, + GN_ReversalCallback cb, + void *cb_cls, + const struct GNUNET_CONFIGURATION_Handle *cfg); + - /** * Start the gnunet-helper-nat-server and process incoming * requests. @@ -70,7 +70,7 @@ GN_start_gnunet_nat_server_ (const struct in_addr *internal_address, * @param h helper context to stop */ void -GN_stop_gnunet_nat_server_ (struct HelperContext *h); +GN_stop_gnunet_nat_server_(struct HelperContext *h); /** @@ -86,10 +86,10 @@ GN_stop_gnunet_nat_server_ (struct HelperContext *h); * #GNUNET_OK otherwise */ int -GN_request_connection_reversal (const struct in_addr *internal_address, - uint16_t internal_port, - const struct in_addr *remote_v4, - const struct GNUNET_CONFIGURATION_Handle *cfg); +GN_request_connection_reversal(const struct in_addr *internal_address, + uint16_t internal_port, + const struct in_addr *remote_v4, + const struct GNUNET_CONFIGURATION_Handle *cfg); /* end of gnunet-service-nat_helper.h */ diff --git a/src/nat/gnunet-service-nat_mini.c b/src/nat/gnunet-service-nat_mini.c index 375188ff2..94534e81c 100644 --- a/src/nat/gnunet-service-nat_mini.c +++ b/src/nat/gnunet-service-nat_mini.c @@ -11,12 +11,12 @@ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . SPDX-License-Identifier: AGPL3.0-or-later -*/ + */ /** * @file nat/gnunet-service-nat_mini.c @@ -29,24 +29,24 @@ #include "gnunet-service-nat_mini.h" #include "nat.h" -#define LOG(kind, ...) GNUNET_log_from (kind, "nat", __VA_ARGS__) +#define LOG(kind, ...) GNUNET_log_from(kind, "nat", __VA_ARGS__) /** * How long do we give upnpc to create a mapping? */ -#define MAP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15) +#define MAP_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 15) /** * How long do we give upnpc to remove a mapping? */ #define UNMAP_TIMEOUT \ - GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1) + GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1) /** * How often do we check for changes in the mapping? */ #define MAP_REFRESH_FREQ \ - GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5) + GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5) /* ************************* external-ip calling ************************ */ @@ -54,9 +54,7 @@ /** * Opaque handle to cancel "GNUNET_NAT_mini_get_external_ipv4" operation. */ -struct GNUNET_NAT_ExternalHandle -{ - +struct GNUNET_NAT_ExternalHandle { /** * Function to call with the result. */ @@ -111,43 +109,43 @@ struct GNUNET_NAT_ExternalHandle * @param cls the `struct GNUNET_NAT_ExternalHandle` */ static void -read_external_ipv4 (void *cls) +read_external_ipv4(void *cls) { struct GNUNET_NAT_ExternalHandle *eh = cls; ssize_t ret; struct in_addr addr; eh->task = NULL; - ret = GNUNET_DISK_file_read (eh->r, - &eh->buf[eh->off], - sizeof (eh->buf) - eh->off); + ret = GNUNET_DISK_file_read(eh->r, + &eh->buf[eh->off], + sizeof(eh->buf) - eh->off); if (ret > 0) - { - /* try to read more */ - eh->off += ret; - eh->task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, - eh->r, - &read_external_ipv4, - eh); - return; - } + { + /* try to read more */ + eh->off += ret; + eh->task = GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL, + eh->r, + &read_external_ipv4, + eh); + return; + } eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID; if ((eh->off > 7) && (eh->buf[eh->off - 1] == '\n')) - { - eh->buf[eh->off - 1] = '\0'; - if (1 == inet_pton (AF_INET, eh->buf, &addr)) { - if (0 == addr.s_addr) - eh->ret = - GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID; /* got 0.0.0.0 */ - else - eh->ret = GNUNET_NAT_ERROR_SUCCESS; + eh->buf[eh->off - 1] = '\0'; + if (1 == inet_pton(AF_INET, eh->buf, &addr)) + { + if (0 == addr.s_addr) + eh->ret = + GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID; /* got 0.0.0.0 */ + else + eh->ret = GNUNET_NAT_ERROR_SUCCESS; + } } - } - eh->cb (eh->cb_cls, - (GNUNET_NAT_ERROR_SUCCESS == eh->ret) ? &addr : NULL, - eh->ret); - GNUNET_NAT_mini_get_external_ipv4_cancel_ (eh); + eh->cb(eh->cb_cls, + (GNUNET_NAT_ERROR_SUCCESS == eh->ret) ? &addr : NULL, + eh->ret); + GNUNET_NAT_mini_get_external_ipv4_cancel_(eh); } @@ -157,13 +155,13 @@ read_external_ipv4 (void *cls) * @param cls the `struct GNUNET_NAT_ExternalHandle` (freed) */ static void -signal_external_ip_error (void *cls) +signal_external_ip_error(void *cls) { struct GNUNET_NAT_ExternalHandle *eh = cls; eh->task = NULL; - eh->cb (eh->cb_cls, NULL, eh->ret); - GNUNET_free (eh); + eh->cb(eh->cb_cls, NULL, eh->ret); + GNUNET_free(eh); } @@ -175,52 +173,52 @@ signal_external_ip_error (void *cls) * @return handle for cancellation (can only be used until @a cb is called), never NULL */ struct GNUNET_NAT_ExternalHandle * -GNUNET_NAT_mini_get_external_ipv4_ (GNUNET_NAT_IPCallback cb, void *cb_cls) +GNUNET_NAT_mini_get_external_ipv4_(GNUNET_NAT_IPCallback cb, void *cb_cls) { struct GNUNET_NAT_ExternalHandle *eh; - eh = GNUNET_new (struct GNUNET_NAT_ExternalHandle); + eh = GNUNET_new(struct GNUNET_NAT_ExternalHandle); eh->cb = cb; eh->cb_cls = cb_cls; eh->ret = GNUNET_NAT_ERROR_SUCCESS; if (GNUNET_SYSERR == - GNUNET_OS_check_helper_binary ("external-ip", GNUNET_NO, NULL)) - { - LOG (GNUNET_ERROR_TYPE_INFO, _ ("`external-ip' command not found\n")); - eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND; - eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, eh); - return eh; - } - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Running `external-ip' to determine our external IP\n"); - eh->opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES); + GNUNET_OS_check_helper_binary("external-ip", GNUNET_NO, NULL)) + { + LOG(GNUNET_ERROR_TYPE_INFO, _("`external-ip' command not found\n")); + eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND; + eh->task = GNUNET_SCHEDULER_add_now(&signal_external_ip_error, eh); + return eh; + } + LOG(GNUNET_ERROR_TYPE_DEBUG, + "Running `external-ip' to determine our external IP\n"); + eh->opipe = GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES); if (NULL == eh->opipe) - { - eh->ret = GNUNET_NAT_ERROR_IPC_FAILURE; - eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, eh); - return eh; - } - eh->eip = GNUNET_OS_start_process (GNUNET_NO, - 0, - NULL, - eh->opipe, - NULL, - "external-ip", - "external-ip", - NULL); + { + eh->ret = GNUNET_NAT_ERROR_IPC_FAILURE; + eh->task = GNUNET_SCHEDULER_add_now(&signal_external_ip_error, eh); + return eh; + } + eh->eip = GNUNET_OS_start_process(GNUNET_NO, + 0, + NULL, + eh->opipe, + NULL, + "external-ip", + "external-ip", + NULL); if (NULL == eh->eip) - { - GNUNET_DISK_pipe_close (eh->opipe); - eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED; - eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, eh); - return eh; - } - GNUNET_DISK_pipe_close_end (eh->opipe, GNUNET_DISK_PIPE_END_WRITE); - eh->r = GNUNET_DISK_pipe_handle (eh->opipe, GNUNET_DISK_PIPE_END_READ); - eh->task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, - eh->r, - &read_external_ipv4, - eh); + { + GNUNET_DISK_pipe_close(eh->opipe); + eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED; + eh->task = GNUNET_SCHEDULER_add_now(&signal_external_ip_error, eh); + return eh; + } + GNUNET_DISK_pipe_close_end(eh->opipe, GNUNET_DISK_PIPE_END_WRITE); + eh->r = GNUNET_DISK_pipe_handle(eh->opipe, GNUNET_DISK_PIPE_END_READ); + eh->task = GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL, + eh->r, + &read_external_ipv4, + eh); return eh; } @@ -231,25 +229,25 @@ GNUNET_NAT_mini_get_external_ipv4_ (GNUNET_NAT_IPCallback cb, void *cb_cls) * @param eh operation to cancel */ void -GNUNET_NAT_mini_get_external_ipv4_cancel_ (struct GNUNET_NAT_ExternalHandle *eh) +GNUNET_NAT_mini_get_external_ipv4_cancel_(struct GNUNET_NAT_ExternalHandle *eh) { if (NULL != eh->eip) - { - (void) GNUNET_OS_process_kill (eh->eip, SIGKILL); - GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (eh->eip)); - GNUNET_OS_process_destroy (eh->eip); - } + { + (void)GNUNET_OS_process_kill(eh->eip, SIGKILL); + GNUNET_break(GNUNET_OK == GNUNET_OS_process_wait(eh->eip)); + GNUNET_OS_process_destroy(eh->eip); + } if (NULL != eh->opipe) - { - GNUNET_DISK_pipe_close (eh->opipe); - eh->opipe = NULL; - } + { + GNUNET_DISK_pipe_close(eh->opipe); + eh->opipe = NULL; + } if (NULL != eh->task) - { - GNUNET_SCHEDULER_cancel (eh->task); - eh->task = NULL; - } - GNUNET_free (eh); + { + GNUNET_SCHEDULER_cancel(eh->task); + eh->task = NULL; + } + GNUNET_free(eh); } @@ -259,9 +257,7 @@ GNUNET_NAT_mini_get_external_ipv4_cancel_ (struct GNUNET_NAT_ExternalHandle *eh) /** * Handle to a mapping created with upnpc. */ -struct GNUNET_NAT_MiniHandle -{ - +struct GNUNET_NAT_MiniHandle { /** * Function to call on mapping changes. */ @@ -326,7 +322,7 @@ struct GNUNET_NAT_MiniHandle * @param cls the `struct GNUNET_NAT_MiniHandle` */ static void -do_refresh (void *cls); +do_refresh(void *cls); /** @@ -336,7 +332,7 @@ do_refresh (void *cls); * @param line line of output, NULL at the end */ static void -process_map_output (void *cls, const char *line); +process_map_output(void *cls, const char *line); /** @@ -345,29 +341,29 @@ process_map_output (void *cls, const char *line); * @param mini our handle */ static void -run_upnpc_r (struct GNUNET_NAT_MiniHandle *mini) +run_upnpc_r(struct GNUNET_NAT_MiniHandle *mini) { char pstr[6]; - GNUNET_snprintf (pstr, sizeof (pstr), "%u", (unsigned int) mini->port); - mini->map_cmd = GNUNET_OS_command_run (&process_map_output, - mini, - MAP_TIMEOUT, - "upnpc", - "upnpc", - "-r", - pstr, - mini->is_tcp ? "tcp" : "udp", - NULL); + GNUNET_snprintf(pstr, sizeof(pstr), "%u", (unsigned int)mini->port); + mini->map_cmd = GNUNET_OS_command_run(&process_map_output, + mini, + MAP_TIMEOUT, + "upnpc", + "upnpc", + "-r", + pstr, + mini->is_tcp ? "tcp" : "udp", + NULL); if (NULL == mini->map_cmd) - { - mini->ac (mini->ac_cls, - GNUNET_SYSERR, - NULL, - 0, - GNUNET_NAT_ERROR_UPNPC_FAILED); - return; - } + { + mini->ac(mini->ac_cls, + GNUNET_SYSERR, + NULL, + 0, + GNUNET_NAT_ERROR_UPNPC_FAILED); + return; + } } @@ -379,7 +375,7 @@ run_upnpc_r (struct GNUNET_NAT_MiniHandle *mini) * @param line line of output, NULL at the end */ static void -process_refresh_output (void *cls, const char *line) +process_refresh_output(void *cls, const char *line) { struct GNUNET_NAT_MiniHandle *mini = cls; char pstr[9]; @@ -388,53 +384,53 @@ process_refresh_output (void *cls, const char *line) struct in_addr exip; if (NULL == line) - { - GNUNET_OS_command_stop (mini->refresh_cmd); - mini->refresh_cmd = NULL; - if (GNUNET_NO == mini->found) { - /* mapping disappeared, try to re-create */ - if (GNUNET_YES == mini->did_map) - { - mini->ac (mini->ac_cls, - GNUNET_NO, - (const struct sockaddr *) &mini->current_addr, - sizeof (mini->current_addr), - GNUNET_NAT_ERROR_SUCCESS); - mini->did_map = GNUNET_NO; - } - run_upnpc_r (mini); + GNUNET_OS_command_stop(mini->refresh_cmd); + mini->refresh_cmd = NULL; + if (GNUNET_NO == mini->found) + { + /* mapping disappeared, try to re-create */ + if (GNUNET_YES == mini->did_map) + { + mini->ac(mini->ac_cls, + GNUNET_NO, + (const struct sockaddr *)&mini->current_addr, + sizeof(mini->current_addr), + GNUNET_NAT_ERROR_SUCCESS); + mini->did_map = GNUNET_NO; + } + run_upnpc_r(mini); + } + return; } - return; - } - if (! mini->did_map) + if (!mini->did_map) return; /* never mapped, won't find our mapping anyway */ /* we're looking for output of the form: - * "ExternalIPAddress = 12.134.41.124" */ + * "ExternalIPAddress = 12.134.41.124" */ - s = strstr (line, "ExternalIPAddress = "); + s = strstr(line, "ExternalIPAddress = "); if (NULL != s) - { - s += strlen ("ExternalIPAddress = "); - if (1 != inet_pton (AF_INET, s, &exip)) - return; /* skip */ - if (exip.s_addr == mini->current_addr.sin_addr.s_addr) - return; /* no change */ - /* update mapping */ - mini->ac (mini->ac_cls, - GNUNET_NO, - (const struct sockaddr *) &mini->current_addr, - sizeof (mini->current_addr), - GNUNET_NAT_ERROR_SUCCESS); - mini->current_addr.sin_addr = exip; - mini->ac (mini->ac_cls, - GNUNET_YES, - (const struct sockaddr *) &mini->current_addr, - sizeof (mini->current_addr), - GNUNET_NAT_ERROR_SUCCESS); - return; - } + { + s += strlen("ExternalIPAddress = "); + if (1 != inet_pton(AF_INET, s, &exip)) + return; /* skip */ + if (exip.s_addr == mini->current_addr.sin_addr.s_addr) + return; /* no change */ + /* update mapping */ + mini->ac(mini->ac_cls, + GNUNET_NO, + (const struct sockaddr *)&mini->current_addr, + sizeof(mini->current_addr), + GNUNET_NAT_ERROR_SUCCESS); + mini->current_addr.sin_addr = exip; + mini->ac(mini->ac_cls, + GNUNET_YES, + (const struct sockaddr *)&mini->current_addr, + sizeof(mini->current_addr), + GNUNET_NAT_ERROR_SUCCESS); + return; + } /* * we're looking for output of the form: * @@ -446,32 +442,32 @@ process_refresh_output (void *cls, const char *line) * "%s TCP PORT->STRING:OURPORT *" or * "%s UDP PORT->STRING:OURPORT *" */ - GNUNET_snprintf (pstr, sizeof (pstr), ":%u ", mini->port); - if (NULL == (s = strstr (line, "->"))) + GNUNET_snprintf(pstr, sizeof(pstr), ":%u ", mini->port); + if (NULL == (s = strstr(line, "->"))) return; /* skip */ - if (NULL == strstr (s, pstr)) + if (NULL == strstr(s, pstr)) return; /* skip */ - if (1 != sscanf (line, - (mini->is_tcp) ? "%*u TCP %u->%*s:%*u %*s" - : "%*u UDP %u->%*s:%*u %*s", - &nport)) + if (1 != sscanf(line, + (mini->is_tcp) ? "%*u TCP %u->%*s:%*u %*s" + : "%*u UDP %u->%*s:%*u %*s", + &nport)) return; /* skip */ mini->found = GNUNET_YES; - if (nport == ntohs (mini->current_addr.sin_port)) + if (nport == ntohs(mini->current_addr.sin_port)) return; /* no change */ /* external port changed, update mapping */ - mini->ac (mini->ac_cls, - GNUNET_NO, - (const struct sockaddr *) &mini->current_addr, - sizeof (mini->current_addr), - GNUNET_NAT_ERROR_SUCCESS); - mini->current_addr.sin_port = htons ((uint16_t) nport); - mini->ac (mini->ac_cls, - GNUNET_YES, - (const struct sockaddr *) &mini->current_addr, - sizeof (mini->current_addr), - GNUNET_NAT_ERROR_SUCCESS); + mini->ac(mini->ac_cls, + GNUNET_NO, + (const struct sockaddr *)&mini->current_addr, + sizeof(mini->current_addr), + GNUNET_NAT_ERROR_SUCCESS); + mini->current_addr.sin_port = htons((uint16_t)nport); + mini->ac(mini->ac_cls, + GNUNET_YES, + (const struct sockaddr *)&mini->current_addr, + sizeof(mini->current_addr), + GNUNET_NAT_ERROR_SUCCESS); } @@ -481,44 +477,44 @@ process_refresh_output (void *cls, const char *line) * @param cls the 'struct GNUNET_NAT_MiniHandle' */ static void -do_refresh (void *cls) +do_refresh(void *cls) { struct GNUNET_NAT_MiniHandle *mini = cls; int ac; mini->refresh_task = - GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, mini); - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Running `upnpc' to check if our mapping still exists\n"); + GNUNET_SCHEDULER_add_delayed(MAP_REFRESH_FREQ, &do_refresh, mini); + LOG(GNUNET_ERROR_TYPE_DEBUG, + "Running `upnpc' to check if our mapping still exists\n"); mini->found = GNUNET_NO; ac = GNUNET_NO; if (NULL != mini->map_cmd) - { - /* took way too long, abort it! */ - GNUNET_OS_command_stop (mini->map_cmd); - mini->map_cmd = NULL; - ac = GNUNET_YES; - } + { + /* took way too long, abort it! */ + GNUNET_OS_command_stop(mini->map_cmd); + mini->map_cmd = NULL; + ac = GNUNET_YES; + } if (NULL != mini->refresh_cmd) - { - /* took way too long, abort it! */ - GNUNET_OS_command_stop (mini->refresh_cmd); - mini->refresh_cmd = NULL; - ac = GNUNET_YES; - } - mini->refresh_cmd = GNUNET_OS_command_run (&process_refresh_output, - mini, - MAP_TIMEOUT, - "upnpc", - "upnpc", - "-l", - NULL); + { + /* took way too long, abort it! */ + GNUNET_OS_command_stop(mini->refresh_cmd); + mini->refresh_cmd = NULL; + ac = GNUNET_YES; + } + mini->refresh_cmd = GNUNET_OS_command_run(&process_refresh_output, + mini, + MAP_TIMEOUT, + "upnpc", + "upnpc", + "-l", + NULL); if (GNUNET_YES == ac) - mini->ac (mini->ac_cls, - GNUNET_SYSERR, - NULL, - 0, - GNUNET_NAT_ERROR_UPNPC_TIMEOUT); + mini->ac(mini->ac_cls, + GNUNET_SYSERR, + NULL, + 0, + GNUNET_NAT_ERROR_UPNPC_TIMEOUT); } @@ -529,7 +525,7 @@ do_refresh (void *cls) * @param line line of output, NULL at the end */ static void -process_map_output (void *cls, const char *line) +process_map_output(void *cls, const char *line) { struct GNUNET_NAT_MiniHandle *mini = cls; const char *ipaddr; @@ -538,51 +534,51 @@ process_map_output (void *cls, const char *line) unsigned int port; if (NULL == line) - { - GNUNET_OS_command_stop (mini->map_cmd); - mini->map_cmd = NULL; - if (GNUNET_YES != mini->did_map) - mini->ac (mini->ac_cls, - GNUNET_SYSERR, - NULL, - 0, - GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED); - if (NULL == mini->refresh_task) - mini->refresh_task = - GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, mini); - return; - } + { + GNUNET_OS_command_stop(mini->map_cmd); + mini->map_cmd = NULL; + if (GNUNET_YES != mini->did_map) + mini->ac(mini->ac_cls, + GNUNET_SYSERR, + NULL, + 0, + GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED); + if (NULL == mini->refresh_task) + mini->refresh_task = + GNUNET_SCHEDULER_add_delayed(MAP_REFRESH_FREQ, &do_refresh, mini); + return; + } /* * The upnpc output we're after looks like this: * * "external 87.123.42.204:3000 TCP is redirected to internal 192.168.2.150:3000" */ - if ((NULL == (ipaddr = strstr (line, " "))) || - (NULL == (pstr = strstr (ipaddr, ":"))) || - (1 != sscanf (pstr + 1, "%u", &port))) - { - return; /* skip line */ - } - ipa = GNUNET_strdup (ipaddr + 1); - strstr (ipa, ":")[0] = '\0'; - if (1 != inet_pton (AF_INET, ipa, &mini->current_addr.sin_addr)) - { - GNUNET_free (ipa); - return; /* skip line */ - } - GNUNET_free (ipa); - - mini->current_addr.sin_port = htons (port); + if ((NULL == (ipaddr = strstr(line, " "))) || + (NULL == (pstr = strstr(ipaddr, ":"))) || + (1 != sscanf(pstr + 1, "%u", &port))) + { + return; /* skip line */ + } + ipa = GNUNET_strdup(ipaddr + 1); + strstr(ipa, ":")[0] = '\0'; + if (1 != inet_pton(AF_INET, ipa, &mini->current_addr.sin_addr)) + { + GNUNET_free(ipa); + return; /* skip line */ + } + GNUNET_free(ipa); + + mini->current_addr.sin_port = htons(port); mini->current_addr.sin_family = AF_INET; #if HAVE_SOCKADDR_IN_SIN_LEN - mini->current_addr.sin_len = sizeof (struct sockaddr_in); + mini->current_addr.sin_len = sizeof(struct sockaddr_in); #endif mini->did_map = GNUNET_YES; - mini->ac (mini->ac_cls, - GNUNET_YES, - (const struct sockaddr *) &mini->current_addr, - sizeof (mini->current_addr), - GNUNET_NAT_ERROR_SUCCESS); + mini->ac(mini->ac_cls, + GNUNET_YES, + (const struct sockaddr *)&mini->current_addr, + sizeof(mini->current_addr), + GNUNET_NAT_ERROR_SUCCESS); } @@ -600,28 +596,28 @@ process_map_output (void *cls, const char *line) * @return NULL on error (no 'upnpc' installed) */ struct GNUNET_NAT_MiniHandle * -GNUNET_NAT_mini_map_start (uint16_t port, - int is_tcp, - GNUNET_NAT_MiniAddressCallback ac, - void *ac_cls) +GNUNET_NAT_mini_map_start(uint16_t port, + int is_tcp, + GNUNET_NAT_MiniAddressCallback ac, + void *ac_cls) { struct GNUNET_NAT_MiniHandle *ret; - if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary ("upnpc", GNUNET_NO, NULL)) - { - LOG (GNUNET_ERROR_TYPE_INFO, _ ("`upnpc' command not found\n")); - ac (ac_cls, GNUNET_SYSERR, NULL, 0, GNUNET_NAT_ERROR_UPNPC_NOT_FOUND); - return NULL; - } - LOG (GNUNET_ERROR_TYPE_DEBUG, "Running `upnpc' to install mapping\n"); - ret = GNUNET_new (struct GNUNET_NAT_MiniHandle); + if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary("upnpc", GNUNET_NO, NULL)) + { + LOG(GNUNET_ERROR_TYPE_INFO, _("`upnpc' command not found\n")); + ac(ac_cls, GNUNET_SYSERR, NULL, 0, GNUNET_NAT_ERROR_UPNPC_NOT_FOUND); + return NULL; + } + LOG(GNUNET_ERROR_TYPE_DEBUG, "Running `upnpc' to install mapping\n"); + ret = GNUNET_new(struct GNUNET_NAT_MiniHandle); ret->ac = ac; ret->ac_cls = ac_cls; ret->is_tcp = is_tcp; ret->port = port; ret->refresh_task = - GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, ret); - run_upnpc_r (ret); + GNUNET_SCHEDULER_add_delayed(MAP_REFRESH_FREQ, &do_refresh, ret); + run_upnpc_r(ret); return ret; } @@ -633,18 +629,18 @@ GNUNET_NAT_mini_map_start (uint16_t port, * @param line line of output, NULL at the end */ static void -process_unmap_output (void *cls, const char *line) +process_unmap_output(void *cls, const char *line) { struct GNUNET_NAT_MiniHandle *mini = cls; if (NULL == line) - { - LOG (GNUNET_ERROR_TYPE_DEBUG, "UPnP unmap done\n"); - GNUNET_OS_command_stop (mini->unmap_cmd); - mini->unmap_cmd = NULL; - GNUNET_free (mini); - return; - } + { + LOG(GNUNET_ERROR_TYPE_DEBUG, "UPnP unmap done\n"); + GNUNET_OS_command_stop(mini->unmap_cmd); + mini->unmap_cmd = NULL; + GNUNET_free(mini); + return; + } /* we don't really care about the output... */ } @@ -658,54 +654,54 @@ process_unmap_output (void *cls, const char *line) * @param mini the handle */ void -GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini) +GNUNET_NAT_mini_map_stop(struct GNUNET_NAT_MiniHandle *mini) { char pstr[6]; if (NULL != mini->refresh_task) - { - GNUNET_SCHEDULER_cancel (mini->refresh_task); - mini->refresh_task = NULL; - } + { + GNUNET_SCHEDULER_cancel(mini->refresh_task); + mini->refresh_task = NULL; + } if (NULL != mini->refresh_cmd) - { - GNUNET_OS_command_stop (mini->refresh_cmd); - mini->refresh_cmd = NULL; - } + { + GNUNET_OS_command_stop(mini->refresh_cmd); + mini->refresh_cmd = NULL; + } if (NULL != mini->map_cmd) - { - GNUNET_OS_command_stop (mini->map_cmd); - mini->map_cmd = NULL; - } + { + GNUNET_OS_command_stop(mini->map_cmd); + mini->map_cmd = NULL; + } if (GNUNET_NO == mini->did_map) - { - GNUNET_free (mini); - return; - } - mini->ac (mini->ac_cls, - GNUNET_NO, - (const struct sockaddr *) &mini->current_addr, - sizeof (mini->current_addr), - GNUNET_NAT_ERROR_SUCCESS); + { + GNUNET_free(mini); + return; + } + mini->ac(mini->ac_cls, + GNUNET_NO, + (const struct sockaddr *)&mini->current_addr, + sizeof(mini->current_addr), + GNUNET_NAT_ERROR_SUCCESS); /* Note: oddly enough, deletion uses the external port whereas * addition uses the internal port; this rarely matters since they * often are the same, but it might... */ - GNUNET_snprintf (pstr, - sizeof (pstr), - "%u", - (unsigned int) ntohs (mini->current_addr.sin_port)); - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Unmapping port %u with UPnP\n", - ntohs (mini->current_addr.sin_port)); - mini->unmap_cmd = GNUNET_OS_command_run (&process_unmap_output, - mini, - UNMAP_TIMEOUT, - "upnpc", - "upnpc", - "-d", - pstr, - mini->is_tcp ? "tcp" : "udp", - NULL); + GNUNET_snprintf(pstr, + sizeof(pstr), + "%u", + (unsigned int)ntohs(mini->current_addr.sin_port)); + LOG(GNUNET_ERROR_TYPE_DEBUG, + "Unmapping port %u with UPnP\n", + ntohs(mini->current_addr.sin_port)); + mini->unmap_cmd = GNUNET_OS_command_run(&process_unmap_output, + mini, + UNMAP_TIMEOUT, + "upnpc", + "upnpc", + "-d", + pstr, + mini->is_tcp ? "tcp" : "udp", + NULL); } diff --git a/src/nat/gnunet-service-nat_mini.h b/src/nat/gnunet-service-nat_mini.h index 4b84fb425..031b8e935 100644 --- a/src/nat/gnunet-service-nat_mini.h +++ b/src/nat/gnunet-service-nat_mini.h @@ -11,12 +11,12 @@ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . SPDX-License-Identifier: AGPL3.0-or-later -*/ + */ /** * @file nat/gnunet-service-nat_mini.c @@ -54,8 +54,8 @@ struct GNUNET_NAT_ExternalHandle; * @return handle for cancellation (can only be used until @a cb is called), NULL on error */ struct GNUNET_NAT_ExternalHandle * -GNUNET_NAT_mini_get_external_ipv4_ (GNUNET_NAT_IPCallback cb, - void *cb_cls); +GNUNET_NAT_mini_get_external_ipv4_(GNUNET_NAT_IPCallback cb, + void *cb_cls); /** @@ -64,7 +64,7 @@ GNUNET_NAT_mini_get_external_ipv4_ (GNUNET_NAT_IPCallback cb, * @param eh operation to cancel */ void -GNUNET_NAT_mini_get_external_ipv4_cancel_ (struct GNUNET_NAT_ExternalHandle *eh); +GNUNET_NAT_mini_get_external_ipv4_cancel_(struct GNUNET_NAT_ExternalHandle *eh); /** @@ -106,10 +106,10 @@ typedef void * @return NULL on error */ struct GNUNET_NAT_MiniHandle * -GNUNET_NAT_mini_map_start (uint16_t port, - int is_tcp, - GNUNET_NAT_MiniAddressCallback ac, - void *ac_cls); +GNUNET_NAT_mini_map_start(uint16_t port, + int is_tcp, + GNUNET_NAT_MiniAddressCallback ac, + void *ac_cls); /** @@ -121,7 +121,7 @@ GNUNET_NAT_mini_map_start (uint16_t port, * @param mini the handle */ void -GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini); +GNUNET_NAT_mini_map_stop(struct GNUNET_NAT_MiniHandle *mini); #endif diff --git a/src/nat/gnunet-service-nat_stun.c b/src/nat/gnunet-service-nat_stun.c index 639289207..d8b791d0a 100644 --- a/src/nat/gnunet-service-nat_stun.c +++ b/src/nat/gnunet-service-nat_stun.c @@ -11,12 +11,12 @@ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . SPDX-License-Identifier: AGPL3.0-or-later -*/ + */ /** * This code provides some support for doing STUN transactions. We * receive the simplest possible packet as the STUN server and try @@ -40,15 +40,14 @@ #include "gnunet_util_lib.h" #include "nat_stun.h" -#define LOG(kind,...) GNUNET_log_from (kind, "stun", __VA_ARGS__) +#define LOG(kind, ...) GNUNET_log_from(kind, "stun", __VA_ARGS__) /** - * Context for #stun_get_mapped(). + * Context for #stun_get_mapped(). * Used to store state across processing attributes. */ -struct StunState -{ +struct StunState { uint16_t attr; }; @@ -65,41 +64,44 @@ struct StunState * @return #GNUNET_OK if @a arg was initialized */ static int -stun_get_mapped (struct StunState *st, - const struct stun_attr *attr, - uint32_t magic, - struct sockaddr_in *arg) +stun_get_mapped(struct StunState *st, + const struct stun_attr *attr, + uint32_t magic, + struct sockaddr_in *arg) { const struct stun_addr *returned_addr; - struct sockaddr_in *sa = (struct sockaddr_in *) arg; - uint16_t type = ntohs (attr->attr); + struct sockaddr_in *sa = (struct sockaddr_in *)arg; + uint16_t type = ntohs(attr->attr); switch (type) - { - case STUN_MAPPED_ADDRESS: - if ( (st->attr == STUN_XOR_MAPPED_ADDRESS) || - (st->attr == STUN_MS_XOR_MAPPED_ADDRESS) ) - return GNUNET_NO; - magic = 0; - break; - case STUN_MS_XOR_MAPPED_ADDRESS: - if (st->attr == STUN_XOR_MAPPED_ADDRESS) + { + case STUN_MAPPED_ADDRESS: + if ((st->attr == STUN_XOR_MAPPED_ADDRESS) || + (st->attr == STUN_MS_XOR_MAPPED_ADDRESS)) + return GNUNET_NO; + magic = 0; + break; + + case STUN_MS_XOR_MAPPED_ADDRESS: + if (st->attr == STUN_XOR_MAPPED_ADDRESS) + return GNUNET_NO; + break; + + case STUN_XOR_MAPPED_ADDRESS: + break; + + default: return GNUNET_NO; - break; - case STUN_XOR_MAPPED_ADDRESS: - break; - default: - return GNUNET_NO; - } - - if (ntohs (attr->len) < sizeof (struct stun_addr)) + } + + if (ntohs(attr->len) < sizeof(struct stun_addr)) return GNUNET_NO; returned_addr = (const struct stun_addr *)(attr + 1); if (AF_INET != returned_addr->family) return GNUNET_NO; st->attr = type; sa->sin_family = AF_INET; - sa->sin_port = returned_addr->port ^ htons (ntohl(magic) >> 16); + sa->sin_port = returned_addr->port ^ htons(ntohl(magic) >> 16); sa->sin_addr.s_addr = returned_addr->addr ^ magic; return GNUNET_OK; } @@ -119,9 +121,9 @@ stun_get_mapped (struct StunState *st, * #GNUNET_NO if the packet is invalid (not a stun packet) */ int -GNUNET_NAT_stun_handle_packet_ (const void *data, - size_t len, - struct sockaddr_in *arg) +GNUNET_NAT_stun_handle_packet_(const void *data, + size_t len, + struct sockaddr_in *arg) { const struct stun_header *hdr; const struct stun_attr *attr; @@ -135,76 +137,76 @@ GNUNET_NAT_stun_handle_packet_ (const void *data, * while 'data' is advanced accordingly. */ if (len < sizeof(struct stun_header)) - { - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Packet too short to be a STUN packet\n"); - return GNUNET_NO; - } + { + LOG(GNUNET_ERROR_TYPE_DEBUG, + "Packet too short to be a STUN packet\n"); + return GNUNET_NO; + } hdr = data; /* Skip header as it is already in hdr */ len -= sizeof(struct stun_header); data += sizeof(struct stun_header); /* len as advertised in the message */ - advertised_message_size = ntohs (hdr->msglen); - message_magic_cookie = ntohl (hdr->magic); + advertised_message_size = ntohs(hdr->msglen); + message_magic_cookie = ntohl(hdr->magic); /* Compare if the cookie match */ if (STUN_MAGIC_COOKIE != message_magic_cookie) - { - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Invalid magic cookie for STUN packet\n"); - return GNUNET_NO; - } + { + LOG(GNUNET_ERROR_TYPE_DEBUG, + "Invalid magic cookie for STUN packet\n"); + return GNUNET_NO; + } - LOG (GNUNET_ERROR_TYPE_INFO, - "STUN Packet, msg %s (%04x), length: %d\n", - stun_msg2str (ntohs (hdr->msgtype)), - ntohs (hdr->msgtype), - advertised_message_size); + LOG(GNUNET_ERROR_TYPE_INFO, + "STUN Packet, msg %s (%04x), length: %d\n", + stun_msg2str(ntohs(hdr->msgtype)), + ntohs(hdr->msgtype), + advertised_message_size); if (advertised_message_size > len) - { - LOG (GNUNET_ERROR_TYPE_INFO, - "Scrambled STUN packet length (got %d, expecting %d)\n", - advertised_message_size, - (int) len); - return GNUNET_NO; - } - len = advertised_message_size; - memset (&st, 0, sizeof(st)); - - while (len > 0) - { - if (len < sizeof (struct stun_attr)) { - LOG (GNUNET_ERROR_TYPE_INFO, - "Attribute too short (got %d, expecting %d)\n", - (int) len, - (int) sizeof (struct stun_attr)); - break; + LOG(GNUNET_ERROR_TYPE_INFO, + "Scrambled STUN packet length (got %d, expecting %d)\n", + advertised_message_size, + (int)len); + return GNUNET_NO; } - attr = (const struct stun_attr *) data; - - /* compute total attribute length */ - advertised_message_size = ntohs (attr->len) + sizeof (struct stun_attr); + len = advertised_message_size; + memset(&st, 0, sizeof(st)); - /* Check if we still have space in our buffer */ - if (advertised_message_size > len) + while (len > 0) { - LOG (GNUNET_ERROR_TYPE_INFO, - "Inconsistent attribute (length %d exceeds remaining msg len %d)\n", - advertised_message_size, - (int) len); - break; + if (len < sizeof(struct stun_attr)) + { + LOG(GNUNET_ERROR_TYPE_INFO, + "Attribute too short (got %d, expecting %d)\n", + (int)len, + (int)sizeof(struct stun_attr)); + break; + } + attr = (const struct stun_attr *)data; + + /* compute total attribute length */ + advertised_message_size = ntohs(attr->len) + sizeof(struct stun_attr); + + /* Check if we still have space in our buffer */ + if (advertised_message_size > len) + { + LOG(GNUNET_ERROR_TYPE_INFO, + "Inconsistent attribute (length %d exceeds remaining msg len %d)\n", + advertised_message_size, + (int)len); + break; + } + if (GNUNET_OK == + stun_get_mapped(&st, + attr, + hdr->magic, + arg)) + ret = GNUNET_OK; + data += advertised_message_size; + len -= advertised_message_size; } - if (GNUNET_OK == - stun_get_mapped (&st, - attr, - hdr->magic, - arg)) - ret = GNUNET_OK; - data += advertised_message_size; - len -= advertised_message_size; - } return ret; } diff --git a/src/nat/gnunet-service-nat_stun.h b/src/nat/gnunet-service-nat_stun.h index 4a61c702f..af6520fbf 100644 --- a/src/nat/gnunet-service-nat_stun.h +++ b/src/nat/gnunet-service-nat_stun.h @@ -11,12 +11,12 @@ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . SPDX-License-Identifier: AGPL3.0-or-later -*/ + */ /** * This code provides some support for doing STUN transactions. We * receive the simplest possible packet as the STUN server and try @@ -54,8 +54,8 @@ * #GNUNET_NO if the packet is invalid (not a stun packet) */ int -GNUNET_NAT_stun_handle_packet_ (const void *data, - size_t len, - struct sockaddr_in *arg); +GNUNET_NAT_stun_handle_packet_(const void *data, + size_t len, + struct sockaddr_in *arg); #endif diff --git a/src/nat/nat.h b/src/nat/nat.h index 86de3c2f6..62377435a 100644 --- a/src/nat/nat.h +++ b/src/nat/nat.h @@ -11,12 +11,12 @@ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . SPDX-License-Identifier: AGPL3.0-or-later -*/ + */ /** * @file src/nat/nat.h @@ -36,8 +36,7 @@ GNUNET_NETWORK_STRUCT_BEGIN * Request to test NAT traversal, sent to the gnunet-nat-server * (not the service!). */ -struct GNUNET_NAT_TestMessage -{ +struct GNUNET_NAT_TestMessage { /** * Header with type #GNUNET_MESSAGE_TYPE_NAT_TEST */ @@ -62,7 +61,6 @@ struct GNUNET_NAT_TestMessage * #GNUNET_YES for TCP, #GNUNET_NO for UDP. */ int32_t is_tcp; - }; @@ -70,8 +68,7 @@ struct GNUNET_NAT_TestMessage * Flags specifying the events this client would be * interested in being told about. */ -enum GNUNET_NAT_RegisterFlags -{ +enum GNUNET_NAT_RegisterFlags { /** * This client does not want any notifications. */ @@ -93,8 +90,7 @@ enum GNUNET_NAT_RegisterFlags /** * Message sent by a client to register with its addresses. */ -struct GNUNET_NAT_RegisterMessage -{ +struct GNUNET_NAT_RegisterMessage { /** * Header with type #GNUNET_MESSAGE_TYPE_NAT_REGISTER */ @@ -127,15 +123,13 @@ struct GNUNET_NAT_RegisterMessage sockaddr' */ /* Followed by @e str_len section name to use for options */ - }; /** * Client telling the service to (possibly) handle a STUN message. */ -struct GNUNET_NAT_HandleStunMessage -{ +struct GNUNET_NAT_HandleStunMessage { /** * Header with type #GNUNET_MESSAGE_TYPE_NAT_HANDLE_STUN */ @@ -160,8 +154,7 @@ struct GNUNET_NAT_HandleStunMessage /** * Client asking the service to initiate connection reversal. */ -struct GNUNET_NAT_RequestConnectionReversalMessage -{ +struct GNUNET_NAT_RequestConnectionReversalMessage { /** * Header with type #GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL */ @@ -180,31 +173,27 @@ struct GNUNET_NAT_RequestConnectionReversalMessage /* followed by a `struct sockaddr` of @e local_addr_size bytes */ /* followed by a `struct sockaddr` of @e remote_addr_size bytes */ - }; /** * Service telling a client that connection reversal was requested. */ -struct GNUNET_NAT_ConnectionReversalRequestedMessage -{ +struct GNUNET_NAT_ConnectionReversalRequestedMessage { /** * Header with type #GNUNET_MESSAGE_TYPE_NAT_CONNECTION_REVERSAL_REQUESTED */ struct GNUNET_MessageHeader header; /* followed by a `struct sockaddr_in` */ - }; /** - * Service notifying the client about changes in the set of + * Service notifying the client about changes in the set of * addresses it has. */ -struct GNUNET_NAT_AddressChangeNotificationMessage -{ +struct GNUNET_NAT_AddressChangeNotificationMessage { /** * Header with type #GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE */ @@ -212,7 +201,7 @@ struct GNUNET_NAT_AddressChangeNotificationMessage /** * #GNUNET_YES to add, #GNUNET_NO to remove the address from the list. - */ + */ int32_t add_remove GNUNET_PACKED; /** @@ -220,7 +209,6 @@ struct GNUNET_NAT_AddressChangeNotificationMessage */ uint32_t addr_class GNUNET_PACKED; /* followed by a `struct sockaddr` */ - }; diff --git a/src/nat/nat_api.c b/src/nat/nat_api.c index 0dea501d3..877589d85 100644 --- a/src/nat/nat_api.c +++ b/src/nat/nat_api.c @@ -16,7 +16,7 @@ along with this program. If not, see . SPDX-License-Identifier: AGPL3.0-or-later -*/ + */ /** * @author Christian Grothoff @@ -35,9 +35,7 @@ /** * Entry in DLL of addresses of this peer. */ -struct AddrEntry -{ - +struct AddrEntry { /** * DLL. */ @@ -69,9 +67,7 @@ struct AddrEntry /** * Handle for active NAT registrations. */ -struct GNUNET_NAT_Handle -{ - +struct GNUNET_NAT_Handle { /** * Configuration we use. */ @@ -130,7 +126,7 @@ struct GNUNET_NAT_Handle * @param cls our `struct GNUNET_NAT_Handle *` */ static void -do_connect (void *cls); +do_connect(void *cls); /** @@ -139,29 +135,29 @@ do_connect (void *cls); * @param nh handle to reconnect */ static void -reconnect (struct GNUNET_NAT_Handle *nh) +reconnect(struct GNUNET_NAT_Handle *nh) { struct AddrEntry *ae; if (NULL != nh->mq) - { - GNUNET_MQ_destroy (nh->mq); - nh->mq = NULL; - } + { + GNUNET_MQ_destroy(nh->mq); + nh->mq = NULL; + } while (NULL != (ae = nh->ae_head)) - { - GNUNET_CONTAINER_DLL_remove (nh->ae_head, nh->ae_tail, ae); - nh->address_callback (nh->callback_cls, - &ae->app_ctx, - GNUNET_NO, - ae->ac, - (const struct sockaddr *) &ae[1], - ae->addrlen); - GNUNET_free (ae); - } - nh->reconnect_delay = GNUNET_TIME_STD_BACKOFF (nh->reconnect_delay); + { + GNUNET_CONTAINER_DLL_remove(nh->ae_head, nh->ae_tail, ae); + nh->address_callback(nh->callback_cls, + &ae->app_ctx, + GNUNET_NO, + ae->ac, + (const struct sockaddr *)&ae[1], + ae->addrlen); + GNUNET_free(ae); + } + nh->reconnect_delay = GNUNET_TIME_STD_BACKOFF(nh->reconnect_delay); nh->reconnect_task = - GNUNET_SCHEDULER_add_delayed (nh->reconnect_delay, &do_connect, nh); + GNUNET_SCHEDULER_add_delayed(nh->reconnect_delay, &do_connect, nh); } @@ -173,15 +169,15 @@ reconnect (struct GNUNET_NAT_Handle *nh) * @return #GNUNET_OK if @a crm is well-formed */ static int -check_connection_reversal_request ( +check_connection_reversal_request( void *cls, const struct GNUNET_NAT_ConnectionReversalRequestedMessage *crm) { - if (ntohs (crm->header.size) != sizeof (*crm) + sizeof (struct sockaddr_in)) - { - GNUNET_break (0); - return GNUNET_SYSERR; - } + if (ntohs(crm->header.size) != sizeof(*crm) + sizeof(struct sockaddr_in)) + { + GNUNET_break(0); + return GNUNET_SYSERR; + } return GNUNET_OK; } @@ -193,15 +189,15 @@ check_connection_reversal_request ( * @param crm the message */ static void -handle_connection_reversal_request ( +handle_connection_reversal_request( void *cls, const struct GNUNET_NAT_ConnectionReversalRequestedMessage *crm) { struct GNUNET_NAT_Handle *nh = cls; - nh->reversal_callback (nh->callback_cls, - (const struct sockaddr *) &crm[1], - sizeof (struct sockaddr_in)); + nh->reversal_callback(nh->callback_cls, + (const struct sockaddr *)&crm[1], + sizeof(struct sockaddr_in)); } @@ -213,36 +209,38 @@ handle_connection_reversal_request ( * @return #GNUNET_OK if @a crm is well-formed */ static int -check_address_change_notification ( +check_address_change_notification( void *cls, const struct GNUNET_NAT_AddressChangeNotificationMessage *acn) { - size_t alen = ntohs (acn->header.size) - sizeof (*acn); + size_t alen = ntohs(acn->header.size) - sizeof(*acn); switch (alen) - { - case sizeof (struct sockaddr_in): { - const struct sockaddr_in *s4 = (const struct sockaddr_in *) &acn[1]; - if (AF_INET != s4->sin_family) { - GNUNET_break (0); - return GNUNET_SYSERR; + case sizeof(struct sockaddr_in): { + const struct sockaddr_in *s4 = (const struct sockaddr_in *)&acn[1]; + if (AF_INET != s4->sin_family) + { + GNUNET_break(0); + return GNUNET_SYSERR; + } } - } - break; - case sizeof (struct sockaddr_in6): { - const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) &acn[1]; - if (AF_INET6 != s6->sin6_family) - { - GNUNET_break (0); + break; + + case sizeof(struct sockaddr_in6): { + const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *)&acn[1]; + if (AF_INET6 != s6->sin6_family) + { + GNUNET_break(0); + return GNUNET_SYSERR; + } + } + break; + + default: + GNUNET_break(0); return GNUNET_SYSERR; } - } - break; - default: - GNUNET_break (0); - return GNUNET_SYSERR; - } return GNUNET_OK; } @@ -254,53 +252,53 @@ check_address_change_notification ( * @param acn the message */ static void -handle_address_change_notification ( +handle_address_change_notification( void *cls, const struct GNUNET_NAT_AddressChangeNotificationMessage *acn) { struct GNUNET_NAT_Handle *nh = cls; - size_t alen = ntohs (acn->header.size) - sizeof (*acn); - const struct sockaddr *sa = (const struct sockaddr *) &acn[1]; + size_t alen = ntohs(acn->header.size) - sizeof(*acn); + const struct sockaddr *sa = (const struct sockaddr *)&acn[1]; enum GNUNET_NAT_AddressClass ac; struct AddrEntry *ae; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Received address change notification\n"); - ac = (enum GNUNET_NAT_AddressClass) ntohl (acn->addr_class); - if (GNUNET_YES == ntohl (acn->add_remove)) - { - ae = GNUNET_malloc (sizeof (*ae) + alen); - ae->ac = ac; - ae->addrlen = alen; - GNUNET_memcpy (&ae[1], sa, alen); - GNUNET_CONTAINER_DLL_insert (nh->ae_head, nh->ae_tail, ae); - nh->address_callback (nh->callback_cls, - &ae->app_ctx, - ntohl (acn->add_remove), - ac, - sa, - alen); - } + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "Received address change notification\n"); + ac = (enum GNUNET_NAT_AddressClass)ntohl(acn->addr_class); + if (GNUNET_YES == ntohl(acn->add_remove)) + { + ae = GNUNET_malloc(sizeof(*ae) + alen); + ae->ac = ac; + ae->addrlen = alen; + GNUNET_memcpy(&ae[1], sa, alen); + GNUNET_CONTAINER_DLL_insert(nh->ae_head, nh->ae_tail, ae); + nh->address_callback(nh->callback_cls, + &ae->app_ctx, + ntohl(acn->add_remove), + ac, + sa, + alen); + } else - { - for (ae = nh->ae_head; NULL != ae; ae = ae->next) - if ((ae->addrlen == alen) && (0 == memcmp (&ae[1], sa, alen))) - break; - if (NULL == ae) { - GNUNET_break (0); - reconnect (nh); - return; + for (ae = nh->ae_head; NULL != ae; ae = ae->next) + if ((ae->addrlen == alen) && (0 == memcmp(&ae[1], sa, alen))) + break; + if (NULL == ae) + { + GNUNET_break(0); + reconnect(nh); + return; + } + GNUNET_CONTAINER_DLL_remove(nh->ae_head, nh->ae_tail, ae); + nh->address_callback(nh->callback_cls, + &ae->app_ctx, + ntohl(acn->add_remove), + ac, + sa, + alen); + GNUNET_free(ae); } - GNUNET_CONTAINER_DLL_remove (nh->ae_head, nh->ae_tail, ae); - nh->address_callback (nh->callback_cls, - &ae->app_ctx, - ntohl (acn->add_remove), - ac, - sa, - alen); - GNUNET_free (ae); - } } @@ -311,11 +309,11 @@ handle_address_change_notification ( * @param error details about the error */ static void -mq_error_handler (void *cls, enum GNUNET_MQ_Error error) +mq_error_handler(void *cls, enum GNUNET_MQ_Error error) { struct GNUNET_NAT_Handle *nh = cls; - reconnect (nh); + reconnect(nh); } @@ -325,32 +323,32 @@ mq_error_handler (void *cls, enum GNUNET_MQ_Error error) * @param cls our `struct GNUNET_NAT_Handle *` */ static void -do_connect (void *cls) +do_connect(void *cls) { struct GNUNET_NAT_Handle *nh = cls; struct GNUNET_MQ_MessageHandler handlers[] = - {GNUNET_MQ_hd_var_size (connection_reversal_request, - GNUNET_MESSAGE_TYPE_NAT_CONNECTION_REVERSAL_REQUESTED, - struct - GNUNET_NAT_ConnectionReversalRequestedMessage, - nh), - GNUNET_MQ_hd_var_size (address_change_notification, - GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE, - struct GNUNET_NAT_AddressChangeNotificationMessage, - nh), - GNUNET_MQ_handler_end ()}; + { GNUNET_MQ_hd_var_size(connection_reversal_request, + GNUNET_MESSAGE_TYPE_NAT_CONNECTION_REVERSAL_REQUESTED, + struct + GNUNET_NAT_ConnectionReversalRequestedMessage, + nh), + GNUNET_MQ_hd_var_size(address_change_notification, + GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE, + struct GNUNET_NAT_AddressChangeNotificationMessage, + nh), + GNUNET_MQ_handler_end() }; struct GNUNET_MQ_Envelope *env; nh->reconnect_task = NULL; nh->mq = - GNUNET_CLIENT_connect (nh->cfg, "nat", handlers, &mq_error_handler, nh); + GNUNET_CLIENT_connect(nh->cfg, "nat", handlers, &mq_error_handler, nh); if (NULL == nh->mq) - { - reconnect (nh); - return; - } - env = GNUNET_MQ_msg_copy (nh->reg); - GNUNET_MQ_send (nh->mq, env); + { + reconnect(nh); + return; + } + env = GNUNET_MQ_msg_copy(nh->reg); + GNUNET_MQ_send(nh->mq, env); } @@ -375,15 +373,15 @@ do_connect (void *cls) * @return NULL on error, otherwise handle that can be used to unregister */ struct GNUNET_NAT_Handle * -GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg, - const char *config_section, - uint8_t proto, - unsigned int num_addrs, - const struct sockaddr **addrs, - const socklen_t *addrlens, - GNUNET_NAT_AddressCallback address_callback, - GNUNET_NAT_ReversalCallback reversal_callback, - void *callback_cls) +GNUNET_NAT_register(const struct GNUNET_CONFIGURATION_Handle *cfg, + const char *config_section, + uint8_t proto, + unsigned int num_addrs, + const struct sockaddr **addrs, + const socklen_t *addrlens, + GNUNET_NAT_AddressCallback address_callback, + GNUNET_NAT_ReversalCallback reversal_callback, + void *callback_cls) { struct GNUNET_NAT_Handle *nh; struct GNUNET_NAT_RegisterMessage *rm; @@ -394,73 +392,75 @@ GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg, len = 0; for (unsigned int i = 0; i < num_addrs; i++) len += addrlens[i]; - str_len = strlen (config_section) + 1; + str_len = strlen(config_section) + 1; len += str_len; - if ((len > GNUNET_MAX_MESSAGE_SIZE - sizeof (*rm)) || + if ((len > GNUNET_MAX_MESSAGE_SIZE - sizeof(*rm)) || (num_addrs > UINT16_MAX)) - { - GNUNET_break (0); - return NULL; - } - rm = GNUNET_malloc (sizeof (*rm) + len); - rm->header.size = htons (sizeof (*rm) + len); - rm->header.type = htons (GNUNET_MESSAGE_TYPE_NAT_REGISTER); + { + GNUNET_break(0); + return NULL; + } + rm = GNUNET_malloc(sizeof(*rm) + len); + rm->header.size = htons(sizeof(*rm) + len); + rm->header.type = htons(GNUNET_MESSAGE_TYPE_NAT_REGISTER); rm->flags = GNUNET_NAT_RF_NONE; if (NULL != address_callback) rm->flags |= GNUNET_NAT_RF_ADDRESSES; if (NULL != reversal_callback) rm->flags |= GNUNET_NAT_RF_REVERSAL; rm->proto = proto; - rm->str_len = htons (str_len); - rm->num_addrs = htons ((uint16_t) num_addrs); - off = (char *) &rm[1]; + rm->str_len = htons(str_len); + rm->num_addrs = htons((uint16_t)num_addrs); + off = (char *)&rm[1]; for (unsigned int i = 0; i < num_addrs; i++) - { - switch (addrs[i]->sa_family) { - case AF_INET: - if (sizeof (struct sockaddr_in) != addrlens[i]) - { - GNUNET_break (0); - GNUNET_free (rm); - return NULL; - } - break; - case AF_INET6: - if (sizeof (struct sockaddr_in6) != addrlens[i]) - { - GNUNET_break (0); - GNUNET_free (rm); - return NULL; - } - break; + switch (addrs[i]->sa_family) + { + case AF_INET: + if (sizeof(struct sockaddr_in) != addrlens[i]) + { + GNUNET_break(0); + GNUNET_free(rm); + return NULL; + } + break; + + case AF_INET6: + if (sizeof(struct sockaddr_in6) != addrlens[i]) + { + GNUNET_break(0); + GNUNET_free(rm); + return NULL; + } + break; + #if AF_UNIX - case AF_UNIX: - if (sizeof (struct sockaddr_un) != addrlens[i]) - { - GNUNET_break (0); - GNUNET_free (rm); - return NULL; - } - break; + case AF_UNIX: + if (sizeof(struct sockaddr_un) != addrlens[i]) + { + GNUNET_break(0); + GNUNET_free(rm); + return NULL; + } + break; #endif - default: - GNUNET_break (0); - GNUNET_free (rm); - return NULL; + default: + GNUNET_break(0); + GNUNET_free(rm); + return NULL; + } + GNUNET_memcpy(off, addrs[i], addrlens[i]); + off += addrlens[i]; } - GNUNET_memcpy (off, addrs[i], addrlens[i]); - off += addrlens[i]; - } - GNUNET_memcpy (off, config_section, str_len); + GNUNET_memcpy(off, config_section, str_len); - nh = GNUNET_new (struct GNUNET_NAT_Handle); + nh = GNUNET_new(struct GNUNET_NAT_Handle); nh->reg = &rm->header; nh->cfg = cfg; nh->address_callback = address_callback; nh->reversal_callback = reversal_callback; nh->callback_cls = callback_cls; - do_connect (nh); + do_connect(nh); return nh; } @@ -474,7 +474,7 @@ GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg, * #GNUNET_NO if the packet is invalid (not a stun packet) */ static int -test_stun_packet (const void *data, size_t len) +test_stun_packet(const void *data, size_t len) { const struct stun_header *hdr; const struct stun_attr *attr; @@ -485,71 +485,71 @@ test_stun_packet (const void *data, size_t len) * initial checks it becomes the size of unprocessed options, * while 'data' is advanced accordingly. */ - if (len < sizeof (struct stun_header)) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "STUN packet too short (only %d, wanting at least %d)\n", - (int) len, - (int) sizeof (struct stun_header)); - return GNUNET_NO; - } - hdr = (const struct stun_header *) data; + if (len < sizeof(struct stun_header)) + { + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "STUN packet too short (only %d, wanting at least %d)\n", + (int)len, + (int)sizeof(struct stun_header)); + return GNUNET_NO; + } + hdr = (const struct stun_header *)data; /* Skip header as it is already in hdr */ - len -= sizeof (struct stun_header); - data += sizeof (struct stun_header); + len -= sizeof(struct stun_header); + data += sizeof(struct stun_header); /* len as advertised in the message */ - advertised_message_size = ntohs (hdr->msglen); + advertised_message_size = ntohs(hdr->msglen); - message_magic_cookie = ntohl (hdr->magic); + message_magic_cookie = ntohl(hdr->magic); /* Compare if the cookie match */ if (STUN_MAGIC_COOKIE != message_magic_cookie) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Invalid magic cookie for STUN\n"); - return GNUNET_NO; - } - - if (advertised_message_size > len) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Scrambled STUN packet length (got %d, expecting %d)\n", - advertised_message_size, - (int) len); - return GNUNET_NO; - } - len = advertised_message_size; - while (len > 0) - { - if (len < sizeof (struct stun_attr)) { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Attribute too short in STUN packet (got %d, expecting %d)\n", - (int) len, - (int) sizeof (struct stun_attr)); + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Invalid magic cookie for STUN\n"); return GNUNET_NO; } - attr = (const struct stun_attr *) data; - - /* compute total attribute length */ - advertised_message_size = ntohs (attr->len) + sizeof (struct stun_attr); - /* Check if we still have space in our buffer */ - if (advertised_message_size > len) + if (advertised_message_size > len) { - GNUNET_log ( - GNUNET_ERROR_TYPE_DEBUG, - "Inconsistent Attribute (length %d exceeds remaining msg len %d)\n", - advertised_message_size, - (int) len); + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "Scrambled STUN packet length (got %d, expecting %d)\n", + advertised_message_size, + (int)len); return GNUNET_NO; } - data += advertised_message_size; - len -= advertised_message_size; - } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "STUN Packet, msg %04x, length: %d\n", - ntohs (hdr->msgtype), - advertised_message_size); + len = advertised_message_size; + while (len > 0) + { + if (len < sizeof(struct stun_attr)) + { + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "Attribute too short in STUN packet (got %d, expecting %d)\n", + (int)len, + (int)sizeof(struct stun_attr)); + return GNUNET_NO; + } + attr = (const struct stun_attr *)data; + + /* compute total attribute length */ + advertised_message_size = ntohs(attr->len) + sizeof(struct stun_attr); + + /* Check if we still have space in our buffer */ + if (advertised_message_size > len) + { + GNUNET_log( + GNUNET_ERROR_TYPE_DEBUG, + "Inconsistent Attribute (length %d exceeds remaining msg len %d)\n", + advertised_message_size, + (int)len); + return GNUNET_NO; + } + data += advertised_message_size; + len -= advertised_message_size; + } + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "STUN Packet, msg %04x, length: %d\n", + ntohs(hdr->msgtype), + advertised_message_size); return GNUNET_OK; } @@ -578,30 +578,30 @@ test_stun_packet (const void *data, size_t len) * #GNUNET_SYSERR on internal error handling the packet */ int -GNUNET_NAT_stun_handle_packet (struct GNUNET_NAT_Handle *nh, - const struct sockaddr *sender_addr, - size_t sender_addr_len, - const void *data, - size_t data_size) +GNUNET_NAT_stun_handle_packet(struct GNUNET_NAT_Handle *nh, + const struct sockaddr *sender_addr, + size_t sender_addr_len, + const void *data, + size_t data_size) { struct GNUNET_MQ_Envelope *env; struct GNUNET_NAT_HandleStunMessage *hsn; char *buf; - if (GNUNET_YES != test_stun_packet (data, data_size)) + if (GNUNET_YES != test_stun_packet(data, data_size)) return GNUNET_NO; if (NULL == nh->mq) return GNUNET_SYSERR; - env = GNUNET_MQ_msg_extra (hsn, - data_size + sender_addr_len, - GNUNET_MESSAGE_TYPE_NAT_HANDLE_STUN); - hsn->sender_addr_size = htons ((uint16_t) sender_addr_len); - hsn->payload_size = htons ((uint16_t) data_size); - buf = (char *) &hsn[1]; - GNUNET_memcpy (buf, sender_addr, sender_addr_len); + env = GNUNET_MQ_msg_extra(hsn, + data_size + sender_addr_len, + GNUNET_MESSAGE_TYPE_NAT_HANDLE_STUN); + hsn->sender_addr_size = htons((uint16_t)sender_addr_len); + hsn->payload_size = htons((uint16_t)data_size); + buf = (char *)&hsn[1]; + GNUNET_memcpy(buf, sender_addr, sender_addr_len); buf += sender_addr_len; - GNUNET_memcpy (buf, data, data_size); - GNUNET_MQ_send (nh->mq, env); + GNUNET_memcpy(buf, data, data_size); + GNUNET_MQ_send(nh->mq, env); return GNUNET_OK; } @@ -620,20 +620,20 @@ GNUNET_NAT_stun_handle_packet (struct GNUNET_NAT_Handle *nh, * #GNUNET_SYSERR if the address is malformed */ int -GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *nh, - const void *addr, - socklen_t addrlen) +GNUNET_NAT_test_address(struct GNUNET_NAT_Handle *nh, + const void *addr, + socklen_t addrlen) { struct AddrEntry *ae; - if ((addrlen != sizeof (struct sockaddr_in)) && - (addrlen != sizeof (struct sockaddr_in6))) - { - GNUNET_break (0); - return GNUNET_SYSERR; - } + if ((addrlen != sizeof(struct sockaddr_in)) && + (addrlen != sizeof(struct sockaddr_in6))) + { + GNUNET_break(0); + return GNUNET_SYSERR; + } for (ae = nh->ae_head; NULL != ae; ae = ae->next) - if ((addrlen == ae->addrlen) && (0 == memcmp (addr, &ae[1], addrlen))) + if ((addrlen == ae->addrlen) && (0 == memcmp(addr, &ae[1], addrlen))) return GNUNET_YES; return GNUNET_NO; } @@ -652,9 +652,9 @@ GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *nh, * #GNUNET_OK otherwise (presumably in progress) */ int -GNUNET_NAT_request_reversal (struct GNUNET_NAT_Handle *nh, - const struct sockaddr_in *local_sa, - const struct sockaddr_in *remote_sa) +GNUNET_NAT_request_reversal(struct GNUNET_NAT_Handle *nh, + const struct sockaddr_in *local_sa, + const struct sockaddr_in *remote_sa) { struct GNUNET_MQ_Envelope *env; struct GNUNET_NAT_RequestConnectionReversalMessage *req; @@ -662,19 +662,19 @@ GNUNET_NAT_request_reversal (struct GNUNET_NAT_Handle *nh, if (NULL == nh->mq) return GNUNET_SYSERR; - GNUNET_break (AF_INET == local_sa->sin_family); - GNUNET_break (AF_INET == remote_sa->sin_family); + GNUNET_break(AF_INET == local_sa->sin_family); + GNUNET_break(AF_INET == remote_sa->sin_family); env = - GNUNET_MQ_msg_extra (req, - 2 * sizeof (struct sockaddr_in), - GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL); - req->local_addr_size = htons (sizeof (struct sockaddr_in)); - req->remote_addr_size = htons (sizeof (struct sockaddr_in)); - buf = (char *) &req[1]; - GNUNET_memcpy (buf, local_sa, sizeof (struct sockaddr_in)); - buf += sizeof (struct sockaddr_in); - GNUNET_memcpy (buf, remote_sa, sizeof (struct sockaddr_in)); - GNUNET_MQ_send (nh->mq, env); + GNUNET_MQ_msg_extra(req, + 2 * sizeof(struct sockaddr_in), + GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL); + req->local_addr_size = htons(sizeof(struct sockaddr_in)); + req->remote_addr_size = htons(sizeof(struct sockaddr_in)); + buf = (char *)&req[1]; + GNUNET_memcpy(buf, local_sa, sizeof(struct sockaddr_in)); + buf += sizeof(struct sockaddr_in); + GNUNET_memcpy(buf, remote_sa, sizeof(struct sockaddr_in)); + GNUNET_MQ_send(nh->mq, env); return GNUNET_OK; } @@ -687,20 +687,20 @@ GNUNET_NAT_request_reversal (struct GNUNET_NAT_Handle *nh, * @param nh the handle to stop */ void -GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *nh) +GNUNET_NAT_unregister(struct GNUNET_NAT_Handle *nh) { if (NULL != nh->mq) - { - GNUNET_MQ_destroy (nh->mq); - nh->mq = NULL; - } + { + GNUNET_MQ_destroy(nh->mq); + nh->mq = NULL; + } if (NULL != nh->reconnect_task) - { - GNUNET_SCHEDULER_cancel (nh->reconnect_task); - nh->reconnect_task = NULL; - } - GNUNET_free (nh->reg); - GNUNET_free (nh); + { + GNUNET_SCHEDULER_cancel(nh->reconnect_task); + nh->reconnect_task = NULL; + } + GNUNET_free(nh->reg); + GNUNET_free(nh); } diff --git a/src/nat/nat_api_stun.c b/src/nat/nat_api_stun.c index a0931a06f..0f8694867 100644 --- a/src/nat/nat_api_stun.c +++ b/src/nat/nat_api_stun.c @@ -11,12 +11,12 @@ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . SPDX-License-Identifier: AGPL3.0-or-later -*/ + */ /** * This code provides some support for doing STUN transactions. * We send simplest possible packet ia REQUEST with BIND to a STUN server. @@ -43,9 +43,9 @@ #include "nat_stun.h" -#define LOG(kind,...) GNUNET_log_from (kind, "stun", __VA_ARGS__) +#define LOG(kind, ...) GNUNET_log_from(kind, "stun", __VA_ARGS__) -#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15) +#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 15) /** @@ -53,9 +53,7 @@ * the request prior to the timeout or successful execution. Also * used to track our internal state for the request. */ -struct GNUNET_NAT_STUN_Handle -{ - +struct GNUNET_NAT_STUN_Handle { /** * Handle to a pending DNS lookup request. */ @@ -90,7 +88,6 @@ struct GNUNET_NAT_STUN_Handle * STUN port */ uint16_t stun_port; - }; @@ -102,11 +99,11 @@ struct GNUNET_NAT_STUN_Handle * @return message in a STUN compatible format */ static int -encode_message (enum StunClasses msg_class, - enum StunMethods method) +encode_message(enum StunClasses msg_class, + enum StunMethods method) { return ((msg_class & 1) << 4) | ((msg_class & 2) << 7) | - (method & 0x000f) | ((method & 0x0070) << 1) | ((method & 0x0f800) << 2); + (method & 0x000f) | ((method & 0x0070) << 1) | ((method & 0x0f800) << 2); } @@ -116,12 +113,12 @@ encode_message (enum StunClasses msg_class, * @param req, stun header to be filled */ static void -generate_request_id (struct stun_header *req) +generate_request_id(struct stun_header *req) { req->magic = htonl(STUN_MAGIC_COOKIE); for (unsigned int x = 0; x < 3; x++) - req->id.id[x] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, - UINT32_MAX); + req->id.id[x] = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE, + UINT32_MAX); } @@ -133,67 +130,67 @@ generate_request_id (struct stun_header *req) * @param addrlen length of @a addr */ static void -stun_dns_callback (void *cls, - const struct sockaddr *addr, - socklen_t addrlen) +stun_dns_callback(void *cls, + const struct sockaddr *addr, + socklen_t addrlen) { struct GNUNET_NAT_STUN_Handle *rh = cls; struct stun_header req; struct sockaddr_in server; if (NULL == addr) - { - rh->dns_active = NULL; - if (GNUNET_NO == rh->dns_success) - { - LOG (GNUNET_ERROR_TYPE_INFO, - "Error resolving host %s\n", - rh->stun_server); - rh->cb (rh->cb_cls, - GNUNET_NAT_ERROR_NOT_ONLINE); - } - else if (GNUNET_SYSERR == rh->dns_success) { - rh->cb (rh->cb_cls, - GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR); + rh->dns_active = NULL; + if (GNUNET_NO == rh->dns_success) + { + LOG(GNUNET_ERROR_TYPE_INFO, + "Error resolving host %s\n", + rh->stun_server); + rh->cb(rh->cb_cls, + GNUNET_NAT_ERROR_NOT_ONLINE); + } + else if (GNUNET_SYSERR == rh->dns_success) + { + rh->cb(rh->cb_cls, + GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR); + } + else + { + rh->cb(rh->cb_cls, + GNUNET_NAT_ERROR_SUCCESS); + } + GNUNET_NAT_stun_make_request_cancel(rh); + return; } - else - { - rh->cb (rh->cb_cls, - GNUNET_NAT_ERROR_SUCCESS); - } - GNUNET_NAT_stun_make_request_cancel (rh); - return; - } rh->dns_success = GNUNET_YES; - memset (&server, 0, sizeof(server)); + memset(&server, 0, sizeof(server)); server.sin_family = AF_INET; server.sin_addr = ((struct sockaddr_in *)addr)->sin_addr; - server.sin_port = htons (rh->stun_port); + server.sin_port = htons(rh->stun_port); #if HAVE_SOCKADDR_IN_SIN_LEN - server.sin_len = (u_char) sizeof (struct sockaddr_in); + server.sin_len = (u_char)sizeof(struct sockaddr_in); #endif /* Craft the simplest possible STUN packet. A request binding */ - generate_request_id (&req); - req.msglen = htons (0); - req.msgtype = htons (encode_message (STUN_REQUEST, - STUN_BINDING)); + generate_request_id(&req); + req.msglen = htons(0); + req.msgtype = htons(encode_message(STUN_REQUEST, + STUN_BINDING)); /* Send the packet */ if (-1 == - GNUNET_NETWORK_socket_sendto (rh->sock, - &req, - sizeof (req), - (const struct sockaddr *) &server, - sizeof (server))) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, - "sendto"); - rh->dns_success = GNUNET_SYSERR; - return; - } + GNUNET_NETWORK_socket_sendto(rh->sock, + &req, + sizeof(req), + (const struct sockaddr *)&server, + sizeof(server))) + { + GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, + "sendto"); + rh->dns_success = GNUNET_SYSERR; + return; + } } @@ -209,31 +206,31 @@ stun_dns_callback (void *cls, * @return NULL on error */ struct GNUNET_NAT_STUN_Handle * -GNUNET_NAT_stun_make_request (const char *server, - uint16_t port, - struct GNUNET_NETWORK_Handle *sock, - GNUNET_NAT_TestCallback cb, - void *cb_cls) +GNUNET_NAT_stun_make_request(const char *server, + uint16_t port, + struct GNUNET_NETWORK_Handle *sock, + GNUNET_NAT_TestCallback cb, + void *cb_cls) { struct GNUNET_NAT_STUN_Handle *rh; - rh = GNUNET_new (struct GNUNET_NAT_STUN_Handle); + rh = GNUNET_new(struct GNUNET_NAT_STUN_Handle); rh->sock = sock; rh->cb = cb; rh->cb_cls = cb_cls; - rh->stun_server = GNUNET_strdup (server); + rh->stun_server = GNUNET_strdup(server); rh->stun_port = port; rh->dns_success = GNUNET_NO; - rh->dns_active = GNUNET_RESOLVER_ip_get (rh->stun_server, - AF_INET, - TIMEOUT, - &stun_dns_callback, - rh); + rh->dns_active = GNUNET_RESOLVER_ip_get(rh->stun_server, + AF_INET, + TIMEOUT, + &stun_dns_callback, + rh); if (NULL == rh->dns_active) - { - GNUNET_NAT_stun_make_request_cancel (rh); - return NULL; - } + { + GNUNET_NAT_stun_make_request_cancel(rh); + return NULL; + } return rh; } @@ -245,15 +242,15 @@ GNUNET_NAT_stun_make_request (const char *server, * @param rh request to cancel */ void -GNUNET_NAT_stun_make_request_cancel (struct GNUNET_NAT_STUN_Handle *rh) +GNUNET_NAT_stun_make_request_cancel(struct GNUNET_NAT_STUN_Handle *rh) { if (NULL != rh->dns_active) - { - GNUNET_RESOLVER_request_cancel (rh->dns_active); - rh->dns_active = NULL; - } - GNUNET_free (rh->stun_server); - GNUNET_free (rh); + { + GNUNET_RESOLVER_request_cancel(rh->dns_active); + rh->dns_active = NULL; + } + GNUNET_free(rh->stun_server); + GNUNET_free(rh); } diff --git a/src/nat/nat_stun.h b/src/nat/nat_stun.h index fb5262dfa..85b177271 100644 --- a/src/nat/nat_stun.h +++ b/src/nat/nat_stun.h @@ -11,12 +11,12 @@ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . SPDX-License-Identifier: AGPL3.0-or-later -*/ + */ /** * Message types for STUN server resolution * @@ -28,18 +28,17 @@ */ -#define STUN_IGNORE (0) -#define STUN_ACCEPT (1) +#define STUN_IGNORE (0) +#define STUN_ACCEPT (1) -#define STUN_MAGIC_COOKIE 0x2112A442 +#define STUN_MAGIC_COOKIE 0x2112A442 typedef struct { uint32_t id[3]; } GNUNET_PACKED stun_trans_id; -struct stun_header -{ +struct stun_header { uint16_t msgtype; uint16_t msglen; uint32_t magic; @@ -47,8 +46,7 @@ struct stun_header } GNUNET_PACKED; -struct stun_attr -{ +struct stun_attr { uint16_t attr; uint16_t len; } GNUNET_PACKED; @@ -57,14 +55,13 @@ struct stun_attr /** * The format normally used for addresses carried by STUN messages. */ -struct stun_addr -{ - uint8_t unused; +struct stun_addr { + uint8_t unused; /** * Address family, we expect AF_INET. */ - uint8_t family; + uint8_t family; /** * Port number. @@ -74,7 +71,7 @@ struct stun_addr /** * IPv4 address. Should this be "struct in_addr"? */ - uint32_t addr; + uint32_t addr; } GNUNET_PACKED; @@ -136,10 +133,10 @@ enum StunAttributes { * @return the converted StunClass */ static enum StunClasses -decode_class (int msg) +decode_class(int msg) { /* Sorry for the magic, but this maps the class according to rfc5245 */ - return (enum StunClasses) ((msg & 0x0010) >> 4) | ((msg & 0x0100) >> 7); + return (enum StunClasses)((msg & 0x0010) >> 4) | ((msg & 0x0100) >> 7); } @@ -150,9 +147,9 @@ decode_class (int msg) * @return the converted StunMethod */ static enum StunMethods -decode_method (int msg) +decode_method(int msg) { - return (enum StunMethods) (msg & 0x000f) | ((msg & 0x00e0) >> 1) | ((msg & 0x3e00) >> 2); + return (enum StunMethods)(msg & 0x000f) | ((msg & 0x00e0) >> 1) | ((msg & 0x3e00) >> 2); } @@ -164,7 +161,7 @@ decode_method (int msg) */ GNUNET_UNUSED static const char * -stun_msg2str (int msg) +stun_msg2str(int msg) { static const struct { enum StunClasses value; @@ -189,25 +186,25 @@ stun_msg2str (int msg) enum StunClasses cvalue; enum StunMethods mvalue; - cvalue = decode_class (msg); + cvalue = decode_class(msg); for (unsigned int i = 0; classes[i].name; i++) if (classes[i].value == cvalue) - { - msg_class = classes[i].name; - break; - } - mvalue = decode_method (msg); + { + msg_class = classes[i].name; + break; + } + mvalue = decode_method(msg); for (unsigned int i = 0; methods[i].name; i++) if (methods[i].value == mvalue) - { - method = methods[i].name; - break; - } - GNUNET_snprintf (result, - sizeof(result), - "%s %s", - method ? : "Unknown Method", - msg_class ? : "Unknown Class Message"); + { + method = methods[i].name; + break; + } + GNUNET_snprintf(result, + sizeof(result), + "%s %s", + method ? : "Unknown Method", + msg_class ? : "Unknown Class Message"); return result; } @@ -220,7 +217,7 @@ stun_msg2str (int msg) */ GNUNET_UNUSED static const char * -stun_attr2str (enum StunAttributes msg) +stun_attr2str(enum StunAttributes msg) { static const struct { enum StunAttributes value; diff --git a/src/nat/test_nat.c b/src/nat/test_nat.c index 80d7cf792..5758f63b9 100644 --- a/src/nat/test_nat.c +++ b/src/nat/test_nat.c @@ -11,12 +11,12 @@ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . SPDX-License-Identifier: AGPL3.0-or-later -*/ + */ /** * Testcase for port redirection and public IP address retrieval. @@ -43,7 +43,7 @@ /** * Time to wait before stopping NAT, in seconds */ -#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5) +#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5) /** @@ -51,15 +51,15 @@ * believes to be valid for the transport. */ static void -addr_callback (void *cls, int add_remove, const struct sockaddr *addr, - socklen_t addrlen) +addr_callback(void *cls, int add_remove, const struct sockaddr *addr, + socklen_t addrlen) { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Address changed: %s `%s' (%u bytes)\n", - add_remove == GNUNET_YES ? "added" : "removed", - GNUNET_a2s (addr, - addrlen), - (unsigned int) addrlen); + GNUNET_log(GNUNET_ERROR_TYPE_INFO, + "Address changed: %s `%s' (%u bytes)\n", + add_remove == GNUNET_YES ? "added" : "removed", + GNUNET_a2s(addr, + addrlen), + (unsigned int)addrlen); } @@ -67,18 +67,17 @@ addr_callback (void *cls, int add_remove, const struct sockaddr *addr, * Function that terminates the test. */ static void -stop (void *cls) +stop(void *cls) { struct GNUNET_NAT_Handle *nat = cls; - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Stopping NAT and quitting...\n"); - GNUNET_NAT_unregister (nat); + GNUNET_log(GNUNET_ERROR_TYPE_INFO, + "Stopping NAT and quitting...\n"); + GNUNET_NAT_unregister(nat); } -struct addr_cls -{ +struct addr_cls { struct sockaddr *addr; socklen_t addrlen; }; @@ -96,21 +95,21 @@ struct addr_cls * @return #GNUNET_OK to continue iterating */ static int -process_if (void *cls, - const char *name, - int isDefault, - const struct sockaddr *addr, - const struct sockaddr *broadcast_addr, - const struct sockaddr *netmask, - socklen_t addrlen) +process_if(void *cls, + const char *name, + int isDefault, + const struct sockaddr *addr, + const struct sockaddr *broadcast_addr, + const struct sockaddr *netmask, + socklen_t addrlen) { struct addr_cls *data = cls; if (addr == NULL) return GNUNET_OK; - GNUNET_free_non_null (data->addr); - data->addr = GNUNET_malloc (addrlen); - GNUNET_memcpy (data->addr, addr, addrlen); + GNUNET_free_non_null(data->addr); + data->addr = GNUNET_malloc(addrlen); + GNUNET_memcpy(data->addr, addr, addrlen); data->addrlen = addrlen; if (isDefault) return GNUNET_SYSERR; @@ -122,44 +121,44 @@ process_if (void *cls, * Main function run with scheduler. */ static void -run (void *cls, - char *const *args, - const char *cfgfile, - const struct GNUNET_CONFIGURATION_Handle *cfg) +run(void *cls, + char *const *args, + const char *cfgfile, + const struct GNUNET_CONFIGURATION_Handle *cfg) { struct GNUNET_NAT_Handle *nat; struct addr_cls data; struct sockaddr *addr; data.addr = NULL; - GNUNET_OS_network_interfaces_list (process_if, &data); + GNUNET_OS_network_interfaces_list(process_if, &data); if (NULL == data.addr) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Could not find a valid interface address!\n"); - exit (77); /* marks test as skipped */ - } + { + GNUNET_log(GNUNET_ERROR_TYPE_ERROR, + "Could not find a valid interface address!\n"); + exit(77); /* marks test as skipped */ + } addr = data.addr; - GNUNET_assert (addr->sa_family == AF_INET || addr->sa_family == AF_INET6); + GNUNET_assert(addr->sa_family == AF_INET || addr->sa_family == AF_INET6); if (addr->sa_family == AF_INET) - ((struct sockaddr_in *) addr)->sin_port = htons (2086); + ((struct sockaddr_in *)addr)->sin_port = htons(2086); else - ((struct sockaddr_in6 *) addr)->sin6_port = htons (2086); + ((struct sockaddr_in6 *)addr)->sin6_port = htons(2086); - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Requesting NAT redirection from address %s...\n", - GNUNET_a2s (addr, data.addrlen)); + GNUNET_log(GNUNET_ERROR_TYPE_INFO, + "Requesting NAT redirection from address %s...\n", + GNUNET_a2s(addr, data.addrlen)); - nat = GNUNET_NAT_register (cfg, GNUNET_YES /* tcp */ , - 2086, 1, (const struct sockaddr **) &addr, - &data.addrlen, &addr_callback, NULL, NULL, NULL); - GNUNET_free (addr); - GNUNET_SCHEDULER_add_delayed (TIMEOUT, &stop, nat); + nat = GNUNET_NAT_register(cfg, GNUNET_YES /* tcp */, + 2086, 1, (const struct sockaddr **)&addr, + &data.addrlen, &addr_callback, NULL, NULL, NULL); + GNUNET_free(addr); + GNUNET_SCHEDULER_add_delayed(TIMEOUT, &stop, nat); } int -main (int argc, char *const argv[]) +main(int argc, char *const argv[]) { struct GNUNET_GETOPT_CommandLineOption options[] = { GNUNET_GETOPT_OPTION_END @@ -171,19 +170,20 @@ main (int argc, char *const argv[]) "test_nat_data.conf", NULL }; - GNUNET_log_setup ("test-nat", - "WARNING", - NULL); - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Testing NAT library, timeout set to %s\n", - GNUNET_STRINGS_relative_time_to_string (TIMEOUT, - GNUNET_YES)); - GNUNET_PROGRAM_run (3, - argv_prog, - "test-nat", - "nohelp", - options, - &run, NULL); + + GNUNET_log_setup("test-nat", + "WARNING", + NULL); + GNUNET_log(GNUNET_ERROR_TYPE_INFO, + "Testing NAT library, timeout set to %s\n", + GNUNET_STRINGS_relative_time_to_string(TIMEOUT, + GNUNET_YES)); + GNUNET_PROGRAM_run(3, + argv_prog, + "test-nat", + "nohelp", + options, + &run, NULL); return 0; } diff --git a/src/nat/test_nat_mini.c b/src/nat/test_nat_mini.c index 8a7ff16f4..09231d8ec 100644 --- a/src/nat/test_nat_mini.c +++ b/src/nat/test_nat_mini.c @@ -11,12 +11,12 @@ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . SPDX-License-Identifier: AGPL3.0-or-later -*/ + */ /** * Testcase for port redirection and public IP address retrieval. @@ -35,31 +35,31 @@ #include "gnunet_nat_lib.h" /* Time to wait before stopping NAT, in seconds */ -#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5) +#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5) /** * Function called on each address that the NAT service * believes to be valid for the transport. */ static void -addr_callback (void *cls, int add_remove, - const struct sockaddr *addr, - socklen_t addrlen, - enum GNUNET_NAT_StatusCode ret) +addr_callback(void *cls, int add_remove, + const struct sockaddr *addr, + socklen_t addrlen, + enum GNUNET_NAT_StatusCode ret) { if (GNUNET_NAT_ERROR_SUCCESS == ret) - { - fprintf (stderr, - "Address changed: %s `%s' (%u bytes)\n", - add_remove == GNUNET_YES - ? "added" : "removed", - GNUNET_a2s (addr, - addrlen), - (unsigned int) addrlen); - } + { + fprintf(stderr, + "Address changed: %s `%s' (%u bytes)\n", + add_remove == GNUNET_YES + ? "added" : "removed", + GNUNET_a2s(addr, + addrlen), + (unsigned int)addrlen); + } else ; - //TODO: proper error handling! + //TODO: proper error handling! } @@ -67,12 +67,12 @@ addr_callback (void *cls, int add_remove, * Function that terminates the test. */ static void -stop (void *cls) +stop(void *cls) { struct GNUNET_NAT_MiniHandle *mini = cls; - GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping NAT and quitting...\n"); - GNUNET_NAT_mini_map_stop (mini); + GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Stopping NAT and quitting...\n"); + GNUNET_NAT_mini_map_stop(mini); } #define PORT 10000 @@ -81,27 +81,27 @@ stop (void *cls) * Main function run with scheduler. */ static void -run (void *cls, char *const *args, const char *cfgfile, - const struct GNUNET_CONFIGURATION_Handle *cfg) +run(void *cls, char *const *args, const char *cfgfile, + const struct GNUNET_CONFIGURATION_Handle *cfg) { struct GNUNET_NAT_MiniHandle *mini; - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Requesting NAT redirection for port %u...\n", - PORT); - mini = GNUNET_NAT_mini_map_start (PORT, GNUNET_YES /* tcp */ , - &addr_callback, NULL); + GNUNET_log(GNUNET_ERROR_TYPE_INFO, + "Requesting NAT redirection for port %u...\n", + PORT); + mini = GNUNET_NAT_mini_map_start(PORT, GNUNET_YES /* tcp */, + &addr_callback, NULL); if (NULL == mini) - { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Could not start UPnP interaction\n"); - return; - } - GNUNET_SCHEDULER_add_delayed (TIMEOUT, &stop, mini); + { + GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Could not start UPnP interaction\n"); + return; + } + GNUNET_SCHEDULER_add_delayed(TIMEOUT, &stop, mini); } int -main (int argc, char *const argv[]) +main(int argc, char *const argv[]) { struct GNUNET_GETOPT_CommandLineOption options[] = { GNUNET_GETOPT_OPTION_END @@ -116,16 +116,16 @@ main (int argc, char *const argv[]) NULL }; - GNUNET_log_setup ("test-nat-mini", - "WARNING", - NULL); + GNUNET_log_setup("test-nat-mini", + "WARNING", + NULL); - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "UPnP test for NAT library, timeout set to %s\n", - GNUNET_STRINGS_relative_time_to_string (TIMEOUT, - GNUNET_YES)); - GNUNET_PROGRAM_run (5, argv_prog, "test-nat-mini", "nohelp", options, &run, - NULL); + GNUNET_log(GNUNET_ERROR_TYPE_INFO, + "UPnP test for NAT library, timeout set to %s\n", + GNUNET_STRINGS_relative_time_to_string(TIMEOUT, + GNUNET_YES)); + GNUNET_PROGRAM_run(5, argv_prog, "test-nat-mini", "nohelp", options, &run, + NULL); return 0; } diff --git a/src/nat/test_nat_test.c b/src/nat/test_nat_test.c index c9479a688..e217c1d29 100644 --- a/src/nat/test_nat_test.c +++ b/src/nat/test_nat_test.c @@ -11,12 +11,12 @@ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . SPDX-License-Identifier: AGPL3.0-or-later -*/ + */ /** * @file nat/test_nat_test.c * @brief Testcase for NAT testing functions @@ -29,7 +29,7 @@ /** * Time to wait before stopping NAT test, in seconds */ -#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15) +#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 15) static int ret = 1; @@ -40,32 +40,32 @@ static struct GNUNET_SCHEDULER_Task * tsk; static void -report_result (void *cls, - enum GNUNET_NAT_StatusCode aret) +report_result(void *cls, + enum GNUNET_NAT_StatusCode aret) { if (GNUNET_NAT_ERROR_TIMEOUT == aret) - fprintf (stderr, - "NAT test timed out\n"); + fprintf(stderr, + "NAT test timed out\n"); else if (GNUNET_NAT_ERROR_SUCCESS != aret) - fprintf (stderr, - "NAT test reported error %d\n", aret); + fprintf(stderr, + "NAT test reported error %d\n", aret); else ret = 0; - GNUNET_NAT_test_stop (tst); + GNUNET_NAT_test_stop(tst); tst = NULL; - GNUNET_SCHEDULER_cancel (tsk); + GNUNET_SCHEDULER_cancel(tsk); tsk = NULL; } static void -failed_timeout (void *cls) +failed_timeout(void *cls) { tsk = NULL; - fprintf (stderr, - "NAT test failed to terminate on timeout\n"); + fprintf(stderr, + "NAT test failed to terminate on timeout\n"); ret = 2; - GNUNET_NAT_test_stop (tst); + GNUNET_NAT_test_stop(tst); tst = NULL; } @@ -74,23 +74,22 @@ failed_timeout (void *cls) * Main function run with scheduler. */ static void -run (void *cls, char *const *args, const char *cfgfile, - const struct GNUNET_CONFIGURATION_Handle *cfg) +run(void *cls, char *const *args, const char *cfgfile, + const struct GNUNET_CONFIGURATION_Handle *cfg) { tst = - GNUNET_NAT_test_start (cfg, GNUNET_YES, 1285, 1285, TIMEOUT, - &report_result, - NULL); - tsk = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (TIMEOUT, - 2), - &failed_timeout, - NULL); - + GNUNET_NAT_test_start(cfg, GNUNET_YES, 1285, 1285, TIMEOUT, + &report_result, + NULL); + tsk = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(TIMEOUT, + 2), + &failed_timeout, + NULL); } int -main (int argc, char *const argv[]) +main(int argc, char *const argv[]) { struct GNUNET_GETOPT_CommandLineOption options[] = { GNUNET_GETOPT_OPTION_END @@ -104,37 +103,37 @@ main (int argc, char *const argv[]) NULL }; - GNUNET_log_setup ("test-nat-test", - "WARNING", - NULL); + GNUNET_log_setup("test-nat-test", + "WARNING", + NULL); - nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server", GNUNET_NO, NULL); + nat_res = GNUNET_OS_check_helper_binary("gnunet-nat-server", GNUNET_NO, NULL); if (GNUNET_SYSERR == nat_res) - { - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Cannot run NAT test: `%s' file not found\n", - "gnunet-nat-server"); - return 0; - } - - gns = GNUNET_OS_start_process (GNUNET_YES, - GNUNET_OS_INHERIT_STD_OUT_AND_ERR, - NULL, NULL, NULL, - "gnunet-nat-server", - "gnunet-nat-server", - "-c", "test_nat_test_data.conf", - "12345", NULL); - GNUNET_assert (NULL != gns); - GNUNET_PROGRAM_run (3, argv_prog, - "test-nat-test", "nohelp", - options, &run, - NULL); - GNUNET_break (0 == GNUNET_OS_process_kill (gns, GNUNET_TERM_SIG)); - GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (gns)); - GNUNET_OS_process_destroy (gns); + { + GNUNET_log(GNUNET_ERROR_TYPE_WARNING, + "Cannot run NAT test: `%s' file not found\n", + "gnunet-nat-server"); + return 0; + } + + gns = GNUNET_OS_start_process(GNUNET_YES, + GNUNET_OS_INHERIT_STD_OUT_AND_ERR, + NULL, NULL, NULL, + "gnunet-nat-server", + "gnunet-nat-server", + "-c", "test_nat_test_data.conf", + "12345", NULL); + GNUNET_assert(NULL != gns); + GNUNET_PROGRAM_run(3, argv_prog, + "test-nat-test", "nohelp", + options, &run, + NULL); + GNUNET_break(0 == GNUNET_OS_process_kill(gns, GNUNET_TERM_SIG)); + GNUNET_break(GNUNET_OK == GNUNET_OS_process_wait(gns)); + GNUNET_OS_process_destroy(gns); if (0 != ret) - fprintf (stderr, - "NAT test failed to report success\n"); + fprintf(stderr, + "NAT test failed to report success\n"); return ret; } diff --git a/src/nat/test_stun.c b/src/nat/test_stun.c index ee2db35e5..ee755905e 100644 --- a/src/nat/test_stun.c +++ b/src/nat/test_stun.c @@ -11,12 +11,12 @@ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . SPDX-License-Identifier: AGPL3.0-or-later -*/ + */ /** * Testcase for STUN server resolution @@ -36,12 +36,12 @@ -#define LOG(kind,...) GNUNET_log_from (kind, "test-stun", __VA_ARGS__) +#define LOG(kind, ...) GNUNET_log_from(kind, "test-stun", __VA_ARGS__) /** * Time to wait before stopping NAT, in seconds */ -#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5) +#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5) /** @@ -74,10 +74,10 @@ static struct GNUNET_NAT_STUN_Handle *rh; static void print_answer(struct sockaddr_in* answer) { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "External IP is: %s , with port %d\n", - inet_ntoa (answer->sin_addr), - ntohs (answer->sin_port)); + GNUNET_log(GNUNET_ERROR_TYPE_INFO, + "External IP is: %s , with port %d\n", + inet_ntoa(answer->sin_addr), + ntohs(answer->sin_port)); } @@ -85,25 +85,25 @@ print_answer(struct sockaddr_in* answer) * Function that terminates the test. */ static void -stop () +stop() { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Stopping NAT and quitting...\n"); + GNUNET_log(GNUNET_ERROR_TYPE_INFO, + "Stopping NAT and quitting...\n"); if (NULL != ltask4) - { - GNUNET_SCHEDULER_cancel (ltask4); - ltask4 = NULL; - } - if(NULL != lsock4) - { - GNUNET_NETWORK_socket_close(lsock4); - lsock4 = NULL; - } + { + GNUNET_SCHEDULER_cancel(ltask4); + ltask4 = NULL; + } + if (NULL != lsock4) + { + GNUNET_NETWORK_socket_close(lsock4); + lsock4 = NULL; + } if (NULL != rh) - { - GNUNET_NAT_stun_make_request_cancel (rh); - rh = NULL; - } + { + GNUNET_NAT_stun_make_request_cancel(rh); + rh = NULL; + } } @@ -114,7 +114,7 @@ stop () * @param cls */ static void -do_udp_read (void *cls) +do_udp_read(void *cls) { //struct GNUNET_NAT_Test *tst = cls; unsigned char reply_buf[1024]; @@ -123,36 +123,36 @@ do_udp_read (void *cls) const struct GNUNET_SCHEDULER_TaskContext *tc; ltask4 = NULL; - tc = GNUNET_SCHEDULER_get_task_context (); - if ( (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) || - (! GNUNET_NETWORK_fdset_isset (tc->read_ready, - lsock4)) ) - { - fprintf (stderr, - "Timeout waiting for STUN response\n"); - stop(); - } - rlen = GNUNET_NETWORK_socket_recv (lsock4, - reply_buf, - sizeof (reply_buf)); - memset (&answer, - 0, - sizeof(struct sockaddr_in)); + tc = GNUNET_SCHEDULER_get_task_context(); + if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) || + (!GNUNET_NETWORK_fdset_isset(tc->read_ready, + lsock4))) + { + fprintf(stderr, + "Timeout waiting for STUN response\n"); + stop(); + } + rlen = GNUNET_NETWORK_socket_recv(lsock4, + reply_buf, + sizeof(reply_buf)); + memset(&answer, + 0, + sizeof(struct sockaddr_in)); if (GNUNET_OK != - GNUNET_NAT_stun_handle_packet (reply_buf, - rlen, - &answer)) - { - fprintf (stderr, - "Unexpected UDP packet, trying to read more\n"); - ltask4 = GNUNET_SCHEDULER_add_read_net (TIMEOUT, - lsock4, - &do_udp_read, NULL); - return; - } + GNUNET_NAT_stun_handle_packet(reply_buf, + rlen, + &answer)) + { + fprintf(stderr, + "Unexpected UDP packet, trying to read more\n"); + ltask4 = GNUNET_SCHEDULER_add_read_net(TIMEOUT, + lsock4, + &do_udp_read, NULL); + return; + } ret = 0; - print_answer (&answer); - stop (); + print_answer(&answer); + stop(); } @@ -162,33 +162,33 @@ do_udp_read (void *cls) * @return NULL on error */ static struct GNUNET_NETWORK_Handle * -bind_v4 () +bind_v4() { struct GNUNET_NETWORK_Handle *ls; struct sockaddr_in sa4; int eno; - memset (&sa4, 0, sizeof (sa4)); + memset(&sa4, 0, sizeof(sa4)); sa4.sin_family = AF_INET; - sa4.sin_port = htons (port); + sa4.sin_port = htons(port); #if HAVE_SOCKADDR_IN_SIN_LEN - sa4.sin_len = sizeof (sa4); + sa4.sin_len = sizeof(sa4); #endif - ls = GNUNET_NETWORK_socket_create (AF_INET, - SOCK_DGRAM, - 0); + ls = GNUNET_NETWORK_socket_create(AF_INET, + SOCK_DGRAM, + 0); if (NULL == ls) return NULL; if (GNUNET_OK != - GNUNET_NETWORK_socket_bind (ls, - (const struct sockaddr *) &sa4, - sizeof (sa4))) - { - eno = errno; - GNUNET_NETWORK_socket_close (ls); - errno = eno; - return NULL; - } + GNUNET_NETWORK_socket_bind(ls, + (const struct sockaddr *)&sa4, + sizeof(sa4))) + { + eno = errno; + GNUNET_NETWORK_socket_close(ls); + errno = eno; + return NULL; + } return ls; } @@ -200,29 +200,29 @@ bind_v4 () * @param error status code from STUN */ static void -request_callback (void *cls, - enum GNUNET_NAT_StatusCode error) +request_callback(void *cls, + enum GNUNET_NAT_StatusCode error) { rh = NULL; if (GNUNET_NAT_ERROR_SUCCESS == error) - { - /* all good, start to receive */ - ltask4 = GNUNET_SCHEDULER_add_read_net (TIMEOUT, - lsock4, - &do_udp_read, - NULL); - return; - } + { + /* all good, start to receive */ + ltask4 = GNUNET_SCHEDULER_add_read_net(TIMEOUT, + lsock4, + &do_udp_read, + NULL); + return; + } if (error == GNUNET_NAT_ERROR_NOT_ONLINE) - { - ret = 77; /* report 'skip' */ - fprintf (stderr, - "System is offline, cannot test STUN request.\n"); - } + { + ret = 77; /* report 'skip' */ + fprintf(stderr, + "System is offline, cannot test STUN request.\n"); + } else - { - ret = error; - } + { + ret = error; + } stop(); } @@ -231,81 +231,81 @@ request_callback (void *cls, * Main function run with scheduler. */ static void -run (void *cls, - char *const *args, - const char *cfgfile, - const struct GNUNET_CONFIGURATION_Handle *cfg) +run(void *cls, + char *const *args, + const char *cfgfile, + const struct GNUNET_CONFIGURATION_Handle *cfg) { //Lets create the socket - lsock4 = bind_v4 (); + lsock4 = bind_v4(); if (NULL == lsock4) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, - "bind"); - GNUNET_SCHEDULER_shutdown (); - return; - } - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Service listens on port %u\n", - (unsigned int) port); - rh = GNUNET_NAT_stun_make_request (stun_server, - stun_port, - lsock4, - &request_callback, NULL); - GNUNET_SCHEDULER_add_delayed (TIMEOUT, - &stop, NULL); + { + GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, + "bind"); + GNUNET_SCHEDULER_shutdown(); + return; + } + GNUNET_log(GNUNET_ERROR_TYPE_INFO, + "Service listens on port %u\n", + (unsigned int)port); + rh = GNUNET_NAT_stun_make_request(stun_server, + stun_port, + lsock4, + &request_callback, NULL); + GNUNET_SCHEDULER_add_delayed(TIMEOUT, + &stop, NULL); } int -main (int argc, char *const argv[]) +main(int argc, char *const argv[]) { struct GNUNET_GETOPT_CommandLineOption options[] = { - GNUNET_GETOPT_OPTION_END + GNUNET_GETOPT_OPTION_END }; char *const argv_prog[] = { - "test-stun", - "-c", - "test_stun.conf", - NULL + "test-stun", + "-c", + "test_stun.conf", + NULL }; char *fn; struct GNUNET_OS_Process *proc; - GNUNET_log_setup ("test-stun", - "WARNING", - NULL); + GNUNET_log_setup("test-stun", + "WARNING", + NULL); /* Lets start resolver */ - fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver"); - proc = GNUNET_OS_start_process (GNUNET_YES, - GNUNET_OS_INHERIT_STD_OUT_AND_ERR, - NULL, NULL, NULL, - fn, - "gnunet-service-resolver", - "-c", "test_stun.conf", NULL); + fn = GNUNET_OS_get_libexec_binary_path("gnunet-service-resolver"); + proc = GNUNET_OS_start_process(GNUNET_YES, + GNUNET_OS_INHERIT_STD_OUT_AND_ERR, + NULL, NULL, NULL, + fn, + "gnunet-service-resolver", + "-c", "test_stun.conf", NULL); if (NULL == proc) - { - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "This test was unable to start gnunet-service-resolver, and it is required to run ...\n"); - exit(1); - } + { + GNUNET_log(GNUNET_ERROR_TYPE_INFO, + "This test was unable to start gnunet-service-resolver, and it is required to run ...\n"); + exit(1); + } - GNUNET_PROGRAM_run (3, argv_prog, - "test-stun", "nohelp", - options, - &run, NULL); + GNUNET_PROGRAM_run(3, argv_prog, + "test-stun", "nohelp", + options, + &run, NULL); /* Now kill the resolver */ - if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG)) - { - GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); - } - GNUNET_OS_process_wait (proc); - GNUNET_OS_process_destroy (proc); + if (0 != GNUNET_OS_process_kill(proc, GNUNET_TERM_SIG)) + { + GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill"); + } + GNUNET_OS_process_wait(proc); + GNUNET_OS_process_destroy(proc); proc = NULL; - GNUNET_free (fn); + GNUNET_free(fn); return ret; } -- cgit v1.2.3