From 35f13ef51dbf99c3d45d04f572e9e88795df7ae6 Mon Sep 17 00:00:00 2001 From: iphydf Date: Wed, 14 Feb 2018 20:51:55 +0000 Subject: [PATCH] Get rid of the only GNU extension we used. --- auto_tests/TCP_test.c | 4 +-- auto_tests/dht_test.c | 14 ++++----- auto_tests/network_test.c | 30 +++++++++--------- auto_tests/onion_test.c | 6 ++-- toxcore/DHT.c | 16 +++++----- toxcore/LAN_discovery.c | 36 ++++++++++----------- toxcore/Messenger.c | 2 +- toxcore/TCP_client.c | 4 +-- toxcore/TCP_server.c | 10 +++--- toxcore/ccompat.h | 7 ----- toxcore/net_crypto.c | 4 +-- toxcore/network.c | 66 +++++++++++++++++++-------------------- toxcore/network.h | 8 ++--- toxcore/onion.c | 8 ++--- toxcore/onion_client.c | 6 ++-- 15 files changed, 107 insertions(+), 114 deletions(-) diff --git a/auto_tests/TCP_test.c b/auto_tests/TCP_test.c index 26a95942..2ae94618 100644 --- a/auto_tests/TCP_test.c +++ b/auto_tests/TCP_test.c @@ -39,10 +39,10 @@ static inline IP get_loopback() IP ip; #if USE_IPV6 ip.family = TOX_AF_INET6; - ip.ip6 = get_ip6_loopback(); + ip.ip.v6 = get_ip6_loopback(); #else ip.family = TOX_AF_INET; - ip.ip4 = get_ip4_loopback(); + ip.ip.v4 = get_ip4_loopback(); #endif return ip; } diff --git a/auto_tests/dht_test.c b/auto_tests/dht_test.c index 8e735dbd..10c9c7bf 100644 --- a/auto_tests/dht_test.c +++ b/auto_tests/dht_test.c @@ -38,10 +38,10 @@ static inline IP get_loopback() IP ip; #if USE_IPV6 ip.family = TOX_AF_INET6; - ip.ip6 = get_ip6_loopback(); + ip.ip.v6 = get_ip6_loopback(); #else ip.family = TOX_AF_INET; - ip.ip4 = get_ip4_loopback(); + ip.ip.v4 = get_ip4_loopback(); #endif return ip; } @@ -489,7 +489,7 @@ static void test_list_main(void) IP_Port ip_port; ip_init(&ip_port.ip, 0); - ip_port.ip.ip4.uint32 = rand(); + ip_port.ip.ip.v4.uint32 = rand(); ip_port.port = rand() % (UINT16_MAX - 1); ++ip_port.port; addto_lists(dhts[j], ip_port, dhts[i]->self_public_key); @@ -723,11 +723,11 @@ static void random_ip(IP_Port *ipp, int family) size_t size; if (family == TOX_AF_INET || family == TCP_INET) { - ip = (uint8_t *)&ipp->ip.ip4; - size = sizeof(ipp->ip.ip4); + ip = (uint8_t *)&ipp->ip.ip.v4; + size = sizeof(ipp->ip.ip.v4); } else if (family == TOX_AF_INET6 || family == TCP_INET6) { - ip = (uint8_t *)&ipp->ip.ip6; - size = sizeof(ipp->ip.ip6); + ip = (uint8_t *)&ipp->ip.ip.v6; + size = sizeof(ipp->ip.ip.v6); } else { return; } diff --git a/auto_tests/network_test.c b/auto_tests/network_test.c index 1885c81f..f05f0dbd 100644 --- a/auto_tests/network_test.c +++ b/auto_tests/network_test.c @@ -43,7 +43,7 @@ START_TEST(test_addr_resolv_localhost) char ip_str[IP_NTOA_LEN]; ck_assert_msg(ip.family == TOX_AF_INET, "Expected family TOX_AF_INET, got %u.", ip.family); const uint32_t loopback = get_ip4_loopback().uint32; - ck_assert_msg(ip.ip4.uint32 == loopback, "Expected 127.0.0.1, got %s.", + ck_assert_msg(ip.ip.v4.uint32 == loopback, "Expected 127.0.0.1, got %s.", ip_ntoa(&ip, ip_str, sizeof(ip_str))); ip_init(&ip, 1); // ipv6enabled = 1 @@ -58,7 +58,7 @@ START_TEST(test_addr_resolv_localhost) ck_assert_msg(ip.family == TOX_AF_INET6, "Expected family TOX_AF_INET6 (%u), got %u.", TOX_AF_INET6, ip.family); IP6 ip6_loopback = get_ip6_loopback(); - ck_assert_msg(!memcmp(&ip.ip6, &ip6_loopback, sizeof(IP6)), "Expected ::1, got %s.", + ck_assert_msg(!memcmp(&ip.ip.v6, &ip6_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip, ip_str, sizeof(ip_str))); if (localhost_split) { @@ -75,15 +75,15 @@ START_TEST(test_addr_resolv_localhost) #if USE_IPV6 ck_assert_msg(ip.family == TOX_AF_INET6, "Expected family TOX_AF_INET6 (%u), got %u.", TOX_AF_INET6, ip.family); - ck_assert_msg(!memcmp(&ip.ip6, &ip6_loopback, sizeof(IP6)), "Expected ::1, got %s.", + ck_assert_msg(!memcmp(&ip.ip.v6, &ip6_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip, ip_str, sizeof(ip_str))); ck_assert_msg(extra.family == TOX_AF_INET, "Expected family TOX_AF_INET (%u), got %u.", TOX_AF_INET, extra.family); - ck_assert_msg(extra.ip4.uint32 == loopback, "Expected 127.0.0.1, got %s.", + ck_assert_msg(extra.ip.v4.uint32 == loopback, "Expected 127.0.0.1, got %s.", ip_ntoa(&ip, ip_str, sizeof(ip_str))); #else ck_assert_msg(ip.family == TOX_AF_INET, "Expected family TOX_AF_INET (%u), got %u.", TOX_AF_INET, ip.family); - ck_assert_msg(ip.ip4.uint32 == loopback, "Expected 127.0.0.1, got %s.", + ck_assert_msg(ip.ip.v4.uint32 == loopback, "Expected 127.0.0.1, got %s.", ip_ntoa(&ip, ip_str, sizeof(ip_str))); #endif } @@ -106,32 +106,32 @@ START_TEST(test_ip_equal) ck_assert_msg(res == 0, "ip_equal(NULL, PTR): expected result 0, got %u.", res); ip1.family = TOX_AF_INET; - ip1.ip4.uint32 = net_htonl(0x7F000001); + ip1.ip.v4.uint32 = net_htonl(0x7F000001); res = ip_equal(&ip1, &ip2); ck_assert_msg(res == 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_UNSPEC, 0} ): " "expected result 0, got %u.", res); ip2.family = TOX_AF_INET; - ip2.ip4.uint32 = net_htonl(0x7F000001); + ip2.ip.v4.uint32 = net_htonl(0x7F000001); res = ip_equal(&ip1, &ip2); ck_assert_msg(res != 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_INET, 127.0.0.1} ): " "expected result != 0, got 0."); - ip2.ip4.uint32 = net_htonl(0x7F000002); + ip2.ip.v4.uint32 = net_htonl(0x7F000002); res = ip_equal(&ip1, &ip2); ck_assert_msg(res == 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_INET, 127.0.0.2} ): " "expected result 0, got %u.", res); ip2.family = TOX_AF_INET6; - ip2.ip6.uint32[0] = 0; - ip2.ip6.uint32[1] = 0; - ip2.ip6.uint32[2] = net_htonl(0xFFFF); - ip2.ip6.uint32[3] = net_htonl(0x7F000001); + ip2.ip.v6.uint32[0] = 0; + ip2.ip.v6.uint32[1] = 0; + ip2.ip.v6.uint32[2] = net_htonl(0xFFFF); + ip2.ip.v6.uint32[3] = net_htonl(0x7F000001); - ck_assert_msg(IPV6_IPV4_IN_V6(ip2.ip6) != 0, + ck_assert_msg(IPV6_IPV4_IN_V6(ip2.ip.v6) != 0, "IPV6_IPV4_IN_V6(::ffff:127.0.0.1): expected != 0, got 0."); res = ip_equal(&ip1, &ip2); @@ -139,7 +139,7 @@ START_TEST(test_ip_equal) "expected result != 0, got 0."); IP6 ip6_loopback = get_ip6_loopback(); - memcpy(&ip2.ip6, &ip6_loopback, sizeof(IP6)); + memcpy(&ip2.ip.v6, &ip6_loopback, sizeof(IP6)); res = ip_equal(&ip1, &ip2); ck_assert_msg(res == 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_INET6, ::1} ): expected result 0, got %u.", res); @@ -147,7 +147,7 @@ START_TEST(test_ip_equal) res = ip_equal(&ip1, &ip2); ck_assert_msg(res != 0, "ip_equal( {TOX_AF_INET6, ::1}, {TOX_AF_INET6, ::1} ): expected result != 0, got 0."); - ip2.ip6.uint8[15]++; + ip2.ip.v6.uint8[15]++; res = ip_equal(&ip1, &ip2); ck_assert_msg(res == 0, "ip_equal( {TOX_AF_INET6, ::1}, {TOX_AF_INET6, ::2} ): expected result 0, got %u.", res); } diff --git a/auto_tests/onion_test.c b/auto_tests/onion_test.c index 5989df17..aa505a0b 100644 --- a/auto_tests/onion_test.c +++ b/auto_tests/onion_test.c @@ -30,10 +30,10 @@ static inline IP get_loopback() IP ip; #if USE_IPV6 ip.family = TOX_AF_INET6; - ip.ip6 = get_ip6_loopback(); + ip.ip.v6 = get_ip6_loopback(); #else ip.family = TOX_AF_INET; - ip.ip4 = get_ip4_loopback(); + ip.ip.v4 = get_ip4_loopback(); #endif return ip; } @@ -308,7 +308,7 @@ typedef struct { static Onions *new_onions(uint16_t port) { IP ip = get_loopback(); - ip.ip6.uint8[15] = 1; + ip.ip.v6.uint8[15] = 1; Onions *on = (Onions *)malloc(sizeof(Onions)); if (!on) { diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 28ff75c5..1b0302b3 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -406,7 +406,7 @@ int pack_ip_port(uint8_t *data, uint16_t length, const IP_Port *ip_port) } data[0] = net_family; - memcpy(data + 1, &ip_port->ip.ip4, SIZE_IP4); + memcpy(data + 1, &ip_port->ip.ip.v4, SIZE_IP4); memcpy(data + 1 + SIZE_IP4, &ip_port->port, sizeof(uint16_t)); return size; } else { @@ -417,7 +417,7 @@ int pack_ip_port(uint8_t *data, uint16_t length, const IP_Port *ip_port) } data[0] = net_family; - memcpy(data + 1, &ip_port->ip.ip6, SIZE_IP6); + memcpy(data + 1, &ip_port->ip.ip.v6, SIZE_IP6); memcpy(data + 1 + SIZE_IP6, &ip_port->port, sizeof(uint16_t)); return size; } @@ -491,7 +491,7 @@ int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, uint8 } ip_port->ip.family = host_family; - memcpy(&ip_port->ip.ip4, data + 1, SIZE_IP4); + memcpy(&ip_port->ip.ip.v4, data + 1, SIZE_IP4); memcpy(&ip_port->port, data + 1 + SIZE_IP4, sizeof(uint16_t)); return size; } else { @@ -502,7 +502,7 @@ int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, uint8 } ip_port->ip.family = host_family; - memcpy(&ip_port->ip.ip6, data + 1, SIZE_IP6); + memcpy(&ip_port->ip.ip.v6, data + 1, SIZE_IP6); memcpy(&ip_port->port, data + 1 + SIZE_IP6, sizeof(uint16_t)); return size; } @@ -1143,9 +1143,9 @@ uint32_t addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *public_key) uint32_t used = 0; /* convert IPv4-in-IPv6 to IPv4 */ - if ((ip_port.ip.family == TOX_AF_INET6) && IPV6_IPV4_IN_V6(ip_port.ip.ip6)) { + if ((ip_port.ip.family == TOX_AF_INET6) && IPV6_IPV4_IN_V6(ip_port.ip.ip.v6)) { ip_port.ip.family = TOX_AF_INET; - ip_port.ip.ip4.uint32 = ip_port.ip.ip6.uint32[3]; + ip_port.ip.ip.v4.uint32 = ip_port.ip.ip.v6.uint32[3]; } /* NOTE: Current behavior if there are two clients with the same id is @@ -1223,9 +1223,9 @@ static bool update_client_data(Client_data *array, size_t size, IP_Port ip_port, static void returnedip_ports(DHT *dht, IP_Port ip_port, const uint8_t *public_key, const uint8_t *nodepublic_key) { /* convert IPv4-in-IPv6 to IPv4 */ - if ((ip_port.ip.family == TOX_AF_INET6) && IPV6_IPV4_IN_V6(ip_port.ip.ip6)) { + if ((ip_port.ip.family == TOX_AF_INET6) && IPV6_IPV4_IN_V6(ip_port.ip.ip.v6)) { ip_port.ip.family = TOX_AF_INET; - ip_port.ip.ip4.uint32 = ip_port.ip.ip6.uint32[3]; + ip_port.ip.ip.v4.uint32 = ip_port.ip.ip.v6.uint32[3]; } if (id_equal(public_key, dht->self_public_key)) { diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c index 2f43ea11..f5883060 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c @@ -79,9 +79,9 @@ static void fetch_broadcast_info(uint16_t port) if (gateway.family == TOX_AF_INET && subnet_mask.family == TOX_AF_INET) { IP_Port *ip_port = &ip_ports[count]; ip_port->ip.family = TOX_AF_INET; - uint32_t gateway_ip = net_ntohl(gateway.ip4.uint32), subnet_ip = net_ntohl(subnet_mask.ip4.uint32); + uint32_t gateway_ip = net_ntohl(gateway.ip.v4.uint32), subnet_ip = net_ntohl(subnet_mask.ip.v4.uint32); uint32_t broadcast_ip = gateway_ip + ~subnet_ip - 1; - ip_port->ip.ip4.uint32 = net_htonl(broadcast_ip); + ip_port->ip.ip.v4.uint32 = net_htonl(broadcast_ip); ip_port->port = port; count++; @@ -177,9 +177,9 @@ static void fetch_broadcast_info(uint16_t port) IP_Port *ip_port = &ip_ports[count]; ip_port->ip.family = TOX_AF_INET; - ip_port->ip.ip4.uint32 = sock4->sin_addr.s_addr; + ip_port->ip.ip.v4.uint32 = sock4->sin_addr.s_addr; - if (ip_port->ip.ip4.uint32 == 0) { + if (ip_port->ip.ip.v4.uint32 == 0) { continue; } @@ -240,17 +240,17 @@ static IP broadcast_ip(Family family_socket, Family family_broadcast) /* FF02::1 is - according to RFC 4291 - multicast all-nodes link-local */ /* FE80::*: MUST be exact, for that we would need to look over all * interfaces and check in which status they are */ - ip.ip6.uint8[ 0] = 0xFF; - ip.ip6.uint8[ 1] = 0x02; - ip.ip6.uint8[15] = 0x01; + ip.ip.v6.uint8[ 0] = 0xFF; + ip.ip.v6.uint8[ 1] = 0x02; + ip.ip.v6.uint8[15] = 0x01; } else if (family_broadcast == TOX_AF_INET) { ip.family = TOX_AF_INET6; - ip.ip6 = IP6_BROADCAST; + ip.ip.v6 = IP6_BROADCAST; } } else if (family_socket == TOX_AF_INET) { if (family_broadcast == TOX_AF_INET) { ip.family = TOX_AF_INET; - ip.ip4 = IP4_BROADCAST; + ip.ip.v4 = IP4_BROADCAST; } } @@ -261,7 +261,7 @@ static IP broadcast_ip(Family family_socket, Family family_broadcast) bool ip_is_local(IP ip) { if (ip.family == TOX_AF_INET) { - IP4 ip4 = ip.ip4; + IP4 ip4 = ip.ip.v4; /* Loopback. */ if (ip4.uint8[0] == 127) { @@ -269,15 +269,15 @@ bool ip_is_local(IP ip) } } else { /* embedded IPv4-in-IPv6 */ - if (IPV6_IPV4_IN_V6(ip.ip6)) { + if (IPV6_IPV4_IN_V6(ip.ip.v6)) { IP ip4; ip4.family = TOX_AF_INET; - ip4.ip4.uint32 = ip.ip6.uint32[3]; + ip4.ip.v4.uint32 = ip.ip.v6.uint32[3]; return ip_is_local(ip4); } /* localhost in IPv6 (::1) */ - if (ip.ip6.uint64[0] == 0 && ip.ip6.uint32[2] == 0 && ip.ip6.uint32[3] == net_htonl(1)) { + if (ip.ip.v6.uint64[0] == 0 && ip.ip.v6.uint32[2] == 0 && ip.ip.v6.uint32[3] == net_htonl(1)) { return 1; } } @@ -295,7 +295,7 @@ int ip_is_lan(IP ip) } if (ip.family == TOX_AF_INET) { - IP4 ip4 = ip.ip4; + IP4 ip4 = ip.ip.v4; /* 10.0.0.0 to 10.255.255.255 range. */ if (ip4.uint8[0] == 10) { @@ -327,16 +327,16 @@ int ip_is_lan(IP ip) /* autogenerated for each interface: FE80::* (up to FEBF::*) FF02::1 is - according to RFC 4291 - multicast all-nodes link-local */ - if (((ip.ip6.uint8[0] == 0xFF) && (ip.ip6.uint8[1] < 3) && (ip.ip6.uint8[15] == 1)) || - ((ip.ip6.uint8[0] == 0xFE) && ((ip.ip6.uint8[1] & 0xC0) == 0x80))) { + if (((ip.ip.v6.uint8[0] == 0xFF) && (ip.ip.v6.uint8[1] < 3) && (ip.ip.v6.uint8[15] == 1)) || + ((ip.ip.v6.uint8[0] == 0xFE) && ((ip.ip.v6.uint8[1] & 0xC0) == 0x80))) { return 0; } /* embedded IPv4-in-IPv6 */ - if (IPV6_IPV4_IN_V6(ip.ip6)) { + if (IPV6_IPV4_IN_V6(ip.ip.v6)) { IP ip4; ip4.family = TOX_AF_INET; - ip4.ip4.uint32 = ip.ip6.uint32[3]; + ip4.ip.v4.uint32 = ip.ip.v6.uint32[3]; return ip_is_lan(ip4); } } diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 73fab888..3baf7401 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -2555,7 +2555,7 @@ void do_messenger(Messenger *m, void *userdata) IP_Port local_ip_port; local_ip_port.port = m->options.tcp_server_port; local_ip_port.ip.family = TOX_AF_INET; - local_ip_port.ip.ip4 = get_ip4_loopback(); + local_ip_port.ip.ip.v4 = get_ip4_loopback(); add_tcp_relay(m->net_crypto, local_ip_port, tcp_server_public_key(m->tcp_server)); } diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index c3d6f63d..e3f55a73 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -230,12 +230,12 @@ static void proxy_socks5_generate_connection_request(TCP_Client_Connection *TCP_ if (TCP_conn->ip_port.ip.family == TOX_AF_INET) { TCP_conn->last_packet[3] = 1; /* IPv4 address */ ++length; - memcpy(TCP_conn->last_packet + length, TCP_conn->ip_port.ip.ip4.uint8, sizeof(IP4)); + memcpy(TCP_conn->last_packet + length, TCP_conn->ip_port.ip.ip.v4.uint8, sizeof(IP4)); length += sizeof(IP4); } else { TCP_conn->last_packet[3] = 4; /* IPv6 address */ ++length; - memcpy(TCP_conn->last_packet + length, TCP_conn->ip_port.ip.ip6.uint8, sizeof(IP6)); + memcpy(TCP_conn->last_packet + length, TCP_conn->ip_port.ip.ip.v6.uint8, sizeof(IP6)); length += sizeof(IP6); } diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index d5772cc1..ae73b804 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c @@ -788,7 +788,7 @@ static int rm_connection_index(TCP_Server *TCP_server, TCP_Secure_Connection *co static int handle_onion_recv_1(void *object, IP_Port dest, const uint8_t *data, uint16_t length) { TCP_Server *TCP_server = (TCP_Server *)object; - uint32_t index = dest.ip.ip6.uint32[0]; + uint32_t index = dest.ip.ip.v6.uint32[0]; if (index >= TCP_server->size_accepted_connections) { return 1; @@ -796,7 +796,7 @@ static int handle_onion_recv_1(void *object, IP_Port dest, const uint8_t *data, TCP_Secure_Connection *con = &TCP_server->accepted_connection_array[index]; - if (con->identifier != dest.ip.ip6.uint64[1]) { + if (con->identifier != dest.ip.ip.v6.uint64[1]) { return 1; } @@ -896,9 +896,9 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, const uint IP_Port source; source.port = 0; // dummy initialise source.ip.family = TCP_ONION_FAMILY; - source.ip.ip6.uint32[0] = con_id; - source.ip.ip6.uint32[1] = 0; - source.ip.ip6.uint64[1] = con->identifier; + source.ip.ip.v6.uint32[0] = con_id; + source.ip.ip.v6.uint32[1] = 0; + source.ip.ip.v6.uint64[1] = con->identifier; onion_send_1(TCP_server->onion, data + 1 + CRYPTO_NONCE_SIZE, length - (1 + CRYPTO_NONCE_SIZE), source, data + 1); } diff --git a/toxcore/ccompat.h b/toxcore/ccompat.h index dfbfe3eb..90db8bc6 100644 --- a/toxcore/ccompat.h +++ b/toxcore/ccompat.h @@ -4,13 +4,6 @@ #ifndef CCOMPAT_H #define CCOMPAT_H -// Marking GNU extensions to avoid warnings. -#if defined(__GNUC__) -#define GNU_EXTENSION __extension__ -#else -#define GNU_EXTENSION -#endif - // Variable length arrays. // VLA(type, name, size) allocates a variable length array with automatic // storage duration. VLA_SIZE(name) evaluates to the runtime size of that array diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 5027ce25..f0d701fe 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -1850,7 +1850,7 @@ static int crypto_connection_add_source(Net_Crypto *c, int crypt_connection_id, } if (source.ip.family == TCP_FAMILY) { - if (add_tcp_number_relay_connection(c->tcp_c, conn->connection_number_tcp, source.ip.ip6.uint32[0]) == 0) { + if (add_tcp_number_relay_connection(c->tcp_c, conn->connection_number_tcp, source.ip.ip.v6.uint32[0]) == 0) { return 1; } } @@ -2134,7 +2134,7 @@ static int tcp_oob_callback(void *object, const uint8_t *public_key, unsigned in IP_Port source; source.port = 0; source.ip.family = TCP_FAMILY; - source.ip.ip6.uint32[0] = tcp_connections_number; + source.ip.ip.v6.uint32[0] = tcp_connections_number; if (handle_new_connection_handshake(c, source, data, length, userdata) != 0) { return -1; diff --git a/toxcore/network.c b/toxcore/network.c index 06a875cc..8a951dbb 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -450,7 +450,7 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1 ip6.uint32[0] = 0; ip6.uint32[1] = 0; ip6.uint32[2] = net_htonl(0xFFFF); - ip6.uint32[3] = ip_port.ip.ip4.uint32; + ip6.uint32[3] = ip_port.ip.ip.v4.uint32; fill_addr6(ip6, &addr6->sin6_addr); addr6->sin6_flowinfo = 0; @@ -460,7 +460,7 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1 addrsize = sizeof(struct sockaddr_in); addr4->sin_family = AF_INET; - fill_addr4(ip_port.ip.ip4, &addr4->sin_addr); + fill_addr4(ip_port.ip.ip.v4, &addr4->sin_addr); addr4->sin_port = ip_port.port; } } else if (ip_port.ip.family == TOX_AF_INET6) { @@ -469,7 +469,7 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1 addrsize = sizeof(struct sockaddr_in6); addr6->sin6_family = AF_INET6; addr6->sin6_port = ip_port.port; - fill_addr6(ip_port.ip.ip6, &addr6->sin6_addr); + fill_addr6(ip_port.ip.ip.v6, &addr6->sin6_addr); addr6->sin6_flowinfo = 0; addr6->sin6_scope_id = 0; @@ -517,17 +517,17 @@ static int receivepacket(Logger *log, Socket sock, IP_Port *ip_port, uint8_t *da struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr; ip_port->ip.family = make_tox_family(addr_in->sin_family); - get_ip4(&ip_port->ip.ip4, &addr_in->sin_addr); + get_ip4(&ip_port->ip.ip.v4, &addr_in->sin_addr); ip_port->port = addr_in->sin_port; } else if (addr.ss_family == AF_INET6) { struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)&addr; ip_port->ip.family = make_tox_family(addr_in6->sin6_family); - get_ip6(&ip_port->ip.ip6, &addr_in6->sin6_addr); + get_ip6(&ip_port->ip.ip.v6, &addr_in6->sin6_addr); ip_port->port = addr_in6->sin6_port; - if (IPV6_IPV4_IN_V6(ip_port->ip.ip6)) { + if (IPV6_IPV4_IN_V6(ip_port->ip.ip.v6)) { ip_port->ip.family = TOX_AF_INET; - ip_port->ip.ip4.uint32 = ip_port->ip.ip6.uint32[3]; + ip_port->ip.ip.v4.uint32 = ip_port->ip.ip.v6.uint32[3]; } } else { return -1; @@ -741,7 +741,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 addrsize = sizeof(struct sockaddr_in); addr4->sin_family = AF_INET; addr4->sin_port = 0; - fill_addr4(ip.ip4, &addr4->sin_addr); + fill_addr4(ip.ip.v4, &addr4->sin_addr); portptr = &addr4->sin_port; } else if (temp->family == TOX_AF_INET6) { @@ -750,7 +750,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 addrsize = sizeof(struct sockaddr_in6); addr6->sin6_family = AF_INET6; addr6->sin6_port = 0; - fill_addr6(ip.ip6, &addr6->sin6_addr); + fill_addr6(ip.ip.v6, &addr6->sin6_addr); addr6->sin6_flowinfo = 0; addr6->sin6_scope_id = 0; @@ -890,14 +890,14 @@ int ip_equal(const IP *a, const IP *b) if (a->family == TOX_AF_INET || a->family == TCP_INET) { struct in_addr addr_a; struct in_addr addr_b; - fill_addr4(a->ip4, &addr_a); - fill_addr4(b->ip4, &addr_b); + fill_addr4(a->ip.v4, &addr_a); + fill_addr4(b->ip.v4, &addr_b); return addr_a.s_addr == addr_b.s_addr; } if (a->family == TOX_AF_INET6 || a->family == TCP_INET6) { - return a->ip6.uint64[0] == b->ip6.uint64[0] && - a->ip6.uint64[1] == b->ip6.uint64[1]; + return a->ip.v6.uint64[0] == b->ip.v6.uint64[0] && + a->ip.v6.uint64[1] == b->ip.v6.uint64[1]; } return 0; @@ -905,16 +905,16 @@ int ip_equal(const IP *a, const IP *b) /* different family: check on the IPv6 one if it is the IPv4 one embedded */ if ((a->family == TOX_AF_INET) && (b->family == TOX_AF_INET6)) { - if (IPV6_IPV4_IN_V6(b->ip6)) { + if (IPV6_IPV4_IN_V6(b->ip.v6)) { struct in_addr addr_a; - fill_addr4(a->ip4, &addr_a); - return addr_a.s_addr == b->ip6.uint32[3]; + fill_addr4(a->ip.v4, &addr_a); + return addr_a.s_addr == b->ip.v6.uint32[3]; } } else if ((a->family == TOX_AF_INET6) && (b->family == TOX_AF_INET)) { - if (IPV6_IPV4_IN_V6(a->ip6)) { + if (IPV6_IPV4_IN_V6(a->ip.v6)) { struct in_addr addr_b; - fill_addr4(b->ip4, &addr_b); - return a->ip6.uint32[3] == addr_b.s_addr; + fill_addr4(b->ip.v4, &addr_b); + return a->ip.v6.uint32[3] == addr_b.s_addr; } } @@ -1027,14 +1027,14 @@ const char *ip_ntoa(const IP *ip, char *ip_str, size_t length) if (ip->family == TOX_AF_INET) { /* returns standard quad-dotted notation */ struct in_addr addr; - fill_addr4(ip->ip4, &addr); + fill_addr4(ip->ip.v4, &addr); ip_str[0] = 0; inet_ntop(family, &addr, ip_str, length); } else if (ip->family == TOX_AF_INET6) { /* returns hex-groups enclosed into square brackets */ struct in6_addr addr; - fill_addr6(ip->ip6, &addr); + fill_addr6(ip->ip.v6, &addr); ip_str[0] = '['; inet_ntop(family, &addr, &ip_str[1], length - 3); @@ -1075,12 +1075,12 @@ int ip_parse_addr(const IP *ip, char *address, size_t length) } if (ip->family == TOX_AF_INET) { - const struct in_addr *addr = (const struct in_addr *)&ip->ip4; + const struct in_addr *addr = (const struct in_addr *)&ip->ip.v4; return inet_ntop(ip->family, addr, address, length) != nullptr; } if (ip->family == TOX_AF_INET6) { - const struct in6_addr *addr = (const struct in6_addr *)&ip->ip6; + const struct in6_addr *addr = (const struct in6_addr *)&ip->ip.v6; return inet_ntop(ip->family, addr, address, length) != nullptr; } @@ -1110,7 +1110,7 @@ int addr_parse_ip(const char *address, IP *to) if (inet_pton(AF_INET, address, &addr4) == 1) { to->family = TOX_AF_INET; - get_ip4(&to->ip4, &addr4); + get_ip4(&to->ip.v4, &addr4); return 1; } @@ -1118,7 +1118,7 @@ int addr_parse_ip(const char *address, IP *to) if (inet_pton(AF_INET6, address, &addr6) == 1) { to->family = TOX_AF_INET6; - get_ip6(&to->ip6, &addr6); + get_ip6(&to->ip.v6, &addr6); return 1; } @@ -1183,12 +1183,12 @@ int addr_resolve(const char *address, IP *to, IP *extra) case AF_INET: if (walker->ai_family == family) { /* AF_INET requested, done */ struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr; - get_ip4(&to->ip4, &addr->sin_addr); + get_ip4(&to->ip.v4, &addr->sin_addr); result = TOX_ADDR_RESOLVE_INET; done = 1; } else if (!(result & TOX_ADDR_RESOLVE_INET)) { /* AF_UNSPEC requested, store away */ struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr; - get_ip4(&ip4.ip4, &addr->sin_addr); + get_ip4(&ip4.ip.v4, &addr->sin_addr); result |= TOX_ADDR_RESOLVE_INET; } @@ -1198,14 +1198,14 @@ int addr_resolve(const char *address, IP *to, IP *extra) if (walker->ai_family == family) { /* AF_INET6 requested, done */ if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) { struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr; - get_ip6(&to->ip6, &addr->sin6_addr); + get_ip6(&to->ip.v6, &addr->sin6_addr); result = TOX_ADDR_RESOLVE_INET6; done = 1; } } else if (!(result & TOX_ADDR_RESOLVE_INET6)) { /* AF_UNSPEC requested, store away */ if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) { struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr; - get_ip6(&ip6.ip6, &addr->sin6_addr); + get_ip6(&ip6.ip.v6, &addr->sin6_addr); result |= TOX_ADDR_RESOLVE_INET6; } } @@ -1268,14 +1268,14 @@ int net_connect(Socket sock, IP_Port ip_port) addrsize = sizeof(struct sockaddr_in); addr4->sin_family = AF_INET; - fill_addr4(ip_port.ip.ip4, &addr4->sin_addr); + fill_addr4(ip_port.ip.ip.v4, &addr4->sin_addr); addr4->sin_port = ip_port.port; } else if (ip_port.ip.family == TOX_AF_INET6) { struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; addrsize = sizeof(struct sockaddr_in6); addr6->sin6_family = AF_INET6; - fill_addr6(ip_port.ip.ip6, &addr6->sin6_addr); + fill_addr6(ip_port.ip.ip.v6, &addr6->sin6_addr); addr6->sin6_port = ip_port.port; } else { return 0; @@ -1335,10 +1335,10 @@ int32_t net_getipport(const char *node, IP_Port **res, int tox_type) if (cur->ai_family == AF_INET) { struct sockaddr_in *addr = (struct sockaddr_in *)cur->ai_addr; - memcpy(&ip_port->ip.ip4, &addr->sin_addr, sizeof(IP4)); + memcpy(&ip_port->ip.ip.v4, &addr->sin_addr, sizeof(IP4)); } else if (cur->ai_family == AF_INET6) { struct sockaddr_in6 *addr = (struct sockaddr_in6 *)cur->ai_addr; - memcpy(&ip_port->ip.ip6, &addr->sin6_addr, sizeof(IP6)); + memcpy(&ip_port->ip.ip.v6, &addr->sin6_addr, sizeof(IP6)); } else { continue; } diff --git a/toxcore/network.h b/toxcore/network.h index 5bb6ab48..8b5af47c 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -151,10 +151,10 @@ extern const IP6 IP6_BROADCAST; #define IP_DEFINED typedef struct IP { uint8_t family; - GNU_EXTENSION union { - IP4 ip4; - IP6 ip6; - }; + union { + IP4 v4; + IP6 v6; + } ip; } IP; #define IP_PORT_DEFINED diff --git a/toxcore/onion.c b/toxcore/onion.c index ffe99c2d..48cc4d76 100644 --- a/toxcore/onion.c +++ b/toxcore/onion.c @@ -55,9 +55,9 @@ static void ip_pack(uint8_t *data, IP source) if (source.family == TOX_AF_INET || source.family == TOX_TCP_INET) { memset(data + 1, 0, SIZE_IP6); - memcpy(data + 1, source.ip4.uint8, SIZE_IP4); + memcpy(data + 1, source.ip.v4.uint8, SIZE_IP4); } else { - memcpy(data + 1, source.ip6.uint8, SIZE_IP6); + memcpy(data + 1, source.ip.v6.uint8, SIZE_IP6); } } @@ -71,9 +71,9 @@ static int ip_unpack(IP *target, const uint8_t *data, unsigned int data_size, bo target->family = data[0]; if (target->family == TOX_AF_INET || target->family == TOX_TCP_INET) { - memcpy(target->ip4.uint8, data + 1, SIZE_IP4); + memcpy(target->ip.v4.uint8, data + 1, SIZE_IP4); } else { - memcpy(target->ip6.uint8, data + 1, SIZE_IP6); + memcpy(target->ip.v6.uint8, data + 1, SIZE_IP6); } bool valid = disable_family_check || diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 4c5258e3..bc16e17d 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -280,7 +280,7 @@ static uint16_t random_nodes_path_onion(const Onion_Client *onion_c, Node_format if (num_nodes >= 2) { nodes[0].ip_port.ip.family = TCP_FAMILY; - nodes[0].ip_port.ip.ip4.uint32 = random_tcp; + nodes[0].ip_port.ip.ip.v4.uint32 = random_tcp; for (i = 1; i < max_num; ++i) { nodes[i] = onion_c->path_nodes[rand() % num_nodes]; @@ -294,7 +294,7 @@ static uint16_t random_nodes_path_onion(const Onion_Client *onion_c, Node_format } nodes[0].ip_port.ip.family = TCP_FAMILY; - nodes[0].ip_port.ip.ip4.uint32 = random_tcp; + nodes[0].ip_port.ip.ip.v4.uint32 = random_tcp; for (i = 1; i < max_num; ++i) { nodes[i] = onion_c->path_nodes_bs[rand() % num_nodes_bs]; @@ -485,7 +485,7 @@ static int send_onion_packet_tcp_udp(const Onion_Client *onion_c, const Onion_Pa return -1; } - return send_tcp_onion_request(onion_c->c, path->ip_port1.ip.ip4.uint32, packet, len); + return send_tcp_onion_request(onion_c->c, path->ip_port1.ip.ip.v4.uint32, packet, len); } return -1;