1use std::cmp::min;
10use std::ffi::OsStr;
11#[cfg(not(target_os = "redox"))]
12use std::io::IoSlice;
13use std::marker::PhantomData;
14use std::mem::{self, size_of, MaybeUninit};
15use std::net::Shutdown;
16use std::net::{Ipv4Addr, Ipv6Addr};
17#[cfg(all(
18 feature = "all",
19 any(
20 target_os = "ios",
21 target_os = "visionos",
22 target_os = "macos",
23 target_os = "tvos",
24 target_os = "watchos",
25 target_os = "illumos",
26 target_os = "solaris",
27 )
28))]
29use std::num::NonZeroU32;
30#[cfg(all(
31 feature = "all",
32 any(
33 target_os = "aix",
34 target_os = "android",
35 target_os = "freebsd",
36 target_os = "ios",
37 target_os = "visionos",
38 target_os = "linux",
39 target_os = "macos",
40 target_os = "tvos",
41 target_os = "watchos",
42 )
43))]
44use std::num::NonZeroUsize;
45use std::os::unix::ffi::OsStrExt;
46#[cfg(all(
47 feature = "all",
48 any(
49 target_os = "aix",
50 target_os = "android",
51 target_os = "freebsd",
52 target_os = "ios",
53 target_os = "visionos",
54 target_os = "linux",
55 target_os = "macos",
56 target_os = "tvos",
57 target_os = "watchos",
58 )
59))]
60use std::os::unix::io::RawFd;
61use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd};
62#[cfg(feature = "all")]
63use std::os::unix::net::{UnixDatagram, UnixListener, UnixStream};
64use std::path::Path;
65use std::ptr;
66use std::time::{Duration, Instant};
67use std::{io, slice};
68
69#[cfg(not(any(
70 target_os = "ios",
71 target_os = "visionos",
72 target_os = "macos",
73 target_os = "tvos",
74 target_os = "watchos",
75)))]
76use libc::ssize_t;
77use libc::{in6_addr, in_addr};
78
79use crate::{Domain, Protocol, SockAddr, TcpKeepalive, Type};
80#[cfg(not(target_os = "redox"))]
81use crate::{MsgHdr, MsgHdrMut, RecvFlags};
82
83pub(crate) use libc::c_int;
84
85pub(crate) use libc::{AF_INET, AF_INET6, AF_UNIX};
87#[cfg(all(feature = "all", target_os = "linux"))]
89pub(crate) use libc::SOCK_DCCP;
90#[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))]
91pub(crate) use libc::SOCK_RAW;
92#[cfg(all(feature = "all", not(target_os = "espidf")))]
93pub(crate) use libc::SOCK_SEQPACKET;
94pub(crate) use libc::{SOCK_DGRAM, SOCK_STREAM};
95#[cfg(all(feature = "all", target_os = "linux"))]
97pub(crate) use libc::IPPROTO_DCCP;
98#[cfg(target_os = "linux")]
99pub(crate) use libc::IPPROTO_MPTCP;
100#[cfg(all(feature = "all", any(target_os = "freebsd", target_os = "linux")))]
101pub(crate) use libc::IPPROTO_SCTP;
102#[cfg(all(
103 feature = "all",
104 any(
105 target_os = "android",
106 target_os = "freebsd",
107 target_os = "fuchsia",
108 target_os = "linux",
109 )
110))]
111pub(crate) use libc::IPPROTO_UDPLITE;
112pub(crate) use libc::{IPPROTO_ICMP, IPPROTO_ICMPV6, IPPROTO_TCP, IPPROTO_UDP};
113#[cfg(all(feature = "all", any(target_os = "freebsd", target_os = "openbsd")))]
115pub(crate) use libc::IPPROTO_DIVERT;
116pub(crate) use libc::{
117 sa_family_t, sockaddr, sockaddr_in, sockaddr_in6, sockaddr_storage, socklen_t,
118};
119#[cfg(not(any(target_os = "redox", target_os = "espidf")))]
121pub(crate) use libc::MSG_TRUNC;
122#[cfg(not(target_os = "redox"))]
123pub(crate) use libc::SO_OOBINLINE;
124#[cfg(not(target_os = "nto"))]
126pub(crate) use libc::ipv6_mreq as Ipv6Mreq;
127#[cfg(all(
128 feature = "all",
129 not(any(
130 target_os = "dragonfly",
131 target_os = "fuchsia",
132 target_os = "hurd",
133 target_os = "illumos",
134 target_os = "netbsd",
135 target_os = "openbsd",
136 target_os = "redox",
137 target_os = "solaris",
138 target_os = "haiku",
139 target_os = "espidf",
140 target_os = "vita",
141 ))
142))]
143pub(crate) use libc::IPV6_RECVHOPLIMIT;
144#[cfg(not(any(
145 target_os = "dragonfly",
146 target_os = "fuchsia",
147 target_os = "hurd",
148 target_os = "illumos",
149 target_os = "netbsd",
150 target_os = "openbsd",
151 target_os = "redox",
152 target_os = "solaris",
153 target_os = "haiku",
154 target_os = "espidf",
155 target_os = "vita",
156)))]
157pub(crate) use libc::IPV6_RECVTCLASS;
158#[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))]
159pub(crate) use libc::IP_HDRINCL;
160#[cfg(not(any(
161 target_os = "aix",
162 target_os = "dragonfly",
163 target_os = "fuchsia",
164 target_os = "illumos",
165 target_os = "netbsd",
166 target_os = "openbsd",
167 target_os = "redox",
168 target_os = "solaris",
169 target_os = "haiku",
170 target_os = "hurd",
171 target_os = "nto",
172 target_os = "espidf",
173 target_os = "vita",
174)))]
175pub(crate) use libc::IP_RECVTOS;
176#[cfg(not(any(
177 target_os = "fuchsia",
178 target_os = "redox",
179 target_os = "solaris",
180 target_os = "haiku",
181 target_os = "illumos",
182)))]
183pub(crate) use libc::IP_TOS;
184#[cfg(not(any(
185 target_os = "ios",
186 target_os = "visionos",
187 target_os = "macos",
188 target_os = "tvos",
189 target_os = "watchos",
190)))]
191pub(crate) use libc::SO_LINGER;
192#[cfg(any(
193 target_os = "ios",
194 target_os = "visionos",
195 target_os = "macos",
196 target_os = "tvos",
197 target_os = "watchos",
198))]
199pub(crate) use libc::SO_LINGER_SEC as SO_LINGER;
200#[cfg(target_os = "linux")]
201pub(crate) use libc::SO_PASSCRED;
202pub(crate) use libc::{
203 ip_mreq as IpMreq, linger, IPPROTO_IP, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, IPV6_MULTICAST_IF,
204 IPV6_MULTICAST_LOOP, IPV6_UNICAST_HOPS, IPV6_V6ONLY, IP_ADD_MEMBERSHIP, IP_DROP_MEMBERSHIP,
205 IP_MULTICAST_IF, IP_MULTICAST_LOOP, IP_MULTICAST_TTL, IP_TTL, MSG_OOB, MSG_PEEK, SOL_SOCKET,
206 SO_BROADCAST, SO_ERROR, SO_KEEPALIVE, SO_RCVBUF, SO_RCVTIMEO, SO_REUSEADDR, SO_SNDBUF,
207 SO_SNDTIMEO, SO_TYPE, TCP_NODELAY,
208};
209#[cfg(not(any(
210 target_os = "dragonfly",
211 target_os = "haiku",
212 target_os = "hurd",
213 target_os = "netbsd",
214 target_os = "openbsd",
215 target_os = "redox",
216 target_os = "fuchsia",
217 target_os = "nto",
218 target_os = "espidf",
219 target_os = "vita",
220)))]
221pub(crate) use libc::{
222 ip_mreq_source as IpMreqSource, IP_ADD_SOURCE_MEMBERSHIP, IP_DROP_SOURCE_MEMBERSHIP,
223};
224#[cfg(not(any(
225 target_os = "dragonfly",
226 target_os = "freebsd",
227 target_os = "haiku",
228 target_os = "illumos",
229 target_os = "ios",
230 target_os = "visionos",
231 target_os = "macos",
232 target_os = "netbsd",
233 target_os = "nto",
234 target_os = "openbsd",
235 target_os = "solaris",
236 target_os = "tvos",
237 target_os = "watchos",
238)))]
239pub(crate) use libc::{IPV6_ADD_MEMBERSHIP, IPV6_DROP_MEMBERSHIP};
240#[cfg(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 = "openbsd",
250 target_os = "solaris",
251 target_os = "tvos",
252 target_os = "watchos",
253))]
254pub(crate) use libc::{
255 IPV6_JOIN_GROUP as IPV6_ADD_MEMBERSHIP, IPV6_LEAVE_GROUP as IPV6_DROP_MEMBERSHIP,
256};
257#[cfg(all(
258 feature = "all",
259 any(
260 target_os = "android",
261 target_os = "dragonfly",
262 target_os = "freebsd",
263 target_os = "fuchsia",
264 target_os = "illumos",
265 target_os = "ios",
266 target_os = "visionos",
267 target_os = "linux",
268 target_os = "macos",
269 target_os = "netbsd",
270 target_os = "tvos",
271 target_os = "watchos",
272 )
273))]
274pub(crate) use libc::{TCP_KEEPCNT, TCP_KEEPINTVL};
275
276pub(crate) type Bool = c_int;
278
279#[cfg(any(
280 target_os = "ios",
281 target_os = "visionos",
282 target_os = "macos",
283 target_os = "nto",
284 target_os = "tvos",
285 target_os = "watchos",
286))]
287use libc::TCP_KEEPALIVE as KEEPALIVE_TIME;
288#[cfg(not(any(
289 target_os = "haiku",
290 target_os = "ios",
291 target_os = "visionos",
292 target_os = "macos",
293 target_os = "nto",
294 target_os = "openbsd",
295 target_os = "tvos",
296 target_os = "watchos",
297 target_os = "vita",
298)))]
299use libc::TCP_KEEPIDLE as KEEPALIVE_TIME;
300
301macro_rules! syscall {
303 ($fn: ident ( $($arg: expr),* $(,)* ) ) => {{
304 #[allow(unused_unsafe)]
305 let res = unsafe { libc::$fn($($arg, )*) };
306 if res == -1 {
307 Err(std::io::Error::last_os_error())
308 } else {
309 Ok(res)
310 }
311 }};
312}
313
314#[cfg(not(any(
316 target_os = "ios",
317 target_os = "visionos",
318 target_os = "macos",
319 target_os = "tvos",
320 target_os = "watchos",
321)))]
322const MAX_BUF_LEN: usize = ssize_t::MAX as usize;
323
324#[cfg(any(
333 target_os = "ios",
334 target_os = "visionos",
335 target_os = "macos",
336 target_os = "tvos",
337 target_os = "watchos",
338))]
339const MAX_BUF_LEN: usize = c_int::MAX as usize - 1;
340
341#[cfg(all(feature = "all", any(target_os = "freebsd", target_os = "linux")))]
343const TCP_CA_NAME_MAX: usize = 16;
344
345#[cfg(any(
346 all(
347 target_os = "linux",
348 any(
349 target_env = "gnu",
350 all(target_env = "uclibc", target_pointer_width = "64")
351 )
352 ),
353 target_os = "android",
354))]
355type IovLen = usize;
356
357#[cfg(any(
358 all(
359 target_os = "linux",
360 any(
361 target_env = "musl",
362 target_env = "ohos",
363 all(target_env = "uclibc", target_pointer_width = "32")
364 )
365 ),
366 target_os = "aix",
367 target_os = "dragonfly",
368 target_os = "freebsd",
369 target_os = "fuchsia",
370 target_os = "haiku",
371 target_os = "hurd",
372 target_os = "illumos",
373 target_os = "ios",
374 target_os = "visionos",
375 target_os = "macos",
376 target_os = "netbsd",
377 target_os = "nto",
378 target_os = "openbsd",
379 target_os = "solaris",
380 target_os = "tvos",
381 target_os = "watchos",
382 target_os = "espidf",
383 target_os = "vita",
384))]
385type IovLen = c_int;
386
387impl Domain {
389 #[cfg(all(
391 feature = "all",
392 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
393 ))]
394 #[cfg_attr(
395 docsrs,
396 doc(cfg(all(
397 feature = "all",
398 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
399 )))
400 )]
401 pub const PACKET: Domain = Domain(libc::AF_PACKET);
402
403 #[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))]
405 #[cfg_attr(
406 docsrs,
407 doc(cfg(all(feature = "all", any(target_os = "android", target_os = "linux"))))
408 )]
409 pub const VSOCK: Domain = Domain(libc::AF_VSOCK);
410}
411
412impl_debug!(
413 Domain,
414 libc::AF_INET,
415 libc::AF_INET6,
416 libc::AF_UNIX,
417 #[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
418 #[cfg_attr(
419 docsrs,
420 doc(cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux")))
421 )]
422 libc::AF_PACKET,
423 #[cfg(any(target_os = "android", target_os = "linux"))]
424 #[cfg_attr(docsrs, doc(cfg(any(target_os = "android", target_os = "linux"))))]
425 libc::AF_VSOCK,
426 libc::AF_UNSPEC, );
428
429impl Type {
431 #[cfg(all(
433 feature = "all",
434 any(
435 target_os = "android",
436 target_os = "dragonfly",
437 target_os = "freebsd",
438 target_os = "fuchsia",
439 target_os = "illumos",
440 target_os = "linux",
441 target_os = "netbsd",
442 target_os = "openbsd"
443 )
444 ))]
445 #[cfg_attr(
446 docsrs,
447 doc(cfg(all(
448 feature = "all",
449 any(
450 target_os = "android",
451 target_os = "dragonfly",
452 target_os = "freebsd",
453 target_os = "fuchsia",
454 target_os = "illumos",
455 target_os = "linux",
456 target_os = "netbsd",
457 target_os = "openbsd"
458 )
459 )))
460 )]
461 pub const fn nonblocking(self) -> Type {
462 Type(self.0 | libc::SOCK_NONBLOCK)
463 }
464
465 #[cfg(all(
467 feature = "all",
468 any(
469 target_os = "android",
470 target_os = "dragonfly",
471 target_os = "freebsd",
472 target_os = "fuchsia",
473 target_os = "hurd",
474 target_os = "illumos",
475 target_os = "linux",
476 target_os = "netbsd",
477 target_os = "openbsd",
478 target_os = "redox",
479 target_os = "solaris",
480 )
481 ))]
482 #[cfg_attr(
483 docsrs,
484 doc(cfg(all(
485 feature = "all",
486 any(
487 target_os = "android",
488 target_os = "dragonfly",
489 target_os = "freebsd",
490 target_os = "fuchsia",
491 target_os = "hurd",
492 target_os = "illumos",
493 target_os = "linux",
494 target_os = "netbsd",
495 target_os = "openbsd",
496 target_os = "redox",
497 target_os = "solaris",
498 )
499 )))
500 )]
501 pub const fn cloexec(self) -> Type {
502 self._cloexec()
503 }
504
505 #[cfg(any(
506 target_os = "android",
507 target_os = "dragonfly",
508 target_os = "freebsd",
509 target_os = "fuchsia",
510 target_os = "hurd",
511 target_os = "illumos",
512 target_os = "linux",
513 target_os = "netbsd",
514 target_os = "openbsd",
515 target_os = "redox",
516 target_os = "solaris",
517 ))]
518 pub(crate) const fn _cloexec(self) -> Type {
519 Type(self.0 | libc::SOCK_CLOEXEC)
520 }
521}
522
523impl_debug!(
524 Type,
525 libc::SOCK_STREAM,
526 libc::SOCK_DGRAM,
527 #[cfg(all(feature = "all", target_os = "linux"))]
528 libc::SOCK_DCCP,
529 #[cfg(not(any(target_os = "redox", target_os = "espidf")))]
530 libc::SOCK_RAW,
531 #[cfg(not(any(target_os = "redox", target_os = "haiku", target_os = "espidf")))]
532 libc::SOCK_RDM,
533 #[cfg(not(target_os = "espidf"))]
534 libc::SOCK_SEQPACKET,
535 );
558
559impl_debug!(
560 Protocol,
561 libc::IPPROTO_ICMP,
562 libc::IPPROTO_ICMPV6,
563 libc::IPPROTO_TCP,
564 libc::IPPROTO_UDP,
565 #[cfg(target_os = "linux")]
566 libc::IPPROTO_MPTCP,
567 #[cfg(all(feature = "all", target_os = "linux"))]
568 libc::IPPROTO_DCCP,
569 #[cfg(all(feature = "all", any(target_os = "freebsd", target_os = "linux")))]
570 libc::IPPROTO_SCTP,
571 #[cfg(all(
572 feature = "all",
573 any(
574 target_os = "android",
575 target_os = "freebsd",
576 target_os = "fuchsia",
577 target_os = "linux",
578 )
579 ))]
580 libc::IPPROTO_UDPLITE,
581 #[cfg(all(feature = "all", any(target_os = "freebsd", target_os = "openbsd")))]
582 libc::IPPROTO_DIVERT,
583);
584
585#[cfg(not(target_os = "redox"))]
587impl RecvFlags {
588 #[cfg(not(target_os = "espidf"))]
598 pub const fn is_end_of_record(self) -> bool {
599 self.0 & libc::MSG_EOR != 0
600 }
601
602 pub const fn is_out_of_band(self) -> bool {
609 self.0 & libc::MSG_OOB != 0
610 }
611
612 #[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))]
620 #[cfg_attr(
621 docsrs,
622 doc(cfg(all(feature = "all", any(target_os = "android", target_os = "linux"))))
623 )]
624 pub const fn is_confirm(self) -> bool {
625 self.0 & libc::MSG_CONFIRM != 0
626 }
627
628 #[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))]
635 #[cfg_attr(
636 docsrs,
637 doc(cfg(all(feature = "all", any(target_os = "android", target_os = "linux"))))
638 )]
639 pub const fn is_dontroute(self) -> bool {
640 self.0 & libc::MSG_DONTROUTE != 0
641 }
642}
643
644#[cfg(not(target_os = "redox"))]
645impl std::fmt::Debug for RecvFlags {
646 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
647 let mut s = f.debug_struct("RecvFlags");
648 #[cfg(not(target_os = "espidf"))]
649 s.field("is_end_of_record", &self.is_end_of_record());
650 s.field("is_out_of_band", &self.is_out_of_band());
651 #[cfg(not(target_os = "espidf"))]
652 s.field("is_truncated", &self.is_truncated());
653 #[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))]
654 s.field("is_confirm", &self.is_confirm());
655 #[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))]
656 s.field("is_dontroute", &self.is_dontroute());
657 s.finish()
658 }
659}
660
661#[repr(transparent)]
662pub struct MaybeUninitSlice<'a> {
663 vec: libc::iovec,
664 _lifetime: PhantomData<&'a mut [MaybeUninit<u8>]>,
665}
666
667unsafe impl<'a> Send for MaybeUninitSlice<'a> {}
668
669unsafe impl<'a> Sync for MaybeUninitSlice<'a> {}
670
671impl<'a> MaybeUninitSlice<'a> {
672 pub(crate) fn new(buf: &'a mut [MaybeUninit<u8>]) -> MaybeUninitSlice<'a> {
673 MaybeUninitSlice {
674 vec: libc::iovec {
675 iov_base: buf.as_mut_ptr().cast(),
676 iov_len: buf.len(),
677 },
678 _lifetime: PhantomData,
679 }
680 }
681
682 pub(crate) fn as_slice(&self) -> &[MaybeUninit<u8>] {
683 unsafe { slice::from_raw_parts(self.vec.iov_base.cast(), self.vec.iov_len) }
684 }
685
686 pub(crate) fn as_mut_slice(&mut self) -> &mut [MaybeUninit<u8>] {
687 unsafe { slice::from_raw_parts_mut(self.vec.iov_base.cast(), self.vec.iov_len) }
688 }
689}
690
691pub(crate) fn offset_of_path(storage: &libc::sockaddr_un) -> usize {
693 let base = storage as *const _ as usize;
694 let path = ptr::addr_of!(storage.sun_path) as usize;
695 path - base
696}
697
698#[allow(unsafe_op_in_unsafe_fn)]
699pub(crate) fn unix_sockaddr(path: &Path) -> io::Result<SockAddr> {
700 let mut storage = unsafe { mem::zeroed::<sockaddr_storage>() };
702 let len = {
703 let storage = unsafe { &mut *ptr::addr_of_mut!(storage).cast::<libc::sockaddr_un>() };
704
705 let bytes = path.as_os_str().as_bytes();
706 let too_long = match bytes.first() {
707 None => false,
708 Some(&0) => bytes.len() > storage.sun_path.len(),
710 Some(_) => bytes.len() >= storage.sun_path.len(),
711 };
712 if too_long {
713 return Err(io::Error::new(
714 io::ErrorKind::InvalidInput,
715 "path must be shorter than SUN_LEN",
716 ));
717 }
718
719 storage.sun_family = libc::AF_UNIX as sa_family_t;
720 unsafe {
725 ptr::copy_nonoverlapping(
726 bytes.as_ptr(),
727 storage.sun_path.as_mut_ptr().cast(),
728 bytes.len(),
729 );
730 }
731
732 let sun_path_offset = offset_of_path(storage);
733 sun_path_offset
734 + bytes.len()
735 + match bytes.first() {
736 Some(&0) | None => 0,
737 Some(_) => 1,
738 }
739 };
740 Ok(unsafe { SockAddr::new(storage, len as socklen_t) })
741}
742
743#[cfg(not(target_os = "redox"))]
745pub(crate) use libc::msghdr;
746
747#[cfg(not(target_os = "redox"))]
748pub(crate) fn set_msghdr_name(msg: &mut msghdr, name: &SockAddr) {
749 msg.msg_name = name.as_ptr() as *mut _;
750 msg.msg_namelen = name.len();
751}
752
753#[cfg(not(target_os = "redox"))]
754#[allow(clippy::unnecessary_cast)] pub(crate) fn set_msghdr_iov(msg: &mut msghdr, ptr: *mut libc::iovec, len: usize) {
756 msg.msg_iov = ptr;
757 msg.msg_iovlen = min(len, IovLen::MAX as usize) as IovLen;
758}
759
760#[cfg(not(target_os = "redox"))]
761pub(crate) fn set_msghdr_control(msg: &mut msghdr, ptr: *mut libc::c_void, len: usize) {
762 msg.msg_control = ptr;
763 msg.msg_controllen = len as _;
764}
765
766#[cfg(not(target_os = "redox"))]
767pub(crate) fn set_msghdr_flags(msg: &mut msghdr, flags: libc::c_int) {
768 msg.msg_flags = flags;
769}
770
771#[cfg(not(target_os = "redox"))]
772pub(crate) fn msghdr_flags(msg: &msghdr) -> RecvFlags {
773 RecvFlags(msg.msg_flags)
774}
775
776#[cfg(not(target_os = "redox"))]
777pub(crate) fn msghdr_control_len(msg: &msghdr) -> usize {
778 msg.msg_controllen as _
779}
780
781impl SockAddr {
783 #[allow(unsafe_op_in_unsafe_fn)]
790 #[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))]
791 #[cfg_attr(
792 docsrs,
793 doc(cfg(all(feature = "all", any(target_os = "android", target_os = "linux"))))
794 )]
795 pub fn vsock(cid: u32, port: u32) -> SockAddr {
796 let mut storage = unsafe { mem::zeroed::<sockaddr_storage>() };
798 {
799 let storage: &mut libc::sockaddr_vm =
800 unsafe { &mut *((&mut storage as *mut sockaddr_storage).cast()) };
801 storage.svm_family = libc::AF_VSOCK as sa_family_t;
802 storage.svm_cid = cid;
803 storage.svm_port = port;
804 }
805 unsafe { SockAddr::new(storage, mem::size_of::<libc::sockaddr_vm>() as socklen_t) }
806 }
807
808 #[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))]
811 #[cfg_attr(
812 docsrs,
813 doc(cfg(all(feature = "all", any(target_os = "android", target_os = "linux"))))
814 )]
815 pub fn as_vsock_address(&self) -> Option<(u32, u32)> {
816 if self.family() == libc::AF_VSOCK as sa_family_t {
817 let addr = unsafe { &*(self.as_ptr() as *const libc::sockaddr_vm) };
819 Some((addr.svm_cid, addr.svm_port))
820 } else {
821 None
822 }
823 }
824
825 pub fn is_unnamed(&self) -> bool {
828 self.as_sockaddr_un()
829 .map(|storage| {
830 self.len() == offset_of_path(storage) as _
831 || (cfg!(not(any(target_os = "linux", target_os = "android")))
836 && storage.sun_path[0] == 0)
837 })
838 .unwrap_or_default()
839 }
840
841 pub(crate) fn as_sockaddr_un(&self) -> Option<&libc::sockaddr_un> {
844 self.is_unix().then(|| {
845 unsafe { &*self.as_ptr().cast::<libc::sockaddr_un>() }
848 })
849 }
850
851 fn path_len(&self, storage: &libc::sockaddr_un) -> usize {
856 debug_assert!(!self.is_unnamed());
857 self.len() as usize - offset_of_path(storage) - 1
858 }
859
860 fn path_bytes(&self, storage: &libc::sockaddr_un, abstract_name: bool) -> &[u8] {
864 debug_assert!(!self.is_unnamed());
865 unsafe {
873 slice::from_raw_parts(
874 (storage.sun_path.as_ptr() as *const u8).offset(abstract_name as isize),
875 self.path_len(storage),
876 )
877 }
878 }
879
880 pub fn as_unix(&self) -> Option<std::os::unix::net::SocketAddr> {
883 let path = self.as_pathname()?;
884 Some(std::os::unix::net::SocketAddr::from_pathname(path).unwrap())
887 }
888
889 pub fn as_pathname(&self) -> Option<&Path> {
892 self.as_sockaddr_un().and_then(|storage| {
893 (self.len() > offset_of_path(storage) as _ && storage.sun_path[0] != 0).then(|| {
894 let path_slice = self.path_bytes(storage, false);
895 Path::new::<OsStr>(OsStrExt::from_bytes(path_slice))
896 })
897 })
898 }
899
900 pub fn as_abstract_namespace(&self) -> Option<&[u8]> {
906 #[cfg(any(target_os = "linux", target_os = "android"))]
909 {
910 self.as_sockaddr_un().and_then(|storage| {
911 (self.len() > offset_of_path(storage) as _ && storage.sun_path[0] == 0)
912 .then(|| self.path_bytes(storage, true))
913 })
914 }
915 #[cfg(not(any(target_os = "linux", target_os = "android")))]
916 None
917 }
918}
919
920pub(crate) type Socket = c_int;
921
922pub(crate) unsafe fn socket_from_raw(socket: Socket) -> crate::socket::Inner {
923 crate::socket::Inner::from_raw_fd(socket)
924}
925
926pub(crate) fn socket_as_raw(socket: &crate::socket::Inner) -> Socket {
927 socket.as_raw_fd()
928}
929
930pub(crate) fn socket_into_raw(socket: crate::socket::Inner) -> Socket {
931 socket.into_raw_fd()
932}
933
934pub(crate) fn socket(family: c_int, ty: c_int, protocol: c_int) -> io::Result<Socket> {
935 syscall!(socket(family, ty, protocol))
936}
937
938#[cfg(all(feature = "all", unix))]
939#[cfg_attr(docsrs, doc(cfg(all(feature = "all", unix))))]
940pub(crate) fn socketpair(family: c_int, ty: c_int, protocol: c_int) -> io::Result<[Socket; 2]> {
941 let mut fds = [0, 0];
942 syscall!(socketpair(family, ty, protocol, fds.as_mut_ptr())).map(|_| fds)
943}
944
945pub(crate) fn bind(fd: Socket, addr: &SockAddr) -> io::Result<()> {
946 syscall!(bind(fd, addr.as_ptr(), addr.len() as _)).map(|_| ())
947}
948
949pub(crate) fn connect(fd: Socket, addr: &SockAddr) -> io::Result<()> {
950 syscall!(connect(fd, addr.as_ptr(), addr.len())).map(|_| ())
951}
952
953pub(crate) fn poll_connect(socket: &crate::Socket, timeout: Duration) -> io::Result<()> {
954 let start = Instant::now();
955
956 let mut pollfd = libc::pollfd {
957 fd: socket.as_raw(),
958 events: libc::POLLIN | libc::POLLOUT,
959 revents: 0,
960 };
961
962 loop {
963 let elapsed = start.elapsed();
964 if elapsed >= timeout {
965 return Err(io::ErrorKind::TimedOut.into());
966 }
967
968 let timeout = (timeout - elapsed).as_millis();
969 let timeout = timeout.clamp(1, c_int::MAX as u128) as c_int;
970
971 match syscall!(poll(&mut pollfd, 1, timeout)) {
972 Ok(0) => return Err(io::ErrorKind::TimedOut.into()),
973 Ok(_) => {
974 if (pollfd.revents & libc::POLLHUP) != 0 || (pollfd.revents & libc::POLLERR) != 0 {
976 match socket.take_error() {
977 Ok(Some(err)) | Err(err) => return Err(err),
978 Ok(None) => {
979 return Err(io::Error::new(
980 io::ErrorKind::Other,
981 "no error set after POLLHUP",
982 ))
983 }
984 }
985 }
986 return Ok(());
987 }
988 Err(ref err) if err.kind() == io::ErrorKind::Interrupted => continue,
990 Err(err) => return Err(err),
991 }
992 }
993}
994
995pub(crate) fn listen(fd: Socket, backlog: c_int) -> io::Result<()> {
996 syscall!(listen(fd, backlog)).map(|_| ())
997}
998
999pub(crate) fn accept(fd: Socket) -> io::Result<(Socket, SockAddr)> {
1000 unsafe { SockAddr::try_init(|storage, len| syscall!(accept(fd, storage.cast(), len))) }
1002}
1003
1004pub(crate) fn getsockname(fd: Socket) -> io::Result<SockAddr> {
1005 unsafe { SockAddr::try_init(|storage, len| syscall!(getsockname(fd, storage.cast(), len))) }
1007 .map(|(_, addr)| addr)
1008}
1009
1010pub(crate) fn getpeername(fd: Socket) -> io::Result<SockAddr> {
1011 unsafe { SockAddr::try_init(|storage, len| syscall!(getpeername(fd, storage.cast(), len))) }
1013 .map(|(_, addr)| addr)
1014}
1015
1016pub(crate) fn try_clone(fd: Socket) -> io::Result<Socket> {
1017 syscall!(fcntl(fd, libc::F_DUPFD_CLOEXEC, 0))
1018}
1019
1020#[cfg(all(feature = "all", unix, not(target_os = "vita")))]
1021pub(crate) fn nonblocking(fd: Socket) -> io::Result<bool> {
1022 let file_status_flags = fcntl_get(fd, libc::F_GETFL)?;
1023 Ok((file_status_flags & libc::O_NONBLOCK) != 0)
1024}
1025
1026#[cfg(all(feature = "all", target_os = "vita"))]
1027pub(crate) fn nonblocking(fd: Socket) -> io::Result<bool> {
1028 unsafe {
1029 getsockopt::<Bool>(fd, libc::SOL_SOCKET, libc::SO_NONBLOCK).map(|non_block| non_block != 0)
1030 }
1031}
1032
1033#[cfg(not(target_os = "vita"))]
1034pub(crate) fn set_nonblocking(fd: Socket, nonblocking: bool) -> io::Result<()> {
1035 if nonblocking {
1036 fcntl_add(fd, libc::F_GETFL, libc::F_SETFL, libc::O_NONBLOCK)
1037 } else {
1038 fcntl_remove(fd, libc::F_GETFL, libc::F_SETFL, libc::O_NONBLOCK)
1039 }
1040}
1041
1042#[cfg(target_os = "vita")]
1043pub(crate) fn set_nonblocking(fd: Socket, nonblocking: bool) -> io::Result<()> {
1044 unsafe {
1045 setsockopt(
1046 fd,
1047 libc::SOL_SOCKET,
1048 libc::SO_NONBLOCK,
1049 nonblocking as libc::c_int,
1050 )
1051 }
1052}
1053
1054pub(crate) fn shutdown(fd: Socket, how: Shutdown) -> io::Result<()> {
1055 let how = match how {
1056 Shutdown::Write => libc::SHUT_WR,
1057 Shutdown::Read => libc::SHUT_RD,
1058 Shutdown::Both => libc::SHUT_RDWR,
1059 };
1060 syscall!(shutdown(fd, how)).map(|_| ())
1061}
1062
1063pub(crate) fn recv(fd: Socket, buf: &mut [MaybeUninit<u8>], flags: c_int) -> io::Result<usize> {
1064 syscall!(recv(
1065 fd,
1066 buf.as_mut_ptr().cast(),
1067 min(buf.len(), MAX_BUF_LEN),
1068 flags,
1069 ))
1070 .map(|n| n as usize)
1071}
1072
1073pub(crate) fn recv_from(
1074 fd: Socket,
1075 buf: &mut [MaybeUninit<u8>],
1076 flags: c_int,
1077) -> io::Result<(usize, SockAddr)> {
1078 unsafe {
1080 SockAddr::try_init(|addr, addrlen| {
1081 syscall!(recvfrom(
1082 fd,
1083 buf.as_mut_ptr().cast(),
1084 min(buf.len(), MAX_BUF_LEN),
1085 flags,
1086 addr.cast(),
1087 addrlen
1088 ))
1089 .map(|n| n as usize)
1090 })
1091 }
1092}
1093
1094pub(crate) fn peek_sender(fd: Socket) -> io::Result<SockAddr> {
1095 let (_, sender) = recv_from(fd, &mut [MaybeUninit::uninit(); 8], MSG_PEEK)?;
1100 Ok(sender)
1101}
1102
1103#[cfg(not(target_os = "redox"))]
1104pub(crate) fn recv_vectored(
1105 fd: Socket,
1106 bufs: &mut [crate::MaybeUninitSlice<'_>],
1107 flags: c_int,
1108) -> io::Result<(usize, RecvFlags)> {
1109 let mut msg = MsgHdrMut::new().with_buffers(bufs);
1110 let n = recvmsg(fd, &mut msg, flags)?;
1111 Ok((n, msg.flags()))
1112}
1113
1114#[cfg(not(target_os = "redox"))]
1115pub(crate) fn recv_from_vectored(
1116 fd: Socket,
1117 bufs: &mut [crate::MaybeUninitSlice<'_>],
1118 flags: c_int,
1119) -> io::Result<(usize, RecvFlags, SockAddr)> {
1120 let mut msg = MsgHdrMut::new().with_buffers(bufs);
1121 let (n, addr) = unsafe {
1124 SockAddr::try_init(|storage, len| {
1125 msg.inner.msg_name = storage.cast();
1126 msg.inner.msg_namelen = *len;
1127 let n = recvmsg(fd, &mut msg, flags)?;
1128 *len = msg.inner.msg_namelen;
1130 Ok(n)
1131 })?
1132 };
1133 Ok((n, msg.flags(), addr))
1134}
1135
1136#[cfg(not(target_os = "redox"))]
1137pub(crate) fn recvmsg(
1138 fd: Socket,
1139 msg: &mut MsgHdrMut<'_, '_, '_>,
1140 flags: c_int,
1141) -> io::Result<usize> {
1142 syscall!(recvmsg(fd, &mut msg.inner, flags)).map(|n| n as usize)
1143}
1144
1145pub(crate) fn send(fd: Socket, buf: &[u8], flags: c_int) -> io::Result<usize> {
1146 syscall!(send(
1147 fd,
1148 buf.as_ptr().cast(),
1149 min(buf.len(), MAX_BUF_LEN),
1150 flags,
1151 ))
1152 .map(|n| n as usize)
1153}
1154
1155#[cfg(not(target_os = "redox"))]
1156pub(crate) fn send_vectored(fd: Socket, bufs: &[IoSlice<'_>], flags: c_int) -> io::Result<usize> {
1157 let msg = MsgHdr::new().with_buffers(bufs);
1158 sendmsg(fd, &msg, flags)
1159}
1160
1161pub(crate) fn send_to(fd: Socket, buf: &[u8], addr: &SockAddr, flags: c_int) -> io::Result<usize> {
1162 syscall!(sendto(
1163 fd,
1164 buf.as_ptr().cast(),
1165 min(buf.len(), MAX_BUF_LEN),
1166 flags,
1167 addr.as_ptr(),
1168 addr.len(),
1169 ))
1170 .map(|n| n as usize)
1171}
1172
1173#[cfg(not(target_os = "redox"))]
1174pub(crate) fn send_to_vectored(
1175 fd: Socket,
1176 bufs: &[IoSlice<'_>],
1177 addr: &SockAddr,
1178 flags: c_int,
1179) -> io::Result<usize> {
1180 let msg = MsgHdr::new().with_addr(addr).with_buffers(bufs);
1181 sendmsg(fd, &msg, flags)
1182}
1183
1184#[cfg(not(target_os = "redox"))]
1185pub(crate) fn sendmsg(fd: Socket, msg: &MsgHdr<'_, '_, '_>, flags: c_int) -> io::Result<usize> {
1186 syscall!(sendmsg(fd, &msg.inner, flags)).map(|n| n as usize)
1187}
1188
1189pub(crate) fn timeout_opt(fd: Socket, opt: c_int, val: c_int) -> io::Result<Option<Duration>> {
1191 unsafe { getsockopt(fd, opt, val).map(from_timeval) }
1192}
1193
1194const fn from_timeval(duration: libc::timeval) -> Option<Duration> {
1195 if duration.tv_sec == 0 && duration.tv_usec == 0 {
1196 None
1197 } else {
1198 let sec = duration.tv_sec as u64;
1199 let nsec = (duration.tv_usec as u32) * 1000;
1200 Some(Duration::new(sec, nsec))
1201 }
1202}
1203
1204pub(crate) fn set_timeout_opt(
1206 fd: Socket,
1207 opt: c_int,
1208 val: c_int,
1209 duration: Option<Duration>,
1210) -> io::Result<()> {
1211 let duration = into_timeval(duration);
1212 unsafe { setsockopt(fd, opt, val, duration) }
1213}
1214
1215fn into_timeval(duration: Option<Duration>) -> libc::timeval {
1216 match duration {
1217 #[cfg_attr(target_env = "musl", allow(deprecated))]
1219 Some(duration) => libc::timeval {
1220 tv_sec: min(duration.as_secs(), libc::time_t::MAX as u64) as libc::time_t,
1221 tv_usec: duration.subsec_micros() as libc::suseconds_t,
1222 },
1223 None => libc::timeval {
1224 tv_sec: 0,
1225 tv_usec: 0,
1226 },
1227 }
1228}
1229
1230#[cfg(all(
1231 feature = "all",
1232 not(any(target_os = "haiku", target_os = "openbsd", target_os = "vita"))
1233))]
1234#[cfg_attr(
1235 docsrs,
1236 doc(cfg(all(
1237 feature = "all",
1238 not(any(target_os = "haiku", target_os = "openbsd", target_os = "vita"))
1239 )))
1240)]
1241pub(crate) fn keepalive_time(fd: Socket) -> io::Result<Duration> {
1242 unsafe {
1243 getsockopt::<c_int>(fd, IPPROTO_TCP, KEEPALIVE_TIME)
1244 .map(|secs| Duration::from_secs(secs as u64))
1245 }
1246}
1247
1248#[allow(unused_variables)]
1249pub(crate) fn set_tcp_keepalive(fd: Socket, keepalive: &TcpKeepalive) -> io::Result<()> {
1250 #[cfg(not(any(
1251 target_os = "haiku",
1252 target_os = "openbsd",
1253 target_os = "nto",
1254 target_os = "vita"
1255 )))]
1256 if let Some(time) = keepalive.time {
1257 let secs = into_secs(time);
1258 unsafe { setsockopt(fd, libc::IPPROTO_TCP, KEEPALIVE_TIME, secs)? }
1259 }
1260
1261 #[cfg(any(
1262 target_os = "aix",
1263 target_os = "android",
1264 target_os = "dragonfly",
1265 target_os = "freebsd",
1266 target_os = "fuchsia",
1267 target_os = "hurd",
1268 target_os = "illumos",
1269 target_os = "ios",
1270 target_os = "visionos",
1271 target_os = "linux",
1272 target_os = "macos",
1273 target_os = "netbsd",
1274 target_os = "tvos",
1275 target_os = "watchos",
1276 ))]
1277 {
1278 if let Some(interval) = keepalive.interval {
1279 let secs = into_secs(interval);
1280 unsafe { setsockopt(fd, libc::IPPROTO_TCP, libc::TCP_KEEPINTVL, secs)? }
1281 }
1282
1283 if let Some(retries) = keepalive.retries {
1284 unsafe { setsockopt(fd, libc::IPPROTO_TCP, libc::TCP_KEEPCNT, retries as c_int)? }
1285 }
1286 }
1287
1288 #[cfg(target_os = "nto")]
1289 if let Some(time) = keepalive.time {
1290 let secs = into_timeval(Some(time));
1291 unsafe { setsockopt(fd, libc::IPPROTO_TCP, KEEPALIVE_TIME, secs)? }
1292 }
1293
1294 Ok(())
1295}
1296
1297#[cfg(not(any(
1298 target_os = "haiku",
1299 target_os = "openbsd",
1300 target_os = "nto",
1301 target_os = "vita"
1302)))]
1303fn into_secs(duration: Duration) -> c_int {
1304 min(duration.as_secs(), c_int::MAX as u64) as c_int
1305}
1306
1307#[cfg(not(target_os = "vita"))]
1309fn fcntl_get(fd: Socket, cmd: c_int) -> io::Result<c_int> {
1310 syscall!(fcntl(fd, cmd))
1311}
1312
1313#[cfg(not(target_os = "vita"))]
1315fn fcntl_add(fd: Socket, get_cmd: c_int, set_cmd: c_int, flag: c_int) -> io::Result<()> {
1316 let previous = fcntl_get(fd, get_cmd)?;
1317 let new = previous | flag;
1318 if new != previous {
1319 syscall!(fcntl(fd, set_cmd, new)).map(|_| ())
1320 } else {
1321 Ok(())
1323 }
1324}
1325
1326#[cfg(not(target_os = "vita"))]
1328fn fcntl_remove(fd: Socket, get_cmd: c_int, set_cmd: c_int, flag: c_int) -> io::Result<()> {
1329 let previous = fcntl_get(fd, get_cmd)?;
1330 let new = previous & !flag;
1331 if new != previous {
1332 syscall!(fcntl(fd, set_cmd, new)).map(|_| ())
1333 } else {
1334 Ok(())
1336 }
1337}
1338
1339pub(crate) unsafe fn getsockopt<T>(fd: Socket, opt: c_int, val: c_int) -> io::Result<T> {
1341 let mut payload: MaybeUninit<T> = MaybeUninit::uninit();
1342 let mut len = size_of::<T>() as libc::socklen_t;
1343 syscall!(getsockopt(
1344 fd,
1345 opt,
1346 val,
1347 payload.as_mut_ptr().cast(),
1348 &mut len,
1349 ))
1350 .map(|_| {
1351 debug_assert_eq!(len as usize, size_of::<T>());
1352 payload.assume_init()
1354 })
1355}
1356
1357pub(crate) unsafe fn setsockopt<T>(
1359 fd: Socket,
1360 opt: c_int,
1361 val: c_int,
1362 payload: T,
1363) -> io::Result<()> {
1364 let payload = ptr::addr_of!(payload).cast();
1365 syscall!(setsockopt(
1366 fd,
1367 opt,
1368 val,
1369 payload,
1370 mem::size_of::<T>() as libc::socklen_t,
1371 ))
1372 .map(|_| ())
1373}
1374
1375pub(crate) const fn to_in_addr(addr: &Ipv4Addr) -> in_addr {
1376 in_addr {
1380 s_addr: u32::from_ne_bytes(addr.octets()),
1381 }
1382}
1383
1384pub(crate) fn from_in_addr(in_addr: in_addr) -> Ipv4Addr {
1385 Ipv4Addr::from(in_addr.s_addr.to_ne_bytes())
1386}
1387
1388pub(crate) const fn to_in6_addr(addr: &Ipv6Addr) -> in6_addr {
1389 in6_addr {
1390 s6_addr: addr.octets(),
1391 }
1392}
1393
1394pub(crate) fn from_in6_addr(addr: in6_addr) -> Ipv6Addr {
1395 Ipv6Addr::from(addr.s6_addr)
1396}
1397
1398#[cfg(not(any(
1399 target_os = "aix",
1400 target_os = "haiku",
1401 target_os = "illumos",
1402 target_os = "netbsd",
1403 target_os = "openbsd",
1404 target_os = "redox",
1405 target_os = "solaris",
1406 target_os = "nto",
1407 target_os = "espidf",
1408 target_os = "vita",
1409)))]
1410pub(crate) const fn to_mreqn(
1411 multiaddr: &Ipv4Addr,
1412 interface: &crate::socket::InterfaceIndexOrAddress,
1413) -> libc::ip_mreqn {
1414 match interface {
1415 crate::socket::InterfaceIndexOrAddress::Index(interface) => libc::ip_mreqn {
1416 imr_multiaddr: to_in_addr(multiaddr),
1417 imr_address: to_in_addr(&Ipv4Addr::UNSPECIFIED),
1418 imr_ifindex: *interface as _,
1419 },
1420 crate::socket::InterfaceIndexOrAddress::Address(interface) => libc::ip_mreqn {
1421 imr_multiaddr: to_in_addr(multiaddr),
1422 imr_address: to_in_addr(interface),
1423 imr_ifindex: 0,
1424 },
1425 }
1426}
1427
1428#[cfg(all(
1429 feature = "all",
1430 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
1431))]
1432pub(crate) fn original_dst(fd: Socket) -> io::Result<SockAddr> {
1433 unsafe {
1435 SockAddr::try_init(|storage, len| {
1436 syscall!(getsockopt(
1437 fd,
1438 libc::SOL_IP,
1439 libc::SO_ORIGINAL_DST,
1440 storage.cast(),
1441 len
1442 ))
1443 })
1444 }
1445 .map(|(_, addr)| addr)
1446}
1447
1448#[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))]
1453pub(crate) fn original_dst_ipv6(fd: Socket) -> io::Result<SockAddr> {
1454 unsafe {
1456 SockAddr::try_init(|storage, len| {
1457 syscall!(getsockopt(
1458 fd,
1459 libc::SOL_IPV6,
1460 libc::IP6T_SO_ORIGINAL_DST,
1461 storage.cast(),
1462 len
1463 ))
1464 })
1465 }
1466 .map(|(_, addr)| addr)
1467}
1468
1469impl crate::Socket {
1471 #[doc = man_links!(unix: accept4(2))]
1479 #[cfg(all(
1480 feature = "all",
1481 any(
1482 target_os = "android",
1483 target_os = "dragonfly",
1484 target_os = "freebsd",
1485 target_os = "fuchsia",
1486 target_os = "illumos",
1487 target_os = "linux",
1488 target_os = "netbsd",
1489 target_os = "openbsd",
1490 )
1491 ))]
1492 #[cfg_attr(
1493 docsrs,
1494 doc(cfg(all(
1495 feature = "all",
1496 any(
1497 target_os = "android",
1498 target_os = "dragonfly",
1499 target_os = "freebsd",
1500 target_os = "fuchsia",
1501 target_os = "illumos",
1502 target_os = "linux",
1503 target_os = "netbsd",
1504 target_os = "openbsd",
1505 )
1506 )))
1507 )]
1508 pub fn accept4(&self, flags: c_int) -> io::Result<(crate::Socket, SockAddr)> {
1509 self._accept4(flags)
1510 }
1511
1512 #[cfg(any(
1513 target_os = "android",
1514 target_os = "dragonfly",
1515 target_os = "freebsd",
1516 target_os = "fuchsia",
1517 target_os = "illumos",
1518 target_os = "linux",
1519 target_os = "netbsd",
1520 target_os = "openbsd",
1521 ))]
1522 pub(crate) fn _accept4(&self, flags: c_int) -> io::Result<(crate::Socket, SockAddr)> {
1523 unsafe {
1525 SockAddr::try_init(|storage, len| {
1526 syscall!(accept4(self.as_raw(), storage.cast(), len, flags))
1527 .map(crate::Socket::from_raw)
1528 })
1529 }
1530 }
1531
1532 #[cfg_attr(
1538 any(
1539 target_os = "ios",
1540 target_os = "visionos",
1541 target_os = "macos",
1542 target_os = "tvos",
1543 target_os = "watchos"
1544 ),
1545 allow(rustdoc::broken_intra_doc_links)
1546 )]
1547 #[cfg(all(feature = "all", not(target_os = "vita")))]
1548 #[cfg_attr(docsrs, doc(cfg(all(feature = "all", unix))))]
1549 pub fn set_cloexec(&self, close_on_exec: bool) -> io::Result<()> {
1550 self._set_cloexec(close_on_exec)
1551 }
1552
1553 #[cfg(not(target_os = "vita"))]
1554 pub(crate) fn _set_cloexec(&self, close_on_exec: bool) -> io::Result<()> {
1555 if close_on_exec {
1556 fcntl_add(
1557 self.as_raw(),
1558 libc::F_GETFD,
1559 libc::F_SETFD,
1560 libc::FD_CLOEXEC,
1561 )
1562 } else {
1563 fcntl_remove(
1564 self.as_raw(),
1565 libc::F_GETFD,
1566 libc::F_SETFD,
1567 libc::FD_CLOEXEC,
1568 )
1569 }
1570 }
1571
1572 #[cfg(all(
1574 feature = "all",
1575 any(
1576 target_os = "ios",
1577 target_os = "visionos",
1578 target_os = "macos",
1579 target_os = "tvos",
1580 target_os = "watchos",
1581 )
1582 ))]
1583 #[cfg_attr(
1584 docsrs,
1585 doc(cfg(all(
1586 feature = "all",
1587 any(
1588 target_os = "ios",
1589 target_os = "visionos",
1590 target_os = "macos",
1591 target_os = "tvos",
1592 target_os = "watchos",
1593 )
1594 )))
1595 )]
1596 pub fn set_nosigpipe(&self, nosigpipe: bool) -> io::Result<()> {
1597 self._set_nosigpipe(nosigpipe)
1598 }
1599
1600 #[cfg(any(
1601 target_os = "ios",
1602 target_os = "visionos",
1603 target_os = "macos",
1604 target_os = "tvos",
1605 target_os = "watchos",
1606 ))]
1607 pub(crate) fn _set_nosigpipe(&self, nosigpipe: bool) -> io::Result<()> {
1608 unsafe {
1609 setsockopt(
1610 self.as_raw(),
1611 libc::SOL_SOCKET,
1612 libc::SO_NOSIGPIPE,
1613 nosigpipe as c_int,
1614 )
1615 }
1616 }
1617
1618 #[cfg(all(feature = "all", not(target_os = "redox")))]
1624 #[cfg_attr(docsrs, doc(cfg(all(feature = "all", unix, not(target_os = "redox")))))]
1625 pub fn mss(&self) -> io::Result<u32> {
1626 unsafe {
1627 getsockopt::<c_int>(self.as_raw(), libc::IPPROTO_TCP, libc::TCP_MAXSEG)
1628 .map(|mss| mss as u32)
1629 }
1630 }
1631
1632 #[cfg(all(feature = "all", not(target_os = "redox")))]
1637 #[cfg_attr(docsrs, doc(cfg(all(feature = "all", unix, not(target_os = "redox")))))]
1638 pub fn set_mss(&self, mss: u32) -> io::Result<()> {
1639 unsafe {
1640 setsockopt(
1641 self.as_raw(),
1642 libc::IPPROTO_TCP,
1643 libc::TCP_MAXSEG,
1644 mss as c_int,
1645 )
1646 }
1647 }
1648
1649 #[cfg(all(
1652 feature = "all",
1653 any(
1654 target_os = "aix",
1655 target_os = "android",
1656 target_os = "freebsd",
1657 target_os = "fuchsia",
1658 target_os = "linux",
1659 )
1660 ))]
1661 #[cfg_attr(
1662 docsrs,
1663 doc(cfg(all(
1664 feature = "all",
1665 any(
1666 target_os = "aix",
1667 target_os = "android",
1668 target_os = "freebsd",
1669 target_os = "fuchsia",
1670 target_os = "linux",
1671 )
1672 )))
1673 )]
1674 pub fn is_listener(&self) -> io::Result<bool> {
1675 unsafe {
1676 getsockopt::<c_int>(self.as_raw(), libc::SOL_SOCKET, libc::SO_ACCEPTCONN)
1677 .map(|v| v != 0)
1678 }
1679 }
1680
1681 #[cfg(all(
1684 feature = "all",
1685 any(
1686 target_os = "android",
1687 target_os = "fuchsia",
1690 target_os = "linux",
1691 )
1692 ))]
1693 #[cfg_attr(docsrs, doc(cfg(all(
1694 feature = "all",
1695 any(
1696 target_os = "android",
1697 target_os = "fuchsia",
1700 target_os = "linux",
1701 )
1702 ))))]
1703 pub fn domain(&self) -> io::Result<Domain> {
1704 unsafe { getsockopt::<c_int>(self.as_raw(), libc::SOL_SOCKET, libc::SO_DOMAIN).map(Domain) }
1705 }
1706
1707 #[cfg(all(
1710 feature = "all",
1711 any(
1712 target_os = "android",
1713 target_os = "freebsd",
1714 target_os = "fuchsia",
1715 target_os = "linux",
1716 )
1717 ))]
1718 #[cfg_attr(
1719 docsrs,
1720 doc(cfg(all(
1721 feature = "all",
1722 any(
1723 target_os = "android",
1724 target_os = "freebsd",
1725 target_os = "fuchsia",
1726 target_os = "linux",
1727 )
1728 )))
1729 )]
1730 pub fn protocol(&self) -> io::Result<Option<Protocol>> {
1731 unsafe {
1732 getsockopt::<c_int>(self.as_raw(), libc::SOL_SOCKET, libc::SO_PROTOCOL).map(|v| match v
1733 {
1734 0 => None,
1735 p => Some(Protocol(p)),
1736 })
1737 }
1738 }
1739
1740 #[cfg(all(
1747 feature = "all",
1748 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
1749 ))]
1750 #[cfg_attr(
1751 docsrs,
1752 doc(cfg(all(
1753 feature = "all",
1754 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
1755 )))
1756 )]
1757 pub fn mark(&self) -> io::Result<u32> {
1758 unsafe {
1759 getsockopt::<c_int>(self.as_raw(), libc::SOL_SOCKET, libc::SO_MARK)
1760 .map(|mark| mark as u32)
1761 }
1762 }
1763
1764 #[cfg(all(
1772 feature = "all",
1773 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
1774 ))]
1775 #[cfg_attr(
1776 docsrs,
1777 doc(cfg(all(
1778 feature = "all",
1779 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
1780 )))
1781 )]
1782 pub fn set_mark(&self, mark: u32) -> io::Result<()> {
1783 unsafe {
1784 setsockopt::<c_int>(
1785 self.as_raw(),
1786 libc::SOL_SOCKET,
1787 libc::SO_MARK,
1788 mark as c_int,
1789 )
1790 }
1791 }
1792
1793 #[cfg(all(
1799 feature = "all",
1800 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
1801 ))]
1802 #[cfg_attr(
1803 docsrs,
1804 doc(cfg(all(
1805 feature = "all",
1806 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
1807 )))
1808 )]
1809 pub fn cork(&self) -> io::Result<bool> {
1810 unsafe {
1811 getsockopt::<Bool>(self.as_raw(), libc::IPPROTO_TCP, libc::TCP_CORK)
1812 .map(|cork| cork != 0)
1813 }
1814 }
1815
1816 #[cfg(all(
1823 feature = "all",
1824 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
1825 ))]
1826 #[cfg_attr(
1827 docsrs,
1828 doc(cfg(all(
1829 feature = "all",
1830 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
1831 )))
1832 )]
1833 pub fn set_cork(&self, cork: bool) -> io::Result<()> {
1834 unsafe {
1835 setsockopt(
1836 self.as_raw(),
1837 libc::IPPROTO_TCP,
1838 libc::TCP_CORK,
1839 cork as c_int,
1840 )
1841 }
1842 }
1843
1844 #[cfg(all(
1850 feature = "all",
1851 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
1852 ))]
1853 #[cfg_attr(
1854 docsrs,
1855 doc(cfg(all(
1856 feature = "all",
1857 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
1858 )))
1859 )]
1860 pub fn quickack(&self) -> io::Result<bool> {
1861 unsafe {
1862 getsockopt::<Bool>(self.as_raw(), libc::IPPROTO_TCP, libc::TCP_QUICKACK)
1863 .map(|quickack| quickack != 0)
1864 }
1865 }
1866
1867 #[cfg(all(
1874 feature = "all",
1875 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
1876 ))]
1877 #[cfg_attr(
1878 docsrs,
1879 doc(cfg(all(
1880 feature = "all",
1881 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
1882 )))
1883 )]
1884 pub fn set_quickack(&self, quickack: bool) -> io::Result<()> {
1885 unsafe {
1886 setsockopt(
1887 self.as_raw(),
1888 libc::IPPROTO_TCP,
1889 libc::TCP_QUICKACK,
1890 quickack as c_int,
1891 )
1892 }
1893 }
1894
1895 #[cfg(all(
1901 feature = "all",
1902 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
1903 ))]
1904 #[cfg_attr(
1905 docsrs,
1906 doc(cfg(all(
1907 feature = "all",
1908 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
1909 )))
1910 )]
1911 pub fn thin_linear_timeouts(&self) -> io::Result<bool> {
1912 unsafe {
1913 getsockopt::<Bool>(
1914 self.as_raw(),
1915 libc::IPPROTO_TCP,
1916 libc::TCP_THIN_LINEAR_TIMEOUTS,
1917 )
1918 .map(|timeouts| timeouts != 0)
1919 }
1920 }
1921
1922 #[cfg(all(
1928 feature = "all",
1929 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
1930 ))]
1931 #[cfg_attr(
1932 docsrs,
1933 doc(cfg(all(
1934 feature = "all",
1935 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
1936 )))
1937 )]
1938 pub fn set_thin_linear_timeouts(&self, timeouts: bool) -> io::Result<()> {
1939 unsafe {
1940 setsockopt(
1941 self.as_raw(),
1942 libc::IPPROTO_TCP,
1943 libc::TCP_THIN_LINEAR_TIMEOUTS,
1944 timeouts as c_int,
1945 )
1946 }
1947 }
1948
1949 #[cfg(all(
1953 feature = "all",
1954 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
1955 ))]
1956 #[cfg_attr(
1957 docsrs,
1958 doc(cfg(all(
1959 feature = "all",
1960 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
1961 )))
1962 )]
1963 pub fn device(&self) -> io::Result<Option<Vec<u8>>> {
1964 let mut buf: [MaybeUninit<u8>; libc::IFNAMSIZ] =
1966 unsafe { MaybeUninit::uninit().assume_init() };
1967 let mut len = buf.len() as libc::socklen_t;
1968 syscall!(getsockopt(
1969 self.as_raw(),
1970 libc::SOL_SOCKET,
1971 libc::SO_BINDTODEVICE,
1972 buf.as_mut_ptr().cast(),
1973 &mut len,
1974 ))?;
1975 if len == 0 {
1976 Ok(None)
1977 } else {
1978 let buf = &buf[..len as usize - 1];
1979 Ok(Some(unsafe { &*(buf as *const [_] as *const [u8]) }.into()))
1981 }
1982 }
1983
1984 #[cfg(all(
1992 feature = "all",
1993 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
1994 ))]
1995 #[cfg_attr(
1996 docsrs,
1997 doc(cfg(all(
1998 feature = "all",
1999 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
2000 )))
2001 )]
2002 pub fn bind_device(&self, interface: Option<&[u8]>) -> io::Result<()> {
2003 let (value, len) = if let Some(interface) = interface {
2004 (interface.as_ptr(), interface.len())
2005 } else {
2006 (ptr::null(), 0)
2007 };
2008 syscall!(setsockopt(
2009 self.as_raw(),
2010 libc::SOL_SOCKET,
2011 libc::SO_BINDTODEVICE,
2012 value.cast(),
2013 len as libc::socklen_t,
2014 ))
2015 .map(|_| ())
2016 }
2017
2018 #[cfg(all(feature = "all", target_os = "freebsd"))]
2022 #[cfg_attr(docsrs, doc(cfg(all(feature = "all", target_os = "freebsd"))))]
2023 pub fn set_fib(&self, fib: u32) -> io::Result<()> {
2024 syscall!(setsockopt(
2025 self.as_raw(),
2026 libc::SOL_SOCKET,
2027 libc::SO_SETFIB,
2028 (&fib as *const u32).cast(),
2029 mem::size_of::<u32>() as libc::socklen_t,
2030 ))
2031 .map(|_| ())
2032 }
2033
2034 #[cfg(all(
2036 feature = "all",
2037 any(
2038 target_os = "ios",
2039 target_os = "visionos",
2040 target_os = "macos",
2041 target_os = "tvos",
2042 target_os = "watchos",
2043 )
2044 ))]
2045 #[cfg_attr(
2046 docsrs,
2047 doc(cfg(all(
2048 feature = "all",
2049 any(
2050 target_os = "ios",
2051 target_os = "visionos",
2052 target_os = "macos",
2053 target_os = "tvos",
2054 target_os = "watchos",
2055 )
2056 )))
2057 )]
2058 #[deprecated = "Use `Socket::bind_device_by_index_v4` instead"]
2059 pub fn bind_device_by_index(&self, interface: Option<NonZeroU32>) -> io::Result<()> {
2060 self.bind_device_by_index_v4(interface)
2061 }
2062
2063 #[cfg(all(
2074 feature = "all",
2075 any(
2076 target_os = "ios",
2077 target_os = "visionos",
2078 target_os = "macos",
2079 target_os = "tvos",
2080 target_os = "watchos",
2081 target_os = "illumos",
2082 target_os = "solaris",
2083 )
2084 ))]
2085 #[cfg_attr(
2086 docsrs,
2087 doc(cfg(all(
2088 feature = "all",
2089 any(
2090 target_os = "ios",
2091 target_os = "visionos",
2092 target_os = "macos",
2093 target_os = "tvos",
2094 target_os = "watchos",
2095 )
2096 )))
2097 )]
2098 pub fn bind_device_by_index_v4(&self, interface: Option<NonZeroU32>) -> io::Result<()> {
2099 let index = interface.map_or(0, NonZeroU32::get);
2100 unsafe { setsockopt(self.as_raw(), IPPROTO_IP, libc::IP_BOUND_IF, index) }
2101 }
2102
2103 #[cfg(all(
2114 feature = "all",
2115 any(
2116 target_os = "ios",
2117 target_os = "visionos",
2118 target_os = "macos",
2119 target_os = "tvos",
2120 target_os = "watchos",
2121 target_os = "illumos",
2122 target_os = "solaris",
2123 )
2124 ))]
2125 #[cfg_attr(
2126 docsrs,
2127 doc(cfg(all(
2128 feature = "all",
2129 any(
2130 target_os = "ios",
2131 target_os = "visionos",
2132 target_os = "macos",
2133 target_os = "tvos",
2134 target_os = "watchos",
2135 )
2136 )))
2137 )]
2138 pub fn bind_device_by_index_v6(&self, interface: Option<NonZeroU32>) -> io::Result<()> {
2139 let index = interface.map_or(0, NonZeroU32::get);
2140 unsafe { setsockopt(self.as_raw(), IPPROTO_IPV6, libc::IPV6_BOUND_IF, index) }
2141 }
2142
2143 #[cfg(all(
2149 feature = "all",
2150 any(
2151 target_os = "ios",
2152 target_os = "visionos",
2153 target_os = "macos",
2154 target_os = "tvos",
2155 target_os = "watchos",
2156 target_os = "illumos",
2157 target_os = "solaris",
2158 )
2159 ))]
2160 #[cfg_attr(
2161 docsrs,
2162 doc(cfg(all(
2163 feature = "all",
2164 any(
2165 target_os = "ios",
2166 target_os = "visionos",
2167 target_os = "macos",
2168 target_os = "tvos",
2169 target_os = "watchos",
2170 )
2171 )))
2172 )]
2173 pub fn device_index_v4(&self) -> io::Result<Option<NonZeroU32>> {
2174 let index =
2175 unsafe { getsockopt::<libc::c_uint>(self.as_raw(), IPPROTO_IP, libc::IP_BOUND_IF)? };
2176 Ok(NonZeroU32::new(index))
2177 }
2178
2179 #[cfg(all(
2181 feature = "all",
2182 any(
2183 target_os = "ios",
2184 target_os = "visionos",
2185 target_os = "macos",
2186 target_os = "tvos",
2187 target_os = "watchos",
2188 )
2189 ))]
2190 #[cfg_attr(
2191 docsrs,
2192 doc(cfg(all(
2193 feature = "all",
2194 any(
2195 target_os = "ios",
2196 target_os = "visionos",
2197 target_os = "macos",
2198 target_os = "tvos",
2199 target_os = "watchos",
2200 )
2201 )))
2202 )]
2203 #[deprecated = "Use `Socket::device_index_v4` instead"]
2204 pub fn device_index(&self) -> io::Result<Option<NonZeroU32>> {
2205 self.device_index_v4()
2206 }
2207
2208 #[cfg(all(
2214 feature = "all",
2215 any(
2216 target_os = "ios",
2217 target_os = "visionos",
2218 target_os = "macos",
2219 target_os = "tvos",
2220 target_os = "watchos",
2221 target_os = "illumos",
2222 target_os = "solaris",
2223 )
2224 ))]
2225 #[cfg_attr(
2226 docsrs,
2227 doc(cfg(all(
2228 feature = "all",
2229 any(
2230 target_os = "ios",
2231 target_os = "visionos",
2232 target_os = "macos",
2233 target_os = "tvos",
2234 target_os = "watchos",
2235 )
2236 )))
2237 )]
2238 pub fn device_index_v6(&self) -> io::Result<Option<NonZeroU32>> {
2239 let index = unsafe {
2240 getsockopt::<libc::c_uint>(self.as_raw(), IPPROTO_IPV6, libc::IPV6_BOUND_IF)?
2241 };
2242 Ok(NonZeroU32::new(index))
2243 }
2244
2245 #[cfg(all(feature = "all", target_os = "linux"))]
2251 #[cfg_attr(docsrs, doc(cfg(all(feature = "all", target_os = "linux"))))]
2252 pub fn cpu_affinity(&self) -> io::Result<usize> {
2253 unsafe {
2254 getsockopt::<c_int>(self.as_raw(), libc::SOL_SOCKET, libc::SO_INCOMING_CPU)
2255 .map(|cpu| cpu as usize)
2256 }
2257 }
2258
2259 #[cfg(all(feature = "all", target_os = "linux"))]
2263 #[cfg_attr(docsrs, doc(cfg(all(feature = "all", target_os = "linux"))))]
2264 pub fn set_cpu_affinity(&self, cpu: usize) -> io::Result<()> {
2265 unsafe {
2266 setsockopt(
2267 self.as_raw(),
2268 libc::SOL_SOCKET,
2269 libc::SO_INCOMING_CPU,
2270 cpu as c_int,
2271 )
2272 }
2273 }
2274
2275 #[cfg(all(
2281 feature = "all",
2282 not(any(target_os = "solaris", target_os = "illumos"))
2283 ))]
2284 #[cfg_attr(
2285 docsrs,
2286 doc(cfg(all(
2287 feature = "all",
2288 unix,
2289 not(any(target_os = "solaris", target_os = "illumos"))
2290 )))
2291 )]
2292 pub fn reuse_port(&self) -> io::Result<bool> {
2293 unsafe {
2294 getsockopt::<c_int>(self.as_raw(), libc::SOL_SOCKET, libc::SO_REUSEPORT)
2295 .map(|reuse| reuse != 0)
2296 }
2297 }
2298
2299 #[cfg(all(
2305 feature = "all",
2306 not(any(target_os = "solaris", target_os = "illumos"))
2307 ))]
2308 #[cfg_attr(
2309 docsrs,
2310 doc(cfg(all(
2311 feature = "all",
2312 unix,
2313 not(any(target_os = "solaris", target_os = "illumos"))
2314 )))
2315 )]
2316 pub fn set_reuse_port(&self, reuse: bool) -> io::Result<()> {
2317 unsafe {
2318 setsockopt(
2319 self.as_raw(),
2320 libc::SOL_SOCKET,
2321 libc::SO_REUSEPORT,
2322 reuse as c_int,
2323 )
2324 }
2325 }
2326
2327 #[cfg(all(feature = "all", target_os = "freebsd"))]
2333 pub fn reuse_port_lb(&self) -> io::Result<bool> {
2334 unsafe {
2335 getsockopt::<c_int>(self.as_raw(), libc::SOL_SOCKET, libc::SO_REUSEPORT_LB)
2336 .map(|reuse| reuse != 0)
2337 }
2338 }
2339
2340 #[cfg(all(feature = "all", target_os = "freebsd"))]
2345 pub fn set_reuse_port_lb(&self, reuse: bool) -> io::Result<()> {
2346 unsafe {
2347 setsockopt(
2348 self.as_raw(),
2349 libc::SOL_SOCKET,
2350 libc::SO_REUSEPORT_LB,
2351 reuse as c_int,
2352 )
2353 }
2354 }
2355
2356 #[cfg(all(
2362 feature = "all",
2363 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
2364 ))]
2365 #[cfg_attr(
2366 docsrs,
2367 doc(cfg(all(
2368 feature = "all",
2369 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
2370 )))
2371 )]
2372 pub fn freebind(&self) -> io::Result<bool> {
2373 unsafe {
2374 getsockopt::<c_int>(self.as_raw(), libc::SOL_IP, libc::IP_FREEBIND)
2375 .map(|freebind| freebind != 0)
2376 }
2377 }
2378
2379 #[cfg(all(
2387 feature = "all",
2388 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
2389 ))]
2390 #[cfg_attr(
2391 docsrs,
2392 doc(cfg(all(
2393 feature = "all",
2394 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
2395 )))
2396 )]
2397 pub fn set_freebind(&self, freebind: bool) -> io::Result<()> {
2398 unsafe {
2399 setsockopt(
2400 self.as_raw(),
2401 libc::SOL_IP,
2402 libc::IP_FREEBIND,
2403 freebind as c_int,
2404 )
2405 }
2406 }
2407
2408 #[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))]
2416 #[cfg_attr(
2417 docsrs,
2418 doc(cfg(all(feature = "all", any(target_os = "android", target_os = "linux"))))
2419 )]
2420 pub fn freebind_ipv6(&self) -> io::Result<bool> {
2421 unsafe {
2422 getsockopt::<c_int>(self.as_raw(), libc::SOL_IPV6, libc::IPV6_FREEBIND)
2423 .map(|freebind| freebind != 0)
2424 }
2425 }
2426
2427 #[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))]
2458 #[cfg_attr(
2459 docsrs,
2460 doc(cfg(all(feature = "all", any(target_os = "android", target_os = "linux"))))
2461 )]
2462 pub fn set_freebind_ipv6(&self, freebind: bool) -> io::Result<()> {
2463 unsafe {
2464 setsockopt(
2465 self.as_raw(),
2466 libc::SOL_IPV6,
2467 libc::IPV6_FREEBIND,
2468 freebind as c_int,
2469 )
2470 }
2471 }
2472
2473 #[doc = man_links!(unix: sendfile(2))]
2483 #[cfg(all(
2493 feature = "all",
2494 any(
2495 target_os = "aix",
2496 target_os = "android",
2497 target_os = "freebsd",
2498 target_os = "ios",
2499 target_os = "visionos",
2500 target_os = "linux",
2501 target_os = "macos",
2502 target_os = "tvos",
2503 target_os = "watchos",
2504 )
2505 ))]
2506 #[cfg_attr(
2507 docsrs,
2508 doc(cfg(all(
2509 feature = "all",
2510 any(
2511 target_os = "aix",
2512 target_os = "android",
2513 target_os = "freebsd",
2514 target_os = "ios",
2515 target_os = "visionos",
2516 target_os = "linux",
2517 target_os = "macos",
2518 target_os = "tvos",
2519 target_os = "watchos",
2520 )
2521 )))
2522 )]
2523 pub fn sendfile<F>(
2524 &self,
2525 file: &F,
2526 offset: usize,
2527 length: Option<NonZeroUsize>,
2528 ) -> io::Result<usize>
2529 where
2530 F: AsRawFd,
2531 {
2532 self._sendfile(file.as_raw_fd(), offset as _, length)
2533 }
2534
2535 #[cfg(all(
2536 feature = "all",
2537 any(
2538 target_os = "ios",
2539 target_os = "visionos",
2540 target_os = "macos",
2541 target_os = "tvos",
2542 target_os = "watchos",
2543 )
2544 ))]
2545 fn _sendfile(
2546 &self,
2547 file: RawFd,
2548 offset: libc::off_t,
2549 length: Option<NonZeroUsize>,
2550 ) -> io::Result<usize> {
2551 let mut length = match length {
2554 Some(n) => n.get() as libc::off_t,
2555 None => 0,
2557 };
2558 syscall!(sendfile(
2559 file,
2560 self.as_raw(),
2561 offset,
2562 &mut length,
2563 ptr::null_mut(),
2564 0,
2565 ))
2566 .map(|_| length as usize)
2567 }
2568
2569 #[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))]
2570 fn _sendfile(
2571 &self,
2572 file: RawFd,
2573 offset: libc::off_t,
2574 length: Option<NonZeroUsize>,
2575 ) -> io::Result<usize> {
2576 let count = match length {
2577 Some(n) => n.get() as libc::size_t,
2578 None => 0x7ffff000, };
2581 let mut offset = offset;
2582 syscall!(sendfile(self.as_raw(), file, &mut offset, count)).map(|n| n as usize)
2583 }
2584
2585 #[cfg(all(feature = "all", target_os = "freebsd"))]
2586 fn _sendfile(
2587 &self,
2588 file: RawFd,
2589 offset: libc::off_t,
2590 length: Option<NonZeroUsize>,
2591 ) -> io::Result<usize> {
2592 let nbytes = match length {
2593 Some(n) => n.get() as libc::size_t,
2594 None => 0,
2596 };
2597 let mut sbytes: libc::off_t = 0;
2598 syscall!(sendfile(
2599 file,
2600 self.as_raw(),
2601 offset,
2602 nbytes,
2603 ptr::null_mut(),
2604 &mut sbytes,
2605 0,
2606 ))
2607 .map(|_| sbytes as usize)
2608 }
2609
2610 #[cfg(all(feature = "all", target_os = "aix"))]
2611 fn _sendfile(
2612 &self,
2613 file: RawFd,
2614 offset: libc::off_t,
2615 length: Option<NonZeroUsize>,
2616 ) -> io::Result<usize> {
2617 let nbytes = match length {
2618 Some(n) => n.get() as i64,
2619 None => -1,
2620 };
2621 let mut params = libc::sf_parms {
2622 header_data: ptr::null_mut(),
2623 header_length: 0,
2624 file_descriptor: file,
2625 file_size: 0,
2626 file_offset: offset as u64,
2627 file_bytes: nbytes,
2628 trailer_data: ptr::null_mut(),
2629 trailer_length: 0,
2630 bytes_sent: 0,
2631 };
2632 syscall!(send_file(
2634 &mut self.as_raw() as *mut _,
2635 &mut params as *mut _,
2636 libc::SF_CLOSE as libc::c_uint,
2637 ))
2638 .map(|_| params.bytes_sent as usize)
2639 }
2640
2641 #[cfg(all(
2652 feature = "all",
2653 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
2654 ))]
2655 #[cfg_attr(
2656 docsrs,
2657 doc(cfg(all(
2658 feature = "all",
2659 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
2660 )))
2661 )]
2662 pub fn set_tcp_user_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
2663 let timeout = timeout.map_or(0, |to| {
2664 min(to.as_millis(), libc::c_uint::MAX as u128) as libc::c_uint
2665 });
2666 unsafe {
2667 setsockopt(
2668 self.as_raw(),
2669 libc::IPPROTO_TCP,
2670 libc::TCP_USER_TIMEOUT,
2671 timeout,
2672 )
2673 }
2674 }
2675
2676 #[cfg(all(
2682 feature = "all",
2683 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
2684 ))]
2685 #[cfg_attr(
2686 docsrs,
2687 doc(cfg(all(
2688 feature = "all",
2689 any(target_os = "android", target_os = "fuchsia", target_os = "linux")
2690 )))
2691 )]
2692 pub fn tcp_user_timeout(&self) -> io::Result<Option<Duration>> {
2693 unsafe {
2694 getsockopt::<libc::c_uint>(self.as_raw(), libc::IPPROTO_TCP, libc::TCP_USER_TIMEOUT)
2695 .map(|millis| {
2696 if millis == 0 {
2697 None
2698 } else {
2699 Some(Duration::from_millis(millis as u64))
2700 }
2701 })
2702 }
2703 }
2704
2705 #[cfg(all(feature = "all", any(target_os = "linux", target_os = "android")))]
2712 pub fn attach_filter(&self, filters: &[libc::sock_filter]) -> io::Result<()> {
2713 let prog = libc::sock_fprog {
2714 len: filters.len() as u16,
2715 filter: filters.as_ptr() as *mut _,
2716 };
2717
2718 unsafe {
2719 setsockopt(
2720 self.as_raw(),
2721 libc::SOL_SOCKET,
2722 libc::SO_ATTACH_FILTER,
2723 prog,
2724 )
2725 }
2726 }
2727
2728 #[cfg(all(feature = "all", any(target_os = "linux", target_os = "android")))]
2734 pub fn detach_filter(&self) -> io::Result<()> {
2735 unsafe { setsockopt(self.as_raw(), libc::SOL_SOCKET, libc::SO_DETACH_FILTER, 0) }
2736 }
2737
2738 #[cfg(all(feature = "all", target_os = "linux"))]
2745 #[cfg_attr(docsrs, doc(cfg(all(feature = "all", target_os = "linux"))))]
2746 pub fn cookie(&self) -> io::Result<u64> {
2747 unsafe { getsockopt::<libc::c_ulonglong>(self.as_raw(), libc::SOL_SOCKET, libc::SO_COOKIE) }
2748 }
2749
2750 #[cfg(all(
2756 feature = "all",
2757 any(
2758 target_os = "android",
2759 target_os = "dragonfly",
2760 target_os = "freebsd",
2761 target_os = "fuchsia",
2762 target_os = "linux",
2763 target_os = "macos",
2764 target_os = "netbsd",
2765 target_os = "openbsd"
2766 )
2767 ))]
2768 #[cfg_attr(
2769 docsrs,
2770 doc(cfg(all(
2771 feature = "all",
2772 any(
2773 target_os = "android",
2774 target_os = "dragonfly",
2775 target_os = "freebsd",
2776 target_os = "fuchsia",
2777 target_os = "linux",
2778 target_os = "macos",
2779 target_os = "netbsd",
2780 target_os = "openbsd"
2781 )
2782 )))
2783 )]
2784 pub fn tclass_v6(&self) -> io::Result<u32> {
2785 unsafe {
2786 getsockopt::<c_int>(self.as_raw(), IPPROTO_IPV6, libc::IPV6_TCLASS)
2787 .map(|tclass| tclass as u32)
2788 }
2789 }
2790
2791 #[cfg(all(
2796 feature = "all",
2797 any(
2798 target_os = "android",
2799 target_os = "dragonfly",
2800 target_os = "freebsd",
2801 target_os = "fuchsia",
2802 target_os = "linux",
2803 target_os = "macos",
2804 target_os = "netbsd",
2805 target_os = "openbsd"
2806 )
2807 ))]
2808 #[cfg_attr(
2809 docsrs,
2810 doc(cfg(all(
2811 feature = "all",
2812 any(
2813 target_os = "android",
2814 target_os = "dragonfly",
2815 target_os = "freebsd",
2816 target_os = "fuchsia",
2817 target_os = "linux",
2818 target_os = "macos",
2819 target_os = "netbsd",
2820 target_os = "openbsd"
2821 )
2822 )))
2823 )]
2824 pub fn set_tclass_v6(&self, tclass: u32) -> io::Result<()> {
2825 unsafe {
2826 setsockopt(
2827 self.as_raw(),
2828 IPPROTO_IPV6,
2829 libc::IPV6_TCLASS,
2830 tclass as c_int,
2831 )
2832 }
2833 }
2834
2835 #[cfg(all(feature = "all", any(target_os = "freebsd", target_os = "linux")))]
2841 #[cfg_attr(
2842 docsrs,
2843 doc(cfg(all(feature = "all", any(target_os = "freebsd", target_os = "linux"))))
2844 )]
2845 pub fn tcp_congestion(&self) -> io::Result<Vec<u8>> {
2846 let mut payload: [u8; TCP_CA_NAME_MAX] = [0; TCP_CA_NAME_MAX];
2847 let mut len = payload.len() as libc::socklen_t;
2848 syscall!(getsockopt(
2849 self.as_raw(),
2850 IPPROTO_TCP,
2851 libc::TCP_CONGESTION,
2852 payload.as_mut_ptr().cast(),
2853 &mut len,
2854 ))
2855 .map(|_| payload[..len as usize].to_vec())
2856 }
2857
2858 #[cfg(all(feature = "all", any(target_os = "freebsd", target_os = "linux")))]
2865 #[cfg_attr(
2866 docsrs,
2867 doc(cfg(all(feature = "all", any(target_os = "freebsd", target_os = "linux"))))
2868 )]
2869 pub fn set_tcp_congestion(&self, tcp_ca_name: &[u8]) -> io::Result<()> {
2870 syscall!(setsockopt(
2871 self.as_raw(),
2872 IPPROTO_TCP,
2873 libc::TCP_CONGESTION,
2874 tcp_ca_name.as_ptr() as *const _,
2875 tcp_ca_name.len() as libc::socklen_t,
2876 ))
2877 .map(|_| ())
2878 }
2879
2880 #[cfg(all(feature = "all", target_os = "linux"))]
2891 #[cfg_attr(docsrs, doc(cfg(all(feature = "all", target_os = "linux"))))]
2892 pub fn set_dccp_service(&self, code: u32) -> io::Result<()> {
2893 unsafe {
2894 setsockopt(
2895 self.as_raw(),
2896 libc::SOL_DCCP,
2897 libc::DCCP_SOCKOPT_SERVICE,
2898 code,
2899 )
2900 }
2901 }
2902
2903 #[cfg(all(feature = "all", target_os = "linux"))]
2909 #[cfg_attr(docsrs, doc(cfg(all(feature = "all", target_os = "linux"))))]
2910 pub fn dccp_service(&self) -> io::Result<u32> {
2911 unsafe { getsockopt(self.as_raw(), libc::SOL_DCCP, libc::DCCP_SOCKOPT_SERVICE) }
2912 }
2913
2914 #[cfg(all(feature = "all", target_os = "linux"))]
2918 #[cfg_attr(docsrs, doc(cfg(all(feature = "all", target_os = "linux"))))]
2919 pub fn set_dccp_ccid(&self, ccid: u8) -> io::Result<()> {
2920 unsafe { setsockopt(self.as_raw(), libc::SOL_DCCP, libc::DCCP_SOCKOPT_CCID, ccid) }
2921 }
2922
2923 #[cfg(all(feature = "all", target_os = "linux"))]
2929 #[cfg_attr(docsrs, doc(cfg(all(feature = "all", target_os = "linux"))))]
2930 pub fn dccp_tx_ccid(&self) -> io::Result<u32> {
2931 unsafe { getsockopt(self.as_raw(), libc::SOL_DCCP, libc::DCCP_SOCKOPT_TX_CCID) }
2932 }
2933
2934 #[cfg(all(feature = "all", target_os = "linux"))]
2940 #[cfg_attr(docsrs, doc(cfg(all(feature = "all", target_os = "linux"))))]
2941 pub fn dccp_xx_ccid(&self) -> io::Result<u32> {
2942 unsafe { getsockopt(self.as_raw(), libc::SOL_DCCP, libc::DCCP_SOCKOPT_RX_CCID) }
2943 }
2944
2945 #[cfg(all(feature = "all", target_os = "linux"))]
2950 #[cfg_attr(docsrs, doc(cfg(all(feature = "all", target_os = "linux"))))]
2951 pub fn set_dccp_server_timewait(&self, hold_timewait: bool) -> io::Result<()> {
2952 unsafe {
2953 setsockopt(
2954 self.as_raw(),
2955 libc::SOL_DCCP,
2956 libc::DCCP_SOCKOPT_SERVER_TIMEWAIT,
2957 hold_timewait as c_int,
2958 )
2959 }
2960 }
2961
2962 #[cfg(all(feature = "all", target_os = "linux"))]
2968 #[cfg_attr(docsrs, doc(cfg(all(feature = "all", target_os = "linux"))))]
2969 pub fn dccp_server_timewait(&self) -> io::Result<bool> {
2970 unsafe {
2971 getsockopt(
2972 self.as_raw(),
2973 libc::SOL_DCCP,
2974 libc::DCCP_SOCKOPT_SERVER_TIMEWAIT,
2975 )
2976 }
2977 }
2978
2979 #[cfg(all(feature = "all", target_os = "linux"))]
2987 #[cfg_attr(docsrs, doc(cfg(all(feature = "all", target_os = "linux"))))]
2988 pub fn set_dccp_send_cscov(&self, level: u32) -> io::Result<()> {
2989 unsafe {
2990 setsockopt(
2991 self.as_raw(),
2992 libc::SOL_DCCP,
2993 libc::DCCP_SOCKOPT_SEND_CSCOV,
2994 level,
2995 )
2996 }
2997 }
2998
2999 #[cfg(all(feature = "all", target_os = "linux"))]
3005 #[cfg_attr(docsrs, doc(cfg(all(feature = "all", target_os = "linux"))))]
3006 pub fn dccp_send_cscov(&self) -> io::Result<u32> {
3007 unsafe { getsockopt(self.as_raw(), libc::SOL_DCCP, libc::DCCP_SOCKOPT_SEND_CSCOV) }
3008 }
3009
3010 #[cfg(all(feature = "all", target_os = "linux"))]
3016 #[cfg_attr(docsrs, doc(cfg(all(feature = "all", target_os = "linux"))))]
3017 pub fn set_dccp_recv_cscov(&self, level: u32) -> io::Result<()> {
3018 unsafe {
3019 setsockopt(
3020 self.as_raw(),
3021 libc::SOL_DCCP,
3022 libc::DCCP_SOCKOPT_RECV_CSCOV,
3023 level,
3024 )
3025 }
3026 }
3027
3028 #[cfg(all(feature = "all", target_os = "linux"))]
3034 #[cfg_attr(docsrs, doc(cfg(all(feature = "all", target_os = "linux"))))]
3035 pub fn dccp_recv_cscov(&self) -> io::Result<u32> {
3036 unsafe { getsockopt(self.as_raw(), libc::SOL_DCCP, libc::DCCP_SOCKOPT_RECV_CSCOV) }
3037 }
3038
3039 #[cfg(all(feature = "all", target_os = "linux"))]
3044 #[cfg_attr(docsrs, doc(cfg(all(feature = "all", target_os = "linux"))))]
3045 pub fn set_dccp_qpolicy_txqlen(&self, length: u32) -> io::Result<()> {
3046 unsafe {
3047 setsockopt(
3048 self.as_raw(),
3049 libc::SOL_DCCP,
3050 libc::DCCP_SOCKOPT_QPOLICY_TXQLEN,
3051 length,
3052 )
3053 }
3054 }
3055
3056 #[cfg(all(feature = "all", target_os = "linux"))]
3062 #[cfg_attr(docsrs, doc(cfg(all(feature = "all", target_os = "linux"))))]
3063 pub fn dccp_qpolicy_txqlen(&self) -> io::Result<u32> {
3064 unsafe {
3065 getsockopt(
3066 self.as_raw(),
3067 libc::SOL_DCCP,
3068 libc::DCCP_SOCKOPT_QPOLICY_TXQLEN,
3069 )
3070 }
3071 }
3072
3073 #[cfg(all(feature = "all", target_os = "linux"))]
3083 #[cfg_attr(docsrs, doc(cfg(all(feature = "all", target_os = "linux"))))]
3084 pub fn dccp_available_ccids<const N: usize>(&self) -> io::Result<CcidEndpoints<N>> {
3085 let mut endpoints = [0; N];
3086 let mut length = endpoints.len() as libc::socklen_t;
3087 syscall!(getsockopt(
3088 self.as_raw(),
3089 libc::SOL_DCCP,
3090 libc::DCCP_SOCKOPT_AVAILABLE_CCIDS,
3091 endpoints.as_mut_ptr().cast(),
3092 &mut length,
3093 ))?;
3094 Ok(CcidEndpoints { endpoints, length })
3095 }
3096
3097 #[cfg(all(feature = "all", target_os = "linux"))]
3102 #[cfg_attr(docsrs, doc(cfg(all(feature = "all", target_os = "linux"))))]
3103 pub fn dccp_cur_mps(&self) -> io::Result<u32> {
3104 unsafe {
3105 getsockopt(
3106 self.as_raw(),
3107 libc::SOL_DCCP,
3108 libc::DCCP_SOCKOPT_GET_CUR_MPS,
3109 )
3110 }
3111 }
3112}
3113
3114#[cfg(all(feature = "all", target_os = "linux"))]
3116#[cfg_attr(docsrs, doc(cfg(all(feature = "all", target_os = "linux"))))]
3117#[derive(Debug)]
3118pub struct CcidEndpoints<const N: usize> {
3119 endpoints: [u8; N],
3120 length: u32,
3121}
3122
3123#[cfg(all(feature = "all", target_os = "linux"))]
3124#[cfg_attr(docsrs, doc(cfg(all(feature = "all", target_os = "linux"))))]
3125impl<const N: usize> std::ops::Deref for CcidEndpoints<N> {
3126 type Target = [u8];
3127
3128 fn deref(&self) -> &[u8] {
3129 &self.endpoints[0..self.length as usize]
3130 }
3131}
3132
3133#[cfg_attr(docsrs, doc(cfg(unix)))]
3134impl AsFd for crate::Socket {
3135 fn as_fd(&self) -> BorrowedFd<'_> {
3136 unsafe { BorrowedFd::borrow_raw(self.as_raw()) }
3138 }
3139}
3140
3141#[cfg_attr(docsrs, doc(cfg(unix)))]
3142impl AsRawFd for crate::Socket {
3143 fn as_raw_fd(&self) -> c_int {
3144 self.as_raw()
3145 }
3146}
3147
3148#[cfg_attr(docsrs, doc(cfg(unix)))]
3149impl From<crate::Socket> for OwnedFd {
3150 fn from(sock: crate::Socket) -> OwnedFd {
3151 unsafe { OwnedFd::from_raw_fd(sock.into_raw()) }
3153 }
3154}
3155
3156#[cfg_attr(docsrs, doc(cfg(unix)))]
3157impl IntoRawFd for crate::Socket {
3158 fn into_raw_fd(self) -> c_int {
3159 self.into_raw()
3160 }
3161}
3162
3163#[cfg_attr(docsrs, doc(cfg(unix)))]
3164impl From<OwnedFd> for crate::Socket {
3165 fn from(fd: OwnedFd) -> crate::Socket {
3166 unsafe { crate::Socket::from_raw_fd(fd.into_raw_fd()) }
3168 }
3169}
3170
3171#[cfg_attr(docsrs, doc(cfg(unix)))]
3172impl FromRawFd for crate::Socket {
3173 unsafe fn from_raw_fd(fd: c_int) -> crate::Socket {
3174 crate::Socket::from_raw(fd)
3175 }
3176}
3177
3178#[cfg(feature = "all")]
3179from!(UnixStream, crate::Socket);
3180#[cfg(feature = "all")]
3181from!(UnixListener, crate::Socket);
3182#[cfg(feature = "all")]
3183from!(UnixDatagram, crate::Socket);
3184#[cfg(feature = "all")]
3185from!(crate::Socket, UnixStream);
3186#[cfg(feature = "all")]
3187from!(crate::Socket, UnixListener);
3188#[cfg(feature = "all")]
3189from!(crate::Socket, UnixDatagram);
3190
3191#[test]
3192fn in_addr_convertion() {
3193 let ip = Ipv4Addr::new(127, 0, 0, 1);
3194 let raw = to_in_addr(&ip);
3195 let a = raw.s_addr;
3197 assert_eq!(a, u32::from_ne_bytes([127, 0, 0, 1]));
3198 assert_eq!(from_in_addr(raw), ip);
3199
3200 let ip = Ipv4Addr::new(127, 34, 4, 12);
3201 let raw = to_in_addr(&ip);
3202 let a = raw.s_addr;
3203 assert_eq!(a, u32::from_ne_bytes([127, 34, 4, 12]));
3204 assert_eq!(from_in_addr(raw), ip);
3205}
3206
3207#[test]
3208fn in6_addr_convertion() {
3209 let ip = Ipv6Addr::new(0x2000, 1, 2, 3, 4, 5, 6, 7);
3210 let raw = to_in6_addr(&ip);
3211 let want = [32, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7];
3212 assert_eq!(raw.s6_addr, want);
3213 assert_eq!(from_in6_addr(raw), ip);
3214}