summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-12-25 15:04:31 -0500
committerirungentoo <irungentoo@gmail.com>2014-12-25 15:04:31 -0500
commit895f269285d15c54b47646a42f77603a7f83bfd9 (patch)
treec9ee6e42bb85e16c887bd4c8ae554ec2bd9599b6
parent18b45ffade3984b30739eed793a79d26cbbb1c8a (diff)
Onion client improvements.
Put bootstrap nodes in a separate list than known good nodes.
-rw-r--r--toxcore/Messenger.c2
-rw-r--r--toxcore/onion_client.c93
-rw-r--r--toxcore/onion_client.h7
-rw-r--r--toxcore/tox.c2
4 files changed, 87 insertions, 17 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 6264eddd..f9bd68c2 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -2796,7 +2796,7 @@ static int messenger_load_state_callback(void *outer, const uint8_t *data, uint3
2796 uint32_t i; 2796 uint32_t i;
2797 2797
2798 for (i = 0; i < NUM_SAVED_PATH_NODES; ++i) { 2798 for (i = 0; i < NUM_SAVED_PATH_NODES; ++i) {
2799 onion_add_path_node(m->onion_c, nodes[i].ip_port, nodes[i].client_id); 2799 onion_add_bs_path_node(m->onion_c, nodes[i].ip_port, nodes[i].client_id);
2800 } 2800 }
2801 2801
2802 break; 2802 break;
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c
index 288f5649..d2fb9449 100644
--- a/toxcore/onion_client.c
+++ b/toxcore/onion_client.c
@@ -33,12 +33,42 @@
33#define ANNOUNCE_ARRAY_SIZE 256 33#define ANNOUNCE_ARRAY_SIZE 256
34#define ANNOUNCE_TIMEOUT 10 34#define ANNOUNCE_TIMEOUT 10
35 35
36/* Add a node to the path_nodes bootstrap array.
37 *
38 * return -1 on failure
39 * return 0 on success
40 */
41int onion_add_bs_path_node(Onion_Client *onion_c, IP_Port ip_port, const uint8_t *client_id)
42{
43 if (ip_port.ip.family != AF_INET && ip_port.ip.family != AF_INET6)
44 return -1;
45
46 unsigned int i;
47
48 for (i = 0; i < MAX_PATH_NODES; ++i) {
49 if (memcmp(client_id, onion_c->path_nodes_bs[i].client_id, crypto_box_PUBLICKEYBYTES) == 0)
50 return -1;
51 }
52
53 onion_c->path_nodes_bs[onion_c->path_nodes_index_bs % MAX_PATH_NODES].ip_port = ip_port;
54 memcpy(onion_c->path_nodes_bs[onion_c->path_nodes_index_bs % MAX_PATH_NODES].client_id, client_id,
55 crypto_box_PUBLICKEYBYTES);
56
57 uint16_t last = onion_c->path_nodes_index_bs;
58 ++onion_c->path_nodes_index_bs;
59
60 if (onion_c->path_nodes_index_bs < last)
61 onion_c->path_nodes_index_bs = MAX_PATH_NODES + 1;
62
63 return 0;
64}
65
36/* Add a node to the path_nodes array. 66/* Add a node to the path_nodes array.
37 * 67 *
38 * return -1 on failure 68 * return -1 on failure
39 * return 0 on success 69 * return 0 on success
40 */ 70 */
41int onion_add_path_node(Onion_Client *onion_c, IP_Port ip_port, const uint8_t *client_id) 71static int onion_add_path_node(Onion_Client *onion_c, IP_Port ip_port, const uint8_t *client_id)
42{ 72{
43 if (ip_port.ip.family != AF_INET && ip_port.ip.family != AF_INET6) 73 if (ip_port.ip.family != AF_INET && ip_port.ip.family != AF_INET6)
44 return -1; 74 return -1;
@@ -110,13 +140,24 @@ static uint16_t random_nodes_path_onion(const Onion_Client *onion_c, Node_format
110 nodes[i] = onion_c->path_nodes[rand() % num_nodes]; 140 nodes[i] = onion_c->path_nodes[rand() % num_nodes];
111 } 141 }
112 } else { 142 } else {
113 if (num_nodes == 0) 143 if (num_nodes >= 2) {
114 return 0; 144 nodes[0].ip_port.ip.family = TCP_FAMILY;
115 145
116 nodes[0].ip_port.ip.family = TCP_FAMILY; 146 for (i = 1; i < max_num; ++i) {
147 nodes[i] = onion_c->path_nodes[rand() % num_nodes];
148 }
149 } else {
150 unsigned int num_nodes_bs = (onion_c->path_nodes_index_bs < MAX_PATH_NODES) ? onion_c->path_nodes_index_bs :
151 MAX_PATH_NODES;
117 152
118 for (i = 1; i < max_num; ++i) { 153 if (num_nodes_bs == 0)
119 nodes[i] = onion_c->path_nodes[rand() % num_nodes]; 154 return 0;
155
156 nodes[0].ip_port.ip.family = TCP_FAMILY;
157
158 for (i = 1; i < max_num; ++i) {
159 nodes[i] = onion_c->path_nodes_bs[rand() % num_nodes_bs];
160 }
120 } 161 }
121 } 162 }
122 163
@@ -216,6 +257,18 @@ static uint32_t set_path_timeouts(Onion_Client *onion_c, uint32_t num, uint32_t
216 if (onion_paths->paths[path_num % NUMBER_ONION_PATHS].path_num == path_num) { 257 if (onion_paths->paths[path_num % NUMBER_ONION_PATHS].path_num == path_num) {
217 onion_paths->last_path_success[path_num % NUMBER_ONION_PATHS] = unix_time(); 258 onion_paths->last_path_success[path_num % NUMBER_ONION_PATHS] = unix_time();
218 onion_paths->last_path_used_times[path_num % NUMBER_ONION_PATHS] = 0; 259 onion_paths->last_path_used_times[path_num % NUMBER_ONION_PATHS] = 0;
260
261 unsigned int path_len = 3;
262 Node_format nodes[path_len];
263
264 if (onion_path_to_nodes(nodes, path_len, &onion_paths->paths[path_num % NUMBER_ONION_PATHS]) == 0) {
265 unsigned int i;
266
267 for (i = 0; i < path_len; ++i) {
268 onion_add_path_node(onion_c, nodes[i].ip_port, nodes[i].client_id);
269 }
270 }
271
219 return path_num % NUMBER_ONION_PATHS; 272 return path_num % NUMBER_ONION_PATHS;
220 } 273 }
221 274
@@ -1158,9 +1211,6 @@ static void do_friend(Onion_Client *onion_c, uint16_t friendnum)
1158 Onion_Node *list_nodes = onion_c->friends_list[friendnum].clients_list; 1211 Onion_Node *list_nodes = onion_c->friends_list[friendnum].clients_list;
1159 1212
1160 if (!onion_c->friends_list[friendnum].is_online) { 1213 if (!onion_c->friends_list[friendnum].is_online) {
1161
1162 ++onion_c->friends_list[friendnum].run_count;
1163
1164 for (i = 0; i < MAX_ONION_CLIENTS; ++i) { 1214 for (i = 0; i < MAX_ONION_CLIENTS; ++i) {
1165 if (is_timeout(list_nodes[i].timestamp, FRIEND_ONION_NODE_TIMEOUT)) 1215 if (is_timeout(list_nodes[i].timestamp, FRIEND_ONION_NODE_TIMEOUT))
1166 continue; 1216 continue;
@@ -1196,7 +1246,11 @@ static void do_friend(Onion_Client *onion_c, uint16_t friendnum)
1196 client_send_announce_request(onion_c, friendnum + 1, onion_c->path_nodes[num].ip_port, 1246 client_send_announce_request(onion_c, friendnum + 1, onion_c->path_nodes[num].ip_port,
1197 onion_c->path_nodes[num].client_id, 0, ~0); 1247 onion_c->path_nodes[num].client_id, 0, ~0);
1198 } 1248 }
1249
1250 ++onion_c->friends_list[friendnum].run_count;
1199 } 1251 }
1252 } else {
1253 ++onion_c->friends_list[friendnum].run_count;
1200 } 1254 }
1201 1255
1202 /* send packets to friend telling them our fake DHT id. */ 1256 /* send packets to friend telling them our fake DHT id. */
@@ -1254,13 +1308,23 @@ static void do_announce(Onion_Client *onion_c)
1254 } 1308 }
1255 1309
1256 if (count != MAX_ONION_CLIENTS) { 1310 if (count != MAX_ONION_CLIENTS) {
1257 if (count < (uint32_t)rand() % MAX_ONION_CLIENTS) { 1311 unsigned int num_nodes;
1312 Node_format *path_nodes;
1258 1313
1259 unsigned int num_nodes = (onion_c->path_nodes_index < MAX_PATH_NODES) ? onion_c->path_nodes_index : MAX_PATH_NODES; 1314 if (rand() % 2 == 0 || onion_c->path_nodes_index == 0) {
1315 num_nodes = (onion_c->path_nodes_index_bs < MAX_PATH_NODES) ? onion_c->path_nodes_index_bs : MAX_PATH_NODES;
1316 path_nodes = onion_c->path_nodes_bs;
1317 } else {
1318 num_nodes = (onion_c->path_nodes_index < MAX_PATH_NODES) ? onion_c->path_nodes_index : MAX_PATH_NODES;
1319 path_nodes = onion_c->path_nodes;
1320 }
1260 1321
1322 if (count < (uint32_t)rand() % MAX_ONION_CLIENTS) {
1261 if (num_nodes != 0) { 1323 if (num_nodes != 0) {
1262 unsigned int num = rand() % num_nodes; 1324 for (i = 0; i < (MAX_ONION_CLIENTS / 2); ++i) {
1263 client_send_announce_request(onion_c, 0, onion_c->path_nodes[num].ip_port, onion_c->path_nodes[num].client_id, 0, ~0); 1325 unsigned int num = rand() % num_nodes;
1326 client_send_announce_request(onion_c, 0, path_nodes[num].ip_port, path_nodes[num].client_id, 0, ~0);
1327 }
1264 } 1328 }
1265 } 1329 }
1266 } 1330 }
@@ -1342,6 +1406,9 @@ int onion_isconnected(const Onion_Client *onion_c)
1342 if (is_timeout(onion_c->last_packet_recv, ONION_OFFLINE_TIMEOUT)) 1406 if (is_timeout(onion_c->last_packet_recv, ONION_OFFLINE_TIMEOUT))
1343 return 0; 1407 return 0;
1344 1408
1409 if (onion_c->path_nodes_index == 0)
1410 return 0;
1411
1345 for (i = 0; i < MAX_ONION_CLIENTS; ++i) { 1412 for (i = 0; i < MAX_ONION_CLIENTS; ++i) {
1346 if (!is_timeout(onion_c->clients_announce_list[i].timestamp, ONION_NODE_TIMEOUT)) { 1413 if (!is_timeout(onion_c->clients_announce_list[i].timestamp, ONION_NODE_TIMEOUT)) {
1347 ++num; 1414 ++num;
diff --git a/toxcore/onion_client.h b/toxcore/onion_client.h
index a99383c5..7119a8bf 100644
--- a/toxcore/onion_client.h
+++ b/toxcore/onion_client.h
@@ -143,6 +143,9 @@ typedef struct {
143 Node_format path_nodes[MAX_PATH_NODES]; 143 Node_format path_nodes[MAX_PATH_NODES];
144 uint16_t path_nodes_index; 144 uint16_t path_nodes_index;
145 145
146 Node_format path_nodes_bs[MAX_PATH_NODES];
147 uint16_t path_nodes_index_bs;
148
146 Ping_Array announce_ping_array; 149 Ping_Array announce_ping_array;
147 uint8_t last_pinged_index; 150 uint8_t last_pinged_index;
148 struct { 151 struct {
@@ -154,12 +157,12 @@ typedef struct {
154} Onion_Client; 157} Onion_Client;
155 158
156 159
157/* Add a node to the path_nodes array. 160/* Add a node to the path_nodes bootstrap array.
158 * 161 *
159 * return -1 on failure 162 * return -1 on failure
160 * return 0 on success 163 * return 0 on success
161 */ 164 */
162int onion_add_path_node(Onion_Client *onion_c, IP_Port ip_port, const uint8_t *client_id); 165int onion_add_bs_path_node(Onion_Client *onion_c, IP_Port ip_port, const uint8_t *client_id);
163 166
164/* Put up to max_num nodes in nodes. 167/* Put up to max_num nodes in nodes.
165 * 168 *
diff --git a/toxcore/tox.c b/toxcore/tox.c
index 66587011..3f458ac2 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -969,7 +969,7 @@ int tox_add_tcp_relay(Tox *tox, const char *address, uint16_t port, const uint8_
969 969
970 ip_port.port = htons(port); 970 ip_port.port = htons(port);
971 add_tcp_relay(m->net_crypto, ip_port, public_key); 971 add_tcp_relay(m->net_crypto, ip_port, public_key);
972 onion_add_path_node(m->onion_c, ip_port, public_key); //TODO: move this 972 onion_add_bs_path_node(m->onion_c, ip_port, public_key); //TODO: move this
973 return 1; 973 return 1;
974} 974}
975 975