summaryrefslogtreecommitdiff
path: root/toxcore/friend_connection.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2015-04-21 20:12:24 -0400
committerirungentoo <irungentoo@gmail.com>2015-04-21 20:12:24 -0400
commit3bd4f5902cb4dd4301ba9a8433f073fe31d6d2b5 (patch)
tree8e570409b0a0660ee6c8cb6d87ef2e44636e81d6 /toxcore/friend_connection.c
parente1a98987ffeea04dd075be760af7621824fdc30a (diff)
Move the send tcp relay packet from Messenger to friend connection.
Diffstat (limited to 'toxcore/friend_connection.c')
-rw-r--r--toxcore/friend_connection.c64
1 files changed, 59 insertions, 5 deletions
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_
198 } 198 }
199} 199}
200 200
201static unsigned int send_relays(Friend_Connections *fr_c, int friendcon_id)
202{
203 Friend_Conn *friend_con = get_conn(fr_c, friendcon_id);
204
205 if (!friend_con)
206 return 0;
207
208 Node_format nodes[MAX_SHARED_RELAYS];
209 uint8_t data[1024];
210 int n, length;
211
212 n = copy_connected_tcp_relays(fr_c->net_crypto, nodes, MAX_SHARED_RELAYS);
213
214 unsigned int i;
215
216 for (i = 0; i < n; ++i) {
217 /* Associated the relays being sent with this connection.
218 On receiving the peer will do the same which will establish the connection. */
219 friend_add_tcp_relay(fr_c, friendcon_id, nodes[i].ip_port, nodes[i].public_key);
220 }
221
222 length = pack_nodes(data + 1, sizeof(data) - 1, nodes, n);
223
224 if (length <= 0)
225 return 0;
226
227 data[0] = PACKET_ID_SHARE_RELAYS;
228 ++length;
229
230 if (write_cryptpacket(fr_c->net_crypto, friend_con->crypt_connection_id, data, length, 0) != -1) {
231 friend_con->share_relays_lastsent = unix_time();
232 return 1;
233 }
234
235 return 0;
236}
237
201/* callback for recv TCP relay nodes. */ 238/* callback for recv TCP relay nodes. */
202static int tcp_relay_node_callback(void *object, uint32_t number, IP_Port ip_port, const uint8_t *public_key) 239static int tcp_relay_node_callback(void *object, uint32_t number, IP_Port ip_port, const uint8_t *public_key)
203{ 240{
@@ -293,6 +330,7 @@ static int handle_status(void *object, int number, uint8_t status)
293 call_cb = 1; 330 call_cb = 1;
294 friend_con->status = FRIENDCONN_STATUS_CONNECTED; 331 friend_con->status = FRIENDCONN_STATUS_CONNECTED;
295 friend_con->ping_lastrecv = unix_time(); 332 friend_con->ping_lastrecv = unix_time();
333 friend_con->share_relays_lastsent = 0;
296 onion_set_friend_online(fr_c->onion_c, friend_con->onion_friendnum, status); 334 onion_set_friend_online(fr_c->onion_c, friend_con->onion_friendnum, status);
297 } else { /* Went offline. */ 335 } else { /* Went offline. */
298 if (friend_con->status != FRIENDCONN_STATUS_CONNECTING) { 336 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
326 Friend_Connections *fr_c = object; 364 Friend_Connections *fr_c = object;
327 Friend_Conn *friend_con = get_conn(fr_c, number); 365 Friend_Conn *friend_con = get_conn(fr_c, number);
328 366
367 if (!friend_con)
368 return -1;
369
329 if (data[0] == PACKET_ID_FRIEND_REQUESTS) { 370 if (data[0] == PACKET_ID_FRIEND_REQUESTS) {
330 if (fr_c->fr_request_callback) 371 if (fr_c->fr_request_callback)
331 fr_c->fr_request_callback(fr_c->fr_request_object, friend_con->real_public_key, data, length); 372 fr_c->fr_request_callback(fr_c->fr_request_object, friend_con->real_public_key, data, length);
332 373
333 return 0; 374 return 0;
334 } 375 } else if (data[0] == PACKET_ID_ALIVE) {
376 friend_con->ping_lastrecv = unix_time();
377 return 0;
378 } else if (data[0] == PACKET_ID_SHARE_RELAYS) {
379 Node_format nodes[MAX_SHARED_RELAYS];
380 int n;
335 381
336 if (!friend_con) 382 if ((n = unpack_nodes(nodes, MAX_SHARED_RELAYS, NULL, data + 1, length - 1, 1)) == -1)
337 return -1; 383 return -1;
384
385 int j;
386
387 for (j = 0; j < n; j++) {
388 friend_add_tcp_relay(fr_c, number, nodes[j].ip_port, nodes[j].public_key);
389 }
338 390
339 if (data[0] == PACKET_ID_ALIVE) {
340 friend_con->ping_lastrecv = unix_time();
341 return 0; 391 return 0;
342 } 392 }
343 393
@@ -745,6 +795,10 @@ void do_friend_connections(Friend_Connections *fr_c)
745 send_ping(fr_c, i); 795 send_ping(fr_c, i);
746 } 796 }
747 797
798 if (friend_con->share_relays_lastsent + SHARE_RELAYS_INTERVAL < temp_time) {
799 send_relays(fr_c, i);
800 }
801
748 if (friend_con->ping_lastrecv + FRIEND_CONNECTION_TIMEOUT < temp_time) { 802 if (friend_con->ping_lastrecv + FRIEND_CONNECTION_TIMEOUT < temp_time) {
749 /* If we stopped receiving ping packets, kill it. */ 803 /* If we stopped receiving ping packets, kill it. */
750 crypto_kill(fr_c->net_crypto, friend_con->crypt_connection_id); 804 crypto_kill(fr_c->net_crypto, friend_con->crypt_connection_id);