-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrudp.c
3149 lines (2805 loc) · 74 KB
/
rudp.c
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
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdlib.h>
#include <stdio.h>
#include "rudp_imp.h"
#include "rudp.h"
#include "crc32.h"
#include <assert.h>
#ifndef min
#define min(x,y) ((x)<(y)?(x):(y))
#endif
#ifndef max
#define max(x,y) ((x)>(y)?(x):(y))
#endif
#ifndef offsetof
#define offsetof(s, m) ((int)(&((s*)0)->m))
#endif
#if defined(WIN32) || defined(ARM_UCOS_LWIP)
#define SETEVENT(event) PA_SetEvent(event)
#elif defined(__LINUX__) || defined(__ANDROID__)
#define SETEVENT(event) pthread_cond_signal(&event)
#endif
#define SAFE_FREE(p) if(p) { free(p); p = NULL; }
//---------------------------------------------------------
PA_MUTEX mutex_sock_list;
LIST_HEAD(sock_list);
PA_HTHREAD hthd;
static volatile int run = 1;
static PA_MUTEX mutex_pkt_pool;
static int n_free_pkt = 0;
static struct rudp_pkt *free_pkt = NULL;
//static int sockw_r = -1, sockw_s;
struct output_notify {
struct rudp_socket *s;
int chno;
};
unsigned int rudp_now = 0;
//==========================================================
#define RO_NORMAL 0
#define RO_FORCE 1
#define RO_REXMT 2 //rexmt as RCT_REXMT timer
#define RO_ONLYONE 3
#define RO_REXMT_FAST 4 //rexmt as duplicated ack received
#define INITIAL_SEQ_NO 0
#define FASTRETRANS 0
#define FASTRETRANS2 1 //multiple packet lost
#define CONGESTED 2
//value for should_ack
#define ACKT_DELAYED 1
#define ACKT_OPENWND 2
#ifdef _DEBUG_RUDP
static char* IP2STR(unsigned int ip, char ips[16])
{
int len = sprintf(ips, "%d.", ip&0xFF);
len += sprintf(ips+len, "%d.", (ip>>8)&0xFF);
len += sprintf(ips+len, "%d.", (ip>>16)&0xFF);
len += sprintf(ips+len, "%d", (ip>>24)&0xFF);
return ips;
}
static void _printTime()
{
char sf[16];
#if defined(ARM_UCOS_LWIP)
#elif defined(WIN32)
SYSTEMTIME t;
GetLocalTime(&t);
sprintf(sf, "%.06f", t.wMilliseconds/1000.0);
PRINTF("%02d:%02d:%02d.%s ", t.wHour, t.wMinute, t.wSecond, sf+2);
#else
struct timeval tv;
struct tm _tm;
gettimeofday(&tv, NULL);
localtime_r(&tv.tv_sec, &_tm);
sprintf(sf, "%.06f", tv.tv_usec/1000000.0);
PRINTF("%02d:%02d:%02d.%s ", _tm.tm_hour, _tm.tm_min, _tm.tm_sec, sf+2);
#endif
}
#define PHF_FROM 0x10000000
#define PHF_DATA 0x20000000
static void __printHdr(const struct rudp_pcb *pcb, const struct rudp_hdr *phdr, const struct sockaddr_in *pa, int phf, int data_len)
{
char ip[16];
_printTime();
if(phf & PHF_FROM)
{
PRINTF("%s.%d > ", IP2STR(pa->sin_addr.s_addr, ip), (int)ntohs(pa->sin_port));
if(pcb) PRINTF("%s.%d ", IP2STR(pcb->local.sin_addr.s_addr, ip), (int)ntohs(pcb->local.sin_port));
}
else
{
if(pcb) PRINTF("%s.%d > ", IP2STR(pcb->local.sin_addr.s_addr, ip), (int)ntohs(pcb->local.sin_port));
PRINTF("%s.%d ", IP2STR(pa->sin_addr.s_addr, ip), (int)ntohs(pa->sin_port));
}
PRINTF("c:%d ", phdr->flags.chno);
if(data_len)
{
PRINTF("P s:%u(%d) ", ntohl(phdr->seqno), data_len);
}
else printf(". ");
if(phdr->flags.syn) PRINTF("syn ");
if(phdr->flags.ack) PRINTF("ack %u(%d) ", ntohl(phdr->ackno), phdr->flags.n_loss);
if(phdr->flags.rst) PRINTF("rst ");
if(phdr->flags.fin) PRINTF("fin ");
PRINTF("win %d ", (int)WINDOW_NTOH(phdr->flags.window));
if(pcb)
{
PRINTF("[rwnd %d cwnd %d ssth %d", pcb->channel[phdr->flags.chno].sbuf.rwnd, pcb->cwnd, pcb->ssthresh);
if(phdr->flags.ack && (phf&PHF_FROM))
{
PRINTF(" rto %d", pcb->rto);
PRINTF(" rtw %d", pcb->rtw_size);
if(phdr->flags.n_loss) PRINTF(" lost %d", phdr->flags.n_loss);
}
PRINTF(" una %d", pcb->channel[phdr->flags.chno].sbuf.n_unacked);
PRINTF("]");
}
PRINTF("\n");
#if defined(WIN32)
fflush(stdout);
#endif
}
static void _printHdr(const struct rudp_pcb *pcb, const struct rudp_hdr *phdr, const struct sockaddr_in *pa)
{
__printHdr(pcb, phdr, pa, 0, 0);
}
static void _printPkt(const struct rudp_pcb *pcb, const struct rudp_pkt *pkt, int phf, const struct sockaddr_in *pa)
{
if(pkt->len) { phf |= PHF_DATA; }
__printHdr(pcb, &pkt->hdr, pa, phf, pkt->len);
}
#else
#define _printHdr(a,b,c)
#define _printPkt(a,b,c,d)
#define _printTime()
#endif
#define DELAY_ACK_MS 100
#define RTT_UINT 200 //accuracy of RTT, ms
#define RTT_MIN (1000/RTT_UINT) //count in 1 second.
#define MAX_REXMT_ATTEMPT 6
static int rudp_backoff[MAX_REXMT_ATTEMPT+1] = { 1, 2, 4, 8, 16, 32, 32/*, 64, 64, 64, 64, 64*/ };
#define MAX_RECONN_ATTEMPT 5
static int conn_backoff[MAX_RECONN_ATTEMPT+1] = { RTT_MIN, 1*RTT_MIN, 2*RTT_MIN, 2*RTT_MIN, 2*RTT_MIN, 2*RTT_MIN }; //s
static struct rudp_pkt *_MBufGetPacket();
static void _MBufPutPacket(struct rudp_pkt *pkt);
static int _ProcessPacket(struct rudp_socket *s, struct rudp_pkt *pkt, const struct sockaddr *from, int from_len);
int _DispatchPacket(struct rudp_socket *s, struct rudp_pkt *pkt, const struct sockaddr *from, int from_len);
static INLINE void _sendPacket(struct rudp_socket *s, struct rudp_channel *pch, struct rudp_pkt *pkt, int opt);
static void _sendReset(struct rudp_socket *s, const struct sockaddr *to);
static /*INLINE */void _sendHeader(struct rudp_socket *s, struct rudp_hdr *phdr);
static void _sendSyn(struct rudp_socket *s);
static void _sendSynAck(struct rudp_socket *s);
static void _sendAck(struct rudp_socket *s, int chno);
static void _sendFin(struct rudp_socket *s);
typedef void (*TimerHandler)(struct rudp_socket *s);
static void _timerProc(TimerHandler handler);
static void _handleTimer500ms(struct rudp_socket *s);
static void _handleTimer200ms(struct rudp_socket *s);
static struct rudp_pkt *_MBufGetPacket()
{
struct rudp_pkt *p;
PA_MutexLock(mutex_pkt_pool);
if(free_pkt)
{
p = free_pkt;
free_pkt = free_pkt->next;
n_free_pkt --;
}
else
{
p = (struct rudp_pkt*)malloc(sizeof(struct rudp_pkt));
if(!p) {
PA_MutexUnlock(mutex_pkt_pool);
return NULL;
}
}
p->next = NULL;
p->hdr.u32_flags = 0;
p->hdr.flags.rudp = RUDP_HEADER_TAG;
p->len = 0;
p->trans = 0;
p->pdata = p->data;
PA_MutexUnlock(mutex_pkt_pool);
return p;
}
static void _MBufPutPacket(struct rudp_pkt *pkt)
{
PA_MutexLock(mutex_pkt_pool);
if(n_free_pkt > 256)
{
free(pkt);
}
else
{
pkt->next = free_pkt;
free_pkt = pkt;
n_free_pkt++;
}
PA_MutexUnlock(mutex_pkt_pool);
}
static struct rudp_socket *_AllocRudpSocket()
{
struct rudp_socket *sock = (struct rudp_socket*)calloc(sizeof(struct rudp_socket), 1);
sock->tag = RUDP_SOCKET_TAG;
sock->state = RS_CLOSED;
sock->rcvbuf_sz = DEFAULT_RCVBUF_SIZE;
PA_MutexInit(sock->mutex_r);
PA_MutexInit(sock->mutex_w);
#if defined(WIN32) || defined(ARM_UCOS_LWIP)
PA_EventInit(sock->event_r);
PA_EventInit(sock->event_w);
#else
pthread_cond_init(&sock->event_r, NULL);
pthread_cond_init(&sock->event_w, NULL);
#endif
INIT_LIST_HEAD(&sock->inst_list);
INIT_LIST_HEAD(&sock->listen_queue);
INIT_LIST_HEAD(&sock->accepted_list);
return sock;
}
static struct rudp_pcb *_AllocRudpPcb(uint32_t rcvbuf_size, uint32_t initial_seqno, uint32_t peer_initial_seqno, int rawnd)
{
struct rudp_pcb *pcb;
int i;
pcb = (struct rudp_pcb*)calloc(sizeof(struct rudp_pcb), 1);
for(i=0; i<MAX_PHY_CHANNELS; i++)
{
struct sndbuf *psbuf;
struct rcvbuf *prbuf;
psbuf = &pcb->channel[i].sbuf;
psbuf->seqno = initial_seqno;
psbuf->max_pkts = DEFAULT_SNDBUF_SIZE;
psbuf->rwnd = psbuf->rawnd = rawnd;
prbuf = &pcb->channel[i].rbuf;
prbuf->expected_seqno = prbuf->first_seq = peer_initial_seqno;
prbuf->q_size = rcvbuf_size;;
prbuf->pkt_q = (struct rudp_pkt**)calloc(sizeof(void*), rcvbuf_size);
prbuf->win = prbuf->q_size - 1;
}
pcb->cwnd = 2;
pcb->rtw_size = rawnd/2;//8;
pcb->rwin_size = rawnd;
pcb->ssthresh = rawnd;
pcb->srtt = 0;
pcb->sdev = 3;
pcb->rto = 6; //As [Jacobson 1988] rto = srtt + 2*sdev, but it seems too large for us
return pcb;
}
static void _terminateSocketInternally(struct rudp_socket *s, int err)
{
if(s->state == RS_DEAD) return;
PA_MutexLock(s->mutex_r);
PA_MutexLock(s->mutex_w);
s->state = RS_DEAD;
s->err = err;
if(s->pcb)
{
int i;
for(i=0; i<MAX_PHY_CHANNELS; i++)
{
struct rudp_pkt *c;
struct rcvbuf *prb;
c = s->pcb->channel[i].sbuf.first;
while(c)
{
struct rudp_pkt *p = c;
c = c->next;
_MBufPutPacket(p);
}
prb = &s->pcb->channel[i].rbuf;
for(; prb->head != prb->tail; prb->head = (prb->head+1)%prb->q_size)
if(prb->pkt_q[prb->head])
_MBufPutPacket(prb->pkt_q[prb->head]);
free(prb->pkt_q);
}
free(s->pcb);
s->pcb = NULL;
}
SETEVENT(s->event_r);
SETEVENT(s->event_w);
PA_MutexUnlock(s->mutex_w);
PA_MutexUnlock(s->mutex_r);
#if 0
//Still in listening queue
if(list_empty(&s->inst_list) && !list_empty(&s->listen_queue))
{
list_del(&s->listen_queue);
free(s);
}
#endif
//dbg_msg("################## _terminateSocketInternally ##########\n");
}
/// \brief Should be called with "mutex_sock_list" hold
// \param err error code for the reason to cleanup this socket
static void _CleanupSocket(struct rudp_socket *s, int err)
{
if(s->state == RS_DEAD) return;
_terminateSocketInternally(s, err);
PA_MutexUninit(s->mutex_r);
PA_MutexUninit(s->mutex_w);
#if defined(WIN32) || defined(ARM_UCOS_LWIP)
PA_EventUninit(s->event_r);
PA_EventUninit(s->event_w);
#else
pthread_cond_destroy(&s->event_r);
pthread_cond_destroy(&s->event_w);
#endif
if(!list_empty(&s->listen_queue))
{
struct list_head *pp, *qq;
list_for_each_safe(pp, qq, &s->listen_queue)
{
struct rudp_socket *sl = list_entry(pp, struct rudp_socket, listen_queue);
_CleanupSocket(sl, 0);
list_del(pp);
free(sl);
}
}
s->state = RS_DEAD;
//s->tag = 0;
}
static void _CleanAndFreeSocket(struct rudp_socket *s)
{
if(list_empty(&s->inst_list)) //accepted
{
list_del(&s->accepted_list);
}
else
{
list_del(&s->inst_list);
if(list_empty(&s->accepted_list))
PA_SocketClose(s->udp_sock);
else
{
struct rudp_socket *aa = list_entry(s->accepted_list.next, struct rudp_socket, accepted_list);
INIT_LIST_HEAD(&aa->inst_list);
list_add_tail(&aa->inst_list, &sock_list);
}
}
if(s->state != RS_DEAD) _CleanupSocket(s, 0);
free(s);
}
void _timerProc(TimerHandler handler)
{
struct list_head *p, *q, *pp, *qq;
//PA_MutexLock(mutex_sock_list);
list_for_each_safe(p, q, &sock_list)
{
struct rudp_socket *s, *ss;
s = list_entry(p, struct rudp_socket, inst_list);
if(s->state == RS_DEAD) continue;
list_for_each_safe(pp, qq, &s->accepted_list)
{
ss = list_entry(pp, struct rudp_socket, accepted_list);
handler(ss);
}
list_for_each_safe(pp, qq, &s->listen_queue) //a socket in listen_queue may be removed due to timeout
{
ss = list_entry(pp, struct rudp_socket, listen_queue);
handler(ss);
}
handler(s);
}
//PA_MutexUnlock(mutex_sock_list);
}
static void _congestionAvoidance(struct rudp_socket *s)
{
if(s->pcb->cwnd < s->pcb->ssthresh)
{//slow start
s->pcb->cwnd++;
#if 1
s->pcb->ca_cnt++;
if(s->pcb->ca_cnt >= s->pcb->rwin_size)
#endif
if(s->pcb->rtw_size < s->pcb->rwin_size)
s->pcb->rtw_size++;
}
else
{//congestion avoidance, increase cwnd slowly
s->pcb->ca_cnt++;
if(s->pcb->ca_cnt >= s->pcb->cwnd)
{
s->pcb->ca_cnt = 0;
if(s->pcb->cwnd < s->pcb->rwin_size)
{
s->pcb->cwnd++;
//s->pcb->ssthresh++; ???
}
if(s->pcb->rtw_size < s->pcb->rwin_size)
s->pcb->rtw_size++;
}
}
}
static void _congestionDetected(struct rudp_socket *s, int chno, int what)
{
//int rawnd, i;
struct rudp_pcb *pcb;
//for(i=rawnd=0; i<MAX_CHANNELS; i++) rawnd += pcb->channel[chno].sbuf.rawnd;
pcb = s->pcb;
pcb->ssthresh = min(pcb->channel[chno].sbuf.rawnd, pcb->cwnd)/2;
if(pcb->ssthresh < 2) pcb->ssthresh = 2;
if(what == CONGESTED) pcb->cwnd = 1;
else pcb->cwnd = pcb->ssthresh + 3;
switch(what)
{
case CONGESTED:
case FASTRETRANS2:
pcb->rtw_size >>= 1;
if(pcb->rtw_size < 8) pcb->rtw_size = 8;
break;
case FASTRETRANS:
pcb->rtw_size -= pcb->rtw_size >> 2;
if(pcb->rtw_size < 8) pcb->rtw_size = 8;
break;
}
}
static INLINE unsigned int _calcCurWnd(struct rudp_socket *s, struct sndbuf *psbuf)
{
//if receiver's rawnd is 0, still send one more packet then transmission can be
//started again by re-transfer timer, even when the receiver's OPENWND ACK(s) are lost
return min(s->pcb->cwnd, psbuf->rwnd);
//return min(min(s->pcb->cwnd, psbuf->rwnd),psbuf->rawnd);
//return psbuf->rwnd;
}
/** Output a packet
*
* mutex_w shall be hold before call _RudpOutput
*
* return: 1 - if a packet is sent; otherwise, no packet is sent
*/
static int _RudpOutput(struct rudp_socket *s, int chno, int opt)
{
struct sndbuf *psbuf;
struct rcvbuf *prbuf;
struct rudp_channel *pch;
if(s->tag != RUDP_SOCKET_TAG) return -1;
if(s->state <= RS_CLOSED) return -1;
pch = &s->pcb->channel[chno];
psbuf = &pch->sbuf;
prbuf = &pch->rbuf;
//if(pch->congested && opt == RO_NORMAL) return;
if(opt == RO_REXMT || opt == RO_REXMT_FAST) //packets are queued before
{
struct rudp_pkt *pkt = opt == RO_REXMT_FAST ? psbuf->rexmt : psbuf->first;
if(!pkt) return 0;
if(prbuf->should_ack == ACKT_DELAYED)
{
prbuf->should_ack = 0;
pkt->hdr.flags.ack = 1;
pkt->hdr.ackno = ntohl(prbuf->expected_seqno);
pkt->hdr.flags.n_loss = prbuf->n_lost;
//if(psbuf->first->trans) psbuf->first->hdr.crc32 = calc_crc32(0, (char*)&psbuf->first->hdr, offsetof(struct rudp_hdr, crc32));
}
_sendPacket(s, pch, pkt, opt);
pkt->hdr.flags.ack = 0;
return 1;
}
if(((prbuf->should_ack == ACKT_DELAYED) && !psbuf->first) || prbuf->should_ack == ACKT_OPENWND)
{
_sendAck(s, chno);
prbuf->should_ack = 0;
return 0;
}
if(opt == 0 && pch->sbuf.rawnd == 0)
{
if(pch->timer[RCT_REXMT] == 0)
pch->timer[RCT_PERSIST] = s->pcb->rto * rudp_backoff[pch->sbuf.first?pch->sbuf.first->trans:0];
//signalOutput(s, chno);
}
else
{
struct rudp_pkt *p;
p = psbuf->not_sent;
pch->timer[RCT_PERSIST] = 0;
if(p && (opt == RO_FORCE /*|| prbuf->should_ack == ACKT_DELAYED */
|| _calcCurWnd(s, psbuf) > psbuf->n_unacked
/* && psbuf->n_unacked < s->pcb->rtw_size*/))
//psbuf->n_unacked < psbuf->rawnd) )
{
if(prbuf->should_ack == ACKT_DELAYED)
{
prbuf->should_ack = 0;
p->hdr.flags.ack = 1;
p->hdr.ackno = ntohl(prbuf->expected_seqno);
p->hdr.flags.n_loss = prbuf->n_lost;
//if(p->trans) p->hdr.crc32 = calc_crc32(0, (char*)&p->hdr, offsetof(struct rudp_hdr, crc32));
}
//psbuf->n_unacked++;
_sendPacket(s, pch, p, opt);
p->hdr.flags.ack = 0;
p = p->next;
//psbuf->not_sent = p = p->next;
if(p && p->seqno < psbuf->not_sent->seqno)
{
dbg_msg("#####################################################\n");
}
psbuf->not_sent = p;
//if(psbuf->rwnd > 0)
psbuf->rwnd--;
return 1;
if(opt == RO_ONLYONE) return 1;
if(_calcCurWnd(s, psbuf) && p)
{
//signalOutput(s, chno);
//struct output_notify notify = { s, chno };
//PA_Send(sockw_s, ¬ify, sizeof(notify), 0);
}
}
}
return 0;
}
void _handleTimer500ms(struct rudp_socket *s)
{
int i, j;
struct rudp_pcb *pcb;
if(s->state == RS_LISTEN || s->state == RS_DEAD) return;
pcb = s->pcb;
if(pcb)
{
int sbuf_is_empty = 1;
//rudp_now ++;
PA_MutexLock(s->mutex_w);
for(i=0; i<MAX_PHY_CHANNELS; i++)
{
struct rudp_channel *pch = &pcb->channel[i];
if(pch->sbuf.first) sbuf_is_empty = 0;
for(j=0; j<RCT_CNT; j++)
{
if(pch->timer[j] == 0) continue;
pch->timer[j] --;
if(pch->timer[j] == 0)
{
switch(j)
{
case RCT_PERSIST:
dbg_msg("Persist timeout.\n");
_RudpOutput(s, i, RO_FORCE);
break;
case RCT_REXMT:
if(pch->sbuf.first)
{
if(pch->sbuf.first->trans >= MAX_REXMT_ATTEMPT)
{
PA_MutexUnlock(s->mutex_w);
_CleanupSocket(s, ERUDP_TIMEOUTED);
s->state = RS_DEAD;
s->err = ERUDP_TIMEOUTED;
return;
}
else if(pch->sbuf.first->trans)
{
_congestionDetected(s, i, CONGESTED);
_printTime(); dbg_msg("congested: cwnd=%d, ssthresh=%d\n", pcb->cwnd, pcb->ssthresh);
_congestionAvoidance(s);
_RudpOutput(s, i, RO_REXMT);
pch->congested = 1;
pch->sbuf.pkt_rttm_start = pch->sbuf.not_sent;
}
else
_RudpOutput(s, i, 0);
}
break;
}
}
}
}
if(sbuf_is_empty && s->state == RS_FIN_QUEUED)
{
_sendFin(s);
s->state = RS_FIN_WAIT_1;
s->timer[RT_KEEP] = RTT_MIN * RTV_KEEP_CLOSE;
}
PA_MutexUnlock(s->mutex_w);
}
for(i=0; i<RT_CNT; i++)
{
if(s->timer[i] == 0) continue;
s->timer[i]--;
if(s->timer[i] == 0)
{
switch(i)
{
case RT_KEEP: //for connecting timeout
if(s->state == RS_SYN_RCVD || s->state == RS_SYN_SENT)
{
if(s->pcb->retr_cnt >= MAX_RECONN_ATTEMPT)
{
if(list_empty(&s->inst_list))
{
list_del(&s->listen_queue);
INIT_LIST_HEAD(&s->listen_queue);
_CleanupSocket(s, ERUDP_TIMEOUTED);
free(s);
}
else
{
//_CleanupSocket(s, ERUDP_TIMEOUTED);
s->err = ERUDP_TIMEOUTED;
s->state = RS_CLOSED;
SETEVENT(s->event_w);
}
return;
}
else
{
s->timer[RT_KEEP] = conn_backoff[++s->pcb->retr_cnt];
if(s->state == RS_SYN_SENT)
_sendSyn(s);
else
_sendSynAck(s);
}
}
else if(s->state >= RS_FIN_QUEUED)
{
dbg_msg("clean and free %p\n", s);
_CleanAndFreeSocket(s);
}
break;
case RT_2MSL:
break;
}
}
}
}
void _handleTimer200ms(struct rudp_socket *s)
{
if(s->pcb)// && (s->pcb->r_flags & RUDPF_DELAYACK))
{
int i;
for(i=0; i<MAX_PHY_CHANNELS; i++)
{
struct rcvbuf *prb = &s->pcb->channel[i].rbuf;
if(prb->should_ack)
{
//_printTime(); dbg_msg("delayed ack.\n");
PA_MutexLock(s->mutex_w);
_RudpOutput(s, i, 0);
//signalOutput(s, i);
PA_MutexUnlock(s->mutex_w);
}
}
}
}
void _sendHeader(struct rudp_socket *s, struct rudp_hdr *phdr)
{
_printHdr(s->pcb, phdr, &s->pcb->peer);
phdr->crc32 = calc_crc32(0, (char*)phdr, offsetof(struct rudp_hdr, crc32));
PA_SendTo(s->udp_sock, phdr, sizeof(struct rudp_hdr), 0,
s->connected?NULL:(struct sockaddr*)&s->pcb->peer,
sizeof(struct sockaddr));
}
void _sendPacket(struct rudp_socket *s, struct rudp_channel *pch, struct rudp_pkt *pkt, int opt)
{
if(pch->timer[RCT_REXMT] == 0)
pch->timer[RCT_REXMT] = s->pcb->rto * rudp_backoff[pkt->trans];
pkt->ts = rudp_now;
//dbg_msg("............. seq %u, ts %d...........", pkt->seqno, rudp_now);
pkt->hdr.flags.window = WINDOW_HTON(pch->rbuf.win);
if(opt != RO_REXMT_FAST)
{
if(!pkt->trans) pch->sbuf.n_unacked++;
pkt->trans ++;
if(pkt->trans >= MAX_REXMT_ATTEMPT) pkt->trans = MAX_REXMT_ATTEMPT;
}
pkt->hdr.crc32 = calc_crc32(0, (char*)&pkt->hdr, offsetof(struct rudp_hdr, crc32));
_printPkt(s->pcb, pkt, PHF_DATA|pch->sbuf.rwnd, &s->pcb->peer);
PA_SendTo(s->udp_sock, &pkt->hdr, sizeof(struct rudp_hdr) + pkt->len, 0,
s->connected?NULL:(struct sockaddr*)&s->pcb->peer,
sizeof(struct sockaddr));
}
void _sendSyn(struct rudp_socket *s)
{
struct rudp_hdr hdr;
hdr.u32_flags = 0;
hdr.flags.rudp = RUDP_HEADER_TAG;
hdr.flags.syn = 1;
hdr.seqno = htonl(s->pcb->channel[0].sbuf.seqno);
hdr.ackno = 0;
hdr.flags.window = WINDOW_HTON(s->pcb->channel[0].rbuf.win);
_sendHeader(s, &hdr);
}
void _sendSynAck(struct rudp_socket *s)
{
struct rudp_hdr hdr;
hdr.u32_flags = 0;
hdr.flags.rudp = RUDP_HEADER_TAG;
hdr.flags.syn = 1;
hdr.seqno = htonl(s->pcb->channel[0].sbuf.seqno);
hdr.flags.ack = 1;
hdr.ackno = htonl(s->pcb->channel[0].rbuf.expected_seqno);
hdr.flags.window = WINDOW_HTON(s->pcb->channel[0].rbuf.win);
_sendHeader(s, &hdr);
}
//only ack flag
void _sendEmptyAck(struct rudp_socket *s, int chno)
{
struct rudp_hdr hdr;
hdr.u32_flags = 0;
hdr.flags.rudp = RUDP_HEADER_TAG;
hdr.seqno = 0;
hdr.flags.chno = chno;
hdr.flags.ack = 1;
hdr.ackno = 0;
hdr.flags.window = 0;
_sendHeader(s, &hdr);
}
//ack without data
void _sendAck(struct rudp_socket *s, int chno)
{
struct rudp_hdr hdr;
struct rcvbuf *pr = &s->pcb->channel[chno].rbuf;
hdr.u32_flags = 0;
hdr.flags.rudp = RUDP_HEADER_TAG;
hdr.flags.ack = 1;
hdr.flags.chno = chno;
hdr.seqno = htonl(s->pcb->channel[chno].sbuf.seqno);
hdr.ackno = htonl(pr->expected_seqno);
hdr.flags.n_loss = pr->n_lost;
hdr.flags.window = WINDOW_HTON(pr->win);
_sendHeader(s, &hdr);
pr->acked_seqno = pr->expected_seqno;
pr->should_ack = 0;
}
void _sendFin(struct rudp_socket *s)
{
struct rudp_hdr hdr;
hdr.u32_flags = 0;
hdr.flags.rudp = RUDP_HEADER_TAG;
hdr.flags.fin = 1;
hdr.seqno = 0;
hdr.ackno = 0;
hdr.flags.window = 0;
_sendHeader(s, &hdr);
}
void _updateRTO(struct rudp_socket *s, int rtt)
{
int rto0 = s->pcb->rto;
#if 0
/* [Jacobson 1988], refresh rto */
int drtt = rtt - s->pcb->srtt;
if(drtt > 0)
{
s->pcb->srtt += drtt >> 3;
if(drtt > s->pcb->sdev)
s->pcb->sdev += (drtt - s->pcb->sdev) >> 2;
else
s->pcb->sdev -= (s->pcb->sdev - drtt) >> 2;
}
else
{
drtt = -drtt;
s->pcb->srtt -= drtt >> 3;
if(drtt > s->pcb->sdev)
s->pcb->sdev += (drtt - s->pcb->sdev) >> 2;
else
s->pcb->sdev -= (s->pcb->sdev - drtt) >> 2;
}
s->pcb->rto = s->pcb->srtt + (s->pcb->sdev > 0) ?(s->pcb->sdev << 2):(-s->pcb->sdev << 2);
if(s->pcb->rto < 2) s->pcb->rto = 2;
dbg_msg("rtt = %d, rto = %d\n", rtt, s->pcb->rto);
#else
if(rtt < RTT_MIN) rtt = RTT_MIN;
//if(rtt < 2) rtt = 2;
s->pcb->rto = rtt;
#endif
if(s->pcb->rto - rto0 > 0)
{
s->pcb->cwnd -= s->pcb->cwnd >> 2;
//s->pcb->rtw_size -= s->pcb->rtw_size >> 2;
}
}
INLINE BOOL _isPacketValid(struct rudp_pkt *pkt)
{
#ifdef _DEBUG
return (calc_crc32(0, (char*)&pkt->hdr, offsetof(struct rudp_hdr, crc32)) == pkt->hdr.crc32)?TRUE:(printf("Invalid packet!\n"),FALSE);
#else
return calc_crc32(0, (char*)&pkt->hdr, offsetof(struct rudp_hdr, crc32)) == pkt->hdr.crc32;
#endif
}
int _DispatchPacket(struct rudp_socket *s, struct rudp_pkt *pkt, const struct sockaddr *from, int from_len)
{
struct list_head *pp, *qq;
struct rudp_socket *sa;
struct sockaddr_in *sp, *sf;
sf = (struct sockaddr_in*)from;
/* We must search each queue to make sure there is no duplicated connection for a listening socket */
if(s->state == RS_LISTEN)
{
list_for_each_safe(pp, qq, &s->listen_queue)
{
sa = list_entry(pp, struct rudp_socket, listen_queue);
if(!sa->pcb) continue;
sp = (struct sockaddr_in*)&sa->pcb->peer;
if(sp->sin_addr.s_addr == sf->sin_addr.s_addr && sp->sin_port == sf->sin_port)
{
int check_again;
if(pkt->hdr.flags.rst)
{
list_del(&sa->listen_queue);
INIT_LIST_HEAD(&sa->listen_queue);
_CleanupSocket(sa, ERUDP_RESETED);
free(sa);
return 0;
}
check_again = 0;
if(pkt->hdr.flags.ack && sa->state == RS_SYN_RCVD)
check_again = 1;
if(_ProcessPacket(sa, pkt, from, from_len) < 0)
_MBufPutPacket(pkt);
if(check_again && sa->state == RS_ESTABLISHED)
SETEVENT(s->event_r);
return 0;
}
}
}
list_for_each(pp, &s->accepted_list)
{
sa = list_entry(pp, struct rudp_socket, accepted_list);
if(sa->state >= RS_ESTABLISHED)
{
sp = (struct sockaddr_in*)&sa->pcb->peer;
if(sp->sin_addr.s_addr == sf->sin_addr.s_addr && sp->sin_port == sf->sin_port)
{
if(_ProcessPacket(sa, pkt, from, from_len) < 0)
_MBufPutPacket(pkt);
return 0;
}
}
}
/* It's time to Me */
if(s->state == RS_LISTEN/*listening socket*/ ||
( s->pcb && (s->connected/*client*/
|| (((sp = (struct sockaddr_in*)&s->pcb->peer), sp->sin_addr.s_addr == sf->sin_addr.s_addr)
&& (sp->sin_port == sf->sin_port)/*accepted(listening socket is closed)*/) )
)
)
{
if(_ProcessPacket(s, pkt, from, from_len) < 0)
_MBufPutPacket(pkt);
return 0;
}
/* Nobody want this packet */
if(!(pkt->hdr.flags.rst && s->state <= 0))
_sendReset(s, from);
_MBufPutPacket(pkt);
return 0;
}
static int _PPState_Established(struct rudp_socket *s, struct rudp_pkt *pkt, const struct sockaddr *from, int from_len)
{
struct rudp_channel *pch;
struct sndbuf *psbuf;
struct rcvbuf *prbuf;
int old_rawnd;
int chno;
if(pkt->hdr.flags.syn)
{
if(pkt->hdr.flags.ack)
_sendAck(s, 0);
else
_sendReset(s, from);
return -1;
}
if(pkt->hdr.flags.fin)
{
int i, ii;
PA_MutexLock(s->mutex_w);
s->state = RS_CLOSE_WAIT;
_sendAck(s, 0);
s->timer[RT_KEEP] = RTT_MIN * RTV_KEEP_CLOSE;
//
// Cleanup sndbuf
//
for(i=0; i<MAX_PHY_CHANNELS; i++)
{
struct sndbuf *psb = &s->pcb->channel[i].sbuf;
struct rudp_pkt *c = psb->first;
while(c)
{
struct rudp_pkt *p = c;
c = c->next;
_MBufPutPacket(p);
}
memset(psb, 0, sizeof(struct sndbuf));
for(ii=0; ii<RCT_CNT; ii++) s->pcb->channel[i].timer[ii] = 0;
}
for(i=0; i<RT_CNT; i++) s->timer[i] = 0;
PA_MutexUnlock(s->mutex_w);
goto _checkdata;
}
chno = pkt->hdr.flags.chno;
pch = &s->pcb->channel[chno];
psbuf = &pch->sbuf;
old_rawnd = psbuf->rawnd;
psbuf->rawnd = WINDOW_NTOH(pkt->hdr.flags.window);
//psbuf->rwnd += old_rawnd - psbuf->rawnd;
psbuf->rwnd = psbuf->rawnd - psbuf->n_unacked;
if(psbuf->rwnd < 0) psbuf->rwnd = 0;
if(pkt->hdr.flags.ack && psbuf->first)
{
uint32_t ackno = ntohl(pkt->hdr.ackno);