diff --git a/auto_tests/forwarding_test.c b/auto_tests/forwarding_test.c index 75e330a73e9..9cad1d3715a 100644 --- a/auto_tests/forwarding_test.c +++ b/auto_tests/forwarding_test.c @@ -126,7 +126,7 @@ static Forwarding_Subtox *new_forwarding_subtox(const Memory *mem, bool no_udp, subtox->dht = new_dht(subtox->log, mem, rng, ns, subtox->mono_time, subtox->net, true, true); - const TCP_Proxy_Info inf = {{{{0}}}}; + const TCP_Proxy_Info inf = {{{0}}}; subtox->c = new_net_crypto(subtox->log, mem, rng, ns, subtox->mono_time, subtox->dht, &inf); subtox->forwarding = new_forwarding(subtox->log, rng, subtox->mono_time, subtox->dht); diff --git a/auto_tests/network_test.c b/auto_tests/network_test.c index 76cb94a17ff..b55bdb9e5c2 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); 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,8 +58,8 @@ 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, - ip.family.value); + ck_assert_msg(net_family_is_ipv6(ip.family), "Expected family NET_FAMILY_IPV6 (%d), got %u.", NET_FAMILY_IPV6, + ip.family); IP6 ip6_loopback = get_ip6_loopback(); ck_assert_msg(!memcmp(&ip.ip.v6, &ip6_loopback, sizeof(IP6)), "Expected ::1, got %s.", net_ip_ntoa(&ip, &ip_str)); @@ -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, - ip.family.value); + ck_assert_msg(net_family_is_ipv6(ip.family), "Expected family NET_FAMILY_IPV6 (%d), got %u.", NET_FAMILY_IPV6, + ip.family); 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, - extra.family.value); + ck_assert_msg(net_family_is_ipv4(extra.family), "Expected family NET_FAMILY_IPV4 (%d), got %u.", NET_FAMILY_IPV4, + extra.family); 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); 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/auto_tests/onion_test.c b/auto_tests/onion_test.c index 3a81df41b8f..0815a9bcdc1 100644 --- a/auto_tests/onion_test.c +++ b/auto_tests/onion_test.c @@ -471,7 +471,7 @@ static Onions *new_onions(const Memory *mem, const Random *rng, uint16_t port, u return nullptr; } - TCP_Proxy_Info inf = {{{{0}}}}; + TCP_Proxy_Info inf = {{{0}}}; on->onion_c = new_onion_client(on->log, mem, rng, on->mono_time, new_net_crypto(on->log, mem, rng, ns, on->mono_time, dht, &inf)); if (!on->onion_c) { 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.c b/toxcore/DHT.c index 2567d1b5a8f..c427fdcd1cb 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -606,7 +606,7 @@ static bool client_or_ip_port_in_list(const Logger *log, const Mono_Time *mono_t LOGGER_DEBUG(log, "coipil[%u]: switching public_key (ipv%d)", index, ip_version); /* kill the other address, if it was set */ - const IPPTsPng empty_ipptspng = {{{{0}}}}; + const IPPTsPng empty_ipptspng = {{{0}}}; *assoc = empty_ipptspng; return true; } @@ -964,7 +964,7 @@ static void update_client_with_reset(const Mono_Time *mono_time, Client_data *cl ipptp_write->ret_ip_self = false; /* zero out other address */ - const IPPTsPng empty_ipptp = {{{{0}}}}; + const IPPTsPng empty_ipptp = {{{0}}}; *ipptp_clear = empty_ipptp; } diff --git a/toxcore/DHT_test.cc b/toxcore/DHT_test.cc index 1028fc7f18c..20dd9beea90 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_client.c b/toxcore/TCP_client.c index 2b8c6e448f5..34d7840ea06 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -594,7 +594,7 @@ TCP_Client_Connection *new_tcp_connection( return nullptr; } - const TCP_Proxy_Info default_proxyinfo = {{{{0}}}}; + const TCP_Proxy_Info default_proxyinfo = {{{0}}}; if (proxy_info == nullptr) { proxy_info = &default_proxyinfo; diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c index c7161a92894..096420271fc 100644 --- a/toxcore/TCP_connection.c +++ b/toxcore/TCP_connection.c @@ -578,7 +578,7 @@ void set_forwarding_packet_tcp_connection_callback(TCP_Connections *tcp_c, */ IP_Port tcp_connections_number_to_ip_port(unsigned int tcp_connections_number) { - IP_Port ip_port = {{{0}}}; + IP_Port ip_port = {{0}}; ip_port.ip.family = net_family_tcp_server(); ip_port.ip.ip.v6.uint32[0] = tcp_connections_number; return ip_port; @@ -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/TCP_server.c b/toxcore/TCP_server.c index 1363d902207..70831cbff9a 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c @@ -355,7 +355,7 @@ static int handle_tcp_handshake(const Logger *logger, TCP_Secure_Connection *con return -1; } - const IP_Port ipp = {{{0}}}; + const IP_Port ipp = {{0}}; if (TCP_SERVER_HANDSHAKE_SIZE != net_send(con->con.ns, logger, con->con.sock, response, TCP_SERVER_HANDSHAKE_SIZE, &ipp)) { crypto_memzero(shared_key, sizeof(shared_key)); @@ -584,7 +584,7 @@ static int rm_connection_index(TCP_Server *tcp_server, TCP_Secure_Connection *co */ static IP_Port con_id_to_ip_port(uint32_t con_id, uint64_t identifier) { - IP_Port ip_port = {{{0}}}; + IP_Port ip_port = {{0}}; ip_port.ip.family = net_family_tcp_client(); ip_port.ip.ip.v6.uint32[0] = con_id; ip_port.ip.ip.v6.uint64[1] = identifier; @@ -919,7 +919,7 @@ static Socket new_listening_tcp_socket(const Logger *logger, const Network *ns, const Socket sock = net_socket(ns, family, TOX_SOCK_STREAM, TOX_PROTO_TCP); if (!sock_valid(sock)) { - LOGGER_ERROR(logger, "TCP socket creation failed (family = %d)", family.value); + LOGGER_ERROR(logger, "TCP socket creation failed (family = %d)", family); return net_invalid_socket(); } @@ -938,7 +938,7 @@ static Socket new_listening_tcp_socket(const Logger *logger, const Network *ns, if (!ok) { char *const error = net_new_strerror(net_error()); LOGGER_WARNING(logger, "could not bind to TCP port %d (family = %d): %s", - port, family.value, error != nullptr ? error : "(null)"); + port, family, error != nullptr ? error : "(null)"); net_kill_strerror(error); kill_sock(ns, sock); return net_invalid_socket(); diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c index fae54c62122..c113107270e 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c @@ -2491,7 +2491,7 @@ static int handle_gc_ping(GC_Chat *chat, GC_Connection *gconn, const uint8_t *da do_gc_peer_state_sync(chat, gconn, data, length); if (length > GC_PING_PACKET_MIN_DATA_SIZE) { - IP_Port ip_port = {{{0}}}; + IP_Port ip_port = {{0}}; if (unpack_ip_port(&ip_port, data + GC_PING_PACKET_MIN_DATA_SIZE, length - GC_PING_PACKET_MIN_DATA_SIZE, false) > 0) { diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 1680b078b72..798422c7958 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -624,7 +624,7 @@ static int add_ip_port_connection(Net_Crypto *c, int crypt_connection_id, const non_null() static IP_Port return_ip_port_connection(const Net_Crypto *c, int crypt_connection_id) { - const IP_Port empty = {{{0}}}; + const IP_Port empty = {{0}}; const Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); @@ -2023,7 +2023,7 @@ static int handle_new_connection_handshake(Net_Crypto *c, const IP_Port *source, return -1; } - New_Connection n_c = {{{{0}}}}; + New_Connection n_c = {{{0}}}; n_c.cookie = cookie; n_c.source = *source; n_c.cookie_length = COOKIE_LENGTH; @@ -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 c33c1cdac75..f86ce1ba346 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,32 @@ 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}; +// TODO(https://github.com/TokTok/c-toxcore/pull/2595): Remove casts. +static const Family family_unspec = (force Family)NET_FAMILY_UNSPEC; +static const Family family_ipv4 = (force Family)NET_FAMILY_IPV4; +static const Family family_ipv6 = (force Family)NET_FAMILY_IPV6; +static const Family family_tcp_server = (force Family)NET_FAMILY_TCP_SERVER; +static const Family family_tcp_client = (force Family)NET_FAMILY_TCP_CLIENT; +static const Family family_tcp_ipv4 = (force Family)NET_FAMILY_TCP_IPV4; +static const Family family_tcp_ipv6 = (force Family)NET_FAMILY_TCP_IPV6; +static const Family family_tox_tcp_ipv4 = (force Family)NET_FAMILY_TOX_TCP_IPV4; +static const Family family_tox_tcp_ipv6 = (force Family)NET_FAMILY_TOX_TCP_IPV6; static const Family *make_tox_family(int family) { @@ -303,6 +305,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 family) +{ + return (force Family)family; +} + +uint8_t family_to_int(Family family) +{ + // TODO(https://github.com/TokTok/c-toxcore/pull/2595): Remove force. + return (force uint8_t)family; +} + non_null() static void get_ip4(IP4 *result, const struct in_addr *addr) { @@ -431,47 +490,47 @@ Family net_family_tox_tcp_ipv6(void) bool net_family_is_unspec(Family family) { - return family.value == family_unspec.value; + return family == family_unspec; } bool net_family_is_ipv4(Family family) { - return family.value == family_ipv4.value; + return family == family_ipv4; } bool net_family_is_ipv6(Family family) { - return family.value == family_ipv6.value; + return family == family_ipv6; } bool net_family_is_tcp_server(Family family) { - return family.value == family_tcp_server.value; + return family == family_tcp_server; } bool net_family_is_tcp_client(Family family) { - return family.value == family_tcp_client.value; + return family == family_tcp_client; } bool net_family_is_tcp_ipv4(Family family) { - return family.value == family_tcp_ipv4.value; + return family == family_tcp_ipv4; } bool net_family_is_tcp_ipv6(Family family) { - return family.value == family_tcp_ipv6.value; + return family == family_tcp_ipv6; } bool net_family_is_tox_tcp_ipv4(Family family) { - return family.value == family_tox_tcp_ipv4.value; + return family == family_tox_tcp_ipv4; } bool net_family_is_tox_tcp_ipv6(Family family) { - return family.value == family_tox_tcp_ipv6.value; + return family == family_tox_tcp_ipv6; } bool sock_valid(Socket sock) @@ -770,7 +829,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,13 +1005,13 @@ 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. Ip_Ntoa ip_str; LOGGER_WARNING(net->log, "attempted to send message with network family %d (probably IPv6) on IPv4 socket (%s)", - ipp_copy.ip.family.value, net_ip_ntoa(&ipp_copy.ip, &ip_str)); + ipp_copy.ip.family, net_ip_ntoa(&ipp_copy.ip, &ip_str)); return -1; } @@ -991,7 +1050,7 @@ int send_packet(const Networking_Core *net, const IP_Port *ip_port, Packet packe addr6->sin6_flowinfo = 0; addr6->sin6_scope_id = 0; } else { - LOGGER_ERROR(net->log, "unknown address type: %d", ipp_copy.ip.family.value); + LOGGER_ERROR(net->log, "unknown address type: %d", ipp_copy.ip.family); return -1; } @@ -1153,7 +1212,7 @@ Networking_Core *new_networking_ex( /* maybe check for invalid IPs like 224+.x.y.z? if there is any IP set ever */ if (!net_family_is_ipv4(ip->family) && !net_family_is_ipv6(ip->family)) { - LOGGER_ERROR(log, "invalid address family: %u", ip->family.value); + LOGGER_ERROR(log, "invalid address family: %u", ip->family); return nullptr; } @@ -1396,7 +1455,7 @@ bool ip_equal(const IP *a, const IP *b) } /* same family */ - if (a->family.value == b->family.value) { + if (a->family == b->family) { if (net_family_is_ipv4(a->family) || net_family_is_tcp_ipv4(a->family)) { struct in_addr addr_a; struct in_addr addr_b; @@ -1460,25 +1519,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. @@ -1499,7 +1563,7 @@ int ipport_cmp_handler(const void *a, const void *b, size_t size) return cmp_uint(ipp_a->port, ipp_b->port); } -static const IP empty_ip = {{0}}; +static const IP empty_ip = {0}; /** nulls out ip */ void ip_reset(IP *ip) @@ -1511,7 +1575,7 @@ void ip_reset(IP *ip) *ip = empty_ip; } -static const IP_Port empty_ip_port = {{{0}}}; +static const IP_Port empty_ip_port = {{0}}; /** nulls out ip_port */ void ipport_reset(IP_Port *ipport) @@ -1616,16 +1680,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 +1735,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; } @@ -1735,7 +1799,7 @@ const char *net_ip_ntoa(const IP *ip, Ip_Ntoa *ip_str) } if (!ip_parse_addr(ip, ip_str->buf, sizeof(ip_str->buf))) { - snprintf(ip_str->buf, sizeof(ip_str->buf), "(IP invalid, family %u)", ip->family.value); + snprintf(ip_str->buf, sizeof(ip_str->buf), "(IP invalid, family %u)", ip->family); ip_str->length = (uint16_t)strlen(ip_str->buf); return ip_str->buf; } @@ -1807,13 +1871,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,12 +2038,12 @@ 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); // Try parsing as IP address first. - IP_Port parsed = {{{0}}}; + IP_Port parsed = {{0}}; // Initialise to nullptr. In error paths, at least we initialise the out // parameter. *res = nullptr; @@ -2113,7 +2177,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..b6877f01c40 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -81,9 +81,31 @@ typedef struct Network { const Network *os_network(void); -typedef struct Family { - uint8_t value; -} Family; +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; + +// TODO(https://github.com/TokTok/c-toxcore/pull/2595): Use the enum. +typedef bitwise uint8_t Family; + +non_null() +bool net_family_from_int(uint8_t family, Net_Family *out); + +// TODO(https://github.com/TokTok/c-toxcore/pull/2595): Remove. +Family family_from_net_family(Net_Family family); +// TODO(https://github.com/TokTok/c-toxcore/pull/2595): Rename to family_to_int. +uint8_t family_to_int(Family family); bool net_family_is_unspec(Family family); bool net_family_is_ipv4(Family family); @@ -151,7 +173,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 +182,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 +233,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 +329,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 +413,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 +530,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..cd0df523ca8 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.cc b/toxcore/network_test_util.cc index fdf55f2a63e..10a24c896fc 100644 --- a/toxcore/network_test_util.cc +++ b/toxcore/network_test_util.cc @@ -106,8 +106,6 @@ IP_Port random_ip_port(const Random *rng) return ip_port; } -bool operator==(Family const &a, Family const &b) { return a.value == b.value; } - bool operator==(IP4 const &a, IP4 const &b) { return a.uint32 == b.uint32; } bool operator==(IP6 const &a, IP6 const &b) diff --git a/toxcore/network_test_util.hh b/toxcore/network_test_util.hh index 1f437d791d2..de66f51e08d 100644 --- a/toxcore/network_test_util.hh +++ b/toxcore/network_test_util.hh @@ -83,8 +83,6 @@ public: IP_Port operator()(); }; -bool operator==(Family const &a, Family const &b); - bool operator==(IP4 const &a, IP4 const &b); bool operator==(IP6 const &a, IP6 const &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); diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 9b0ac96102a..bd72522dd5d 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -1236,7 +1236,7 @@ static int handle_tcp_onion(void *object, const uint8_t *data, uint16_t length, return 1; } - IP_Port ip_port = {{{0}}}; + IP_Port ip_port = {{0}}; ip_port.ip.family = net_family_tcp_server(); if (data[0] == NET_PACKET_ANNOUNCE_RESPONSE) {