summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2018-08-27 16:10:01 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-08-30 21:33:15 +0000
commita5cd4764aa3e5f8f0ba18d8348f5995d255f6c00 (patch)
tree1bb41adb2c1bf79f5ff3a20b762a3bd316ce1f5f /toxcore
parent515196dfa2097a9a9f04eeff5130860fc4137b0f (diff)
Clean up `add_to_list` function a bit.
Can't trivially get rid of recursion here, since it's a non-linear recursive function.
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/DHT.c18
-rw-r--r--toxcore/DHT.h7
2 files changed, 12 insertions, 13 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 68143421..b2f2e625 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -755,30 +755,26 @@ static int client_or_ip_port_in_list(const Logger *log, const Mono_Time *mono_ti
755 return 1; 755 return 1;
756} 756}
757 757
758/* Add node to the node list making sure only the nodes closest to cmp_pk are in the list. 758bool add_to_list(Node_format *nodes_list, uint32_t length, const uint8_t *pk, IP_Port ip_port,
759 */
760bool add_to_list(Node_format *nodes_list, unsigned int length, const uint8_t *pk, IP_Port ip_port,
761 const uint8_t *cmp_pk) 759 const uint8_t *cmp_pk)
762{ 760{
763 uint8_t pk_bak[CRYPTO_PUBLIC_KEY_SIZE]; 761 for (uint32_t i = 0; i < length; ++i) {
764 IP_Port ip_port_bak;
765
766 for (size_t i = 0; i < length; ++i) {
767 if (id_closest(cmp_pk, nodes_list[i].public_key, pk) == 2) { 762 if (id_closest(cmp_pk, nodes_list[i].public_key, pk) == 2) {
763 uint8_t pk_bak[CRYPTO_PUBLIC_KEY_SIZE];
768 memcpy(pk_bak, nodes_list[i].public_key, CRYPTO_PUBLIC_KEY_SIZE); 764 memcpy(pk_bak, nodes_list[i].public_key, CRYPTO_PUBLIC_KEY_SIZE);
769 ip_port_bak = nodes_list[i].ip_port; 765 const IP_Port ip_port_bak = nodes_list[i].ip_port;
770 memcpy(nodes_list[i].public_key, pk, CRYPTO_PUBLIC_KEY_SIZE); 766 memcpy(nodes_list[i].public_key, pk, CRYPTO_PUBLIC_KEY_SIZE);
771 nodes_list[i].ip_port = ip_port; 767 nodes_list[i].ip_port = ip_port;
772 768
773 if (i != (length - 1)) { 769 if (i != length - 1) {
774 add_to_list(nodes_list, length, pk_bak, ip_port_bak, cmp_pk); 770 add_to_list(nodes_list, length, pk_bak, ip_port_bak, cmp_pk);
775 } 771 }
776 772
777 return 1; 773 return true;
778 } 774 }
779 } 775 }
780 776
781 return 0; 777 return false;
782} 778}
783 779
784/* TODO(irungentoo): change this to 7 when done*/ 780/* TODO(irungentoo): change this to 7 when done*/
diff --git a/toxcore/DHT.h b/toxcore/DHT.h
index 4a7af4c7..9f5f1ec7 100644
--- a/toxcore/DHT.h
+++ b/toxcore/DHT.h
@@ -302,9 +302,12 @@ int dht_getfriendip(const DHT *dht, const uint8_t *public_key, IP_Port *ip_port)
302 */ 302 */
303int id_closest(const uint8_t *pk, const uint8_t *pk1, const uint8_t *pk2); 303int id_closest(const uint8_t *pk, const uint8_t *pk1, const uint8_t *pk2);
304 304
305/* Add node to the node list making sure only the nodes closest to cmp_pk are in the list. 305/**
306 * Add node to the node list making sure only the nodes closest to cmp_pk are in the list.
307 *
308 * @return true iff the node was added to the list.
306 */ 309 */
307bool add_to_list(Node_format *nodes_list, unsigned int length, const uint8_t *pk, IP_Port ip_port, 310bool add_to_list(Node_format *nodes_list, uint32_t length, const uint8_t *pk, IP_Port ip_port,
308 const uint8_t *cmp_pk); 311 const uint8_t *cmp_pk);
309 312
310/* Return 1 if node can be added to close list, 0 if it can't. 313/* Return 1 if node can be added to close list, 0 if it can't.