Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more Socket options #183

Merged
merged 7 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jobs:
name: Test
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
build: [stable, beta, nightly, macos, windows]
include:
Expand Down Expand Up @@ -55,6 +56,7 @@ jobs:
name: Check
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
# TODO: add the following targets, currently broken.
# "x86_64-unknown-redox"
Expand Down
74 changes: 71 additions & 3 deletions src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use libc::{c_void, in6_addr, in_addr};

#[cfg(not(target_os = "redox"))]
use crate::RecvFlags;
use crate::{Domain, SockAddr, TcpKeepalive, Type};
use crate::{Domain, Protocol, SockAddr, TcpKeepalive, Type};

pub(crate) use libc::c_int;

Expand Down Expand Up @@ -265,7 +265,7 @@ impl Type {
}

impl_debug!(
crate::Type,
Type,
libc::SOCK_STREAM,
libc::SOCK_DGRAM,
#[cfg(not(target_os = "redox"))]
Expand Down Expand Up @@ -298,7 +298,7 @@ impl_debug!(
);

impl_debug!(
crate::Protocol,
Protocol,
libc::IPPROTO_ICMP,
libc::IPPROTO_ICMPV6,
libc::IPPROTO_TCP,
Expand Down Expand Up @@ -974,6 +974,74 @@ impl crate::Socket {
}
}

/// Returns `true` if `listen(2)` was called on this socket by checking the
/// `SO_ACCEPTCONN` option on this socket.
#[cfg(all(
feature = "all",
any(
target_os = "android",
target_os = "freebsd",
target_os = "fuchsia",
target_os = "linux",
)
))]
pub fn is_listener(&self) -> io::Result<bool> {
unsafe {
getsockopt::<c_int>(self.inner, libc::SOL_SOCKET, libc::SO_ACCEPTCONN).map(|v| v != 0)
}
}

/// Returns the [`Domain`] of this socket by checking the `SO_DOMAIN` option
/// on this socket.
#[cfg(all(
feature = "all",
any(
target_os = "android",
// TODO: add FreeBSD.
// target_os = "freebsd",
target_os = "fuchsia",
target_os = "linux",
)
))]
pub fn domain(&self) -> io::Result<Domain> {
unsafe { getsockopt::<c_int>(self.inner, libc::SOL_SOCKET, libc::SO_DOMAIN).map(Domain) }
}

/// Returns the [`Protocol`] of this socket by checking the `SO_PROTOCOL`
/// option on this socket.
#[cfg(all(
feature = "all",
any(
target_os = "android",
target_os = "freebsd",
target_os = "fuchsia",
target_os = "linux",
)
))]
pub fn protocol(&self) -> io::Result<Option<Protocol>> {
unsafe {
getsockopt::<c_int>(self.inner, libc::SOL_SOCKET, libc::SO_PROTOCOL).map(|v| match v {
0 => None,
p => Some(Protocol(p)),
})
}
}

/// Returns the [`Type`] of this socket by checking the `SO_TYPE` option on
/// this socket.
#[cfg(all(
feature = "all",
any(
target_os = "android",
target_os = "freebsd",
target_os = "fuchsia",
target_os = "linux",
)
))]
pub fn r#type(&self) -> io::Result<Type> {
unsafe { getsockopt::<c_int>(self.inner, libc::SOL_SOCKET, libc::SO_TYPE).map(Type) }
}

/// Gets the value for the `SO_MARK` option on this socket.
///
/// This value gets the socket mark field for each packet sent through
Expand Down
90 changes: 90 additions & 0 deletions tests/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,96 @@ fn spare_capacity_mut(buf: &mut Vec<u8>) -> &mut [MaybeUninit<u8>] {
}
}

#[cfg(all(
feature = "all",
any(
target_os = "android",
target_os = "freebsd",
target_os = "fuchsia",
target_os = "linux",
)
))]
#[test]
fn is_listener() {
let socket = Socket::new(Domain::IPV4, Type::STREAM, Some(Protocol::TCP)).unwrap();
assert_eq!(socket.is_listener().unwrap(), false);

socket.listen(1).unwrap();
assert_eq!(socket.is_listener().unwrap(), true);
}

#[cfg(all(
feature = "all",
any(
target_os = "android",
// TODO: add FreeBSD.
// target_os = "freebsd",
target_os = "fuchsia",
target_os = "linux",
)
))]
#[test]
fn domain() {
let socket = Socket::new(Domain::IPV4, Type::STREAM, None).unwrap();
assert_eq!(socket.domain().unwrap(), Domain::IPV4);

let socket = Socket::new(Domain::IPV6, Type::STREAM, None).unwrap();
assert_eq!(socket.domain().unwrap(), Domain::IPV6);

let socket = Socket::new(Domain::UNIX, Type::STREAM, None).unwrap();
assert_eq!(socket.domain().unwrap(), Domain::UNIX);
}

#[cfg(all(
feature = "all",
any(
target_os = "android",
target_os = "freebsd",
target_os = "fuchsia",
target_os = "linux",
)
))]
#[test]
fn protocol() {
let socket = Socket::new(Domain::UNIX, Type::STREAM, None).unwrap();
assert_eq!(socket.protocol().unwrap(), None);

/* Don't have permission for this on CI.
let socket = Socket::new(Domain::IPV4, Type::DGRAM, Some(Protocol::ICMPV4)).unwrap();
assert_eq!(socket.protocol().unwrap(), Some(Protocol::ICMPV4));

let socket = Socket::new(Domain::IPV6, Type::DGRAM, Some(Protocol::ICMPV6)).unwrap();
assert_eq!(socket.protocol().unwrap(), Some(Protocol::ICMPV6));
*/

let socket = Socket::new(Domain::IPV4, Type::STREAM, Some(Protocol::TCP)).unwrap();
assert_eq!(socket.protocol().unwrap(), Some(Protocol::TCP));

let socket = Socket::new(Domain::IPV6, Type::DGRAM, Some(Protocol::UDP)).unwrap();
assert_eq!(socket.protocol().unwrap(), Some(Protocol::UDP));
}

#[cfg(all(
feature = "all",
any(
target_os = "android",
target_os = "freebsd",
target_os = "fuchsia",
target_os = "linux",
)
))]
#[test]
fn r#type() {
let socket = Socket::new(Domain::IPV4, Type::STREAM, None).unwrap();
assert_eq!(socket.r#type().unwrap(), Type::STREAM);

let socket = Socket::new(Domain::IPV6, Type::DGRAM, None).unwrap();
assert_eq!(socket.r#type().unwrap(), Type::DGRAM);

let socket = Socket::new(Domain::UNIX, Type::SEQPACKET, None).unwrap();
assert_eq!(socket.r#type().unwrap(), Type::SEQPACKET);
}

fn any_ipv4() -> SockAddr {
SocketAddrV4::new(Ipv4Addr::LOCALHOST, 0).into()
}
Expand Down