diff --git a/auto_tests/auto_test_support.c b/auto_tests/auto_test_support.c index 213e70570ce..18c0f1f8658 100644 --- a/auto_tests/auto_test_support.c +++ b/auto_tests/auto_test_support.c @@ -36,34 +36,38 @@ static const struct BootstrapNodes { const uint8_t key[32]; } bootstrap_nodes[] = { { - "tox.abilinski.com", 33445, - 0x10, 0xC0, 0x0E, 0xB2, 0x50, 0xC3, 0x23, 0x3E, - 0x34, 0x3E, 0x2A, 0xEB, 0xA0, 0x71, 0x15, 0xA5, - 0xC2, 0x89, 0x20, 0xE9, 0xC8, 0xD2, 0x94, 0x92, - 0xF6, 0xD0, 0x0B, 0x29, 0x04, 0x9E, 0xDC, 0x7E, + "tox.abilinski.com", 33445, { + 0x10, 0xC0, 0x0E, 0xB2, 0x50, 0xC3, 0x23, 0x3E, + 0x34, 0x3E, 0x2A, 0xEB, 0xA0, 0x71, 0x15, 0xA5, + 0xC2, 0x89, 0x20, 0xE9, 0xC8, 0xD2, 0x94, 0x92, + 0xF6, 0xD0, 0x0B, 0x29, 0x04, 0x9E, 0xDC, 0x7E, + }, }, { - "tox.initramfs.io", 33445, - 0x02, 0x80, 0x7C, 0xF4, 0xF8, 0xBB, 0x8F, 0xB3, - 0x90, 0xCC, 0x37, 0x94, 0xBD, 0xF1, 0xE8, 0x44, - 0x9E, 0x9A, 0x83, 0x92, 0xC5, 0xD3, 0xF2, 0x20, - 0x00, 0x19, 0xDA, 0x9F, 0x1E, 0x81, 0x2E, 0x46, + "tox.initramfs.io", 33445, { + 0x02, 0x80, 0x7C, 0xF4, 0xF8, 0xBB, 0x8F, 0xB3, + 0x90, 0xCC, 0x37, 0x94, 0xBD, 0xF1, 0xE8, 0x44, + 0x9E, 0x9A, 0x83, 0x92, 0xC5, 0xD3, 0xF2, 0x20, + 0x00, 0x19, 0xDA, 0x9F, 0x1E, 0x81, 0x2E, 0x46, + }, }, { - "tox.plastiras.org", 33445, - 0x8E, 0x8B, 0x63, 0x29, 0x9B, 0x3D, 0x52, 0x0F, - 0xB3, 0x77, 0xFE, 0x51, 0x00, 0xE6, 0x5E, 0x33, - 0x22, 0xF7, 0xAE, 0x5B, 0x20, 0xA0, 0xAC, 0xED, - 0x29, 0x81, 0x76, 0x9F, 0xC5, 0xB4, 0x37, 0x25, + "tox.plastiras.org", 33445, { + 0x8E, 0x8B, 0x63, 0x29, 0x9B, 0x3D, 0x52, 0x0F, + 0xB3, 0x77, 0xFE, 0x51, 0x00, 0xE6, 0x5E, 0x33, + 0x22, 0xF7, 0xAE, 0x5B, 0x20, 0xA0, 0xAC, 0xED, + 0x29, 0x81, 0x76, 0x9F, 0xC5, 0xB4, 0x37, 0x25, + }, }, { - "tox.novg.net", 33445, - 0xD5, 0x27, 0xE5, 0x84, 0x7F, 0x83, 0x30, 0xD6, - 0x28, 0xDA, 0xB1, 0x81, 0x4F, 0x0A, 0x42, 0x2F, - 0x6D, 0xC9, 0xD0, 0xA3, 0x00, 0xE6, 0xC3, 0x57, - 0x63, 0x4E, 0xE2, 0xDA, 0x88, 0xC3, 0x54, 0x63, + "tox.novg.net", 33445, { + 0xD5, 0x27, 0xE5, 0x84, 0x7F, 0x83, 0x30, 0xD6, + 0x28, 0xDA, 0xB1, 0x81, 0x4F, 0x0A, 0x42, 0x2F, + 0x6D, 0xC9, 0xD0, 0xA3, 0x00, 0xE6, 0xC3, 0x57, + 0x63, 0x4E, 0xE2, 0xDA, 0x88, 0xC3, 0x54, 0x63, + }, }, - { nullptr, 0, 0 }, + { nullptr, 0, {0} }, }; void bootstrap_tox_live_network(Tox *tox, bool enable_tcp) diff --git a/auto_tests/network_test.c b/auto_tests/network_test.c index 76cb94a17ff..ef0c7ba2a00 100644 --- a/auto_tests/network_test.c +++ b/auto_tests/network_test.c @@ -36,7 +36,7 @@ static void test_addr_resolv_localhost(void) net_kill_strerror(strerror); Ip_Ntoa ip_str; - ck_assert_msg(net_family_is_ipv4(ip.family), "Expected family TOX_AF_INET, got %u.", ip.family.value); + ck_assert_msg(net_family_is_ipv4(ip.family), "Expected family NET_FAMILY_IPV4, 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.", net_ip_ntoa(&ip, &ip_str)); @@ -58,7 +58,7 @@ static void test_addr_resolv_localhost(void) ck_assert_msg(res, "Resolver failed: %d, %s", error, strerror); net_kill_strerror(strerror); - ck_assert_msg(net_family_is_ipv6(ip.family), "Expected family TOX_AF_INET6 (%d), got %u.", TOX_AF_INET6, + ck_assert_msg(net_family_is_ipv6(ip.family), "Expected family NET_FAMILY_IPV6 (%d), got %u.", NET_FAMILY_IPV6, ip.family.value); IP6 ip6_loopback = get_ip6_loopback(); ck_assert_msg(!memcmp(&ip.ip.v6, &ip6_loopback, sizeof(IP6)), "Expected ::1, got %s.", @@ -82,18 +82,18 @@ static void test_addr_resolv_localhost(void) net_kill_strerror(strerror); #if USE_IPV6 - ck_assert_msg(net_family_is_ipv6(ip.family), "Expected family TOX_AF_INET6 (%d), got %u.", TOX_AF_INET6, + ck_assert_msg(net_family_is_ipv6(ip.family), "Expected family NET_FAMILY_IPV6 (%d), got %u.", NET_FAMILY_IPV6, ip.family.value); ck_assert_msg(!memcmp(&ip.ip.v6, &ip6_loopback, sizeof(IP6)), "Expected ::1, got %s.", net_ip_ntoa(&ip, &ip_str)); - ck_assert_msg(net_family_is_ipv4(extra.family), "Expected family TOX_AF_INET (%d), got %u.", TOX_AF_INET, + ck_assert_msg(net_family_is_ipv4(extra.family), "Expected family NET_FAMILY_IPV4 (%d), got %u.", NET_FAMILY_IPV4, extra.family.value); ck_assert_msg(extra.ip.v4.uint32 == loopback, "Expected 127.0.0.1, got %s.", net_ip_ntoa(&ip, &ip_str)); #elif 0 // TODO(iphydf): Fix this to work on IPv6-supporting systems. - 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(net_family_is_ipv4(ip.family), "Expected family NET_FAMILY_IPV4 (%d), got %u.", NET_FAMILY_IPV4, ip.family.value); ck_assert_msg(ip.ip.v4.uint32 == loopback, "Expected 127.0.0.1, got %s.", net_ip_ntoa(&ip, &ip_str)); #endif @@ -119,20 +119,20 @@ static void test_ip_equal(void) 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} ): " + ck_assert_msg(res == 0, "ip_equal( {NET_FAMILY_IPV4, 127.0.0.1}, {NET_FAMILY_UNSPEC, 0} ): " "expected result 0, got %d.", res); ip2.family = net_family_ipv4(); ip2.ip.v4.uint32 = net_htonl(0x7F000001); res = ip_equal(&ip1, &ip2); - ck_assert_msg(res != 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_INET, 127.0.0.1} ): " + ck_assert_msg(res != 0, "ip_equal( {NET_FAMILY_IPV4, 127.0.0.1}, {NET_FAMILY_IPV4, 127.0.0.1} ): " "expected result != 0, got 0."); ip2.ip.v4.uint32 = net_htonl(0x7F000002); res = ip_equal(&ip1, &ip2); - ck_assert_msg(res == 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_INET, 127.0.0.2} ): " + ck_assert_msg(res == 0, "ip_equal( {NET_FAMILY_IPV4, 127.0.0.1}, {NET_FAMILY_IPV4, 127.0.0.2} ): " "expected result 0, got %d.", res); ip2.family = net_family_ipv6(); @@ -145,21 +145,21 @@ static void test_ip_equal(void) "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( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_INET6, ::ffff:127.0.0.1} ): " + ck_assert_msg(res != 0, "ip_equal( {NET_FAMILY_IPV4, 127.0.0.1}, {NET_FAMILY_IPV6, ::ffff:127.0.0.1} ): " "expected result != 0, got 0."); IP6 ip6_loopback = get_ip6_loopback(); memcpy(&ip2.ip.v6, &ip6_loopback, sizeof(IP6)); res = ip_equal(&ip1, &ip2); - ck_assert_msg(res == 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_INET6, ::1} ): expected result 0, got %d.", res); + ck_assert_msg(res == 0, "ip_equal( {NET_FAMILY_IPV4, 127.0.0.1}, {NET_FAMILY_IPV6, ::1} ): expected result 0, got %d.", res); memcpy(&ip1, &ip2, sizeof(IP)); res = ip_equal(&ip1, &ip2); - ck_assert_msg(res != 0, "ip_equal( {TOX_AF_INET6, ::1}, {TOX_AF_INET6, ::1} ): expected result != 0, got 0."); + ck_assert_msg(res != 0, "ip_equal( {NET_FAMILY_IPV6, ::1}, {NET_FAMILY_IPV6, ::1} ): expected result != 0, got 0."); ip2.ip.v6.uint8[15]++; res = ip_equal(&ip1, &ip2); - ck_assert_msg(res == 0, "ip_equal( {TOX_AF_INET6, ::1}, {TOX_AF_INET6, ::2} ): expected result 0, got %d.", res); + ck_assert_msg(res == 0, "ip_equal( {NET_FAMILY_IPV6, ::1}, {NET_FAMILY_IPV6, ::2} ): expected result 0, got %d.", res); } int main(void) diff --git a/other/bootstrap_node_packets.c b/other/bootstrap_node_packets.c index c250b2552bc..697ea9b25c4 100644 --- a/other/bootstrap_node_packets.c +++ b/other/bootstrap_node_packets.c @@ -21,7 +21,7 @@ static uint8_t bootstrap_motd[MAX_MOTD_LENGTH]; static uint16_t bootstrap_motd_length; /* To request this packet just send a packet of length INFO_REQUEST_PACKET_LENGTH - * with the first byte being BOOTSTRAP_INFO_PACKET_ID + * with the first byte being NET_PACKET_BOOTSTRAP_INFO */ static int handle_info_request(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length, void *userdata) @@ -33,7 +33,7 @@ static int handle_info_request(void *object, const IP_Port *source, const uint8_ const Networking_Core *nc = (const Networking_Core *)object; uint8_t data[1 + sizeof(bootstrap_version) + MAX_MOTD_LENGTH]; - data[0] = BOOTSTRAP_INFO_PACKET_ID; + data[0] = NET_PACKET_BOOTSTRAP_INFO; memcpy(data + 1, &bootstrap_version, sizeof(bootstrap_version)); const uint16_t len = 1 + sizeof(bootstrap_version) + bootstrap_motd_length; memcpy(data + 1 + sizeof(bootstrap_version), bootstrap_motd, bootstrap_motd_length); @@ -55,6 +55,6 @@ int bootstrap_set_callbacks(Networking_Core *net, uint32_t version, const uint8_ memcpy(bootstrap_motd, motd, motd_length); bootstrap_motd_length = motd_length; - networking_registerhandler(net, BOOTSTRAP_INFO_PACKET_ID, &handle_info_request, net); + networking_registerhandler(net, NET_PACKET_BOOTSTRAP_INFO, &handle_info_request, net); return 0; } diff --git a/toxcore/DHT_test.cc b/toxcore/DHT_test.cc index 1028fc7f18c..6fba834239a 100644 --- a/toxcore/DHT_test.cc +++ b/toxcore/DHT_test.cc @@ -116,7 +116,7 @@ TEST(AddToList, OverridesKeysWithCloserKeys) std::array nodes{}; - IP_Port ip_port = {0}; + IP_Port ip_port = {{{NET_FAMILY_UNSPEC}}}; EXPECT_TRUE(add_to_list(nodes.data(), nodes.size(), keys[0].data(), &ip_port, self_pk.data())); EXPECT_TRUE(add_to_list(nodes.data(), nodes.size(), keys[1].data(), &ip_port, self_pk.data())); EXPECT_TRUE(add_to_list(nodes.data(), nodes.size(), keys[2].data(), &ip_port, self_pk.data())); @@ -355,7 +355,7 @@ TEST(AnnounceNodes, SetAndTest) PublicKey pk2(to_array(pk_data)); ASSERT_NE(pk2, pk1); - IP_Port ip_port = {0}; + IP_Port ip_port = {{{NET_FAMILY_UNSPEC}}}; ip_port.ip.family = net_family_ipv4(); set_announce_node(dht.get(), pk1.data()); diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c index aead9759116..701194edf9b 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c @@ -134,7 +134,7 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns) * so it's wrapped in `__linux__` for now. * Definitely won't work like this on Windows... */ - const Socket sock = net_socket(ns, net_family_ipv4(), TOX_SOCK_STREAM, 0); + const Socket sock = net_socket(ns, net_family_ipv4(), TOX_SOCK_STREAM, TOX_PROTO_TCP); if (!sock_valid(sock)) { free(broadcast); diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c index c7161a92894..7892feb6369 100644 --- a/toxcore/TCP_connection.c +++ b/toxcore/TCP_connection.c @@ -1475,7 +1475,7 @@ static bool copy_tcp_relay_conn(const TCP_Connections *tcp_c, Node_format *tcp_r /** @brief Copy a maximum of max_num TCP relays we are connected to to tcp_relays. * - * NOTE that the family of the copied ip ports will be set to TCP_INET or TCP_INET6. + * NOTE that the family of the copied ip ports will be set to NET_FAMILY_TCP_IPV4 or NET_FAMILY_TCP_IPV6. * * return number of relays copied to tcp_relays on success. * return 0 on failure. diff --git a/toxcore/TCP_connection.h b/toxcore/TCP_connection.h index 2d35919f10e..ea2c536916a 100644 --- a/toxcore/TCP_connection.h +++ b/toxcore/TCP_connection.h @@ -278,7 +278,7 @@ int add_tcp_relay_global(TCP_Connections *tcp_c, const IP_Port *ip_port, const u /** @brief Copy a maximum of max_num TCP relays we are connected to to tcp_relays. * - * NOTE that the family of the copied ip ports will be set to TCP_INET or TCP_INET6. + * NOTE that the family of the copied ip ports will be set to NET_FAMILY_TCP_IPV4 or NET_FAMILY_TCP_IPV6. * * return number of relays copied to tcp_relays on success. * return 0 on failure. diff --git a/toxcore/group_announce_test.cc b/toxcore/group_announce_test.cc index 2918dc76c67..e50bd627f94 100644 --- a/toxcore/group_announce_test.cc +++ b/toxcore/group_announce_test.cc @@ -215,7 +215,7 @@ TEST_F(AnnouncesPack, AnnouncesValidationCheck) EXPECT_EQ(gca_is_valid_announce(nullptr), false); #endif - GC_Announce announce = {0}; + GC_Announce announce = {{{{0}}}}; EXPECT_EQ(gca_is_valid_announce(&announce), false); EXPECT_EQ(gca_is_valid_announce(&announces_[0]), true); EXPECT_EQ(gca_is_valid_announce(&announces_[1]), true); diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 1680b078b72..dfac7e8e968 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -2381,7 +2381,7 @@ int send_tcp_forward_request(const Logger *logger, Net_Crypto *c, const IP_Port /** @brief Copy a maximum of num random TCP relays we are connected to to tcp_relays. * - * NOTE that the family of the copied ip ports will be set to TCP_INET or TCP_INET6. + * NOTE that the family of the copied ip ports will be set to NET_FAMILY_TCP_IPV4 or NET_FAMILY_TCP_IPV6. * * return number of relays copied to tcp_relays on success. * return 0 on failure. diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index 0d817e43151..dd4d89592e3 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h @@ -345,7 +345,7 @@ int send_tcp_forward_request(const Logger *logger, Net_Crypto *c, const IP_Port /** @brief Copy a maximum of num random TCP relays we are connected to to tcp_relays. * - * NOTE that the family of the copied ip ports will be set to TCP_INET or TCP_INET6. + * NOTE that the family of the copied ip ports will be set to NET_FAMILY_TCP_IPV4 or NET_FAMILY_TCP_IPV6. * * return number of relays copied to tcp_relays on success. * return 0 on failure. diff --git a/toxcore/network.c b/toxcore/network.c index 55aa4e28184..29e43ec27b8 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -231,7 +231,7 @@ static_assert(TOX_INET6_ADDRSTRLEN >= INET6_ADDRSTRLEN, static_assert(TOX_INET_ADDRSTRLEN >= INET_ADDRSTRLEN, "TOX_INET_ADDRSTRLEN should be greater or equal to INET_ADDRSTRLEN (#INET_ADDRSTRLEN)"); -static int make_proto(int proto) +static int make_proto(Tox_Proto proto) { switch (proto) { case TOX_PROTO_TCP: @@ -245,7 +245,7 @@ static int make_proto(int proto) } } -static int make_socktype(int type) +static int make_socktype(Tox_Sock_Type type) { switch (type) { case TOX_SOCK_STREAM: @@ -261,30 +261,31 @@ static int make_socktype(int type) static int make_family(Family tox_family) { - switch (tox_family.value) { - case TOX_AF_INET: + const uint8_t family = family_to_int(tox_family); + switch (family) { + case NET_FAMILY_IPV4: return AF_INET; - case TOX_AF_INET6: + case NET_FAMILY_IPV6: return AF_INET6; - case TOX_AF_UNSPEC: + case NET_FAMILY_UNSPEC: return AF_UNSPEC; default: - return tox_family.value; + return family; } } -static const Family family_unspec = {TOX_AF_UNSPEC}; -static const Family family_ipv4 = {TOX_AF_INET}; -static const Family family_ipv6 = {TOX_AF_INET6}; -static const Family family_tcp_server = {TCP_SERVER_FAMILY}; -static const Family family_tcp_client = {TCP_CLIENT_FAMILY}; -static const Family family_tcp_ipv4 = {TCP_INET}; -static const Family family_tcp_ipv6 = {TCP_INET6}; -static const Family family_tox_tcp_ipv4 = {TOX_TCP_INET}; -static const Family family_tox_tcp_ipv6 = {TOX_TCP_INET6}; +static const Family family_unspec = {(force Family_Value)NET_FAMILY_UNSPEC}; +static const Family family_ipv4 = {(force Family_Value)NET_FAMILY_IPV4}; +static const Family family_ipv6 = {(force Family_Value)NET_FAMILY_IPV6}; +static const Family family_tcp_server = {(force Family_Value)NET_FAMILY_TCP_SERVER}; +static const Family family_tcp_client = {(force Family_Value)NET_FAMILY_TCP_CLIENT}; +static const Family family_tcp_ipv4 = {(force Family_Value)NET_FAMILY_TCP_IPV4}; +static const Family family_tcp_ipv6 = {(force Family_Value)NET_FAMILY_TCP_IPV6}; +static const Family family_tox_tcp_ipv4 = {(force Family_Value)NET_FAMILY_TOX_TCP_IPV4}; +static const Family family_tox_tcp_ipv6 = {(force Family_Value)NET_FAMILY_TOX_TCP_IPV6}; static const Family *make_tox_family(int family) { @@ -303,6 +304,63 @@ static const Family *make_tox_family(int family) } } +bool net_family_from_int(uint8_t family, Net_Family *out) +{ + switch (family) { + case NET_FAMILY_UNSPEC: { + *out = NET_FAMILY_UNSPEC; + return true; + } + case NET_FAMILY_IPV4: { + *out = NET_FAMILY_IPV4; + return true; + } + case NET_FAMILY_IPV6: { + *out = NET_FAMILY_IPV6; + return true; + } + case NET_FAMILY_TOX_TCP_IPV4: { + *out = NET_FAMILY_TOX_TCP_IPV4; + return true; + } + case NET_FAMILY_TOX_TCP_IPV6: { + *out = NET_FAMILY_TOX_TCP_IPV6; + return true; + } + case NET_FAMILY_TCP_CLIENT: { + *out = NET_FAMILY_TCP_CLIENT; + return true; + } + case NET_FAMILY_TCP_IPV4: { + *out = NET_FAMILY_TCP_IPV4; + return true; + } + case NET_FAMILY_TCP_IPV6: { + *out = NET_FAMILY_TCP_IPV6; + return true; + } + case NET_FAMILY_TCP_SERVER: { + *out = NET_FAMILY_TCP_SERVER; + return true; + } + default: { + *out = NET_FAMILY_UNSPEC; + return false; + } + } +} + +Family family_from_net_family(Net_Family value) +{ + const Family family = {(force Family_Value)value}; + return family; +} + +uint8_t family_to_int(Family family) +{ + return (force uint8_t)family.value; +} + non_null() static void get_ip4(IP4 *result, const struct in_addr *addr) { @@ -770,7 +828,7 @@ static const char *net_packet_type_name(Net_Packet_Type type) case NET_PACKET_STORE_ANNOUNCE_RESPONSE: return "STORE_ANNOUNCE_RESPONSE"; - case BOOTSTRAP_INFO_PACKET_ID: + case NET_PACKET_BOOTSTRAP_INFO: return "BOOTSTRAP_INFO"; case NET_PACKET_MAX: @@ -946,7 +1004,7 @@ int send_packet(const Networking_Core *net, const IP_Port *ip_port, Packet packe return -1; } - /* socket TOX_AF_INET, but target IP NOT: can't send */ + /* socket NET_FAMILY_IPV4, but target IP NOT: can't send */ if (net_family_is_ipv4(net->family) && !net_family_is_ipv4(ipp_copy.ip.family)) { // TODO(iphydf): Make this an error. Occasionally we try to send to an // all-zero ip_port. @@ -1460,25 +1518,30 @@ static int ip6_cmp(const IP6 *a, const IP6 *b) return cmp_uint(a->uint64[1], b->uint64[1]); } +static int family_cmp(Family a, Family b) +{ + return cmp_uint(family_to_int(a), family_to_int(b)); +} + non_null() static int ip_cmp(const IP *a, const IP *b) { - const int res = cmp_uint(a->family.value, b->family.value); + const int res = family_cmp(a->family, b->family); if (res != 0) { return res; } - switch (a->family.value) { - case TOX_AF_UNSPEC: + switch (family_to_int(a->family)) { + case NET_FAMILY_UNSPEC: return 0; - case TOX_AF_INET: - case TCP_INET: - case TOX_TCP_INET: + case NET_FAMILY_IPV4: + case NET_FAMILY_TCP_IPV4: + case NET_FAMILY_TOX_TCP_IPV4: return ip4_cmp(&a->ip.v4, &b->ip.v4); - case TOX_AF_INET6: - case TCP_INET6: - case TOX_TCP_INET6: - case TCP_SERVER_FAMILY: // these happen to be ipv6 according to TCP_server.c. - case TCP_CLIENT_FAMILY: + case NET_FAMILY_IPV6: + case NET_FAMILY_TCP_IPV6: + case NET_FAMILY_TOX_TCP_IPV6: + case NET_FAMILY_TCP_SERVER: // these happen to be ipv6 according to TCP_server.c. + case NET_FAMILY_TCP_CLIENT: return ip6_cmp(&a->ip.v6, &b->ip.v6); } // Invalid, we don't compare any further and consider them equal. @@ -1616,16 +1679,16 @@ bool bin_pack_ip_port(Bin_Pack *bp, const Logger *logger, const IP_Port *ip_port if (net_family_is_ipv4(ip_port->ip.family)) { // TODO(irungentoo): use functions to convert endianness is_ipv4 = true; - family = TOX_AF_INET; + family = NET_FAMILY_IPV4; } else if (net_family_is_tcp_ipv4(ip_port->ip.family)) { is_ipv4 = true; - family = TOX_TCP_INET; + family = NET_FAMILY_TOX_TCP_IPV4; } else if (net_family_is_ipv6(ip_port->ip.family)) { is_ipv4 = false; - family = TOX_AF_INET6; + family = NET_FAMILY_IPV6; } else if (net_family_is_tcp_ipv6(ip_port->ip.family)) { is_ipv4 = false; - family = TOX_TCP_INET6; + family = NET_FAMILY_TOX_TCP_IPV6; } else { Ip_Ntoa ip_str; // TODO(iphydf): Find out why we're trying to pack invalid IPs, stop @@ -1671,20 +1734,20 @@ int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, bool bool is_ipv4; Family host_family; - if (data[0] == TOX_AF_INET) { + if (data[0] == NET_FAMILY_IPV4) { is_ipv4 = true; host_family = net_family_ipv4(); - } else if (data[0] == TOX_TCP_INET) { + } else if (data[0] == NET_FAMILY_TOX_TCP_IPV4) { if (!tcp_enabled) { return -1; } is_ipv4 = true; host_family = net_family_tcp_ipv4(); - } else if (data[0] == TOX_AF_INET6) { + } else if (data[0] == NET_FAMILY_IPV6) { is_ipv4 = false; host_family = net_family_ipv6(); - } else if (data[0] == TOX_TCP_INET6) { + } else if (data[0] == NET_FAMILY_TOX_TCP_IPV6) { if (!tcp_enabled) { return -1; } @@ -1807,13 +1870,13 @@ bool addr_parse_ip(const char *address, IP *to) * * @param address a hostname (or something parseable to an IP address) * @param to to.family MUST be initialized, either set to a specific IP version - * (TOX_AF_INET/TOX_AF_INET6) or to the unspecified TOX_AF_UNSPEC (0), if both + * (NET_FAMILY_IPV4/NET_FAMILY_IPV6) or to the unspecified NET_FAMILY_UNSPEC (0), if both * IP versions are acceptable * @param 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 TOX_AF_UNSPEC and both available - * Returns in `*extra` an IPv4 address, if family was TOX_AF_UNSPEC and `*to` is TOX_AF_INET6 + * prefers v6 if `ip.family` was NET_FAMILY_UNSPEC and both available + * Returns in `*extra` an IPv4 address, if family was NET_FAMILY_UNSPEC and `*to` is NET_FAMILY_IPV6 * * @return 0 on failure, `TOX_ADDR_RESOLVE_*` on success. */ @@ -1974,7 +2037,7 @@ bool net_connect(const Memory *mem, const Logger *log, Socket sock, const IP_Por return true; } -int32_t net_getipport(const Memory *mem, const char *node, IP_Port **res, int tox_type) +int32_t net_getipport(const Memory *mem, const char *node, IP_Port **res, Tox_Sock_Type tox_type) { assert(node != nullptr); @@ -2113,7 +2176,7 @@ bool bind_to_port(const Network *ns, Socket sock, Family family, uint16_t port) return net_bind(ns, sock, &addr) == 0; } -Socket net_socket(const Network *ns, Family domain, int type, int protocol) +Socket net_socket(const Network *ns, Family domain, Tox_Sock_Type type, Tox_Proto protocol) { const int platform_domain = make_family(domain); const int platform_type = make_socktype(type); diff --git a/toxcore/network.h b/toxcore/network.h index 06857b89179..3cb29f0c714 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -81,10 +81,33 @@ typedef struct Network { const Network *os_network(void); +typedef enum Net_Family { + /** Redefinitions of variables for safe transfer over wire. */ + NET_FAMILY_UNSPEC = 0, + NET_FAMILY_IPV4 = 2, + NET_FAMILY_IPV6 = 10, + NET_FAMILY_TOX_TCP_IPV4 = 130, + NET_FAMILY_TOX_TCP_IPV6 = 138, + + /** TCP related */ + NET_FAMILY_TCP_CLIENT = NET_FAMILY_IPV6 + 1, + NET_FAMILY_TCP_IPV4 = NET_FAMILY_IPV6 + 2, + NET_FAMILY_TCP_IPV6 = NET_FAMILY_IPV6 + 3, + NET_FAMILY_TCP_SERVER = NET_FAMILY_IPV6 + 4, +} Net_Family; + +typedef bitwise uint8_t Family_Value; + typedef struct Family { - uint8_t value; + Family_Value value; } Family; +non_null() +bool net_family_from_int(uint8_t family, Net_Family *out); + +Family family_from_net_family(Net_Family value); +uint8_t family_to_int(Family family); + bool net_family_is_unspec(Family family); bool net_family_is_ipv4(Family family); bool net_family_is_ipv6(Family family); @@ -151,7 +174,7 @@ typedef enum Net_Packet_Type { NET_PACKET_STORE_ANNOUNCE_REQUEST = 0x97, NET_PACKET_STORE_ANNOUNCE_RESPONSE = 0x98, - BOOTSTRAP_INFO_PACKET_ID = 0xf0, /* Only used for bootstrap nodes */ + NET_PACKET_BOOTSTRAP_INFO = 0xf0, /* Only used for bootstrap nodes */ NET_PACKET_MAX = 0xff, /* This type must remain within a single uint8. */ } Net_Packet_Type; @@ -160,24 +183,15 @@ typedef enum Net_Packet_Type { #define TOX_PORTRANGE_TO 33545 #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 -#define TOX_TCP_INET6 138 - -#define TOX_SOCK_STREAM 1 -#define TOX_SOCK_DGRAM 2 - -#define TOX_PROTO_TCP 1 -#define TOX_PROTO_UDP 2 +typedef enum Tox_Sock_Type { + TOX_SOCK_STREAM = 1, + TOX_SOCK_DGRAM = 2, +} Tox_Sock_Type; -/** TCP related */ -#define TCP_CLIENT_FAMILY (TOX_AF_INET6 + 1) -#define TCP_INET (TOX_AF_INET6 + 2) -#define TCP_INET6 (TOX_AF_INET6 + 3) -#define TCP_SERVER_FAMILY (TOX_AF_INET6 + 4) +typedef enum Tox_Proto { + TOX_PROTO_TCP = 1, + TOX_PROTO_UDP = 2, +} Tox_Proto; #define SIZE_IP4 4 #define SIZE_IP6 16 @@ -220,7 +234,7 @@ typedef struct IP_Port { } IP_Port; non_null() -Socket net_socket(const Network *ns, Family domain, int type, int protocol); +Socket net_socket(const Network *ns, Family domain, Tox_Sock_Type type, Tox_Proto protocol); /** * Check if socket is valid. @@ -316,10 +330,10 @@ const char *net_ip_ntoa(const IP *ip, Ip_Ntoa *ip_str); /** * Parses IP structure into an address string. * - * @param ip IP of TOX_AF_INET or TOX_AF_INET6 families. + * @param ip IP of NET_FAMILY_IPV4 or NET_FAMILY_IPV6 families. * @param length length of the address buffer. - * Must be at least TOX_INET_ADDRSTRLEN for TOX_AF_INET - * and TOX_INET6_ADDRSTRLEN for TOX_AF_INET6 + * Must be at least TOX_INET_ADDRSTRLEN for NET_FAMILY_IPV4 + * and TOX_INET6_ADDRSTRLEN for NET_FAMILY_IPV6 * * @param address dotted notation (IPv4: quad, IPv6: 16) or colon notation (IPv6). * @@ -400,12 +414,12 @@ void ipport_copy(IP_Port *target, const IP_Port *source); * * @param address a hostname (or something parseable to an IP address) * @param to to.family MUST be initialized, either set to a specific IP version - * (TOX_AF_INET/TOX_AF_INET6) or to the unspecified TOX_AF_UNSPEC (0), if both + * (NET_FAMILY_IPV4/NET_FAMILY_IPV6) or to the unspecified NET_FAMILY_UNSPEC (0), if both * IP versions are acceptable * @param extra can be NULL and is only set in special circumstances, see returns * * Returns in `*to` a matching address (IPv6 or IPv4) - * Returns in `*extra`, if not NULL, an IPv4 address, if `to->family` was TOX_AF_UNSPEC + * Returns in `*extra`, if not NULL, an IPv4 address, if `to->family` was NET_FAMILY_UNSPEC * * @return true on success, false on failure */ @@ -517,7 +531,7 @@ bool net_connect(const Memory *mem, const Logger *log, Socket sock, const IP_Por * @retval -1 on error. */ non_null() -int32_t net_getipport(const Memory *mem, const char *node, IP_Port **res, int tox_type); +int32_t net_getipport(const Memory *mem, const char *node, IP_Port **res, Tox_Sock_Type tox_type); /** Deallocates memory allocated by net_getipport */ non_null(1) nullable(2) diff --git a/toxcore/network_test.cc b/toxcore/network_test.cc index 8969c7d8541..a9fa6d800f4 100644 --- a/toxcore/network_test.cc +++ b/toxcore/network_test.cc @@ -27,18 +27,6 @@ TEST(IpNtoa, DoesntWriteOutOfBounds) EXPECT_LT(std::string(ip_str.buf).length(), IP_NTOA_LEN); } -TEST(IpNtoa, ReportsInvalidIpFamily) -{ - Ip_Ntoa ip_str; - IP ip; - ip.family.value = 255 - net_family_ipv6().value; - ip.ip.v4.uint32 = 0; - - net_ip_ntoa(&ip, &ip_str); - - EXPECT_EQ(std::string(ip_str.buf), "(IP invalid, family 245)"); -} - TEST(IpNtoa, FormatsIPv4) { Ip_Ntoa ip_str; @@ -86,8 +74,8 @@ TEST(IpportCmp, BehavesLikeMemcmp) { auto cmp_val = [](int val) { return val < 0 ? -1 : val > 0 ? 1 : 0; }; - IP_Port a = {0}; - IP_Port b = {0}; + IP_Port a = {{{NET_FAMILY_UNSPEC}}}; + IP_Port b = {{{NET_FAMILY_UNSPEC}}}; a.ip.family = net_family_ipv4(); b.ip.family = net_family_ipv4(); @@ -121,8 +109,8 @@ TEST(IpportCmp, BehavesLikeMemcmp) TEST(IpportCmp, Ipv6BeginAndEndCompareCorrectly) { - IP_Port a = {0}; - IP_Port b = {0}; + IP_Port a = {{{NET_FAMILY_UNSPEC}}}; + IP_Port b = {{{NET_FAMILY_UNSPEC}}}; a.ip.family = net_family_ipv6(); b.ip.family = net_family_ipv6(); @@ -142,8 +130,8 @@ TEST(IpportCmp, Ipv6BeginAndEndCompareCorrectly) TEST(IpportCmp, UnspecAlwaysComparesEqual) { - IP_Port a = {0}; - IP_Port b = {0}; + IP_Port a = {{{NET_FAMILY_UNSPEC}}}; + IP_Port b = {{{NET_FAMILY_UNSPEC}}}; a.ip.family = net_family_unspec(); b.ip.family = net_family_unspec(); @@ -154,18 +142,4 @@ TEST(IpportCmp, UnspecAlwaysComparesEqual) EXPECT_EQ(ipport_cmp_handler(&a, &b, sizeof(IP_Port)), 0); } -TEST(IpportCmp, InvalidAlwaysComparesEqual) -{ - IP_Port a = {0}; - IP_Port b = {0}; - - a.ip.family.value = 0xff; - b.ip.family.value = 0xff; - - a.ip.ip.v4.uint8[0] = 0xab; - b.ip.ip.v4.uint8[0] = 0xba; - - EXPECT_EQ(ipport_cmp_handler(&a, &b, sizeof(IP_Port)), 0); -} - } // namespace diff --git a/toxcore/network_test_util.hh b/toxcore/network_test_util.hh index 70f238aa929..dc4ca0d8c42 100644 --- a/toxcore/network_test_util.hh +++ b/toxcore/network_test_util.hh @@ -84,7 +84,6 @@ public: }; bool operator==(Family a, Family b); - bool operator==(IP4 a, IP4 b); bool operator==(IP6 a, IP6 b); bool operator==(IP const &a, IP const &b); diff --git a/toxcore/onion.c b/toxcore/onion.c index 9fab57af90a..6281349ee48 100644 --- a/toxcore/onion.c +++ b/toxcore/onion.c @@ -51,7 +51,7 @@ static void change_symmetric_key(Onion *onion) non_null() static void ip_pack_to_bytes(uint8_t *data, const IP *source) { - data[0] = source->family.value; + data[0] = family_to_int(source->family); if (net_family_is_ipv4(source->family) || net_family_is_tox_tcp_ipv4(source->family)) { memzero(data + 1, SIZE_IP6); @@ -69,8 +69,12 @@ static int ip_unpack_from_bytes(IP *target, const uint8_t *data, unsigned int da return -1; } - // TODO(iphydf): Validate input. - target->family.value = data[0]; + Net_Family family; + if (!net_family_from_int(data[0], &family)) { + return -1; + } + + target->family = family_from_net_family(family); if (net_family_is_ipv4(target->family) || net_family_is_tox_tcp_ipv4(target->family)) { memcpy(target->ip.v4.uint8, data + 1, SIZE_IP4); diff --git a/toxcore/onion.h b/toxcore/onion.h index a5d3554e7b0..9c732472f63 100644 --- a/toxcore/onion.h +++ b/toxcore/onion.h @@ -141,13 +141,13 @@ int send_onion_response(const Logger *log, const Networking_Core *net, * * 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 TOX_AF_INET6 or TOX_AF_INET so that the callback gets called + * Source family must be set to something else than NET_FAMILY_IPV6 or NET_FAMILY_IPV4 so that the callback gets called * when the response is received. */ non_null() int onion_send_1(const Onion *onion, const uint8_t *plain, uint16_t len, const IP_Port *source, const uint8_t *nonce); -/** Set the callback to be called when the dest ip_port doesn't have TOX_AF_INET6 or TOX_AF_INET as the family. */ +/** Set the callback to be called when the dest ip_port doesn't have NET_FAMILY_IPV6 or NET_FAMILY_IPV4 as the family. */ non_null(1) nullable(2, 3) void set_callback_handle_recv_1(Onion *onion, onion_recv_1_cb *function, void *object);