]> git.proxmox.com Git - rustc.git/blob - vendor/rustix/src/imp/libc/net/ext.rs
New upstream version 1.66.0+dfsg1
[rustc.git] / vendor / rustix / src / imp / libc / net / ext.rs
1 use super::super::c;
2
3 /// The windows `sockaddr_in6` type is a union with accessor functions which
4 /// are not `const fn`. Define our own layout-compatible version so that we
5 /// can transmute in and out of it.
6 #[cfg(windows)]
7 #[repr(C)]
8 struct sockaddr_in6 {
9 sin6_family: u16,
10 sin6_port: u16,
11 sin6_flowinfo: u32,
12 sin6_addr: c::in6_addr,
13 sin6_scope_id: u32,
14 }
15
16 #[cfg(not(windows))]
17 #[inline]
18 pub(crate) const fn in_addr_s_addr(addr: c::in_addr) -> u32 {
19 addr.s_addr
20 }
21
22 #[cfg(not(feature = "std"))]
23 #[cfg(windows)]
24 #[inline]
25 pub(crate) const fn in_addr_s_addr(addr: c::in_addr) -> u32 {
26 // This should be `*addr.S_un.S_addr()`, except that isn't a `const fn`.
27 unsafe { core::mem::transmute(addr) }
28 }
29
30 // TODO: With Rust 1.55, we can use the above `in_addr_s_addr` definition that
31 // uses a const-fn transmute.
32 #[cfg(feature = "std")]
33 #[cfg(windows)]
34 #[inline]
35 pub(crate) fn in_addr_s_addr(addr: c::in_addr) -> u32 {
36 // This should be `*addr.S_un.S_addr()`, except that isn't a `const fn`.
37 unsafe { core::mem::transmute(addr) }
38 }
39
40 #[cfg(not(windows))]
41 #[inline]
42 pub(crate) const fn in_addr_new(s_addr: u32) -> c::in_addr {
43 c::in_addr { s_addr }
44 }
45
46 #[cfg(not(feature = "std"))]
47 #[cfg(windows)]
48 #[inline]
49 pub(crate) const fn in_addr_new(s_addr: u32) -> c::in_addr {
50 unsafe { core::mem::transmute(s_addr) }
51 }
52
53 // TODO: With Rust 1.55, we can use the above `in_addr_new` definition that
54 // uses a const-fn transmute.
55 #[cfg(feature = "std")]
56 #[cfg(windows)]
57 #[inline]
58 pub(crate) fn in_addr_new(s_addr: u32) -> c::in_addr {
59 unsafe { core::mem::transmute(s_addr) }
60 }
61
62 #[cfg(not(windows))]
63 #[inline]
64 pub(crate) const fn in6_addr_s6_addr(addr: c::in6_addr) -> [u8; 16] {
65 addr.s6_addr
66 }
67
68 #[cfg(not(feature = "std"))]
69 #[cfg(windows)]
70 #[inline]
71 pub(crate) const fn in6_addr_s6_addr(addr: c::in6_addr) -> [u8; 16] {
72 unsafe { core::mem::transmute(addr) }
73 }
74
75 // TODO: With Rust 1.55, we can use the above `in6_addr_s6_addr` definition
76 // that uses a const-fn transmute.
77 #[cfg(feature = "std")]
78 #[cfg(windows)]
79 #[inline]
80 pub(crate) fn in6_addr_s6_addr(addr: c::in6_addr) -> [u8; 16] {
81 unsafe { core::mem::transmute(addr) }
82 }
83
84 #[cfg(not(windows))]
85 #[inline]
86 pub(crate) const fn in6_addr_new(s6_addr: [u8; 16]) -> c::in6_addr {
87 c::in6_addr { s6_addr }
88 }
89
90 #[cfg(not(feature = "std"))]
91 #[cfg(windows)]
92 #[inline]
93 pub(crate) const fn in6_addr_new(s6_addr: [u8; 16]) -> c::in6_addr {
94 unsafe { core::mem::transmute(s6_addr) }
95 }
96
97 // TODO: With Rust 1.55, we can use the above `in6_addr_new` definition that
98 // uses a const-fn transmute.
99 #[cfg(feature = "std")]
100 #[cfg(windows)]
101 #[inline]
102 pub(crate) fn in6_addr_new(s6_addr: [u8; 16]) -> c::in6_addr {
103 unsafe { core::mem::transmute(s6_addr) }
104 }
105
106 #[cfg(not(windows))]
107 #[inline]
108 pub(crate) const fn sockaddr_in6_sin6_scope_id(addr: c::sockaddr_in6) -> u32 {
109 addr.sin6_scope_id
110 }
111
112 #[cfg(not(feature = "std"))]
113 #[cfg(windows)]
114 #[inline]
115 pub(crate) const fn sockaddr_in6_sin6_scope_id(addr: c::sockaddr_in6) -> u32 {
116 let addr: sockaddr_in6 = unsafe { core::mem::transmute(addr) };
117 addr.sin6_scope_id
118 }
119
120 // TODO: With Rust 1.55, we can use the above `sockaddr_in6_sin6_scope_id`
121 // definition that uses a const-fn transmute.
122 #[cfg(feature = "std")]
123 #[cfg(windows)]
124 #[inline]
125 pub(crate) fn sockaddr_in6_sin6_scope_id(addr: c::sockaddr_in6) -> u32 {
126 let addr: sockaddr_in6 = unsafe { core::mem::transmute(addr) };
127 addr.sin6_scope_id
128 }
129
130 #[cfg(not(feature = "std"))]
131 #[cfg(not(windows))]
132 #[inline]
133 pub(crate) fn sockaddr_in6_sin6_scope_id_mut(addr: &mut c::sockaddr_in6) -> &mut u32 {
134 &mut addr.sin6_scope_id
135 }
136
137 #[cfg(not(feature = "std"))]
138 #[cfg(windows)]
139 #[inline]
140 pub(crate) fn sockaddr_in6_sin6_scope_id_mut(addr: &mut c::sockaddr_in6) -> &mut u32 {
141 let addr: &mut sockaddr_in6 = unsafe { core::mem::transmute(addr) };
142 &mut addr.sin6_scope_id
143 }
144
145 #[cfg(not(windows))]
146 #[inline]
147 pub(crate) const fn sockaddr_in6_new(
148 #[cfg(any(
149 target_os = "dragonfly",
150 target_os = "freebsd",
151 target_os = "ios",
152 target_os = "macos",
153 target_os = "netbsd",
154 target_os = "openbsd",
155 ))]
156 sin6_len: u8,
157 sin6_family: c::sa_family_t,
158 sin6_port: u16,
159 sin6_flowinfo: u32,
160 sin6_addr: c::in6_addr,
161 sin6_scope_id: u32,
162 ) -> c::sockaddr_in6 {
163 c::sockaddr_in6 {
164 #[cfg(any(
165 target_os = "dragonfly",
166 target_os = "freebsd",
167 target_os = "ios",
168 target_os = "macos",
169 target_os = "netbsd",
170 target_os = "openbsd",
171 ))]
172 sin6_len,
173 sin6_family,
174 sin6_port,
175 sin6_flowinfo,
176 sin6_addr,
177 sin6_scope_id,
178 #[cfg(target_os = "illumos")]
179 __sin6_src_id: 0,
180 }
181 }
182
183 #[cfg(not(feature = "std"))]
184 #[cfg(windows)]
185 #[inline]
186 pub(crate) const fn sockaddr_in6_new(
187 sin6_family: u16,
188 sin6_port: u16,
189 sin6_flowinfo: u32,
190 sin6_addr: c::in6_addr,
191 sin6_scope_id: u32,
192 ) -> c::sockaddr_in6 {
193 let addr = sockaddr_in6 {
194 sin6_family,
195 sin6_port,
196 sin6_flowinfo,
197 sin6_addr,
198 sin6_scope_id,
199 };
200 unsafe { core::mem::transmute(addr) }
201 }
202
203 // TODO: With Rust 1.55, we can use the above `sockaddr_in6_new` definition
204 // that uses a const-fn transmute.
205 #[cfg(feature = "std")]
206 #[cfg(windows)]
207 #[inline]
208 pub(crate) fn sockaddr_in6_new(
209 sin6_family: u16,
210 sin6_port: u16,
211 sin6_flowinfo: u32,
212 sin6_addr: c::in6_addr,
213 sin6_scope_id: u32,
214 ) -> c::sockaddr_in6 {
215 let addr = sockaddr_in6 {
216 sin6_family,
217 sin6_port,
218 sin6_flowinfo,
219 sin6_addr,
220 sin6_scope_id,
221 };
222 unsafe { core::mem::transmute(addr) }
223 }