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
128
129
130
133
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
176
177
178
179
180
181
184
185
186
187
188
189
190
191
192
193
194
195
196
197
200
201
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
233
234
245
246
247
248
249
250
251
252
253
254
255
256
257
262
263
264
265
268
269
281
282
283
284
285
286
287
288
289
290
291
292
293
294
306
307
308
309
310
311
312
313
314
315
316
317
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
356
357
358
360
361
362
363
364
365
366
372
373
374
375
376
378
405
406
407
408
409
410
411
412
413
414
441
442
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
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
524
537
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
581
585
586
587
588
589
590
591
592
593
594
604
607
610
611
615
616
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
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
678
679
680
681
682
683
684
685
686
695
696
697
707
708
738
739
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
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
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
866
867
882
883
887
888
889
890
891
892
893
894
895
896
897
898
899
902
903
905
906
907
908
909
910
911
913
914
915
916
917
920
921
922
923
929
930
931
932
933
934
935
936
937
942
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
989
990
991
992
996
1000
1001
1002
1003
1004
1005
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1022
1023
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1092
1096
1097
1098
1105
1106
1107
1114
1115
1116
1122
1123
1124
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1152
1153
1154
1155
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1212
1213
1214
1215
1216
1217
1223
1224
1225
1226
1227
1228
1235
1270
1271
1274
1275
1276
1277
1278
1279
1280
1283
1284
1286
1291
1292
1293
1294
1295
1296
1297
1298
1301
1302
1303
1304
1305
1306
1307
1309
1310
1311
1312
1313
1314
1315
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1348
1349
1350
1353
1354
1360
1361
1362
1369
1370
1371
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1396
1397
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1428
1429
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1448
1449
1450
1451
1452
1453
1454
1462
1463
1464
1465
1469
1470
1471
1472
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1499
1500
1505
1506
1507
1508
1509
1517
1518
1521
1522
1523
1524
1525
1526
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1555
1556
1557
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1574
1577
1578
1581
1582
1589
1590
1591
1595
1596
1597
1598
1599
1600
1601
1602
1607
1608
1616
1617
1622
1623
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1696
1697
1698
1699
1700
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1717
1720
1721
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1745
1746
1747
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1763
1771
1777
1778
1779
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1798
1803
1804
1805
1810
1811
1826
1827
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1872
1873
1877
1878
1882
1883
1887
1888
1889
1890
1891
1892
1893
1895
1896
1897
1899
1900
1901
1902
1903
1904
1905
1906
1907
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2144
2147
2150
2151
2156
2157
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2214
2215
2216
2217
2218
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2255
2256
2257
2258
2259
2260
2266
2267
2268
2269
2270
2271
2272
2273
2274
2277
2278
2279
2280
2281
2282
2285
2286
2287
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2312
2313
2314
2315
2316
2317
2318
2319
2322
2323
2324
2325
2330
2331
2352
2353
2354
2358
2359
2360
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2440
2441
2445
2446
2451
2452
2453
2454
2457
2460
2463
2466
2469
2472
2475
2478
2479
2480
2481
2484
2485
2486
2487
2488
2489
2490
2491
2497
2498
2499
2503
2504
2505
2511
2512
2513
2516
2517
2518
2519
2520
2525
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2644
2645
2646
2647
2648
2649
2650
2657
2658
2659
2665
2666
2667
2668
2669
2670
2671
2672
2682
2683
2684
2685
2686
/* ... */
/* ... */
#include "lwip/opt.h"
#if LWIP_TCP
#include "lwip/def.h"
#include "lwip/mem.h"
#include "lwip/memp.h"
#include "lwip/tcp.h"
#include "lwip/priv/tcp_priv.h"
#include "lwip/debug.h"
#include "lwip/stats.h"
#include "lwip/ip6.h"
#include "lwip/ip6_addr.h"
#include "lwip/nd6.h"
#include <string.h>
11 includes
#ifdef LWIP_HOOK_FILENAME
#include LWIP_HOOK_FILENAME
#endif
#ifndef TCP_LOCAL_PORT_RANGE_START
/* ... */
#define TCP_LOCAL_PORT_RANGE_START 0xc000
#define TCP_LOCAL_PORT_RANGE_END 0xffff
#define TCP_ENSURE_LOCAL_PORT_RANGE(port) ((u16_t)(((port) & (u16_t)~TCP_LOCAL_PORT_RANGE_START) + TCP_LOCAL_PORT_RANGE_START))
/* ... */#endif
#if LWIP_TCP_KEEPALIVE
#define TCP_KEEP_DUR(pcb) ((pcb)->keep_cnt * (pcb)->keep_intvl)
#define TCP_KEEP_INTVL(pcb) ((pcb)->keep_intvl)
/* ... */#else
#define TCP_KEEP_DUR(pcb) TCP_MAXIDLE
#define TCP_KEEP_INTVL(pcb) TCP_KEEPINTVL_DEFAULT
/* ... */#endif
#if TCP_MSS > 536
#define INITIAL_MSS 536
#else
#define INITIAL_MSS TCP_MSS
#endif
static const char *const tcp_state_str[] = {
"CLOSED",
"LISTEN",
"SYN_SENT",
"SYN_RCVD",
"ESTABLISHED",
"FIN_WAIT_1",
"FIN_WAIT_2",
"CLOSE_WAIT",
"CLOSING",
"LAST_ACK",
"TIME_WAIT"
...};
static u16_t tcp_port = TCP_LOCAL_PORT_RANGE_START;
u32_t tcp_ticks;
static const u8_t tcp_backoff[13] =
{ 1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7};
static const u8_t tcp_persist_backoff[7] = { 3, 6, 12, 24, 48, 96, 120 };
struct tcp_pcb *tcp_bound_pcbs;
union tcp_listen_pcbs_t tcp_listen_pcbs;
/* ... */
struct tcp_pcb *tcp_active_pcbs;
struct tcp_pcb *tcp_tw_pcbs;
struct tcp_pcb **const tcp_pcb_lists[] = {&tcp_listen_pcbs.pcbs, &tcp_bound_pcbs,
&tcp_active_pcbs, &tcp_tw_pcbs
...};
u8_t tcp_active_pcbs_changed;
static u8_t tcp_timer;
static u8_t tcp_timer_ctr;
static u16_t tcp_new_port(void);
static err_t tcp_close_shutdown_fin(struct tcp_pcb *pcb);
#if LWIP_TCP_PCB_NUM_EXT_ARGS
static void tcp_ext_arg_invoke_callbacks_destroyed(struct tcp_pcb_ext_args *ext_args);
#endif
/* ... */
void
tcp_init(void)
{
#ifdef LWIP_RAND
tcp_port = TCP_ENSURE_LOCAL_PORT_RANGE(LWIP_RAND());
#endif
}{ ... }
void
tcp_free(struct tcp_pcb *pcb)
{
LWIP_ASSERT("tcp_free: LISTEN", pcb->state != LISTEN);
#if LWIP_TCP_PCB_NUM_EXT_ARGS
tcp_ext_arg_invoke_callbacks_destroyed(pcb->ext_args);
#endif
memp_free(MEMP_TCP_PCB, pcb);
}{ ... }
static void
tcp_free_listen(struct tcp_pcb *pcb)
{
LWIP_ASSERT("tcp_free_listen: !LISTEN", pcb->state != LISTEN);
#if LWIP_TCP_PCB_NUM_EXT_ARGS
tcp_ext_arg_invoke_callbacks_destroyed(pcb->ext_args);
#endif
memp_free(MEMP_TCP_PCB_LISTEN, pcb);
}{ ... }
/* ... */
void
tcp_tmr(void)
{
tcp_fasttmr();
if (++tcp_timer & 1) {
/* ... */
tcp_slowtmr();
}if (++tcp_timer & 1) { ... }
}{ ... }
#if LWIP_CALLBACK_API || TCP_LISTEN_BACKLOG
/* ... */
static void
tcp_remove_listener(struct tcp_pcb *list, struct tcp_pcb_listen *lpcb)
{
struct tcp_pcb *pcb;
LWIP_ASSERT("tcp_remove_listener: invalid listener", lpcb != NULL);
for (pcb = list; pcb != NULL; pcb = pcb->next) {
if (pcb->listener == lpcb) {
pcb->listener = NULL;
}if (pcb->listener == lpcb) { ... }
}for (pcb = list; pcb != NULL; pcb = pcb->next) { ... }
}{ ... }
#endif/* ... */
/* ... */
static void
tcp_listen_closed(struct tcp_pcb *pcb)
{
#if LWIP_CALLBACK_API || TCP_LISTEN_BACKLOG
size_t i;
LWIP_ASSERT("pcb != NULL", pcb != NULL);
LWIP_ASSERT("pcb->state == LISTEN", pcb->state == LISTEN);
for (i = 1; i < LWIP_ARRAYSIZE(tcp_pcb_lists); i++) {
tcp_remove_listener(*tcp_pcb_lists[i], (struct tcp_pcb_listen *)pcb);
}for (i = 1; i < LWIP_ARRAYSIZE(tcp_pcb_lists); i++) { ... }
/* ... */#endif
LWIP_UNUSED_ARG(pcb);
}{ ... }
#if TCP_LISTEN_BACKLOG
/* ... */
void
tcp_backlog_delayed(struct tcp_pcb *pcb)
{
LWIP_ASSERT("pcb != NULL", pcb != NULL);
LWIP_ASSERT_CORE_LOCKED();
if ((pcb->flags & TF_BACKLOGPEND) == 0) {
if (pcb->listener != NULL) {
pcb->listener->accepts_pending++;
LWIP_ASSERT("accepts_pending != 0", pcb->listener->accepts_pending != 0);
tcp_set_flags(pcb, TF_BACKLOGPEND);
}if (pcb->listener != NULL) { ... }
}if ((pcb->flags & TF_BACKLOGPEND) == 0) { ... }
}tcp_backlog_delayed (struct tcp_pcb *pcb) { ... }
/* ... */
void
tcp_backlog_accepted(struct tcp_pcb *pcb)
{
LWIP_ASSERT("pcb != NULL", pcb != NULL);
LWIP_ASSERT_CORE_LOCKED();
if ((pcb->flags & TF_BACKLOGPEND) != 0) {
if (pcb->listener != NULL) {
LWIP_ASSERT("accepts_pending != 0", pcb->listener->accepts_pending != 0);
pcb->listener->accepts_pending--;
tcp_clear_flags(pcb, TF_BACKLOGPEND);
}if (pcb->listener != NULL) { ... }
}if ((pcb->flags & TF_BACKLOGPEND) != 0) { ... }
}tcp_backlog_accepted (struct tcp_pcb *pcb) { ... }
/* ... */#endif
/* ... */
static err_t
tcp_close_shutdown(struct tcp_pcb *pcb, u8_t rst_on_unacked_data)
{
LWIP_ASSERT("tcp_close_shutdown: invalid pcb", pcb != NULL);
if (rst_on_unacked_data && ((pcb->state == ESTABLISHED) || (pcb->state == CLOSE_WAIT))) {
if ((pcb->refused_data != NULL) || (pcb->rcv_wnd != TCP_WND_MAX(pcb))) {
/* ... */
LWIP_ASSERT("pcb->flags & TF_RXCLOSED", pcb->flags & TF_RXCLOSED);
/* ... */
tcp_rst(pcb, pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip,
pcb->local_port, pcb->remote_port);
tcp_pcb_purge(pcb);
TCP_RMV_ACTIVE(pcb);
if (tcp_input_pcb == pcb) {
tcp_trigger_input_pcb_close();
}if (tcp_input_pcb == pcb) { ... } else {
tcp_free(pcb);
}else { ... }
return ERR_OK;
}if ((pcb->refused_data != NULL) || (pcb->rcv_wnd != TCP_WND_MAX(pcb))) { ... }
}if (rst_on_unacked_data && ((pcb->state == ESTABLISHED) || (pcb->state == CLOSE_WAIT))) { ... }
/* ... */
switch (pcb->state) {
case CLOSED:
/* ... */
if (pcb->local_port != 0) {
TCP_RMV(&tcp_bound_pcbs, pcb);
}if (pcb->local_port != 0) { ... }
tcp_free(pcb);
break;case CLOSED:
case LISTEN:
tcp_listen_closed(pcb);
tcp_pcb_remove(&tcp_listen_pcbs.pcbs, pcb);
tcp_free_listen(pcb);
break;case LISTEN:
case SYN_SENT:
TCP_PCB_REMOVE_ACTIVE(pcb);
tcp_free(pcb);
MIB2_STATS_INC(mib2.tcpattemptfails);
break;case SYN_SENT:
default:
return tcp_close_shutdown_fin(pcb);default
}switch (pcb->state) { ... }
return ERR_OK;
}{ ... }
static err_t
tcp_close_shutdown_fin(struct tcp_pcb *pcb)
{
err_t err;
LWIP_ASSERT("pcb != NULL", pcb != NULL);
switch (pcb->state) {
case SYN_RCVD:
err = tcp_send_fin(pcb);
if (err == ERR_OK) {
tcp_backlog_accepted(pcb);
MIB2_STATS_INC(mib2.tcpattemptfails);
pcb->state = FIN_WAIT_1;
}if (err == ERR_OK) { ... }
break;case SYN_RCVD:
case ESTABLISHED:
err = tcp_send_fin(pcb);
if (err == ERR_OK) {
MIB2_STATS_INC(mib2.tcpestabresets);
pcb->state = FIN_WAIT_1;
}if (err == ERR_OK) { ... }
break;case ESTABLISHED:
case CLOSE_WAIT:
err = tcp_send_fin(pcb);
if (err == ERR_OK) {
MIB2_STATS_INC(mib2.tcpestabresets);
pcb->state = LAST_ACK;
}if (err == ERR_OK) { ... }
break;case CLOSE_WAIT:
default:
return ERR_OK;default
}switch (pcb->state) { ... }
if (err == ERR_OK) {
/* ... */
tcp_output(pcb);
}if (err == ERR_OK) { ... } else if (err == ERR_MEM) {
tcp_set_flags(pcb, TF_CLOSEPEND);
/* ... */
return ERR_OK;
}else if (err == ERR_MEM) { ... }
return err;
}{ ... }
/* ... */
err_t
tcp_close(struct tcp_pcb *pcb)
{
LWIP_ASSERT_CORE_LOCKED();
LWIP_ERROR("tcp_close: invalid pcb", pcb != NULL, return ERR_ARG);
LWIP_DEBUGF(TCP_DEBUG, ("tcp_close: closing in "));
tcp_debug_print_state(pcb->state);
if (pcb->state != LISTEN) {
tcp_set_flags(pcb, TF_RXCLOSED);
}if (pcb->state != LISTEN) { ... }
return tcp_close_shutdown(pcb, 1);
}{ ... }
/* ... */
err_t
tcp_shutdown(struct tcp_pcb *pcb, int shut_rx, int shut_tx)
{
LWIP_ASSERT_CORE_LOCKED();
LWIP_ERROR("tcp_shutdown: invalid pcb", pcb != NULL, return ERR_ARG);
if (pcb->state == LISTEN) {
return ERR_CONN;
}if (pcb->state == LISTEN) { ... }
if (shut_rx) {
tcp_set_flags(pcb, TF_RXCLOSED);
if (shut_tx) {
return tcp_close_shutdown(pcb, 1);
}if (shut_tx) { ... }
if (pcb->refused_data != NULL) {
pbuf_free(pcb->refused_data);
pcb->refused_data = NULL;
}if (pcb->refused_data != NULL) { ... }
}if (shut_rx) { ... }
if (shut_tx) {
/* ... */
switch (pcb->state) {
case SYN_RCVD:
case ESTABLISHED:
case CLOSE_WAIT:
return tcp_close_shutdown(pcb, (u8_t)shut_rx);case CLOSE_WAIT:
default:
/* ... */
return ERR_CONN;default
}switch (pcb->state) { ... }
}if (shut_tx) { ... }
return ERR_OK;
}{ ... }
/* ... */
void
tcp_abandon(struct tcp_pcb *pcb, int reset)
{
u32_t seqno, ackno;
#if LWIP_CALLBACK_API
tcp_err_fn errf;
#endif
void *errf_arg;
LWIP_ASSERT_CORE_LOCKED();
LWIP_ERROR("tcp_abandon: invalid pcb", pcb != NULL, return);
LWIP_ASSERT("don't call tcp_abort/tcp_abandon for listen-pcbs",
pcb->state != LISTEN);
/* ... */
if (pcb->state == TIME_WAIT) {
tcp_pcb_remove(&tcp_tw_pcbs, pcb);
tcp_free(pcb);
}if (pcb->state == TIME_WAIT) { ... } else {
int send_rst = 0;
u16_t local_port = 0;
enum tcp_state last_state;
seqno = pcb->snd_nxt;
ackno = pcb->rcv_nxt;
#if LWIP_CALLBACK_API
errf = pcb->errf;
#endif
errf_arg = pcb->callback_arg;
if (pcb->state == CLOSED) {
if (pcb->local_port != 0) {
TCP_RMV(&tcp_bound_pcbs, pcb);
}if (pcb->local_port != 0) { ... }
}if (pcb->state == CLOSED) { ... } else {
send_rst = reset;
local_port = pcb->local_port;
TCP_PCB_REMOVE_ACTIVE(pcb);
}else { ... }
if (pcb->unacked != NULL) {
tcp_segs_free(pcb->unacked);
}if (pcb->unacked != NULL) { ... }
if (pcb->unsent != NULL) {
tcp_segs_free(pcb->unsent);
}if (pcb->unsent != NULL) { ... }
#if TCP_QUEUE_OOSEQ
if (pcb->ooseq != NULL) {
tcp_segs_free(pcb->ooseq);
}if (pcb->ooseq != NULL) { ... }
/* ... */#endif
tcp_backlog_accepted(pcb);
if (send_rst) {
LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_abandon: sending RST\n"));
tcp_rst(pcb, seqno, ackno, &pcb->local_ip, &pcb->remote_ip, local_port, pcb->remote_port);
}if (send_rst) { ... }
last_state = pcb->state;
tcp_free(pcb);
TCP_EVENT_ERR(last_state, errf, errf_arg, ERR_ABRT);
}else { ... }
}{ ... }
/* ... */
void
tcp_abort(struct tcp_pcb *pcb)
{
tcp_abandon(pcb, 1);
}{ ... }
/* ... */
err_t
tcp_bind(struct tcp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port)
{
int i;
int max_pcb_list = NUM_TCP_PCB_LISTS;
struct tcp_pcb *cpcb;
#if LWIP_IPV6 && LWIP_IPV6_SCOPES
ip_addr_t zoned_ipaddr;
#endif
LWIP_ASSERT_CORE_LOCKED();
#if LWIP_IPV4
if (ipaddr == NULL) {
ipaddr = IP4_ADDR_ANY;
}if (ipaddr == NULL) { ... }
/* ... */#else
LWIP_ERROR("tcp_bind: invalid ipaddr", ipaddr != NULL, return ERR_ARG);
#endif
LWIP_ERROR("tcp_bind: invalid pcb", pcb != NULL, return ERR_ARG);
LWIP_ERROR("tcp_bind: can only bind in state CLOSED", pcb->state == CLOSED, return ERR_VAL);
#if SO_REUSE
/* ... */
if (ip_get_option(pcb, SOF_REUSEADDR)) {
max_pcb_list = NUM_TCP_PCB_LISTS_NO_TIME_WAIT;
}if (ip_get_option(pcb, SOF_REUSEADDR)) { ... }
/* ... */#endif
#if LWIP_IPV6 && LWIP_IPV6_SCOPES
/* ... */
if (IP_IS_V6(ipaddr) && ip6_addr_lacks_zone(ip_2_ip6(ipaddr), IP6_UNICAST)) {
ip_addr_copy(zoned_ipaddr, *ipaddr);
ip6_addr_select_zone(ip_2_ip6(&zoned_ipaddr), ip_2_ip6(&zoned_ipaddr));
ipaddr = &zoned_ipaddr;
}if (IP_IS_V6(ipaddr) && ip6_addr_lacks_zone(ip_2_ip6(ipaddr), IP6_UNICAST)) { ... }
/* ... */#endif
if (port == 0) {
port = tcp_new_port();
if (port == 0) {
return ERR_BUF;
}if (port == 0) { ... }
}if (port == 0) { ... } else {
for (i = 0; i < max_pcb_list; i++) {
for (cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) {
if (cpcb->local_port == port) {
#if SO_REUSE
/* ... */
if (!ip_get_option(pcb, SOF_REUSEADDR) ||
!ip_get_option(cpcb, SOF_REUSEADDR))/* ... */
#endif
{
if ((IP_IS_V6(ipaddr) == IP_IS_V6_VAL(cpcb->local_ip)) &&
(ip_addr_isany(&cpcb->local_ip) ||
ip_addr_isany(ipaddr) ||
ip_addr_cmp(&cpcb->local_ip, ipaddr))) {
return ERR_USE;
}if ((IP_IS_V6(ipaddr) == IP_IS_V6_VAL(cpcb->local_ip)) && (ip_addr_isany(&cpcb->local_ip) || ip_addr_isany(ipaddr) || ip_addr_cmp(&cpcb->local_ip, ipaddr))) { ... }
...}
}if (cpcb->local_port == port) { ... }
}for (cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) { ... }
}for (i = 0; i < max_pcb_list; i++) { ... }
}else { ... }
if (!ip_addr_isany(ipaddr)
#if LWIP_IPV4 && LWIP_IPV6
|| (IP_GET_TYPE(ipaddr) != IP_GET_TYPE(&pcb->local_ip))
#endif
) {
ip_addr_set(&pcb->local_ip, ipaddr);
}if (!ip_addr_isany(ipaddr) #if LWIP_IPV4 && LWIP_IPV6 || (IP_GET_TYPE(ipaddr) != IP_GET_TYPE(&pcb->local_ip)) #endif /* LWIP_IPV4 && LWIP_IPV6 */) { ... }
pcb->local_port = port;
TCP_REG(&tcp_bound_pcbs, pcb);
LWIP_DEBUGF(TCP_DEBUG, ("tcp_bind: bind to port %"U16_F"\n", port));
return ERR_OK;
}{ ... }
/* ... */
void
tcp_bind_netif(struct tcp_pcb *pcb, const struct netif *netif)
{
LWIP_ASSERT_CORE_LOCKED();
if (netif != NULL) {
pcb->netif_idx = netif_get_index(netif);
}if (netif != NULL) { ... } else {
pcb->netif_idx = NETIF_NO_INDEX;
}else { ... }
}{ ... }
#if LWIP_CALLBACK_API
/* ... */
static err_t
tcp_accept_null(void *arg, struct tcp_pcb *pcb, err_t err)
{
LWIP_UNUSED_ARG(arg);
LWIP_UNUSED_ARG(err);
LWIP_ASSERT("tcp_accept_null: invalid pcb", pcb != NULL);
tcp_abort(pcb);
return ERR_ABRT;
}{ ... }
#endif/* ... */
/* ... */
struct tcp_pcb *
tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog)
{
LWIP_ASSERT_CORE_LOCKED();
return tcp_listen_with_backlog_and_err(pcb, backlog, NULL);
}{ ... }
/* ... */
struct tcp_pcb *
tcp_listen_with_backlog_and_err(struct tcp_pcb *pcb, u8_t backlog, err_t *err)
{
struct tcp_pcb_listen *lpcb = NULL;
err_t res;
LWIP_UNUSED_ARG(backlog);
LWIP_ASSERT_CORE_LOCKED();
LWIP_ERROR("tcp_listen_with_backlog_and_err: invalid pcb", pcb != NULL, res = ERR_ARG; goto done);
LWIP_ERROR("tcp_listen_with_backlog_and_err: pcb already connected", pcb->state == CLOSED, res = ERR_CLSD; goto done);
if (pcb->state == LISTEN) {
lpcb = (struct tcp_pcb_listen *)pcb;
res = ERR_ALREADY;
goto done;
}if (pcb->state == LISTEN) { ... }
#if SO_REUSE
if (ip_get_option(pcb, SOF_REUSEADDR)) {
/* ... */
for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
if ((lpcb->local_port == pcb->local_port) &&
ip_addr_cmp(&lpcb->local_ip, &pcb->local_ip)) {
lpcb = NULL;
res = ERR_USE;
goto done;
}if ((lpcb->local_port == pcb->local_port) && ip_addr_cmp(&lpcb->local_ip, &pcb->local_ip)) { ... }
}for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) { ... }
}if (ip_get_option(pcb, SOF_REUSEADDR)) { ... }
/* ... */#endif
lpcb = (struct tcp_pcb_listen *)memp_malloc(MEMP_TCP_PCB_LISTEN);
if (lpcb == NULL) {
res = ERR_MEM;
goto done;
}if (lpcb == NULL) { ... }
lpcb->callback_arg = pcb->callback_arg;
lpcb->local_port = pcb->local_port;
lpcb->state = LISTEN;
lpcb->prio = pcb->prio;
lpcb->so_options = pcb->so_options;
lpcb->netif_idx = NETIF_NO_INDEX;
lpcb->ttl = pcb->ttl;
lpcb->tos = pcb->tos;
#if LWIP_IPV4 && LWIP_IPV6
IP_SET_TYPE_VAL(lpcb->remote_ip, pcb->local_ip.type);
#endif
ip_addr_copy(lpcb->local_ip, pcb->local_ip);
if (pcb->local_port != 0) {
TCP_RMV(&tcp_bound_pcbs, pcb);
}if (pcb->local_port != 0) { ... }
#if LWIP_TCP_PCB_NUM_EXT_ARGS
memcpy(&lpcb->ext_args, &pcb->ext_args, sizeof(pcb->ext_args));/* ... */
#endif
tcp_free(pcb);
#if LWIP_CALLBACK_API
lpcb->accept = tcp_accept_null;
#endif
#if TCP_LISTEN_BACKLOG
lpcb->accepts_pending = 0;
tcp_backlog_set(lpcb, backlog);/* ... */
#endif
TCP_REG(&tcp_listen_pcbs.pcbs, (struct tcp_pcb *)lpcb);
res = ERR_OK;
done:
if (err != NULL) {
*err = res;
}if (err != NULL) { ... }
return (struct tcp_pcb *)lpcb;
}{ ... }
/* ... */
u32_t
tcp_update_rcv_ann_wnd(struct tcp_pcb *pcb)
{
u32_t new_right_edge;
LWIP_ASSERT("tcp_update_rcv_ann_wnd: invalid pcb", pcb != NULL);
new_right_edge = pcb->rcv_nxt + pcb->rcv_wnd;
if (TCP_SEQ_GEQ(new_right_edge, pcb->rcv_ann_right_edge + LWIP_MIN((TCP_WND / 2), pcb->mss))) {
pcb->rcv_ann_wnd = pcb->rcv_wnd;
return new_right_edge - pcb->rcv_ann_right_edge;
}if (TCP_SEQ_GEQ(new_right_edge, pcb->rcv_ann_right_edge + LWIP_MIN((TCP_WND / 2), pcb->mss))) { ... } else {
if (TCP_SEQ_GT(pcb->rcv_nxt, pcb->rcv_ann_right_edge)) {
/* ... */
pcb->rcv_ann_wnd = 0;
}if (TCP_SEQ_GT(pcb->rcv_nxt, pcb->rcv_ann_right_edge)) { ... } else {
u32_t new_rcv_ann_wnd = pcb->rcv_ann_right_edge - pcb->rcv_nxt;
#if !LWIP_WND_SCALE
LWIP_ASSERT("new_rcv_ann_wnd <= 0xffff", new_rcv_ann_wnd <= 0xffff);
#endif
pcb->rcv_ann_wnd = (tcpwnd_size_t)new_rcv_ann_wnd;
}else { ... }
return 0;
}else { ... }
}{ ... }
/* ... */
void
tcp_recved(struct tcp_pcb *pcb, u16_t len)
{
u32_t wnd_inflation;
tcpwnd_size_t rcv_wnd;
LWIP_ASSERT_CORE_LOCKED();
LWIP_ERROR("tcp_recved: invalid pcb", pcb != NULL, return);
LWIP_ASSERT("don't call tcp_recved for listen-pcbs",
pcb->state != LISTEN);
rcv_wnd = (tcpwnd_size_t)(pcb->rcv_wnd + len);
if ((rcv_wnd > TCP_WND_MAX(pcb)) || (rcv_wnd < pcb->rcv_wnd)) {
LWIP_DEBUGF(TCP_DEBUG, ("tcp_recved: window got too big or tcpwnd_size_t overflow\n"));
pcb->rcv_wnd = TCP_WND_MAX(pcb);
}if ((rcv_wnd > TCP_WND_MAX(pcb)) || (rcv_wnd < pcb->rcv_wnd)) { ... } else {
pcb->rcv_wnd = rcv_wnd;
}else { ... }
wnd_inflation = tcp_update_rcv_ann_wnd(pcb);
/* ... */
if (wnd_inflation >= TCP_WND_UPDATE_THRESHOLD) {
tcp_ack_now(pcb);
tcp_output(pcb);
}if (wnd_inflation >= TCP_WND_UPDATE_THRESHOLD) { ... }
LWIP_DEBUGF(TCP_DEBUG, ("tcp_recved: received %"U16_F" bytes, wnd %"TCPWNDSIZE_F" (%"TCPWNDSIZE_F").\n",
len, pcb->rcv_wnd, (u16_t)(TCP_WND_MAX(pcb) - pcb->rcv_wnd)));
}{ ... }
/* ... */
static u16_t
tcp_new_port(void)
{
u8_t i;
u16_t n = 0;
struct tcp_pcb *pcb;
again:
tcp_port++;
if (tcp_port == TCP_LOCAL_PORT_RANGE_END) {
tcp_port = TCP_LOCAL_PORT_RANGE_START;
}if (tcp_port == TCP_LOCAL_PORT_RANGE_END) { ... }
for (i = 0; i < NUM_TCP_PCB_LISTS; i++) {
for (pcb = *tcp_pcb_lists[i]; pcb != NULL; pcb = pcb->next) {
if (pcb->local_port == tcp_port) {
n++;
if (n > (TCP_LOCAL_PORT_RANGE_END - TCP_LOCAL_PORT_RANGE_START)) {
return 0;
}if (n > (TCP_LOCAL_PORT_RANGE_END - TCP_LOCAL_PORT_RANGE_START)) { ... }
goto again;
}if (pcb->local_port == tcp_port) { ... }
}for (pcb = *tcp_pcb_lists[i]; pcb != NULL; pcb = pcb->next) { ... }
}for (i = 0; i < NUM_TCP_PCB_LISTS; i++) { ... }
return tcp_port;
}{ ... }
/* ... */
err_t
tcp_connect(struct tcp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port,
tcp_connected_fn connected)
{
struct netif *netif = NULL;
err_t ret;
u32_t iss;
u16_t old_local_port;
LWIP_ASSERT_CORE_LOCKED();
LWIP_ERROR("tcp_connect: invalid pcb", pcb != NULL, return ERR_ARG);
LWIP_ERROR("tcp_connect: invalid ipaddr", ipaddr != NULL, return ERR_ARG);
LWIP_ERROR("tcp_connect: can only connect from state CLOSED", pcb->state == CLOSED, return ERR_ISCONN);
LWIP_DEBUGF(TCP_DEBUG, ("tcp_connect to port %"U16_F"\n", port));
ip_addr_set(&pcb->remote_ip, ipaddr);
pcb->remote_port = port;
if (pcb->netif_idx != NETIF_NO_INDEX) {
netif = netif_get_by_index(pcb->netif_idx);
}if (pcb->netif_idx != NETIF_NO_INDEX) { ... } else {
netif = ip_route(&pcb->local_ip, &pcb->remote_ip);
}else { ... }
if (netif == NULL) {
return ERR_RTE;
}if (netif == NULL) { ... }
if (ip_addr_isany(&pcb->local_ip)) {
const ip_addr_t *local_ip = ip_netif_get_local_ip(netif, ipaddr);
if (local_ip == NULL) {
return ERR_RTE;
}if (local_ip == NULL) { ... }
ip_addr_copy(pcb->local_ip, *local_ip);
}if (ip_addr_isany(&pcb->local_ip)) { ... }
#if LWIP_IPV6 && LWIP_IPV6_SCOPES
/* ... */
if (IP_IS_V6(&pcb->remote_ip) &&
ip6_addr_lacks_zone(ip_2_ip6(&pcb->remote_ip), IP6_UNICAST)) {
ip6_addr_assign_zone(ip_2_ip6(&pcb->remote_ip), IP6_UNICAST, netif);
}if (IP_IS_V6(&pcb->remote_ip) && ip6_addr_lacks_zone(ip_2_ip6(&pcb->remote_ip), IP6_UNICAST)) { ... }
/* ... */#endif
old_local_port = pcb->local_port;
if (pcb->local_port == 0) {
pcb->local_port = tcp_new_port();
if (pcb->local_port == 0) {
return ERR_BUF;
}if (pcb->local_port == 0) { ... }
}if (pcb->local_port == 0) { ... } else {
#if SO_REUSE
if (ip_get_option(pcb, SOF_REUSEADDR)) {
/* ... */
struct tcp_pcb *cpcb;
int i;
for (i = 2; i < NUM_TCP_PCB_LISTS; i++) {
for (cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) {
if ((cpcb->local_port == pcb->local_port) &&
(cpcb->remote_port == port) &&
ip_addr_cmp(&cpcb->local_ip, &pcb->local_ip) &&
ip_addr_cmp(&cpcb->remote_ip, ipaddr)) {
return ERR_USE;
}if ((cpcb->local_port == pcb->local_port) && (cpcb->remote_port == port) && ip_addr_cmp(&cpcb->local_ip, &pcb->local_ip) && ip_addr_cmp(&cpcb->remote_ip, ipaddr)) { ... }
}for (cpcb = *tcp_pcb_lists[i]; cpcb != NULL; cpcb = cpcb->next) { ... }
}for (i = 2; i < NUM_TCP_PCB_LISTS; i++) { ... }
}if (ip_get_option(pcb, SOF_REUSEADDR)) { ... }
/* ... */#endif
}else { ... }
iss = tcp_next_iss(pcb);
pcb->rcv_nxt = 0;
pcb->snd_nxt = iss;
pcb->lastack = iss - 1;
pcb->snd_wl2 = iss - 1;
pcb->snd_lbb = iss - 1;
/* ... */
pcb->rcv_wnd = pcb->rcv_ann_wnd = TCPWND_MIN16(TCP_WND);
pcb->rcv_ann_right_edge = pcb->rcv_nxt;
pcb->snd_wnd = TCP_WND;
/* ... */
pcb->mss = INITIAL_MSS;
#if TCP_CALCULATE_EFF_SEND_MSS
pcb->mss = tcp_eff_send_mss_netif(pcb->mss, netif, &pcb->remote_ip);
#endif
pcb->cwnd = 1;
#if LWIP_CALLBACK_API
pcb->connected = connected;
#else
LWIP_UNUSED_ARG(connected);
#endif
ret = tcp_enqueue_flags(pcb, TCP_SYN);
if (ret == ERR_OK) {
pcb->state = SYN_SENT;
if (old_local_port != 0) {
TCP_RMV(&tcp_bound_pcbs, pcb);
}if (old_local_port != 0) { ... }
TCP_REG_ACTIVE(pcb);
MIB2_STATS_INC(mib2.tcpactiveopens);
tcp_output(pcb);
}if (ret == ERR_OK) { ... }
return ret;
}{ ... }
/* ... */
void
tcp_slowtmr(void)
{
struct tcp_pcb *pcb, *prev;
tcpwnd_size_t eff_wnd;
u8_t pcb_remove;
u8_t pcb_reset;
err_t err;
err = ERR_OK;
++tcp_ticks;
++tcp_timer_ctr;
tcp_slowtmr_start:
prev = NULL;
pcb = tcp_active_pcbs;
if (pcb == NULL) {
LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: no active pcbs\n"));
}if (pcb == NULL) { ... }
while (pcb != NULL) {
LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: processing active pcb\n"));
LWIP_ASSERT("tcp_slowtmr: active pcb->state != CLOSED\n", pcb->state != CLOSED);
LWIP_ASSERT("tcp_slowtmr: active pcb->state != LISTEN\n", pcb->state != LISTEN);
LWIP_ASSERT("tcp_slowtmr: active pcb->state != TIME-WAIT\n", pcb->state != TIME_WAIT);
if (pcb->last_timer == tcp_timer_ctr) {
prev = pcb;
pcb = pcb->next;
continue;
}if (pcb->last_timer == tcp_timer_ctr) { ... }
pcb->last_timer = tcp_timer_ctr;
pcb_remove = 0;
pcb_reset = 0;
if (pcb->state == SYN_SENT && pcb->nrtx >= TCP_SYNMAXRTX) {
++pcb_remove;
LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max SYN retries reached\n"));
}if (pcb->state == SYN_SENT && pcb->nrtx >= TCP_SYNMAXRTX) { ... } else if (pcb->nrtx >= TCP_MAXRTX) {
++pcb_remove;
LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: max DATA retries reached\n"));
}else if (pcb->nrtx >= TCP_MAXRTX) { ... } else {
if (pcb->persist_backoff > 0) {
LWIP_ASSERT("tcp_slowtimr: persist ticking with in-flight data", pcb->unacked == NULL);
LWIP_ASSERT("tcp_slowtimr: persist ticking with empty send buffer", pcb->unsent != NULL);
if (pcb->persist_probe >= TCP_MAXRTX) {
++pcb_remove;
}if (pcb->persist_probe >= TCP_MAXRTX) { ... } else {
u8_t backoff_cnt = tcp_persist_backoff[pcb->persist_backoff - 1];
if (pcb->persist_cnt < backoff_cnt) {
pcb->persist_cnt++;
}if (pcb->persist_cnt < backoff_cnt) { ... }
if (pcb->persist_cnt >= backoff_cnt) {
int next_slot = 1;
if (pcb->snd_wnd == 0) {
if (tcp_zero_window_probe(pcb) != ERR_OK) {
next_slot = 0;
}if (tcp_zero_window_probe(pcb) != ERR_OK) { ... }
}if (pcb->snd_wnd == 0) { ... } else {
if (tcp_split_unsent_seg(pcb, (u16_t)pcb->snd_wnd) == ERR_OK) {
if (tcp_output(pcb) == ERR_OK) {
next_slot = 0;
}if (tcp_output(pcb) == ERR_OK) { ... }
}if (tcp_split_unsent_seg(pcb, (u16_t)pcb->snd_wnd) == ERR_OK) { ... }
}else { ... }
if (next_slot) {
pcb->persist_cnt = 0;
if (pcb->persist_backoff < sizeof(tcp_persist_backoff)) {
pcb->persist_backoff++;
}if (pcb->persist_backoff < sizeof(tcp_persist_backoff)) { ... }
}if (next_slot) { ... }
}if (pcb->persist_cnt >= backoff_cnt) { ... }
}else { ... }
}if (pcb->persist_backoff > 0) { ... } else {
if ((pcb->rtime >= 0) && (pcb->rtime < 0x7FFF)) {
++pcb->rtime;
}if ((pcb->rtime >= 0) && (pcb->rtime < 0x7FFF)) { ... }
if (pcb->rtime >= pcb->rto) {
LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_slowtmr: rtime %"S16_F
" pcb->rto %"S16_F"\n",
pcb->rtime, pcb->rto));
/* ... */
if ((tcp_rexmit_rto_prepare(pcb) == ERR_OK) || ((pcb->unacked == NULL) && (pcb->unsent != NULL))) {
/* ... */
if (pcb->state != SYN_SENT) {
u8_t backoff_idx = LWIP_MIN(pcb->nrtx, sizeof(tcp_backoff) - 1);
int calc_rto = ((pcb->sa >> 3) + pcb->sv) << tcp_backoff[backoff_idx];
pcb->rto = (s16_t)LWIP_MIN(calc_rto, 0x7FFF);
}if (pcb->state != SYN_SENT) { ... }
pcb->rtime = 0;
eff_wnd = LWIP_MIN(pcb->cwnd, pcb->snd_wnd);
pcb->ssthresh = eff_wnd >> 1;
if (pcb->ssthresh < (tcpwnd_size_t)(pcb->mss << 1)) {
pcb->ssthresh = (tcpwnd_size_t)(pcb->mss << 1);
}if (pcb->ssthresh < (tcpwnd_size_t)(pcb->mss << 1)) { ... }
pcb->cwnd = pcb->mss;
LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: cwnd %"TCPWNDSIZE_F
" ssthresh %"TCPWNDSIZE_F"\n",
pcb->cwnd, pcb->ssthresh));
pcb->bytes_acked = 0;
/* ... */
tcp_rexmit_rto_commit(pcb);
}if ((tcp_rexmit_rto_prepare(pcb) == ERR_OK) || ((pcb->unacked == NULL) && (pcb->unsent != NULL))) { ... }
}if (pcb->rtime >= pcb->rto) { ... }
}else { ... }
}else { ... }
if (pcb->state == FIN_WAIT_2) {
if (pcb->flags & TF_RXCLOSED) {
/* ... */
if ((u32_t)(tcp_ticks - pcb->tmr) >
TCP_FIN_WAIT_TIMEOUT / TCP_SLOW_INTERVAL) {
++pcb_remove;
LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in FIN-WAIT-2\n"));
}if ((u32_t)(tcp_ticks - pcb->tmr) > TCP_FIN_WAIT_TIMEOUT / TCP_SLOW_INTERVAL) { ... }
}if (pcb->flags & TF_RXCLOSED) { ... }
}if (pcb->state == FIN_WAIT_2) { ... }
if (ip_get_option(pcb, SOF_KEEPALIVE) &&
((pcb->state == ESTABLISHED) ||
(pcb->state == CLOSE_WAIT))) {
if ((u32_t)(tcp_ticks - pcb->tmr) >
(pcb->keep_idle + TCP_KEEP_DUR(pcb)) / TCP_SLOW_INTERVAL) {
LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: KEEPALIVE timeout. Aborting connection to "));
ip_addr_debug_print_val(TCP_DEBUG, pcb->remote_ip);
LWIP_DEBUGF(TCP_DEBUG, ("\n"));
++pcb_remove;
++pcb_reset;
}if ((u32_t)(tcp_ticks - pcb->tmr) > (pcb->keep_idle + TCP_KEEP_DUR(pcb)) / TCP_SLOW_INTERVAL) { ... } else if ((u32_t)(tcp_ticks - pcb->tmr) >
(pcb->keep_idle + pcb->keep_cnt_sent * TCP_KEEP_INTVL(pcb))
/ TCP_SLOW_INTERVAL) {
err = tcp_keepalive(pcb);
if (err == ERR_OK) {
pcb->keep_cnt_sent++;
}if (err == ERR_OK) { ... }
}else if ((u32_t)(tcp_ticks - pcb->tmr) > (pcb->keep_idle + pcb->keep_cnt_sent * TCP_KEEP_INTVL(pcb)) / TCP_SLOW_INTERVAL) { ... }
}if (ip_get_option(pcb, SOF_KEEPALIVE) && ((pcb->state == ESTABLISHED) || (pcb->state == CLOSE_WAIT))) { ... }
/* ... */
#if TCP_QUEUE_OOSEQ
if (pcb->ooseq != NULL &&
(tcp_ticks - pcb->tmr >= (u32_t)pcb->rto * TCP_OOSEQ_TIMEOUT)) {
LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_slowtmr: dropping OOSEQ queued data\n"));
tcp_free_ooseq(pcb);
}if (pcb->ooseq != NULL && (tcp_ticks - pcb->tmr >= (u32_t)pcb->rto * TCP_OOSEQ_TIMEOUT)) { ... }
/* ... */#endif
if (pcb->state == SYN_RCVD) {
if ((u32_t)(tcp_ticks - pcb->tmr) >
TCP_SYN_RCVD_TIMEOUT / TCP_SLOW_INTERVAL) {
++pcb_remove;
LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in SYN-RCVD\n"));
}if ((u32_t)(tcp_ticks - pcb->tmr) > TCP_SYN_RCVD_TIMEOUT / TCP_SLOW_INTERVAL) { ... }
}if (pcb->state == SYN_RCVD) { ... }
if (pcb->state == LAST_ACK) {
if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) {
++pcb_remove;
LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: removing pcb stuck in LAST-ACK\n"));
}if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) { ... }
}if (pcb->state == LAST_ACK) { ... }
if (pcb_remove) {
struct tcp_pcb *pcb2;
#if LWIP_CALLBACK_API
tcp_err_fn err_fn = pcb->errf;
#endif
void *err_arg;
enum tcp_state last_state;
tcp_pcb_purge(pcb);
if (prev != NULL) {
LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_active_pcbs", pcb != tcp_active_pcbs);
prev->next = pcb->next;
}if (prev != NULL) { ... } else {
LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_active_pcbs", tcp_active_pcbs == pcb);
tcp_active_pcbs = pcb->next;
}else { ... }
if (pcb_reset) {
tcp_rst(pcb, pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip,
pcb->local_port, pcb->remote_port);
}if (pcb_reset) { ... }
err_arg = pcb->callback_arg;
last_state = pcb->state;
pcb2 = pcb;
pcb = pcb->next;
tcp_free(pcb2);
tcp_active_pcbs_changed = 0;
TCP_EVENT_ERR(last_state, err_fn, err_arg, ERR_ABRT);
if (tcp_active_pcbs_changed) {
goto tcp_slowtmr_start;
}if (tcp_active_pcbs_changed) { ... }
}if (pcb_remove) { ... } else {
prev = pcb;
pcb = pcb->next;
++prev->polltmr;
if (prev->polltmr >= prev->pollinterval) {
prev->polltmr = 0;
LWIP_DEBUGF(TCP_DEBUG, ("tcp_slowtmr: polling application\n"));
tcp_active_pcbs_changed = 0;
TCP_EVENT_POLL(prev, err);
if (tcp_active_pcbs_changed) {
goto tcp_slowtmr_start;
}if (tcp_active_pcbs_changed) { ... }
if (err == ERR_OK) {
tcp_output(prev);
}if (err == ERR_OK) { ... }
}if (prev->polltmr >= prev->pollinterval) { ... }
}else { ... }
}while (pcb != NULL) { ... }
prev = NULL;
pcb = tcp_tw_pcbs;
while (pcb != NULL) {
LWIP_ASSERT("tcp_slowtmr: TIME-WAIT pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
pcb_remove = 0;
if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) {
++pcb_remove;
}if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) { ... }
if (pcb_remove) {
struct tcp_pcb *pcb2;
tcp_pcb_purge(pcb);
if (prev != NULL) {
LWIP_ASSERT("tcp_slowtmr: middle tcp != tcp_tw_pcbs", pcb != tcp_tw_pcbs);
prev->next = pcb->next;
}if (prev != NULL) { ... } else {
LWIP_ASSERT("tcp_slowtmr: first pcb == tcp_tw_pcbs", tcp_tw_pcbs == pcb);
tcp_tw_pcbs = pcb->next;
}else { ... }
pcb2 = pcb;
pcb = pcb->next;
tcp_free(pcb2);
}if (pcb_remove) { ... } else {
prev = pcb;
pcb = pcb->next;
}else { ... }
}while (pcb != NULL) { ... }
}{ ... }
/* ... */
void
tcp_fasttmr(void)
{
struct tcp_pcb *pcb;
++tcp_timer_ctr;
tcp_fasttmr_start:
pcb = tcp_active_pcbs;
while (pcb != NULL) {
if (pcb->last_timer != tcp_timer_ctr) {
struct tcp_pcb *next;
pcb->last_timer = tcp_timer_ctr;
if (pcb->flags & TF_ACK_DELAY) {
LWIP_DEBUGF(TCP_DEBUG, ("tcp_fasttmr: delayed ACK\n"));
tcp_ack_now(pcb);
tcp_output(pcb);
tcp_clear_flags(pcb, TF_ACK_DELAY | TF_ACK_NOW);
}if (pcb->flags & TF_ACK_DELAY) { ... }
if (pcb->flags & TF_CLOSEPEND) {
LWIP_DEBUGF(TCP_DEBUG, ("tcp_fasttmr: pending FIN\n"));
tcp_clear_flags(pcb, TF_CLOSEPEND);
tcp_close_shutdown_fin(pcb);
}if (pcb->flags & TF_CLOSEPEND) { ... }
next = pcb->next;
if (pcb->refused_data != NULL) {
tcp_active_pcbs_changed = 0;
tcp_process_refused_data(pcb);
if (tcp_active_pcbs_changed) {
goto tcp_fasttmr_start;
}if (tcp_active_pcbs_changed) { ... }
}if (pcb->refused_data != NULL) { ... }
pcb = next;
}if (pcb->last_timer != tcp_timer_ctr) { ... } else {
pcb = pcb->next;
}else { ... }
}while (pcb != NULL) { ... }
}{ ... }
void
tcp_txnow(void)
{
struct tcp_pcb *pcb;
for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
if (pcb->flags & TF_NAGLEMEMERR) {
tcp_output(pcb);
}if (pcb->flags & TF_NAGLEMEMERR) { ... }
}for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) { ... }
}{ ... }
err_t
tcp_process_refused_data(struct tcp_pcb *pcb)
{
#if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
struct pbuf *rest;
#endif
LWIP_ERROR("tcp_process_refused_data: invalid pcb", pcb != NULL, return ERR_ARG);
#if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
while (pcb->refused_data != NULL)
#endif
{
err_t err;
u8_t refused_flags = pcb->refused_data->flags;
/* ... */
struct pbuf *refused_data = pcb->refused_data;
#if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
pbuf_split_64k(refused_data, &rest);
pcb->refused_data = rest;/* ... */
#else
pcb->refused_data = NULL;
#endif
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: notify kept packet\n"));
TCP_EVENT_RECV(pcb, refused_data, ERR_OK, err);
if (err == ERR_OK) {
if ((refused_flags & PBUF_FLAG_TCP_FIN)
#if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
&& (rest == NULL)
#endif
) {
/* ... */
if (pcb->rcv_wnd != TCP_WND_MAX(pcb)) {
pcb->rcv_wnd++;
}if (pcb->rcv_wnd != TCP_WND_MAX(pcb)) { ... }
TCP_EVENT_CLOSED(pcb, err);
if (err == ERR_ABRT) {
return ERR_ABRT;
}if (err == ERR_ABRT) { ... }
}if ((refused_flags & PBUF_FLAG_TCP_FIN) #if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE && (rest == NULL) #endif /* TCP_QUEUE_OOSEQ && LWIP_WND_SCALE */) { ... }
}if (err == ERR_OK) { ... } else if (err == ERR_ABRT) {
/* ... */
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: drop incoming packets, because pcb is \"full\"\n"));
return ERR_ABRT;
}else if (err == ERR_ABRT) { ... } else {
#if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
if (rest != NULL) {
pbuf_cat(refused_data, rest);
}if (rest != NULL) { ... }
/* ... */#endif
pcb->refused_data = refused_data;
return ERR_INPROGRESS;
}else { ... }
...}
return ERR_OK;
}{ ... }
/* ... */
void
tcp_segs_free(struct tcp_seg *seg)
{
while (seg != NULL) {
struct tcp_seg *next = seg->next;
tcp_seg_free(seg);
seg = next;
}while (seg != NULL) { ... }
}{ ... }
/* ... */
void
tcp_seg_free(struct tcp_seg *seg)
{
if (seg != NULL) {
if (seg->p != NULL) {
pbuf_free(seg->p);
#if TCP_DEBUG
seg->p = NULL;
#endif
}if (seg->p != NULL) { ... }
memp_free(MEMP_TCP_SEG, seg);
}if (seg != NULL) { ... }
}{ ... }
/* ... */
void
tcp_setprio(struct tcp_pcb *pcb, u8_t prio)
{
LWIP_ASSERT_CORE_LOCKED();
LWIP_ERROR("tcp_setprio: invalid pcb", pcb != NULL, return);
pcb->prio = prio;
}{ ... }
#if TCP_QUEUE_OOSEQ
/* ... */
struct tcp_seg *
tcp_seg_copy(struct tcp_seg *seg)
{
struct tcp_seg *cseg;
LWIP_ASSERT("tcp_seg_copy: invalid seg", seg != NULL);
cseg = (struct tcp_seg *)memp_malloc(MEMP_TCP_SEG);
if (cseg == NULL) {
return NULL;
}if (cseg == NULL) { ... }
SMEMCPY((u8_t *)cseg, (const u8_t *)seg, sizeof(struct tcp_seg));
pbuf_ref(cseg->p);
return cseg;
}tcp_seg_copy (struct tcp_seg *seg) { ... }
/* ... */#endif
#if LWIP_CALLBACK_API
/* ... */
err_t
tcp_recv_null(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
{
LWIP_UNUSED_ARG(arg);
LWIP_ERROR("tcp_recv_null: invalid pcb", pcb != NULL, return ERR_ARG);
if (p != NULL) {
tcp_recved(pcb, p->tot_len);
pbuf_free(p);
}if (p != NULL) { ... } else if (err == ERR_OK) {
return tcp_close(pcb);
}else if (err == ERR_OK) { ... }
return ERR_OK;
}{ ... }
#endif/* ... */
/* ... */
static void
tcp_kill_prio(u8_t prio)
{
struct tcp_pcb *pcb, *inactive;
u32_t inactivity;
u8_t mprio;
mprio = LWIP_MIN(TCP_PRIO_MAX, prio);
/* ... */
if (mprio == 0) {
return;
}if (mprio == 0) { ... }
/* ... */
mprio--;
inactivity = 0;
inactive = NULL;
for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
if ((pcb->prio < mprio) ||
((pcb->prio == mprio) && ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity))) {
inactivity = tcp_ticks - pcb->tmr;
inactive = pcb;
mprio = pcb->prio;
}if ((pcb->prio < mprio) || /* longer inactivity is also a kill candidate */ ((pcb->prio == mprio) && ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity))) { ... }
}for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) { ... }
if (inactive != NULL) {
LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_prio: killing oldest PCB %p (%"S32_F")\n",
(void *)inactive, inactivity));
tcp_abort(inactive);
}if (inactive != NULL) { ... }
}{ ... }
/* ... */
static void
tcp_kill_state(enum tcp_state state)
{
struct tcp_pcb *pcb, *inactive;
u32_t inactivity;
LWIP_ASSERT("invalid state", (state == CLOSING) || (state == LAST_ACK));
inactivity = 0;
inactive = NULL;
/* ... */
for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
if (pcb->state == state) {
if ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
inactivity = tcp_ticks - pcb->tmr;
inactive = pcb;
}if ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity) { ... }
}if (pcb->state == state) { ... }
}for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) { ... }
if (inactive != NULL) {
LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_closing: killing oldest %s PCB %p (%"S32_F")\n",
tcp_state_str[state], (void *)inactive, inactivity));
tcp_abandon(inactive, 0);
}if (inactive != NULL) { ... }
}{ ... }
/* ... */
static void
tcp_kill_timewait(void)
{
struct tcp_pcb *pcb, *inactive;
u32_t inactivity;
inactivity = 0;
inactive = NULL;
for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
if ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity) {
inactivity = tcp_ticks - pcb->tmr;
inactive = pcb;
}if ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity) { ... }
}for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) { ... }
if (inactive != NULL) {
LWIP_DEBUGF(TCP_DEBUG, ("tcp_kill_timewait: killing oldest TIME-WAIT PCB %p (%"S32_F")\n",
(void *)inactive, inactivity));
tcp_abort(inactive);
}if (inactive != NULL) { ... }
}{ ... }
/* ... */
static void
tcp_handle_closepend(void)
{
struct tcp_pcb *pcb = tcp_active_pcbs;
while (pcb != NULL) {
struct tcp_pcb *next = pcb->next;
if (pcb->flags & TF_CLOSEPEND) {
LWIP_DEBUGF(TCP_DEBUG, ("tcp_handle_closepend: pending FIN\n"));
tcp_clear_flags(pcb, TF_CLOSEPEND);
tcp_close_shutdown_fin(pcb);
}if (pcb->flags & TF_CLOSEPEND) { ... }
pcb = next;
}while (pcb != NULL) { ... }
}{ ... }
/* ... */
struct tcp_pcb *
tcp_alloc(u8_t prio)
{
struct tcp_pcb *pcb;
LWIP_ASSERT_CORE_LOCKED();
pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
if (pcb == NULL) {
tcp_handle_closepend();
LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest TIME-WAIT connection\n"));
tcp_kill_timewait();
pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
if (pcb == NULL) {
LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest LAST-ACK connection\n"));
tcp_kill_state(LAST_ACK);
pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
if (pcb == NULL) {
LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest CLOSING connection\n"));
tcp_kill_state(CLOSING);
pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
if (pcb == NULL) {
LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing oldest connection with prio lower than %d\n", prio));
tcp_kill_prio(prio);
pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
if (pcb != NULL) {
MEMP_STATS_DEC(err, MEMP_TCP_PCB);
}if (pcb != NULL) { ... }
}if (pcb == NULL) { ... }
if (pcb != NULL) {
MEMP_STATS_DEC(err, MEMP_TCP_PCB);
}if (pcb != NULL) { ... }
}if (pcb == NULL) { ... }
if (pcb != NULL) {
MEMP_STATS_DEC(err, MEMP_TCP_PCB);
}if (pcb != NULL) { ... }
}if (pcb == NULL) { ... }
if (pcb != NULL) {
MEMP_STATS_DEC(err, MEMP_TCP_PCB);
}if (pcb != NULL) { ... }
}if (pcb == NULL) { ... }
if (pcb != NULL) {
memset(pcb, 0, sizeof(struct tcp_pcb));
pcb->prio = prio;
pcb->snd_buf = TCP_SND_BUF;
/* ... */
pcb->rcv_wnd = pcb->rcv_ann_wnd = TCPWND_MIN16(TCP_WND);
pcb->ttl = TCP_TTL;
/* ... */
pcb->mss = INITIAL_MSS;
pcb->rto = 3000 / TCP_SLOW_INTERVAL;
pcb->sv = 3000 / TCP_SLOW_INTERVAL;
pcb->rtime = -1;
pcb->cwnd = 1;
pcb->tmr = tcp_ticks;
pcb->last_timer = tcp_timer_ctr;
/* ... */
pcb->ssthresh = TCP_SND_BUF;
#if LWIP_CALLBACK_API
pcb->recv = tcp_recv_null;
#endif
pcb->keep_idle = TCP_KEEPIDLE_DEFAULT;
#if LWIP_TCP_KEEPALIVE
pcb->keep_intvl = TCP_KEEPINTVL_DEFAULT;
pcb->keep_cnt = TCP_KEEPCNT_DEFAULT;/* ... */
#endif
}if (pcb != NULL) { ... }
return pcb;
}{ ... }
/* ... */
struct tcp_pcb *
tcp_new(void)
{
return tcp_alloc(TCP_PRIO_NORMAL);
}{ ... }
/* ... */
struct tcp_pcb *
tcp_new_ip_type(u8_t type)
{
struct tcp_pcb *pcb;
pcb = tcp_alloc(TCP_PRIO_NORMAL);
#if LWIP_IPV4 && LWIP_IPV6
if (pcb != NULL) {
IP_SET_TYPE_VAL(pcb->local_ip, type);
IP_SET_TYPE_VAL(pcb->remote_ip, type);
}if (pcb != NULL) { ... }
/* ... */#else
LWIP_UNUSED_ARG(type);
#endif
return pcb;
}{ ... }
/* ... */
void
tcp_arg(struct tcp_pcb *pcb, void *arg)
{
LWIP_ASSERT_CORE_LOCKED();
/* ... */
if (pcb != NULL) {
pcb->callback_arg = arg;
}if (pcb != NULL) { ... }
}{ ... }
#if LWIP_CALLBACK_API
/* ... */
void
tcp_recv(struct tcp_pcb *pcb, tcp_recv_fn recv)
{
LWIP_ASSERT_CORE_LOCKED();
if (pcb != NULL) {
LWIP_ASSERT("invalid socket state for recv callback", pcb->state != LISTEN);
pcb->recv = recv;
}if (pcb != NULL) { ... }
}{ ... }
/* ... */
void
tcp_sent(struct tcp_pcb *pcb, tcp_sent_fn sent)
{
LWIP_ASSERT_CORE_LOCKED();
if (pcb != NULL) {
LWIP_ASSERT("invalid socket state for sent callback", pcb->state != LISTEN);
pcb->sent = sent;
}if (pcb != NULL) { ... }
}{ ... }
/* ... */
void
tcp_err(struct tcp_pcb *pcb, tcp_err_fn err)
{
LWIP_ASSERT_CORE_LOCKED();
if (pcb != NULL) {
LWIP_ASSERT("invalid socket state for err callback", pcb->state != LISTEN);
pcb->errf = err;
}if (pcb != NULL) { ... }
}{ ... }
/* ... */
void
tcp_accept(struct tcp_pcb *pcb, tcp_accept_fn accept)
{
LWIP_ASSERT_CORE_LOCKED();
if ((pcb != NULL) && (pcb->state == LISTEN)) {
struct tcp_pcb_listen *lpcb = (struct tcp_pcb_listen *)pcb;
lpcb->accept = accept;
}if ((pcb != NULL) && (pcb->state == LISTEN)) { ... }
}{ ... }
#endif/* ... */
/* ... */
void
tcp_poll(struct tcp_pcb *pcb, tcp_poll_fn poll, u8_t interval)
{
LWIP_ASSERT_CORE_LOCKED();
LWIP_ERROR("tcp_poll: invalid pcb", pcb != NULL, return);
LWIP_ASSERT("invalid socket state for poll", pcb->state != LISTEN);
#if LWIP_CALLBACK_API
pcb->poll = poll;
#else
LWIP_UNUSED_ARG(poll);
#endif
pcb->pollinterval = interval;
}{ ... }
/* ... */
void
tcp_pcb_purge(struct tcp_pcb *pcb)
{
LWIP_ERROR("tcp_pcb_purge: invalid pcb", pcb != NULL, return);
if (pcb->state != CLOSED &&
pcb->state != TIME_WAIT &&
pcb->state != LISTEN) {
LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge\n"));
tcp_backlog_accepted(pcb);
if (pcb->refused_data != NULL) {
LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->refused_data\n"));
pbuf_free(pcb->refused_data);
pcb->refused_data = NULL;
}if (pcb->refused_data != NULL) { ... }
if (pcb->unsent != NULL) {
LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: not all data sent\n"));
}if (pcb->unsent != NULL) { ... }
if (pcb->unacked != NULL) {
LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->unacked\n"));
}if (pcb->unacked != NULL) { ... }
#if TCP_QUEUE_OOSEQ
if (pcb->ooseq != NULL) {
LWIP_DEBUGF(TCP_DEBUG, ("tcp_pcb_purge: data left on ->ooseq\n"));
tcp_free_ooseq(pcb);
}if (pcb->ooseq != NULL) { ... }
/* ... */#endif
/* ... */
pcb->rtime = -1;
tcp_segs_free(pcb->unsent);
tcp_segs_free(pcb->unacked);
pcb->unacked = pcb->unsent = NULL;
#if TCP_OVERSIZE
pcb->unsent_oversize = 0;
#endif
}if (pcb->state != CLOSED && pcb->state != TIME_WAIT && pcb->state != LISTEN) { ... }
}{ ... }
/* ... */
void
tcp_pcb_remove(struct tcp_pcb **pcblist, struct tcp_pcb *pcb)
{
LWIP_ASSERT("tcp_pcb_remove: invalid pcb", pcb != NULL);
LWIP_ASSERT("tcp_pcb_remove: invalid pcblist", pcblist != NULL);
TCP_RMV(pcblist, pcb);
tcp_pcb_purge(pcb);
if ((pcb->state != TIME_WAIT) &&
(pcb->state != LISTEN) &&
(pcb->flags & TF_ACK_DELAY)) {
tcp_ack_now(pcb);
tcp_output(pcb);
}if ((pcb->state != TIME_WAIT) && (pcb->state != LISTEN) && (pcb->flags & TF_ACK_DELAY)) { ... }
if (pcb->state != LISTEN) {
LWIP_ASSERT("unsent segments leaking", pcb->unsent == NULL);
LWIP_ASSERT("unacked segments leaking", pcb->unacked == NULL);
#if TCP_QUEUE_OOSEQ
LWIP_ASSERT("ooseq segments leaking", pcb->ooseq == NULL);
#endif
}if (pcb->state != LISTEN) { ... }
pcb->state = CLOSED;
pcb->local_port = 0;
LWIP_ASSERT("tcp_pcb_remove: tcp_pcbs_sane()", tcp_pcbs_sane());
}{ ... }
/* ... */
u32_t
tcp_next_iss(struct tcp_pcb *pcb)
{
#ifdef LWIP_HOOK_TCP_ISN
LWIP_ASSERT("tcp_next_iss: invalid pcb", pcb != NULL);
return LWIP_HOOK_TCP_ISN(&pcb->local_ip, pcb->local_port, &pcb->remote_ip, pcb->remote_port);/* ... */
#else
static u32_t iss = 6510;
LWIP_ASSERT("tcp_next_iss: invalid pcb", pcb != NULL);
LWIP_UNUSED_ARG(pcb);
iss += tcp_ticks;
return iss;/* ... */
#endif
}{ ... }
#if TCP_CALCULATE_EFF_SEND_MSS
/* ... */
u16_t
tcp_eff_send_mss_netif(u16_t sendmss, struct netif *outif, const ip_addr_t *dest)
{
u16_t mss_s;
u16_t mtu;
LWIP_UNUSED_ARG(dest);
LWIP_ASSERT("tcp_eff_send_mss_netif: invalid dst_ip", dest != NULL);
#if LWIP_IPV6
#if LWIP_IPV4
if (IP_IS_V6(dest))
#endif
{
mtu = nd6_get_destination_mtu(ip_2_ip6(dest), outif);
...}
#if LWIP_IPV4
else
#endif /* ... */
#endif
#if LWIP_IPV4
{
if (outif == NULL) {
return sendmss;
}if (outif == NULL) { ... }
mtu = outif->mtu;
...}/* ... */
#endif
if (mtu != 0) {
u16_t offset;
#if LWIP_IPV6
#if LWIP_IPV4
if (IP_IS_V6(dest))
#endif
{
offset = IP6_HLEN + TCP_HLEN;
...}
#if LWIP_IPV4
else
#endif /* ... */
#endif
#if LWIP_IPV4
{
offset = IP_HLEN + TCP_HLEN;
...}/* ... */
#endif
mss_s = (mtu > offset) ? (u16_t)(mtu - offset) : 0;
/* ... */
sendmss = LWIP_MIN(sendmss, mss_s);
}if (mtu != 0) { ... }
return sendmss;
}{ ... }
#endif/* ... */
static void
tcp_netif_ip_addr_changed_pcblist(const ip_addr_t *old_addr, struct tcp_pcb *pcb_list)
{
struct tcp_pcb *pcb;
pcb = pcb_list;
LWIP_ASSERT("tcp_netif_ip_addr_changed_pcblist: invalid old_addr", old_addr != NULL);
while (pcb != NULL) {
if (ip_addr_cmp(&pcb->local_ip, old_addr)
#if LWIP_AUTOIP
&& (!IP_IS_V4_VAL(pcb->local_ip) || !ip4_addr_islinklocal(ip_2_ip4(&pcb->local_ip)))/* ... */
#endif
) {
struct tcp_pcb *next = pcb->next;
LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: aborting TCP pcb %p\n", (void *)pcb));
tcp_abort(pcb);
pcb = next;
}if (ip_addr_cmp(&pcb->local_ip, old_addr) #if LWIP_AUTOIP /* connections to link-local addresses must persist (RFC3927 ch. 1.9) */ && (!IP_IS_V4_VAL(pcb->local_ip) || !ip4_addr_islinklocal(ip_2_ip4(&pcb->local_ip))) #endif /* LWIP_AUTOIP */) { ... } else {
pcb = pcb->next;
}else { ... }
}while (pcb != NULL) { ... }
}{ ... }
/* ... */
void
tcp_netif_ip_addr_changed(const ip_addr_t *old_addr, const ip_addr_t *new_addr)
{
struct tcp_pcb_listen *lpcb;
if (!ip_addr_isany(old_addr)) {
tcp_netif_ip_addr_changed_pcblist(old_addr, tcp_active_pcbs);
tcp_netif_ip_addr_changed_pcblist(old_addr, tcp_bound_pcbs);
if (!ip_addr_isany(new_addr)) {
for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
if (ip_addr_cmp(&lpcb->local_ip, old_addr)) {
/* ... */
ip_addr_copy(lpcb->local_ip, *new_addr);
}if (ip_addr_cmp(&lpcb->local_ip, old_addr)) { ... }
}for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) { ... }
}if (!ip_addr_isany(new_addr)) { ... }
}if (!ip_addr_isany(old_addr)) { ... }
}{ ... }
const char *
tcp_debug_state_str(enum tcp_state s)
{
return tcp_state_str[s];
}{ ... }
err_t
tcp_tcp_get_tcp_addrinfo(struct tcp_pcb *pcb, int local, ip_addr_t *addr, u16_t *port)
{
if (pcb) {
if (local) {
if (addr) {
*addr = pcb->local_ip;
}if (addr) { ... }
if (port) {
*port = pcb->local_port;
}if (port) { ... }
}if (local) { ... } else {
if (addr) {
*addr = pcb->remote_ip;
}if (addr) { ... }
if (port) {
*port = pcb->remote_port;
}if (port) { ... }
}else { ... }
return ERR_OK;
}if (pcb) { ... }
return ERR_VAL;
}{ ... }
#if TCP_QUEUE_OOSEQ
void
tcp_free_ooseq(struct tcp_pcb *pcb)
{
if (pcb->ooseq) {
tcp_segs_free(pcb->ooseq);
pcb->ooseq = NULL;
#if LWIP_TCP_SACK_OUT
memset(pcb->rcv_sacks, 0, sizeof(pcb->rcv_sacks));
#endif
}if (pcb->ooseq) { ... }
}tcp_free_ooseq (struct tcp_pcb *pcb) { ... }
/* ... */#endif
#if TCP_DEBUG || TCP_INPUT_DEBUG || TCP_OUTPUT_DEBUG
/* ... */
void
tcp_debug_print(struct tcp_hdr *tcphdr)
{
LWIP_DEBUGF(TCP_DEBUG, ("TCP header:\n"));
LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
LWIP_DEBUGF(TCP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n",
lwip_ntohs(tcphdr->src), lwip_ntohs(tcphdr->dest)));
LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
LWIP_DEBUGF(TCP_DEBUG, ("| %010"U32_F" | (seq no)\n",
lwip_ntohl(tcphdr->seqno)));
LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
LWIP_DEBUGF(TCP_DEBUG, ("| %010"U32_F" | (ack no)\n",
lwip_ntohl(tcphdr->ackno)));
LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
LWIP_DEBUGF(TCP_DEBUG, ("| %2"U16_F" | |%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"| %5"U16_F" | (hdrlen, flags (",
TCPH_HDRLEN(tcphdr),
(u16_t)(TCPH_FLAGS(tcphdr) >> 5 & 1),
(u16_t)(TCPH_FLAGS(tcphdr) >> 4 & 1),
(u16_t)(TCPH_FLAGS(tcphdr) >> 3 & 1),
(u16_t)(TCPH_FLAGS(tcphdr) >> 2 & 1),
(u16_t)(TCPH_FLAGS(tcphdr) >> 1 & 1),
(u16_t)(TCPH_FLAGS(tcphdr) & 1),
lwip_ntohs(tcphdr->wnd)));
tcp_debug_print_flags(TCPH_FLAGS(tcphdr));
LWIP_DEBUGF(TCP_DEBUG, ("), win)\n"));
LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
LWIP_DEBUGF(TCP_DEBUG, ("| 0x%04"X16_F" | %5"U16_F" | (chksum, urgp)\n",
lwip_ntohs(tcphdr->chksum), lwip_ntohs(tcphdr->urgp)));
LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
}tcp_debug_print (struct tcp_hdr *tcphdr) { ... }
/* ... */
void
tcp_debug_print_state(enum tcp_state s)
{
LWIP_DEBUGF(TCP_DEBUG, ("State: %s\n", tcp_state_str[s]));
}tcp_debug_print_state (enum tcp_state s) { ... }
/* ... */
void
tcp_debug_print_flags(u8_t flags)
{
if (flags & TCP_FIN) {
LWIP_DEBUGF(TCP_DEBUG, ("FIN "));
}if (flags & TCP_FIN) { ... }
if (flags & TCP_SYN) {
LWIP_DEBUGF(TCP_DEBUG, ("SYN "));
}if (flags & TCP_SYN) { ... }
if (flags & TCP_RST) {
LWIP_DEBUGF(TCP_DEBUG, ("RST "));
}if (flags & TCP_RST) { ... }
if (flags & TCP_PSH) {
LWIP_DEBUGF(TCP_DEBUG, ("PSH "));
}if (flags & TCP_PSH) { ... }
if (flags & TCP_ACK) {
LWIP_DEBUGF(TCP_DEBUG, ("ACK "));
}if (flags & TCP_ACK) { ... }
if (flags & TCP_URG) {
LWIP_DEBUGF(TCP_DEBUG, ("URG "));
}if (flags & TCP_URG) { ... }
if (flags & TCP_ECE) {
LWIP_DEBUGF(TCP_DEBUG, ("ECE "));
}if (flags & TCP_ECE) { ... }
if (flags & TCP_CWR) {
LWIP_DEBUGF(TCP_DEBUG, ("CWR "));
}if (flags & TCP_CWR) { ... }
LWIP_DEBUGF(TCP_DEBUG, ("\n"));
}tcp_debug_print_flags (u8_t flags) { ... }
/* ... */
void
tcp_debug_print_pcbs(void)
{
struct tcp_pcb *pcb;
struct tcp_pcb_listen *pcbl;
LWIP_DEBUGF(TCP_DEBUG, ("Active PCB states:\n"));
for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ",
pcb->local_port, pcb->remote_port,
pcb->snd_nxt, pcb->rcv_nxt));
tcp_debug_print_state(pcb->state);
}for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) { ... }
LWIP_DEBUGF(TCP_DEBUG, ("Listen PCB states:\n"));
for (pcbl = tcp_listen_pcbs.listen_pcbs; pcbl != NULL; pcbl = pcbl->next) {
LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F" ", pcbl->local_port));
tcp_debug_print_state(pcbl->state);
}for (pcbl = tcp_listen_pcbs.listen_pcbs; pcbl != NULL; pcbl = pcbl->next) { ... }
LWIP_DEBUGF(TCP_DEBUG, ("TIME-WAIT PCB states:\n"));
for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
LWIP_DEBUGF(TCP_DEBUG, ("Local port %"U16_F", foreign port %"U16_F" snd_nxt %"U32_F" rcv_nxt %"U32_F" ",
pcb->local_port, pcb->remote_port,
pcb->snd_nxt, pcb->rcv_nxt));
tcp_debug_print_state(pcb->state);
}for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) { ... }
}tcp_debug_print_pcbs (void) { ... }
/* ... */
s16_t
tcp_pcbs_sane(void)
{
struct tcp_pcb *pcb;
for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != CLOSED", pcb->state != CLOSED);
LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != LISTEN", pcb->state != LISTEN);
LWIP_ASSERT("tcp_pcbs_sane: active pcb->state != TIME-WAIT", pcb->state != TIME_WAIT);
}for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) { ... }
for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
LWIP_ASSERT("tcp_pcbs_sane: tw pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
}for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) { ... }
return 1;
}tcp_pcbs_sane (void) { ... }
/* ... */#endif
#if LWIP_TCP_PCB_NUM_EXT_ARGS
/* ... */
static u8_t tcp_ext_arg_id;
/* ... */
u8_t
tcp_ext_arg_alloc_id(void)
{
u8_t result = tcp_ext_arg_id;
tcp_ext_arg_id++;
LWIP_ASSERT_CORE_LOCKED();
#if LWIP_TCP_PCB_NUM_EXT_ARGS >= 255
#error LWIP_TCP_PCB_NUM_EXT_ARGS
#endif
LWIP_ASSERT("Increase LWIP_TCP_PCB_NUM_EXT_ARGS in lwipopts.h", result < LWIP_TCP_PCB_NUM_EXT_ARGS);
return result;
}tcp_ext_arg_alloc_id (void) { ... }
/* ... */
void
tcp_ext_arg_set_callbacks(struct tcp_pcb *pcb, uint8_t id, const struct tcp_ext_arg_callbacks * const callbacks)
{
LWIP_ASSERT("pcb != NULL", pcb != NULL);
LWIP_ASSERT("id < LWIP_TCP_PCB_NUM_EXT_ARGS", id < LWIP_TCP_PCB_NUM_EXT_ARGS);
LWIP_ASSERT("callbacks != NULL", callbacks != NULL);
LWIP_ASSERT_CORE_LOCKED();
pcb->ext_args[id].callbacks = callbacks;
}tcp_ext_arg_set_callbacks (struct tcp_pcb *pcb, uint8_t id, const struct tcp_ext_arg_callbacks * const callbacks) { ... }
/* ... */
void tcp_ext_arg_set(struct tcp_pcb *pcb, uint8_t id, void *arg)
{
LWIP_ASSERT("pcb != NULL", pcb != NULL);
LWIP_ASSERT("id < LWIP_TCP_PCB_NUM_EXT_ARGS", id < LWIP_TCP_PCB_NUM_EXT_ARGS);
LWIP_ASSERT_CORE_LOCKED();
pcb->ext_args[id].data = arg;
}tcp_ext_arg_set (struct tcp_pcb *pcb, uint8_t id, void *arg) { ... }
/* ... */
void *tcp_ext_arg_get(const struct tcp_pcb *pcb, uint8_t id)
{
LWIP_ASSERT("pcb != NULL", pcb != NULL);
LWIP_ASSERT("id < LWIP_TCP_PCB_NUM_EXT_ARGS", id < LWIP_TCP_PCB_NUM_EXT_ARGS);
LWIP_ASSERT_CORE_LOCKED();
return pcb->ext_args[id].data;
}tcp_ext_arg_get (const struct tcp_pcb *pcb, uint8_t id) { ... }
/* ... */
static void
tcp_ext_arg_invoke_callbacks_destroyed(struct tcp_pcb_ext_args *ext_args)
{
int i;
LWIP_ASSERT("ext_args != NULL", ext_args != NULL);
for (i = 0; i < LWIP_TCP_PCB_NUM_EXT_ARGS; i++) {
if (ext_args[i].callbacks != NULL) {
if (ext_args[i].callbacks->destroy != NULL) {
ext_args[i].callbacks->destroy((u8_t)i, ext_args[i].data);
}if (ext_args[i].callbacks->destroy != NULL) { ... }
}if (ext_args[i].callbacks != NULL) { ... }
}for (i = 0; i < LWIP_TCP_PCB_NUM_EXT_ARGS; i++) { ... }
}tcp_ext_arg_invoke_callbacks_destroyed (struct tcp_pcb_ext_args *ext_args) { ... }
/* ... */
err_t
tcp_ext_arg_invoke_callbacks_passive_open(struct tcp_pcb_listen *lpcb, struct tcp_pcb *cpcb)
{
int i;
LWIP_ASSERT("lpcb != NULL", lpcb != NULL);
LWIP_ASSERT("cpcb != NULL", cpcb != NULL);
for (i = 0; i < LWIP_TCP_PCB_NUM_EXT_ARGS; i++) {
if (lpcb->ext_args[i].callbacks != NULL) {
if (lpcb->ext_args[i].callbacks->passive_open != NULL) {
err_t err = lpcb->ext_args[i].callbacks->passive_open((u8_t)i, lpcb, cpcb);
if (err != ERR_OK) {
return err;
}if (err != ERR_OK) { ... }
}if (lpcb->ext_args[i].callbacks->passive_open != NULL) { ... }
}if (lpcb->ext_args[i].callbacks != NULL) { ... }
}for (i = 0; i < LWIP_TCP_PCB_NUM_EXT_ARGS; i++) { ... }
return ERR_OK;
}tcp_ext_arg_invoke_callbacks_passive_open (struct tcp_pcb_listen *lpcb, struct tcp_pcb *cpcb) { ... }
/* ... */#endif
/* ... */
#endif