summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2015-04-13 19:29:45 -0400
committerirungentoo <irungentoo@gmail.com>2015-04-13 19:29:45 -0400
commit4b29aff04a1653781485d224de330d144e0819ff (patch)
tree93261d6e0d9e57bf936b4719340769f3f77489f9 /toxcore
parentd05e39274ca4ce27ddb02c0ec15c0ed1c46000a3 (diff)
TCP connection mostly done.
Only thing left is testing and integrating it in net_crypto.
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/TCP_connection.c147
-rw-r--r--toxcore/TCP_connection.h67
2 files changed, 205 insertions, 9 deletions
diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c
index ddcbd1d8..328aa518 100644
--- a/toxcore/TCP_connection.c
+++ b/toxcore/TCP_connection.c
@@ -207,14 +207,17 @@ int send_packet_tcp_connection(TCP_Connections *tcp_c, int connections_number, u
207 return -1; 207 return -1;
208 } 208 }
209 209
210 //TODO: detect and kill bad relays.
211 //TODO: thread safety?
210 unsigned int i; 212 unsigned int i;
211 int ret = -1; 213 int ret = -1;
212 214
213 for (i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) { 215 for (i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
214 uint32_t tcp_con_num = con_to->connections[i].tcp_connection; 216 uint32_t tcp_con_num = con_to->connections[i].tcp_connection;
217 uint8_t status = con_to->connections[i].status;
218 uint8_t connection_id = con_to->connections[i].connection_id;
215 219
216 //TODO 220 if (tcp_con_num && status == TCP_CONN_CONNECTED) {
217 if (tcp_con_num) {
218 tcp_con_num -= 1; 221 tcp_con_num -= 1;
219 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_con_num); 222 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_con_num);
220 223
@@ -222,13 +225,66 @@ int send_packet_tcp_connection(TCP_Connections *tcp_c, int connections_number, u
222 continue; 225 continue;
223 } 226 }
224 227
225 // ret = send_data(c->tcp_connections[tcp_index], conn->con_number_tcp[tcp_index], packet, length); 228 ret = send_data(tcp_con->connection, connection_id, packet, length);
229
230 if (ret == 1) {
231 break;
232 }
226 } 233 }
227 } 234 }
228 235
229 return ret; 236 if (ret == 1) {
237 return 0;
238 } else {
239 return -1;
240 }
230} 241}
231 242
243/* Return a random TCP connection number for use in send_tcp_onion_request.
244 *
245 * TODO: This number is just the index of an array that the elements can
246 * change without warning.
247 *
248 * return TCP connection number on success.
249 * return -1 on failure.
250 */
251int get_random_tcp_conn_number(TCP_Connections *tcp_c)
252{
253 unsigned int i, r = rand();
254
255 for (i = 0; i < tcp_c->tcp_connections_length; ++i) {
256 if (tcp_c->tcp_connections[(i + r) % tcp_c->tcp_connections_length].status == TCP_CONN_CONNECTED) {
257 return ((i + r) % tcp_c->tcp_connections_length);
258 }
259 }
260
261 return -1;
262}
263
264/* Send an onion packet via the TCP relay corresponding to tcp_connections_number.
265 *
266 * return 0 on success.
267 * return -1 on failure.
268 */
269int tcp_send_onion_request(TCP_Connections *tcp_c, unsigned int tcp_connections_number, const uint8_t *data,
270 uint16_t length)
271{
272 if (tcp_connections_number >= tcp_c->tcp_connections_length) {
273 return -1;
274 }
275
276 if (tcp_c->tcp_connections[tcp_connections_number].status == TCP_CONN_CONNECTED) {
277 int ret = send_onion_request(tcp_c->tcp_connections[tcp_connections_number].connection, data, length);
278
279 if (ret == 1)
280 return 0;
281 }
282
283 return -1;
284}
285
286/* Set the callback for TCP data packets.
287 */
232void set_packet_tcp_connection_callback(TCP_Connections *tcp_c, int (*tcp_data_callback)(void *object, int id, 288void set_packet_tcp_connection_callback(TCP_Connections *tcp_c, int (*tcp_data_callback)(void *object, int id,
233 const uint8_t *data, uint16_t length), void *object) 289 const uint8_t *data, uint16_t length), void *object)
234{ 290{
@@ -236,6 +292,25 @@ void set_packet_tcp_connection_callback(TCP_Connections *tcp_c, int (*tcp_data_c
236 tcp_c->tcp_data_callback_object = object; 292 tcp_c->tcp_data_callback_object = object;
237} 293}
238 294
295/* Set the callback for TCP oob data packets.
296 */
297void set_oob_packet_tcp_connection_callback(TCP_Connections *tcp_c, int (*tcp_oob_callback)(void *object,
298 const uint8_t *public_key, const uint8_t *relay_pk, const uint8_t *data, uint16_t length), void *object)
299{
300 tcp_c->tcp_oob_callback = tcp_oob_callback;
301 tcp_c->tcp_oob_callback_object = object;
302}
303
304/* Set the callback for TCP oob data packets.
305 */
306void set_onion_packet_tcp_connection_callback(TCP_Connections *tcp_c, int (*tcp_onion_callback)(void *object,
307 const uint8_t *data, uint16_t length), void *object)
308{
309 tcp_c->tcp_onion_callback = tcp_onion_callback;
310 tcp_c->tcp_onion_callback_object = object;
311}
312
313
239/* Find the TCP connection with public_key. 314/* Find the TCP connection with public_key.
240 * 315 *
241 * return connections_number on success. 316 * return connections_number on success.
@@ -316,7 +391,24 @@ int kill_tcp_connection_to(TCP_Connections *tcp_c, int connections_number)
316 if (!con_to) 391 if (!con_to)
317 return -1; 392 return -1;
318 393
319 //TODO 394 unsigned int i;
395
396 for (i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
397 if (con_to->connections[i].tcp_connection) {
398 unsigned int tcp_connections_number = con_to->connections[i].tcp_connection - 1;
399 TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
400
401 if (!tcp_con)
402 continue;
403
404 if (tcp_con->status == TCP_CONN_CONNECTED) {
405 send_disconnect_request(tcp_con->connection, con_to->connections[i].connection_id);
406 }
407
408 --tcp_con->lock_count;
409 }
410 }
411
320 return wipe_connection(tcp_c, connections_number); 412 return wipe_connection(tcp_c, connections_number);
321} 413}
322 414
@@ -621,6 +713,19 @@ static int add_tcp_relay(TCP_Connections *tcp_c, IP_Port ip_port, const uint8_t
621 return tcp_connections_number; 713 return tcp_connections_number;
622} 714}
623 715
716/* Add a TCP relay to the instance.
717 *
718 * return 0 on success.
719 * return -1 on failure.
720 */
721int add_tcp_relay_global(TCP_Connections *tcp_c, IP_Port ip_port, const uint8_t *relay_pk)
722{
723 if (add_tcp_relay(tcp_c, ip_port, relay_pk) == -1)
724 return -1;
725
726 return 0;
727}
728
624/* Add a TCP relay tied to a connection. 729/* Add a TCP relay tied to a connection.
625 * 730 *
626 * return 0 on success. 731 * return 0 on success.
@@ -668,7 +773,6 @@ int add_tcp_relay_connection(TCP_Connections *tcp_c, int connections_number, IP_
668 } 773 }
669} 774}
670 775
671
672TCP_Connections *new_tcp_connections(DHT *dht) 776TCP_Connections *new_tcp_connections(DHT *dht)
673{ 777{
674 if (dht == NULL) 778 if (dht == NULL)
@@ -705,17 +809,42 @@ static void do_tcp_conns(TCP_Connections *tcp_c)
705 } 809 }
706} 810}
707 811
812static void kill_nonused_tcp(TCP_Connections *tcp_c)
813{
814 unsigned int i, num_online = 0;
815
816 for (i = 0; i < tcp_c->tcp_connections_length; ++i) {
817 TCP_con *tcp_con = get_tcp_connection(tcp_c, i);
818
819 if (tcp_con) {
820 if (tcp_con->status == TCP_CONN_CONNECTED) {
821 if (!tcp_con->lock_count && num_online >= MAX_FRIEND_TCP_CONNECTIONS) {
822 kill_tcp_relay_connection(tcp_c, i);
823 continue;
824 }
825
826 ++num_online;
827 }
828 }
829 }
830}
708 831
709void do_tcp_connections(TCP_Connections *tcp_c) 832void do_tcp_connections(TCP_Connections *tcp_c)
710{ 833{
711//TODO kill unused conns
712
713 do_tcp_conns(tcp_c); 834 do_tcp_conns(tcp_c);
835 kill_nonused_tcp(tcp_c);
714} 836}
715 837
716void kill_tcp_connections(TCP_Connections *tcp_c) 838void kill_tcp_connections(TCP_Connections *tcp_c)
717{ 839{
718 //TODO 840 unsigned int i;
841
842 for (i = 0; i < tcp_c->tcp_connections_length; ++i) {
843 kill_TCP_connection(tcp_c->tcp_connections[i].connection);
844 }
845
846 free(tcp_c->tcp_connections);
847 free(tcp_c->connections);
719 free(tcp_c); 848 free(tcp_c);
720} 849}
721 850
diff --git a/toxcore/TCP_connection.h b/toxcore/TCP_connection.h
index 94213814..fc7925a3 100644
--- a/toxcore/TCP_connection.h
+++ b/toxcore/TCP_connection.h
@@ -78,6 +78,73 @@ typedef struct {
78 TCP_Proxy_Info proxy_info; 78 TCP_Proxy_Info proxy_info;
79} TCP_Connections; 79} TCP_Connections;
80 80
81/* Send a packet to the TCP connection.
82 *
83 * return -1 on failure.
84 * return 0 on success.
85 */
86int send_packet_tcp_connection(TCP_Connections *tcp_c, int connections_number, uint8_t *packet, uint16_t length);
87
88/* Return a random TCP connection number for use in send_tcp_onion_request.
89 *
90 * TODO: This number is just the index of an array that the elements can
91 * change without warning.
92 *
93 * return TCP connection number on success.
94 * return -1 on failure.
95 */
96int get_random_tcp_conn_number(TCP_Connections *tcp_c);
97
98/* Send an onion packet via the TCP relay corresponding to tcp_connections_number.
99 *
100 * return 0 on success.
101 * return -1 on failure.
102 */
103int tcp_send_onion_request(TCP_Connections *tcp_c, unsigned int tcp_connections_number, const uint8_t *data,
104 uint16_t length);
105
106/* Set the callback for TCP data packets.
107 */
108void set_packet_tcp_connection_callback(TCP_Connections *tcp_c, int (*tcp_data_callback)(void *object, int id,
109 const uint8_t *data, uint16_t length), void *object);
110
111/* Set the callback for TCP oob data packets.
112 */
113void set_onion_packet_tcp_connection_callback(TCP_Connections *tcp_c, int (*tcp_onion_callback)(void *object,
114 const uint8_t *data, uint16_t length), void *object);
115
116/* Set the callback for TCP oob data packets.
117 */
118void set_oob_packet_tcp_connection_callback(TCP_Connections *tcp_c, int (*tcp_oob_callback)(void *object,
119 const uint8_t *public_key, const uint8_t *relay_pk, const uint8_t *data, uint16_t length), void *object);
120
121/* Create a new TCP connection to public_key.
122 *
123 * id is the id in the callbacks for that connection.
124 *
125 * return connections_number on success.
126 * return -1 on failure.
127 */
128int new_tcp_connection_to(TCP_Connections *tcp_c, const uint8_t *public_key, int id);
129
130/* return 0 on success.
131 * return -1 on failure.
132 */
133int kill_tcp_connection_to(TCP_Connections *tcp_c, int connections_number);
134
135/* Add a TCP relay tied to a connection.
136 *
137 * return 0 on success.
138 * return -1 on failure.
139 */
140int add_tcp_relay_connection(TCP_Connections *tcp_c, int connections_number, IP_Port ip_port, const uint8_t *relay_pk);
141
142/* Add a TCP relay to the instance.
143 *
144 * return 0 on success.
145 * return -1 on failure.
146 */
147int add_tcp_relay_global(TCP_Connections *tcp_c, IP_Port ip_port, const uint8_t *relay_pk);
81 148
82 149
83TCP_Connections *new_tcp_connections(DHT *dht); 150TCP_Connections *new_tcp_connections(DHT *dht);