From eb4427556145e0f055f080268b81fdec615a11f8 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Mon, 17 Oct 2022 10:50:11 -0600 Subject: [PATCH] Remove special casing for Redox as it is supported by unix target family --- Cargo.toml | 2 +- src/ext.rs | 113 ++++------------------------------------- src/lib.rs | 3 +- src/socket.rs | 2 +- src/sys/redox/impls.rs | 43 ---------------- src/sys/redox/mod.rs | 81 ----------------------------- 6 files changed, 12 insertions(+), 232 deletions(-) delete mode 100644 src/sys/redox/impls.rs delete mode 100644 src/sys/redox/mod.rs diff --git a/Cargo.toml b/Cargo.toml index 5b341f2..06c6e8e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ include = [ [target."cfg(windows)".dependencies] winapi = { version = "0.3", features = ["handleapi", "winsock2", "ws2def", "ws2ipdef", "ws2tcpip"] } -[target.'cfg(any(target_os="redox", unix, target_os="wasi"))'.dependencies] +[target.'cfg(any(unix, target_os="wasi"))'.dependencies] libc = "0.2.54" [dependencies] diff --git a/src/ext.rs b/src/ext.rs index ca64bef..76e58d9 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -40,10 +40,9 @@ cfg_if! { use std::time::Duration; -#[cfg(any(unix, target_os = "redox", target_os = "wasi"))] use libc::*; -#[cfg(any(unix, target_os = "redox"))] use std::os::unix::prelude::*; +#[cfg(any(unix, target_os = "wasi"))] use libc::*; +#[cfg(any(unix))] use std::os::unix::prelude::*; #[cfg(target_os = "wasi")] use std::os::wasi::prelude::*; -#[cfg(target_os = "redox")] pub type Socket = usize; #[cfg(unix)] pub type Socket = c_int; #[cfg(target_os = "wasi")] pub type Socket = std::os::wasi::io::RawFd; #[cfg(windows)] pub type Socket = SOCKET; @@ -59,7 +58,7 @@ struct tcp_keepalive { keepaliveinterval: c_ulong, } -#[cfg(any(unix, target_os = "redox", target_os = "wasi"))] fn v(opt: c_int) -> c_int { opt } +#[cfg(any(unix, target_os = "wasi"))] fn v(opt: c_int) -> c_int { opt } #[cfg(windows)] fn v(opt: IPPROTO) -> c_int { opt as c_int } #[cfg(target_os = "wasi")] @@ -73,8 +72,6 @@ pub fn set_opt(sock: Socket, opt: c_int, val: c_int, payload: T) -> io::Result<()> { unsafe { let payload = &payload as *const T as *const c_void; - #[cfg(target_os = "redox")] - let sock = sock as c_int; try!(::cvt(setsockopt(sock, opt, val, payload as *const _, mem::size_of::() as socklen_t))); } @@ -90,8 +87,6 @@ pub fn get_opt(sock: Socket, opt: c_int, val: c_int) -> io::Result { unsafe { let mut slot: T = mem::zeroed(); let mut len = mem::size_of::() as socklen_t; - #[cfg(target_os = "redox")] - let sock = sock as c_int; try!(::cvt(getsockopt(sock, opt, val, &mut slot as *mut _ as *mut _, &mut len))); @@ -656,7 +651,7 @@ pub trait AsSock { fn as_sock(&self) -> Socket; } -#[cfg(any(unix, target_os = "redox", target_os = "wasi"))] +#[cfg(any(unix, target_os = "wasi"))] impl AsSock for T { fn as_sock(&self) -> Socket { self.as_raw_fd() } } @@ -672,8 +667,6 @@ cfg_if! { use libc::SO_KEEPALIVE as KEEPALIVE_OPTION; } else if #[cfg(unix)] { use libc::TCP_KEEPIDLE as KEEPALIVE_OPTION; - } else if #[cfg(target_os = "redox")] { - use libc::TCP_KEEPIDLE as KEEPALIVE_OPTION; } else { // ... } @@ -715,29 +708,6 @@ impl TcpStreamExt for TcpStream { self.keepalive_ms().map(|o| o.map(ms2dur)) } - #[cfg(target_os = "redox")] - fn set_keepalive_ms(&self, keepalive: Option) -> io::Result<()> { - try!(set_opt(self.as_sock(), SOL_SOCKET, SO_KEEPALIVE, - keepalive.is_some() as c_int)); - if let Some(dur) = keepalive { - try!(set_opt(self.as_sock(), v(IPPROTO_TCP), KEEPALIVE_OPTION, - (dur / 1000) as c_int)); - } - Ok(()) - } - - #[cfg(target_os = "redox")] - fn keepalive_ms(&self) -> io::Result> { - let keepalive = try!(get_opt::(self.as_sock(), SOL_SOCKET, - SO_KEEPALIVE)); - if keepalive == 0 { - return Ok(None) - } - let secs = try!(get_opt::(self.as_sock(), v(IPPROTO_TCP), - KEEPALIVE_OPTION)); - Ok(Some((secs as u32) * 1000)) - } - #[cfg(unix)] fn set_keepalive_ms(&self, keepalive: Option) -> io::Result<()> { try!(set_opt(self.as_sock(), SOL_SOCKET, SO_KEEPALIVE, @@ -893,7 +863,7 @@ impl TcpStreamExt for TcpStream { } } -#[cfg(any(target_os = "redox", unix, target_os = "wasi"))] +#[cfg(any(unix, target_os = "wasi"))] fn ms2timeout(dur: Option) -> timeval { // TODO: be more rigorous match dur { @@ -905,7 +875,7 @@ fn ms2timeout(dur: Option) -> timeval { } } -#[cfg(any(target_os = "redox", unix, target_os = "wasi"))] +#[cfg(any(unix, target_os = "wasi"))] fn timeout2ms(dur: timeval) -> Option { if dur.tv_sec == 0 && dur.tv_usec == 0 { None @@ -950,7 +920,7 @@ fn dur2linger(dur: Option) -> linger { } } -#[cfg(any(target_os = "redox", unix, target_os = "wasi"))] +#[cfg(any(unix, target_os = "wasi"))] fn dur2linger(dur: Option) -> linger { match dur { Some(d) => { @@ -1027,24 +997,18 @@ impl UdpSocketExt for UdpSocket { set_opt(self.as_sock(), IPPROTO_IP, IP_MULTICAST_TTL, multicast_ttl_v4 as c_int) } - + fn multicast_ttl_v4(&self) -> io::Result { get_opt::(self.as_sock(), IPPROTO_IP, IP_MULTICAST_TTL) .map(|b| b as u32) } fn set_multicast_hops_v6(&self, _hops: u32) -> io::Result<()> { - #[cfg(target_os = "redox")] - return Err(io::Error::new(io::ErrorKind::Other, "Not implemented yet")); - #[cfg(not(target_os = "redox"))] set_opt(self.as_sock(), v(IPPROTO_IPV6), IPV6_MULTICAST_HOPS, _hops as c_int) } fn multicast_hops_v6(&self) -> io::Result { - #[cfg(target_os = "redox")] - return Err(io::Error::new(io::ErrorKind::Other, "Not implemented yet")); - #[cfg(not(target_os = "redox"))] get_opt::(self.as_sock(), v(IPPROTO_IPV6), IPV6_MULTICAST_HOPS) .map(|b| b as u32) } @@ -1059,30 +1023,18 @@ impl UdpSocketExt for UdpSocket { } fn set_multicast_if_v4(&self, _interface: &Ipv4Addr) -> io::Result<()> { - #[cfg(target_os = "redox")] - return Err(io::Error::new(io::ErrorKind::Other, "Not implemented yet")); - #[cfg(not(target_os = "redox"))] set_opt(self.as_sock(), IPPROTO_IP, IP_MULTICAST_IF, ip2in_addr(_interface)) } fn multicast_if_v4(&self) -> io::Result { - #[cfg(target_os = "redox")] - return Err(io::Error::new(io::ErrorKind::Other, "Not implemented yet")); - #[cfg(not(target_os = "redox"))] get_opt(self.as_sock(), IPPROTO_IP, IP_MULTICAST_IF).map(in_addr2ip) } fn set_multicast_if_v6(&self, _interface: u32) -> io::Result<()> { - #[cfg(target_os = "redox")] - return Err(io::Error::new(io::ErrorKind::Other, "Not implemented yet")); - #[cfg(not(target_os = "redox"))] set_opt(self.as_sock(), v(IPPROTO_IPV6), IPV6_MULTICAST_IF, to_ipv6mr_interface(_interface)) } fn multicast_if_v6(&self) -> io::Result { - #[cfg(target_os = "redox")] - return Err(io::Error::new(io::ErrorKind::Other, "Not implemented yet")); - #[cfg(not(target_os = "redox"))] get_opt::(self.as_sock(), v(IPPROTO_IPV6), IPV6_MULTICAST_IF).map(|b| b as u32) } @@ -1096,16 +1048,10 @@ impl UdpSocketExt for UdpSocket { } fn set_unicast_hops_v6(&self, _ttl: u32) -> io::Result<()> { - #[cfg(target_os = "redox")] - return Err(io::Error::new(io::ErrorKind::Other, "Not implemented yet")); - #[cfg(not(target_os = "redox"))] set_opt(self.as_sock(), v(IPPROTO_IPV6), IPV6_UNICAST_HOPS, _ttl as c_int) } fn unicast_hops_v6(&self) -> io::Result { - #[cfg(target_os = "redox")] - return Err(io::Error::new(io::ErrorKind::Other, "Not implemented yet")); - #[cfg(not(target_os = "redox"))] get_opt::(self.as_sock(), IPPROTO_IP, IPV6_UNICAST_HOPS) .map(|b| b as u32) } @@ -1200,13 +1146,6 @@ impl UdpSocketExt for UdpSocket { do_connect(self.as_sock(), addr) } - #[cfg(target_os = "redox")] - fn send(&self, buf: &[u8]) -> io::Result { - unsafe { - ::cvt(write(self.as_sock() as c_int, buf.as_ptr() as *const _, buf.len())).map(|n| n as usize) - } - } - #[cfg(unix)] fn send(&self, buf: &[u8]) -> io::Result { unsafe { @@ -1240,14 +1179,6 @@ impl UdpSocketExt for UdpSocket { } } - #[cfg(target_os = "redox")] - fn recv(&self, buf: &mut [u8]) -> io::Result { - unsafe { - ::cvt(read(self.as_sock() as c_int, buf.as_mut_ptr() as *mut _, buf.len())) - .map(|n| n as usize) - } - } - #[cfg(unix)] fn recv(&self, buf: &mut [u8]) -> io::Result { unsafe { @@ -1302,21 +1233,6 @@ fn do_connect(sock: Socket, addr: A) -> io::Result<()> { return ret } -#[cfg(target_os = "redox")] -fn set_nonblocking(sock: Socket, nonblocking: bool) -> io::Result<()> { - let mut flags = ::cvt(unsafe { - fcntl(sock as c_int, F_GETFL) - })?; - if nonblocking { - flags |= O_NONBLOCK; - } else { - flags &= !O_NONBLOCK; - } - ::cvt(unsafe { - fcntl(sock as c_int, F_SETFL, flags) - }).and(Ok(())) -} - #[cfg(unix)] fn set_nonblocking(sock: Socket, nonblocking: bool) -> io::Result<()> { let mut nonblocking = nonblocking as c_ulong; @@ -1338,17 +1254,6 @@ fn set_nonblocking(sock: Socket, nonblocking: bool) -> io::Result<()> { }).map(|_| ()) } -#[cfg(target_os = "redox")] -fn ip2in_addr(ip: &Ipv4Addr) -> in_addr { - let oct = ip.octets(); - in_addr { - s_addr: ::hton(((oct[0] as u32) << 24) | - ((oct[1] as u32) << 16) | - ((oct[2] as u32) << 8) | - ((oct[3] as u32) << 0)), - } -} - #[cfg(any(unix, target_os = "wasi"))] fn ip2in_addr(ip: &Ipv4Addr) -> in_addr { let oct = ip.octets(); @@ -1377,7 +1282,7 @@ fn ip2in_addr(ip: &Ipv4Addr) -> in_addr { fn in_addr2ip(ip: &in_addr) -> Ipv4Addr { let h_addr = c::in_addr_to_u32(ip); - + let a: u8 = (h_addr >> 24) as u8; let b: u8 = (h_addr >> 16) as u8; let c: u8 = (h_addr >> 8) as u8; diff --git a/src/lib.rs b/src/lib.rs index fcbb7b4..a4b460e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,7 +46,7 @@ #![cfg_attr(target_os = "wasi", feature(wasi_ext))] -#[cfg(any(target_os = "redox", target_os = "wasi", unix))] extern crate libc; +#[cfg(any(target_os = "wasi", unix))] extern crate libc; #[cfg(windows)] extern crate winapi; @@ -64,7 +64,6 @@ mod socket; mod ext; mod utils; -#[cfg(target_os="redox")] #[path = "sys/redox/mod.rs"] mod sys; #[cfg(unix)] #[path = "sys/unix/mod.rs"] mod sys; #[cfg(windows)] #[path = "sys/windows/mod.rs"] mod sys; #[cfg(target_os = "wasi")] #[path = "sys/wasi/mod.rs"] mod sys; diff --git a/src/socket.rs b/src/socket.rs index d9e92e0..5b0f493 100644 --- a/src/socket.rs +++ b/src/socket.rs @@ -12,7 +12,7 @@ use std::fmt; use std::io; use std::mem; use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6}; -#[cfg(any(unix, target_os = "redox", target_os = "wasi"))] +#[cfg(any(unix, target_os = "wasi"))] use libc::c_int; #[cfg(windows)] use winapi::ctypes::c_int; diff --git a/src/sys/redox/impls.rs b/src/sys/redox/impls.rs deleted file mode 100644 index 388ec69..0000000 --- a/src/sys/redox/impls.rs +++ /dev/null @@ -1,43 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use std::os::unix::io::{FromRawFd, AsRawFd}; - -use {TcpBuilder, UdpBuilder, FromInner, AsInner}; -use socket::Socket; -use sys; - -impl FromRawFd for TcpBuilder { - unsafe fn from_raw_fd(fd: usize) -> TcpBuilder { - let sock = sys::Socket::from_inner(fd); - TcpBuilder::from_inner(Socket::from_inner(sock)) - } -} - -impl AsRawFd for TcpBuilder { - fn as_raw_fd(&self) -> usize { - // TODO: this unwrap() is very bad - self.as_inner().borrow().as_ref().unwrap().as_inner().raw() as usize - } -} - -impl FromRawFd for UdpBuilder { - unsafe fn from_raw_fd(fd: usize) -> UdpBuilder { - let sock = sys::Socket::from_inner(fd); - UdpBuilder::from_inner(Socket::from_inner(sock)) - } -} - -impl AsRawFd for UdpBuilder { - fn as_raw_fd(&self) -> usize { - // TODO: this unwrap() is very bad - self.as_inner().borrow().as_ref().unwrap().as_inner().raw() as usize - } -} diff --git a/src/sys/redox/mod.rs b/src/sys/redox/mod.rs deleted file mode 100644 index 9fd9b1f..0000000 --- a/src/sys/redox/mod.rs +++ /dev/null @@ -1,81 +0,0 @@ -// 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - - -use std::io; -use std::mem; -use std::net::{TcpListener, TcpStream, UdpSocket}; -use std::os::unix::io::FromRawFd; -use libc::{self, c_int}; - -mod impls; - -pub mod c { - pub use libc::*; - - pub fn sockaddr_in_u32(sa: &sockaddr_in) -> u32 { - ::ntoh((*sa).sin_addr.s_addr) - } - - pub fn in_addr_to_u32(addr: &in_addr) -> u32 { - ::ntoh(addr.s_addr) - } -} - -pub struct Socket { - fd: c_int, -} - -impl Socket { - pub fn new(family: c_int, ty: c_int) -> io::Result { - unsafe { - let fd = ::cvt(libc::socket(family, ty, 0))?; - let mut flags = ::cvt(libc::fcntl(fd, libc::F_GETFD))?; - flags |= libc::O_CLOEXEC; - ::cvt(libc::fcntl(fd, libc::F_SETFD, flags))?; - Ok(Socket { fd: fd }) - } - } - - pub fn raw(&self) -> c_int { self.fd } - - fn into_fd(self) -> c_int { - let fd = self.fd; - mem::forget(self); - fd - } - - pub fn into_tcp_listener(self) -> TcpListener { - unsafe { TcpListener::from_raw_fd(self.into_fd() as usize) } - } - - pub fn into_tcp_stream(self) -> TcpStream { - unsafe { TcpStream::from_raw_fd(self.into_fd() as usize) } - } - - pub fn into_udp_socket(self) -> UdpSocket { - unsafe { UdpSocket::from_raw_fd(self.into_fd() as usize) } - } -} - -impl ::FromInner for Socket { - type Inner = usize; - fn from_inner(fd: usize) -> Socket { - Socket { fd: fd as c_int } - } -} - -impl Drop for Socket { - fn drop(&mut self) { - unsafe { - let _ = libc::close(self.fd); - } - } -}