/* Create a new path or use an old suitable one (if pathnum is valid) * or a rondom one from onion_paths. * * return -1 on failure * return 0 on success * * TODO: Make this function better, it currently probably is vulnerable to some attacks that * could de anonimize us. */ static int random_path(const DHT *dht, Onion_Client_Paths *onion_paths, uint32_t pathnum, Onion_Path *path) { if (pathnum >= NUMBER_ONION_PATHS) pathnum = rand() % NUMBER_ONION_PATHS; if (is_timeout(onion_paths->last_path_success[pathnum], ONION_PATH_TIMEOUT) || is_timeout(onion_paths->path_creation_time[pathnum], ONION_PATH_MAX_LIFETIME)) { Node_format nodes[3]; if (random_nodes_path(dht, nodes, 3) != 3) return -1; int n = is_path_used(onion_paths, nodes); if (n == -1) { if (create_onion_path(dht, &onion_paths->paths[pathnum], nodes) == -1) return -1; onion_paths->last_path_success[pathnum] = unix_time() + ONION_PATH_FIRST_TIMEOUT - ONION_PATH_TIMEOUT; onion_paths->path_creation_time[pathnum] = unix_time(); } else { pathnum = n; } } memcpy(path, &onion_paths->paths[pathnum], sizeof(Onion_Path)); return 0; }
/* Put up to max_num random nodes in nodes. * * return the number of nodes. */ static uint16_t random_nodes_path_onion(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 (DHT_non_lan_connected(onion_c->dht)) { if (DHT_isconnected(onion_c->dht)) { if (num_nodes < 3) return random_nodes_path(onion_c->dht, nodes, max_num); for (i = 0; i < max_num; ++i) { nodes[i] = onion_c->path_nodes[rand() % num_nodes]; } } else { if (num_nodes == 0) return 0; nodes[0].ip_port.ip.family = TCP_FAMILY; for (i = 1; i < max_num; ++i) { nodes[i] = onion_c->path_nodes[rand() % num_nodes]; } } return max_num; }
/* Takes 3 random nodes that we know and puts them in nodes * * nodes must be longer than 3. * * return -1 on failure * return 0 on success * */ int random_path(Onion_Client *onion_c, Node_format *nodes) { if (random_nodes_path(onion_c->dht, nodes, 3) != 3) return -1; return 0; }