summaryrefslogtreecommitdiffstats
path: root/library/std/src/sys/hermit/net.rs
diff options
context:
space:
mode:
Diffstat (limited to 'library/std/src/sys/hermit/net.rs')
-rw-r--r--library/std/src/sys/hermit/net.rs639
1 files changed, 251 insertions, 388 deletions
diff --git a/library/std/src/sys/hermit/net.rs b/library/std/src/sys/hermit/net.rs
index 8a13879d8..5fb6281aa 100644
--- a/library/std/src/sys/hermit/net.rs
+++ b/library/std/src/sys/hermit/net.rs
@@ -1,490 +1,353 @@
-use crate::fmt;
-use crate::io::{self, ErrorKind, IoSlice, IoSliceMut};
-use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr, Shutdown, SocketAddr};
-use crate::str;
-use crate::sync::Arc;
-use crate::sys::hermit::abi;
-use crate::sys::hermit::abi::IpAddress::{Ipv4, Ipv6};
-use crate::sys::unsupported;
-use crate::sys_common::AsInner;
+#![allow(dead_code)]
+
+use crate::cmp;
+use crate::io::{self, IoSlice, IoSliceMut};
+use crate::mem;
+use crate::net::{Shutdown, SocketAddr};
+use crate::os::hermit::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, RawFd};
+use crate::sys::hermit::fd::FileDesc;
+use crate::sys::time::Instant;
+use crate::sys_common::net::{getsockopt, setsockopt, sockaddr_to_addr};
+use crate::sys_common::{AsInner, FromInner, IntoInner};
use crate::time::Duration;
-/// Checks whether the HermitCore's socket interface has been started already, and
-/// if not, starts it.
-pub fn init() -> io::Result<()> {
- if abi::network_init() < 0 {
- return Err(io::const_io_error!(
- ErrorKind::Uncategorized,
- "Unable to initialize network interface",
- ));
- }
-
- Ok(())
-}
-
-#[derive(Debug, Clone)]
-pub struct Socket(abi::Handle);
-
-impl AsInner<abi::Handle> for Socket {
- fn as_inner(&self) -> &abi::Handle {
- &self.0
- }
-}
+use core::ffi::c_int;
-impl Drop for Socket {
- fn drop(&mut self) {
- let _ = abi::tcpstream::close(self.0);
- }
-}
+#[allow(unused_extern_crates)]
+pub extern crate hermit_abi as netc;
-// Arc is used to count the number of used sockets.
-// Only if all sockets are released, the drop
-// method will close the socket.
-#[derive(Clone)]
-pub struct TcpStream(Arc<Socket>);
-
-impl TcpStream {
- pub fn connect(addr: io::Result<&SocketAddr>) -> io::Result<TcpStream> {
- let addr = addr?;
-
- match abi::tcpstream::connect(addr.ip().to_string().as_bytes(), addr.port(), None) {
- Ok(handle) => Ok(TcpStream(Arc::new(Socket(handle)))),
- _ => Err(io::const_io_error!(
- ErrorKind::Uncategorized,
- "Unable to initiate a connection on a socket",
- )),
- }
- }
+pub use crate::sys::{cvt, cvt_r};
- pub fn connect_timeout(saddr: &SocketAddr, duration: Duration) -> io::Result<TcpStream> {
- match abi::tcpstream::connect(
- saddr.ip().to_string().as_bytes(),
- saddr.port(),
- Some(duration.as_millis() as u64),
- ) {
- Ok(handle) => Ok(TcpStream(Arc::new(Socket(handle)))),
- _ => Err(io::const_io_error!(
- ErrorKind::Uncategorized,
- "Unable to initiate a connection on a socket",
- )),
- }
- }
+pub type wrlen_t = usize;
- pub fn set_read_timeout(&self, duration: Option<Duration>) -> io::Result<()> {
- abi::tcpstream::set_read_timeout(*self.0.as_inner(), duration.map(|d| d.as_millis() as u64))
- .map_err(|_| {
- io::const_io_error!(ErrorKind::Uncategorized, "Unable to set timeout value")
- })
+pub fn cvt_gai(err: i32) -> io::Result<()> {
+ if err == 0 {
+ return Ok(());
}
- pub fn set_write_timeout(&self, duration: Option<Duration>) -> io::Result<()> {
- abi::tcpstream::set_write_timeout(
- *self.0.as_inner(),
- duration.map(|d| d.as_millis() as u64),
- )
- .map_err(|_| io::const_io_error!(ErrorKind::Uncategorized, "Unable to set timeout value"))
- }
+ let detail = "";
- pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
- let duration = abi::tcpstream::get_read_timeout(*self.0.as_inner()).map_err(|_| {
- io::const_io_error!(ErrorKind::Uncategorized, "Unable to determine timeout value")
- })?;
+ Err(io::Error::new(
+ io::ErrorKind::Uncategorized,
+ &format!("failed to lookup address information: {detail}")[..],
+ ))
+}
- Ok(duration.map(|d| Duration::from_millis(d)))
+/// Checks whether the HermitCore's socket interface has been started already, and
+/// if not, starts it.
+pub fn init() {
+ if unsafe { netc::network_init() } < 0 {
+ panic!("Unable to initialize network interface");
}
+}
- pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
- let duration = abi::tcpstream::get_write_timeout(*self.0.as_inner()).map_err(|_| {
- io::const_io_error!(ErrorKind::Uncategorized, "Unable to determine timeout value")
- })?;
+#[derive(Debug)]
+pub struct Socket(FileDesc);
- Ok(duration.map(|d| Duration::from_millis(d)))
+impl Socket {
+ pub fn new(addr: &SocketAddr, ty: i32) -> io::Result<Socket> {
+ let fam = match *addr {
+ SocketAddr::V4(..) => netc::AF_INET,
+ SocketAddr::V6(..) => netc::AF_INET6,
+ };
+ Socket::new_raw(fam, ty)
}
- pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
- abi::tcpstream::peek(*self.0.as_inner(), buf)
- .map_err(|_| io::const_io_error!(ErrorKind::Uncategorized, "peek failed"))
+ pub fn new_raw(fam: i32, ty: i32) -> io::Result<Socket> {
+ let fd = cvt(unsafe { netc::socket(fam, ty, 0) })?;
+ Ok(Socket(unsafe { FileDesc::from_raw_fd(fd) }))
}
- pub fn read(&self, buffer: &mut [u8]) -> io::Result<usize> {
- self.read_vectored(&mut [IoSliceMut::new(buffer)])
+ pub fn new_pair(_fam: i32, _ty: i32) -> io::Result<(Socket, Socket)> {
+ unimplemented!()
}
- pub fn read_vectored(&self, ioslice: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
- let mut size: usize = 0;
-
- for i in ioslice.iter_mut() {
- let ret = abi::tcpstream::read(*self.0.as_inner(), &mut i[0..]).map_err(|_| {
- io::const_io_error!(ErrorKind::Uncategorized, "Unable to read on socket")
- })?;
+ pub fn connect_timeout(&self, addr: &SocketAddr, timeout: Duration) -> io::Result<()> {
+ self.set_nonblocking(true)?;
+ let r = unsafe {
+ let (addr, len) = addr.into_inner();
+ cvt(netc::connect(self.as_raw_fd(), addr.as_ptr(), len))
+ };
+ self.set_nonblocking(false)?;
- if ret != 0 {
- size += ret;
- }
+ match r {
+ Ok(_) => return Ok(()),
+ // there's no ErrorKind for EINPROGRESS :(
+ Err(ref e) if e.raw_os_error() == Some(netc::errno::EINPROGRESS) => {}
+ Err(e) => return Err(e),
}
- Ok(size)
- }
-
- #[inline]
- pub fn is_read_vectored(&self) -> bool {
- true
- }
-
- pub fn write(&self, buffer: &[u8]) -> io::Result<usize> {
- self.write_vectored(&[IoSlice::new(buffer)])
- }
+ let mut pollfd = netc::pollfd { fd: self.as_raw_fd(), events: netc::POLLOUT, revents: 0 };
- pub fn write_vectored(&self, ioslice: &[IoSlice<'_>]) -> io::Result<usize> {
- let mut size: usize = 0;
-
- for i in ioslice.iter() {
- size += abi::tcpstream::write(*self.0.as_inner(), i).map_err(|_| {
- io::const_io_error!(ErrorKind::Uncategorized, "Unable to write on socket")
- })?;
+ if timeout.as_secs() == 0 && timeout.subsec_nanos() == 0 {
+ return Err(io::const_io_error!(
+ io::ErrorKind::InvalidInput,
+ "cannot set a 0 duration timeout",
+ ));
}
- Ok(size)
- }
-
- #[inline]
- pub fn is_write_vectored(&self) -> bool {
- true
- }
-
- pub fn peer_addr(&self) -> io::Result<SocketAddr> {
- let (ipaddr, port) = abi::tcpstream::peer_addr(*self.0.as_inner())
- .map_err(|_| io::const_io_error!(ErrorKind::Uncategorized, "peer_addr failed"))?;
+ let start = Instant::now();
- let saddr = match ipaddr {
- Ipv4(ref addr) => SocketAddr::new(IpAddr::V4(Ipv4Addr::from(addr.0)), port),
- Ipv6(ref addr) => SocketAddr::new(IpAddr::V6(Ipv6Addr::from(addr.0)), port),
- _ => {
- return Err(io::const_io_error!(ErrorKind::Uncategorized, "peer_addr failed"));
+ loop {
+ let elapsed = start.elapsed();
+ if elapsed >= timeout {
+ return Err(io::const_io_error!(io::ErrorKind::TimedOut, "connection timed out"));
}
- };
-
- Ok(saddr)
- }
-
- pub fn socket_addr(&self) -> io::Result<SocketAddr> {
- unsupported()
- }
-
- pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
- abi::tcpstream::shutdown(*self.0.as_inner(), how as i32)
- .map_err(|_| io::const_io_error!(ErrorKind::Uncategorized, "unable to shutdown socket"))
- }
-
- pub fn duplicate(&self) -> io::Result<TcpStream> {
- Ok(self.clone())
- }
-
- pub fn set_linger(&self, _linger: Option<Duration>) -> io::Result<()> {
- unsupported()
- }
-
- pub fn linger(&self) -> io::Result<Option<Duration>> {
- unsupported()
- }
-
- pub fn set_nodelay(&self, mode: bool) -> io::Result<()> {
- abi::tcpstream::set_nodelay(*self.0.as_inner(), mode)
- .map_err(|_| io::const_io_error!(ErrorKind::Uncategorized, "set_nodelay failed"))
- }
-
- pub fn nodelay(&self) -> io::Result<bool> {
- abi::tcpstream::nodelay(*self.0.as_inner())
- .map_err(|_| io::const_io_error!(ErrorKind::Uncategorized, "nodelay failed"))
- }
-
- pub fn set_ttl(&self, tll: u32) -> io::Result<()> {
- abi::tcpstream::set_tll(*self.0.as_inner(), tll)
- .map_err(|_| io::const_io_error!(ErrorKind::Uncategorized, "unable to set TTL"))
- }
-
- pub fn ttl(&self) -> io::Result<u32> {
- abi::tcpstream::get_tll(*self.0.as_inner())
- .map_err(|_| io::const_io_error!(ErrorKind::Uncategorized, "unable to get TTL"))
- }
- pub fn take_error(&self) -> io::Result<Option<io::Error>> {
- unsupported()
- }
-
- pub fn set_nonblocking(&self, mode: bool) -> io::Result<()> {
- abi::tcpstream::set_nonblocking(*self.0.as_inner(), mode).map_err(|_| {
- io::const_io_error!(ErrorKind::Uncategorized, "unable to set blocking mode")
- })
- }
-}
-
-impl fmt::Debug for TcpStream {
- fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
- Ok(())
- }
-}
-
-#[derive(Clone)]
-pub struct TcpListener(SocketAddr);
-
-impl TcpListener {
- pub fn bind(addr: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
- let addr = addr?;
-
- Ok(TcpListener(*addr))
- }
-
- pub fn socket_addr(&self) -> io::Result<SocketAddr> {
- Ok(self.0)
- }
-
- pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
- let (handle, ipaddr, port) = abi::tcplistener::accept(self.0.port())
- .map_err(|_| io::const_io_error!(ErrorKind::Uncategorized, "accept failed"))?;
- let saddr = match ipaddr {
- Ipv4(ref addr) => SocketAddr::new(IpAddr::V4(Ipv4Addr::from(addr.0)), port),
- Ipv6(ref addr) => SocketAddr::new(IpAddr::V6(Ipv6Addr::from(addr.0)), port),
- _ => {
- return Err(io::const_io_error!(ErrorKind::Uncategorized, "accept failed"));
+ let timeout = timeout - elapsed;
+ let mut timeout = timeout
+ .as_secs()
+ .saturating_mul(1_000)
+ .saturating_add(timeout.subsec_nanos() as u64 / 1_000_000);
+ if timeout == 0 {
+ timeout = 1;
}
- };
-
- Ok((TcpStream(Arc::new(Socket(handle))), saddr))
- }
- pub fn duplicate(&self) -> io::Result<TcpListener> {
- Ok(self.clone())
- }
-
- pub fn set_ttl(&self, _: u32) -> io::Result<()> {
- unsupported()
- }
-
- pub fn ttl(&self) -> io::Result<u32> {
- unsupported()
- }
-
- pub fn set_only_v6(&self, _: bool) -> io::Result<()> {
- unsupported()
+ let timeout = cmp::min(timeout, c_int::MAX as u64) as c_int;
+
+ match unsafe { netc::poll(&mut pollfd, 1, timeout) } {
+ -1 => {
+ let err = io::Error::last_os_error();
+ if err.kind() != io::ErrorKind::Interrupted {
+ return Err(err);
+ }
+ }
+ 0 => {}
+ _ => {
+ // linux returns POLLOUT|POLLERR|POLLHUP for refused connections (!), so look
+ // for POLLHUP rather than read readiness
+ if pollfd.revents & netc::POLLHUP != 0 {
+ let e = self.take_error()?.unwrap_or_else(|| {
+ io::const_io_error!(
+ io::ErrorKind::Uncategorized,
+ "no error set after POLLHUP",
+ )
+ });
+ return Err(e);
+ }
+
+ return Ok(());
+ }
+ }
+ }
}
- pub fn only_v6(&self) -> io::Result<bool> {
- unsupported()
+ pub fn accept(
+ &self,
+ storage: *mut netc::sockaddr,
+ len: *mut netc::socklen_t,
+ ) -> io::Result<Socket> {
+ let fd = cvt(unsafe { netc::accept(self.0.as_raw_fd(), storage, len) })?;
+ Ok(Socket(unsafe { FileDesc::from_raw_fd(fd) }))
}
- pub fn take_error(&self) -> io::Result<Option<io::Error>> {
- unsupported()
+ pub fn duplicate(&self) -> io::Result<Socket> {
+ let fd = cvt(unsafe { netc::dup(self.0.as_raw_fd()) })?;
+ Ok(Socket(unsafe { FileDesc::from_raw_fd(fd) }))
}
- pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
- unsupported()
+ fn recv_with_flags(&self, buf: &mut [u8], flags: i32) -> io::Result<usize> {
+ let ret =
+ cvt(unsafe { netc::recv(self.0.as_raw_fd(), buf.as_mut_ptr(), buf.len(), flags) })?;
+ Ok(ret as usize)
}
-}
-impl fmt::Debug for TcpListener {
- fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
- Ok(())
+ pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
+ self.recv_with_flags(buf, 0)
}
-}
-
-pub struct UdpSocket(abi::Handle);
-impl UdpSocket {
- pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<UdpSocket> {
- unsupported()
+ pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
+ self.recv_with_flags(buf, netc::MSG_PEEK)
}
- pub fn peer_addr(&self) -> io::Result<SocketAddr> {
- unsupported()
- }
+ pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
+ let mut size: isize = 0;
- pub fn socket_addr(&self) -> io::Result<SocketAddr> {
- unsupported()
- }
+ for i in bufs.iter_mut() {
+ let ret: isize =
+ cvt(unsafe { netc::read(self.0.as_raw_fd(), i.as_mut_ptr(), i.len()) })?;
- pub fn recv_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
- unsupported()
- }
+ if ret != 0 {
+ size += ret;
+ }
+ }
- pub fn peek_from(&self, _: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
- unsupported()
+ Ok(size.try_into().unwrap())
}
- pub fn send_to(&self, _: &[u8], _: &SocketAddr) -> io::Result<usize> {
- unsupported()
+ #[inline]
+ pub fn is_read_vectored(&self) -> bool {
+ true
}
- pub fn duplicate(&self) -> io::Result<UdpSocket> {
- unsupported()
- }
+ fn recv_from_with_flags(&self, buf: &mut [u8], flags: i32) -> io::Result<(usize, SocketAddr)> {
+ let mut storage: netc::sockaddr_storage = unsafe { mem::zeroed() };
+ let mut addrlen = mem::size_of_val(&storage) as netc::socklen_t;
- pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> {
- unsupported()
+ let n = cvt(unsafe {
+ netc::recvfrom(
+ self.as_raw_fd(),
+ buf.as_mut_ptr(),
+ buf.len(),
+ flags,
+ &mut storage as *mut _ as *mut _,
+ &mut addrlen,
+ )
+ })?;
+ Ok((n as usize, sockaddr_to_addr(&storage, addrlen as usize)?))
}
- pub fn set_write_timeout(&self, _: Option<Duration>) -> io::Result<()> {
- unsupported()
+ pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
+ self.recv_from_with_flags(buf, 0)
}
- pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
- unsupported()
+ pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
+ self.recv_from_with_flags(buf, netc::MSG_PEEK)
}
- pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
- unsupported()
+ pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
+ let sz = cvt(unsafe { netc::write(self.0.as_raw_fd(), buf.as_ptr(), buf.len()) })?;
+ Ok(sz.try_into().unwrap())
}
- pub fn set_broadcast(&self, _: bool) -> io::Result<()> {
- unsupported()
- }
+ pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+ let mut size: isize = 0;
- pub fn broadcast(&self) -> io::Result<bool> {
- unsupported()
- }
+ for i in bufs.iter() {
+ size += cvt(unsafe { netc::write(self.0.as_raw_fd(), i.as_ptr(), i.len()) })?;
+ }
- pub fn set_multicast_loop_v4(&self, _: bool) -> io::Result<()> {
- unsupported()
+ Ok(size.try_into().unwrap())
}
- pub fn multicast_loop_v4(&self) -> io::Result<bool> {
- unsupported()
+ pub fn is_write_vectored(&self) -> bool {
+ true
}
- pub fn set_multicast_ttl_v4(&self, _: u32) -> io::Result<()> {
- unsupported()
- }
+ pub fn set_timeout(&self, dur: Option<Duration>, kind: i32) -> io::Result<()> {
+ let timeout = match dur {
+ Some(dur) => {
+ if dur.as_secs() == 0 && dur.subsec_nanos() == 0 {
+ return Err(io::const_io_error!(
+ io::ErrorKind::InvalidInput,
+ "cannot set a 0 duration timeout",
+ ));
+ }
+
+ let secs = if dur.as_secs() > netc::time_t::MAX as u64 {
+ netc::time_t::MAX
+ } else {
+ dur.as_secs() as netc::time_t
+ };
+ let mut timeout = netc::timeval {
+ tv_sec: secs,
+ tv_usec: dur.subsec_micros() as netc::suseconds_t,
+ };
+ if timeout.tv_sec == 0 && timeout.tv_usec == 0 {
+ timeout.tv_usec = 1;
+ }
+ timeout
+ }
+ None => netc::timeval { tv_sec: 0, tv_usec: 0 },
+ };
- pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
- unsupported()
+ setsockopt(self, netc::SOL_SOCKET, kind, timeout)
}
- pub fn set_multicast_loop_v6(&self, _: bool) -> io::Result<()> {
- unsupported()
+ pub fn timeout(&self, kind: i32) -> io::Result<Option<Duration>> {
+ let raw: netc::timeval = getsockopt(self, netc::SOL_SOCKET, kind)?;
+ if raw.tv_sec == 0 && raw.tv_usec == 0 {
+ Ok(None)
+ } else {
+ let sec = raw.tv_sec as u64;
+ let nsec = (raw.tv_usec as u32) * 1000;
+ Ok(Some(Duration::new(sec, nsec)))
+ }
}
- pub fn multicast_loop_v6(&self) -> io::Result<bool> {
- unsupported()
+ pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
+ let how = match how {
+ Shutdown::Write => netc::SHUT_WR,
+ Shutdown::Read => netc::SHUT_RD,
+ Shutdown::Both => netc::SHUT_RDWR,
+ };
+ cvt(unsafe { netc::shutdown_socket(self.as_raw_fd(), how) })?;
+ Ok(())
}
- pub fn join_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> {
- unsupported()
- }
+ pub fn set_linger(&self, linger: Option<Duration>) -> io::Result<()> {
+ let linger = netc::linger {
+ l_onoff: linger.is_some() as i32,
+ l_linger: linger.unwrap_or_default().as_secs() as libc::c_int,
+ };
- pub fn join_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> {
- unsupported()
+ setsockopt(self, netc::SOL_SOCKET, netc::SO_LINGER, linger)
}
- pub fn leave_multicast_v4(&self, _: &Ipv4Addr, _: &Ipv4Addr) -> io::Result<()> {
- unsupported()
- }
+ pub fn linger(&self) -> io::Result<Option<Duration>> {
+ let val: netc::linger = getsockopt(self, netc::SOL_SOCKET, netc::SO_LINGER)?;
- pub fn leave_multicast_v6(&self, _: &Ipv6Addr, _: u32) -> io::Result<()> {
- unsupported()
+ Ok((val.l_onoff != 0).then(|| Duration::from_secs(val.l_linger as u64)))
}
- pub fn set_ttl(&self, _: u32) -> io::Result<()> {
- unsupported()
+ pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
+ let value: i32 = if nodelay { 1 } else { 0 };
+ setsockopt(self, netc::IPPROTO_TCP, netc::TCP_NODELAY, value)
}
- pub fn ttl(&self) -> io::Result<u32> {
- unsupported()
+ pub fn nodelay(&self) -> io::Result<bool> {
+ let raw: i32 = getsockopt(self, netc::IPPROTO_TCP, netc::TCP_NODELAY)?;
+ Ok(raw != 0)
+ }
+
+ pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
+ let mut nonblocking: i32 = if nonblocking { 1 } else { 0 };
+ cvt(unsafe {
+ netc::ioctl(
+ self.as_raw_fd(),
+ netc::FIONBIO,
+ &mut nonblocking as *mut _ as *mut core::ffi::c_void,
+ )
+ })
+ .map(drop)
}
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
- unsupported()
- }
-
- pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
- unsupported()
- }
-
- pub fn recv(&self, _: &mut [u8]) -> io::Result<usize> {
- unsupported()
- }
-
- pub fn peek(&self, _: &mut [u8]) -> io::Result<usize> {
- unsupported()
+ unimplemented!()
}
- pub fn send(&self, _: &[u8]) -> io::Result<usize> {
- unsupported()
- }
-
- pub fn connect(&self, _: io::Result<&SocketAddr>) -> io::Result<()> {
- unsupported()
+ // This is used by sys_common code to abstract over Windows and Unix.
+ pub fn as_raw(&self) -> RawFd {
+ self.0.as_raw_fd()
}
}
-impl fmt::Debug for UdpSocket {
- fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
- Ok(())
+impl AsInner<FileDesc> for Socket {
+ fn as_inner(&self) -> &FileDesc {
+ &self.0
}
}
-pub struct LookupHost(!);
-
-impl LookupHost {
- pub fn port(&self) -> u16 {
+impl IntoInner<FileDesc> for Socket {
+ fn into_inner(self) -> FileDesc {
self.0
}
}
-impl Iterator for LookupHost {
- type Item = SocketAddr;
- fn next(&mut self) -> Option<SocketAddr> {
- self.0
+impl FromInner<FileDesc> for Socket {
+ fn from_inner(file_desc: FileDesc) -> Self {
+ Self(file_desc)
}
}
-impl TryFrom<&str> for LookupHost {
- type Error = io::Error;
-
- fn try_from(_v: &str) -> io::Result<LookupHost> {
- unsupported()
+impl AsFd for Socket {
+ fn as_fd(&self) -> BorrowedFd<'_> {
+ self.0.as_fd()
}
}
-impl<'a> TryFrom<(&'a str, u16)> for LookupHost {
- type Error = io::Error;
-
- fn try_from(_v: (&'a str, u16)) -> io::Result<LookupHost> {
- unsupported()
+impl AsRawFd for Socket {
+ fn as_raw_fd(&self) -> RawFd {
+ self.0.as_raw_fd()
}
}
-
-#[allow(nonstandard_style)]
-pub mod netc {
- pub const AF_INET: u8 = 0;
- pub const AF_INET6: u8 = 1;
- pub type sa_family_t = u8;
-
- #[derive(Copy, Clone)]
- pub struct in_addr {
- pub s_addr: u32,
- }
-
- #[derive(Copy, Clone)]
- pub struct sockaddr_in {
- pub sin_family: sa_family_t,
- pub sin_port: u16,
- pub sin_addr: in_addr,
- }
-
- #[derive(Copy, Clone)]
- pub struct in6_addr {
- pub s6_addr: [u8; 16],
- }
-
- #[derive(Copy, Clone)]
- pub struct sockaddr_in6 {
- pub sin6_family: sa_family_t,
- pub sin6_port: u16,
- pub sin6_addr: in6_addr,
- pub sin6_flowinfo: u32,
- pub sin6_scope_id: u32,
- }
-
- #[derive(Copy, Clone)]
- pub struct sockaddr {}
-}