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