1
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
73
74
82
83
84
85
86
87
88
89
99
100
101
102
103
104
105
106
107
108
114
115
116
117
118
119
120
121
123
126
132
133
135
136
145
154
155
156
157
159
160
161
162
163
166
169
172
173
174
175
176
177
178
186
187
188
189
190
194
201
202
203
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
279
280
281
285
286
287
288
291
292
293
294
295
297
298
299
300
301
302
303
304
305
317
318
319
320
324
325
326
327
331
332
333
334
335
336
337
338
339
340
341
342
343
351
352
353
354
355
356
357
358
359
360
361
365
366
367
368
369
370
371
376
377
378
379
380
381
382
383
384
385
386
387
388
396
397
398
401
402
403
404
408
409
410
411
420
421
422
426
427
428
429
439
440
441
442
452
453
459
460
471
472
478
479
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
519
520
521
523
524
525
526
532
533
534
535
536
537
538
539
540
547
548
549
550
551
552
559
560
561
562
563
564
565
566
567
568
569
571
572
586
587
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
611
612
613
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
636
637
638
639
651
652
653
654
660
661
662
663
668
669
670
671
672
673
681
682
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
702
703
704
705
706
707
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
729
730
736
737
738
739
740
741
742
743
744
745
746
747
748
749
755
756
757
758
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
784
785
790
791
792
794
795
796
798
799
800
801
806
807
808
809
810
811
812
813
814
815
816
817
818
819
822
823
829
830
831
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
855
856
857
858
859
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
892
893
894
895
896
897
898
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
932
933
934
935
936
937
938
939
940
943
945
946
947
948
949
950
955
956
957
958
959
964
965
966
967
968
969
970
971
972
977
981
982
984
985
986
987
988
989
990
991
992
993
1007
1008
1009
1010
1011
1012
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1042
1043
1044
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1079
1080
1081
1082
1083
1084
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1104
1105
1106
1107
1108
1109
1111
1112
1113
1114
1115
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1133
1134
1136
1137
1138
1139
1140
1141
1142
1143
1144
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1177
1178
1179
1180
1181
1182
1185
1186
1187
1188
1192
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1210
1211
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
1244
1245
1246
1247
1248
1249
1250
1251
1252
1256
1257
1258
1259
1260
1261
1262
1263
1264
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1296
1297
1298
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1328
1337
1338
1339
1340
1344
1345
1346
1347
1348
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1369
1370
1371
1372
1373
1374
1375
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1396
1397
1398
1399
1401
1402
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1428
1429
1430
1431
1432
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
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1486
1487
1488
1489
1493
1494
1498
1499
1502
1503
1504
1508
1509
1514
1515
1516
1517
1518
1519
1520
1521
1522
1526
1527
1531
1532
1533
1534
1545
1546
1547
1550
1551
1552
1553
1554
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1598
1599
1600
1601
1603
1604
1608
1609
1610
1611
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1661
1662
1663
1664
1665
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1700
1701
1702
1703
1704
1705
1706
1707
1712
1713
1718
1723
1724
1725
1730
1731
1732
1733
1738
1739
1740
1741
1742
1743
1744
1745
1746
1750
1751
1752
1753
1754
1755
1756
1757
1758
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1783
1784
1785
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1804
1811
1812
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
1849
1850
1851
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1873
1874
1879
1880
1885
1886
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
1922
1923
1924
1925
1926
1927
1928
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1958
1959
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
1990
1991
1992
1993
1995
1996
1997
2006
2007
2012
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
2075
2076
2077
2078
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2112
2113
2114
2120
2121
2122
2123
2124
2125
2126
2131
2132
2133
2134
2135
2140
2141
2151
2152
2153
2154
2155
2156
2159
2162
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
2205
2206
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2237
2238
2239
2240
2241
2242
2246
2247
2251
2252
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2268
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2296
2297
2298
2299
2313
2314
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2337
2338
2339
2343
2344
2345
2346
2347
2348
2352
2353
2354
2355
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2379
2380
2381
2382
2383
2391
2392
2393
2394
2395
2396
2397
2399
2400
2401
2402
2403
2404
2409
2410
2411
2412
2413
2418
2419
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2437
2438
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
2494
2495
2504
2505
2506
2507
2508
2509
2512
2515
2516
2517
2518
2519
2520
2526
2530
2536
2540
2543
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2582
2583
2584
2585
2586
2587
2591
2592
2593
2594
2595
2596
2597
2601
2602
2603
2604
2605
2623
2624
2631
2632
2633
2634
2635
2636
2637
2638
2639
2643
2644
2645
2646
2647
2648
2649
2650
2651
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2667
2668
2679
2680
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2712
2713
2714
2715
2720
2721
2722
2729
2730
2731
2732
2733
2734
2735
2736
2739
2740
2741
2742
2743
2744
2745
2746
2747
2751
2752
2753
2757
2758
2759
2760
2761
2762
2763
2764
2766
2767
2768
2771
2772
2777
2778
2779
2784
2785
2786
2787
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2816
2817
2818
2819
2820
2821
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
2877
2878
2881
2882
2883
2884
2885
2886
2889
2890
2891
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2909
2914
2915
2916
2917
2918
2919
2921
2922
2923
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2973
2979
2985
2986
2987
2988
2989
2990
2997
2998
2999
3000
3001
3002
3003
3004
3010
3011
3012
3013
3018
3019
3020
3021
3022
3023
3024
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3043
3044
3045
3046
3047
3048
3049
3053
3054
3055
3056
3057
3058
3059
3064
3065
3066
3067
3068
3073
3074
3075
3076
3077
3078
3079
3080
3081
3085
3086
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3108
3109
3110
3111
3112
3113
3114
3119
3120
3121
3122
3123
3124
3140
3141
3142
3143
3144
3145
3146
3147
3148
3152
3169
3170
3171
3172
3173
3174
3175
3176
3177
3182
3183
3184
3185
3186
3191
3192
3193
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3212
3213
3214
3217
3218
3223
3224
3225
3230
3231
3232
3233
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
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
3302
3303
3304
3305
3306
3307
3310
3311
3312
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3329
3330
3331
3336
3337
3338
3339
3340
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3374
3375
3376
3377
3378
3379
3385
3386
3387
3388
3401
3402
3403
3404
3405
3406
3407
3408
3414
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3435
3436
3437
3438
3443
3448
3452
3455
3456
3457
3458
3463
3464
3465
3466
3467
3468
3469
3475
3476
3477
3478
3479
3480
3481
3482
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3535
3538
3539
3540
3541
3546
3547
3548
3549
3550
3551
3552
3553
3554
3558
3559
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3585
3586
3587
3588
3589
3590
3591
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3630
3631
3643
3646
3647
3648
3649
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3668
3695
3696
3697
3698
3699
3700
3701
3702
3704
3708
3709
3710
3720
3721
3722
3723
3724
3729
3730
3731
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3754
3755
3756
3757
3758
3763
3764
3765
3766
3779
3780
3781
3782
3783
3784
3785
3786
3787
3790
3791
3792
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3815
3816
3817
3818
3819
3820
3821
3823
3824
3825
3826
3827
3828
3829
3830
3835
3836
3837
3838
3839
3840
3841
3842
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3859
3860
3861
3870
3871
3872
3873
3874
3875
3878
3879
3880
3881
3882
3883
3895
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
3927
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3962
3963
3965
3966
3967
3970
3971
3972
3973
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4016
4017
4018
4019
4020
4021
4022
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4043
4044
4045
4046
4047
4048
4049
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
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4104
4105
4106
4107
4108
4109
4110
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4131
4132
4133
4134
4135
4136
4137
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
/* ... */
/* ... */
#include "lwip/opt.h"
#if LWIP_SOCKET
#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 !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
#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
#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
#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
#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
#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
#define LWIP_SETGETSOCKOPT_DATA_VAR_ALLOC(name, sock)
#endif
#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
/* ... */
union sockaddr_aligned {
struct sockaddr sa;
#if LWIP_IPV6
struct sockaddr_in6 sin6;
#endif
#if LWIP_IPV4
struct sockaddr_in sin;
#endif
...};
#ifndef LWIP_SOCKET_MAX_MEMBERSHIPS
#define LWIP_SOCKET_MAX_MEMBERSHIPS NUM_SOCKETS
#endif
#if LWIP_IGMP
/* ... */
struct lwip_socket_multicast_pair {
struct lwip_sock *sock;
ip4_addr_t if_addr;
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
#if LWIP_IPV6_MLD
/* ... */
struct lwip_socket_multicast_mld6_pair {
struct lwip_sock *sock;
u8_t if_idx;
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
static struct lwip_sock sockets[NUM_SOCKETS];
#if LWIP_SOCKET_SELECT || LWIP_SOCKET_POLL
#if LWIP_TCPIP_CORE_LOCKING
#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
#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)
/* ... */
static volatile int select_cb_ctr;/* ... */
#endif
static struct lwip_select_cb *select_cb_list;/* ... */
#endif
#define sock_set_errno(sk, e) do { \
const int sockerr = (e); \
set_errno(sockerr); \
...} while (0)...
#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
void
lwip_socket_thread_init(void)
{
netconn_thread_init();
}lwip_socket_thread_init (void) { ... }
void
lwip_socket_thread_cleanup(void)
{
netconn_thread_cleanup();
}lwip_socket_thread_cleanup (void) { ... }
#if LWIP_NETCONN_FULLDUPLEX
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) {
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) { ... }
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) { ... }
/* ... */
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) {
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
#define sock_inc_used(sock) 1
#define sock_inc_used_locked(sock) 1
#define done_socket(sock)
/* ... */#endif
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) { ... }
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) { ... }
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) { ... }
/* ... */
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) { ... }
/* ... */
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) { ... }
/* ... */
static int
alloc_socket(struct netconn *newconn, int accepted)
{
int i;
SYS_ARCH_DECL_PROTECT(lev);
LWIP_UNUSED_ARG(accepted);
for (i = 0; i < NUM_SOCKETS; ++i) {
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;
/* ... */
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;
/* ... */
sockets[i].sendevent = (NETCONNTYPE_GROUP(newconn->type) == NETCONN_TCP ? (accepted != 0) : 1);
sockets[i].errevent = 0;/* ... */
#endif
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) { ... }
/* ... */
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_UNUSED_ARG(is_tcp);
#endif
*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) { ... }
/* ... */
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_delete(conn);
}if (conn != NULL) { ... }
}free_socket_free_elements (int is_tcp, struct netconn *conn, union lwip_sock_lastdata *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);
SYS_ARCH_PROTECT(lev);
freed = free_socket_locked(sock, is_tcp, &conn, &lastdata);
SYS_ARCH_UNPROTECT(lev);
if (freed) {
free_socket_free_elements(is_tcp, conn, &lastdata);
}if (freed) { ... }
}free_socket (struct lwip_sock *sock, int is_tcp) { ... }
/* ... */
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) { ... }
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];
/* ... */
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) { ... }
/* ... */
if ((addr != NULL) && (addrlen != NULL)) {
union sockaddr_aligned tempaddr;
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)) {
sock_set_errno(sock, err_to_errno(ERR_VAL));
done_socket(sock);
return -1;
}if (!SOCK_ADDR_TYPE_MATCH(name, sock)) { ... }
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
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
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
lwip_socket_drop_registered_memberships(s);/* ... */
#endif
#if LWIP_IPV6_MLD
lwip_socket_drop_registered_mld6_memberships(s);/* ... */
#endif
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)) {
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;
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
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
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) { ... }
/* ... */
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) { ... }
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
/* ... */
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));
if (sock->lastdata.pbuf) {
p = sock->lastdata.pbuf;
}if (sock->lastdata.pbuf) { ... } else {
/* ... */
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) {
/* ... */
goto lwip_recv_tcp_done;
}if (recvd > 0) { ... }
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) {
copylen = (u16_t)(SSIZE_MAX - recvd);
}if (recvd + copylen < recvd) { ... }
/* ... */
pbuf_copy_partial(p, (u8_t *)mem + recvd, copylen, 0);
recvd += copylen;
LWIP_ASSERT("invalid copylen, len would underflow", recv_left >= copylen);
recv_left -= copylen;
if ((flags & MSG_PEEK) == 0) {
LWIP_ASSERT("invalid copylen", p->tot_len >= copylen);
if (p->tot_len - copylen > 0) {
/* ... */
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) { ... }
apiflags |= NETCONN_DONTBLOCK | NETCONN_NOFIN;
...} while ((recv_left > 0) && !(flags & MSG_PEEK));
lwip_recv_tcp_done:
if ((recvd > 0) && !(flags & MSG_PEEK)) {
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
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
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
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
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
{
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
/* ... */
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));
buf = sock->lastdata.netbuf;
if (buf == NULL) {
/* ... */
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;
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 { ... }
/* ... */
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++) { ... }
#if !SOCKETS_DEBUG
if (msg->msg_name && msg->msg_namelen)
#endif
{
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) { ... }
...}
msg->msg_flags = 0;
if (msg->msg_control) {
u8_t wrote_msg = 0;
#if LWIP_NETBUF_RECVINFO
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);
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
}if (IP_IS_V4(&buf->toaddr)) { ... }
}if (buf->flags & NETBUF_FLAG_DESTADDR) { ... }
/* ... */#endif
if (!wrote_msg) {
msg->msg_controllen = 0;
}if (!wrote_msg) { ... }
}if (msg->msg_control) { ... }
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;
/* ... */
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) { ... }
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;
buflen = 0;
for (i = 0; i < message->msg_iovlen; i++) {
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) {
buflen += recvd_local;
}if (recvd_local > 0) { ... }
if ((recvd_local < 0) || (recvd_local < (int)message->msg_iov[i].iov_len) ||
(flags & MSG_PEEK)) {
if (buflen <= 0) {
buflen = recvd_local;
}if (buflen <= 0) { ... }
break;
}if ((recvd_local < 0) || (recvd_local < (int)message->msg_iov[i].iov_len) || (flags & MSG_PEEK)) { ... }
recv_flags |= MSG_DONTWAIT;
}for (i = 0; i < message->msg_iovlen; i++) { ... }
if (buflen > 0) {
sock_set_errno(sock, 0);
}if (buflen > 0) { ... }
done_socket(sock);
return buflen;/* ... */
#else
sock_set_errno(sock, err_to_errno(ERR_ARG));
done_socket(sock);
return -1;/* ... */
#endif
}if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_TCP) { ... }
#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
sock_set_errno(sock, err_to_errno(ERR_ARG));
done_socket(sock);
return -1;/* ... */
#endif
}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
sock_set_errno(sock, err_to_errno(ERR_ARG));
done_socket(sock);
return -1;/* ... */
#endif
}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);
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);
return (err == ERR_OK ? (ssize_t)written : -1);/* ... */
#else
sock_set_errno(sock, err_to_errno(ERR_ARG));
done_socket(sock);
return -1;/* ... */
#endif
}if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_TCP) { ... }
#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;);
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)) {
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) {
goto sendmsg_emsgsize;
}if (size > 0xFFFF) { ... }
if (netbuf_alloc(&chain_buf, (u16_t)size) == NULL) {
err = ERR_MEM;
}if (netbuf_alloc(&chain_buf, (u16_t)size) == NULL) { ... } else {
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
{
u16_t chksum = ~inet_chksum_pbuf(chain_buf.p);
netbuf_set_chksum(&chain_buf, chksum);
...}/* ... */
#endif
err = ERR_OK;
}else { ... }
/* ... */#else
/* ... */
for (i = 0; i < msg->msg_iovlen; i++) {
struct pbuf *p;
if (msg->msg_iov[i].iov_len > 0xFFFF) {
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;
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;
if (chain_buf.p == NULL) {
chain_buf.p = chain_buf.ptr = p;
}if (chain_buf.p == NULL) { ... } else {
if (chain_buf.p->tot_len + p->len > 0xffff) {
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++) { ... }
if (err == ERR_OK) {
size = netbuf_len(&chain_buf);
}if (err == ERR_OK) { ... }
/* ... */#endif
if (err == ERR_OK) {
#if LWIP_IPV4 && LWIP_IPV6
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
err = netconn_send(sock->conn, &chain_buf);
}if (err == ERR_OK) { ... }
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
sock_set_errno(sock, err_to_errno(ERR_ARG));
done_socket(sock);
return -1;/* ... */
#endif
}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_UNUSED_ARG(flags);
sock_set_errno(sock, err_to_errno(ERR_ARG));
done_socket(sock);
return -1;/* ... */
#endif
}if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_TCP) { ... }
if (size > LWIP_MIN(0xFFFF, SSIZE_MAX)) {
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);
buf.p = buf.ptr = NULL;
#if LWIP_CHECKSUM_ON_COPY
buf.flags = 0;
#endif
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));
#if LWIP_NETIF_TX_SINGLE_PBUF
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
{
MEMCPY(buf.p->payload, data, short_size);
}else { ... }
err = ERR_OK;
}else { ... }
/* ... */#else
err = netbuf_ref(&buf, data, short_size);
#endif
if (err == ERR_OK) {
#if LWIP_IPV4 && LWIP_IPV6
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
err = netconn_send(sock->conn, &buf);
}if (err == ERR_OK) { ... }
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);
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) {
conn->flags &= ~NETCONN_FLAG_PKTINFO;
}if (conn) { ... }
/* ... */#endif
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;
/* ... */
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
static void
lwip_link_select_cb(struct lwip_select_cb *select_cb)
{
LWIP_SOCKET_SELECT_DECL_PROTECT(lev);
LWIP_SOCKET_SELECT_PROTECT(lev);
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
select_cb_ctr++;/* ... */
#endif
LWIP_SOCKET_SELECT_UNPROTECT(lev);
}lwip_link_select_cb (struct lwip_select_cb *select_cb) { ... }
static void
lwip_unlink_select_cb(struct lwip_select_cb *select_cb)
{
LWIP_SOCKET_SELECT_DECL_PROTECT(lev);
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
select_cb_ctr++;/* ... */
#endif
LWIP_SOCKET_SELECT_UNPROTECT(lev);
}lwip_unlink_select_cb (struct lwip_select_cb *select_cb) { ... }
/* ... */#endif
#if LWIP_SOCKET_SELECT
/* ... */
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);
/* ... */
for (i = LWIP_SOCKET_OFFSET; i < maxfdp1; i++) {
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))) { ... }
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);
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))) { ... }
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)) { ... }
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);
return -1;
}else { ... }
}for (i = LWIP_SOCKET_OFFSET; i < maxfdp1; i++) { ... }
*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
/* ... */
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 (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) {
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) { ... }
/* ... */
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) { ... }
static void
lwip_select_dec_sockets_used(int maxfdp, fd_set *used_sockets)
{
int i;
for (i = LWIP_SOCKET_OFFSET; i < maxfdp; i++) {
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
#define lwip_select_inc_sockets_used(maxfdp1, readset, writeset, exceptset, used_sockets)
#define lwip_select_dec_sockets_used(maxfdp1, used_sockets)
/* ... */#endif
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);
/* ... */
nready = lwip_selscan(maxfdp1, readset, writeset, exceptset, &lreadset, &lwriteset, &lexceptset);
if (nready < 0) {
set_errno(EBADF);
lwip_select_dec_sockets_used(maxfdp1, &used_sockets);
return -1;
}if (nready < 0) { ... } else if (nready > 0) {
LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: nready=%d\n", nready));
}else if (nready > 0) { ... } else {
if (timeout && timeout->tv_sec == 0 && timeout->tv_usec == 0) {
LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: no timeout, returning 0\n"));
/* ... */
}if (timeout && timeout->tv_sec == 0 && timeout->tv_usec == 0) { ... } else {
/* ... */
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
if (sys_sem_new(&API_SELECT_CB_VAR_REF(select_cb).sem, 0) != ERR_OK) {
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_link_select_cb(&API_SELECT_CB_VAR_REF(select_cb));
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) {
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 {
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) {
/* ... */
nready = lwip_selscan(maxfdp1, readset, writeset, exceptset, &lreadset, &lwriteset, &lexceptset);
if (!nready) {
if (timeout == 0) {
msectimeout = 0;
}if (timeout == 0) { ... } else {
long msecs_long = ((timeout->tv_sec * 1000) + ((timeout->tv_usec + 500) / 1000));
if (msecs_long <= 0) {
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) { ... }
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) {
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);
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))) {
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
sys_sem_free(&API_SELECT_CB_VAR_REF(select_cb).sem);
#endif
API_SELECT_CB_VAR_FREE(select_cb);
if (nready < 0) {
lwip_select_dec_sockets_used(maxfdp1, &used_sockets);
return -1;
}if (nready < 0) { ... }
if (waitres == SYS_ARCH_TIMEOUT) {
LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: timeout expired\n"));
/* ... */
}if (waitres == SYS_ARCH_TIMEOUT) { ... } else {
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
#if LWIP_SOCKET_POLL
enum lwip_pollscan_opts
{
LWIP_POLLSCAN_CLEAR = 1,
LWIP_POLLSCAN_INC_WAIT = 2,
LWIP_POLLSCAN_DEC_WAIT = 4
...};
/* ... */
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);
for (fdi = 0; fdi < nfds; fdi++) {
if ((opts & LWIP_POLLSCAN_CLEAR) != 0) {
fds[fdi].revents = 0;
}if ((opts & LWIP_POLLSCAN_CLEAR) != 0) { ... }
/* ... */
if (fds[fdi].fd >= 0 && (fds[fdi].revents & POLLNVAL) == 0) {
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) {
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) {
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);
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))) { ... }
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)) { ... }
if (errevent != 0) {
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 {
SYS_ARCH_UNPROTECT(lev);
fds[fdi].revents |= POLLNVAL;
return -1;
}else { ... }
}if (fds[fdi].fd >= 0 && (fds[fdi].revents & POLLNVAL) == 0) { ... }
/* ... */
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
/* ... */
static void
lwip_poll_inc_sockets_used(struct pollfd *fds, nfds_t nfds)
{
nfds_t fdi;
if(fds) {
for (fdi = 0; fdi < nfds; fdi++) {
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) { ... }
static void
lwip_poll_dec_sockets_used(struct pollfd *fds, nfds_t nfds)
{
nfds_t fdi;
if(fds) {
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
#define lwip_poll_inc_sockets_used(fds, nfds)
#define lwip_poll_dec_sockets_used(fds, nfds)
/* ... */#endif
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);
/* ... */
nready = lwip_pollscan(fds, nfds, LWIP_POLLSCAN_CLEAR);
if (nready < 0) {
lwip_poll_dec_sockets_used(fds, nfds);
return -1;
}if (nready < 0) { ... }
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));
/* ... */
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
if (sys_sem_new(&API_SELECT_CB_VAR_REF(select_cb).sem, 0) != ERR_OK) {
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_link_select_cb(&API_SELECT_CB_VAR_REF(select_cb));
/* ... */
nready = lwip_pollscan(fds, nfds, LWIP_POLLSCAN_INC_WAIT);
if (!nready) {
if (timeout < 0) {
msectimeout = 0;
}if (timeout < 0) { ... } else {
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) { ... }
/* ... */
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))) {
sys_arch_sem_wait(API_SELECT_CB_VAR_REF(select_cb).sem, 1);
}if (select_cb.sem_signalled && (!waited || (waitres == SYS_ARCH_TIMEOUT))) { ... }
/* ... */#else
sys_sem_free(&API_SELECT_CB_VAR_REF(select_cb).sem);
#endif
API_SELECT_CB_VAR_FREE(select_cb);
if (nready < 0) {
lwip_poll_dec_sockets_used(fds, nfds);
return -1;
}if (nready < 0) { ... }
if (waitres == SYS_ARCH_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) { ... }
/* ... */
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) {
/* ... */
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) {
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
#if LWIP_SOCKET_SELECT || LWIP_SOCKET_POLL
/* ... */
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);
if (conn) {
s = conn->socket;
if (s < 0) {
/* ... */
SYS_ARCH_PROTECT(lev);
if (conn->socket < 0) {
if (evt == NETCONN_EVT_RCVPLUS) {
/* ... */
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);
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) {
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);
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) { ... }
/* ... */
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_ASSERT_CORE_LOCKED();
#if !LWIP_TCPIP_CORE_LOCKING
SYS_ARCH_PROTECT(lev);
again:
last_select_cb_ctr = select_cb_ctr;/* ... */
#endif
for (scb = select_cb_list; scb != NULL; scb = scb->next) {
if (scb->sem_signalled == 0) {
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
#if LWIP_SOCKET_SELECT && LWIP_SOCKET_POLL
else
#endif
#if LWIP_SOCKET_SELECT
{
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
if (do_signal) {
scb->sem_signalled = 1;
/* ... */
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
SYS_ARCH_UNPROTECT(lev);
SYS_ARCH_PROTECT(lev);
if (last_select_cb_ctr != select_cb_ctr) {
goto again;
}if (last_select_cb_ctr != select_cb_ctr) { ... }
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
/* ... */
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) { ... }
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
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
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
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
LOCK_TCPIP_CORE();
err = lwip_getsockopt_impl(s, level, optname, optval, optlen);
UNLOCK_TCPIP_CORE();
/* ... */
#else
#if LWIP_MPU_COMPATIBLE
if (*optlen > LWIP_SETGETSOCKOPT_MAXOPTLEN) {
sock_set_errno(sock, ENOBUFS);
done_socket(sock);
return -1;
}if (*optlen > LWIP_SETGETSOCKOPT_MAXOPTLEN) { ... }
/* ... */#endif
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_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);
*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
err = LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).err;
LWIP_SETGETSOCKOPT_DATA_VAR_FREE(data);/* ... */
#endif
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
/* ... */
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
data->optval.p,
#endif
&data->optlen);
sys_sem_signal((sys_sem_t *)(data->completed_sem));
}lwip_getsockopt_callback (void *arg) { ... }
/* ... */#endif
static int
lwip_sockopt_to_ipopt(int optname)
{
/* ... */
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) { ... }
/* ... */
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) {
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
case SO_ACCEPTCONN:
case SO_BROADCAST:
case SO_KEEPALIVE:
#if SO_REUSEcase SO_KEEPALIVE:
case SO_REUSEADDR:
#endif
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:
*(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))) { ... }
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
#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
#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
#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
#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)) {
done_socket(sock);
return EAFNOSUPPORT;
}if (udp_is_flag_set(sock->conn->pcb.udp, UDP_FLAGS_UDPLITE)) { ... }
/* ... */#endif
*(int *)optval = udp_is_flag_set(sock->conn->pcb.udp, UDP_FLAGS_NOCHKSUM) ? 1 : 0;
break;/* ... */
#endif 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) { ... }
break;
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
default:
LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, UNIMPL: optname=0x%x, ..)\n",
s, optname));
err = ENOPROTOOPT;
break;default
}switch (optname) { ... }
break;
#if LWIP_TCP
case IPPROTO_TCP:
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
default:
LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, UNIMPL: optname=0x%x, ..)\n",
s, optname));
err = ENOPROTOOPT;
break;default
}switch (optname) { ... }
break;/* ... */
#endif
#if LWIP_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) { ... }
break;/* ... */
#endif
#if LWIP_UDP && LWIP_UDPLITE
case IPPROTO_UDPLITE:
LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, int);
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) { ... }
break;/* ... */
#endif
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 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) { ... }
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) { ... }
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
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
LOCK_TCPIP_CORE();
err = lwip_setsockopt_impl(s, level, optname, optval, optlen);
UNLOCK_TCPIP_CORE();
/* ... */
#else
#if LWIP_MPU_COMPATIBLE
if (optlen > LWIP_SETGETSOCKOPT_MAXOPTLEN) {
sock_set_errno(sock, ENOBUFS);
done_socket(sock);
return -1;
}if (optlen > LWIP_SETGETSOCKOPT_MAXOPTLEN) { ... }
/* ... */#endif
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_SETGETSOCKOPT_DATA_VAR_REF(data).optval.pc = (const void *)optval;
#endif
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);
err = LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).err;
LWIP_SETGETSOCKOPT_DATA_VAR_FREE(data);/* ... */
#endif
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
/* ... */
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
data->optval.pc,
#endif
data->optlen);
sys_sem_signal((sys_sem_t *)(data->completed_sem));
}lwip_setsockopt_callback (void *arg) { ... }
/* ... */#endif
/* ... */
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) {
case SOL_SOCKET:
switch (optname) {
case SO_BROADCAST:
case SO_KEEPALIVE:
#if SO_REUSEcase SO_KEEPALIVE:
case SO_REUSEADDR:
#endif
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;
#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
#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
#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
#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
#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)) {
done_socket(sock);
return EAFNOSUPPORT;
}if (udp_is_flag_set(sock->conn->pcb.udp, UDP_FLAGS_UDPLITE)) { ... }
/* ... */#endif
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 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) { ... }
break;
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
#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
#if LWIP_IGMP
case IP_ADD_MEMBERSHIP:
case IP_DROP_MEMBERSHIP: {
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)) {
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
default:
LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, UNIMPL: optname=0x%x, ..)\n",
s, optname));
err = ENOPROTOOPT;
break;default
}switch (optname) { ... }
break;
#if LWIP_TCP
case IPPROTO_TCP:
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
default:
LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, UNIMPL: optname=0x%x, ..)\n",
s, optname));
err = ENOPROTOOPT;
break;default
}switch (optname) { ... }
break;/* ... */
#endif
#if LWIP_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: {
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)) {
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
default:
LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IPV6, UNIMPL: optname=0x%x, ..)\n",
s, optname));
err = ENOPROTOOPT;
break;default
}switch (optname) { ... }
break;/* ... */
#endif
#if LWIP_UDP && LWIP_UDPLITE
case IPPROTO_UDPLITE:
LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, int);
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))) {
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))) {
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) { ... }
break;/* ... */
#endif
case IPPROTO_RAW:
switch (optname) {
#if LWIP_IPV6 && LWIP_RAW
case IPV6_CHECKSUM:
/* ... */
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) {
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 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) { ... }
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) { ... }
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
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
#if LWIP_SO_RCVBUF
SYS_ARCH_GET(sock->conn->recv_avail, recv_avail);
if (recv_avail < 0) {
recv_avail = 0;
}if (recv_avail < 0) { ... }
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
break;
#endif /* ... */
#endif
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) { ... }
LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, UNIMPL: 0x%lx, %p)\n", s, cmd, argp));
sock_set_errno(sock, ENOSYS);
done_socket(sock);
return -1;
}lwip_ioctl (int s, long cmd, void *argp) { ... }
/* ... */
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);
/* ... */
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 { ... }
ret |= (op_mode == (O_RDONLY | O_WRONLY)) ? O_RDWR : op_mode;
break;case F_GETFL:
case F_SETFL:
val &= ~(O_RDONLY | O_WRONLY | O_RDWR);
if ((val & ~O_NONBLOCK) == 0) {
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);
}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);
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: {
/* ... */
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
/* ... */
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) { ... }
/* ... */
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) { ... }
/* ... */
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
#if LWIP_IPV6_MLD
/* ... */
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) { ... }
/* ... */
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) { ... }
/* ... */
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
/* ... */
#endif