From 987a8d9850f981aad11c3e1924fd05c82e0432db Mon Sep 17 00:00:00 2001 From: notsecure Date: Mon, 19 May 2014 17:39:50 -0400 Subject: [PATCH 1/3] tcp relay sharing (PACKET_ID_SHARE_RELAYS) --- toxcore/Messenger.c | 36 ++++++++++++++++++++++++++++++++++++ toxcore/Messenger.h | 6 ++++++ 2 files changed, 42 insertions(+) diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 96005237..a32911fa 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -714,6 +714,25 @@ static int send_ping(Messenger *m, int32_t friendnumber) return ret; } +static int send_relays(Messenger *m, int32_t friendnumber) +{ + Node_format nodes[MAX_TCP_CONNECTIONS]; + uint8_t data[1024]; + int n, length; + + n = copy_connected_tcp_relays(m->net_crypto, nodes, MAX_TCP_CONNECTIONS); + length = pack_nodes(data, sizeof(data), nodes, n); + + int ret = write_cryptpacket_id(m, friendnumber, PACKET_ID_SHARE_RELAYS, data, length); + + if (ret == 1) + m->friendlist[friendnumber].share_relays_lastsent = unix_time(); + + return ret; +} + + + static int set_friend_statusmessage(Messenger *m, int32_t friendnumber, uint8_t *status, uint16_t length) { if (friend_not_valid(m, friendnumber)) @@ -2160,6 +2179,19 @@ static int handle_packet(void *object, int i, uint8_t *temp, uint16_t len) (*m->msi_packet)(m, i, data, data_length, m->msi_packet_userdata); } + case PACKET_ID_SHARE_RELAYS: { + Node_format nodes[MAX_TCP_CONNECTIONS]; + int n; + + if ((n = unpack_nodes(nodes, MAX_TCP_CONNECTIONS, NULL, data, data_length, 1) == -1)) + break; + + int i; + for(i = 0; i < n; i++) { + add_tcp_relay(m->net_crypto, nodes[i].ip_port, nodes[i].client_id); + } + } + default: { break; } @@ -2274,6 +2306,10 @@ void do_friends(Messenger *m) m->friendlist[i].crypt_connection_id = -1; set_friend_status(m, i, FRIEND_CONFIRMED); } + + if (m->friendlist[i].share_relays_lastsent + FRIEND_SHARE_RELAYS_INTERVAL < temp_time) { + send_relays(m, i); + } } } } diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index b72a0831..c505f1f1 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -41,6 +41,7 @@ /* NOTE: Packet ids below 16 must never be used. */ #define PACKET_ID_ALIVE 16 +#define PACKET_ID_SHARE_RELAYS 17 #define PACKET_ID_NICKNAME 48 #define PACKET_ID_STATUSMESSAGE 49 #define PACKET_ID_USERSTATUS 50 @@ -88,9 +89,13 @@ enum { /* Interval between the sending of ping packets. */ #define FRIEND_PING_INTERVAL 5 +/* Interval between the sending of tcp relay information */ +#define FRIEND_SHARE_RELAYS_INTERVAL (5 * 60) + /* If no packets are received from friend in this time interval, kill the connection. */ #define FRIEND_CONNECTION_TIMEOUT (FRIEND_PING_INTERVAL * 2) + /* USERSTATUS - * Represents userstatuses someone can have. */ @@ -153,6 +158,7 @@ typedef struct { uint32_t friendrequest_nospam; // The nospam number used in the friend request. uint64_t ping_lastrecv; uint64_t ping_lastsent; + uint64_t share_relays_lastsent; struct File_Transfers file_sending[MAX_CONCURRENT_FILE_PIPES]; struct File_Transfers file_receiving[MAX_CONCURRENT_FILE_PIPES]; int invited_groups[MAX_INVITED_GROUPS]; From 1b0a810a65bbf1e750b92e41dd44b82fae48538b Mon Sep 17 00:00:00 2001 From: notsecure Date: Mon, 19 May 2014 19:03:52 -0400 Subject: [PATCH 2/3] max number of shared relays --- toxcore/Messenger.c | 11 ++++++----- toxcore/Messenger.h | 3 +++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index a32911fa..1d2c1310 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -716,11 +716,11 @@ static int send_ping(Messenger *m, int32_t friendnumber) static int send_relays(Messenger *m, int32_t friendnumber) { - Node_format nodes[MAX_TCP_CONNECTIONS]; + Node_format nodes[MAX_SHARED_RELAYS]; uint8_t data[1024]; int n, length; - n = copy_connected_tcp_relays(m->net_crypto, nodes, MAX_TCP_CONNECTIONS); + n = copy_connected_tcp_relays(m->net_crypto, nodes, MAX_SHARED_RELAYS); length = pack_nodes(data, sizeof(data), nodes, n); int ret = write_cryptpacket_id(m, friendnumber, PACKET_ID_SHARE_RELAYS, data, length); @@ -2180,14 +2180,15 @@ static int handle_packet(void *object, int i, uint8_t *temp, uint16_t len) } case PACKET_ID_SHARE_RELAYS: { - Node_format nodes[MAX_TCP_CONNECTIONS]; + Node_format nodes[MAX_SHARED_RELAYS]; int n; - if ((n = unpack_nodes(nodes, MAX_TCP_CONNECTIONS, NULL, data, data_length, 1) == -1)) + if ((n = unpack_nodes(nodes, MAX_SHARED_RELAYS, NULL, data, data_length, 1) == -1)) break; int i; - for(i = 0; i < n; i++) { + + for (i = 0; i < n; i++) { add_tcp_relay(m->net_crypto, nodes[i].ip_port, nodes[i].client_id); } } diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index c505f1f1..fb070607 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -60,6 +60,9 @@ /* Max number of groups we can invite someone at the same time to. */ #define MAX_INVITED_GROUPS 64 +/* Max number of tcp relays sent to friends */ +#define MAX_SHARED_RELAYS 16 + /* Status definitions. */ enum { NOFRIEND, From 403ab7c2abaa28ef38d19c9a4fe39dc9d3f4381f Mon Sep 17 00:00:00 2001 From: notsecure Date: Mon, 19 May 2014 19:28:20 -0400 Subject: [PATCH 3/3] misplaced bracket --- toxcore/Messenger.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 1d2c1310..1e6e6c06 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -2183,7 +2183,7 @@ static int handle_packet(void *object, int i, uint8_t *temp, uint16_t len) Node_format nodes[MAX_SHARED_RELAYS]; int n; - if ((n = unpack_nodes(nodes, MAX_SHARED_RELAYS, NULL, data, data_length, 1) == -1)) + if ((n = unpack_nodes(nodes, MAX_SHARED_RELAYS, NULL, data, data_length, 1)) == -1) break; int i;