1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
|
/* Lossless_UDP.c
*
* An implementation of the Lossless_UDP protocol as seen in http://wiki.tox.im/index.php/Lossless_UDP
*
* Copyright (C) 2013 Tox project All Rights Reserved.
*
* This file is part of Tox.
*
* Tox is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tox is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
* TODO: clean this file a bit.
* There are a couple of useless variables to get rid of.
*/
#include "Lossless_UDP.h"
/* Functions */
/*
* Get connection id from IP_Port
* Return -1 if there are no connections like we are looking for
* Return id if it found it
*/
int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port)
{
/*vx-t uint32_t i;
for (i = 0; i < ludp->connections_length; ++i) {
if (ludp->connections[i].ip_port.ip.i == ip_port.ip.i &&
ludp->connections[i].ip_port.port == ip_port.port &&
ludp->connections[i].status > 0)
return i;
}*/
tox_array_for_each(&ludp->connections, Connection, tmp) {
if (tmp.ip_port.ip.i == ip_port.ip.i &&
tmp.ip_port.port == ip_port.port &&
tmp.status > 0)
{
return tmp_i;
}
}
return -1;
}
/*
* Generate a handshake_id which depends on the ip_port.
* This function will always give one unique handshake_id per ip_port.
*
* TODO: make this better
*/
static uint32_t handshake_id(Lossless_UDP *ludp, IP_Port source)
{
uint32_t id = 0, i;
for (i = 0; i < 6; ++i) {
if (ludp->randtable[i][((uint8_t *)&source)[i]] == 0)
ludp->randtable[i][((uint8_t *)&source)[i]] = random_int();
id ^= ludp->randtable[i][((uint8_t *)&source)[i]];
}
if (id == 0) /* id can't be zero */
id = 1;
return id;
}
/*
* Change the hanshake id associated with that ip_port
*
* TODO: make this better
*/
static void change_handshake(Lossless_UDP *ludp, IP_Port source)
{
uint8_t rand = random_int() % 4;
ludp->randtable[rand][((uint8_t *)&source)[rand]] = random_int();
}
/*
* Initialize a new connection to ip_port
* Returns an integer corresponding to the connection idt
* Return -1 if it could not initialize the connectiont
* If there already was an existing connection to that ip_port return its number.
*/
int new_connection(Lossless_UDP *ludp, IP_Port ip_port)
{
int connect = getconnection_id(ludp, ip_port);
if (connect != -1)
return connect;
/* TODO: this allocates space regardless of whether it's needed or not
* Rewrite so it will first scan for empty, and if not found make a new one.
*/
tox_array_push_ptr(&ludp->connections, 0);
memset(&tox_array_get(&ludp->connections, ludp->connections.len, Connection), 0, sizeof(Connection));
/*vx-t if (ludp->connections_number == ludp->connections_length) {
Connection *temp;
temp = realloc(ludp->connections, sizeof(Connection) * (ludp->connections_length + 1));
if (temp == NULL)
return -1;
memset(&temp[ludp->connections_length], 0, sizeof(Connection));
++ludp->connections_length;
ludp->connections = temp;
}*/
tox_array_for_each(&ludp->connections, Connection, tmp) {
if (tmp.status == 0) {
memset(&tox_array_get(&ludp->connections, tmp_i, Connection), 0, sizeof(Connection));
uint32_t handshake_id1 = handshake_id(ludp, ip_port);
tox_array_get(&ludp->connections, tmp_i, Connection) = (Connection) {
.ip_port = ip_port,
.status = 1,
.inbound = 0,
.handshake_id1 = handshake_id1,
.sent_packetnum = handshake_id1,
.sendbuff_packetnum = handshake_id1,
.successful_sent = handshake_id1,
.SYNC_rate = SYNC_RATE,
.data_rate = DATA_SYNC_RATE,
.last_recvSYNC = current_time(),
.last_sent = current_time(),
.killat = ~0,
.send_counter = 0,
/* add randomness to timeout to prevent connections getting stuck in a loop. */
.timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT
};
return tmp_i;
}
}
/*vx-t uint32_t i;
for (i = 0; i < ludp->connections_length; ++i) {
if (ludp->connections[i].status == 0) {
memset(&ludp->connections[i], 0, sizeof(Connection));
uint32_t handshake_id1 = handshake_id(ludp, ip_port);
ludp->connections[i] = (Connection) {
.ip_port = ip_port,
.status = 1,
.inbound = 0,
.handshake_id1 = handshake_id1,
.sent_packetnum = handshake_id1,
.sendbuff_packetnum = handshake_id1,
.successful_sent = handshake_id1,
.SYNC_rate = SYNC_RATE,
.data_rate = DATA_SYNC_RATE,
.last_recvSYNC = current_time(),
.last_sent = current_time(),
.killat = ~0,
.send_counter = 0,
/* add randomness to timeout to prevent connections getting stuck in a loop. /
.timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT
};
++ludp->connections_number;
return i;
}
}*/
return -1;
}
/*
* Initialize a new inbound connection from ip_port
* Returns an integer corresponding to the connection id.
* Return -1 if it could not initialize the connection.
*/
static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port)
{
if (getconnection_id(ludp, ip_port) != -1)
return -1;
/* TODO: See comment in int new_connection(Lossless_UDP *ludp, IP_Port ip_port) */
tox_array_push_ptr(&ludp->connections, 0);
memset(&tox_array_get(&ludp->connections, ludp->connections.len, Connection), 0, sizeof(Connection));
/*vx-t if (ludp->connections_number == ludp->connections_length) {
Connection *temp;
temp = realloc(ludp->connections, sizeof(Connection) * (ludp->connections_length + 1));
if (temp == NULL)
return -1;
memset(&temp[ludp->connections_length], 0, sizeof(Connection));
++ludp->connections_length;
ludp->connections = temp;
}*/
/* vx-k: IMPORTANT!!!!!!!!! DO THIS
uint32_t i;
for (i = 0; i < ludp->connections_length; ++i) {
if (ludp->connections[i].status == 0) {
memset(&ludp->connections[i], 0, sizeof(Connection));
uint64_t timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT;
ludp->connections[i] = (Connection) {
.ip_port = ip_port,
.status = 2,
.inbound = 2,
.SYNC_rate = SYNC_RATE,
.data_rate = DATA_SYNC_RATE,
.last_recvSYNC = current_time(),
.last_sent = current_time(),
.send_counter = 127,
/* add randomness to timeout to prevent connections getting stuck in a loop. /
.timeout = timeout,
/* if this connection isn't handled within the timeout kill it. /
.killat = current_time() + 1000000UL * timeout
};
++ludp->connections_number;
return i;
}
}
*/
return -1;
}
/*
* Returns an integer corresponding to the next connection in our incoming connection list.
* Return -1 if there are no new incoming connections in the list.
*/
int incoming_connection(Lossless_UDP *ludp)
{
tox_array_for_each(&ludp->connections, Connection, tmp) {
if (tmp.inbound == 2) {
tmp.inbound = 1;
return tmp_i;
}
}
/*vx-k uint32_t i;
for (i = 0; i < ludp->connections_length; ++i) {
if (ludp->connections[i].inbound == 2) {
ludp->connections[i].inbound = 1;
return i;
}
}*/
return -1;
}
/* Try to free some memory from the connections array. */
static void free_connections(Lossless_UDP *ludp)
{
/*vx-k: fix this to work with new tox_array
uint32_t i;
for (i = ludp->connections_length; i != 0; --i)
if (ludp->connections[i - 1].status != 0)
break;
if (ludp->connections_length == i)
return;
if (i == 0) {
free(ludp->connections);
ludp->connections = NULL;
ludp->connections_length = i;
return;
}
Connection *temp;
temp = realloc(ludp->connections, sizeof(Connection) * i);
if (temp == NULL && i != 0)
return;
ludp->connections = temp;
ludp->connections_length = i;
*/
}
/*
* Return -1 if it could not kill the connection.
* Return 0 if killed successfully
*/
int kill_connection(Lossless_UDP *ludp, int connection_id)
{
if (connection_id >= 0 && connection_id < ludp->connections.len) {
if (tox_array_get(&ludp->connections, connection_id, Connection).status > 0) {
tox_array_get(&ludp->connections, connection_id, Connection).status = 0;
change_handshake(ludp, tox_array_get(&ludp->connections, connection_id, Connection).ip_port);
tox_array_pop(&ludp->connections, 0);
free_connections(ludp);
return 0;
}
}
return -1;
}
/*
* Kill connection in seconds.
* Return -1 if it can not kill the connection.
* Return 0 if it will kill it.
*/
int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds)
{
if (connection_id >= 0 && connection_id < ludp->connections.len) {
if (tox_array_get(&ludp->connections, connection_id, Connection).status > 0) {
tox_array_get(&ludp->connections, connection_id, Connection).killat = current_time() + 1000000UL * seconds;
return 0;
}
}
return -1;
}
/*
* Check if connection is connected:
* Return 0 no.
* Return 1 if attempting handshake.
* Return 2 if handshake is done.
* Return 3 if fully connected.
* Return 4 if timed out and waiting to be killed.
*/
int is_connected(Lossless_UDP *ludp, int connection_id)
{
if (connection_id >= 0 && connection_id < ludp->connections.len)
return tox_array_get(&ludp->connections, connection_id, Connection).status;
return 0;
}
/* returns the ip_port of the corresponding connection. */
IP_Port connection_ip(Lossless_UDP *ludp, int connection_id)
{
if (connection_id >= 0 && connection_id < ludp->connections.len)
return tox_array_get(&ludp->connections, connection_id, Connection).ip_port;
IP_Port zero = {{{0}}, 0};
return zero;
}
/* returns the number of packets in the queue waiting to be successfully sent. */
uint32_t sendqueue(Lossless_UDP *ludp, int connection_id)
{
if (connection_id < 0 || connection_id >= ludp->connections.len)
return 0;
return tox_array_get(&ludp->connections, connection_id, Connection).sendbuff_packetnum -
tox_array_get(&ludp->connections, connection_id, Connection).successful_sent;
}
/* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */
uint32_t recvqueue(Lossless_UDP *ludp, int connection_id)
{
if (connection_id < 0 || connection_id >= ludp->connections.len)
return 0;
return tox_array_get(&ludp->connections, connection_id, Connection).recv_packetnum - tox_array_get(&ludp->connections, connection_id, Connection).successful_read;
}
/* returns the id of the next packet in the queue
return -1 if no packet in queue */
char id_packet(Lossless_UDP *ludp, int connection_id)
{
if (connection_id < 0 || connection_id >= ludp->connections.len)
return -1;
if (recvqueue(ludp, connection_id) != 0 && tox_array_get(&ludp->connections, connection_id, Connection).status != 0)
return tox_array_get(&ludp->connections, connection_id, Connection).recvbuffer[tox_array_get(&ludp->connections, connection_id, Connection).successful_read %
MAX_QUEUE_NUM].data[0];
return -1;
}
/* return 0 if there is no received data in the buffer.
return length of received packet if successful */
int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data)
{
if (recvqueue(ludp, connection_id) != 0) {
uint16_t index = tox_array_get(&ludp->connections, connection_id, Connection).successful_read % MAX_QUEUE_NUM;
uint16_t size = tox_array_get(&ludp->connections, connection_id, Connection).recvbuffer[index].size;
memcpy(data, tox_array_get(&ludp->connections, connection_id, Connection).recvbuffer[index].data, size);
++tox_array_get(&ludp->connections, connection_id, Connection).successful_read;
tox_array_get(&ludp->connections, connection_id, Connection).recvbuffer[index].size = 0;
return size;
}
return 0;
}
/*
* Return 0 if data could not be put in packet queue
* Return 1 if data was put into the queue
*/
int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t length)
{
if (length > MAX_DATA_SIZE || length == 0)
return 0;
if (sendqueue(ludp, connection_id) < BUFFER_PACKET_NUM) {
uint32_t index = tox_array_get(&ludp->connections, connection_id, Connection).sendbuff_packetnum % MAX_QUEUE_NUM;
memcpy(tox_array_get(&ludp->connections, connection_id, Connection).sendbuffer[index].data, data, length);
tox_array_get(&ludp->connections, connection_id, Connection).sendbuffer[index].size = length;
tox_array_get(&ludp->connections, connection_id, Connection).sendbuff_packetnum++;
return 1;
}
return 0;
}
/* put the packet numbers the we are missing in requested and return the number */
uint32_t missing_packets(Lossless_UDP *ludp, int connection_id, uint32_t *requested)
{
uint32_t number = 0;
uint32_t i;
uint32_t temp;
/* don't request packets if the buffer is full. */
if (recvqueue(ludp, connection_id) >= (BUFFER_PACKET_NUM - 1))
return 0;
for (i = tox_array_get(&ludp->connections, connection_id, Connection).recv_packetnum;
i != tox_array_get(&ludp->connections, connection_id, Connection).osent_packetnum;
i++)
{
if (tox_array_get(&ludp->connections, connection_id, Connection).recvbuffer[i % MAX_QUEUE_NUM].size == 0) {
temp = htonl(i);
memcpy(requested + number, &temp, 4);
++number;
}
}
if (number == 0)
tox_array_get(&ludp->connections, connection_id, Connection).recv_packetnum = tox_array_get(&ludp->connections, connection_id, Connection).osent_packetnum;
return number;
}
/*
* BEGIN Packet sending functions
* One per packet type.
* see http://wiki.tox.im/index.php/Lossless_UDP for more information.
*/
static int send_handshake(Lossless_UDP *ludp, IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2)
{
uint8_t packet[1 + 4 + 4];
uint32_t temp;
packet[0] = NET_PACKET_HANDSHAKE;
temp = htonl(handshake_id1);
memcpy(packet + 1, &temp, 4);
temp = htonl(handshake_id2);
memcpy(packet + 5, &temp, 4);
return sendpacket(ludp->net->sock, ip_port, packet, sizeof(packet));
}
static int send_SYNC(Lossless_UDP *ludp, uint32_t connection_id)
{
uint8_t packet[(BUFFER_PACKET_NUM * 4 + 4 + 4 + 2)];
uint16_t index = 0;
IP_Port ip_port = tox_array_get(&ludp->connections, connection_id, Connection).ip_port;
uint8_t counter = tox_array_get(&ludp->connections, connection_id, Connection).send_counter;
uint32_t recv_packetnum = htonl(tox_array_get(&ludp->connections, connection_id, Connection).recv_packetnum);
uint32_t sent_packetnum = htonl(tox_array_get(&ludp->connections, connection_id, Connection).sent_packetnum);
uint32_t requested[BUFFER_PACKET_NUM];
uint32_t number = missing_packets(ludp, connection_id, requested);
packet[0] = NET_PACKET_SYNC;
index += 1;
memcpy(packet + index, &counter, 1);
index += 1;
memcpy(packet + index, &recv_packetnum, 4);
index += 4;
memcpy(packet + index, &sent_packetnum, 4);
index += 4;
memcpy(packet + index, requested, 4 * number);
return sendpacket(ludp->net->sock, ip_port, packet, (number * 4 + 4 + 4 + 2));
}
static int send_data_packet(Lossless_UDP *ludp, uint32_t connection_id, uint32_t packet_num)
{
uint32_t index = packet_num % MAX_QUEUE_NUM;
uint32_t temp;
uint8_t packet[1 + 4 + MAX_DATA_SIZE];
packet[0] = NET_PACKET_DATA;
temp = htonl(packet_num);
memcpy(packet + 1, &temp, 4);
memcpy(packet + 5, tox_array_get(&ludp->connections, connection_id, Connection).sendbuffer[index].data,
tox_array_get(&ludp->connections, connection_id, Connection).sendbuffer[index].size);
return sendpacket(ludp->net->sock, tox_array_get(&ludp->connections, connection_id, Connection).ip_port, packet,
1 + 4 + tox_array_get(&ludp->connections, connection_id, Connection).sendbuffer[index].size);
}
/* sends 1 data packet */
static int send_DATA(Lossless_UDP *ludp, uint32_t connection_id)
{
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
int ret;
uint32_t buffer[BUFFER_PACKET_NUM];
if (connection->num_req_paquets > 0) {
ret = send_data_packet(ludp, connection_id, connection->req_packets[0]);
connection->num_req_paquets--;
memcpy(buffer, connection->req_packets + 1, connection->num_req_paquets * 4);
memcpy(connection->req_packets, buffer, connection->num_req_paquets * 4);
return ret;
}
if (connection->sendbuff_packetnum != connection->sent_packetnum) {
ret = send_data_packet(ludp, connection_id, connection->sent_packetnum);
connection->sent_packetnum++;
return ret;
}
return 0;
}
/*
* END of packet sending functions
*
*
* BEGIN Packet handling functions
* One to handle each type of packets we receive
*/
/* Return 0 if handled correctly, 1 if packet is bad. */
static int handle_handshake(void *object, IP_Port source, uint8_t *packet, uint32_t length)
{
Lossless_UDP *ludp = object;
if (length != (1 + 4 + 4))
return 1;
uint32_t temp;
uint32_t handshake_id1, handshake_id2;
int connection_id = getconnection_id(ludp, source);
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
memcpy(&temp, packet + 1, 4);
handshake_id1 = ntohl(temp);
memcpy(&temp, packet + 5, 4);
handshake_id2 = ntohl(temp);
if (handshake_id2 == 0 && is_connected(ludp, connection_id) < 3) {
send_handshake(ludp, source, handshake_id(ludp, source), handshake_id1);
return 0;
}
if (is_connected(ludp, connection_id) != 1)
return 1;
/* if handshake_id2 is what we sent previously as handshake_id1 */
if (handshake_id2 == connection->handshake_id1) {
connection->status = 2;
/* NOTE: is this necessary?
connection->handshake_id2 = handshake_id1; */
connection->orecv_packetnum = handshake_id2;
connection->osent_packetnum = handshake_id1;
connection->recv_packetnum = handshake_id1;
connection->successful_read = handshake_id1;
}
return 0;
}
/* returns 1 if sync packet is valid 0 if not. */
static int SYNC_valid(uint32_t length)
{
if (length < 4 + 4 + 2)
return 0;
if (length > (BUFFER_PACKET_NUM * 4 + 4 + 4 + 2) ||
((length - 4 - 4 - 2) % 4) != 0)
return 0;
return 1;
}
/* case 1 in handle_SYNC: */
static int handle_SYNC1(Lossless_UDP *ludp, IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum)
{
if (handshake_id(ludp, source) == recv_packetnum) {
int connection_id = new_inconnection(ludp, source);
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
if (connection_id != -1) {
connection->orecv_packetnum = recv_packetnum;
connection->sent_packetnum = recv_packetnum;
connection->sendbuff_packetnum = recv_packetnum;
connection->successful_sent = recv_packetnum;
connection->osent_packetnum = sent_packetnum;
connection->recv_packetnum = sent_packetnum;
connection->successful_read = sent_packetnum;
return connection_id;
}
}
return -1;
}
/* case 2 in handle_SYNC: */
static int handle_SYNC2(Lossless_UDP *ludp, int connection_id, uint8_t counter, uint32_t recv_packetnum,
uint32_t sent_packetnum)
{
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
if (recv_packetnum == connection->orecv_packetnum) {
/* && sent_packetnum == connection->osent_packetnum) */
connection->status = 3;
connection->recv_counter = counter;
++connection->send_counter;
send_SYNC(ludp, connection_id);
return 0;
}
return 1;
}
/* case 3 in handle_SYNC: */
static int handle_SYNC3(Lossless_UDP *ludp, int connection_id, uint8_t counter, uint32_t recv_packetnum,
uint32_t sent_packetnum,
uint32_t *req_packets,
uint16_t number)
{
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
uint8_t comp_counter = (counter - connection->recv_counter);
uint32_t i, temp;
/* uint32_t comp_1 = (recv_packetnum - connection->successful_sent);
uint32_t comp_2 = (sent_packetnum - connection->successful_read); */
uint32_t comp_1 = (recv_packetnum - connection->orecv_packetnum);
uint32_t comp_2 = (sent_packetnum - connection->osent_packetnum);
/* packet valid */
if (comp_1 <= BUFFER_PACKET_NUM &&
comp_2 <= BUFFER_PACKET_NUM &&
comp_counter < 10 && comp_counter != 0) {
connection->orecv_packetnum = recv_packetnum;
connection->osent_packetnum = sent_packetnum;
connection->successful_sent = recv_packetnum;
connection->last_recvSYNC = current_time();
connection->recv_counter = counter;
++connection->send_counter;
for (i = 0; i < number; ++i) {
temp = ntohl(req_packets[i]);
memcpy(connection->req_packets + i, &temp, 4 * number);
}
connection->num_req_paquets = number;
return 0;
}
return 1;
}
static int handle_SYNC(void *object, IP_Port source, uint8_t *packet, uint32_t length)
{
Lossless_UDP *ludp = object;
if (!SYNC_valid(length))
return 1;
uint8_t counter;
uint32_t temp;
uint32_t recv_packetnum, sent_packetnum;
uint32_t req_packets[BUFFER_PACKET_NUM];
uint16_t number = (length - 4 - 4 - 2) / 4;
memcpy(&counter, packet + 1, 1);
memcpy(&temp, packet + 2, 4);
recv_packetnum = ntohl(temp);
memcpy(&temp, packet + 6, 4);
sent_packetnum = ntohl(temp);
if (number != 0)
memcpy(req_packets, packet + 10, 4 * number);
int connection_id = getconnection_id(ludp, source);
if (connection_id == -1)
return handle_SYNC1(ludp, source, recv_packetnum, sent_packetnum);
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
if (connection->status == 2)
return handle_SYNC2(ludp, connection_id, counter,
recv_packetnum, sent_packetnum);
if (connection->status == 3)
return handle_SYNC3(ludp, connection_id, counter, recv_packetnum,
sent_packetnum, req_packets, number);
return 0;
}
/*
* Add a packet to the received buffer and set the recv_packetnum of the
* connection to its proper value. Return 1 if data was too big, 0 if not.
*/
static int add_recv(Lossless_UDP *ludp, int connection_id, uint32_t data_num, uint8_t *data, uint16_t size)
{
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
if (size > MAX_DATA_SIZE)
return 1;
uint32_t i;
uint32_t maxnum = connection->successful_read + BUFFER_PACKET_NUM;
uint32_t sent_packet = data_num - connection->osent_packetnum;
for (i = connection->recv_packetnum; i != maxnum; ++i) {
if (i == data_num) {
memcpy(connection->recvbuffer[i % MAX_QUEUE_NUM].data, data, size);
connection->recvbuffer[i % MAX_QUEUE_NUM].size = size;
connection->last_recvdata = current_time();
if (sent_packet < BUFFER_PACKET_NUM) {
connection->osent_packetnum = data_num;
}
break;
}
}
for (i = connection->recv_packetnum; i != maxnum; ++i) {
if (connection->recvbuffer[i % MAX_QUEUE_NUM].size != 0)
connection->recv_packetnum = i;
else
break;
}
return 0;
}
static int handle_data(void *object, IP_Port source, uint8_t *packet, uint32_t length)
{
Lossless_UDP *ludp = object;
int connection_id = getconnection_id(ludp, source);
if (connection_id == -1)
return 1;
Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
/* Drop the data packet if connection is not connected. */
if (connection->status != 3)
return 1;
if (length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1)
return 1;
uint32_t temp;
uint32_t number;
uint16_t size = length - 1 - 4;
memcpy(&temp, packet + 1, 4);
number = ntohl(temp);
return add_recv(ludp, connection_id, number, packet + 5, size);
}
/*
* END of packet handling functions
*/
Lossless_UDP *new_lossless_udp(Networking_Core *net)
{
if (net == NULL)
return NULL;
Lossless_UDP *temp = calloc(1, sizeof(Lossless_UDP));
if (temp == NULL)
return NULL;
temp->net = net;
networking_registerhandler(net, NET_PACKET_HANDSHAKE, &handle_handshake, temp);
networking_registerhandler(net, NET_PACKET_SYNC, &handle_SYNC, temp);
networking_registerhandler(net, NET_PACKET_DATA, &handle_data, temp);
return temp;
}
/*
* Send handshake requests
* handshake packets are sent at the same rate as SYNC packets
*/
static void do_new(Lossless_UDP *ludp)
{
uint32_t i;
uint64_t temp_time = current_time();
tox_array_for_each(&ludp->connections, Connection, tmp) {
if (tmp.status == 1)
if ((tmp.last_sent + (1000000UL / tmp.SYNC_rate)) <= temp_time) {
send_handshake(ludp, tmp.ip_port, tmp.handshake_id1, 0);
tmp.last_sent = temp_time;
}
/* kill all timed out connections */
if (tmp.status > 0 && (tmp.last_recvSYNC + tmp.timeout * 1000000UL) < temp_time && tmp.status != 4) {
tmp.status = 4;
/* kill_connection(i); */
}
if (tmp.status > 0 && tmp.killat < temp_time)
kill_connection(ludp, i);
}
}
static void do_SYNC(Lossless_UDP *ludp)
{
uint32_t i;
uint64_t temp_time = current_time();
tox_array_for_each(&ludp->connections, Connection, tmp) {
if (tmp.status == 2 || tmp.status == 3)
if ((tmp.last_SYNC + (1000000UL / tmp.SYNC_rate)) <= temp_time) {
send_SYNC(ludp, i);
tmp.last_SYNC = temp_time;
}
}
}
static void do_data(Lossless_UDP *ludp)
{
uint32_t i;
uint64_t j;
uint64_t temp_time = current_time();
tox_array_for_each(&ludp->connections, Connection, tmp) {
if (tmp.status == 3 && sendqueue(ludp, i) != 0 &&
(tmp.last_sent + (1000000UL / tmp.data_rate)) <= temp_time)
{
for (j = tmp.last_sent; j < temp_time; j += (1000000UL / tmp.data_rate))
send_DATA(ludp, i);
tmp.last_sent = temp_time;
}
}
}
#define MAX_SYNC_RATE 10
/*
* Automatically adjusts send rates of packets for optimal transmission.
*
* TODO: flow control.
*/
static void adjust_rates(Lossless_UDP *ludp)
{
uint32_t i;
uint64_t temp_time = current_time();
tox_array_for_each(&ludp->connections, Connection, tmp) {
if (tmp.status == 1 || tmp.status == 2)
tmp.SYNC_rate = MAX_SYNC_RATE;
if (tmp.status == 3) {
if (sendqueue(ludp, i) != 0) {
tmp.data_rate = (BUFFER_PACKET_NUM - tmp.num_req_paquets) * MAX_SYNC_RATE;
tmp.SYNC_rate = MAX_SYNC_RATE;
} else if (tmp.last_recvdata + 1000000UL > temp_time)
tmp.SYNC_rate = MAX_SYNC_RATE;
else
tmp.SYNC_rate = SYNC_RATE;
}
}
}
/* Call this function a couple times per second It's the main loop. */
void do_lossless_udp(Lossless_UDP *ludp)
{
do_new(ludp);
do_SYNC(ludp);
do_data(ludp);
adjust_rates(ludp);
}
void kill_lossless_udp(Lossless_UDP *ludp)
{
tox_array_delete(&ludp->connections);
free(ludp);
}
|