1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
//! Types and constants for `rustix::net`.

use crate::backend::c;
use bitflags::bitflags;

/// A type for holding raw integer socket types.
pub type RawSocketType = u32;

/// `SOCK_*` constants for use with [`socket`].
///
/// [`socket`]: crate::net::socket()
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
#[repr(transparent)]
pub struct SocketType(pub(crate) RawSocketType);

#[rustfmt::skip]
impl SocketType {
    /// `SOCK_STREAM`
    pub const STREAM: Self = Self(c::SOCK_STREAM as _);

    /// `SOCK_DGRAM`
    pub const DGRAM: Self = Self(c::SOCK_DGRAM as _);

    /// `SOCK_SEQPACKET`
    #[cfg(not(target_os = "espidf"))]
    pub const SEQPACKET: Self = Self(c::SOCK_SEQPACKET as _);

    /// `SOCK_RAW`
    #[cfg(not(target_os = "espidf"))]
    pub const RAW: Self = Self(c::SOCK_RAW as _);

    /// `SOCK_RDM`
    #[cfg(not(any(target_os = "espidf", target_os = "haiku")))]
    pub const RDM: Self = Self(c::SOCK_RDM as _);

    /// Constructs a `SocketType` from a raw integer.
    #[inline]
    pub const fn from_raw(raw: RawSocketType) -> Self {
        Self(raw)
    }

    /// Returns the raw integer for this `SocketType`.
    #[inline]
    pub const fn as_raw(self) -> RawSocketType {
        self.0
    }
}

/// A type for holding raw integer address families.
pub type RawAddressFamily = c::sa_family_t;

/// `AF_*` constants for use with [`socket`], [`socket_with`], and
/// [`socketpair`].
///
/// [`socket`]: crate::net::socket()
/// [`socket_with`]: crate::net::socket_with
/// [`socketpair`]: crate::net::socketpair()
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
#[repr(transparent)]
pub struct AddressFamily(pub(crate) RawAddressFamily);

#[rustfmt::skip]
#[allow(non_upper_case_globals)]
impl AddressFamily {
    /// `AF_UNSPEC`
    pub const UNSPEC: Self = Self(c::AF_UNSPEC as _);
    /// `AF_INET`
    ///
    /// # References
    ///  - [Linux]
    ///
    /// [Linux]: https://man7.org/linux/man-pages/man7/ip.7.html
    pub const INET: Self = Self(c::AF_INET as _);
    /// `AF_INET6`
    ///
    /// # References
    ///  - [Linux]
    ///
    /// [Linux]: https://man7.org/linux/man-pages/man7/ipv6.7.html
    pub const INET6: Self = Self(c::AF_INET6 as _);
    /// `AF_NETLINK`
    ///
    /// # References
    ///  - [Linux]
    ///
    /// [Linux]: https://man7.org/linux/man-pages/man7/netlink.7.html
    #[cfg(not(any(
        bsd,
        solarish,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const NETLINK: Self = Self(c::AF_NETLINK as _);
    /// `AF_UNIX`, aka `AF_LOCAL`
    #[doc(alias = "LOCAL")]
    pub const UNIX: Self = Self(c::AF_UNIX as _);
    /// `AF_AX25`
    #[cfg(not(any(
        bsd,
        solarish,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const AX25: Self = Self(c::AF_AX25 as _);
    /// `AF_IPX`
    #[cfg(not(any(
        target_os = "aix",
        target_os = "espidf",
        target_os = "vita",
    )))]
    pub const IPX: Self = Self(c::AF_IPX as _);
    /// `AF_APPLETALK`
    #[cfg(not(any(target_os = "espidf", target_os = "vita")))]
    pub const APPLETALK: Self = Self(c::AF_APPLETALK as _);
    /// `AF_NETROM`
    #[cfg(not(any(
        bsd,
        solarish,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const NETROM: Self = Self(c::AF_NETROM as _);
    /// `AF_BRIDGE`
    #[cfg(not(any(
        bsd,
        solarish,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const BRIDGE: Self = Self(c::AF_BRIDGE as _);
    /// `AF_ATMPVC`
    #[cfg(not(any(
        bsd,
        solarish,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const ATMPVC: Self = Self(c::AF_ATMPVC as _);
    /// `AF_X25`
    #[cfg(not(any(
        bsd,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const X25: Self = Self(c::AF_X25 as _);
    /// `AF_ROSE`
    #[cfg(not(any(
        bsd,
        solarish,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const ROSE: Self = Self(c::AF_ROSE as _);
    /// `AF_DECnet`
    #[cfg(not(any(target_os = "espidf", target_os = "haiku", target_os = "vita")))]
    pub const DECnet: Self = Self(c::AF_DECnet as _);
    /// `AF_NETBEUI`
    #[cfg(not(any(
        bsd,
        solarish,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const NETBEUI: Self = Self(c::AF_NETBEUI as _);
    /// `AF_SECURITY`
    #[cfg(not(any(
        bsd,
        solarish,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const SECURITY: Self = Self(c::AF_SECURITY as _);
    /// `AF_KEY`
    #[cfg(not(any(
        bsd,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const KEY: Self = Self(c::AF_KEY as _);
    /// `AF_PACKET`
    ///
    /// # References
    ///  - [Linux]
    ///
    /// [Linux]: https://man7.org/linux/man-pages/man7/packet.7.html
    #[cfg(not(any(
        bsd,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const PACKET: Self = Self(c::AF_PACKET as _);
    /// `AF_ASH`
    #[cfg(not(any(
        bsd,
        solarish,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const ASH: Self = Self(c::AF_ASH as _);
    /// `AF_ECONET`
    #[cfg(not(any(
        bsd,
        solarish,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const ECONET: Self = Self(c::AF_ECONET as _);
    /// `AF_ATMSVC`
    #[cfg(not(any(
        bsd,
        solarish,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const ATMSVC: Self = Self(c::AF_ATMSVC as _);
    /// `AF_RDS`
    #[cfg(not(any(
        bsd,
        solarish,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const RDS: Self = Self(c::AF_RDS as _);
    /// `AF_SNA`
    #[cfg(not(any(target_os = "espidf", target_os = "haiku", target_os = "vita")))]
    pub const SNA: Self = Self(c::AF_SNA as _);
    /// `AF_IRDA`
    #[cfg(not(any(
        bsd,
        solarish,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const IRDA: Self = Self(c::AF_IRDA as _);
    /// `AF_PPPOX`
    #[cfg(not(any(
        bsd,
        solarish,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const PPPOX: Self = Self(c::AF_PPPOX as _);
    /// `AF_WANPIPE`
    #[cfg(not(any(
        bsd,
        solarish,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const WANPIPE: Self = Self(c::AF_WANPIPE as _);
    /// `AF_LLC`
    #[cfg(not(any(
        bsd,
        solarish,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const LLC: Self = Self(c::AF_LLC as _);
    /// `AF_CAN`
    #[cfg(not(any(
        bsd,
        solarish,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const CAN: Self = Self(c::AF_CAN as _);
    /// `AF_TIPC`
    #[cfg(not(any(
        bsd,
        solarish,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const TIPC: Self = Self(c::AF_TIPC as _);
    /// `AF_BLUETOOTH`
    #[cfg(not(any(
        apple,
        solarish,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "vita",
    )))]
    pub const BLUETOOTH: Self = Self(c::AF_BLUETOOTH as _);
    /// `AF_IUCV`
    #[cfg(not(any(
        bsd,
        solarish,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const IUCV: Self = Self(c::AF_IUCV as _);
    /// `AF_RXRPC`
    #[cfg(not(any(
        bsd,
        solarish,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const RXRPC: Self = Self(c::AF_RXRPC as _);
    /// `AF_ISDN`
    #[cfg(not(any(
        solarish,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "vita",
    )))]
    pub const ISDN: Self = Self(c::AF_ISDN as _);
    /// `AF_PHONET`
    #[cfg(not(any(
        bsd,
        solarish,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const PHONET: Self = Self(c::AF_PHONET as _);
    /// `AF_IEEE802154`
    #[cfg(not(any(
        bsd,
        solarish,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const IEEE802154: Self = Self(c::AF_IEEE802154 as _);
    /// `AF_802`
    #[cfg(solarish)]
    pub const EIGHT_ZERO_TWO: Self = Self(c::AF_802 as _);
    #[cfg(target_os = "fuchsia")]
    /// `AF_ALG`
    pub const ALG: Self = Self(c::AF_ALG as _);
    #[cfg(any(target_os = "freebsd", target_os = "netbsd", target_os = "nto"))]
    /// `AF_ARP`
    pub const ARP: Self = Self(c::AF_ARP as _);
    /// `AF_ATM`
    #[cfg(freebsdlike)]
    pub const ATM: Self = Self(c::AF_ATM as _);
    /// `AF_CAIF`
    #[cfg(any(target_os = "android", target_os = "emscripten", target_os = "fuchsia"))]
    pub const CAIF: Self = Self(c::AF_CAIF as _);
    /// `AF_CCITT`
    #[cfg(any(bsd, solarish, target_os = "aix", target_os = "nto"))]
    pub const CCITT: Self = Self(c::AF_CCITT as _);
    /// `AF_CHAOS`
    #[cfg(any(bsd, solarish, target_os = "aix", target_os = "nto"))]
    pub const CHAOS: Self = Self(c::AF_CHAOS as _);
    /// `AF_CNT`
    #[cfg(any(bsd, target_os = "nto"))]
    pub const CNT: Self = Self(c::AF_CNT as _);
    /// `AF_COIP`
    #[cfg(any(bsd, target_os = "nto"))]
    pub const COIP: Self = Self(c::AF_COIP as _);
    /// `AF_DATAKIT`
    #[cfg(any(bsd, solarish, target_os = "aix", target_os = "nto"))]
    pub const DATAKIT: Self = Self(c::AF_DATAKIT as _);
    /// `AF_DLI`
    #[cfg(any(bsd, solarish, target_os = "aix", target_os = "haiku", target_os = "nto"))]
    pub const DLI: Self = Self(c::AF_DLI as _);
    /// `AF_E164`
    #[cfg(any(bsd, target_os = "nto"))]
    pub const E164: Self = Self(c::AF_E164 as _);
    /// `AF_ECMA`
    #[cfg(any(apple, freebsdlike, solarish, target_os = "aix", target_os = "nto", target_os = "openbsd"))]
    pub const ECMA: Self = Self(c::AF_ECMA as _);
    /// `AF_ENCAP`
    #[cfg(target_os = "openbsd")]
    pub const ENCAP: Self = Self(c::AF_ENCAP as _);
    /// `AF_FILE`
    #[cfg(solarish)]
    pub const FILE: Self = Self(c::AF_FILE as _);
    /// `AF_GOSIP`
    #[cfg(solarish)]
    pub const GOSIP: Self = Self(c::AF_GOSIP as _);
    /// `AF_HYLINK`
    #[cfg(any(bsd, solarish, target_os = "aix", target_os = "nto"))]
    pub const HYLINK: Self = Self(c::AF_HYLINK as _);
    /// `AF_IB`
    #[cfg(any(target_os = "emscripten", target_os = "fuchsia"))]
    pub const IB: Self = Self(c::AF_IB as _);
    /// `AF_IMPLINK`
    #[cfg(any(bsd, solarish, target_os = "aix", target_os = "nto"))]
    pub const IMPLINK: Self = Self(c::AF_IMPLINK as _);
    /// `AF_IEEE80211`
    #[cfg(any(apple, freebsdlike, target_os = "netbsd"))]
    pub const IEEE80211: Self = Self(c::AF_IEEE80211 as _);
    /// `AF_INET6_SDP`
    #[cfg(target_os = "freebsd")]
    pub const INET6_SDP: Self = Self(c::AF_INET6_SDP as _);
    /// `AF_INET_OFFLOAD`
    #[cfg(solarish)]
    pub const INET_OFFLOAD: Self = Self(c::AF_INET_OFFLOAD as _);
    /// `AF_INET_SDP`
    #[cfg(target_os = "freebsd")]
    pub const INET_SDP: Self = Self(c::AF_INET_SDP as _);
    /// `AF_INTF`
    #[cfg(target_os = "aix")]
    pub const INTF: Self = Self(c::AF_INTF as _);
    /// `AF_ISO`
    #[cfg(any(bsd, target_os = "aix", target_os = "nto"))]
    pub const ISO: Self = Self(c::AF_ISO as _);
    /// `AF_LAT`
    #[cfg(any(bsd, solarish, target_os = "aix", target_os = "nto"))]
    pub const LAT: Self = Self(c::AF_LAT as _);
    /// `AF_LINK`
    #[cfg(any(bsd, solarish, target_os = "aix", target_os = "haiku", target_os = "nto"))]
    pub const LINK: Self = Self(c::AF_LINK as _);
    /// `AF_MPLS`
    #[cfg(any(netbsdlike, target_os = "dragonfly", target_os = "emscripten", target_os = "fuchsia"))]
    pub const MPLS: Self = Self(c::AF_MPLS as _);
    /// `AF_NATM`
    #[cfg(any(bsd, target_os = "nto"))]
    pub const NATM: Self = Self(c::AF_NATM as _);
    /// `AF_NBS`
    #[cfg(solarish)]
    pub const NBS: Self = Self(c::AF_NBS as _);
    /// `AF_NCA`
    #[cfg(solarish)]
    pub const NCA: Self = Self(c::AF_NCA as _);
    /// `AF_NDD`
    #[cfg(target_os = "aix")]
    pub const NDD: Self = Self(c::AF_NDD as _);
    /// `AF_NDRV`
    #[cfg(apple)]
    pub const NDRV: Self = Self(c::AF_NDRV as _);
    /// `AF_NETBIOS`
    #[cfg(any(apple, freebsdlike))]
    pub const NETBIOS: Self = Self(c::AF_NETBIOS as _);
    /// `AF_NETGRAPH`
    #[cfg(freebsdlike)]
    pub const NETGRAPH: Self = Self(c::AF_NETGRAPH as _);
    /// `AF_NIT`
    #[cfg(solarish)]
    pub const NIT: Self = Self(c::AF_NIT as _);
    /// `AF_NOTIFY`
    #[cfg(target_os = "haiku")]
    pub const NOTIFY: Self = Self(c::AF_NOTIFY as _);
    /// `AF_NFC`
    #[cfg(any(target_os = "emscripten", target_os = "fuchsia"))]
    pub const NFC: Self = Self(c::AF_NFC as _);
    /// `AF_NS`
    #[cfg(any(apple, solarish, netbsdlike, target_os = "aix", target_os = "nto"))]
    pub const NS: Self = Self(c::AF_NS as _);
    /// `AF_OROUTE`
    #[cfg(target_os = "netbsd")]
    pub const OROUTE: Self = Self(c::AF_OROUTE as _);
    /// `AF_OSI`
    #[cfg(any(bsd, solarish, target_os = "aix", target_os = "nto"))]
    pub const OSI: Self = Self(c::AF_OSI as _);
    /// `AF_OSINET`
    #[cfg(solarish)]
    pub const OSINET: Self = Self(c::AF_OSINET as _);
    /// `AF_POLICY`
    #[cfg(solarish)]
    pub const POLICY: Self = Self(c::AF_POLICY as _);
    /// `AF_PPP`
    #[cfg(apple)]
    pub const PPP: Self = Self(c::AF_PPP as _);
    /// `AF_PUP`
    #[cfg(any(bsd, solarish, target_os = "aix", target_os = "nto"))]
    pub const PUP: Self = Self(c::AF_PUP as _);
    /// `AF_RIF`
    #[cfg(target_os = "aix")]
    pub const RIF: Self = Self(c::AF_RIF as _);
    /// `AF_ROUTE`
    #[cfg(any(bsd, solarish, target_os = "android", target_os = "emscripten", target_os = "fuchsia", target_os = "haiku", target_os = "nto"))]
    pub const ROUTE: Self = Self(c::AF_ROUTE as _);
    /// `AF_SCLUSTER`
    #[cfg(target_os = "freebsd")]
    pub const SCLUSTER: Self = Self(c::AF_SCLUSTER as _);
    /// `AF_SIP`
    #[cfg(any(apple, target_os = "freebsd", target_os = "openbsd"))]
    pub const SIP: Self = Self(c::AF_SIP as _);
    /// `AF_SLOW`
    #[cfg(target_os = "freebsd")]
    pub const SLOW: Self = Self(c::AF_SLOW as _);
    /// `AF_SYS_CONTROL`
    #[cfg(apple)]
    pub const SYS_CONTROL: Self = Self(c::AF_SYS_CONTROL as _);
    /// `AF_SYSTEM`
    #[cfg(apple)]
    pub const SYSTEM: Self = Self(c::AF_SYSTEM as _);
    /// `AF_TRILL`
    #[cfg(solarish)]
    pub const TRILL: Self = Self(c::AF_TRILL as _);
    /// `AF_UTUN`
    #[cfg(apple)]
    pub const UTUN: Self = Self(c::AF_UTUN as _);
    /// `AF_VSOCK`
    #[cfg(any(apple, target_os = "emscripten", target_os = "fuchsia"))]
    pub const VSOCK: Self = Self(c::AF_VSOCK as _);
    /// `AF_XDP`
    #[cfg(target_os = "linux")]
    pub const XDP: Self = Self(c::AF_XDP as _);

    /// Constructs a `AddressFamily` from a raw integer.
    #[inline]
    pub const fn from_raw(raw: RawAddressFamily) -> Self {
        Self(raw)
    }

    /// Returns the raw integer for this `AddressFamily`.
    #[inline]
    pub const fn as_raw(self) -> RawAddressFamily {
        self.0
    }
}

/// A type for holding raw integer protocols.
pub type RawProtocol = core::num::NonZeroU32;

const fn new_raw_protocol(u: u32) -> RawProtocol {
    match RawProtocol::new(u) {
        Some(p) => p,
        None => panic!("new_raw_protocol: protocol must be non-zero"),
    }
}

/// `IPPROTO_*` and other constants for use with [`socket`], [`socket_with`],
/// and [`socketpair`] when a nondefault value is desired. See the [`ipproto`],
/// [`sysproto`], and [`netlink`] modules for possible values.
///
/// For the default values, such as `IPPROTO_IP` or `NETLINK_ROUTE`, pass
/// `None` as the `protocol` argument in these functions.
///
/// [`socket`]: crate::net::socket()
/// [`socket_with`]: crate::net::socket_with
/// [`socketpair`]: crate::net::socketpair()
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
#[repr(transparent)]
#[doc(alias = "IPPROTO_IP")]
#[doc(alias = "NETLINK_ROUTE")]
pub struct Protocol(pub(crate) RawProtocol);

/// `IPPROTO_*` constants.
///
/// For `IPPROTO_IP`, pass `None` as the `protocol` argument.
pub mod ipproto {
    use super::{new_raw_protocol, Protocol};
    use crate::backend::c;

    /// `IPPROTO_ICMP`
    pub const ICMP: Protocol = Protocol(new_raw_protocol(c::IPPROTO_ICMP as _));
    /// `IPPROTO_IGMP`
    #[cfg(not(any(
        solarish,
        target_os = "espidf",
        target_os = "haiku",
        target_os = "vita"
    )))]
    pub const IGMP: Protocol = Protocol(new_raw_protocol(c::IPPROTO_IGMP as _));
    /// `IPPROTO_IPIP`
    #[cfg(not(any(
        solarish,
        windows,
        target_os = "espidf",
        target_os = "haiku",
        target_os = "vita"
    )))]
    pub const IPIP: Protocol = Protocol(new_raw_protocol(c::IPPROTO_IPIP as _));
    /// `IPPROTO_TCP`
    pub const TCP: Protocol = Protocol(new_raw_protocol(c::IPPROTO_TCP as _));
    /// `IPPROTO_EGP`
    #[cfg(not(any(
        solarish,
        target_os = "espidf",
        target_os = "haiku",
        target_os = "vita"
    )))]
    pub const EGP: Protocol = Protocol(new_raw_protocol(c::IPPROTO_EGP as _));
    /// `IPPROTO_PUP`
    #[cfg(not(any(
        solarish,
        target_os = "espidf",
        target_os = "haiku",
        target_os = "vita"
    )))]
    pub const PUP: Protocol = Protocol(new_raw_protocol(c::IPPROTO_PUP as _));
    /// `IPPROTO_UDP`
    pub const UDP: Protocol = Protocol(new_raw_protocol(c::IPPROTO_UDP as _));
    /// `IPPROTO_IDP`
    #[cfg(not(any(
        solarish,
        target_os = "espidf",
        target_os = "haiku",
        target_os = "vita"
    )))]
    pub const IDP: Protocol = Protocol(new_raw_protocol(c::IPPROTO_IDP as _));
    /// `IPPROTO_TP`
    #[cfg(not(any(
        solarish,
        windows,
        target_os = "espidf",
        target_os = "haiku",
        target_os = "vita"
    )))]
    pub const TP: Protocol = Protocol(new_raw_protocol(c::IPPROTO_TP as _));
    /// `IPPROTO_DCCP`
    #[cfg(not(any(
        apple,
        solarish,
        windows,
        target_os = "aix",
        target_os = "dragonfly",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "openbsd",
        target_os = "vita",
    )))]
    pub const DCCP: Protocol = Protocol(new_raw_protocol(c::IPPROTO_DCCP as _));
    /// `IPPROTO_IPV6`
    pub const IPV6: Protocol = Protocol(new_raw_protocol(c::IPPROTO_IPV6 as _));
    /// `IPPROTO_RSVP`
    #[cfg(not(any(
        solarish,
        windows,
        target_os = "espidf",
        target_os = "haiku",
        target_os = "vita"
    )))]
    pub const RSVP: Protocol = Protocol(new_raw_protocol(c::IPPROTO_RSVP as _));
    /// `IPPROTO_GRE`
    #[cfg(not(any(
        solarish,
        windows,
        target_os = "espidf",
        target_os = "haiku",
        target_os = "vita"
    )))]
    pub const GRE: Protocol = Protocol(new_raw_protocol(c::IPPROTO_GRE as _));
    /// `IPPROTO_ESP`
    #[cfg(not(any(
        solarish,
        target_os = "espidf",
        target_os = "haiku",
        target_os = "vita"
    )))]
    pub const ESP: Protocol = Protocol(new_raw_protocol(c::IPPROTO_ESP as _));
    /// `IPPROTO_AH`
    #[cfg(not(any(
        solarish,
        target_os = "espidf",
        target_os = "haiku",
        target_os = "vita"
    )))]
    pub const AH: Protocol = Protocol(new_raw_protocol(c::IPPROTO_AH as _));
    /// `IPPROTO_MTP`
    #[cfg(not(any(
        solarish,
        netbsdlike,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const MTP: Protocol = Protocol(new_raw_protocol(c::IPPROTO_MTP as _));
    /// `IPPROTO_BEETPH`
    #[cfg(not(any(
        bsd,
        solarish,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const BEETPH: Protocol = Protocol(new_raw_protocol(c::IPPROTO_BEETPH as _));
    /// `IPPROTO_ENCAP`
    #[cfg(not(any(
        solarish,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "vita",
    )))]
    pub const ENCAP: Protocol = Protocol(new_raw_protocol(c::IPPROTO_ENCAP as _));
    /// `IPPROTO_PIM`
    #[cfg(not(any(
        solarish,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "vita"
    )))]
    pub const PIM: Protocol = Protocol(new_raw_protocol(c::IPPROTO_PIM as _));
    /// `IPPROTO_COMP`
    #[cfg(not(any(
        bsd,
        solarish,
        windows,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const COMP: Protocol = Protocol(new_raw_protocol(c::IPPROTO_COMP as _));
    /// `IPPROTO_SCTP`
    #[cfg(not(any(
        solarish,
        target_os = "dragonfly",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "openbsd",
        target_os = "vita",
    )))]
    pub const SCTP: Protocol = Protocol(new_raw_protocol(c::IPPROTO_SCTP as _));
    /// `IPPROTO_UDPLITE`
    #[cfg(not(any(
        apple,
        netbsdlike,
        solarish,
        windows,
        target_os = "aix",
        target_os = "dragonfly",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const UDPLITE: Protocol = Protocol(new_raw_protocol(c::IPPROTO_UDPLITE as _));
    /// `IPPROTO_MPLS`
    #[cfg(not(any(
        apple,
        solarish,
        windows,
        target_os = "aix",
        target_os = "dragonfly",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "netbsd",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const MPLS: Protocol = Protocol(new_raw_protocol(c::IPPROTO_MPLS as _));
    /// `IPPROTO_ETHERNET`
    #[cfg(linux_kernel)]
    pub const ETHERNET: Protocol = Protocol(new_raw_protocol(c::IPPROTO_ETHERNET as _));
    /// `IPPROTO_RAW`
    #[cfg(not(any(target_os = "espidf", target_os = "vita")))]
    pub const RAW: Protocol = Protocol(new_raw_protocol(c::IPPROTO_RAW as _));
    /// `IPPROTO_MPTCP`
    #[cfg(not(any(
        bsd,
        solarish,
        windows,
        target_os = "aix",
        target_os = "emscripten",
        target_os = "espidf",
        target_os = "fuchsia",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const MPTCP: Protocol = Protocol(new_raw_protocol(c::IPPROTO_MPTCP as _));
    /// `IPPROTO_FRAGMENT`
    #[cfg(not(any(
        solarish,
        target_os = "espidf",
        target_os = "haiku",
        target_os = "vita"
    )))]
    pub const FRAGMENT: Protocol = Protocol(new_raw_protocol(c::IPPROTO_FRAGMENT as _));
    /// `IPPROTO_ICMPV6`
    pub const ICMPV6: Protocol = Protocol(new_raw_protocol(c::IPPROTO_ICMPV6 as _));
    /// `IPPROTO_MH`
    #[cfg(not(any(
        apple,
        netbsdlike,
        solarish,
        windows,
        target_os = "dragonfly",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita",
    )))]
    pub const MH: Protocol = Protocol(new_raw_protocol(c::IPPROTO_MH as _));
    /// `IPPROTO_ROUTING`
    #[cfg(not(any(
        solarish,
        target_os = "espidf",
        target_os = "haiku",
        target_os = "vita"
    )))]
    pub const ROUTING: Protocol = Protocol(new_raw_protocol(c::IPPROTO_ROUTING as _));
}

/// `SYSPROTO_*` constants.
pub mod sysproto {
    #[cfg(apple)]
    use {
        super::{new_raw_protocol, Protocol},
        crate::backend::c,
    };

    /// `SYSPROTO_EVENT`
    #[cfg(apple)]
    pub const EVENT: Protocol = Protocol(new_raw_protocol(c::SYSPROTO_EVENT as _));

    /// `SYSPROTO_CONTROL`
    #[cfg(apple)]
    pub const CONTROL: Protocol = Protocol(new_raw_protocol(c::SYSPROTO_CONTROL as _));
}

/// `NETLINK_*` constants.
///
/// For `NETLINK_ROUTE`, pass `None` as the `protocol` argument.
pub mod netlink {
    #[cfg(linux_kernel)]
    use {
        super::{new_raw_protocol, Protocol},
        crate::backend::c,
    };

    /// `NETLINK_UNUSED`
    #[cfg(linux_kernel)]
    pub const UNUSED: Protocol = Protocol(new_raw_protocol(c::NETLINK_UNUSED as _));
    /// `NETLINK_USERSOCK`
    #[cfg(linux_kernel)]
    pub const USERSOCK: Protocol = Protocol(new_raw_protocol(c::NETLINK_USERSOCK as _));
    /// `NETLINK_FIREWALL`
    #[cfg(linux_kernel)]
    pub const FIREWALL: Protocol = Protocol(new_raw_protocol(c::NETLINK_FIREWALL as _));
    /// `NETLINK_SOCK_DIAG`
    #[cfg(linux_kernel)]
    pub const SOCK_DIAG: Protocol = Protocol(new_raw_protocol(c::NETLINK_SOCK_DIAG as _));
    /// `NETLINK_NFLOG`
    #[cfg(linux_kernel)]
    pub const NFLOG: Protocol = Protocol(new_raw_protocol(c::NETLINK_NFLOG as _));
    /// `NETLINK_XFRM`
    #[cfg(linux_kernel)]
    pub const XFRM: Protocol = Protocol(new_raw_protocol(c::NETLINK_XFRM as _));
    /// `NETLINK_SELINUX`
    #[cfg(linux_kernel)]
    pub const SELINUX: Protocol = Protocol(new_raw_protocol(c::NETLINK_SELINUX as _));
    /// `NETLINK_ISCSI`
    #[cfg(linux_kernel)]
    pub const ISCSI: Protocol = Protocol(new_raw_protocol(c::NETLINK_ISCSI as _));
    /// `NETLINK_AUDIT`
    #[cfg(linux_kernel)]
    pub const AUDIT: Protocol = Protocol(new_raw_protocol(c::NETLINK_AUDIT as _));
    /// `NETLINK_FIB_LOOKUP`
    #[cfg(linux_kernel)]
    pub const FIB_LOOKUP: Protocol = Protocol(new_raw_protocol(c::NETLINK_FIB_LOOKUP as _));
    /// `NETLINK_CONNECTOR`
    #[cfg(linux_kernel)]
    pub const CONNECTOR: Protocol = Protocol(new_raw_protocol(c::NETLINK_CONNECTOR as _));
    /// `NETLINK_NETFILTER`
    #[cfg(linux_kernel)]
    pub const NETFILTER: Protocol = Protocol(new_raw_protocol(c::NETLINK_NETFILTER as _));
    /// `NETLINK_IP6_FW`
    #[cfg(linux_kernel)]
    pub const IP6_FW: Protocol = Protocol(new_raw_protocol(c::NETLINK_IP6_FW as _));
    /// `NETLINK_DNRTMSG`
    #[cfg(linux_kernel)]
    pub const DNRTMSG: Protocol = Protocol(new_raw_protocol(c::NETLINK_DNRTMSG as _));
    /// `NETLINK_KOBJECT_UEVENT`
    #[cfg(linux_kernel)]
    pub const KOBJECT_UEVENT: Protocol = Protocol(new_raw_protocol(c::NETLINK_KOBJECT_UEVENT as _));
    /// `NETLINK_GENERIC`
    // This is defined on FreeBSD too, but it has the value 0, so it doesn't
    // fit in or `NonZeroU32`. It's unclear whether FreeBSD intends
    // `NETLINK_GENERIC` to be the default when Linux has `NETLINK_ROUTE`
    // as the default.
    #[cfg(linux_kernel)]
    pub const GENERIC: Protocol = Protocol(new_raw_protocol(c::NETLINK_GENERIC as _));
    /// `NETLINK_SCSITRANSPORT`
    #[cfg(linux_kernel)]
    pub const SCSITRANSPORT: Protocol = Protocol(new_raw_protocol(c::NETLINK_SCSITRANSPORT as _));
    /// `NETLINK_ECRYPTFS`
    #[cfg(linux_kernel)]
    pub const ECRYPTFS: Protocol = Protocol(new_raw_protocol(c::NETLINK_ECRYPTFS as _));
    /// `NETLINK_RDMA`
    #[cfg(linux_kernel)]
    pub const RDMA: Protocol = Protocol(new_raw_protocol(c::NETLINK_RDMA as _));
    /// `NETLINK_CRYPTO`
    #[cfg(linux_kernel)]
    pub const CRYPTO: Protocol = Protocol(new_raw_protocol(c::NETLINK_CRYPTO as _));
    /// `NETLINK_INET_DIAG`
    #[cfg(linux_kernel)]
    pub const INET_DIAG: Protocol = Protocol(new_raw_protocol(c::NETLINK_INET_DIAG as _));
    /// `NETLINK_ADD_MEMBERSHIP`
    #[cfg(linux_kernel)]
    pub const ADD_MEMBERSHIP: Protocol = Protocol(new_raw_protocol(c::NETLINK_ADD_MEMBERSHIP as _));
    /// `NETLINK_DROP_MEMBERSHIP`
    #[cfg(linux_kernel)]
    pub const DROP_MEMBERSHIP: Protocol =
        Protocol(new_raw_protocol(c::NETLINK_DROP_MEMBERSHIP as _));
    /// `NETLINK_PKTINFO`
    #[cfg(linux_kernel)]
    pub const PKTINFO: Protocol = Protocol(new_raw_protocol(c::NETLINK_PKTINFO as _));
    /// `NETLINK_BROADCAST_ERROR`
    #[cfg(linux_kernel)]
    pub const BROADCAST_ERROR: Protocol =
        Protocol(new_raw_protocol(c::NETLINK_BROADCAST_ERROR as _));
    /// `NETLINK_NO_ENOBUFS`
    #[cfg(linux_kernel)]
    pub const NO_ENOBUFS: Protocol = Protocol(new_raw_protocol(c::NETLINK_NO_ENOBUFS as _));
    /// `NETLINK_RX_RING`
    #[cfg(linux_kernel)]
    pub const RX_RING: Protocol = Protocol(new_raw_protocol(c::NETLINK_RX_RING as _));
    /// `NETLINK_TX_RING`
    #[cfg(linux_kernel)]
    pub const TX_RING: Protocol = Protocol(new_raw_protocol(c::NETLINK_TX_RING as _));
    /// `NETLINK_LISTEN_ALL_NSID`
    #[cfg(linux_kernel)]
    pub const LISTEN_ALL_NSID: Protocol =
        Protocol(new_raw_protocol(c::NETLINK_LISTEN_ALL_NSID as _));
    /// `NETLINK_LIST_MEMBERSHIPS`
    #[cfg(linux_kernel)]
    pub const LIST_MEMBERSHIPS: Protocol =
        Protocol(new_raw_protocol(c::NETLINK_LIST_MEMBERSHIPS as _));
    /// `NETLINK_CAP_ACK`
    #[cfg(linux_kernel)]
    pub const CAP_ACK: Protocol = Protocol(new_raw_protocol(c::NETLINK_CAP_ACK as _));
    /// `NETLINK_EXT_ACK`
    #[cfg(linux_kernel)]
    pub const EXT_ACK: Protocol = Protocol(new_raw_protocol(c::NETLINK_EXT_ACK as _));
    /// `NETLINK_GET_STRICT_CHK`
    #[cfg(linux_kernel)]
    pub const GET_STRICT_CHK: Protocol = Protocol(new_raw_protocol(c::NETLINK_GET_STRICT_CHK as _));
}

/// `ETH_P_*` constants.
// These are translated into 16-bit big-endian form because that's what the
// [`AddressFamily::PACKET`] address family [expects].
//
// [expects]: https://man7.org/linux/man-pages/man7/packet.7.html
pub mod eth {
    #[cfg(linux_kernel)]
    use {
        super::{new_raw_protocol, Protocol},
        crate::backend::c,
    };

    /// `ETH_P_LOOP`
    #[cfg(linux_kernel)]
    pub const LOOP: Protocol = Protocol(new_raw_protocol((c::ETH_P_LOOP as u16).to_be() as u32));
    /// `ETH_P_PUP`
    #[cfg(linux_kernel)]
    pub const PUP: Protocol = Protocol(new_raw_protocol((c::ETH_P_PUP as u16).to_be() as u32));
    /// `ETH_P_PUPAT`
    #[cfg(linux_kernel)]
    pub const PUPAT: Protocol = Protocol(new_raw_protocol((c::ETH_P_PUPAT as u16).to_be() as u32));
    /// `ETH_P_TSN`
    #[cfg(linux_kernel)]
    pub const TSN: Protocol = Protocol(new_raw_protocol((c::ETH_P_TSN as u16).to_be() as u32));
    /// `ETH_P_ERSPAN2`
    #[cfg(linux_kernel)]
    pub const ERSPAN2: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_ERSPAN2 as u16).to_be() as u32));
    /// `ETH_P_IP`
    #[cfg(linux_kernel)]
    pub const IP: Protocol = Protocol(new_raw_protocol((c::ETH_P_IP as u16).to_be() as u32));
    /// `ETH_P_X25`
    #[cfg(linux_kernel)]
    pub const X25: Protocol = Protocol(new_raw_protocol((c::ETH_P_X25 as u16).to_be() as u32));
    /// `ETH_P_ARP`
    #[cfg(linux_kernel)]
    pub const ARP: Protocol = Protocol(new_raw_protocol((c::ETH_P_ARP as u16).to_be() as u32));
    /// `ETH_P_BPQ`
    #[cfg(linux_kernel)]
    pub const BPQ: Protocol = Protocol(new_raw_protocol((c::ETH_P_BPQ as u16).to_be() as u32));
    /// `ETH_P_IEEEPUP`
    #[cfg(linux_kernel)]
    pub const IEEEPUP: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_IEEEPUP as u16).to_be() as u32));
    /// `ETH_P_IEEEPUPAT`
    #[cfg(linux_kernel)]
    pub const IEEEPUPAT: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_IEEEPUPAT as u16).to_be() as u32));
    /// `ETH_P_BATMAN`
    #[cfg(linux_kernel)]
    pub const BATMAN: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_BATMAN as u16).to_be() as u32));
    /// `ETH_P_DEC`
    #[cfg(linux_kernel)]
    pub const DEC: Protocol = Protocol(new_raw_protocol((c::ETH_P_DEC as u16).to_be() as u32));
    /// `ETH_P_DNA_DL`
    #[cfg(linux_kernel)]
    pub const DNA_DL: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_DNA_DL as u16).to_be() as u32));
    /// `ETH_P_DNA_RC`
    #[cfg(linux_kernel)]
    pub const DNA_RC: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_DNA_RC as u16).to_be() as u32));
    /// `ETH_P_DNA_RT`
    #[cfg(linux_kernel)]
    pub const DNA_RT: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_DNA_RT as u16).to_be() as u32));
    /// `ETH_P_LAT`
    #[cfg(linux_kernel)]
    pub const LAT: Protocol = Protocol(new_raw_protocol((c::ETH_P_LAT as u16).to_be() as u32));
    /// `ETH_P_DIAG`
    #[cfg(linux_kernel)]
    pub const DIAG: Protocol = Protocol(new_raw_protocol((c::ETH_P_DIAG as u16).to_be() as u32));
    /// `ETH_P_CUST`
    #[cfg(linux_kernel)]
    pub const CUST: Protocol = Protocol(new_raw_protocol((c::ETH_P_CUST as u16).to_be() as u32));
    /// `ETH_P_SCA`
    #[cfg(linux_kernel)]
    pub const SCA: Protocol = Protocol(new_raw_protocol((c::ETH_P_SCA as u16).to_be() as u32));
    /// `ETH_P_TEB`
    #[cfg(linux_kernel)]
    pub const TEB: Protocol = Protocol(new_raw_protocol((c::ETH_P_TEB as u16).to_be() as u32));
    /// `ETH_P_RARP`
    #[cfg(linux_kernel)]
    pub const RARP: Protocol = Protocol(new_raw_protocol((c::ETH_P_RARP as u16).to_be() as u32));
    /// `ETH_P_ATALK`
    #[cfg(linux_kernel)]
    pub const ATALK: Protocol = Protocol(new_raw_protocol((c::ETH_P_ATALK as u16).to_be() as u32));
    /// `ETH_P_AARP`
    #[cfg(linux_kernel)]
    pub const AARP: Protocol = Protocol(new_raw_protocol((c::ETH_P_AARP as u16).to_be() as u32));
    /// `ETH_P_8021Q`
    #[cfg(linux_kernel)]
    pub const P_8021Q: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_8021Q as u16).to_be() as u32));
    /// `ETH_P_ERSPAN`
    #[cfg(linux_kernel)]
    pub const ERSPAN: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_ERSPAN as u16).to_be() as u32));
    /// `ETH_P_IPX`
    #[cfg(linux_kernel)]
    pub const IPX: Protocol = Protocol(new_raw_protocol((c::ETH_P_IPX as u16).to_be() as u32));
    /// `ETH_P_IPV6`
    #[cfg(linux_kernel)]
    pub const IPV6: Protocol = Protocol(new_raw_protocol((c::ETH_P_IPV6 as u16).to_be() as u32));
    /// `ETH_P_PAUSE`
    #[cfg(linux_kernel)]
    pub const PAUSE: Protocol = Protocol(new_raw_protocol((c::ETH_P_PAUSE as u16).to_be() as u32));
    /// `ETH_P_SLOW`
    #[cfg(linux_kernel)]
    pub const SLOW: Protocol = Protocol(new_raw_protocol((c::ETH_P_SLOW as u16).to_be() as u32));
    /// `ETH_P_WCCP`
    #[cfg(linux_kernel)]
    pub const WCCP: Protocol = Protocol(new_raw_protocol((c::ETH_P_WCCP as u16).to_be() as u32));
    /// `ETH_P_MPLS_UC`
    #[cfg(linux_kernel)]
    pub const MPLS_UC: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_MPLS_UC as u16).to_be() as u32));
    /// `ETH_P_MPLS_MC`
    #[cfg(linux_kernel)]
    pub const MPLS_MC: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_MPLS_MC as u16).to_be() as u32));
    /// `ETH_P_ATMMPOA`
    #[cfg(linux_kernel)]
    pub const ATMMPOA: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_ATMMPOA as u16).to_be() as u32));
    /// `ETH_P_PPP_DISC`
    #[cfg(linux_kernel)]
    pub const PPP_DISC: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_PPP_DISC as u16).to_be() as u32));
    /// `ETH_P_PPP_SES`
    #[cfg(linux_kernel)]
    pub const PPP_SES: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_PPP_SES as u16).to_be() as u32));
    /// `ETH_P_LINK_CTL`
    #[cfg(linux_kernel)]
    pub const LINK_CTL: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_LINK_CTL as u16).to_be() as u32));
    /// `ETH_P_ATMFATE`
    #[cfg(linux_kernel)]
    pub const ATMFATE: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_ATMFATE as u16).to_be() as u32));
    /// `ETH_P_PAE`
    #[cfg(linux_kernel)]
    pub const PAE: Protocol = Protocol(new_raw_protocol((c::ETH_P_PAE as u16).to_be() as u32));
    /// `ETH_P_PROFINET`
    #[cfg(linux_kernel)]
    pub const PROFINET: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_PROFINET as u16).to_be() as u32));
    /// `ETH_P_REALTEK`
    #[cfg(linux_kernel)]
    pub const REALTEK: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_REALTEK as u16).to_be() as u32));
    /// `ETH_P_AOE`
    #[cfg(linux_kernel)]
    pub const AOE: Protocol = Protocol(new_raw_protocol((c::ETH_P_AOE as u16).to_be() as u32));
    /// `ETH_P_ETHERCAT`
    #[cfg(linux_kernel)]
    pub const ETHERCAT: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_ETHERCAT as u16).to_be() as u32));
    /// `ETH_P_8021AD`
    #[cfg(linux_kernel)]
    pub const P_8021AD: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_8021AD as u16).to_be() as u32));
    /// `ETH_P_802_EX1`
    #[cfg(linux_kernel)]
    pub const P_802_EX1: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_802_EX1 as u16).to_be() as u32));
    /// `ETH_P_PREAUTH`
    #[cfg(linux_kernel)]
    pub const PREAUTH: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_PREAUTH as u16).to_be() as u32));
    /// `ETH_P_TIPC`
    #[cfg(linux_kernel)]
    pub const TIPC: Protocol = Protocol(new_raw_protocol((c::ETH_P_TIPC as u16).to_be() as u32));
    /// `ETH_P_LLDP`
    #[cfg(linux_kernel)]
    pub const LLDP: Protocol = Protocol(new_raw_protocol((c::ETH_P_LLDP as u16).to_be() as u32));
    /// `ETH_P_MRP`
    #[cfg(linux_kernel)]
    pub const MRP: Protocol = Protocol(new_raw_protocol((c::ETH_P_MRP as u16).to_be() as u32));
    /// `ETH_P_MACSEC`
    #[cfg(linux_kernel)]
    pub const MACSEC: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_MACSEC as u16).to_be() as u32));
    /// `ETH_P_8021AH`
    #[cfg(linux_kernel)]
    pub const P_8021AH: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_8021AH as u16).to_be() as u32));
    /// `ETH_P_MVRP`
    #[cfg(linux_kernel)]
    pub const MVRP: Protocol = Protocol(new_raw_protocol((c::ETH_P_MVRP as u16).to_be() as u32));
    /// `ETH_P_1588`
    #[cfg(linux_kernel)]
    pub const P_1588: Protocol = Protocol(new_raw_protocol((c::ETH_P_1588 as u16).to_be() as u32));
    /// `ETH_P_NCSI`
    #[cfg(linux_kernel)]
    pub const NCSI: Protocol = Protocol(new_raw_protocol((c::ETH_P_NCSI as u16).to_be() as u32));
    /// `ETH_P_PRP`
    #[cfg(linux_kernel)]
    pub const PRP: Protocol = Protocol(new_raw_protocol((c::ETH_P_PRP as u16).to_be() as u32));
    /// `ETH_P_CFM`
    #[cfg(linux_kernel)]
    pub const CFM: Protocol = Protocol(new_raw_protocol((c::ETH_P_CFM as u16).to_be() as u32));
    /// `ETH_P_FCOE`
    #[cfg(linux_kernel)]
    pub const FCOE: Protocol = Protocol(new_raw_protocol((c::ETH_P_FCOE as u16).to_be() as u32));
    /// `ETH_P_IBOE`
    #[cfg(linux_kernel)]
    pub const IBOE: Protocol = Protocol(new_raw_protocol((c::ETH_P_IBOE as u16).to_be() as u32));
    /// `ETH_P_TDLS`
    #[cfg(linux_kernel)]
    pub const TDLS: Protocol = Protocol(new_raw_protocol((c::ETH_P_TDLS as u16).to_be() as u32));
    /// `ETH_P_FIP`
    #[cfg(linux_kernel)]
    pub const FIP: Protocol = Protocol(new_raw_protocol((c::ETH_P_FIP as u16).to_be() as u32));
    /// `ETH_P_80221`
    #[cfg(linux_kernel)]
    pub const P_80221: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_80221 as u16).to_be() as u32));
    /// `ETH_P_HSR`
    #[cfg(linux_kernel)]
    pub const HSR: Protocol = Protocol(new_raw_protocol((c::ETH_P_HSR as u16).to_be() as u32));
    /// `ETH_P_NSH`
    #[cfg(linux_kernel)]
    pub const NSH: Protocol = Protocol(new_raw_protocol((c::ETH_P_NSH as u16).to_be() as u32));
    /// `ETH_P_LOOPBACK`
    #[cfg(linux_kernel)]
    pub const LOOPBACK: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_LOOPBACK as u16).to_be() as u32));
    /// `ETH_P_QINQ1`
    #[cfg(linux_kernel)]
    pub const QINQ1: Protocol = Protocol(new_raw_protocol((c::ETH_P_QINQ1 as u16).to_be() as u32));
    /// `ETH_P_QINQ2`
    #[cfg(linux_kernel)]
    pub const QINQ2: Protocol = Protocol(new_raw_protocol((c::ETH_P_QINQ2 as u16).to_be() as u32));
    /// `ETH_P_QINQ3`
    #[cfg(linux_kernel)]
    pub const QINQ3: Protocol = Protocol(new_raw_protocol((c::ETH_P_QINQ3 as u16).to_be() as u32));
    /// `ETH_P_EDSA`
    #[cfg(linux_kernel)]
    pub const EDSA: Protocol = Protocol(new_raw_protocol((c::ETH_P_EDSA as u16).to_be() as u32));
    /// `ETH_P_DSA_8021Q`
    #[cfg(linux_kernel)]
    pub const DSA_8021Q: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_DSA_8021Q as u16).to_be() as u32));
    /// `ETH_P_DSA_A5PSW`
    #[cfg(linux_kernel)]
    pub const DSA_A5PSW: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_DSA_A5PSW as u16).to_be() as u32));
    /// `ETH_P_IFE`
    #[cfg(linux_kernel)]
    pub const IFE: Protocol = Protocol(new_raw_protocol((c::ETH_P_IFE as u16).to_be() as u32));
    /// `ETH_P_AF_IUCV`
    #[cfg(linux_kernel)]
    pub const AF_IUCV: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_AF_IUCV as u16).to_be() as u32));
    /// `ETH_P_802_3_MIN`
    #[cfg(linux_kernel)]
    pub const P_802_3_MIN: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_802_3_MIN as u16).to_be() as u32));
    /// `ETH_P_802_3`
    #[cfg(linux_kernel)]
    pub const P_802_3: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_802_3 as u16).to_be() as u32));
    /// `ETH_P_AX25`
    #[cfg(linux_kernel)]
    pub const AX25: Protocol = Protocol(new_raw_protocol((c::ETH_P_AX25 as u16).to_be() as u32));
    /// `ETH_P_ALL`
    #[cfg(linux_kernel)]
    pub const ALL: Protocol = Protocol(new_raw_protocol((c::ETH_P_ALL as u16).to_be() as u32));
    /// `ETH_P_802_2`
    #[cfg(linux_kernel)]
    pub const P_802_2: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_802_2 as u16).to_be() as u32));
    /// `ETH_P_SNAP`
    #[cfg(linux_kernel)]
    pub const SNAP: Protocol = Protocol(new_raw_protocol((c::ETH_P_SNAP as u16).to_be() as u32));
    /// `ETH_P_DDCMP`
    #[cfg(linux_kernel)]
    pub const DDCMP: Protocol = Protocol(new_raw_protocol((c::ETH_P_DDCMP as u16).to_be() as u32));
    /// `ETH_P_WAN_PPP`
    #[cfg(linux_kernel)]
    pub const WAN_PPP: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_WAN_PPP as u16).to_be() as u32));
    /// `ETH_P_PPP_MP`
    #[cfg(linux_kernel)]
    pub const PPP_MP: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_PPP_MP as u16).to_be() as u32));
    /// `ETH_P_LOCALTALK`
    #[cfg(linux_kernel)]
    pub const LOCALTALK: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_LOCALTALK as u16).to_be() as u32));
    /// `ETH_P_CAN`
    #[cfg(linux_kernel)]
    pub const CAN: Protocol = Protocol(new_raw_protocol((c::ETH_P_CAN as u16).to_be() as u32));
    /// `ETH_P_CANFD`
    #[cfg(linux_kernel)]
    pub const CANFD: Protocol = Protocol(new_raw_protocol((c::ETH_P_CANFD as u16).to_be() as u32));
    /// `ETH_P_CANXL`
    #[cfg(linux_kernel)]
    pub const CANXL: Protocol = Protocol(new_raw_protocol((c::ETH_P_CANXL as u16).to_be() as u32));
    /// `ETH_P_PPPTALK`
    #[cfg(linux_kernel)]
    pub const PPPTALK: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_PPPTALK as u16).to_be() as u32));
    /// `ETH_P_TR_802_2`
    #[cfg(linux_kernel)]
    pub const TR_802_2: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_TR_802_2 as u16).to_be() as u32));
    /// `ETH_P_MOBITEX`
    #[cfg(linux_kernel)]
    pub const MOBITEX: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_MOBITEX as u16).to_be() as u32));
    /// `ETH_P_CONTROL`
    #[cfg(linux_kernel)]
    pub const CONTROL: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_CONTROL as u16).to_be() as u32));
    /// `ETH_P_IRDA`
    #[cfg(linux_kernel)]
    pub const IRDA: Protocol = Protocol(new_raw_protocol((c::ETH_P_IRDA as u16).to_be() as u32));
    /// `ETH_P_ECONET`
    #[cfg(linux_kernel)]
    pub const ECONET: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_ECONET as u16).to_be() as u32));
    /// `ETH_P_HDLC`
    #[cfg(linux_kernel)]
    pub const HDLC: Protocol = Protocol(new_raw_protocol((c::ETH_P_HDLC as u16).to_be() as u32));
    /// `ETH_P_ARCNET`
    #[cfg(linux_kernel)]
    pub const ARCNET: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_ARCNET as u16).to_be() as u32));
    /// `ETH_P_DSA`
    #[cfg(linux_kernel)]
    pub const DSA: Protocol = Protocol(new_raw_protocol((c::ETH_P_DSA as u16).to_be() as u32));
    /// `ETH_P_TRAILER`
    #[cfg(linux_kernel)]
    pub const TRAILER: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_TRAILER as u16).to_be() as u32));
    /// `ETH_P_PHONET`
    #[cfg(linux_kernel)]
    pub const PHONET: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_PHONET as u16).to_be() as u32));
    /// `ETH_P_IEEE802154`
    #[cfg(linux_kernel)]
    pub const IEEE802154: Protocol =
        Protocol(new_raw_protocol((c::ETH_P_IEEE802154 as u16).to_be() as u32));
    /// `ETH_P_CAIF`
    #[cfg(linux_kernel)]
    pub const CAIF: Protocol = Protocol(new_raw_protocol((c::ETH_P_CAIF as u16).to_be() as u32));
    /// `ETH_P_XDSA`
    #[cfg(linux_kernel)]
    pub const XDSA: Protocol = Protocol(new_raw_protocol((c::ETH_P_XDSA as u16).to_be() as u32));
    /// `ETH_P_MAP`
    #[cfg(linux_kernel)]
    pub const MAP: Protocol = Protocol(new_raw_protocol((c::ETH_P_MAP as u16).to_be() as u32));
    /// `ETH_P_MCTP`
    #[cfg(linux_kernel)]
    pub const MCTP: Protocol = Protocol(new_raw_protocol((c::ETH_P_MCTP as u16).to_be() as u32));
}

#[rustfmt::skip]
impl Protocol {
    /// Constructs a `Protocol` from a raw integer.
    #[inline]
    pub const fn from_raw(raw: RawProtocol) -> Self {
        Self(raw)
    }

    /// Returns the raw integer for this `Protocol`.
    #[inline]
    pub const fn as_raw(self) -> RawProtocol {
        self.0
    }
}

/// `SHUT_*` constants for use with [`shutdown`].
///
/// [`shutdown`]: crate::net::shutdown
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
#[repr(u32)]
pub enum Shutdown {
    /// `SHUT_RD`—Disable further read operations.
    Read = c::SHUT_RD as _,
    /// `SHUT_WR`—Disable further write operations.
    Write = c::SHUT_WR as _,
    /// `SHUT_RDWR`—Disable further read and write operations.
    ReadWrite = c::SHUT_RDWR as _,
}

bitflags! {
    /// `SOCK_*` constants for use with [`socket_with`], [`accept_with`] and
    /// [`acceptfrom_with`].
    ///
    /// [`socket_with`]: crate::net::socket_with
    /// [`accept_with`]: crate::net::accept_with
    /// [`acceptfrom_with`]: crate::net::acceptfrom_with
    #[repr(transparent)]
    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct SocketFlags: c::c_uint {
        /// `SOCK_NONBLOCK`
        #[cfg(not(any(
            apple,
            windows,
            target_os = "aix",
            target_os = "espidf",
            target_os = "haiku",
            target_os = "nto",
            target_os = "vita",
        )))]
        const NONBLOCK = bitcast!(c::SOCK_NONBLOCK);

        /// `SOCK_CLOEXEC`
        #[cfg(not(any(apple, windows, target_os = "aix", target_os = "haiku")))]
        const CLOEXEC = bitcast!(c::SOCK_CLOEXEC);

        // This deliberately lacks a `const _ = !0`, so that users can use
        // `from_bits_truncate` to extract the `SocketFlags` from a flags
        // value that also includes a `SocketType`.
    }
}

/// `AF_XDP` related types and constants.
#[cfg(target_os = "linux")]
pub mod xdp {
    use super::{bitflags, c};

    bitflags! {
        /// `XDP_OPTIONS_*` constants returned by [`get_xdp_options`].
        ///
        /// [`get_xdp_options`]: crate::net::sockopt::get_xdp_options
        #[repr(transparent)]
        #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
        pub struct XdpOptionsFlags: u32 {
            /// `XDP_OPTIONS_ZEROCOPY`
            const XDP_OPTIONS_ZEROCOPY = bitcast!(c::XDP_OPTIONS_ZEROCOPY);
        }
    }

    // Constant needs to be cast because bindgen does generate a u32 but the struct expects a u16.
    // https://github.com/torvalds/linux/blob/v6.6/include/uapi/linux/if_xdp.h#L15-L44
    bitflags! {
        /// `XDP_*` constants for use in [`SockaddrXdp`].
        #[repr(transparent)]
        #[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Debug)]
        pub struct SockaddrXdpFlags: u16 {
            /// `XDP_SHARED_UMEM`
            const XDP_SHARED_UMEM = bitcast!(c::XDP_SHARED_UMEM as u16);
            /// `XDP_COPY`
            const XDP_COPY = bitcast!(c::XDP_COPY  as u16);
            /// `XDP_COPY`
            const XDP_ZEROCOPY = bitcast!(c::XDP_ZEROCOPY as u16);
            /// `XDP_USE_NEED_WAKEUP`
            const XDP_USE_NEED_WAKEUP = bitcast!(c::XDP_USE_NEED_WAKEUP as u16);
            // requires kernel 6.6
            /// `XDP_USE_SG`
            const XDP_USE_SG = bitcast!(c::XDP_USE_SG as u16);
        }
    }

    bitflags! {
        /// `XDP_RING_*` constants for use in fill and/or Tx ring.
        #[repr(transparent)]
        #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
        pub struct XdpRingFlags: u32 {
            /// `XDP_RING_NEED_WAKEUP`
            const XDP_RING_NEED_WAKEUP = bitcast!(c::XDP_RING_NEED_WAKEUP);
        }
    }

    bitflags! {
        /// `XDP_UMEM_*` constants for use in [`XdpUmemReg`].
        #[repr(transparent)]
        #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
        pub struct XdpUmemRegFlags: u32 {
            /// `XDP_UMEM_UNALIGNED_CHUNK_FLAG`
            const XDP_UMEM_UNALIGNED_CHUNK_FLAG = bitcast!(c::XDP_UMEM_UNALIGNED_CHUNK_FLAG);
        }
    }

    /// A XDP socket address.
    ///
    /// Used to bind to XDP socket.
    ///
    /// Not ABI compatible with `struct sockaddr_xdp`
    // https://github.com/torvalds/linux/blob/v6.6/include/uapi/linux/if_xdp.h#L38-L44
    #[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Debug)]
    pub struct SocketAddrXdp {
        /// Flags.
        sxdp_flags: SockaddrXdpFlags,
        /// Interface index.
        sxdp_ifindex: u32,
        /// Queue ID.
        sxdp_queue_id: u32,
        /// Shared UMEM file descriptor.
        sxdp_shared_umem_fd: u32,
    }

    impl SocketAddrXdp {
        /// Construct a new XDP address.
        #[inline]
        pub fn new(
            flags: SockaddrXdpFlags,
            interface_index: u32,
            queue_id: u32,
            share_umem_fd: u32,
        ) -> Self {
            Self {
                sxdp_flags: flags,
                sxdp_ifindex: interface_index,
                sxdp_queue_id: queue_id,
                sxdp_shared_umem_fd: share_umem_fd,
            }
        }

        /// Return flags.
        #[inline]
        pub fn flags(&self) -> SockaddrXdpFlags {
            self.sxdp_flags
        }

        /// Set flags.
        #[inline]
        pub fn set_flags(&mut self, flags: SockaddrXdpFlags) {
            self.sxdp_flags = flags;
        }

        /// Return interface index.
        #[inline]
        pub fn interface_index(&self) -> u32 {
            self.sxdp_ifindex
        }

        /// Set interface index.
        #[inline]
        pub fn set_interface_index(&mut self, interface_index: u32) {
            self.sxdp_ifindex = interface_index;
        }

        /// Return queue ID.
        #[inline]
        pub fn queue_id(&self) -> u32 {
            self.sxdp_queue_id
        }

        /// Set queue ID.
        #[inline]
        pub fn set_queue_id(&mut self, queue_id: u32) {
            self.sxdp_queue_id = queue_id;
        }

        /// Return shared UMEM file descriptor.
        #[inline]
        pub fn shared_umem_fd(&self) -> u32 {
            self.sxdp_shared_umem_fd
        }

        /// Set shared UMEM file descriptor.
        #[inline]
        pub fn set_shared_umem_fd(&mut self, shared_umem_fd: u32) {
            self.sxdp_shared_umem_fd = shared_umem_fd;
        }
    }

    /// XDP ring offset.
    ///
    /// Used to mmap rings from kernel.
    ///
    /// Not ABI compatible with `struct xdp_ring_offset`.
    // https://github.com/torvalds/linux/blob/v6.6/include/uapi/linux/if_xdp.h#L49-L54
    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct XdpRingOffset {
        /// Producer offset.
        pub producer: u64,
        /// Consumer offset.
        pub consumer: u64,
        /// Descriptors offset.
        pub desc: u64,
        /// Flags offset.
        ///
        /// Is `None` if the kernel version (<5.4) does not yet support flags.
        pub flags: Option<u64>,
    }

    /// XDP mmap offsets.
    ///
    /// Not ABI compatible with `struct xdp_mmap_offsets`
    // https://github.com/torvalds/linux/blob/v6.6/include/uapi/linux/if_xdp.h#L56-L61
    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct XdpMmapOffsets {
        /// Rx ring offsets.
        pub rx: XdpRingOffset,
        /// Tx ring offsets.
        pub tx: XdpRingOffset,
        /// Fill ring offsets.
        pub fr: XdpRingOffset,
        /// Completion ring offsets.
        pub cr: XdpRingOffset,
    }

    /// XDP umem registration.
    ///
    /// `struct xdp_umem_reg`
    // https://github.com/torvalds/linux/blob/v6.6/include/uapi/linux/if_xdp.h#L73-L79
    #[repr(C)]
    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct XdpUmemReg {
        /// Start address of UMEM.
        pub addr: u64,
        /// Umem length in bytes.
        pub len: u64,
        /// Chunk size in bytes.
        pub chunk_size: u32,
        /// Headroom in bytes.
        pub headroom: u32,
        /// Flags.
        ///
        /// Requires kernel version 5.4.
        pub flags: XdpUmemRegFlags,
    }

    /// XDP statistics.
    ///
    /// Not ABI compatible with `struct xdp_statistics`
    // https://github.com/torvalds/linux/blob/v6.6/include/uapi/linux/if_xdp.h#L81-L88
    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct XdpStatistics {
        /// Rx dropped.
        pub rx_dropped: u64,
        /// Rx invalid descriptors.
        pub rx_invalid_descs: u64,
        /// Tx invalid descriptors.
        pub tx_invalid_descs: u64,
        /// Rx ring full.
        ///
        /// Is `None` if the kernel version (<5.9) does not yet support flags.
        pub rx_ring_full: Option<u64>,
        /// Rx fill ring empty descriptors.
        ///
        /// Is `None` if the kernel version (<5.9) does not yet support flags.
        pub rx_fill_ring_empty_descs: Option<u64>,
        /// Tx ring empty descriptors.
        ///
        /// Is `None` if the kernel version (<5.9) does not yet support flags.
        pub tx_ring_empty_descs: Option<u64>,
    }

    /// XDP options.
    ///
    /// Requires kernel version 5.3.
    /// `struct xdp_options`
    // https://github.com/torvalds/linux/blob/v6.6/include/uapi/linux/if_xdp.h#L90-L92
    #[repr(C)]
    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct XdpOptions {
        /// Flags.
        pub flags: XdpOptionsFlags,
    }

    /// XDP rx/tx frame descriptor.
    ///
    /// `struct xdp_desc`
    // https://github.com/torvalds/linux/blob/v6.6/include/uapi/linux/if_xdp.h#L109-L113
    #[repr(C)]
    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
    pub struct XdpDesc {
        /// Offset from the start of the UMEM.
        pub addr: u64,
        /// Length of packet in bytes.
        pub len: u32,
        /// Options.
        pub options: XdpDescOptions,
    }

    #[cfg(target_os = "linux")]
    bitflags! {
        #[repr(transparent)]
        #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
        /// `XDP_*` constants for use in [`XdpDesc`].
        ///
        /// Requires kernel version 6.6.
        pub struct XdpDescOptions: u32 {
            /// `XDP_PKT_CONTD`
            const XDP_PKT_CONTD = bitcast!(c::XDP_PKT_CONTD);
        }
    }

    /// Offset for mmapping rx ring.
    pub const XDP_PGOFF_RX_RING: u64 = c::XDP_PGOFF_RX_RING as u64;
    /// Offset for mmapping tx ring.
    pub const XDP_PGOFF_TX_RING: u64 = c::XDP_PGOFF_TX_RING as u64;
    /// Offset for mmapping fill ring.
    pub const XDP_UMEM_PGOFF_FILL_RING: u64 = c::XDP_UMEM_PGOFF_FILL_RING;
    /// Offset for mmapping completion ring.
    pub const XDP_UMEM_PGOFF_COMPLETION_RING: u64 = c::XDP_UMEM_PGOFF_COMPLETION_RING;

    /// Offset used to shift the [`XdpDesc`] addr to the right to extract the address offset in
    /// unaligned mode.
    pub const XSK_UNALIGNED_BUF_OFFSET_SHIFT: u64 = c::XSK_UNALIGNED_BUF_OFFSET_SHIFT as u64;
    /// Mask used to binary `and` the [`XdpDesc`] addr to extract the address without the offset
    /// carried in the upper 16 bits of the address in unaligned mode.
    pub const XSK_UNALIGNED_BUF_ADDR_MASK: u64 = c::XSK_UNALIGNED_BUF_ADDR_MASK;
}

/// UNIX credentials of socket peer, for use with [`get_socket_peercred`]
/// [`SendAncillaryMessage::ScmCredentials`] and
/// [`RecvAncillaryMessage::ScmCredentials`].
///
/// [`get_socket_peercred`]: crate::net::sockopt::get_socket_peercred
/// [`SendAncillaryMessage::ScmCredentials`]: crate::net::SendAncillaryMessage::ScmCredentials
/// [`RecvAncillaryMessage::ScmCredentials`]: crate::net::RecvAncillaryMessage::ScmCredentials
#[cfg(linux_kernel)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
#[repr(C)]
pub struct UCred {
    /// Process ID of peer
    pub pid: crate::pid::Pid,
    /// User ID of peer
    pub uid: crate::ugid::Uid,
    /// Group ID of peer
    pub gid: crate::ugid::Gid,
}

#[test]
fn test_sizes() {
    use crate::backend::c;
    use c::c_int;
    use core::mem::transmute;

    // Backend code needs to cast these to `c_int` so make sure that cast isn't
    // lossy.
    assert_eq_size!(RawProtocol, c_int);
    assert_eq_size!(Protocol, c_int);
    assert_eq_size!(Option<RawProtocol>, c_int);
    assert_eq_size!(Option<Protocol>, c_int);
    assert_eq_size!(RawSocketType, c_int);
    assert_eq_size!(SocketType, c_int);
    assert_eq_size!(SocketFlags, c_int);

    // Rustix doesn't depend on `Option<Protocol>` matching the ABI of a raw
    // integer for correctness, but it should work nonetheless.
    #[allow(unsafe_code)]
    unsafe {
        let t: Option<Protocol> = None;
        assert_eq!(0_u32, transmute::<Option<Protocol>, u32>(t));

        let t: Option<Protocol> = Some(Protocol::from_raw(RawProtocol::new(4567).unwrap()));
        assert_eq!(4567_u32, transmute::<Option<Protocol>, u32>(t));
    }

    #[cfg(linux_kernel)]
    assert_eq_size!(UCred, libc::ucred);

    #[cfg(target_os = "linux")]
    assert_eq_size!(super::xdp::XdpUmemReg, c::xdp_umem_reg);
    #[cfg(target_os = "linux")]
    assert_eq_size!(super::xdp::XdpOptions, c::xdp_options);
    #[cfg(target_os = "linux")]
    assert_eq_size!(super::xdp::XdpDesc, c::xdp_desc);
}