Skip to content

Commit

Permalink
refactor: Use strong typedef for network family.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Feb 9, 2024
1 parent 3ba7a0d commit fc0b2ae
Show file tree
Hide file tree
Showing 22 changed files with 218 additions and 167 deletions.
2 changes: 1 addition & 1 deletion auto_tests/forwarding_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
30 changes: 15 additions & 15 deletions auto_tests/network_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
Expand All @@ -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
Expand All @@ -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();
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion auto_tests/onion_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions other/bootstrap_node_packets.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand All @@ -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;
}
4 changes: 2 additions & 2 deletions toxcore/DHT.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions toxcore/DHT_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ TEST(AddToList, OverridesKeysWithCloserKeys)

std::array<Node_format, 4> 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()));
Expand Down Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion toxcore/LAN_discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion toxcore/TCP_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions toxcore/TCP_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion toxcore/TCP_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions toxcore/TCP_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion toxcore/group_chats.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions toxcore/net_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion toxcore/net_crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading

0 comments on commit fc0b2ae

Please sign in to comment.