Wrote random_path function.

Added onion_client to the build system.
This commit is contained in:
irungentoo 2014-01-16 10:00:36 -05:00
parent aff78b159c
commit 9fcb707ec4
4 changed files with 94 additions and 2 deletions

View File

@ -2034,6 +2034,7 @@ uint16_t closelist_nodes(DHT *dht, Node_format *nodes, uint16_t max_num)
Client_data *list = dht->close_clientlist;
uint32_t i;
for (i = LCLIENT_LIST; i != 0; --i) {
IPPTsPng *assoc = NULL;
@ -2060,6 +2061,83 @@ uint16_t closelist_nodes(DHT *dht, Node_format *nodes, uint16_t max_num)
return count;
}
/* Put a random node from list of list_size in node. LAN_ok is 1 if LAN ips are ok, 0 if we don't want them. */
static int random_node_fromlist(Client_data *list, uint16_t list_size, Node_format *node, uint8_t LAN_ok)
{
uint32_t i;
uint32_t num_nodes = 0;
Client_data *client_list[list_size * 2];
IPPTsPng *assoc_list[list_size * 2];
for (i = 0; i < list_size; i++) {
/* If node is not dead. */
Client_data *client = &list[i];
IPPTsPng *assoc;
uint32_t a;
for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4) {
/* If node is good. */
if (!is_timeout(assoc->timestamp, BAD_NODE_TIMEOUT)) {
if (!LAN_ok) {
if (LAN_ip(assoc->ip_port.ip) == 0)
continue;
}
client_list[num_nodes] = client;
assoc_list[num_nodes] = assoc;
++num_nodes;
}
}
}
if (num_nodes == 0)
return -1;
uint32_t rand_node = rand() % num_nodes;
node->ip_port = assoc_list[rand_node]->ip_port;
memcpy(node->client_id, client_list[rand_node]->client_id, CLIENT_ID_SIZE);
return 0;
}
/* Put up to max_num random nodes in nodes.
*
* return the number of nodes.
*
* NOTE:this is used to pick nodes for paths.
*/
uint16_t random_nodes_path(DHT *dht, Node_format *nodes, uint16_t max_num)
{
if (max_num == 0)
return 0;
uint16_t count = 0;
Client_data *list = NULL;
uint16_t list_size = 0;
uint32_t i;
for (i = 0; i < max_num; ++i) {
uint16_t rand_num = rand() % dht->num_friends + 1;
if (rand_num == dht->num_friends) {
list = dht->close_clientlist;
list_size = LCLIENT_LIST;
} else {
list = dht->friends_list[rand_num].client_list;
list_size = MAX_FRIEND_CLIENTS;
}
uint8_t LAN_ok = 1;
if (count != 0 && LAN_ip(nodes[0].ip_port.ip) != 0)
LAN_ok = 0;
if (random_node_fromlist(list, list_size, &nodes[count], LAN_ok) == 0)
++count;
}
return count;
}
void do_hardening(DHT *dht)
{
uint32_t i;

View File

@ -227,6 +227,14 @@ int get_close_nodes(DHT *dht, uint8_t *client_id, Node_format *nodes_list, sa_fa
*/
uint16_t closelist_nodes(DHT *dht, Node_format *nodes, uint16_t max_num);
/* Put up to max_num random nodes in nodes.
*
* return the number of nodes.
*
* NOTE:this is used to pick nodes for paths.
*/
uint16_t random_nodes_path(DHT *dht, Node_format *nodes, uint16_t max_num);
/* Run this function at least a couple times per second (It's the main loop). */
void do_DHT(DHT *dht);

View File

@ -33,6 +33,8 @@ libtoxcore_la_SOURCES = ../toxcore/DHT.h \
../toxcore/onion.c \
../toxcore/onion_announce.h \
../toxcore/onion_announce.c \
../toxcore/onion_client.h \
../toxcore/onion_client.c \
../toxcore/misc_tools.h
libtoxcore_la_CFLAGS = -I$(top_srcdir) \

View File

@ -447,6 +447,8 @@ static int send_fakeid_announce(Onion_Client *onion_c, uint16_t friend_num)
uint16_t num_nodes = closelist_nodes(onion_c->dht, nodes, MAX_SENT_NODES);
memcpy(data + FAKEID_DATA_MIN_LENGTH, nodes, sizeof(Node_format) * num_nodes);
return send_onion_data(onion_c, friend_num, data, FAKEID_DATA_MIN_LENGTH + sizeof(Node_format) * num_nodes);
//TODO: somehow make this function send our DHT client id directly to the other if we know theirs but they don't
//seem to know ours.
}
/* Get the friend_num of a friend.
@ -583,8 +585,10 @@ int onion_getfriendip(Onion_Client *onion_c, int friend_num, IP_Port *ip_port)
*/
int random_path(Onion_Client *onion_c, Node_format *nodes)
{
//TODO
return -1;
if (random_nodes_path(onion_c->dht, nodes, 3) != 3)
return -1;
return 0;
}
#define ANNOUNCE_FRIEND 30