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::fd::{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::{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(any(target_os = "vxworks", target_os = "wasi")))]
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    #[cfg(not(target_os = "wasi"))]
279    pub fn send_with_flags(&self, buf: &[u8], flags: c_int) -> io::Result<usize> {
280        let len = cmp::min(buf.len(), <wrlen_t>::MAX as usize) as wrlen_t;
281        let ret = cvt(unsafe {
282            libc::send(self.as_raw_fd(), buf.as_ptr() as *const c_void, len, flags)
283        })?;
284        Ok(ret as usize)
285    }
286
287    fn recv_with_flags(&self, mut buf: BorrowedCursor<'_>, flags: c_int) -> io::Result<()> {
288        let ret = cvt(unsafe {
289            libc::recv(
290                self.as_raw_fd(),
291                buf.as_mut().as_mut_ptr() as *mut c_void,
292                buf.capacity(),
293                flags,
294            )
295        })?;
296        unsafe {
297            buf.advance(ret as usize);
298        }
299        Ok(())
300    }
301
302    pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
303        let mut buf = BorrowedBuf::from(buf);
304        self.recv_with_flags(buf.unfilled(), 0)?;
305        Ok(buf.len())
306    }
307
308    pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
309        let mut buf = BorrowedBuf::from(buf);
310        self.recv_with_flags(buf.unfilled(), MSG_PEEK)?;
311        Ok(buf.len())
312    }
313
314    pub fn read_buf(&self, buf: BorrowedCursor<'_>) -> io::Result<()> {
315        self.recv_with_flags(buf, 0)
316    }
317
318    pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
319        self.0.read_vectored(bufs)
320    }
321
322    #[inline]
323    pub fn is_read_vectored(&self) -> bool {
324        self.0.is_read_vectored()
325    }
326
327    fn recv_from_with_flags(
328        &self,
329        buf: &mut [u8],
330        flags: c_int,
331    ) -> io::Result<(usize, SocketAddr)> {
332        // The `recvfrom` function will fill in the storage with the address,
333        // so we don't need to zero it here.
334        // reference: https://linux.die.net/man/2/recvfrom
335        let mut storage: mem::MaybeUninit<libc::sockaddr_storage> = mem::MaybeUninit::uninit();
336        let mut addrlen = size_of_val(&storage) as libc::socklen_t;
337
338        let n = cvt(unsafe {
339            libc::recvfrom(
340                self.as_raw_fd(),
341                buf.as_mut_ptr() as *mut c_void,
342                buf.len(),
343                flags,
344                (&raw mut storage) as *mut _,
345                &mut addrlen,
346            )
347        })?;
348        Ok((n as usize, unsafe { socket_addr_from_c(storage.as_ptr(), addrlen as usize)? }))
349    }
350
351    pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
352        self.recv_from_with_flags(buf, 0)
353    }
354
355    #[cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin"))]
356    pub fn recv_msg(&self, msg: &mut libc::msghdr) -> io::Result<usize> {
357        let n = cvt(unsafe { libc::recvmsg(self.as_raw_fd(), msg, libc::MSG_CMSG_CLOEXEC) })?;
358        Ok(n as usize)
359    }
360
361    pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
362        self.recv_from_with_flags(buf, MSG_PEEK)
363    }
364
365    #[cfg(not(target_os = "wasi"))]
366    pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
367        self.0.write(buf)
368    }
369
370    pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
371        self.0.write_vectored(bufs)
372    }
373
374    #[inline]
375    pub fn is_write_vectored(&self) -> bool {
376        self.0.is_write_vectored()
377    }
378
379    #[cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin"))]
380    pub fn send_msg(&self, msg: &mut libc::msghdr) -> io::Result<usize> {
381        let n = cvt(unsafe { libc::sendmsg(self.as_raw_fd(), msg, 0) })?;
382        Ok(n as usize)
383    }
384
385    pub fn set_timeout(&self, dur: Option<Duration>, kind: libc::c_int) -> io::Result<()> {
386        let timeout = match dur {
387            Some(dur) => {
388                if dur.as_secs() == 0 && dur.subsec_nanos() == 0 {
389                    return Err(io::Error::ZERO_TIMEOUT);
390                }
391
392                let secs = if dur.as_secs() > libc::time_t::MAX as u64 {
393                    libc::time_t::MAX
394                } else {
395                    dur.as_secs() as libc::time_t
396                };
397                let mut timeout = libc::timeval {
398                    tv_sec: secs,
399                    tv_usec: dur.subsec_micros() as libc::suseconds_t,
400                };
401                if timeout.tv_sec == 0 && timeout.tv_usec == 0 {
402                    timeout.tv_usec = 1;
403                }
404                timeout
405            }
406            None => libc::timeval { tv_sec: 0, tv_usec: 0 },
407        };
408        unsafe { setsockopt(self, libc::SOL_SOCKET, kind, timeout) }
409    }
410
411    pub fn timeout(&self, kind: libc::c_int) -> io::Result<Option<Duration>> {
412        let raw: libc::timeval = unsafe { getsockopt(self, libc::SOL_SOCKET, kind)? };
413        if raw.tv_sec == 0 && raw.tv_usec == 0 {
414            Ok(None)
415        } else {
416            let sec = raw.tv_sec as u64;
417            let nsec = (raw.tv_usec as u32) * 1000;
418            Ok(Some(Duration::new(sec, nsec)))
419        }
420    }
421
422    pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
423        let how = match how {
424            Shutdown::Write => libc::SHUT_WR,
425            Shutdown::Read => libc::SHUT_RD,
426            Shutdown::Both => libc::SHUT_RDWR,
427        };
428        cvt(unsafe { libc::shutdown(self.as_raw_fd(), how) })?;
429        Ok(())
430    }
431
432    #[cfg(not(target_os = "cygwin"))]
433    pub fn set_linger(&self, linger: Option<Duration>) -> io::Result<()> {
434        let linger = libc::linger {
435            l_onoff: linger.is_some() as libc::c_int,
436            l_linger: linger.unwrap_or_default().as_secs() as libc::c_int,
437        };
438
439        unsafe { setsockopt(self, libc::SOL_SOCKET, SO_LINGER, linger) }
440    }
441
442    #[cfg(target_os = "cygwin")]
443    pub fn set_linger(&self, linger: Option<Duration>) -> io::Result<()> {
444        let linger = libc::linger {
445            l_onoff: linger.is_some() as libc::c_ushort,
446            l_linger: linger.unwrap_or_default().as_secs() as libc::c_ushort,
447        };
448
449        unsafe { setsockopt(self, libc::SOL_SOCKET, SO_LINGER, linger) }
450    }
451
452    pub fn linger(&self) -> io::Result<Option<Duration>> {
453        let val: libc::linger = unsafe { getsockopt(self, libc::SOL_SOCKET, SO_LINGER)? };
454
455        Ok((val.l_onoff != 0).then(|| Duration::from_secs(val.l_linger as u64)))
456    }
457
458    pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
459        unsafe { setsockopt(self, libc::IPPROTO_TCP, libc::TCP_NODELAY, nodelay as c_int) }
460    }
461
462    pub fn nodelay(&self) -> io::Result<bool> {
463        let raw: c_int = unsafe { getsockopt(self, libc::IPPROTO_TCP, libc::TCP_NODELAY)? };
464        Ok(raw != 0)
465    }
466
467    #[cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin"))]
468    pub fn set_quickack(&self, quickack: bool) -> io::Result<()> {
469        unsafe { setsockopt(self, libc::IPPROTO_TCP, libc::TCP_QUICKACK, quickack as c_int) }
470    }
471
472    #[cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin"))]
473    pub fn quickack(&self) -> io::Result<bool> {
474        let raw: c_int = unsafe { getsockopt(self, libc::IPPROTO_TCP, libc::TCP_QUICKACK)? };
475        Ok(raw != 0)
476    }
477
478    // bionic libc makes no use of this flag
479    #[cfg(target_os = "linux")]
480    pub fn set_deferaccept(&self, accept: Duration) -> io::Result<()> {
481        let val = cmp::min(accept.as_secs(), c_int::MAX as u64) as c_int;
482        unsafe { setsockopt(self, libc::IPPROTO_TCP, libc::TCP_DEFER_ACCEPT, val) }
483    }
484
485    #[cfg(target_os = "linux")]
486    pub fn deferaccept(&self) -> io::Result<Duration> {
487        let raw: c_int = unsafe { getsockopt(self, libc::IPPROTO_TCP, libc::TCP_DEFER_ACCEPT)? };
488        Ok(Duration::from_secs(raw as _))
489    }
490
491    #[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
492    pub fn set_acceptfilter(&self, name: &CStr) -> io::Result<()> {
493        if !name.to_bytes().is_empty() {
494            const AF_NAME_MAX: usize = 16;
495            let mut buf = [0; AF_NAME_MAX];
496            for (src, dst) in name.to_bytes().iter().zip(&mut buf[..AF_NAME_MAX - 1]) {
497                *dst = *src as libc::c_char;
498            }
499            let mut arg: libc::accept_filter_arg = unsafe { mem::zeroed() };
500            arg.af_name = buf;
501            unsafe { setsockopt(self, libc::SOL_SOCKET, libc::SO_ACCEPTFILTER, &mut arg) }
502        } else {
503            unsafe {
504                setsockopt(
505                    self,
506                    libc::SOL_SOCKET,
507                    libc::SO_ACCEPTFILTER,
508                    core::ptr::null_mut() as *mut c_void,
509                )
510            }
511        }
512    }
513
514    #[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
515    pub fn acceptfilter(&self) -> io::Result<&CStr> {
516        let arg: libc::accept_filter_arg =
517            unsafe { getsockopt(self, libc::SOL_SOCKET, libc::SO_ACCEPTFILTER)? };
518        let s: &[u8] =
519            unsafe { core::slice::from_raw_parts(arg.af_name.as_ptr() as *const u8, 16) };
520        let name = CStr::from_bytes_with_nul(s).unwrap();
521        Ok(name)
522    }
523
524    #[cfg(any(target_os = "solaris", target_os = "illumos"))]
525    pub fn set_exclbind(&self, excl: bool) -> io::Result<()> {
526        // not yet on libc crate
527        const SO_EXCLBIND: i32 = 0x1015;
528        unsafe { setsockopt(self, libc::SOL_SOCKET, SO_EXCLBIND, excl) }
529    }
530
531    #[cfg(any(target_os = "solaris", target_os = "illumos"))]
532    pub fn exclbind(&self) -> io::Result<bool> {
533        // not yet on libc crate
534        const SO_EXCLBIND: i32 = 0x1015;
535        let raw: c_int = unsafe { getsockopt(self, libc::SOL_SOCKET, SO_EXCLBIND)? };
536        Ok(raw != 0)
537    }
538
539    #[cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin"))]
540    pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
541        unsafe { setsockopt(self, libc::SOL_SOCKET, libc::SO_PASSCRED, passcred as libc::c_int) }
542    }
543
544    #[cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin"))]
545    pub fn passcred(&self) -> io::Result<bool> {
546        let passcred: libc::c_int =
547            unsafe { getsockopt(self, libc::SOL_SOCKET, libc::SO_PASSCRED)? };
548        Ok(passcred != 0)
549    }
550
551    #[cfg(target_os = "netbsd")]
552    pub fn set_local_creds(&self, local_creds: bool) -> io::Result<()> {
553        unsafe { setsockopt(self, 0 as libc::c_int, libc::LOCAL_CREDS, local_creds as libc::c_int) }
554    }
555
556    #[cfg(target_os = "netbsd")]
557    pub fn local_creds(&self) -> io::Result<bool> {
558        let local_creds: libc::c_int =
559            unsafe { getsockopt(self, 0 as libc::c_int, libc::LOCAL_CREDS)? };
560        Ok(local_creds != 0)
561    }
562
563    #[cfg(target_os = "freebsd")]
564    pub fn set_local_creds_persistent(&self, local_creds_persistent: bool) -> io::Result<()> {
565        unsafe {
566            setsockopt(
567                self,
568                libc::AF_LOCAL,
569                libc::LOCAL_CREDS_PERSISTENT,
570                local_creds_persistent as libc::c_int,
571            )
572        }
573    }
574
575    #[cfg(target_os = "freebsd")]
576    pub fn local_creds_persistent(&self) -> io::Result<bool> {
577        let local_creds_persistent: libc::c_int =
578            unsafe { getsockopt(self, libc::AF_LOCAL, libc::LOCAL_CREDS_PERSISTENT)? };
579        Ok(local_creds_persistent != 0)
580    }
581
582    #[cfg(not(any(target_os = "solaris", target_os = "illumos", target_os = "vita")))]
583    pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
584        let mut nonblocking = nonblocking as libc::c_int;
585        cvt(unsafe { libc::ioctl(self.as_raw_fd(), libc::FIONBIO, &mut nonblocking) }).map(drop)
586    }
587
588    #[cfg(target_os = "vita")]
589    pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
590        let option = nonblocking as libc::c_int;
591        unsafe { setsockopt(self, libc::SOL_SOCKET, libc::SO_NONBLOCK, option) }
592    }
593
594    #[cfg(any(target_os = "solaris", target_os = "illumos"))]
595    pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
596        // FIONBIO is inadequate for sockets on illumos/Solaris, so use the
597        // fcntl(F_[GS]ETFL)-based method provided by FileDesc instead.
598        self.0.set_nonblocking(nonblocking)
599    }
600
601    #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
602    pub fn set_mark(&self, mark: u32) -> io::Result<()> {
603        #[cfg(target_os = "linux")]
604        let option = libc::SO_MARK;
605        #[cfg(target_os = "freebsd")]
606        let option = libc::SO_USER_COOKIE;
607        #[cfg(target_os = "openbsd")]
608        let option = libc::SO_RTABLE;
609        unsafe { setsockopt(self, libc::SOL_SOCKET, option, mark as libc::c_int) }
610    }
611
612    pub fn take_error(&self) -> io::Result<Option<io::Error>> {
613        let raw: c_int = unsafe { getsockopt(self, libc::SOL_SOCKET, libc::SO_ERROR)? };
614        if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }
615    }
616
617    // This is used by sys_common code to abstract over Windows and Unix.
618    pub fn as_raw(&self) -> RawFd {
619        self.as_raw_fd()
620    }
621}
622
623impl AsInner<FileDesc> for Socket {
624    #[inline]
625    fn as_inner(&self) -> &FileDesc {
626        &self.0
627    }
628}
629
630impl IntoInner<FileDesc> for Socket {
631    fn into_inner(self) -> FileDesc {
632        self.0
633    }
634}
635
636impl FromInner<FileDesc> for Socket {
637    fn from_inner(file_desc: FileDesc) -> Self {
638        Self(file_desc)
639    }
640}
641
642impl AsFd for Socket {
643    fn as_fd(&self) -> BorrowedFd<'_> {
644        self.0.as_fd()
645    }
646}
647
648impl AsRawFd for Socket {
649    #[inline]
650    fn as_raw_fd(&self) -> RawFd {
651        self.0.as_raw_fd()
652    }
653}
654
655impl IntoRawFd for Socket {
656    fn into_raw_fd(self) -> RawFd {
657        self.0.into_raw_fd()
658    }
659}
660
661impl FromRawFd for Socket {
662    unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
663        Self(FromRawFd::from_raw_fd(raw_fd))
664    }
665}
666
667// In versions of glibc prior to 2.26, there's a bug where the DNS resolver
668// will cache the contents of /etc/resolv.conf, so changes to that file on disk
669// can be ignored by a long-running program. That can break DNS lookups on e.g.
670// laptops where the network comes and goes. See
671// https://sourceware.org/bugzilla/show_bug.cgi?id=984. Note however that some
672// distros including Debian have patched glibc to fix this for a long time.
673//
674// A workaround for this bug is to call the res_init libc function, to clear
675// the cached configs. Unfortunately, while we believe glibc's implementation
676// of res_init is thread-safe, we know that other implementations are not
677// (https://github.com/rust-lang/rust/issues/43592). Code here in std could
678// try to synchronize its res_init calls with a Mutex, but that wouldn't
679// protect programs that call into libc in other ways. So instead of calling
680// res_init unconditionally, we call it only when we detect we're linking
681// against glibc version < 2.26. (That is, when we both know its needed and
682// believe it's thread-safe).
683#[cfg(all(target_os = "linux", target_env = "gnu"))]
684fn on_resolver_failure() {
685    use crate::sys;
686
687    // If the version fails to parse, we treat it the same as "not glibc".
688    if let Some(version) = sys::os::glibc_version() {
689        if version < (2, 26) {
690            unsafe { libc::res_init() };
691        }
692    }
693}
694
695#[cfg(not(all(target_os = "linux", target_env = "gnu")))]
696fn on_resolver_failure() {}