Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Make add_to_list non-recursive. #2688

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions toxcore/DHT.c
Original file line number Diff line number Diff line change
Expand Up @@ -615,27 +615,42 @@ bool add_to_list(
Node_format *nodes_list, uint32_t length, const uint8_t pk[CRYPTO_PUBLIC_KEY_SIZE],
const IP_Port *ip_port, const uint8_t cmp_pk[CRYPTO_PUBLIC_KEY_SIZE])
{
for (uint32_t i = 0; i < length; ++i) {
Node_format *node = &nodes_list[i];
uint8_t pk_cur[CRYPTO_PUBLIC_KEY_SIZE];
memcpy(pk_cur, pk, CRYPTO_PUBLIC_KEY_SIZE);
IP_Port ip_port_cur = *ip_port;

if (id_closest(cmp_pk, node->public_key, pk) == 2) {
uint8_t pk_bak[CRYPTO_PUBLIC_KEY_SIZE];
memcpy(pk_bak, node->public_key, CRYPTO_PUBLIC_KEY_SIZE);
bool inserted = false;
bool done = false;

const IP_Port ip_port_bak = node->ip_port;
memcpy(node->public_key, pk, CRYPTO_PUBLIC_KEY_SIZE);
while (!done) {
done = true;

node->ip_port = *ip_port;
for (uint32_t i = 0; i < length; ++i) {
Node_format *node = &nodes_list[i];

if (i != length - 1) {
add_to_list(nodes_list, length, pk_bak, &ip_port_bak, cmp_pk);
}
if (id_closest(cmp_pk, node->public_key, pk_cur) == 2) {
uint8_t pk_bak[CRYPTO_PUBLIC_KEY_SIZE];
memcpy(pk_bak, node->public_key, CRYPTO_PUBLIC_KEY_SIZE);

return true;
const IP_Port ip_port_bak = node->ip_port;
memcpy(node->public_key, pk_cur, CRYPTO_PUBLIC_KEY_SIZE);

node->ip_port = ip_port_cur;

if (i == length - 1) {
return true;
}

memcpy(pk_cur, pk_bak, CRYPTO_PUBLIC_KEY_SIZE);
ip_port_cur = ip_port_bak;
done = false;
inserted = true;
break;
}
}
}

return false;
return inserted;
}

/**
Expand Down
Loading