diff options
Diffstat (limited to 'protocols/Tox/libtox/src/toxcore/onion_client.c')
-rw-r--r-- | protocols/Tox/libtox/src/toxcore/onion_client.c | 46 |
1 files changed, 26 insertions, 20 deletions
diff --git a/protocols/Tox/libtox/src/toxcore/onion_client.c b/protocols/Tox/libtox/src/toxcore/onion_client.c index 1c3b34dabf..d11a91e81d 100644 --- a/protocols/Tox/libtox/src/toxcore/onion_client.c +++ b/protocols/Tox/libtox/src/toxcore/onion_client.c @@ -232,27 +232,35 @@ static int onion_add_path_node(Onion_Client *onion_c, IP_Port ip_port, const uin */ uint16_t onion_backup_nodes(const Onion_Client *onion_c, Node_format *nodes, uint16_t max_num) { - unsigned int i; - if (!max_num) { return 0; } - unsigned int num_nodes = (onion_c->path_nodes_index < MAX_PATH_NODES) ? onion_c->path_nodes_index : MAX_PATH_NODES; + const uint16_t num_nodes = min_u16(onion_c->path_nodes_index, MAX_PATH_NODES); + uint16_t i = 0; - if (num_nodes == 0) { - return 0; + while (i < max_num && i < num_nodes) { + nodes[i] = onion_c->path_nodes[(onion_c->path_nodes_index - (1 + i)) % num_nodes]; + ++i; } - if (num_nodes < max_num) { - max_num = num_nodes; - } + for (uint16_t j = 0; i < max_num && j < MAX_PATH_NODES && j < onion_c->path_nodes_index_bs; ++j) { + bool already_saved = false; - for (i = 0; i < max_num; ++i) { - nodes[i] = onion_c->path_nodes[(onion_c->path_nodes_index - (1 + i)) % num_nodes]; + for (uint16_t k = 0; k < num_nodes; ++k) { + if (public_key_cmp(nodes[k].public_key, onion_c->path_nodes_bs[j].public_key) == 0) { + already_saved = true; + break; + } + } + + if (!already_saved) { + nodes[i] = onion_c->path_nodes_bs[j]; + ++i; + } } - return max_num; + return i; } /* Put up to max_num random nodes in nodes. @@ -267,7 +275,7 @@ static uint16_t random_nodes_path_onion(const Onion_Client *onion_c, Node_format return 0; } - const uint32_t num_nodes = (onion_c->path_nodes_index < MAX_PATH_NODES) ? onion_c->path_nodes_index : MAX_PATH_NODES; + const uint16_t num_nodes = min_u16(onion_c->path_nodes_index, MAX_PATH_NODES); // if (dht_non_lan_connected(onion_c->dht)) { if (dht_isconnected(onion_c->dht)) { @@ -293,8 +301,7 @@ static uint16_t random_nodes_path_onion(const Onion_Client *onion_c, Node_format nodes[i] = onion_c->path_nodes[random_u32() % num_nodes]; } } else { - unsigned int num_nodes_bs = (onion_c->path_nodes_index_bs < MAX_PATH_NODES) ? onion_c->path_nodes_index_bs : - MAX_PATH_NODES; + const uint16_t num_nodes_bs = min_u16(onion_c->path_nodes_index_bs, MAX_PATH_NODES); if (num_nodes_bs == 0) { return 0; @@ -1611,9 +1618,8 @@ static void do_friend(Onion_Client *onion_c, uint16_t friendnum) } if (count != MAX_ONION_CLIENTS) { - unsigned int num_nodes = (onion_c->path_nodes_index < MAX_PATH_NODES) ? onion_c->path_nodes_index : MAX_PATH_NODES; - - unsigned int n = num_nodes; + const uint16_t num_nodes = min_u16(onion_c->path_nodes_index, MAX_PATH_NODES); + uint16_t n = num_nodes; if (num_nodes > (MAX_ONION_CLIENTS / 2)) { n = (MAX_ONION_CLIENTS / 2); @@ -1732,14 +1738,14 @@ static void do_announce(Onion_Client *onion_c) } if (count != MAX_ONION_CLIENTS_ANNOUNCE) { - unsigned int num_nodes; + uint16_t num_nodes; Node_format *path_nodes; if (random_u08() % 2 == 0 || onion_c->path_nodes_index == 0) { - num_nodes = (onion_c->path_nodes_index_bs < MAX_PATH_NODES) ? onion_c->path_nodes_index_bs : MAX_PATH_NODES; + num_nodes = min_u16(onion_c->path_nodes_index_bs, MAX_PATH_NODES); path_nodes = onion_c->path_nodes_bs; } else { - num_nodes = (onion_c->path_nodes_index < MAX_PATH_NODES) ? onion_c->path_nodes_index : MAX_PATH_NODES; + num_nodes = min_u16(onion_c->path_nodes_index, MAX_PATH_NODES); path_nodes = onion_c->path_nodes; } |