linux_api/
socket.rs

1use num_enum::{IntoPrimitive, TryFromPrimitive};
2
3use crate::{bindings, const_conversions};
4
5#[allow(non_camel_case_types)]
6pub type sa_family_t = bindings::linux___kernel_sa_family_t;
7
8#[derive(Copy, Clone, Eq, PartialEq)]
9pub struct AddressFamily(sa_family_t);
10
11#[allow(non_upper_case_globals)]
12impl AddressFamily {
13    pub const AF_UNSPEC: Self = Self::from_u32(bindings::LINUX_AF_UNSPEC);
14    pub const AF_UNIX: Self = Self::from_u32(bindings::LINUX_AF_UNIX);
15    pub const AF_LOCAL: Self = Self::from_u32(bindings::LINUX_AF_LOCAL);
16    pub const AF_INET: Self = Self::from_u32(bindings::LINUX_AF_INET);
17    pub const AF_AX25: Self = Self::from_u32(bindings::LINUX_AF_AX25);
18    pub const AF_IPX: Self = Self::from_u32(bindings::LINUX_AF_IPX);
19    pub const AF_APPLETALK: Self = Self::from_u32(bindings::LINUX_AF_APPLETALK);
20    pub const AF_NETROM: Self = Self::from_u32(bindings::LINUX_AF_NETROM);
21    pub const AF_BRIDGE: Self = Self::from_u32(bindings::LINUX_AF_BRIDGE);
22    pub const AF_ATMPVC: Self = Self::from_u32(bindings::LINUX_AF_ATMPVC);
23    pub const AF_X25: Self = Self::from_u32(bindings::LINUX_AF_X25);
24    pub const AF_INET6: Self = Self::from_u32(bindings::LINUX_AF_INET6);
25    pub const AF_ROSE: Self = Self::from_u32(bindings::LINUX_AF_ROSE);
26    pub const AF_DECnet: Self = Self::from_u32(bindings::LINUX_AF_DECnet);
27    pub const AF_NETBEUI: Self = Self::from_u32(bindings::LINUX_AF_NETBEUI);
28    pub const AF_SECURITY: Self = Self::from_u32(bindings::LINUX_AF_SECURITY);
29    pub const AF_KEY: Self = Self::from_u32(bindings::LINUX_AF_KEY);
30    pub const AF_NETLINK: Self = Self::from_u32(bindings::LINUX_AF_NETLINK);
31    pub const AF_ROUTE: Self = Self::from_u32(bindings::LINUX_AF_ROUTE);
32    pub const AF_PACKET: Self = Self::from_u32(bindings::LINUX_AF_PACKET);
33    pub const AF_ASH: Self = Self::from_u32(bindings::LINUX_AF_ASH);
34    pub const AF_ECONET: Self = Self::from_u32(bindings::LINUX_AF_ECONET);
35    pub const AF_ATMSVC: Self = Self::from_u32(bindings::LINUX_AF_ATMSVC);
36    pub const AF_RDS: Self = Self::from_u32(bindings::LINUX_AF_RDS);
37    pub const AF_SNA: Self = Self::from_u32(bindings::LINUX_AF_SNA);
38    pub const AF_IRDA: Self = Self::from_u32(bindings::LINUX_AF_IRDA);
39    pub const AF_PPPOX: Self = Self::from_u32(bindings::LINUX_AF_PPPOX);
40    pub const AF_WANPIPE: Self = Self::from_u32(bindings::LINUX_AF_WANPIPE);
41    pub const AF_LLC: Self = Self::from_u32(bindings::LINUX_AF_LLC);
42    pub const AF_IB: Self = Self::from_u32(bindings::LINUX_AF_IB);
43    pub const AF_MPLS: Self = Self::from_u32(bindings::LINUX_AF_MPLS);
44    pub const AF_CAN: Self = Self::from_u32(bindings::LINUX_AF_CAN);
45    pub const AF_TIPC: Self = Self::from_u32(bindings::LINUX_AF_TIPC);
46    pub const AF_BLUETOOTH: Self = Self::from_u32(bindings::LINUX_AF_BLUETOOTH);
47    pub const AF_IUCV: Self = Self::from_u32(bindings::LINUX_AF_IUCV);
48    pub const AF_RXRPC: Self = Self::from_u32(bindings::LINUX_AF_RXRPC);
49    pub const AF_ISDN: Self = Self::from_u32(bindings::LINUX_AF_ISDN);
50    pub const AF_PHONET: Self = Self::from_u32(bindings::LINUX_AF_PHONET);
51    pub const AF_IEEE802154: Self = Self::from_u32(bindings::LINUX_AF_IEEE802154);
52    pub const AF_CAIF: Self = Self::from_u32(bindings::LINUX_AF_CAIF);
53    pub const AF_ALG: Self = Self::from_u32(bindings::LINUX_AF_ALG);
54    pub const AF_NFC: Self = Self::from_u32(bindings::LINUX_AF_NFC);
55    pub const AF_VSOCK: Self = Self::from_u32(bindings::LINUX_AF_VSOCK);
56    pub const AF_KCM: Self = Self::from_u32(bindings::LINUX_AF_KCM);
57    pub const AF_QIPCRTR: Self = Self::from_u32(bindings::LINUX_AF_QIPCRTR);
58    pub const AF_SMC: Self = Self::from_u32(bindings::LINUX_AF_SMC);
59    pub const AF_XDP: Self = Self::from_u32(bindings::LINUX_AF_XDP);
60    pub const AF_MCTP: Self = Self::from_u32(bindings::LINUX_AF_MCTP);
61    // add new entries to `to_str` below
62
63    #[inline]
64    pub const fn new(val: sa_family_t) -> Self {
65        Self(val)
66    }
67
68    #[inline]
69    pub const fn val(&self) -> sa_family_t {
70        self.0
71    }
72
73    const fn from_u32(val: u32) -> Self {
74        Self::new(const_conversions::u16_from_u32(val))
75    }
76
77    pub const fn to_str(&self) -> Option<&'static str> {
78        match *self {
79            Self::AF_UNSPEC => Some("AF_UNSPEC"),
80            Self::AF_UNIX => Some("AF_UNIX"),
81            // Self::AF_LOCAL == Self::AF_UNIX
82            Self::AF_INET => Some("AF_INET"),
83            Self::AF_AX25 => Some("AF_AX25"),
84            Self::AF_IPX => Some("AF_IPX"),
85            Self::AF_APPLETALK => Some("AF_APPLETALK"),
86            Self::AF_NETROM => Some("AF_NETROM"),
87            Self::AF_BRIDGE => Some("AF_BRIDGE"),
88            Self::AF_ATMPVC => Some("AF_ATMPVC"),
89            Self::AF_X25 => Some("AF_X25"),
90            Self::AF_INET6 => Some("AF_INET6"),
91            Self::AF_ROSE => Some("AF_ROSE"),
92            Self::AF_DECnet => Some("AF_DECnet"),
93            Self::AF_NETBEUI => Some("AF_NETBEUI"),
94            Self::AF_SECURITY => Some("AF_SECURITY"),
95            Self::AF_KEY => Some("AF_KEY"),
96            Self::AF_NETLINK => Some("AF_NETLINK"),
97            // Self::AF_ROUTE == Self::AF_NETLINK
98            Self::AF_PACKET => Some("AF_PACKET"),
99            Self::AF_ASH => Some("AF_ASH"),
100            Self::AF_ECONET => Some("AF_ECONET"),
101            Self::AF_ATMSVC => Some("AF_ATMSVC"),
102            Self::AF_RDS => Some("AF_RDS"),
103            Self::AF_SNA => Some("AF_SNA"),
104            Self::AF_IRDA => Some("AF_IRDA"),
105            Self::AF_PPPOX => Some("AF_PPPOX"),
106            Self::AF_WANPIPE => Some("AF_WANPIPE"),
107            Self::AF_LLC => Some("AF_LLC"),
108            Self::AF_IB => Some("AF_IB"),
109            Self::AF_MPLS => Some("AF_MPLS"),
110            Self::AF_CAN => Some("AF_CAN"),
111            Self::AF_TIPC => Some("AF_TIPC"),
112            Self::AF_BLUETOOTH => Some("AF_BLUETOOTH"),
113            Self::AF_IUCV => Some("AF_IUCV"),
114            Self::AF_RXRPC => Some("AF_RXRPC"),
115            Self::AF_ISDN => Some("AF_ISDN"),
116            Self::AF_PHONET => Some("AF_PHONET"),
117            Self::AF_IEEE802154 => Some("AF_IEEE802154"),
118            Self::AF_CAIF => Some("AF_CAIF"),
119            Self::AF_ALG => Some("AF_ALG"),
120            Self::AF_NFC => Some("AF_NFC"),
121            Self::AF_VSOCK => Some("AF_VSOCK"),
122            Self::AF_KCM => Some("AF_KCM"),
123            Self::AF_QIPCRTR => Some("AF_QIPCRTR"),
124            Self::AF_SMC => Some("AF_SMC"),
125            Self::AF_XDP => Some("AF_XDP"),
126            Self::AF_MCTP => Some("AF_MCTP"),
127            _ => None,
128        }
129    }
130}
131
132impl core::fmt::Display for AddressFamily {
133    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
134        match self.to_str() {
135            Some(s) => formatter.write_str(s),
136            None => write!(formatter, "(unknown socket family {})", self.0),
137        }
138    }
139}
140
141impl core::fmt::Debug for AddressFamily {
142    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
143        match self.to_str() {
144            Some(s) => write!(formatter, "AddressFamily::{s}"),
145            None => write!(formatter, "AddressFamily::<{}>", self.0),
146        }
147    }
148}
149
150impl From<AddressFamily> for sa_family_t {
151    #[inline]
152    fn from(val: AddressFamily) -> Self {
153        val.val()
154    }
155}
156
157impl From<sa_family_t> for AddressFamily {
158    #[inline]
159    fn from(val: sa_family_t) -> Self {
160        Self::new(val)
161    }
162}
163
164// it's very unlikely that another shutdown option will be added to linux, so it's fine to use an
165// enum here
166#[derive(Debug, Copy, Clone, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
167#[repr(u32)]
168#[allow(non_camel_case_types)]
169pub enum Shutdown {
170    SHUT_RD = bindings::LINUX_sock_shutdown_cmd_SHUT_RD,
171    SHUT_WR = bindings::LINUX_sock_shutdown_cmd_SHUT_WR,
172    SHUT_RDWR = bindings::LINUX_sock_shutdown_cmd_SHUT_RDWR,
173}