diff options
-rw-r--r-- | toxcore/onion_client.c | 39 |
1 files changed, 33 insertions, 6 deletions
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 9b63d253..fbabea6d 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c | |||
@@ -49,11 +49,33 @@ static int add_path_node(Onion_Client *onion_c, Node_format *node) | |||
49 | 49 | ||
50 | onion_c->path_nodes[onion_c->path_nodes_index % MAX_PATH_NODES] = *node; | 50 | onion_c->path_nodes[onion_c->path_nodes_index % MAX_PATH_NODES] = *node; |
51 | 51 | ||
52 | uint16_t last = onion_c->path_nodes_index; | ||
52 | ++onion_c->path_nodes_index; | 53 | ++onion_c->path_nodes_index; |
53 | 54 | ||
55 | if (onion_c->path_nodes_index < last) | ||
56 | onion_c->path_nodes_index = MAX_PATH_NODES + 1; | ||
57 | |||
54 | return 0; | 58 | return 0; |
55 | } | 59 | } |
56 | 60 | ||
61 | /* Put up to max_num random nodes in nodes. | ||
62 | * | ||
63 | * return the number of nodes. | ||
64 | */ | ||
65 | static uint16_t random_nodes_path_onion(const Onion_Client *onion_c, Node_format *nodes, uint16_t max_num) | ||
66 | { | ||
67 | if (onion_c->path_nodes_index < 3) | ||
68 | return random_nodes_path(onion_c->dht, nodes, max_num); | ||
69 | |||
70 | unsigned int i, num_nodes = onion_c->path_nodes_index < MAX_PATH_NODES ? onion_c->path_nodes_index : MAX_PATH_NODES; | ||
71 | |||
72 | for (i = 0; i < max_num; ++i) { | ||
73 | nodes[i] = onion_c->path_nodes[rand() % num_nodes]; | ||
74 | } | ||
75 | |||
76 | return num_nodes; | ||
77 | } | ||
78 | |||
57 | /* | 79 | /* |
58 | * return -1 if nodes are suitable for creating a new path. | 80 | * return -1 if nodes are suitable for creating a new path. |
59 | * return path number of already existing similar path if one already exists. | 81 | * return path number of already existing similar path if one already exists. |
@@ -88,7 +110,7 @@ static int is_path_used(const Onion_Client_Paths *onion_paths, const Node_format | |||
88 | * TODO: Make this function better, it currently probably is vulnerable to some attacks that | 110 | * TODO: Make this function better, it currently probably is vulnerable to some attacks that |
89 | * could de anonimize us. | 111 | * could de anonimize us. |
90 | */ | 112 | */ |
91 | static int random_path(const DHT *dht, Onion_Client_Paths *onion_paths, uint32_t pathnum, Onion_Path *path) | 113 | static int random_path(const Onion_Client *onion_c, Onion_Client_Paths *onion_paths, uint32_t pathnum, Onion_Path *path) |
92 | { | 114 | { |
93 | if (pathnum >= NUMBER_ONION_PATHS) | 115 | if (pathnum >= NUMBER_ONION_PATHS) |
94 | pathnum = rand() % NUMBER_ONION_PATHS; | 116 | pathnum = rand() % NUMBER_ONION_PATHS; |
@@ -97,13 +119,13 @@ static int random_path(const DHT *dht, Onion_Client_Paths *onion_paths, uint32_t | |||
97 | || is_timeout(onion_paths->path_creation_time[pathnum], ONION_PATH_MAX_LIFETIME)) { | 119 | || is_timeout(onion_paths->path_creation_time[pathnum], ONION_PATH_MAX_LIFETIME)) { |
98 | Node_format nodes[3]; | 120 | Node_format nodes[3]; |
99 | 121 | ||
100 | if (random_nodes_path(dht, nodes, 3) != 3) | 122 | if (random_nodes_path_onion(onion_c, nodes, 3) != 3) |
101 | return -1; | 123 | return -1; |
102 | 124 | ||
103 | int n = is_path_used(onion_paths, nodes); | 125 | int n = is_path_used(onion_paths, nodes); |
104 | 126 | ||
105 | if (n == -1) { | 127 | if (n == -1) { |
106 | if (create_onion_path(dht, &onion_paths->paths[pathnum], nodes) == -1) | 128 | if (create_onion_path(onion_c->dht, &onion_paths->paths[pathnum], nodes) == -1) |
107 | return -1; | 129 | return -1; |
108 | 130 | ||
109 | onion_paths->last_path_success[pathnum] = unix_time() + ONION_PATH_FIRST_TIMEOUT - ONION_PATH_TIMEOUT; | 131 | onion_paths->last_path_success[pathnum] = unix_time() + ONION_PATH_FIRST_TIMEOUT - ONION_PATH_TIMEOUT; |
@@ -242,7 +264,7 @@ static int client_send_announce_request(Onion_Client *onion_c, uint32_t num, IP_ | |||
242 | memcpy(dest_node.client_id, dest_pubkey, crypto_box_PUBLICKEYBYTES); | 264 | memcpy(dest_node.client_id, dest_pubkey, crypto_box_PUBLICKEYBYTES); |
243 | 265 | ||
244 | if (num == 0) { | 266 | if (num == 0) { |
245 | if (random_path(onion_c->dht, &onion_c->onion_paths, pathnum, &path) == -1) | 267 | if (random_path(onion_c, &onion_c->onion_paths, pathnum, &path) == -1) |
246 | return -1; | 268 | return -1; |
247 | 269 | ||
248 | uint8_t packet[ONION_MAX_PACKET_SIZE]; | 270 | uint8_t packet[ONION_MAX_PACKET_SIZE]; |
@@ -255,7 +277,7 @@ static int client_send_announce_request(Onion_Client *onion_c, uint32_t num, IP_ | |||
255 | 277 | ||
256 | return send_onion_packet_tcp_udp(onion_c, path.ip_port1, packet, len); | 278 | return send_onion_packet_tcp_udp(onion_c, path.ip_port1, packet, len); |
257 | } else { | 279 | } else { |
258 | if (random_path(onion_c->dht, &onion_c->friends_list[num - 1].onion_paths, pathnum, &path) == -1) | 280 | if (random_path(onion_c, &onion_c->friends_list[num - 1].onion_paths, pathnum, &path) == -1) |
259 | return -1; | 281 | return -1; |
260 | 282 | ||
261 | uint8_t packet[ONION_MAX_PACKET_SIZE]; | 283 | uint8_t packet[ONION_MAX_PACKET_SIZE]; |
@@ -346,6 +368,11 @@ static int client_add_to_list(Onion_Client *onion_c, uint32_t num, const uint8_t | |||
346 | memcpy(list_nodes[index].client_id, public_key, CLIENT_ID_SIZE); | 368 | memcpy(list_nodes[index].client_id, public_key, CLIENT_ID_SIZE); |
347 | list_nodes[index].ip_port = ip_port; | 369 | list_nodes[index].ip_port = ip_port; |
348 | 370 | ||
371 | Node_format add_node; //TODO: remove this and find a better source of nodes to use for paths. | ||
372 | memcpy(add_node.client_id, public_key, CLIENT_ID_SIZE); | ||
373 | add_node.ip_port = ip_port; | ||
374 | add_path_node(onion_c, &add_node); | ||
375 | |||
349 | if (is_stored) { | 376 | if (is_stored) { |
350 | memcpy(list_nodes[index].data_public_key, pingid_or_key, crypto_box_PUBLICKEYBYTES); | 377 | memcpy(list_nodes[index].data_public_key, pingid_or_key, crypto_box_PUBLICKEYBYTES); |
351 | } else { | 378 | } else { |
@@ -615,7 +642,7 @@ int send_onion_data(const Onion_Client *onion_c, int friend_num, const uint8_t * | |||
615 | ++num_nodes; | 642 | ++num_nodes; |
616 | 643 | ||
617 | if (list_nodes[i].is_stored) { | 644 | if (list_nodes[i].is_stored) { |
618 | if (random_path(onion_c->dht, &onion_c->friends_list[friend_num].onion_paths, ~0, &path[num_good]) == -1) | 645 | if (random_path(onion_c, &onion_c->friends_list[friend_num].onion_paths, ~0, &path[num_good]) == -1) |
619 | continue; | 646 | continue; |
620 | 647 | ||
621 | good_nodes[num_good] = i; | 648 | good_nodes[num_good] = i; |