summaryrefslogtreecommitdiff
path: root/toxcore/DHT.c
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2018-01-28 21:30:39 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-01-30 23:35:50 +0000
commit92ffad1a72bc8c422426d52ac408bd71242dd047 (patch)
treef592f353068dd2043525dd2cc04d6124a4ed4bc4 /toxcore/DHT.c
parent623e9ac331df7323660e21c8a2226523a5ee713b (diff)
Use nullptr as NULL pointer constant instead of NULL or 0.
This changes only code, no string literals or comments.
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r--toxcore/DHT.c82
1 files changed, 41 insertions, 41 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 747f7328..4ee84fd2 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -376,7 +376,7 @@ int packed_node_size(uint8_t ip_family)
376 */ 376 */
377int pack_ip_port(uint8_t *data, uint16_t length, const IP_Port *ip_port) 377int pack_ip_port(uint8_t *data, uint16_t length, const IP_Port *ip_port)
378{ 378{
379 if (data == NULL) { 379 if (data == nullptr) {
380 return -1; 380 return -1;
381 } 381 }
382 382
@@ -454,7 +454,7 @@ static int DHT_create_packet(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE],
454 */ 454 */
455int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, uint8_t tcp_enabled) 455int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, uint8_t tcp_enabled)
456{ 456{
457 if (data == NULL) { 457 if (data == nullptr) {
458 return -1; 458 return -1;
459 } 459 }
460 460
@@ -772,7 +772,7 @@ static void get_close_nodes_inner(const uint8_t *public_key, Node_format *nodes_
772 continue; 772 continue;
773 } 773 }
774 774
775 const IPPTsPng *ipptp = NULL; 775 const IPPTsPng *ipptp = nullptr;
776 776
777 if (sa_family == TOX_AF_INET) { 777 if (sa_family == TOX_AF_INET) {
778 ipptp = &client->assoc4; 778 ipptp = &client->assoc4;
@@ -942,8 +942,8 @@ static void sort_client_list(Client_data *list, unsigned int length, const uint8
942 942
943static void update_client_with_reset(Client_data *client, const IP_Port *ip_port) 943static void update_client_with_reset(Client_data *client, const IP_Port *ip_port)
944{ 944{
945 IPPTsPng *ipptp_write = NULL; 945 IPPTsPng *ipptp_write = nullptr;
946 IPPTsPng *ipptp_clear = NULL; 946 IPPTsPng *ipptp_clear = nullptr;
947 947
948 if (ip_port->ip.family == TOX_AF_INET) { 948 if (ip_port->ip.family == TOX_AF_INET) {
949 ipptp_write = &client->assoc4; 949 ipptp_write = &client->assoc4;
@@ -1161,7 +1161,7 @@ uint32_t addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *public_key)
1161 used++; 1161 used++;
1162 } 1162 }
1163 1163
1164 DHT_Friend *friend_foundip = 0; 1164 DHT_Friend *friend_foundip = nullptr;
1165 1165
1166 for (uint32_t i = 0; i < dht->num_friends; ++i) { 1166 for (uint32_t i = 0; i < dht->num_friends; ++i) {
1167 const bool in_list = client_or_ip_port_in_list(dht->log, dht->friends_list[i].client_list, 1167 const bool in_list = client_or_ip_port_in_list(dht->log, dht->friends_list[i].client_list,
@@ -1265,7 +1265,7 @@ static int getnodes(DHT *dht, IP_Port ip_port, const uint8_t *public_key, const
1265 1265
1266 uint64_t ping_id = 0; 1266 uint64_t ping_id = 0;
1267 1267
1268 if (sendback_node != NULL) { 1268 if (sendback_node != nullptr) {
1269 memcpy(plain_message + sizeof(receiver), sendback_node, sizeof(Node_format)); 1269 memcpy(plain_message + sizeof(receiver), sendback_node, sizeof(Node_format));
1270 ping_id = ping_array_add(dht->dht_harden_ping_array, plain_message, sizeof(plain_message)); 1270 ping_id = ping_array_add(dht->dht_harden_ping_array, plain_message, sizeof(plain_message));
1271 } else { 1271 } else {
@@ -1533,7 +1533,7 @@ int DHT_addfriend(DHT *dht, const uint8_t *public_key, void (*ip_callback)(void
1533 1533
1534 DHT_Friend *temp = (DHT_Friend *)realloc(dht->friends_list, sizeof(DHT_Friend) * (dht->num_friends + 1)); 1534 DHT_Friend *temp = (DHT_Friend *)realloc(dht->friends_list, sizeof(DHT_Friend) * (dht->num_friends + 1));
1535 1535
1536 if (temp == NULL) { 1536 if (temp == nullptr) {
1537 return -1; 1537 return -1;
1538 } 1538 }
1539 1539
@@ -1573,8 +1573,8 @@ int DHT_delfriend(DHT *dht, const uint8_t *public_key, uint16_t lock_count)
1573 1573
1574 if (dht_friend->lock_count && lock_count) { /* DHT friend is still in use.*/ 1574 if (dht_friend->lock_count && lock_count) { /* DHT friend is still in use.*/
1575 --lock_count; 1575 --lock_count;
1576 dht_friend->callbacks[lock_count].ip_callback = NULL; 1576 dht_friend->callbacks[lock_count].ip_callback = nullptr;
1577 dht_friend->callbacks[lock_count].data = NULL; 1577 dht_friend->callbacks[lock_count].data = nullptr;
1578 dht_friend->callbacks[lock_count].number = 0; 1578 dht_friend->callbacks[lock_count].number = 0;
1579 return 0; 1579 return 0;
1580 } 1580 }
@@ -1589,13 +1589,13 @@ int DHT_delfriend(DHT *dht, const uint8_t *public_key, uint16_t lock_count)
1589 1589
1590 if (dht->num_friends == 0) { 1590 if (dht->num_friends == 0) {
1591 free(dht->friends_list); 1591 free(dht->friends_list);
1592 dht->friends_list = NULL; 1592 dht->friends_list = nullptr;
1593 return 0; 1593 return 0;
1594 } 1594 }
1595 1595
1596 DHT_Friend *temp = (DHT_Friend *)realloc(dht->friends_list, sizeof(DHT_Friend) * (dht->num_friends)); 1596 DHT_Friend *temp = (DHT_Friend *)realloc(dht->friends_list, sizeof(DHT_Friend) * (dht->num_friends));
1597 1597
1598 if (temp == NULL) { 1598 if (temp == nullptr) {
1599 return -1; 1599 return -1;
1600 } 1600 }
1601 1601
@@ -1664,7 +1664,7 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co
1664 not_kill++; 1664 not_kill++;
1665 1665
1666 if (is_timeout(assoc->last_pinged, PING_INTERVAL)) { 1666 if (is_timeout(assoc->last_pinged, PING_INTERVAL)) {
1667 getnodes(dht, assoc->ip_port, client->public_key, public_key, NULL); 1667 getnodes(dht, assoc->ip_port, client->public_key, public_key, nullptr);
1668 assoc->last_pinged = temp_time; 1668 assoc->last_pinged = temp_time;
1669 } 1669 }
1670 1670
@@ -1696,7 +1696,7 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co
1696 rand_node += rand() % (num_nodes - (rand_node + 1)); 1696 rand_node += rand() % (num_nodes - (rand_node + 1));
1697 } 1697 }
1698 1698
1699 getnodes(dht, assoc_list[rand_node]->ip_port, client_list[rand_node]->public_key, public_key, NULL); 1699 getnodes(dht, assoc_list[rand_node]->ip_port, client_list[rand_node]->public_key, public_key, nullptr);
1700 1700
1701 *lastgetnode = temp_time; 1701 *lastgetnode = temp_time;
1702 ++*bootstrap_times; 1702 ++*bootstrap_times;
@@ -1715,7 +1715,7 @@ static void do_DHT_friends(DHT *dht)
1715 1715
1716 for (size_t j = 0; j < dht_friend->num_to_bootstrap; ++j) { 1716 for (size_t j = 0; j < dht_friend->num_to_bootstrap; ++j) {
1717 getnodes(dht, dht_friend->to_bootstrap[j].ip_port, dht_friend->to_bootstrap[j].public_key, dht_friend->public_key, 1717 getnodes(dht, dht_friend->to_bootstrap[j].ip_port, dht_friend->to_bootstrap[j].public_key, dht_friend->public_key,
1718 NULL); 1718 nullptr);
1719 } 1719 }
1720 1720
1721 dht_friend->num_to_bootstrap = 0; 1721 dht_friend->num_to_bootstrap = 0;
@@ -1732,7 +1732,7 @@ static void do_DHT_friends(DHT *dht)
1732static void do_Close(DHT *dht) 1732static void do_Close(DHT *dht)
1733{ 1733{
1734 for (size_t i = 0; i < dht->num_to_bootstrap; ++i) { 1734 for (size_t i = 0; i < dht->num_to_bootstrap; ++i) {
1735 getnodes(dht, dht->to_bootstrap[i].ip_port, dht->to_bootstrap[i].public_key, dht->self_public_key, NULL); 1735 getnodes(dht, dht->to_bootstrap[i].ip_port, dht->to_bootstrap[i].public_key, dht->self_public_key, nullptr);
1736 } 1736 }
1737 1737
1738 dht->num_to_bootstrap = 0; 1738 dht->num_to_bootstrap = 0;
@@ -1767,18 +1767,18 @@ static void do_Close(DHT *dht)
1767 1767
1768void DHT_getnodes(DHT *dht, const IP_Port *from_ipp, const uint8_t *from_id, const uint8_t *which_id) 1768void DHT_getnodes(DHT *dht, const IP_Port *from_ipp, const uint8_t *from_id, const uint8_t *which_id)
1769{ 1769{
1770 getnodes(dht, *from_ipp, from_id, which_id, NULL); 1770 getnodes(dht, *from_ipp, from_id, which_id, nullptr);
1771} 1771}
1772 1772
1773void DHT_bootstrap(DHT *dht, IP_Port ip_port, const uint8_t *public_key) 1773void DHT_bootstrap(DHT *dht, IP_Port ip_port, const uint8_t *public_key)
1774{ 1774{
1775 getnodes(dht, ip_port, public_key, dht->self_public_key, NULL); 1775 getnodes(dht, ip_port, public_key, dht->self_public_key, nullptr);
1776} 1776}
1777int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enabled, 1777int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enabled,
1778 uint16_t port, const uint8_t *public_key) 1778 uint16_t port, const uint8_t *public_key)
1779{ 1779{
1780 IP_Port ip_port_v64; 1780 IP_Port ip_port_v64;
1781 IP *ip_extra = NULL; 1781 IP *ip_extra = nullptr;
1782 IP_Port ip_port_v4; 1782 IP_Port ip_port_v4;
1783 ip_init(&ip_port_v64.ip, ipv6enabled); 1783 ip_init(&ip_port_v64.ip, ipv6enabled);
1784 1784
@@ -1793,7 +1793,7 @@ int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enable
1793 ip_port_v64.port = port; 1793 ip_port_v64.port = port;
1794 DHT_bootstrap(dht, ip_port_v64, public_key); 1794 DHT_bootstrap(dht, ip_port_v64, public_key);
1795 1795
1796 if ((ip_extra != NULL) && ip_isset(ip_extra)) { 1796 if ((ip_extra != nullptr) && ip_isset(ip_extra)) {
1797 ip_port_v4.port = port; 1797 ip_port_v4.port = port;
1798 DHT_bootstrap(dht, ip_port_v4, public_key); 1798 DHT_bootstrap(dht, ip_port_v4, public_key);
1799 } 1799 }
@@ -2319,7 +2319,7 @@ static IPPTsPng *get_closelist_IPPTsPng(DHT *dht, const uint8_t *public_key, Fam
2319 } 2319 }
2320 } 2320 }
2321 2321
2322 return NULL; 2322 return nullptr;
2323} 2323}
2324 2324
2325/* 2325/*
@@ -2391,7 +2391,7 @@ static int handle_hardening(void *object, IP_Port source, const uint8_t *source_
2391 2391
2392 uint16_t length_nodes = length - 1 - CRYPTO_PUBLIC_KEY_SIZE; 2392 uint16_t length_nodes = length - 1 - CRYPTO_PUBLIC_KEY_SIZE;
2393 Node_format nodes[MAX_SENT_NODES]; 2393 Node_format nodes[MAX_SENT_NODES];
2394 int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, 0, packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, length_nodes, 0); 2394 int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, nullptr, packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, length_nodes, 0);
2395 2395
2396 /* TODO(irungentoo): MAX_SENT_NODES nodes should be returned at all times 2396 /* TODO(irungentoo): MAX_SENT_NODES nodes should be returned at all times
2397 (right now we have a small network size so it could cause problems for testing and etc..) */ 2397 (right now we have a small network size so it could cause problems for testing and etc..) */
@@ -2406,7 +2406,7 @@ static int handle_hardening(void *object, IP_Port source, const uint8_t *source_
2406 2406
2407 IPPTsPng *temp = get_closelist_IPPTsPng(dht, packet + 1, nodes[0].ip_port.ip.family); 2407 IPPTsPng *temp = get_closelist_IPPTsPng(dht, packet + 1, nodes[0].ip_port.ip.family);
2408 2408
2409 if (temp == NULL) { 2409 if (temp == nullptr) {
2410 return 1; 2410 return 1;
2411 } 2411 }
2412 2412
@@ -2465,21 +2465,21 @@ static uint16_t list_nodes(Client_data *list, size_t length, Node_format *nodes,
2465 uint16_t count = 0; 2465 uint16_t count = 0;
2466 2466
2467 for (size_t i = length; i != 0; --i) { 2467 for (size_t i = length; i != 0; --i) {
2468 IPPTsPng *assoc = NULL; 2468 IPPTsPng *assoc = nullptr;
2469 2469
2470 if (!is_timeout(list[i - 1].assoc4.timestamp, BAD_NODE_TIMEOUT)) { 2470 if (!is_timeout(list[i - 1].assoc4.timestamp, BAD_NODE_TIMEOUT)) {
2471 assoc = &list[i - 1].assoc4; 2471 assoc = &list[i - 1].assoc4;
2472 } 2472 }
2473 2473
2474 if (!is_timeout(list[i - 1].assoc6.timestamp, BAD_NODE_TIMEOUT)) { 2474 if (!is_timeout(list[i - 1].assoc6.timestamp, BAD_NODE_TIMEOUT)) {
2475 if (assoc == NULL) { 2475 if (assoc == nullptr) {
2476 assoc = &list[i - 1].assoc6; 2476 assoc = &list[i - 1].assoc6;
2477 } else if (rand() % 2) { 2477 } else if (rand() % 2) {
2478 assoc = &list[i - 1].assoc6; 2478 assoc = &list[i - 1].assoc6;
2479 } 2479 }
2480 } 2480 }
2481 2481
2482 if (assoc != NULL) { 2482 if (assoc != nullptr) {
2483 memcpy(nodes[count].public_key, list[i - 1].public_key, CRYPTO_PUBLIC_KEY_SIZE); 2483 memcpy(nodes[count].public_key, list[i - 1].public_key, CRYPTO_PUBLIC_KEY_SIZE);
2484 nodes[count].ip_port = assoc->ip_port; 2484 nodes[count].ip_port = assoc->ip_port;
2485 ++count; 2485 ++count;
@@ -2635,14 +2635,14 @@ DHT *new_DHT(Logger *log, Networking_Core *net, bool holepunching_enabled)
2635 /* init time */ 2635 /* init time */
2636 unix_time_update(); 2636 unix_time_update();
2637 2637
2638 if (net == NULL) { 2638 if (net == nullptr) {
2639 return NULL; 2639 return nullptr;
2640 } 2640 }
2641 2641
2642 DHT *dht = (DHT *)calloc(1, sizeof(DHT)); 2642 DHT *dht = (DHT *)calloc(1, sizeof(DHT));
2643 2643
2644 if (dht == NULL) { 2644 if (dht == nullptr) {
2645 return NULL; 2645 return nullptr;
2646 } 2646 }
2647 2647
2648 dht->log = log; 2648 dht->log = log;
@@ -2652,9 +2652,9 @@ DHT *new_DHT(Logger *log, Networking_Core *net, bool holepunching_enabled)
2652 2652
2653 dht->ping = ping_new(dht); 2653 dht->ping = ping_new(dht);
2654 2654
2655 if (dht->ping == NULL) { 2655 if (dht->ping == nullptr) {
2656 kill_DHT(dht); 2656 kill_DHT(dht);
2657 return NULL; 2657 return nullptr;
2658 } 2658 }
2659 2659
2660 networking_registerhandler(dht->net, NET_PACKET_GET_NODES, &handle_getnodes, dht); 2660 networking_registerhandler(dht->net, NET_PACKET_GET_NODES, &handle_getnodes, dht);
@@ -2673,9 +2673,9 @@ DHT *new_DHT(Logger *log, Networking_Core *net, bool holepunching_enabled)
2673 uint8_t random_key_bytes[CRYPTO_PUBLIC_KEY_SIZE]; 2673 uint8_t random_key_bytes[CRYPTO_PUBLIC_KEY_SIZE];
2674 random_bytes(random_key_bytes, sizeof(random_key_bytes)); 2674 random_bytes(random_key_bytes, sizeof(random_key_bytes));
2675 2675
2676 if (DHT_addfriend(dht, random_key_bytes, 0, 0, 0, 0) != 0) { 2676 if (DHT_addfriend(dht, random_key_bytes, nullptr, nullptr, 0, nullptr) != 0) {
2677 kill_DHT(dht); 2677 kill_DHT(dht);
2678 return NULL; 2678 return nullptr;
2679 } 2679 }
2680 } 2680 }
2681 2681
@@ -2706,10 +2706,10 @@ void do_DHT(DHT *dht)
2706} 2706}
2707void kill_DHT(DHT *dht) 2707void kill_DHT(DHT *dht)
2708{ 2708{
2709 networking_registerhandler(dht->net, NET_PACKET_GET_NODES, NULL, NULL); 2709 networking_registerhandler(dht->net, NET_PACKET_GET_NODES, nullptr, nullptr);
2710 networking_registerhandler(dht->net, NET_PACKET_SEND_NODES_IPV6, NULL, NULL); 2710 networking_registerhandler(dht->net, NET_PACKET_SEND_NODES_IPV6, nullptr, nullptr);
2711 cryptopacket_registerhandler(dht, CRYPTO_PACKET_NAT_PING, NULL, NULL); 2711 cryptopacket_registerhandler(dht, CRYPTO_PACKET_NAT_PING, nullptr, nullptr);
2712 cryptopacket_registerhandler(dht, CRYPTO_PACKET_HARDENING, NULL, NULL); 2712 cryptopacket_registerhandler(dht, CRYPTO_PACKET_HARDENING, nullptr, nullptr);
2713 ping_array_kill(dht->dht_ping_array); 2713 ping_array_kill(dht->dht_ping_array);
2714 ping_array_kill(dht->dht_harden_ping_array); 2714 ping_array_kill(dht->dht_harden_ping_array);
2715 ping_kill(dht->ping); 2715 ping_kill(dht->ping);
@@ -2817,7 +2817,7 @@ void DHT_save(DHT *dht, uint8_t *data)
2817/* Start sending packets after DHT loaded_friends_list and loaded_clients_list are set */ 2817/* Start sending packets after DHT loaded_friends_list and loaded_clients_list are set */
2818int DHT_connect_after_load(DHT *dht) 2818int DHT_connect_after_load(DHT *dht)
2819{ 2819{
2820 if (dht == NULL) { 2820 if (dht == nullptr) {
2821 return -1; 2821 return -1;
2822 } 2822 }
2823 2823
@@ -2828,7 +2828,7 @@ int DHT_connect_after_load(DHT *dht)
2828 /* DHT is connected, stop. */ 2828 /* DHT is connected, stop. */
2829 if (DHT_non_lan_connected(dht)) { 2829 if (DHT_non_lan_connected(dht)) {
2830 free(dht->loaded_nodes_list); 2830 free(dht->loaded_nodes_list);
2831 dht->loaded_nodes_list = NULL; 2831 dht->loaded_nodes_list = nullptr;
2832 dht->loaded_num_nodes = 0; 2832 dht->loaded_num_nodes = 0;
2833 return 0; 2833 return 0;
2834 } 2834 }
@@ -2857,7 +2857,7 @@ static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t le
2857 // Copy to loaded_clients_list 2857 // Copy to loaded_clients_list
2858 dht->loaded_nodes_list = (Node_format *)calloc(MAX_SAVED_DHT_NODES, sizeof(Node_format)); 2858 dht->loaded_nodes_list = (Node_format *)calloc(MAX_SAVED_DHT_NODES, sizeof(Node_format));
2859 2859
2860 int num = unpack_nodes(dht->loaded_nodes_list, MAX_SAVED_DHT_NODES, NULL, data, length, 0); 2860 int num = unpack_nodes(dht->loaded_nodes_list, MAX_SAVED_DHT_NODES, nullptr, data, length, 0);
2861 2861
2862 if (num > 0) { 2862 if (num > 0) {
2863 dht->loaded_num_nodes = num; 2863 dht->loaded_num_nodes = num;