]> git.proxmox.com Git - rustc.git/blob - library/std/src/sys/solid/net.rs
New upstream version 1.61.0+dfsg1
[rustc.git] / library / std / src / sys / solid / net.rs
1 use super::abi;
2 use crate::{
3 cmp,
4 ffi::CStr,
5 io::{self, ErrorKind, IoSlice, IoSliceMut},
6 mem,
7 net::{Shutdown, SocketAddr},
8 ptr, str,
9 sys_common::net::{getsockopt, setsockopt, sockaddr_to_addr},
10 sys_common::{AsInner, FromInner, IntoInner},
11 time::Duration,
12 };
13
14 use self::netc::{sockaddr, socklen_t, MSG_PEEK};
15 use libc::{c_int, c_void, size_t};
16
17 pub mod netc {
18 pub use super::super::abi::sockets::*;
19 }
20
21 pub type wrlen_t = size_t;
22
23 const READ_LIMIT: usize = libc::ssize_t::MAX as usize;
24
25 const fn max_iov() -> usize {
26 // Judging by the source code, it's unlimited, but specify a lower
27 // value just in case.
28 1024
29 }
30
31 /// A file descriptor.
32 #[rustc_layout_scalar_valid_range_start(0)]
33 // libstd/os/raw/mod.rs assures me that every libstd-supported platform has a
34 // 32-bit c_int. Below is -2, in two's complement, but that only works out
35 // because c_int is 32 bits.
36 #[rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE)]
37 struct FileDesc {
38 fd: c_int,
39 }
40
41 impl FileDesc {
42 #[inline]
43 fn new(fd: c_int) -> FileDesc {
44 assert_ne!(fd, -1i32);
45 // Safety: we just asserted that the value is in the valid range and
46 // isn't `-1` (the only value bigger than `0xFF_FF_FF_FE` unsigned)
47 unsafe { FileDesc { fd } }
48 }
49
50 #[inline]
51 fn raw(&self) -> c_int {
52 self.fd
53 }
54
55 /// Extracts the actual file descriptor without closing it.
56 #[inline]
57 fn into_raw(self) -> c_int {
58 let fd = self.fd;
59 mem::forget(self);
60 fd
61 }
62
63 fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
64 let ret = cvt(unsafe {
65 netc::read(self.fd, buf.as_mut_ptr() as *mut c_void, cmp::min(buf.len(), READ_LIMIT))
66 })?;
67 Ok(ret as usize)
68 }
69
70 fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
71 let ret = cvt(unsafe {
72 netc::readv(
73 self.fd,
74 bufs.as_ptr() as *const netc::iovec,
75 cmp::min(bufs.len(), max_iov()) as c_int,
76 )
77 })?;
78 Ok(ret as usize)
79 }
80
81 #[inline]
82 fn is_read_vectored(&self) -> bool {
83 true
84 }
85
86 fn write(&self, buf: &[u8]) -> io::Result<usize> {
87 let ret = cvt(unsafe {
88 netc::write(self.fd, buf.as_ptr() as *const c_void, cmp::min(buf.len(), READ_LIMIT))
89 })?;
90 Ok(ret as usize)
91 }
92
93 fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
94 let ret = cvt(unsafe {
95 netc::writev(
96 self.fd,
97 bufs.as_ptr() as *const netc::iovec,
98 cmp::min(bufs.len(), max_iov()) as c_int,
99 )
100 })?;
101 Ok(ret as usize)
102 }
103
104 #[inline]
105 fn is_write_vectored(&self) -> bool {
106 true
107 }
108
109 fn duplicate(&self) -> io::Result<FileDesc> {
110 cvt(unsafe { netc::dup(self.fd) }).map(Self::new)
111 }
112 }
113
114 impl AsInner<c_int> for FileDesc {
115 fn as_inner(&self) -> &c_int {
116 &self.fd
117 }
118 }
119
120 impl Drop for FileDesc {
121 fn drop(&mut self) {
122 unsafe { netc::close(self.fd) };
123 }
124 }
125
126 #[doc(hidden)]
127 pub trait IsMinusOne {
128 fn is_minus_one(&self) -> bool;
129 }
130
131 macro_rules! impl_is_minus_one {
132 ($($t:ident)*) => ($(impl IsMinusOne for $t {
133 fn is_minus_one(&self) -> bool {
134 *self == -1
135 }
136 })*)
137 }
138
139 impl_is_minus_one! { i8 i16 i32 i64 isize }
140
141 pub fn cvt<T: IsMinusOne>(t: T) -> io::Result<T> {
142 if t.is_minus_one() { Err(last_error()) } else { Ok(t) }
143 }
144
145 /// A variant of `cvt` for `getaddrinfo` which return 0 for a success.
146 pub fn cvt_gai(err: c_int) -> io::Result<()> {
147 if err == 0 {
148 Ok(())
149 } else {
150 let msg: &dyn crate::fmt::Display = match err {
151 netc::EAI_NONAME => &"name or service not known",
152 netc::EAI_SERVICE => &"service not supported",
153 netc::EAI_FAIL => &"non-recoverable failure in name resolution",
154 netc::EAI_MEMORY => &"memory allocation failure",
155 netc::EAI_FAMILY => &"family not supported",
156 _ => &err,
157 };
158 Err(io::Error::new(
159 io::ErrorKind::Uncategorized,
160 &format!("failed to lookup address information: {msg}")[..],
161 ))
162 }
163 }
164
165 /// Just to provide the same interface as sys/unix/net.rs
166 pub fn cvt_r<T, F>(mut f: F) -> io::Result<T>
167 where
168 T: IsMinusOne,
169 F: FnMut() -> T,
170 {
171 cvt(f())
172 }
173
174 /// Returns the last error from the network subsystem.
175 fn last_error() -> io::Error {
176 io::Error::from_raw_os_error(unsafe { netc::SOLID_NET_GetLastError() })
177 }
178
179 pub(super) fn error_name(er: abi::ER) -> Option<&'static str> {
180 unsafe { CStr::from_ptr(netc::strerror(er)) }.to_str().ok()
181 }
182
183 pub(super) fn decode_error_kind(er: abi::ER) -> ErrorKind {
184 let errno = netc::SOLID_NET_ERR_BASE - er;
185 match errno as libc::c_int {
186 libc::ECONNREFUSED => ErrorKind::ConnectionRefused,
187 libc::ECONNRESET => ErrorKind::ConnectionReset,
188 libc::EPERM | libc::EACCES => ErrorKind::PermissionDenied,
189 libc::EPIPE => ErrorKind::BrokenPipe,
190 libc::ENOTCONN => ErrorKind::NotConnected,
191 libc::ECONNABORTED => ErrorKind::ConnectionAborted,
192 libc::EADDRNOTAVAIL => ErrorKind::AddrNotAvailable,
193 libc::EADDRINUSE => ErrorKind::AddrInUse,
194 libc::ENOENT => ErrorKind::NotFound,
195 libc::EINTR => ErrorKind::Interrupted,
196 libc::EINVAL => ErrorKind::InvalidInput,
197 libc::ETIMEDOUT => ErrorKind::TimedOut,
198 libc::EEXIST => ErrorKind::AlreadyExists,
199 libc::ENOSYS => ErrorKind::Unsupported,
200 libc::ENOMEM => ErrorKind::OutOfMemory,
201 libc::EAGAIN => ErrorKind::WouldBlock,
202
203 _ => ErrorKind::Uncategorized,
204 }
205 }
206
207 pub fn init() {}
208
209 pub struct Socket(FileDesc);
210
211 impl Socket {
212 pub fn new(addr: &SocketAddr, ty: c_int) -> io::Result<Socket> {
213 let fam = match *addr {
214 SocketAddr::V4(..) => netc::AF_INET,
215 SocketAddr::V6(..) => netc::AF_INET6,
216 };
217 Socket::new_raw(fam, ty)
218 }
219
220 pub fn new_raw(fam: c_int, ty: c_int) -> io::Result<Socket> {
221 unsafe {
222 let fd = cvt(netc::socket(fam, ty, 0))?;
223 let fd = FileDesc::new(fd);
224 let socket = Socket(fd);
225
226 Ok(socket)
227 }
228 }
229
230 pub fn connect_timeout(&self, addr: &SocketAddr, timeout: Duration) -> io::Result<()> {
231 self.set_nonblocking(true)?;
232 let r = unsafe {
233 let (addrp, len) = addr.into_inner();
234 cvt(netc::connect(self.0.raw(), addrp, len))
235 };
236 self.set_nonblocking(false)?;
237
238 match r {
239 Ok(_) => return Ok(()),
240 // there's no ErrorKind for EINPROGRESS
241 Err(ref e) if e.raw_os_error() == Some(netc::EINPROGRESS) => {}
242 Err(e) => return Err(e),
243 }
244
245 if timeout.as_secs() == 0 && timeout.subsec_nanos() == 0 {
246 return Err(io::const_io_error!(
247 io::ErrorKind::InvalidInput,
248 "cannot set a 0 duration timeout",
249 ));
250 }
251
252 let mut timeout =
253 netc::timeval { tv_sec: timeout.as_secs() as _, tv_usec: timeout.subsec_micros() as _ };
254 if timeout.tv_sec == 0 && timeout.tv_usec == 0 {
255 timeout.tv_usec = 1;
256 }
257
258 let fds = netc::fd_set { num_fds: 1, fds: [self.0.raw()] };
259
260 let mut writefds = fds;
261 let mut errorfds = fds;
262
263 let n = unsafe {
264 cvt(netc::select(
265 self.0.raw() + 1,
266 ptr::null_mut(),
267 &mut writefds,
268 &mut errorfds,
269 &mut timeout,
270 ))?
271 };
272
273 match n {
274 0 => Err(io::const_io_error!(io::ErrorKind::TimedOut, "connection timed out")),
275 _ => {
276 let can_write = writefds.num_fds != 0;
277 if !can_write {
278 if let Some(e) = self.take_error()? {
279 return Err(e);
280 }
281 }
282 Ok(())
283 }
284 }
285 }
286
287 pub fn accept(&self, storage: *mut sockaddr, len: *mut socklen_t) -> io::Result<Socket> {
288 let fd = cvt_r(|| unsafe { netc::accept(self.0.raw(), storage, len) })?;
289 let fd = FileDesc::new(fd);
290 Ok(Socket(fd))
291 }
292
293 pub fn duplicate(&self) -> io::Result<Socket> {
294 self.0.duplicate().map(Socket)
295 }
296
297 fn recv_with_flags(&self, buf: &mut [u8], flags: c_int) -> io::Result<usize> {
298 let ret = cvt(unsafe {
299 netc::recv(self.0.raw(), buf.as_mut_ptr() as *mut c_void, buf.len(), flags)
300 })?;
301 Ok(ret as usize)
302 }
303
304 pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
305 self.recv_with_flags(buf, 0)
306 }
307
308 pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
309 self.recv_with_flags(buf, MSG_PEEK)
310 }
311
312 pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
313 self.0.read_vectored(bufs)
314 }
315
316 #[inline]
317 pub fn is_read_vectored(&self) -> bool {
318 self.0.is_read_vectored()
319 }
320
321 fn recv_from_with_flags(
322 &self,
323 buf: &mut [u8],
324 flags: c_int,
325 ) -> io::Result<(usize, SocketAddr)> {
326 let mut storage: netc::sockaddr_storage = unsafe { mem::zeroed() };
327 let mut addrlen = mem::size_of_val(&storage) as netc::socklen_t;
328
329 let n = cvt(unsafe {
330 netc::recvfrom(
331 self.0.raw(),
332 buf.as_mut_ptr() as *mut c_void,
333 buf.len(),
334 flags,
335 &mut storage as *mut _ as *mut _,
336 &mut addrlen,
337 )
338 })?;
339 Ok((n as usize, sockaddr_to_addr(&storage, addrlen as usize)?))
340 }
341
342 pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
343 self.recv_from_with_flags(buf, 0)
344 }
345
346 pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
347 self.recv_from_with_flags(buf, MSG_PEEK)
348 }
349
350 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
351 self.0.write(buf)
352 }
353
354 pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
355 self.0.write_vectored(bufs)
356 }
357
358 #[inline]
359 pub fn is_write_vectored(&self) -> bool {
360 self.0.is_write_vectored()
361 }
362
363 pub fn set_timeout(&self, dur: Option<Duration>, kind: c_int) -> io::Result<()> {
364 let timeout = match dur {
365 Some(dur) => {
366 if dur.as_secs() == 0 && dur.subsec_nanos() == 0 {
367 return Err(io::const_io_error!(
368 io::ErrorKind::InvalidInput,
369 "cannot set a 0 duration timeout",
370 ));
371 }
372
373 let secs = if dur.as_secs() > netc::c_long::MAX as u64 {
374 netc::c_long::MAX
375 } else {
376 dur.as_secs() as netc::c_long
377 };
378 let mut timeout = netc::timeval { tv_sec: secs, tv_usec: dur.subsec_micros() as _ };
379 if timeout.tv_sec == 0 && timeout.tv_usec == 0 {
380 timeout.tv_usec = 1;
381 }
382 timeout
383 }
384 None => netc::timeval { tv_sec: 0, tv_usec: 0 },
385 };
386 setsockopt(self, netc::SOL_SOCKET, kind, timeout)
387 }
388
389 pub fn timeout(&self, kind: c_int) -> io::Result<Option<Duration>> {
390 let raw: netc::timeval = getsockopt(self, netc::SOL_SOCKET, kind)?;
391 if raw.tv_sec == 0 && raw.tv_usec == 0 {
392 Ok(None)
393 } else {
394 let sec = raw.tv_sec as u64;
395 let nsec = (raw.tv_usec as u32) * 1000;
396 Ok(Some(Duration::new(sec, nsec)))
397 }
398 }
399
400 pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
401 let how = match how {
402 Shutdown::Write => netc::SHUT_WR,
403 Shutdown::Read => netc::SHUT_RD,
404 Shutdown::Both => netc::SHUT_RDWR,
405 };
406 cvt(unsafe { netc::shutdown(self.0.raw(), how) })?;
407 Ok(())
408 }
409
410 pub fn set_linger(&self, linger: Option<Duration>) -> io::Result<()> {
411 let linger = netc::linger {
412 l_onoff: linger.is_some() as netc::c_int,
413 l_linger: linger.unwrap_or_default().as_secs() as netc::c_int,
414 };
415
416 setsockopt(self, netc::SOL_SOCKET, netc::SO_LINGER, linger)
417 }
418
419 pub fn linger(&self) -> io::Result<Option<Duration>> {
420 let val: netc::linger = getsockopt(self, netc::SOL_SOCKET, netc::SO_LINGER)?;
421
422 Ok((val.l_onoff != 0).then(|| Duration::from_secs(val.l_linger as u64)))
423 }
424
425 pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
426 setsockopt(self, netc::IPPROTO_TCP, netc::TCP_NODELAY, nodelay as c_int)
427 }
428
429 pub fn nodelay(&self) -> io::Result<bool> {
430 let raw: c_int = getsockopt(self, netc::IPPROTO_TCP, netc::TCP_NODELAY)?;
431 Ok(raw != 0)
432 }
433
434 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
435 let mut nonblocking = nonblocking as c_int;
436 cvt(unsafe {
437 netc::ioctl(*self.as_inner(), netc::FIONBIO, (&mut nonblocking) as *mut c_int as _)
438 })
439 .map(drop)
440 }
441
442 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
443 let raw: c_int = getsockopt(self, netc::SOL_SOCKET, netc::SO_ERROR)?;
444 if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }
445 }
446
447 // This method is used by sys_common code to abstract over targets.
448 pub fn as_raw(&self) -> c_int {
449 *self.as_inner()
450 }
451 }
452
453 impl AsInner<c_int> for Socket {
454 fn as_inner(&self) -> &c_int {
455 self.0.as_inner()
456 }
457 }
458
459 impl FromInner<c_int> for Socket {
460 fn from_inner(fd: c_int) -> Socket {
461 Socket(FileDesc::new(fd))
462 }
463 }
464
465 impl IntoInner<c_int> for Socket {
466 fn into_inner(self) -> c_int {
467 self.0.into_raw()
468 }
469 }