Skip to content

Commit

Permalink
refactor: Use struct wrapper for Family.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Feb 9, 2024
1 parent fc0b2ae commit 132b8bd
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 85 deletions.
46 changes: 25 additions & 21 deletions auto_tests/auto_test_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
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
10 changes: 5 additions & 5 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 NET_FAMILY_IPV4, got %u.", ip.family);
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));
Expand All @@ -59,7 +59,7 @@ static void test_addr_resolv_localhost(void)
net_kill_strerror(strerror);

ck_assert_msg(net_family_is_ipv6(ip.family), "Expected family NET_FAMILY_IPV6 (%d), got %u.", NET_FAMILY_IPV6,
ip.family);
ip.family.value);
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 @@ -83,17 +83,17 @@ static void test_addr_resolv_localhost(void)

#if USE_IPV6
ck_assert_msg(net_family_is_ipv6(ip.family), "Expected family NET_FAMILY_IPV6 (%d), got %u.", NET_FAMILY_IPV6,
ip.family);
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 NET_FAMILY_IPV4 (%d), got %u.", NET_FAMILY_IPV4,
extra.family);
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 NET_FAMILY_IPV4 (%d), got %u.", NET_FAMILY_IPV4, ip.family);
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
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
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 = {NET_FAMILY_UNSPEC};
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 = {NET_FAMILY_UNSPEC};
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/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
2 changes: 1 addition & 1 deletion 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
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);
LOGGER_ERROR(logger, "TCP socket creation failed (family = %d)", family.value);
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, error != nullptr ? error : "(null)");
port, family.value, 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_announce_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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
4 changes: 2 additions & 2 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
Loading

0 comments on commit 132b8bd

Please sign in to comment.