std/sys/net/connection/socket/
mod.rs

1#[cfg(test)]
2mod tests;
3
4use crate::ffi::{c_int, c_void};
5use crate::io::{self, BorrowedCursor, ErrorKind, IoSlice, IoSliceMut};
6use crate::mem::MaybeUninit;
7use crate::net::{
8    Ipv4Addr, Ipv6Addr, Shutdown, SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs,
9};
10use crate::sys::common::small_c_string::run_with_cstr;
11use crate::sys::net::connection::each_addr;
12use crate::sys_common::{AsInner, FromInner};
13use crate::time::Duration;
14use crate::{cmp, fmt, mem, ptr};
15
16cfg_select! {
17    target_os = "hermit" => {
18        mod hermit;
19        pub use hermit::*;
20    }
21    target_os = "solid_asp3" => {
22        mod solid;
23        pub use solid::*;
24    }
25    target_family = "unix" => {
26        mod unix;
27        pub use unix::*;
28    }
29    all(target_os = "wasi", target_env = "p2") => {
30        mod wasip2;
31        pub use wasip2::*;
32    }
33    target_os = "windows" => {
34        mod windows;
35        pub use windows::*;
36    }
37    _ => {}
38}
39
40use netc as c;
41
42cfg_select! {
43    any(
44        target_os = "dragonfly",
45        target_os = "freebsd",
46        target_os = "openbsd",
47        target_os = "netbsd",
48        target_os = "illumos",
49        target_os = "solaris",
50        target_os = "haiku",
51        target_os = "l4re",
52        target_os = "nto",
53        target_os = "nuttx",
54        target_vendor = "apple",
55    ) => {
56        use c::IPV6_JOIN_GROUP as IPV6_ADD_MEMBERSHIP;
57        use c::IPV6_LEAVE_GROUP as IPV6_DROP_MEMBERSHIP;
58    }
59    _ => {
60        use c::IPV6_ADD_MEMBERSHIP;
61        use c::IPV6_DROP_MEMBERSHIP;
62    }
63}
64
65cfg_select! {
66    any(
67        target_os = "linux", target_os = "android",
68        target_os = "hurd",
69        target_os = "dragonfly", target_os = "freebsd",
70        target_os = "openbsd", target_os = "netbsd",
71        target_os = "solaris", target_os = "illumos",
72        target_os = "haiku", target_os = "nto",
73        target_os = "cygwin",
74    ) => {
75        use libc::MSG_NOSIGNAL;
76    }
77    _ => {
78        const MSG_NOSIGNAL: c_int = 0x0;
79    }
80}
81
82cfg_select! {
83    any(
84        target_os = "dragonfly", target_os = "freebsd",
85        target_os = "openbsd", target_os = "netbsd",
86        target_os = "solaris", target_os = "illumos",
87        target_os = "nto",
88    ) => {
89        use crate::ffi::c_uchar;
90        type IpV4MultiCastType = c_uchar;
91    }
92    _ => {
93        type IpV4MultiCastType = c_int;
94    }
95}
96
97////////////////////////////////////////////////////////////////////////////////
98// address conversions
99////////////////////////////////////////////////////////////////////////////////
100
101fn ip_v4_addr_to_c(addr: &Ipv4Addr) -> c::in_addr {
102    // `s_addr` is stored as BE on all machines and the array is in BE order.
103    // So the native endian conversion method is used so that it's never swapped.
104    c::in_addr { s_addr: u32::from_ne_bytes(addr.octets()) }
105}
106
107fn ip_v6_addr_to_c(addr: &Ipv6Addr) -> c::in6_addr {
108    c::in6_addr { s6_addr: addr.octets() }
109}
110
111fn ip_v4_addr_from_c(addr: c::in_addr) -> Ipv4Addr {
112    Ipv4Addr::from(addr.s_addr.to_ne_bytes())
113}
114
115fn ip_v6_addr_from_c(addr: c::in6_addr) -> Ipv6Addr {
116    Ipv6Addr::from(addr.s6_addr)
117}
118
119fn socket_addr_v4_to_c(addr: &SocketAddrV4) -> c::sockaddr_in {
120    c::sockaddr_in {
121        sin_family: c::AF_INET as c::sa_family_t,
122        sin_port: addr.port().to_be(),
123        sin_addr: ip_v4_addr_to_c(addr.ip()),
124        ..unsafe { mem::zeroed() }
125    }
126}
127
128fn socket_addr_v6_to_c(addr: &SocketAddrV6) -> c::sockaddr_in6 {
129    c::sockaddr_in6 {
130        sin6_family: c::AF_INET6 as c::sa_family_t,
131        sin6_port: addr.port().to_be(),
132        sin6_addr: ip_v6_addr_to_c(addr.ip()),
133        sin6_flowinfo: addr.flowinfo(),
134        sin6_scope_id: addr.scope_id(),
135        ..unsafe { mem::zeroed() }
136    }
137}
138
139fn socket_addr_v4_from_c(addr: c::sockaddr_in) -> SocketAddrV4 {
140    SocketAddrV4::new(ip_v4_addr_from_c(addr.sin_addr), u16::from_be(addr.sin_port))
141}
142
143fn socket_addr_v6_from_c(addr: c::sockaddr_in6) -> SocketAddrV6 {
144    SocketAddrV6::new(
145        ip_v6_addr_from_c(addr.sin6_addr),
146        u16::from_be(addr.sin6_port),
147        addr.sin6_flowinfo,
148        addr.sin6_scope_id,
149    )
150}
151
152/// A type with the same memory layout as `c::sockaddr`. Used in converting Rust level
153/// SocketAddr* types into their system representation. The benefit of this specific
154/// type over using `c::sockaddr_storage` is that this type is exactly as large as it
155/// needs to be and not a lot larger. And it can be initialized more cleanly from Rust.
156#[repr(C)]
157union SocketAddrCRepr {
158    v4: c::sockaddr_in,
159    v6: c::sockaddr_in6,
160}
161
162impl SocketAddrCRepr {
163    fn as_ptr(&self) -> *const c::sockaddr {
164        self as *const _ as *const c::sockaddr
165    }
166}
167
168fn socket_addr_to_c(addr: &SocketAddr) -> (SocketAddrCRepr, c::socklen_t) {
169    match addr {
170        SocketAddr::V4(a) => {
171            let sockaddr = SocketAddrCRepr { v4: socket_addr_v4_to_c(a) };
172            (sockaddr, size_of::<c::sockaddr_in>() as c::socklen_t)
173        }
174        SocketAddr::V6(a) => {
175            let sockaddr = SocketAddrCRepr { v6: socket_addr_v6_to_c(a) };
176            (sockaddr, size_of::<c::sockaddr_in6>() as c::socklen_t)
177        }
178    }
179}
180
181fn addr_family(addr: &SocketAddr) -> c_int {
182    match addr {
183        SocketAddr::V4(..) => c::AF_INET,
184        SocketAddr::V6(..) => c::AF_INET6,
185    }
186}
187
188/// Converts the C socket address stored in `storage` to a Rust `SocketAddr`.
189///
190/// # Safety
191/// * `storage` must contain a valid C socket address whose length is no larger
192///   than `len`.
193unsafe fn socket_addr_from_c(
194    storage: *const c::sockaddr_storage,
195    len: usize,
196) -> io::Result<SocketAddr> {
197    match (*storage).ss_family as c_int {
198        c::AF_INET => {
199            assert!(len >= size_of::<c::sockaddr_in>());
200            Ok(SocketAddr::V4(socket_addr_v4_from_c(unsafe {
201                *(storage as *const _ as *const c::sockaddr_in)
202            })))
203        }
204        c::AF_INET6 => {
205            assert!(len >= size_of::<c::sockaddr_in6>());
206            Ok(SocketAddr::V6(socket_addr_v6_from_c(unsafe {
207                *(storage as *const _ as *const c::sockaddr_in6)
208            })))
209        }
210        _ => Err(io::const_error!(ErrorKind::InvalidInput, "invalid argument")),
211    }
212}
213
214////////////////////////////////////////////////////////////////////////////////
215// sockaddr and misc bindings
216////////////////////////////////////////////////////////////////////////////////
217
218/// Sets the value of a socket option.
219///
220/// # Safety
221/// `T` must be the type associated with the given socket option.
222pub unsafe fn setsockopt<T>(
223    sock: &Socket,
224    level: c_int,
225    option_name: c_int,
226    option_value: T,
227) -> io::Result<()> {
228    let option_len = size_of::<T>() as c::socklen_t;
229    // SAFETY:
230    // * `sock` is opened for the duration of this call, as `sock` owns the socket.
231    // * the pointer to `option_value` is readable at a size of `size_of::<T>`
232    //   bytes
233    // * the value of `option_value` has a valid type for the given socket option
234    //   (guaranteed by caller).
235    cvt(unsafe {
236        c::setsockopt(
237            sock.as_raw(),
238            level,
239            option_name,
240            (&raw const option_value) as *const _,
241            option_len,
242        )
243    })?;
244    Ok(())
245}
246
247/// Gets the value of a socket option.
248///
249/// # Safety
250/// `T` must be the type associated with the given socket option.
251pub unsafe fn getsockopt<T: Copy>(
252    sock: &Socket,
253    level: c_int,
254    option_name: c_int,
255) -> io::Result<T> {
256    let mut option_value = MaybeUninit::<T>::zeroed();
257    let mut option_len = size_of::<T>() as c::socklen_t;
258
259    // SAFETY:
260    // * `sock` is opened for the duration of this call, as `sock` owns the socket.
261    // * the pointer to `option_value` is writable and the stack allocation has
262    //   space for `size_of::<T>` bytes.
263    cvt(unsafe {
264        c::getsockopt(
265            sock.as_raw(),
266            level,
267            option_name,
268            option_value.as_mut_ptr().cast(),
269            &mut option_len,
270        )
271    })?;
272
273    // SAFETY: the `getsockopt` call succeeded and the caller guarantees that
274    //         `T` is the type of this option, thus `option_value` must have
275    //         been initialized by the system.
276    Ok(unsafe { option_value.assume_init() })
277}
278
279/// Wraps a call to a platform function that returns a socket address.
280///
281/// # Safety
282/// * if `f` returns a success (i.e. `cvt` returns `Ok` when called on the
283///   return value), the buffer provided to `f` must have been initialized
284///   with a valid C socket address, the length of which must be written
285///   to the second argument.
286unsafe fn sockname<F>(f: F) -> io::Result<SocketAddr>
287where
288    F: FnOnce(*mut c::sockaddr, *mut c::socklen_t) -> c_int,
289{
290    let mut storage = MaybeUninit::<c::sockaddr_storage>::zeroed();
291    let mut len = size_of::<c::sockaddr_storage>() as c::socklen_t;
292    cvt(f(storage.as_mut_ptr().cast(), &mut len))?;
293    // SAFETY:
294    // The caller guarantees that the storage has been successfully initialized
295    // and its size written to `len` if `f` returns a success.
296    unsafe { socket_addr_from_c(storage.as_ptr(), len as usize) }
297}
298
299#[cfg(target_os = "android")]
300fn to_ipv6mr_interface(value: u32) -> c_int {
301    value as c_int
302}
303
304#[cfg(not(target_os = "android"))]
305fn to_ipv6mr_interface(value: u32) -> crate::ffi::c_uint {
306    value as crate::ffi::c_uint
307}
308
309////////////////////////////////////////////////////////////////////////////////
310// lookup_host
311////////////////////////////////////////////////////////////////////////////////
312
313pub struct LookupHost {
314    original: *mut c::addrinfo,
315    cur: *mut c::addrinfo,
316    port: u16,
317}
318
319impl Iterator for LookupHost {
320    type Item = SocketAddr;
321    fn next(&mut self) -> Option<SocketAddr> {
322        loop {
323            unsafe {
324                let cur = self.cur.as_ref()?;
325                self.cur = cur.ai_next;
326                match socket_addr_from_c(cur.ai_addr.cast(), cur.ai_addrlen as usize) {
327                    Ok(mut addr) => {
328                        addr.set_port(self.port);
329                        return Some(addr);
330                    }
331                    Err(_) => continue,
332                }
333            }
334        }
335    }
336}
337
338unsafe impl Sync for LookupHost {}
339unsafe impl Send for LookupHost {}
340
341impl Drop for LookupHost {
342    fn drop(&mut self) {
343        unsafe { c::freeaddrinfo(self.original) }
344    }
345}
346
347pub fn lookup_host(host: &str, port: u16) -> io::Result<LookupHost> {
348    init();
349    run_with_cstr(host.as_bytes(), &|c_host| {
350        let mut hints: c::addrinfo = unsafe { mem::zeroed() };
351        hints.ai_socktype = c::SOCK_STREAM;
352        let mut res = ptr::null_mut();
353        unsafe {
354            cvt_gai(c::getaddrinfo(c_host.as_ptr(), ptr::null(), &hints, &mut res))
355                .map(|_| LookupHost { original: res, cur: res, port })
356        }
357    })
358}
359
360////////////////////////////////////////////////////////////////////////////////
361// TCP streams
362////////////////////////////////////////////////////////////////////////////////
363
364pub struct TcpStream {
365    inner: Socket,
366}
367
368impl TcpStream {
369    pub fn connect<A: ToSocketAddrs>(addr: A) -> io::Result<TcpStream> {
370        init();
371        return each_addr(addr, inner);
372
373        fn inner(addr: &SocketAddr) -> io::Result<TcpStream> {
374            let sock = Socket::new(addr_family(addr), c::SOCK_STREAM)?;
375            sock.connect(addr)?;
376            Ok(TcpStream { inner: sock })
377        }
378    }
379
380    pub fn connect_timeout(addr: &SocketAddr, timeout: Duration) -> io::Result<TcpStream> {
381        init();
382
383        let sock = Socket::new(addr_family(addr), c::SOCK_STREAM)?;
384        sock.connect_timeout(addr, timeout)?;
385        Ok(TcpStream { inner: sock })
386    }
387
388    #[inline]
389    pub fn socket(&self) -> &Socket {
390        &self.inner
391    }
392
393    pub fn into_socket(self) -> Socket {
394        self.inner
395    }
396
397    pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
398        self.inner.set_timeout(dur, c::SO_RCVTIMEO)
399    }
400
401    pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
402        self.inner.set_timeout(dur, c::SO_SNDTIMEO)
403    }
404
405    pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
406        self.inner.timeout(c::SO_RCVTIMEO)
407    }
408
409    pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
410        self.inner.timeout(c::SO_SNDTIMEO)
411    }
412
413    pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
414        self.inner.peek(buf)
415    }
416
417    pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
418        self.inner.read(buf)
419    }
420
421    pub fn read_buf(&self, buf: BorrowedCursor<'_>) -> io::Result<()> {
422        self.inner.read_buf(buf)
423    }
424
425    pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
426        self.inner.read_vectored(bufs)
427    }
428
429    #[inline]
430    pub fn is_read_vectored(&self) -> bool {
431        self.inner.is_read_vectored()
432    }
433
434    pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
435        let len = cmp::min(buf.len(), <wrlen_t>::MAX as usize) as wrlen_t;
436        let ret = cvt(unsafe {
437            c::send(self.inner.as_raw(), buf.as_ptr() as *const c_void, len, MSG_NOSIGNAL)
438        })?;
439        Ok(ret as usize)
440    }
441
442    pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
443        self.inner.write_vectored(bufs)
444    }
445
446    #[inline]
447    pub fn is_write_vectored(&self) -> bool {
448        self.inner.is_write_vectored()
449    }
450
451    pub fn peer_addr(&self) -> io::Result<SocketAddr> {
452        unsafe { sockname(|buf, len| c::getpeername(self.inner.as_raw(), buf, len)) }
453    }
454
455    pub fn socket_addr(&self) -> io::Result<SocketAddr> {
456        unsafe { sockname(|buf, len| c::getsockname(self.inner.as_raw(), buf, len)) }
457    }
458
459    pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
460        self.inner.shutdown(how)
461    }
462
463    pub fn duplicate(&self) -> io::Result<TcpStream> {
464        self.inner.duplicate().map(|s| TcpStream { inner: s })
465    }
466
467    pub fn set_linger(&self, linger: Option<Duration>) -> io::Result<()> {
468        self.inner.set_linger(linger)
469    }
470
471    pub fn linger(&self) -> io::Result<Option<Duration>> {
472        self.inner.linger()
473    }
474
475    pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
476        self.inner.set_nodelay(nodelay)
477    }
478
479    pub fn nodelay(&self) -> io::Result<bool> {
480        self.inner.nodelay()
481    }
482
483    pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
484        unsafe { setsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL, ttl as c_int) }
485    }
486
487    pub fn ttl(&self) -> io::Result<u32> {
488        let raw: c_int = unsafe { getsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL)? };
489        Ok(raw as u32)
490    }
491
492    pub fn take_error(&self) -> io::Result<Option<io::Error>> {
493        self.inner.take_error()
494    }
495
496    pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
497        self.inner.set_nonblocking(nonblocking)
498    }
499}
500
501impl AsInner<Socket> for TcpStream {
502    #[inline]
503    fn as_inner(&self) -> &Socket {
504        &self.inner
505    }
506}
507
508impl FromInner<Socket> for TcpStream {
509    fn from_inner(socket: Socket) -> TcpStream {
510        TcpStream { inner: socket }
511    }
512}
513
514impl fmt::Debug for TcpStream {
515    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
516        let mut res = f.debug_struct("TcpStream");
517
518        if let Ok(addr) = self.socket_addr() {
519            res.field("addr", &addr);
520        }
521
522        if let Ok(peer) = self.peer_addr() {
523            res.field("peer", &peer);
524        }
525
526        let name = if cfg!(windows) { "socket" } else { "fd" };
527        res.field(name, &self.inner.as_raw()).finish()
528    }
529}
530
531////////////////////////////////////////////////////////////////////////////////
532// TCP listeners
533////////////////////////////////////////////////////////////////////////////////
534
535pub struct TcpListener {
536    inner: Socket,
537}
538
539impl TcpListener {
540    pub fn bind<A: ToSocketAddrs>(addr: A) -> io::Result<TcpListener> {
541        init();
542        return each_addr(addr, inner);
543
544        fn inner(addr: &SocketAddr) -> io::Result<TcpListener> {
545            let sock = Socket::new(addr_family(addr), c::SOCK_STREAM)?;
546
547            // On platforms with Berkeley-derived sockets, this allows to quickly
548            // rebind a socket, without needing to wait for the OS to clean up the
549            // previous one.
550            //
551            // On Windows, this allows rebinding sockets which are actively in use,
552            // which allows “socket hijacking”, so we explicitly don't set it here.
553            // https://docs.microsoft.com/en-us/windows/win32/winsock/using-so-reuseaddr-and-so-exclusiveaddruse
554            #[cfg(not(windows))]
555            unsafe {
556                setsockopt(&sock, c::SOL_SOCKET, c::SO_REUSEADDR, 1 as c_int)?
557            };
558
559            // Bind our new socket
560            let (addr, len) = socket_addr_to_c(addr);
561            cvt(unsafe { c::bind(sock.as_raw(), addr.as_ptr(), len as _) })?;
562
563            let backlog = if cfg!(target_os = "horizon") {
564                // The 3DS doesn't support a big connection backlog. Sometimes
565                // it allows up to about 37, but other times it doesn't even
566                // accept 32. There may be a global limitation causing this.
567                20
568            } else if cfg!(target_os = "haiku") {
569                // Haiku does not support a queue length > 32
570                // https://github.com/haiku/haiku/blob/979a0bc487864675517fb2fab28f87dc8bf43041/headers/posix/sys/socket.h#L81
571                32
572            } else {
573                // The default for all other platforms
574                128
575            };
576
577            // Start listening
578            cvt(unsafe { c::listen(sock.as_raw(), backlog) })?;
579            Ok(TcpListener { inner: sock })
580        }
581    }
582
583    #[inline]
584    pub fn socket(&self) -> &Socket {
585        &self.inner
586    }
587
588    pub fn into_socket(self) -> Socket {
589        self.inner
590    }
591
592    pub fn socket_addr(&self) -> io::Result<SocketAddr> {
593        unsafe { sockname(|buf, len| c::getsockname(self.inner.as_raw(), buf, len)) }
594    }
595
596    pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
597        // The `accept` function will fill in the storage with the address,
598        // so we don't need to zero it here.
599        // reference: https://linux.die.net/man/2/accept4
600        let mut storage = MaybeUninit::<c::sockaddr_storage>::uninit();
601        let mut len = size_of::<c::sockaddr_storage>() as c::socklen_t;
602        let sock = self.inner.accept(storage.as_mut_ptr() as *mut _, &mut len)?;
603        let addr = unsafe { socket_addr_from_c(storage.as_ptr(), len as usize)? };
604        Ok((TcpStream { inner: sock }, addr))
605    }
606
607    pub fn duplicate(&self) -> io::Result<TcpListener> {
608        self.inner.duplicate().map(|s| TcpListener { inner: s })
609    }
610
611    pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
612        unsafe { setsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL, ttl as c_int) }
613    }
614
615    pub fn ttl(&self) -> io::Result<u32> {
616        let raw: c_int = unsafe { getsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL)? };
617        Ok(raw as u32)
618    }
619
620    pub fn set_only_v6(&self, only_v6: bool) -> io::Result<()> {
621        unsafe { setsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_V6ONLY, only_v6 as c_int) }
622    }
623
624    pub fn only_v6(&self) -> io::Result<bool> {
625        let raw: c_int = unsafe { getsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_V6ONLY)? };
626        Ok(raw != 0)
627    }
628
629    pub fn take_error(&self) -> io::Result<Option<io::Error>> {
630        self.inner.take_error()
631    }
632
633    pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
634        self.inner.set_nonblocking(nonblocking)
635    }
636}
637
638impl FromInner<Socket> for TcpListener {
639    fn from_inner(socket: Socket) -> TcpListener {
640        TcpListener { inner: socket }
641    }
642}
643
644impl fmt::Debug for TcpListener {
645    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
646        let mut res = f.debug_struct("TcpListener");
647
648        if let Ok(addr) = self.socket_addr() {
649            res.field("addr", &addr);
650        }
651
652        let name = if cfg!(windows) { "socket" } else { "fd" };
653        res.field(name, &self.inner.as_raw()).finish()
654    }
655}
656
657////////////////////////////////////////////////////////////////////////////////
658// UDP
659////////////////////////////////////////////////////////////////////////////////
660
661pub struct UdpSocket {
662    inner: Socket,
663}
664
665impl UdpSocket {
666    pub fn bind<A: ToSocketAddrs>(addr: A) -> io::Result<UdpSocket> {
667        init();
668        return each_addr(addr, inner);
669
670        fn inner(addr: &SocketAddr) -> io::Result<UdpSocket> {
671            let sock = Socket::new(addr_family(addr), c::SOCK_DGRAM)?;
672            let (addr, len) = socket_addr_to_c(addr);
673            cvt(unsafe { c::bind(sock.as_raw(), addr.as_ptr(), len as _) })?;
674            Ok(UdpSocket { inner: sock })
675        }
676    }
677
678    #[inline]
679    pub fn socket(&self) -> &Socket {
680        &self.inner
681    }
682
683    pub fn into_socket(self) -> Socket {
684        self.inner
685    }
686
687    pub fn peer_addr(&self) -> io::Result<SocketAddr> {
688        unsafe { sockname(|buf, len| c::getpeername(self.inner.as_raw(), buf, len)) }
689    }
690
691    pub fn socket_addr(&self) -> io::Result<SocketAddr> {
692        unsafe { sockname(|buf, len| c::getsockname(self.inner.as_raw(), buf, len)) }
693    }
694
695    pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
696        self.inner.recv_from(buf)
697    }
698
699    pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
700        self.inner.peek_from(buf)
701    }
702
703    pub fn send_to(&self, buf: &[u8], dst: &SocketAddr) -> io::Result<usize> {
704        let len = cmp::min(buf.len(), <wrlen_t>::MAX as usize) as wrlen_t;
705        let (dst, dstlen) = socket_addr_to_c(dst);
706        let ret = cvt(unsafe {
707            c::sendto(
708                self.inner.as_raw(),
709                buf.as_ptr() as *const c_void,
710                len,
711                MSG_NOSIGNAL,
712                dst.as_ptr(),
713                dstlen,
714            )
715        })?;
716        Ok(ret as usize)
717    }
718
719    pub fn duplicate(&self) -> io::Result<UdpSocket> {
720        self.inner.duplicate().map(|s| UdpSocket { inner: s })
721    }
722
723    pub fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
724        self.inner.set_timeout(dur, c::SO_RCVTIMEO)
725    }
726
727    pub fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
728        self.inner.set_timeout(dur, c::SO_SNDTIMEO)
729    }
730
731    pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
732        self.inner.timeout(c::SO_RCVTIMEO)
733    }
734
735    pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
736        self.inner.timeout(c::SO_SNDTIMEO)
737    }
738
739    pub fn set_broadcast(&self, broadcast: bool) -> io::Result<()> {
740        unsafe { setsockopt(&self.inner, c::SOL_SOCKET, c::SO_BROADCAST, broadcast as c_int) }
741    }
742
743    pub fn broadcast(&self) -> io::Result<bool> {
744        let raw: c_int = unsafe { getsockopt(&self.inner, c::SOL_SOCKET, c::SO_BROADCAST)? };
745        Ok(raw != 0)
746    }
747
748    pub fn set_multicast_loop_v4(&self, multicast_loop_v4: bool) -> io::Result<()> {
749        unsafe {
750            setsockopt(
751                &self.inner,
752                c::IPPROTO_IP,
753                c::IP_MULTICAST_LOOP,
754                multicast_loop_v4 as IpV4MultiCastType,
755            )
756        }
757    }
758
759    pub fn multicast_loop_v4(&self) -> io::Result<bool> {
760        let raw: IpV4MultiCastType =
761            unsafe { getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_LOOP)? };
762        Ok(raw != 0)
763    }
764
765    pub fn set_multicast_ttl_v4(&self, multicast_ttl_v4: u32) -> io::Result<()> {
766        unsafe {
767            setsockopt(
768                &self.inner,
769                c::IPPROTO_IP,
770                c::IP_MULTICAST_TTL,
771                multicast_ttl_v4 as IpV4MultiCastType,
772            )
773        }
774    }
775
776    pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
777        let raw: IpV4MultiCastType =
778            unsafe { getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL)? };
779        Ok(raw as u32)
780    }
781
782    pub fn set_multicast_loop_v6(&self, multicast_loop_v6: bool) -> io::Result<()> {
783        unsafe {
784            setsockopt(
785                &self.inner,
786                c::IPPROTO_IPV6,
787                c::IPV6_MULTICAST_LOOP,
788                multicast_loop_v6 as c_int,
789            )
790        }
791    }
792
793    pub fn multicast_loop_v6(&self) -> io::Result<bool> {
794        let raw: c_int =
795            unsafe { getsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_MULTICAST_LOOP)? };
796        Ok(raw != 0)
797    }
798
799    pub fn join_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> io::Result<()> {
800        let mreq = c::ip_mreq {
801            imr_multiaddr: ip_v4_addr_to_c(multiaddr),
802            imr_interface: ip_v4_addr_to_c(interface),
803        };
804        unsafe { setsockopt(&self.inner, c::IPPROTO_IP, c::IP_ADD_MEMBERSHIP, mreq) }
805    }
806
807    pub fn join_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32) -> io::Result<()> {
808        let mreq = c::ipv6_mreq {
809            ipv6mr_multiaddr: ip_v6_addr_to_c(multiaddr),
810            ipv6mr_interface: to_ipv6mr_interface(interface),
811        };
812        unsafe { setsockopt(&self.inner, c::IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, mreq) }
813    }
814
815    pub fn leave_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> io::Result<()> {
816        let mreq = c::ip_mreq {
817            imr_multiaddr: ip_v4_addr_to_c(multiaddr),
818            imr_interface: ip_v4_addr_to_c(interface),
819        };
820        unsafe { setsockopt(&self.inner, c::IPPROTO_IP, c::IP_DROP_MEMBERSHIP, mreq) }
821    }
822
823    pub fn leave_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32) -> io::Result<()> {
824        let mreq = c::ipv6_mreq {
825            ipv6mr_multiaddr: ip_v6_addr_to_c(multiaddr),
826            ipv6mr_interface: to_ipv6mr_interface(interface),
827        };
828        unsafe { setsockopt(&self.inner, c::IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, mreq) }
829    }
830
831    pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
832        unsafe { setsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL, ttl as c_int) }
833    }
834
835    pub fn ttl(&self) -> io::Result<u32> {
836        let raw: c_int = unsafe { getsockopt(&self.inner, c::IPPROTO_IP, c::IP_TTL)? };
837        Ok(raw as u32)
838    }
839
840    pub fn take_error(&self) -> io::Result<Option<io::Error>> {
841        self.inner.take_error()
842    }
843
844    pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
845        self.inner.set_nonblocking(nonblocking)
846    }
847
848    pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
849        self.inner.read(buf)
850    }
851
852    pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
853        self.inner.peek(buf)
854    }
855
856    pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
857        let len = cmp::min(buf.len(), <wrlen_t>::MAX as usize) as wrlen_t;
858        let ret = cvt(unsafe {
859            c::send(self.inner.as_raw(), buf.as_ptr() as *const c_void, len, MSG_NOSIGNAL)
860        })?;
861        Ok(ret as usize)
862    }
863
864    pub fn connect<A: ToSocketAddrs>(&self, addr: A) -> io::Result<()> {
865        return each_addr(addr, |addr| inner(self, addr));
866
867        fn inner(this: &UdpSocket, addr: &SocketAddr) -> io::Result<()> {
868            let (addr, len) = socket_addr_to_c(addr);
869            cvt_r(|| unsafe { c::connect(this.inner.as_raw(), addr.as_ptr(), len) }).map(drop)
870        }
871    }
872}
873
874impl FromInner<Socket> for UdpSocket {
875    fn from_inner(socket: Socket) -> UdpSocket {
876        UdpSocket { inner: socket }
877    }
878}
879
880impl fmt::Debug for UdpSocket {
881    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
882        let mut res = f.debug_struct("UdpSocket");
883
884        if let Ok(addr) = self.socket_addr() {
885            res.field("addr", &addr);
886        }
887
888        let name = if cfg!(windows) { "socket" } else { "fd" };
889        res.field(name, &self.inner.as_raw()).finish()
890    }
891}