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 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(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 = "qnx",
80 target_os = "solaris",
81 ) => {
82 let fd = cvt(unsafe { libc::socket(family, ty | libc::SOCK_CLOEXEC, 0) })?;
86 let socket = Socket(unsafe { FileDesc::from_raw_fd(fd) });
87
88 #[cfg(any(target_os = "freebsd", target_os = "netbsd", target_os = "dragonfly"))]
91 unsafe { setsockopt(&socket, libc::SOL_SOCKET, libc::SO_NOSIGPIPE, 1)? };
92
93 Ok(socket)
94 }
95 _ => {
96 let fd = cvt(unsafe { libc::socket(family, ty, 0) })?;
97 let fd = unsafe { FileDesc::from_raw_fd(fd) };
98 fd.set_cloexec()?;
99 let socket = Socket(fd);
100
101 #[cfg(target_vendor = "apple")]
104 unsafe { setsockopt(&socket, libc::SOL_SOCKET, libc::SO_NOSIGPIPE, 1)? };
105
106 Ok(socket)
107 }
108 }
109 }
110
111 #[cfg(not(any(target_os = "vxworks", target_os = "wasi")))]
112 pub fn new_pair(fam: c_int, ty: c_int) -> io::Result<(Socket, Socket)> {
113 unsafe {
114 let mut fds = [0, 0];
115
116 cfg_select! {
117 any(
118 target_os = "android",
119 target_os = "dragonfly",
120 target_os = "freebsd",
121 target_os = "illumos",
122 target_os = "linux",
123 target_os = "hurd",
124 target_os = "netbsd",
125 target_os = "openbsd",
126 target_os = "cygwin",
127 target_os = "nto",
128 target_os = "qnx",
129 ) => {
130 cvt(libc::socketpair(fam, ty | libc::SOCK_CLOEXEC, 0, fds.as_mut_ptr()))?;
132 Ok((Socket(FileDesc::from_raw_fd(fds[0])), Socket(FileDesc::from_raw_fd(fds[1]))))
133 }
134 _ => {
135 cvt(libc::socketpair(fam, ty, 0, fds.as_mut_ptr()))?;
136 let a = FileDesc::from_raw_fd(fds[0]);
137 let b = FileDesc::from_raw_fd(fds[1]);
138 a.set_cloexec()?;
139 b.set_cloexec()?;
140 Ok((Socket(a), Socket(b)))
141 }
142 }
143 }
144 }
145
146 #[cfg(target_os = "vxworks")]
147 pub fn new_pair(_fam: c_int, _ty: c_int) -> io::Result<(Socket, Socket)> {
148 unimplemented!()
149 }
150
151 pub fn connect(&self, addr: &SocketAddr) -> io::Result<()> {
152 let (addr, len) = socket_addr_to_c(addr);
153 loop {
154 let result = unsafe { libc::connect(self.as_raw_fd(), addr.as_ptr(), len) };
155 if result.is_minus_one() {
156 let err = crate::sys::io::errno();
157 match err {
158 libc::EINTR => continue,
159 libc::EISCONN => return Ok(()),
160 _ => return Err(io::Error::from_raw_os_error(err)),
161 }
162 }
163 return Ok(());
164 }
165 }
166
167 pub fn connect_timeout(&self, addr: &SocketAddr, timeout: Duration) -> io::Result<()> {
168 self.set_nonblocking(true)?;
169 let r = unsafe {
170 let (addr, len) = socket_addr_to_c(addr);
171 cvt(libc::connect(self.as_raw_fd(), addr.as_ptr(), len))
172 };
173 self.set_nonblocking(false)?;
174
175 match r {
176 Ok(_) => return Ok(()),
177 Err(ref e) if e.raw_os_error() == Some(libc::EINPROGRESS) => {}
179 Err(e) => return Err(e),
180 }
181
182 let mut pollfd = libc::pollfd { fd: self.as_raw_fd(), events: libc::POLLOUT, revents: 0 };
183
184 if timeout.as_secs() == 0 && timeout.subsec_nanos() == 0 {
185 return Err(io::Error::ZERO_TIMEOUT);
186 }
187
188 let start = Instant::now();
189
190 loop {
191 let elapsed = start.elapsed();
192 if elapsed >= timeout {
193 return Err(io::const_error!(io::ErrorKind::TimedOut, "connection timed out"));
194 }
195
196 let timeout = timeout - elapsed;
197 let mut timeout = timeout
198 .as_secs()
199 .saturating_mul(1_000)
200 .saturating_add(timeout.subsec_nanos() as u64 / 1_000_000);
201 if timeout == 0 {
202 timeout = 1;
203 }
204
205 let timeout = cmp::min(timeout, c_int::MAX as u64) as c_int;
206
207 match unsafe { libc::poll(&mut pollfd, 1, timeout) } {
208 -1 => {
209 let err = io::Error::last_os_error();
210 if !err.is_interrupted() {
211 return Err(err);
212 }
213 }
214 0 => {}
215 _ => {
216 if cfg!(target_os = "vxworks") {
217 if let Some(e) = self.take_error()? {
221 return Err(e);
222 }
223 } else {
224 if pollfd.revents & (libc::POLLHUP | libc::POLLERR) != 0 {
227 let e = self.take_error()?.unwrap_or_else(|| {
228 io::const_error!(
229 io::ErrorKind::Uncategorized,
230 "no error set after POLLHUP",
231 )
232 });
233 return Err(e);
234 }
235 }
236
237 return Ok(());
238 }
239 }
240 }
241 }
242
243 pub fn accept(&self, storage: *mut sockaddr, len: *mut socklen_t) -> io::Result<Socket> {
244 cfg_select! {
249 any(
250 target_os = "android",
251 target_os = "dragonfly",
252 target_os = "freebsd",
253 target_os = "illumos",
254 target_os = "linux",
255 target_os = "hurd",
256 target_os = "netbsd",
257 target_os = "openbsd",
258 target_os = "cygwin",
259 ) => {
260 unsafe {
261 let fd = cvt_r(|| libc::accept4(self.as_raw_fd(), storage, len, libc::SOCK_CLOEXEC))?;
262 Ok(Socket(FileDesc::from_raw_fd(fd)))
263 }
264 }
265 _ => {
266 unsafe {
267 let fd = cvt_r(|| libc::accept(self.as_raw_fd(), storage, len))?;
268 let fd = FileDesc::from_raw_fd(fd);
269 fd.set_cloexec()?;
270 Ok(Socket(fd))
271 }
272 }
273 }
274 }
275
276 pub fn duplicate(&self) -> io::Result<Socket> {
277 self.0.duplicate().map(Socket)
278 }
279
280 #[cfg(not(target_os = "wasi"))]
281 pub fn send_with_flags(&self, buf: &[u8], flags: c_int) -> io::Result<usize> {
282 let len = cmp::min(buf.len(), <wrlen_t>::MAX as usize) as wrlen_t;
283 let ret = cvt(unsafe {
284 libc::send(self.as_raw_fd(), buf.as_ptr() as *const c_void, len, flags)
285 })?;
286 Ok(ret as usize)
287 }
288
289 fn recv_with_flags(&self, mut buf: BorrowedCursor<'_, u8>, flags: c_int) -> io::Result<()> {
290 let ret = cvt(unsafe {
291 libc::recv(
292 self.as_raw_fd(),
293 buf.as_mut().as_mut_ptr() as *mut c_void,
294 buf.capacity(),
295 flags,
296 )
297 })?;
298 unsafe {
299 buf.advance(ret as usize);
300 }
301 Ok(())
302 }
303
304 pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
305 let mut buf = BorrowedBuf::from(buf);
306 self.recv_with_flags(buf.unfilled(), 0)?;
307 Ok(buf.len())
308 }
309
310 pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
311 let mut buf = BorrowedBuf::from(buf);
312 self.recv_with_flags(buf.unfilled(), MSG_PEEK)?;
313 Ok(buf.len())
314 }
315
316 pub fn read_buf(&self, buf: BorrowedCursor<'_, u8>) -> io::Result<()> {
317 self.recv_with_flags(buf, 0)
318 }
319
320 pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
321 self.0.read_vectored(bufs)
322 }
323
324 #[inline]
325 pub fn is_read_vectored(&self) -> bool {
326 self.0.is_read_vectored()
327 }
328
329 fn recv_from_with_flags(
330 &self,
331 buf: &mut [u8],
332 flags: c_int,
333 ) -> io::Result<(usize, SocketAddr)> {
334 let mut storage: mem::MaybeUninit<libc::sockaddr_storage> = mem::MaybeUninit::uninit();
338 let mut addrlen = size_of_val(&storage) as libc::socklen_t;
339
340 let n = cvt(unsafe {
341 libc::recvfrom(
342 self.as_raw_fd(),
343 buf.as_mut_ptr() as *mut c_void,
344 buf.len(),
345 flags,
346 (&raw mut storage) as *mut _,
347 &mut addrlen,
348 )
349 })?;
350 Ok((n as usize, unsafe { socket_addr_from_c(storage.as_ptr(), addrlen as usize)? }))
351 }
352
353 pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
354 self.recv_from_with_flags(buf, 0)
355 }
356
357 #[cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin"))]
358 pub fn recv_msg(&self, msg: &mut libc::msghdr) -> io::Result<usize> {
359 let n = cvt(unsafe { libc::recvmsg(self.as_raw_fd(), msg, libc::MSG_CMSG_CLOEXEC) })?;
360 Ok(n as usize)
361 }
362
363 pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
364 self.recv_from_with_flags(buf, MSG_PEEK)
365 }
366
367 #[cfg(not(target_os = "wasi"))]
368 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
369 self.0.write(buf)
370 }
371
372 pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
373 self.0.write_vectored(bufs)
374 }
375
376 #[inline]
377 pub fn is_write_vectored(&self) -> bool {
378 self.0.is_write_vectored()
379 }
380
381 #[cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin"))]
382 pub fn send_msg(&self, msg: &mut libc::msghdr) -> io::Result<usize> {
383 let n = cvt(unsafe { libc::sendmsg(self.as_raw_fd(), msg, 0) })?;
384 Ok(n as usize)
385 }
386
387 pub fn set_timeout(&self, dur: Option<Duration>, kind: libc::c_int) -> io::Result<()> {
388 let timeout = match dur {
389 Some(dur) => {
390 if dur.as_secs() == 0 && dur.subsec_nanos() == 0 {
391 return Err(io::Error::ZERO_TIMEOUT);
392 }
393
394 let secs = if dur.as_secs() > libc::time_t::MAX as u64 {
395 libc::time_t::MAX
396 } else {
397 dur.as_secs() as libc::time_t
398 };
399 let mut timeout = libc::timeval {
400 tv_sec: secs,
401 tv_usec: dur.subsec_micros() as libc::suseconds_t,
402 };
403 if timeout.tv_sec == 0 && timeout.tv_usec == 0 {
404 timeout.tv_usec = 1;
405 }
406 timeout
407 }
408 None => libc::timeval { tv_sec: 0, tv_usec: 0 },
409 };
410 unsafe { setsockopt(self, libc::SOL_SOCKET, kind, timeout) }
411 }
412
413 pub fn timeout(&self, kind: libc::c_int) -> io::Result<Option<Duration>> {
414 let raw: libc::timeval = unsafe { getsockopt(self, libc::SOL_SOCKET, kind)? };
415 if raw.tv_sec == 0 && raw.tv_usec == 0 {
416 Ok(None)
417 } else {
418 let sec = raw.tv_sec as u64;
419 let nsec = (raw.tv_usec as u32) * 1000;
420 Ok(Some(Duration::new(sec, nsec)))
421 }
422 }
423
424 pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
425 let how = match how {
426 Shutdown::Write => libc::SHUT_WR,
427 Shutdown::Read => libc::SHUT_RD,
428 Shutdown::Both => libc::SHUT_RDWR,
429 };
430 cvt(unsafe { libc::shutdown(self.as_raw_fd(), how) })?;
431 Ok(())
432 }
433
434 #[cfg(not(target_os = "cygwin"))]
435 pub fn set_linger(&self, linger: Option<Duration>) -> io::Result<()> {
436 let linger = libc::linger {
437 l_onoff: linger.is_some() as c_int,
438 l_linger: cmp::min(linger.unwrap_or_default().as_secs(), c_int::MAX as u64) as c_int,
439 };
440
441 unsafe { setsockopt(self, libc::SOL_SOCKET, SO_LINGER, linger) }
442 }
443
444 #[cfg(target_os = "cygwin")]
445 pub fn set_linger(&self, linger: Option<Duration>) -> io::Result<()> {
446 let linger = libc::linger {
447 l_onoff: linger.is_some() as libc::c_ushort,
448 l_linger: cmp::min(linger.unwrap_or_default().as_secs(), libc::c_ushort::MAX as u64)
449 as libc::c_ushort,
450 };
451
452 unsafe { setsockopt(self, libc::SOL_SOCKET, SO_LINGER, linger) }
453 }
454
455 pub fn linger(&self) -> io::Result<Option<Duration>> {
456 let val: libc::linger = unsafe { getsockopt(self, libc::SOL_SOCKET, SO_LINGER)? };
457
458 Ok((val.l_onoff != 0).then(|| Duration::from_secs(val.l_linger as u64)))
459 }
460
461 pub fn set_keepalive(&self, keepalive: bool) -> io::Result<()> {
462 unsafe { setsockopt(self, libc::SOL_SOCKET, libc::SO_KEEPALIVE, keepalive as c_int) }
463 }
464
465 pub fn keepalive(&self) -> io::Result<bool> {
466 let raw: c_int = unsafe { getsockopt(self, libc::SOL_SOCKET, libc::SO_KEEPALIVE)? };
467 Ok(raw != 0)
468 }
469
470 pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
471 unsafe { setsockopt(self, libc::IPPROTO_TCP, libc::TCP_NODELAY, nodelay as c_int) }
472 }
473
474 pub fn nodelay(&self) -> io::Result<bool> {
475 let raw: c_int = unsafe { getsockopt(self, libc::IPPROTO_TCP, libc::TCP_NODELAY)? };
476 Ok(raw != 0)
477 }
478
479 #[cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin"))]
480 pub fn set_quickack(&self, quickack: bool) -> io::Result<()> {
481 unsafe { setsockopt(self, libc::IPPROTO_TCP, libc::TCP_QUICKACK, quickack as c_int) }
482 }
483
484 #[cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin"))]
485 pub fn quickack(&self) -> io::Result<bool> {
486 let raw: c_int = unsafe { getsockopt(self, libc::IPPROTO_TCP, libc::TCP_QUICKACK)? };
487 Ok(raw != 0)
488 }
489
490 #[cfg(target_os = "linux")]
492 pub fn set_deferaccept(&self, accept: Duration) -> io::Result<()> {
493 let val = cmp::min(accept.as_secs(), c_int::MAX as u64) as c_int;
494 unsafe { setsockopt(self, libc::IPPROTO_TCP, libc::TCP_DEFER_ACCEPT, val) }
495 }
496
497 #[cfg(target_os = "linux")]
498 pub fn deferaccept(&self) -> io::Result<Duration> {
499 let raw: c_int = unsafe { getsockopt(self, libc::IPPROTO_TCP, libc::TCP_DEFER_ACCEPT)? };
500 Ok(Duration::from_secs(raw as _))
501 }
502
503 #[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
504 pub fn set_acceptfilter(&self, name: &CStr) -> io::Result<()> {
505 if !name.to_bytes().is_empty() {
506 const AF_NAME_MAX: usize = 16;
507 let mut buf = [0; AF_NAME_MAX];
508 for (src, dst) in name.to_bytes().iter().zip(&mut buf[..AF_NAME_MAX - 1]) {
509 *dst = *src as libc::c_char;
510 }
511 let mut arg: libc::accept_filter_arg = unsafe { mem::zeroed() };
512 arg.af_name = buf;
513 unsafe { setsockopt(self, libc::SOL_SOCKET, libc::SO_ACCEPTFILTER, &mut arg) }
514 } else {
515 unsafe {
516 setsockopt(
517 self,
518 libc::SOL_SOCKET,
519 libc::SO_ACCEPTFILTER,
520 core::ptr::null_mut() as *mut c_void,
521 )
522 }
523 }
524 }
525
526 #[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
527 pub fn acceptfilter(&self) -> io::Result<&CStr> {
528 let arg: libc::accept_filter_arg =
529 unsafe { getsockopt(self, libc::SOL_SOCKET, libc::SO_ACCEPTFILTER)? };
530 let s: &[u8] =
531 unsafe { core::slice::from_raw_parts(arg.af_name.as_ptr() as *const u8, 16) };
532 let name = CStr::from_bytes_with_nul(s).unwrap();
533 Ok(name)
534 }
535
536 #[cfg(any(target_os = "solaris", target_os = "illumos"))]
537 pub fn set_exclbind(&self, excl: bool) -> io::Result<()> {
538 const SO_EXCLBIND: i32 = 0x1015;
540 unsafe { setsockopt(self, libc::SOL_SOCKET, SO_EXCLBIND, excl) }
541 }
542
543 #[cfg(any(target_os = "solaris", target_os = "illumos"))]
544 pub fn exclbind(&self) -> io::Result<bool> {
545 const SO_EXCLBIND: i32 = 0x1015;
547 let raw: c_int = unsafe { getsockopt(self, libc::SOL_SOCKET, SO_EXCLBIND)? };
548 Ok(raw != 0)
549 }
550
551 #[cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin"))]
552 pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
553 unsafe { setsockopt(self, libc::SOL_SOCKET, libc::SO_PASSCRED, passcred as libc::c_int) }
554 }
555
556 #[cfg(any(target_os = "android", target_os = "linux", target_os = "cygwin"))]
557 pub fn passcred(&self) -> io::Result<bool> {
558 let passcred: libc::c_int =
559 unsafe { getsockopt(self, libc::SOL_SOCKET, libc::SO_PASSCRED)? };
560 Ok(passcred != 0)
561 }
562
563 #[cfg(target_os = "netbsd")]
564 pub fn set_local_creds(&self, local_creds: bool) -> io::Result<()> {
565 unsafe { setsockopt(self, 0 as libc::c_int, libc::LOCAL_CREDS, local_creds as libc::c_int) }
566 }
567
568 #[cfg(target_os = "netbsd")]
569 pub fn local_creds(&self) -> io::Result<bool> {
570 let local_creds: libc::c_int =
571 unsafe { getsockopt(self, 0 as libc::c_int, libc::LOCAL_CREDS)? };
572 Ok(local_creds != 0)
573 }
574
575 #[cfg(target_os = "freebsd")]
576 pub fn set_local_creds_persistent(&self, local_creds_persistent: bool) -> io::Result<()> {
577 unsafe {
578 setsockopt(
579 self,
580 libc::AF_LOCAL,
581 libc::LOCAL_CREDS_PERSISTENT,
582 local_creds_persistent as libc::c_int,
583 )
584 }
585 }
586
587 #[cfg(target_os = "freebsd")]
588 pub fn local_creds_persistent(&self) -> io::Result<bool> {
589 let local_creds_persistent: libc::c_int =
590 unsafe { getsockopt(self, libc::AF_LOCAL, libc::LOCAL_CREDS_PERSISTENT)? };
591 Ok(local_creds_persistent != 0)
592 }
593
594 #[cfg(not(any(target_os = "solaris", target_os = "illumos", target_os = "vita")))]
595 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
596 let mut nonblocking = nonblocking as libc::c_int;
597 cvt(unsafe { libc::ioctl(self.as_raw_fd(), libc::FIONBIO, &mut nonblocking) }).map(drop)
598 }
599
600 #[cfg(target_os = "vita")]
601 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
602 let option = nonblocking as libc::c_int;
603 unsafe { setsockopt(self, libc::SOL_SOCKET, libc::SO_NONBLOCK, option) }
604 }
605
606 #[cfg(any(target_os = "solaris", target_os = "illumos"))]
607 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
608 self.0.set_nonblocking(nonblocking)
611 }
612
613 #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
614 pub fn set_mark(&self, mark: u32) -> io::Result<()> {
615 #[cfg(target_os = "linux")]
616 let option = libc::SO_MARK;
617 #[cfg(target_os = "freebsd")]
618 let option = libc::SO_USER_COOKIE;
619 #[cfg(target_os = "openbsd")]
620 let option = libc::SO_RTABLE;
621 unsafe { setsockopt(self, libc::SOL_SOCKET, option, mark as libc::c_int) }
622 }
623
624 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
625 let raw: c_int = unsafe { getsockopt(self, libc::SOL_SOCKET, libc::SO_ERROR)? };
626 if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }
627 }
628
629 pub fn as_raw(&self) -> RawFd {
630 self.as_raw_fd()
631 }
632}
633
634impl AsInner<FileDesc> for Socket {
635 #[inline]
636 fn as_inner(&self) -> &FileDesc {
637 &self.0
638 }
639}
640
641impl IntoInner<FileDesc> for Socket {
642 fn into_inner(self) -> FileDesc {
643 self.0
644 }
645}
646
647impl FromInner<FileDesc> for Socket {
648 fn from_inner(file_desc: FileDesc) -> Self {
649 Self(file_desc)
650 }
651}
652
653impl AsFd for Socket {
654 fn as_fd(&self) -> BorrowedFd<'_> {
655 self.0.as_fd()
656 }
657}
658
659impl AsRawFd for Socket {
660 #[inline]
661 fn as_raw_fd(&self) -> RawFd {
662 self.0.as_raw_fd()
663 }
664}
665
666impl IntoRawFd for Socket {
667 fn into_raw_fd(self) -> RawFd {
668 self.0.into_raw_fd()
669 }
670}
671
672impl FromRawFd for Socket {
673 unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
674 Self(FromRawFd::from_raw_fd(raw_fd))
675 }
676}
677
678#[cfg(all(target_os = "linux", target_env = "gnu"))]
695fn on_resolver_failure() {
696 use crate::sys;
697
698 if let Some(version) = sys::pal::conf::glibc_version() {
700 if version < (2, 26) {
701 unsafe { libc::res_init() };
702 }
703 }
704}
705
706#[cfg(not(all(target_os = "linux", target_env = "gnu")))]
707fn on_resolver_failure() {}