From f0b2cd7ad2b6f8223161a6ed850c7bc6548dde62 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Thu, 14 Aug 2014 17:34:42 -0400 Subject: [PATCH] Function to save path nodes. Forgot to commit it with before last commit. --- toxcore/onion_client.c | 26 ++++++++++++++++++++++++++ toxcore/onion_client.h | 6 ++++++ 2 files changed, 32 insertions(+) diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 719bd261..424711d5 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -59,6 +59,32 @@ int onion_add_path_node(Onion_Client *onion_c, IP_Port ip_port, const uint8_t *c return 0; } +/* Put up to max_num nodes in nodes. + * + * return the number of nodes. + */ +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; + + if (num_nodes == 0) + return 0; + + if (num_nodes < max_num) + max_num = num_nodes; + + for (i = 0; i < max_num; ++i) { + nodes[i] = onion_c->path_nodes[(onion_c->path_nodes_index - (1 + i)) % num_nodes]; + } + + return max_num; +} + /* Put up to max_num random nodes in nodes. * * return the number of nodes. diff --git a/toxcore/onion_client.h b/toxcore/onion_client.h index 61e0eca9..d0393712 100644 --- a/toxcore/onion_client.h +++ b/toxcore/onion_client.h @@ -145,6 +145,12 @@ typedef struct { */ int onion_add_path_node(Onion_Client *onion_c, IP_Port ip_port, const uint8_t *client_id); +/* Put up to max_num nodes in nodes. + * + * return the number of nodes. + */ +uint16_t onion_backup_nodes(const Onion_Client *onion_c, Node_format *nodes, uint16_t max_num); + /* Add a friend who we want to connect to. * * return -1 on failure.