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 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 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 let fd = cvt(libc::socket(fam, ty | libc::SOCK_CLOEXEC, 0))?;
94 let socket = Socket(FileDesc::from_raw_fd(fd));
95
96 #[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 #[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 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 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 if let Some(e) = self.take_error()? {
229 return Err(e);
230 }
231 } else {
232 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 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 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", target_os = "cygwin"))]
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", target_os = "cygwin"))]
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", target_os = "cygwin"))]
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", target_os = "cygwin"))]
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 #[cfg(target_os = "linux")]
488 pub fn set_deferaccept(&self, accept: Duration) -> io::Result<()> {
489 let val = cmp::min(accept.as_secs(), c_int::MAX as u64) as c_int;
490 setsockopt(self, libc::IPPROTO_TCP, libc::TCP_DEFER_ACCEPT, val)
491 }
492
493 #[cfg(target_os = "linux")]
494 pub fn deferaccept(&self) -> io::Result<Duration> {
495 let raw: c_int = getsockopt(self, libc::IPPROTO_TCP, libc::TCP_DEFER_ACCEPT)?;
496 Ok(Duration::from_secs(raw as _))
497 }
498
499 #[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
500 pub fn set_acceptfilter(&self, name: &CStr) -> io::Result<()> {
501 if !name.to_bytes().is_empty() {
502 const AF_NAME_MAX: usize = 16;
503 let mut buf = [0; AF_NAME_MAX];
504 for (src, dst) in name.to_bytes().iter().zip(&mut buf[..AF_NAME_MAX - 1]) {
505 *dst = *src as libc::c_char;
506 }
507 let mut arg: libc::accept_filter_arg = unsafe { mem::zeroed() };
508 arg.af_name = buf;
509 setsockopt(self, libc::SOL_SOCKET, libc::SO_ACCEPTFILTER, &mut arg)
510 } else {
511 setsockopt(
512 self,
513 libc::SOL_SOCKET,
514 libc::SO_ACCEPTFILTER,
515 core::ptr::null_mut() as *mut c_void,
516 )
517 }
518 }
519
520 #[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
521 pub fn acceptfilter(&self) -> io::Result<&CStr> {
522 let arg: libc::accept_filter_arg =
523 getsockopt(self, libc::SOL_SOCKET, libc::SO_ACCEPTFILTER)?;
524 let s: &[u8] =
525 unsafe { core::slice::from_raw_parts(arg.af_name.as_ptr() as *const u8, 16) };
526 let name = CStr::from_bytes_with_nul(s).unwrap();
527 Ok(name)
528 }
529
530 #[cfg(any(target_os = "solaris", target_os = "illumos"))]
531 pub fn set_exclbind(&self, excl: bool) -> io::Result<()> {
532 const SO_EXCLBIND: i32 = 0x1015;
534 setsockopt(self, libc::SOL_SOCKET, SO_EXCLBIND, excl)
535 }
536
537 #[cfg(any(target_os = "solaris", target_os = "illumos"))]
538 pub fn exclbind(&self) -> io::Result<bool> {
539 const SO_EXCLBIND: i32 = 0x1015;
541 let raw: c_int = getsockopt(self, libc::SOL_SOCKET, SO_EXCLBIND)?;
542 Ok(raw != 0)
543 }
544
545 #[cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin"))]
546 pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
547 setsockopt(self, libc::SOL_SOCKET, libc::SO_PASSCRED, passcred as libc::c_int)
548 }
549
550 #[cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin"))]
551 pub fn passcred(&self) -> io::Result<bool> {
552 let passcred: libc::c_int = getsockopt(self, libc::SOL_SOCKET, libc::SO_PASSCRED)?;
553 Ok(passcred != 0)
554 }
555
556 #[cfg(target_os = "netbsd")]
557 pub fn set_local_creds(&self, local_creds: bool) -> io::Result<()> {
558 setsockopt(self, 0 as libc::c_int, libc::LOCAL_CREDS, local_creds as libc::c_int)
559 }
560
561 #[cfg(target_os = "netbsd")]
562 pub fn local_creds(&self) -> io::Result<bool> {
563 let local_creds: libc::c_int = getsockopt(self, 0 as libc::c_int, libc::LOCAL_CREDS)?;
564 Ok(local_creds != 0)
565 }
566
567 #[cfg(target_os = "freebsd")]
568 pub fn set_local_creds_persistent(&self, local_creds_persistent: bool) -> io::Result<()> {
569 setsockopt(
570 self,
571 libc::AF_LOCAL,
572 libc::LOCAL_CREDS_PERSISTENT,
573 local_creds_persistent as libc::c_int,
574 )
575 }
576
577 #[cfg(target_os = "freebsd")]
578 pub fn local_creds_persistent(&self) -> io::Result<bool> {
579 let local_creds_persistent: libc::c_int =
580 getsockopt(self, libc::AF_LOCAL, libc::LOCAL_CREDS_PERSISTENT)?;
581 Ok(local_creds_persistent != 0)
582 }
583
584 #[cfg(not(any(target_os = "solaris", target_os = "illumos", target_os = "vita")))]
585 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
586 let mut nonblocking = nonblocking as libc::c_int;
587 cvt(unsafe { libc::ioctl(self.as_raw_fd(), libc::FIONBIO, &mut nonblocking) }).map(drop)
588 }
589
590 #[cfg(target_os = "vita")]
591 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
592 let option = nonblocking as libc::c_int;
593 setsockopt(self, libc::SOL_SOCKET, libc::SO_NONBLOCK, option)
594 }
595
596 #[cfg(any(target_os = "solaris", target_os = "illumos"))]
597 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
598 self.0.set_nonblocking(nonblocking)
601 }
602
603 #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
604 pub fn set_mark(&self, mark: u32) -> io::Result<()> {
605 #[cfg(target_os = "linux")]
606 let option = libc::SO_MARK;
607 #[cfg(target_os = "freebsd")]
608 let option = libc::SO_USER_COOKIE;
609 #[cfg(target_os = "openbsd")]
610 let option = libc::SO_RTABLE;
611 setsockopt(self, libc::SOL_SOCKET, option, mark as libc::c_int)
612 }
613
614 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
615 let raw: c_int = getsockopt(self, libc::SOL_SOCKET, libc::SO_ERROR)?;
616 if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }
617 }
618
619 pub fn as_raw(&self) -> RawFd {
621 self.as_raw_fd()
622 }
623}
624
625impl AsInner<FileDesc> for Socket {
626 #[inline]
627 fn as_inner(&self) -> &FileDesc {
628 &self.0
629 }
630}
631
632impl IntoInner<FileDesc> for Socket {
633 fn into_inner(self) -> FileDesc {
634 self.0
635 }
636}
637
638impl FromInner<FileDesc> for Socket {
639 fn from_inner(file_desc: FileDesc) -> Self {
640 Self(file_desc)
641 }
642}
643
644impl AsFd for Socket {
645 fn as_fd(&self) -> BorrowedFd<'_> {
646 self.0.as_fd()
647 }
648}
649
650impl AsRawFd for Socket {
651 #[inline]
652 fn as_raw_fd(&self) -> RawFd {
653 self.0.as_raw_fd()
654 }
655}
656
657impl IntoRawFd for Socket {
658 fn into_raw_fd(self) -> RawFd {
659 self.0.into_raw_fd()
660 }
661}
662
663impl FromRawFd for Socket {
664 unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
665 Self(FromRawFd::from_raw_fd(raw_fd))
666 }
667}
668
669#[cfg(all(target_os = "linux", target_env = "gnu"))]
686fn on_resolver_failure() {
687 use crate::sys;
688
689 if let Some(version) = sys::os::glibc_version() {
691 if version < (2, 26) {
692 unsafe { libc::res_init() };
693 }
694 }
695}
696
697#[cfg(not(all(target_os = "linux", target_env = "gnu")))]
698fn on_resolver_failure() {}