/rust/registry/src/index.crates.io-1949cf8c6b5b557f/socket2-0.6.3/src/sys/unix.rs
Line | Count | Source |
1 | | // Copyright 2015 The Rust Project Developers. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
4 | | // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
5 | | // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your |
6 | | // option. This file may not be copied, modified, or distributed |
7 | | // except according to those terms. |
8 | | |
9 | | use std::cmp::min; |
10 | | #[cfg(not(target_os = "wasi"))] |
11 | | use std::ffi::OsStr; |
12 | | #[cfg(not(any(target_os = "redox", target_os = "wasi")))] |
13 | | use std::io::IoSlice; |
14 | | use std::marker::PhantomData; |
15 | | use std::mem::{self, size_of, MaybeUninit}; |
16 | | use std::net::Shutdown; |
17 | | use std::net::{Ipv4Addr, Ipv6Addr}; |
18 | | #[cfg(all( |
19 | | feature = "all", |
20 | | any( |
21 | | target_os = "ios", |
22 | | target_os = "visionos", |
23 | | target_os = "macos", |
24 | | target_os = "tvos", |
25 | | target_os = "watchos", |
26 | | target_os = "illumos", |
27 | | target_os = "solaris", |
28 | | target_os = "linux", |
29 | | target_os = "android", |
30 | | ) |
31 | | ))] |
32 | | use std::num::NonZeroU32; |
33 | | #[cfg(all( |
34 | | feature = "all", |
35 | | any( |
36 | | target_os = "aix", |
37 | | target_os = "android", |
38 | | target_os = "freebsd", |
39 | | target_os = "ios", |
40 | | target_os = "visionos", |
41 | | target_os = "linux", |
42 | | target_os = "macos", |
43 | | target_os = "tvos", |
44 | | target_os = "watchos", |
45 | | ) |
46 | | ))] |
47 | | use std::num::NonZeroUsize; |
48 | | use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd}; |
49 | | #[cfg(not(target_os = "wasi"))] |
50 | | use std::os::unix::ffi::OsStrExt; |
51 | | #[cfg(all(feature = "all", unix))] |
52 | | use std::os::unix::net::{UnixDatagram, UnixListener, UnixStream}; |
53 | | #[cfg(not(target_os = "wasi"))] |
54 | | use std::path::Path; |
55 | | use std::ptr; |
56 | | use std::time::{Duration, Instant}; |
57 | | use std::{io, slice}; |
58 | | |
59 | | #[cfg(not(any( |
60 | | target_os = "ios", |
61 | | target_os = "visionos", |
62 | | target_os = "macos", |
63 | | target_os = "tvos", |
64 | | target_os = "watchos", |
65 | | target_os = "cygwin", |
66 | | )))] |
67 | | use libc::ssize_t; |
68 | | use libc::{in6_addr, in_addr}; |
69 | | |
70 | | #[cfg(not(target_os = "wasi"))] |
71 | | use crate::SockAddrStorage; |
72 | | use crate::{Domain, Protocol, SockAddr, TcpKeepalive, Type}; |
73 | | #[cfg(not(any(target_os = "redox", target_os = "wasi")))] |
74 | | use crate::{MsgHdr, MsgHdrMut, RecvFlags}; |
75 | | |
76 | | pub(crate) use std::ffi::c_int; |
77 | | |
78 | | // Used in `Domain`. |
79 | | #[cfg(not(target_os = "wasi"))] |
80 | | pub(crate) use libc::AF_UNIX; |
81 | | pub(crate) use libc::{AF_INET, AF_INET6}; |
82 | | // Used in `Type`. |
83 | | #[cfg(all(feature = "all", target_os = "linux"))] |
84 | | pub(crate) use libc::SOCK_DCCP; |
85 | | #[cfg(all( |
86 | | feature = "all", |
87 | | not(any(target_os = "redox", target_os = "espidf", target_os = "wasi")) |
88 | | ))] |
89 | | pub(crate) use libc::SOCK_RAW; |
90 | | #[cfg(all(feature = "all", not(any(target_os = "espidf", target_os = "wasi"))))] |
91 | | pub(crate) use libc::SOCK_SEQPACKET; |
92 | | pub(crate) use libc::{SOCK_DGRAM, SOCK_STREAM}; |
93 | | // Used in `Protocol`. |
94 | | #[cfg(all(feature = "all", target_os = "linux"))] |
95 | | pub(crate) use libc::IPPROTO_DCCP; |
96 | | #[cfg(target_os = "linux")] |
97 | | pub(crate) use libc::IPPROTO_MPTCP; |
98 | | #[cfg(all(feature = "all", any(target_os = "freebsd", target_os = "linux")))] |
99 | | pub(crate) use libc::IPPROTO_SCTP; |
100 | | #[cfg(all( |
101 | | feature = "all", |
102 | | any( |
103 | | target_os = "android", |
104 | | target_os = "freebsd", |
105 | | target_os = "fuchsia", |
106 | | target_os = "linux", |
107 | | ) |
108 | | ))] |
109 | | pub(crate) use libc::IPPROTO_UDPLITE; |
110 | | #[cfg(not(target_os = "wasi"))] |
111 | | pub(crate) use libc::{IPPROTO_ICMP, IPPROTO_ICMPV6}; |
112 | | pub(crate) use libc::{IPPROTO_TCP, IPPROTO_UDP}; |
113 | | // Used in `SockAddr`. |
114 | | #[cfg(all(feature = "all", any(target_os = "freebsd", target_os = "openbsd")))] |
115 | | pub(crate) use libc::IPPROTO_DIVERT; |
116 | | pub(crate) use libc::{ |
117 | | sa_family_t, sockaddr, sockaddr_in, sockaddr_in6, sockaddr_storage, socklen_t, |
118 | | }; |
119 | | // Used in `RecvFlags`. |
120 | | #[cfg(not(any(target_os = "redox", target_os = "espidf", target_os = "wasi")))] |
121 | | pub(crate) use libc::MSG_TRUNC; |
122 | | #[cfg(not(any(target_os = "redox", target_os = "wasi")))] |
123 | | pub(crate) use libc::SO_OOBINLINE; |
124 | | // Used in `Socket`. |
125 | | #[cfg(not(target_os = "nto"))] |
126 | | pub(crate) use libc::ipv6_mreq as Ipv6Mreq; |
127 | | #[cfg(all(feature = "all", target_os = "linux"))] |
128 | | pub(crate) use libc::IPV6_HDRINCL; |
129 | | #[cfg(all( |
130 | | feature = "all", |
131 | | not(any( |
132 | | target_os = "dragonfly", |
133 | | target_os = "fuchsia", |
134 | | target_os = "hurd", |
135 | | target_os = "illumos", |
136 | | target_os = "netbsd", |
137 | | target_os = "openbsd", |
138 | | target_os = "redox", |
139 | | target_os = "solaris", |
140 | | target_os = "haiku", |
141 | | target_os = "espidf", |
142 | | target_os = "vita", |
143 | | target_os = "wasi", |
144 | | target_os = "cygwin", |
145 | | )) |
146 | | ))] |
147 | | pub(crate) use libc::IPV6_RECVHOPLIMIT; |
148 | | #[cfg(not(any( |
149 | | target_os = "dragonfly", |
150 | | target_os = "fuchsia", |
151 | | target_os = "hurd", |
152 | | target_os = "illumos", |
153 | | target_os = "netbsd", |
154 | | target_os = "openbsd", |
155 | | target_os = "redox", |
156 | | target_os = "solaris", |
157 | | target_os = "haiku", |
158 | | target_os = "espidf", |
159 | | target_os = "vita", |
160 | | target_os = "wasi", |
161 | | )))] |
162 | | pub(crate) use libc::IPV6_RECVTCLASS; |
163 | | #[cfg(all( |
164 | | feature = "all", |
165 | | not(any(target_os = "redox", target_os = "espidf", target_os = "wasi")) |
166 | | ))] |
167 | | pub(crate) use libc::IP_HDRINCL; |
168 | | #[cfg(not(any( |
169 | | target_os = "aix", |
170 | | target_os = "dragonfly", |
171 | | target_os = "fuchsia", |
172 | | target_os = "illumos", |
173 | | target_os = "netbsd", |
174 | | target_os = "openbsd", |
175 | | target_os = "redox", |
176 | | target_os = "solaris", |
177 | | target_os = "haiku", |
178 | | target_os = "hurd", |
179 | | target_os = "nto", |
180 | | target_os = "espidf", |
181 | | target_os = "vita", |
182 | | target_os = "wasi", |
183 | | target_os = "cygwin", |
184 | | )))] |
185 | | pub(crate) use libc::IP_RECVTOS; |
186 | | #[cfg(not(any( |
187 | | target_os = "fuchsia", |
188 | | target_os = "redox", |
189 | | target_os = "solaris", |
190 | | target_os = "haiku", |
191 | | target_os = "illumos", |
192 | | target_os = "wasi", |
193 | | )))] |
194 | | pub(crate) use libc::IP_TOS; |
195 | | #[cfg(not(any( |
196 | | target_os = "ios", |
197 | | target_os = "visionos", |
198 | | target_os = "macos", |
199 | | target_os = "tvos", |
200 | | target_os = "watchos", |
201 | | )))] |
202 | | pub(crate) use libc::SO_LINGER; |
203 | | #[cfg(any( |
204 | | target_os = "ios", |
205 | | target_os = "visionos", |
206 | | target_os = "macos", |
207 | | target_os = "tvos", |
208 | | target_os = "watchos", |
209 | | ))] |
210 | | pub(crate) use libc::SO_LINGER_SEC as SO_LINGER; |
211 | | #[cfg(any(target_os = "linux", target_os = "cygwin"))] |
212 | | pub(crate) use libc::SO_PASSCRED; |
213 | | #[cfg(all( |
214 | | feature = "all", |
215 | | any(target_os = "linux", target_os = "android", target_os = "fuchsia") |
216 | | ))] |
217 | | pub(crate) use libc::SO_PRIORITY; |
218 | | pub(crate) use libc::{ |
219 | | ip_mreq as IpMreq, linger, IPPROTO_IP, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, IPV6_UNICAST_HOPS, |
220 | | IPV6_V6ONLY, IP_ADD_MEMBERSHIP, IP_DROP_MEMBERSHIP, IP_MULTICAST_LOOP, IP_MULTICAST_TTL, |
221 | | IP_TTL, MSG_PEEK, SOL_SOCKET, SO_BROADCAST, SO_ERROR, SO_KEEPALIVE, SO_RCVBUF, SO_RCVTIMEO, |
222 | | SO_REUSEADDR, SO_SNDBUF, SO_SNDTIMEO, SO_TYPE, TCP_NODELAY, |
223 | | }; |
224 | | #[cfg(not(any( |
225 | | target_os = "dragonfly", |
226 | | target_os = "haiku", |
227 | | target_os = "hurd", |
228 | | target_os = "netbsd", |
229 | | target_os = "openbsd", |
230 | | target_os = "redox", |
231 | | target_os = "fuchsia", |
232 | | target_os = "nto", |
233 | | target_os = "espidf", |
234 | | target_os = "vita", |
235 | | target_os = "wasi", |
236 | | )))] |
237 | | pub(crate) use libc::{ |
238 | | ip_mreq_source as IpMreqSource, IP_ADD_SOURCE_MEMBERSHIP, IP_DROP_SOURCE_MEMBERSHIP, |
239 | | }; |
240 | | #[cfg(not(any( |
241 | | target_os = "dragonfly", |
242 | | target_os = "freebsd", |
243 | | target_os = "haiku", |
244 | | target_os = "illumos", |
245 | | target_os = "ios", |
246 | | target_os = "visionos", |
247 | | target_os = "macos", |
248 | | target_os = "netbsd", |
249 | | target_os = "nto", |
250 | | target_os = "openbsd", |
251 | | target_os = "solaris", |
252 | | target_os = "tvos", |
253 | | target_os = "watchos", |
254 | | target_os = "wasi", |
255 | | )))] |
256 | | pub(crate) use libc::{IPV6_ADD_MEMBERSHIP, IPV6_DROP_MEMBERSHIP}; |
257 | | #[cfg(any( |
258 | | target_os = "dragonfly", |
259 | | target_os = "freebsd", |
260 | | target_os = "haiku", |
261 | | target_os = "illumos", |
262 | | target_os = "ios", |
263 | | target_os = "visionos", |
264 | | target_os = "macos", |
265 | | target_os = "netbsd", |
266 | | target_os = "openbsd", |
267 | | target_os = "solaris", |
268 | | target_os = "tvos", |
269 | | target_os = "watchos", |
270 | | all(target_os = "wasi", not(target_env = "p1")), |
271 | | ))] |
272 | | pub(crate) use libc::{ |
273 | | IPV6_JOIN_GROUP as IPV6_ADD_MEMBERSHIP, IPV6_LEAVE_GROUP as IPV6_DROP_MEMBERSHIP, |
274 | | }; |
275 | | #[cfg(not(target_os = "wasi"))] |
276 | | pub(crate) use libc::{IPV6_MULTICAST_HOPS, IPV6_MULTICAST_IF, IP_MULTICAST_IF, MSG_OOB}; |
277 | | #[cfg(all( |
278 | | feature = "all", |
279 | | any( |
280 | | target_os = "android", |
281 | | target_os = "dragonfly", |
282 | | target_os = "freebsd", |
283 | | target_os = "fuchsia", |
284 | | target_os = "illumos", |
285 | | target_os = "ios", |
286 | | target_os = "visionos", |
287 | | target_os = "linux", |
288 | | target_os = "macos", |
289 | | target_os = "netbsd", |
290 | | target_os = "tvos", |
291 | | target_os = "watchos", |
292 | | target_os = "cygwin", |
293 | | all(target_os = "wasi", not(target_env = "p1")), |
294 | | ) |
295 | | ))] |
296 | | pub(crate) use libc::{TCP_KEEPCNT, TCP_KEEPINTVL}; |
297 | | |
298 | | // See this type in the Windows file. |
299 | | pub(crate) type Bool = c_int; |
300 | | |
301 | | #[cfg(any( |
302 | | target_os = "ios", |
303 | | target_os = "visionos", |
304 | | target_os = "macos", |
305 | | target_os = "nto", |
306 | | target_os = "tvos", |
307 | | target_os = "watchos", |
308 | | ))] |
309 | | use libc::TCP_KEEPALIVE as KEEPALIVE_TIME; |
310 | | #[cfg(not(any( |
311 | | target_os = "haiku", |
312 | | target_os = "ios", |
313 | | target_os = "visionos", |
314 | | target_os = "macos", |
315 | | target_os = "nto", |
316 | | target_os = "openbsd", |
317 | | target_os = "tvos", |
318 | | target_os = "watchos", |
319 | | target_os = "vita", |
320 | | )))] |
321 | | use libc::TCP_KEEPIDLE as KEEPALIVE_TIME; |
322 | | |
323 | | /// Helper macro to execute a system call that returns an `io::Result`. |
324 | | macro_rules! syscall { |
325 | | ($fn: ident ( $($arg: expr),* $(,)* ) ) => {{ |
326 | | #[allow(unused_unsafe)] |
327 | | let res = unsafe { libc::$fn($($arg, )*) }; |
328 | | if res == -1 { |
329 | | Err(std::io::Error::last_os_error()) |
330 | | } else { |
331 | | Ok(res) |
332 | | } |
333 | | }}; |
334 | | } |
335 | | |
336 | | /// Maximum size of a buffer passed to system call like `recv` and `send`. |
337 | | #[cfg(not(any( |
338 | | target_os = "ios", |
339 | | target_os = "visionos", |
340 | | target_os = "macos", |
341 | | target_os = "tvos", |
342 | | target_os = "watchos", |
343 | | target_os = "cygwin", |
344 | | )))] |
345 | | const MAX_BUF_LEN: usize = ssize_t::MAX as usize; |
346 | | |
347 | | // The maximum read limit on most posix-like systems is `SSIZE_MAX`, with the |
348 | | // man page quoting that if the count of bytes to read is greater than |
349 | | // `SSIZE_MAX` the result is "unspecified". |
350 | | // |
351 | | // On macOS, however, apparently the 64-bit libc is either buggy or |
352 | | // intentionally showing odd behavior by rejecting any read with a size larger |
353 | | // than or equal to INT_MAX. To handle both of these the read size is capped on |
354 | | // both platforms. |
355 | | #[cfg(any( |
356 | | target_os = "ios", |
357 | | target_os = "visionos", |
358 | | target_os = "macos", |
359 | | target_os = "tvos", |
360 | | target_os = "watchos", |
361 | | target_os = "cygwin", |
362 | | ))] |
363 | | const MAX_BUF_LEN: usize = c_int::MAX as usize - 1; |
364 | | |
365 | | // TCP_CA_NAME_MAX isn't defined in user space include files(not in libc) |
366 | | #[cfg(all(feature = "all", any(target_os = "freebsd", target_os = "linux")))] |
367 | | const TCP_CA_NAME_MAX: usize = 16; |
368 | | |
369 | | #[cfg(any( |
370 | | all( |
371 | | target_os = "linux", |
372 | | any( |
373 | | target_env = "gnu", |
374 | | all(target_env = "uclibc", target_pointer_width = "64") |
375 | | ) |
376 | | ), |
377 | | target_os = "android", |
378 | | ))] |
379 | | type IovLen = usize; |
380 | | |
381 | | #[cfg(any( |
382 | | all( |
383 | | target_os = "linux", |
384 | | any( |
385 | | target_env = "musl", |
386 | | target_env = "ohos", |
387 | | all(target_env = "uclibc", target_pointer_width = "32") |
388 | | ) |
389 | | ), |
390 | | target_os = "aix", |
391 | | target_os = "dragonfly", |
392 | | target_os = "freebsd", |
393 | | target_os = "fuchsia", |
394 | | target_os = "haiku", |
395 | | target_os = "hurd", |
396 | | target_os = "illumos", |
397 | | target_os = "ios", |
398 | | target_os = "visionos", |
399 | | target_os = "macos", |
400 | | target_os = "netbsd", |
401 | | target_os = "nto", |
402 | | target_os = "openbsd", |
403 | | target_os = "solaris", |
404 | | target_os = "tvos", |
405 | | target_os = "watchos", |
406 | | target_os = "espidf", |
407 | | target_os = "vita", |
408 | | target_os = "cygwin", |
409 | | ))] |
410 | | type IovLen = c_int; |
411 | | |
412 | | /// Unix only API. |
413 | | impl Domain { |
414 | | /// Domain for low-level packet interface, corresponding to `AF_PACKET`. |
415 | | #[cfg(all( |
416 | | feature = "all", |
417 | | any(target_os = "android", target_os = "fuchsia", target_os = "linux") |
418 | | ))] |
419 | | pub const PACKET: Domain = Domain(libc::AF_PACKET); |
420 | | |
421 | | /// Domain for low-level VSOCK interface, corresponding to `AF_VSOCK`. |
422 | | #[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))] |
423 | | pub const VSOCK: Domain = Domain(libc::AF_VSOCK); |
424 | | } |
425 | | |
426 | | impl_debug!( |
427 | | Domain, |
428 | | libc::AF_INET, |
429 | | libc::AF_INET6, |
430 | | #[cfg(not(target_os = "wasi"))] |
431 | | libc::AF_UNIX, |
432 | | #[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))] |
433 | | libc::AF_PACKET, |
434 | | #[cfg(any(target_os = "android", target_os = "linux"))] |
435 | | libc::AF_VSOCK, |
436 | | libc::AF_UNSPEC, // = 0. |
437 | | ); |
438 | | |
439 | | /// Unix only API. |
440 | | impl Type { |
441 | | /// Set `SOCK_NONBLOCK` on the `Type`. |
442 | | #[cfg(all( |
443 | | feature = "all", |
444 | | any( |
445 | | target_os = "android", |
446 | | target_os = "dragonfly", |
447 | | target_os = "freebsd", |
448 | | target_os = "fuchsia", |
449 | | target_os = "illumos", |
450 | | target_os = "linux", |
451 | | target_os = "netbsd", |
452 | | target_os = "openbsd", |
453 | | target_os = "cygwin", |
454 | | all(target_os = "wasi", not(target_env = "p1")), |
455 | | ) |
456 | | ))] |
457 | 0 | pub const fn nonblocking(self) -> Type { |
458 | 0 | Type(self.0 | libc::SOCK_NONBLOCK) |
459 | 0 | } |
460 | | |
461 | | /// Set `SOCK_CLOEXEC` on the `Type`. |
462 | | #[cfg(all( |
463 | | feature = "all", |
464 | | any( |
465 | | target_os = "android", |
466 | | target_os = "dragonfly", |
467 | | target_os = "freebsd", |
468 | | target_os = "fuchsia", |
469 | | target_os = "hurd", |
470 | | target_os = "illumos", |
471 | | target_os = "linux", |
472 | | target_os = "netbsd", |
473 | | target_os = "openbsd", |
474 | | target_os = "redox", |
475 | | target_os = "solaris", |
476 | | target_os = "cygwin", |
477 | | ) |
478 | | ))] |
479 | 0 | pub const fn cloexec(self) -> Type { |
480 | 0 | self._cloexec() |
481 | 0 | } |
482 | | |
483 | | #[cfg(any( |
484 | | target_os = "android", |
485 | | target_os = "dragonfly", |
486 | | target_os = "freebsd", |
487 | | target_os = "fuchsia", |
488 | | target_os = "hurd", |
489 | | target_os = "illumos", |
490 | | target_os = "linux", |
491 | | target_os = "netbsd", |
492 | | target_os = "openbsd", |
493 | | target_os = "redox", |
494 | | target_os = "solaris", |
495 | | target_os = "cygwin", |
496 | | ))] |
497 | 0 | pub(crate) const fn _cloexec(self) -> Type { |
498 | 0 | Type(self.0 | libc::SOCK_CLOEXEC) |
499 | 0 | } |
500 | | } |
501 | | |
502 | | impl_debug!( |
503 | | Type, |
504 | | libc::SOCK_STREAM, |
505 | | libc::SOCK_DGRAM, |
506 | | #[cfg(all(feature = "all", target_os = "linux"))] |
507 | | libc::SOCK_DCCP, |
508 | | #[cfg(not(any(target_os = "redox", target_os = "espidf", target_os = "wasi")))] |
509 | | libc::SOCK_RAW, |
510 | | #[cfg(not(any( |
511 | | target_os = "redox", |
512 | | target_os = "haiku", |
513 | | target_os = "espidf", |
514 | | target_os = "wasi" |
515 | | )))] |
516 | | libc::SOCK_RDM, |
517 | | #[cfg(not(any(target_os = "espidf", target_os = "wasi")))] |
518 | | libc::SOCK_SEQPACKET, |
519 | | /* TODO: add these optional bit OR-ed flags: |
520 | | #[cfg(any( |
521 | | target_os = "android", |
522 | | target_os = "dragonfly", |
523 | | target_os = "freebsd", |
524 | | target_os = "fuchsia", |
525 | | target_os = "linux", |
526 | | target_os = "netbsd", |
527 | | target_os = "openbsd" |
528 | | ))] |
529 | | libc::SOCK_NONBLOCK, |
530 | | #[cfg(any( |
531 | | target_os = "android", |
532 | | target_os = "dragonfly", |
533 | | target_os = "freebsd", |
534 | | target_os = "fuchsia", |
535 | | target_os = "linux", |
536 | | target_os = "netbsd", |
537 | | target_os = "openbsd" |
538 | | ))] |
539 | | libc::SOCK_CLOEXEC, |
540 | | */ |
541 | | ); |
542 | | |
543 | | impl_debug!( |
544 | | Protocol, |
545 | | #[cfg(not(target_os = "wasi"))] |
546 | | libc::IPPROTO_ICMP, |
547 | | #[cfg(not(target_os = "wasi"))] |
548 | | libc::IPPROTO_ICMPV6, |
549 | | libc::IPPROTO_TCP, |
550 | | libc::IPPROTO_UDP, |
551 | | #[cfg(target_os = "linux")] |
552 | | libc::IPPROTO_MPTCP, |
553 | | #[cfg(all(feature = "all", target_os = "linux"))] |
554 | | libc::IPPROTO_DCCP, |
555 | | #[cfg(all(feature = "all", any(target_os = "freebsd", target_os = "linux")))] |
556 | | libc::IPPROTO_SCTP, |
557 | | #[cfg(all( |
558 | | feature = "all", |
559 | | any( |
560 | | target_os = "android", |
561 | | target_os = "freebsd", |
562 | | target_os = "fuchsia", |
563 | | target_os = "linux", |
564 | | ) |
565 | | ))] |
566 | | libc::IPPROTO_UDPLITE, |
567 | | #[cfg(all(feature = "all", any(target_os = "freebsd", target_os = "openbsd")))] |
568 | | libc::IPPROTO_DIVERT, |
569 | | ); |
570 | | |
571 | | /// Unix-only API. |
572 | | #[cfg(not(any(target_os = "redox", target_os = "wasi")))] |
573 | | impl RecvFlags { |
574 | | /// Check if the message terminates a record. |
575 | | /// |
576 | | /// Not all socket types support the notion of records. For socket types |
577 | | /// that do support it (such as [`SEQPACKET`]), a record is terminated by |
578 | | /// sending a message with the end-of-record flag set. |
579 | | /// |
580 | | /// On Unix this corresponds to the `MSG_EOR` flag. |
581 | | /// |
582 | | /// [`SEQPACKET`]: Type::SEQPACKET |
583 | | #[cfg(not(target_os = "espidf"))] |
584 | 0 | pub const fn is_end_of_record(self) -> bool { |
585 | 0 | self.0 & libc::MSG_EOR != 0 |
586 | 0 | } |
587 | | |
588 | | /// Check if the message contains out-of-band data. |
589 | | /// |
590 | | /// This is useful for protocols where you receive out-of-band data |
591 | | /// mixed in with the normal data stream. |
592 | | /// |
593 | | /// On Unix this corresponds to the `MSG_OOB` flag. |
594 | 0 | pub const fn is_out_of_band(self) -> bool { |
595 | 0 | self.0 & libc::MSG_OOB != 0 |
596 | 0 | } |
597 | | |
598 | | /// Check if the confirm flag is set. |
599 | | /// |
600 | | /// This is used by SocketCAN to indicate a frame was sent via the |
601 | | /// socket it is received on. This flag can be interpreted as a |
602 | | /// 'transmission confirmation'. |
603 | | /// |
604 | | /// On Unix this corresponds to the `MSG_CONFIRM` flag. |
605 | | #[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))] |
606 | 0 | pub const fn is_confirm(self) -> bool { |
607 | 0 | self.0 & libc::MSG_CONFIRM != 0 |
608 | 0 | } |
609 | | |
610 | | /// Check if the don't route flag is set. |
611 | | /// |
612 | | /// This is used by SocketCAN to indicate a frame was created |
613 | | /// on the local host. |
614 | | /// |
615 | | /// On Unix this corresponds to the `MSG_DONTROUTE` flag. |
616 | | #[cfg(all( |
617 | | feature = "all", |
618 | | any(target_os = "android", target_os = "linux", target_os = "cygwin"), |
619 | | ))] |
620 | 0 | pub const fn is_dontroute(self) -> bool { |
621 | 0 | self.0 & libc::MSG_DONTROUTE != 0 |
622 | 0 | } |
623 | | } |
624 | | |
625 | | #[cfg(not(any(target_os = "redox", target_os = "wasi")))] |
626 | | impl std::fmt::Debug for RecvFlags { |
627 | 0 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
628 | 0 | let mut s = f.debug_struct("RecvFlags"); |
629 | | #[cfg(not(target_os = "espidf"))] |
630 | 0 | s.field("is_end_of_record", &self.is_end_of_record()); |
631 | 0 | s.field("is_out_of_band", &self.is_out_of_band()); |
632 | | #[cfg(not(target_os = "espidf"))] |
633 | 0 | s.field("is_truncated", &self.is_truncated()); |
634 | | #[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))] |
635 | 0 | s.field("is_confirm", &self.is_confirm()); |
636 | | #[cfg(all( |
637 | | feature = "all", |
638 | | any(target_os = "android", target_os = "linux", target_os = "cygwin"), |
639 | | ))] |
640 | 0 | s.field("is_dontroute", &self.is_dontroute()); |
641 | 0 | s.finish() |
642 | 0 | } |
643 | | } |
644 | | |
645 | | #[repr(transparent)] |
646 | | pub struct MaybeUninitSlice<'a> { |
647 | | vec: libc::iovec, |
648 | | _lifetime: PhantomData<&'a mut [MaybeUninit<u8>]>, |
649 | | } |
650 | | |
651 | | unsafe impl<'a> Send for MaybeUninitSlice<'a> {} |
652 | | |
653 | | unsafe impl<'a> Sync for MaybeUninitSlice<'a> {} |
654 | | |
655 | | impl<'a> MaybeUninitSlice<'a> { |
656 | 0 | pub(crate) fn new(buf: &'a mut [MaybeUninit<u8>]) -> MaybeUninitSlice<'a> { |
657 | 0 | MaybeUninitSlice { |
658 | 0 | vec: libc::iovec { |
659 | 0 | iov_base: buf.as_mut_ptr().cast(), |
660 | 0 | iov_len: buf.len(), |
661 | 0 | }, |
662 | 0 | _lifetime: PhantomData, |
663 | 0 | } |
664 | 0 | } |
665 | | |
666 | 0 | pub(crate) fn as_slice(&self) -> &[MaybeUninit<u8>] { |
667 | 0 | unsafe { slice::from_raw_parts(self.vec.iov_base.cast(), self.vec.iov_len) } |
668 | 0 | } |
669 | | |
670 | 0 | pub(crate) fn as_mut_slice(&mut self) -> &mut [MaybeUninit<u8>] { |
671 | 0 | unsafe { slice::from_raw_parts_mut(self.vec.iov_base.cast(), self.vec.iov_len) } |
672 | 0 | } |
673 | | } |
674 | | |
675 | | /// Returns the offset of the `sun_path` member of the passed unix socket address. |
676 | | #[cfg(not(target_os = "wasi"))] |
677 | 0 | pub(crate) fn offset_of_path(storage: &libc::sockaddr_un) -> usize { |
678 | 0 | let base = storage as *const _ as usize; |
679 | 0 | let path = ptr::addr_of!(storage.sun_path) as usize; |
680 | 0 | path - base |
681 | 0 | } |
682 | | |
683 | | #[cfg(not(target_os = "wasi"))] |
684 | | #[allow(unsafe_op_in_unsafe_fn)] |
685 | 0 | pub(crate) fn unix_sockaddr(path: &Path) -> io::Result<SockAddr> { |
686 | 0 | let mut storage = SockAddrStorage::zeroed(); |
687 | 0 | let len = { |
688 | | // SAFETY: sockaddr_un is one of the sockaddr_* types defined by this platform. |
689 | 0 | let storage = unsafe { storage.view_as::<libc::sockaddr_un>() }; |
690 | | |
691 | 0 | let bytes = path.as_os_str().as_bytes(); |
692 | 0 | let too_long = match bytes.first() { |
693 | 0 | None => false, |
694 | | // linux abstract namespaces aren't null-terminated |
695 | 0 | Some(&0) => bytes.len() > storage.sun_path.len(), |
696 | 0 | Some(_) => bytes.len() >= storage.sun_path.len(), |
697 | | }; |
698 | 0 | if too_long { |
699 | 0 | return Err(io::Error::new( |
700 | 0 | io::ErrorKind::InvalidInput, |
701 | 0 | "path must be shorter than SUN_LEN", |
702 | 0 | )); |
703 | 0 | } |
704 | | |
705 | 0 | storage.sun_family = libc::AF_UNIX as sa_family_t; |
706 | | // SAFETY: `bytes` and `addr.sun_path` are not overlapping and |
707 | | // both point to valid memory. |
708 | | // `storage` was initialized to zero above, so the path is |
709 | | // already NULL terminated. |
710 | 0 | unsafe { |
711 | 0 | ptr::copy_nonoverlapping( |
712 | 0 | bytes.as_ptr(), |
713 | 0 | storage.sun_path.as_mut_ptr().cast(), |
714 | 0 | bytes.len(), |
715 | 0 | ); |
716 | 0 | } |
717 | | |
718 | 0 | let sun_path_offset = offset_of_path(storage); |
719 | 0 | sun_path_offset |
720 | 0 | + bytes.len() |
721 | 0 | + match bytes.first() { |
722 | 0 | Some(&0) | None => 0, |
723 | 0 | Some(_) => 1, |
724 | | } |
725 | | }; |
726 | 0 | Ok(unsafe { SockAddr::new(storage, len as socklen_t) }) |
727 | 0 | } |
728 | | |
729 | | // Used in `MsgHdr`. |
730 | | #[cfg(not(any(target_os = "redox", target_os = "wasi")))] |
731 | | pub(crate) use libc::msghdr; |
732 | | |
733 | | #[cfg(not(any(target_os = "redox", target_os = "wasi")))] |
734 | 0 | pub(crate) fn set_msghdr_name(msg: &mut msghdr, name: &SockAddr) { |
735 | 0 | msg.msg_name = name.as_ptr() as *mut _; |
736 | 0 | msg.msg_namelen = name.len(); |
737 | 0 | } |
738 | | |
739 | | #[cfg(not(any(target_os = "redox", target_os = "wasi")))] |
740 | | #[allow(clippy::unnecessary_cast)] // IovLen type can be `usize`. |
741 | 0 | pub(crate) fn set_msghdr_iov(msg: &mut msghdr, ptr: *mut libc::iovec, len: usize) { |
742 | 0 | msg.msg_iov = ptr; |
743 | 0 | msg.msg_iovlen = min(len, IovLen::MAX as usize) as IovLen; |
744 | 0 | } |
745 | | |
746 | | #[cfg(not(any(target_os = "redox", target_os = "wasi")))] |
747 | 0 | pub(crate) fn set_msghdr_control(msg: &mut msghdr, ptr: *mut libc::c_void, len: usize) { |
748 | 0 | msg.msg_control = ptr; |
749 | 0 | msg.msg_controllen = len as _; |
750 | 0 | } |
751 | | |
752 | | #[cfg(not(any(target_os = "redox", target_os = "wasi")))] |
753 | 0 | pub(crate) fn set_msghdr_flags(msg: &mut msghdr, flags: c_int) { |
754 | 0 | msg.msg_flags = flags; |
755 | 0 | } |
756 | | |
757 | | #[cfg(not(any(target_os = "redox", target_os = "wasi")))] |
758 | 0 | pub(crate) fn msghdr_flags(msg: &msghdr) -> RecvFlags { |
759 | 0 | RecvFlags(msg.msg_flags) |
760 | 0 | } |
761 | | |
762 | | #[cfg(not(any(target_os = "redox", target_os = "wasi")))] |
763 | 0 | pub(crate) fn msghdr_control_len(msg: &msghdr) -> usize { |
764 | 0 | msg.msg_controllen as _ |
765 | 0 | } |
766 | | |
767 | | /// Unix only API. |
768 | | impl SockAddr { |
769 | | /// Constructs a `SockAddr` with the family `AF_VSOCK` and the provided CID/port. |
770 | | /// |
771 | | /// # Errors |
772 | | /// |
773 | | /// This function can never fail. In a future version of this library it will be made |
774 | | /// infallible. |
775 | | #[allow(unsafe_op_in_unsafe_fn)] |
776 | | #[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))] |
777 | 0 | pub fn vsock(cid: u32, port: u32) -> SockAddr { |
778 | 0 | let mut storage = SockAddrStorage::zeroed(); |
779 | 0 | { |
780 | 0 | // SAFETY: sockaddr_vm is one of the sockaddr_* types defined by this platform. |
781 | 0 | let storage = unsafe { storage.view_as::<libc::sockaddr_vm>() }; |
782 | 0 | storage.svm_family = libc::AF_VSOCK as sa_family_t; |
783 | 0 | storage.svm_cid = cid; |
784 | 0 | storage.svm_port = port; |
785 | 0 | } |
786 | 0 | unsafe { SockAddr::new(storage, mem::size_of::<libc::sockaddr_vm>() as socklen_t) } |
787 | 0 | } |
788 | | |
789 | | /// Returns this address VSOCK CID/port if it is in the `AF_VSOCK` family, |
790 | | /// otherwise return `None`. |
791 | | #[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))] |
792 | 0 | pub fn as_vsock_address(&self) -> Option<(u32, u32)> { |
793 | 0 | if self.family() == libc::AF_VSOCK as sa_family_t { |
794 | | // Safety: if the ss_family field is AF_VSOCK then storage must be a sockaddr_vm. |
795 | 0 | let addr = unsafe { &*(self.as_ptr() as *const libc::sockaddr_vm) }; |
796 | 0 | Some((addr.svm_cid, addr.svm_port)) |
797 | | } else { |
798 | 0 | None |
799 | | } |
800 | 0 | } |
801 | | } |
802 | | |
803 | | /// Unix only API (not available on WASI). |
804 | | #[cfg(not(target_os = "wasi"))] |
805 | | impl SockAddr { |
806 | | /// Returns true if this address is an unnamed address from the `AF_UNIX` family (for local |
807 | | /// interprocess communication), false otherwise. |
808 | 0 | pub fn is_unnamed(&self) -> bool { |
809 | 0 | self.as_sockaddr_un() |
810 | 0 | .map(|storage| { |
811 | 0 | self.len() == offset_of_path(storage) as _ |
812 | | // On some non-linux platforms a zeroed path is returned for unnamed. |
813 | | // Abstract addresses only exist on Linux. |
814 | | // NOTE: although Fuchsia does define `AF_UNIX` it's not actually implemented. |
815 | | // See https://github.com/rust-lang/socket2/pull/403#discussion_r1123557978 |
816 | 0 | || (cfg!(not(any(target_os = "linux", target_os = "android", target_os = "cygwin"))) |
817 | 0 | && storage.sun_path[0] == 0) |
818 | 0 | }) |
819 | 0 | .unwrap_or_default() |
820 | 0 | } |
821 | | |
822 | | /// Returns the underlying `sockaddr_un` object if this address is from the `AF_UNIX` family, |
823 | | /// otherwise returns `None`. |
824 | 0 | pub(crate) fn as_sockaddr_un(&self) -> Option<&libc::sockaddr_un> { |
825 | 0 | self.is_unix().then(|| { |
826 | | // SAFETY: if unix socket, i.e. the `ss_family` field is `AF_UNIX` then storage must be |
827 | | // a `sockaddr_un`. |
828 | 0 | unsafe { &*self.as_ptr().cast::<libc::sockaddr_un>() } |
829 | 0 | }) |
830 | 0 | } |
831 | | |
832 | | /// Get the length of the path bytes of the address, not including the terminating or initial |
833 | | /// (for abstract names) null byte. |
834 | | /// |
835 | | /// Should not be called on unnamed addresses. |
836 | 0 | fn path_len(&self, storage: &libc::sockaddr_un) -> usize { |
837 | 0 | debug_assert!(!self.is_unnamed()); |
838 | 0 | self.len() as usize - offset_of_path(storage) - 1 |
839 | 0 | } |
840 | | |
841 | | /// Get a u8 slice for the bytes of the pathname or abstract name. |
842 | | /// |
843 | | /// Should not be called on unnamed addresses. |
844 | 0 | fn path_bytes(&self, storage: &libc::sockaddr_un, abstract_name: bool) -> &[u8] { |
845 | 0 | debug_assert!(!self.is_unnamed()); |
846 | | // SAFETY: the pointed objects of type `i8` have the same memory layout as `u8`. The path is |
847 | | // the last field in the storage and so its length is equal to |
848 | | // TOTAL_LENGTH - OFFSET_OF_PATH -1 |
849 | | // Where the 1 is either a terminating null if we have a pathname address, or the initial |
850 | | // null byte, if it's an abstract name address. In the latter case, the path bytes start |
851 | | // after the initial null byte, hence the `offset`. |
852 | | // There is no safe way to convert a `&[i8]` to `&[u8]` |
853 | | unsafe { |
854 | 0 | slice::from_raw_parts( |
855 | 0 | (storage.sun_path.as_ptr() as *const u8).offset(abstract_name as isize), |
856 | 0 | self.path_len(storage), |
857 | 0 | ) |
858 | | } |
859 | 0 | } |
860 | | |
861 | | /// Returns this address as Unix `SocketAddr` if it is an `AF_UNIX` pathname |
862 | | /// address, otherwise returns `None`. |
863 | 0 | pub fn as_unix(&self) -> Option<std::os::unix::net::SocketAddr> { |
864 | 0 | let path = self.as_pathname()?; |
865 | | // SAFETY: we can represent this as a valid pathname, then so can the |
866 | | // standard library. |
867 | 0 | Some(std::os::unix::net::SocketAddr::from_pathname(path).unwrap()) |
868 | 0 | } |
869 | | |
870 | | /// Returns this address as a `Path` reference if it is an `AF_UNIX` |
871 | | /// pathname address, otherwise returns `None`. |
872 | 0 | pub fn as_pathname(&self) -> Option<&Path> { |
873 | 0 | self.as_sockaddr_un().and_then(|storage| { |
874 | 0 | (self.len() > offset_of_path(storage) as _ && storage.sun_path[0] != 0).then(|| { |
875 | 0 | let path_slice = self.path_bytes(storage, false); |
876 | 0 | Path::new::<OsStr>(OsStrExt::from_bytes(path_slice)) |
877 | 0 | }) |
878 | 0 | }) |
879 | 0 | } |
880 | | |
881 | | /// Returns this address as a slice of bytes representing an abstract address if it is an |
882 | | /// `AF_UNIX` abstract address, otherwise returns `None`. |
883 | | /// |
884 | | /// Abstract addresses are a Linux extension, so this method returns `None` on all non-Linux |
885 | | /// platforms. |
886 | 0 | pub fn as_abstract_namespace(&self) -> Option<&[u8]> { |
887 | | // NOTE: although Fuchsia does define `AF_UNIX` it's not actually implemented. |
888 | | // See https://github.com/rust-lang/socket2/pull/403#discussion_r1123557978 |
889 | | #[cfg(any(target_os = "linux", target_os = "android", target_os = "cygwin"))] |
890 | | { |
891 | 0 | self.as_sockaddr_un().and_then(|storage| { |
892 | 0 | (self.len() > offset_of_path(storage) as _ && storage.sun_path[0] == 0) |
893 | 0 | .then(|| self.path_bytes(storage, true)) |
894 | 0 | }) |
895 | | } |
896 | | #[cfg(not(any(target_os = "linux", target_os = "android", target_os = "cygwin")))] |
897 | | None |
898 | 0 | } |
899 | | } |
900 | | |
901 | | pub(crate) type Socket = std::os::fd::OwnedFd; |
902 | | pub(crate) type RawSocket = c_int; |
903 | | |
904 | 0 | pub(crate) unsafe fn socket_from_raw(socket: RawSocket) -> Socket { |
905 | 0 | Socket::from_raw_fd(socket) |
906 | 0 | } |
907 | | |
908 | 0 | pub(crate) fn socket_as_raw(socket: &Socket) -> RawSocket { |
909 | 0 | socket.as_raw_fd() |
910 | 0 | } |
911 | | |
912 | 0 | pub(crate) fn socket_into_raw(socket: Socket) -> RawSocket { |
913 | 0 | socket.into_raw_fd() |
914 | 0 | } |
915 | | |
916 | 0 | pub(crate) fn socket(family: c_int, ty: c_int, protocol: c_int) -> io::Result<RawSocket> { |
917 | 0 | syscall!(socket(family, ty, protocol)) |
918 | 0 | } |
919 | | |
920 | | #[cfg(all(feature = "all", unix))] |
921 | 0 | pub(crate) fn socketpair(family: c_int, ty: c_int, protocol: c_int) -> io::Result<[RawSocket; 2]> { |
922 | 0 | let mut fds = [0, 0]; |
923 | 0 | syscall!(socketpair(family, ty, protocol, fds.as_mut_ptr())).map(|_| fds) |
924 | 0 | } |
925 | | |
926 | 0 | pub(crate) fn bind(fd: RawSocket, addr: &SockAddr) -> io::Result<()> { |
927 | 0 | syscall!(bind(fd, addr.as_ptr().cast::<sockaddr>(), addr.len() as _)).map(|_| ()) |
928 | 0 | } |
929 | | |
930 | 0 | pub(crate) fn connect(fd: RawSocket, addr: &SockAddr) -> io::Result<()> { |
931 | 0 | syscall!(connect(fd, addr.as_ptr().cast::<sockaddr>(), addr.len())).map(|_| ()) |
932 | 0 | } |
933 | | |
934 | 0 | pub(crate) fn poll_connect(socket: &crate::Socket, timeout: Duration) -> io::Result<()> { |
935 | 0 | let start = Instant::now(); |
936 | | |
937 | 0 | let mut pollfd = libc::pollfd { |
938 | 0 | fd: socket.as_raw(), |
939 | 0 | events: libc::POLLIN | libc::POLLOUT, |
940 | 0 | revents: 0, |
941 | 0 | }; |
942 | | |
943 | | loop { |
944 | 0 | let elapsed = start.elapsed(); |
945 | 0 | if elapsed >= timeout { |
946 | 0 | return Err(io::ErrorKind::TimedOut.into()); |
947 | 0 | } |
948 | | |
949 | 0 | let timeout = (timeout - elapsed).as_millis(); |
950 | 0 | let timeout = timeout.clamp(1, c_int::MAX as u128) as c_int; |
951 | | |
952 | 0 | match syscall!(poll(&mut pollfd, 1, timeout)) { |
953 | 0 | Ok(0) => return Err(io::ErrorKind::TimedOut.into()), |
954 | | Ok(_) => { |
955 | | // Error or hang up indicates an error (or failure to connect). |
956 | 0 | if (pollfd.revents & libc::POLLHUP) != 0 || (pollfd.revents & libc::POLLERR) != 0 { |
957 | 0 | match socket.take_error() { |
958 | 0 | Ok(Some(err)) | Err(err) => return Err(err), |
959 | | Ok(None) => { |
960 | 0 | return Err(io::Error::new( |
961 | 0 | io::ErrorKind::Other, |
962 | 0 | "no error set after POLLHUP", |
963 | 0 | )) |
964 | | } |
965 | | } |
966 | 0 | } |
967 | 0 | return Ok(()); |
968 | | } |
969 | | // Got interrupted, try again. |
970 | 0 | Err(ref err) if err.kind() == io::ErrorKind::Interrupted => continue, |
971 | 0 | Err(err) => return Err(err), |
972 | | } |
973 | | } |
974 | 0 | } |
975 | | |
976 | 0 | pub(crate) fn listen(fd: RawSocket, backlog: c_int) -> io::Result<()> { |
977 | 0 | syscall!(listen(fd, backlog)).map(|_| ()) |
978 | 0 | } |
979 | | |
980 | 0 | pub(crate) fn accept(fd: RawSocket) -> io::Result<(RawSocket, SockAddr)> { |
981 | | // Safety: `accept` initialises the `SockAddr` for us. |
982 | 0 | unsafe { SockAddr::try_init(|storage, len| syscall!(accept(fd, storage.cast(), len))) } |
983 | 0 | } |
984 | | |
985 | 0 | pub(crate) fn getsockname(fd: RawSocket) -> io::Result<SockAddr> { |
986 | | // Safety: `accept` initialises the `SockAddr` for us. |
987 | 0 | unsafe { SockAddr::try_init(|storage, len| syscall!(getsockname(fd, storage.cast(), len))) } |
988 | 0 | .map(|(_, addr)| addr) |
989 | 0 | } |
990 | | |
991 | 0 | pub(crate) fn getpeername(fd: RawSocket) -> io::Result<SockAddr> { |
992 | | // Safety: `accept` initialises the `SockAddr` for us. |
993 | 0 | unsafe { SockAddr::try_init(|storage, len| syscall!(getpeername(fd, storage.cast(), len))) } |
994 | 0 | .map(|(_, addr)| addr) |
995 | 0 | } |
996 | | |
997 | | #[cfg(not(target_os = "wasi"))] |
998 | 0 | pub(crate) fn try_clone(fd: RawSocket) -> io::Result<RawSocket> { |
999 | 0 | syscall!(fcntl(fd, libc::F_DUPFD_CLOEXEC, 0)) |
1000 | 0 | } |
1001 | | |
1002 | | #[cfg(all( |
1003 | | feature = "all", |
1004 | | any(unix, all(target_os = "wasi", not(target_env = "p1"))), |
1005 | | not(target_os = "vita") |
1006 | | ))] |
1007 | 0 | pub(crate) fn nonblocking(fd: RawSocket) -> io::Result<bool> { |
1008 | 0 | let file_status_flags = fcntl_get(fd, libc::F_GETFL)?; |
1009 | 0 | Ok((file_status_flags & libc::O_NONBLOCK) != 0) |
1010 | 0 | } |
1011 | | |
1012 | | #[cfg(all(feature = "all", target_os = "vita"))] |
1013 | | pub(crate) fn nonblocking(fd: RawSocket) -> io::Result<bool> { |
1014 | | unsafe { |
1015 | | getsockopt::<Bool>(fd, libc::SOL_SOCKET, libc::SO_NONBLOCK).map(|non_block| non_block != 0) |
1016 | | } |
1017 | | } |
1018 | | |
1019 | | #[cfg(not(target_os = "vita"))] |
1020 | 0 | pub(crate) fn set_nonblocking(fd: RawSocket, nonblocking: bool) -> io::Result<()> { |
1021 | 0 | if nonblocking { |
1022 | 0 | fcntl_add(fd, libc::F_GETFL, libc::F_SETFL, libc::O_NONBLOCK) |
1023 | | } else { |
1024 | 0 | fcntl_remove(fd, libc::F_GETFL, libc::F_SETFL, libc::O_NONBLOCK) |
1025 | | } |
1026 | 0 | } |
1027 | | |
1028 | | #[cfg(target_os = "vita")] |
1029 | | pub(crate) fn set_nonblocking(fd: RawSocket, nonblocking: bool) -> io::Result<()> { |
1030 | | unsafe { |
1031 | | setsockopt( |
1032 | | fd, |
1033 | | libc::SOL_SOCKET, |
1034 | | libc::SO_NONBLOCK, |
1035 | | nonblocking as c_int, |
1036 | | ) |
1037 | | } |
1038 | | } |
1039 | | |
1040 | 0 | pub(crate) fn shutdown(fd: RawSocket, how: Shutdown) -> io::Result<()> { |
1041 | 0 | let how = match how { |
1042 | 0 | Shutdown::Write => libc::SHUT_WR, |
1043 | 0 | Shutdown::Read => libc::SHUT_RD, |
1044 | 0 | Shutdown::Both => libc::SHUT_RDWR, |
1045 | | }; |
1046 | 0 | syscall!(shutdown(fd, how)).map(|_| ()) |
1047 | 0 | } |
1048 | | |
1049 | 0 | pub(crate) fn recv(fd: RawSocket, buf: &mut [MaybeUninit<u8>], flags: c_int) -> io::Result<usize> { |
1050 | 0 | syscall!(recv( |
1051 | 0 | fd, |
1052 | 0 | buf.as_mut_ptr().cast(), |
1053 | 0 | min(buf.len(), MAX_BUF_LEN), |
1054 | 0 | flags, |
1055 | | )) |
1056 | 0 | .map(|n| n as usize) |
1057 | 0 | } |
1058 | | |
1059 | 0 | pub(crate) fn recv_from( |
1060 | 0 | fd: RawSocket, |
1061 | 0 | buf: &mut [MaybeUninit<u8>], |
1062 | 0 | flags: c_int, |
1063 | 0 | ) -> io::Result<(usize, SockAddr)> { |
1064 | | // Safety: `recvfrom` initialises the `SockAddr` for us. |
1065 | | unsafe { |
1066 | 0 | SockAddr::try_init(|addr, addrlen| { |
1067 | 0 | syscall!(recvfrom( |
1068 | 0 | fd, |
1069 | 0 | buf.as_mut_ptr().cast(), |
1070 | 0 | min(buf.len(), MAX_BUF_LEN), |
1071 | 0 | flags, |
1072 | 0 | addr.cast(), |
1073 | 0 | addrlen |
1074 | | )) |
1075 | 0 | .map(|n| n as usize) |
1076 | 0 | }) |
1077 | | } |
1078 | 0 | } |
1079 | | |
1080 | 0 | pub(crate) fn peek_sender(fd: RawSocket) -> io::Result<SockAddr> { |
1081 | | // Unix-like platforms simply truncate the returned data, so this implementation is trivial. |
1082 | | // However, for Windows this requires suppressing the `WSAEMSGSIZE` error, |
1083 | | // so that requires a different approach. |
1084 | | // NOTE: macOS does not populate `sockaddr` if you pass a zero-sized buffer. |
1085 | 0 | let (_, sender) = recv_from(fd, &mut [MaybeUninit::uninit(); 8], MSG_PEEK)?; |
1086 | 0 | Ok(sender) |
1087 | 0 | } |
1088 | | |
1089 | | #[cfg(not(any(target_os = "redox", target_os = "wasi")))] |
1090 | 0 | pub(crate) fn recv_vectored( |
1091 | 0 | fd: RawSocket, |
1092 | 0 | bufs: &mut [crate::MaybeUninitSlice<'_>], |
1093 | 0 | flags: c_int, |
1094 | 0 | ) -> io::Result<(usize, RecvFlags)> { |
1095 | 0 | let mut msg = MsgHdrMut::new().with_buffers(bufs); |
1096 | 0 | let n = recvmsg(fd, &mut msg, flags)?; |
1097 | 0 | Ok((n, msg.flags())) |
1098 | 0 | } |
1099 | | |
1100 | | #[cfg(not(any(target_os = "redox", target_os = "wasi")))] |
1101 | 0 | pub(crate) fn recv_from_vectored( |
1102 | 0 | fd: RawSocket, |
1103 | 0 | bufs: &mut [crate::MaybeUninitSlice<'_>], |
1104 | 0 | flags: c_int, |
1105 | 0 | ) -> io::Result<(usize, RecvFlags, SockAddr)> { |
1106 | 0 | let mut msg = MsgHdrMut::new().with_buffers(bufs); |
1107 | | // SAFETY: `recvmsg` initialises the address storage and we set the length |
1108 | | // manually. |
1109 | 0 | let (n, addr) = unsafe { |
1110 | 0 | SockAddr::try_init(|storage, len| { |
1111 | 0 | msg.inner.msg_name = storage.cast(); |
1112 | 0 | msg.inner.msg_namelen = *len; |
1113 | 0 | let n = recvmsg(fd, &mut msg, flags)?; |
1114 | | // Set the correct address length. |
1115 | 0 | *len = msg.inner.msg_namelen; |
1116 | 0 | Ok(n) |
1117 | 0 | })? |
1118 | | }; |
1119 | 0 | Ok((n, msg.flags(), addr)) |
1120 | 0 | } |
1121 | | |
1122 | | #[cfg(not(any(target_os = "redox", target_os = "wasi")))] |
1123 | 0 | pub(crate) fn recvmsg( |
1124 | 0 | fd: RawSocket, |
1125 | 0 | msg: &mut MsgHdrMut<'_, '_, '_>, |
1126 | 0 | flags: c_int, |
1127 | 0 | ) -> io::Result<usize> { |
1128 | 0 | syscall!(recvmsg(fd, &mut msg.inner, flags)).map(|n| n as usize) |
1129 | 0 | } |
1130 | | |
1131 | 0 | pub(crate) fn send(fd: RawSocket, buf: &[u8], flags: c_int) -> io::Result<usize> { |
1132 | 0 | syscall!(send( |
1133 | 0 | fd, |
1134 | 0 | buf.as_ptr().cast(), |
1135 | 0 | min(buf.len(), MAX_BUF_LEN), |
1136 | 0 | flags, |
1137 | | )) |
1138 | 0 | .map(|n| n as usize) |
1139 | 0 | } |
1140 | | |
1141 | | #[cfg(not(any(target_os = "redox", target_os = "wasi")))] |
1142 | 0 | pub(crate) fn send_vectored( |
1143 | 0 | fd: RawSocket, |
1144 | 0 | bufs: &[IoSlice<'_>], |
1145 | 0 | flags: c_int, |
1146 | 0 | ) -> io::Result<usize> { |
1147 | 0 | let msg = MsgHdr::new().with_buffers(bufs); |
1148 | 0 | sendmsg(fd, &msg, flags) |
1149 | 0 | } |
1150 | | |
1151 | 0 | pub(crate) fn send_to( |
1152 | 0 | fd: RawSocket, |
1153 | 0 | buf: &[u8], |
1154 | 0 | addr: &SockAddr, |
1155 | 0 | flags: c_int, |
1156 | 0 | ) -> io::Result<usize> { |
1157 | 0 | syscall!(sendto( |
1158 | 0 | fd, |
1159 | 0 | buf.as_ptr().cast(), |
1160 | 0 | min(buf.len(), MAX_BUF_LEN), |
1161 | 0 | flags, |
1162 | 0 | addr.as_ptr().cast::<sockaddr>(), |
1163 | 0 | addr.len(), |
1164 | | )) |
1165 | 0 | .map(|n| n as usize) |
1166 | 0 | } |
1167 | | |
1168 | | #[cfg(not(any(target_os = "redox", target_os = "wasi")))] |
1169 | 0 | pub(crate) fn send_to_vectored( |
1170 | 0 | fd: RawSocket, |
1171 | 0 | bufs: &[IoSlice<'_>], |
1172 | 0 | addr: &SockAddr, |
1173 | 0 | flags: c_int, |
1174 | 0 | ) -> io::Result<usize> { |
1175 | 0 | let msg = MsgHdr::new().with_addr(addr).with_buffers(bufs); |
1176 | 0 | sendmsg(fd, &msg, flags) |
1177 | 0 | } |
1178 | | |
1179 | | #[cfg(not(any(target_os = "redox", target_os = "wasi")))] |
1180 | 0 | pub(crate) fn sendmsg(fd: RawSocket, msg: &MsgHdr<'_, '_, '_>, flags: c_int) -> io::Result<usize> { |
1181 | 0 | syscall!(sendmsg(fd, &msg.inner, flags)).map(|n| n as usize) |
1182 | 0 | } |
1183 | | |
1184 | | /// Wrapper around `getsockopt` to deal with platform specific timeouts. |
1185 | 0 | pub(crate) fn timeout_opt(fd: RawSocket, opt: c_int, val: c_int) -> io::Result<Option<Duration>> { |
1186 | 0 | unsafe { getsockopt(fd, opt, val).map(from_timeval) } |
1187 | 0 | } |
1188 | | |
1189 | 0 | const fn from_timeval(duration: libc::timeval) -> Option<Duration> { |
1190 | 0 | if duration.tv_sec == 0 && duration.tv_usec == 0 { |
1191 | 0 | None |
1192 | | } else { |
1193 | 0 | let sec = duration.tv_sec as u64; |
1194 | 0 | let nsec = (duration.tv_usec as u32) * 1000; |
1195 | 0 | Some(Duration::new(sec, nsec)) |
1196 | | } |
1197 | 0 | } |
1198 | | |
1199 | | /// Wrapper around `setsockopt` to deal with platform specific timeouts. |
1200 | 0 | pub(crate) fn set_timeout_opt( |
1201 | 0 | fd: RawSocket, |
1202 | 0 | opt: c_int, |
1203 | 0 | val: c_int, |
1204 | 0 | duration: Option<Duration>, |
1205 | 0 | ) -> io::Result<()> { |
1206 | 0 | let duration = into_timeval(duration); |
1207 | 0 | unsafe { setsockopt(fd, opt, val, duration) } |
1208 | 0 | } |
1209 | | |
1210 | 0 | fn into_timeval(duration: Option<Duration>) -> libc::timeval { |
1211 | 0 | match duration { |
1212 | | // https://github.com/rust-lang/libc/issues/1848 |
1213 | | #[cfg_attr(target_env = "musl", allow(deprecated))] |
1214 | 0 | Some(duration) => libc::timeval { |
1215 | 0 | tv_sec: min(duration.as_secs(), libc::time_t::MAX as u64) as libc::time_t, |
1216 | 0 | tv_usec: duration.subsec_micros() as libc::suseconds_t, |
1217 | 0 | }, |
1218 | 0 | None => libc::timeval { |
1219 | 0 | tv_sec: 0, |
1220 | 0 | tv_usec: 0, |
1221 | 0 | }, |
1222 | | } |
1223 | 0 | } |
1224 | | |
1225 | | #[cfg(all( |
1226 | | feature = "all", |
1227 | | not(any(target_os = "haiku", target_os = "openbsd", target_os = "vita")) |
1228 | | ))] |
1229 | 0 | pub(crate) fn tcp_keepalive_time(fd: RawSocket) -> io::Result<Duration> { |
1230 | | unsafe { |
1231 | 0 | getsockopt::<c_int>(fd, IPPROTO_TCP, KEEPALIVE_TIME) |
1232 | 0 | .map(|secs| Duration::from_secs(secs as u64)) |
1233 | | } |
1234 | 0 | } |
1235 | | |
1236 | | #[allow(unused_variables)] |
1237 | 0 | pub(crate) fn set_tcp_keepalive(fd: RawSocket, keepalive: &TcpKeepalive) -> io::Result<()> { |
1238 | | #[cfg(not(any( |
1239 | | target_os = "haiku", |
1240 | | target_os = "openbsd", |
1241 | | target_os = "nto", |
1242 | | target_os = "vita" |
1243 | | )))] |
1244 | 0 | if let Some(time) = keepalive.time { |
1245 | 0 | let secs = into_secs(time); |
1246 | 0 | unsafe { setsockopt(fd, libc::IPPROTO_TCP, KEEPALIVE_TIME, secs)? } |
1247 | 0 | } |
1248 | | |
1249 | | #[cfg(any( |
1250 | | target_os = "aix", |
1251 | | target_os = "android", |
1252 | | target_os = "dragonfly", |
1253 | | target_os = "freebsd", |
1254 | | target_os = "fuchsia", |
1255 | | target_os = "hurd", |
1256 | | target_os = "illumos", |
1257 | | target_os = "ios", |
1258 | | target_os = "visionos", |
1259 | | target_os = "linux", |
1260 | | target_os = "macos", |
1261 | | target_os = "netbsd", |
1262 | | target_os = "tvos", |
1263 | | target_os = "watchos", |
1264 | | target_os = "cygwin", |
1265 | | all(target_os = "wasi", not(target_env = "p1")), |
1266 | | ))] |
1267 | | { |
1268 | 0 | if let Some(interval) = keepalive.interval { |
1269 | 0 | let secs = into_secs(interval); |
1270 | 0 | unsafe { setsockopt(fd, libc::IPPROTO_TCP, libc::TCP_KEEPINTVL, secs)? } |
1271 | 0 | } |
1272 | | |
1273 | 0 | if let Some(retries) = keepalive.retries { |
1274 | 0 | unsafe { setsockopt(fd, libc::IPPROTO_TCP, libc::TCP_KEEPCNT, retries as c_int)? } |
1275 | 0 | } |
1276 | | } |
1277 | | |
1278 | | #[cfg(target_os = "nto")] |
1279 | | if let Some(time) = keepalive.time { |
1280 | | let secs = into_timeval(Some(time)); |
1281 | | unsafe { setsockopt(fd, libc::IPPROTO_TCP, KEEPALIVE_TIME, secs)? } |
1282 | | } |
1283 | | |
1284 | 0 | Ok(()) |
1285 | 0 | } |
1286 | | |
1287 | | #[cfg(not(any( |
1288 | | target_os = "haiku", |
1289 | | target_os = "openbsd", |
1290 | | target_os = "nto", |
1291 | | target_os = "vita" |
1292 | | )))] |
1293 | 0 | fn into_secs(duration: Duration) -> c_int { |
1294 | 0 | min(duration.as_secs(), c_int::MAX as u64) as c_int |
1295 | 0 | } |
1296 | | |
1297 | | /// Get the flags using `cmd`. |
1298 | | #[cfg(not(target_os = "vita"))] |
1299 | 0 | fn fcntl_get(fd: RawSocket, cmd: c_int) -> io::Result<c_int> { |
1300 | 0 | syscall!(fcntl(fd, cmd)) |
1301 | 0 | } |
1302 | | |
1303 | | /// Add `flag` to the current set flags of `F_GETFD`. |
1304 | | #[cfg(not(target_os = "vita"))] |
1305 | 0 | fn fcntl_add(fd: RawSocket, get_cmd: c_int, set_cmd: c_int, flag: c_int) -> io::Result<()> { |
1306 | 0 | let previous = fcntl_get(fd, get_cmd)?; |
1307 | 0 | let new = previous | flag; |
1308 | 0 | if new != previous { |
1309 | 0 | syscall!(fcntl(fd, set_cmd, new)).map(|_| ()) |
1310 | | } else { |
1311 | | // Flag was already set. |
1312 | 0 | Ok(()) |
1313 | | } |
1314 | 0 | } |
1315 | | |
1316 | | /// Remove `flag` to the current set flags of `F_GETFD`. |
1317 | | #[cfg(not(target_os = "vita"))] |
1318 | 0 | fn fcntl_remove(fd: RawSocket, get_cmd: c_int, set_cmd: c_int, flag: c_int) -> io::Result<()> { |
1319 | 0 | let previous = fcntl_get(fd, get_cmd)?; |
1320 | 0 | let new = previous & !flag; |
1321 | 0 | if new != previous { |
1322 | 0 | syscall!(fcntl(fd, set_cmd, new)).map(|_| ()) |
1323 | | } else { |
1324 | | // Flag was already set. |
1325 | 0 | Ok(()) |
1326 | | } |
1327 | 0 | } |
1328 | | |
1329 | | /// Caller must ensure `T` is the correct type for `opt` and `val`. |
1330 | 0 | pub(crate) unsafe fn getsockopt<T>(fd: RawSocket, opt: c_int, val: c_int) -> io::Result<T> { |
1331 | 0 | let mut payload: MaybeUninit<T> = MaybeUninit::uninit(); |
1332 | 0 | let mut len = size_of::<T>() as libc::socklen_t; |
1333 | 0 | syscall!(getsockopt( |
1334 | 0 | fd, |
1335 | 0 | opt, |
1336 | 0 | val, |
1337 | 0 | payload.as_mut_ptr().cast(), |
1338 | 0 | &mut len, |
1339 | | )) |
1340 | 0 | .map(|_| { |
1341 | 0 | debug_assert_eq!(len as usize, size_of::<T>()); |
1342 | | // Safety: `getsockopt` initialised `payload` for us. |
1343 | 0 | payload.assume_init() |
1344 | 0 | }) Unexecuted instantiation: socket2::sys::getsockopt::<libc::unix::linger>::{closure#0}Unexecuted instantiation: socket2::sys::getsockopt::<libc::unix::timeval>::{closure#0}Unexecuted instantiation: socket2::sys::getsockopt::<libc::unix::linux_like::in_addr>::{closure#0}Unexecuted instantiation: socket2::sys::getsockopt::<bool>::{closure#0}Unexecuted instantiation: socket2::sys::getsockopt::<i32>::{closure#0}Unexecuted instantiation: socket2::sys::getsockopt::<u32>::{closure#0}Unexecuted instantiation: socket2::sys::getsockopt::<u64>::{closure#0} |
1345 | 0 | } Unexecuted instantiation: socket2::sys::getsockopt::<libc::unix::linger> Unexecuted instantiation: socket2::sys::getsockopt::<libc::unix::timeval> Unexecuted instantiation: socket2::sys::getsockopt::<libc::unix::linux_like::in_addr> Unexecuted instantiation: socket2::sys::getsockopt::<bool> Unexecuted instantiation: socket2::sys::getsockopt::<i32> Unexecuted instantiation: socket2::sys::getsockopt::<u32> Unexecuted instantiation: socket2::sys::getsockopt::<u64> |
1346 | | |
1347 | | /// Caller must ensure `T` is the correct type for `opt` and `val`. |
1348 | 0 | pub(crate) unsafe fn setsockopt<T>( |
1349 | 0 | fd: RawSocket, |
1350 | 0 | opt: c_int, |
1351 | 0 | val: c_int, |
1352 | 0 | payload: T, |
1353 | 0 | ) -> io::Result<()> { |
1354 | 0 | let payload = ptr::addr_of!(payload).cast(); |
1355 | 0 | syscall!(setsockopt( |
1356 | 0 | fd, |
1357 | 0 | opt, |
1358 | 0 | val, |
1359 | 0 | payload, |
1360 | 0 | mem::size_of::<T>() as libc::socklen_t, |
1361 | | )) |
1362 | 0 | .map(|_| ()) |
1363 | 0 | } Unexecuted instantiation: socket2::sys::setsockopt::<libc::unix::linger> Unexecuted instantiation: socket2::sys::setsockopt::<libc::unix::timeval> Unexecuted instantiation: socket2::sys::setsockopt::<libc::unix::ipv6_mreq> Unexecuted instantiation: socket2::sys::setsockopt::<libc::unix::linux_like::sock_fprog> Unexecuted instantiation: socket2::sys::setsockopt::<libc::unix::linux_like::ip_mreq_source> Unexecuted instantiation: socket2::sys::setsockopt::<libc::unix::linux_like::in_addr> Unexecuted instantiation: socket2::sys::setsockopt::<libc::unix::linux_like::ip_mreq> Unexecuted instantiation: socket2::sys::setsockopt::<libc::unix::linux_like::ip_mreqn> Unexecuted instantiation: socket2::sys::setsockopt::<u8> Unexecuted instantiation: socket2::sys::setsockopt::<i32> Unexecuted instantiation: socket2::sys::setsockopt::<u32> |
1364 | | |
1365 | 0 | pub(crate) const fn to_in_addr(addr: &Ipv4Addr) -> in_addr { |
1366 | | // `s_addr` is stored as BE on all machines, and the array is in BE order. |
1367 | | // So the native endian conversion method is used so that it's never |
1368 | | // swapped. |
1369 | 0 | in_addr { |
1370 | 0 | s_addr: u32::from_ne_bytes(addr.octets()), |
1371 | 0 | } |
1372 | 0 | } |
1373 | | |
1374 | 0 | pub(crate) fn from_in_addr(in_addr: in_addr) -> Ipv4Addr { |
1375 | 0 | Ipv4Addr::from(in_addr.s_addr.to_ne_bytes()) |
1376 | 0 | } |
1377 | | |
1378 | 0 | pub(crate) const fn to_in6_addr(addr: &Ipv6Addr) -> in6_addr { |
1379 | 0 | in6_addr { |
1380 | 0 | s6_addr: addr.octets(), |
1381 | 0 | } |
1382 | 0 | } |
1383 | | |
1384 | 0 | pub(crate) fn from_in6_addr(addr: in6_addr) -> Ipv6Addr { |
1385 | 0 | Ipv6Addr::from(addr.s6_addr) |
1386 | 0 | } |
1387 | | |
1388 | | #[cfg(not(any( |
1389 | | target_os = "aix", |
1390 | | target_os = "haiku", |
1391 | | target_os = "illumos", |
1392 | | target_os = "netbsd", |
1393 | | target_os = "openbsd", |
1394 | | target_os = "redox", |
1395 | | target_os = "solaris", |
1396 | | target_os = "nto", |
1397 | | target_os = "espidf", |
1398 | | target_os = "vita", |
1399 | | target_os = "cygwin", |
1400 | | target_os = "wasi", |
1401 | | )))] |
1402 | 0 | pub(crate) const fn to_mreqn( |
1403 | 0 | multiaddr: &Ipv4Addr, |
1404 | 0 | interface: &crate::socket::InterfaceIndexOrAddress, |
1405 | 0 | ) -> libc::ip_mreqn { |
1406 | 0 | match interface { |
1407 | 0 | crate::socket::InterfaceIndexOrAddress::Index(interface) => libc::ip_mreqn { |
1408 | 0 | imr_multiaddr: to_in_addr(multiaddr), |
1409 | 0 | imr_address: to_in_addr(&Ipv4Addr::UNSPECIFIED), |
1410 | 0 | imr_ifindex: *interface as _, |
1411 | 0 | }, |
1412 | 0 | crate::socket::InterfaceIndexOrAddress::Address(interface) => libc::ip_mreqn { |
1413 | 0 | imr_multiaddr: to_in_addr(multiaddr), |
1414 | 0 | imr_address: to_in_addr(interface), |
1415 | 0 | imr_ifindex: 0, |
1416 | 0 | }, |
1417 | | } |
1418 | 0 | } |
1419 | | |
1420 | | #[cfg(all( |
1421 | | feature = "all", |
1422 | | any(target_os = "android", target_os = "fuchsia", target_os = "linux") |
1423 | | ))] |
1424 | 0 | pub(crate) fn original_dst_v4(fd: RawSocket) -> io::Result<SockAddr> { |
1425 | | // Safety: `getsockopt` initialises the `SockAddr` for us. |
1426 | | unsafe { |
1427 | 0 | SockAddr::try_init(|storage, len| { |
1428 | 0 | syscall!(getsockopt( |
1429 | 0 | fd, |
1430 | | libc::SOL_IP, |
1431 | | libc::SO_ORIGINAL_DST, |
1432 | 0 | storage.cast(), |
1433 | 0 | len |
1434 | | )) |
1435 | 0 | }) |
1436 | | } |
1437 | 0 | .map(|(_, addr)| addr) |
1438 | 0 | } |
1439 | | |
1440 | | /// Get the value for the `IP6T_SO_ORIGINAL_DST` option on this socket. |
1441 | | /// |
1442 | | /// This value contains the original destination IPv6 address of the connection |
1443 | | /// redirected using `ip6tables` `REDIRECT` or `TPROXY`. |
1444 | | #[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))] |
1445 | 0 | pub(crate) fn original_dst_v6(fd: RawSocket) -> io::Result<SockAddr> { |
1446 | | // Safety: `getsockopt` initialises the `SockAddr` for us. |
1447 | | unsafe { |
1448 | 0 | SockAddr::try_init(|storage, len| { |
1449 | 0 | syscall!(getsockopt( |
1450 | 0 | fd, |
1451 | | libc::SOL_IPV6, |
1452 | | libc::IP6T_SO_ORIGINAL_DST, |
1453 | 0 | storage.cast(), |
1454 | 0 | len |
1455 | | )) |
1456 | 0 | }) |
1457 | | } |
1458 | 0 | .map(|(_, addr)| addr) |
1459 | 0 | } |
1460 | | |
1461 | | /// Unix only API. |
1462 | | impl crate::Socket { |
1463 | | /// Accept a new incoming connection from this listener. |
1464 | | /// |
1465 | | /// This function directly corresponds to the `accept4(2)` function. |
1466 | | /// |
1467 | | /// This function will block the calling thread until a new connection is |
1468 | | /// established. When established, the corresponding `Socket` and the remote |
1469 | | /// peer's address will be returned. |
1470 | | #[doc = man_links!(unix: accept4(2))] |
1471 | | #[cfg(all( |
1472 | | feature = "all", |
1473 | | any( |
1474 | | target_os = "android", |
1475 | | target_os = "dragonfly", |
1476 | | target_os = "freebsd", |
1477 | | target_os = "fuchsia", |
1478 | | target_os = "illumos", |
1479 | | target_os = "linux", |
1480 | | target_os = "netbsd", |
1481 | | target_os = "openbsd", |
1482 | | target_os = "cygwin", |
1483 | | ) |
1484 | | ))] |
1485 | 0 | pub fn accept4(&self, flags: c_int) -> io::Result<(crate::Socket, SockAddr)> { |
1486 | 0 | self._accept4(flags) |
1487 | 0 | } |
1488 | | |
1489 | | #[cfg(any( |
1490 | | target_os = "android", |
1491 | | target_os = "dragonfly", |
1492 | | target_os = "freebsd", |
1493 | | target_os = "fuchsia", |
1494 | | target_os = "illumos", |
1495 | | target_os = "linux", |
1496 | | target_os = "netbsd", |
1497 | | target_os = "openbsd", |
1498 | | target_os = "cygwin", |
1499 | | ))] |
1500 | 0 | pub(crate) fn _accept4(&self, flags: c_int) -> io::Result<(crate::Socket, SockAddr)> { |
1501 | | // Safety: `accept4` initialises the `SockAddr` for us. |
1502 | | unsafe { |
1503 | 0 | SockAddr::try_init(|storage, len| { |
1504 | 0 | syscall!(accept4(self.as_raw(), storage.cast(), len, flags)) |
1505 | 0 | .map(crate::Socket::from_raw) |
1506 | 0 | }) |
1507 | | } |
1508 | 0 | } |
1509 | | |
1510 | | /// Sets `CLOEXEC` on the socket. |
1511 | | /// |
1512 | | /// # Notes |
1513 | | /// |
1514 | | /// On supported platforms you can use [`Type::cloexec`]. |
1515 | | #[cfg_attr( |
1516 | | any( |
1517 | | target_os = "ios", |
1518 | | target_os = "visionos", |
1519 | | target_os = "macos", |
1520 | | target_os = "tvos", |
1521 | | target_os = "watchos", |
1522 | | target_os = "wasi", |
1523 | | ), |
1524 | | allow(rustdoc::broken_intra_doc_links) |
1525 | | )] |
1526 | | #[cfg(all(feature = "all", not(target_os = "vita")))] |
1527 | 0 | pub fn set_cloexec(&self, close_on_exec: bool) -> io::Result<()> { |
1528 | 0 | self._set_cloexec(close_on_exec) |
1529 | 0 | } |
1530 | | |
1531 | | #[cfg(not(target_os = "vita"))] |
1532 | 0 | pub(crate) fn _set_cloexec(&self, close_on_exec: bool) -> io::Result<()> { |
1533 | 0 | if close_on_exec { |
1534 | 0 | fcntl_add( |
1535 | 0 | self.as_raw(), |
1536 | | libc::F_GETFD, |
1537 | | libc::F_SETFD, |
1538 | | libc::FD_CLOEXEC, |
1539 | | ) |
1540 | | } else { |
1541 | 0 | fcntl_remove( |
1542 | 0 | self.as_raw(), |
1543 | | libc::F_GETFD, |
1544 | | libc::F_SETFD, |
1545 | | libc::FD_CLOEXEC, |
1546 | | ) |
1547 | | } |
1548 | 0 | } |
1549 | | |
1550 | | /// Sets `SO_PEERCRED` to null on the socket. |
1551 | | /// |
1552 | | /// This is a Cygwin extension. |
1553 | | /// |
1554 | | /// Normally the Unix domain sockets of Cygwin are implemented by TCP sockets, |
1555 | | /// so it performs a handshake on `connect` and `accept` to verify the remote |
1556 | | /// connection and exchange peer cred info. At the time of writing, this |
1557 | | /// means that `connect` on a Unix domain socket will block until the server |
1558 | | /// calls `accept` on Cygwin. This behavior is inconsistent with most other |
1559 | | /// platforms, and this option can be used to disable that. |
1560 | | /// |
1561 | | /// See also: the [mailing list](https://inbox.sourceware.org/cygwin/TYCPR01MB10926FF8926CA63704867ADC8F8AA2@TYCPR01MB10926.jpnprd01.prod.outlook.com/) |
1562 | | #[cfg(target_os = "cygwin")] |
1563 | | #[cfg(any(doc, target_os = "cygwin"))] |
1564 | | pub fn set_no_peercred(&self) -> io::Result<()> { |
1565 | | syscall!(setsockopt( |
1566 | | self.as_raw(), |
1567 | | libc::SOL_SOCKET, |
1568 | | libc::SO_PEERCRED, |
1569 | | ptr::null_mut(), |
1570 | | 0, |
1571 | | )) |
1572 | | .map(|_| ()) |
1573 | | } |
1574 | | |
1575 | | /// Sets `SO_NOSIGPIPE` on the socket. |
1576 | | #[cfg(all( |
1577 | | feature = "all", |
1578 | | any( |
1579 | | target_os = "ios", |
1580 | | target_os = "visionos", |
1581 | | target_os = "macos", |
1582 | | target_os = "tvos", |
1583 | | target_os = "watchos", |
1584 | | ) |
1585 | | ))] |
1586 | | pub fn set_nosigpipe(&self, nosigpipe: bool) -> io::Result<()> { |
1587 | | self._set_nosigpipe(nosigpipe) |
1588 | | } |
1589 | | |
1590 | | #[cfg(any( |
1591 | | target_os = "ios", |
1592 | | target_os = "visionos", |
1593 | | target_os = "macos", |
1594 | | target_os = "tvos", |
1595 | | target_os = "watchos", |
1596 | | ))] |
1597 | | pub(crate) fn _set_nosigpipe(&self, nosigpipe: bool) -> io::Result<()> { |
1598 | | unsafe { |
1599 | | setsockopt( |
1600 | | self.as_raw(), |
1601 | | libc::SOL_SOCKET, |
1602 | | libc::SO_NOSIGPIPE, |
1603 | | nosigpipe as c_int, |
1604 | | ) |
1605 | | } |
1606 | | } |
1607 | | |
1608 | | /// Gets the value of the `TCP_MAXSEG` option on this socket. |
1609 | | /// |
1610 | | /// For more information about this option, see [`set_tcp_mss`]. |
1611 | | /// |
1612 | | /// [`set_tcp_mss`]: crate::Socket::set_tcp_mss |
1613 | | #[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "wasi"))))] |
1614 | 0 | pub fn tcp_mss(&self) -> io::Result<u32> { |
1615 | | unsafe { |
1616 | 0 | getsockopt::<c_int>(self.as_raw(), libc::IPPROTO_TCP, libc::TCP_MAXSEG) |
1617 | 0 | .map(|mss| mss as u32) |
1618 | | } |
1619 | 0 | } |
1620 | | |
1621 | | /// Sets the value of the `TCP_MAXSEG` option on this socket. |
1622 | | /// |
1623 | | /// The `TCP_MAXSEG` option denotes the TCP Maximum Segment Size and is only |
1624 | | /// available on TCP sockets. |
1625 | | #[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "wasi"))))] |
1626 | 0 | pub fn set_tcp_mss(&self, mss: u32) -> io::Result<()> { |
1627 | | unsafe { |
1628 | 0 | setsockopt( |
1629 | 0 | self.as_raw(), |
1630 | | libc::IPPROTO_TCP, |
1631 | | libc::TCP_MAXSEG, |
1632 | 0 | mss as c_int, |
1633 | | ) |
1634 | | } |
1635 | 0 | } |
1636 | | |
1637 | | /// Returns `true` if `listen(2)` was called on this socket by checking the |
1638 | | /// `SO_ACCEPTCONN` option on this socket. |
1639 | | #[cfg(all( |
1640 | | feature = "all", |
1641 | | any( |
1642 | | target_os = "aix", |
1643 | | target_os = "android", |
1644 | | target_os = "freebsd", |
1645 | | target_os = "fuchsia", |
1646 | | target_os = "linux", |
1647 | | target_os = "cygwin", |
1648 | | ) |
1649 | | ))] |
1650 | 0 | pub fn is_listener(&self) -> io::Result<bool> { |
1651 | | unsafe { |
1652 | 0 | getsockopt::<c_int>(self.as_raw(), libc::SOL_SOCKET, libc::SO_ACCEPTCONN) |
1653 | 0 | .map(|v| v != 0) |
1654 | | } |
1655 | 0 | } |
1656 | | |
1657 | | /// Returns the [`Domain`] of this socket by checking the `SO_DOMAIN` option |
1658 | | /// on this socket. |
1659 | | #[cfg(all( |
1660 | | feature = "all", |
1661 | | any( |
1662 | | target_os = "android", |
1663 | | // TODO: add FreeBSD. |
1664 | | // target_os = "freebsd", |
1665 | | target_os = "fuchsia", |
1666 | | target_os = "linux", |
1667 | | ) |
1668 | | ))] |
1669 | 0 | pub fn domain(&self) -> io::Result<Domain> { |
1670 | 0 | unsafe { getsockopt::<c_int>(self.as_raw(), libc::SOL_SOCKET, libc::SO_DOMAIN).map(Domain) } |
1671 | 0 | } |
1672 | | |
1673 | | /// Returns the [`Protocol`] of this socket by checking the `SO_PROTOCOL` |
1674 | | /// option on this socket. |
1675 | | #[cfg(all( |
1676 | | feature = "all", |
1677 | | any( |
1678 | | target_os = "android", |
1679 | | target_os = "freebsd", |
1680 | | target_os = "fuchsia", |
1681 | | target_os = "linux", |
1682 | | ) |
1683 | | ))] |
1684 | 0 | pub fn protocol(&self) -> io::Result<Option<Protocol>> { |
1685 | | unsafe { |
1686 | 0 | getsockopt::<c_int>(self.as_raw(), libc::SOL_SOCKET, libc::SO_PROTOCOL).map(|v| match v |
1687 | | { |
1688 | 0 | 0 => None, |
1689 | 0 | p => Some(Protocol(p)), |
1690 | 0 | }) |
1691 | | } |
1692 | 0 | } |
1693 | | |
1694 | | /// Gets the value for the `SO_MARK` option on this socket. |
1695 | | /// |
1696 | | /// This value gets the socket mark field for each packet sent through |
1697 | | /// this socket. |
1698 | | /// |
1699 | | /// On Linux this function requires the `CAP_NET_ADMIN` capability. |
1700 | | #[cfg(all( |
1701 | | feature = "all", |
1702 | | any(target_os = "android", target_os = "fuchsia", target_os = "linux") |
1703 | | ))] |
1704 | 0 | pub fn mark(&self) -> io::Result<u32> { |
1705 | | unsafe { |
1706 | 0 | getsockopt::<c_int>(self.as_raw(), libc::SOL_SOCKET, libc::SO_MARK) |
1707 | 0 | .map(|mark| mark as u32) |
1708 | | } |
1709 | 0 | } |
1710 | | |
1711 | | /// Sets the value for the `SO_MARK` option on this socket. |
1712 | | /// |
1713 | | /// This value sets the socket mark field for each packet sent through |
1714 | | /// this socket. Changing the mark can be used for mark-based routing |
1715 | | /// without netfilter or for packet filtering. |
1716 | | /// |
1717 | | /// On Linux this function requires the `CAP_NET_ADMIN` capability. |
1718 | | #[cfg(all( |
1719 | | feature = "all", |
1720 | | any(target_os = "android", target_os = "fuchsia", target_os = "linux") |
1721 | | ))] |
1722 | 0 | pub fn set_mark(&self, mark: u32) -> io::Result<()> { |
1723 | | unsafe { |
1724 | 0 | setsockopt::<c_int>( |
1725 | 0 | self.as_raw(), |
1726 | | libc::SOL_SOCKET, |
1727 | | libc::SO_MARK, |
1728 | 0 | mark as c_int, |
1729 | | ) |
1730 | | } |
1731 | 0 | } |
1732 | | |
1733 | | /// Get the value of the `TCP_CORK` option on this socket. |
1734 | | /// |
1735 | | /// For more information about this option, see [`set_tcp_cork`]. |
1736 | | /// |
1737 | | /// [`set_tcp_cork`]: crate::Socket::set_tcp_cork |
1738 | | #[cfg(all( |
1739 | | feature = "all", |
1740 | | any(target_os = "android", target_os = "fuchsia", target_os = "linux") |
1741 | | ))] |
1742 | 0 | pub fn tcp_cork(&self) -> io::Result<bool> { |
1743 | | unsafe { |
1744 | 0 | getsockopt::<Bool>(self.as_raw(), libc::IPPROTO_TCP, libc::TCP_CORK) |
1745 | 0 | .map(|cork| cork != 0) |
1746 | | } |
1747 | 0 | } |
1748 | | |
1749 | | /// Set the value of the `TCP_CORK` option on this socket. |
1750 | | /// |
1751 | | /// If set, don't send out partial frames. All queued partial frames are |
1752 | | /// sent when the option is cleared again. There is a 200 millisecond ceiling on |
1753 | | /// the time for which output is corked by `TCP_CORK`. If this ceiling is reached, |
1754 | | /// then queued data is automatically transmitted. |
1755 | | #[cfg(all( |
1756 | | feature = "all", |
1757 | | any(target_os = "android", target_os = "fuchsia", target_os = "linux") |
1758 | | ))] |
1759 | 0 | pub fn set_tcp_cork(&self, cork: bool) -> io::Result<()> { |
1760 | | unsafe { |
1761 | 0 | setsockopt( |
1762 | 0 | self.as_raw(), |
1763 | | libc::IPPROTO_TCP, |
1764 | | libc::TCP_CORK, |
1765 | 0 | cork as c_int, |
1766 | | ) |
1767 | | } |
1768 | 0 | } |
1769 | | |
1770 | | /// Get the value of the `TCP_QUICKACK` option on this socket. |
1771 | | /// |
1772 | | /// For more information about this option, see [`set_tcp_quickack`]. |
1773 | | /// |
1774 | | /// [`set_tcp_quickack`]: crate::Socket::set_tcp_quickack |
1775 | | #[cfg(all( |
1776 | | feature = "all", |
1777 | | any( |
1778 | | target_os = "android", |
1779 | | target_os = "fuchsia", |
1780 | | target_os = "linux", |
1781 | | target_os = "cygwin", |
1782 | | ) |
1783 | | ))] |
1784 | 0 | pub fn tcp_quickack(&self) -> io::Result<bool> { |
1785 | | unsafe { |
1786 | 0 | getsockopt::<Bool>(self.as_raw(), libc::IPPROTO_TCP, libc::TCP_QUICKACK) |
1787 | 0 | .map(|quickack| quickack != 0) |
1788 | | } |
1789 | 0 | } |
1790 | | |
1791 | | /// Set the value of the `TCP_QUICKACK` option on this socket. |
1792 | | /// |
1793 | | /// If set, acks are sent immediately, rather than delayed if needed in accordance to normal |
1794 | | /// TCP operation. This flag is not permanent, it only enables a switch to or from quickack mode. |
1795 | | /// Subsequent operation of the TCP protocol will once again enter/leave quickack mode depending on |
1796 | | /// internal protocol processing and factors such as delayed ack timeouts occurring and data transfer. |
1797 | | #[cfg(all( |
1798 | | feature = "all", |
1799 | | any( |
1800 | | target_os = "android", |
1801 | | target_os = "fuchsia", |
1802 | | target_os = "linux", |
1803 | | target_os = "cygwin", |
1804 | | ) |
1805 | | ))] |
1806 | 0 | pub fn set_tcp_quickack(&self, quickack: bool) -> io::Result<()> { |
1807 | | unsafe { |
1808 | 0 | setsockopt( |
1809 | 0 | self.as_raw(), |
1810 | | libc::IPPROTO_TCP, |
1811 | | libc::TCP_QUICKACK, |
1812 | 0 | quickack as c_int, |
1813 | | ) |
1814 | | } |
1815 | 0 | } |
1816 | | |
1817 | | /// Get the value of the `TCP_THIN_LINEAR_TIMEOUTS` option on this socket. |
1818 | | /// |
1819 | | /// For more information about this option, see [`set_tcp_thin_linear_timeouts`]. |
1820 | | /// |
1821 | | /// [`set_tcp_thin_linear_timeouts`]: crate::Socket::set_tcp_thin_linear_timeouts |
1822 | | #[cfg(all( |
1823 | | feature = "all", |
1824 | | any(target_os = "android", target_os = "fuchsia", target_os = "linux") |
1825 | | ))] |
1826 | 0 | pub fn tcp_thin_linear_timeouts(&self) -> io::Result<bool> { |
1827 | | unsafe { |
1828 | 0 | getsockopt::<Bool>( |
1829 | 0 | self.as_raw(), |
1830 | | libc::IPPROTO_TCP, |
1831 | | libc::TCP_THIN_LINEAR_TIMEOUTS, |
1832 | | ) |
1833 | 0 | .map(|timeouts| timeouts != 0) |
1834 | | } |
1835 | 0 | } |
1836 | | |
1837 | | /// Set the value of the `TCP_THIN_LINEAR_TIMEOUTS` option on this socket. |
1838 | | /// |
1839 | | /// If set, the kernel will dynamically detect a thin-stream connection if there are less than four packets in flight. |
1840 | | /// With less than four packets in flight the normal TCP fast retransmission will not be effective. |
1841 | | /// The kernel will modify the retransmission to avoid the very high latencies that thin stream suffer because of exponential backoff. |
1842 | | #[cfg(all( |
1843 | | feature = "all", |
1844 | | any(target_os = "android", target_os = "fuchsia", target_os = "linux") |
1845 | | ))] |
1846 | 0 | pub fn set_tcp_thin_linear_timeouts(&self, timeouts: bool) -> io::Result<()> { |
1847 | | unsafe { |
1848 | 0 | setsockopt( |
1849 | 0 | self.as_raw(), |
1850 | | libc::IPPROTO_TCP, |
1851 | | libc::TCP_THIN_LINEAR_TIMEOUTS, |
1852 | 0 | timeouts as c_int, |
1853 | | ) |
1854 | | } |
1855 | 0 | } |
1856 | | |
1857 | | /// Get the value of the `TCP_NOTSENT_LOWAT` option on this socket. |
1858 | | /// |
1859 | | /// For more information about this option, see [`set_tcp_notsent_lowat`]. |
1860 | | /// |
1861 | | /// [`set_tcp_notsent_lowat`]: crate::Socket::set_tcp_notsent_lowat |
1862 | | #[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))] |
1863 | 0 | pub fn tcp_notsent_lowat(&self) -> io::Result<u32> { |
1864 | | unsafe { |
1865 | 0 | getsockopt::<c_int>(self.as_raw(), libc::IPPROTO_TCP, libc::TCP_NOTSENT_LOWAT) |
1866 | 0 | .map(|lowat| lowat as u32) |
1867 | | } |
1868 | 0 | } |
1869 | | |
1870 | | /// Set the value of the `TCP_NOTSENT_LOWAT` option on this socket. |
1871 | | /// |
1872 | | /// If set the kernel will limit the amount of _unsent_ data in the sendbuffer. |
1873 | | /// This differs from `set_send_buffer_size` which limits the sum of unsent and unacknowledged data. |
1874 | | #[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))] |
1875 | 0 | pub fn set_tcp_notsent_lowat(&self, lowat: u32) -> io::Result<()> { |
1876 | | unsafe { |
1877 | 0 | setsockopt( |
1878 | 0 | self.as_raw(), |
1879 | | libc::IPPROTO_TCP, |
1880 | | libc::TCP_NOTSENT_LOWAT, |
1881 | 0 | lowat as c_int, |
1882 | | ) |
1883 | | } |
1884 | 0 | } |
1885 | | |
1886 | | /// Gets the value for the `SO_BINDTODEVICE` option on this socket. |
1887 | | /// |
1888 | | /// This value gets the socket binded device's interface name. |
1889 | | #[cfg(all( |
1890 | | feature = "all", |
1891 | | any(target_os = "android", target_os = "fuchsia", target_os = "linux") |
1892 | | ))] |
1893 | 0 | pub fn device(&self) -> io::Result<Option<Vec<u8>>> { |
1894 | | // TODO: replace with `MaybeUninit::uninit_array` once stable. |
1895 | 0 | let mut buf: [MaybeUninit<u8>; libc::IFNAMSIZ] = |
1896 | 0 | unsafe { MaybeUninit::uninit().assume_init() }; |
1897 | 0 | let mut len = buf.len() as libc::socklen_t; |
1898 | 0 | syscall!(getsockopt( |
1899 | 0 | self.as_raw(), |
1900 | | libc::SOL_SOCKET, |
1901 | | libc::SO_BINDTODEVICE, |
1902 | 0 | buf.as_mut_ptr().cast(), |
1903 | 0 | &mut len, |
1904 | 0 | ))?; |
1905 | 0 | if len == 0 { |
1906 | 0 | Ok(None) |
1907 | | } else { |
1908 | 0 | let buf = &buf[..len as usize - 1]; |
1909 | | // TODO: use `MaybeUninit::slice_assume_init_ref` once stable. |
1910 | 0 | Ok(Some(unsafe { &*(buf as *const [_] as *const [u8]) }.into())) |
1911 | | } |
1912 | 0 | } |
1913 | | |
1914 | | /// Sets the value for the `SO_BINDTODEVICE` option on this socket. |
1915 | | /// |
1916 | | /// If a socket is bound to an interface, only packets received from that |
1917 | | /// particular interface are processed by the socket. Note that this only |
1918 | | /// works for some socket types, particularly `AF_INET` sockets. |
1919 | | /// |
1920 | | /// If `interface` is `None` or an empty string it removes the binding. |
1921 | | #[cfg(all( |
1922 | | feature = "all", |
1923 | | any(target_os = "android", target_os = "fuchsia", target_os = "linux") |
1924 | | ))] |
1925 | 0 | pub fn bind_device(&self, interface: Option<&[u8]>) -> io::Result<()> { |
1926 | 0 | let (value, len) = if let Some(interface) = interface { |
1927 | 0 | (interface.as_ptr(), interface.len()) |
1928 | | } else { |
1929 | 0 | (ptr::null(), 0) |
1930 | | }; |
1931 | 0 | syscall!(setsockopt( |
1932 | 0 | self.as_raw(), |
1933 | | libc::SOL_SOCKET, |
1934 | | libc::SO_BINDTODEVICE, |
1935 | 0 | value.cast(), |
1936 | 0 | len as libc::socklen_t, |
1937 | | )) |
1938 | 0 | .map(|_| ()) |
1939 | 0 | } |
1940 | | |
1941 | | /// Sets the value for the `SO_SETFIB` option on this socket. |
1942 | | /// |
1943 | | /// Bind socket to the specified forwarding table (VRF) on a FreeBSD. |
1944 | | #[cfg(all(feature = "all", target_os = "freebsd"))] |
1945 | | pub fn set_fib(&self, fib: u32) -> io::Result<()> { |
1946 | | syscall!(setsockopt( |
1947 | | self.as_raw(), |
1948 | | libc::SOL_SOCKET, |
1949 | | libc::SO_SETFIB, |
1950 | | (&fib as *const u32).cast(), |
1951 | | mem::size_of::<u32>() as libc::socklen_t, |
1952 | | )) |
1953 | | .map(|_| ()) |
1954 | | } |
1955 | | |
1956 | | /// Sets the value for `IP_BOUND_IF` or `SO_BINDTOIFINDEX` option on this socket. |
1957 | | /// |
1958 | | /// If a socket is bound to an interface, only packets received from that |
1959 | | /// particular interface are processed by the socket. |
1960 | | /// |
1961 | | /// If `interface` is `None`, the binding is removed. If the `interface` |
1962 | | /// index is not valid, an error is returned. |
1963 | | /// |
1964 | | /// One can use [`libc::if_nametoindex`] to convert an interface alias to an |
1965 | | /// index. |
1966 | | #[cfg(all( |
1967 | | feature = "all", |
1968 | | any( |
1969 | | target_os = "ios", |
1970 | | target_os = "visionos", |
1971 | | target_os = "macos", |
1972 | | target_os = "tvos", |
1973 | | target_os = "watchos", |
1974 | | target_os = "illumos", |
1975 | | target_os = "solaris", |
1976 | | target_os = "linux", |
1977 | | target_os = "android", |
1978 | | ) |
1979 | | ))] |
1980 | 0 | pub fn bind_device_by_index_v4(&self, interface: Option<NonZeroU32>) -> io::Result<()> { |
1981 | 0 | let index = interface.map_or(0, NonZeroU32::get); |
1982 | | |
1983 | | #[cfg(any( |
1984 | | target_os = "ios", |
1985 | | target_os = "visionos", |
1986 | | target_os = "macos", |
1987 | | target_os = "tvos", |
1988 | | target_os = "watchos", |
1989 | | target_os = "illumos", |
1990 | | target_os = "solaris", |
1991 | | ))] |
1992 | | unsafe { |
1993 | | setsockopt(self.as_raw(), IPPROTO_IP, libc::IP_BOUND_IF, index) |
1994 | | } |
1995 | | |
1996 | | #[cfg(any(target_os = "linux", target_os = "android",))] |
1997 | | unsafe { |
1998 | 0 | setsockopt( |
1999 | 0 | self.as_raw(), |
2000 | | libc::SOL_SOCKET, |
2001 | | libc::SO_BINDTOIFINDEX, |
2002 | 0 | index, |
2003 | | ) |
2004 | | } |
2005 | 0 | } |
2006 | | |
2007 | | /// Sets the value for `IPV6_BOUND_IF` or `SO_BINDTOIFINDEX` option on this socket. |
2008 | | /// |
2009 | | /// If a socket is bound to an interface, only packets received from that |
2010 | | /// particular interface are processed by the socket. |
2011 | | /// |
2012 | | /// If `interface` is `None`, the binding is removed. If the `interface` |
2013 | | /// index is not valid, an error is returned. |
2014 | | /// |
2015 | | /// One can use [`libc::if_nametoindex`] to convert an interface alias to an |
2016 | | /// index. |
2017 | | #[cfg(all( |
2018 | | feature = "all", |
2019 | | any( |
2020 | | target_os = "ios", |
2021 | | target_os = "visionos", |
2022 | | target_os = "macos", |
2023 | | target_os = "tvos", |
2024 | | target_os = "watchos", |
2025 | | target_os = "illumos", |
2026 | | target_os = "solaris", |
2027 | | target_os = "linux", |
2028 | | target_os = "android", |
2029 | | ) |
2030 | | ))] |
2031 | 0 | pub fn bind_device_by_index_v6(&self, interface: Option<NonZeroU32>) -> io::Result<()> { |
2032 | 0 | let index = interface.map_or(0, NonZeroU32::get); |
2033 | | |
2034 | | #[cfg(any( |
2035 | | target_os = "ios", |
2036 | | target_os = "visionos", |
2037 | | target_os = "macos", |
2038 | | target_os = "tvos", |
2039 | | target_os = "watchos", |
2040 | | target_os = "illumos", |
2041 | | target_os = "solaris", |
2042 | | ))] |
2043 | | unsafe { |
2044 | | setsockopt(self.as_raw(), IPPROTO_IPV6, libc::IPV6_BOUND_IF, index) |
2045 | | } |
2046 | | |
2047 | | #[cfg(any(target_os = "linux", target_os = "android",))] |
2048 | | unsafe { |
2049 | 0 | setsockopt( |
2050 | 0 | self.as_raw(), |
2051 | | libc::SOL_SOCKET, |
2052 | | libc::SO_BINDTOIFINDEX, |
2053 | 0 | index, |
2054 | | ) |
2055 | | } |
2056 | 0 | } |
2057 | | |
2058 | | /// Gets the value for `IP_BOUND_IF` or `SO_BINDTOIFINDEX` option on this |
2059 | | /// socket, i.e. the index for the interface to which the socket is bound. |
2060 | | /// |
2061 | | /// Returns `None` if the socket is not bound to any interface, otherwise |
2062 | | /// returns an interface index. |
2063 | | #[cfg(all( |
2064 | | feature = "all", |
2065 | | any( |
2066 | | target_os = "ios", |
2067 | | target_os = "visionos", |
2068 | | target_os = "macos", |
2069 | | target_os = "tvos", |
2070 | | target_os = "watchos", |
2071 | | target_os = "illumos", |
2072 | | target_os = "solaris", |
2073 | | target_os = "linux", |
2074 | | target_os = "android", |
2075 | | ) |
2076 | | ))] |
2077 | 0 | pub fn device_index_v4(&self) -> io::Result<Option<NonZeroU32>> { |
2078 | | #[cfg(any( |
2079 | | target_os = "ios", |
2080 | | target_os = "visionos", |
2081 | | target_os = "macos", |
2082 | | target_os = "tvos", |
2083 | | target_os = "watchos", |
2084 | | target_os = "illumos", |
2085 | | target_os = "solaris", |
2086 | | ))] |
2087 | | let index = |
2088 | | unsafe { getsockopt::<libc::c_uint>(self.as_raw(), IPPROTO_IP, libc::IP_BOUND_IF)? }; |
2089 | | |
2090 | | #[cfg(any(target_os = "linux", target_os = "android",))] |
2091 | 0 | let index = unsafe { |
2092 | 0 | getsockopt::<libc::c_uint>(self.as_raw(), libc::SOL_SOCKET, libc::SO_BINDTOIFINDEX)? |
2093 | | }; |
2094 | | |
2095 | 0 | Ok(NonZeroU32::new(index)) |
2096 | 0 | } |
2097 | | |
2098 | | /// Gets the value for `IPV6_BOUND_IF` or `SO_BINDTOIFINDEX` option on this |
2099 | | /// socket, i.e. the index for the interface to which the socket is bound. |
2100 | | /// |
2101 | | /// Returns `None` if the socket is not bound to any interface, otherwise |
2102 | | /// returns an interface index. |
2103 | | #[cfg(all( |
2104 | | feature = "all", |
2105 | | any( |
2106 | | target_os = "ios", |
2107 | | target_os = "visionos", |
2108 | | target_os = "macos", |
2109 | | target_os = "tvos", |
2110 | | target_os = "watchos", |
2111 | | target_os = "illumos", |
2112 | | target_os = "solaris", |
2113 | | target_os = "linux", |
2114 | | target_os = "android", |
2115 | | ) |
2116 | | ))] |
2117 | 0 | pub fn device_index_v6(&self) -> io::Result<Option<NonZeroU32>> { |
2118 | | #[cfg(any( |
2119 | | target_os = "ios", |
2120 | | target_os = "visionos", |
2121 | | target_os = "macos", |
2122 | | target_os = "tvos", |
2123 | | target_os = "watchos", |
2124 | | target_os = "illumos", |
2125 | | target_os = "solaris", |
2126 | | ))] |
2127 | | let index = unsafe { |
2128 | | getsockopt::<libc::c_uint>(self.as_raw(), IPPROTO_IPV6, libc::IPV6_BOUND_IF)? |
2129 | | }; |
2130 | | |
2131 | | #[cfg(any(target_os = "linux", target_os = "android",))] |
2132 | 0 | let index = unsafe { |
2133 | 0 | getsockopt::<libc::c_uint>(self.as_raw(), libc::SOL_SOCKET, libc::SO_BINDTOIFINDEX)? |
2134 | | }; |
2135 | | |
2136 | 0 | Ok(NonZeroU32::new(index)) |
2137 | 0 | } |
2138 | | |
2139 | | /// Get the value of the `SO_INCOMING_CPU` option on this socket. |
2140 | | /// |
2141 | | /// For more information about this option, see [`set_cpu_affinity`]. |
2142 | | /// |
2143 | | /// [`set_cpu_affinity`]: crate::Socket::set_cpu_affinity |
2144 | | #[cfg(all(feature = "all", target_os = "linux"))] |
2145 | 0 | pub fn cpu_affinity(&self) -> io::Result<usize> { |
2146 | | unsafe { |
2147 | 0 | getsockopt::<c_int>(self.as_raw(), libc::SOL_SOCKET, libc::SO_INCOMING_CPU) |
2148 | 0 | .map(|cpu| cpu as usize) |
2149 | | } |
2150 | 0 | } |
2151 | | |
2152 | | /// Set value for the `SO_INCOMING_CPU` option on this socket. |
2153 | | /// |
2154 | | /// Sets the CPU affinity of the socket. |
2155 | | #[cfg(all(feature = "all", target_os = "linux"))] |
2156 | 0 | pub fn set_cpu_affinity(&self, cpu: usize) -> io::Result<()> { |
2157 | | unsafe { |
2158 | 0 | setsockopt( |
2159 | 0 | self.as_raw(), |
2160 | | libc::SOL_SOCKET, |
2161 | | libc::SO_INCOMING_CPU, |
2162 | 0 | cpu as c_int, |
2163 | | ) |
2164 | | } |
2165 | 0 | } |
2166 | | |
2167 | | /// Get the value of the `SO_REUSEPORT` option on this socket. |
2168 | | /// |
2169 | | /// For more information about this option, see [`set_reuse_port`]. |
2170 | | /// |
2171 | | /// [`set_reuse_port`]: crate::Socket::set_reuse_port |
2172 | | #[cfg(all( |
2173 | | feature = "all", |
2174 | | not(any( |
2175 | | target_os = "solaris", |
2176 | | target_os = "illumos", |
2177 | | target_os = "cygwin", |
2178 | | target_os = "wasi" |
2179 | | )) |
2180 | | ))] |
2181 | 0 | pub fn reuse_port(&self) -> io::Result<bool> { |
2182 | | unsafe { |
2183 | 0 | getsockopt::<c_int>(self.as_raw(), libc::SOL_SOCKET, libc::SO_REUSEPORT) |
2184 | 0 | .map(|reuse| reuse != 0) |
2185 | | } |
2186 | 0 | } |
2187 | | |
2188 | | /// Set value for the `SO_REUSEPORT` option on this socket. |
2189 | | /// |
2190 | | /// This indicates that further calls to `bind` may allow reuse of local |
2191 | | /// addresses. For IPv4 sockets this means that a socket may bind even when |
2192 | | /// there's a socket already listening on this port. |
2193 | | #[cfg(all( |
2194 | | feature = "all", |
2195 | | not(any( |
2196 | | target_os = "solaris", |
2197 | | target_os = "illumos", |
2198 | | target_os = "cygwin", |
2199 | | target_os = "wasi" |
2200 | | )) |
2201 | | ))] |
2202 | 0 | pub fn set_reuse_port(&self, reuse: bool) -> io::Result<()> { |
2203 | | unsafe { |
2204 | 0 | setsockopt( |
2205 | 0 | self.as_raw(), |
2206 | | libc::SOL_SOCKET, |
2207 | | libc::SO_REUSEPORT, |
2208 | 0 | reuse as c_int, |
2209 | | ) |
2210 | | } |
2211 | 0 | } |
2212 | | |
2213 | | /// Get the value of the `SO_REUSEPORT_LB` option on this socket. |
2214 | | /// |
2215 | | /// For more information about this option, see [`set_reuse_port_lb`]. |
2216 | | /// |
2217 | | /// [`set_reuse_port_lb`]: crate::Socket::set_reuse_port_lb |
2218 | | #[cfg(all(feature = "all", target_os = "freebsd"))] |
2219 | | pub fn reuse_port_lb(&self) -> io::Result<bool> { |
2220 | | unsafe { |
2221 | | getsockopt::<c_int>(self.as_raw(), libc::SOL_SOCKET, libc::SO_REUSEPORT_LB) |
2222 | | .map(|reuse| reuse != 0) |
2223 | | } |
2224 | | } |
2225 | | |
2226 | | /// Set value for the `SO_REUSEPORT_LB` option on this socket. |
2227 | | /// |
2228 | | /// This allows multiple programs or threads to bind to the same port and |
2229 | | /// incoming connections will be load balanced using a hash function. |
2230 | | #[cfg(all(feature = "all", target_os = "freebsd"))] |
2231 | | pub fn set_reuse_port_lb(&self, reuse: bool) -> io::Result<()> { |
2232 | | unsafe { |
2233 | | setsockopt( |
2234 | | self.as_raw(), |
2235 | | libc::SOL_SOCKET, |
2236 | | libc::SO_REUSEPORT_LB, |
2237 | | reuse as c_int, |
2238 | | ) |
2239 | | } |
2240 | | } |
2241 | | |
2242 | | /// Get the value of the `IP_FREEBIND` option on this socket. |
2243 | | /// |
2244 | | /// For more information about this option, see [`set_freebind_v4`]. |
2245 | | /// |
2246 | | /// [`set_freebind_v4`]: crate::Socket::set_freebind_v4 |
2247 | | #[cfg(all( |
2248 | | feature = "all", |
2249 | | any(target_os = "android", target_os = "fuchsia", target_os = "linux") |
2250 | | ))] |
2251 | 0 | pub fn freebind_v4(&self) -> io::Result<bool> { |
2252 | | unsafe { |
2253 | 0 | getsockopt::<c_int>(self.as_raw(), libc::SOL_IP, libc::IP_FREEBIND) |
2254 | 0 | .map(|freebind| freebind != 0) |
2255 | | } |
2256 | 0 | } |
2257 | | |
2258 | | /// Set value for the `IP_FREEBIND` option on this socket. |
2259 | | /// |
2260 | | /// If enabled, this boolean option allows binding to an IP address that is |
2261 | | /// nonlocal or does not (yet) exist. This permits listening on a socket, |
2262 | | /// without requiring the underlying network interface or the specified |
2263 | | /// dynamic IP address to be up at the time that the application is trying |
2264 | | /// to bind to it. |
2265 | | #[cfg(all( |
2266 | | feature = "all", |
2267 | | any(target_os = "android", target_os = "fuchsia", target_os = "linux") |
2268 | | ))] |
2269 | 0 | pub fn set_freebind_v4(&self, freebind: bool) -> io::Result<()> { |
2270 | | unsafe { |
2271 | 0 | setsockopt( |
2272 | 0 | self.as_raw(), |
2273 | | libc::SOL_IP, |
2274 | | libc::IP_FREEBIND, |
2275 | 0 | freebind as c_int, |
2276 | | ) |
2277 | | } |
2278 | 0 | } |
2279 | | |
2280 | | /// Get the value of the `IPV6_FREEBIND` option on this socket. |
2281 | | /// |
2282 | | /// This is an IPv6 counterpart of `IP_FREEBIND` socket option on |
2283 | | /// Android/Linux. For more information about this option, see |
2284 | | /// [`set_freebind_v4`]. |
2285 | | /// |
2286 | | /// [`set_freebind_v4`]: crate::Socket::set_freebind_v4 |
2287 | | #[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))] |
2288 | 0 | pub fn freebind_v6(&self) -> io::Result<bool> { |
2289 | | unsafe { |
2290 | 0 | getsockopt::<c_int>(self.as_raw(), libc::SOL_IPV6, libc::IPV6_FREEBIND) |
2291 | 0 | .map(|freebind| freebind != 0) |
2292 | | } |
2293 | 0 | } |
2294 | | |
2295 | | /// Set value for the `IPV6_FREEBIND` option on this socket. |
2296 | | /// |
2297 | | /// This is an IPv6 counterpart of `IP_FREEBIND` socket option on |
2298 | | /// Android/Linux. For more information about this option, see |
2299 | | /// [`set_freebind_v4`]. |
2300 | | /// |
2301 | | /// [`set_freebind_v4`]: crate::Socket::set_freebind_v4 |
2302 | | /// |
2303 | | /// # Examples |
2304 | | /// |
2305 | | /// On Linux: |
2306 | | /// |
2307 | | /// ``` |
2308 | | /// use socket2::{Domain, Socket, Type}; |
2309 | | /// use std::io::{self, Error, ErrorKind}; |
2310 | | /// |
2311 | | /// fn enable_freebind(socket: &Socket) -> io::Result<()> { |
2312 | | /// match socket.domain()? { |
2313 | | /// Domain::IPV4 => socket.set_freebind_v4(true)?, |
2314 | | /// Domain::IPV6 => socket.set_freebind_v6(true)?, |
2315 | | /// _ => return Err(Error::new(ErrorKind::Other, "unsupported domain")), |
2316 | | /// }; |
2317 | | /// Ok(()) |
2318 | | /// } |
2319 | | /// |
2320 | | /// # fn main() -> io::Result<()> { |
2321 | | /// # let socket = Socket::new(Domain::IPV6, Type::STREAM, None)?; |
2322 | | /// # enable_freebind(&socket) |
2323 | | /// # } |
2324 | | /// ``` |
2325 | | #[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))] |
2326 | 0 | pub fn set_freebind_v6(&self, freebind: bool) -> io::Result<()> { |
2327 | | unsafe { |
2328 | 0 | setsockopt( |
2329 | 0 | self.as_raw(), |
2330 | | libc::SOL_IPV6, |
2331 | | libc::IPV6_FREEBIND, |
2332 | 0 | freebind as c_int, |
2333 | | ) |
2334 | | } |
2335 | 0 | } |
2336 | | |
2337 | | /// Copies data between a `file` and this socket using the `sendfile(2)` |
2338 | | /// system call. Because this copying is done within the kernel, |
2339 | | /// `sendfile()` is more efficient than the combination of `read(2)` and |
2340 | | /// `write(2)`, which would require transferring data to and from user |
2341 | | /// space. |
2342 | | /// |
2343 | | /// Different OSs support different kinds of `file`s, see the OS |
2344 | | /// documentation for what kind of files are supported. Generally *regular* |
2345 | | /// files are supported by all OSs. |
2346 | | #[doc = man_links!(unix: sendfile(2))] |
2347 | | /// |
2348 | | /// The `offset` is the absolute offset into the `file` to use as starting |
2349 | | /// point. |
2350 | | /// |
2351 | | /// Depending on the OS this function *may* change the offset of `file`. For |
2352 | | /// the best results reset the offset of the file before using it again. |
2353 | | /// |
2354 | | /// The `length` determines how many bytes to send, where a length of `None` |
2355 | | /// means it will try to send all bytes. |
2356 | | #[cfg(all( |
2357 | | feature = "all", |
2358 | | any( |
2359 | | target_os = "aix", |
2360 | | target_os = "android", |
2361 | | target_os = "freebsd", |
2362 | | target_os = "ios", |
2363 | | target_os = "visionos", |
2364 | | target_os = "linux", |
2365 | | target_os = "macos", |
2366 | | target_os = "tvos", |
2367 | | target_os = "watchos", |
2368 | | ) |
2369 | | ))] |
2370 | 0 | pub fn sendfile<F>( |
2371 | 0 | &self, |
2372 | 0 | file: &F, |
2373 | 0 | offset: usize, |
2374 | 0 | length: Option<NonZeroUsize>, |
2375 | 0 | ) -> io::Result<usize> |
2376 | 0 | where |
2377 | 0 | F: AsRawFd, |
2378 | | { |
2379 | 0 | self._sendfile(file.as_raw_fd(), offset as _, length) |
2380 | 0 | } |
2381 | | |
2382 | | #[cfg(all( |
2383 | | feature = "all", |
2384 | | any( |
2385 | | target_os = "ios", |
2386 | | target_os = "visionos", |
2387 | | target_os = "macos", |
2388 | | target_os = "tvos", |
2389 | | target_os = "watchos", |
2390 | | ) |
2391 | | ))] |
2392 | | fn _sendfile( |
2393 | | &self, |
2394 | | file: RawFd, |
2395 | | offset: libc::off_t, |
2396 | | length: Option<NonZeroUsize>, |
2397 | | ) -> io::Result<usize> { |
2398 | | // On macOS `length` is value-result parameter. It determines the number |
2399 | | // of bytes to write and returns the number of bytes written. |
2400 | | let mut length = match length { |
2401 | | Some(n) => n.get() as libc::off_t, |
2402 | | // A value of `0` means send all bytes. |
2403 | | None => 0, |
2404 | | }; |
2405 | | syscall!(sendfile( |
2406 | | file, |
2407 | | self.as_raw(), |
2408 | | offset, |
2409 | | &mut length, |
2410 | | ptr::null_mut(), |
2411 | | 0, |
2412 | | )) |
2413 | | .map(|_| length as usize) |
2414 | | } |
2415 | | |
2416 | | #[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))] |
2417 | 0 | fn _sendfile( |
2418 | 0 | &self, |
2419 | 0 | file: RawFd, |
2420 | 0 | offset: libc::off_t, |
2421 | 0 | length: Option<NonZeroUsize>, |
2422 | 0 | ) -> io::Result<usize> { |
2423 | 0 | let count = match length { |
2424 | 0 | Some(n) => n.get() as libc::size_t, |
2425 | | // The maximum the Linux kernel will write in a single call. |
2426 | 0 | None => 0x7ffff000, // 2,147,479,552 bytes. |
2427 | | }; |
2428 | 0 | let mut offset = offset; |
2429 | 0 | syscall!(sendfile(self.as_raw(), file, &mut offset, count)).map(|n| n as usize) |
2430 | 0 | } |
2431 | | |
2432 | | #[cfg(all(feature = "all", target_os = "freebsd"))] |
2433 | | fn _sendfile( |
2434 | | &self, |
2435 | | file: RawFd, |
2436 | | offset: libc::off_t, |
2437 | | length: Option<NonZeroUsize>, |
2438 | | ) -> io::Result<usize> { |
2439 | | let nbytes = match length { |
2440 | | Some(n) => n.get() as libc::size_t, |
2441 | | // A value of `0` means send all bytes. |
2442 | | None => 0, |
2443 | | }; |
2444 | | let mut sbytes: libc::off_t = 0; |
2445 | | syscall!(sendfile( |
2446 | | file, |
2447 | | self.as_raw(), |
2448 | | offset, |
2449 | | nbytes, |
2450 | | ptr::null_mut(), |
2451 | | &mut sbytes, |
2452 | | 0, |
2453 | | )) |
2454 | | .map(|_| sbytes as usize) |
2455 | | } |
2456 | | |
2457 | | #[cfg(all(feature = "all", target_os = "aix"))] |
2458 | | fn _sendfile( |
2459 | | &self, |
2460 | | file: RawFd, |
2461 | | offset: libc::off_t, |
2462 | | length: Option<NonZeroUsize>, |
2463 | | ) -> io::Result<usize> { |
2464 | | let nbytes = match length { |
2465 | | Some(n) => n.get() as i64, |
2466 | | None => -1, |
2467 | | }; |
2468 | | let mut params = libc::sf_parms { |
2469 | | header_data: ptr::null_mut(), |
2470 | | header_length: 0, |
2471 | | file_descriptor: file, |
2472 | | file_size: 0, |
2473 | | file_offset: offset as u64, |
2474 | | file_bytes: nbytes, |
2475 | | trailer_data: ptr::null_mut(), |
2476 | | trailer_length: 0, |
2477 | | bytes_sent: 0, |
2478 | | }; |
2479 | | // AIX doesn't support SF_REUSE, socket will be closed after successful transmission. |
2480 | | syscall!(send_file( |
2481 | | &mut self.as_raw() as *mut _, |
2482 | | &mut params as *mut _, |
2483 | | libc::SF_CLOSE as libc::c_uint, |
2484 | | )) |
2485 | | .map(|_| params.bytes_sent as usize) |
2486 | | } |
2487 | | |
2488 | | /// Set the value of the `TCP_USER_TIMEOUT` option on this socket. |
2489 | | /// |
2490 | | /// If set, this specifies the maximum amount of time that transmitted data may remain |
2491 | | /// unacknowledged or buffered data may remain untransmitted before TCP will forcibly close the |
2492 | | /// corresponding connection. |
2493 | | /// |
2494 | | /// Setting `timeout` to `None` or a zero duration causes the system default timeouts to |
2495 | | /// be used. If `timeout` in milliseconds is larger than `c_uint::MAX`, the timeout is clamped |
2496 | | /// to `c_uint::MAX`. For example, when `c_uint` is a 32-bit value, this limits the timeout to |
2497 | | /// approximately 49.71 days. |
2498 | | #[cfg(all( |
2499 | | feature = "all", |
2500 | | any( |
2501 | | target_os = "android", |
2502 | | target_os = "fuchsia", |
2503 | | target_os = "linux", |
2504 | | target_os = "cygwin", |
2505 | | ) |
2506 | | ))] |
2507 | 0 | pub fn set_tcp_user_timeout(&self, timeout: Option<Duration>) -> io::Result<()> { |
2508 | 0 | let timeout = timeout.map_or(0, |to| { |
2509 | 0 | min(to.as_millis(), libc::c_uint::MAX as u128) as libc::c_uint |
2510 | 0 | }); |
2511 | | unsafe { |
2512 | 0 | setsockopt( |
2513 | 0 | self.as_raw(), |
2514 | | libc::IPPROTO_TCP, |
2515 | | libc::TCP_USER_TIMEOUT, |
2516 | 0 | timeout, |
2517 | | ) |
2518 | | } |
2519 | 0 | } |
2520 | | |
2521 | | /// Get the value of the `TCP_USER_TIMEOUT` option on this socket. |
2522 | | /// |
2523 | | /// For more information about this option, see [`set_tcp_user_timeout`]. |
2524 | | /// |
2525 | | /// [`set_tcp_user_timeout`]: crate::Socket::set_tcp_user_timeout |
2526 | | #[cfg(all( |
2527 | | feature = "all", |
2528 | | any( |
2529 | | target_os = "android", |
2530 | | target_os = "fuchsia", |
2531 | | target_os = "linux", |
2532 | | target_os = "cygwin", |
2533 | | ) |
2534 | | ))] |
2535 | 0 | pub fn tcp_user_timeout(&self) -> io::Result<Option<Duration>> { |
2536 | | unsafe { |
2537 | 0 | getsockopt::<libc::c_uint>(self.as_raw(), libc::IPPROTO_TCP, libc::TCP_USER_TIMEOUT) |
2538 | 0 | .map(|millis| { |
2539 | 0 | if millis == 0 { |
2540 | 0 | None |
2541 | | } else { |
2542 | 0 | Some(Duration::from_millis(millis as u64)) |
2543 | | } |
2544 | 0 | }) |
2545 | | } |
2546 | 0 | } |
2547 | | |
2548 | | /// Attach Berkeley Packet Filter (BPF) on this socket. |
2549 | | /// |
2550 | | /// BPF allows a user-space program to attach a filter onto any socket |
2551 | | /// and allow or disallow certain types of data to come through the socket. |
2552 | | /// |
2553 | | /// For more information about this option, see [filter](https://www.kernel.org/doc/html/v5.12/networking/filter.html) |
2554 | | #[cfg(all(feature = "all", any(target_os = "linux", target_os = "android")))] |
2555 | 0 | pub fn attach_filter(&self, filters: &[SockFilter]) -> io::Result<()> { |
2556 | 0 | let prog = libc::sock_fprog { |
2557 | 0 | len: filters.len() as u16, |
2558 | 0 | // SAFETY: this is safe due to `repr(transparent)`. |
2559 | 0 | filter: filters.as_ptr() as *mut _, |
2560 | 0 | }; |
2561 | | |
2562 | | unsafe { |
2563 | 0 | setsockopt( |
2564 | 0 | self.as_raw(), |
2565 | | libc::SOL_SOCKET, |
2566 | | libc::SO_ATTACH_FILTER, |
2567 | 0 | prog, |
2568 | | ) |
2569 | | } |
2570 | 0 | } |
2571 | | |
2572 | | /// Detach Berkeley Packet Filter(BPF) from this socket. |
2573 | | /// |
2574 | | /// For more information about this option, see [`attach_filter`] |
2575 | | /// |
2576 | | /// [`attach_filter`]: crate::Socket::attach_filter |
2577 | | #[cfg(all(feature = "all", any(target_os = "linux", target_os = "android")))] |
2578 | 0 | pub fn detach_filter(&self) -> io::Result<()> { |
2579 | 0 | unsafe { setsockopt(self.as_raw(), libc::SOL_SOCKET, libc::SO_DETACH_FILTER, 0) } |
2580 | 0 | } |
2581 | | |
2582 | | /// Gets the value for the `SO_COOKIE` option on this socket. |
2583 | | /// |
2584 | | /// The socket cookie is a unique, kernel-managed identifier tied to each socket. |
2585 | | /// Therefore, there is no corresponding `set` helper. |
2586 | | /// |
2587 | | /// For more information about this option, see [Linux patch](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=5daab9db7b65df87da26fd8cfa695fb9546a1ddb) |
2588 | | #[cfg(all(feature = "all", target_os = "linux"))] |
2589 | 0 | pub fn cookie(&self) -> io::Result<u64> { |
2590 | 0 | unsafe { getsockopt::<libc::c_ulonglong>(self.as_raw(), libc::SOL_SOCKET, libc::SO_COOKIE) } |
2591 | 0 | } |
2592 | | |
2593 | | /// Get the value of the `IPV6_TCLASS` option for this socket. |
2594 | | /// |
2595 | | /// For more information about this option, see [`set_tclass_v6`]. |
2596 | | /// |
2597 | | /// [`set_tclass_v6`]: crate::Socket::set_tclass_v6 |
2598 | | #[cfg(all( |
2599 | | feature = "all", |
2600 | | any( |
2601 | | target_os = "android", |
2602 | | target_os = "dragonfly", |
2603 | | target_os = "freebsd", |
2604 | | target_os = "fuchsia", |
2605 | | target_os = "linux", |
2606 | | target_os = "macos", |
2607 | | target_os = "netbsd", |
2608 | | target_os = "openbsd", |
2609 | | target_os = "cygwin", |
2610 | | ) |
2611 | | ))] |
2612 | 0 | pub fn tclass_v6(&self) -> io::Result<u32> { |
2613 | | unsafe { |
2614 | 0 | getsockopt::<c_int>(self.as_raw(), IPPROTO_IPV6, libc::IPV6_TCLASS) |
2615 | 0 | .map(|tclass| tclass as u32) |
2616 | | } |
2617 | 0 | } |
2618 | | |
2619 | | /// Set the value of the `IPV6_TCLASS` option for this socket. |
2620 | | /// |
2621 | | /// Specifies the traffic class field that is used in every packets |
2622 | | /// sent from this socket. |
2623 | | #[cfg(all( |
2624 | | feature = "all", |
2625 | | any( |
2626 | | target_os = "android", |
2627 | | target_os = "dragonfly", |
2628 | | target_os = "freebsd", |
2629 | | target_os = "fuchsia", |
2630 | | target_os = "linux", |
2631 | | target_os = "macos", |
2632 | | target_os = "netbsd", |
2633 | | target_os = "openbsd", |
2634 | | target_os = "cygwin", |
2635 | | ) |
2636 | | ))] |
2637 | 0 | pub fn set_tclass_v6(&self, tclass: u32) -> io::Result<()> { |
2638 | | unsafe { |
2639 | 0 | setsockopt( |
2640 | 0 | self.as_raw(), |
2641 | | IPPROTO_IPV6, |
2642 | | libc::IPV6_TCLASS, |
2643 | 0 | tclass as c_int, |
2644 | | ) |
2645 | | } |
2646 | 0 | } |
2647 | | |
2648 | | /// Get the value of the `TCP_CONGESTION` option for this socket. |
2649 | | /// |
2650 | | /// For more information about this option, see [`set_tcp_congestion`]. |
2651 | | /// |
2652 | | /// [`set_tcp_congestion`]: crate::Socket::set_tcp_congestion |
2653 | | #[cfg(all(feature = "all", any(target_os = "freebsd", target_os = "linux")))] |
2654 | 0 | pub fn tcp_congestion(&self) -> io::Result<Vec<u8>> { |
2655 | 0 | let mut payload: [u8; TCP_CA_NAME_MAX] = [0; TCP_CA_NAME_MAX]; |
2656 | 0 | let mut len = payload.len() as libc::socklen_t; |
2657 | 0 | syscall!(getsockopt( |
2658 | 0 | self.as_raw(), |
2659 | | IPPROTO_TCP, |
2660 | | libc::TCP_CONGESTION, |
2661 | 0 | payload.as_mut_ptr().cast(), |
2662 | 0 | &mut len, |
2663 | | )) |
2664 | 0 | .map(|_| payload[..len as usize].to_vec()) |
2665 | 0 | } |
2666 | | |
2667 | | /// Set the value of the `TCP_CONGESTION` option for this socket. |
2668 | | /// |
2669 | | /// Specifies the TCP congestion control algorithm to use for this socket. |
2670 | | /// |
2671 | | /// The value must be a valid TCP congestion control algorithm name of the |
2672 | | /// platform. For example, Linux may supports "reno", "cubic". |
2673 | | #[cfg(all(feature = "all", any(target_os = "freebsd", target_os = "linux")))] |
2674 | 0 | pub fn set_tcp_congestion(&self, tcp_ca_name: &[u8]) -> io::Result<()> { |
2675 | 0 | syscall!(setsockopt( |
2676 | 0 | self.as_raw(), |
2677 | | IPPROTO_TCP, |
2678 | | libc::TCP_CONGESTION, |
2679 | 0 | tcp_ca_name.as_ptr() as *const _, |
2680 | 0 | tcp_ca_name.len() as libc::socklen_t, |
2681 | | )) |
2682 | 0 | .map(|_| ()) |
2683 | 0 | } |
2684 | | |
2685 | | /// Set value for the `DCCP_SOCKOPT_SERVICE` option on this socket. |
2686 | | /// |
2687 | | /// Sets the DCCP service. The specification mandates use of service codes. |
2688 | | /// If this socket option is not set, the socket will fall back to 0 (which |
2689 | | /// means that no meaningful service code is present). On active sockets |
2690 | | /// this is set before [`connect`]. On passive sockets up to 32 service |
2691 | | /// codes can be set before calling [`bind`] |
2692 | | /// |
2693 | | /// [`connect`]: crate::Socket::connect |
2694 | | /// [`bind`]: crate::Socket::bind |
2695 | | #[cfg(all(feature = "all", target_os = "linux"))] |
2696 | 0 | pub fn set_dccp_service(&self, code: u32) -> io::Result<()> { |
2697 | | unsafe { |
2698 | 0 | setsockopt( |
2699 | 0 | self.as_raw(), |
2700 | | libc::SOL_DCCP, |
2701 | | libc::DCCP_SOCKOPT_SERVICE, |
2702 | 0 | code, |
2703 | | ) |
2704 | | } |
2705 | 0 | } |
2706 | | |
2707 | | /// Get the value of the `DCCP_SOCKOPT_SERVICE` option on this socket. |
2708 | | /// |
2709 | | /// For more information about this option see [`set_dccp_service`] |
2710 | | /// |
2711 | | /// [`set_dccp_service`]: crate::Socket::set_dccp_service |
2712 | | #[cfg(all(feature = "all", target_os = "linux"))] |
2713 | 0 | pub fn dccp_service(&self) -> io::Result<u32> { |
2714 | 0 | unsafe { getsockopt(self.as_raw(), libc::SOL_DCCP, libc::DCCP_SOCKOPT_SERVICE) } |
2715 | 0 | } |
2716 | | |
2717 | | /// Set value for the `DCCP_SOCKOPT_CCID` option on this socket. |
2718 | | /// |
2719 | | /// This option sets both the TX and RX CCIDs at the same time. |
2720 | | #[cfg(all(feature = "all", target_os = "linux"))] |
2721 | 0 | pub fn set_dccp_ccid(&self, ccid: u8) -> io::Result<()> { |
2722 | 0 | unsafe { setsockopt(self.as_raw(), libc::SOL_DCCP, libc::DCCP_SOCKOPT_CCID, ccid) } |
2723 | 0 | } |
2724 | | |
2725 | | /// Get the value of the `DCCP_SOCKOPT_TX_CCID` option on this socket. |
2726 | | /// |
2727 | | /// For more information about this option see [`set_dccp_ccid`]. |
2728 | | /// |
2729 | | /// [`set_dccp_ccid`]: crate::Socket::set_dccp_ccid |
2730 | | #[cfg(all(feature = "all", target_os = "linux"))] |
2731 | 0 | pub fn dccp_tx_ccid(&self) -> io::Result<u32> { |
2732 | 0 | unsafe { getsockopt(self.as_raw(), libc::SOL_DCCP, libc::DCCP_SOCKOPT_TX_CCID) } |
2733 | 0 | } |
2734 | | |
2735 | | /// Get the value of the `DCCP_SOCKOPT_RX_CCID` option on this socket. |
2736 | | /// |
2737 | | /// For more information about this option see [`set_dccp_ccid`]. |
2738 | | /// |
2739 | | /// [`set_dccp_ccid`]: crate::Socket::set_dccp_ccid |
2740 | | #[cfg(all(feature = "all", target_os = "linux"))] |
2741 | 0 | pub fn dccp_xx_ccid(&self) -> io::Result<u32> { |
2742 | 0 | unsafe { getsockopt(self.as_raw(), libc::SOL_DCCP, libc::DCCP_SOCKOPT_RX_CCID) } |
2743 | 0 | } |
2744 | | |
2745 | | /// Set value for the `DCCP_SOCKOPT_SERVER_TIMEWAIT` option on this socket. |
2746 | | /// |
2747 | | /// Enables a listening socket to hold timewait state when closing the |
2748 | | /// connection. This option must be set after `accept` returns. |
2749 | | #[cfg(all(feature = "all", target_os = "linux"))] |
2750 | 0 | pub fn set_dccp_server_timewait(&self, hold_timewait: bool) -> io::Result<()> { |
2751 | | unsafe { |
2752 | 0 | setsockopt( |
2753 | 0 | self.as_raw(), |
2754 | | libc::SOL_DCCP, |
2755 | | libc::DCCP_SOCKOPT_SERVER_TIMEWAIT, |
2756 | 0 | hold_timewait as c_int, |
2757 | | ) |
2758 | | } |
2759 | 0 | } |
2760 | | |
2761 | | /// Get the value of the `DCCP_SOCKOPT_SERVER_TIMEWAIT` option on this socket. |
2762 | | /// |
2763 | | /// For more information see [`set_dccp_server_timewait`] |
2764 | | /// |
2765 | | /// [`set_dccp_server_timewait`]: crate::Socket::set_dccp_server_timewait |
2766 | | #[cfg(all(feature = "all", target_os = "linux"))] |
2767 | 0 | pub fn dccp_server_timewait(&self) -> io::Result<bool> { |
2768 | | unsafe { |
2769 | 0 | getsockopt( |
2770 | 0 | self.as_raw(), |
2771 | | libc::SOL_DCCP, |
2772 | | libc::DCCP_SOCKOPT_SERVER_TIMEWAIT, |
2773 | | ) |
2774 | | } |
2775 | 0 | } |
2776 | | |
2777 | | /// Set value for the `DCCP_SOCKOPT_SEND_CSCOV` option on this socket. |
2778 | | /// |
2779 | | /// Both this option and `DCCP_SOCKOPT_RECV_CSCOV` are used for setting the |
2780 | | /// partial checksum coverage. The default is that checksums always cover |
2781 | | /// the entire packet and that only fully covered application data is |
2782 | | /// accepted by the receiver. Hence, when using this feature on the sender, |
2783 | | /// it must be enabled at the receiver too, with suitable choice of CsCov. |
2784 | | #[cfg(all(feature = "all", target_os = "linux"))] |
2785 | 0 | pub fn set_dccp_send_cscov(&self, level: u32) -> io::Result<()> { |
2786 | | unsafe { |
2787 | 0 | setsockopt( |
2788 | 0 | self.as_raw(), |
2789 | | libc::SOL_DCCP, |
2790 | | libc::DCCP_SOCKOPT_SEND_CSCOV, |
2791 | 0 | level, |
2792 | | ) |
2793 | | } |
2794 | 0 | } |
2795 | | |
2796 | | /// Get the value of the `DCCP_SOCKOPT_SEND_CSCOV` option on this socket. |
2797 | | /// |
2798 | | /// For more information on this option see [`set_dccp_send_cscov`]. |
2799 | | /// |
2800 | | /// [`set_dccp_send_cscov`]: crate::Socket::set_dccp_send_cscov |
2801 | | #[cfg(all(feature = "all", target_os = "linux"))] |
2802 | 0 | pub fn dccp_send_cscov(&self) -> io::Result<u32> { |
2803 | 0 | unsafe { getsockopt(self.as_raw(), libc::SOL_DCCP, libc::DCCP_SOCKOPT_SEND_CSCOV) } |
2804 | 0 | } |
2805 | | |
2806 | | /// Set the value of the `DCCP_SOCKOPT_RECV_CSCOV` option on this socket. |
2807 | | /// |
2808 | | /// This option is only useful when combined with [`set_dccp_send_cscov`]. |
2809 | | /// |
2810 | | /// [`set_dccp_send_cscov`]: crate::Socket::set_dccp_send_cscov |
2811 | | #[cfg(all(feature = "all", target_os = "linux"))] |
2812 | 0 | pub fn set_dccp_recv_cscov(&self, level: u32) -> io::Result<()> { |
2813 | | unsafe { |
2814 | 0 | setsockopt( |
2815 | 0 | self.as_raw(), |
2816 | | libc::SOL_DCCP, |
2817 | | libc::DCCP_SOCKOPT_RECV_CSCOV, |
2818 | 0 | level, |
2819 | | ) |
2820 | | } |
2821 | 0 | } |
2822 | | |
2823 | | /// Get the value of the `DCCP_SOCKOPT_RECV_CSCOV` option on this socket. |
2824 | | /// |
2825 | | /// For more information on this option see [`set_dccp_recv_cscov`]. |
2826 | | /// |
2827 | | /// [`set_dccp_recv_cscov`]: crate::Socket::set_dccp_recv_cscov |
2828 | | #[cfg(all(feature = "all", target_os = "linux"))] |
2829 | 0 | pub fn dccp_recv_cscov(&self) -> io::Result<u32> { |
2830 | 0 | unsafe { getsockopt(self.as_raw(), libc::SOL_DCCP, libc::DCCP_SOCKOPT_RECV_CSCOV) } |
2831 | 0 | } |
2832 | | |
2833 | | /// Set value for the `DCCP_SOCKOPT_QPOLICY_TXQLEN` option on this socket. |
2834 | | /// |
2835 | | /// This option sets the maximum length of the output queue. A zero value is |
2836 | | /// interpreted as unbounded queue length. |
2837 | | #[cfg(all(feature = "all", target_os = "linux"))] |
2838 | 0 | pub fn set_dccp_qpolicy_txqlen(&self, length: u32) -> io::Result<()> { |
2839 | | unsafe { |
2840 | 0 | setsockopt( |
2841 | 0 | self.as_raw(), |
2842 | | libc::SOL_DCCP, |
2843 | | libc::DCCP_SOCKOPT_QPOLICY_TXQLEN, |
2844 | 0 | length, |
2845 | | ) |
2846 | | } |
2847 | 0 | } |
2848 | | |
2849 | | /// Get the value of the `DCCP_SOCKOPT_QPOLICY_TXQLEN` on this socket. |
2850 | | /// |
2851 | | /// For more information on this option see [`set_dccp_qpolicy_txqlen`]. |
2852 | | /// |
2853 | | /// [`set_dccp_qpolicy_txqlen`]: crate::Socket::set_dccp_qpolicy_txqlen |
2854 | | #[cfg(all(feature = "all", target_os = "linux"))] |
2855 | 0 | pub fn dccp_qpolicy_txqlen(&self) -> io::Result<u32> { |
2856 | | unsafe { |
2857 | 0 | getsockopt( |
2858 | 0 | self.as_raw(), |
2859 | | libc::SOL_DCCP, |
2860 | | libc::DCCP_SOCKOPT_QPOLICY_TXQLEN, |
2861 | | ) |
2862 | | } |
2863 | 0 | } |
2864 | | |
2865 | | /// Get the value of the `DCCP_SOCKOPT_AVAILABLE_CCIDS` option on this socket. |
2866 | | /// |
2867 | | /// Returns the list of CCIDs supported by the endpoint. |
2868 | | /// |
2869 | | /// The parameter `N` is used to get the maximum number of supported |
2870 | | /// endpoints. The [documentation] recommends a minimum of four at the time |
2871 | | /// of writing. |
2872 | | /// |
2873 | | /// [documentation]: https://www.kernel.org/doc/html/latest/networking/dccp.html |
2874 | | #[cfg(all(feature = "all", target_os = "linux"))] |
2875 | 0 | pub fn dccp_available_ccids<const N: usize>(&self) -> io::Result<CcidEndpoints<N>> { |
2876 | 0 | let mut endpoints = [0; N]; |
2877 | 0 | let mut length = endpoints.len() as libc::socklen_t; |
2878 | 0 | syscall!(getsockopt( |
2879 | 0 | self.as_raw(), |
2880 | | libc::SOL_DCCP, |
2881 | | libc::DCCP_SOCKOPT_AVAILABLE_CCIDS, |
2882 | 0 | endpoints.as_mut_ptr().cast(), |
2883 | 0 | &mut length, |
2884 | 0 | ))?; |
2885 | 0 | Ok(CcidEndpoints { endpoints, length }) |
2886 | 0 | } |
2887 | | |
2888 | | /// Get the value of the `DCCP_SOCKOPT_GET_CUR_MPS` option on this socket. |
2889 | | /// |
2890 | | /// This option retrieves the current maximum packet size (application |
2891 | | /// payload size) in bytes. |
2892 | | #[cfg(all(feature = "all", target_os = "linux"))] |
2893 | 0 | pub fn dccp_cur_mps(&self) -> io::Result<u32> { |
2894 | | unsafe { |
2895 | 0 | getsockopt( |
2896 | 0 | self.as_raw(), |
2897 | | libc::SOL_DCCP, |
2898 | | libc::DCCP_SOCKOPT_GET_CUR_MPS, |
2899 | | ) |
2900 | | } |
2901 | 0 | } |
2902 | | |
2903 | | /// Get the value for the `SO_BUSY_POLL` option on this socket. |
2904 | | /// |
2905 | | /// On Linux this function requires the `CAP_NET_ADMIN` capability. |
2906 | | #[cfg(all(feature = "all", target_os = "linux"))] |
2907 | 0 | pub fn busy_poll(&self) -> io::Result<u32> { |
2908 | 0 | unsafe { getsockopt(self.as_raw(), libc::SOL_SOCKET, libc::SO_BUSY_POLL) } |
2909 | 0 | } |
2910 | | |
2911 | | /// Set the value for the `SO_BUSY_POLL` option on this socket. |
2912 | | /// |
2913 | | /// On Linux this function requires the `CAP_NET_ADMIN` capability. |
2914 | | #[cfg(all(feature = "all", target_os = "linux"))] |
2915 | 0 | pub fn set_busy_poll(&self, busy_poll: u32) -> io::Result<()> { |
2916 | | unsafe { |
2917 | 0 | setsockopt( |
2918 | 0 | self.as_raw(), |
2919 | | libc::SOL_SOCKET, |
2920 | | libc::SO_BUSY_POLL, |
2921 | 0 | busy_poll as c_int, |
2922 | | ) |
2923 | | } |
2924 | 0 | } |
2925 | | } |
2926 | | |
2927 | | /// Berkeley Packet Filter (BPF). |
2928 | | /// |
2929 | | /// See [`Socket::attach_filter`]. |
2930 | | /// |
2931 | | /// [`Socket::attach_filter`]: crate::Socket::attach_filter |
2932 | | #[cfg(all(feature = "all", any(target_os = "linux", target_os = "android")))] |
2933 | | #[repr(transparent)] |
2934 | | pub struct SockFilter { |
2935 | | // This field is only read indirectly by transmutes / pointer casts, so |
2936 | | // rustc emits a spurious warning saying that the field is never read. |
2937 | | #[allow(dead_code)] |
2938 | | filter: libc::sock_filter, |
2939 | | } |
2940 | | |
2941 | | #[cfg(all(feature = "all", any(target_os = "linux", target_os = "android")))] |
2942 | | impl SockFilter { |
2943 | | /// Create new `SockFilter`. |
2944 | 0 | pub const fn new(code: u16, jt: u8, jf: u8, k: u32) -> SockFilter { |
2945 | 0 | SockFilter { |
2946 | 0 | filter: libc::sock_filter { code, jt, jf, k }, |
2947 | 0 | } |
2948 | 0 | } |
2949 | | } |
2950 | | |
2951 | | #[cfg(all(feature = "all", any(target_os = "linux", target_os = "android")))] |
2952 | | impl std::fmt::Debug for SockFilter { |
2953 | 0 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
2954 | 0 | f.debug_struct("SockFilter").finish_non_exhaustive() |
2955 | 0 | } |
2956 | | } |
2957 | | |
2958 | | /// See [`Socket::dccp_available_ccids`]. |
2959 | | /// |
2960 | | /// [`Socket::dccp_available_ccids`]: crate::Socket::dccp_available_ccids |
2961 | | #[cfg(all(feature = "all", target_os = "linux"))] |
2962 | | #[derive(Debug)] |
2963 | | pub struct CcidEndpoints<const N: usize> { |
2964 | | endpoints: [u8; N], |
2965 | | length: u32, |
2966 | | } |
2967 | | |
2968 | | #[cfg(all(feature = "all", target_os = "linux"))] |
2969 | | impl<const N: usize> std::ops::Deref for CcidEndpoints<N> { |
2970 | | type Target = [u8]; |
2971 | | |
2972 | 0 | fn deref(&self) -> &[u8] { |
2973 | 0 | &self.endpoints[0..self.length as usize] |
2974 | 0 | } |
2975 | | } |
2976 | | |
2977 | | impl AsFd for crate::Socket { |
2978 | 0 | fn as_fd(&self) -> BorrowedFd<'_> { |
2979 | | // SAFETY: lifetime is bound by self. |
2980 | 0 | unsafe { BorrowedFd::borrow_raw(self.as_raw()) } |
2981 | 0 | } |
2982 | | } |
2983 | | |
2984 | | impl AsRawFd for crate::Socket { |
2985 | 0 | fn as_raw_fd(&self) -> RawFd { |
2986 | 0 | self.as_raw() |
2987 | 0 | } |
2988 | | } |
2989 | | |
2990 | | impl From<crate::Socket> for OwnedFd { |
2991 | 0 | fn from(sock: crate::Socket) -> OwnedFd { |
2992 | | // SAFETY: sock.into_raw() always returns a valid fd. |
2993 | 0 | unsafe { OwnedFd::from_raw_fd(sock.into_raw()) } |
2994 | 0 | } |
2995 | | } |
2996 | | |
2997 | | impl IntoRawFd for crate::Socket { |
2998 | 0 | fn into_raw_fd(self) -> c_int { |
2999 | 0 | self.into_raw() |
3000 | 0 | } |
3001 | | } |
3002 | | |
3003 | | impl From<OwnedFd> for crate::Socket { |
3004 | 0 | fn from(fd: OwnedFd) -> crate::Socket { |
3005 | | // SAFETY: `OwnedFd` ensures the fd is valid. |
3006 | 0 | unsafe { crate::Socket::from_raw_fd(fd.into_raw_fd()) } |
3007 | 0 | } |
3008 | | } |
3009 | | |
3010 | | impl FromRawFd for crate::Socket { |
3011 | 0 | unsafe fn from_raw_fd(fd: c_int) -> crate::Socket { |
3012 | 0 | crate::Socket::from_raw(fd) |
3013 | 0 | } |
3014 | | } |
3015 | | |
3016 | | #[cfg(all(feature = "all", unix))] |
3017 | | from!(UnixStream, crate::Socket); |
3018 | | #[cfg(all(feature = "all", unix))] |
3019 | | from!(UnixListener, crate::Socket); |
3020 | | #[cfg(all(feature = "all", unix))] |
3021 | | from!(UnixDatagram, crate::Socket); |
3022 | | #[cfg(all(feature = "all", unix))] |
3023 | | from!(crate::Socket, UnixStream); |
3024 | | #[cfg(all(feature = "all", unix))] |
3025 | | from!(crate::Socket, UnixListener); |
3026 | | #[cfg(all(feature = "all", unix))] |
3027 | | from!(crate::Socket, UnixDatagram); |
3028 | | |
3029 | | #[test] |
3030 | | fn in_addr_convertion() { |
3031 | | let ip = Ipv4Addr::new(127, 0, 0, 1); |
3032 | | let raw = to_in_addr(&ip); |
3033 | | // NOTE: `in_addr` is packed on NetBSD and it's unsafe to borrow. |
3034 | | let a = raw.s_addr; |
3035 | | assert_eq!(a, u32::from_ne_bytes([127, 0, 0, 1])); |
3036 | | assert_eq!(from_in_addr(raw), ip); |
3037 | | |
3038 | | let ip = Ipv4Addr::new(127, 34, 4, 12); |
3039 | | let raw = to_in_addr(&ip); |
3040 | | let a = raw.s_addr; |
3041 | | assert_eq!(a, u32::from_ne_bytes([127, 34, 4, 12])); |
3042 | | assert_eq!(from_in_addr(raw), ip); |
3043 | | } |
3044 | | |
3045 | | #[test] |
3046 | | fn in6_addr_convertion() { |
3047 | | let ip = Ipv6Addr::new(0x2000, 1, 2, 3, 4, 5, 6, 7); |
3048 | | let raw = to_in6_addr(&ip); |
3049 | | let want = [32, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7]; |
3050 | | assert_eq!(raw.s6_addr, want); |
3051 | | assert_eq!(from_in6_addr(raw), ip); |
3052 | | } |