Skip to content

Commit

Permalink
Make more functions take const pointers to IP_Port
Browse files Browse the repository at this point in the history
We additionally now make local copies of the IP_Port param instead
of modifying the passed argument
  • Loading branch information
JFreegman committed Feb 3, 2022
1 parent 5880971 commit 5dad391
Show file tree
Hide file tree
Showing 20 changed files with 173 additions and 165 deletions.
28 changes: 14 additions & 14 deletions auto_tests/dht_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ static void test_addto_lists_update(DHT *dht,
ipport_copy(&test_ipp, ipv6 ? &list[test].assoc6.ip_port : &list[test].assoc4.ip_port);

random_bytes(test_id, sizeof(test_id));
used = addto_lists(dht, test_ipp, test_id);
used = addto_lists(dht, &test_ipp, test_id);
ck_assert_msg(used >= 1, "Wrong number of added clients");
// it is possible to have ip_port duplicates in the list, so ip_port @ found not always equal to ip_port @ test
found = client_in_list(list, length, test_id);
Expand All @@ -107,7 +107,7 @@ static void test_addto_lists_update(DHT *dht,
test_ipp.port = random_u32() % TOX_PORT_DEFAULT;
id_copy(test_id, list[test].public_key);

used = addto_lists(dht, test_ipp, test_id);
used = addto_lists(dht, &test_ipp, test_id);
ck_assert_msg(used >= 1, "Wrong number of added clients");
// it is not possible to have id duplicates in the list, so id @ found must be equal id @ test
ck_assert_msg(client_in_list(list, length, test_id) == test, "Client id is not in the list");
Expand All @@ -127,7 +127,7 @@ static void test_addto_lists_update(DHT *dht,
list[test2].assoc4.ip_port.port = -1;
}

used = addto_lists(dht, test_ipp, test_id);
used = addto_lists(dht, &test_ipp, test_id);
ck_assert_msg(used >= 1, "Wrong number of added clients");
ck_assert_msg(client_in_list(list, length, test_id) == test2, "Client id is not in the list");
ck_assert_msg(ipport_equal(&test_ipp, ipv6 ? &list[test2].assoc6.ip_port : &list[test2].assoc4.ip_port),
Expand All @@ -146,7 +146,7 @@ static void test_addto_lists_update(DHT *dht,
list[test1].assoc4.ip_port.port = -1;
}

used = addto_lists(dht, test_ipp, test_id);
used = addto_lists(dht, &test_ipp, test_id);
ck_assert_msg(used >= 1, "Wrong number of added clients");
ck_assert_msg(client_in_list(list, length, test_id) == test1, "Client id is not in the list");
ck_assert_msg(ipport_equal(&test_ipp, ipv6 ? &list[test1].assoc6.ip_port : &list[test1].assoc4.ip_port),
Expand Down Expand Up @@ -188,7 +188,7 @@ static void test_addto_lists_bad(DHT *dht,
}

ip_port->port += 1;
used = addto_lists(dht, *ip_port, public_key);
used = addto_lists(dht, ip_port, public_key);
ck_assert_msg(used >= 1, "Wrong number of added clients");

ck_assert_msg(client_in_list(list, length, public_key) >= 0, "Client id is not in the list");
Expand All @@ -213,7 +213,7 @@ static void test_addto_lists_good(DHT *dht,
} while (is_furthest(comp_client_id, list, length, public_key));

ip_port->port += 1;
addto_lists(dht, *ip_port, public_key);
addto_lists(dht, ip_port, public_key);
ck_assert_msg(client_in_list(list, length, public_key) >= 0, "Good client id is not in the list");

// check "good" client id skip
Expand All @@ -222,7 +222,7 @@ static void test_addto_lists_good(DHT *dht,
} while (!is_furthest(comp_client_id, list, length, public_key));

ip_port->port += 1;
addto_lists(dht, *ip_port, public_key);
addto_lists(dht, ip_port, public_key);
ck_assert_msg(client_in_list(list, length, public_key) == -1, "Good client id is in the list");
}

Expand All @@ -239,7 +239,7 @@ static void test_addto_lists(IP ip)
Mono_Time *mono_time = mono_time_new();
ck_assert_msg(mono_time != nullptr, "Failed to create Mono_Time");

Networking_Core *net = new_networking(log, ip, TOX_PORT_DEFAULT);
Networking_Core *net = new_networking(log, &ip, TOX_PORT_DEFAULT);
ck_assert_msg(net != nullptr, "Failed to create Networking_Core");

DHT *dht = new_dht(log, mono_time, net, true);
Expand All @@ -254,20 +254,20 @@ static void test_addto_lists(IP ip)
// check lists filling
for (i = 0; i < MAX(LCLIENT_LIST, MAX_FRIEND_CLIENTS); ++i) {
random_bytes(public_key, sizeof(public_key));
used = addto_lists(dht, ip_port, public_key);
used = addto_lists(dht, &ip_port, public_key);
ck_assert_msg(used == dht->num_friends + 1, "Wrong number of added clients with existing ip_port");
}

for (i = 0; i < MAX(LCLIENT_LIST, MAX_FRIEND_CLIENTS); ++i) {
ip_port.port += 1;
used = addto_lists(dht, ip_port, public_key);
used = addto_lists(dht, &ip_port, public_key);
ck_assert_msg(used == dht->num_friends + 1, "Wrong number of added clients with existing public_key");
}

for (i = 0; i < MAX(LCLIENT_LIST, MAX_FRIEND_CLIENTS); ++i) {
ip_port.port += 1;
random_bytes(public_key, sizeof(public_key));
used = addto_lists(dht, ip_port, public_key);
used = addto_lists(dht, &ip_port, public_key);
ck_assert_msg(used >= 1, "Wrong number of added clients");
}

Expand Down Expand Up @@ -379,7 +379,7 @@ static void test_list_main(void)

mono_times[i] = mono_time_new();

dhts[i] = new_dht(logs[i], mono_times[i], new_networking(logs[i], ip, DHT_DEFAULT_PORT + i), true);
dhts[i] = new_dht(logs[i], mono_times[i], new_networking(logs[i], &ip, DHT_DEFAULT_PORT + i), true);
ck_assert_msg(dhts[i] != nullptr, "Failed to create dht instances %u", i);
ck_assert_msg(net_port(dhts[i]->net) != DHT_DEFAULT_PORT + i,
"Bound to wrong port: %d", net_port(dhts[i]->net));
Expand All @@ -402,7 +402,7 @@ static void test_list_main(void)
ip_port.ip.ip.v4.uint32 = random_u32();
ip_port.port = random_u32() % (UINT16_MAX - 1);
++ip_port.port;
addto_lists(dhts[i], ip_port, dhts[j]->self_public_key);
addto_lists(dhts[i], &ip_port, dhts[j]->self_public_key);
}
}

Expand Down Expand Up @@ -536,7 +536,7 @@ static void test_DHT_test(void)
clock[i] = current_time_monotonic(mono_times[i]);
mono_time_set_current_time_callback(mono_times[i], get_clock_callback, &clock[i]);

dhts[i] = new_dht(logs[i], mono_times[i], new_networking(logs[i], ip, DHT_DEFAULT_PORT + i), true);
dhts[i] = new_dht(logs[i], mono_times[i], new_networking(logs[i], &ip, DHT_DEFAULT_PORT + i), true);
ck_assert_msg(dhts[i] != nullptr, "Failed to create dht instances %u", i);
ck_assert_msg(net_port(dhts[i]->net) != DHT_DEFAULT_PORT + i, "Bound to wrong port");
}
Expand Down
2 changes: 1 addition & 1 deletion auto_tests/network_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ static void test_ip_equal(void)
ip2.ip.v6.uint32[2] = net_htonl(0xFFFF);
ip2.ip.v6.uint32[3] = net_htonl(0x7F000001);

ck_assert_msg(ipv6_ipv4_in_v6(ip2.ip.v6) != 0,
ck_assert_msg(ipv6_ipv4_in_v6(&ip2.ip.v6) != 0,
"ipv6_ipv4_in_v6(::ffff:127.0.0.1): expected != 0, got 0.");

res = ip_equal(&ip1, &ip2);
Expand Down
8 changes: 4 additions & 4 deletions auto_tests/onion_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ static void test_basic(void)
Mono_Time *mono_time2 = mono_time_new();

IP ip = get_loopback();
Onion *onion1 = new_onion(log1, mono_time1, new_dht(log1, mono_time1, new_networking(log1, ip, 36567), true));
Onion *onion2 = new_onion(log2, mono_time2, new_dht(log2, mono_time2, new_networking(log2, ip, 36568), true));
Onion *onion1 = new_onion(log1, mono_time1, new_dht(log1, mono_time1, new_networking(log1, &ip, 36567), true));
Onion *onion2 = new_onion(log2, mono_time2, new_dht(log2, mono_time2, new_networking(log2, &ip, 36568), true));
ck_assert_msg((onion1 != nullptr) && (onion2 != nullptr), "Onion failed initializing.");
networking_registerhandler(onion2->net, NET_PACKET_ANNOUNCE_REQUEST, &handle_test_1, onion2);

Expand Down Expand Up @@ -270,7 +270,7 @@ static void test_basic(void)

Mono_Time *mono_time3 = mono_time_new();

Onion *onion3 = new_onion(log3, mono_time3, new_dht(log3, mono_time3, new_networking(log3, ip, 36569), true));
Onion *onion3 = new_onion(log3, mono_time3, new_dht(log3, mono_time3, new_networking(log3, &ip, 36569), true));
ck_assert_msg((onion3 != nullptr), "Onion failed initializing.");

random_nonce(nonce);
Expand Down Expand Up @@ -362,7 +362,7 @@ static Onions *new_onions(uint16_t port, uint32_t *index)
return nullptr;
}

Networking_Core *net = new_networking(on->log, ip, port);
Networking_Core *net = new_networking(on->log, &ip, port);

if (!net) {
mono_time_free(on->mono_time);
Expand Down
2 changes: 1 addition & 1 deletion other/DHT_bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ int main(int argc, char *argv[])
}

Mono_Time *mono_time = mono_time_new();
DHT *dht = new_dht(logger, mono_time, new_networking(logger, ip, PORT), true);
DHT *dht = new_dht(logger, mono_time, new_networking(logger, &ip, PORT), true);
Onion *onion = new_onion(logger, mono_time, dht);
const Onion_Announce *onion_a = new_onion_announce(logger, mono_time, dht);

Expand Down
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/docker/tox-bootstrapd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ae94a886c54a0f1b62cf240c693dd9294a19d1b5f9460a48bbc420c53aa3e5fb /usr/local/bin/tox-bootstrapd
fda21b60377d3add8c3537835741a903499df08f3b3d4dd1beac769988baf559 /usr/local/bin/tox-bootstrapd
4 changes: 2 additions & 2 deletions other/bootstrap_daemon/src/tox-bootstrapd.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,14 @@ int main(int argc, char *argv[])
logger_callback_log(logger, toxcore_logger_callback, nullptr, nullptr);
}

Networking_Core *net = new_networking(logger, ip, port);
Networking_Core *net = new_networking(logger, &ip, port);

if (net == nullptr) {
if (enable_ipv6 && enable_ipv4_fallback) {
log_write(LOG_LEVEL_WARNING, "Couldn't initialize IPv6 networking. Falling back to using IPv4.\n");
enable_ipv6 = 0;
ip_init(&ip, enable_ipv6);
net = new_networking(logger, ip, port);
net = new_networking(logger, &ip, port);

if (net == nullptr) {
log_write(LOG_LEVEL_ERROR, "Couldn't fallback to IPv4. Exiting.\n");
Expand Down
2 changes: 1 addition & 1 deletion testing/DHT_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ int main(int argc, char *argv[])

Mono_Time *const mono_time = mono_time_new();
Logger *const logger = logger_new();
DHT *dht = new_dht(logger, mono_time, new_networking(logger, ip, PORT), true);
DHT *dht = new_dht(logger, mono_time, new_networking(logger, &ip, PORT), true);
printf("OUR ID: ");

for (uint32_t i = 0; i < 32; i++) {
Expand Down
Loading

0 comments on commit 5dad391

Please sign in to comment.