From 565d73713aa75f0d5437f0152ee7cb22f3113c59 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Thu, 16 Apr 2015 14:17:57 -0400 Subject: Store TCP relays tied to friend and reconnect to some when reconnecting. --- toxcore/friend_connection.h | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'toxcore/friend_connection.h') diff --git a/toxcore/friend_connection.h b/toxcore/friend_connection.h index b814eb0c..641d2872 100644 --- a/toxcore/friend_connection.h +++ b/toxcore/friend_connection.h @@ -47,6 +47,7 @@ /* Time before friend is removed from the DHT after last hearing about him. */ #define FRIEND_DHT_TIMEOUT BAD_NODE_TIMEOUT +#define FRIEND_MAX_STORED_TCP_RELAYS (MAX_FRIEND_TCP_CONNECTIONS * 4) enum { FRIENDCONN_STATUS_NONE, @@ -61,7 +62,7 @@ typedef struct { uint8_t dht_temp_pk[crypto_box_PUBLICKEYBYTES]; uint16_t dht_lock; IP_Port dht_ip_port; - uint64_t dht_ping_lastrecv, dht_ip_port_lastrecv; + uint64_t dht_pk_lastrecv, dht_ip_port_lastrecv; int onion_friendnum; int crypt_connection_id; @@ -83,6 +84,9 @@ typedef struct { } callbacks[MAX_FRIEND_CONNECTION_CALLBACKS]; uint16_t lock_count; + + Node_format tcp_relays[FRIEND_MAX_STORED_TCP_RELAYS]; + uint16_t tcp_relay_counter; } Friend_Conn; @@ -127,6 +131,13 @@ int get_friendcon_public_keys(uint8_t *real_pk, uint8_t *dht_temp_pk, Friend_Con */ void set_dht_temp_pk(Friend_Connections *fr_c, int friendcon_id, const uint8_t *dht_temp_pk); +/* Add a TCP relay associated to the friend. + * + * return -1 on failure. + * return 0 on success. + */ +int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_port, const uint8_t *public_key); + /* Set the callbacks for the friend connection. * index is the index (0 to (MAX_FRIEND_CONNECTION_CALLBACKS - 1)) we want the callback to set in the array. * -- cgit v1.2.3 From 3bd4f5902cb4dd4301ba9a8433f073fe31d6d2b5 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Tue, 21 Apr 2015 20:12:24 -0400 Subject: Move the send tcp relay packet from Messenger to friend connection. --- toxcore/Messenger.c | 52 ------------------------------------ toxcore/Messenger.h | 10 +------ toxcore/friend_connection.c | 64 +++++++++++++++++++++++++++++++++++++++++---- toxcore/friend_connection.h | 9 +++++++ 4 files changed, 69 insertions(+), 66 deletions(-) (limited to 'toxcore/friend_connection.h') diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 1c9510ab..efb66961 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -749,37 +749,6 @@ static int send_user_istyping(const Messenger *m, int32_t friendnumber, uint8_t return write_cryptpacket_id(m, friendnumber, PACKET_ID_TYPING, &typing, sizeof(typing), 0); } -static int send_relays(const Messenger *m, int32_t friendnumber) -{ - if (friend_not_valid(m, friendnumber)) - return 0; - - Node_format nodes[MAX_SHARED_RELAYS]; - uint8_t data[1024]; - int n, length; - - n = copy_connected_tcp_relays(m->net_crypto, nodes, MAX_SHARED_RELAYS); - - unsigned int i; - - for (i = 0; i < n; ++i) { - /* Associated the relays being sent with this connection. - On receiving the peer will do the same which will establish the connection. */ - friend_add_tcp_relay(m->fr_c, m->friendlist[friendnumber].friendcon_id, nodes[i].ip_port, nodes[i].public_key); - } - - length = pack_nodes(data, sizeof(data), nodes, n); - - int ret = write_cryptpacket_id(m, friendnumber, PACKET_ID_SHARE_RELAYS, data, length, 0); - - if (ret == 1) - m->friendlist[friendnumber].share_relays_lastsent = unix_time(); - - return ret; -} - - - static int set_friend_statusmessage(const Messenger *m, int32_t friendnumber, const uint8_t *status, uint16_t length) { if (friend_not_valid(m, friendnumber)) @@ -1922,7 +1891,6 @@ static int handle_status(void *object, int i, uint8_t status) m->friendlist[i].userstatus_sent = 0; m->friendlist[i].statusmessage_sent = 0; m->friendlist[i].user_istyping_sent = 0; - m->friendlist[i].share_relays_lastsent = 0; } else { /* Went offline. */ if (m->friendlist[i].status == FRIEND_ONLINE) { set_friend_status(m, i, FRIEND_CONFIRMED); @@ -2187,22 +2155,6 @@ static int handle_packet(void *object, int i, uint8_t *temp, uint16_t len) break; } - case PACKET_ID_SHARE_RELAYS: { - Node_format nodes[MAX_SHARED_RELAYS]; - int n; - - if ((n = unpack_nodes(nodes, MAX_SHARED_RELAYS, NULL, data, data_length, 1)) == -1) - break; - - int j; - - for (j = 0; j < n; j++) { - friend_add_tcp_relay(m->fr_c, m->friendlist[i].friendcon_id, nodes[j].ip_port, nodes[j].public_key); - } - - break; - } - default: { handle_custom_lossless_packet(object, i, temp, len); break; @@ -2260,10 +2212,6 @@ void do_friends(Messenger *m) m->friendlist[i].user_istyping_sent = 1; } - if (m->friendlist[i].share_relays_lastsent + FRIEND_SHARE_RELAYS_INTERVAL < temp_time) { - send_relays(m, i); - } - check_friend_tcp_udp(m, i); do_receipts(m, i); do_reqchunk_filecb(m, i); diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index e17bbb42..c9573639 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -42,8 +42,7 @@ enum { MESSAGE_ACTION }; -/* NOTE: Packet ids below 20 must never be used. */ -#define PACKET_ID_SHARE_RELAYS 23 +/* NOTE: Packet ids below 24 must never be used. */ #define PACKET_ID_ONLINE 24 #define PACKET_ID_OFFLINE 25 #define PACKET_ID_NICKNAME 48 @@ -62,9 +61,6 @@ enum { #define PACKET_ID_MESSAGE_GROUPCHAT 99 #define PACKET_ID_LOSSY_GROUPCHAT 199 -/* Max number of tcp relays sent to friends */ -#define MAX_SHARED_RELAYS (RECOMMENDED_FRIEND_TCP_CONNECTIONS) - /* All packets starting with a byte in this range can be used for anything. */ #define PACKET_ID_LOSSLESS_RANGE_START 160 #define PACKET_ID_LOSSLESS_RANGE_SIZE 32 @@ -110,9 +106,6 @@ enum { /* Default start timeout in seconds between friend requests. */ #define FRIENDREQUEST_TIMEOUT 5; -/* Interval between the sending of tcp relay information */ -#define FRIEND_SHARE_RELAYS_INTERVAL (5 * 60) - enum { CONNECTION_NONE, CONNECTION_TCP, @@ -199,7 +192,6 @@ typedef struct { uint32_t message_id; // a semi-unique id used in read receipts. uint32_t friendrequest_nospam; // The nospam number used in the friend request. uint64_t last_seen_time; - uint64_t share_relays_lastsent; uint8_t last_connection_udp_tcp; struct File_Transfers file_sending[MAX_CONCURRENT_FILE_PIPES]; unsigned int num_sending_files; diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c index 0f171100..892048ad 100644 --- a/toxcore/friend_connection.c +++ b/toxcore/friend_connection.c @@ -198,6 +198,43 @@ static void connect_to_saved_tcp_relays(Friend_Connections *fr_c, int friendcon_ } } +static unsigned int send_relays(Friend_Connections *fr_c, int friendcon_id) +{ + Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); + + if (!friend_con) + return 0; + + Node_format nodes[MAX_SHARED_RELAYS]; + uint8_t data[1024]; + int n, length; + + n = copy_connected_tcp_relays(fr_c->net_crypto, nodes, MAX_SHARED_RELAYS); + + unsigned int i; + + for (i = 0; i < n; ++i) { + /* Associated the relays being sent with this connection. + On receiving the peer will do the same which will establish the connection. */ + friend_add_tcp_relay(fr_c, friendcon_id, nodes[i].ip_port, nodes[i].public_key); + } + + length = pack_nodes(data + 1, sizeof(data) - 1, nodes, n); + + if (length <= 0) + return 0; + + data[0] = PACKET_ID_SHARE_RELAYS; + ++length; + + if (write_cryptpacket(fr_c->net_crypto, friend_con->crypt_connection_id, data, length, 0) != -1) { + friend_con->share_relays_lastsent = unix_time(); + return 1; + } + + return 0; +} + /* callback for recv TCP relay nodes. */ static int tcp_relay_node_callback(void *object, uint32_t number, IP_Port ip_port, const uint8_t *public_key) { @@ -293,6 +330,7 @@ static int handle_status(void *object, int number, uint8_t status) call_cb = 1; friend_con->status = FRIENDCONN_STATUS_CONNECTED; friend_con->ping_lastrecv = unix_time(); + friend_con->share_relays_lastsent = 0; onion_set_friend_online(fr_c->onion_c, friend_con->onion_friendnum, status); } else { /* Went offline. */ if (friend_con->status != FRIENDCONN_STATUS_CONNECTING) { @@ -326,18 +364,30 @@ static int handle_packet(void *object, int number, uint8_t *data, uint16_t lengt Friend_Connections *fr_c = object; Friend_Conn *friend_con = get_conn(fr_c, number); + if (!friend_con) + return -1; + if (data[0] == PACKET_ID_FRIEND_REQUESTS) { if (fr_c->fr_request_callback) fr_c->fr_request_callback(fr_c->fr_request_object, friend_con->real_public_key, data, length); return 0; - } + } else if (data[0] == PACKET_ID_ALIVE) { + friend_con->ping_lastrecv = unix_time(); + return 0; + } else if (data[0] == PACKET_ID_SHARE_RELAYS) { + Node_format nodes[MAX_SHARED_RELAYS]; + int n; - if (!friend_con) - return -1; + if ((n = unpack_nodes(nodes, MAX_SHARED_RELAYS, NULL, data + 1, length - 1, 1)) == -1) + return -1; + + int j; + + for (j = 0; j < n; j++) { + friend_add_tcp_relay(fr_c, number, nodes[j].ip_port, nodes[j].public_key); + } - if (data[0] == PACKET_ID_ALIVE) { - friend_con->ping_lastrecv = unix_time(); return 0; } @@ -745,6 +795,10 @@ void do_friend_connections(Friend_Connections *fr_c) send_ping(fr_c, i); } + if (friend_con->share_relays_lastsent + SHARE_RELAYS_INTERVAL < temp_time) { + send_relays(fr_c, i); + } + if (friend_con->ping_lastrecv + FRIEND_CONNECTION_TIMEOUT < temp_time) { /* If we stopped receiving ping packets, kill it. */ crypto_kill(fr_c->net_crypto, friend_con->crypt_connection_id); diff --git a/toxcore/friend_connection.h b/toxcore/friend_connection.h index 641d2872..60b62646 100644 --- a/toxcore/friend_connection.h +++ b/toxcore/friend_connection.h @@ -36,6 +36,7 @@ #define GROUPCHAT_CALLBACK_INDEX 1 #define PACKET_ID_ALIVE 16 +#define PACKET_ID_SHARE_RELAYS 17 #define PACKET_ID_FRIEND_REQUESTS 18 /* Interval between the sending of ping packets. */ @@ -49,6 +50,13 @@ #define FRIEND_MAX_STORED_TCP_RELAYS (MAX_FRIEND_TCP_CONNECTIONS * 4) +/* Max number of tcp relays sent to friends */ +#define MAX_SHARED_RELAYS (RECOMMENDED_FRIEND_TCP_CONNECTIONS) + +/* Interval between the sending of tcp relay information */ +#define SHARE_RELAYS_INTERVAL (5 * 60) + + enum { FRIENDCONN_STATUS_NONE, FRIENDCONN_STATUS_CONNECTING, @@ -68,6 +76,7 @@ typedef struct { int crypt_connection_id; uint64_t ping_lastrecv, ping_lastsent; + uint64_t share_relays_lastsent; struct { int (*status_callback)(void *object, int id, uint8_t status); -- cgit v1.2.3 From 69e3e5f3a4510e4883edecc78b5556d38cb61318 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Mon, 27 Apr 2015 16:13:04 -0400 Subject: Move LAN discovery from Messenger to friend_connection. --- toxcore/LAN_discovery.c | 5 +++++ toxcore/LAN_discovery.h | 3 +++ toxcore/Messenger.c | 11 ----------- toxcore/Messenger.h | 2 -- toxcore/friend_connection.c | 13 +++++++++++++ toxcore/friend_connection.h | 2 ++ 6 files changed, 23 insertions(+), 13 deletions(-) (limited to 'toxcore/friend_connection.h') diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c index bc020d87..dbce762e 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c @@ -336,3 +336,8 @@ void LANdiscovery_init(DHT *dht) { networking_registerhandler(dht->net, NET_PACKET_LAN_DISCOVERY, &handle_LANdiscovery, dht); } + +void LANdiscovery_kill(DHT *dht) +{ + networking_registerhandler(dht->net, NET_PACKET_LAN_DISCOVERY, NULL, NULL); +} diff --git a/toxcore/LAN_discovery.h b/toxcore/LAN_discovery.h index 5dffc3ad..5243bd93 100644 --- a/toxcore/LAN_discovery.h +++ b/toxcore/LAN_discovery.h @@ -37,6 +37,9 @@ int send_LANdiscovery(uint16_t port, DHT *dht); /* Sets up packet handlers. */ void LANdiscovery_init(DHT *dht); +/* Clear packet handlers. */ +void LANdiscovery_kill(DHT *dht); + /* checks if a given IP isn't routable * * return 0 if ip is a LAN ip. diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index fd17ab98..a7e0a9fe 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -1765,15 +1765,6 @@ static int friend_already_added(const uint8_t *real_pk, void *data) return -1; } -/* Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds. */ -static void LANdiscovery(Messenger *m) -{ - if (m->last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) { - send_LANdiscovery(htons(TOX_PORT_DEFAULT), m->dht); - m->last_LANdiscovery = unix_time(); - } -} - /* Run this at startup. */ Messenger *new_messenger(Messenger_Options *options, unsigned int *error) { @@ -1842,7 +1833,6 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error) m->options = *options; friendreq_init(&(m->fr), m->fr_c); - LANdiscovery_init(m->dht); set_nospam(&(m->fr), random_int()); set_filter_function(&(m->fr), &friend_already_added, m); @@ -2308,7 +2298,6 @@ void do_messenger(Messenger *m) do_onion_client(m->onion_c); do_friend_connections(m->fr_c); do_friends(m); - LANdiscovery(m); connection_status_cb(m); #ifdef LOGGING diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 5221e639..6943475f 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -234,8 +234,6 @@ struct Messenger { uint32_t numonline_friends; - uint64_t last_LANdiscovery; - #define NUM_SAVED_TCP_RELAYS 8 uint8_t has_added_relays; // If the first connection has occurred in do_messenger Node_format loaded_relays[NUM_SAVED_TCP_RELAYS]; // Relays loaded from config diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c index 5182ce28..c13ca949 100644 --- a/toxcore/friend_connection.c +++ b/toxcore/friend_connection.c @@ -758,10 +758,20 @@ Friend_Connections *new_friend_connections(Onion_Client *onion_c) temp->onion_c = onion_c; new_connection_handler(temp->net_crypto, &handle_new_connections, temp); + LANdiscovery_init(temp->dht); return temp; } +/* Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds. */ +static void LANdiscovery(Friend_Connections *fr_c) +{ + if (fr_c->last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) { + send_LANdiscovery(htons(TOX_PORT_DEFAULT), fr_c->dht); + fr_c->last_LANdiscovery = unix_time(); + } +} + /* main friend_connections loop. */ void do_friend_connections(Friend_Connections *fr_c) { @@ -809,6 +819,8 @@ void do_friend_connections(Friend_Connections *fr_c) } } } + + LANdiscovery(fr_c); } /* Free everything related with friend_connections. */ @@ -823,5 +835,6 @@ void kill_friend_connections(Friend_Connections *fr_c) kill_friend_connection(fr_c, i); } + LANdiscovery_kill(fr_c->dht); free(fr_c); } diff --git a/toxcore/friend_connection.h b/toxcore/friend_connection.h index 60b62646..baca4b76 100644 --- a/toxcore/friend_connection.h +++ b/toxcore/friend_connection.h @@ -109,6 +109,8 @@ typedef struct { int (*fr_request_callback)(void *object, const uint8_t *source_pubkey, const uint8_t *data, uint16_t len); void *fr_request_object; + + uint64_t last_LANdiscovery; } Friend_Connections; /* return friendcon_id corresponding to the real public key on success. -- cgit v1.2.3