Select one of the symbols to view example projects that use it.
 
Outline
#include "lwip/opt.h"
#include "lwip/sockets.h"
#include "lwip/priv/sockets_priv.h"
#include "lwip/api.h"
#include "lwip/igmp.h"
#include "lwip/inet.h"
#include "lwip/tcp.h"
#include "lwip/raw.h"
#include "lwip/udp.h"
#include "lwip/memp.h"
#include "lwip/pbuf.h"
#include "lwip/netif.h"
#include "lwip/priv/tcpip_priv.h"
#include "lwip/mld6.h"
#include "lwip/inet_chksum.h"
#include <stdarg.h>
#include <string.h>
#include
#define LWIP_NETCONN
#include "api_msg.c"
#include "api_lib.c"
#include "netbuf.c"
#define LWIP_NETCONN
#define API_SELECT_CB_VAR_REF
#define API_SELECT_CB_VAR_DECLARE
#define API_SELECT_CB_VAR_ALLOC
#define API_SELECT_CB_VAR_FREE
#define SOCKADDR_TO_IPADDR_PORT
#define IS_SOCK_ADDR_LEN_VALID
#define IS_SOCK_ADDR_TYPE_VALID
#define SOCK_ADDR_TYPE_MATCH
#define DOMAIN_TO_NETCONN_TYPE
#define IS_SOCK_ADDR_LEN_VALID
#define IS_SOCK_ADDR_TYPE_VALID
#define SOCK_ADDR_TYPE_MATCH
#define DOMAIN_TO_NETCONN_TYPE
#define IS_SOCK_ADDR_ALIGNED
#define LWIP_SOCKOPT_CHECK_OPTLEN
#define LWIP_SETGETSOCKOPT_DATA_VAR_REF
#define LWIP_SETGETSOCKOPT_DATA_VAR_DECLARE
#define LWIP_SETGETSOCKOPT_DATA_VAR_FREE
#define LWIP_SETGETSOCKOPT_DATA_VAR_ALLOC
#define LWIP_SO_SNDRCVTIMEO_OPTTYPE
#define LWIP_SO_SNDRCVTIMEO_SET
#define LWIP_SO_SNDRCVTIMEO_GET_MS
#define LWIP_SO_SNDRCVTIMEO_OPTTYPE
#define LWIP_SO_SNDRCVTIMEO_GET_MS
#define LWIP_SOCKET_MAX_MEMBERSHIPS
#define LWIP_SOCKET_SELECT_DECL_PROTECT
#define LWIP_SOCKET_SELECT_PROTECT
#define LWIP_SOCKET_SELECT_UNPROTECT
#define LWIP_SOCKET_SELECT_DECL_PROTECT
#define LWIP_SOCKET_SELECT_PROTECT
#define LWIP_SOCKET_SELECT_UNPROTECT
#define DEFAULT_SOCKET_EVENTCB
#define DEFAULT_SOCKET_EVENTCB
#define sock_inc_used
#define sock_inc_used_locked
#define done_socket
#define lwip_select_inc_sockets_used
#define lwip_select_dec_sockets_used
#define lwip_poll_inc_sockets_used
#define lwip_poll_dec_sockets_used
Files
loading...
SourceVuSTM32 Libraries and SamplesLwIPsrc/api/sockets.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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
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
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
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
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
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
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
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
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
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
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
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
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
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
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
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
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
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
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** * @file * Sockets BSD-Like API module *//* ... */ /* * Copyright (c) 2001-2004 Swedish Institute of Computer Science. * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. * * This file is part of the lwIP TCP/IP stack. * * Author: Adam Dunkels <adam@sics.se> * * Improved by Marc Boucher <marc@mbsi.ca> and David Haas <dhaas@alum.rpi.edu> * *//* ... */ #include "lwip/opt.h" #if LWIP_SOCKET /* don't build if not configured for use in lwipopts.h */ #include "lwip/sockets.h" #include "lwip/priv/sockets_priv.h" #include "lwip/api.h" #include "lwip/igmp.h" #include "lwip/inet.h" #include "lwip/tcp.h" #include "lwip/raw.h" #include "lwip/udp.h" #include "lwip/memp.h" #include "lwip/pbuf.h" #include "lwip/netif.h" #include "lwip/priv/tcpip_priv.h" #include "lwip/mld6.h" 13 includes#if LWIP_CHECKSUM_ON_COPY #include "lwip/inet_chksum.h" #endif #if LWIP_COMPAT_SOCKETS == 2 && LWIP_POSIX_SOCKETS_IO_NAMES #include <stdarg.h> #endif #include <string.h> #ifdef LWIP_HOOK_FILENAME #include LWIP_HOOK_FILENAME #endif /* If the netconn API is not required publicly, then we include the necessary files here to get the implementation *//* ... */ #if !LWIP_NETCONN #undef LWIP_NETCONN #define LWIP_NETCONN 1 #include "api_msg.c" #include "api_lib.c" #include "netbuf.c" #undef LWIP_NETCONN #define LWIP_NETCONN 0 /* ... */#endif #define API_SELECT_CB_VAR_REF(name) API_VAR_REF(name) #define API_SELECT_CB_VAR_DECLARE(name) API_VAR_DECLARE(struct lwip_select_cb, name) #define API_SELECT_CB_VAR_ALLOC(name, retblock) API_VAR_ALLOC_EXT(struct lwip_select_cb, MEMP_SELECT_CB, name, retblock) #define API_SELECT_CB_VAR_FREE(name) API_VAR_FREE(MEMP_SELECT_CB, name) #if LWIP_IPV4 #define IP4ADDR_PORT_TO_SOCKADDR(sin, ipaddr, port) do { \ (sin)->sin_len = sizeof(struct sockaddr_in); \ (sin)->sin_family = AF_INET; \ (sin)->sin_port = lwip_htons((port)); \ inet_addr_from_ip4addr(&(sin)->sin_addr, ipaddr); \ memset((sin)->sin_zero, 0, SIN_ZERO_LEN); ...}while(0)... #define SOCKADDR4_TO_IP4ADDR_PORT(sin, ipaddr, port) do { \ inet_addr_to_ip4addr(ip_2_ip4(ipaddr), &((sin)->sin_addr)); \ (port) = lwip_ntohs((sin)->sin_port); ...}while(0)... /* ... */#endif /* LWIP_IPV4 */ #if LWIP_IPV6 #define IP6ADDR_PORT_TO_SOCKADDR(sin6, ipaddr, port) do { \ (sin6)->sin6_len = sizeof(struct sockaddr_in6); \ (sin6)->sin6_family = AF_INET6; \ (sin6)->sin6_port = lwip_htons((port)); \ (sin6)->sin6_flowinfo = 0; \ inet6_addr_from_ip6addr(&(sin6)->sin6_addr, ipaddr); \ (sin6)->sin6_scope_id = ip6_addr_zone(ipaddr); ...}while(0)... #define SOCKADDR6_TO_IP6ADDR_PORT(sin6, ipaddr, port) do { \ inet6_addr_to_ip6addr(ip_2_ip6(ipaddr), &((sin6)->sin6_addr)); \ if (ip6_addr_has_scope(ip_2_ip6(ipaddr), IP6_UNKNOWN)) { \ ip6_addr_set_zone(ip_2_ip6(ipaddr), (u8_t)((sin6)->sin6_scope_id)); \ }if (ip6_addr_has_scope(ip_2_ip6(ipaddr), IP6_UNKNOWN)) { ... } \ (port) = lwip_ntohs((sin6)->sin6_port); ...}while(0)... /* ... */#endif /* LWIP_IPV6 */ #if LWIP_IPV4 && LWIP_IPV6 static void sockaddr_to_ipaddr_port(const struct sockaddr *sockaddr, ip_addr_t *ipaddr, u16_t *port); #define IS_SOCK_ADDR_LEN_VALID(namelen) (((namelen) == sizeof(struct sockaddr_in)) || \ ((namelen) == sizeof(struct sockaddr_in6)))... #define IS_SOCK_ADDR_TYPE_VALID(name) (((name)->sa_family == AF_INET) || \ ((name)->sa_family == AF_INET6))... #define SOCK_ADDR_TYPE_MATCH(name, sock) \ ((((name)->sa_family == AF_INET) && !(NETCONNTYPE_ISIPV6((sock)->conn->type))) || \ (((name)->sa_family == AF_INET6) && (NETCONNTYPE_ISIPV6((sock)->conn->type))))... #define IPADDR_PORT_TO_SOCKADDR(sockaddr, ipaddr, port) do { \ if (IP_IS_ANY_TYPE_VAL(*ipaddr) || IP_IS_V6_VAL(*ipaddr)) { \ IP6ADDR_PORT_TO_SOCKADDR((struct sockaddr_in6*)(void*)(sockaddr), ip_2_ip6(ipaddr), port); \ }if (IP_IS_ANY_TYPE_VAL(*ipaddr) || IP_IS_V6_VAL(*ipaddr)) { ... } else { \ IP4ADDR_PORT_TO_SOCKADDR((struct sockaddr_in*)(void*)(sockaddr), ip_2_ip4(ipaddr), port); \ }else { ... } ...} while(0)... #define SOCKADDR_TO_IPADDR_PORT(sockaddr, ipaddr, port) sockaddr_to_ipaddr_port(sockaddr, ipaddr, &(port)) #define DOMAIN_TO_NETCONN_TYPE(domain, type) (((domain) == AF_INET) ? \ (type) : (enum netconn_type)((type) | NETCONN_TYPE_IPV6))... 6 defines#elif/* ... */ LWIP_IPV6 /* LWIP_IPV4 && LWIP_IPV6 */ #define IS_SOCK_ADDR_LEN_VALID(namelen) ((namelen) == sizeof(struct sockaddr_in6)) #define IS_SOCK_ADDR_TYPE_VALID(name) ((name)->sa_family == AF_INET6) #define SOCK_ADDR_TYPE_MATCH(name, sock) 1 #define IPADDR_PORT_TO_SOCKADDR(sockaddr, ipaddr, port) \ IP6ADDR_PORT_TO_SOCKADDR((struct sockaddr_in6*)(void*)(sockaddr), ip_2_ip6(ipaddr), port)... #define SOCKADDR_TO_IPADDR_PORT(sockaddr, ipaddr, port) \ SOCKADDR6_TO_IP6ADDR_PORT((const struct sockaddr_in6*)(const void*)(sockaddr), ipaddr, port)... #define DOMAIN_TO_NETCONN_TYPE(domain, netconn_type) (netconn_type) /* ... */#else /*-> LWIP_IPV4: LWIP_IPV4 && LWIP_IPV6 */ #define IS_SOCK_ADDR_LEN_VALID(namelen) ((namelen) == sizeof(struct sockaddr_in)) #define IS_SOCK_ADDR_TYPE_VALID(name) ((name)->sa_family == AF_INET) #define SOCK_ADDR_TYPE_MATCH(name, sock) 1 #define IPADDR_PORT_TO_SOCKADDR(sockaddr, ipaddr, port) \ IP4ADDR_PORT_TO_SOCKADDR((struct sockaddr_in*)(void*)(sockaddr), ip_2_ip4(ipaddr), port)... #define SOCKADDR_TO_IPADDR_PORT(sockaddr, ipaddr, port) \ SOCKADDR4_TO_IP4ADDR_PORT((const struct sockaddr_in*)(const void*)(sockaddr), ipaddr, port)... #define DOMAIN_TO_NETCONN_TYPE(domain, netconn_type) (netconn_type) /* ... */#endif /* LWIP_IPV6 */ #define IS_SOCK_ADDR_TYPE_VALID_OR_UNSPEC(name) (((name)->sa_family == AF_UNSPEC) || \ IS_SOCK_ADDR_TYPE_VALID(name))... #define SOCK_ADDR_TYPE_MATCH_OR_UNSPEC(name, sock) (((name)->sa_family == AF_UNSPEC) || \ SOCK_ADDR_TYPE_MATCH(name, sock))... #define IS_SOCK_ADDR_ALIGNED(name) ((((mem_ptr_t)(name)) % 4) == 0) #define LWIP_SOCKOPT_CHECK_OPTLEN(sock, optlen, opttype) do { if ((optlen) < sizeof(opttype)) { done_socket(sock); return EINVAL; }}while(0) #define LWIP_SOCKOPT_CHECK_OPTLEN_CONN(sock, optlen, opttype) do { \ LWIP_SOCKOPT_CHECK_OPTLEN(sock, optlen, opttype); \ if ((sock)->conn == NULL) { done_socket(sock); return EINVAL; } ...}while(0)... #define LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, opttype) do { \ LWIP_SOCKOPT_CHECK_OPTLEN(sock, optlen, opttype); \ if (((sock)->conn == NULL) || ((sock)->conn->pcb.tcp == NULL)) { done_socket(sock); return EINVAL; } ...}while(0)... #define LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, opttype, netconntype) do { \ LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, opttype); \ if (NETCONNTYPE_GROUP(netconn_type((sock)->conn)) != netconntype) { done_socket(sock); return ENOPROTOOPT; } ...}while(0)... #define LWIP_SETGETSOCKOPT_DATA_VAR_REF(name) API_VAR_REF(name) #define LWIP_SETGETSOCKOPT_DATA_VAR_DECLARE(name) API_VAR_DECLARE(struct lwip_setgetsockopt_data, name) #define LWIP_SETGETSOCKOPT_DATA_VAR_FREE(name) API_VAR_FREE(MEMP_SOCKET_SETGETSOCKOPT_DATA, name) 10 defines#if LWIP_MPU_COMPATIBLE #define LWIP_SETGETSOCKOPT_DATA_VAR_ALLOC(name, sock) do { \ name = (struct lwip_setgetsockopt_data *)memp_malloc(MEMP_SOCKET_SETGETSOCKOPT_DATA); \ if (name == NULL) { \ sock_set_errno(sock, ENOMEM); \ done_socket(sock); \ return -1; \ }if (name == NULL) { ... } ...}while(0)... /* ... */#else /* LWIP_MPU_COMPATIBLE */ #define LWIP_SETGETSOCKOPT_DATA_VAR_ALLOC(name, sock) #endif /* LWIP_MPU_COMPATIBLE */ #if LWIP_SO_SNDRCVTIMEO_NONSTANDARD #define LWIP_SO_SNDRCVTIMEO_OPTTYPE int #define LWIP_SO_SNDRCVTIMEO_SET(optval, val) (*(int *)(optval) = (val)) #define LWIP_SO_SNDRCVTIMEO_GET_MS(optval) ((long)*(const int*)(optval)) /* ... */#else #define LWIP_SO_SNDRCVTIMEO_OPTTYPE struct timeval #define LWIP_SO_SNDRCVTIMEO_SET(optval, val) do { \ u32_t loc = (val); \ ((struct timeval *)(optval))->tv_sec = (long)((loc) / 1000U); \ ((struct timeval *)(optval))->tv_usec = (long)(((loc) % 1000U) * 1000U); ...}while(0)... #define LWIP_SO_SNDRCVTIMEO_GET_MS(optval) ((((const struct timeval *)(optval))->tv_sec * 1000) + (((const struct timeval *)(optval))->tv_usec / 1000)) /* ... */#endif /** A struct sockaddr replacement that has the same alignment as sockaddr_in/ * sockaddr_in6 if instantiated. *//* ... */ union sockaddr_aligned { struct sockaddr sa; #if LWIP_IPV6 struct sockaddr_in6 sin6; #endif /* LWIP_IPV6 */ #if LWIP_IPV4 struct sockaddr_in sin; #endif /* LWIP_IPV4 */ ...}; /* Define the number of IPv4 multicast memberships, default is one per socket */ #ifndef LWIP_SOCKET_MAX_MEMBERSHIPS #define LWIP_SOCKET_MAX_MEMBERSHIPS NUM_SOCKETS #endif #if LWIP_IGMP /* This is to keep track of IP_ADD_MEMBERSHIP calls to drop the membership when a socket is closed *//* ... */ struct lwip_socket_multicast_pair { /** the socket */ struct lwip_sock *sock; /** the interface address */ ip4_addr_t if_addr; /** the group address */ ip4_addr_t multi_addr; ...}; static struct lwip_socket_multicast_pair socket_ipv4_multicast_memberships[LWIP_SOCKET_MAX_MEMBERSHIPS]; static int lwip_socket_register_membership(int s, const ip4_addr_t *if_addr, const ip4_addr_t *multi_addr); static void lwip_socket_unregister_membership(int s, const ip4_addr_t *if_addr, const ip4_addr_t *multi_addr); static void lwip_socket_drop_registered_memberships(int s);/* ... */ #endif /* LWIP_IGMP */ #if LWIP_IPV6_MLD /* This is to keep track of IP_JOIN_GROUP calls to drop the membership when a socket is closed *//* ... */ struct lwip_socket_multicast_mld6_pair { /** the socket */ struct lwip_sock *sock; /** the interface index */ u8_t if_idx; /** the group address */ ip6_addr_t multi_addr; ...}; static struct lwip_socket_multicast_mld6_pair socket_ipv6_multicast_memberships[LWIP_SOCKET_MAX_MEMBERSHIPS]; static int lwip_socket_register_mld6_membership(int s, unsigned int if_idx, const ip6_addr_t *multi_addr); static void lwip_socket_unregister_mld6_membership(int s, unsigned int if_idx, const ip6_addr_t *multi_addr); static void lwip_socket_drop_registered_mld6_memberships(int s);/* ... */ #endif /* LWIP_IPV6_MLD */ /** The global array of available sockets */ static struct lwip_sock sockets[NUM_SOCKETS]; #if LWIP_SOCKET_SELECT || LWIP_SOCKET_POLL #if LWIP_TCPIP_CORE_LOCKING /* protect the select_cb_list using core lock */ #define LWIP_SOCKET_SELECT_DECL_PROTECT(lev) #define LWIP_SOCKET_SELECT_PROTECT(lev) LOCK_TCPIP_CORE() #define LWIP_SOCKET_SELECT_UNPROTECT(lev) UNLOCK_TCPIP_CORE() /* ... */#else /* LWIP_TCPIP_CORE_LOCKING */ /* protect the select_cb_list using SYS_LIGHTWEIGHT_PROT */ #define LWIP_SOCKET_SELECT_DECL_PROTECT(lev) SYS_ARCH_DECL_PROTECT(lev) #define LWIP_SOCKET_SELECT_PROTECT(lev) SYS_ARCH_PROTECT(lev) #define LWIP_SOCKET_SELECT_UNPROTECT(lev) SYS_ARCH_UNPROTECT(lev) /** This counter is increased from lwip_select when the list is changed and checked in select_check_waiters to see if it has changed. *//* ... */ static volatile int select_cb_ctr;/* ... */ #endif /* LWIP_TCPIP_CORE_LOCKING */ /** The global list of tasks waiting for select */ static struct lwip_select_cb *select_cb_list;/* ... */ #endif /* LWIP_SOCKET_SELECT || LWIP_SOCKET_POLL */ #define sock_set_errno(sk, e) do { \ const int sockerr = (e); \ set_errno(sockerr); \ ...} while (0)... /* Forward declaration of some functions */ #if LWIP_SOCKET_SELECT || LWIP_SOCKET_POLL static void event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len); #define DEFAULT_SOCKET_EVENTCB event_callback static void select_check_waiters(int s, int has_recvevent, int has_sendevent, int has_errevent);/* ... */ #else #define DEFAULT_SOCKET_EVENTCB NULL #endif #if !LWIP_TCPIP_CORE_LOCKING static void lwip_getsockopt_callback(void *arg); static void lwip_setsockopt_callback(void *arg);/* ... */ #endif static int lwip_getsockopt_impl(int s, int level, int optname, void *optval, socklen_t *optlen); static int lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_t optlen); static int free_socket_locked(struct lwip_sock *sock, int is_tcp, struct netconn **conn, union lwip_sock_lastdata *lastdata); static void free_socket_free_elements(int is_tcp, struct netconn *conn, union lwip_sock_lastdata *lastdata); #if LWIP_IPV4 && LWIP_IPV6 static void sockaddr_to_ipaddr_port(const struct sockaddr *sockaddr, ip_addr_t *ipaddr, u16_t *port) { if ((sockaddr->sa_family) == AF_INET6) { SOCKADDR6_TO_IP6ADDR_PORT((const struct sockaddr_in6 *)(const void *)(sockaddr), ipaddr, *port); ipaddr->type = IPADDR_TYPE_V6; }if ((sockaddr->sa_family) == AF_INET6) { ... } else { SOCKADDR4_TO_IP4ADDR_PORT((const struct sockaddr_in *)(const void *)(sockaddr), ipaddr, *port); ipaddr->type = IPADDR_TYPE_V4; }else { ... } }sockaddr_to_ipaddr_port (const struct sockaddr *sockaddr, ip_addr_t *ipaddr, u16_t *port) { ... } /* ... */#endif /* LWIP_IPV4 && LWIP_IPV6 */ /** LWIP_NETCONN_SEM_PER_THREAD==1: initialize thread-local semaphore */ void lwip_socket_thread_init(void) { netconn_thread_init(); }lwip_socket_thread_init (void) { ... } /** LWIP_NETCONN_SEM_PER_THREAD==1: destroy thread-local semaphore */ void lwip_socket_thread_cleanup(void) { netconn_thread_cleanup(); }lwip_socket_thread_cleanup (void) { ... } #if LWIP_NETCONN_FULLDUPLEX /* Thread-safe increment of sock->fd_used, with overflow check */ static int sock_inc_used(struct lwip_sock *sock) { int ret; SYS_ARCH_DECL_PROTECT(lev); LWIP_ASSERT("sock != NULL", sock != NULL); SYS_ARCH_PROTECT(lev); if (sock->fd_free_pending) { /* prevent new usage of this socket if free is pending */ ret = 0; }if (sock->fd_free_pending) { ... } else { ++sock->fd_used; ret = 1; LWIP_ASSERT("sock->fd_used != 0", sock->fd_used != 0); }else { ... } SYS_ARCH_UNPROTECT(lev); return ret; }sock_inc_used (struct lwip_sock *sock) { ... } /* Like sock_inc_used(), but called under SYS_ARCH_PROTECT lock. */ static int sock_inc_used_locked(struct lwip_sock *sock) { LWIP_ASSERT("sock != NULL", sock != NULL); if (sock->fd_free_pending) { LWIP_ASSERT("sock->fd_used != 0", sock->fd_used != 0); return 0; }if (sock->fd_free_pending) { ... } ++sock->fd_used; LWIP_ASSERT("sock->fd_used != 0", sock->fd_used != 0); return 1; }sock_inc_used_locked (struct lwip_sock *sock) { ... } /* In full-duplex mode,sock->fd_used != 0 prevents a socket descriptor from being * released (and possibly reused) when used from more than one thread * (e.g. read-while-write or close-while-write, etc) * This function is called at the end of functions using (try)get_socket*(). *//* ... */ static void done_socket(struct lwip_sock *sock) { int freed = 0; int is_tcp = 0; struct netconn *conn = NULL; union lwip_sock_lastdata lastdata; SYS_ARCH_DECL_PROTECT(lev); LWIP_ASSERT("sock != NULL", sock != NULL); SYS_ARCH_PROTECT(lev); LWIP_ASSERT("sock->fd_used > 0", sock->fd_used > 0); if (--sock->fd_used == 0) { if (sock->fd_free_pending) { /* free the socket */ sock->fd_used = 1; is_tcp = sock->fd_free_pending & LWIP_SOCK_FD_FREE_TCP; freed = free_socket_locked(sock, is_tcp, &conn, &lastdata); }if (sock->fd_free_pending) { ... } }if (--sock->fd_used == 0) { ... } SYS_ARCH_UNPROTECT(lev); if (freed) { free_socket_free_elements(is_tcp, conn, &lastdata); }if (freed) { ... } }done_socket (struct lwip_sock *sock) { ... } /* ... */ #else /* LWIP_NETCONN_FULLDUPLEX */ #define sock_inc_used(sock) 1 #define sock_inc_used_locked(sock) 1 #define done_socket(sock) /* ... */#endif /* LWIP_NETCONN_FULLDUPLEX */ /* Translate a socket 'int' into a pointer (only fails if the index is invalid) */ static struct lwip_sock * tryget_socket_unconn_nouse(int fd) { int s = fd - LWIP_SOCKET_OFFSET; if ((s < 0) || (s >= NUM_SOCKETS)) { LWIP_DEBUGF(SOCKETS_DEBUG, ("tryget_socket_unconn(%d): invalid\n", fd)); return NULL; }if ((s < 0) || (s >= NUM_SOCKETS)) { ... } return &sockets[s]; }tryget_socket_unconn_nouse (int fd) { ... } struct lwip_sock * lwip_socket_dbg_get_socket(int fd) { return tryget_socket_unconn_nouse(fd); }lwip_socket_dbg_get_socket (int fd) { ... } /* Translate a socket 'int' into a pointer (only fails if the index is invalid) */ static struct lwip_sock * tryget_socket_unconn(int fd) { struct lwip_sock *ret = tryget_socket_unconn_nouse(fd); if (ret != NULL) { if (!sock_inc_used(ret)) { return NULL; }if (!sock_inc_used(ret)) { ... } }if (ret != NULL) { ... } return ret; }tryget_socket_unconn (int fd) { ... } /* Like tryget_socket_unconn(), but called under SYS_ARCH_PROTECT lock. */ static struct lwip_sock * tryget_socket_unconn_locked(int fd) { struct lwip_sock *ret = tryget_socket_unconn_nouse(fd); if (ret != NULL) { if (!sock_inc_used_locked(ret)) { return NULL; }if (!sock_inc_used_locked(ret)) { ... } }if (ret != NULL) { ... } return ret; }tryget_socket_unconn_locked (int fd) { ... } /** * Same as get_socket but doesn't set errno * * @param fd externally used socket index * @return struct lwip_sock for the socket or NULL if not found *//* ... */ static struct lwip_sock * tryget_socket(int fd) { struct lwip_sock *sock = tryget_socket_unconn(fd); if (sock != NULL) { if (sock->conn) { return sock; }if (sock->conn) { ... } done_socket(sock); }if (sock != NULL) { ... } return NULL; }tryget_socket (int fd) { ... } /** * Map a externally used socket index to the internal socket representation. * * @param fd externally used socket index * @return struct lwip_sock for the socket or NULL if not found *//* ... */ static struct lwip_sock * get_socket(int fd) { struct lwip_sock *sock = tryget_socket(fd); if (!sock) { if ((fd < LWIP_SOCKET_OFFSET) || (fd >= (LWIP_SOCKET_OFFSET + NUM_SOCKETS))) { LWIP_DEBUGF(SOCKETS_DEBUG, ("get_socket(%d): invalid\n", fd)); }if ((fd < LWIP_SOCKET_OFFSET) || (fd >= (LWIP_SOCKET_OFFSET + NUM_SOCKETS))) { ... } set_errno(EBADF); return NULL; }if (!sock) { ... } return sock; }get_socket (int fd) { ... } /** * Allocate a new socket for a given netconn. * * @param newconn the netconn for which to allocate a socket * @param accepted 1 if socket has been created by accept(), * 0 if socket has been created by socket() * @return the index of the new socket; -1 on error *//* ... */ static int alloc_socket(struct netconn *newconn, int accepted) { int i; SYS_ARCH_DECL_PROTECT(lev); LWIP_UNUSED_ARG(accepted); /* allocate a new socket identifier */ for (i = 0; i < NUM_SOCKETS; ++i) { /* Protect socket array */ SYS_ARCH_PROTECT(lev); if (!sockets[i].conn) { #if LWIP_NETCONN_FULLDUPLEX if (sockets[i].fd_used) { SYS_ARCH_UNPROTECT(lev); continue; }if (sockets[i].fd_used) { ... } sockets[i].fd_used = 1; sockets[i].fd_free_pending = 0;/* ... */ #endif sockets[i].conn = newconn; /* The socket is not yet known to anyone, so no need to protect after having marked it as used. *//* ... */ SYS_ARCH_UNPROTECT(lev); sockets[i].lastdata.pbuf = NULL; #if LWIP_SOCKET_SELECT || LWIP_SOCKET_POLL LWIP_ASSERT("sockets[i].select_waiting == 0", sockets[i].select_waiting == 0); sockets[i].rcvevent = 0; /* TCP sendbuf is empty, but the socket is not yet writable until connected * (unless it has been created by accept()). *//* ... */ sockets[i].sendevent = (NETCONNTYPE_GROUP(newconn->type) == NETCONN_TCP ? (accepted != 0) : 1); sockets[i].errevent = 0;/* ... */ #endif /* LWIP_SOCKET_SELECT || LWIP_SOCKET_POLL */ return i + LWIP_SOCKET_OFFSET; }if (!sockets[i].conn) { ... } SYS_ARCH_UNPROTECT(lev); }for (i = 0; i < NUM_SOCKETS; ++i) { ... } return -1; }alloc_socket (struct netconn *newconn, int accepted) { ... } /** Free a socket (under lock) * * @param sock the socket to free * @param is_tcp != 0 for TCP sockets, used to free lastdata * @param conn the socekt's netconn is stored here, must be freed externally * @param lastdata lastdata is stored here, must be freed externally *//* ... */ static int free_socket_locked(struct lwip_sock *sock, int is_tcp, struct netconn **conn, union lwip_sock_lastdata *lastdata) { #if LWIP_NETCONN_FULLDUPLEX LWIP_ASSERT("sock->fd_used > 0", sock->fd_used > 0); sock->fd_used--; if (sock->fd_used > 0) { sock->fd_free_pending = LWIP_SOCK_FD_FREE_FREE | (is_tcp ? LWIP_SOCK_FD_FREE_TCP : 0); return 0; }if (sock->fd_used > 0) { ... } /* ... */#else /* LWIP_NETCONN_FULLDUPLEX */ LWIP_UNUSED_ARG(is_tcp); #endif /* LWIP_NETCONN_FULLDUPLEX */ *lastdata = sock->lastdata; sock->lastdata.pbuf = NULL; *conn = sock->conn; sock->conn = NULL; return 1; }free_socket_locked (struct lwip_sock *sock, int is_tcp, struct netconn **conn, union lwip_sock_lastdata *lastdata) { ... } /** Free a socket's leftover members. *//* ... */ static void free_socket_free_elements(int is_tcp, struct netconn *conn, union lwip_sock_lastdata *lastdata) { if (lastdata->pbuf != NULL) { if (is_tcp) { pbuf_free(lastdata->pbuf); }if (is_tcp) { ... } else { netbuf_delete(lastdata->netbuf); }else { ... } }if (lastdata->pbuf != NULL) { ... } if (conn != NULL) { /* netconn_prepare_delete() has already been called, here we only free the conn */ netconn_delete(conn); }if (conn != NULL) { ... } }free_socket_free_elements (int is_tcp, struct netconn *conn, union lwip_sock_lastdata *lastdata) { ... } /** Free a socket. The socket's netconn must have been * delete before! * * @param sock the socket to free * @param is_tcp != 0 for TCP sockets, used to free lastdata *//* ... */ static void free_socket(struct lwip_sock *sock, int is_tcp) { int freed; struct netconn *conn; union lwip_sock_lastdata lastdata; SYS_ARCH_DECL_PROTECT(lev); /* Protect socket array */ SYS_ARCH_PROTECT(lev); freed = free_socket_locked(sock, is_tcp, &conn, &lastdata); SYS_ARCH_UNPROTECT(lev); /* don't use 'sock' after this line, as another task might have allocated it */ if (freed) { free_socket_free_elements(is_tcp, conn, &lastdata); }if (freed) { ... } }free_socket (struct lwip_sock *sock, int is_tcp) { ... } /* Below this, the well-known socket functions are implemented. * Use google.com or opengroup.org to get a good description :-) * * Exceptions are documented! *//* ... */ int lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen) { struct lwip_sock *sock, *nsock; struct netconn *newconn; ip_addr_t naddr; u16_t port = 0; int newsock; err_t err; int recvevent; SYS_ARCH_DECL_PROTECT(lev); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d)...\n", s)); sock = get_socket(s); if (!sock) { return -1; }if (!sock) { ... } /* wait for a new connection */ err = netconn_accept(sock->conn, &newconn); if (err != ERR_OK) { LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d): netconn_acept failed, err=%d\n", s, err)); if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) != NETCONN_TCP) { sock_set_errno(sock, EOPNOTSUPP); }if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) != NETCONN_TCP) { ... } else if (err == ERR_CLSD) { sock_set_errno(sock, EINVAL); }else if (err == ERR_CLSD) { ... } else { sock_set_errno(sock, err_to_errno(err)); }else { ... } done_socket(sock); return -1; }if (err != ERR_OK) { ... } LWIP_ASSERT("newconn != NULL", newconn != NULL); newsock = alloc_socket(newconn, 1); if (newsock == -1) { netconn_delete(newconn); sock_set_errno(sock, ENFILE); done_socket(sock); return -1; }if (newsock == -1) { ... } LWIP_ASSERT("invalid socket index", (newsock >= LWIP_SOCKET_OFFSET) && (newsock < NUM_SOCKETS + LWIP_SOCKET_OFFSET)); nsock = &sockets[newsock - LWIP_SOCKET_OFFSET]; /* See event_callback: If data comes in right away after an accept, even * though the server task might not have created a new socket yet. * In that case, newconn->socket is counted down (newconn->socket--), * so nsock->rcvevent is >= 1 here! *//* ... */ SYS_ARCH_PROTECT(lev); recvevent = (s16_t)(-1 - newconn->socket); newconn->socket = newsock; SYS_ARCH_UNPROTECT(lev); if (newconn->callback) { LOCK_TCPIP_CORE(); while (recvevent > 0) { recvevent--; newconn->callback(newconn, NETCONN_EVT_RCVPLUS, 0); }while (recvevent > 0) { ... } UNLOCK_TCPIP_CORE(); }if (newconn->callback) { ... } /* Note that POSIX only requires us to check addr is non-NULL. addrlen must * not be NULL if addr is valid. *//* ... */ if ((addr != NULL) && (addrlen != NULL)) { union sockaddr_aligned tempaddr; /* get the IP address and port of the remote host */ err = netconn_peer(newconn, &naddr, &port); if (err != ERR_OK) { LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d): netconn_peer failed, err=%d\n", s, err)); netconn_delete(newconn); free_socket(nsock, 1); sock_set_errno(sock, err_to_errno(err)); done_socket(sock); return -1; }if (err != ERR_OK) { ... } IPADDR_PORT_TO_SOCKADDR(&tempaddr, &naddr, port); if (*addrlen > tempaddr.sa.sa_len) { *addrlen = tempaddr.sa.sa_len; }if (*addrlen > tempaddr.sa.sa_len) { ... } MEMCPY(addr, &tempaddr, *addrlen); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d) returning new sock=%d addr=", s, newsock)); ip_addr_debug_print_val(SOCKETS_DEBUG, naddr); LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F"\n", port)); }if ((addr != NULL) && (addrlen != NULL)) { ... } else { LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d) returning new sock=%d", s, newsock)); }else { ... } sock_set_errno(sock, 0); done_socket(sock); done_socket(nsock); return newsock; }lwip_accept (int s, struct sockaddr *addr, socklen_t *addrlen) { ... } int lwip_bind(int s, const struct sockaddr *name, socklen_t namelen) { struct lwip_sock *sock; ip_addr_t local_addr; u16_t local_port; err_t err; sock = get_socket(s); if (!sock) { return -1; }if (!sock) { ... } if (!SOCK_ADDR_TYPE_MATCH(name, sock)) { /* sockaddr does not match socket type (IPv4/IPv6) */ sock_set_errno(sock, err_to_errno(ERR_VAL)); done_socket(sock); return -1; }if (!SOCK_ADDR_TYPE_MATCH(name, sock)) { ... } /* check size, family and alignment of 'name' */ LWIP_ERROR("lwip_bind: invalid address", (IS_SOCK_ADDR_LEN_VALID(namelen) && IS_SOCK_ADDR_TYPE_VALID(name) && IS_SOCK_ADDR_ALIGNED(name)), sock_set_errno(sock, err_to_errno(ERR_ARG)); done_socket(sock); return -1;); LWIP_UNUSED_ARG(namelen); SOCKADDR_TO_IPADDR_PORT(name, &local_addr, local_port); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d, addr=", s)); ip_addr_debug_print_val(SOCKETS_DEBUG, local_addr); LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F")\n", local_port)); #if LWIP_IPV4 && LWIP_IPV6 /* Dual-stack: Unmap IPv4 mapped IPv6 addresses */ if (IP_IS_V6_VAL(local_addr) && ip6_addr_isipv4mappedipv6(ip_2_ip6(&local_addr))) { unmap_ipv4_mapped_ipv6(ip_2_ip4(&local_addr), ip_2_ip6(&local_addr)); IP_SET_TYPE_VAL(local_addr, IPADDR_TYPE_V4); }if (IP_IS_V6_VAL(local_addr) && ip6_addr_isipv4mappedipv6(ip_2_ip6(&local_addr))) { ... } /* ... */#endif /* LWIP_IPV4 && LWIP_IPV6 */ err = netconn_bind(sock->conn, &local_addr, local_port); if (err != ERR_OK) { LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d) failed, err=%d\n", s, err)); sock_set_errno(sock, err_to_errno(err)); done_socket(sock); return -1; }if (err != ERR_OK) { ... } LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d) succeeded\n", s)); sock_set_errno(sock, 0); done_socket(sock); return 0; }lwip_bind (int s, const struct sockaddr *name, socklen_t namelen) { ... } int lwip_close(int s) { struct lwip_sock *sock; int is_tcp = 0; err_t err; LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_close(%d)\n", s)); sock = get_socket(s); if (!sock) { return -1; }if (!sock) { ... } if (sock->conn != NULL) { is_tcp = NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_TCP; }if (sock->conn != NULL) { ... } else { LWIP_ASSERT("sock->lastdata == NULL", sock->lastdata.pbuf == NULL); }else { ... } #if LWIP_IGMP /* drop all possibly joined IGMP memberships */ lwip_socket_drop_registered_memberships(s);/* ... */ #endif /* LWIP_IGMP */ #if LWIP_IPV6_MLD /* drop all possibly joined MLD6 memberships */ lwip_socket_drop_registered_mld6_memberships(s);/* ... */ #endif /* LWIP_IPV6_MLD */ err = netconn_prepare_delete(sock->conn); if (err != ERR_OK) { sock_set_errno(sock, err_to_errno(err)); done_socket(sock); return -1; }if (err != ERR_OK) { ... } free_socket(sock, is_tcp); set_errno(0); return 0; }lwip_close (int s) { ... } int lwip_connect(int s, const struct sockaddr *name, socklen_t namelen) { struct lwip_sock *sock; err_t err; sock = get_socket(s); if (!sock) { return -1; }if (!sock) { ... } if (!SOCK_ADDR_TYPE_MATCH_OR_UNSPEC(name, sock)) { /* sockaddr does not match socket type (IPv4/IPv6) */ sock_set_errno(sock, err_to_errno(ERR_VAL)); done_socket(sock); return -1; }if (!SOCK_ADDR_TYPE_MATCH_OR_UNSPEC(name, sock)) { ... } LWIP_UNUSED_ARG(namelen); if (name->sa_family == AF_UNSPEC) { LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d, AF_UNSPEC)\n", s)); err = netconn_disconnect(sock->conn); }if (name->sa_family == AF_UNSPEC) { ... } else { ip_addr_t remote_addr; u16_t remote_port; /* check size, family and alignment of 'name' */ LWIP_ERROR("lwip_connect: invalid address", IS_SOCK_ADDR_LEN_VALID(namelen) && IS_SOCK_ADDR_TYPE_VALID_OR_UNSPEC(name) && IS_SOCK_ADDR_ALIGNED(name), sock_set_errno(sock, err_to_errno(ERR_ARG)); done_socket(sock); return -1;); SOCKADDR_TO_IPADDR_PORT(name, &remote_addr, remote_port); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d, addr=", s)); ip_addr_debug_print_val(SOCKETS_DEBUG, remote_addr); LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F")\n", remote_port)); #if LWIP_IPV4 && LWIP_IPV6 /* Dual-stack: Unmap IPv4 mapped IPv6 addresses */ if (IP_IS_V6_VAL(remote_addr) && ip6_addr_isipv4mappedipv6(ip_2_ip6(&remote_addr))) { unmap_ipv4_mapped_ipv6(ip_2_ip4(&remote_addr), ip_2_ip6(&remote_addr)); IP_SET_TYPE_VAL(remote_addr, IPADDR_TYPE_V4); }if (IP_IS_V6_VAL(remote_addr) && ip6_addr_isipv4mappedipv6(ip_2_ip6(&remote_addr))) { ... } /* ... */#endif /* LWIP_IPV4 && LWIP_IPV6 */ err = netconn_connect(sock->conn, &remote_addr, remote_port); }else { ... } if (err != ERR_OK) { LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d) failed, err=%d\n", s, err)); sock_set_errno(sock, err_to_errno(err)); done_socket(sock); return -1; }if (err != ERR_OK) { ... } LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d) succeeded\n", s)); sock_set_errno(sock, 0); done_socket(sock); return 0; }lwip_connect (int s, const struct sockaddr *name, socklen_t namelen) { ... } /** * Set a socket into listen mode. * The socket may not have been used for another connection previously. * * @param s the socket to set to listening mode * @param backlog (ATTENTION: needs TCP_LISTEN_BACKLOG=1) * @return 0 on success, non-zero on failure *//* ... */ int lwip_listen(int s, int backlog) { struct lwip_sock *sock; err_t err; LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_listen(%d, backlog=%d)\n", s, backlog)); sock = get_socket(s); if (!sock) { return -1; }if (!sock) { ... } /* limit the "backlog" parameter to fit in an u8_t */ backlog = LWIP_MIN(LWIP_MAX(backlog, 0), 0xff); err = netconn_listen_with_backlog(sock->conn, (u8_t)backlog); if (err != ERR_OK) { LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_listen(%d) failed, err=%d\n", s, err)); if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) != NETCONN_TCP) { sock_set_errno(sock, EOPNOTSUPP); }if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) != NETCONN_TCP) { ... } else { sock_set_errno(sock, err_to_errno(err)); }else { ... } done_socket(sock); return -1; }if (err != ERR_OK) { ... } sock_set_errno(sock, 0); done_socket(sock); return 0; }lwip_listen (int s, int backlog) { ... } #if LWIP_TCP /* Helper function to loop over receiving pbufs from netconn * until "len" bytes are received or we're otherwise done. * Keeps sock->lastdata for peeking or partly copying. *//* ... */ static ssize_t lwip_recv_tcp(struct lwip_sock *sock, void *mem, size_t len, int flags) { u8_t apiflags = NETCONN_NOAUTORCVD; ssize_t recvd = 0; ssize_t recv_left = (len <= SSIZE_MAX) ? (ssize_t)len : SSIZE_MAX; LWIP_ASSERT("no socket given", sock != NULL); LWIP_ASSERT("this should be checked internally", NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_TCP); if (flags & MSG_DONTWAIT) { apiflags |= NETCONN_DONTBLOCK; }if (flags & MSG_DONTWAIT) { ... } do { struct pbuf *p; err_t err; u16_t copylen; LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recv_tcp: top while sock->lastdata=%p\n", (void *)sock->lastdata.pbuf)); /* Check if there is data left from the last recv operation. */ if (sock->lastdata.pbuf) { p = sock->lastdata.pbuf; }if (sock->lastdata.pbuf) { ... } else { /* No data was left from the previous operation, so we try to get some from the network. *//* ... */ err = netconn_recv_tcp_pbuf_flags(sock->conn, &p, apiflags); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recv_tcp: netconn_recv err=%d, pbuf=%p\n", err, (void *)p)); if (err != ERR_OK) { if (recvd > 0) { /* already received data, return that (this trusts in getting the same error from netconn layer again next time netconn_recv is called) *//* ... */ goto lwip_recv_tcp_done; }if (recvd > 0) { ... } /* We should really do some error checking here. */ LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recv_tcp: p == NULL, error is \"%s\"!\n", lwip_strerr(err))); sock_set_errno(sock, err_to_errno(err)); if (err == ERR_CLSD) { return 0; }if (err == ERR_CLSD) { ... } else { return -1; }else { ... } }if (err != ERR_OK) { ... } LWIP_ASSERT("p != NULL", p != NULL); sock->lastdata.pbuf = p; }else { ... } LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recv_tcp: buflen=%"U16_F" recv_left=%d off=%d\n", p->tot_len, (int)recv_left, (int)recvd)); if (recv_left > p->tot_len) { copylen = p->tot_len; }if (recv_left > p->tot_len) { ... } else { copylen = (u16_t)recv_left; }else { ... } if (recvd + copylen < recvd) { /* overflow */ copylen = (u16_t)(SSIZE_MAX - recvd); }if (recvd + copylen < recvd) { ... } /* copy the contents of the received buffer into the supplied memory pointer mem *//* ... */ pbuf_copy_partial(p, (u8_t *)mem + recvd, copylen, 0); recvd += copylen; /* TCP combines multiple pbufs for one recv */ LWIP_ASSERT("invalid copylen, len would underflow", recv_left >= copylen); recv_left -= copylen; /* Unless we peek the incoming message... */ if ((flags & MSG_PEEK) == 0) { /* ... check if there is data left in the pbuf */ LWIP_ASSERT("invalid copylen", p->tot_len >= copylen); if (p->tot_len - copylen > 0) { /* If so, it should be saved in the sock structure for the next recv call. We store the pbuf but hide/free the consumed data: *//* ... */ sock->lastdata.pbuf = pbuf_free_header(p, copylen); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recv_tcp: lastdata now pbuf=%p\n", (void *)sock->lastdata.pbuf)); }if (p->tot_len - copylen > 0) { ... } else { sock->lastdata.pbuf = NULL; LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recv_tcp: deleting pbuf=%p\n", (void *)p)); pbuf_free(p); }else { ... } }if ((flags & MSG_PEEK) == 0) { ... } /* once we have some data to return, only add more if we don't need to wait */ apiflags |= NETCONN_DONTBLOCK | NETCONN_NOFIN; /* @todo: do we need to support peeking more than one pbuf? */ ...} while ((recv_left > 0) && !(flags & MSG_PEEK)); lwip_recv_tcp_done: if ((recvd > 0) && !(flags & MSG_PEEK)) { /* ensure window update after copying all data */ netconn_tcp_recvd(sock->conn, (size_t)recvd); }if ((recvd > 0) && !(flags & MSG_PEEK)) { ... } sock_set_errno(sock, 0); return recvd; }lwip_recv_tcp (struct lwip_sock *sock, void *mem, size_t len, int flags) { ... } /* ... */#endif /* Convert a netbuf's address data to struct sockaddr */ static int lwip_sock_make_addr(struct netconn *conn, ip_addr_t *fromaddr, u16_t port, struct sockaddr *from, socklen_t *fromlen) { int truncated = 0; union sockaddr_aligned saddr; LWIP_UNUSED_ARG(conn); LWIP_ASSERT("fromaddr != NULL", fromaddr != NULL); LWIP_ASSERT("from != NULL", from != NULL); LWIP_ASSERT("fromlen != NULL", fromlen != NULL); #if LWIP_IPV4 && LWIP_IPV6 /* Dual-stack: Map IPv4 addresses to IPv4 mapped IPv6 */ if (NETCONNTYPE_ISIPV6(netconn_type(conn)) && IP_IS_V4(fromaddr)) { ip4_2_ipv4_mapped_ipv6(ip_2_ip6(fromaddr), ip_2_ip4(fromaddr)); IP_SET_TYPE(fromaddr, IPADDR_TYPE_V6); }if (NETCONNTYPE_ISIPV6(netconn_type(conn)) && IP_IS_V4(fromaddr)) { ... } /* ... */#endif /* LWIP_IPV4 && LWIP_IPV6 */ IPADDR_PORT_TO_SOCKADDR(&saddr, fromaddr, port); if (*fromlen < saddr.sa.sa_len) { truncated = 1; }if (*fromlen < saddr.sa.sa_len) { ... } else if (*fromlen > saddr.sa.sa_len) { *fromlen = saddr.sa.sa_len; }else if (*fromlen > saddr.sa.sa_len) { ... } MEMCPY(from, &saddr, *fromlen); return truncated; }lwip_sock_make_addr (struct netconn *conn, ip_addr_t *fromaddr, u16_t port, struct sockaddr *from, socklen_t *fromlen) { ... } #if LWIP_TCP /* Helper function to get a tcp socket's remote address info */ static int lwip_recv_tcp_from(struct lwip_sock *sock, struct sockaddr *from, socklen_t *fromlen, const char *dbg_fn, int dbg_s, ssize_t dbg_ret) { if (sock == NULL) { return 0; }if (sock == NULL) { ... } LWIP_UNUSED_ARG(dbg_fn); LWIP_UNUSED_ARG(dbg_s); LWIP_UNUSED_ARG(dbg_ret); #if !SOCKETS_DEBUG if (from && fromlen) #endif /* !SOCKETS_DEBUG */ { /* get remote addr/port from tcp_pcb */ u16_t port; ip_addr_t tmpaddr; netconn_getaddr(sock->conn, &tmpaddr, &port, 0); LWIP_DEBUGF(SOCKETS_DEBUG, ("%s(%d): addr=", dbg_fn, dbg_s)); ip_addr_debug_print_val(SOCKETS_DEBUG, tmpaddr); LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F" len=%d\n", port, (int)dbg_ret)); if (from && fromlen) { return lwip_sock_make_addr(sock->conn, &tmpaddr, port, from, fromlen); }if (from && fromlen) { ... } ...} return 0; }lwip_recv_tcp_from (struct lwip_sock *sock, struct sockaddr *from, socklen_t *fromlen, const char *dbg_fn, int dbg_s, ssize_t dbg_ret) { ... } /* ... */#endif /* Helper function to receive a netbuf from a udp or raw netconn. * Keeps sock->lastdata for peeking. *//* ... */ static err_t lwip_recvfrom_udp_raw(struct lwip_sock *sock, int flags, struct msghdr *msg, u16_t *datagram_len, int dbg_s) { struct netbuf *buf; u8_t apiflags; err_t err; u16_t buflen, copylen, copied; int i; LWIP_UNUSED_ARG(dbg_s); LWIP_ERROR("lwip_recvfrom_udp_raw: invalid arguments", (msg->msg_iov != NULL) || (msg->msg_iovlen <= 0), return ERR_ARG;); if (flags & MSG_DONTWAIT) { apiflags = NETCONN_DONTBLOCK; }if (flags & MSG_DONTWAIT) { ... } else { apiflags = 0; }else { ... } LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom_udp_raw[UDP/RAW]: top sock->lastdata=%p\n", (void *)sock->lastdata.netbuf)); /* Check if there is data left from the last recv operation. */ buf = sock->lastdata.netbuf; if (buf == NULL) { /* No data was left from the previous operation, so we try to get some from the network. *//* ... */ err = netconn_recv_udp_raw_netbuf_flags(sock->conn, &buf, apiflags); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom_udp_raw[UDP/RAW]: netconn_recv err=%d, netbuf=%p\n", err, (void *)buf)); if (err != ERR_OK) { return err; }if (err != ERR_OK) { ... } LWIP_ASSERT("buf != NULL", buf != NULL); sock->lastdata.netbuf = buf; }if (buf == NULL) { ... } buflen = buf->p->tot_len; LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom_udp_raw: buflen=%"U16_F"\n", buflen)); copied = 0; /* copy the pbuf payload into the iovs */ for (i = 0; (i < msg->msg_iovlen) && (copied < buflen); i++) { u16_t len_left = (u16_t)(buflen - copied); if (msg->msg_iov[i].iov_len > len_left) { copylen = len_left; }if (msg->msg_iov[i].iov_len > len_left) { ... } else { copylen = (u16_t)msg->msg_iov[i].iov_len; }else { ... } /* copy the contents of the received buffer into the supplied memory buffer *//* ... */ pbuf_copy_partial(buf->p, (u8_t *)msg->msg_iov[i].iov_base, copylen, copied); copied = (u16_t)(copied + copylen); }for (i = 0; (i < msg->msg_iovlen) && (copied < buflen); i++) { ... } /* Check to see from where the data was.*/ #if !SOCKETS_DEBUG if (msg->msg_name && msg->msg_namelen) #endif /* !SOCKETS_DEBUG */ { LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom_udp_raw(%d): addr=", dbg_s)); ip_addr_debug_print_val(SOCKETS_DEBUG, *netbuf_fromaddr(buf)); LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F" len=%d\n", netbuf_fromport(buf), copied)); if (msg->msg_name && msg->msg_namelen) { lwip_sock_make_addr(sock->conn, netbuf_fromaddr(buf), netbuf_fromport(buf), (struct sockaddr *)msg->msg_name, &msg->msg_namelen); }if (msg->msg_name && msg->msg_namelen) { ... } ...} /* Initialize flag output */ msg->msg_flags = 0; if (msg->msg_control) { u8_t wrote_msg = 0; #if LWIP_NETBUF_RECVINFO /* Check if packet info was recorded */ if (buf->flags & NETBUF_FLAG_DESTADDR) { if (IP_IS_V4(&buf->toaddr)) { #if LWIP_IPV4 if (msg->msg_controllen >= CMSG_SPACE(sizeof(struct in_pktinfo))) { struct cmsghdr *chdr = CMSG_FIRSTHDR(msg); /* This will always return a header!! */ struct in_pktinfo *pkti = (struct in_pktinfo *)CMSG_DATA(chdr); chdr->cmsg_level = IPPROTO_IP; chdr->cmsg_type = IP_PKTINFO; chdr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo)); pkti->ipi_ifindex = buf->p->if_idx; inet_addr_from_ip4addr(&pkti->ipi_addr, ip_2_ip4(netbuf_destaddr(buf))); msg->msg_controllen = CMSG_SPACE(sizeof(struct in_pktinfo)); wrote_msg = 1; }if (msg->msg_controllen >= CMSG_SPACE(sizeof(struct in_pktinfo))) { ... } else { msg->msg_flags |= MSG_CTRUNC; }else { ... } /* ... */#endif /* LWIP_IPV4 */ }if (IP_IS_V4(&buf->toaddr)) { ... } }if (buf->flags & NETBUF_FLAG_DESTADDR) { ... } /* ... */#endif /* LWIP_NETBUF_RECVINFO */ if (!wrote_msg) { msg->msg_controllen = 0; }if (!wrote_msg) { ... } }if (msg->msg_control) { ... } /* If we don't peek the incoming message: zero lastdata pointer and free the netbuf */ if ((flags & MSG_PEEK) == 0) { sock->lastdata.netbuf = NULL; netbuf_delete(buf); }if ((flags & MSG_PEEK) == 0) { ... } if (datagram_len) { *datagram_len = buflen; }if (datagram_len) { ... } return ERR_OK; }lwip_recvfrom_udp_raw (struct lwip_sock *sock, int flags, struct msghdr *msg, u16_t *datagram_len, int dbg_s) { ... } ssize_t lwip_recvfrom(int s, void *mem, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen) { struct lwip_sock *sock; ssize_t ret; LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d, %p, %"SZT_F", 0x%x, ..)\n", s, mem, len, flags)); sock = get_socket(s); if (!sock) { return -1; }if (!sock) { ... } #if LWIP_TCP if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_TCP) { ret = lwip_recv_tcp(sock, mem, len, flags); lwip_recv_tcp_from(sock, from, fromlen, "lwip_recvfrom", s, ret); done_socket(sock); return ret; }if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_TCP) { ... } else #endif { u16_t datagram_len = 0; struct iovec vec; struct msghdr msg; err_t err; vec.iov_base = mem; vec.iov_len = len; msg.msg_control = NULL; msg.msg_controllen = 0; msg.msg_flags = 0; msg.msg_iov = &vec; msg.msg_iovlen = 1; msg.msg_name = from; msg.msg_namelen = (fromlen ? *fromlen : 0); err = lwip_recvfrom_udp_raw(sock, flags, &msg, &datagram_len, s); if (err != ERR_OK) { LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom[UDP/RAW](%d): buf == NULL, error is \"%s\"!\n", s, lwip_strerr(err))); sock_set_errno(sock, err_to_errno(err)); done_socket(sock); return -1; }if (err != ERR_OK) { ... } ret = (ssize_t)LWIP_MIN(LWIP_MIN(len, datagram_len), SSIZE_MAX); if (fromlen) { *fromlen = msg.msg_namelen; }if (fromlen) { ... } }else { ... } sock_set_errno(sock, 0); done_socket(sock); return ret; }lwip_recvfrom (int s, void *mem, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen) { ... } ssize_t lwip_read(int s, void *mem, size_t len) { return lwip_recvfrom(s, mem, len, 0, NULL, NULL); }lwip_read (int s, void *mem, size_t len) { ... } ssize_t lwip_readv(int s, const struct iovec *iov, int iovcnt) { struct msghdr msg; msg.msg_name = NULL; msg.msg_namelen = 0; /* Hack: we have to cast via number to cast from 'const' pointer to non-const. Blame the opengroup standard for this inconsistency. *//* ... */ msg.msg_iov = LWIP_CONST_CAST(struct iovec *, iov); msg.msg_iovlen = iovcnt; msg.msg_control = NULL; msg.msg_controllen = 0; msg.msg_flags = 0; return lwip_recvmsg(s, &msg, 0); }lwip_readv (int s, const struct iovec *iov, int iovcnt) { ... } ssize_t lwip_recv(int s, void *mem, size_t len, int flags) { return lwip_recvfrom(s, mem, len, flags, NULL, NULL); }lwip_recv (int s, void *mem, size_t len, int flags) { ... } ssize_t lwip_recvmsg(int s, struct msghdr *message, int flags) { struct lwip_sock *sock; int i; ssize_t buflen; LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvmsg(%d, message=%p, flags=0x%x)\n", s, (void *)message, flags)); LWIP_ERROR("lwip_recvmsg: invalid message pointer", message != NULL, return ERR_ARG;); LWIP_ERROR("lwip_recvmsg: unsupported flags", (flags & ~(MSG_PEEK|MSG_DONTWAIT)) == 0, set_errno(EOPNOTSUPP); return -1;); if ((message->msg_iovlen <= 0) || (message->msg_iovlen > IOV_MAX)) { set_errno(EMSGSIZE); return -1; }if ((message->msg_iovlen <= 0) || (message->msg_iovlen > IOV_MAX)) { ... } sock = get_socket(s); if (!sock) { return -1; }if (!sock) { ... } /* check for valid vectors */ buflen = 0; for (i = 0; i < message->msg_iovlen; i++) { if ((message->msg_iov[i].iov_base == NULL) || ((ssize_t)message->msg_iov[i].iov_len <= 0) || ((size_t)(ssize_t)message->msg_iov[i].iov_len != message->msg_iov[i].iov_len) || ((ssize_t)(buflen + (ssize_t)message->msg_iov[i].iov_len) <= 0)) { sock_set_errno(sock, err_to_errno(ERR_VAL)); done_socket(sock); return -1; }if ((message->msg_iov[i].iov_base == NULL) || ((ssize_t)message->msg_iov[i].iov_len <= 0) || ((size_t)(ssize_t)message->msg_iov[i].iov_len != message->msg_iov[i].iov_len) || ((ssize_t)(buflen + (ssize_t)message->msg_iov[i].iov_len) <= 0)) { ... } buflen = (ssize_t)(buflen + (ssize_t)message->msg_iov[i].iov_len); }for (i = 0; i < message->msg_iovlen; i++) { ... } if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_TCP) { #if LWIP_TCP int recv_flags = flags; message->msg_flags = 0; /* recv the data */ buflen = 0; for (i = 0; i < message->msg_iovlen; i++) { /* try to receive into this vector's buffer */ ssize_t recvd_local = lwip_recv_tcp(sock, message->msg_iov[i].iov_base, message->msg_iov[i].iov_len, recv_flags); if (recvd_local > 0) { /* sum up received bytes */ buflen += recvd_local; }if (recvd_local > 0) { ... } if ((recvd_local < 0) || (recvd_local < (int)message->msg_iov[i].iov_len) || (flags & MSG_PEEK)) { /* returned prematurely (or peeking, which might actually be limitated to the first iov) */ if (buflen <= 0) { /* nothing received at all, propagate the error */ buflen = recvd_local; }if (buflen <= 0) { ... } break; }if ((recvd_local < 0) || (recvd_local < (int)message->msg_iov[i].iov_len) || (flags & MSG_PEEK)) { ... } /* pass MSG_DONTWAIT to lwip_recv_tcp() to prevent waiting for more data */ recv_flags |= MSG_DONTWAIT; }for (i = 0; i < message->msg_iovlen; i++) { ... } if (buflen > 0) { /* reset socket error since we have received something */ sock_set_errno(sock, 0); }if (buflen > 0) { ... } /* " If the socket is connected, the msg_name and msg_namelen members shall be ignored." */ done_socket(sock); return buflen;/* ... */ #else /* LWIP_TCP */ sock_set_errno(sock, err_to_errno(ERR_ARG)); done_socket(sock); return -1;/* ... */ #endif /* LWIP_TCP */ }if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_TCP) { ... } /* else, UDP and RAW NETCONNs */ #if LWIP_UDP || LWIP_RAW { u16_t datagram_len = 0; err_t err; err = lwip_recvfrom_udp_raw(sock, flags, message, &datagram_len, s); if (err != ERR_OK) { LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvmsg[UDP/RAW](%d): buf == NULL, error is \"%s\"!\n", s, lwip_strerr(err))); sock_set_errno(sock, err_to_errno(err)); done_socket(sock); return -1; }if (err != ERR_OK) { ... } if (datagram_len > buflen) { message->msg_flags |= MSG_TRUNC; }if (datagram_len > buflen) { ... } sock_set_errno(sock, 0); done_socket(sock); return (int)datagram_len; ...}/* ... */ #else /* LWIP_UDP || LWIP_RAW */ sock_set_errno(sock, err_to_errno(ERR_ARG)); done_socket(sock); return -1;/* ... */ #endif /* LWIP_UDP || LWIP_RAW */ }lwip_recvmsg (int s, struct msghdr *message, int flags) { ... } ssize_t lwip_send(int s, const void *data, size_t size, int flags) { struct lwip_sock *sock; err_t err; u8_t write_flags; size_t written; LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d, data=%p, size=%"SZT_F", flags=0x%x)\n", s, data, size, flags)); sock = get_socket(s); if (!sock) { return -1; }if (!sock) { ... } if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) != NETCONN_TCP) { #if (LWIP_UDP || LWIP_RAW) done_socket(sock); return lwip_sendto(s, data, size, flags, NULL, 0);/* ... */ #else /* (LWIP_UDP || LWIP_RAW) */ sock_set_errno(sock, err_to_errno(ERR_ARG)); done_socket(sock); return -1;/* ... */ #endif /* (LWIP_UDP || LWIP_RAW) */ }if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) != NETCONN_TCP) { ... } write_flags = (u8_t)(NETCONN_COPY | ((flags & MSG_MORE) ? NETCONN_MORE : 0) | ((flags & MSG_DONTWAIT) ? NETCONN_DONTBLOCK : 0)); written = 0; err = netconn_write_partly(sock->conn, data, size, write_flags, &written); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d) err=%d written=%"SZT_F"\n", s, err, written)); sock_set_errno(sock, err_to_errno(err)); done_socket(sock); /* casting 'written' to ssize_t is OK here since the netconn API limits it to SSIZE_MAX */ return (err == ERR_OK ? (ssize_t)written : -1); }lwip_send (int s, const void *data, size_t size, int flags) { ... } ssize_t lwip_sendmsg(int s, const struct msghdr *msg, int flags) { struct lwip_sock *sock; #if LWIP_TCP u8_t write_flags; size_t written;/* ... */ #endif err_t err = ERR_OK; sock = get_socket(s); if (!sock) { return -1; }if (!sock) { ... } LWIP_ERROR("lwip_sendmsg: invalid msghdr", msg != NULL, sock_set_errno(sock, err_to_errno(ERR_ARG)); done_socket(sock); return -1;); LWIP_ERROR("lwip_sendmsg: invalid msghdr iov", msg->msg_iov != NULL, sock_set_errno(sock, err_to_errno(ERR_ARG)); done_socket(sock); return -1;); LWIP_ERROR("lwip_sendmsg: maximum iovs exceeded", (msg->msg_iovlen > 0) && (msg->msg_iovlen <= IOV_MAX), sock_set_errno(sock, EMSGSIZE); done_socket(sock); return -1;); LWIP_ERROR("lwip_sendmsg: unsupported flags", (flags & ~(MSG_DONTWAIT | MSG_MORE)) == 0, sock_set_errno(sock, EOPNOTSUPP); done_socket(sock); return -1;); LWIP_UNUSED_ARG(msg->msg_control); LWIP_UNUSED_ARG(msg->msg_controllen); LWIP_UNUSED_ARG(msg->msg_flags); if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_TCP) { #if LWIP_TCP write_flags = (u8_t)(NETCONN_COPY | ((flags & MSG_MORE) ? NETCONN_MORE : 0) | ((flags & MSG_DONTWAIT) ? NETCONN_DONTBLOCK : 0)); written = 0; err = netconn_write_vectors_partly(sock->conn, (struct netvector *)msg->msg_iov, (u16_t)msg->msg_iovlen, write_flags, &written); sock_set_errno(sock, err_to_errno(err)); done_socket(sock); /* casting 'written' to ssize_t is OK here since the netconn API limits it to SSIZE_MAX */ return (err == ERR_OK ? (ssize_t)written : -1);/* ... */ #else /* LWIP_TCP */ sock_set_errno(sock, err_to_errno(ERR_ARG)); done_socket(sock); return -1;/* ... */ #endif /* LWIP_TCP */ }if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_TCP) { ... } /* else, UDP and RAW NETCONNs */ #if LWIP_UDP || LWIP_RAW { struct netbuf chain_buf; int i; ssize_t size = 0; LWIP_UNUSED_ARG(flags); LWIP_ERROR("lwip_sendmsg: invalid msghdr name", (((msg->msg_name == NULL) && (msg->msg_namelen == 0)) || IS_SOCK_ADDR_LEN_VALID(msg->msg_namelen)), sock_set_errno(sock, err_to_errno(ERR_ARG)); done_socket(sock); return -1;); /* initialize chain buffer with destination */ memset(&chain_buf, 0, sizeof(struct netbuf)); if (msg->msg_name) { u16_t remote_port; SOCKADDR_TO_IPADDR_PORT((const struct sockaddr *)msg->msg_name, &chain_buf.addr, remote_port); netbuf_fromport(&chain_buf) = remote_port; }if (msg->msg_name) { ... } #if LWIP_NETIF_TX_SINGLE_PBUF for (i = 0; i < msg->msg_iovlen; i++) { size += msg->msg_iov[i].iov_len; if ((msg->msg_iov[i].iov_len > INT_MAX) || (size < (int)msg->msg_iov[i].iov_len)) { /* overflow */ goto sendmsg_emsgsize; }if ((msg->msg_iov[i].iov_len > INT_MAX) || (size < (int)msg->msg_iov[i].iov_len)) { ... } }for (i = 0; i < msg->msg_iovlen; i++) { ... } if (size > 0xFFFF) { /* overflow */ goto sendmsg_emsgsize; }if (size > 0xFFFF) { ... } /* Allocate a new netbuf and copy the data into it. */ if (netbuf_alloc(&chain_buf, (u16_t)size) == NULL) { err = ERR_MEM; }if (netbuf_alloc(&chain_buf, (u16_t)size) == NULL) { ... } else { /* flatten the IO vectors */ size_t offset = 0; for (i = 0; i < msg->msg_iovlen; i++) { MEMCPY(&((u8_t *)chain_buf.p->payload)[offset], msg->msg_iov[i].iov_base, msg->msg_iov[i].iov_len); offset += msg->msg_iov[i].iov_len; }for (i = 0; i < msg->msg_iovlen; i++) { ... } #if LWIP_CHECKSUM_ON_COPY { /* This can be improved by using LWIP_CHKSUM_COPY() and aggregating the checksum for each IO vector */ u16_t chksum = ~inet_chksum_pbuf(chain_buf.p); netbuf_set_chksum(&chain_buf, chksum); ...}/* ... */ #endif /* LWIP_CHECKSUM_ON_COPY */ err = ERR_OK; }else { ... } /* ... */#else /* LWIP_NETIF_TX_SINGLE_PBUF */ /* create a chained netbuf from the IO vectors. NOTE: we assemble a pbuf chain manually to avoid having to allocate, chain, and delete a netbuf for each iov *//* ... */ for (i = 0; i < msg->msg_iovlen; i++) { struct pbuf *p; if (msg->msg_iov[i].iov_len > 0xFFFF) { /* overflow */ goto sendmsg_emsgsize; }if (msg->msg_iov[i].iov_len > 0xFFFF) { ... } p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_REF); if (p == NULL) { err = ERR_MEM; /* let netbuf_delete() cleanup chain_buf */ break; }if (p == NULL) { ... } p->payload = msg->msg_iov[i].iov_base; p->len = p->tot_len = (u16_t)msg->msg_iov[i].iov_len; /* netbuf empty, add new pbuf */ if (chain_buf.p == NULL) { chain_buf.p = chain_buf.ptr = p; /* add pbuf to existing pbuf chain */ }if (chain_buf.p == NULL) { ... } else { if (chain_buf.p->tot_len + p->len > 0xffff) { /* overflow */ pbuf_free(p); goto sendmsg_emsgsize; }if (chain_buf.p->tot_len + p->len > 0xffff) { ... } pbuf_cat(chain_buf.p, p); }else { ... } }for (i = 0; i < msg->msg_iovlen; i++) { ... } /* save size of total chain */ if (err == ERR_OK) { size = netbuf_len(&chain_buf); }if (err == ERR_OK) { ... } /* ... */#endif /* LWIP_NETIF_TX_SINGLE_PBUF */ if (err == ERR_OK) { #if LWIP_IPV4 && LWIP_IPV6 /* Dual-stack: Unmap IPv4 mapped IPv6 addresses */ if (IP_IS_V6_VAL(chain_buf.addr) && ip6_addr_isipv4mappedipv6(ip_2_ip6(&chain_buf.addr))) { unmap_ipv4_mapped_ipv6(ip_2_ip4(&chain_buf.addr), ip_2_ip6(&chain_buf.addr)); IP_SET_TYPE_VAL(chain_buf.addr, IPADDR_TYPE_V4); }if (IP_IS_V6_VAL(chain_buf.addr) && ip6_addr_isipv4mappedipv6(ip_2_ip6(&chain_buf.addr))) { ... } /* ... */#endif /* LWIP_IPV4 && LWIP_IPV6 */ /* send the data */ err = netconn_send(sock->conn, &chain_buf); }if (err == ERR_OK) { ... } /* deallocated the buffer */ netbuf_free(&chain_buf); sock_set_errno(sock, err_to_errno(err)); done_socket(sock); return (err == ERR_OK ? size : -1); sendmsg_emsgsize: sock_set_errno(sock, EMSGSIZE); netbuf_free(&chain_buf); done_socket(sock); return -1; ...}/* ... */ #else /* LWIP_UDP || LWIP_RAW */ sock_set_errno(sock, err_to_errno(ERR_ARG)); done_socket(sock); return -1;/* ... */ #endif /* LWIP_UDP || LWIP_RAW */ }lwip_sendmsg (int s, const struct msghdr *msg, int flags) { ... } ssize_t lwip_sendto(int s, const void *data, size_t size, int flags, const struct sockaddr *to, socklen_t tolen) { struct lwip_sock *sock; err_t err; u16_t short_size; u16_t remote_port; struct netbuf buf; sock = get_socket(s); if (!sock) { return -1; }if (!sock) { ... } if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_TCP) { #if LWIP_TCP done_socket(sock); return lwip_send(s, data, size, flags);/* ... */ #else /* LWIP_TCP */ LWIP_UNUSED_ARG(flags); sock_set_errno(sock, err_to_errno(ERR_ARG)); done_socket(sock); return -1;/* ... */ #endif /* LWIP_TCP */ }if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_TCP) { ... } if (size > LWIP_MIN(0xFFFF, SSIZE_MAX)) { /* cannot fit into one datagram (at least for us) */ sock_set_errno(sock, EMSGSIZE); done_socket(sock); return -1; }if (size > LWIP_MIN(0xFFFF, SSIZE_MAX)) { ... } short_size = (u16_t)size; LWIP_ERROR("lwip_sendto: invalid address", (((to == NULL) && (tolen == 0)) || (IS_SOCK_ADDR_LEN_VALID(tolen) && ((to != NULL) && (IS_SOCK_ADDR_TYPE_VALID(to) && IS_SOCK_ADDR_ALIGNED(to))))), sock_set_errno(sock, err_to_errno(ERR_ARG)); done_socket(sock); return -1;); LWIP_UNUSED_ARG(tolen); /* initialize a buffer */ buf.p = buf.ptr = NULL; #if LWIP_CHECKSUM_ON_COPY buf.flags = 0; #endif /* LWIP_CHECKSUM_ON_COPY */ if (to) { SOCKADDR_TO_IPADDR_PORT(to, &buf.addr, remote_port); }if (to) { ... } else { remote_port = 0; ip_addr_set_any(NETCONNTYPE_ISIPV6(netconn_type(sock->conn)), &buf.addr); }else { ... } netbuf_fromport(&buf) = remote_port; LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_sendto(%d, data=%p, short_size=%"U16_F", flags=0x%x to=", s, data, short_size, flags)); ip_addr_debug_print_val(SOCKETS_DEBUG, buf.addr); LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F"\n", remote_port)); /* make the buffer point to the data that should be sent */ #if LWIP_NETIF_TX_SINGLE_PBUF /* Allocate a new netbuf and copy the data into it. */ if (netbuf_alloc(&buf, short_size) == NULL) { err = ERR_MEM; }if (netbuf_alloc(&buf, short_size) == NULL) { ... } else { #if LWIP_CHECKSUM_ON_COPY if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) != NETCONN_RAW) { u16_t chksum = LWIP_CHKSUM_COPY(buf.p->payload, data, short_size); netbuf_set_chksum(&buf, chksum); }if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) != NETCONN_RAW) { ... } else #endif /* LWIP_CHECKSUM_ON_COPY */ { MEMCPY(buf.p->payload, data, short_size); }else { ... } err = ERR_OK; }else { ... } /* ... */#else /* LWIP_NETIF_TX_SINGLE_PBUF */ err = netbuf_ref(&buf, data, short_size); #endif /* LWIP_NETIF_TX_SINGLE_PBUF */ if (err == ERR_OK) { #if LWIP_IPV4 && LWIP_IPV6 /* Dual-stack: Unmap IPv4 mapped IPv6 addresses */ if (IP_IS_V6_VAL(buf.addr) && ip6_addr_isipv4mappedipv6(ip_2_ip6(&buf.addr))) { unmap_ipv4_mapped_ipv6(ip_2_ip4(&buf.addr), ip_2_ip6(&buf.addr)); IP_SET_TYPE_VAL(buf.addr, IPADDR_TYPE_V4); }if (IP_IS_V6_VAL(buf.addr) && ip6_addr_isipv4mappedipv6(ip_2_ip6(&buf.addr))) { ... } /* ... */#endif /* LWIP_IPV4 && LWIP_IPV6 */ /* send the data */ err = netconn_send(sock->conn, &buf); }if (err == ERR_OK) { ... } /* deallocated the buffer */ netbuf_free(&buf); sock_set_errno(sock, err_to_errno(err)); done_socket(sock); return (err == ERR_OK ? short_size : -1); }lwip_sendto (int s, const void *data, size_t size, int flags, const struct sockaddr *to, socklen_t tolen) { ... } int lwip_socket(int domain, int type, int protocol) { struct netconn *conn; int i; LWIP_UNUSED_ARG(domain); /* @todo: check this */ /* create a netconn */ switch (type) { case SOCK_RAW: conn = netconn_new_with_proto_and_callback(DOMAIN_TO_NETCONN_TYPE(domain, NETCONN_RAW), (u8_t)protocol, DEFAULT_SOCKET_EVENTCB); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_RAW, %d) = ", domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol)); break;case SOCK_RAW: case SOCK_DGRAM: conn = netconn_new_with_callback(DOMAIN_TO_NETCONN_TYPE(domain, ((protocol == IPPROTO_UDPLITE) ? NETCONN_UDPLITE : NETCONN_UDP)), DEFAULT_SOCKET_EVENTCB); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_DGRAM, %d) = ", domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol)); #if LWIP_NETBUF_RECVINFO if (conn) { /* netconn layer enables pktinfo by default, sockets default to off */ conn->flags &= ~NETCONN_FLAG_PKTINFO; }if (conn) { ... } /* ... */#endif /* LWIP_NETBUF_RECVINFO */ break;case SOCK_DGRAM: case SOCK_STREAM: conn = netconn_new_with_callback(DOMAIN_TO_NETCONN_TYPE(domain, NETCONN_TCP), DEFAULT_SOCKET_EVENTCB); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_STREAM, %d) = ", domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol)); break;case SOCK_STREAM: default: LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%d, %d/UNKNOWN, %d) = -1\n", domain, type, protocol)); set_errno(EINVAL); return -1;default }switch (type) { ... } if (!conn) { LWIP_DEBUGF(SOCKETS_DEBUG, ("-1 / ENOBUFS (could not create netconn)\n")); set_errno(ENOBUFS); return -1; }if (!conn) { ... } i = alloc_socket(conn, 0); if (i == -1) { netconn_delete(conn); set_errno(ENFILE); return -1; }if (i == -1) { ... } conn->socket = i; done_socket(&sockets[i - LWIP_SOCKET_OFFSET]); LWIP_DEBUGF(SOCKETS_DEBUG, ("%d\n", i)); set_errno(0); return i; }lwip_socket (int domain, int type, int protocol) { ... } ssize_t lwip_write(int s, const void *data, size_t size) { return lwip_send(s, data, size, 0); }lwip_write (int s, const void *data, size_t size) { ... } ssize_t lwip_writev(int s, const struct iovec *iov, int iovcnt) { struct msghdr msg; msg.msg_name = NULL; msg.msg_namelen = 0; /* Hack: we have to cast via number to cast from 'const' pointer to non-const. Blame the opengroup standard for this inconsistency. *//* ... */ msg.msg_iov = LWIP_CONST_CAST(struct iovec *, iov); msg.msg_iovlen = iovcnt; msg.msg_control = NULL; msg.msg_controllen = 0; msg.msg_flags = 0; return lwip_sendmsg(s, &msg, 0); }lwip_writev (int s, const struct iovec *iov, int iovcnt) { ... } #if LWIP_SOCKET_SELECT || LWIP_SOCKET_POLL /* Add select_cb to select_cb_list. */ static void lwip_link_select_cb(struct lwip_select_cb *select_cb) { LWIP_SOCKET_SELECT_DECL_PROTECT(lev); /* Protect the select_cb_list */ LWIP_SOCKET_SELECT_PROTECT(lev); /* Put this select_cb on top of list */ select_cb->next = select_cb_list; if (select_cb_list != NULL) { select_cb_list->prev = select_cb; }if (select_cb_list != NULL) { ... } select_cb_list = select_cb; #if !LWIP_TCPIP_CORE_LOCKING /* Increasing this counter tells select_check_waiters that the list has changed. */ select_cb_ctr++;/* ... */ #endif /* Now we can safely unprotect */ LWIP_SOCKET_SELECT_UNPROTECT(lev); }lwip_link_select_cb (struct lwip_select_cb *select_cb) { ... } /* Remove select_cb from select_cb_list. */ static void lwip_unlink_select_cb(struct lwip_select_cb *select_cb) { LWIP_SOCKET_SELECT_DECL_PROTECT(lev); /* Take us off the list */ LWIP_SOCKET_SELECT_PROTECT(lev); if (select_cb->next != NULL) { select_cb->next->prev = select_cb->prev; }if (select_cb->next != NULL) { ... } if (select_cb_list == select_cb) { LWIP_ASSERT("select_cb->prev == NULL", select_cb->prev == NULL); select_cb_list = select_cb->next; }if (select_cb_list == select_cb) { ... } else { LWIP_ASSERT("select_cb->prev != NULL", select_cb->prev != NULL); select_cb->prev->next = select_cb->next; }else { ... } #if !LWIP_TCPIP_CORE_LOCKING /* Increasing this counter tells select_check_waiters that the list has changed. */ select_cb_ctr++;/* ... */ #endif LWIP_SOCKET_SELECT_UNPROTECT(lev); }lwip_unlink_select_cb (struct lwip_select_cb *select_cb) { ... } /* ... */#endif /* LWIP_SOCKET_SELECT || LWIP_SOCKET_POLL */ #if LWIP_SOCKET_SELECT /** * Go through the readset and writeset lists and see which socket of the sockets * set in the sets has events. On return, readset, writeset and exceptset have * the sockets enabled that had events. * * @param maxfdp1 the highest socket index in the sets * @param readset_in set of sockets to check for read events * @param writeset_in set of sockets to check for write events * @param exceptset_in set of sockets to check for error events * @param readset_out set of sockets that had read events * @param writeset_out set of sockets that had write events * @param exceptset_out set os sockets that had error events * @return number of sockets that had events (read/write/exception) (>= 0) *//* ... */ static int lwip_selscan(int maxfdp1, fd_set *readset_in, fd_set *writeset_in, fd_set *exceptset_in, fd_set *readset_out, fd_set *writeset_out, fd_set *exceptset_out) { int i, nready = 0; fd_set lreadset, lwriteset, lexceptset; struct lwip_sock *sock; SYS_ARCH_DECL_PROTECT(lev); FD_ZERO(&lreadset); FD_ZERO(&lwriteset); FD_ZERO(&lexceptset); /* Go through each socket in each list to count number of sockets which currently match *//* ... */ for (i = LWIP_SOCKET_OFFSET; i < maxfdp1; i++) { /* if this FD is not in the set, continue */ if (!(readset_in && FD_ISSET(i, readset_in)) && !(writeset_in && FD_ISSET(i, writeset_in)) && !(exceptset_in && FD_ISSET(i, exceptset_in))) { continue; }if (!(readset_in && FD_ISSET(i, readset_in)) && !(writeset_in && FD_ISSET(i, writeset_in)) && !(exceptset_in && FD_ISSET(i, exceptset_in))) { ... } /* First get the socket's status (protected)... */ SYS_ARCH_PROTECT(lev); sock = tryget_socket_unconn_locked(i); if (sock != NULL) { void *lastdata = sock->lastdata.pbuf; s16_t rcvevent = sock->rcvevent; u16_t sendevent = sock->sendevent; u16_t errevent = sock->errevent; SYS_ARCH_UNPROTECT(lev); /* ... then examine it: */ /* See if netconn of this socket is ready for read */ if (readset_in && FD_ISSET(i, readset_in) && ((lastdata != NULL) || (rcvevent > 0))) { FD_SET(i, &lreadset); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_selscan: fd=%d ready for reading\n", i)); nready++; }if (readset_in && FD_ISSET(i, readset_in) && ((lastdata != NULL) || (rcvevent > 0))) { ... } /* See if netconn of this socket is ready for write */ if (writeset_in && FD_ISSET(i, writeset_in) && (sendevent != 0)) { FD_SET(i, &lwriteset); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_selscan: fd=%d ready for writing\n", i)); nready++; }if (writeset_in && FD_ISSET(i, writeset_in) && (sendevent != 0)) { ... } /* See if netconn of this socket had an error */ if (exceptset_in && FD_ISSET(i, exceptset_in) && (errevent != 0)) { FD_SET(i, &lexceptset); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_selscan: fd=%d ready for exception\n", i)); nready++; }if (exceptset_in && FD_ISSET(i, exceptset_in) && (errevent != 0)) { ... } done_socket(sock); }if (sock != NULL) { ... } else { SYS_ARCH_UNPROTECT(lev); /* no a valid open socket */ return -1; }else { ... } }for (i = LWIP_SOCKET_OFFSET; i < maxfdp1; i++) { ... } /* copy local sets to the ones provided as arguments */ *readset_out = lreadset; *writeset_out = lwriteset; *exceptset_out = lexceptset; LWIP_ASSERT("nready >= 0", nready >= 0); return nready; }lwip_selscan (int maxfdp1, fd_set *readset_in, fd_set *writeset_in, fd_set *exceptset_in, fd_set *readset_out, fd_set *writeset_out, fd_set *exceptset_out) { ... } #if LWIP_NETCONN_FULLDUPLEX /* Mark all of the set sockets in one of the three fdsets passed to select as used. * All sockets are marked (and later unmarked), whether they are open or not. * This is OK as lwip_selscan aborts select when non-open sockets are found. *//* ... */ static void lwip_select_inc_sockets_used_set(int maxfdp, fd_set *fdset, fd_set *used_sockets) { SYS_ARCH_DECL_PROTECT(lev); if (fdset) { int i; for (i = LWIP_SOCKET_OFFSET; i < maxfdp; i++) { /* if this FD is in the set, lock it (unless already done) */ if (FD_ISSET(i, fdset) && !FD_ISSET(i, used_sockets)) { struct lwip_sock *sock; SYS_ARCH_PROTECT(lev); sock = tryget_socket_unconn_locked(i); if (sock != NULL) { /* leave the socket used until released by lwip_select_dec_sockets_used */ FD_SET(i, used_sockets); }if (sock != NULL) { ... } SYS_ARCH_UNPROTECT(lev); }if (FD_ISSET(i, fdset) && !FD_ISSET(i, used_sockets)) { ... } }for (i = LWIP_SOCKET_OFFSET; i < maxfdp; i++) { ... } }if (fdset) { ... } }lwip_select_inc_sockets_used_set (int maxfdp, fd_set *fdset, fd_set *used_sockets) { ... } /* Mark all sockets passed to select as used to prevent them from being freed * from other threads while select is running. * Marked sockets are added to 'used_sockets' to mark them only once an be able * to unmark them correctly. *//* ... */ static void lwip_select_inc_sockets_used(int maxfdp, fd_set *fdset1, fd_set *fdset2, fd_set *fdset3, fd_set *used_sockets) { FD_ZERO(used_sockets); lwip_select_inc_sockets_used_set(maxfdp, fdset1, used_sockets); lwip_select_inc_sockets_used_set(maxfdp, fdset2, used_sockets); lwip_select_inc_sockets_used_set(maxfdp, fdset3, used_sockets); }lwip_select_inc_sockets_used (int maxfdp, fd_set *fdset1, fd_set *fdset2, fd_set *fdset3, fd_set *used_sockets) { ... } /* Let go all sockets that were marked as used when starting select */ static void lwip_select_dec_sockets_used(int maxfdp, fd_set *used_sockets) { int i; for (i = LWIP_SOCKET_OFFSET; i < maxfdp; i++) { /* if this FD is not in the set, continue */ if (FD_ISSET(i, used_sockets)) { struct lwip_sock *sock = tryget_socket_unconn_nouse(i); LWIP_ASSERT("socket gone at the end of select", sock != NULL); if (sock != NULL) { done_socket(sock); }if (sock != NULL) { ... } }if (FD_ISSET(i, used_sockets)) { ... } }for (i = LWIP_SOCKET_OFFSET; i < maxfdp; i++) { ... } }lwip_select_dec_sockets_used (int maxfdp, fd_set *used_sockets) { ... } /* ... */#else /* LWIP_NETCONN_FULLDUPLEX */ #define lwip_select_inc_sockets_used(maxfdp1, readset, writeset, exceptset, used_sockets) #define lwip_select_dec_sockets_used(maxfdp1, used_sockets) /* ... */#endif /* LWIP_NETCONN_FULLDUPLEX */ int lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, struct timeval *timeout) { u32_t waitres = 0; int nready; fd_set lreadset, lwriteset, lexceptset; u32_t msectimeout; int i; int maxfdp2; #if LWIP_NETCONN_SEM_PER_THREAD int waited = 0; #endif #if LWIP_NETCONN_FULLDUPLEX fd_set used_sockets; #endif SYS_ARCH_DECL_PROTECT(lev); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select(%d, %p, %p, %p, tvsec=%"S32_F" tvusec=%"S32_F")\n", maxfdp1, (void *)readset, (void *) writeset, (void *) exceptset, timeout ? (s32_t)timeout->tv_sec : (s32_t) - 1, timeout ? (s32_t)timeout->tv_usec : (s32_t) - 1)); if ((maxfdp1 < 0) || (maxfdp1 > LWIP_SELECT_MAXNFDS)) { set_errno(EINVAL); return -1; }if ((maxfdp1 < 0) || (maxfdp1 > LWIP_SELECT_MAXNFDS)) { ... } lwip_select_inc_sockets_used(maxfdp1, readset, writeset, exceptset, &used_sockets); /* Go through each socket in each list to count number of sockets which currently match *//* ... */ nready = lwip_selscan(maxfdp1, readset, writeset, exceptset, &lreadset, &lwriteset, &lexceptset); if (nready < 0) { /* one of the sockets in one of the fd_sets was invalid */ set_errno(EBADF); lwip_select_dec_sockets_used(maxfdp1, &used_sockets); return -1; }if (nready < 0) { ... } else if (nready > 0) { /* one or more sockets are set, no need to wait */ LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: nready=%d\n", nready)); }else if (nready > 0) { ... } else { /* If we don't have any current events, then suspend if we are supposed to */ if (timeout && timeout->tv_sec == 0 && timeout->tv_usec == 0) { LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: no timeout, returning 0\n")); /* This is OK as the local fdsets are empty and nready is zero, or we would have returned earlier. *//* ... */ }if (timeout && timeout->tv_sec == 0 && timeout->tv_usec == 0) { ... } else { /* None ready: add our semaphore to list: We don't actually need any dynamic memory. Our entry on the list is only valid while we are in this function, so it's ok to use local variables (unless we're running in MPU compatible mode). *//* ... */ API_SELECT_CB_VAR_DECLARE(select_cb); API_SELECT_CB_VAR_ALLOC(select_cb, set_errno(ENOMEM); lwip_select_dec_sockets_used(maxfdp1, &used_sockets); return -1); memset(&API_SELECT_CB_VAR_REF(select_cb), 0, sizeof(struct lwip_select_cb)); API_SELECT_CB_VAR_REF(select_cb).readset = readset; API_SELECT_CB_VAR_REF(select_cb).writeset = writeset; API_SELECT_CB_VAR_REF(select_cb).exceptset = exceptset; #if LWIP_NETCONN_SEM_PER_THREAD API_SELECT_CB_VAR_REF(select_cb).sem = LWIP_NETCONN_THREAD_SEM_GET(); #else /* LWIP_NETCONN_SEM_PER_THREAD */ if (sys_sem_new(&API_SELECT_CB_VAR_REF(select_cb).sem, 0) != ERR_OK) { /* failed to create semaphore */ set_errno(ENOMEM); lwip_select_dec_sockets_used(maxfdp1, &used_sockets); API_SELECT_CB_VAR_FREE(select_cb); return -1; }if (sys_sem_new(&API_SELECT_CB_VAR_REF(select_cb).sem, 0) != ERR_OK) { ... } /* ... */#endif /* LWIP_NETCONN_SEM_PER_THREAD */ lwip_link_select_cb(&API_SELECT_CB_VAR_REF(select_cb)); /* Increase select_waiting for each socket we are interested in */ maxfdp2 = maxfdp1; for (i = LWIP_SOCKET_OFFSET; i < maxfdp1; i++) { if ((readset && FD_ISSET(i, readset)) || (writeset && FD_ISSET(i, writeset)) || (exceptset && FD_ISSET(i, exceptset))) { struct lwip_sock *sock; SYS_ARCH_PROTECT(lev); sock = tryget_socket_unconn_locked(i); if (sock != NULL) { sock->select_waiting++; if (sock->select_waiting == 0) { /* overflow - too many threads waiting */ sock->select_waiting--; nready = -1; maxfdp2 = i; SYS_ARCH_UNPROTECT(lev); done_socket(sock); set_errno(EBUSY); break; }if (sock->select_waiting == 0) { ... } SYS_ARCH_UNPROTECT(lev); done_socket(sock); }if (sock != NULL) { ... } else { /* Not a valid socket */ nready = -1; maxfdp2 = i; SYS_ARCH_UNPROTECT(lev); set_errno(EBADF); break; }else { ... } }if ((readset && FD_ISSET(i, readset)) || (writeset && FD_ISSET(i, writeset)) || (exceptset && FD_ISSET(i, exceptset))) { ... } }for (i = LWIP_SOCKET_OFFSET; i < maxfdp1; i++) { ... } if (nready >= 0) { /* Call lwip_selscan again: there could have been events between the last scan (without us on the list) and putting us on the list! *//* ... */ nready = lwip_selscan(maxfdp1, readset, writeset, exceptset, &lreadset, &lwriteset, &lexceptset); if (!nready) { /* Still none ready, just wait to be woken */ if (timeout == 0) { /* Wait forever */ msectimeout = 0; }if (timeout == 0) { ... } else { long msecs_long = ((timeout->tv_sec * 1000) + ((timeout->tv_usec + 500) / 1000)); if (msecs_long <= 0) { /* Wait 1ms at least (0 means wait forever) */ msectimeout = 1; }if (msecs_long <= 0) { ... } else { msectimeout = (u32_t)msecs_long; }else { ... } }else { ... } waitres = sys_arch_sem_wait(SELECT_SEM_PTR(API_SELECT_CB_VAR_REF(select_cb).sem), msectimeout); #if LWIP_NETCONN_SEM_PER_THREAD waited = 1; #endif }if (!nready) { ... } }if (nready >= 0) { ... } /* Decrease select_waiting for each socket we are interested in */ for (i = LWIP_SOCKET_OFFSET; i < maxfdp2; i++) { if ((readset && FD_ISSET(i, readset)) || (writeset && FD_ISSET(i, writeset)) || (exceptset && FD_ISSET(i, exceptset))) { struct lwip_sock *sock; SYS_ARCH_PROTECT(lev); sock = tryget_socket_unconn_locked(i); if (sock != NULL) { /* for now, handle select_waiting==0... */ LWIP_ASSERT("sock->select_waiting > 0", sock->select_waiting > 0); if (sock->select_waiting > 0) { sock->select_waiting--; }if (sock->select_waiting > 0) { ... } SYS_ARCH_UNPROTECT(lev); done_socket(sock); }if (sock != NULL) { ... } else { SYS_ARCH_UNPROTECT(lev); /* Not a valid socket */ nready = -1; set_errno(EBADF); }else { ... } }if ((readset && FD_ISSET(i, readset)) || (writeset && FD_ISSET(i, writeset)) || (exceptset && FD_ISSET(i, exceptset))) { ... } }for (i = LWIP_SOCKET_OFFSET; i < maxfdp2; i++) { ... } lwip_unlink_select_cb(&API_SELECT_CB_VAR_REF(select_cb)); #if LWIP_NETCONN_SEM_PER_THREAD if (API_SELECT_CB_VAR_REF(select_cb).sem_signalled && (!waited || (waitres == SYS_ARCH_TIMEOUT))) { /* don't leave the thread-local semaphore signalled */ sys_arch_sem_wait(API_SELECT_CB_VAR_REF(select_cb).sem, 1); }if (API_SELECT_CB_VAR_REF(select_cb).sem_signalled && (!waited || (waitres == SYS_ARCH_TIMEOUT))) { ... } /* ... */#else /* LWIP_NETCONN_SEM_PER_THREAD */ sys_sem_free(&API_SELECT_CB_VAR_REF(select_cb).sem); #endif /* LWIP_NETCONN_SEM_PER_THREAD */ API_SELECT_CB_VAR_FREE(select_cb); if (nready < 0) { /* This happens when a socket got closed while waiting */ lwip_select_dec_sockets_used(maxfdp1, &used_sockets); return -1; }if (nready < 0) { ... } if (waitres == SYS_ARCH_TIMEOUT) { /* Timeout */ LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: timeout expired\n")); /* This is OK as the local fdsets are empty and nready is zero, or we would have returned earlier. *//* ... */ }if (waitres == SYS_ARCH_TIMEOUT) { ... } else { /* See what's set now after waiting */ nready = lwip_selscan(maxfdp1, readset, writeset, exceptset, &lreadset, &lwriteset, &lexceptset); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: nready=%d\n", nready)); }else { ... } }else { ... } }else { ... } lwip_select_dec_sockets_used(maxfdp1, &used_sockets); set_errno(0); if (readset) { *readset = lreadset; }if (readset) { ... } if (writeset) { *writeset = lwriteset; }if (writeset) { ... } if (exceptset) { *exceptset = lexceptset; }if (exceptset) { ... } return nready; }lwip_select (int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, struct timeval *timeout) { ... } /* ... */#endif /* LWIP_SOCKET_SELECT */ #if LWIP_SOCKET_POLL /** Options for the lwip_pollscan function. */ enum lwip_pollscan_opts { /** Clear revents in each struct pollfd. */ LWIP_POLLSCAN_CLEAR = 1, /** Increment select_waiting in each struct lwip_sock. */ LWIP_POLLSCAN_INC_WAIT = 2, /** Decrement select_waiting in each struct lwip_sock. */ LWIP_POLLSCAN_DEC_WAIT = 4 ...}; /** * Update revents in each struct pollfd. * Optionally update select_waiting in struct lwip_sock. * * @param fds array of structures to update * @param nfds number of structures in fds * @param opts what to update and how * @return number of structures that have revents != 0 *//* ... */ static int lwip_pollscan(struct pollfd *fds, nfds_t nfds, enum lwip_pollscan_opts opts) { int nready = 0; nfds_t fdi; struct lwip_sock *sock; SYS_ARCH_DECL_PROTECT(lev); /* Go through each struct pollfd in the array. */ for (fdi = 0; fdi < nfds; fdi++) { if ((opts & LWIP_POLLSCAN_CLEAR) != 0) { fds[fdi].revents = 0; }if ((opts & LWIP_POLLSCAN_CLEAR) != 0) { ... } /* Negative fd means the caller wants us to ignore this struct. POLLNVAL means we already detected that the fd is invalid; if another thread has since opened a new socket with that fd, we must not use that socket. *//* ... */ if (fds[fdi].fd >= 0 && (fds[fdi].revents & POLLNVAL) == 0) { /* First get the socket's status (protected)... */ SYS_ARCH_PROTECT(lev); sock = tryget_socket_unconn_locked(fds[fdi].fd); if (sock != NULL) { void* lastdata = sock->lastdata.pbuf; s16_t rcvevent = sock->rcvevent; u16_t sendevent = sock->sendevent; u16_t errevent = sock->errevent; if ((opts & LWIP_POLLSCAN_INC_WAIT) != 0) { sock->select_waiting++; if (sock->select_waiting == 0) { /* overflow - too many threads waiting */ sock->select_waiting--; nready = -1; SYS_ARCH_UNPROTECT(lev); done_socket(sock); break; }if (sock->select_waiting == 0) { ... } }if ((opts & LWIP_POLLSCAN_INC_WAIT) != 0) { ... } else if ((opts & LWIP_POLLSCAN_DEC_WAIT) != 0) { /* for now, handle select_waiting==0... */ LWIP_ASSERT("sock->select_waiting > 0", sock->select_waiting > 0); if (sock->select_waiting > 0) { sock->select_waiting--; }if (sock->select_waiting > 0) { ... } }else if ((opts & LWIP_POLLSCAN_DEC_WAIT) != 0) { ... } SYS_ARCH_UNPROTECT(lev); done_socket(sock); /* ... then examine it: */ /* See if netconn of this socket is ready for read */ if ((fds[fdi].events & POLLIN) != 0 && ((lastdata != NULL) || (rcvevent > 0))) { fds[fdi].revents |= POLLIN; LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_pollscan: fd=%d ready for reading\n", fds[fdi].fd)); }if ((fds[fdi].events & POLLIN) != 0 && ((lastdata != NULL) || (rcvevent > 0))) { ... } /* See if netconn of this socket is ready for write */ if ((fds[fdi].events & POLLOUT) != 0 && (sendevent != 0)) { fds[fdi].revents |= POLLOUT; LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_pollscan: fd=%d ready for writing\n", fds[fdi].fd)); }if ((fds[fdi].events & POLLOUT) != 0 && (sendevent != 0)) { ... } /* See if netconn of this socket had an error */ if (errevent != 0) { /* POLLERR is output only. */ fds[fdi].revents |= POLLERR; LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_pollscan: fd=%d ready for exception\n", fds[fdi].fd)); }if (errevent != 0) { ... } }if (sock != NULL) { ... } else { /* Not a valid socket */ SYS_ARCH_UNPROTECT(lev); /* POLLNVAL is output only. */ fds[fdi].revents |= POLLNVAL; return -1; }else { ... } }if (fds[fdi].fd >= 0 && (fds[fdi].revents & POLLNVAL) == 0) { ... } /* Will return the number of structures that have events, not the number of events. *//* ... */ if (fds[fdi].revents != 0) { nready++; }if (fds[fdi].revents != 0) { ... } }for (fdi = 0; fdi < nfds; fdi++) { ... } LWIP_ASSERT("nready >= 0", nready >= 0); return nready; }lwip_pollscan (struct pollfd *fds, nfds_t nfds, enum lwip_pollscan_opts opts) { ... } #if LWIP_NETCONN_FULLDUPLEX /* Mark all sockets as used. * * All sockets are marked (and later unmarked), whether they are open or not. * This is OK as lwip_pollscan aborts select when non-open sockets are found. *//* ... */ static void lwip_poll_inc_sockets_used(struct pollfd *fds, nfds_t nfds) { nfds_t fdi; if(fds) { /* Go through each struct pollfd in the array. */ for (fdi = 0; fdi < nfds; fdi++) { /* Increase the reference counter */ tryget_socket_unconn(fds[fdi].fd); }for (fdi = 0; fdi < nfds; fdi++) { ... } }if (fds) { ... } }lwip_poll_inc_sockets_used (struct pollfd *fds, nfds_t nfds) { ... } /* Let go all sockets that were marked as used when starting poll */ static void lwip_poll_dec_sockets_used(struct pollfd *fds, nfds_t nfds) { nfds_t fdi; if(fds) { /* Go through each struct pollfd in the array. */ for (fdi = 0; fdi < nfds; fdi++) { struct lwip_sock *sock = tryget_socket_unconn_nouse(fds[fdi].fd); if (sock != NULL) { done_socket(sock); }if (sock != NULL) { ... } }for (fdi = 0; fdi < nfds; fdi++) { ... } }if (fds) { ... } }lwip_poll_dec_sockets_used (struct pollfd *fds, nfds_t nfds) { ... } /* ... */#else /* LWIP_NETCONN_FULLDUPLEX */ #define lwip_poll_inc_sockets_used(fds, nfds) #define lwip_poll_dec_sockets_used(fds, nfds) /* ... */#endif /* LWIP_NETCONN_FULLDUPLEX */ int lwip_poll(struct pollfd *fds, nfds_t nfds, int timeout) { u32_t waitres = 0; int nready; u32_t msectimeout; #if LWIP_NETCONN_SEM_PER_THREAD int waited = 0; #endif LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_poll(%p, %d, %d)\n", (void*)fds, (int)nfds, timeout)); LWIP_ERROR("lwip_poll: invalid fds", ((fds != NULL && nfds > 0) || (fds == NULL && nfds == 0)), set_errno(EINVAL); return -1;); lwip_poll_inc_sockets_used(fds, nfds); /* Go through each struct pollfd to count number of structures which currently match *//* ... */ nready = lwip_pollscan(fds, nfds, LWIP_POLLSCAN_CLEAR); if (nready < 0) { lwip_poll_dec_sockets_used(fds, nfds); return -1; }if (nready < 0) { ... } /* If we don't have any current events, then suspend if we are supposed to */ if (!nready) { API_SELECT_CB_VAR_DECLARE(select_cb); if (timeout == 0) { LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_poll: no timeout, returning 0\n")); goto return_success; }if (timeout == 0) { ... } API_SELECT_CB_VAR_ALLOC(select_cb, set_errno(EAGAIN); lwip_poll_dec_sockets_used(fds, nfds); return -1); memset(&API_SELECT_CB_VAR_REF(select_cb), 0, sizeof(struct lwip_select_cb)); /* None ready: add our semaphore to list: We don't actually need any dynamic memory. Our entry on the list is only valid while we are in this function, so it's ok to use local variables. *//* ... */ API_SELECT_CB_VAR_REF(select_cb).poll_fds = fds; API_SELECT_CB_VAR_REF(select_cb).poll_nfds = nfds; #if LWIP_NETCONN_SEM_PER_THREAD API_SELECT_CB_VAR_REF(select_cb).sem = LWIP_NETCONN_THREAD_SEM_GET(); #else /* LWIP_NETCONN_SEM_PER_THREAD */ if (sys_sem_new(&API_SELECT_CB_VAR_REF(select_cb).sem, 0) != ERR_OK) { /* failed to create semaphore */ set_errno(EAGAIN); lwip_poll_dec_sockets_used(fds, nfds); API_SELECT_CB_VAR_FREE(select_cb); return -1; }if (sys_sem_new(&API_SELECT_CB_VAR_REF(select_cb).sem, 0) != ERR_OK) { ... } /* ... */#endif /* LWIP_NETCONN_SEM_PER_THREAD */ lwip_link_select_cb(&API_SELECT_CB_VAR_REF(select_cb)); /* Increase select_waiting for each socket we are interested in. Also, check for events again: there could have been events between the last scan (without us on the list) and putting us on the list! *//* ... */ nready = lwip_pollscan(fds, nfds, LWIP_POLLSCAN_INC_WAIT); if (!nready) { /* Still none ready, just wait to be woken */ if (timeout < 0) { /* Wait forever */ msectimeout = 0; }if (timeout < 0) { ... } else { /* timeout == 0 would have been handled earlier. */ LWIP_ASSERT("timeout > 0", timeout > 0); msectimeout = timeout; }else { ... } waitres = sys_arch_sem_wait(SELECT_SEM_PTR(API_SELECT_CB_VAR_REF(select_cb).sem), msectimeout); #if LWIP_NETCONN_SEM_PER_THREAD waited = 1; #endif }if (!nready) { ... } /* Decrease select_waiting for each socket we are interested in, and check which events occurred while we waited. *//* ... */ nready = lwip_pollscan(fds, nfds, LWIP_POLLSCAN_DEC_WAIT); lwip_unlink_select_cb(&API_SELECT_CB_VAR_REF(select_cb)); #if LWIP_NETCONN_SEM_PER_THREAD if (select_cb.sem_signalled && (!waited || (waitres == SYS_ARCH_TIMEOUT))) { /* don't leave the thread-local semaphore signalled */ sys_arch_sem_wait(API_SELECT_CB_VAR_REF(select_cb).sem, 1); }if (select_cb.sem_signalled && (!waited || (waitres == SYS_ARCH_TIMEOUT))) { ... } /* ... */#else /* LWIP_NETCONN_SEM_PER_THREAD */ sys_sem_free(&API_SELECT_CB_VAR_REF(select_cb).sem); #endif /* LWIP_NETCONN_SEM_PER_THREAD */ API_SELECT_CB_VAR_FREE(select_cb); if (nready < 0) { /* This happens when a socket got closed while waiting */ lwip_poll_dec_sockets_used(fds, nfds); return -1; }if (nready < 0) { ... } if (waitres == SYS_ARCH_TIMEOUT) { /* Timeout */ LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_poll: timeout expired\n")); goto return_success; }if (waitres == SYS_ARCH_TIMEOUT) { ... } }if (!nready) { ... } LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_poll: nready=%d\n", nready)); return_success: lwip_poll_dec_sockets_used(fds, nfds); set_errno(0); return nready; }lwip_poll (struct pollfd *fds, nfds_t nfds, int timeout) { ... } /** * Check whether event_callback should wake up a thread waiting in * lwip_poll. *//* ... */ static int lwip_poll_should_wake(const struct lwip_select_cb *scb, int fd, int has_recvevent, int has_sendevent, int has_errevent) { nfds_t fdi; for (fdi = 0; fdi < scb->poll_nfds; fdi++) { const struct pollfd *pollfd = &scb->poll_fds[fdi]; if (pollfd->fd == fd) { /* Do not update pollfd->revents right here; that would be a data race because lwip_pollscan accesses revents without protecting. *//* ... */ if (has_recvevent && (pollfd->events & POLLIN) != 0) { return 1; }if (has_recvevent && (pollfd->events & POLLIN) != 0) { ... } if (has_sendevent && (pollfd->events & POLLOUT) != 0) { return 1; }if (has_sendevent && (pollfd->events & POLLOUT) != 0) { ... } if (has_errevent) { /* POLLERR is output only. */ return 1; }if (has_errevent) { ... } }if (pollfd->fd == fd) { ... } }for (fdi = 0; fdi < scb->poll_nfds; fdi++) { ... } return 0; }lwip_poll_should_wake (const struct lwip_select_cb *scb, int fd, int has_recvevent, int has_sendevent, int has_errevent) { ... } /* ... */#endif /* LWIP_SOCKET_POLL */ #if LWIP_SOCKET_SELECT || LWIP_SOCKET_POLL /** * Callback registered in the netconn layer for each socket-netconn. * Processes recvevent (data available) and wakes up tasks waiting for select. * * @note for LWIP_TCPIP_CORE_LOCKING any caller of this function * must have the core lock held when signaling the following events * as they might cause select_list_cb to be checked: * NETCONN_EVT_RCVPLUS * NETCONN_EVT_SENDPLUS * NETCONN_EVT_ERROR * This requirement will be asserted in select_check_waiters() *//* ... */ static void event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len) { int s, check_waiters; struct lwip_sock *sock; SYS_ARCH_DECL_PROTECT(lev); LWIP_UNUSED_ARG(len); /* Get socket */ if (conn) { s = conn->socket; if (s < 0) { /* Data comes in right away after an accept, even though * the server task might not have created a new socket yet. * Just count down (or up) if that's the case and we * will use the data later. Note that only receive events * can happen before the new socket is set up. *//* ... */ SYS_ARCH_PROTECT(lev); if (conn->socket < 0) { if (evt == NETCONN_EVT_RCVPLUS) { /* conn->socket is -1 on initialization lwip_accept adjusts sock->recvevent if conn->socket < -1 *//* ... */ conn->socket--; }if (evt == NETCONN_EVT_RCVPLUS) { ... } SYS_ARCH_UNPROTECT(lev); return; }if (conn->socket < 0) { ... } s = conn->socket; SYS_ARCH_UNPROTECT(lev); }if (s < 0) { ... } sock = get_socket(s); if (!sock) { return; }if (!sock) { ... } }if (conn) { ... } else { return; }else { ... } check_waiters = 1; SYS_ARCH_PROTECT(lev); /* Set event as required */ switch (evt) { case NETCONN_EVT_RCVPLUS: sock->rcvevent++; if (sock->rcvevent > 1) { check_waiters = 0; }if (sock->rcvevent > 1) { ... } break;case NETCONN_EVT_RCVPLUS: case NETCONN_EVT_RCVMINUS: sock->rcvevent--; check_waiters = 0; break;case NETCONN_EVT_RCVMINUS: case NETCONN_EVT_SENDPLUS: if (sock->sendevent) { check_waiters = 0; }if (sock->sendevent) { ... } sock->sendevent = 1; break;case NETCONN_EVT_SENDPLUS: case NETCONN_EVT_SENDMINUS: sock->sendevent = 0; check_waiters = 0; break;case NETCONN_EVT_SENDMINUS: case NETCONN_EVT_ERROR: sock->errevent = 1; break;case NETCONN_EVT_ERROR: default: LWIP_ASSERT("unknown event", 0); break;default }switch (evt) { ... } if (sock->select_waiting && check_waiters) { /* Save which events are active */ int has_recvevent, has_sendevent, has_errevent; has_recvevent = sock->rcvevent > 0; has_sendevent = sock->sendevent != 0; has_errevent = sock->errevent != 0; SYS_ARCH_UNPROTECT(lev); /* Check any select calls waiting on this socket */ select_check_waiters(s, has_recvevent, has_sendevent, has_errevent); }if (sock->select_waiting && check_waiters) { ... } else { SYS_ARCH_UNPROTECT(lev); }else { ... } done_socket(sock); }event_callback (struct netconn *conn, enum netconn_evt evt, u16_t len) { ... } /** * Check if any select waiters are waiting on this socket and its events * * @note on synchronization of select_cb_list: * LWIP_TCPIP_CORE_LOCKING: the select_cb_list must only be accessed while holding * the core lock. We do a single pass through the list and signal any waiters. * Core lock should already be held when calling here!!!! * !LWIP_TCPIP_CORE_LOCKING: we use SYS_ARCH_PROTECT but unlock on each iteration * of the loop, thus creating a possibility where a thread could modify the * select_cb_list during our UNPROTECT/PROTECT. We use a generational counter to * detect this change and restart the list walk. The list is expected to be small *//* ... */ static void select_check_waiters(int s, int has_recvevent, int has_sendevent, int has_errevent) { struct lwip_select_cb *scb; #if !LWIP_TCPIP_CORE_LOCKING int last_select_cb_ctr; SYS_ARCH_DECL_PROTECT(lev);/* ... */ #endif /* !LWIP_TCPIP_CORE_LOCKING */ LWIP_ASSERT_CORE_LOCKED(); #if !LWIP_TCPIP_CORE_LOCKING SYS_ARCH_PROTECT(lev); again: /* remember the state of select_cb_list to detect changes */ last_select_cb_ctr = select_cb_ctr;/* ... */ #endif /* !LWIP_TCPIP_CORE_LOCKING */ for (scb = select_cb_list; scb != NULL; scb = scb->next) { if (scb->sem_signalled == 0) { /* semaphore not signalled yet */ int do_signal = 0; #if LWIP_SOCKET_POLL if (scb->poll_fds != NULL) { do_signal = lwip_poll_should_wake(scb, s, has_recvevent, has_sendevent, has_errevent); }if (scb->poll_fds != NULL) { ... } /* ... */#endif /* LWIP_SOCKET_POLL */ #if LWIP_SOCKET_SELECT && LWIP_SOCKET_POLL else #endif /* LWIP_SOCKET_SELECT && LWIP_SOCKET_POLL */ #if LWIP_SOCKET_SELECT { /* Test this select call for our socket */ if (has_recvevent) { if (scb->readset && FD_ISSET(s, scb->readset)) { do_signal = 1; }if (scb->readset && FD_ISSET(s, scb->readset)) { ... } }if (has_recvevent) { ... } if (has_sendevent) { if (!do_signal && scb->writeset && FD_ISSET(s, scb->writeset)) { do_signal = 1; }if (!do_signal && scb->writeset && FD_ISSET(s, scb->writeset)) { ... } }if (has_sendevent) { ... } if (has_errevent) { if (!do_signal && scb->exceptset && FD_ISSET(s, scb->exceptset)) { do_signal = 1; }if (!do_signal && scb->exceptset && FD_ISSET(s, scb->exceptset)) { ... } }if (has_errevent) { ... } ...}/* ... */ #endif /* LWIP_SOCKET_SELECT */ if (do_signal) { scb->sem_signalled = 1; /* For !LWIP_TCPIP_CORE_LOCKING, we don't call SYS_ARCH_UNPROTECT() before signaling the semaphore, as this might lead to the select thread taking itself off the list, invalidating the semaphore. *//* ... */ sys_sem_signal(SELECT_SEM_PTR(scb->sem)); }if (do_signal) { ... } }if (scb->sem_signalled == 0) { ... } #if LWIP_TCPIP_CORE_LOCKING }for (scb = select_cb_list; scb != NULL; scb = scb->next) { ... } #else /* unlock interrupts with each step */ SYS_ARCH_UNPROTECT(lev); /* this makes sure interrupt protection time is short */ SYS_ARCH_PROTECT(lev); if (last_select_cb_ctr != select_cb_ctr) { /* someone has changed select_cb_list, restart at the beginning */ goto again; }if (last_select_cb_ctr != select_cb_ctr) { ... } /* remember the state of select_cb_list to detect changes */ last_select_cb_ctr = select_cb_ctr; }select_check_waiters (int s, int has_recvevent, int has_sendevent, int has_errevent) { ... } SYS_ARCH_UNPROTECT(lev); #endif }/* ... */ #endif /* LWIP_SOCKET_SELECT || LWIP_SOCKET_POLL */ /** * Close one end of a full-duplex connection. *//* ... */ int lwip_shutdown(int s, int how) { struct lwip_sock *sock; err_t err; u8_t shut_rx = 0, shut_tx = 0; LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_shutdown(%d, how=%d)\n", s, how)); sock = get_socket(s); if (!sock) { return -1; }if (!sock) { ... } if (sock->conn != NULL) { if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) != NETCONN_TCP) { sock_set_errno(sock, EOPNOTSUPP); done_socket(sock); return -1; }if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) != NETCONN_TCP) { ... } }if (sock->conn != NULL) { ... } else { sock_set_errno(sock, ENOTCONN); done_socket(sock); return -1; }else { ... } if (how == SHUT_RD) { shut_rx = 1; }if (how == SHUT_RD) { ... } else if (how == SHUT_WR) { shut_tx = 1; }else if (how == SHUT_WR) { ... } else if (how == SHUT_RDWR) { shut_rx = 1; shut_tx = 1; }else if (how == SHUT_RDWR) { ... } else { sock_set_errno(sock, EINVAL); done_socket(sock); return -1; }else { ... } err = netconn_shutdown(sock->conn, shut_rx, shut_tx); sock_set_errno(sock, err_to_errno(err)); done_socket(sock); return (err == ERR_OK ? 0 : -1); }lwip_shutdown (int s, int how) { ... } static int lwip_getaddrname(int s, struct sockaddr *name, socklen_t *namelen, u8_t local) { struct lwip_sock *sock; union sockaddr_aligned saddr; ip_addr_t naddr; u16_t port; err_t err; sock = get_socket(s); if (!sock) { return -1; }if (!sock) { ... } /* get the IP address and port */ err = netconn_getaddr(sock->conn, &naddr, &port, local); if (err != ERR_OK) { sock_set_errno(sock, err_to_errno(err)); done_socket(sock); return -1; }if (err != ERR_OK) { ... } #if LWIP_IPV4 && LWIP_IPV6 /* Dual-stack: Map IPv4 addresses to IPv4 mapped IPv6 */ if (NETCONNTYPE_ISIPV6(netconn_type(sock->conn)) && IP_IS_V4_VAL(naddr)) { ip4_2_ipv4_mapped_ipv6(ip_2_ip6(&naddr), ip_2_ip4(&naddr)); IP_SET_TYPE_VAL(naddr, IPADDR_TYPE_V6); }if (NETCONNTYPE_ISIPV6(netconn_type(sock->conn)) && IP_IS_V4_VAL(naddr)) { ... } /* ... */#endif /* LWIP_IPV4 && LWIP_IPV6 */ IPADDR_PORT_TO_SOCKADDR(&saddr, &naddr, port); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getaddrname(%d, addr=", s)); ip_addr_debug_print_val(SOCKETS_DEBUG, naddr); LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F")\n", port)); if (*namelen > saddr.sa.sa_len) { *namelen = saddr.sa.sa_len; }if (*namelen > saddr.sa.sa_len) { ... } MEMCPY(name, &saddr, *namelen); sock_set_errno(sock, 0); done_socket(sock); return 0; }lwip_getaddrname (int s, struct sockaddr *name, socklen_t *namelen, u8_t local) { ... } int lwip_getpeername(int s, struct sockaddr *name, socklen_t *namelen) { return lwip_getaddrname(s, name, namelen, 0); }lwip_getpeername (int s, struct sockaddr *name, socklen_t *namelen) { ... } int lwip_getsockname(int s, struct sockaddr *name, socklen_t *namelen) { return lwip_getaddrname(s, name, namelen, 1); }lwip_getsockname (int s, struct sockaddr *name, socklen_t *namelen) { ... } int lwip_getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen) { int err; struct lwip_sock *sock = get_socket(s); #if !LWIP_TCPIP_CORE_LOCKING err_t cberr; LWIP_SETGETSOCKOPT_DATA_VAR_DECLARE(data);/* ... */ #endif /* !LWIP_TCPIP_CORE_LOCKING */ if (!sock) { return -1; }if (!sock) { ... } if ((NULL == optval) || (NULL == optlen)) { sock_set_errno(sock, EFAULT); done_socket(sock); return -1; }if ((NULL == optval) || (NULL == optlen)) { ... } #if LWIP_TCPIP_CORE_LOCKING /* core-locking can just call the -impl function */ LOCK_TCPIP_CORE(); err = lwip_getsockopt_impl(s, level, optname, optval, optlen); UNLOCK_TCPIP_CORE(); /* ... */ #else /* LWIP_TCPIP_CORE_LOCKING */ #if LWIP_MPU_COMPATIBLE /* MPU_COMPATIBLE copies the optval data, so check for max size here */ if (*optlen > LWIP_SETGETSOCKOPT_MAXOPTLEN) { sock_set_errno(sock, ENOBUFS); done_socket(sock); return -1; }if (*optlen > LWIP_SETGETSOCKOPT_MAXOPTLEN) { ... } /* ... */#endif /* LWIP_MPU_COMPATIBLE */ LWIP_SETGETSOCKOPT_DATA_VAR_ALLOC(data, sock); LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).s = s; LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).level = level; LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optname = optname; LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optlen = *optlen; #if !LWIP_MPU_COMPATIBLE LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optval.p = optval; #endif /* !LWIP_MPU_COMPATIBLE */ LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).err = 0; #if LWIP_NETCONN_SEM_PER_THREAD LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).completed_sem = LWIP_NETCONN_THREAD_SEM_GET(); #else LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).completed_sem = &sock->conn->op_completed; #endif cberr = tcpip_callback(lwip_getsockopt_callback, &LWIP_SETGETSOCKOPT_DATA_VAR_REF(data)); if (cberr != ERR_OK) { LWIP_SETGETSOCKOPT_DATA_VAR_FREE(data); sock_set_errno(sock, err_to_errno(cberr)); done_socket(sock); return -1; }if (cberr != ERR_OK) { ... } sys_arch_sem_wait((sys_sem_t *)(LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).completed_sem), 0); /* write back optlen and optval */ *optlen = LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optlen; #if LWIP_MPU_COMPATIBLE MEMCPY(optval, LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optval, LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optlen);/* ... */ #endif /* LWIP_MPU_COMPATIBLE */ /* maybe lwip_getsockopt_internal has changed err */ err = LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).err; LWIP_SETGETSOCKOPT_DATA_VAR_FREE(data);/* ... */ #endif /* LWIP_TCPIP_CORE_LOCKING */ sock_set_errno(sock, err); done_socket(sock); return err ? -1 : 0; }lwip_getsockopt (int s, int level, int optname, void *optval, socklen_t *optlen) { ... } #if !LWIP_TCPIP_CORE_LOCKING /** lwip_getsockopt_callback: only used without CORE_LOCKING * to get into the tcpip_thread *//* ... */ static void lwip_getsockopt_callback(void *arg) { struct lwip_setgetsockopt_data *data; LWIP_ASSERT("arg != NULL", arg != NULL); data = (struct lwip_setgetsockopt_data *)arg; data->err = lwip_getsockopt_impl(data->s, data->level, data->optname, #if LWIP_MPU_COMPATIBLE data->optval, #else /* LWIP_MPU_COMPATIBLE */ data->optval.p, #endif /* LWIP_MPU_COMPATIBLE */ &data->optlen); sys_sem_signal((sys_sem_t *)(data->completed_sem)); }lwip_getsockopt_callback (void *arg) { ... } /* ... */#endif /* LWIP_TCPIP_CORE_LOCKING */ static int lwip_sockopt_to_ipopt(int optname) { /* Map SO_* values to our internal SOF_* values * We should not rely on #defines in socket.h * being in sync with ip.h. *//* ... */ switch (optname) { case SO_BROADCAST: return SOF_BROADCAST;case SO_BROADCAST: case SO_KEEPALIVE: return SOF_KEEPALIVE;case SO_KEEPALIVE: case SO_REUSEADDR: return SOF_REUSEADDR;case SO_REUSEADDR: default: LWIP_ASSERT("Unknown socket option", 0); return 0;default }switch (optname) { ... } }lwip_sockopt_to_ipopt (int optname) { ... } /** lwip_getsockopt_impl: the actual implementation of getsockopt: * same argument as lwip_getsockopt, either called directly or through callback *//* ... */ static int lwip_getsockopt_impl(int s, int level, int optname, void *optval, socklen_t *optlen) { int err = 0; struct lwip_sock *sock = tryget_socket(s); if (!sock) { return EBADF; }if (!sock) { ... } #ifdef LWIP_HOOK_SOCKETS_GETSOCKOPT if (LWIP_HOOK_SOCKETS_GETSOCKOPT(s, sock, level, optname, optval, optlen, &err)) { return err; }if (LWIP_HOOK_SOCKETS_GETSOCKOPT(s, sock, level, optname, optval, optlen, &err)) { ... } /* ... */#endif switch (level) { /* Level: SOL_SOCKET */ case SOL_SOCKET: switch (optname) { #if LWIP_TCP case SO_ACCEPTCONN: LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, int); if (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_TCP) { done_socket(sock); return ENOPROTOOPT; }if (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_TCP) { ... } if ((sock->conn->pcb.tcp != NULL) && (sock->conn->pcb.tcp->state == LISTEN)) { *(int *)optval = 1; }if ((sock->conn->pcb.tcp != NULL) && (sock->conn->pcb.tcp->state == LISTEN)) { ... } else { *(int *)optval = 0; }else { ... } break;/* ... */ #endif /* LWIP_TCP */ /* The option flags */case SO_ACCEPTCONN: case SO_BROADCAST: case SO_KEEPALIVE: #if SO_REUSEcase SO_KEEPALIVE: case SO_REUSEADDR: #endif /* SO_REUSE */ if ((optname == SO_BROADCAST) && (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_UDP)) { done_socket(sock); return ENOPROTOOPT; }if ((optname == SO_BROADCAST) && (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_UDP)) { ... } optname = lwip_sockopt_to_ipopt(optname); LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, int); *(int *)optval = ip_get_option(sock->conn->pcb.ip, optname); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, optname=0x%x, ..) = %s\n", s, optname, (*(int *)optval ? "on" : "off"))); break; case SO_REUSEADDR: case SO_TYPE: LWIP_SOCKOPT_CHECK_OPTLEN_CONN(sock, *optlen, int); switch (NETCONNTYPE_GROUP(netconn_type(sock->conn))) { case NETCONN_RAW: *(int *)optval = SOCK_RAW; break;case NETCONN_RAW: case NETCONN_TCP: *(int *)optval = SOCK_STREAM; break;case NETCONN_TCP: case NETCONN_UDP: *(int *)optval = SOCK_DGRAM; break;case NETCONN_UDP: default: /* unrecognized socket type */ *(int *)optval = netconn_type(sock->conn); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, SO_TYPE): unrecognized socket type %d\n", s, *(int *)optval));default }switch (NETCONNTYPE_GROUP(netconn_type(sock->conn))) { ... } /* switch (netconn_type(sock->conn)) */ LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, SO_TYPE) = %d\n", s, *(int *)optval)); break; case SO_TYPE: case SO_ERROR: LWIP_SOCKOPT_CHECK_OPTLEN(sock, *optlen, int); *(int *)optval = err_to_errno(netconn_err(sock->conn)); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, SO_ERROR) = %d\n", s, *(int *)optval)); break; #if LWIP_SO_SNDTIMEOcase SO_ERROR: case SO_SNDTIMEO: LWIP_SOCKOPT_CHECK_OPTLEN_CONN(sock, *optlen, LWIP_SO_SNDRCVTIMEO_OPTTYPE); LWIP_SO_SNDRCVTIMEO_SET(optval, netconn_get_sendtimeout(sock->conn)); break;/* ... */ #endif /* LWIP_SO_SNDTIMEO */ #if LWIP_SO_RCVTIMEOcase SO_SNDTIMEO: case SO_RCVTIMEO: LWIP_SOCKOPT_CHECK_OPTLEN_CONN(sock, *optlen, LWIP_SO_SNDRCVTIMEO_OPTTYPE); LWIP_SO_SNDRCVTIMEO_SET(optval, netconn_get_recvtimeout(sock->conn)); break;/* ... */ #endif /* LWIP_SO_RCVTIMEO */ #if LWIP_SO_RCVBUFcase SO_RCVTIMEO: case SO_RCVBUF: LWIP_SOCKOPT_CHECK_OPTLEN_CONN(sock, *optlen, int); *(int *)optval = netconn_get_recvbufsize(sock->conn); break;/* ... */ #endif /* LWIP_SO_RCVBUF */ #if LWIP_SO_LINGERcase SO_RCVBUF: case SO_LINGER: { s16_t conn_linger; struct linger *linger = (struct linger *)optval; LWIP_SOCKOPT_CHECK_OPTLEN_CONN(sock, *optlen, struct linger); conn_linger = sock->conn->linger; if (conn_linger >= 0) { linger->l_onoff = 1; linger->l_linger = (int)conn_linger; }if (conn_linger >= 0) { ... } else { linger->l_onoff = 0; linger->l_linger = 0; }else { ... } ...} break;/* ... */ #endif /* LWIP_SO_LINGER */ #if LWIP_UDPcase SO_LINGER: case SO_NO_CHECK: LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, *optlen, int, NETCONN_UDP); #if LWIP_UDPLITE if (udp_is_flag_set(sock->conn->pcb.udp, UDP_FLAGS_UDPLITE)) { /* this flag is only available for UDP, not for UDP lite */ done_socket(sock); return EAFNOSUPPORT; }if (udp_is_flag_set(sock->conn->pcb.udp, UDP_FLAGS_UDPLITE)) { ... } /* ... */#endif /* LWIP_UDPLITE */ *(int *)optval = udp_is_flag_set(sock->conn->pcb.udp, UDP_FLAGS_NOCHKSUM) ? 1 : 0; break;/* ... */ #endif /* LWIP_UDP*/case SO_NO_CHECK: default: LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, UNIMPL: optname=0x%x, ..)\n", s, optname)); err = ENOPROTOOPT; break;default }switch (optname) { ... } /* switch (optname) */ break; /* Level: IPPROTO_IP */case SOL_SOCKET: case IPPROTO_IP: switch (optname) { case IP_TTL: LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, int); *(int *)optval = sock->conn->pcb.ip->ttl; LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_TTL) = %d\n", s, *(int *)optval)); break;case IP_TTL: case IP_TOS: LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, int); *(int *)optval = sock->conn->pcb.ip->tos; LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_TOS) = %d\n", s, *(int *)optval)); break; #if LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS && LWIP_UDPcase IP_TOS: case IP_MULTICAST_TTL: LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, u8_t); if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) != NETCONN_UDP) { done_socket(sock); return ENOPROTOOPT; }if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) != NETCONN_UDP) { ... } *(u8_t *)optval = udp_get_multicast_ttl(sock->conn->pcb.udp); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_MULTICAST_TTL) = %d\n", s, *(int *)optval)); break;case IP_MULTICAST_TTL: case IP_MULTICAST_IF: LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, struct in_addr); if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) != NETCONN_UDP) { done_socket(sock); return ENOPROTOOPT; }if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) != NETCONN_UDP) { ... } inet_addr_from_ip4addr((struct in_addr *)optval, udp_get_multicast_netif_addr(sock->conn->pcb.udp)); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_MULTICAST_IF) = 0x%"X32_F"\n", s, *(u32_t *)optval)); break;case IP_MULTICAST_IF: case IP_MULTICAST_LOOP: LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, u8_t); if ((sock->conn->pcb.udp->flags & UDP_FLAGS_MULTICAST_LOOP) != 0) { *(u8_t *)optval = 1; }if ((sock->conn->pcb.udp->flags & UDP_FLAGS_MULTICAST_LOOP) != 0) { ... } else { *(u8_t *)optval = 0; }else { ... } LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_MULTICAST_LOOP) = %d\n", s, *(int *)optval)); break;/* ... */ #endif /* LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS && LWIP_UDP */ default: LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, UNIMPL: optname=0x%x, ..)\n", s, optname)); err = ENOPROTOOPT; break;default }switch (optname) { ... } /* switch (optname) */ break; #if LWIP_TCP /* Level: IPPROTO_TCP */ case IPPROTO_TCP: /* Special case: all IPPROTO_TCP option take an int */ LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, *optlen, int, NETCONN_TCP); if (sock->conn->pcb.tcp->state == LISTEN) { done_socket(sock); return EINVAL; }if (sock->conn->pcb.tcp->state == LISTEN) { ... } switch (optname) { case TCP_NODELAY: *(int *)optval = tcp_nagle_disabled(sock->conn->pcb.tcp); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, TCP_NODELAY) = %s\n", s, (*(int *)optval) ? "on" : "off") ); break;case TCP_NODELAY: case TCP_KEEPALIVE: *(int *)optval = (int)sock->conn->pcb.tcp->keep_idle; LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, TCP_KEEPALIVE) = %d\n", s, *(int *)optval)); break; #if LWIP_TCP_KEEPALIVEcase TCP_KEEPALIVE: case TCP_KEEPIDLE: *(int *)optval = (int)(sock->conn->pcb.tcp->keep_idle / 1000); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, TCP_KEEPIDLE) = %d\n", s, *(int *)optval)); break;case TCP_KEEPIDLE: case TCP_KEEPINTVL: *(int *)optval = (int)(sock->conn->pcb.tcp->keep_intvl / 1000); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, TCP_KEEPINTVL) = %d\n", s, *(int *)optval)); break;case TCP_KEEPINTVL: case TCP_KEEPCNT: *(int *)optval = (int)sock->conn->pcb.tcp->keep_cnt; LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, TCP_KEEPCNT) = %d\n", s, *(int *)optval)); break;/* ... */ #endif /* LWIP_TCP_KEEPALIVE */ default: LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, UNIMPL: optname=0x%x, ..)\n", s, optname)); err = ENOPROTOOPT; break;default }switch (optname) { ... } /* switch (optname) */ break;/* ... */ #endif /* LWIP_TCP */ #if LWIP_IPV6 /* Level: IPPROTO_IPV6 */ case IPPROTO_IPV6: switch (optname) { case IPV6_V6ONLY: LWIP_SOCKOPT_CHECK_OPTLEN_CONN(sock, *optlen, int); *(int *)optval = (netconn_get_ipv6only(sock->conn) ? 1 : 0); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IPV6, IPV6_V6ONLY) = %d\n", s, *(int *)optval)); break;case IPV6_V6ONLY: default: LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IPV6, UNIMPL: optname=0x%x, ..)\n", s, optname)); err = ENOPROTOOPT; break;default }switch (optname) { ... } /* switch (optname) */ break;/* ... */ #endif /* LWIP_IPV6 */ #if LWIP_UDP && LWIP_UDPLITE /* Level: IPPROTO_UDPLITE */ case IPPROTO_UDPLITE: /* Special case: all IPPROTO_UDPLITE option take an int */ LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, int); /* If this is no UDP lite socket, ignore any options. */ if (!NETCONNTYPE_ISUDPLITE(netconn_type(sock->conn))) { done_socket(sock); return ENOPROTOOPT; }if (!NETCONNTYPE_ISUDPLITE(netconn_type(sock->conn))) { ... } switch (optname) { case UDPLITE_SEND_CSCOV: *(int *)optval = sock->conn->pcb.udp->chksum_len_tx; LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UDPLITE_SEND_CSCOV) = %d\n", s, (*(int *)optval)) ); break;case UDPLITE_SEND_CSCOV: case UDPLITE_RECV_CSCOV: *(int *)optval = sock->conn->pcb.udp->chksum_len_rx; LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UDPLITE_RECV_CSCOV) = %d\n", s, (*(int *)optval)) ); break;case UDPLITE_RECV_CSCOV: default: LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UNIMPL: optname=0x%x, ..)\n", s, optname)); err = ENOPROTOOPT; break;default }switch (optname) { ... } /* switch (optname) */ break;/* ... */ #endif /* LWIP_UDP */ /* Level: IPPROTO_RAW */ case IPPROTO_RAW: switch (optname) { #if LWIP_IPV6 && LWIP_RAW case IPV6_CHECKSUM: LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, *optlen, int, NETCONN_RAW); if (sock->conn->pcb.raw->chksum_reqd == 0) { *(int *)optval = -1; }if (sock->conn->pcb.raw->chksum_reqd == 0) { ... } else { *(int *)optval = sock->conn->pcb.raw->chksum_offset; }else { ... } LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_RAW, IPV6_CHECKSUM) = %d\n", s, (*(int *)optval)) ); break;/* ... */ #endif /* LWIP_IPV6 && LWIP_RAW */case IPV6_CHECKSUM: default: LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_RAW, UNIMPL: optname=0x%x, ..)\n", s, optname)); err = ENOPROTOOPT; break;default }switch (optname) { ... } /* switch (optname) */ break;case IPPROTO_RAW: default: LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, level=0x%x, UNIMPL: optname=0x%x, ..)\n", s, level, optname)); err = ENOPROTOOPT; break;default }switch (level) { ... } /* switch (level) */ done_socket(sock); return err; }lwip_getsockopt_impl (int s, int level, int optname, void *optval, socklen_t *optlen) { ... } int lwip_setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen) { int err = 0; struct lwip_sock *sock = get_socket(s); #if !LWIP_TCPIP_CORE_LOCKING err_t cberr; LWIP_SETGETSOCKOPT_DATA_VAR_DECLARE(data);/* ... */ #endif /* !LWIP_TCPIP_CORE_LOCKING */ if (!sock) { return -1; }if (!sock) { ... } if (NULL == optval) { sock_set_errno(sock, EFAULT); done_socket(sock); return -1; }if (NULL == optval) { ... } #if LWIP_TCPIP_CORE_LOCKING /* core-locking can just call the -impl function */ LOCK_TCPIP_CORE(); err = lwip_setsockopt_impl(s, level, optname, optval, optlen); UNLOCK_TCPIP_CORE(); /* ... */ #else /* LWIP_TCPIP_CORE_LOCKING */ #if LWIP_MPU_COMPATIBLE /* MPU_COMPATIBLE copies the optval data, so check for max size here */ if (optlen > LWIP_SETGETSOCKOPT_MAXOPTLEN) { sock_set_errno(sock, ENOBUFS); done_socket(sock); return -1; }if (optlen > LWIP_SETGETSOCKOPT_MAXOPTLEN) { ... } /* ... */#endif /* LWIP_MPU_COMPATIBLE */ LWIP_SETGETSOCKOPT_DATA_VAR_ALLOC(data, sock); LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).s = s; LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).level = level; LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optname = optname; LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optlen = optlen; #if LWIP_MPU_COMPATIBLE MEMCPY(LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optval, optval, optlen); #else /* LWIP_MPU_COMPATIBLE */ LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optval.pc = (const void *)optval; #endif /* LWIP_MPU_COMPATIBLE */ LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).err = 0; #if LWIP_NETCONN_SEM_PER_THREAD LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).completed_sem = LWIP_NETCONN_THREAD_SEM_GET(); #else LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).completed_sem = &sock->conn->op_completed; #endif cberr = tcpip_callback(lwip_setsockopt_callback, &LWIP_SETGETSOCKOPT_DATA_VAR_REF(data)); if (cberr != ERR_OK) { LWIP_SETGETSOCKOPT_DATA_VAR_FREE(data); sock_set_errno(sock, err_to_errno(cberr)); done_socket(sock); return -1; }if (cberr != ERR_OK) { ... } sys_arch_sem_wait((sys_sem_t *)(LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).completed_sem), 0); /* maybe lwip_getsockopt_internal has changed err */ err = LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).err; LWIP_SETGETSOCKOPT_DATA_VAR_FREE(data);/* ... */ #endif /* LWIP_TCPIP_CORE_LOCKING */ sock_set_errno(sock, err); done_socket(sock); return err ? -1 : 0; }lwip_setsockopt (int s, int level, int optname, const void *optval, socklen_t optlen) { ... } #if !LWIP_TCPIP_CORE_LOCKING /** lwip_setsockopt_callback: only used without CORE_LOCKING * to get into the tcpip_thread *//* ... */ static void lwip_setsockopt_callback(void *arg) { struct lwip_setgetsockopt_data *data; LWIP_ASSERT("arg != NULL", arg != NULL); data = (struct lwip_setgetsockopt_data *)arg; data->err = lwip_setsockopt_impl(data->s, data->level, data->optname, #if LWIP_MPU_COMPATIBLE data->optval, #else /* LWIP_MPU_COMPATIBLE */ data->optval.pc, #endif /* LWIP_MPU_COMPATIBLE */ data->optlen); sys_sem_signal((sys_sem_t *)(data->completed_sem)); }lwip_setsockopt_callback (void *arg) { ... } /* ... */#endif /* LWIP_TCPIP_CORE_LOCKING */ /** lwip_setsockopt_impl: the actual implementation of setsockopt: * same argument as lwip_setsockopt, either called directly or through callback *//* ... */ static int lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_t optlen) { int err = 0; struct lwip_sock *sock = tryget_socket(s); if (!sock) { return EBADF; }if (!sock) { ... } #ifdef LWIP_HOOK_SOCKETS_SETSOCKOPT if (LWIP_HOOK_SOCKETS_SETSOCKOPT(s, sock, level, optname, optval, optlen, &err)) { return err; }if (LWIP_HOOK_SOCKETS_SETSOCKOPT(s, sock, level, optname, optval, optlen, &err)) { ... } /* ... */#endif switch (level) { /* Level: SOL_SOCKET */ case SOL_SOCKET: switch (optname) { /* SO_ACCEPTCONN is get-only */ /* The option flags */ case SO_BROADCAST: case SO_KEEPALIVE: #if SO_REUSEcase SO_KEEPALIVE: case SO_REUSEADDR: #endif /* SO_REUSE */ if ((optname == SO_BROADCAST) && (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_UDP)) { done_socket(sock); return ENOPROTOOPT; }if ((optname == SO_BROADCAST) && (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_UDP)) { ... } optname = lwip_sockopt_to_ipopt(optname); LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, int); if (*(const int *)optval) { ip_set_option(sock->conn->pcb.ip, optname); }if (*(const int *)optval) { ... } else { ip_reset_option(sock->conn->pcb.ip, optname); }else { ... } LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, SOL_SOCKET, optname=0x%x, ..) -> %s\n", s, optname, (*(const int *)optval ? "on" : "off"))); break; /* SO_TYPE is get-only */ /* SO_ERROR is get-only */ #if LWIP_SO_SNDTIMEOcase SO_REUSEADDR: case SO_SNDTIMEO: { long ms_long; LWIP_SOCKOPT_CHECK_OPTLEN_CONN(sock, optlen, LWIP_SO_SNDRCVTIMEO_OPTTYPE); ms_long = LWIP_SO_SNDRCVTIMEO_GET_MS(optval); if (ms_long < 0) { done_socket(sock); return EINVAL; }if (ms_long < 0) { ... } netconn_set_sendtimeout(sock->conn, ms_long); break; ...}/* ... */ #endif /* LWIP_SO_SNDTIMEO */ #if LWIP_SO_RCVTIMEOcase SO_SNDTIMEO: case SO_RCVTIMEO: { long ms_long; LWIP_SOCKOPT_CHECK_OPTLEN_CONN(sock, optlen, LWIP_SO_SNDRCVTIMEO_OPTTYPE); ms_long = LWIP_SO_SNDRCVTIMEO_GET_MS(optval); if (ms_long < 0) { done_socket(sock); return EINVAL; }if (ms_long < 0) { ... } netconn_set_recvtimeout(sock->conn, (u32_t)ms_long); break; ...}/* ... */ #endif /* LWIP_SO_RCVTIMEO */ #if LWIP_SO_RCVBUFcase SO_RCVTIMEO: case SO_RCVBUF: LWIP_SOCKOPT_CHECK_OPTLEN_CONN(sock, optlen, int); netconn_set_recvbufsize(sock->conn, *(const int *)optval); break;/* ... */ #endif /* LWIP_SO_RCVBUF */ #if LWIP_SO_LINGERcase SO_RCVBUF: case SO_LINGER: { const struct linger *linger = (const struct linger *)optval; LWIP_SOCKOPT_CHECK_OPTLEN_CONN(sock, optlen, struct linger); if (linger->l_onoff) { int lingersec = linger->l_linger; if (lingersec < 0) { done_socket(sock); return EINVAL; }if (lingersec < 0) { ... } if (lingersec > 0xFFFF) { lingersec = 0xFFFF; }if (lingersec > 0xFFFF) { ... } sock->conn->linger = (s16_t)lingersec; }if (linger->l_onoff) { ... } else { sock->conn->linger = -1; }else { ... } ...} break;/* ... */ #endif /* LWIP_SO_LINGER */ #if LWIP_UDPcase SO_LINGER: case SO_NO_CHECK: LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, int, NETCONN_UDP); #if LWIP_UDPLITE if (udp_is_flag_set(sock->conn->pcb.udp, UDP_FLAGS_UDPLITE)) { /* this flag is only available for UDP, not for UDP lite */ done_socket(sock); return EAFNOSUPPORT; }if (udp_is_flag_set(sock->conn->pcb.udp, UDP_FLAGS_UDPLITE)) { ... } /* ... */#endif /* LWIP_UDPLITE */ if (*(const int *)optval) { udp_set_flags(sock->conn->pcb.udp, UDP_FLAGS_NOCHKSUM); }if (*(const int *)optval) { ... } else { udp_clear_flags(sock->conn->pcb.udp, UDP_FLAGS_NOCHKSUM); }else { ... } break;/* ... */ #endif /* LWIP_UDP */case SO_NO_CHECK: case SO_BINDTODEVICE: { const struct ifreq *iface; struct netif *n = NULL; LWIP_SOCKOPT_CHECK_OPTLEN_CONN(sock, optlen, struct ifreq); iface = (const struct ifreq *)optval; if (iface->ifr_name[0] != 0) { n = netif_find(iface->ifr_name); if (n == NULL) { done_socket(sock); return ENODEV; }if (n == NULL) { ... } }if (iface->ifr_name[0] != 0) { ... } switch (NETCONNTYPE_GROUP(netconn_type(sock->conn))) { #if LWIP_TCP case NETCONN_TCP: tcp_bind_netif(sock->conn->pcb.tcp, n); break;/* ... */ #endif #if LWIP_UDPcase NETCONN_TCP: case NETCONN_UDP: udp_bind_netif(sock->conn->pcb.udp, n); break;/* ... */ #endif #if LWIP_RAWcase NETCONN_UDP: case NETCONN_RAW: raw_bind_netif(sock->conn->pcb.raw, n); break;/* ... */ #endifcase NETCONN_RAW: default: LWIP_ASSERT("Unhandled netconn type in SO_BINDTODEVICE", 0); break;default }switch (NETCONNTYPE_GROUP(netconn_type(sock->conn))) { ... } ...} break;case SO_BINDTODEVICE: default: LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, SOL_SOCKET, UNIMPL: optname=0x%x, ..)\n", s, optname)); err = ENOPROTOOPT; break;default }switch (optname) { ... } /* switch (optname) */ break; /* Level: IPPROTO_IP */case SOL_SOCKET: case IPPROTO_IP: switch (optname) { case IP_TTL: LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, int); sock->conn->pcb.ip->ttl = (u8_t)(*(const int *)optval); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, IP_TTL, ..) -> %d\n", s, sock->conn->pcb.ip->ttl)); break;case IP_TTL: case IP_TOS: LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, int); sock->conn->pcb.ip->tos = (u8_t)(*(const int *)optval); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, IP_TOS, ..)-> %d\n", s, sock->conn->pcb.ip->tos)); break; #if LWIP_NETBUF_RECVINFOcase IP_TOS: case IP_PKTINFO: LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, int, NETCONN_UDP); if (*(const int *)optval) { sock->conn->flags |= NETCONN_FLAG_PKTINFO; }if (*(const int *)optval) { ... } else { sock->conn->flags &= ~NETCONN_FLAG_PKTINFO; }else { ... } break;/* ... */ #endif /* LWIP_NETBUF_RECVINFO */ #if LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS && LWIP_UDPcase IP_PKTINFO: case IP_MULTICAST_TTL: LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, u8_t, NETCONN_UDP); udp_set_multicast_ttl(sock->conn->pcb.udp, (u8_t)(*(const u8_t *)optval)); break;case IP_MULTICAST_TTL: case IP_MULTICAST_IF: { ip4_addr_t if_addr; LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, struct in_addr, NETCONN_UDP); inet_addr_to_ip4addr(&if_addr, (const struct in_addr *)optval); udp_set_multicast_netif_addr(sock->conn->pcb.udp, &if_addr); ...} break;case IP_MULTICAST_IF: case IP_MULTICAST_LOOP: LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, u8_t, NETCONN_UDP); if (*(const u8_t *)optval) { udp_set_flags(sock->conn->pcb.udp, UDP_FLAGS_MULTICAST_LOOP); }if (*(const u8_t *)optval) { ... } else { udp_clear_flags(sock->conn->pcb.udp, UDP_FLAGS_MULTICAST_LOOP); }else { ... } break;/* ... */ #endif /* LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS && LWIP_UDP */ #if LWIP_IGMP case IP_ADD_MEMBERSHIP: case IP_DROP_MEMBERSHIP: { /* If this is a TCP or a RAW socket, ignore these options. */ err_t igmp_err; const struct ip_mreq *imr = (const struct ip_mreq *)optval; ip4_addr_t if_addr; ip4_addr_t multi_addr; LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, struct ip_mreq, NETCONN_UDP); inet_addr_to_ip4addr(&if_addr, &imr->imr_interface); inet_addr_to_ip4addr(&multi_addr, &imr->imr_multiaddr); if (optname == IP_ADD_MEMBERSHIP) { if (!lwip_socket_register_membership(s, &if_addr, &multi_addr)) { /* cannot track membership (out of memory) */ err = ENOMEM; igmp_err = ERR_OK; }if (!lwip_socket_register_membership(s, &if_addr, &multi_addr)) { ... } else { igmp_err = igmp_joingroup(&if_addr, &multi_addr); }else { ... } }if (optname == IP_ADD_MEMBERSHIP) { ... } else { igmp_err = igmp_leavegroup(&if_addr, &multi_addr); lwip_socket_unregister_membership(s, &if_addr, &multi_addr); }else { ... } if (igmp_err != ERR_OK) { err = EADDRNOTAVAIL; }if (igmp_err != ERR_OK) { ... } ...} break;/* ... */ #endif /* LWIP_IGMP */ default: LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, UNIMPL: optname=0x%x, ..)\n", s, optname)); err = ENOPROTOOPT; break;default }switch (optname) { ... } /* switch (optname) */ break; #if LWIP_TCP /* Level: IPPROTO_TCP */ case IPPROTO_TCP: /* Special case: all IPPROTO_TCP option take an int */ LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, int, NETCONN_TCP); if (sock->conn->pcb.tcp->state == LISTEN) { done_socket(sock); return EINVAL; }if (sock->conn->pcb.tcp->state == LISTEN) { ... } switch (optname) { case TCP_NODELAY: if (*(const int *)optval) { tcp_nagle_disable(sock->conn->pcb.tcp); }if (*(const int *)optval) { ... } else { tcp_nagle_enable(sock->conn->pcb.tcp); }else { ... } LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_NODELAY) -> %s\n", s, (*(const int *)optval) ? "on" : "off") ); break;case TCP_NODELAY: case TCP_KEEPALIVE: sock->conn->pcb.tcp->keep_idle = (u32_t)(*(const int *)optval); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPALIVE) -> %"U32_F"\n", s, sock->conn->pcb.tcp->keep_idle)); break; #if LWIP_TCP_KEEPALIVEcase TCP_KEEPALIVE: case TCP_KEEPIDLE: sock->conn->pcb.tcp->keep_idle = 1000 * (u32_t)(*(const int *)optval); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPIDLE) -> %"U32_F"\n", s, sock->conn->pcb.tcp->keep_idle)); break;case TCP_KEEPIDLE: case TCP_KEEPINTVL: sock->conn->pcb.tcp->keep_intvl = 1000 * (u32_t)(*(const int *)optval); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPINTVL) -> %"U32_F"\n", s, sock->conn->pcb.tcp->keep_intvl)); break;case TCP_KEEPINTVL: case TCP_KEEPCNT: sock->conn->pcb.tcp->keep_cnt = (u32_t)(*(const int *)optval); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPCNT) -> %"U32_F"\n", s, sock->conn->pcb.tcp->keep_cnt)); break;/* ... */ #endif /* LWIP_TCP_KEEPALIVE */ default: LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, UNIMPL: optname=0x%x, ..)\n", s, optname)); err = ENOPROTOOPT; break;default }switch (optname) { ... } /* switch (optname) */ break;/* ... */ #endif /* LWIP_TCP*/ #if LWIP_IPV6 /* Level: IPPROTO_IPV6 */ case IPPROTO_IPV6: switch (optname) { case IPV6_V6ONLY: LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, int); if (*(const int *)optval) { netconn_set_ipv6only(sock->conn, 1); }if (*(const int *)optval) { ... } else { netconn_set_ipv6only(sock->conn, 0); }else { ... } LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IPV6, IPV6_V6ONLY, ..) -> %d\n", s, (netconn_get_ipv6only(sock->conn) ? 1 : 0))); break; #if LWIP_IPV6_MLDcase IPV6_V6ONLY: case IPV6_JOIN_GROUP: case IPV6_LEAVE_GROUP: { /* If this is a TCP or a RAW socket, ignore these options. */ err_t mld6_err; struct netif *netif; ip6_addr_t multi_addr; const struct ipv6_mreq *imr = (const struct ipv6_mreq *)optval; LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, struct ipv6_mreq, NETCONN_UDP); inet6_addr_to_ip6addr(&multi_addr, &imr->ipv6mr_multiaddr); LWIP_ASSERT("Invalid netif index", imr->ipv6mr_interface <= 0xFFu); netif = netif_get_by_index((u8_t)imr->ipv6mr_interface); if (netif == NULL) { err = EADDRNOTAVAIL; break; }if (netif == NULL) { ... } if (optname == IPV6_JOIN_GROUP) { if (!lwip_socket_register_mld6_membership(s, imr->ipv6mr_interface, &multi_addr)) { /* cannot track membership (out of memory) */ err = ENOMEM; mld6_err = ERR_OK; }if (!lwip_socket_register_mld6_membership(s, imr->ipv6mr_interface, &multi_addr)) { ... } else { mld6_err = mld6_joingroup_netif(netif, &multi_addr); }else { ... } }if (optname == IPV6_JOIN_GROUP) { ... } else { mld6_err = mld6_leavegroup_netif(netif, &multi_addr); lwip_socket_unregister_mld6_membership(s, imr->ipv6mr_interface, &multi_addr); }else { ... } if (mld6_err != ERR_OK) { err = EADDRNOTAVAIL; }if (mld6_err != ERR_OK) { ... } ...} break;/* ... */ #endif /* LWIP_IPV6_MLD */ default: LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IPV6, UNIMPL: optname=0x%x, ..)\n", s, optname)); err = ENOPROTOOPT; break;default }switch (optname) { ... } /* switch (optname) */ break;/* ... */ #endif /* LWIP_IPV6 */ #if LWIP_UDP && LWIP_UDPLITE /* Level: IPPROTO_UDPLITE */ case IPPROTO_UDPLITE: /* Special case: all IPPROTO_UDPLITE option take an int */ LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, int); /* If this is no UDP lite socket, ignore any options. */ if (!NETCONNTYPE_ISUDPLITE(netconn_type(sock->conn))) { done_socket(sock); return ENOPROTOOPT; }if (!NETCONNTYPE_ISUDPLITE(netconn_type(sock->conn))) { ... } switch (optname) { case UDPLITE_SEND_CSCOV: if ((*(const int *)optval != 0) && ((*(const int *)optval < 8) || (*(const int *)optval > 0xffff))) { /* don't allow illegal values! */ sock->conn->pcb.udp->chksum_len_tx = 8; }if ((*(const int *)optval != 0) && ((*(const int *)optval < 8) || (*(const int *)optval > 0xffff))) { ... } else { sock->conn->pcb.udp->chksum_len_tx = (u16_t) * (const int *)optval; }else { ... } LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_UDPLITE, UDPLITE_SEND_CSCOV) -> %d\n", s, (*(const int *)optval)) ); break;case UDPLITE_SEND_CSCOV: case UDPLITE_RECV_CSCOV: if ((*(const int *)optval != 0) && ((*(const int *)optval < 8) || (*(const int *)optval > 0xffff))) { /* don't allow illegal values! */ sock->conn->pcb.udp->chksum_len_rx = 8; }if ((*(const int *)optval != 0) && ((*(const int *)optval < 8) || (*(const int *)optval > 0xffff))) { ... } else { sock->conn->pcb.udp->chksum_len_rx = (u16_t) * (const int *)optval; }else { ... } LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_UDPLITE, UDPLITE_RECV_CSCOV) -> %d\n", s, (*(const int *)optval)) ); break;case UDPLITE_RECV_CSCOV: default: LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_UDPLITE, UNIMPL: optname=0x%x, ..)\n", s, optname)); err = ENOPROTOOPT; break;default }switch (optname) { ... } /* switch (optname) */ break;/* ... */ #endif /* LWIP_UDP */ /* Level: IPPROTO_RAW */ case IPPROTO_RAW: switch (optname) { #if LWIP_IPV6 && LWIP_RAW case IPV6_CHECKSUM: /* It should not be possible to disable the checksum generation with ICMPv6 * as per RFC 3542 chapter 3.1 *//* ... */ if (sock->conn->pcb.raw->protocol == IPPROTO_ICMPV6) { done_socket(sock); return EINVAL; }if (sock->conn->pcb.raw->protocol == IPPROTO_ICMPV6) { ... } LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, int, NETCONN_RAW); if (*(const int *)optval < 0) { sock->conn->pcb.raw->chksum_reqd = 0; }if (*(const int *)optval < 0) { ... } else if (*(const int *)optval & 1) { /* Per RFC3542, odd offsets are not allowed */ done_socket(sock); return EINVAL; }else if (*(const int *)optval & 1) { ... } else { sock->conn->pcb.raw->chksum_reqd = 1; sock->conn->pcb.raw->chksum_offset = (u16_t) * (const int *)optval; }else { ... } LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_RAW, IPV6_CHECKSUM, ..) -> %d\n", s, sock->conn->pcb.raw->chksum_reqd)); break;/* ... */ #endif /* LWIP_IPV6 && LWIP_RAW */case IPV6_CHECKSUM: default: LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_RAW, UNIMPL: optname=0x%x, ..)\n", s, optname)); err = ENOPROTOOPT; break;default }switch (optname) { ... } /* switch (optname) */ break;case IPPROTO_RAW: default: LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, level=0x%x, UNIMPL: optname=0x%x, ..)\n", s, level, optname)); err = ENOPROTOOPT; break;default }switch (level) { ... } /* switch (level) */ done_socket(sock); return err; }lwip_setsockopt_impl (int s, int level, int optname, const void *optval, socklen_t optlen) { ... } int lwip_ioctl(int s, long cmd, void *argp) { struct lwip_sock *sock = get_socket(s); u8_t val; #if LWIP_SO_RCVBUF int recv_avail; #endif /* LWIP_SO_RCVBUF */ if (!sock) { return -1; }if (!sock) { ... } switch (cmd) { #if LWIP_SO_RCVBUF || LWIP_FIONREAD_LINUXMODE case FIONREAD: if (!argp) { sock_set_errno(sock, EINVAL); done_socket(sock); return -1; }if (!argp) { ... } #if LWIP_FIONREAD_LINUXMODE if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) != NETCONN_TCP) { struct netbuf *nb; if (sock->lastdata.netbuf) { nb = sock->lastdata.netbuf; *((int *)argp) = nb->p->tot_len; }if (sock->lastdata.netbuf) { ... } else { struct netbuf *rxbuf; err_t err = netconn_recv_udp_raw_netbuf_flags(sock->conn, &rxbuf, NETCONN_DONTBLOCK); if (err != ERR_OK) { *((int *)argp) = 0; }if (err != ERR_OK) { ... } else { sock->lastdata.netbuf = rxbuf; *((int *)argp) = rxbuf->p->tot_len; }else { ... } }else { ... } done_socket(sock); return 0; }if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) != NETCONN_TCP) { ... } /* ... */#endif /* LWIP_FIONREAD_LINUXMODE */ #if LWIP_SO_RCVBUF /* we come here if either LWIP_FIONREAD_LINUXMODE==0 or this is a TCP socket */ SYS_ARCH_GET(sock->conn->recv_avail, recv_avail); if (recv_avail < 0) { recv_avail = 0; }if (recv_avail < 0) { ... } /* Check if there is data left from the last recv operation. /maq 041215 */ if (sock->lastdata.netbuf) { if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_TCP) { recv_avail += sock->lastdata.pbuf->tot_len; }if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_TCP) { ... } else { recv_avail += sock->lastdata.netbuf->p->tot_len; }else { ... } }if (sock->lastdata.netbuf) { ... } *((int *)argp) = recv_avail; LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, FIONREAD, %p) = %"U16_F"\n", s, argp, *((u16_t *)argp))); sock_set_errno(sock, 0); done_socket(sock); return 0;/* ... */ #else /* LWIP_SO_RCVBUF */ break; #endif /* LWIP_SO_RCVBUF *//* ... */ #endif /* LWIP_SO_RCVBUF || LWIP_FIONREAD_LINUXMODE */ case FIONREAD: case (long)FIONBIO: val = 0; if (argp && *(int *)argp) { val = 1; }if (argp && *(int *)argp) { ... } netconn_set_nonblocking(sock->conn, val); LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, FIONBIO, %d)\n", s, val)); sock_set_errno(sock, 0); done_socket(sock); return 0; case (long)FIONBIO: default: break;default }switch (cmd) { ... } /* switch (cmd) */ LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, UNIMPL: 0x%lx, %p)\n", s, cmd, argp)); sock_set_errno(sock, ENOSYS); /* not yet implemented */ done_socket(sock); return -1; }lwip_ioctl (int s, long cmd, void *argp) { ... } /** A minimal implementation of fcntl. * Currently only the commands F_GETFL and F_SETFL are implemented. * The flag O_NONBLOCK and access modes are supported for F_GETFL, only * the flag O_NONBLOCK is implemented for F_SETFL. *//* ... */ int lwip_fcntl(int s, int cmd, int val) { struct lwip_sock *sock = get_socket(s); int ret = -1; int op_mode = 0; if (!sock) { return -1; }if (!sock) { ... } switch (cmd) { case F_GETFL: ret = netconn_is_nonblocking(sock->conn) ? O_NONBLOCK : 0; sock_set_errno(sock, 0); if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_TCP) { #if LWIP_TCPIP_CORE_LOCKING LOCK_TCPIP_CORE(); #else SYS_ARCH_DECL_PROTECT(lev); /* the proper thing to do here would be to get into the tcpip_thread, but locking should be OK as well since we only *read* some flags *//* ... */ SYS_ARCH_PROTECT(lev);/* ... */ #endif #if LWIP_TCP if (sock->conn->pcb.tcp) { if (!(sock->conn->pcb.tcp->flags & TF_RXCLOSED)) { op_mode |= O_RDONLY; }if (!(sock->conn->pcb.tcp->flags & TF_RXCLOSED)) { ... } if (!(sock->conn->pcb.tcp->flags & TF_FIN)) { op_mode |= O_WRONLY; }if (!(sock->conn->pcb.tcp->flags & TF_FIN)) { ... } }if (sock->conn->pcb.tcp) { ... } /* ... */#endif #if LWIP_TCPIP_CORE_LOCKING UNLOCK_TCPIP_CORE(); #else SYS_ARCH_UNPROTECT(lev); #endif }if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_TCP) { ... } else { op_mode |= O_RDWR; }else { ... } /* ensure O_RDWR for (O_RDONLY|O_WRONLY) != O_RDWR cases */ ret |= (op_mode == (O_RDONLY | O_WRONLY)) ? O_RDWR : op_mode; break;case F_GETFL: case F_SETFL: /* Bits corresponding to the file access mode and the file creation flags [..] that are set in arg shall be ignored */ val &= ~(O_RDONLY | O_WRONLY | O_RDWR); if ((val & ~O_NONBLOCK) == 0) { /* only O_NONBLOCK, all other bits are zero */ netconn_set_nonblocking(sock->conn, val & O_NONBLOCK); ret = 0; sock_set_errno(sock, 0); }if ((val & ~O_NONBLOCK) == 0) { ... } else { sock_set_errno(sock, ENOSYS); /* not yet implemented */ }else { ... } break;case F_SETFL: default: LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_fcntl(%d, UNIMPL: %d, %d)\n", s, cmd, val)); sock_set_errno(sock, ENOSYS); /* not yet implemented */ break;default }switch (cmd) { ... } done_socket(sock); return ret; }lwip_fcntl (int s, int cmd, int val) { ... } #if LWIP_COMPAT_SOCKETS == 2 && LWIP_POSIX_SOCKETS_IO_NAMES int fcntl(int s, int cmd, ...) { va_list ap; int val; va_start(ap, cmd); val = va_arg(ap, int); va_end(ap); return lwip_fcntl(s, cmd, val); }fcntl (int s, int cmd, ...) { ... } /* ... */#endif const char * lwip_inet_ntop(int af, const void *src, char *dst, socklen_t size) { const char *ret = NULL; int size_int = (int)size; if (size_int < 0) { set_errno(ENOSPC); return NULL; }if (size_int < 0) { ... } switch (af) { #if LWIP_IPV4 case AF_INET: ret = ip4addr_ntoa_r((const ip4_addr_t *)src, dst, size_int); if (ret == NULL) { set_errno(ENOSPC); }if (ret == NULL) { ... } break;/* ... */ #endif #if LWIP_IPV6case AF_INET: case AF_INET6: ret = ip6addr_ntoa_r((const ip6_addr_t *)src, dst, size_int); if (ret == NULL) { set_errno(ENOSPC); }if (ret == NULL) { ... } break;/* ... */ #endifcase AF_INET6: default: set_errno(EAFNOSUPPORT); break;default }switch (af) { ... } return ret; }lwip_inet_ntop (int af, const void *src, char *dst, socklen_t size) { ... } int lwip_inet_pton(int af, const char *src, void *dst) { int err; switch (af) { #if LWIP_IPV4 case AF_INET: err = ip4addr_aton(src, (ip4_addr_t *)dst); break;/* ... */ #endif #if LWIP_IPV6case AF_INET: case AF_INET6: { /* convert into temporary variable since ip6_addr_t might be larger than in6_addr when scopes are enabled *//* ... */ ip6_addr_t addr; err = ip6addr_aton(src, &addr); if (err) { memcpy(dst, &addr.addr, sizeof(addr.addr)); }if (err) { ... } break; ...}/* ... */ #endifcase AF_INET6: default: err = -1; set_errno(EAFNOSUPPORT); break;default }switch (af) { ... } return err; }lwip_inet_pton (int af, const char *src, void *dst) { ... } #if LWIP_IGMP /** Register a new IGMP membership. On socket close, the membership is dropped automatically. * * ATTENTION: this function is called from tcpip_thread (or under CORE_LOCK). * * @return 1 on success, 0 on failure *//* ... */ static int lwip_socket_register_membership(int s, const ip4_addr_t *if_addr, const ip4_addr_t *multi_addr) { struct lwip_sock *sock = get_socket(s); int i; if (!sock) { return 0; }if (!sock) { ... } for (i = 0; i < LWIP_SOCKET_MAX_MEMBERSHIPS; i++) { if (socket_ipv4_multicast_memberships[i].sock == NULL) { socket_ipv4_multicast_memberships[i].sock = sock; ip4_addr_copy(socket_ipv4_multicast_memberships[i].if_addr, *if_addr); ip4_addr_copy(socket_ipv4_multicast_memberships[i].multi_addr, *multi_addr); done_socket(sock); return 1; }if (socket_ipv4_multicast_memberships[i].sock == NULL) { ... } }for (i = 0; i < LWIP_SOCKET_MAX_MEMBERSHIPS; i++) { ... } done_socket(sock); return 0; }lwip_socket_register_membership (int s, const ip4_addr_t *if_addr, const ip4_addr_t *multi_addr) { ... } /** Unregister a previously registered membership. This prevents dropping the membership * on socket close. * * ATTENTION: this function is called from tcpip_thread (or under CORE_LOCK). *//* ... */ static void lwip_socket_unregister_membership(int s, const ip4_addr_t *if_addr, const ip4_addr_t *multi_addr) { struct lwip_sock *sock = get_socket(s); int i; if (!sock) { return; }if (!sock) { ... } for (i = 0; i < LWIP_SOCKET_MAX_MEMBERSHIPS; i++) { if ((socket_ipv4_multicast_memberships[i].sock == sock) && ip4_addr_cmp(&socket_ipv4_multicast_memberships[i].if_addr, if_addr) && ip4_addr_cmp(&socket_ipv4_multicast_memberships[i].multi_addr, multi_addr)) { socket_ipv4_multicast_memberships[i].sock = NULL; ip4_addr_set_zero(&socket_ipv4_multicast_memberships[i].if_addr); ip4_addr_set_zero(&socket_ipv4_multicast_memberships[i].multi_addr); break; }if ((socket_ipv4_multicast_memberships[i].sock == sock) && ip4_addr_cmp(&socket_ipv4_multicast_memberships[i].if_addr, if_addr) && ip4_addr_cmp(&socket_ipv4_multicast_memberships[i].multi_addr, multi_addr)) { ... } }for (i = 0; i < LWIP_SOCKET_MAX_MEMBERSHIPS; i++) { ... } done_socket(sock); }lwip_socket_unregister_membership (int s, const ip4_addr_t *if_addr, const ip4_addr_t *multi_addr) { ... } /** Drop all memberships of a socket that were not dropped explicitly via setsockopt. * * ATTENTION: this function is NOT called from tcpip_thread (or under CORE_LOCK). *//* ... */ static void lwip_socket_drop_registered_memberships(int s) { struct lwip_sock *sock = get_socket(s); int i; if (!sock) { return; }if (!sock) { ... } for (i = 0; i < LWIP_SOCKET_MAX_MEMBERSHIPS; i++) { if (socket_ipv4_multicast_memberships[i].sock == sock) { ip_addr_t multi_addr, if_addr; ip_addr_copy_from_ip4(multi_addr, socket_ipv4_multicast_memberships[i].multi_addr); ip_addr_copy_from_ip4(if_addr, socket_ipv4_multicast_memberships[i].if_addr); socket_ipv4_multicast_memberships[i].sock = NULL; ip4_addr_set_zero(&socket_ipv4_multicast_memberships[i].if_addr); ip4_addr_set_zero(&socket_ipv4_multicast_memberships[i].multi_addr); netconn_join_leave_group(sock->conn, &multi_addr, &if_addr, NETCONN_LEAVE); }if (socket_ipv4_multicast_memberships[i].sock == sock) { ... } }for (i = 0; i < LWIP_SOCKET_MAX_MEMBERSHIPS; i++) { ... } done_socket(sock); }lwip_socket_drop_registered_memberships (int s) { ... } /* ... */#endif /* LWIP_IGMP */ #if LWIP_IPV6_MLD /** Register a new MLD6 membership. On socket close, the membership is dropped automatically. * * ATTENTION: this function is called from tcpip_thread (or under CORE_LOCK). * * @return 1 on success, 0 on failure *//* ... */ static int lwip_socket_register_mld6_membership(int s, unsigned int if_idx, const ip6_addr_t *multi_addr) { struct lwip_sock *sock = get_socket(s); int i; if (!sock) { return 0; }if (!sock) { ... } for (i = 0; i < LWIP_SOCKET_MAX_MEMBERSHIPS; i++) { if (socket_ipv6_multicast_memberships[i].sock == NULL) { socket_ipv6_multicast_memberships[i].sock = sock; socket_ipv6_multicast_memberships[i].if_idx = (u8_t)if_idx; ip6_addr_copy(socket_ipv6_multicast_memberships[i].multi_addr, *multi_addr); done_socket(sock); return 1; }if (socket_ipv6_multicast_memberships[i].sock == NULL) { ... } }for (i = 0; i < LWIP_SOCKET_MAX_MEMBERSHIPS; i++) { ... } done_socket(sock); return 0; }lwip_socket_register_mld6_membership (int s, unsigned int if_idx, const ip6_addr_t *multi_addr) { ... } /** Unregister a previously registered MLD6 membership. This prevents dropping the membership * on socket close. * * ATTENTION: this function is called from tcpip_thread (or under CORE_LOCK). *//* ... */ static void lwip_socket_unregister_mld6_membership(int s, unsigned int if_idx, const ip6_addr_t *multi_addr) { struct lwip_sock *sock = get_socket(s); int i; if (!sock) { return; }if (!sock) { ... } for (i = 0; i < LWIP_SOCKET_MAX_MEMBERSHIPS; i++) { if ((socket_ipv6_multicast_memberships[i].sock == sock) && (socket_ipv6_multicast_memberships[i].if_idx == if_idx) && ip6_addr_cmp(&socket_ipv6_multicast_memberships[i].multi_addr, multi_addr)) { socket_ipv6_multicast_memberships[i].sock = NULL; socket_ipv6_multicast_memberships[i].if_idx = NETIF_NO_INDEX; ip6_addr_set_zero(&socket_ipv6_multicast_memberships[i].multi_addr); break; }if ((socket_ipv6_multicast_memberships[i].sock == sock) && (socket_ipv6_multicast_memberships[i].if_idx == if_idx) && ip6_addr_cmp(&socket_ipv6_multicast_memberships[i].multi_addr, multi_addr)) { ... } }for (i = 0; i < LWIP_SOCKET_MAX_MEMBERSHIPS; i++) { ... } done_socket(sock); }lwip_socket_unregister_mld6_membership (int s, unsigned int if_idx, const ip6_addr_t *multi_addr) { ... } /** Drop all MLD6 memberships of a socket that were not dropped explicitly via setsockopt. * * ATTENTION: this function is NOT called from tcpip_thread (or under CORE_LOCK). *//* ... */ static void lwip_socket_drop_registered_mld6_memberships(int s) { struct lwip_sock *sock = get_socket(s); int i; if (!sock) { return; }if (!sock) { ... } for (i = 0; i < LWIP_SOCKET_MAX_MEMBERSHIPS; i++) { if (socket_ipv6_multicast_memberships[i].sock == sock) { ip_addr_t multi_addr; u8_t if_idx; ip_addr_copy_from_ip6(multi_addr, socket_ipv6_multicast_memberships[i].multi_addr); if_idx = socket_ipv6_multicast_memberships[i].if_idx; socket_ipv6_multicast_memberships[i].sock = NULL; socket_ipv6_multicast_memberships[i].if_idx = NETIF_NO_INDEX; ip6_addr_set_zero(&socket_ipv6_multicast_memberships[i].multi_addr); netconn_join_leave_group_netif(sock->conn, &multi_addr, if_idx, NETCONN_LEAVE); }if (socket_ipv6_multicast_memberships[i].sock == sock) { ... } }for (i = 0; i < LWIP_SOCKET_MAX_MEMBERSHIPS; i++) { ... } done_socket(sock); }lwip_socket_drop_registered_mld6_memberships (int s) { ... } /* ... */#endif /* LWIP_IPV6_MLD */ /* ... */ #endif /* LWIP_SOCKET */
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.