forked from rust-lang/socket2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunix.rs
322 lines (290 loc) · 9.86 KB
/
unix.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::io;
use std::mem::{self, size_of, MaybeUninit};
use std::net::Shutdown;
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
use std::os::unix::net::{UnixDatagram, UnixListener, UnixStream};
use crate::{Domain, Protocol, SockAddr, Socket, Type};
// Used in conversions for `Domain`, `Type` and `Protocol`.
#[allow(non_camel_case_types)]
pub(crate) type c_int = libc::c_int;
// Used in `Domain`.
pub(crate) use libc::{AF_INET, AF_INET6};
// Used in `Type`.
pub(crate) use libc::{SOCK_DGRAM, SOCK_RAW, SOCK_SEQPACKET, SOCK_STREAM};
// Used in `Protocol`.
pub(crate) use libc::{IPPROTO_ICMP, IPPROTO_ICMPV6, IPPROTO_TCP, IPPROTO_UDP};
// Used in `Socket`.
pub(crate) use std::os::unix::io::RawFd as RawSocket;
/// Unix only API.
impl Domain {
/// Domain for Unix socket communication, corresponding to `AF_UNIX`.
pub const UNIX: Domain = Domain(libc::AF_UNIX);
/// Domain for low-level packet interface, corresponding to `AF_PACKET`.
///
/// # Notes
///
/// This function is only available on Linux.
#[cfg(target_os = "linux")]
pub const PACKET: Domain = Domain(libc::AF_PACKET);
}
/// Unix only API.
impl Type {
/// Set `SOCK_NONBLOCK` on the `Type`.
///
/// # Notes
///
/// This function is only available on Android, DragonFlyBSD, FreeBSD,
/// Linux, NetBSD and OpenBSD.
#[cfg(any(
target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd"
))]
pub fn non_blocking(self) -> Type {
Type(self.0 | libc::SOCK_NONBLOCK)
}
/// Set `SOCK_CLOEXEC` on the `Type`.
///
/// # Notes
///
/// This function is only available on Android, DragonFlyBSD, FreeBSD,
/// Linux, NetBSD and OpenBSD.
#[cfg(any(
target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd"
))]
pub fn cloexec(self) -> Type {
Type(self.0 | libc::SOCK_CLOEXEC)
}
}
/// Helper macro to execute a system call that returns an `io::Result`.
macro_rules! syscall {
($fn: ident ( $($arg: expr),* $(,)* ) ) => {{
let res = unsafe { libc::$fn($($arg, )*) };
if res == -1 {
Err(std::io::Error::last_os_error())
} else {
Ok(res)
}
}};
}
pub(crate) fn socket(domain: c_int, type_: c_int, protocol: c_int) -> io::Result<Socket> {
syscall!(socket(domain, type_, protocol)).map(|fd| Socket { inner: fd })
}
pub(crate) fn connect(
sockfd: RawSocket,
addr: *const libc::sockaddr_storage,
addrlen: libc::socklen_t,
) -> io::Result<()> {
// Most OSes don't actually use `sockaddr_storage` in the `connect(2)` call,
// but `sockaddr_storage` can be converted safely into the correct type.
syscall!(connect(sockfd, addr as *const _, addrlen)).map(|_| ())
}
pub(crate) fn bind(
sockfd: RawSocket,
addr: *const libc::sockaddr_storage,
addrlen: libc::socklen_t,
) -> io::Result<()> {
// Most OSes don't actually use `sockaddr_storage` in the `bind(2)` call,
// but `sockaddr_storage` can be converted safely into the correct type.
syscall!(bind(sockfd, addr as *const _, addrlen)).map(|_| ())
}
pub(crate) fn listen(sockfd: RawSocket, backlog: c_int) -> io::Result<()> {
syscall!(listen(sockfd, backlog)).map(|_| ())
}
pub(crate) fn accept(sockfd: RawSocket) -> io::Result<(Socket, SockAddr)> {
let mut addr: MaybeUninit<libc::sockaddr_storage> = MaybeUninit::uninit();
let mut addrlen = size_of::<libc::sockaddr_storage>() as libc::socklen_t;
syscall!(accept(sockfd, addr.as_mut_ptr() as *mut _, &mut addrlen)).map(|stream_fd| {
// This is safe because `accept(2)` filled in the address for us.
let addr = unsafe { SockAddr::from_raw_parts(addr.assume_init(), addrlen) };
(Socket { inner: stream_fd }, addr)
})
}
pub(crate) fn getsockname(sockfd: RawSocket) -> io::Result<SockAddr> {
let mut addr: MaybeUninit<libc::sockaddr_storage> = MaybeUninit::uninit();
let mut addrlen = size_of::<libc::sockaddr_storage>() as libc::socklen_t;
syscall!(getsockname(
sockfd,
addr.as_mut_ptr() as *mut _,
&mut addrlen
))
.map(|_| {
// This is safe because `getsockname(2)` filled in the address for us.
unsafe { SockAddr::from_raw_parts(addr.assume_init(), addrlen) }
})
}
pub(crate) fn getpeername(sockfd: RawSocket) -> io::Result<SockAddr> {
let mut addr: MaybeUninit<libc::sockaddr_storage> = MaybeUninit::uninit();
let mut addrlen = size_of::<libc::sockaddr_storage>() as libc::socklen_t;
syscall!(getpeername(
sockfd,
addr.as_mut_ptr() as *mut _,
&mut addrlen
))
.map(|_| {
// This is safe because `getpeername(2)` filled in the address for us.
unsafe { SockAddr::from_raw_parts(addr.assume_init(), addrlen) }
})
}
pub(crate) fn shutdown(sockfd: RawSocket, how: Shutdown) -> io::Result<()> {
let how = match how {
Shutdown::Write => libc::SHUT_WR,
Shutdown::Read => libc::SHUT_RD,
Shutdown::Both => libc::SHUT_RDWR,
};
syscall!(shutdown(sockfd, how)).map(|_| ())
}
pub(crate) fn setsockopt<T>(
sockfd: RawSocket,
level: c_int,
optname: c_int,
opt: &T,
) -> io::Result<()> {
syscall!(setsockopt(
sockfd,
level,
optname,
opt as *const _ as *const _,
size_of::<T>() as libc::socklen_t,
))
.map(|_| ())
}
pub(crate) fn getsockopt<T>(sockfd: RawSocket, level: c_int, optname: c_int) -> io::Result<T> {
let mut optval: MaybeUninit<T> = MaybeUninit::uninit();
let mut optlen = size_of::<T>() as libc::socklen_t;
syscall!(getsockopt(
sockfd,
level,
optname,
optval.as_mut_ptr() as *mut _,
&mut optlen
))
.map(|_| unsafe {
// Safe because `getsockopt(2)` initialised the value for us.
debug_assert_eq!(optlen as usize, size_of::<T>());
optval.assume_init()
})
}
pub(crate) fn fcntl<T>(sockfd: RawSocket, cmd: c_int, arg: T) -> io::Result<c_int> {
syscall!(fcntl(sockfd, cmd, arg))
}
/// Unix only API.
impl Socket {
/// Creates a pair of sockets which are connected to each other.
///
/// This function corresponds to `socketpair(2)`.
pub fn pair(
domain: Domain,
type_: Type,
protocol: Option<Protocol>,
) -> io::Result<(Socket, Socket)> {
let mut fds = [0, 0];
let protocol = protocol.map(|p| p.0).unwrap_or(0);
syscall!(socketpair(domain.0, type_.0, protocol, fds.as_mut_ptr()))
.map(|_| (Socket { inner: fds[0] }, Socket { inner: fds[1] }))
}
/// Accept a new incoming connection from this listener.
///
/// This function directly corresponds to the `accept4(2)` function.
///
/// # Notes
///
/// This only available on Android, DragonFlyBSD, FreeBSD, Linux and
/// OpenBSD. Once https://github.com/rust-lang/libc/issues/1636 is fixed
/// NetBSD will also support it.
#[cfg(any(
target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "linux",
// NetBSD 8.0 actually has `accept4(2)`, but libc doesn't expose it
// (yet). See https://github.com/rust-lang/libc/issues/1636.
//target_os = "netbsd",
target_os = "openbsd"
))]
pub fn accept4(&self, flags: c_int) -> io::Result<(Socket, SockAddr)> {
let mut addr: MaybeUninit<libc::sockaddr_storage> = MaybeUninit::uninit();
let mut addrlen = size_of::<libc::sockaddr_storage>() as libc::socklen_t;
syscall!(accept4(
self.inner,
addr.as_mut_ptr() as *mut _,
&mut addrlen,
flags
))
.map(|stream_fd| {
// This is safe because `accept(2)` filled in the address for us.
let addr = unsafe { SockAddr::from_raw_parts(addr.assume_init(), addrlen) };
(Socket { inner: stream_fd }, addr)
})
}
}
impl From<UnixStream> for Socket {
fn from(socket: UnixStream) -> Socket {
unsafe { Socket::from_raw_fd(socket.into_raw_fd()) }
}
}
impl Into<UnixStream> for Socket {
fn into(self) -> UnixStream {
unsafe { UnixStream::from_raw_fd(self.into_raw_fd()) }
}
}
impl From<UnixListener> for Socket {
fn from(socket: UnixListener) -> Socket {
unsafe { Socket::from_raw_fd(socket.into_raw_fd()) }
}
}
impl Into<UnixListener> for Socket {
fn into(self) -> UnixListener {
unsafe { UnixListener::from_raw_fd(self.into_raw_fd()) }
}
}
impl From<UnixDatagram> for Socket {
fn from(socket: UnixDatagram) -> Socket {
unsafe { Socket::from_raw_fd(socket.into_raw_fd()) }
}
}
impl Into<UnixDatagram> for Socket {
fn into(self) -> UnixDatagram {
unsafe { UnixDatagram::from_raw_fd(self.into_raw_fd()) }
}
}
impl FromRawFd for Socket {
unsafe fn from_raw_fd(fd: RawFd) -> Socket {
Socket { inner: fd }
}
}
impl AsRawFd for Socket {
fn as_raw_fd(&self) -> RawFd {
self.inner
}
}
impl IntoRawFd for Socket {
fn into_raw_fd(self) -> RawFd {
let fd = self.inner;
mem::forget(self);
fd
}
}
impl Drop for Socket {
fn drop(&mut self) {
// Can't handle the error here, nor can we do much with it.
let _ = unsafe { libc::close(self.inner) };
}
}