Skip to content

Fix Android and NetBSD #171

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

Merged
merged 3 commits into from
Dec 26, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 39 additions & 11 deletions src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
// except according to those terms.

use std::cmp::min;
#[cfg(all(feature = "all", any(target_os = "fuchsia", target_os = "linux")))]
#[cfg(all(
feature = "all",
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
))]
use std::ffi::{CStr, CString};
#[cfg(not(target_os = "redox"))]
use std::io::IoSlice;
Expand Down Expand Up @@ -89,6 +92,7 @@ pub(crate) use libc::{
#[cfg(all(
feature = "all",
any(
target_os = "android",
target_os = "freebsd",
target_os = "fuchsia",
target_os = "linux",
Expand Down Expand Up @@ -162,7 +166,10 @@ impl Domain {
/// # Notes
///
/// This function is only available on Fuchsia and Linux.
#[cfg(all(feature = "all", any(target_os = "fuchsia", target_os = "linux")))]
#[cfg(all(
feature = "all",
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
))]
pub const PACKET: Domain = Domain(libc::AF_PACKET);
}

Expand All @@ -171,7 +178,7 @@ impl_debug!(
libc::AF_INET,
libc::AF_INET6,
libc::AF_UNIX,
#[cfg(any(target_os = "fuchsia", target_os = "linux"))]
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
libc::AF_PACKET,
libc::AF_UNSPEC, // = 0.
);
Expand Down Expand Up @@ -887,7 +894,10 @@ impl crate::Socket {
///
/// This function is only available on Fuchsia and Linux. On Linux it
/// requires the `CAP_NET_ADMIN` capability.
#[cfg(all(feature = "all", any(target_os = "fuchsia", target_os = "linux")))]
#[cfg(all(
feature = "all",
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
))]
pub fn mark(&self) -> io::Result<u32> {
unsafe {
getsockopt::<c_int>(self.inner, libc::SOL_SOCKET, libc::SO_MARK).map(|mark| mark as u32)
Expand All @@ -902,7 +912,10 @@ impl crate::Socket {
///
/// This function is only available on Fuchsia and Linux. On Linux it
/// requires the `CAP_NET_ADMIN` capability.
#[cfg(all(feature = "all", any(target_os = "fuchsia", target_os = "linux")))]
#[cfg(all(
feature = "all",
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
))]
pub fn set_mark(&self, mark: u32) -> io::Result<()> {
unsafe { setsockopt::<c_int>(self.inner, libc::SOL_SOCKET, libc::SO_MARK, mark as c_int) }
}
Expand All @@ -912,7 +925,10 @@ impl crate::Socket {
/// This value gets the socket binded device's interface name.
///
/// This function is only available on Fuchsia and Linux.
#[cfg(all(feature = "all", any(target_os = "fuchsia", target_os = "linux")))]
#[cfg(all(
feature = "all",
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
))]
pub fn device(&self) -> io::Result<Option<CString>> {
// TODO: replace with `MaybeUninit::uninit_array` once stable.
let mut buf: [MaybeUninit<u8>; libc::IFNAMSIZ] =
Expand Down Expand Up @@ -957,7 +973,10 @@ impl crate::Socket {
/// If `interface` is `None` or an empty string it removes the binding.
///
/// This function is only available on Fuchsia and Linux.
#[cfg(all(feature = "all", any(target_os = "fuchsia", target_os = "linux")))]
#[cfg(all(
feature = "all",
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
))]
pub fn bind_device(&self, interface: Option<&CStr>) -> io::Result<()> {
let (value, len) = if let Some(interface) = interface {
(interface.as_ptr(), interface.to_bytes_with_nul().len())
Expand Down Expand Up @@ -1021,7 +1040,10 @@ impl crate::Socket {
/// This function is only available on Fuchsia and Linux.
///
/// [`set_freebind`]: Socket::set_freebind
#[cfg(all(feature = "all", any(target_os = "fuchsia", target_os = "linux")))]
#[cfg(all(
feature = "all",
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
))]
pub fn freebind(&self) -> io::Result<bool> {
unsafe {
getsockopt::<c_int>(self.inner, libc::SOL_SOCKET, libc::IP_FREEBIND)
Expand All @@ -1038,7 +1060,10 @@ impl crate::Socket {
/// to bind to it.
///
/// This function is only available on Fuchsia and Linux.
#[cfg(all(feature = "all", any(target_os = "fuchsia", target_os = "linux")))]
#[cfg(all(
feature = "all",
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
))]
pub fn set_freebind(&self, reuse: bool) -> io::Result<()> {
unsafe {
setsockopt(
Expand Down Expand Up @@ -1088,12 +1113,15 @@ from!(crate::Socket, UnixDatagram);
fn in_addr_convertion() {
let ip = Ipv4Addr::new(127, 0, 0, 1);
let raw = to_in_addr(&ip);
assert_eq!(raw.s_addr, 127 << 0 | 1 << 24);
// NOTE: `in_addr` is packed on NetBSD and it's unsafe to borrow.
let a = raw.s_addr;
assert_eq!(a, 127 << 0 | 1 << 24);
assert_eq!(from_in_addr(raw), ip);

let ip = Ipv4Addr::new(127, 34, 4, 12);
let raw = to_in_addr(&ip);
assert_eq!(raw.s_addr, 127 << 0 | 34 << 8 | 4 << 16 | 12 << 24);
let a = raw.s_addr;
assert_eq!(a, 127 << 0 | 34 << 8 | 4 << 16 | 12 << 24);
assert_eq!(from_in_addr(raw), ip);
}

Expand Down