summaryrefslogtreecommitdiff
path: root/toxcore/Lossless_UDP.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/Lossless_UDP.c')
-rw-r--r--toxcore/Lossless_UDP.c110
1 files changed, 59 insertions, 51 deletions
diff --git a/toxcore/Lossless_UDP.c b/toxcore/Lossless_UDP.c
index c30eb903..6046c09b 100644
--- a/toxcore/Lossless_UDP.c
+++ b/toxcore/Lossless_UDP.c
@@ -32,9 +32,9 @@
32/* Functions */ 32/* Functions */
33 33
34/* 34/*
35 * Get connection id from IP_Port 35 * Get connection id from IP_Port.
36 * Return -1 if there are no connections like we are looking for 36 * return -1 if there are no connections like we are looking for.
37 * Return id if it found it 37 * return id if it found it.
38 */ 38 */
39int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port) 39int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port)
40{ 40{
@@ -68,16 +68,17 @@ static uint32_t handshake_id(Lossless_UDP *ludp, IP_Port source)
68 id ^= ludp->randtable[i][((uint8_t *)&source)[i]]; 68 id ^= ludp->randtable[i][((uint8_t *)&source)[i]];
69 } 69 }
70 70
71 if (id == 0) /* id can't be zero */ 71 /* id can't be zero. */
72 if (id == 0)
72 id = 1; 73 id = 1;
73 74
74 return id; 75 return id;
75} 76}
76 77
77/* 78/*
78 * Change the hanshake id associated with that ip_port 79 * Change the handshake id associated with that ip_port.
79 * 80 *
80 * TODO: make this better 81 * TODO: Make this better
81 */ 82 */
82static void change_handshake(Lossless_UDP *ludp, IP_Port source) 83static void change_handshake(Lossless_UDP *ludp, IP_Port source)
83{ 84{
@@ -86,9 +87,9 @@ static void change_handshake(Lossless_UDP *ludp, IP_Port source)
86} 87}
87 88
88/* 89/*
89 * Initialize a new connection to ip_port 90 * Initialize a new connection to ip_port.
90 * Returns an integer corresponding to the connection idt 91 * return an integer corresponding to the connection idt.
91 * Return -1 if it could not initialize the connectiont 92 * return -1 if it could not initialize the connectiont
92 * If there already was an existing connection to that ip_port return its number. 93 * If there already was an existing connection to that ip_port return its number.
93 */ 94 */
94int new_connection(Lossless_UDP *ludp, IP_Port ip_port) 95int new_connection(Lossless_UDP *ludp, IP_Port ip_port)
@@ -131,7 +132,7 @@ int new_connection(Lossless_UDP *ludp, IP_Port ip_port)
131 .last_sent = current_time(), 132 .last_sent = current_time(),
132 .killat = ~0, 133 .killat = ~0,
133 .send_counter = 0, 134 .send_counter = 0,
134 /* add randomness to timeout to prevent connections getting stuck in a loop. */ 135 /* Add randomness to timeout to prevent connections getting stuck in a loop. */
135 .timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT 136 .timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT
136 }; 137 };
137 ++ludp->connections_number; 138 ++ludp->connections_number;
@@ -144,9 +145,9 @@ int new_connection(Lossless_UDP *ludp, IP_Port ip_port)
144} 145}
145 146
146/* 147/*
147 * Initialize a new inbound connection from ip_port 148 * Initialize a new inbound connection from ip_port.
148 * Returns an integer corresponding to the connection id. 149 * return an integer corresponding to the connection id.
149 * Return -1 if it could not initialize the connection. 150 * return -1 if it could not initialize the connection.
150 */ 151 */
151static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port) 152static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port)
152{ 153{
@@ -182,10 +183,10 @@ static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port)
182 .last_sent = current_time(), 183 .last_sent = current_time(),
183 .send_counter = 127, 184 .send_counter = 127,
184 185
185 /* add randomness to timeout to prevent connections getting stuck in a loop. */ 186 /* Add randomness to timeout to prevent connections getting stuck in a loop. */
186 .timeout = timeout, 187 .timeout = timeout,
187 188
188 /* if this connection isn't handled within the timeout kill it. */ 189 /* If this connection isn't handled within the timeout kill it. */
189 .killat = current_time() + 1000000UL * timeout 190 .killat = current_time() + 1000000UL * timeout
190 }; 191 };
191 ++ludp->connections_number; 192 ++ludp->connections_number;
@@ -197,8 +198,8 @@ static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port)
197} 198}
198 199
199/* 200/*
200 * Returns an integer corresponding to the next connection in our incoming connection list. 201 * return an integer corresponding to the next connection in our incoming connection list.
201 * Return -1 if there are no new incoming connections in the list. 202 * return -1 if there are no new incoming connections in the list.
202 */ 203 */
203int incoming_connection(Lossless_UDP *ludp) 204int incoming_connection(Lossless_UDP *ludp)
204{ 205{
@@ -244,8 +245,8 @@ static void free_connections(Lossless_UDP *ludp)
244} 245}
245 246
246/* 247/*
247 * Return -1 if it could not kill the connection. 248 * return -1 if it could not kill the connection.
248 * Return 0 if killed successfully 249 * return 0 if killed successfully.
249 */ 250 */
250int kill_connection(Lossless_UDP *ludp, int connection_id) 251int kill_connection(Lossless_UDP *ludp, int connection_id)
251{ 252{
@@ -264,8 +265,8 @@ int kill_connection(Lossless_UDP *ludp, int connection_id)
264 265
265/* 266/*
266 * Kill connection in seconds. 267 * Kill connection in seconds.
267 * Return -1 if it can not kill the connection. 268 * return -1 if it can not kill the connection.
268 * Return 0 if it will kill it. 269 * return 0 if it will kill it.
269 */ 270 */
270int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds) 271int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds)
271{ 272{
@@ -281,11 +282,11 @@ int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds)
281 282
282/* 283/*
283 * Check if connection is connected: 284 * Check if connection is connected:
284 * Return 0 no. 285 * return 0 no.
285 * Return 1 if attempting handshake. 286 * return 1 if attempting handshake.
286 * Return 2 if handshake is done. 287 * return 2 if handshake is done.
287 * Return 3 if fully connected. 288 * return 3 if fully connected.
288 * Return 4 if timed out and waiting to be killed. 289 * return 4 if timed out and waiting to be killed.
289 */ 290 */
290int is_connected(Lossless_UDP *ludp, int connection_id) 291int is_connected(Lossless_UDP *ludp, int connection_id)
291{ 292{
@@ -295,7 +296,7 @@ int is_connected(Lossless_UDP *ludp, int connection_id)
295 return 0; 296 return 0;
296} 297}
297 298
298/* returns the ip_port of the corresponding connection. */ 299/* return the ip_port of the corresponding connection. */
299IP_Port connection_ip(Lossless_UDP *ludp, int connection_id) 300IP_Port connection_ip(Lossless_UDP *ludp, int connection_id)
300{ 301{
301 if (connection_id >= 0 && connection_id < ludp->connections_length) 302 if (connection_id >= 0 && connection_id < ludp->connections_length)
@@ -314,7 +315,7 @@ uint32_t sendqueue(Lossless_UDP *ludp, int connection_id)
314 return ludp->connections[connection_id].sendbuff_packetnum - ludp->connections[connection_id].successful_sent; 315 return ludp->connections[connection_id].sendbuff_packetnum - ludp->connections[connection_id].successful_sent;
315} 316}
316 317
317/* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */ 318/* returns the number of packets in the queue waiting to be successfully read with read_packet(...). */
318uint32_t recvqueue(Lossless_UDP *ludp, int connection_id) 319uint32_t recvqueue(Lossless_UDP *ludp, int connection_id)
319{ 320{
320 if (connection_id < 0 || connection_id >= ludp->connections_length) 321 if (connection_id < 0 || connection_id >= ludp->connections_length)
@@ -323,8 +324,9 @@ uint32_t recvqueue(Lossless_UDP *ludp, int connection_id)
323 return ludp->connections[connection_id].recv_packetnum - ludp->connections[connection_id].successful_read; 324 return ludp->connections[connection_id].recv_packetnum - ludp->connections[connection_id].successful_read;
324} 325}
325 326
326/* returns the id of the next packet in the queue 327/* returns the id of the next packet in the queue.
327 return -1 if no packet in queue */ 328 * return -1 if no packet in queue.
329 */
328char id_packet(Lossless_UDP *ludp, int connection_id) 330char id_packet(Lossless_UDP *ludp, int connection_id)
329{ 331{
330 if (connection_id < 0 || connection_id >= ludp->connections_length) 332 if (connection_id < 0 || connection_id >= ludp->connections_length)
@@ -338,7 +340,8 @@ char id_packet(Lossless_UDP *ludp, int connection_id)
338} 340}
339 341
340/* return 0 if there is no received data in the buffer. 342/* return 0 if there is no received data in the buffer.
341 return length of received packet if successful */ 343 * return length of received packet if successful.
344 */
342int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data) 345int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data)
343{ 346{
344 if (recvqueue(ludp, connection_id) != 0) { 347 if (recvqueue(ludp, connection_id) != 0) {
@@ -354,8 +357,8 @@ int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data)
354} 357}
355 358
356/* 359/*
357 * Return 0 if data could not be put in packet queue 360 * return 0 if data could not be put in packet queue.
358 * Return 1 if data was put into the queue 361 * return 1 if data was put into the queue.
359 */ 362 */
360int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t length) 363int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t length)
361{ 364{
@@ -373,14 +376,14 @@ int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t
373 return 0; 376 return 0;
374} 377}
375 378
376/* put the packet numbers the we are missing in requested and return the number */ 379/* Put the packet numbers the we are missing in requested and return the number. */
377uint32_t missing_packets(Lossless_UDP *ludp, int connection_id, uint32_t *requested) 380uint32_t missing_packets(Lossless_UDP *ludp, int connection_id, uint32_t *requested)
378{ 381{
379 uint32_t number = 0; 382 uint32_t number = 0;
380 uint32_t i; 383 uint32_t i;
381 uint32_t temp; 384 uint32_t temp;
382 385
383 /* don't request packets if the buffer is full. */ 386 /* Don't request packets if the buffer is full. */
384 if (recvqueue(ludp, connection_id) >= (BUFFER_PACKET_NUM - 1)) 387 if (recvqueue(ludp, connection_id) >= (BUFFER_PACKET_NUM - 1))
385 return 0; 388 return 0;
386 389
@@ -399,9 +402,9 @@ uint32_t missing_packets(Lossless_UDP *ludp, int connection_id, uint32_t *reques
399} 402}
400 403
401/* 404/*
402 * BEGIN Packet sending functions 405 * BEGIN Packet sending functions.
403 * One per packet type. 406 * One per packet type.
404 * see http://wiki.tox.im/index.php/Lossless_UDP for more information. 407 * See http://wiki.tox.im/index.php/Lossless_UDP for more information.
405 */ 408 */
406 409
407static int send_handshake(Lossless_UDP *ludp, IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2) 410static int send_handshake(Lossless_UDP *ludp, IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2)
@@ -459,7 +462,7 @@ static int send_data_packet(Lossless_UDP *ludp, uint32_t connection_id, uint32_t
459 1 + 4 + ludp->connections[connection_id].sendbuffer[index].size); 462 1 + 4 + ludp->connections[connection_id].sendbuffer[index].size);
460} 463}
461 464
462/* sends 1 data packet */ 465/* Sends 1 data packet. */
463static int send_DATA(Lossless_UDP *ludp, uint32_t connection_id) 466static int send_DATA(Lossless_UDP *ludp, uint32_t connection_id)
464{ 467{
465 int ret; 468 int ret;
@@ -483,15 +486,17 @@ static int send_DATA(Lossless_UDP *ludp, uint32_t connection_id)
483} 486}
484 487
485/* 488/*
486 * END of packet sending functions 489 * END of packet sending functions.
487 * 490 *
488 * 491 *
489 * BEGIN Packet handling functions 492 * BEGIN Packet handling functions.
490 * One to handle each type of packets we receive 493 * One to handle each type of packets we receive.
491 */ 494 */
492 495
493 496
494/* Return 0 if handled correctly, 1 if packet is bad. */ 497/* return 0 if handled correctly.
498 * return 1 if packet is bad.
499 */
495static int handle_handshake(void *object, IP_Port source, uint8_t *packet, uint32_t length) 500static int handle_handshake(void *object, IP_Port source, uint8_t *packet, uint32_t length)
496{ 501{
497 Lossless_UDP *ludp = object; 502 Lossless_UDP *ludp = object;
@@ -516,11 +521,12 @@ static int handle_handshake(void *object, IP_Port source, uint8_t *packet, uint3
516 if (is_connected(ludp, connection) != 1) 521 if (is_connected(ludp, connection) != 1)
517 return 1; 522 return 1;
518 523
519 /* if handshake_id2 is what we sent previously as handshake_id1 */ 524 /* If handshake_id2 is what we sent previously as handshake_id1. */
520 if (handshake_id2 == ludp->connections[connection].handshake_id1) { 525 if (handshake_id2 == ludp->connections[connection].handshake_id1) {
521 ludp->connections[connection].status = 2; 526 ludp->connections[connection].status = 2;
522 /* NOTE: is this necessary? 527 /* NOTE: is this necessary?
523 ludp->connections[connection].handshake_id2 = handshake_id1; */ 528 * ludp->connections[connection].handshake_id2 = handshake_id1;
529 */
524 ludp->connections[connection].orecv_packetnum = handshake_id2; 530 ludp->connections[connection].orecv_packetnum = handshake_id2;
525 ludp->connections[connection].osent_packetnum = handshake_id1; 531 ludp->connections[connection].osent_packetnum = handshake_id1;
526 ludp->connections[connection].recv_packetnum = handshake_id1; 532 ludp->connections[connection].recv_packetnum = handshake_id1;
@@ -593,7 +599,7 @@ static int handle_SYNC3(Lossless_UDP *ludp, int connection_id, uint8_t counter,
593 uint32_t comp_1 = (recv_packetnum - ludp->connections[connection_id].orecv_packetnum); 599 uint32_t comp_1 = (recv_packetnum - ludp->connections[connection_id].orecv_packetnum);
594 uint32_t comp_2 = (sent_packetnum - ludp->connections[connection_id].osent_packetnum); 600 uint32_t comp_2 = (sent_packetnum - ludp->connections[connection_id].osent_packetnum);
595 601
596 /* packet valid */ 602 /* Packet valid. */
597 if (comp_1 <= BUFFER_PACKET_NUM && 603 if (comp_1 <= BUFFER_PACKET_NUM &&
598 comp_2 <= BUFFER_PACKET_NUM && 604 comp_2 <= BUFFER_PACKET_NUM &&
599 comp_counter < 10 && comp_counter != 0) { 605 comp_counter < 10 && comp_counter != 0) {
@@ -657,7 +663,9 @@ static int handle_SYNC(void *object, IP_Port source, uint8_t *packet, uint32_t l
657 663
658/* 664/*
659 * Add a packet to the received buffer and set the recv_packetnum of the 665 * Add a packet to the received buffer and set the recv_packetnum of the
660 * connection to its proper value. Return 1 if data was too big, 0 if not. 666 * connection to its proper value.
667 * return 1 if data was too big.
668 * return 0 if not.
661 */ 669 */
662static int add_recv(Lossless_UDP *ludp, int connection_id, uint32_t data_num, uint8_t *data, uint16_t size) 670static int add_recv(Lossless_UDP *ludp, int connection_id, uint32_t data_num, uint8_t *data, uint16_t size)
663{ 671{
@@ -719,7 +727,7 @@ static int handle_data(void *object, IP_Port source, uint8_t *packet, uint32_t l
719} 727}
720 728
721/* 729/*
722 * END of packet handling functions 730 * END of packet handling functions.
723 */ 731 */
724 732
725Lossless_UDP *new_lossless_udp(Networking_Core *net) 733Lossless_UDP *new_lossless_udp(Networking_Core *net)
@@ -740,8 +748,8 @@ Lossless_UDP *new_lossless_udp(Networking_Core *net)
740} 748}
741 749
742/* 750/*
743 * Send handshake requests 751 * Send handshake requests.
744 * handshake packets are sent at the same rate as SYNC packets 752 * Handshake packets are sent at the same rate as SYNC packets.
745 */ 753 */
746static void do_new(Lossless_UDP *ludp) 754static void do_new(Lossless_UDP *ludp)
747{ 755{
@@ -755,7 +763,7 @@ static void do_new(Lossless_UDP *ludp)
755 ludp->connections[i].last_sent = temp_time; 763 ludp->connections[i].last_sent = temp_time;
756 } 764 }
757 765
758 /* kill all timed out connections */ 766 /* Kill all timed out connections. */
759 if (ludp->connections[i].status > 0 && 767 if (ludp->connections[i].status > 0 &&
760 (ludp->connections[i].last_recvSYNC + ludp->connections[i].timeout * 1000000UL) < temp_time && 768 (ludp->connections[i].last_recvSYNC + ludp->connections[i].timeout * 1000000UL) < temp_time &&
761 ludp->connections[i].status != 4) { 769 ludp->connections[i].status != 4) {
@@ -803,7 +811,7 @@ static void do_data(Lossless_UDP *ludp)
803/* 811/*
804 * Automatically adjusts send rates of packets for optimal transmission. 812 * Automatically adjusts send rates of packets for optimal transmission.
805 * 813 *
806 * TODO: flow control. 814 * TODO: Flow control.
807 */ 815 */
808static void adjust_rates(Lossless_UDP *ludp) 816static void adjust_rates(Lossless_UDP *ludp)
809{ 817{