Skip to content

Commit 12cfaec

Browse files
committed
refactor: Use strong typedef for network family.
1 parent 0ec4978 commit 12cfaec

16 files changed

+208
-150
lines changed

auto_tests/auto_test_support.c

+25-21
Original file line numberDiff line numberDiff line change
@@ -36,34 +36,38 @@ static const struct BootstrapNodes {
3636
const uint8_t key[32];
3737
} bootstrap_nodes[] = {
3838
{
39-
"tox.abilinski.com", 33445,
40-
0x10, 0xC0, 0x0E, 0xB2, 0x50, 0xC3, 0x23, 0x3E,
41-
0x34, 0x3E, 0x2A, 0xEB, 0xA0, 0x71, 0x15, 0xA5,
42-
0xC2, 0x89, 0x20, 0xE9, 0xC8, 0xD2, 0x94, 0x92,
43-
0xF6, 0xD0, 0x0B, 0x29, 0x04, 0x9E, 0xDC, 0x7E,
39+
"tox.abilinski.com", 33445, {
40+
0x10, 0xC0, 0x0E, 0xB2, 0x50, 0xC3, 0x23, 0x3E,
41+
0x34, 0x3E, 0x2A, 0xEB, 0xA0, 0x71, 0x15, 0xA5,
42+
0xC2, 0x89, 0x20, 0xE9, 0xC8, 0xD2, 0x94, 0x92,
43+
0xF6, 0xD0, 0x0B, 0x29, 0x04, 0x9E, 0xDC, 0x7E,
44+
},
4445
},
4546
{
46-
"tox.initramfs.io", 33445,
47-
0x02, 0x80, 0x7C, 0xF4, 0xF8, 0xBB, 0x8F, 0xB3,
48-
0x90, 0xCC, 0x37, 0x94, 0xBD, 0xF1, 0xE8, 0x44,
49-
0x9E, 0x9A, 0x83, 0x92, 0xC5, 0xD3, 0xF2, 0x20,
50-
0x00, 0x19, 0xDA, 0x9F, 0x1E, 0x81, 0x2E, 0x46,
47+
"tox.initramfs.io", 33445, {
48+
0x02, 0x80, 0x7C, 0xF4, 0xF8, 0xBB, 0x8F, 0xB3,
49+
0x90, 0xCC, 0x37, 0x94, 0xBD, 0xF1, 0xE8, 0x44,
50+
0x9E, 0x9A, 0x83, 0x92, 0xC5, 0xD3, 0xF2, 0x20,
51+
0x00, 0x19, 0xDA, 0x9F, 0x1E, 0x81, 0x2E, 0x46,
52+
},
5153
},
5254
{
53-
"tox.plastiras.org", 33445,
54-
0x8E, 0x8B, 0x63, 0x29, 0x9B, 0x3D, 0x52, 0x0F,
55-
0xB3, 0x77, 0xFE, 0x51, 0x00, 0xE6, 0x5E, 0x33,
56-
0x22, 0xF7, 0xAE, 0x5B, 0x20, 0xA0, 0xAC, 0xED,
57-
0x29, 0x81, 0x76, 0x9F, 0xC5, 0xB4, 0x37, 0x25,
55+
"tox.plastiras.org", 33445, {
56+
0x8E, 0x8B, 0x63, 0x29, 0x9B, 0x3D, 0x52, 0x0F,
57+
0xB3, 0x77, 0xFE, 0x51, 0x00, 0xE6, 0x5E, 0x33,
58+
0x22, 0xF7, 0xAE, 0x5B, 0x20, 0xA0, 0xAC, 0xED,
59+
0x29, 0x81, 0x76, 0x9F, 0xC5, 0xB4, 0x37, 0x25,
60+
},
5861
},
5962
{
60-
"tox.novg.net", 33445,
61-
0xD5, 0x27, 0xE5, 0x84, 0x7F, 0x83, 0x30, 0xD6,
62-
0x28, 0xDA, 0xB1, 0x81, 0x4F, 0x0A, 0x42, 0x2F,
63-
0x6D, 0xC9, 0xD0, 0xA3, 0x00, 0xE6, 0xC3, 0x57,
64-
0x63, 0x4E, 0xE2, 0xDA, 0x88, 0xC3, 0x54, 0x63,
63+
"tox.novg.net", 33445, {
64+
0xD5, 0x27, 0xE5, 0x84, 0x7F, 0x83, 0x30, 0xD6,
65+
0x28, 0xDA, 0xB1, 0x81, 0x4F, 0x0A, 0x42, 0x2F,
66+
0x6D, 0xC9, 0xD0, 0xA3, 0x00, 0xE6, 0xC3, 0x57,
67+
0x63, 0x4E, 0xE2, 0xDA, 0x88, 0xC3, 0x54, 0x63,
68+
},
6569
},
66-
{ nullptr, 0, 0 },
70+
{ nullptr, 0, {0} },
6771
};
6872

6973
void bootstrap_tox_live_network(Tox *tox, bool enable_tcp)

auto_tests/network_test.c

+12-12
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.value);
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,7 +58,7 @@ 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,
61+
ck_assert_msg(net_family_is_ipv6(ip.family), "Expected family NET_FAMILY_IPV6 (%d), got %u.", NET_FAMILY_IPV6,
6262
ip.family.value);
6363
IP6 ip6_loopback = get_ip6_loopback();
6464
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)
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,
85+
ck_assert_msg(net_family_is_ipv6(ip.family), "Expected family NET_FAMILY_IPV6 (%d), got %u.", NET_FAMILY_IPV6,
8686
ip.family.value);
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,
90+
ck_assert_msg(net_family_is_ipv4(extra.family), "Expected family NET_FAMILY_IPV4 (%d), got %u.", NET_FAMILY_IPV4,
9191
extra.family.value);
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.value);
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)

other/bootstrap_node_packets.c

+3-3
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_test.cc

+2-2
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

+1-1
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_connection.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -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

+1-1
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.

toxcore/group_announce_test.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ TEST_F(AnnouncesPack, AnnouncesValidationCheck)
215215
EXPECT_EQ(gca_is_valid_announce(nullptr), false);
216216
#endif
217217

218-
GC_Announce announce = {0};
218+
GC_Announce announce = {{{{0}}}};
219219
EXPECT_EQ(gca_is_valid_announce(&announce), false);
220220
EXPECT_EQ(gca_is_valid_announce(&announces_[0]), true);
221221
EXPECT_EQ(gca_is_valid_announce(&announces_[1]), true);

toxcore/net_crypto.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2381,7 +2381,7 @@ int send_tcp_forward_request(const Logger *logger, Net_Crypto *c, const IP_Port
23812381

23822382
/** @brief Copy a maximum of num random TCP relays we are connected to to tcp_relays.
23832383
*
2384-
* NOTE that the family of the copied ip ports will be set to TCP_INET or TCP_INET6.
2384+
* NOTE that the family of the copied ip ports will be set to NET_FAMILY_TCP_IPV4 or NET_FAMILY_TCP_IPV6.
23852385
*
23862386
* return number of relays copied to tcp_relays on success.
23872387
* return 0 on failure.

toxcore/net_crypto.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ int send_tcp_forward_request(const Logger *logger, Net_Crypto *c, const IP_Port
345345

346346
/** @brief Copy a maximum of num random TCP relays we are connected to to tcp_relays.
347347
*
348-
* NOTE that the family of the copied ip ports will be set to TCP_INET or TCP_INET6.
348+
* NOTE that the family of the copied ip ports will be set to NET_FAMILY_TCP_IPV4 or NET_FAMILY_TCP_IPV6.
349349
*
350350
* return number of relays copied to tcp_relays on success.
351351
* return 0 on failure.

0 commit comments

Comments
 (0)