summaryrefslogtreecommitdiff
path: root/core/Lossless_UDP.c
diff options
context:
space:
mode:
Diffstat (limited to 'core/Lossless_UDP.c')
-rw-r--r--core/Lossless_UDP.c92
1 files changed, 50 insertions, 42 deletions
diff --git a/core/Lossless_UDP.c b/core/Lossless_UDP.c
index 7ece28e1..15e8dea3 100644
--- a/core/Lossless_UDP.c
+++ b/core/Lossless_UDP.c
@@ -36,7 +36,7 @@
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{
41 uint32_t i; 41 uint32_t i;
42 42
@@ -57,7 +57,7 @@ int getconnection_id(Lossless_UDP * ludp, IP_Port ip_port)
57 * 57 *
58 * TODO: make this better 58 * TODO: make this better
59 */ 59 */
60static uint32_t handshake_id(Lossless_UDP * ludp, IP_Port source) 60static uint32_t handshake_id(Lossless_UDP *ludp, IP_Port source)
61{ 61{
62 uint32_t id = 0, i; 62 uint32_t id = 0, i;
63 63
@@ -79,7 +79,7 @@ static uint32_t handshake_id(Lossless_UDP * ludp, IP_Port source)
79 * 79 *
80 * TODO: make this better 80 * TODO: make this better
81 */ 81 */
82static void change_handshake(Lossless_UDP * ludp, IP_Port source) 82static void change_handshake(Lossless_UDP *ludp, IP_Port source)
83{ 83{
84 uint8_t rand = random_int() % 4; 84 uint8_t rand = random_int() % 4;
85 ludp->randtable[rand][((uint8_t *)&source)[rand]] = random_int(); 85 ludp->randtable[rand][((uint8_t *)&source)[rand]] = random_int();
@@ -91,7 +91,7 @@ static void change_handshake(Lossless_UDP * ludp, IP_Port source)
91 * Return -1 if it could not initialize the connectiont 91 * Return -1 if it could not initialize the connectiont
92 * If there already was an existing connection to that ip_port return its number. 92 * If there already was an existing connection to that ip_port return its number.
93 */ 93 */
94int new_connection(Lossless_UDP * ludp, IP_Port ip_port) 94int new_connection(Lossless_UDP *ludp, IP_Port ip_port)
95{ 95{
96 int connect = getconnection_id(ludp, ip_port); 96 int connect = getconnection_id(ludp, ip_port);
97 97
@@ -148,7 +148,7 @@ int new_connection(Lossless_UDP * ludp, IP_Port ip_port)
148 * Returns an integer corresponding to the connection id. 148 * Returns an integer corresponding to the connection id.
149 * Return -1 if it could not initialize the connection. 149 * Return -1 if it could not initialize the connection.
150 */ 150 */
151static int new_inconnection(Lossless_UDP * ludp, IP_Port ip_port) 151static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port)
152{ 152{
153 if (getconnection_id(ludp, ip_port) != -1) 153 if (getconnection_id(ludp, ip_port) != -1)
154 return -1; 154 return -1;
@@ -200,7 +200,7 @@ static int new_inconnection(Lossless_UDP * ludp, IP_Port ip_port)
200 * Returns an integer corresponding to the next connection in our incoming connection list. 200 * Returns 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. 201 * Return -1 if there are no new incoming connections in the list.
202 */ 202 */
203int incoming_connection(Lossless_UDP * ludp) 203int incoming_connection(Lossless_UDP *ludp)
204{ 204{
205 uint32_t i; 205 uint32_t i;
206 206
@@ -215,7 +215,7 @@ int incoming_connection(Lossless_UDP * ludp)
215} 215}
216 216
217/* Try to free some memory from the connections array. */ 217/* Try to free some memory from the connections array. */
218static void free_connections(Lossless_UDP * ludp) 218static void free_connections(Lossless_UDP *ludp)
219{ 219{
220 uint32_t i; 220 uint32_t i;
221 221
@@ -247,7 +247,7 @@ static void free_connections(Lossless_UDP * ludp)
247 * Return -1 if it could not kill the connection. 247 * Return -1 if it could not kill the connection.
248 * Return 0 if killed successfully 248 * Return 0 if killed successfully
249 */ 249 */
250int kill_connection(Lossless_UDP * ludp, int connection_id) 250int kill_connection(Lossless_UDP *ludp, int connection_id)
251{ 251{
252 if (connection_id >= 0 && connection_id < ludp->connections_length) { 252 if (connection_id >= 0 && connection_id < ludp->connections_length) {
253 if (ludp->connections[connection_id].status > 0) { 253 if (ludp->connections[connection_id].status > 0) {
@@ -267,7 +267,7 @@ int kill_connection(Lossless_UDP * ludp, int connection_id)
267 * Return -1 if it can not kill the connection. 267 * Return -1 if it can not kill the connection.
268 * Return 0 if it will kill it. 268 * Return 0 if it will kill it.
269 */ 269 */
270int kill_connection_in(Lossless_UDP * ludp, int connection_id, uint32_t seconds) 270int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds)
271{ 271{
272 if (connection_id >= 0 && connection_id < ludp->connections_length) { 272 if (connection_id >= 0 && connection_id < ludp->connections_length) {
273 if (ludp->connections[connection_id].status > 0) { 273 if (ludp->connections[connection_id].status > 0) {
@@ -287,7 +287,7 @@ int kill_connection_in(Lossless_UDP * ludp, int connection_id, uint32_t seconds)
287 * Return 3 if fully connected. 287 * Return 3 if fully connected.
288 * Return 4 if timed out and waiting to be killed. 288 * Return 4 if timed out and waiting to be killed.
289 */ 289 */
290int is_connected(Lossless_UDP * ludp, int connection_id) 290int is_connected(Lossless_UDP *ludp, int connection_id)
291{ 291{
292 if (connection_id >= 0 && connection_id < ludp->connections_length) 292 if (connection_id >= 0 && connection_id < ludp->connections_length)
293 return ludp->connections[connection_id].status; 293 return ludp->connections[connection_id].status;
@@ -296,7 +296,7 @@ int is_connected(Lossless_UDP * ludp, int connection_id)
296} 296}
297 297
298/* returns the ip_port of the corresponding connection. */ 298/* returns the ip_port of the corresponding connection. */
299IP_Port connection_ip(Lossless_UDP * ludp, int connection_id) 299IP_Port connection_ip(Lossless_UDP *ludp, int connection_id)
300{ 300{
301 if (connection_id >= 0 && connection_id < ludp->connections_length) 301 if (connection_id >= 0 && connection_id < ludp->connections_length)
302 return ludp->connections[connection_id].ip_port; 302 return ludp->connections[connection_id].ip_port;
@@ -306,7 +306,7 @@ IP_Port connection_ip(Lossless_UDP * ludp, int connection_id)
306} 306}
307 307
308/* returns the number of packets in the queue waiting to be successfully sent. */ 308/* returns the number of packets in the queue waiting to be successfully sent. */
309uint32_t sendqueue(Lossless_UDP * ludp, int connection_id) 309uint32_t sendqueue(Lossless_UDP *ludp, int connection_id)
310{ 310{
311 if (connection_id < 0 || connection_id >= ludp->connections_length) 311 if (connection_id < 0 || connection_id >= ludp->connections_length)
312 return 0; 312 return 0;
@@ -315,7 +315,7 @@ uint32_t sendqueue(Lossless_UDP * ludp, int connection_id)
315} 315}
316 316
317/* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */ 317/* 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) 318uint32_t recvqueue(Lossless_UDP *ludp, int connection_id)
319{ 319{
320 if (connection_id < 0 || connection_id >= ludp->connections_length) 320 if (connection_id < 0 || connection_id >= ludp->connections_length)
321 return 0; 321 return 0;
@@ -325,20 +325,21 @@ uint32_t recvqueue(Lossless_UDP * ludp, int connection_id)
325 325
326/* returns the id of the next packet in the queue 326/* returns the id of the next packet in the queue
327 return -1 if no packet in queue */ 327 return -1 if no packet in queue */
328char id_packet(Lossless_UDP * ludp, int connection_id) 328char id_packet(Lossless_UDP *ludp, int connection_id)
329{ 329{
330 if (connection_id < 0 || connection_id >= ludp->connections_length) 330 if (connection_id < 0 || connection_id >= ludp->connections_length)
331 return -1; 331 return -1;
332 332
333 if (recvqueue(ludp, connection_id) != 0 && ludp->connections[connection_id].status != 0) 333 if (recvqueue(ludp, connection_id) != 0 && ludp->connections[connection_id].status != 0)
334 return ludp->connections[connection_id].recvbuffer[ludp->connections[connection_id].successful_read % MAX_QUEUE_NUM].data[0]; 334 return ludp->connections[connection_id].recvbuffer[ludp->connections[connection_id].successful_read %
335 MAX_QUEUE_NUM].data[0];
335 336
336 return -1; 337 return -1;
337} 338}
338 339
339/* return 0 if there is no received data in the buffer. 340/* return 0 if there is no received data in the buffer.
340 return length of received packet if successful */ 341 return length of received packet if successful */
341int read_packet(Lossless_UDP * ludp, int connection_id, uint8_t *data) 342int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data)
342{ 343{
343 if (recvqueue(ludp, connection_id) != 0) { 344 if (recvqueue(ludp, connection_id) != 0) {
344 uint16_t index = ludp->connections[connection_id].successful_read % MAX_QUEUE_NUM; 345 uint16_t index = ludp->connections[connection_id].successful_read % MAX_QUEUE_NUM;
@@ -356,7 +357,7 @@ int read_packet(Lossless_UDP * ludp, int connection_id, uint8_t *data)
356 * Return 0 if data could not be put in packet queue 357 * Return 0 if data could not be put in packet queue
357 * Return 1 if data was put into the queue 358 * Return 1 if data was put into the queue
358 */ 359 */
359int write_packet(Lossless_UDP * ludp, int connection_id, uint8_t *data, uint32_t length) 360int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t length)
360{ 361{
361 if (length > MAX_DATA_SIZE || length == 0) 362 if (length > MAX_DATA_SIZE || length == 0)
362 return 0; 363 return 0;
@@ -373,7 +374,7 @@ int write_packet(Lossless_UDP * ludp, int connection_id, uint8_t *data, uint32_t
373} 374}
374 375
375/* put the packet numbers the we are missing in requested and return the number */ 376/* put the packet numbers the we are missing in requested and return the number */
376uint32_t missing_packets(Lossless_UDP * ludp, int connection_id, uint32_t *requested) 377uint32_t missing_packets(Lossless_UDP *ludp, int connection_id, uint32_t *requested)
377{ 378{
378 uint32_t number = 0; 379 uint32_t number = 0;
379 uint32_t i; 380 uint32_t i;
@@ -403,7 +404,7 @@ uint32_t missing_packets(Lossless_UDP * ludp, int connection_id, uint32_t *reque
403 * see http://wiki.tox.im/index.php/Lossless_UDP for more information. 404 * see http://wiki.tox.im/index.php/Lossless_UDP for more information.
404 */ 405 */
405 406
406static int send_handshake(Lossless_UDP * ludp, IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2) 407static int send_handshake(Lossless_UDP *ludp, IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2)
407{ 408{
408 uint8_t packet[1 + 4 + 4]; 409 uint8_t packet[1 + 4 + 4];
409 uint32_t temp; 410 uint32_t temp;
@@ -417,7 +418,7 @@ static int send_handshake(Lossless_UDP * ludp, IP_Port ip_port, uint32_t handsha
417 return sendpacket(ludp->net->sock, ip_port, packet, sizeof(packet)); 418 return sendpacket(ludp->net->sock, ip_port, packet, sizeof(packet));
418} 419}
419 420
420static int send_SYNC(Lossless_UDP * ludp, uint32_t connection_id) 421static int send_SYNC(Lossless_UDP *ludp, uint32_t connection_id)
421{ 422{
422 uint8_t packet[(BUFFER_PACKET_NUM * 4 + 4 + 4 + 2)]; 423 uint8_t packet[(BUFFER_PACKET_NUM * 4 + 4 + 4 + 2)];
423 uint16_t index = 0; 424 uint16_t index = 0;
@@ -444,7 +445,7 @@ static int send_SYNC(Lossless_UDP * ludp, uint32_t connection_id)
444 445
445} 446}
446 447
447static int send_data_packet(Lossless_UDP * ludp, uint32_t connection_id, uint32_t packet_num) 448static int send_data_packet(Lossless_UDP *ludp, uint32_t connection_id, uint32_t packet_num)
448{ 449{
449 uint32_t index = packet_num % MAX_QUEUE_NUM; 450 uint32_t index = packet_num % MAX_QUEUE_NUM;
450 uint32_t temp; 451 uint32_t temp;
@@ -459,7 +460,7 @@ static int send_data_packet(Lossless_UDP * ludp, uint32_t connection_id, uint32_
459} 460}
460 461
461/* sends 1 data packet */ 462/* sends 1 data packet */
462static int send_DATA(Lossless_UDP * ludp, uint32_t connection_id) 463static int send_DATA(Lossless_UDP *ludp, uint32_t connection_id)
463{ 464{
464 int ret; 465 int ret;
465 uint32_t buffer[BUFFER_PACKET_NUM]; 466 uint32_t buffer[BUFFER_PACKET_NUM];
@@ -491,9 +492,10 @@ static int send_DATA(Lossless_UDP * ludp, uint32_t connection_id)
491 492
492 493
493/* Return 0 if handled correctly, 1 if packet is bad. */ 494/* Return 0 if handled correctly, 1 if packet is bad. */
494static int handle_handshake(void * object, IP_Port source, uint8_t *packet, uint32_t length) 495static int handle_handshake(void *object, IP_Port source, uint8_t *packet, uint32_t length)
495{ 496{
496 Lossless_UDP * ludp = object; 497 Lossless_UDP *ludp = object;
498
497 if (length != (1 + 4 + 4)) 499 if (length != (1 + 4 + 4))
498 return 1; 500 return 1;
499 501
@@ -542,7 +544,7 @@ static int SYNC_valid(uint32_t length)
542} 544}
543 545
544/* case 1 in handle_SYNC: */ 546/* case 1 in handle_SYNC: */
545static int handle_SYNC1(Lossless_UDP * ludp, IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum) 547static int handle_SYNC1(Lossless_UDP *ludp, IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum)
546{ 548{
547 if (handshake_id(ludp, source) == recv_packetnum) { 549 if (handshake_id(ludp, source) == recv_packetnum) {
548 int x = new_inconnection(ludp, source); 550 int x = new_inconnection(ludp, source);
@@ -564,7 +566,8 @@ static int handle_SYNC1(Lossless_UDP * ludp, IP_Port source, uint32_t recv_packe
564} 566}
565 567
566/* case 2 in handle_SYNC: */ 568/* case 2 in handle_SYNC: */
567static int handle_SYNC2(Lossless_UDP * ludp, int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum) 569static int handle_SYNC2(Lossless_UDP *ludp, int connection_id, uint8_t counter, uint32_t recv_packetnum,
570 uint32_t sent_packetnum)
568{ 571{
569 if (recv_packetnum == ludp->connections[connection_id].orecv_packetnum) { 572 if (recv_packetnum == ludp->connections[connection_id].orecv_packetnum) {
570 /* && sent_packetnum == ludp->connections[connection_id].osent_packetnum) */ 573 /* && sent_packetnum == ludp->connections[connection_id].osent_packetnum) */
@@ -578,7 +581,8 @@ static int handle_SYNC2(Lossless_UDP * ludp, int connection_id, uint8_t counter,
578 return 1; 581 return 1;
579} 582}
580/* case 3 in handle_SYNC: */ 583/* case 3 in handle_SYNC: */
581static int handle_SYNC3(Lossless_UDP * ludp, int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum, 584static int handle_SYNC3(Lossless_UDP *ludp, int connection_id, uint8_t counter, uint32_t recv_packetnum,
585 uint32_t sent_packetnum,
582 uint32_t *req_packets, 586 uint32_t *req_packets,
583 uint16_t number) 587 uint16_t number)
584{ 588{
@@ -614,9 +618,10 @@ static int handle_SYNC3(Lossless_UDP * ludp, int connection_id, uint8_t counter,
614 return 1; 618 return 1;
615} 619}
616 620
617static int handle_SYNC(void * object, IP_Port source, uint8_t *packet, uint32_t length) 621static int handle_SYNC(void *object, IP_Port source, uint8_t *packet, uint32_t length)
618{ 622{
619 Lossless_UDP * ludp = object; 623 Lossless_UDP *ludp = object;
624
620 if (!SYNC_valid(length)) 625 if (!SYNC_valid(length))
621 return 1; 626 return 1;
622 627
@@ -654,7 +659,7 @@ static int handle_SYNC(void * object, IP_Port source, uint8_t *packet, uint32_t
654 * Add a packet to the received buffer and set the recv_packetnum of the 659 * Add a packet to the received buffer and set the recv_packetnum of the
655 * connection to its proper value. Return 1 if data was too big, 0 if not. 660 * connection to its proper value. Return 1 if data was too big, 0 if not.
656 */ 661 */
657static int add_recv(Lossless_UDP * ludp, int connection_id, uint32_t data_num, uint8_t *data, uint16_t size) 662static int add_recv(Lossless_UDP *ludp, int connection_id, uint32_t data_num, uint8_t *data, uint16_t size)
658{ 663{
659 if (size > MAX_DATA_SIZE) 664 if (size > MAX_DATA_SIZE)
660 return 1; 665 return 1;
@@ -688,9 +693,9 @@ static int add_recv(Lossless_UDP * ludp, int connection_id, uint32_t data_num, u
688 return 0; 693 return 0;
689} 694}
690 695
691static int handle_data(void * object, IP_Port source, uint8_t *packet, uint32_t length) 696static int handle_data(void *object, IP_Port source, uint8_t *packet, uint32_t length)
692{ 697{
693 Lossless_UDP * ludp = object; 698 Lossless_UDP *ludp = object;
694 int connection = getconnection_id(ludp, source); 699 int connection = getconnection_id(ludp, source);
695 700
696 if (connection == -1) 701 if (connection == -1)
@@ -717,13 +722,16 @@ static int handle_data(void * object, IP_Port source, uint8_t *packet, uint32_t
717 * END of packet handling functions 722 * END of packet handling functions
718 */ 723 */
719 724
720Lossless_UDP * new_lossless_udp(Networking_Core * net) 725Lossless_UDP *new_lossless_udp(Networking_Core *net)
721{ 726{
722 if(net == NULL) 727 if (net == NULL)
723 return NULL; 728 return NULL;
724 Lossless_UDP * temp = calloc(1, sizeof(Lossless_UDP)); 729
730 Lossless_UDP *temp = calloc(1, sizeof(Lossless_UDP));
731
725 if (temp == NULL) 732 if (temp == NULL)
726 return NULL; 733 return NULL;
734
727 temp->net = net; 735 temp->net = net;
728 networking_registerhandler(net, 16, &handle_handshake, temp); 736 networking_registerhandler(net, 16, &handle_handshake, temp);
729 networking_registerhandler(net, 17, &handle_SYNC, temp); 737 networking_registerhandler(net, 17, &handle_SYNC, temp);
@@ -735,7 +743,7 @@ Lossless_UDP * new_lossless_udp(Networking_Core * net)
735 * Send handshake requests 743 * Send handshake requests
736 * handshake packets are sent at the same rate as SYNC packets 744 * handshake packets are sent at the same rate as SYNC packets
737 */ 745 */
738static void do_new(Lossless_UDP * ludp) 746static void do_new(Lossless_UDP *ludp)
739{ 747{
740 uint32_t i; 748 uint32_t i;
741 uint64_t temp_time = current_time(); 749 uint64_t temp_time = current_time();
@@ -760,7 +768,7 @@ static void do_new(Lossless_UDP * ludp)
760 } 768 }
761} 769}
762 770
763static void do_SYNC(Lossless_UDP * ludp) 771static void do_SYNC(Lossless_UDP *ludp)
764{ 772{
765 uint32_t i; 773 uint32_t i;
766 uint64_t temp_time = current_time(); 774 uint64_t temp_time = current_time();
@@ -774,7 +782,7 @@ static void do_SYNC(Lossless_UDP * ludp)
774 } 782 }
775} 783}
776 784
777static void do_data(Lossless_UDP * ludp) 785static void do_data(Lossless_UDP *ludp)
778{ 786{
779 uint32_t i; 787 uint32_t i;
780 uint64_t j; 788 uint64_t j;
@@ -797,7 +805,7 @@ static void do_data(Lossless_UDP * ludp)
797 * 805 *
798 * TODO: flow control. 806 * TODO: flow control.
799 */ 807 */
800static void adjust_rates(Lossless_UDP * ludp) 808static void adjust_rates(Lossless_UDP *ludp)
801{ 809{
802 uint32_t i; 810 uint32_t i;
803 uint64_t temp_time = current_time(); 811 uint64_t temp_time = current_time();
@@ -819,7 +827,7 @@ static void adjust_rates(Lossless_UDP * ludp)
819} 827}
820 828
821/* Call this function a couple times per second It's the main loop. */ 829/* Call this function a couple times per second It's the main loop. */
822void do_lossless_udp(Lossless_UDP * ludp) 830void do_lossless_udp(Lossless_UDP *ludp)
823{ 831{
824 do_new(ludp); 832 do_new(ludp);
825 do_SYNC(ludp); 833 do_SYNC(ludp);
@@ -827,7 +835,7 @@ void do_lossless_udp(Lossless_UDP * ludp)
827 adjust_rates(ludp); 835 adjust_rates(ludp);
828} 836}
829 837
830void kill_lossless_udp(Lossless_UDP * ludp) 838void kill_lossless_udp(Lossless_UDP *ludp)
831{ 839{
832 free(ludp->connections); 840 free(ludp->connections);
833 free(ludp); 841 free(ludp);