diff options
author | irungentoo <irungentoo@gmail.com> | 2015-04-21 20:12:24 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2015-04-21 20:12:24 -0400 |
commit | 3bd4f5902cb4dd4301ba9a8433f073fe31d6d2b5 (patch) | |
tree | 8e570409b0a0660ee6c8cb6d87ef2e44636e81d6 | |
parent | e1a98987ffeea04dd075be760af7621824fdc30a (diff) |
Move the send tcp relay packet from Messenger to friend connection.
-rw-r--r-- | toxcore/Messenger.c | 52 | ||||
-rw-r--r-- | toxcore/Messenger.h | 10 | ||||
-rw-r--r-- | toxcore/friend_connection.c | 64 | ||||
-rw-r--r-- | toxcore/friend_connection.h | 9 |
4 files changed, 69 insertions, 66 deletions
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 | |||
749 | return write_cryptpacket_id(m, friendnumber, PACKET_ID_TYPING, &typing, sizeof(typing), 0); | 749 | return write_cryptpacket_id(m, friendnumber, PACKET_ID_TYPING, &typing, sizeof(typing), 0); |
750 | } | 750 | } |
751 | 751 | ||
752 | static int send_relays(const Messenger *m, int32_t friendnumber) | ||
753 | { | ||
754 | if (friend_not_valid(m, friendnumber)) | ||
755 | return 0; | ||
756 | |||
757 | Node_format nodes[MAX_SHARED_RELAYS]; | ||
758 | uint8_t data[1024]; | ||
759 | int n, length; | ||
760 | |||
761 | n = copy_connected_tcp_relays(m->net_crypto, nodes, MAX_SHARED_RELAYS); | ||
762 | |||
763 | unsigned int i; | ||
764 | |||
765 | for (i = 0; i < n; ++i) { | ||
766 | /* Associated the relays being sent with this connection. | ||
767 | On receiving the peer will do the same which will establish the connection. */ | ||
768 | friend_add_tcp_relay(m->fr_c, m->friendlist[friendnumber].friendcon_id, nodes[i].ip_port, nodes[i].public_key); | ||
769 | } | ||
770 | |||
771 | length = pack_nodes(data, sizeof(data), nodes, n); | ||
772 | |||
773 | int ret = write_cryptpacket_id(m, friendnumber, PACKET_ID_SHARE_RELAYS, data, length, 0); | ||
774 | |||
775 | if (ret == 1) | ||
776 | m->friendlist[friendnumber].share_relays_lastsent = unix_time(); | ||
777 | |||
778 | return ret; | ||
779 | } | ||
780 | |||
781 | |||
782 | |||
783 | static int set_friend_statusmessage(const Messenger *m, int32_t friendnumber, const uint8_t *status, uint16_t length) | 752 | static int set_friend_statusmessage(const Messenger *m, int32_t friendnumber, const uint8_t *status, uint16_t length) |
784 | { | 753 | { |
785 | if (friend_not_valid(m, friendnumber)) | 754 | if (friend_not_valid(m, friendnumber)) |
@@ -1922,7 +1891,6 @@ static int handle_status(void *object, int i, uint8_t status) | |||
1922 | m->friendlist[i].userstatus_sent = 0; | 1891 | m->friendlist[i].userstatus_sent = 0; |
1923 | m->friendlist[i].statusmessage_sent = 0; | 1892 | m->friendlist[i].statusmessage_sent = 0; |
1924 | m->friendlist[i].user_istyping_sent = 0; | 1893 | m->friendlist[i].user_istyping_sent = 0; |
1925 | m->friendlist[i].share_relays_lastsent = 0; | ||
1926 | } else { /* Went offline. */ | 1894 | } else { /* Went offline. */ |
1927 | if (m->friendlist[i].status == FRIEND_ONLINE) { | 1895 | if (m->friendlist[i].status == FRIEND_ONLINE) { |
1928 | set_friend_status(m, i, FRIEND_CONFIRMED); | 1896 | 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) | |||
2187 | break; | 2155 | break; |
2188 | } | 2156 | } |
2189 | 2157 | ||
2190 | case PACKET_ID_SHARE_RELAYS: { | ||
2191 | Node_format nodes[MAX_SHARED_RELAYS]; | ||
2192 | int n; | ||
2193 | |||
2194 | if ((n = unpack_nodes(nodes, MAX_SHARED_RELAYS, NULL, data, data_length, 1)) == -1) | ||
2195 | break; | ||
2196 | |||
2197 | int j; | ||
2198 | |||
2199 | for (j = 0; j < n; j++) { | ||
2200 | friend_add_tcp_relay(m->fr_c, m->friendlist[i].friendcon_id, nodes[j].ip_port, nodes[j].public_key); | ||
2201 | } | ||
2202 | |||
2203 | break; | ||
2204 | } | ||
2205 | |||
2206 | default: { | 2158 | default: { |
2207 | handle_custom_lossless_packet(object, i, temp, len); | 2159 | handle_custom_lossless_packet(object, i, temp, len); |
2208 | break; | 2160 | break; |
@@ -2260,10 +2212,6 @@ void do_friends(Messenger *m) | |||
2260 | m->friendlist[i].user_istyping_sent = 1; | 2212 | m->friendlist[i].user_istyping_sent = 1; |
2261 | } | 2213 | } |
2262 | 2214 | ||
2263 | if (m->friendlist[i].share_relays_lastsent + FRIEND_SHARE_RELAYS_INTERVAL < temp_time) { | ||
2264 | send_relays(m, i); | ||
2265 | } | ||
2266 | |||
2267 | check_friend_tcp_udp(m, i); | 2215 | check_friend_tcp_udp(m, i); |
2268 | do_receipts(m, i); | 2216 | do_receipts(m, i); |
2269 | do_reqchunk_filecb(m, i); | 2217 | 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 { | |||
42 | MESSAGE_ACTION | 42 | MESSAGE_ACTION |
43 | }; | 43 | }; |
44 | 44 | ||
45 | /* NOTE: Packet ids below 20 must never be used. */ | 45 | /* NOTE: Packet ids below 24 must never be used. */ |
46 | #define PACKET_ID_SHARE_RELAYS 23 | ||
47 | #define PACKET_ID_ONLINE 24 | 46 | #define PACKET_ID_ONLINE 24 |
48 | #define PACKET_ID_OFFLINE 25 | 47 | #define PACKET_ID_OFFLINE 25 |
49 | #define PACKET_ID_NICKNAME 48 | 48 | #define PACKET_ID_NICKNAME 48 |
@@ -62,9 +61,6 @@ enum { | |||
62 | #define PACKET_ID_MESSAGE_GROUPCHAT 99 | 61 | #define PACKET_ID_MESSAGE_GROUPCHAT 99 |
63 | #define PACKET_ID_LOSSY_GROUPCHAT 199 | 62 | #define PACKET_ID_LOSSY_GROUPCHAT 199 |
64 | 63 | ||
65 | /* Max number of tcp relays sent to friends */ | ||
66 | #define MAX_SHARED_RELAYS (RECOMMENDED_FRIEND_TCP_CONNECTIONS) | ||
67 | |||
68 | /* All packets starting with a byte in this range can be used for anything. */ | 64 | /* All packets starting with a byte in this range can be used for anything. */ |
69 | #define PACKET_ID_LOSSLESS_RANGE_START 160 | 65 | #define PACKET_ID_LOSSLESS_RANGE_START 160 |
70 | #define PACKET_ID_LOSSLESS_RANGE_SIZE 32 | 66 | #define PACKET_ID_LOSSLESS_RANGE_SIZE 32 |
@@ -110,9 +106,6 @@ enum { | |||
110 | /* Default start timeout in seconds between friend requests. */ | 106 | /* Default start timeout in seconds between friend requests. */ |
111 | #define FRIENDREQUEST_TIMEOUT 5; | 107 | #define FRIENDREQUEST_TIMEOUT 5; |
112 | 108 | ||
113 | /* Interval between the sending of tcp relay information */ | ||
114 | #define FRIEND_SHARE_RELAYS_INTERVAL (5 * 60) | ||
115 | |||
116 | enum { | 109 | enum { |
117 | CONNECTION_NONE, | 110 | CONNECTION_NONE, |
118 | CONNECTION_TCP, | 111 | CONNECTION_TCP, |
@@ -199,7 +192,6 @@ typedef struct { | |||
199 | uint32_t message_id; // a semi-unique id used in read receipts. | 192 | uint32_t message_id; // a semi-unique id used in read receipts. |
200 | uint32_t friendrequest_nospam; // The nospam number used in the friend request. | 193 | uint32_t friendrequest_nospam; // The nospam number used in the friend request. |
201 | uint64_t last_seen_time; | 194 | uint64_t last_seen_time; |
202 | uint64_t share_relays_lastsent; | ||
203 | uint8_t last_connection_udp_tcp; | 195 | uint8_t last_connection_udp_tcp; |
204 | struct File_Transfers file_sending[MAX_CONCURRENT_FILE_PIPES]; | 196 | struct File_Transfers file_sending[MAX_CONCURRENT_FILE_PIPES]; |
205 | unsigned int num_sending_files; | 197 | 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_ | |||
198 | } | 198 | } |
199 | } | 199 | } |
200 | 200 | ||
201 | static 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. */ |
202 | static int tcp_relay_node_callback(void *object, uint32_t number, IP_Port ip_port, const uint8_t *public_key) | 239 | static 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); |
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 @@ | |||
36 | #define GROUPCHAT_CALLBACK_INDEX 1 | 36 | #define GROUPCHAT_CALLBACK_INDEX 1 |
37 | 37 | ||
38 | #define PACKET_ID_ALIVE 16 | 38 | #define PACKET_ID_ALIVE 16 |
39 | #define PACKET_ID_SHARE_RELAYS 17 | ||
39 | #define PACKET_ID_FRIEND_REQUESTS 18 | 40 | #define PACKET_ID_FRIEND_REQUESTS 18 |
40 | 41 | ||
41 | /* Interval between the sending of ping packets. */ | 42 | /* Interval between the sending of ping packets. */ |
@@ -49,6 +50,13 @@ | |||
49 | 50 | ||
50 | #define FRIEND_MAX_STORED_TCP_RELAYS (MAX_FRIEND_TCP_CONNECTIONS * 4) | 51 | #define FRIEND_MAX_STORED_TCP_RELAYS (MAX_FRIEND_TCP_CONNECTIONS * 4) |
51 | 52 | ||
53 | /* Max number of tcp relays sent to friends */ | ||
54 | #define MAX_SHARED_RELAYS (RECOMMENDED_FRIEND_TCP_CONNECTIONS) | ||
55 | |||
56 | /* Interval between the sending of tcp relay information */ | ||
57 | #define SHARE_RELAYS_INTERVAL (5 * 60) | ||
58 | |||
59 | |||
52 | enum { | 60 | enum { |
53 | FRIENDCONN_STATUS_NONE, | 61 | FRIENDCONN_STATUS_NONE, |
54 | FRIENDCONN_STATUS_CONNECTING, | 62 | FRIENDCONN_STATUS_CONNECTING, |
@@ -68,6 +76,7 @@ typedef struct { | |||
68 | int crypt_connection_id; | 76 | int crypt_connection_id; |
69 | 77 | ||
70 | uint64_t ping_lastrecv, ping_lastsent; | 78 | uint64_t ping_lastrecv, ping_lastsent; |
79 | uint64_t share_relays_lastsent; | ||
71 | 80 | ||
72 | struct { | 81 | struct { |
73 | int (*status_callback)(void *object, int id, uint8_t status); | 82 | int (*status_callback)(void *object, int id, uint8_t status); |