From 21675ce0d2581597b0e0a727ab4cf6cfb796a037 Mon Sep 17 00:00:00 2001 From: iphydf Date: Tue, 27 Feb 2018 01:07:46 +0000 Subject: Finish @Diadlo's network Family abstraction. The Family stuff in toxcore is a big mess. I'm sure I saw a bunch of bugs on the way, but I'm not verifying that code now, so the bugs stay. --- auto_tests/TCP_test.c | 11 ++- auto_tests/dht_test.c | 18 ++-- auto_tests/network_test.c | 21 +++-- auto_tests/onion_test.c | 6 +- toxcore/DHT.c | 89 ++++++++++--------- toxcore/DHT.h | 2 +- toxcore/LAN_discovery.c | 44 +++++----- toxcore/Messenger.c | 10 +-- toxcore/TCP_client.c | 8 +- toxcore/TCP_connection.c | 20 +++-- toxcore/TCP_server.c | 12 +-- toxcore/friend_connection.c | 10 +-- toxcore/net_crypto.c | 26 +++--- toxcore/network.c | 204 +++++++++++++++++++++++++++++++------------- toxcore/network.h | 32 +++++-- toxcore/onion.c | 17 ++-- toxcore/onion_announce.c | 2 +- toxcore/onion_client.c | 22 ++--- toxcore/ping.c | 2 +- toxcore/tox.c | 2 +- 20 files changed, 329 insertions(+), 229 deletions(-) diff --git a/auto_tests/TCP_test.c b/auto_tests/TCP_test.c index 41125116..8b61c4b1 100644 --- a/auto_tests/TCP_test.c +++ b/auto_tests/TCP_test.c @@ -29,18 +29,17 @@ #endif #if !USE_IPV6 -#undef TOX_AF_INET6 -#define TOX_AF_INET6 TOX_AF_INET +#define net_family_ipv6 net_family_ipv4 #endif static inline IP get_loopback() { IP ip; #if USE_IPV6 - ip.family = TOX_AF_INET6; + ip.family = net_family_ipv6; ip.ip.v6 = get_ip6_loopback(); #else - ip.family = TOX_AF_INET; + ip.family = net_family_ipv4; ip.ip.v4 = get_ip4_loopback(); #endif return ip; @@ -57,7 +56,7 @@ START_TEST(test_basic) ck_assert_msg(tcp_s != nullptr, "Failed to create TCP relay server"); ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports"); - Socket sock = net_socket(TOX_AF_INET6, TOX_SOCK_STREAM, TOX_PROTO_TCP); + Socket sock = net_socket(net_family_ipv6, TOX_SOCK_STREAM, TOX_PROTO_TCP); IP_Port ip_port_loopback; ip_port_loopback.ip = get_loopback(); ip_port_loopback.port = net_htons(ports[random_u32() % NUM_PORTS]); @@ -157,7 +156,7 @@ struct sec_TCP_con { static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s) { struct sec_TCP_con *sec_c = (struct sec_TCP_con *)malloc(sizeof(struct sec_TCP_con)); - Socket sock = net_socket(TOX_AF_INET6, TOX_SOCK_STREAM, TOX_PROTO_TCP); + Socket sock = net_socket(net_family_ipv6, TOX_SOCK_STREAM, TOX_PROTO_TCP); IP_Port ip_port_loopback; ip_port_loopback.ip = get_loopback(); diff --git a/auto_tests/dht_test.c b/auto_tests/dht_test.c index 803dad76..17f4563b 100644 --- a/auto_tests/dht_test.c +++ b/auto_tests/dht_test.c @@ -31,10 +31,10 @@ static inline IP get_loopback() { IP ip; #if USE_IPV6 - ip.family = TOX_AF_INET6; + ip.family = net_family_ipv6; ip.ip.v6 = get_ip6_loopback(); #else - ip.family = TOX_AF_INET; + ip.family = net_family_ipv4; ip.ip.v4 = get_ip4_loopback(); #endif return ip; @@ -113,7 +113,7 @@ static void test_addto_lists_update(DHT *dht, int used, test, test1, test2, found; IP_Port test_ipp; uint8_t test_id[CRYPTO_PUBLIC_KEY_SIZE]; - uint8_t ipv6 = ip_port->ip.family == TOX_AF_INET6 ? 1 : 0; + uint8_t ipv6 = net_family_is_ipv6(ip_port->ip.family) ? 1 : 0; // check id update for existing ip_port test = random_u32() % length; @@ -188,7 +188,7 @@ static void test_addto_lists_bad(DHT *dht, int used, test1, test2, test3; uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE], test_id1[CRYPTO_PUBLIC_KEY_SIZE], test_id2[CRYPTO_PUBLIC_KEY_SIZE], test_id3[CRYPTO_PUBLIC_KEY_SIZE]; - uint8_t ipv6 = ip_port->ip.family == TOX_AF_INET6 ? 1 : 0; + uint8_t ipv6 = net_family_is_ipv6(ip_port->ip.family) ? 1 : 0; random_bytes(public_key, sizeof(public_key)); mark_all_good(list, length, ipv6); @@ -232,7 +232,7 @@ static void test_addto_lists_possible_bad(DHT *dht, int used, test1, test2, test3; uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE], test_id1[CRYPTO_PUBLIC_KEY_SIZE], test_id2[CRYPTO_PUBLIC_KEY_SIZE], test_id3[CRYPTO_PUBLIC_KEY_SIZE]; - uint8_t ipv6 = ip_port->ip.family == TOX_AF_INET6 ? 1 : 0; + uint8_t ipv6 = net_family_is_ipv6(ip_port->ip.family) ? 1 : 0; random_bytes(public_key, sizeof(public_key)); mark_all_good(list, length, ipv6); @@ -294,7 +294,7 @@ static void test_addto_lists_good(DHT *dht, const uint8_t *comp_client_id) { uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; - uint8_t ipv6 = ip_port->ip.family == TOX_AF_INET6 ? 1 : 0; + uint8_t ipv6 = net_family_is_ipv6(ip_port->ip.family) ? 1 : 0; mark_all_good(list, length, ipv6); @@ -547,7 +547,7 @@ static void test_list_main(void) ck_assert_msg(count == 1, "Nodes in search don't know ip of friend. %u %u %u", i, j, count); Node_format ln[MAX_SENT_NODES]; - int n = get_close_nodes(dhts[(l + j) % NUM_DHT], dhts[l]->self_public_key, ln, 0, 1, 0); + int n = get_close_nodes(dhts[(l + j) % NUM_DHT], dhts[l]->self_public_key, ln, net_family_unspec, 1, 0); ck_assert_msg(n == MAX_SENT_NODES, "bad num close %u | %u %u", n, i, j); count = 0; @@ -744,8 +744,8 @@ static void random_ip(IP_Port *ipp, int family) uint8_t *port = (uint8_t *)&ipp->port; random_bytes(port, sizeof(ipp->port)); random_bytes(ip, size); - ipp->ip.family = family; - + // TODO(iphydf): Pass the net_family variant to random_ip. + ipp->ip.family.value = family; } #define PACKED_NODES_SIZE (SIZE_IPPORT + CRYPTO_PUBLIC_KEY_SIZE) diff --git a/auto_tests/network_test.c b/auto_tests/network_test.c index 6f535ee1..0abd14a4 100644 --- a/auto_tests/network_test.c +++ b/auto_tests/network_test.c @@ -44,7 +44,7 @@ START_TEST(test_addr_resolv_localhost) net_kill_strerror(strerror); char ip_str[IP_NTOA_LEN]; - ck_assert_msg(ip.family == TOX_AF_INET, "Expected family TOX_AF_INET, got %u.", ip.family); + ck_assert_msg(net_family_is_ipv4(ip.family), "Expected family TOX_AF_INET, got %u.", ip.family.value); const uint32_t loopback = get_ip4_loopback().uint32; ck_assert_msg(ip.ip.v4.uint32 == loopback, "Expected 127.0.0.1, got %s.", ip_ntoa(&ip, ip_str, sizeof(ip_str))); @@ -62,7 +62,8 @@ START_TEST(test_addr_resolv_localhost) ck_assert_msg(res > 0, "Resolver failed: %d, %s", error, strerror); net_kill_strerror(strerror); - ck_assert_msg(ip.family == TOX_AF_INET6, "Expected family TOX_AF_INET6 (%u), got %u.", TOX_AF_INET6, ip.family); + ck_assert_msg(net_family_is_ipv6(ip.family), "Expected family TOX_AF_INET6 (%u), got %u.", TOX_AF_INET6, + ip.family.value); IP6 ip6_loopback = get_ip6_loopback(); ck_assert_msg(!memcmp(&ip.ip.v6, &ip6_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip, ip_str, sizeof(ip_str))); @@ -73,7 +74,7 @@ START_TEST(test_addr_resolv_localhost) } ip_init(&ip, 1); // ipv6enabled = 1 - ip.family = TOX_AF_UNSPEC; + ip.family = net_family_unspec; IP extra; ip_reset(&extra); res = addr_resolve(localhost, &ip, &extra); @@ -83,15 +84,17 @@ START_TEST(test_addr_resolv_localhost) net_kill_strerror(strerror); #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(net_family_is_ipv6(ip.family), "Expected family TOX_AF_INET6 (%u), got %u.", TOX_AF_INET6, + ip.family.value); 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(net_family_is_ipv4(extra.family), "Expected family TOX_AF_INET (%d), got %u.", TOX_AF_INET, + extra.family.value); 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(net_family_is_ipv4(ip.family), "Expected family TOX_AF_INET (%d), got %u.", TOX_AF_INET, ip.family.value); ck_assert_msg(ip.ip.v4.uint32 == loopback, "Expected 127.0.0.1, got %s.", ip_ntoa(&ip, ip_str, sizeof(ip_str))); #endif @@ -114,14 +117,14 @@ START_TEST(test_ip_equal) res = ip_equal(nullptr, &ip1); ck_assert_msg(res == 0, "ip_equal(NULL, PTR): expected result 0, got %u.", res); - ip1.family = TOX_AF_INET; + ip1.family = net_family_ipv4; 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.family = net_family_ipv4; ip2.ip.v4.uint32 = net_htonl(0x7F000001); res = ip_equal(&ip1, &ip2); @@ -134,7 +137,7 @@ START_TEST(test_ip_equal) 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.family = net_family_ipv6; ip2.ip.v6.uint32[0] = 0; ip2.ip.v6.uint32[1] = 0; ip2.ip.v6.uint32[2] = net_htonl(0xFFFF); diff --git a/auto_tests/onion_test.c b/auto_tests/onion_test.c index 501114a5..c60d3e1a 100644 --- a/auto_tests/onion_test.c +++ b/auto_tests/onion_test.c @@ -29,10 +29,10 @@ static inline IP get_loopback() { IP ip; #if USE_IPV6 - ip.family = TOX_AF_INET6; + ip.family = net_family_ipv6; ip.ip.v6 = get_ip6_loopback(); #else - ip.family = TOX_AF_INET; + ip.family = net_family_ipv4; ip.ip.v4 = get_ip4_loopback(); #endif return ip; @@ -375,7 +375,7 @@ static Onions *new_onions(uint16_t port, uint32_t *index) return nullptr; } - TCP_Proxy_Info inf = {{{0}}}; + TCP_Proxy_Info inf = {{{{0}}}}; on->onion_c = new_onion_client(new_net_crypto(on->log, dht, &inf)); if (!on->onion_c) { diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 31d2ca0a..6134f59b 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -385,20 +385,17 @@ int handle_request(const uint8_t *self_public_key, const uint8_t *self_secret_ke /* Return packet size of packed node with ip_family on success. * Return -1 on failure. */ -int packed_node_size(uint8_t ip_family) +int packed_node_size(Family ip_family) { - switch (ip_family) { - case TOX_AF_INET: - case TCP_INET: - return PACKED_NODE_SIZE_IP4; - - case TOX_AF_INET6: - case TCP_INET6: - return PACKED_NODE_SIZE_IP6; + if (net_family_is_ipv4(ip_family) || net_family_is_tcp_ipv4(ip_family)) { + return PACKED_NODE_SIZE_IP4; + } - default: - return -1; + if (net_family_is_ipv6(ip_family) || net_family_is_tcp_ipv6(ip_family)) { + return PACKED_NODE_SIZE_IP6; } + + return -1; } @@ -416,17 +413,17 @@ int pack_ip_port(uint8_t *data, uint16_t length, const IP_Port *ip_port) bool is_ipv4; uint8_t net_family; - if (ip_port->ip.family == TOX_AF_INET) { + if (net_family_is_ipv4(ip_port->ip.family)) { // TODO(irungentoo): use functions to convert endianness is_ipv4 = true; net_family = TOX_AF_INET; - } else if (ip_port->ip.family == TCP_INET) { + } else if (net_family_is_tcp_ipv4(ip_port->ip.family)) { is_ipv4 = true; net_family = TOX_TCP_INET; - } else if (ip_port->ip.family == TOX_AF_INET6) { + } else if (net_family_is_ipv6(ip_port->ip.family)) { is_ipv4 = false; net_family = TOX_AF_INET6; - } else if (ip_port->ip.family == TCP_INET6) { + } else if (net_family_is_tcp_ipv6(ip_port->ip.family)) { is_ipv4 = false; net_family = TOX_TCP_INET6; } else { @@ -492,28 +489,28 @@ int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, uint8 } bool is_ipv4; - uint8_t host_family; + Family host_family; if (data[0] == TOX_AF_INET) { is_ipv4 = true; - host_family = TOX_AF_INET; + host_family = net_family_ipv4; } else if (data[0] == TOX_TCP_INET) { if (!tcp_enabled) { return -1; } is_ipv4 = true; - host_family = TCP_INET; + host_family = net_family_tcp_ipv4; } else if (data[0] == TOX_AF_INET6) { is_ipv4 = false; - host_family = TOX_AF_INET6; + host_family = net_family_ipv6; } else if (data[0] == TOX_TCP_INET6) { if (!tcp_enabled) { return -1; } is_ipv4 = false; - host_family = TCP_INET6; + host_family = net_family_tcp_ipv6; } else { return -1; } @@ -650,8 +647,8 @@ static uint32_t index_of_node_pk(const Node_format *array, uint32_t size, const static uint32_t index_of_client_ip_port(const Client_data *array, uint32_t size, const IP_Port *ip_port) { for (uint32_t i = 0; i < size; ++i) { - if ((ip_port->ip.family == TOX_AF_INET && ipport_equal(&array[i].assoc4.ip_port, ip_port)) || - (ip_port->ip.family == TOX_AF_INET6 && ipport_equal(&array[i].assoc6.ip_port, ip_port))) { + if ((net_family_is_ipv4(ip_port->ip.family) && ipport_equal(&array[i].assoc4.ip_port, ip_port)) || + (net_family_is_ipv6(ip_port->ip.family) && ipport_equal(&array[i].assoc6.ip_port, ip_port))) { return i; } } @@ -666,10 +663,10 @@ static void update_client(Logger *log, int index, Client_data *client, IP_Port i IPPTsPng *assoc; int ip_version; - if (ip_port.ip.family == TOX_AF_INET) { + if (net_family_is_ipv4(ip_port.ip.family)) { assoc = &client->assoc4; ip_version = 4; - } else if (ip_port.ip.family == TOX_AF_INET6) { + } else if (net_family_is_ipv6(ip_port.ip.family)) { assoc = &client->assoc6; ip_version = 6; } else { @@ -727,7 +724,7 @@ static int client_or_ip_port_in_list(Logger *log, Client_data *list, uint16_t le IPPTsPng *assoc; int ip_version; - if (ip_port.ip.family == TOX_AF_INET) { + if (net_family_is_ipv4(ip_port.ip.family)) { assoc = &list[index].assoc4; ip_version = 4; } else { @@ -791,7 +788,7 @@ static void get_close_nodes_inner(const uint8_t *public_key, Node_format *nodes_ Family sa_family, const Client_data *client_list, uint32_t client_list_length, uint32_t *num_nodes_ptr, uint8_t is_LAN, uint8_t want_good) { - if ((sa_family != TOX_AF_INET) && (sa_family != TOX_AF_INET6) && (sa_family != 0)) { + if (!net_family_is_ipv4(sa_family) && !net_family_is_ipv6(sa_family) && !net_family_is_unspec(sa_family)) { return; } @@ -807,9 +804,9 @@ static void get_close_nodes_inner(const uint8_t *public_key, Node_format *nodes_ const IPPTsPng *ipptp = nullptr; - if (sa_family == TOX_AF_INET) { + if (net_family_is_ipv4(sa_family)) { ipptp = &client->assoc4; - } else if (sa_family == TOX_AF_INET6) { + } else if (net_family_is_ipv6(sa_family)) { ipptp = &client->assoc6; } else if (client->assoc4.timestamp >= client->assoc6.timestamp) { ipptp = &client->assoc4; @@ -978,7 +975,7 @@ static void update_client_with_reset(Client_data *client, const IP_Port *ip_port IPPTsPng *ipptp_write = nullptr; IPPTsPng *ipptp_clear = nullptr; - if (ip_port->ip.family == TOX_AF_INET) { + if (net_family_is_ipv4(ip_port->ip.family)) { ipptp_write = &client->assoc4; ipptp_clear = &client->assoc6; } else { @@ -1016,7 +1013,7 @@ static bool replace_all(Client_data *list, IP_Port ip_port, const uint8_t *comp_public_key) { - if ((ip_port.ip.family != TOX_AF_INET) && (ip_port.ip.family != TOX_AF_INET6)) { + if (!net_family_is_ipv4(ip_port.ip.family) && !net_family_is_ipv6(ip_port.ip.family)) { return false; } @@ -1087,7 +1084,7 @@ static bool is_pk_in_client_list(const Client_data *list, unsigned int client_li return 0; } - const IPPTsPng *assoc = ip_port.ip.family == TOX_AF_INET + const IPPTsPng *assoc = net_family_is_ipv4(ip_port.ip.family) ? &list[index].assoc4 : &list[index].assoc6; @@ -1180,8 +1177,8 @@ 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.ip.v6)) { - ip_port.ip.family = TOX_AF_INET; + if (net_family_is_ipv6(ip_port.ip.family) && IPV6_IPV4_IN_V6(ip_port.ip.ip.v6)) { + ip_port.ip.family = net_family_ipv4; ip_port.ip.ip.v4.uint32 = ip_port.ip.ip.v6.uint32[3]; } @@ -1241,9 +1238,9 @@ static bool update_client_data(Client_data *array, size_t size, IP_Port ip_port, Client_data *const data = &array[index]; IPPTsPng *assoc; - if (ip_port.ip.family == TOX_AF_INET) { + if (net_family_is_ipv4(ip_port.ip.family)) { assoc = &data->assoc4; - } else if (ip_port.ip.family == TOX_AF_INET6) { + } else if (net_family_is_ipv6(ip_port.ip.family)) { assoc = &data->assoc6; } else { return true; @@ -1260,8 +1257,8 @@ 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.ip.v6)) { - ip_port.ip.family = TOX_AF_INET; + if (net_family_is_ipv6(ip_port.ip.family) && IPV6_IPV4_IN_V6(ip_port.ip.ip.v6)) { + ip_port.ip.family = net_family_ipv4; ip_port.ip.ip.v4.uint32 = ip_port.ip.ip.v6.uint32[3]; } @@ -1346,7 +1343,8 @@ static int sendnodes_ipv6(const DHT *dht, IP_Port ip_port, const uint8_t *public const size_t node_format_size = sizeof(Node_format); Node_format nodes_list[MAX_SENT_NODES]; - const uint32_t num_nodes = get_close_nodes(dht, client_id, nodes_list, 0, ip_is_lan(ip_port.ip) == 0, 1); + const uint32_t num_nodes = get_close_nodes(dht, client_id, nodes_list, net_family_unspec, ip_is_lan(ip_port.ip) == 0, + 1); VLA(uint8_t, plain, 1 + node_format_size * MAX_SENT_NODES + length); @@ -1591,7 +1589,8 @@ int DHT_addfriend(DHT *dht, const uint8_t *public_key, void (*ip_callback)(void *lock_count = lock_num + 1; } - dht_friend->num_to_bootstrap = get_close_nodes(dht, dht_friend->public_key, dht_friend->to_bootstrap, 0, 1, 0); + dht_friend->num_to_bootstrap = get_close_nodes(dht, dht_friend->public_key, dht_friend->to_bootstrap, net_family_unspec, + 1, 0); return 0; } @@ -1823,7 +1822,7 @@ int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enable if (ipv6enabled) { /* setup for getting BOTH: an IPv6 AND an IPv4 address */ - ip_port_v64.ip.family = TOX_AF_UNSPEC; + ip_port_v64.ip.family = net_family_unspec; ip_reset(&ip_port_v4.ip); ip_extra = &ip_port_v4.ip; } @@ -2350,11 +2349,11 @@ static IPPTsPng *get_closelist_IPPTsPng(DHT *dht, const uint8_t *public_key, Fam continue; } - if (sa_family == TOX_AF_INET) { + if (net_family_is_ipv4(sa_family)) { return &dht->close_clientlist[i].assoc4; } - if (sa_family == TOX_AF_INET6) { + if (net_family_is_ipv6(sa_family)) { return &dht->close_clientlist[i].assoc6; } } @@ -2579,10 +2578,10 @@ static void do_hardening(DHT *dht) if (i % 2 == 0) { cur_iptspng = &dht->close_clientlist[i / 2].assoc4; - sa_family = TOX_AF_INET; + sa_family = net_family_ipv4; } else { cur_iptspng = &dht->close_clientlist[i / 2].assoc6; - sa_family = TOX_AF_INET6; + sa_family = net_family_ipv6; } if (is_timeout(cur_iptspng->timestamp, BAD_NODE_TIMEOUT)) { @@ -2794,7 +2793,7 @@ uint32_t DHT_size(const DHT *dht) const uint32_t size32 = sizeof(uint32_t); const uint32_t sizesubhead = size32 * 2; - return size32 + sizesubhead + (packed_node_size(TOX_AF_INET) * numv4) + (packed_node_size(TOX_AF_INET6) * numv6); + return size32 + sizesubhead + packed_node_size(net_family_ipv4) * numv4 + packed_node_size(net_family_ipv6) * numv6; } static uint8_t *DHT_save_subheader(uint8_t *data, uint32_t len, uint16_t type) diff --git a/toxcore/DHT.h b/toxcore/DHT.h index 3c87804f..3ad28d8c 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h @@ -160,7 +160,7 @@ const Client_data *dht_friend_client(const DHT_Friend *dht_friend, size_t index) /* Return packet size of packed node with ip_family on success. * Return -1 on failure. */ -int packed_node_size(uint8_t ip_family); +int packed_node_size(Family ip_family); /* Packs an IP_Port structure into data of max size length. * diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c index 0bb96926..d25aace3 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c @@ -86,9 +86,9 @@ static void fetch_broadcast_info(uint16_t port) if (addr_parse_ip(pAdapter->IpAddressList.IpMask.String, &subnet_mask) && addr_parse_ip(pAdapter->GatewayList.IpAddress.String, &gateway)) { - if (gateway.family == TOX_AF_INET && subnet_mask.family == TOX_AF_INET) { + if (net_family_is_ipv4(gateway.family) && net_family_is_ipv4(subnet_mask.family)) { IP_Port *ip_port = &ip_ports[count]; - ip_port->ip.family = TOX_AF_INET; + ip_port->ip.family = net_family_ipv4; 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.ip.v4.uint32 = net_htonl(broadcast_ip); @@ -139,7 +139,7 @@ static void fetch_broadcast_info(uint16_t port) * Definitely won't work like this on Windows... */ broadcast_count = 0; - const Socket sock = net_socket(TOX_AF_INET, TOX_SOCK_STREAM, 0); + const Socket sock = net_socket(net_family_ipv4, TOX_SOCK_STREAM, 0); if (!sock_valid(sock)) { return; @@ -190,7 +190,7 @@ 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.family = net_family_ipv4; ip_port->ip.ip.v4.uint32 = sock4->sin_addr.s_addr; if (ip_port->ip.ip.v4.uint32 == 0) { @@ -248,24 +248,22 @@ static IP broadcast_ip(Family family_socket, Family family_broadcast) IP ip; ip_reset(&ip); - if (family_socket == TOX_AF_INET6) { - if (family_broadcast == TOX_AF_INET6) { - ip.family = TOX_AF_INET6; + if (net_family_is_ipv6(family_socket)) { + if (net_family_is_ipv6(family_broadcast)) { + ip.family = net_family_ipv6; /* 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.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; + } else if (net_family_is_ipv4(family_broadcast)) { + ip.family = net_family_ipv6; ip.ip.v6 = IP6_BROADCAST; } - } else if (family_socket == TOX_AF_INET) { - if (family_broadcast == TOX_AF_INET) { - ip.family = TOX_AF_INET; - ip.ip.v4 = IP4_BROADCAST; - } + } else if (net_family_is_ipv4(family_socket) && net_family_is_ipv4(family_broadcast)) { + ip.family = net_family_ipv4; + ip.ip.v4 = IP4_BROADCAST; } return ip; @@ -274,7 +272,7 @@ static IP broadcast_ip(Family family_socket, Family family_broadcast) /* Is IP a local ip or not. */ bool ip_is_local(IP ip) { - if (ip.family == TOX_AF_INET) { + if (net_family_is_ipv4(ip.family)) { IP4 ip4 = ip.ip.v4; /* Loopback. */ @@ -285,7 +283,7 @@ bool ip_is_local(IP ip) /* embedded IPv4-in-IPv6 */ if (IPV6_IPV4_IN_V6(ip.ip.v6)) { IP ip4; - ip4.family = TOX_AF_INET; + ip4.family = net_family_ipv4; ip4.ip.v4.uint32 = ip.ip.v6.uint32[3]; return ip_is_local(ip4); } @@ -308,7 +306,7 @@ int ip_is_lan(IP ip) return 0; } - if (ip.family == TOX_AF_INET) { + if (net_family_is_ipv4(ip.family)) { IP4 ip4 = ip.ip.v4; /* 10.0.0.0 to 10.255.255.255 range. */ @@ -337,7 +335,7 @@ int ip_is_lan(IP ip) if ((ip4.uint8[0] == 100) && ((ip4.uint8[1] & 0xC0) == 0x40)) { return 0; } - } else if (ip.family == TOX_AF_INET6) { + } else if (net_family_is_ipv6(ip.family)) { /* autogenerated for each interface: FE80::* (up to FEBF::*) FF02::1 is - according to RFC 4291 - multicast all-nodes link-local */ @@ -349,7 +347,7 @@ int ip_is_lan(IP ip) /* embedded IPv4-in-IPv6 */ if (IPV6_IPV4_IN_V6(ip.ip.v6)) { IP ip4; - ip4.family = TOX_AF_INET; + ip4.family = net_family_ipv4; ip4.ip.v4.uint32 = ip.ip.v6.uint32[3]; return ip_is_lan(ip4); } @@ -391,8 +389,8 @@ int lan_discovery_send(uint16_t port, DHT *dht) ip_port.port = port; /* IPv6 multicast */ - if (net_family(dht_get_net(dht)) == TOX_AF_INET6) { - ip_port.ip = broadcast_ip(TOX_AF_INET6, TOX_AF_INET6); + if (net_family_is_ipv6(net_family(dht_get_net(dht)))) { + ip_port.ip = broadcast_ip(net_family_ipv6, net_family_ipv6); if (ip_isset(&ip_port.ip)) { if (sendpacket(dht_get_net(dht), ip_port, data, 1 + CRYPTO_PUBLIC_KEY_SIZE) > 0) { @@ -401,8 +399,8 @@ int lan_discovery_send(uint16_t port, DHT *dht) } } - /* IPv4 broadcast (has to be IPv4-in-IPv6 mapping if socket is TOX_AF_INET6 */ - ip_port.ip = broadcast_ip(net_family(dht_get_net(dht)), TOX_AF_INET); + /* IPv4 broadcast (has to be IPv4-in-IPv6 mapping if socket is IPv6 */ + ip_port.ip = broadcast_ip(net_family(dht_get_net(dht)), net_family_ipv4); if (ip_isset(&ip_port.ip)) { if (sendpacket(dht_get_net(dht), ip_port, data, 1 + CRYPTO_PUBLIC_KEY_SIZE)) { diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index c381db0c..11109815 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -2599,7 +2599,7 @@ void do_messenger(Messenger *m, void *userdata) /* Add self tcp server. */ 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.family = net_family_ipv4; 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)); @@ -2970,8 +2970,8 @@ uint32_t messenger_size(const Messenger *m) + sizesubhead + m->name_length // Own nickname. + sizesubhead + m->statusmessage_length // status message + sizesubhead + 1 // status - + sizesubhead + NUM_SAVED_TCP_RELAYS * packed_node_size(TCP_INET6) //TCP relays - + sizesubhead + NUM_SAVED_PATH_NODES * packed_node_size(TCP_INET6) //saved path nodes + + sizesubhead + NUM_SAVED_TCP_RELAYS * packed_node_size(net_family_tcp_ipv6) //TCP relays + + sizesubhead + NUM_SAVED_PATH_NODES * packed_node_size(net_family_tcp_ipv6) //saved path nodes + sizesubhead; } @@ -3041,7 +3041,7 @@ void messenger_save(const Messenger *m, uint8_t *data) uint8_t *temp_data = data; data = messenger_save_subheader(temp_data, 0, type); unsigned int num = copy_connected_tcp_relays(m->net_crypto, relays, NUM_SAVED_TCP_RELAYS); - int l = pack_nodes(data, NUM_SAVED_TCP_RELAYS * packed_node_size(TCP_INET6), relays, num); + int l = pack_nodes(data, NUM_SAVED_TCP_RELAYS * packed_node_size(net_family_tcp_ipv6), relays, num); if (l > 0) { len = l; @@ -3055,7 +3055,7 @@ void messenger_save(const Messenger *m, uint8_t *data) data = messenger_save_subheader(data, 0, type); memset(nodes, 0, sizeof(nodes)); num = onion_backup_nodes(m->onion_c, nodes, NUM_SAVED_PATH_NODES); - l = pack_nodes(data, NUM_SAVED_PATH_NODES * packed_node_size(TCP_INET6), nodes, num); + l = pack_nodes(data, NUM_SAVED_PATH_NODES * packed_node_size(net_family_tcp_ipv6), nodes, num); if (l > 0) { len = l; diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index 58e8de49..d1cd4d66 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -227,7 +227,7 @@ static void proxy_socks5_generate_connection_request(TCP_Client_Connection *TCP_ TCP_conn->last_packet[2] = 0; /* reserved, must be 0 */ uint16_t length = 3; - if (TCP_conn->ip_port.ip.family == TOX_AF_INET) { + if (net_family_is_ipv4(TCP_conn->ip_port.ip.family)) { TCP_conn->last_packet[3] = 1; /* IPv4 address */ ++length; memcpy(TCP_conn->last_packet + length, TCP_conn->ip_port.ip.ip.v4.uint8, sizeof(IP4)); @@ -252,7 +252,7 @@ static void proxy_socks5_generate_connection_request(TCP_Client_Connection *TCP_ */ static int proxy_socks5_read_connection_response(TCP_Client_Connection *TCP_conn) { - if (TCP_conn->ip_port.ip.family == TOX_AF_INET) { + if (net_family_is_ipv4(TCP_conn->ip_port.ip.family)) { uint8_t data[4 + sizeof(IP4) + sizeof(uint16_t)]; int ret = read_TCP_packet(TCP_conn->sock, data, sizeof(data)); @@ -696,7 +696,7 @@ TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, const uint8_t *public return nullptr; } - if (ip_port.ip.family != TOX_AF_INET && ip_port.ip.family != TOX_AF_INET6) { + if (!net_family_is_ipv4(ip_port.ip.family) && !net_family_is_ipv6(ip_port.ip.family)) { return nullptr; } @@ -707,7 +707,7 @@ TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, const uint8_t *public proxy_info = &default_proxyinfo; } - uint8_t family = ip_port.ip.family; + Family family = ip_port.ip.family; if (proxy_info->proxy_type != TCP_PROXY_NONE) { family = proxy_info->ip_port.ip.family; diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c index 72642264..06cb6e34 100644 --- a/toxcore/TCP_connection.c +++ b/toxcore/TCP_connection.c @@ -1125,13 +1125,13 @@ static int tcp_relay_on_online(TCP_Connections *tcp_c, int tcp_connections_numbe static int add_tcp_relay_instance(TCP_Connections *tcp_c, IP_Port ip_port, const uint8_t *relay_pk) { - if (ip_port.ip.family == TCP_INET) { - ip_port.ip.family = TOX_AF_INET; - } else if (ip_port.ip.family == TCP_INET6) { - ip_port.ip.family = TOX_AF_INET6; + if (net_family_is_tcp_ipv4(ip_port.ip.family)) { + ip_port.ip.family = net_family_ipv4; + } else if (net_family_is_tcp_ipv6(ip_port.ip.family)) { + ip_port.ip.family = net_family_ipv6; } - if (ip_port.ip.family != TOX_AF_INET && ip_port.ip.family != TOX_AF_INET6) { + if (!net_family_is_ipv4(ip_port.ip.family) && !net_family_is_ipv6(ip_port.ip.family)) { return -1; } @@ -1286,10 +1286,12 @@ unsigned int tcp_copy_connected_relays(TCP_Connections *tcp_c, Node_format *tcp_ memcpy(tcp_relays[copied].public_key, tcp_con_public_key(tcp_con->connection), CRYPTO_PUBLIC_KEY_SIZE); tcp_relays[copied].ip_port = tcp_con_ip_port(tcp_con->connection); - if (tcp_relays[copied].ip_port.ip.family == TOX_AF_INET) { - tcp_relays[copied].ip_port.ip.family = TCP_INET; - } else if (tcp_relays[copied].ip_port.ip.family == TOX_AF_INET6) { - tcp_relays[copied].ip_port.ip.family = TCP_INET6; + Family *const family = &tcp_relays[copied].ip_port.ip.family; + + if (net_family_is_ipv4(*family)) { + *family = net_family_tcp_ipv4; + } else if (net_family_is_ipv6(*family)) { + *family = net_family_tcp_ipv6; } ++copied; diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index 28a441ad..65dc81c3 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c @@ -889,7 +889,7 @@ 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.family = net_family_tcp_onion; source.ip.ip.v6.uint32[0] = con_id; source.ip.ip.v6.uint32[1] = 0; source.ip.ip.v6.uint64[1] = con->identifier; @@ -1007,7 +1007,7 @@ static Socket new_listening_TCP_socket(Family family, uint16_t port) int ok = set_socket_nonblock(sock); - if (ok && family == TOX_AF_INET6) { + if (ok && net_family_is_ipv6(family)) { ok = set_socket_dualstack(sock); } @@ -1060,13 +1060,7 @@ TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uin #endif - uint8_t family; - - if (ipv6_enabled) { - family = TOX_AF_INET6; - } else { - family = TOX_AF_INET; - } + const Family family = ipv6_enabled ? net_family_ipv6 : net_family_ipv4; uint32_t i; #ifdef TCP_SERVER_USE_EPOLL diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c index 0ba8178f..fd5e3078 100644 --- a/toxcore/friend_connection.c +++ b/toxcore/friend_connection.c @@ -221,7 +221,7 @@ int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_ /* Local ip and same pk means that they are hosting a TCP relay. */ if (ip_is_local(ip_port.ip) && public_key_cmp(friend_con->dht_temp_pk, public_key) == 0) { - if (friend_con->dht_ip_port.ip.family != 0) { + if (!net_family_is_unspec(friend_con->dht_ip_port.ip.family)) { ip_port.ip = friend_con->dht_ip_port.ip; } else { friend_con->hosting_tcp_relay = 0; @@ -231,7 +231,7 @@ int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_ const uint16_t index = friend_con->tcp_relay_counter % FRIEND_MAX_STORED_TCP_RELAYS; for (unsigned i = 0; i < FRIEND_MAX_STORED_TCP_RELAYS; ++i) { - if (friend_con->tcp_relays[i].ip_port.ip.family != 0 + if (!net_family_is_unspec(friend_con->tcp_relays[i].ip_port.ip.family) && public_key_cmp(friend_con->tcp_relays[i].public_key, public_key) == 0) { memset(&friend_con->tcp_relays[i], 0, sizeof(Node_format)); } @@ -256,7 +256,7 @@ static void connect_to_saved_tcp_relays(Friend_Connections *fr_c, int friendcon_ for (unsigned i = 0; (i < FRIEND_MAX_STORED_TCP_RELAYS) && (number != 0); ++i) { const uint16_t index = (friend_con->tcp_relay_counter - (i + 1)) % FRIEND_MAX_STORED_TCP_RELAYS; - if (friend_con->tcp_relays[index].ip_port.ip.family) { + if (!net_family_is_unspec(friend_con->tcp_relays[index].ip_port.ip.family)) { if (add_tcp_relay_peer(fr_c->net_crypto, friend_con->crypt_connection_id, friend_con->tcp_relays[index].ip_port, friend_con->tcp_relays[index].public_key) == 0) { --number; @@ -550,7 +550,7 @@ static int handle_new_connections(void *object, New_Connection *n_c) connection_lossy_data_handler(fr_c->net_crypto, id, &handle_lossy_packet, fr_c, friendcon_id); friend_con->crypt_connection_id = id; - if (n_c->source.ip.family != TOX_AF_INET && n_c->source.ip.family != TOX_AF_INET6) { + if (!net_family_is_ipv4(n_c->source.ip.family) && !net_family_is_ipv6(n_c->source.ip.family)) { set_direct_ip_port(fr_c->net_crypto, friend_con->crypt_connection_id, friend_con->dht_ip_port, 0); } else { friend_con->dht_ip_port = n_c->source; @@ -916,7 +916,7 @@ void do_friend_connections(Friend_Connections *fr_c, void *userdata) } if (friend_con->dht_ip_port_lastrecv + FRIEND_DHT_TIMEOUT < temp_time) { - friend_con->dht_ip_port.ip.family = 0; + friend_con->dht_ip_port.ip.family = net_family_unspec; } if (friend_con->dht_lock) { diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index b9d8a360..5477fea7 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -573,7 +573,7 @@ static int add_ip_port_connection(Net_Crypto *c, int crypt_connection_id, IP_Por return -1; } - if (ip_port.ip.family == TOX_AF_INET) { + if (net_family_is_ipv4(ip_port.ip.family)) { if (!ipport_equal(&ip_port, &conn->ip_portv4) && ip_is_lan(conn->ip_portv4.ip) != 0) { if (!bs_list_add(&c->ip_port_list, (uint8_t *)&ip_port, crypt_connection_id)) { return -1; @@ -583,7 +583,7 @@ static int add_ip_port_connection(Net_Crypto *c, int crypt_connection_id, IP_Por conn->ip_portv4 = ip_port; return 0; } - } else if (ip_port.ip.family == TOX_AF_INET6) { + } else if (net_family_is_ipv6(ip_port.ip.family)) { if (!ipport_equal(&ip_port, &conn->ip_portv6)) { if (!bs_list_add(&c->ip_port_list, (uint8_t *)&ip_port, crypt_connection_id)) { return -1; @@ -605,7 +605,7 @@ static int add_ip_port_connection(Net_Crypto *c, int crypt_connection_id, IP_Por */ static IP_Port return_ip_port_connection(Net_Crypto *c, int crypt_connection_id) { - const IP_Port empty = {{0}}; + const IP_Port empty = {{{0}}}; Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); @@ -628,11 +628,11 @@ static IP_Port return_ip_port_connection(Net_Crypto *c, int crypt_connection_id) return conn->ip_portv4; } - if (v6 && conn->ip_portv6.ip.family == TOX_AF_INET6) { + if (v6 && net_family_is_ipv6(conn->ip_portv6.ip.family)) { return conn->ip_portv6; } - if (conn->ip_portv4.ip.family == TOX_AF_INET) { + if (net_family_is_ipv4(conn->ip_portv4.ip.family)) { return conn->ip_portv4; } @@ -659,7 +659,7 @@ static int send_packet_to(Net_Crypto *c, int crypt_connection_id, const uint8_t IP_Port ip_port = return_ip_port_connection(c, crypt_connection_id); // TODO(irungentoo): on bad networks, direct connections might not last indefinitely. - if (ip_port.ip.family != 0) { + if (!net_family_is_unspec(ip_port.ip.family)) { bool direct_connected = 0; crypto_connection_status(c, crypt_connection_id, &direct_connected, nullptr); @@ -1835,12 +1835,12 @@ static int crypto_connection_add_source(Net_Crypto *c, int crypt_connection_id, return -1; } - if (source.ip.family == TOX_AF_INET || source.ip.family == TOX_AF_INET6) { + if (net_family_is_ipv4(source.ip.family) || net_family_is_ipv6(source.ip.family)) { if (add_ip_port_connection(c, crypt_connection_id, source) != 0) { return -1; } - if (source.ip.family == TOX_AF_INET) { + if (net_family_is_ipv4(source.ip.family)) { conn->direct_lastrecv_timev4 = unix_time(); } else { conn->direct_lastrecv_timev6 = unix_time(); @@ -1849,7 +1849,7 @@ static int crypto_connection_add_source(Net_Crypto *c, int crypt_connection_id, return 0; } - if (source.ip.family == TCP_FAMILY) { + if (net_family_is_tcp_family(source.ip.family)) { if (add_tcp_number_relay_connection(c->tcp_c, conn->connection_number_tcp, source.ip.ip.v6.uint32[0]) == 0) { return 1; } @@ -2065,13 +2065,13 @@ int set_direct_ip_port(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port, if (add_ip_port_connection(c, crypt_connection_id, ip_port) == 0) { if (connected) { - if (ip_port.ip.family == TOX_AF_INET) { + if (net_family_is_ipv4(ip_port.ip.family)) { conn->direct_lastrecv_timev4 = unix_time(); } else { conn->direct_lastrecv_timev6 = unix_time(); } } else { - if (ip_port.ip.family == TOX_AF_INET) { + if (net_family_is_ipv4(ip_port.ip.family)) { conn->direct_lastrecv_timev4 = 0; } else { conn->direct_lastrecv_timev6 = 0; @@ -2133,7 +2133,7 @@ static int tcp_oob_callback(void *object, const uint8_t *public_key, unsigned in if (data[0] == NET_PACKET_CRYPTO_HS) { IP_Port source; source.port = 0; - source.ip.family = TCP_FAMILY; + source.ip.family = net_family_tcp_family; source.ip.ip.v6.uint32[0] = tcp_connections_number; if (handle_new_connection_handshake(c, source, data, length, userdata) != 0) { @@ -2411,7 +2411,7 @@ static int udp_handle_packet(void *object, IP_Port source, const uint8_t *packet pthread_mutex_lock(&conn->mutex); - if (source.ip.family == TOX_AF_INET) { + if (net_family_is_ipv4(source.ip.family)) { conn->direct_lastrecv_timev4 = unix_time(); } else { conn->direct_lastrecv_timev6 = unix_time(); diff --git a/toxcore/network.c b/toxcore/network.c index f357d887..e7e754c5 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -196,7 +196,7 @@ static int make_socktype(int type); static int make_family(Family tox_family) { - switch (tox_family) { + switch (tox_family.value) { case TOX_AF_INET: return AF_INET; @@ -207,24 +207,24 @@ static int make_family(Family tox_family) return AF_UNSPEC; default: - return tox_family; + return tox_family.value; } } -static Family make_tox_family(int family) +static const Family *make_tox_family(int family) { switch (family) { case AF_INET: - return TOX_AF_INET; + return &net_family_ipv4; case AF_INET6: - return TOX_AF_INET6; + return &net_family_ipv6; case AF_UNSPEC: - return TOX_AF_UNSPEC; + return &net_family_unspec; default: - return family; + return nullptr; } } @@ -281,6 +281,61 @@ const Socket net_invalid_socket = { #endif }; +const Family net_family_unspec = {TOX_AF_UNSPEC}; +const Family net_family_ipv4 = {TOX_AF_INET}; +const Family net_family_ipv6 = {TOX_AF_INET6}; +const Family net_family_tcp_family = {TCP_FAMILY}; +const Family net_family_tcp_onion = {TCP_ONION_FAMILY}; +const Family net_family_tcp_ipv4 = {TCP_INET}; +const Family net_family_tcp_ipv6 = {TCP_INET6}; +const Family net_family_tox_tcp_ipv4 = {TOX_TCP_INET}; +const Family net_family_tox_tcp_ipv6 = {TOX_TCP_INET6}; + +bool net_family_is_unspec(Family family) +{ + return family.value == net_family_unspec.value; +} + +bool net_family_is_ipv4(Family family) +{ + return family.value == net_family_ipv4.value; +} + +bool net_family_is_ipv6(Family family) +{ + return family.value == net_family_ipv6.value; +} + +bool net_family_is_tcp_family(Family family) +{ + return family.value == net_family_tcp_family.value; +} + +bool net_family_is_tcp_onion(Family family) +{ + return family.value == net_family_tcp_onion.value; +} + +bool net_family_is_tcp_ipv4(Family family) +{ + return family.value == net_family_tcp_ipv4.value; +} + +bool net_family_is_tcp_ipv6(Family family) +{ + return family.value == net_family_tcp_ipv6.value; +} + +bool net_family_is_tox_tcp_ipv4(Family family) +{ + return family.value == net_family_tox_tcp_ipv4.value; +} + +bool net_family_is_tox_tcp_ipv6(Family family) +{ + return family.value == net_family_tox_tcp_ipv6.value; +} + /* Check if socket is valid. * * return 1 if valid @@ -496,20 +551,19 @@ uint16_t net_port(const Networking_Core *net) */ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint16_t length) { - if (net->family == TOX_AF_UNSPEC) { /* Socket not initialized */ + if (net_family_is_unspec(net->family)) { /* Socket not initialized */ + LOGGER_ERROR(net->log, "attempted to send message of length %u on uninitialised socket", (unsigned)length); return -1; } /* socket TOX_AF_INET, but target IP NOT: can't send */ - if ((net->family == TOX_AF_INET) && (ip_port.ip.family != TOX_AF_INET)) { + if (net_family_is_ipv4(net->family) && !net_family_is_ipv4(ip_port.ip.family)) { + LOGGER_ERROR(net->log, "attempted to send message with network family %d (probably IPv6) on IPv4 socket", + ip_port.ip.family.value); return -1; } - struct sockaddr_storage addr; - - size_t addrsize = 0; - - if (ip_port.ip.family == TOX_AF_INET && net->family == TOX_AF_INET6) { + if (net_family_is_ipv4(ip_port.ip.family) && net_family_is_ipv6(net->family)) { /* must convert to IPV4-in-IPV6 address */ IP6 ip6; @@ -520,29 +574,33 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1 ip6.uint32[2] = net_htonl(0xFFFF); ip6.uint32[3] = ip_port.ip.ip.v4.uint32; - ip_port.ip.family = TOX_AF_INET6; + ip_port.ip.family = net_family_ipv6; ip_port.ip.ip.v6 = ip6; } - if (ip_port.ip.family == TOX_AF_INET) { - struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; + struct sockaddr_storage addr; + + size_t addrsize; + + if (net_family_is_ipv4(ip_port.ip.family)) { + struct sockaddr_in *const addr4 = (struct sockaddr_in *)&addr; addrsize = sizeof(struct sockaddr_in); - fill_addr4(ip_port.ip.ip.v4, &addr4->sin_addr); addr4->sin_family = AF_INET; addr4->sin_port = ip_port.port; - } else if (ip_port.ip.family == TOX_AF_INET6) { - struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; + fill_addr4(ip_port.ip.ip.v4, &addr4->sin_addr); + } else if (net_family_is_ipv6(ip_port.ip.family)) { + struct sockaddr_in6 *const addr6 = (struct sockaddr_in6 *)&addr; addrsize = sizeof(struct sockaddr_in6); - fill_addr6(ip_port.ip.ip.v6, &addr6->sin6_addr); addr6->sin6_family = AF_INET6; addr6->sin6_port = ip_port.port; + fill_addr6(ip_port.ip.ip.v6, &addr6->sin6_addr); addr6->sin6_flowinfo = 0; addr6->sin6_scope_id = 0; } else { - LOGGER_WARNING(net->log, "unknown address type: %d", ip_port.ip.family); + LOGGER_WARNING(net->log, "unknown address type: %d", ip_port.ip.family.value); return -1; } @@ -587,17 +645,31 @@ static int receivepacket(Logger *log, Socket sock, IP_Port *ip_port, uint8_t *da if (addr.ss_family == AF_INET) { struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr; - ip_port->ip.family = make_tox_family(addr_in->sin_family); + const Family *const family = make_tox_family(addr_in->sin_family); + assert(family != nullptr); + + if (family == nullptr) { + return -1; + } + + ip_port->ip.family = *family; 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); + const Family *const family = make_tox_family(addr_in6->sin6_family); + assert(family != nullptr); + + if (family == nullptr) { + return -1; + } + + ip_port->ip.family = *family; 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.ip.v6)) { - ip_port->ip.family = TOX_AF_INET; + ip_port->ip.family = net_family_ipv4; ip_port->ip.ip.v4.uint32 = ip_port->ip.ip.v6.uint32[3]; } } else { @@ -617,7 +689,8 @@ void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handl void networking_poll(Networking_Core *net, void *userdata) { - if (net->family == 0) { /* Socket not initialized */ + if (net_family_is_unspec(net->family)) { + /* Socket not initialized */ return; } @@ -732,8 +805,8 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 } /* maybe check for invalid IPs like 224+.x.y.z? if there is any IP set ever */ - if (ip.family != TOX_AF_INET && ip.family != TOX_AF_INET6) { - LOGGER_ERROR(log, "Invalid address family: %u", ip.family); + if (!net_family_is_ipv4(ip.family) && !net_family_is_ipv6(ip.family)) { + LOGGER_ERROR(log, "invalid address family: %u\n", ip.family.value); return nullptr; } @@ -809,7 +882,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 memset(&addr, 0, sizeof(struct sockaddr_storage)); - if (temp->family == TOX_AF_INET) { + if (net_family_is_ipv4(temp->family)) { struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; addrsize = sizeof(struct sockaddr_in); @@ -818,7 +891,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 fill_addr4(ip.ip.v4, &addr4->sin_addr); portptr = &addr4->sin_port; - } else if (temp->family == TOX_AF_INET6) { + } else if (net_family_is_ipv6(temp->family)) { struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; addrsize = sizeof(struct sockaddr_in6); @@ -835,8 +908,8 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 return nullptr; } - if (ip.family == TOX_AF_INET6) { - int is_dualstack = set_socket_dualstack(temp->sock); + if (net_family_is_ipv6(ip.family)) { + const int is_dualstack = set_socket_dualstack(temp->sock); LOGGER_DEBUG(log, "Dual-stack socket: %s", is_dualstack ? "enabled" : "Failed to enable, won't be able to receive from/send to IPv4 addresses"); /* multicast local nodes */ @@ -944,7 +1017,8 @@ void kill_networking(Networking_Core *net) return; } - if (net->family != 0) { /* Socket not initialized */ + if (!net_family_is_unspec(net->family)) { + /* Socket is initialized, so we close it. */ kill_sock(net->sock); } @@ -965,8 +1039,8 @@ int ip_equal(const IP *a, const IP *b) } /* same family */ - if (a->family == b->family) { - if (a->family == TOX_AF_INET || a->family == TCP_INET) { + if (a->family.value == b->family.value) { + if (net_family_is_ipv4(a->family) || net_family_is_tcp_ipv4(a->family)) { struct in_addr addr_a; struct in_addr addr_b; fill_addr4(a->ip.v4, &addr_a); @@ -974,7 +1048,7 @@ int ip_equal(const IP *a, const IP *b) return addr_a.s_addr == addr_b.s_addr; } - if (a->family == TOX_AF_INET6 || a->family == TCP_INET6) { + if (net_family_is_ipv6(a->family) || net_family_is_tcp_ipv6(a->family)) { return a->ip.v6.uint64[0] == b->ip.v6.uint64[0] && a->ip.v6.uint64[1] == b->ip.v6.uint64[1]; } @@ -983,13 +1057,13 @@ 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 (net_family_is_ipv4(a->family) && net_family_is_ipv6(b->family)) { if (IPV6_IPV4_IN_V6(b->ip.v6)) { struct in_addr addr_a; 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)) { + } else if (net_family_is_ipv6(a->family) && net_family_is_ipv4(b->family)) { if (IPV6_IPV4_IN_V6(a->ip.v6)) { struct in_addr addr_b; fill_addr4(b->ip.v4, &addr_b); @@ -1037,28 +1111,28 @@ void ip_init(IP *ip, bool ipv6enabled) } memset(ip, 0, sizeof(IP)); - ip->family = ipv6enabled ? TOX_AF_INET6 : TOX_AF_INET; + ip->family = ipv6enabled ? net_family_ipv6 : net_family_ipv4; } /* checks if ip is valid */ -int ip_isset(const IP *ip) +bool ip_isset(const IP *ip) { if (!ip) { - return 0; + return false; } - return (ip->family != 0); + return !net_family_is_unspec(ip->family); } /* checks if ip is valid */ -int ipport_isset(const IP_Port *ipport) +bool ipport_isset(const IP_Port *ipport) { if (!ipport) { - return 0; + return true; } if (!ipport->port) { - return 0; + return true; } return ip_isset(&ipport->ip); @@ -1103,14 +1177,14 @@ const char *ip_ntoa(const IP *ip, char *ip_str, size_t length) if (ip) { const int family = make_family(ip->family); - if (ip->family == TOX_AF_INET) { + if (net_family_is_ipv4(ip->family)) { /* returns standard quad-dotted notation */ struct in_addr 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) { + } else if (net_family_is_ipv6(ip->family)) { /* returns hex-groups enclosed into square brackets */ struct in6_addr addr; fill_addr6(ip->ip.v6, &addr); @@ -1121,7 +1195,7 @@ const char *ip_ntoa(const IP *ip, char *ip_str, size_t length) ip_str[len] = ']'; ip_str[len + 1] = 0; } else { - snprintf(ip_str, length, "(IP invalid, family %u)", ip->family); + snprintf(ip_str, length, "(IP invalid, family %u)", ip->family.value); } } else { snprintf(ip_str, length, "(IP invalid: NULL)"); @@ -1153,14 +1227,14 @@ int ip_parse_addr(const IP *ip, char *address, size_t length) return 0; } - if (ip->family == TOX_AF_INET) { + if (net_family_is_ipv4(ip->family)) { const struct in_addr *addr = (const struct in_addr *)&ip->ip.v4; - return inet_ntop(ip->family, addr, address, length) != nullptr; + return inet_ntop(make_family(ip->family), addr, address, length) != nullptr; } - if (ip->family == TOX_AF_INET6) { + if (net_family_is_ipv6(ip->family)) { const struct in6_addr *addr = (const struct in6_addr *)&ip->ip.v6; - return inet_ntop(ip->family, addr, address, length) != nullptr; + return inet_ntop(make_family(ip->family), addr, address, length) != nullptr; } return 0; @@ -1188,7 +1262,7 @@ int addr_parse_ip(const char *address, IP *to) struct in_addr addr4; if (inet_pton(AF_INET, address, &addr4) == 1) { - to->family = TOX_AF_INET; + to->family = net_family_ipv4; get_ip4(&to->ip.v4, &addr4); return 1; } @@ -1196,7 +1270,7 @@ int addr_parse_ip(const char *address, IP *to) struct in6_addr addr6; if (inet_pton(AF_INET6, address, &addr6) == 1) { - to->family = TOX_AF_INET6; + to->family = net_family_ipv6; get_ip6(&to->ip.v6, &addr6); return 1; } @@ -1228,7 +1302,7 @@ int addr_resolve(const char *address, IP *to, IP *extra) } Family tox_family = to->family; - Family family = make_family(tox_family); + int family = make_family(tox_family); struct addrinfo *server = nullptr; struct addrinfo *walker = nullptr; @@ -1342,14 +1416,14 @@ int net_connect(Socket sock, IP_Port ip_port) struct sockaddr_storage addr = {0}; size_t addrsize; - if (ip_port.ip.family == TOX_AF_INET) { + if (net_family_is_ipv4(ip_port.ip.family)) { struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; addrsize = sizeof(struct sockaddr_in); addr4->sin_family = AF_INET; 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) { + } else if (net_family_is_ipv6(ip_port.ip.family)) { struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; addrsize = sizeof(struct sockaddr_in6); @@ -1422,7 +1496,15 @@ int32_t net_getipport(const char *node, IP_Port **res, int tox_type) continue; } - ip_port->ip.family = make_tox_family(cur->ai_family); + const Family *const family = make_tox_family(cur->ai_family); + assert(family != nullptr); + + if (family == nullptr) { + freeaddrinfo(infos); + return -1; + } + + ip_port->ip.family = *family; ip_port++; } @@ -1440,18 +1522,18 @@ void net_freeipport(IP_Port *ip_ports) /* return 1 on success * return 0 on failure */ -int bind_to_port(Socket sock, int family, uint16_t port) +int bind_to_port(Socket sock, Family family, uint16_t port) { struct sockaddr_storage addr = {0}; size_t addrsize; - if (family == TOX_AF_INET) { + if (net_family_is_ipv4(family)) { struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; addrsize = sizeof(struct sockaddr_in); addr4->sin_family = AF_INET; addr4->sin_port = net_htons(port); - } else if (family == TOX_AF_INET6) { + } else if (net_family_is_ipv6(family)) { struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; addrsize = sizeof(struct sockaddr_in6); diff --git a/toxcore/network.h b/toxcore/network.h index bdfebd7e..8071375b 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -34,7 +34,29 @@ extern "C" { #endif -typedef uint8_t Family; +typedef struct Family { + uint8_t value; +} Family; + +bool net_family_is_unspec(Family family); +bool net_family_is_ipv4(Family family); +bool net_family_is_ipv6(Family family); +bool net_family_is_tcp_family(Family family); +bool net_family_is_tcp_onion(Family family); +bool net_family_is_tcp_ipv4(Family family); +bool net_family_is_tcp_ipv6(Family family); +bool net_family_is_tox_tcp_ipv4(Family family); +bool net_family_is_tox_tcp_ipv6(Family family); + +extern const Family net_family_unspec; +extern const Family net_family_ipv4; +extern const Family net_family_ipv6; +extern const Family net_family_tcp_family; +extern const Family net_family_tcp_onion; +extern const Family net_family_tcp_ipv4; +extern const Family net_family_tcp_ipv6; +extern const Family net_family_tox_tcp_ipv4; +extern const Family net_family_tox_tcp_ipv6; typedef struct Socket { int socket; @@ -152,7 +174,7 @@ extern const IP6 IP6_BROADCAST; #define IP_DEFINED typedef struct IP { - uint8_t family; + Family family; union { IP4 v4; IP6 v6; @@ -264,9 +286,9 @@ void ip_reset(IP *ip); /* nulls out ip, sets family according to flag */ void ip_init(IP *ip, bool ipv6enabled); /* checks if ip is valid */ -int ip_isset(const IP *ip); +bool ip_isset(const IP *ip); /* checks if ip is valid */ -int ipport_isset(const IP_Port *ipport); +bool ipport_isset(const IP_Port *ipport); /* copies an ip structure */ void ip_copy(IP *target, const IP *source); /* copies an ip_port structure */ @@ -396,7 +418,7 @@ void net_freeipport(IP_Port *ip_ports); /* return 1 on success * return 0 on failure */ -int bind_to_port(Socket sock, int family, uint16_t port); +int bind_to_port(Socket sock, Family family, uint16_t port); /* Get the last networking error code. * diff --git a/toxcore/onion.c b/toxcore/onion.c index 476f76e9..5bbc7f75 100644 --- a/toxcore/onion.c +++ b/toxcore/onion.c @@ -54,9 +54,9 @@ static void change_symmetric_key(Onion *onion) /* packing and unpacking functions */ static void ip_pack(uint8_t *data, IP source) { - data[0] = source.family; + data[0] = source.family.value; - if (source.family == TOX_AF_INET || source.family == TOX_TCP_INET) { + if (net_family_is_ipv4(source.family) || net_family_is_tox_tcp_ipv4(source.family)) { memset(data + 1, 0, SIZE_IP6); memcpy(data + 1, source.ip.v4.uint8, SIZE_IP4); } else { @@ -71,17 +71,18 @@ static int ip_unpack(IP *target, const uint8_t *data, unsigned int data_size, bo return -1; } - target->family = data[0]; + // TODO(iphydf): Validate input. + target->family.value = data[0]; - if (target->family == TOX_AF_INET || target->family == TOX_TCP_INET) { + if (net_family_is_ipv4(target->family) || net_family_is_tox_tcp_ipv4(target->family)) { memcpy(target->ip.v4.uint8, data + 1, SIZE_IP4); } else { memcpy(target->ip.v6.uint8, data + 1, SIZE_IP6); } bool valid = disable_family_check || - target->family == TOX_AF_INET || - target->family == TOX_AF_INET6; + net_family_is_ipv4(target->family) || + net_family_is_ipv6(target->family); return valid ? 0 : -1; } @@ -642,8 +643,8 @@ static int handle_recv_1(void *object, IP_Port source, const uint8_t *packet, ui uint16_t data_len = length - (1 + RETURN_1); if (onion->recv_1_function && - send_to.ip.family != TOX_AF_INET && - send_to.ip.family != TOX_AF_INET6) { + !net_family_is_ipv4(send_to.ip.family) && + !net_family_is_ipv6(send_to.ip.family)) { return onion->recv_1_function(onion->callback_object, send_to, packet + (1 + RETURN_1), data_len); } diff --git a/toxcore/onion_announce.c b/toxcore/onion_announce.c index 5aec3492..6c9e8659 100644 --- a/toxcore/onion_announce.c +++ b/toxcore/onion_announce.c @@ -405,7 +405,7 @@ static int handle_announce_request(void *object, IP_Port source, const uint8_t * /*Respond with a announce response packet*/ Node_format nodes_list[MAX_SENT_NODES]; - unsigned int num_nodes = get_close_nodes(onion_a->dht, plain + ONION_PING_ID_SIZE, nodes_list, 0, + unsigned int num_nodes = get_close_nodes(onion_a->dht, plain + ONION_PING_ID_SIZE, nodes_list, net_family_unspec, ip_is_lan(source.ip) == 0, 1); uint8_t nonce[CRYPTO_NONCE_SIZE]; random_nonce(nonce); diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 3ee3e0ca..fa6c08c9 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -163,7 +163,7 @@ Net_Crypto *onion_get_net_crypto(const Onion_Client *onion_c) */ int onion_add_bs_path_node(Onion_Client *onion_c, IP_Port ip_port, const uint8_t *public_key) { - if (ip_port.ip.family != TOX_AF_INET && ip_port.ip.family != TOX_AF_INET6) { + if (!net_family_is_ipv4(ip_port.ip.family) && !net_family_is_ipv6(ip_port.ip.family)) { return -1; } @@ -196,7 +196,7 @@ int onion_add_bs_path_node(Onion_Client *onion_c, IP_Port ip_port, const uint8_t */ static int onion_add_path_node(Onion_Client *onion_c, IP_Port ip_port, const uint8_t *public_key) { - if (ip_port.ip.family != TOX_AF_INET && ip_port.ip.family != TOX_AF_INET6) { + if (!net_family_is_ipv4(ip_port.ip.family) && !net_family_is_ipv6(ip_port.ip.family)) { return -1; } @@ -282,7 +282,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.family = net_family_tcp_family; nodes[0].ip_port.ip.ip.v4.uint32 = random_tcp; for (i = 1; i < max_num; ++i) { @@ -296,7 +296,7 @@ static uint16_t random_nodes_path_onion(const Onion_Client *onion_c, Node_format return 0; } - nodes[0].ip_port.ip.family = TCP_FAMILY; + nodes[0].ip_port.ip.family = net_family_tcp_family; nodes[0].ip_port.ip.ip.v4.uint32 = random_tcp; for (i = 1; i < max_num; ++i) { @@ -465,7 +465,7 @@ static uint32_t set_path_timeouts(Onion_Client *onion_c, uint32_t num, uint32_t static int send_onion_packet_tcp_udp(const Onion_Client *onion_c, const Onion_Path *path, IP_Port dest, const uint8_t *data, uint16_t length) { - if (path->ip_port1.ip.family == TOX_AF_INET || path->ip_port1.ip.family == TOX_AF_INET6) { + if (net_family_is_ipv4(path->ip_port1.ip.family) || net_family_is_ipv6(path->ip_port1.ip.family)) { uint8_t packet[ONION_MAX_PACKET_SIZE]; int len = create_onion_packet(packet, sizeof(packet), path, dest, data, length); @@ -480,7 +480,7 @@ static int send_onion_packet_tcp_udp(const Onion_Client *onion_c, const Onion_Pa return 0; } - if (path->ip_port1.ip.family == TCP_FAMILY) { + if (net_family_is_tcp_family(path->ip_port1.ip.family)) { uint8_t packet[ONION_MAX_PACKET_SIZE]; int len = create_onion_packet_tcp(packet, sizeof(packet), path, dest, data, length); @@ -987,11 +987,11 @@ static int handle_dhtpk_announce(void *object, const uint8_t *source_pubkey, con int i; for (i = 0; i < num_nodes; ++i) { - uint8_t family = nodes[i].ip_port.ip.family; + const Family family = nodes[i].ip_port.ip.family; - if (family == TOX_AF_INET || family == TOX_AF_INET6) { + if (net_family_is_ipv4(family) || net_family_is_ipv6(family)) { DHT_getnodes(onion_c->dht, &nodes[i].ip_port, nodes[i].public_key, onion_c->friends_list[friend_num].dht_public_key); - } else if (family == TCP_INET || family == TCP_INET6) { + } else if (net_family_is_tcp_ipv4(family) || net_family_is_tcp_ipv6(family)) { if (onion_c->friends_list[friend_num].tcp_relay_node_callback) { void *obj = onion_c->friends_list[friend_num].tcp_relay_node_callback_object; uint32_t number = onion_c->friends_list[friend_num].tcp_relay_node_callback_number; @@ -1010,8 +1010,8 @@ static int handle_tcp_onion(void *object, const uint8_t *data, uint16_t length, return 1; } - IP_Port ip_port = {{0}}; - ip_port.ip.family = TCP_FAMILY; + IP_Port ip_port = {{{0}}}; + ip_port.ip.family = net_family_tcp_family; if (data[0] == NET_PACKET_ANNOUNCE_RESPONSE) { return handle_announce_response(object, ip_port, data, length, userdata); diff --git a/toxcore/ping.c b/toxcore/ping.c index cc77f2b1..3daeff86 100644 --- a/toxcore/ping.c +++ b/toxcore/ping.c @@ -250,7 +250,7 @@ static int in_list(const Client_data *list, uint16_t length, const uint8_t *publ if (id_equal(list[i].public_key, public_key)) { const IPPTsPng *ipptp; - if (ip_port.ip.family == TOX_AF_INET) { + if (net_family_is_ipv4(ip_port.ip.family)) { ipptp = &list[i].assoc4; } else { ipptp = &list[i].assoc6; diff --git a/toxcore/tox.c b/toxcore/tox.c index 6b50c5e8..2f210209 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -155,7 +155,7 @@ Tox *tox_new(const struct Tox_Options *options, TOX_ERR_NEW *error) ip_init(&m_options.proxy_info.ip_port.ip, m_options.ipv6enabled); if (m_options.ipv6enabled) { - m_options.proxy_info.ip_port.ip.family = TOX_AF_UNSPEC; + m_options.proxy_info.ip_port.ip.family = net_family_unspec; } if (addr_resolve_or_parse_ip(tox_options_get_proxy_host(options), &m_options.proxy_info.ip_port.ip, nullptr) == 0) { -- cgit v1.2.3