Skip to content

Commit fc0b2ae

Browse files
committed
refactor: Use strong typedef for network family.
1 parent 3ba7a0d commit fc0b2ae

22 files changed

+218
-167
lines changed

auto_tests/forwarding_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ static Forwarding_Subtox *new_forwarding_subtox(const Memory *mem, bool no_udp,
126126

127127
subtox->dht = new_dht(subtox->log, mem, rng, ns, subtox->mono_time, subtox->net, true, true);
128128

129-
const TCP_Proxy_Info inf = {{{{0}}}};
129+
const TCP_Proxy_Info inf = {{{0}}};
130130
subtox->c = new_net_crypto(subtox->log, mem, rng, ns, subtox->mono_time, subtox->dht, &inf);
131131

132132
subtox->forwarding = new_forwarding(subtox->log, rng, subtox->mono_time, subtox->dht);

auto_tests/network_test.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static void test_addr_resolv_localhost(void)
3636
net_kill_strerror(strerror);
3737

3838
Ip_Ntoa ip_str;
39-
ck_assert_msg(net_family_is_ipv4(ip.family), "Expected family TOX_AF_INET, got %u.", ip.family.value);
39+
ck_assert_msg(net_family_is_ipv4(ip.family), "Expected family NET_FAMILY_IPV4, got %u.", ip.family);
4040
const uint32_t loopback = get_ip4_loopback().uint32;
4141
ck_assert_msg(ip.ip.v4.uint32 == loopback, "Expected 127.0.0.1, got %s.",
4242
net_ip_ntoa(&ip, &ip_str));
@@ -58,8 +58,8 @@ static void test_addr_resolv_localhost(void)
5858
ck_assert_msg(res, "Resolver failed: %d, %s", error, strerror);
5959
net_kill_strerror(strerror);
6060

61-
ck_assert_msg(net_family_is_ipv6(ip.family), "Expected family TOX_AF_INET6 (%d), got %u.", TOX_AF_INET6,
62-
ip.family.value);
61+
ck_assert_msg(net_family_is_ipv6(ip.family), "Expected family NET_FAMILY_IPV6 (%d), got %u.", NET_FAMILY_IPV6,
62+
ip.family);
6363
IP6 ip6_loopback = get_ip6_loopback();
6464
ck_assert_msg(!memcmp(&ip.ip.v6, &ip6_loopback, sizeof(IP6)), "Expected ::1, got %s.",
6565
net_ip_ntoa(&ip, &ip_str));
@@ -82,18 +82,18 @@ static void test_addr_resolv_localhost(void)
8282
net_kill_strerror(strerror);
8383

8484
#if USE_IPV6
85-
ck_assert_msg(net_family_is_ipv6(ip.family), "Expected family TOX_AF_INET6 (%d), got %u.", TOX_AF_INET6,
86-
ip.family.value);
85+
ck_assert_msg(net_family_is_ipv6(ip.family), "Expected family NET_FAMILY_IPV6 (%d), got %u.", NET_FAMILY_IPV6,
86+
ip.family);
8787
ck_assert_msg(!memcmp(&ip.ip.v6, &ip6_loopback, sizeof(IP6)), "Expected ::1, got %s.",
8888
net_ip_ntoa(&ip, &ip_str));
8989

90-
ck_assert_msg(net_family_is_ipv4(extra.family), "Expected family TOX_AF_INET (%d), got %u.", TOX_AF_INET,
91-
extra.family.value);
90+
ck_assert_msg(net_family_is_ipv4(extra.family), "Expected family NET_FAMILY_IPV4 (%d), got %u.", NET_FAMILY_IPV4,
91+
extra.family);
9292
ck_assert_msg(extra.ip.v4.uint32 == loopback, "Expected 127.0.0.1, got %s.",
9393
net_ip_ntoa(&ip, &ip_str));
9494
#elif 0
9595
// TODO(iphydf): Fix this to work on IPv6-supporting systems.
96-
ck_assert_msg(net_family_is_ipv4(ip.family), "Expected family TOX_AF_INET (%d), got %u.", TOX_AF_INET, ip.family.value);
96+
ck_assert_msg(net_family_is_ipv4(ip.family), "Expected family NET_FAMILY_IPV4 (%d), got %u.", NET_FAMILY_IPV4, ip.family);
9797
ck_assert_msg(ip.ip.v4.uint32 == loopback, "Expected 127.0.0.1, got %s.",
9898
net_ip_ntoa(&ip, &ip_str));
9999
#endif
@@ -119,20 +119,20 @@ static void test_ip_equal(void)
119119
ip1.ip.v4.uint32 = net_htonl(0x7F000001);
120120

121121
res = ip_equal(&ip1, &ip2);
122-
ck_assert_msg(res == 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_UNSPEC, 0} ): "
122+
ck_assert_msg(res == 0, "ip_equal( {NET_FAMILY_IPV4, 127.0.0.1}, {NET_FAMILY_UNSPEC, 0} ): "
123123
"expected result 0, got %d.", res);
124124

125125
ip2.family = net_family_ipv4();
126126
ip2.ip.v4.uint32 = net_htonl(0x7F000001);
127127

128128
res = ip_equal(&ip1, &ip2);
129-
ck_assert_msg(res != 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_INET, 127.0.0.1} ): "
129+
ck_assert_msg(res != 0, "ip_equal( {NET_FAMILY_IPV4, 127.0.0.1}, {NET_FAMILY_IPV4, 127.0.0.1} ): "
130130
"expected result != 0, got 0.");
131131

132132
ip2.ip.v4.uint32 = net_htonl(0x7F000002);
133133

134134
res = ip_equal(&ip1, &ip2);
135-
ck_assert_msg(res == 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_INET, 127.0.0.2} ): "
135+
ck_assert_msg(res == 0, "ip_equal( {NET_FAMILY_IPV4, 127.0.0.1}, {NET_FAMILY_IPV4, 127.0.0.2} ): "
136136
"expected result 0, got %d.", res);
137137

138138
ip2.family = net_family_ipv6();
@@ -145,21 +145,21 @@ static void test_ip_equal(void)
145145
"ipv6_ipv4_in_v6(::ffff:127.0.0.1): expected != 0, got 0.");
146146

147147
res = ip_equal(&ip1, &ip2);
148-
ck_assert_msg(res != 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_INET6, ::ffff:127.0.0.1} ): "
148+
ck_assert_msg(res != 0, "ip_equal( {NET_FAMILY_IPV4, 127.0.0.1}, {NET_FAMILY_IPV6, ::ffff:127.0.0.1} ): "
149149
"expected result != 0, got 0.");
150150

151151
IP6 ip6_loopback = get_ip6_loopback();
152152
memcpy(&ip2.ip.v6, &ip6_loopback, sizeof(IP6));
153153
res = ip_equal(&ip1, &ip2);
154-
ck_assert_msg(res == 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_INET6, ::1} ): expected result 0, got %d.", res);
154+
ck_assert_msg(res == 0, "ip_equal( {NET_FAMILY_IPV4, 127.0.0.1}, {NET_FAMILY_IPV6, ::1} ): expected result 0, got %d.", res);
155155

156156
memcpy(&ip1, &ip2, sizeof(IP));
157157
res = ip_equal(&ip1, &ip2);
158-
ck_assert_msg(res != 0, "ip_equal( {TOX_AF_INET6, ::1}, {TOX_AF_INET6, ::1} ): expected result != 0, got 0.");
158+
ck_assert_msg(res != 0, "ip_equal( {NET_FAMILY_IPV6, ::1}, {NET_FAMILY_IPV6, ::1} ): expected result != 0, got 0.");
159159

160160
ip2.ip.v6.uint8[15]++;
161161
res = ip_equal(&ip1, &ip2);
162-
ck_assert_msg(res == 0, "ip_equal( {TOX_AF_INET6, ::1}, {TOX_AF_INET6, ::2} ): expected result 0, got %d.", res);
162+
ck_assert_msg(res == 0, "ip_equal( {NET_FAMILY_IPV6, ::1}, {NET_FAMILY_IPV6, ::2} ): expected result 0, got %d.", res);
163163
}
164164

165165
int main(void)

auto_tests/onion_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ static Onions *new_onions(const Memory *mem, const Random *rng, uint16_t port, u
471471
return nullptr;
472472
}
473473

474-
TCP_Proxy_Info inf = {{{{0}}}};
474+
TCP_Proxy_Info inf = {{{0}}};
475475
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));
476476

477477
if (!on->onion_c) {

other/bootstrap_node_packets.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static uint8_t bootstrap_motd[MAX_MOTD_LENGTH];
2121
static uint16_t bootstrap_motd_length;
2222

2323
/* To request this packet just send a packet of length INFO_REQUEST_PACKET_LENGTH
24-
* with the first byte being BOOTSTRAP_INFO_PACKET_ID
24+
* with the first byte being NET_PACKET_BOOTSTRAP_INFO
2525
*/
2626
static int handle_info_request(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length,
2727
void *userdata)
@@ -33,7 +33,7 @@ static int handle_info_request(void *object, const IP_Port *source, const uint8_
3333
const Networking_Core *nc = (const Networking_Core *)object;
3434

3535
uint8_t data[1 + sizeof(bootstrap_version) + MAX_MOTD_LENGTH];
36-
data[0] = BOOTSTRAP_INFO_PACKET_ID;
36+
data[0] = NET_PACKET_BOOTSTRAP_INFO;
3737
memcpy(data + 1, &bootstrap_version, sizeof(bootstrap_version));
3838
const uint16_t len = 1 + sizeof(bootstrap_version) + bootstrap_motd_length;
3939
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_
5555
memcpy(bootstrap_motd, motd, motd_length);
5656
bootstrap_motd_length = motd_length;
5757

58-
networking_registerhandler(net, BOOTSTRAP_INFO_PACKET_ID, &handle_info_request, net);
58+
networking_registerhandler(net, NET_PACKET_BOOTSTRAP_INFO, &handle_info_request, net);
5959
return 0;
6060
}

toxcore/DHT.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ static bool client_or_ip_port_in_list(const Logger *log, const Mono_Time *mono_t
606606
LOGGER_DEBUG(log, "coipil[%u]: switching public_key (ipv%d)", index, ip_version);
607607

608608
/* kill the other address, if it was set */
609-
const IPPTsPng empty_ipptspng = {{{{0}}}};
609+
const IPPTsPng empty_ipptspng = {{{0}}};
610610
*assoc = empty_ipptspng;
611611
return true;
612612
}
@@ -964,7 +964,7 @@ static void update_client_with_reset(const Mono_Time *mono_time, Client_data *cl
964964
ipptp_write->ret_ip_self = false;
965965

966966
/* zero out other address */
967-
const IPPTsPng empty_ipptp = {{{{0}}}};
967+
const IPPTsPng empty_ipptp = {{{0}}};
968968
*ipptp_clear = empty_ipptp;
969969
}
970970

toxcore/DHT_test.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ TEST(AddToList, OverridesKeysWithCloserKeys)
116116

117117
std::array<Node_format, 4> nodes{};
118118

119-
IP_Port ip_port = {0};
119+
IP_Port ip_port = {NET_FAMILY_UNSPEC};
120120
EXPECT_TRUE(add_to_list(nodes.data(), nodes.size(), keys[0].data(), &ip_port, self_pk.data()));
121121
EXPECT_TRUE(add_to_list(nodes.data(), nodes.size(), keys[1].data(), &ip_port, self_pk.data()));
122122
EXPECT_TRUE(add_to_list(nodes.data(), nodes.size(), keys[2].data(), &ip_port, self_pk.data()));
@@ -355,7 +355,7 @@ TEST(AnnounceNodes, SetAndTest)
355355
PublicKey pk2(to_array(pk_data));
356356
ASSERT_NE(pk2, pk1);
357357

358-
IP_Port ip_port = {0};
358+
IP_Port ip_port = {NET_FAMILY_UNSPEC};
359359
ip_port.ip.family = net_family_ipv4();
360360

361361
set_announce_node(dht.get(), pk1.data());

toxcore/LAN_discovery.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns)
134134
* so it's wrapped in `__linux__` for now.
135135
* Definitely won't work like this on Windows...
136136
*/
137-
const Socket sock = net_socket(ns, net_family_ipv4(), TOX_SOCK_STREAM, 0);
137+
const Socket sock = net_socket(ns, net_family_ipv4(), TOX_SOCK_STREAM, TOX_PROTO_TCP);
138138

139139
if (!sock_valid(sock)) {
140140
free(broadcast);

toxcore/TCP_client.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ TCP_Client_Connection *new_tcp_connection(
594594
return nullptr;
595595
}
596596

597-
const TCP_Proxy_Info default_proxyinfo = {{{{0}}}};
597+
const TCP_Proxy_Info default_proxyinfo = {{{0}}};
598598

599599
if (proxy_info == nullptr) {
600600
proxy_info = &default_proxyinfo;

toxcore/TCP_connection.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ void set_forwarding_packet_tcp_connection_callback(TCP_Connections *tcp_c,
578578
*/
579579
IP_Port tcp_connections_number_to_ip_port(unsigned int tcp_connections_number)
580580
{
581-
IP_Port ip_port = {{{0}}};
581+
IP_Port ip_port = {{0}};
582582
ip_port.ip.family = net_family_tcp_server();
583583
ip_port.ip.ip.v6.uint32[0] = tcp_connections_number;
584584
return ip_port;
@@ -1475,7 +1475,7 @@ static bool copy_tcp_relay_conn(const TCP_Connections *tcp_c, Node_format *tcp_r
14751475

14761476
/** @brief Copy a maximum of max_num TCP relays we are connected to to tcp_relays.
14771477
*
1478-
* NOTE that the family of the copied ip ports will be set to TCP_INET or TCP_INET6.
1478+
* NOTE that the family of the copied ip ports will be set to NET_FAMILY_TCP_IPV4 or NET_FAMILY_TCP_IPV6.
14791479
*
14801480
* return number of relays copied to tcp_relays on success.
14811481
* return 0 on failure.

toxcore/TCP_connection.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ int add_tcp_relay_global(TCP_Connections *tcp_c, const IP_Port *ip_port, const u
278278

279279
/** @brief Copy a maximum of max_num TCP relays we are connected to to tcp_relays.
280280
*
281-
* NOTE that the family of the copied ip ports will be set to TCP_INET or TCP_INET6.
281+
* NOTE that the family of the copied ip ports will be set to NET_FAMILY_TCP_IPV4 or NET_FAMILY_TCP_IPV6.
282282
*
283283
* return number of relays copied to tcp_relays on success.
284284
* return 0 on failure.

0 commit comments

Comments
 (0)