std/sys/net/connection/socket/
unix.rs

1use libc::{MSG_PEEK, c_int, c_void, size_t, sockaddr, socklen_t};
2
3#[cfg(not(any(target_os = "espidf", target_os = "nuttx")))]
4use crate::ffi::CStr;
5use crate::io::{self, BorrowedBuf, BorrowedCursor, IoSlice, IoSliceMut};
6use crate::net::{Shutdown, SocketAddr};
7use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
8use crate::sys::fd::FileDesc;
9use crate::sys::net::{getsockopt, setsockopt};
10use crate::sys::pal::IsMinusOne;
11use crate::sys_common::{AsInner, FromInner, IntoInner};
12use crate::time::{Duration, Instant};
13use crate::{cmp, mem};
14
15cfg_select! {
16    target_vendor = "apple" => {
17        use libc::SO_LINGER_SEC as SO_LINGER;
18    }
19    _ => {
20        use libc::SO_LINGER;
21    }
22}
23
24pub(super) use libc as netc;
25
26use super::{socket_addr_from_c, socket_addr_to_c};
27pub use crate::sys::{cvt, cvt_r};
28
29#[expect(non_camel_case_types)]
30pub type wrlen_t = size_t;
31
32pub struct Socket(FileDesc);
33
34pub fn init() {}
35
36pub fn cvt_gai(err: c_int) -> io::Result<()> {
37    if err == 0 {
38        return Ok(());
39    }
40
41    // We may need to trigger a glibc workaround. See on_resolver_failure() for details.
42    on_resolver_failure();
43
44    #[cfg(not(any(target_os = "espidf", target_os = "nuttx")))]
45    if err == libc::EAI_SYSTEM {
46        return Err(io::Error::last_os_error());
47    }
48
49    #[cfg(not(any(target_os = "espidf", target_os = "nuttx")))]
50    let detail = unsafe {
51        // We can't always expect a UTF-8 environment. When we don't get that luxury,
52        // it's better to give a low-quality error message than none at all.
53        CStr::from_ptr(libc::gai_strerror(err)).to_string_lossy()
54    };
55
56    #[cfg(any(target_os = "espidf", target_os = "nuttx"))]
57    let detail = "";
58
59    Err(io::Error::new(
60        io::ErrorKind::Uncategorized,
61        &format!("failed to lookup address information: {detail}")[..],
62    ))
63}
64
65impl Socket {
66    pub fn new(family: c_int, ty: c_int) -> io::Result<Socket> {
67        cfg_select! {
68            any(
69                target_os = "android",
70                target_os = "dragonfly",
71                target_os = "freebsd",
72                target_os = "illumos",
73                target_os = "hurd",
74                target_os = "linux",
75                target_os = "netbsd",
76                target_os = "openbsd",
77                target_os = "cygwin",
78                target_os = "nto",
79                target_os = "solaris",
80            ) => {
81                // On platforms that support it we pass the SOCK_CLOEXEC
82                // flag to atomically create the socket and set it as
83                // CLOEXEC. On Linux this was added in 2.6.27.
84                let fd = cvt(unsafe { libc::socket(family, ty | libc::SOCK_CLOEXEC, 0) })?;
85                let socket = Socket(unsafe { FileDesc::from_raw_fd(fd) });
86
87                // DragonFlyBSD, FreeBSD and NetBSD use `SO_NOSIGPIPE` as a `setsockopt`
88                // flag to disable `SIGPIPE` emission on socket.
89                #[cfg(any(target_os = "freebsd", target_os = "netbsd", target_os = "dragonfly"))]
90                unsafe { setsockopt(&socket, libc::SOL_SOCKET, libc::SO_NOSIGPIPE, 1)? };
91
92                Ok(socket)
93            }
94            _ => {
95                let fd = cvt(unsafe { libc::socket(family, ty, 0) })?;
96                let fd = unsafe { FileDesc::from_raw_fd(fd) };
97                fd.set_cloexec()?;
98                let socket = Socket(fd);
99
100                // macOS and iOS use `SO_NOSIGPIPE` as a `setsockopt`
101                // flag to disable `SIGPIPE` emission on socket.
102                #[cfg(target_vendor = "apple")]
103                unsafe { setsockopt(&socket, libc::SOL_SOCKET, libc::SO_NOSIGPIPE, 1)? };
104
105                Ok(socket)
106            }
107        }
108    }
109
110    #[cfg(not(target_os = "vxworks"))]
111    pub fn new_pair(fam: c_int, ty: c_int) -> io::Result<(Socket, Socket)> {
112        unsafe {
113            let mut fds = [0, 0];
114
115            cfg_select! {
116                any(
117                    target_os = "android",
118                    target_os = "dragonfly",
119                    target_os = "freebsd",
120                    target_os = "illumos",
121                    target_os = "linux",
122                    target_os = "hurd",
123                    target_os = "netbsd",
124                    target_os = "openbsd",
125                    target_os = "cygwin",
126                    target_os = "nto",
127                ) => {
128                    // Like above, set cloexec atomically
129                    cvt(libc::socketpair(fam, ty | libc::SOCK_CLOEXEC, 0, fds.as_mut_ptr()))?;
130                    Ok((Socket(FileDesc::from_raw_fd(fds[0])), Socket(FileDesc::from_raw_fd(fds[1]))))
131                }
132                _ => {
133                    cvt(libc::socketpair(fam, ty, 0, fds.as_mut_ptr()))?;
134                    let a = FileDesc::from_raw_fd(fds[0]);
135                    let b = FileDesc::from_raw_fd(fds[1]);
136                    a.set_cloexec()?;
137                    b.set_cloexec()?;
138                    Ok((Socket(a), Socket(b)))
139                }
140            }
141        }
142    }
143
144    #[cfg(target_os = "vxworks")]
145    pub fn new_pair(_fam: c_int, _ty: c_int) -> io::Result<(Socket, Socket)> {
146        unimplemented!()
147    }
148
149    pub fn connect(&self, addr: &SocketAddr) -> io::Result<()> {
150        let (addr, len) = socket_addr_to_c(addr);
151        loop {
152            let result = unsafe { libc::connect(self.as_raw_fd(), addr.as_ptr(), len) };
153            if result.is_minus_one() {
154                let err = crate::sys::os::errno();
155                match err {
156                    libc::EINTR => continue,
157                    libc::EISCONN => return Ok(()),
158                    _ => return Err(io::Error::from_raw_os_error(err)),
159                }
160            }
161            return Ok(());
162        }
163    }
164
165    pub fn connect_timeout(&self, addr: &SocketAddr, timeout: Duration) -> io::Result<()> {
166        self.set_nonblocking(true)?;
167        let r = unsafe {
168            let (addr, len) = socket_addr_to_c(addr);
169            cvt(libc::connect(self.as_raw_fd(), addr.as_ptr(), len))
170        };
171        self.set_nonblocking(false)?;
172
173        match r {
174            Ok(_) => return Ok(()),
175            // there's no ErrorKind for EINPROGRESS :(
176            Err(ref e) if e.raw_os_error() == Some(libc::EINPROGRESS) => {}
177            Err(e) => return Err(e),
178        }
179
180        let mut pollfd = libc::pollfd { fd: self.as_raw_fd(), events: libc::POLLOUT, revents: 0 };
181
182        if timeout.as_secs() == 0 && timeout.subsec_nanos() == 0 {
183            return Err(io::Error::ZERO_TIMEOUT);
184        }
185
186        let start = Instant::now();
187
188        loop {
189            let elapsed = start.elapsed();
190            if elapsed >= timeout {
191                return Err(io::const_error!(io::ErrorKind::TimedOut, "connection timed out"));
192            }
193
194            let timeout = timeout - elapsed;
195            let mut timeout = timeout
196                .as_secs()
197                .saturating_mul(1_000)
198                .saturating_add(timeout.subsec_nanos() as u64 / 1_000_000);
199            if timeout == 0 {
200                timeout = 1;
201            }
202
203            let timeout = cmp::min(timeout, c_int::MAX as u64) as c_int;
204
205            match unsafe { libc::poll(&mut pollfd, 1, timeout) } {
206                -1 => {
207                    let err = io::Error::last_os_error();
208                    if !err.is_interrupted() {
209                        return Err(err);
210                    }
211                }
212                0 => {}
213                _ => {
214                    if cfg!(target_os = "vxworks") {
215                        // VxWorks poll does not return  POLLHUP or POLLERR in revents. Check if the
216                        // connection actually succeeded and return ok only when the socket is
217                        // ready and no errors were found.
218                        if let Some(e) = self.take_error()? {
219                            return Err(e);
220                        }
221                    } else {
222                        // linux returns POLLOUT|POLLERR|POLLHUP for refused connections (!), so look
223                        // for POLLHUP or POLLERR rather than read readiness
224                        if pollfd.revents & (libc::POLLHUP | libc::POLLERR) != 0 {
225                            let e = self.take_error()?.unwrap_or_else(|| {
226                                io::const_error!(
227                                    io::ErrorKind::Uncategorized,
228                                    "no error set after POLLHUP",
229                                )
230                            });
231                            return Err(e);
232                        }
233                    }
234
235                    return Ok(());
236                }
237            }
238        }
239    }
240
241    pub fn accept(&self, storage: *mut sockaddr, len: *mut socklen_t) -> io::Result<Socket> {
242        // Unfortunately the only known way right now to accept a socket and
243        // atomically set the CLOEXEC flag is to use the `accept4` syscall on
244        // platforms that support it. On Linux, this was added in 2.6.28,
245        // glibc 2.10 and musl 0.9.5.
246        cfg_select! {
247            any(
248                target_os = "android",
249                target_os = "dragonfly",
250                target_os = "freebsd",
251                target_os = "illumos",
252                target_os = "linux",
253                target_os = "hurd",
254                target_os = "netbsd",
255                target_os = "openbsd",
256                target_os = "cygwin",
257            ) => {
258                unsafe {
259                    let fd = cvt_r(|| libc::accept4(self.as_raw_fd(), storage, len, libc::SOCK_CLOEXEC))?;
260                    Ok(Socket(FileDesc::from_raw_fd(fd)))
261                }
262            }
263            _ => {
264                unsafe {
265                    let fd = cvt_r(|| libc::accept(self.as_raw_fd(), storage, len))?;
266                    let fd = FileDesc::from_raw_fd(fd);
267                    fd.set_cloexec()?;
268                    Ok(Socket(fd))
269                }
270            }
271        }
272    }
273
274    pub fn duplicate(&self) -> io::Result<Socket> {
275        self.0.duplicate().map(Socket)
276    }
277
278    pub fn send_with_flags(&self, buf: &[u8], flags: c_int) -> io::Result<usize> {
279        let len = cmp::min(buf.len(), <wrlen_t>::MAX as usize) as wrlen_t;
280        let ret = cvt(unsafe {
281            libc::send(self.as_raw_fd(), buf.as_ptr() as *const c_void, len, flags)
282        })?;
283        Ok(ret as usize)
284    }
285
286    fn recv_with_flags(&self, mut buf: BorrowedCursor<'_>, flags: c_int) -> io::Result<()> {
287        let ret = cvt(unsafe {
288            libc::recv(
289                self.as_raw_fd(),
290                buf.as_mut().as_mut_ptr() as *mut c_void,
291                buf.capacity(),
292                flags,
293            )
294        })?;
295        unsafe {
296            buf.advance_unchecked(ret as usize);
297        }
298        Ok(())
299    }
300
301    pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
302        let mut buf = BorrowedBuf::from(buf);
303        self.recv_with_flags(buf.unfilled(), 0)?;
304        Ok(buf.len())
305    }
306
307    pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
308        let mut buf = BorrowedBuf::from(buf);
309        self.recv_with_flags(buf.unfilled(), MSG_PEEK)?;
310        Ok(buf.len())
311    }
312
313    pub fn read_buf(&self, buf: BorrowedCursor<'_>) -> io::Result<()> {
314        self.recv_with_flags(buf, 0)
315    }
316
317    pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
318        self.0.read_vectored(bufs)
319    }
320
321    #[inline]
322    pub fn is_read_vectored(&self) -> bool {
323        self.0.is_read_vectored()
324    }
325
326    fn recv_from_with_flags(
327        &self,
328        buf: &mut [u8],
329        flags: c_int,
330    ) -> io::Result<(usize, SocketAddr)> {
331        // The `recvfrom` function will fill in the storage with the address,
332        // so we don't need to zero it here.
333        // reference: https://linux.die.net/man/2/recvfrom
334        let mut storage: mem::MaybeUninit<libc::sockaddr_storage> = mem::MaybeUninit::uninit();
335        let mut addrlen = size_of_val(&storage) as libc::socklen_t;
336
337        let n = cvt(unsafe {
338            libc::recvfrom(
339                self.as_raw_fd(),
340                buf.as_mut_ptr() as *mut c_void,
341                buf.len(),
342                flags,
343                (&raw mut storage) as *mut _,
344                &mut addrlen,
345            )
346        })?;
347        Ok((n as usize, unsafe { socket_addr_from_c(storage.as_ptr(), addrlen as usize)? }))
348    }
349
350    pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
351        self.recv_from_with_flags(buf, 0)
352    }
353
354    #[cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin"))]
355    pub fn recv_msg(&self, msg: &mut libc::msghdr) -> io::Result<usize> {
356        let n = cvt(unsafe { libc::recvmsg(self.as_raw_fd(), msg, libc::MSG_CMSG_CLOEXEC) })?;
357        Ok(n as usize)
358    }
359
360    pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
361        self.recv_from_with_flags(buf, MSG_PEEK)
362    }
363
364    pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
365        self.0.write(buf)
366    }
367
368    pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
369        self.0.write_vectored(bufs)
370    }
371
372    #[inline]
373    pub fn is_write_vectored(&self) -> bool {
374        self.0.is_write_vectored()
375    }
376
377    #[cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin"))]
378    pub fn send_msg(&self, msg: &mut libc::msghdr) -> io::Result<usize> {
379        let n = cvt(unsafe { libc::sendmsg(self.as_raw_fd(), msg, 0) })?;
380        Ok(n as usize)
381    }
382
383    pub fn set_timeout(&self, dur: Option<Duration>, kind: libc::c_int) -> io::Result<()> {
384        let timeout = match dur {
385            Some(dur) => {
386                if dur.as_secs() == 0 && dur.subsec_nanos() == 0 {
387                    return Err(io::Error::ZERO_TIMEOUT);
388                }
389
390                let secs = if dur.as_secs() > libc::time_t::MAX as u64 {
391                    libc::time_t::MAX
392                } else {
393                    dur.as_secs() as libc::time_t
394                };
395                let mut timeout = libc::timeval {
396                    tv_sec: secs,
397                    tv_usec: dur.subsec_micros() as libc::suseconds_t,
398                };
399                if timeout.tv_sec == 0 && timeout.tv_usec == 0 {
400                    timeout.tv_usec = 1;
401                }
402                timeout
403            }
404            None => libc::timeval { tv_sec: 0, tv_usec: 0 },
405        };
406        unsafe { setsockopt(self, libc::SOL_SOCKET, kind, timeout) }
407    }
408
409    pub fn timeout(&self, kind: libc::c_int) -> io::Result<Option<Duration>> {
410        let raw: libc::timeval = unsafe { getsockopt(self, libc::SOL_SOCKET, kind)? };
411        if raw.tv_sec == 0 && raw.tv_usec == 0 {
412            Ok(None)
413        } else {
414            let sec = raw.tv_sec as u64;
415            let nsec = (raw.tv_usec as u32) * 1000;
416            Ok(Some(Duration::new(sec, nsec)))
417        }
418    }
419
420    pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
421        let how = match how {
422            Shutdown::Write => libc::SHUT_WR,
423            Shutdown::Read => libc::SHUT_RD,
424            Shutdown::Both => libc::SHUT_RDWR,
425        };
426        cvt(unsafe { libc::shutdown(self.as_raw_fd(), how) })?;
427        Ok(())
428    }
429
430    #[cfg(not(target_os = "cygwin"))]
431    pub fn set_linger(&self, linger: Option<Duration>) -> io::Result<()> {
432        let linger = libc::linger {
433            l_onoff: linger.is_some() as libc::c_int,
434            l_linger: linger.unwrap_or_default().as_secs() as libc::c_int,
435        };
436
437        unsafe { setsockopt(self, libc::SOL_SOCKET, SO_LINGER, linger) }
438    }
439
440    #[cfg(target_os = "cygwin")]
441    pub fn set_linger(&self, linger: Option<Duration>) -> io::Result<()> {
442        let linger = libc::linger {
443            l_onoff: linger.is_some() as libc::c_ushort,
444            l_linger: linger.unwrap_or_default().as_secs() as libc::c_ushort,
445        };
446
447        unsafe { setsockopt(self, libc::SOL_SOCKET, SO_LINGER, linger) }
448    }
449
450    pub fn linger(&self) -> io::Result<Option<Duration>> {
451        let val: libc::linger = unsafe { getsockopt(self, libc::SOL_SOCKET, SO_LINGER)? };
452
453        Ok((val.l_onoff != 0).then(|| Duration::from_secs(val.l_linger as u64)))
454    }
455
456    pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
457        unsafe { setsockopt(self, libc::IPPROTO_TCP, libc::TCP_NODELAY, nodelay as c_int) }
458    }
459
460    pub fn nodelay(&self) -> io::Result<bool> {
461        let raw: c_int = unsafe { getsockopt(self, libc::IPPROTO_TCP, libc::TCP_NODELAY)? };
462        Ok(raw != 0)
463    }
464
465    #[cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin"))]
466    pub fn set_quickack(&self, quickack: bool) -> io::Result<()> {
467        unsafe { setsockopt(self, libc::IPPROTO_TCP, libc::TCP_QUICKACK, quickack as c_int) }
468    }
469
470    #[cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin"))]
471    pub fn quickack(&self) -> io::Result<bool> {
472        let raw: c_int = unsafe { getsockopt(self, libc::IPPROTO_TCP, libc::TCP_QUICKACK)? };
473        Ok(raw != 0)
474    }
475
476    // bionic libc makes no use of this flag
477    #[cfg(target_os = "linux")]
478    pub fn set_deferaccept(&self, accept: Duration) -> io::Result<()> {
479        let val = cmp::min(accept.as_secs(), c_int::MAX as u64) as c_int;
480        unsafe { setsockopt(self, libc::IPPROTO_TCP, libc::TCP_DEFER_ACCEPT, val) }
481    }
482
483    #[cfg(target_os = "linux")]
484    pub fn deferaccept(&self) -> io::Result<Duration> {
485        let raw: c_int = unsafe { getsockopt(self, libc::IPPROTO_TCP, libc::TCP_DEFER_ACCEPT)? };
486        Ok(Duration::from_secs(raw as _))
487    }
488
489    #[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
490    pub fn set_acceptfilter(&self, name: &CStr) -> io::Result<()> {
491        if !name.to_bytes().is_empty() {
492            const AF_NAME_MAX: usize = 16;
493            let mut buf = [0; AF_NAME_MAX];
494            for (src, dst) in name.to_bytes().iter().zip(&mut buf[..AF_NAME_MAX - 1]) {
495                *dst = *src as libc::c_char;
496            }
497            let mut arg: libc::accept_filter_arg = unsafe { mem::zeroed() };
498            arg.af_name = buf;
499            unsafe { setsockopt(self, libc::SOL_SOCKET, libc::SO_ACCEPTFILTER, &mut arg) }
500        } else {
501            unsafe {
502                setsockopt(
503                    self,
504                    libc::SOL_SOCKET,
505                    libc::SO_ACCEPTFILTER,
506                    core::ptr::null_mut() as *mut c_void,
507                )
508            }
509        }
510    }
511
512    #[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
513    pub fn acceptfilter(&self) -> io::Result<&CStr> {
514        let arg: libc::accept_filter_arg =
515            unsafe { getsockopt(self, libc::SOL_SOCKET, libc::SO_ACCEPTFILTER)? };
516        let s: &[u8] =
517            unsafe { core::slice::from_raw_parts(arg.af_name.as_ptr() as *const u8, 16) };
518        let name = CStr::from_bytes_with_nul(s).unwrap();
519        Ok(name)
520    }
521
522    #[cfg(any(target_os = "solaris", target_os = "illumos"))]
523    pub fn set_exclbind(&self, excl: bool) -> io::Result<()> {
524        // not yet on libc crate
525        const SO_EXCLBIND: i32 = 0x1015;
526        unsafe { setsockopt(self, libc::SOL_SOCKET, SO_EXCLBIND, excl) }
527    }
528
529    #[cfg(any(target_os = "solaris", target_os = "illumos"))]
530    pub fn exclbind(&self) -> io::Result<bool> {
531        // not yet on libc crate
532        const SO_EXCLBIND: i32 = 0x1015;
533        let raw: c_int = unsafe { getsockopt(self, libc::SOL_SOCKET, SO_EXCLBIND)? };
534        Ok(raw != 0)
535    }
536
537    #[cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin"))]
538    pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
539        unsafe { setsockopt(self, libc::SOL_SOCKET, libc::SO_PASSCRED, passcred as libc::c_int) }
540    }
541
542    #[cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin"))]
543    pub fn passcred(&self) -> io::Result<bool> {
544        let passcred: libc::c_int =
545            unsafe { getsockopt(self, libc::SOL_SOCKET, libc::SO_PASSCRED)? };
546        Ok(passcred != 0)
547    }
548
549    #[cfg(target_os = "netbsd")]
550    pub fn set_local_creds(&self, local_creds: bool) -> io::Result<()> {
551        unsafe { setsockopt(self, 0 as libc::c_int, libc::LOCAL_CREDS, local_creds as libc::c_int) }
552    }
553
554    #[cfg(target_os = "netbsd")]
555    pub fn local_creds(&self) -> io::Result<bool> {
556        let local_creds: libc::c_int =
557            unsafe { getsockopt(self, 0 as libc::c_int, libc::LOCAL_CREDS)? };
558        Ok(local_creds != 0)
559    }
560
561    #[cfg(target_os = "freebsd")]
562    pub fn set_local_creds_persistent(&self, local_creds_persistent: bool) -> io::Result<()> {
563        unsafe {
564            setsockopt(
565                self,
566                libc::AF_LOCAL,
567                libc::LOCAL_CREDS_PERSISTENT,
568                local_creds_persistent as libc::c_int,
569            )
570        }
571    }
572
573    #[cfg(target_os = "freebsd")]
574    pub fn local_creds_persistent(&self) -> io::Result<bool> {
575        let local_creds_persistent: libc::c_int =
576            unsafe { getsockopt(self, libc::AF_LOCAL, libc::LOCAL_CREDS_PERSISTENT)? };
577        Ok(local_creds_persistent != 0)
578    }
579
580    #[cfg(not(any(target_os = "solaris", target_os = "illumos", target_os = "vita")))]
581    pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
582        let mut nonblocking = nonblocking as libc::c_int;
583        cvt(unsafe { libc::ioctl(self.as_raw_fd(), libc::FIONBIO, &mut nonblocking) }).map(drop)
584    }
585
586    #[cfg(target_os = "vita")]
587    pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
588        let option = nonblocking as libc::c_int;
589        unsafe { setsockopt(self, libc::SOL_SOCKET, libc::SO_NONBLOCK, option) }
590    }
591
592    #[cfg(any(target_os = "solaris", target_os = "illumos"))]
593    pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
594        // FIONBIO is inadequate for sockets on illumos/Solaris, so use the
595        // fcntl(F_[GS]ETFL)-based method provided by FileDesc instead.
596        self.0.set_nonblocking(nonblocking)
597    }
598
599    #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
600    pub fn set_mark(&self, mark: u32) -> io::Result<()> {
601        #[cfg(target_os = "linux")]
602        let option = libc::SO_MARK;
603        #[cfg(target_os = "freebsd")]
604        let option = libc::SO_USER_COOKIE;
605        #[cfg(target_os = "openbsd")]
606        let option = libc::SO_RTABLE;
607        unsafe { setsockopt(self, libc::SOL_SOCKET, option, mark as libc::c_int) }
608    }
609
610    pub fn take_error(&self) -> io::Result<Option<io::Error>> {
611        let raw: c_int = unsafe { getsockopt(self, libc::SOL_SOCKET, libc::SO_ERROR)? };
612        if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }
613    }
614
615    // This is used by sys_common code to abstract over Windows and Unix.
616    pub fn as_raw(&self) -> RawFd {
617        self.as_raw_fd()
618    }
619}
620
621impl AsInner<FileDesc> for Socket {
622    #[inline]
623    fn as_inner(&self) -> &FileDesc {
624        &self.0
625    }
626}
627
628impl IntoInner<FileDesc> for Socket {
629    fn into_inner(self) -> FileDesc {
630        self.0
631    }
632}
633
634impl FromInner<FileDesc> for Socket {
635    fn from_inner(file_desc: FileDesc) -> Self {
636        Self(file_desc)
637    }
638}
639
640impl AsFd for Socket {
641    fn as_fd(&self) -> BorrowedFd<'_> {
642        self.0.as_fd()
643    }
644}
645
646impl AsRawFd for Socket {
647    #[inline]
648    fn as_raw_fd(&self) -> RawFd {
649        self.0.as_raw_fd()
650    }
651}
652
653impl IntoRawFd for Socket {
654    fn into_raw_fd(self) -> RawFd {
655        self.0.into_raw_fd()
656    }
657}
658
659impl FromRawFd for Socket {
660    unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
661        Self(FromRawFd::from_raw_fd(raw_fd))
662    }
663}
664
665// In versions of glibc prior to 2.26, there's a bug where the DNS resolver
666// will cache the contents of /etc/resolv.conf, so changes to that file on disk
667// can be ignored by a long-running program. That can break DNS lookups on e.g.
668// laptops where the network comes and goes. See
669// https://sourceware.org/bugzilla/show_bug.cgi?id=984. Note however that some
670// distros including Debian have patched glibc to fix this for a long time.
671//
672// A workaround for this bug is to call the res_init libc function, to clear
673// the cached configs. Unfortunately, while we believe glibc's implementation
674// of res_init is thread-safe, we know that other implementations are not
675// (https://github.com/rust-lang/rust/issues/43592). Code here in std could
676// try to synchronize its res_init calls with a Mutex, but that wouldn't
677// protect programs that call into libc in other ways. So instead of calling
678// res_init unconditionally, we call it only when we detect we're linking
679// against glibc version < 2.26. (That is, when we both know its needed and
680// believe it's thread-safe).
681#[cfg(all(target_os = "linux", target_env = "gnu"))]
682fn on_resolver_failure() {
683    use crate::sys;
684
685    // If the version fails to parse, we treat it the same as "not glibc".
686    if let Some(version) = sys::os::glibc_version() {
687        if version < (2, 26) {
688            unsafe { libc::res_init() };
689        }
690    }
691}
692
693#[cfg(not(all(target_os = "linux", target_env = "gnu")))]
694fn on_resolver_failure() {}