• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use std::io;
2 use std::mem::size_of;
3 use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
4 
new_ip_socket(addr: SocketAddr, socket_type: libc::c_int) -> io::Result<libc::c_int>5 pub(crate) fn new_ip_socket(addr: SocketAddr, socket_type: libc::c_int) -> io::Result<libc::c_int> {
6     let domain = match addr {
7         SocketAddr::V4(..) => libc::AF_INET,
8         SocketAddr::V6(..) => libc::AF_INET6,
9     };
10 
11     new_socket(domain, socket_type)
12 }
13 
14 /// Create a new non-blocking socket.
new_socket(domain: libc::c_int, socket_type: libc::c_int) -> io::Result<libc::c_int>15 pub(crate) fn new_socket(domain: libc::c_int, socket_type: libc::c_int) -> io::Result<libc::c_int> {
16     #[cfg(any(
17         target_os = "android",
18         target_os = "dragonfly",
19         target_os = "freebsd",
20         target_os = "illumos",
21         target_os = "linux",
22         target_os = "netbsd",
23         target_os = "openbsd",
24     ))]
25     let socket_type = socket_type | libc::SOCK_NONBLOCK | libc::SOCK_CLOEXEC;
26 
27     let socket = syscall!(socket(domain, socket_type, 0))?;
28 
29     // Mimick `libstd` and set `SO_NOSIGPIPE` on apple systems.
30     #[cfg(any(target_os = "ios", target_os = "macos"))]
31     if let Err(err) = syscall!(setsockopt(
32         socket,
33         libc::SOL_SOCKET,
34         libc::SO_NOSIGPIPE,
35         &1 as *const libc::c_int as *const libc::c_void,
36         size_of::<libc::c_int>() as libc::socklen_t
37     )) {
38         let _ = syscall!(close(socket));
39         return Err(err);
40     }
41 
42     // Darwin doesn't have SOCK_NONBLOCK or SOCK_CLOEXEC.
43     #[cfg(any(target_os = "ios", target_os = "macos"))]
44     {
45         if let Err(err) = syscall!(fcntl(socket, libc::F_SETFL, libc::O_NONBLOCK)) {
46             let _ = syscall!(close(socket));
47             return Err(err);
48         }
49         if let Err(err) = syscall!(fcntl(socket, libc::F_SETFD, libc::FD_CLOEXEC)) {
50             let _ = syscall!(close(socket));
51             return Err(err);
52         }
53     }
54 
55     Ok(socket)
56 }
57 
58 /// A type with the same memory layout as `libc::sockaddr`. Used in converting Rust level
59 /// SocketAddr* types into their system representation. The benefit of this specific
60 /// type over using `libc::sockaddr_storage` is that this type is exactly as large as it
61 /// needs to be and not a lot larger. And it can be initialized cleaner from Rust.
62 #[repr(C)]
63 pub(crate) union SocketAddrCRepr {
64     v4: libc::sockaddr_in,
65     v6: libc::sockaddr_in6,
66 }
67 
68 impl SocketAddrCRepr {
as_ptr(&self) -> *const libc::sockaddr69     pub(crate) fn as_ptr(&self) -> *const libc::sockaddr {
70         self as *const _ as *const libc::sockaddr
71     }
72 }
73 
74 /// Converts a Rust `SocketAddr` into the system representation.
socket_addr(addr: &SocketAddr) -> (SocketAddrCRepr, libc::socklen_t)75 pub(crate) fn socket_addr(addr: &SocketAddr) -> (SocketAddrCRepr, libc::socklen_t) {
76     match addr {
77         SocketAddr::V4(ref addr) => {
78             // `s_addr` is stored as BE on all machine and the array is in BE order.
79             // So the native endian conversion method is used so that it's never swapped.
80             let sin_addr = libc::in_addr {
81                 s_addr: u32::from_ne_bytes(addr.ip().octets()),
82             };
83 
84             let sockaddr_in = libc::sockaddr_in {
85                 sin_family: libc::AF_INET as libc::sa_family_t,
86                 sin_port: addr.port().to_be(),
87                 sin_addr,
88                 sin_zero: [0; 8],
89                 #[cfg(any(
90                     target_os = "dragonfly",
91                     target_os = "freebsd",
92                     target_os = "ios",
93                     target_os = "macos",
94                     target_os = "netbsd",
95                     target_os = "openbsd",
96                 ))]
97                 sin_len: 0,
98             };
99 
100             let sockaddr = SocketAddrCRepr { v4: sockaddr_in };
101             let socklen = size_of::<libc::sockaddr_in>() as libc::socklen_t;
102             (sockaddr, socklen)
103         }
104         SocketAddr::V6(ref addr) => {
105             let sockaddr_in6 = libc::sockaddr_in6 {
106                 sin6_family: libc::AF_INET6 as libc::sa_family_t,
107                 sin6_port: addr.port().to_be(),
108                 sin6_addr: libc::in6_addr {
109                     s6_addr: addr.ip().octets(),
110                 },
111                 sin6_flowinfo: addr.flowinfo(),
112                 sin6_scope_id: addr.scope_id(),
113                 #[cfg(any(
114                     target_os = "dragonfly",
115                     target_os = "freebsd",
116                     target_os = "ios",
117                     target_os = "macos",
118                     target_os = "netbsd",
119                     target_os = "openbsd",
120                 ))]
121                 sin6_len: 0,
122                 #[cfg(target_os = "illumos")]
123                 __sin6_src_id: 0,
124             };
125 
126             let sockaddr = SocketAddrCRepr { v6: sockaddr_in6 };
127             let socklen = size_of::<libc::sockaddr_in6>() as libc::socklen_t;
128             (sockaddr, socklen)
129         }
130     }
131 }
132 
133 /// Converts a `libc::sockaddr` compatible struct into a native Rust `SocketAddr`.
134 ///
135 /// # Safety
136 ///
137 /// `storage` must have the `ss_family` field correctly initialized.
138 /// `storage` must be initialised to a `sockaddr_in` or `sockaddr_in6`.
to_socket_addr( storage: *const libc::sockaddr_storage, ) -> io::Result<SocketAddr>139 pub(crate) unsafe fn to_socket_addr(
140     storage: *const libc::sockaddr_storage,
141 ) -> io::Result<SocketAddr> {
142     match (*storage).ss_family as libc::c_int {
143         libc::AF_INET => {
144             // Safety: if the ss_family field is AF_INET then storage must be a sockaddr_in.
145             let addr: &libc::sockaddr_in = &*(storage as *const libc::sockaddr_in);
146             let ip = Ipv4Addr::from(addr.sin_addr.s_addr.to_ne_bytes());
147             let port = u16::from_be(addr.sin_port);
148             Ok(SocketAddr::V4(SocketAddrV4::new(ip, port)))
149         }
150         libc::AF_INET6 => {
151             // Safety: if the ss_family field is AF_INET6 then storage must be a sockaddr_in6.
152             let addr: &libc::sockaddr_in6 = &*(storage as *const libc::sockaddr_in6);
153             let ip = Ipv6Addr::from(addr.sin6_addr.s6_addr);
154             let port = u16::from_be(addr.sin6_port);
155             Ok(SocketAddr::V6(SocketAddrV6::new(
156                 ip,
157                 port,
158                 addr.sin6_flowinfo,
159                 addr.sin6_scope_id,
160             )))
161         }
162         _ => Err(io::ErrorKind::InvalidInput.into()),
163     }
164 }
165