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