From 99ae23813b47cc5198a210c4ea8da54d8a817829 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Sat, 18 Jan 2014 18:35:28 -0500 Subject: [PATCH] Some optimizations and fixes. --- auto_tests/tox_test.c | 12 ++++---- toxcore/DHT.c | 5 ++-- toxcore/Messenger.c | 2 ++ toxcore/onion_client.c | 67 +++++++++++++++++++++++++++--------------- toxcore/onion_client.h | 14 ++++++++- 5 files changed, 68 insertions(+), 32 deletions(-) diff --git a/auto_tests/tox_test.c b/auto_tests/tox_test.c index 310419d6..eb8ccf87 100644 --- a/auto_tests/tox_test.c +++ b/auto_tests/tox_test.c @@ -59,7 +59,7 @@ START_TEST(test_few_clients) tox_callback_friend_request(tox2, accept_friend_request, tox2); uint8_t address[TOX_FRIEND_ADDRESS_SIZE]; tox_get_address(tox2, address); - int test = tox_add_friend(tox3, address, "Gentoo", 7); + int test = tox_add_friend(tox3, address, (uint8_t *)"Gentoo", 7); ck_assert_msg(test == 0, "Failed to add friend error code: %i", test); uint8_t off = 1; @@ -84,7 +84,7 @@ START_TEST(test_few_clients) printf("tox clients connected\n"); uint32_t to_compare = 974536; tox_callback_friend_message(tox3, print_message, &to_compare); - tox_send_message(tox2, 0, "Install Gentoo", sizeof("Install Gentoo")); + tox_send_message(tox2, 0, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo")); while (1) { messages_received = 0; @@ -101,7 +101,7 @@ START_TEST(test_few_clients) printf("tox clients messaging succeeded\n"); tox_callback_name_change(tox3, print_nickchange, &to_compare); - tox_set_name(tox2, "Gentoo", sizeof("Gentoo")); + tox_set_name(tox2, (uint8_t *)"Gentoo", sizeof("Gentoo")); while (1) { name_changes = 0; @@ -122,8 +122,8 @@ START_TEST(test_few_clients) } END_TEST -#define NUM_TOXES 66 -#define NUM_FRIENDS 20 +#define NUM_TOXES 33 +#define NUM_FRIENDS 10 START_TEST(test_many_clients) { @@ -149,7 +149,7 @@ loop_top: pairs[i].tox1 = rand() % NUM_TOXES; pairs[i].tox2 = (pairs[i].tox1 + rand() % (NUM_TOXES - 1) + 1) % NUM_TOXES; tox_get_address(toxes[pairs[i].tox1], address); - int test = tox_add_friend(toxes[pairs[i].tox2], address, "Gentoo", 7); + int test = tox_add_friend(toxes[pairs[i].tox2], address, (uint8_t *)"Gentoo", 7); if (test == TOX_FAERR_ALREADYSENT) { goto loop_top; diff --git a/toxcore/DHT.c b/toxcore/DHT.c index c8702c45..f93a6d7a 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -308,7 +308,8 @@ static void get_close_nodes_inner(DHT *dht, uint8_t *client_id, Node_format *nod if (LAN_ip(ipptp->ip_port.ip) == 0 && !is_LAN) continue; - if (LAN_ip(ipptp->ip_port.ip) != 0 && want_good && hardening_correct(&ipptp->hardening) != HARDENING_ALL_OK && !id_equal(client_id, client->client_id)) + if (LAN_ip(ipptp->ip_port.ip) != 0 && want_good && hardening_correct(&ipptp->hardening) != HARDENING_ALL_OK + && !id_equal(client_id, client->client_id)) continue; if (num_nodes < MAX_SENT_NODES) { @@ -1838,7 +1839,7 @@ static void do_NAT(DHT *dht) /*----------------------------------------------------------------------------------*/ /*-----------------------END OF NAT PUNCHING FUNCTIONS------------------------------*/ -#define HARDREQ_DATA_SIZE 768 /* Attempt to prevent amplification/other attacks*/ +#define HARDREQ_DATA_SIZE 384 /* Attempt to prevent amplification/other attacks*/ #define CHECK_TYPE_ROUTE_REQ 0 #define CHECK_TYPE_ROUTE_RES 1 diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 3bc9f211..2f270fb6 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -683,6 +683,8 @@ static void check_friend_connectionstatus(Messenger *m, int friendnumber, uint8_ const uint8_t was_online = m->friendlist[friendnumber].status == FRIEND_ONLINE; const uint8_t is_online = status == FRIEND_ONLINE; + onion_set_friend_online(m->onion_c, m->friendlist[friendnumber].onion_friendnum, is_online); + if (is_online != was_online) { if (was_online) break_files(m, friendnumber); diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 1efe519a..932ffad7 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -582,6 +582,24 @@ int onion_getfriendip(Onion_Client *onion_c, int friend_num, IP_Port *ip_port) return DHT_getfriendip(onion_c->dht, onion_c->friends_list[friend_num].fake_client_id, ip_port); } +/* Set if friend is online or not. + * NOTE: This function is there and should be used so that we don't send useless packets to the friend if he is online. + * + * is_online 1 means friend is online. + * is_online 0 means friend is offline + * + * return -1 on failure. + * return 0 on success. + */ +int onion_set_friend_online(Onion_Client *onion_c, int friend_num, uint8_t is_online) +{ + if ((uint32_t)friend_num >= onion_c->num_friends) + return -1; + + onion_c->friends_list[friend_num].is_online = is_online; + return 0; +} + /* Takes 3 random nodes that we know and puts them in nodes * * nodes must be longer than 3. @@ -598,7 +616,7 @@ int random_path(Onion_Client *onion_c, Node_format *nodes) return 0; } -#define ANNOUNCE_FRIEND 30 +#define ANNOUNCE_FRIEND 120 static void do_friend(Onion_Client *onion_c, uint16_t friendnum) { @@ -611,32 +629,35 @@ static void do_friend(Onion_Client *onion_c, uint16_t friendnum) uint32_t i, count = 0; Onion_Node *list_nodes = onion_c->friends_list[friendnum].clients_list; - for (i = 0; i < MAX_ONION_CLIENTS; ++i) { - if (is_timeout(list_nodes[i].timestamp, ONION_NODE_TIMEOUT)) - continue; + if (!onion_c->friends_list[friendnum].is_online) { + for (i = 0; i < MAX_ONION_CLIENTS; ++i) { + if (is_timeout(list_nodes[i].timestamp, ONION_NODE_TIMEOUT)) + continue; - ++count; + ++count; - if (is_timeout(list_nodes[i].last_pinged, ANNOUNCE_FRIEND)) { - if (client_send_announce_request(onion_c, friendnum + 1, list_nodes[i].ip_port, list_nodes[i].client_id, 0) == 0) { - list_nodes[i].last_pinged = unix_time(); + if (is_timeout(list_nodes[i].last_pinged, ANNOUNCE_FRIEND)) { + if (client_send_announce_request(onion_c, friendnum + 1, list_nodes[i].ip_port, list_nodes[i].client_id, 0) == 0) { + list_nodes[i].last_pinged = unix_time(); + } } } + + if (count < MAX_ONION_CLIENTS / 2) { + Node_format nodes_list[MAX_SENT_NODES]; + uint32_t num_nodes = get_close_nodes(onion_c->dht, onion_c->friends_list[friendnum].real_client_id, nodes_list, + rand() % 2 ? AF_INET : AF_INET6, 1, 0); + + for (i = 0; i < num_nodes; ++i) + client_send_announce_request(onion_c, friendnum + 1, nodes_list[i].ip_port, nodes_list[i].client_id, 0); + } + + + /* send packets to friend telling them our fake DHT id. */ + if (is_timeout(onion_c->friends_list[friendnum].last_fakeid_sent, ONION_FAKEID_INTERVAL)) + if (send_fakeid_announce(onion_c, friendnum) > 1) + onion_c->friends_list[friendnum].last_fakeid_sent = unix_time(); } - - if (count < MAX_ONION_CLIENTS / 2) { - Node_format nodes_list[MAX_SENT_NODES]; - uint32_t num_nodes = get_close_nodes(onion_c->dht, onion_c->friends_list[friendnum].real_client_id, nodes_list, - rand() % 2 ? AF_INET : AF_INET6, 1, 0); - - for (i = 0; i < num_nodes; ++i) - client_send_announce_request(onion_c, friendnum + 1, nodes_list[i].ip_port, nodes_list[i].client_id, 0); - } - - /* send packets to friend telling them our fake DHT id. */ - if (is_timeout(onion_c->friends_list[friendnum].last_fakeid_sent, ONION_FAKEID_INTERVAL)) - if (send_fakeid_announce(onion_c, friendnum) > 1) - onion_c->friends_list[friendnum].last_fakeid_sent = unix_time(); } /* Function to call when onion data packet with contents beginning with byte is received. */ void oniondata_registerhandler(Onion_Client *onion_c, uint8_t byte, oniondata_handler_callback cb, void *object) @@ -646,7 +667,7 @@ void oniondata_registerhandler(Onion_Client *onion_c, uint8_t byte, oniondata_ha } #define ANNOUNCE_INTERVAL_NOT_ANNOUNCED 10 -#define ANNOUNCE_INTERVAL_ANNOUNCED 60 +#define ANNOUNCE_INTERVAL_ANNOUNCED 120 static void do_announce(Onion_Client *onion_c) { diff --git a/toxcore/onion_client.h b/toxcore/onion_client.h index 417e7f80..78587846 100644 --- a/toxcore/onion_client.h +++ b/toxcore/onion_client.h @@ -27,7 +27,7 @@ #include "onion_announce.h" #define MAX_ONION_CLIENTS 8 -#define ONION_NODE_TIMEOUT 200 +#define ONION_NODE_TIMEOUT 240 /* The interval in seconds at which to tell our friends where we are */ #define ONION_FAKEID_INTERVAL 60 @@ -43,6 +43,7 @@ typedef struct { typedef struct { uint8_t status; /* 0 if friend is not valid, 1 if friend is valid.*/ + uint8_t is_online; /* Set by the onion_set_friend_status function. */ uint8_t fake_client_id[crypto_box_PUBLICKEYBYTES]; uint8_t real_client_id[crypto_box_PUBLICKEYBYTES]; @@ -94,6 +95,17 @@ int onion_addfriend(Onion_Client *onion_c, uint8_t *client_id); */ int onion_delfriend(Onion_Client *onion_c, int friend_num); +/* Set if friend is online or not. + * NOTE: This function is there and should be used so that we don't send useless packets to the friend if he is online. + * + * is_online 1 means friend is online. + * is_online 0 means friend is offline + * + * return -1 on failure. + * return 0 on success. + */ +int onion_set_friend_online(Onion_Client *onion_c, int friend_num, uint8_t is_online); + /* Get the ip of friend friendnum and put it in ip_port * * return -1, -- if client_id does NOT refer to a friend