1use std::convert::TryInto;
2use std::io;
3use std::mem::{size_of, MaybeUninit};
4use std::net::{self, SocketAddr};
5use std::os::unix::io::{AsRawFd, FromRawFd};
6
7use crate::sys::unix::net::{new_socket, socket_addr, to_socket_addr};
8
9pub(crate) fn new_for_addr(address: SocketAddr) -> io::Result<libc::c_int> {
10 let domain: i32 = match address {
11 SocketAddr::V4(_) => libc::AF_INET,
12 SocketAddr::V6(_) => libc::AF_INET6,
13 };
14 new_socket(domain, socket_type:libc::SOCK_STREAM)
15}
16
17pub(crate) fn bind(socket: &net::TcpListener, addr: SocketAddr) -> io::Result<()> {
18 let (raw_addr: SocketAddrCRepr, raw_addr_length: u32) = socket_addr(&addr);
19 syscall!(bind(socket.as_raw_fd(), raw_addr.as_ptr(), raw_addr_length))?;
20 Ok(())
21}
22
23pub(crate) fn connect(socket: &net::TcpStream, addr: SocketAddr) -> io::Result<()> {
24 let (raw_addr: SocketAddrCRepr, raw_addr_length: u32) = socket_addr(&addr);
25
26 match syscall!(connect(
27 socket.as_raw_fd(),
28 raw_addr.as_ptr(),
29 raw_addr_length
30 )) {
31 Err(err: Error) if err.raw_os_error() != Some(libc::EINPROGRESS) => Err(err),
32 _ => Ok(()),
33 }
34}
35
36pub(crate) fn listen(socket: &net::TcpListener, backlog: u32) -> io::Result<()> {
37 let backlog: i32 = backlog.try_into().unwrap_or(default:i32::max_value());
38 syscall!(listen(socket.as_raw_fd(), backlog))?;
39 Ok(())
40}
41
42pub(crate) fn set_reuseaddr(socket: &net::TcpListener, reuseaddr: bool) -> io::Result<()> {
43 let val: libc::c_int = i32::from(reuseaddr);
44 syscall!(setsockopt(
45 socket.as_raw_fd(),
46 libc::SOL_SOCKET,
47 libc::SO_REUSEADDR,
48 &val as *const libc::c_int as *const libc::c_void,
49 size_of::<libc::c_int>() as libc::socklen_t,
50 ))?;
51 Ok(())
52}
53
54pub(crate) fn accept(listener: &net::TcpListener) -> io::Result<(net::TcpStream, SocketAddr)> {
55 let mut addr: MaybeUninit<libc::sockaddr_storage> = MaybeUninit::uninit();
56 let mut length = size_of::<libc::sockaddr_storage>() as libc::socklen_t;
57
58 // On platforms that support it we can use `accept4(2)` to set `NONBLOCK`
59 // and `CLOEXEC` in the call to accept the connection.
60 #[cfg(any(
61 // Android x86's seccomp profile forbids calls to `accept4(2)`
62 // See https://github.com/tokio-rs/mio/issues/1445 for details
63 all(not(target_arch="x86"), target_os = "android"),
64 target_os = "dragonfly",
65 target_os = "freebsd",
66 target_os = "illumos",
67 target_os = "linux",
68 target_os = "netbsd",
69 target_os = "openbsd",
70 ))]
71 let stream = {
72 syscall!(accept4(
73 listener.as_raw_fd(),
74 addr.as_mut_ptr() as *mut _,
75 &mut length,
76 libc::SOCK_CLOEXEC | libc::SOCK_NONBLOCK,
77 ))
78 .map(|socket| unsafe { net::TcpStream::from_raw_fd(socket) })
79 }?;
80
81 // But not all platforms have the `accept4(2)` call. Luckily BSD (derived)
82 // OSes inherit the non-blocking flag from the listener, so we just have to
83 // set `CLOEXEC`.
84 #[cfg(any(
85 target_os = "ios",
86 target_os = "macos",
87 target_os = "redox",
88 target_os = "tvos",
89 target_os = "watchos",
90 all(target_arch = "x86", target_os = "android"),
91 ))]
92 let stream = {
93 syscall!(accept(
94 listener.as_raw_fd(),
95 addr.as_mut_ptr() as *mut _,
96 &mut length
97 ))
98 .map(|socket| unsafe { net::TcpStream::from_raw_fd(socket) })
99 .and_then(|s| {
100 syscall!(fcntl(s.as_raw_fd(), libc::F_SETFD, libc::FD_CLOEXEC))?;
101
102 // See https://github.com/tokio-rs/mio/issues/1450
103 #[cfg(all(target_arch = "x86", target_os = "android"))]
104 syscall!(fcntl(s.as_raw_fd(), libc::F_SETFL, libc::O_NONBLOCK))?;
105
106 Ok(s)
107 })
108 }?;
109
110 // This is safe because `accept` calls above ensures the address
111 // initialised.
112 unsafe { to_socket_addr(addr.as_ptr()) }.map(|addr| (stream, addr))
113}
114