From 2e139762f6f895464c86aa106316546ba5b71807 Mon Sep 17 00:00:00 2001 From: jfreegman Date: Thu, 3 Feb 2022 11:15:20 -0500 Subject: [PATCH] Make more functions take const pointers to IP_Port We additionally now make local copies of the IP_Port param instead of modifying the passed argument --- auto_tests/dht_test.c | 28 ++--- auto_tests/network_test.c | 2 +- auto_tests/onion_test.c | 8 +- other/DHT_bootstrap.c | 2 +- .../docker/tox-bootstrapd.sha256 | 2 +- other/bootstrap_daemon/src/tox-bootstrapd.c | 4 +- testing/DHT_test.c | 2 +- toxcore/DHT.c | 110 +++++++++--------- toxcore/DHT.h | 6 +- toxcore/DHT_test.cc | 13 ++- toxcore/LAN_discovery.c | 4 +- toxcore/Messenger.c | 2 +- toxcore/TCP_client.c | 10 +- toxcore/TCP_connection.c | 20 ++-- toxcore/friend_connection.c | 21 ++-- toxcore/friend_connection.h | 7 -- toxcore/network.c | 65 ++++++----- toxcore/network.h | 9 +- toxcore/onion_client.c | 16 +-- toxcore/ping.c | 8 +- 20 files changed, 174 insertions(+), 165 deletions(-) diff --git a/auto_tests/dht_test.c b/auto_tests/dht_test.c index bae31e93fee..5b25f270b9a 100644 --- a/auto_tests/dht_test.c +++ b/auto_tests/dht_test.c @@ -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); @@ -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"); @@ -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), @@ -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), @@ -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"); @@ -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 @@ -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"); } @@ -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); @@ -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"); } @@ -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)); @@ -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); } } @@ -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"); } diff --git a/auto_tests/network_test.c b/auto_tests/network_test.c index 05cf1c2a663..513043dff22 100644 --- a/auto_tests/network_test.c +++ b/auto_tests/network_test.c @@ -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); diff --git a/auto_tests/onion_test.c b/auto_tests/onion_test.c index 2d5a94a3067..22d87ddc20d 100644 --- a/auto_tests/onion_test.c +++ b/auto_tests/onion_test.c @@ -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); @@ -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); @@ -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); diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c index 931d9aa631d..d34ad679777 100644 --- a/other/DHT_bootstrap.c +++ b/other/DHT_bootstrap.c @@ -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); diff --git a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 index d36e0f20f5b..30359a567c0 100644 --- a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 +++ b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 @@ -1 +1 @@ -ae94a886c54a0f1b62cf240c693dd9294a19d1b5f9460a48bbc420c53aa3e5fb /usr/local/bin/tox-bootstrapd +fda21b60377d3add8c3537835741a903499df08f3b3d4dd1beac769988baf559 /usr/local/bin/tox-bootstrapd diff --git a/other/bootstrap_daemon/src/tox-bootstrapd.c b/other/bootstrap_daemon/src/tox-bootstrapd.c index a90b107e1c5..072d46284b4 100644 --- a/other/bootstrap_daemon/src/tox-bootstrapd.c +++ b/other/bootstrap_daemon/src/tox-bootstrapd.c @@ -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"); diff --git a/testing/DHT_test.c b/testing/DHT_test.c index c5865b73349..8e3087faedf 100644 --- a/testing/DHT_test.c +++ b/testing/DHT_test.c @@ -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++) { diff --git a/toxcore/DHT.c b/toxcore/DHT.c index cb5264259e6..e7341f95fdb 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -763,7 +763,7 @@ static int client_or_ip_port_in_list(const Logger *log, const Mono_Time *mono_ti return 1; } -bool add_to_list(Node_format *nodes_list, uint32_t length, const uint8_t *pk, IP_Port ip_port, +bool add_to_list(Node_format *nodes_list, uint32_t length, const uint8_t *pk, const IP_Port *ip_port, const uint8_t *cmp_pk) { for (uint32_t i = 0; i < length; ++i) { @@ -772,10 +772,10 @@ bool add_to_list(Node_format *nodes_list, uint32_t length, const uint8_t *pk, IP memcpy(pk_bak, nodes_list[i].public_key, CRYPTO_PUBLIC_KEY_SIZE); const IP_Port ip_port_bak = nodes_list[i].ip_port; memcpy(nodes_list[i].public_key, pk, CRYPTO_PUBLIC_KEY_SIZE); - nodes_list[i].ip_port = ip_port; + nodes_list[i].ip_port = *ip_port; if (i != length - 1) { - add_to_list(nodes_list, length, pk_bak, ip_port_bak, cmp_pk); + add_to_list(nodes_list, length, pk_bak, &ip_port_bak, cmp_pk); } return true; @@ -833,7 +833,7 @@ static void get_close_nodes_inner(uint64_t cur_time, const uint8_t *public_key, nodes_list[num_nodes].ip_port = ipptp->ip_port; ++num_nodes; } else { - add_to_list(nodes_list, MAX_SENT_NODES, client->public_key, ipptp->ip_port, public_key); + add_to_list(nodes_list, MAX_SENT_NODES, client->public_key, &ipptp->ip_port, public_key); } } @@ -986,10 +986,10 @@ static bool replace_all(const DHT *dht, Client_data *list, uint16_t length, const uint8_t *public_key, - IP_Port ip_port, + const IP_Port *ip_port, const uint8_t *comp_public_key) { - if (!net_family_is_ipv4(ip_port.ip.family) && !net_family_is_ipv6(ip_port.ip.family)) { + if (!net_family_is_ipv4(ip_port->ip.family) && !net_family_is_ipv6(ip_port->ip.family)) { return false; } @@ -1003,7 +1003,7 @@ static bool replace_all(const DHT *dht, Client_data *const client = &list[0]; id_copy(client->public_key, public_key); - update_client_with_reset(dht->mono_time, client, &ip_port); + update_client_with_reset(dht->mono_time, client, ip_port); return true; } @@ -1014,7 +1014,7 @@ static bool replace_all(const DHT *dht, * return -1 on failure. * return 0 on success. */ -static int add_to_close(DHT *dht, const uint8_t *public_key, IP_Port ip_port, bool simulate) +static int add_to_close(DHT *dht, const uint8_t *public_key, const IP_Port *ip_port, bool simulate) { unsigned int index = bit_by_bit_cmp(public_key, dht->self_public_key); @@ -1037,7 +1037,7 @@ static int add_to_close(DHT *dht, const uint8_t *public_key, IP_Port ip_port, bo } id_copy(client->public_key, public_key); - update_client_with_reset(dht->mono_time, client, &ip_port); + update_client_with_reset(dht->mono_time, client, ip_port); return 0; } @@ -1046,13 +1046,13 @@ static int add_to_close(DHT *dht, const uint8_t *public_key, IP_Port ip_port, bo /** Return 1 if node can be added to close list, 0 if it can't. */ -bool node_addable_to_close_list(DHT *dht, const uint8_t *public_key, IP_Port ip_port) +bool node_addable_to_close_list(DHT *dht, const uint8_t *public_key, const IP_Port *ip_port) { return add_to_close(dht, public_key, ip_port, 1) == 0; } static bool is_pk_in_client_list(const Client_data *list, unsigned int client_list_length, uint64_t cur_time, - const uint8_t *public_key, IP_Port ip_port) + const uint8_t *public_key, const IP_Port *ip_port) { const uint32_t index = index_of_client_pk(list, client_list_length, public_key); @@ -1060,14 +1060,14 @@ static bool is_pk_in_client_list(const Client_data *list, unsigned int client_li return 0; } - const IPPTsPng *assoc = net_family_is_ipv4(ip_port.ip.family) + const IPPTsPng *assoc = net_family_is_ipv4(ip_port->ip.family) ? &list[index].assoc4 : &list[index].assoc6; return !assoc_timeout(cur_time, assoc); } -static bool is_pk_in_close_list(const DHT *dht, const uint8_t *public_key, IP_Port ip_port) +static bool is_pk_in_close_list(const DHT *dht, const uint8_t *public_key, const IP_Port *ip_port) { unsigned int index = bit_by_bit_cmp(public_key, dht->self_public_key); @@ -1085,7 +1085,7 @@ static bool is_pk_in_close_list(const DHT *dht, const uint8_t *public_key, IP_Po * return false if the node should not be pinged. * return true if it should. */ -static bool ping_node_from_getnodes_ok(DHT *dht, const uint8_t *public_key, IP_Port ip_port) +static bool ping_node_from_getnodes_ok(DHT *dht, const uint8_t *public_key, const IP_Port *ip_port) { bool ret = false; @@ -1101,7 +1101,7 @@ static bool ping_node_from_getnodes_ok(DHT *dht, const uint8_t *public_key, IP_P if (ret && index == UINT32_MAX && !in_close_list) { if (*num < MAX_CLOSE_TO_BOOTSTRAP_NODES) { memcpy(dht->to_bootstrap[*num].public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); - dht->to_bootstrap[*num].ip_port = ip_port; + dht->to_bootstrap[*num].ip_port = *ip_port; ++*num; } else { // TODO(irungentoo): ipv6 vs v4 @@ -1132,7 +1132,7 @@ static bool ping_node_from_getnodes_ok(DHT *dht, const uint8_t *public_key, IP_P if (*friend_num < MAX_SENT_NODES) { Node_format *const format = &dht_friend->to_bootstrap[*friend_num]; memcpy(format->public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); - format->ip_port = ip_port; + format->ip_port = *ip_port; ++*friend_num; } else { add_to_list(dht_friend->to_bootstrap, MAX_SENT_NODES, public_key, ip_port, dht_friend->public_key); @@ -1150,36 +1150,38 @@ static bool ping_node_from_getnodes_ok(DHT *dht, const uint8_t *public_key, IP_P * * returns 1+ if the item is used in any list, 0 else */ -uint32_t addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *public_key) +uint32_t addto_lists(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key) { + IP_Port ipp_copy = *ip_port; + uint32_t used = 0; /* convert IPv4-in-IPv6 to IPv4 */ - if (net_family_is_ipv6(ip_port.ip.family) && ipv6_ipv4_in_v6(ip_port.ip.ip.v6)) { - ip_port.ip.family = net_family_ipv4; - ip_port.ip.ip.v4.uint32 = ip_port.ip.ip.v6.uint32[3]; + if (net_family_is_ipv6(ipp_copy.ip.family) && ipv6_ipv4_in_v6(&ipp_copy.ip.ip.v6)) { + ipp_copy.ip.family = net_family_ipv4; + ipp_copy.ip.ip.v4.uint32 = ipp_copy.ip.ip.v6.uint32[3]; } /* NOTE: Current behavior if there are two clients with the same id is * to replace the first ip by the second. */ const bool in_close_list = client_or_ip_port_in_list(dht->log, dht->mono_time, dht->close_clientlist, LCLIENT_LIST, - public_key, &ip_port); + public_key, &ipp_copy); /* add_to_close should be called only if !in_list (don't extract to variable) */ - if (in_close_list || add_to_close(dht, public_key, ip_port, 0)) { + if (in_close_list || add_to_close(dht, public_key, &ipp_copy, 0)) { ++used; } - DHT_Friend *friend_foundip = nullptr; + const DHT_Friend *friend_foundip = nullptr; for (uint32_t i = 0; i < dht->num_friends; ++i) { const bool in_list = client_or_ip_port_in_list(dht->log, dht->mono_time, dht->friends_list[i].client_list, - MAX_FRIEND_CLIENTS, public_key, &ip_port); + MAX_FRIEND_CLIENTS, public_key, &ipp_copy); /* replace_all should be called only if !in_list (don't extract to variable) */ if (in_list - || replace_all(dht, dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, public_key, ip_port, + || replace_all(dht, dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, public_key, &ipp_copy, dht->friends_list[i].public_key)) { DHT_Friend *dht_friend = &dht->friends_list[i]; @@ -1198,14 +1200,14 @@ uint32_t addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *public_key) for (uint32_t i = 0; i < friend_foundip->lock_count; ++i) { if (friend_foundip->callbacks[i].ip_callback) { friend_foundip->callbacks[i].ip_callback(friend_foundip->callbacks[i].data, - friend_foundip->callbacks[i].number, &ip_port); + friend_foundip->callbacks[i].number, &ipp_copy); } } return used; } -static bool update_client_data(const Mono_Time *mono_time, Client_data *array, size_t size, IP_Port ip_port, +static bool update_client_data(const Mono_Time *mono_time, Client_data *array, size_t size, const IP_Port *ip_port, const uint8_t *pk, bool node_is_self) { const uint64_t temp_time = mono_time_get(mono_time); @@ -1218,15 +1220,15 @@ static bool update_client_data(const Mono_Time *mono_time, Client_data *array, s Client_data *const data = &array[index]; IPPTsPng *assoc; - if (net_family_is_ipv4(ip_port.ip.family)) { + if (net_family_is_ipv4(ip_port->ip.family)) { assoc = &data->assoc4; - } else if (net_family_is_ipv6(ip_port.ip.family)) { + } else if (net_family_is_ipv6(ip_port->ip.family)) { assoc = &data->assoc6; } else { return true; } - assoc->ret_ip_port = ip_port; + assoc->ret_ip_port = *ip_port; assoc->ret_timestamp = temp_time; assoc->ret_ip_self = node_is_self; @@ -1236,16 +1238,18 @@ static bool update_client_data(const Mono_Time *mono_time, Client_data *array, s /** If public_key is a friend or us, update ret_ip_port * nodepublic_key is the id of the node that sent us this info. */ -static void returnedip_ports(DHT *dht, IP_Port ip_port, const uint8_t *public_key, const uint8_t *nodepublic_key) +static void returnedip_ports(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key, const uint8_t *nodepublic_key) { + IP_Port ipp_copy = *ip_port; + /* convert IPv4-in-IPv6 to IPv4 */ - if (net_family_is_ipv6(ip_port.ip.family) && ipv6_ipv4_in_v6(ip_port.ip.ip.v6)) { - ip_port.ip.family = net_family_ipv4; - ip_port.ip.ip.v4.uint32 = ip_port.ip.ip.v6.uint32[3]; + if (net_family_is_ipv6(ipp_copy.ip.family) && ipv6_ipv4_in_v6(&ipp_copy.ip.ip.v6)) { + ipp_copy.ip.family = net_family_ipv4; + ipp_copy.ip.ip.v4.uint32 = ipp_copy.ip.ip.v6.uint32[3]; } if (id_equal(public_key, dht->self_public_key)) { - update_client_data(dht->mono_time, dht->close_clientlist, LCLIENT_LIST, ip_port, nodepublic_key, true); + update_client_data(dht->mono_time, dht->close_clientlist, LCLIENT_LIST, &ipp_copy, nodepublic_key, true); return; } @@ -1253,7 +1257,7 @@ static void returnedip_ports(DHT *dht, IP_Port ip_port, const uint8_t *public_ke if (id_equal(public_key, dht->friends_list[i].public_key)) { Client_data *const client_list = dht->friends_list[i].client_list; - if (update_client_data(dht->mono_time, client_list, MAX_FRIEND_CLIENTS, ip_port, nodepublic_key, false)) { + if (update_client_data(dht->mono_time, client_list, MAX_FRIEND_CLIENTS, &ipp_copy, nodepublic_key, false)) { return; } } @@ -1482,7 +1486,7 @@ static int handle_sendnodes_core(void *object, const IP_Port *source, const uint } /* store the address the *request* was sent to */ - addto_lists(dht, *source, packet + 1); + addto_lists(dht, source, packet + 1); *num_nodes_out = num_nodes; @@ -1506,8 +1510,8 @@ static int handle_sendnodes_ipv6(void *object, const IP_Port *source, const uint for (uint32_t i = 0; i < num_nodes; ++i) { if (ipport_isset(&plain_nodes[i].ip_port)) { - ping_node_from_getnodes_ok(dht, plain_nodes[i].public_key, plain_nodes[i].ip_port); - returnedip_ports(dht, plain_nodes[i].ip_port, plain_nodes[i].public_key, packet + 1); + ping_node_from_getnodes_ok(dht, plain_nodes[i].public_key, &plain_nodes[i].ip_port); + returnedip_ports(dht, &plain_nodes[i].ip_port, plain_nodes[i].public_key, packet + 1); } } @@ -1925,7 +1929,7 @@ static int friend_iplist(const DHT *dht, IP_Port *ip_portlist, uint16_t friend_n * @param n A pointer to the number that will be returned from `foreach_ip_port`. * @param userdata The `userdata` pointer passed to `foreach_ip_port`. */ -typedef bool foreach_ip_port_cb(const DHT *dht, IP_Port ip_port, uint32_t *n, void *userdata); +typedef bool foreach_ip_port_cb(const DHT *dht, const IP_Port *ip_port, uint32_t *n, void *userdata); /** * Runs a callback on every active connection for a given DHT friend. @@ -1958,7 +1962,7 @@ static uint32_t foreach_ip_port(const DHT *dht, const DHT_Friend *dht_friend, continue; } - if (!callback(dht, assoc->ip_port, &n, userdata)) { + if (!callback(dht, &assoc->ip_port, &n, userdata)) { /* If the callback is happy with just one of the assocs, we * don't give it the second one. */ break; @@ -1969,7 +1973,7 @@ static uint32_t foreach_ip_port(const DHT *dht, const DHT_Friend *dht_friend, return n; } -static bool send_packet_to_friend(const DHT *dht, IP_Port ip_port, uint32_t *n, void *userdata) +static bool send_packet_to_friend(const DHT *dht, const IP_Port *ip_port, uint32_t *n, void *userdata) { const Packet *packet = (const Packet *)userdata; const int retval = send_packet(dht->net, ip_port, *packet); @@ -2011,10 +2015,10 @@ uint32_t route_to_friend(const DHT *dht, const uint8_t *friend_id, const Packet return foreach_ip_port(dht, dht_friend, send_packet_to_friend, &packet_userdata); } -static bool get_ip_port(const DHT *dht, IP_Port ip_port, uint32_t *n, void *userdata) +static bool get_ip_port(const DHT *dht, const IP_Port *ip_port, uint32_t *n, void *userdata) { IP_Port *ip_list = (IP_Port *)userdata; - ip_list[*n] = ip_port; + ip_list[*n] = *ip_port; ++*n; return true; } @@ -2041,7 +2045,7 @@ static uint32_t routeone_to_friend(const DHT *dht, const uint8_t *friend_id, con return 0; } - const int retval = send_packet(dht->net, ip_list[random_u32() % n], *packet); + const int retval = send_packet(dht->net, &ip_list[random_u32() % n], *packet); if ((unsigned int)retval == packet->length) { return 1; @@ -2162,12 +2166,12 @@ static IP nat_commonip(const IP_Port *ip_portlist, uint16_t len, uint16_t min_nu * * return number of ports and puts the list of ports in portlist. */ -static uint16_t nat_getports(uint16_t *portlist, const IP_Port *ip_portlist, uint16_t len, IP ip) +static uint16_t nat_getports(uint16_t *portlist, const IP_Port *ip_portlist, uint16_t len, const IP *ip) { uint16_t num = 0; for (uint32_t i = 0; i < len; ++i) { - if (ip_equal(&ip_portlist[i].ip, &ip)) { + if (ip_equal(&ip_portlist[i].ip, ip)) { portlist[num] = net_ntohs(ip_portlist[i].port); ++num; } @@ -2176,7 +2180,7 @@ static uint16_t nat_getports(uint16_t *portlist, const IP_Port *ip_portlist, uin return num; } -static void punch_holes(DHT *dht, IP ip, const uint16_t *port_list, uint16_t numports, uint16_t friend_num) +static void punch_holes(DHT *dht, const IP *ip, const uint16_t *port_list, uint16_t numports, uint16_t friend_num) { if (!dht->hole_punching_enabled) { return; @@ -2197,7 +2201,7 @@ static void punch_holes(DHT *dht, IP ip, const uint16_t *port_list, uint16_t num if (i == numports) { /* If all ports are the same, only try that one port. */ IP_Port pinging; - ip_copy(&pinging.ip, &ip); + ip_copy(&pinging.ip, ip); pinging.port = net_htons(first_port); ping_send_request(dht->ping, &pinging, dht->friends_list[friend_num].public_key); } else { @@ -2209,7 +2213,7 @@ static void punch_holes(DHT *dht, IP ip, const uint16_t *port_list, uint16_t num const uint32_t index = (it / 2) % numports; const uint16_t port = port_list[index] + delta; IP_Port pinging; - ip_copy(&pinging.ip, &ip); + ip_copy(&pinging.ip, ip); pinging.port = net_htons(port); ping_send_request(dht->ping, &pinging, dht->friends_list[friend_num].public_key); } @@ -2220,7 +2224,7 @@ static void punch_holes(DHT *dht, IP ip, const uint16_t *port_list, uint16_t num if (dht->friends_list[friend_num].nat.tries > MAX_NORMAL_PUNCHING_TRIES) { const uint16_t port = 1024; IP_Port pinging; - ip_copy(&pinging.ip, &ip); + ip_copy(&pinging.ip, ip); for (i = 0; i < MAX_PUNCHING_PORTS; ++i) { uint32_t it = i + dht->friends_list[friend_num].nat.punching_index2; @@ -2269,8 +2273,8 @@ static void do_NAT(DHT *dht) } uint16_t port_list[MAX_FRIEND_CLIENTS]; - const uint16_t numports = nat_getports(port_list, ip_list, num, ip); - punch_holes(dht, ip, port_list, numports, i); + const uint16_t numports = nat_getports(port_list, ip_list, num, &ip); + punch_holes(dht, &ip, port_list, numports, i); dht->friends_list[i].nat.punching_timestamp = temp_time; dht->friends_list[i].nat.hole_punching = 0; diff --git a/toxcore/DHT.h b/toxcore/DHT.h index d5fa0a164fd..4f4202c8c05 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h @@ -287,12 +287,12 @@ int id_closest(const uint8_t *pk, const uint8_t *pk1, const uint8_t *pk2); * * @return true iff the node was added to the list. */ -bool add_to_list(Node_format *nodes_list, uint32_t length, const uint8_t *pk, IP_Port ip_port, +bool add_to_list(Node_format *nodes_list, uint32_t length, const uint8_t *pk, const IP_Port *ip_port, const uint8_t *cmp_pk); /** Return 1 if node can be added to close list, 0 if it can't. */ -bool node_addable_to_close_list(DHT *dht, const uint8_t *public_key, IP_Port ip_port); +bool node_addable_to_close_list(DHT *dht, const uint8_t *public_key, const IP_Port *ip_port); /** Get the (maximum MAX_SENT_NODES) closest nodes to public_key we know * and put them in nodes_list (must be MAX_SENT_NODES big). @@ -403,7 +403,7 @@ bool dht_isconnected(const DHT *dht); bool dht_non_lan_connected(const DHT *dht); -uint32_t addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *public_key); +uint32_t addto_lists(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key); /** Copies our own ip_port structure to `dest`. WAN addresses take priority over LAN addresses. * diff --git a/toxcore/DHT_test.cc b/toxcore/DHT_test.cc index 9ecd3cb93eb..d5da06d8c75 100644 --- a/toxcore/DHT_test.cc +++ b/toxcore/DHT_test.cc @@ -88,10 +88,11 @@ TEST(AddToList, OverridesKeysWithCloserKeys) { std::array nodes{}; - 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())); - EXPECT_TRUE(add_to_list(nodes.data(), nodes.size(), keys[3].data(), IP_Port(), self_pk.data())); + IP_Port ip_port = {0}; + 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())); + EXPECT_TRUE(add_to_list(nodes.data(), nodes.size(), keys[3].data(), &ip_port, self_pk.data())); EXPECT_EQ(to_array(nodes[0].public_key), keys[0]); EXPECT_EQ(to_array(nodes[1].public_key), keys[1]); @@ -99,9 +100,9 @@ TEST(AddToList, OverridesKeysWithCloserKeys) { EXPECT_EQ(to_array(nodes[3].public_key), keys[3]); // key 4 is less close than keys 0-3 - EXPECT_FALSE(add_to_list(nodes.data(), nodes.size(), keys[4].data(), IP_Port(), self_pk.data())); + EXPECT_FALSE(add_to_list(nodes.data(), nodes.size(), keys[4].data(), &ip_port, self_pk.data())); // 5 is closer than all except key 0 - EXPECT_TRUE(add_to_list(nodes.data(), nodes.size(), keys[5].data(), IP_Port(), self_pk.data())); + EXPECT_TRUE(add_to_list(nodes.data(), nodes.size(), keys[5].data(), &ip_port, self_pk.data())); EXPECT_EQ(to_array(nodes[0].public_key), keys[0]); EXPECT_EQ(to_array(nodes[1].public_key), keys[5]); diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c index f97ed1673c0..11282b9a7b8 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c @@ -274,7 +274,7 @@ bool ip_is_local(const IP *ip) } /* embedded IPv4-in-IPv6 */ - if (ipv6_ipv4_in_v6(ip->ip.v6)) { + if (ipv6_ipv4_in_v6(&ip->ip.v6)) { IP4 ip4; ip4.uint32 = ip->ip.v6.uint32[3]; return ip4_is_local(&ip4); @@ -339,7 +339,7 @@ bool ip_is_lan(const IP *ip) } /* embedded IPv4-in-IPv6 */ - if (ipv6_ipv4_in_v6(ip->ip.v6)) { + if (ipv6_ipv4_in_v6(&ip->ip.v6)) { IP4 ip4; ip4.uint32 = ip->ip.v6.uint32[3]; return ip4_is_lan(&ip4); diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index dfb1c9649db..d4e8e0e09e2 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -1918,7 +1918,7 @@ Messenger *new_messenger(Mono_Time *mono_time, Messenger_Options *options, unsig } else { IP ip; ip_init(&ip, options->ipv6enabled); - m->net = new_networking_ex(m->log, ip, options->port_range[0], options->port_range[1], &net_err); + m->net = new_networking_ex(m->log, &ip, options->port_range[0], options->port_range[1], &net_err); } if (m->net == nullptr) { diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index eab4a5dcbe3..1153da4efaf 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -95,14 +95,16 @@ void tcp_con_set_custom_uint(TCP_Client_Connection *con, uint32_t value) /** return 1 on success * return 0 on failure */ -static int connect_sock_to(const Logger *logger, Socket sock, IP_Port ip_port, const TCP_Proxy_Info *proxy_info) +static int connect_sock_to(const Logger *logger, Socket sock, const IP_Port *ip_port, const TCP_Proxy_Info *proxy_info) { + IP_Port ipp_copy = *ip_port; + if (proxy_info->proxy_type != TCP_PROXY_NONE) { - ip_port = proxy_info->ip_port; + ipp_copy = proxy_info->ip_port; } /* nonblocking socket, connect will never return success */ - net_connect(logger, sock, &ip_port); + net_connect(logger, sock, &ipp_copy); return 1; } @@ -539,7 +541,7 @@ TCP_Client_Connection *new_TCP_connection(const Logger *logger, const Mono_Time return nullptr; } - if (!(set_socket_nonblock(sock) && connect_sock_to(logger, sock, *ip_port, proxy_info))) { + if (!(set_socket_nonblock(sock) && connect_sock_to(logger, sock, ip_port, proxy_info))) { kill_sock(sock); return nullptr; } diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c index 71b660fd0a2..6e14d4f0624 100644 --- a/toxcore/TCP_connection.c +++ b/toxcore/TCP_connection.c @@ -1137,15 +1137,17 @@ static int tcp_relay_on_online(TCP_Connections *tcp_c, int tcp_connections_numbe return 0; } -static int add_tcp_relay_instance(TCP_Connections *tcp_c, IP_Port ip_port, const uint8_t *relay_pk) +static int add_tcp_relay_instance(TCP_Connections *tcp_c, const IP_Port *ip_port, const uint8_t *relay_pk) { - if (net_family_is_tcp_ipv4(ip_port.ip.family)) { - ip_port.ip.family = net_family_ipv4; - } else if (net_family_is_tcp_ipv6(ip_port.ip.family)) { - ip_port.ip.family = net_family_ipv6; + IP_Port ipp_copy = *ip_port; + + if (net_family_is_tcp_ipv4(ipp_copy.ip.family)) { + ipp_copy.ip.family = net_family_ipv4; + } else if (net_family_is_tcp_ipv6(ipp_copy.ip.family)) { + ipp_copy.ip.family = net_family_ipv6; } - if (!net_family_is_ipv4(ip_port.ip.family) && !net_family_is_ipv6(ip_port.ip.family)) { + if (!net_family_is_ipv4(ipp_copy.ip.family) && !net_family_is_ipv6(ipp_copy.ip.family)) { return -1; } @@ -1157,7 +1159,7 @@ static int add_tcp_relay_instance(TCP_Connections *tcp_c, IP_Port ip_port, const TCP_con *tcp_con = &tcp_c->tcp_connections[tcp_connections_number]; - tcp_con->connection = new_TCP_connection(tcp_c->logger, tcp_c->mono_time, &ip_port, relay_pk, tcp_c->self_public_key, + tcp_con->connection = new_TCP_connection(tcp_c->logger, tcp_c->mono_time, &ipp_copy, relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key, &tcp_c->proxy_info); if (!tcp_con->connection) { @@ -1182,7 +1184,7 @@ int add_tcp_relay_global(TCP_Connections *tcp_c, const IP_Port *ip_port, const u return -1; } - if (add_tcp_relay_instance(tcp_c, *ip_port, relay_pk) == -1) { + if (add_tcp_relay_instance(tcp_c, ip_port, relay_pk) == -1) { return -1; } @@ -1254,7 +1256,7 @@ int add_tcp_relay_connection(TCP_Connections *tcp_c, int connections_number, con return -1; } - tcp_connections_number = add_tcp_relay_instance(tcp_c, *ip_port, relay_pk); + tcp_connections_number = add_tcp_relay_instance(tcp_c, ip_port, relay_pk); const TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c index dbfbd4dbc8d..3507f8f4e69 100644 --- a/toxcore/friend_connection.c +++ b/toxcore/friend_connection.c @@ -210,8 +210,11 @@ int getfriend_conn_id_pk(const Friend_Connections *fr_c, const uint8_t *real_pk) * return -1 on failure. * return 0 on success. */ -int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_port, const uint8_t *public_key) +static int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, const IP_Port *ip_port, + const uint8_t *public_key) { + IP_Port ipp_copy = *ip_port; + Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); if (!friend_con) { @@ -219,9 +222,9 @@ int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_ } /* Local ip and same pk means that they are hosting a TCP relay. */ - if (ip_is_local(&ip_port.ip) && public_key_cmp(friend_con->dht_temp_pk, public_key) == 0) { + if (ip_is_local(&ipp_copy.ip) && public_key_cmp(friend_con->dht_temp_pk, public_key) == 0) { if (!net_family_is_unspec(friend_con->dht_ip_port.ip.family)) { - ip_port.ip = friend_con->dht_ip_port.ip; + ipp_copy.ip = friend_con->dht_ip_port.ip; } else { friend_con->hosting_tcp_relay = 0; } @@ -236,11 +239,11 @@ int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_ } } - friend_con->tcp_relays[index].ip_port = ip_port; + friend_con->tcp_relays[index].ip_port = ipp_copy; memcpy(friend_con->tcp_relays[index].public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); ++friend_con->tcp_relay_counter; - return add_tcp_relay_peer(fr_c->net_crypto, friend_con->crypt_connection_id, &ip_port, public_key); + return add_tcp_relay_peer(fr_c->net_crypto, friend_con->crypt_connection_id, &ipp_copy, public_key); } /** Connect to number saved relays for friend. */ @@ -280,7 +283,7 @@ static unsigned int send_relays(Friend_Connections *fr_c, int friendcon_id) for (int i = 0; i < n; ++i) { /* Associated the relays being sent with this connection. * On receiving the peer will do the same which will establish the connection. */ - friend_add_tcp_relay(fr_c, friendcon_id, nodes[i].ip_port, nodes[i].public_key); + friend_add_tcp_relay(fr_c, friendcon_id, &nodes[i].ip_port, nodes[i].public_key); } int length = pack_nodes(data + 1, sizeof(data) - 1, nodes, n); @@ -311,7 +314,7 @@ static int tcp_relay_node_callback(void *object, uint32_t number, const IP_Port } if (friend_con->crypt_connection_id != -1) { - return friend_add_tcp_relay(fr_c, number, *ip_port, public_key); + return friend_add_tcp_relay(fr_c, number, ip_port, public_key); } return add_tcp_relay(fr_c->net_crypto, ip_port, public_key); @@ -337,7 +340,7 @@ static void dht_ip_callback(void *object, int32_t number, const IP_Port *ip_port friend_con->dht_ip_port_lastrecv = mono_time_get(fr_c->mono_time); if (friend_con->hosting_tcp_relay) { - friend_add_tcp_relay(fr_c, number, *ip_port, friend_con->dht_temp_pk); + friend_add_tcp_relay(fr_c, number, ip_port, friend_con->dht_temp_pk); friend_con->hosting_tcp_relay = 0; } } @@ -473,7 +476,7 @@ static int handle_packet(void *object, int number, const uint8_t *data, uint16_t } for (int j = 0; j < n; ++j) { - friend_add_tcp_relay(fr_c, number, nodes[j].ip_port, nodes[j].public_key); + friend_add_tcp_relay(fr_c, number, &nodes[j].ip_port, nodes[j].public_key); } return 0; diff --git a/toxcore/friend_connection.h b/toxcore/friend_connection.h index 937f1cf73b7..ba5154a1395 100644 --- a/toxcore/friend_connection.h +++ b/toxcore/friend_connection.h @@ -79,13 +79,6 @@ int get_friendcon_public_keys(uint8_t *real_pk, uint8_t *dht_temp_pk, const Frie */ void set_dht_temp_pk(Friend_Connections *fr_c, int friendcon_id, const uint8_t *dht_temp_pk, void *userdata); -/** Add a TCP relay associated to the friend. - * - * return -1 on failure. - * return 0 on success. - */ -int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_port, const uint8_t *public_key); - typedef int global_status_cb(void *object, int id, uint8_t status, void *userdata); typedef int fc_status_cb(void *object, int id, uint8_t status, void *userdata); diff --git a/toxcore/network.c b/toxcore/network.c index 3be804d97b8..17aa6b3af17 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -539,8 +539,10 @@ uint16_t net_port(const Networking_Core *net) /* Basic network functions: */ -int send_packet(const Networking_Core *net, IP_Port ip_port, Packet packet) +int send_packet(const Networking_Core *net, const IP_Port *ip_port, Packet packet) { + IP_Port ipp_copy = *ip_port; + if (net_family_is_unspec(net->family)) { /* Socket not initialized */ // TODO(iphydf): Make this an error. Currently, the onion client calls // this via DHT getnodes. @@ -549,15 +551,15 @@ int send_packet(const Networking_Core *net, IP_Port ip_port, Packet packet) } /* socket TOX_AF_INET, but target IP NOT: can't send */ - if (net_family_is_ipv4(net->family) && !net_family_is_ipv4(ip_port.ip.family)) { + if (net_family_is_ipv4(net->family) && !net_family_is_ipv4(ipp_copy.ip.family)) { // TODO(iphydf): Make this an error. Occasionally we try to send to an // all-zero ip_port. LOGGER_WARNING(net->log, "attempted to send message with network family %d (probably IPv6) on IPv4 socket", - ip_port.ip.family.value); + ipp_copy.ip.family.value); return -1; } - if (net_family_is_ipv4(ip_port.ip.family) && net_family_is_ipv6(net->family)) { + if (net_family_is_ipv4(ipp_copy.ip.family) && net_family_is_ipv6(net->family)) { /* must convert to IPV4-in-IPV6 address */ IP6 ip6; @@ -566,37 +568,37 @@ int send_packet(const Networking_Core *net, IP_Port ip_port, Packet packet) ip6.uint32[0] = 0; ip6.uint32[1] = 0; ip6.uint32[2] = net_htonl(0xFFFF); - ip6.uint32[3] = ip_port.ip.ip.v4.uint32; + ip6.uint32[3] = ipp_copy.ip.ip.v4.uint32; - ip_port.ip.family = net_family_ipv6; - ip_port.ip.ip.v6 = ip6; + ipp_copy.ip.family = net_family_ipv6; + ipp_copy.ip.ip.v6 = ip6; } struct sockaddr_storage addr; size_t addrsize; - if (net_family_is_ipv4(ip_port.ip.family)) { + if (net_family_is_ipv4(ipp_copy.ip.family)) { struct sockaddr_in *const addr4 = (struct sockaddr_in *)&addr; addrsize = sizeof(struct sockaddr_in); addr4->sin_family = AF_INET; - addr4->sin_port = ip_port.port; - fill_addr4(&ip_port.ip.ip.v4, &addr4->sin_addr); - } else if (net_family_is_ipv6(ip_port.ip.family)) { + addr4->sin_port = ipp_copy.port; + fill_addr4(&ipp_copy.ip.ip.v4, &addr4->sin_addr); + } else if (net_family_is_ipv6(ipp_copy.ip.family)) { struct sockaddr_in6 *const addr6 = (struct sockaddr_in6 *)&addr; addrsize = sizeof(struct sockaddr_in6); addr6->sin6_family = AF_INET6; - addr6->sin6_port = ip_port.port; - fill_addr6(&ip_port.ip.ip.v6, &addr6->sin6_addr); + addr6->sin6_port = ipp_copy.port; + fill_addr6(&ipp_copy.ip.ip.v6, &addr6->sin6_addr); addr6->sin6_flowinfo = 0; addr6->sin6_scope_id = 0; } else { // TODO(iphydf): Make this an error. Currently this fails sometimes when // called from DHT.c:do_ping_and_sendnode_requests. - LOGGER_WARNING(net->log, "unknown address type: %d", ip_port.ip.family.value); + LOGGER_WARNING(net->log, "unknown address type: %d", ipp_copy.ip.family.value); return -1; } @@ -608,7 +610,7 @@ int send_packet(const Networking_Core *net, IP_Port ip_port, Packet packet) (struct sockaddr *)&addr, addrsize); #endif - loglogdata(net->log, "O=>", packet.data, packet.length, &ip_port, res); + loglogdata(net->log, "O=>", packet.data, packet.length, &ipp_copy, res); assert(res <= INT_MAX); return (int)res; @@ -622,7 +624,7 @@ int send_packet(const Networking_Core *net, IP_Port ip_port, Packet packet) int sendpacket(const Networking_Core *net, const IP_Port *ip_port, const uint8_t *data, uint16_t length) { const Packet packet = {data, length}; - return send_packet(net, *ip_port, packet); + return send_packet(net, ip_port, packet); } /** Function to receive data @@ -687,7 +689,7 @@ static int receivepacket(const Logger *log, Socket sock, IP_Port *ip_port, uint8 get_ip6(&ip_port->ip.ip.v6, &addr_in6->sin6_addr); ip_port->port = addr_in6->sin6_port; - if (ipv6_ipv4_in_v6(ip_port->ip.ip.v6)) { + if (ipv6_ipv4_in_v6(&ip_port->ip.ip.v6)) { ip_port->ip.family = net_family_ipv4; ip_port->ip.ip.v4.uint32 = ip_port->ip.ip.v6.uint32[3]; } @@ -783,7 +785,7 @@ static void at_shutdown(void) /** Initialize networking. * Added for reverse compatibility with old new_networking calls. */ -Networking_Core *new_networking(const Logger *log, IP ip, uint16_t port) +Networking_Core *new_networking(const Logger *log, const IP *ip, uint16_t port) { return new_networking_ex(log, ip, port, port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM), nullptr); } @@ -798,7 +800,8 @@ Networking_Core *new_networking(const Logger *log, IP ip, uint16_t port) * * If error is non NULL it is set to 0 if no issues, 1 if socket related error, 2 if other. */ -Networking_Core *new_networking_ex(const Logger *log, IP ip, uint16_t port_from, uint16_t port_to, unsigned int *error) +Networking_Core *new_networking_ex(const Logger *log, const IP *ip, uint16_t port_from, uint16_t port_to, + unsigned int *error) { /* If both from and to are 0, use default port range * If one is 0 and the other is non-0, use the non-0 value as only port @@ -822,8 +825,8 @@ Networking_Core *new_networking_ex(const Logger *log, IP ip, uint16_t port_from, } /* maybe check for invalid IPs like 224+.x.y.z? if there is any IP set ever */ - if (!net_family_is_ipv4(ip.family) && !net_family_is_ipv6(ip.family)) { - LOGGER_ERROR(log, "invalid address family: %u", ip.family.value); + if (!net_family_is_ipv4(ip->family) && !net_family_is_ipv6(ip->family)) { + LOGGER_ERROR(log, "invalid address family: %u", ip->family.value); return nullptr; } @@ -838,7 +841,7 @@ Networking_Core *new_networking_ex(const Logger *log, IP ip, uint16_t port_from, } temp->log = log; - temp->family = ip.family; + temp->family = ip->family; temp->port = 0; /* Initialize our socket. */ @@ -920,7 +923,7 @@ Networking_Core *new_networking_ex(const Logger *log, IP ip, uint16_t port_from, addrsize = sizeof(struct sockaddr_in); addr4->sin_family = AF_INET; addr4->sin_port = 0; - fill_addr4(&ip.ip.v4, &addr4->sin_addr); + fill_addr4(&ip->ip.v4, &addr4->sin_addr); portptr = &addr4->sin_port; } else if (net_family_is_ipv6(temp->family)) { @@ -929,7 +932,7 @@ Networking_Core *new_networking_ex(const Logger *log, IP ip, uint16_t port_from, addrsize = sizeof(struct sockaddr_in6); addr6->sin6_family = AF_INET6; addr6->sin6_port = 0; - fill_addr6(&ip.ip.v6, &addr6->sin6_addr); + fill_addr6(&ip->ip.v6, &addr6->sin6_addr); addr6->sin6_flowinfo = 0; addr6->sin6_scope_id = 0; @@ -942,7 +945,7 @@ Networking_Core *new_networking_ex(const Logger *log, IP ip, uint16_t port_from, #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION - if (net_family_is_ipv6(ip.family)) { + if (net_family_is_ipv6(ip->family)) { const int is_dualstack = set_socket_dualstack(temp->sock); LOGGER_DEBUG(log, "Dual-stack socket: %s", is_dualstack ? "enabled" : "Failed to enable, won't be able to receive from/send to IPv4 addresses"); @@ -1000,7 +1003,7 @@ Networking_Core *new_networking_ex(const Logger *log, IP ip, uint16_t port_from, temp->port = *portptr; char ip_str[IP_NTOA_LEN]; - LOGGER_DEBUG(log, "Bound successfully to %s:%u", ip_ntoa(&ip, ip_str, sizeof(ip_str)), + LOGGER_DEBUG(log, "Bound successfully to %s:%u", ip_ntoa(ip, ip_str, sizeof(ip_str)), net_ntohs(temp->port)); /* errno isn't reset on success, only set on failure, the failed @@ -1030,7 +1033,7 @@ Networking_Core *new_networking_ex(const Logger *log, IP ip, uint16_t port_from, int neterror = net_error(); char *strerror = net_new_strerror(neterror); LOGGER_ERROR(log, "Failed to bind socket: %d, %s IP: %s port_from: %u port_to: %u", neterror, strerror, - ip_ntoa(&ip, ip_str, sizeof(ip_str)), port_from, port_to); + ip_ntoa(ip, ip_str, sizeof(ip_str)), port_from, port_to); net_kill_strerror(strerror); kill_networking(temp); @@ -1101,13 +1104,13 @@ bool ip_equal(const IP *a, const IP *b) /* different family: check on the IPv6 one if it is the IPv4 one embedded */ if (net_family_is_ipv4(a->family) && net_family_is_ipv6(b->family)) { - if (ipv6_ipv4_in_v6(b->ip.v6)) { + if (ipv6_ipv4_in_v6(&b->ip.v6)) { struct in_addr addr_a; fill_addr4(&a->ip.v4, &addr_a); return addr_a.s_addr == b->ip.v6.uint32[3]; } } else if (net_family_is_ipv6(a->family) && net_family_is_ipv4(b->family)) { - if (ipv6_ipv4_in_v6(a->ip.v6)) { + if (ipv6_ipv4_in_v6(&a->ip.v6)) { struct in_addr addr_b; fill_addr4(&b->ip.v4, &addr_b); return a->ip.v6.uint32[3] == addr_b.s_addr; @@ -1714,9 +1717,9 @@ size_t net_unpack_u64(const uint8_t *bytes, uint64_t *v) return p - bytes; } -bool ipv6_ipv4_in_v6(IP6 a) +bool ipv6_ipv4_in_v6(const IP6 *a) { - return a.uint64[0] == 0 && a.uint32[2] == net_htonl(0xffff); + return a->uint64[0] == 0 && a->uint32[2] == net_htonl(0xffff); } int net_error(void) diff --git a/toxcore/network.h b/toxcore/network.h index 4c7071c1836..91eb7466f92 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -200,7 +200,7 @@ size_t net_unpack_u32(const uint8_t *bytes, uint32_t *v); size_t net_unpack_u64(const uint8_t *bytes, uint64_t *v); /** Does the IP6 struct a contain an IPv4 address in an IPv6 one? */ -bool ipv6_ipv4_in_v6(IP6 a); +bool ipv6_ipv4_in_v6(const IP6 *a); #define TOX_ENABLE_IPV6_DEFAULT true @@ -382,7 +382,7 @@ typedef struct Packet { /** * Function to send a network packet to a given IP/port. */ -int send_packet(const Networking_Core *net, IP_Port ip_port, Packet packet); +int send_packet(const Networking_Core *net, const IP_Port *ip_port, Packet packet); /** * Function to send packet(data) of length length to ip_port. @@ -457,7 +457,7 @@ void net_kill_strerror(char *strerror); /** Initialize networking. * Added for reverse compatibility with old new_networking calls. */ -Networking_Core *new_networking(const Logger *log, IP ip, uint16_t port); +Networking_Core *new_networking(const Logger *log, const IP *ip, uint16_t port); /** Initialize networking. * Bind to ip and port. * ip must be in network order EX: 127.0.0.1 = (7F000001). @@ -468,7 +468,8 @@ Networking_Core *new_networking(const Logger *log, IP ip, uint16_t port); * * If error is non NULL it is set to 0 if no issues, 1 if socket related error, 2 if other. */ -Networking_Core *new_networking_ex(const Logger *log, IP ip, uint16_t port_from, uint16_t port_to, unsigned int *error); +Networking_Core *new_networking_ex(const Logger *log, const IP *ip, uint16_t port_from, uint16_t port_to, + unsigned int *error); Networking_Core *new_networking_no_udp(const Logger *log); /** Function to cleanup networking stuff (doesn't do much right now). */ diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 7d5fb051f2f..8004e0b7c9d 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -178,9 +178,9 @@ int onion_add_bs_path_node(Onion_Client *onion_c, const IP_Port *ip_port, const * return -1 on failure * return 0 on success */ -static int onion_add_path_node(Onion_Client *onion_c, IP_Port ip_port, const uint8_t *public_key) +static int onion_add_path_node(Onion_Client *onion_c, const IP_Port *ip_port, const uint8_t *public_key) { - if (!net_family_is_ipv4(ip_port.ip.family) && !net_family_is_ipv6(ip_port.ip.family)) { + if (!net_family_is_ipv4(ip_port->ip.family) && !net_family_is_ipv6(ip_port->ip.family)) { return -1; } @@ -190,7 +190,7 @@ static int onion_add_path_node(Onion_Client *onion_c, IP_Port ip_port, const uin } } - onion_c->path_nodes[onion_c->path_nodes_index % MAX_PATH_NODES].ip_port = ip_port; + onion_c->path_nodes[onion_c->path_nodes_index % MAX_PATH_NODES].ip_port = *ip_port; memcpy(onion_c->path_nodes[onion_c->path_nodes_index % MAX_PATH_NODES].public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); @@ -436,7 +436,7 @@ static uint32_t set_path_timeouts(Onion_Client *onion_c, uint32_t num, uint32_t if (onion_path_to_nodes(nodes, ONION_PATH_LENGTH, &onion_paths->paths[path_num % NUMBER_ONION_PATHS]) == 0) { for (unsigned int i = 0; i < ONION_PATH_LENGTH; ++i) { - onion_add_path_node(onion_c, nodes[i].ip_port, nodes[i].public_key); + onion_add_path_node(onion_c, &nodes[i].ip_port, nodes[i].public_key); } } @@ -657,7 +657,7 @@ static void sort_onion_node_list(Onion_Node *list, unsigned int length, const Mo } } -static int client_add_to_list(Onion_Client *onion_c, uint32_t num, const uint8_t *public_key, IP_Port ip_port, +static int client_add_to_list(Onion_Client *onion_c, uint32_t num, const uint8_t *public_key, const IP_Port *ip_port, uint8_t is_stored, const uint8_t *pingid_or_key, uint32_t path_used) { if (num > onion_c->num_friends) { @@ -713,7 +713,7 @@ static int client_add_to_list(Onion_Client *onion_c, uint32_t num, const uint8_t } memcpy(list_nodes[index].public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); - list_nodes[index].ip_port = ip_port; + list_nodes[index].ip_port = *ip_port; // TODO(irungentoo): remove this and find a better source of nodes to use for paths. onion_add_path_node(onion_c, ip_port, public_key); @@ -862,7 +862,7 @@ static int handle_announce_response(void *object, const IP_Port *source, const u uint32_t path_used = set_path_timeouts(onion_c, num, path_num); - if (client_add_to_list(onion_c, num, public_key, ip_port, plain[0], plain + 1, path_used) == -1) { + if (client_add_to_list(onion_c, num, public_key, &ip_port, plain[0], plain + 1, path_used) == -1) { return 1; } @@ -1491,7 +1491,7 @@ static void populate_path_nodes(Onion_Client *onion_c) unsigned int num_nodes = randfriends_nodes(onion_c->dht, nodes_list, MAX_FRIEND_CLIENTS); for (unsigned int i = 0; i < num_nodes; ++i) { - onion_add_path_node(onion_c, nodes_list[i].ip_port, nodes_list[i].public_key); + onion_add_path_node(onion_c, &nodes_list[i].ip_port, nodes_list[i].public_key); } } diff --git a/toxcore/ping.c b/toxcore/ping.c index d4401f39117..adc2a70bb19 100644 --- a/toxcore/ping.c +++ b/toxcore/ping.c @@ -225,7 +225,7 @@ static int handle_ping_response(void *object, const IP_Port *source, const uint8 return 1; } - addto_lists(dht, *source, packet + 1); + addto_lists(dht, source, packet + 1); return 0; } @@ -273,7 +273,7 @@ int32_t ping_add(Ping *ping, const uint8_t *public_key, const IP_Port *ip_port) return -1; } - if (!node_addable_to_close_list(ping->dht, public_key, *ip_port)) { + if (!node_addable_to_close_list(ping->dht, public_key, ip_port)) { return -1; } @@ -300,7 +300,7 @@ int32_t ping_add(Ping *ping, const uint8_t *public_key, const IP_Port *ip_port) } } - if (add_to_list(ping->to_ping, MAX_TO_PING, public_key, *ip_port, dht_get_self_public_key(ping->dht))) { + if (add_to_list(ping->to_ping, MAX_TO_PING, public_key, ip_port, dht_get_self_public_key(ping->dht))) { return 0; } @@ -328,7 +328,7 @@ void ping_iterate(Ping *ping) break; } - if (!node_addable_to_close_list(ping->dht, ping->to_ping[i].public_key, ping->to_ping[i].ip_port)) { + if (!node_addable_to_close_list(ping->dht, ping->to_ping[i].public_key, &ping->to_ping[i].ip_port)) { continue; }