From 66b8a7685e8fdecd6104f01f840f5d792ce1e041 Mon Sep 17 00:00:00 2001 From: Diadlo Date: Thu, 2 Mar 2017 23:15:51 +0300 Subject: [PATCH] AF_INET -> TOX_AF_INET --- auto_tests/TCP_test.c | 12 ++-- auto_tests/dht_test.c | 32 +++++----- auto_tests/network_test.c | 32 +++++----- testing/dns3_test.c | 2 +- toxcore/DHT.c | 64 +++++++++---------- toxcore/DHT.h | 6 -- toxcore/LAN_discovery.c | 44 ++++++------- toxcore/Messenger.c | 2 +- toxcore/TCP_client.c | 13 ++-- toxcore/TCP_connection.c | 10 +-- toxcore/TCP_server.c | 6 +- toxcore/friend_connection.c | 2 +- toxcore/net_crypto.c | 18 +++--- toxcore/network.c | 121 ++++++++++++++++++++++-------------- toxcore/network.h | 29 ++++----- toxcore/onion.c | 13 ++-- toxcore/onion.h | 4 +- toxcore/onion_client.c | 8 +-- toxcore/ping.c | 2 +- toxcore/tox.c | 4 +- 20 files changed, 226 insertions(+), 198 deletions(-) diff --git a/auto_tests/TCP_test.c b/auto_tests/TCP_test.c index f8412832..fbbb08cd 100644 --- a/auto_tests/TCP_test.c +++ b/auto_tests/TCP_test.c @@ -34,7 +34,7 @@ START_TEST(test_basic) Socket sock = net_socket(TOX_AF_INET6, TOX_SOCK_STREAM, TOX_PROTO_TCP); IP_Port ip_port_loopback; - ip_port_loopback.ip.family = AF_INET6; + ip_port_loopback.ip.family = TOX_AF_INET6; ip_port_loopback.ip.ip6.uint64[0] = 0; ip_port_loopback.ip.ip6.uint64[1] = 0; ip_port_loopback.ip.ip6.uint8[15] = 1; // ::1 @@ -135,7 +135,7 @@ static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s) Socket sock = net_socket(TOX_AF_INET6, TOX_SOCK_STREAM, TOX_PROTO_TCP); IP_Port ip_port_loopback; - ip_port_loopback.ip.family = AF_INET6; + ip_port_loopback.ip.family = TOX_AF_INET6; ip_port_loopback.ip.ip6.uint64[0] = 0; ip_port_loopback.ip.ip6.uint64[1] = 0; ip_port_loopback.ip.ip6.uint8[15] = 1; // ::1 @@ -405,7 +405,7 @@ START_TEST(test_client) IP_Port ip_port_tcp_s; ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); - ip_port_tcp_s.ip.family = AF_INET6; + ip_port_tcp_s.ip.family = TOX_AF_INET6; get_ip6(&ip_port_tcp_s.ip.ip6, &in6addr_loopback); TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, 0); c_sleep(50); @@ -503,7 +503,7 @@ START_TEST(test_client_invalid) IP_Port ip_port_tcp_s; ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); - ip_port_tcp_s.ip.family = AF_INET6; + ip_port_tcp_s.ip.family = TOX_AF_INET6; get_ip6(&ip_port_tcp_s.ip.ip6, &in6addr_loopback); TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, 0); c_sleep(50); @@ -572,7 +572,7 @@ START_TEST(test_tcp_connection) IP_Port ip_port_tcp_s; ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); - ip_port_tcp_s.ip.family = AF_INET6; + ip_port_tcp_s.ip.family = TOX_AF_INET6; get_ip6(&ip_port_tcp_s.ip.ip6, &in6addr_loopback); int connection = new_tcp_connection_to(tc_1, tcp_connections_public_key(tc_2), 123); @@ -681,7 +681,7 @@ START_TEST(test_tcp_connection2) IP_Port ip_port_tcp_s; ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); - ip_port_tcp_s.ip.family = AF_INET6; + ip_port_tcp_s.ip.family = TOX_AF_INET6; get_ip6(&ip_port_tcp_s.ip.ip6, &in6addr_loopback); int connection = new_tcp_connection_to(tc_1, tcp_connections_public_key(tc_2), 123); diff --git a/auto_tests/dht_test.c b/auto_tests/dht_test.c index c04b67e4..5a4f8005 100644 --- a/auto_tests/dht_test.c +++ b/auto_tests/dht_test.c @@ -99,7 +99,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 == AF_INET6 ? 1 : 0; + uint8_t ipv6 = ip_port->ip.family == TOX_AF_INET6 ? 1 : 0; // check id update for existing ip_port test = rand() % length; @@ -174,7 +174,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 == AF_INET6 ? 1 : 0; + uint8_t ipv6 = ip_port->ip.family == TOX_AF_INET6 ? 1 : 0; random_bytes(public_key, sizeof(public_key)); mark_all_good(list, length, ipv6); @@ -218,7 +218,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 == AF_INET6 ? 1 : 0; + uint8_t ipv6 = ip_port->ip.family == TOX_AF_INET6 ? 1 : 0; random_bytes(public_key, sizeof(public_key)); mark_all_good(list, length, ipv6); @@ -280,7 +280,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 == AF_INET6 ? 1 : 0; + uint8_t ipv6 = ip_port->ip.family == TOX_AF_INET6 ? 1 : 0; mark_all_good(list, length, ipv6); @@ -703,10 +703,10 @@ static void random_ip(IP_Port *ipp, int family) uint8_t *ip = NULL; size_t size; - if (family == AF_INET || family == TCP_INET) { + if (family == TOX_AF_INET || family == TCP_INET) { ip = (uint8_t *)&ipp->ip.ip4; size = sizeof(ipp->ip.ip4); - } else if (family == AF_INET6 || family == TCP_INET6) { + } else if (family == TOX_AF_INET6 || family == TCP_INET6) { ip = (uint8_t *)&ipp->ip.ip6; size = sizeof(ipp->ip.ip6); } else { @@ -734,18 +734,18 @@ START_TEST(test_dht_node_packing) random_bytes(nodes[1].public_key, pk_size); random_bytes(nodes[2].public_key, pk_size); - random_ip(&nodes[0].ip_port, AF_INET); - random_ip(&nodes[1].ip_port, AF_INET); - random_ip(&nodes[2].ip_port, AF_INET); + random_ip(&nodes[0].ip_port, TOX_AF_INET); + random_ip(&nodes[1].ip_port, TOX_AF_INET); + random_ip(&nodes[2].ip_port, TOX_AF_INET); dht_pack_unpack(nodes, 3, data, length); - random_ip(&nodes[0].ip_port, AF_INET); - random_ip(&nodes[1].ip_port, AF_INET); + random_ip(&nodes[0].ip_port, TOX_AF_INET); + random_ip(&nodes[1].ip_port, TOX_AF_INET); random_ip(&nodes[2].ip_port, TCP_INET); dht_pack_unpack(nodes, 3, data, length); - random_ip(&nodes[0].ip_port, AF_INET); - random_ip(&nodes[1].ip_port, AF_INET6); + random_ip(&nodes[0].ip_port, TOX_AF_INET); + random_ip(&nodes[1].ip_port, TOX_AF_INET6); random_ip(&nodes[2].ip_port, TCP_INET6); dht_pack_unpack(nodes, 3, data, length); @@ -754,9 +754,9 @@ START_TEST(test_dht_node_packing) random_ip(&nodes[2].ip_port, TCP_INET); dht_pack_unpack(nodes, 3, data, length); - random_ip(&nodes[0].ip_port, AF_INET6); - random_ip(&nodes[1].ip_port, AF_INET6); - random_ip(&nodes[2].ip_port, AF_INET6); + random_ip(&nodes[0].ip_port, TOX_AF_INET6); + random_ip(&nodes[1].ip_port, TOX_AF_INET6); + random_ip(&nodes[2].ip_port, TOX_AF_INET6); dht_pack_unpack(nodes, 3, data, length); free(data); diff --git a/auto_tests/network_test.c b/auto_tests/network_test.c index 67090f74..4a05d5c3 100644 --- a/auto_tests/network_test.c +++ b/auto_tests/network_test.c @@ -37,7 +37,7 @@ START_TEST(test_addr_resolv_localhost) ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); char ip_str[IP_NTOA_LEN]; - ck_assert_msg(ip.family == AF_INET, "Expected family AF_INET, got %u.", ip.family); + ck_assert_msg(ip.family == TOX_AF_INET, "Expected family TOX_AF_INET, got %u.", ip.family); ck_assert_msg(ip.ip4.uint32 == net_htonl(0x7F000001), "Expected 127.0.0.1, got %s.", ip_ntoa(&ip, ip_str, sizeof(ip_str))); @@ -51,7 +51,7 @@ START_TEST(test_addr_resolv_localhost) ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); - ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6 (%u), got %u.", AF_INET6, ip.family); + 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, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip, ip_str, sizeof(ip_str))); @@ -67,11 +67,11 @@ START_TEST(test_addr_resolv_localhost) res = addr_resolve(localhost, &ip, &extra); ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); - ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6 (%u), got %u.", AF_INET6, ip.family); + 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, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip, ip_str, sizeof(ip_str))); - ck_assert_msg(extra.family == AF_INET, "Expected family AF_INET (%u), got %u.", AF_INET, extra.family); + 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 == net_htonl(0x7F000001), "Expected 127.0.0.1, got %s.", ip_ntoa(&ip, ip_str, sizeof(ip_str))); } @@ -93,24 +93,27 @@ START_TEST(test_ip_equal) res = ip_equal(NULL, &ip1); ck_assert_msg(res == 0, "ip_equal(NULL, PTR): expected result 0, got %u.", res); - ip1.family = AF_INET; + ip1.family = TOX_AF_INET; ip1.ip4.uint32 = net_htonl(0x7F000001); res = ip_equal(&ip1, &ip2); - ck_assert_msg(res == 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_UNSPEC, 0} ): expected result 0, got %u.", res); + ck_assert_msg(res == 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {AF_UNSPEC, 0} ): " + "expected result 0, got %u.", res); - ip2.family = AF_INET; + ip2.family = TOX_AF_INET; ip2.ip4.uint32 = net_htonl(0x7F000001); res = ip_equal(&ip1, &ip2); - ck_assert_msg(res != 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_INET, 127.0.0.1} ): expected result != 0, got 0."); + 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); res = ip_equal(&ip1, &ip2); - ck_assert_msg(res == 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_INET, 127.0.0.2} ): expected result 0, got %u.", res); + 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 = AF_INET6; + ip2.family = TOX_AF_INET6; ip2.ip6.uint32[0] = 0; ip2.ip6.uint32[1] = 0; ip2.ip6.uint32[2] = net_htonl(0xFFFF); @@ -120,19 +123,20 @@ START_TEST(test_ip_equal) "IPV6_IPV4_IN_V6(::ffff:127.0.0.1): expected != 0, got 0."); res = ip_equal(&ip1, &ip2); - ck_assert_msg(res != 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_INET6, ::ffff:127.0.0.1} ): expected result != 0, got 0."); + ck_assert_msg(res != 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_INET6, ::ffff:127.0.0.1} ): " + "expected result != 0, got 0."); memcpy(&ip2.ip6, &in6addr_loopback, sizeof(IP6)); res = ip_equal(&ip1, &ip2); - ck_assert_msg(res == 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_INET6, ::1} ): expected result 0, got %u.", res); + ck_assert_msg(res == 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_INET6, ::1} ): expected result 0, got %u.", res); memcpy(&ip1, &ip2, sizeof(IP)); res = ip_equal(&ip1, &ip2); - ck_assert_msg(res != 0, "ip_equal( {AF_INET6, ::1}, {AF_INET6, ::1} ): expected result != 0, got 0."); + ck_assert_msg(res != 0, "ip_equal( {TOX_AF_INET6, ::1}, {TOX_AF_INET6, ::1} ): expected result != 0, got 0."); ip2.ip6.uint8[15]++; res = ip_equal(&ip1, &ip2); - ck_assert_msg(res == 0, "ip_equal( {AF_INET6, ::1}, {AF_INET6, ::2} ): expected result 0, got %res.", res); + ck_assert_msg(res == 0, "ip_equal( {TOX_AF_INET6, ::1}, {TOX_AF_INET6, ::2} ): expected result 0, got %res.", res); } END_TEST diff --git a/testing/dns3_test.c b/testing/dns3_test.c index 4c03fc52..0df7ace2 100644 --- a/testing/dns3_test.c +++ b/testing/dns3_test.c @@ -40,7 +40,7 @@ int main(int argc, char *argv[]) } IP ip = {0}; - ip.family = AF_INET; + ip.family = TOX_AF_INET; Socket sock = net_socket(ip.family, TOX_SOCK_DGRAM, TOX_PROTO_UDP); if (!sock_valid(sock)) { diff --git a/toxcore/DHT.c b/toxcore/DHT.c index e12c4d64..193cb632 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -258,9 +258,9 @@ int handle_request(const uint8_t *self_public_key, const uint8_t *self_secret_ke void to_net_family(IP *ip) { - if (ip->family == AF_INET) { + if (ip->family == TOX_AF_INET) { ip->family = TOX_AF_INET; - } else if (ip->family == AF_INET6) { + } else if (ip->family == TOX_AF_INET6) { ip->family = TOX_AF_INET6; } } @@ -268,12 +268,12 @@ void to_net_family(IP *ip) int to_host_family(IP *ip) { if (ip->family == TOX_AF_INET) { - ip->family = AF_INET; + ip->family = TOX_AF_INET; return 0; } if (ip->family == TOX_AF_INET6) { - ip->family = AF_INET6; + ip->family = TOX_AF_INET6; return 0; } @@ -289,11 +289,11 @@ int to_host_family(IP *ip) int packed_node_size(uint8_t ip_family) { switch (ip_family) { - case AF_INET: + case TOX_AF_INET: case TCP_INET: return PACKED_NODE_SIZE_IP4; - case AF_INET6: + case TOX_AF_INET6: case TCP_INET6: return PACKED_NODE_SIZE_IP6; @@ -317,14 +317,14 @@ static 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 == AF_INET) { + if (ip_port->ip.family == TOX_AF_INET) { // TODO(irungentoo): use functions to convert endianness is_ipv4 = true; net_family = TOX_AF_INET; } else if (ip_port->ip.family == TCP_INET) { is_ipv4 = true; net_family = TOX_TCP_INET; - } else if (ip_port->ip.family == AF_INET6) { + } else if (ip_port->ip.family == TOX_AF_INET6) { is_ipv4 = false; net_family = TOX_AF_INET6; } else if (ip_port->ip.family == TCP_INET6) { @@ -397,7 +397,7 @@ static int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length if (data[0] == TOX_AF_INET) { is_ipv4 = true; - host_family = AF_INET; + host_family = TOX_AF_INET; } else if (data[0] == TOX_TCP_INET) { if (!tcp_enabled) { return -1; @@ -407,7 +407,7 @@ static int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length host_family = TCP_INET; } else if (data[0] == TOX_AF_INET6) { is_ipv4 = false; - host_family = AF_INET6; + host_family = TOX_AF_INET6; } else if (data[0] == TOX_TCP_INET6) { if (!tcp_enabled) { return -1; @@ -551,8 +551,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 == AF_INET && ipport_equal(&array[i].assoc4.ip_port, ip_port) || - ip_port->ip.family == AF_INET6 && ipport_equal(&array[i].assoc6.ip_port, ip_port)) { + 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)) { return i; } } @@ -567,10 +567,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 == AF_INET) { + if (ip_port.ip.family == TOX_AF_INET) { assoc = &client->assoc4; ip_version = 4; - } else if (ip_port.ip.family == AF_INET6) { + } else if (ip_port.ip.family == TOX_AF_INET6) { assoc = &client->assoc6; ip_version = 6; } else { @@ -628,7 +628,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 == AF_INET) { + if (ip_port.ip.family == TOX_AF_INET) { assoc = &list[index].assoc4; ip_version = 4; } else { @@ -692,7 +692,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 != AF_INET) && (sa_family != AF_INET6) && (sa_family != 0)) { + if ((sa_family != TOX_AF_INET) && (sa_family != TOX_AF_INET6) && (sa_family != 0)) { return; } @@ -708,9 +708,9 @@ static void get_close_nodes_inner(const uint8_t *public_key, Node_format *nodes_ const IPPTsPng *ipptp = NULL; - if (sa_family == AF_INET) { + if (sa_family == TOX_AF_INET) { ipptp = &client->assoc4; - } else if (sa_family == AF_INET6) { + } else if (sa_family == TOX_AF_INET6) { ipptp = &client->assoc6; } else if (client->assoc4.timestamp >= client->assoc6.timestamp) { ipptp = &client->assoc4; @@ -879,7 +879,7 @@ static void update_client_with_reset(Client_data *client, const IP_Port *ip_port IPPTsPng *ipptp_write = NULL; IPPTsPng *ipptp_clear = NULL; - if (ip_port->ip.family == AF_INET) { + if (ip_port->ip.family == TOX_AF_INET) { ipptp_write = &client->assoc4; ipptp_clear = &client->assoc6; } else { @@ -917,7 +917,7 @@ static int replace_all(Client_data *list, IP_Port ip_port, const uint8_t *comp_public_key) { - if ((ip_port.ip.family != AF_INET) && (ip_port.ip.family != AF_INET6)) { + if ((ip_port.ip.family != TOX_AF_INET) && (ip_port.ip.family != TOX_AF_INET6)) { return 0; } @@ -988,7 +988,7 @@ static bool is_pk_in_client_list(Client_data *list, unsigned int client_list_len return 0; } - const IPPTsPng *assoc = ip_port.ip.family == AF_INET ? + const IPPTsPng *assoc = ip_port.ip.family == TOX_AF_INET ? &list[index].assoc4 : &list[index].assoc6; @@ -1079,8 +1079,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 == AF_INET6) && IPV6_IPV4_IN_V6(ip_port.ip.ip6)) { - ip_port.ip.family = AF_INET; + if ((ip_port.ip.family == TOX_AF_INET6) && IPV6_IPV4_IN_V6(ip_port.ip.ip6)) { + ip_port.ip.family = TOX_AF_INET; ip_port.ip.ip4.uint32 = ip_port.ip.ip6.uint32[3]; } @@ -1140,9 +1140,9 @@ static bool update_client_data(Client_data *array, size_t size, IP_Port ip_port, Client_data *data = &array[index]; IPPTsPng *assoc; - if (ip_port.ip.family == AF_INET) { + if (ip_port.ip.family == TOX_AF_INET) { assoc = &data->assoc4; - } else if (ip_port.ip.family == AF_INET6) { + } else if (ip_port.ip.family == TOX_AF_INET6) { assoc = &data->assoc6; } else { return true; @@ -1159,8 +1159,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 == AF_INET6) && IPV6_IPV4_IN_V6(ip_port.ip.ip6)) { - ip_port.ip.family = AF_INET; + if ((ip_port.ip.family == TOX_AF_INET6) && IPV6_IPV4_IN_V6(ip_port.ip.ip6)) { + ip_port.ip.family = TOX_AF_INET; ip_port.ip.ip4.uint32 = ip_port.ip.ip6.uint32[3]; } @@ -2237,11 +2237,11 @@ static IPPTsPng *get_closelist_IPPTsPng(DHT *dht, const uint8_t *public_key, Fam continue; } - if (sa_family == AF_INET) { + if (sa_family == TOX_AF_INET) { return &dht->close_clientlist[i].assoc4; } - if (sa_family == AF_INET6) { + if (sa_family == TOX_AF_INET6) { return &dht->close_clientlist[i].assoc6; } } @@ -2464,10 +2464,10 @@ static void do_hardening(DHT *dht) if (i % 2 == 0) { cur_iptspng = &dht->close_clientlist[i / 2].assoc4; - sa_family = AF_INET; + sa_family = TOX_AF_INET; } else { cur_iptspng = &dht->close_clientlist[i / 2].assoc6; - sa_family = AF_INET6; + sa_family = TOX_AF_INET6; } if (is_timeout(cur_iptspng->timestamp, BAD_NODE_TIMEOUT)) { @@ -2675,7 +2675,7 @@ uint32_t DHT_size(const DHT *dht) uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2; - return size32 + sizesubhead + (packed_node_size(AF_INET) * numv4) + (packed_node_size(AF_INET6) * numv6); + return size32 + sizesubhead + (packed_node_size(TOX_AF_INET) * numv4) + (packed_node_size(TOX_AF_INET6) * 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 98fa18d6..edba0ad4 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h @@ -59,12 +59,6 @@ #define PING_ROUNDTRIP 2 #define BAD_NODE_TIMEOUT (PING_INTERVAL + PINGS_MISSED_NODE_GOES_BAD * (PING_INTERVAL + PING_ROUNDTRIP)) -/* Redefinitions of variables for safe transfer over wire. */ -#define TOX_AF_INET 2 -#define TOX_AF_INET6 10 -#define TOX_TCP_INET 130 -#define TOX_TCP_INET6 138 - /* The number of "fake" friends to add (for optimization purposes and so our paths for the onion part are more random) */ #define DHT_FAKE_FRIEND_NUMBER 2 diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c index fdb374fa..ed8a346c 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c @@ -82,9 +82,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 == AF_INET && subnet_mask.family == AF_INET) { + if (gateway.family == TOX_AF_INET && subnet_mask.family == TOX_AF_INET) { IP_Port *ip_port = &ip_ports[count]; - ip_port->ip.family = AF_INET; + 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 broadcast_ip = gateway_ip + ~subnet_ip - 1; ip_port->ip.ip4.uint32 = net_htonl(broadcast_ip); @@ -160,8 +160,8 @@ static void fetch_broadcast_info(uint16_t port) continue; } - /* moot check: only AF_INET returned (backwards compat.) */ - if (i_faces[i].ifr_broadaddr.sa_family != AF_INET) { + /* moot check: only TOX_AF_INET returned (backwards compat.) */ + if (i_faces[i].ifr_broadaddr.sa_family != TOX_AF_INET) { continue; } @@ -172,7 +172,7 @@ static void fetch_broadcast_info(uint16_t port) } IP_Port *ip_port = &ip_ports[count]; - ip_port->ip.family = AF_INET; + ip_port->ip.family = TOX_AF_INET; ip_port->ip.ip4.uint32 = sock4->sin_addr.s_addr; if (ip_port->ip.ip4.uint32 == 0) { @@ -232,25 +232,25 @@ static IP broadcast_ip(Family family_socket, Family family_broadcast) IP ip; ip_reset(&ip); - if (family_socket == AF_INET6) { - if (family_broadcast == AF_INET6) { - ip.family = AF_INET6; + if (family_socket == TOX_AF_INET6) { + if (family_broadcast == TOX_AF_INET6) { + ip.family = TOX_AF_INET6; /* 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; - } else if (family_broadcast == AF_INET) { - ip.family = AF_INET6; + } else if (family_broadcast == TOX_AF_INET) { + ip.family = TOX_AF_INET6; ip.ip6.uint32[0] = 0; ip.ip6.uint32[1] = 0; ip.ip6.uint32[2] = net_htonl(0xFFFF); ip.ip6.uint32[3] = INADDR_BROADCAST; } - } else if (family_socket == AF_INET) { - if (family_broadcast == AF_INET) { - ip.family = AF_INET; + } else if (family_socket == TOX_AF_INET) { + if (family_broadcast == TOX_AF_INET) { + ip.family = TOX_AF_INET; ip.ip4.uint32 = INADDR_BROADCAST; } } @@ -261,7 +261,7 @@ static IP broadcast_ip(Family family_socket, Family family_broadcast) /* Is IP a local ip or not. */ bool Local_ip(IP ip) { - if (ip.family == AF_INET) { + if (ip.family == TOX_AF_INET) { IP4 ip4 = ip.ip4; /* Loopback. */ @@ -272,7 +272,7 @@ bool Local_ip(IP ip) /* embedded IPv4-in-IPv6 */ if (IPV6_IPV4_IN_V6(ip.ip6)) { IP ip4; - ip4.family = AF_INET; + ip4.family = TOX_AF_INET; ip4.ip4.uint32 = ip.ip6.uint32[3]; return Local_ip(ip4); } @@ -295,7 +295,7 @@ int LAN_ip(IP ip) return 0; } - if (ip.family == AF_INET) { + if (ip.family == TOX_AF_INET) { IP4 ip4 = ip.ip4; /* 10.0.0.0 to 10.255.255.255 range. */ @@ -324,7 +324,7 @@ int LAN_ip(IP ip) if ((ip4.uint8[0] == 100) && ((ip4.uint8[1] & 0xC0) == 0x40)) { return 0; } - } else if (ip.family == AF_INET6) { + } else if (ip.family == TOX_AF_INET6) { /* autogenerated for each interface: FE80::* (up to FEBF::*) FF02::1 is - according to RFC 4291 - multicast all-nodes link-local */ @@ -336,7 +336,7 @@ int LAN_ip(IP ip) /* embedded IPv4-in-IPv6 */ if (IPV6_IPV4_IN_V6(ip.ip6)) { IP ip4; - ip4.family = AF_INET; + ip4.family = TOX_AF_INET; ip4.ip4.uint32 = ip.ip6.uint32[3]; return LAN_ip(ip4); } @@ -375,8 +375,8 @@ int send_LANdiscovery(uint16_t port, DHT *dht) ip_port.port = port; /* IPv6 multicast */ - if (dht->net->family == AF_INET6) { - ip_port.ip = broadcast_ip(AF_INET6, AF_INET6); + if (dht->net->family == TOX_AF_INET6) { + ip_port.ip = broadcast_ip(TOX_AF_INET6, TOX_AF_INET6); if (ip_isset(&ip_port.ip)) { if (sendpacket(dht->net, ip_port, data, 1 + CRYPTO_PUBLIC_KEY_SIZE) > 0) { @@ -385,8 +385,8 @@ int send_LANdiscovery(uint16_t port, DHT *dht) } } - /* IPv4 broadcast (has to be IPv4-in-IPv6 mapping if socket is AF_INET6 */ - ip_port.ip = broadcast_ip(dht->net->family, AF_INET); + /* IPv4 broadcast (has to be IPv4-in-IPv6 mapping if socket is TOX_AF_INET6 */ + ip_port.ip = broadcast_ip(dht->net->family, TOX_AF_INET); if (ip_isset(&ip_port.ip)) { if (sendpacket(dht->net, ip_port, data, 1 + CRYPTO_PUBLIC_KEY_SIZE)) { diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index c5638f03..c04c78c9 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -2531,7 +2531,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 = AF_INET; + local_ip_port.ip.family = TOX_AF_INET; local_ip_port.ip.ip4.uint32 = INADDR_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 e584b265..f44fabb3 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -145,7 +145,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 == AF_INET) { + 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)); @@ -170,7 +170,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 == AF_INET) { + if (TCP_conn->ip_port.ip.family == TOX_AF_INET) { uint8_t data[4 + sizeof(IP4) + sizeof(uint16_t)]; int ret = read_TCP_packet(TCP_conn->sock, data, sizeof(data)); @@ -251,7 +251,8 @@ static int client_send_pending_data_nonpriority(TCP_Client_Connection *con) } uint16_t left = con->last_packet_length - con->last_packet_sent; - int len = send(con->sock, (const char *)(con->last_packet + con->last_packet_sent), left, MSG_NOSIGNAL); + const char *data = (const char *)(con->last_packet + con->last_packet_sent); + int len = send(con->sock, data, left, MSG_NOSIGNAL); if (len <= 0) { return -1; @@ -614,12 +615,10 @@ TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, const uint8_t *public return NULL; } - if (ip_port.ip.family != AF_INET && ip_port.ip.family != AF_INET6) { + if (ip_port.ip.family != TOX_AF_INET && ip_port.ip.family != TOX_AF_INET6) { return NULL; } - uint8_t family = ip_port.ip.family; - TCP_Proxy_Info default_proxyinfo; if (proxy_info == NULL) { @@ -627,6 +626,8 @@ 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; + 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 b9c75afe..25159491 100644 --- a/toxcore/TCP_connection.c +++ b/toxcore/TCP_connection.c @@ -1116,12 +1116,12 @@ 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 = AF_INET; + ip_port.ip.family = TOX_AF_INET; } else if (ip_port.ip.family == TCP_INET6) { - ip_port.ip.family = AF_INET6; + ip_port.ip.family = TOX_AF_INET6; } - if (ip_port.ip.family != AF_INET && ip_port.ip.family != AF_INET6) { + if (ip_port.ip.family != TOX_AF_INET && ip_port.ip.family != TOX_AF_INET6) { return -1; } @@ -1276,9 +1276,9 @@ unsigned int tcp_copy_connected_relays(TCP_Connections *tcp_c, Node_format *tcp_ memcpy(tcp_relays[copied].public_key, tcp_con->connection->public_key, CRYPTO_PUBLIC_KEY_SIZE); tcp_relays[copied].ip_port = tcp_con->connection->ip_port; - if (tcp_relays[copied].ip_port.ip.family == AF_INET) { + 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 == AF_INET6) { + } else if (tcp_relays[copied].ip_port.ip.family == TOX_AF_INET6) { tcp_relays[copied].ip_port.ip.family = TCP_INET6; } diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index 3f2e554f..9b94667a 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c @@ -986,7 +986,7 @@ static Socket new_listening_TCP_socket(int family, uint16_t port) int ok = set_socket_nonblock(sock); - if (ok && family == AF_INET6) { + if (ok && family == TOX_AF_INET6) { ok = set_socket_dualstack(sock); } @@ -1042,9 +1042,9 @@ TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uin uint8_t family; if (ipv6_enabled) { - family = AF_INET6; + family = TOX_AF_INET6; } else { - family = AF_INET; + family = TOX_AF_INET; } uint32_t i; diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c index 0fbae406..ccf1621f 100644 --- a/toxcore/friend_connection.c +++ b/toxcore/friend_connection.c @@ -511,7 +511,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 != AF_INET && n_c->source.ip.family != AF_INET6) { + if (n_c->source.ip.family != TOX_AF_INET && n_c->source.ip.family != TOX_AF_INET6) { 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; diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 1d32a489..440db94a 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -431,7 +431,7 @@ static int add_ip_port_connection(Net_Crypto *c, int crypt_connection_id, IP_Por return -1; } - if (ip_port.ip.family == AF_INET) { + if (ip_port.ip.family == TOX_AF_INET) { if (!ipport_equal(&ip_port, &conn->ip_portv4) && LAN_ip(conn->ip_portv4.ip) != 0) { if (!bs_list_add(&c->ip_port_list, (uint8_t *)&ip_port, crypt_connection_id)) { return -1; @@ -441,7 +441,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 == AF_INET6) { + } else if (ip_port.ip.family == TOX_AF_INET6) { 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; @@ -486,11 +486,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 == AF_INET6) { + if (v6 && conn->ip_portv6.ip.family == TOX_AF_INET6) { return conn->ip_portv6; } - if (conn->ip_portv4.ip.family == AF_INET) { + if (conn->ip_portv4.ip.family == TOX_AF_INET) { return conn->ip_portv4; } @@ -1695,12 +1695,12 @@ static int crypto_connection_add_source(Net_Crypto *c, int crypt_connection_id, return -1; } - if (source.ip.family == AF_INET || source.ip.family == AF_INET6) { + if (source.ip.family == TOX_AF_INET || source.ip.family == TOX_AF_INET6) { if (add_ip_port_connection(c, crypt_connection_id, source) != 0) { return -1; } - if (source.ip.family == AF_INET) { + if (source.ip.family == TOX_AF_INET) { conn->direct_lastrecv_timev4 = unix_time(); } else { conn->direct_lastrecv_timev6 = unix_time(); @@ -1925,13 +1925,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 == AF_INET) { + if (ip_port.ip.family == TOX_AF_INET) { conn->direct_lastrecv_timev4 = unix_time(); } else { conn->direct_lastrecv_timev6 = unix_time(); } } else { - if (ip_port.ip.family == AF_INET) { + if (ip_port.ip.family == TOX_AF_INET) { conn->direct_lastrecv_timev4 = 0; } else { conn->direct_lastrecv_timev6 = 0; @@ -2271,7 +2271,7 @@ static int udp_handle_packet(void *object, IP_Port source, const uint8_t *packet pthread_mutex_lock(&conn->mutex); - if (source.ip.family == AF_INET) { + if (source.ip.family == TOX_AF_INET) { conn->direct_lastrecv_timev4 = unix_time(); } else { conn->direct_lastrecv_timev6 = unix_time(); diff --git a/toxcore/network.c b/toxcore/network.c index edf6f11a..45805244 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -70,7 +70,7 @@ static const char *inet_ntop(Family family, const void *addr, char *buf, size_t bufsize) { - if (family == AF_INET) { + if (family == TOX_AF_INET) { struct sockaddr_in saddr; memset(&saddr, 0, sizeof(saddr)); @@ -84,7 +84,7 @@ static const char *inet_ntop(Family family, const void *addr, char *buf, size_t } return buf; - } else if (family == AF_INET6) { + } else if (family == TOX_AF_INET6) { struct sockaddr_in6 saddr; memset(&saddr, 0, sizeof(saddr)); @@ -105,7 +105,7 @@ static const char *inet_ntop(Family family, const void *addr, char *buf, size_t static int inet_pton(Family family, const char *addrString, void *addrbuf) { - if (family == AF_INET) { + if (family == TOX_AF_INET) { struct sockaddr_in saddr; memset(&saddr, 0, sizeof(saddr)); @@ -118,7 +118,7 @@ static int inet_pton(Family family, const char *addrString, void *addrbuf) *(struct in_addr *)addrbuf = saddr.sin_addr; return 1; - } else if (family == AF_INET6) { + } else if (family == TOX_AF_INET6) { struct sockaddr_in6 saddr; memset(&saddr, 0, sizeof(saddr)); @@ -140,7 +140,8 @@ static int inet_pton(Family family, const char *addrString, void *addrbuf) static int make_proto(int proto); static int make_socktype(int type); -static int make_family(int family); +static int make_family(int tox_family); +static int make_tox_family(int family); /* Check if socket is valid. * @@ -366,8 +367,8 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1 return -1; } - /* socket AF_INET, but target IP NOT: can't send */ - if ((net->family == AF_INET) && (ip_port.ip.family != AF_INET)) { + /* socket TOX_AF_INET, but target IP NOT: can't send */ + if ((net->family == TOX_AF_INET) && (ip_port.ip.family != TOX_AF_INET)) { return -1; } @@ -375,8 +376,8 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1 size_t addrsize = 0; - if (ip_port.ip.family == AF_INET) { - if (net->family == AF_INET6) { + if (ip_port.ip.family == TOX_AF_INET) { + if (net->family == TOX_AF_INET6) { /* must convert to IPV4-in-IPV6 address */ struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; @@ -404,7 +405,7 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1 fill_addr4(ip_port.ip.ip4, &addr4->sin_addr); addr4->sin_port = ip_port.port; } - } else if (ip_port.ip.family == AF_INET6) { + } else if (ip_port.ip.family == TOX_AF_INET6) { struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; addrsize = sizeof(struct sockaddr_in6); @@ -457,17 +458,17 @@ 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 = addr_in->sin_family; + ip_port->ip.family = make_tox_family(addr_in->sin_family); get_ip4(&ip_port->ip.ip4, &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 = addr_in6->sin6_family; + ip_port->ip.family = make_tox_family(addr_in6->sin6_family); get_ip6(&ip_port->ip.ip6, &addr_in6->sin6_addr); ip_port->port = addr_in6->sin6_port; if (IPV6_IPV4_IN_V6(ip_port->ip.ip6)) { - ip_port->ip.family = AF_INET; + ip_port->ip.family = TOX_AF_INET; ip_port->ip.ip4.uint32 = ip_port->ip.ip6.uint32[3]; } } else { @@ -602,7 +603,7 @@ 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 != AF_INET && ip.family != AF_INET6) { + if (ip.family != TOX_AF_INET && ip.family != TOX_AF_INET6) { LOGGER_ERROR(log, "Invalid address family: %u\n", ip.family); return NULL; } @@ -674,7 +675,9 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 struct sockaddr_storage addr; size_t addrsize; - if (temp->family == AF_INET) { + memset(&addr, 0, sizeof(struct sockaddr_storage)); + + if (temp->family == TOX_AF_INET) { struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; addrsize = sizeof(struct sockaddr_in); @@ -683,7 +686,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 fill_addr4(ip.ip4, &addr4->sin_addr); portptr = &addr4->sin_port; - } else if (temp->family == AF_INET6) { + } else if (temp->family == TOX_AF_INET6) { struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; addrsize = sizeof(struct sockaddr_in6); @@ -700,7 +703,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 return NULL; } - if (ip.family == AF_INET6) { + if (ip.family == TOX_AF_INET6) { 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"); @@ -812,7 +815,7 @@ int ip_equal(const IP *a, const IP *b) /* same family */ if (a->family == b->family) { - if (a->family == AF_INET || a->family == TCP_INET) { + 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); @@ -820,7 +823,7 @@ int ip_equal(const IP *a, const IP *b) return addr_a.s_addr == addr_b.s_addr; } - if (a->family == AF_INET6 || a->family == TCP_INET6) { + 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]; } @@ -829,13 +832,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 == AF_INET) && (b->family == AF_INET6)) { + if ((a->family == TOX_AF_INET) && (b->family == TOX_AF_INET6)) { if (IPV6_IPV4_IN_V6(b->ip6)) { struct in_addr addr_a; fill_addr4(a->ip4, &addr_a); return addr_a.s_addr == b->ip6.uint32[3]; } - } else if ((a->family == AF_INET6) && (b->family == AF_INET)) { + } else if ((a->family == TOX_AF_INET6) && (b->family == TOX_AF_INET)) { if (IPV6_IPV4_IN_V6(a->ip6)) { struct in_addr addr_b; fill_addr4(b->ip4, &addr_b); @@ -883,7 +886,7 @@ void ip_init(IP *ip, uint8_t ipv6enabled) } memset(ip, 0, sizeof(IP)); - ip->family = ipv6enabled ? AF_INET6 : AF_INET; + ip->family = ipv6enabled ? TOX_AF_INET6 : TOX_AF_INET; } /* checks if ip is valid */ @@ -947,20 +950,22 @@ const char *ip_ntoa(const IP *ip, char *ip_str, size_t length) } if (ip) { - if (ip->family == AF_INET) { + const int family = make_family(ip->family); + + if (ip->family == TOX_AF_INET) { /* returns standard quad-dotted notation */ struct in_addr addr; fill_addr4(ip->ip4, &addr); ip_str[0] = 0; - inet_ntop(ip->family, &addr, ip_str, length); - } else if (ip->family == AF_INET6) { + 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); ip_str[0] = '['; - inet_ntop(ip->family, &addr, &ip_str[1], length - 3); + inet_ntop(family, &addr, &ip_str[1], length - 3); size_t len = strlen(ip_str); ip_str[len] = ']'; ip_str[len + 1] = 0; @@ -981,10 +986,10 @@ const char *ip_ntoa(const IP *ip, char *ip_str, size_t length) * parses IP structure into an address string * * input - * ip: ip of AF_INET or AF_INET6 families + * ip: ip of TOX_AF_INET or TOX_AF_INET6 families * length: length of the address buffer - * Must be at least INET_ADDRSTRLEN for AF_INET - * and INET6_ADDRSTRLEN for AF_INET6 + * Must be at least INET_ADDRSTRLEN for TOX_AF_INET + * and INET6_ADDRSTRLEN for TOX_AF_INET6 * * output * address: dotted notation (IPv4: quad, IPv6: 16) or colon notation (IPv6) @@ -997,12 +1002,12 @@ int ip_parse_addr(const IP *ip, char *address, size_t length) return 0; } - if (ip->family == AF_INET) { + if (ip->family == TOX_AF_INET) { const struct in_addr *addr = (const struct in_addr *)&ip->ip4; return inet_ntop(ip->family, addr, address, length) != NULL; } - if (ip->family == AF_INET6) { + if (ip->family == TOX_AF_INET6) { const struct in6_addr *addr = (const struct in6_addr *)&ip->ip6; return inet_ntop(ip->family, addr, address, length) != NULL; } @@ -1031,16 +1036,16 @@ int addr_parse_ip(const char *address, IP *to) struct in_addr addr4; - if (1 == inet_pton(AF_INET, address, &addr4)) { - to->family = AF_INET; + if (inet_pton(AF_INET, address, &addr4) == 1) { + to->family = TOX_AF_INET; get_ip4(&to->ip4, &addr4); return 1; } struct in6_addr addr6; - if (1 == inet_pton(AF_INET6, address, &addr6)) { - to->family = AF_INET6; + if (inet_pton(AF_INET6, address, &addr6) == 1) { + to->family = TOX_AF_INET6; get_ip6(&to->ip6, &addr6); return 1; } @@ -1056,13 +1061,13 @@ int addr_parse_ip(const char *address, IP *to) * input * address: a hostname (or something parseable to an IP address) * to: to.family MUST be initialized, either set to a specific IP version - * (AF_INET/AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both + * (TOX_AF_INET/TOX_AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both * IP versions are acceptable * extra can be NULL and is only set in special circumstances, see returns * * returns in *to a valid IPAny (v4/v6), * prefers v6 if ip.family was AF_UNSPEC and both available - * returns in *extra an IPv4 address, if family was AF_UNSPEC and *to is AF_INET6 + * returns in *extra an IPv4 address, if family was AF_UNSPEC and *to is TOX_AF_INET6 * returns 0 on failure, TOX_ADDR_RESOLVE_* on success. */ int addr_resolve(const char *address, IP *to, IP *extra) @@ -1071,7 +1076,8 @@ int addr_resolve(const char *address, IP *to, IP *extra) return 0; } - Family family = to->family; + Family tox_family = to->family; + Family family = make_family(tox_family); struct addrinfo *server = NULL; struct addrinfo *walker = NULL; @@ -1160,7 +1166,7 @@ int addr_resolve(const char *address, IP *to, IP *extra) * * address: a hostname (or something parseable to an IP address) * to: to.family MUST be initialized, either set to a specific IP version - * (AF_INET/AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both + * (TOX_AF_INET/TOX_AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both * IP versions are acceptable * extra can be NULL and is only set in special circumstances, see returns * @@ -1185,14 +1191,14 @@ int net_connect(Socket sock, IP_Port ip_port) struct sockaddr_storage addr = {0}; size_t addrsize; - if (ip_port.ip.family == AF_INET) { + if (ip_port.ip.family == TOX_AF_INET) { struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; addrsize = sizeof(struct sockaddr_in); addr4->sin_family = AF_INET; fill_addr4(ip_port.ip.ip4, &addr4->sin_addr); addr4->sin_port = ip_port.port; - } else if (ip_port.ip.family == AF_INET6) { + } else if (ip_port.ip.family == TOX_AF_INET6) { struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; addrsize = sizeof(struct sockaddr_in6); @@ -1206,7 +1212,7 @@ int net_connect(Socket sock, IP_Port ip_port) return connect(sock, (struct sockaddr *)&addr, addrsize); } -int32_t net_getipport(const char *node, IP_Port **res, int type) +int32_t net_getipport(const char *node, IP_Port **res, int tox_type) { struct addrinfo *infos; int ret = getaddrinfo(node, NULL, NULL, &infos); @@ -1218,6 +1224,7 @@ int32_t net_getipport(const char *node, IP_Port **res, int type) // Used to avoid malloc parameter overflow const size_t MAX_COUNT = MIN(SIZE_MAX, INT32_MAX) / sizeof(IP_Port); + int type = make_socktype(tox_type); struct addrinfo *cur; int32_t count = 0; @@ -1262,7 +1269,7 @@ int32_t net_getipport(const char *node, IP_Port **res, int type) continue; } - ip_port->ip.family = cur->ai_family; + ip_port->ip.family = make_tox_family(cur->ai_family); ip_port++; } @@ -1285,13 +1292,13 @@ int bind_to_port(Socket sock, int family, uint16_t port) struct sockaddr_storage addr = {0}; size_t addrsize; - if (family == AF_INET) { + if (family == TOX_AF_INET) { 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 == AF_INET6) { + } else if (family == TOX_AF_INET6) { struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; addrsize = sizeof(struct sockaddr_in6); @@ -1304,17 +1311,37 @@ int bind_to_port(Socket sock, int family, uint16_t port) return (bind(sock, (struct sockaddr *)&addr, addrsize) == 0); } -static int make_family(int family) +static int make_tox_family(int family) { switch (family) { + case AF_INET: + return TOX_AF_INET; + + case AF_INET6: + return TOX_AF_INET6; + + case AF_UNSPEC: + return TOX_AF_UNSPEC; + + default: + return family; + } +} + +static int make_family(int tox_family) +{ + switch (tox_family) { case TOX_AF_INET: return AF_INET; case TOX_AF_INET6: return AF_INET6; + case TOX_AF_UNSPEC: + return AF_UNSPEC; + default: - return family; + return tox_family; } } diff --git a/toxcore/network.h b/toxcore/network.h index bdf4081f..350ec222 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -111,6 +111,7 @@ typedef enum NET_PACKET_TYPE { #define TOX_PORT_DEFAULT TOX_PORTRANGE_FROM /* Redefinitions of variables for safe transfer over wire. */ +#define TOX_AF_UNSPEC 0 #define TOX_AF_INET 2 #define TOX_AF_INET6 10 #define TOX_TCP_INET 130 @@ -123,10 +124,10 @@ typedef enum NET_PACKET_TYPE { #define TOX_PROTO_UDP 2 /* TCP related */ -#define TCP_ONION_FAMILY (AF_INET6 + 1) -#define TCP_INET (AF_INET6 + 2) -#define TCP_INET6 (AF_INET6 + 3) -#define TCP_FAMILY (AF_INET6 + 4) +#define TCP_ONION_FAMILY (TOX_AF_INET6 + 1) +#define TCP_INET (TOX_AF_INET6 + 2) +#define TCP_INET6 (TOX_AF_INET6 + 3) +#define TCP_FAMILY (TOX_AF_INET6 + 4) typedef union { uint8_t uint8[4]; @@ -174,7 +175,7 @@ uint32_t net_ntohl(uint32_t hostlong); uint16_t net_ntohs(uint16_t hostshort); /* Does the IP6 struct a contain an IPv4 address in an IPv6 one? */ -#define IPV6_IPV4_IN_V6(a) ((a.uint64[0] == 0) && (a.uint32[2] == htonl (0xffff))) +#define IPV6_IPV4_IN_V6(a) ((a.uint64[0] == 0) && (a.uint32[2] == net_htonl (0xffff))) #define SIZE_IP4 4 #define SIZE_IP6 16 @@ -206,10 +207,10 @@ const char *ip_ntoa(const IP *ip, char *ip_str, size_t length); * parses IP structure into an address string * * input - * ip: ip of AF_INET or AF_INET6 families + * ip: ip of TOX_AF_INET or TOX_AF_INET6 families * length: length of the address buffer - * Must be at least INET_ADDRSTRLEN for AF_INET - * and INET6_ADDRSTRLEN for AF_INET6 + * Must be at least INET_ADDRSTRLEN for TOX_AF_INET + * and INET6_ADDRSTRLEN for TOX_AF_INET6 * * output * address: dotted notation (IPv4: quad, IPv6: 16) or colon notation (IPv6) @@ -270,13 +271,13 @@ void ipport_copy(IP_Port *target, const IP_Port *source); * input * address: a hostname (or something parseable to an IP address) * to: to.family MUST be initialized, either set to a specific IP version - * (AF_INET/AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both + * (TOX_AF_INET/TOX_AF_INET6) or to the unspecified TOX_AF_UNSPEC (= 0), if both * IP versions are acceptable * extra can be NULL and is only set in special circumstances, see returns * * returns in *to a valid IPAny (v4/v6), - * prefers v6 if ip.family was AF_UNSPEC and both available - * returns in *extra an IPv4 address, if family was AF_UNSPEC and *to is AF_INET6 + * prefers v6 if ip.family was TOX_AF_UNSPEC and both available + * returns in *extra an IPv4 address, if family was TOX_AF_UNSPEC and *to is TOX_AF_INET6 * returns 0 on failure */ int addr_resolve(const char *address, IP *to, IP *extra); @@ -287,12 +288,12 @@ int addr_resolve(const char *address, IP *to, IP *extra); * * address: a hostname (or something parseable to an IP address) * to: to.family MUST be initialized, either set to a specific IP version - * (AF_INET/AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both + * (TOX_AF_INET/TOX_AF_INET6) or to the unspecified TOX_AF_UNSPEC (= 0), if both * IP versions are acceptable * extra can be NULL and is only set in special circumstances, see returns * * returns in *tro a matching address (IPv6 or IPv4) - * returns in *extra, if not NULL, an IPv4 address, if to->family was AF_UNSPEC + * returns in *extra, if not NULL, an IPv4 address, if to->family was TOX_AF_UNSPEC * returns 1 on success * returns 0 on failure */ @@ -394,7 +395,7 @@ int net_connect(Socket sock, IP_Port ip_port); * return number of elements in res array * and -1 on error. */ -int32_t net_getipport(const char *node, IP_Port **res, int type); +int32_t net_getipport(const char *node, IP_Port **res, int tox_type); /* Deallocates memory allocated by net_getipport */ diff --git a/toxcore/onion.c b/toxcore/onion.c index 60fdcce0..805770fa 100644 --- a/toxcore/onion.c +++ b/toxcore/onion.c @@ -78,12 +78,11 @@ static int ip_unpack(IP *target, const uint8_t *data, unsigned int data_size, bo memcpy(target->ip6.uint8, data + 1, SIZE_IP6); } - if (!disable_family_check) { - return to_host_family(target); - } + bool valid = disable_family_check || + target->family == TOX_AF_INET || + target->family == TOX_AF_INET6; - to_host_family(target); - return 0; + return valid ? 0 : -1; } static void ipport_pack(uint8_t *data, const IP_Port *source) @@ -617,7 +616,9 @@ 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 != AF_INET && send_to.ip.family != AF_INET6) { + if (onion->recv_1_function && + send_to.ip.family != TOX_AF_INET && + send_to.ip.family != TOX_AF_INET6) { return onion->recv_1_function(onion->callback_object, send_to, packet + (1 + RETURN_1), data_len); } diff --git a/toxcore/onion.h b/toxcore/onion.h index 1dcaa0fe..e81b3a52 100644 --- a/toxcore/onion.h +++ b/toxcore/onion.h @@ -145,12 +145,12 @@ int send_onion_response(Networking_Core *net, IP_Port dest, const uint8_t *data, * * Used to handle these packets that are received in a non traditional way (by TCP for example). * - * Source family must be set to something else than AF_INET6 or AF_INET so that the callback gets called + * Source family must be set to something else than TOX_AF_INET6 or TOX_AF_INET so that the callback gets called * when the response is received. */ int onion_send_1(const Onion *onion, const uint8_t *plain, uint16_t len, IP_Port source, const uint8_t *nonce); -/* Set the callback to be called when the dest ip_port doesn't have AF_INET6 or AF_INET as the family. +/* Set the callback to be called when the dest ip_port doesn't have TOX_AF_INET6 or TOX_AF_INET as the family. * * Format: function(void *object, IP_Port dest, uint8_t *data, uint16_t length) */ diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 91b5b067..94e9c916 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -43,7 +43,7 @@ */ int onion_add_bs_path_node(Onion_Client *onion_c, IP_Port ip_port, const uint8_t *public_key) { - if (ip_port.ip.family != AF_INET && ip_port.ip.family != AF_INET6) { + if (ip_port.ip.family != TOX_AF_INET && ip_port.ip.family != TOX_AF_INET6) { return -1; } @@ -76,7 +76,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 != AF_INET && ip_port.ip.family != AF_INET6) { + if (ip_port.ip.family != TOX_AF_INET && ip_port.ip.family != TOX_AF_INET6) { return -1; } @@ -345,7 +345,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 == AF_INET || path->ip_port1.ip.family == AF_INET6) { + if (path->ip_port1.ip.family == TOX_AF_INET || path->ip_port1.ip.family == TOX_AF_INET6) { uint8_t packet[ONION_MAX_PACKET_SIZE]; int len = create_onion_packet(packet, sizeof(packet), path, dest, data, length); @@ -866,7 +866,7 @@ static int handle_dhtpk_announce(void *object, const uint8_t *source_pubkey, con for (i = 0; i < num_nodes; ++i) { uint8_t family = nodes[i].ip_port.ip.family; - if (family == AF_INET || family == AF_INET6) { + if (family == TOX_AF_INET || family == TOX_AF_INET6) { 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) { if (onion_c->friends_list[friend_num].tcp_relay_node_callback) { diff --git a/toxcore/ping.c b/toxcore/ping.c index 36c32c61..72b3fe62 100644 --- a/toxcore/ping.c +++ b/toxcore/ping.c @@ -249,7 +249,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 == AF_INET) { + if (ip_port.ip.family == TOX_AF_INET) { ipptp = &list[i].assoc4; } else { ipptp = &list[i].assoc6; diff --git a/toxcore/tox.c b/toxcore/tox.c index 572b4a84..db3ed4a5 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -246,7 +246,7 @@ bool tox_bootstrap(Tox *tox, const char *address, uint16_t port, const uint8_t * unsigned int i; for (i = 0; i < count; i++) { - root[i].port = htons(port); + root[i].port = net_htons(port); Messenger *m = tox; onion_add_bs_path_node(m->onion_c, root[i], public_key); @@ -290,7 +290,7 @@ bool tox_add_tcp_relay(Tox *tox, const char *address, uint16_t port, const uint8 unsigned int i; for (i = 0; i < count; i++) { - root[i].port = htons(port); + root[i].port = net_htons(port); Messenger *m = tox; add_tcp_relay(m->net_crypto, root[i], public_key);