summaryrefslogtreecommitdiff
path: root/toxcore/onion_client.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/onion_client.c')
-rw-r--r--toxcore/onion_client.c113
1 files changed, 72 insertions, 41 deletions
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c
index 3643cb2f..a9fc1643 100644
--- a/toxcore/onion_client.c
+++ b/toxcore/onion_client.c
@@ -1351,6 +1351,64 @@ static void do_announce(Onion_Client *onion_c)
1351 } 1351 }
1352} 1352}
1353 1353
1354/* return 0 if we are not connected to the network.
1355 * return 1 if we are.
1356 */
1357static int onion_isconnected(const Onion_Client *onion_c)
1358{
1359 unsigned int i, num = 0, announced = 0;
1360
1361 if (is_timeout(onion_c->last_packet_recv, ONION_OFFLINE_TIMEOUT))
1362 return 0;
1363
1364 if (onion_c->path_nodes_index == 0)
1365 return 0;
1366
1367 for (i = 0; i < MAX_ONION_CLIENTS; ++i) {
1368 if (!is_timeout(onion_c->clients_announce_list[i].timestamp, ONION_NODE_TIMEOUT)) {
1369 ++num;
1370
1371 if (onion_c->clients_announce_list[i].is_stored) {
1372 ++announced;
1373 }
1374 }
1375 }
1376
1377 unsigned int pnodes = onion_c->path_nodes_index;
1378
1379 if (pnodes > MAX_ONION_CLIENTS) {
1380 pnodes = MAX_ONION_CLIENTS;
1381 }
1382
1383 /* Consider ourselves online if we are announced to half or more nodes
1384 we are connected to */
1385 if (num && announced) {
1386 if ((num / 2) <= announced && (pnodes / 2) <= num)
1387 return 1;
1388 }
1389
1390 return 0;
1391}
1392
1393#define ONION_CONNECTION_SECONDS 2
1394
1395/* return 0 if we are not connected to the network.
1396 * return 1 if we are connected with TCP only.
1397 * return 2 if we are also connected with UDP.
1398 */
1399unsigned int onion_connection_status(const Onion_Client *onion_c)
1400{
1401 if (onion_c->onion_connected >= ONION_CONNECTION_SECONDS) {
1402 if (onion_c->UDP_connected) {
1403 return 2;
1404 } else {
1405 return 1;
1406 }
1407 }
1408
1409 return 0;
1410}
1411
1354void do_onion_client(Onion_Client *onion_c) 1412void do_onion_client(Onion_Client *onion_c)
1355{ 1413{
1356 unsigned int i; 1414 unsigned int i;
@@ -1363,11 +1421,23 @@ void do_onion_client(Onion_Client *onion_c)
1363 do_announce(onion_c); 1421 do_announce(onion_c);
1364 1422
1365 if (onion_isconnected(onion_c)) { 1423 if (onion_isconnected(onion_c)) {
1366 for (i = 0; i < onion_c->num_friends; ++i) { 1424 if (onion_c->onion_connected < ONION_CONNECTION_SECONDS * 2) {
1367 do_friend(onion_c, i); 1425 ++onion_c->onion_connected;
1368 } 1426 }
1427
1428 onion_c->UDP_connected = DHT_non_lan_connected(onion_c->dht);
1369 } else { 1429 } else {
1370 populate_path_nodes_tcp(onion_c); 1430 populate_path_nodes_tcp(onion_c);
1431
1432 if (onion_c->onion_connected != 0) {
1433 --onion_c->onion_connected;
1434 }
1435 }
1436
1437 if (onion_connection_status(onion_c)) {
1438 for (i = 0; i < onion_c->num_friends; ++i) {
1439 do_friend(onion_c, i);
1440 }
1371 } 1441 }
1372 1442
1373 onion_c->last_run = unix_time(); 1443 onion_c->last_run = unix_time();
@@ -1418,42 +1488,3 @@ void kill_onion_client(Onion_Client *onion_c)
1418 free(onion_c); 1488 free(onion_c);
1419} 1489}
1420 1490
1421
1422/* return 0 if we are not connected to the network.
1423 * return 1 if we are.
1424 */
1425int onion_isconnected(const Onion_Client *onion_c)
1426{
1427 unsigned int i, num = 0, announced = 0;
1428
1429 if (is_timeout(onion_c->last_packet_recv, ONION_OFFLINE_TIMEOUT))
1430 return 0;
1431
1432 if (onion_c->path_nodes_index == 0)
1433 return 0;
1434
1435 for (i = 0; i < MAX_ONION_CLIENTS; ++i) {
1436 if (!is_timeout(onion_c->clients_announce_list[i].timestamp, ONION_NODE_TIMEOUT)) {
1437 ++num;
1438
1439 if (onion_c->clients_announce_list[i].is_stored) {
1440 ++announced;
1441 }
1442 }
1443 }
1444
1445 unsigned int pnodes = onion_c->path_nodes_index;
1446
1447 if (pnodes > MAX_ONION_CLIENTS) {
1448 pnodes = MAX_ONION_CLIENTS;
1449 }
1450
1451 /* Consider ourselves online if we are announced to half or more nodes
1452 we are connected to */
1453 if (num && announced) {
1454 if ((num / 2) <= announced && (pnodes / 2) <= num)
1455 return 1;
1456 }
1457
1458 return 0;
1459}