From 92a708578f9aa4c2cff2d3a2de08e28ee61cda1b Mon Sep 17 00:00:00 2001 From: irungentoo Date: Thu, 16 Apr 2015 20:39:09 -0400 Subject: [PATCH] Properly share connected relays and connect to the friend correctly with them. --- toxcore/Messenger.c | 13 +++++++++++++ toxcore/Messenger.h | 2 +- toxcore/TCP_connection.c | 4 ++-- toxcore/TCP_connection.h | 6 +++--- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index bd44115d0..3233a4660 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -755,11 +755,23 @@ static int send_user_istyping(const Messenger *m, int32_t friendnumber, uint8_t 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); @@ -1914,6 +1926,7 @@ 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); diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 63fb28209..e17bbb42c 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -63,7 +63,7 @@ enum { #define PACKET_ID_LOSSY_GROUPCHAT 199 /* Max number of tcp relays sent to friends */ -#define MAX_SHARED_RELAYS 16 +#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 diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c index 49a213a7c..ab18daaca 100644 --- a/toxcore/TCP_connection.c +++ b/toxcore/TCP_connection.c @@ -901,10 +901,10 @@ int add_tcp_relay_connection(TCP_Connections *tcp_c, int connections_number, IP_ */ unsigned int tcp_copy_connected_relays(TCP_Connections *tcp_c, Node_format *tcp_relays, uint16_t max_num) { - unsigned int i, copied = 0; + unsigned int i, copied = 0, r = rand(); for (i = 0; (i < tcp_c->tcp_connections_length) && (copied < max_num); ++i) { - TCP_con *tcp_con = get_tcp_connection(tcp_c, i); + TCP_con *tcp_con = get_tcp_connection(tcp_c, (i + r) % tcp_c->tcp_connections_length); if (!tcp_con) { continue; diff --git a/toxcore/TCP_connection.h b/toxcore/TCP_connection.h index 98f52f8d4..07d7820ce 100644 --- a/toxcore/TCP_connection.h +++ b/toxcore/TCP_connection.h @@ -34,14 +34,14 @@ #define TCP_CONNECTIONS_STATUS_REGISTERED 1 #define TCP_CONNECTIONS_STATUS_ONLINE 2 -#define MAX_FRIEND_TCP_CONNECTIONS 4 +#define MAX_FRIEND_TCP_CONNECTIONS 6 /* Time until connection to friend gets killed (if it doesn't get locked withing that time) */ #define TCP_CONNECTION_ANNOUNCE_TIMEOUT (TCP_CONNECTION_TIMEOUT) /* The amount of recommended connections for each friend - NOTE: Must be equal or smaller than MAX_FRIEND_TCP_CONNECTIONS */ -#define RECOMMENDED_FRIEND_TCP_CONNECTIONS 3 + NOTE: Must be at most (MAX_FRIEND_TCP_CONNECTIONS / 2) */ +#define RECOMMENDED_FRIEND_TCP_CONNECTIONS (MAX_FRIEND_TCP_CONNECTIONS / 2) typedef struct { uint8_t status;