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

Remove FFI types from the public API #186

Merged
merged 2 commits into from
Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 6 additions & 23 deletions src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
// except according to those terms.

use std::cmp::min;
#[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;
use std::marker::PhantomData;
Expand Down Expand Up @@ -942,10 +937,10 @@ impl crate::Socket {
feature = "all",
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
))]
pub fn device(&self) -> io::Result<Option<CString>> {
pub fn device(&self) -> io::Result<Option<Vec<u8>>> {
// TODO: replace with `MaybeUninit::uninit_array` once stable.
let mut buf: [MaybeUninit<u8>; libc::IFNAMSIZ] =
unsafe { MaybeUninit::<[MaybeUninit<u8>; libc::IFNAMSIZ]>::uninit().assume_init() };
unsafe { MaybeUninit::uninit().assume_init() };
let mut len = buf.len() as libc::socklen_t;
unsafe {
syscall!(getsockopt(
Expand All @@ -959,21 +954,9 @@ impl crate::Socket {
if len == 0 {
Ok(None)
} else {
// Allocate a buffer for `CString` with the length including the
// null terminator.
let len = len as usize;
let mut name = Vec::with_capacity(len);

let buf = &buf[..len as usize - 1];
// TODO: use `MaybeUninit::slice_assume_init_ref` once stable.
// Safety: `len` bytes are writen by the OS, this includes a null
// terminator. However we don't copy the null terminator because
// `CString::from_vec_unchecked` adds its own null terminator.
let buf = unsafe { slice::from_raw_parts(buf.as_ptr().cast(), len - 1) };
name.extend_from_slice(buf);

// Safety: the OS initialised the string for us, which shouldn't
// include any null bytes.
Ok(Some(unsafe { CString::from_vec_unchecked(name) }))
Ok(Some(unsafe { &*(buf as *const [_] as *const [u8]) }.into()))
}
}

Expand All @@ -990,9 +973,9 @@ impl crate::Socket {
feature = "all",
any(target_os = "android", target_os = "fuchsia", target_os = "linux")
))]
pub fn bind_device(&self, interface: Option<&CStr>) -> io::Result<()> {
pub fn bind_device(&self, interface: Option<&[u8]>) -> io::Result<()> {
let (value, len) = if let Some(interface) = interface {
(interface.as_ptr(), interface.to_bytes_with_nul().len())
(interface.as_ptr(), interface.len())
} else {
(ptr::null(), 0)
};
Expand Down
22 changes: 15 additions & 7 deletions tests/socket.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#[cfg(all(feature = "all", any(target_os = "fuchsia", target_os = "linux")))]
use std::ffi::CStr;
#[cfg(any(windows, target_vendor = "apple"))]
use std::io;
#[cfg(not(target_os = "redox"))]
Expand Down Expand Up @@ -631,30 +629,40 @@ fn tcp_keepalive() {

#[cfg(all(feature = "all", any(target_os = "fuchsia", target_os = "linux")))]
#[test]
#[ignore = "setting `SO_BINDTODEVICE` requires the `CAP_NET_RAW` capability (works when running as root)"]
fn device() {
// Some common network interface on Linux.
const INTERFACES: &[&str] = &["lo\0", "lo0\0", "eth0\0", "wlan0\0"];
const INTERFACES: &[&str] = &["lo", "lo0", "eth0", "wlan0"];

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

for interface in INTERFACES.iter() {
let interface = CStr::from_bytes_with_nul(interface.as_bytes()).unwrap();
if let Err(err) = socket.bind_device(Some(interface)) {
if let Err(err) = socket.bind_device(Some(interface.as_bytes())) {
// Network interface is not available try another.
if matches!(err.raw_os_error(), Some(libc::ENODEV) | Some(libc::EPERM)) {
Thomasdezeeuw marked this conversation as resolved.
Show resolved Hide resolved
eprintln!("error binding to device (`{}`): {}", interface, err);
continue;
} else {
panic!("unexpected error binding device: {}", err);
}
}
assert_eq!(socket.device().unwrap().as_deref(), Some(interface));
assert_eq!(
socket.device().unwrap().as_deref(),
Some(interface.as_bytes())
);

socket.bind_device(None).unwrap();
assert_eq!(socket.device().unwrap(), None);
// Just need to do it with one interface.
break;
return;
}

panic!(
"failing to bind to any device. \
Note that on Linux this requires the `CAP_NET_RAW` permission. \
This can be given using `sudo setcap cap_net_raw=ep $test_bin`."
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this mention whatever flag is necessary to run this test case given the ignore attribute?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed it to just say "failed to bind to any device", the other info is already in the ignore attribute.

With flag do you mean --ignore passed to cargo test, or do mean another flag? If the first than I don't it's needed.

);
}

fn any_ipv4() -> SockAddr {
Expand Down