Skip to content
This repository has been archived by the owner on Nov 24, 2023. It is now read-only.

Commit

Permalink
Added support for vita
Browse files Browse the repository at this point in the history
  • Loading branch information
nikarh committed Oct 15, 2023
1 parent 6a21bba commit 64a94ee
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 45 deletions.
23 changes: 9 additions & 14 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ jobs:
strategy:
fail-fast: false
matrix:
# TODO: add the following targets, currently broken.
# "x86_64-unknown-redox"
target: ["aarch64-apple-ios", "aarch64-linux-android", "x86_64-apple-darwin", "x86_64-unknown-fuchsia", "x86_64-pc-windows-msvc", "x86_64-pc-solaris", "x86_64-unknown-illumos", "x86_64-unknown-linux-gnu", "x86_64-unknown-netbsd"]
target: ["aarch64-apple-ios", "aarch64-linux-android", "x86_64-apple-darwin", "x86_64-unknown-fuchsia", "x86_64-pc-windows-msvc", "x86_64-pc-solaris", "x86_64-unknown-illumos", "x86_64-unknown-linux-gnu", "x86_64-unknown-netbsd", "x86_64-unknown-redox"]
steps:
- uses: actions/checkout@master
- name: Install Rust
Expand All @@ -72,21 +70,18 @@ jobs:
run: rustup target add ${{ matrix.target }}
- name: Run check
run: cargo hack check --feature-powerset --all-targets --examples --bins --tests --target ${{ matrix.target }}

# Redox needs a nightly compiler for libc:
# https://github.com/rust-lang/libc/issues/2012
Check_Redox:
CheckTier3:
name: Check
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
target: ["x86_64-unknown-redox"]
target: ["armv7-sony-vita-newlibeabihf"]
steps:
- uses: actions/checkout@master
- name: Install Rust nightly
run: rustup update nightly && rustup default nightly
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@nightly
with:
components: "rust-src"
- uses: taiki-e/install-action@cargo-hack
- name: Install Target
run: rustup target add ${{ matrix.target }}
- name: Run check
run: cargo hack check --feature-powerset --all-targets --examples --bins --tests --target ${{ matrix.target }}
run: cargo hack check -Z build-std=std,panic_abort --feature-powerset --all-targets --examples --bins --tests --target ${{ matrix.target }}
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ rustdoc-args = ["--cfg", "docsrs"]
features = ["all"]

[target."cfg(unix)".dependencies]
libc = "0.2.139"
libc = "0.2.149"

[target."cfg(windows)".dependencies]
winapi = { version = "0.3.9", features = ["handleapi", "ws2ipdef", "ws2tcpip"] }
Expand Down
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,15 @@ impl<'a> DerefMut for MaybeUninitSlice<'a> {
/// See [`Socket::set_tcp_keepalive`].
#[derive(Debug, Clone)]
pub struct TcpKeepalive {
#[cfg_attr(target_os = "openbsd", allow(dead_code))]
#[cfg_attr(any(target_os = "openbsd", target_os = "vita"), allow(dead_code))]
time: Option<Duration>,
#[cfg(not(any(
target_os = "openbsd",
target_os = "redox",
target_os = "solaris",
target_os = "nto",
target_os = "espidf",
target_os = "vita",
)))]
interval: Option<Duration>,
#[cfg(not(any(
Expand All @@ -348,6 +349,7 @@ pub struct TcpKeepalive {
target_os = "windows",
target_os = "nto",
target_os = "espidf",
target_os = "vita",
)))]
retries: Option<u32>,
}
Expand All @@ -363,6 +365,7 @@ impl TcpKeepalive {
target_os = "solaris",
target_os = "nto",
target_os = "espidf",
target_os = "vita",
)))]
interval: None,
#[cfg(not(any(
Expand All @@ -372,6 +375,7 @@ impl TcpKeepalive {
target_os = "windows",
target_os = "nto",
target_os = "espidf",
target_os = "vita",
)))]
retries: None,
}
Expand Down
6 changes: 6 additions & 0 deletions src/sockaddr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,11 @@ impl From<SocketAddrV4> for SockAddr {
target_os = "openbsd",
target_os = "nto",
target_os = "espidf",
target_os = "vita",
))]
sin_len: 0,
#[cfg(target_os = "vita")]
sin_vport: addr.port().to_be(),
};
let mut storage = MaybeUninit::<sockaddr_storage>::zeroed();
// Safety: A `sockaddr_in` is memory compatible with a `sockaddr_storage`
Expand Down Expand Up @@ -278,8 +281,11 @@ impl From<SocketAddrV6> for SockAddr {
target_os = "openbsd",
target_os = "nto",
target_os = "espidf",
target_os = "vita",
))]
sin6_len: 0,
#[cfg(target_os = "vita")]
sin6_vport: addr.port().to_be(),
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
__sin6_src_id: 0,
};
Expand Down
21 changes: 19 additions & 2 deletions src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,7 @@ fn set_common_flags(socket: Socket) -> io::Result<Socket> {
target_os = "netbsd",
target_os = "openbsd",
target_os = "espidf",
target_os = "vita",
))
))]
socket._set_cloexec(true)?;
Expand Down Expand Up @@ -1174,6 +1175,7 @@ impl Socket {
target_os = "solaris",
target_os = "nto",
target_os = "espidf",
target_os = "vita",
)))]
pub fn join_multicast_v4_n(
&self,
Expand Down Expand Up @@ -1205,6 +1207,7 @@ impl Socket {
target_os = "solaris",
target_os = "nto",
target_os = "espidf",
target_os = "vita",
)))]
pub fn leave_multicast_v4_n(
&self,
Expand Down Expand Up @@ -1238,6 +1241,7 @@ impl Socket {
target_os = "fuchsia",
target_os = "nto",
target_os = "espidf",
target_os = "vita",
)))]
pub fn join_ssm_v4(
&self,
Expand Down Expand Up @@ -1274,6 +1278,7 @@ impl Socket {
target_os = "fuchsia",
target_os = "nto",
target_os = "espidf",
target_os = "vita",
)))]
pub fn leave_ssm_v4(
&self,
Expand Down Expand Up @@ -1451,6 +1456,7 @@ impl Socket {
target_os = "windows",
target_os = "nto",
target_os = "espidf",
target_os = "vita",
)))]
pub fn set_recv_tos(&self, recv_tos: bool) -> io::Result<()> {
let recv_tos = if recv_tos { 1 } else { 0 };
Expand Down Expand Up @@ -1481,6 +1487,7 @@ impl Socket {
target_os = "windows",
target_os = "nto",
target_os = "espidf",
target_os = "vita",
)))]
pub fn recv_tos(&self) -> io::Result<bool> {
unsafe {
Expand Down Expand Up @@ -1697,14 +1704,24 @@ impl Socket {
doc,
all(
feature = "all",
not(any(windows, target_os = "haiku", target_os = "openbsd"))
not(any(
windows,
target_os = "haiku",
target_os = "openbsd",
target_os = "vita"
))
)
))]
#[cfg_attr(
docsrs,
doc(cfg(all(
feature = "all",
not(any(windows, target_os = "haiku", target_os = "openbsd"))
not(any(
windows,
target_os = "haiku",
target_os = "openbsd",
target_os = "vita"
))
)))
)]
pub fn keepalive_time(&self) -> io::Result<Duration> {
Expand Down
41 changes: 36 additions & 5 deletions src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ pub(crate) use libc::IP_HDRINCL;
target_os = "haiku",
target_os = "nto",
target_os = "espidf",
target_os = "vita",
)))]
pub(crate) use libc::IP_RECVTOS;
#[cfg(not(any(
Expand Down Expand Up @@ -121,6 +122,7 @@ pub(crate) use libc::{
target_os = "fuchsia",
target_os = "nto",
target_os = "espidf",
target_os = "vita",
)))]
pub(crate) use libc::{
ip_mreq_source as IpMreqSource, IP_ADD_SOURCE_MEMBERSHIP, IP_DROP_SOURCE_MEMBERSHIP,
Expand Down Expand Up @@ -175,6 +177,7 @@ use libc::TCP_KEEPALIVE as KEEPALIVE_TIME;
target_os = "haiku",
target_os = "openbsd",
target_os = "nto",
target_os = "vita",
)))]
use libc::TCP_KEEPIDLE as KEEPALIVE_TIME;

Expand Down Expand Up @@ -237,6 +240,7 @@ type IovLen = usize;
target_os = "nto",
target_vendor = "apple",
target_os = "espidf",
target_os = "vita",
))]
type IovLen = c_int;

Expand Down Expand Up @@ -713,6 +717,7 @@ pub(crate) fn try_clone(fd: Socket) -> io::Result<Socket> {
syscall!(fcntl(fd, libc::F_DUPFD_CLOEXEC, 0))
}

#[cfg(not(target_os = "vita"))]
pub(crate) fn set_nonblocking(fd: Socket, nonblocking: bool) -> io::Result<()> {
if nonblocking {
fcntl_add(fd, libc::F_GETFL, libc::F_SETFL, libc::O_NONBLOCK)
Expand All @@ -721,6 +726,18 @@ pub(crate) fn set_nonblocking(fd: Socket, nonblocking: bool) -> io::Result<()> {
}
}

#[cfg(target_os = "vita")]
pub(crate) fn set_nonblocking(fd: Socket, nonblocking: bool) -> io::Result<()> {
unsafe {
setsockopt(
fd,
libc::SOL_SOCKET,
libc::SO_NONBLOCK,
nonblocking as libc::c_int,
)
}
}

pub(crate) fn shutdown(fd: Socket, how: Shutdown) -> io::Result<()> {
let how = match how {
Shutdown::Write => libc::SHUT_WR,
Expand Down Expand Up @@ -923,7 +940,7 @@ fn into_timeval(duration: Option<Duration>) -> libc::timeval {
}

#[cfg(feature = "all")]
#[cfg(not(any(target_os = "haiku", target_os = "openbsd")))]
#[cfg(not(any(target_os = "haiku", target_os = "openbsd", target_os = "vita")))]
pub(crate) fn keepalive_time(fd: Socket) -> io::Result<Duration> {
unsafe {
getsockopt::<c_int>(fd, IPPROTO_TCP, KEEPALIVE_TIME)
Expand All @@ -933,7 +950,12 @@ pub(crate) fn keepalive_time(fd: Socket) -> io::Result<Duration> {

#[allow(unused_variables)]
pub(crate) fn set_tcp_keepalive(fd: Socket, keepalive: &TcpKeepalive) -> io::Result<()> {
#[cfg(not(any(target_os = "haiku", target_os = "openbsd", target_os = "nto")))]
#[cfg(not(any(
target_os = "haiku",
target_os = "openbsd",
target_os = "nto",
target_os = "vita"
)))]
if let Some(time) = keepalive.time {
let secs = into_secs(time);
unsafe { setsockopt(fd, libc::IPPROTO_TCP, KEEPALIVE_TIME, secs)? }
Expand Down Expand Up @@ -969,12 +991,18 @@ pub(crate) fn set_tcp_keepalive(fd: Socket, keepalive: &TcpKeepalive) -> io::Res
Ok(())
}

#[cfg(not(any(target_os = "haiku", target_os = "openbsd", target_os = "nto")))]
#[cfg(not(any(
target_os = "haiku",
target_os = "openbsd",
target_os = "nto",
target_os = "vita"
)))]
fn into_secs(duration: Duration) -> c_int {
min(duration.as_secs(), c_int::max_value() as u64) as c_int
}

/// Add `flag` to the current set flags of `F_GETFD`.
#[cfg(not(target_os = "vita"))]
fn fcntl_add(fd: Socket, get_cmd: c_int, set_cmd: c_int, flag: c_int) -> io::Result<()> {
let previous = syscall!(fcntl(fd, get_cmd))?;
let new = previous | flag;
Expand All @@ -987,6 +1015,7 @@ fn fcntl_add(fd: Socket, get_cmd: c_int, set_cmd: c_int, flag: c_int) -> io::Res
}

/// Remove `flag` to the current set flags of `F_GETFD`.
#[cfg(not(target_os = "vita"))]
fn fcntl_remove(fd: Socket, get_cmd: c_int, set_cmd: c_int, flag: c_int) -> io::Result<()> {
let previous = syscall!(fcntl(fd, get_cmd))?;
let new = previous & !flag;
Expand Down Expand Up @@ -1066,6 +1095,7 @@ pub(crate) fn from_in6_addr(addr: in6_addr) -> Ipv6Addr {
target_os = "solaris",
target_os = "nto",
target_os = "espidf",
target_os = "vita",
)))]
pub(crate) fn to_mreqn(
multiaddr: &Ipv4Addr,
Expand Down Expand Up @@ -1152,12 +1182,13 @@ impl crate::Socket {
/// # Notes
///
/// On supported platforms you can use [`Type::cloexec`].
#[cfg(feature = "all")]
#[cfg_attr(docsrs, doc(cfg(all(feature = "all", unix))))]
#[cfg(all(feature = "all", not(target_os = "vita")))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "all", unix, not(target_os = "vita")))))]
pub fn set_cloexec(&self, close_on_exec: bool) -> io::Result<()> {
self._set_cloexec(close_on_exec)
}

#[cfg(not(target_os = "vita"))]
pub(crate) fn _set_cloexec(&self, close_on_exec: bool) -> io::Result<()> {
if close_on_exec {
fcntl_add(
Expand Down
Loading

0 comments on commit 64a94ee

Please sign in to comment.