Skip to content

Commit

Permalink
fix(macos): fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
leshow committed Apr 26, 2023
1 parent f4c0742 commit 8307b7e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 27 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,29 @@ jobs:
command: test
args: --all-features

test-mac:
name: Test Suite (macOS)
runs-on: macos-latest
strategy:
matrix:
rust:
- stable
steps:
- name: Checkout sources
uses: actions/checkout@v2

- name: Install toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust }}
override: true

- name: Run cargo test
uses: actions-rs/cargo@v1
with:
command: test
args: --all-features

fmt:
name: Rustfmt
runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions src/cmsg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ impl<'a> Encoder<'a> {
/// # Panics
/// - If insufficient buffer space remains.
/// - If `T` has stricter alignment requirements than `cmsghdr`
#[allow(clippy::unnecessary_cast)]
pub fn push<T: Copy + ?Sized>(&mut self, level: libc::c_int, ty: libc::c_int, value: T) {
assert!(mem::align_of::<T>() <= mem::align_of::<libc::cmsghdr>());
let space = unsafe { libc::CMSG_SPACE(mem::size_of_val(&value) as _) as usize };
Expand Down Expand Up @@ -75,6 +76,7 @@ impl<'a> Drop for Encoder<'a> {
/// # Safety
///
/// `cmsg` must refer to a cmsg containing a payload of type `T`
#[allow(clippy::unnecessary_cast)]
pub unsafe fn decode<T: Copy>(cmsg: &libc::cmsghdr) -> T {
assert!(mem::align_of::<T>() <= mem::align_of::<libc::cmsghdr>());
debug_assert_eq!(
Expand Down
37 changes: 14 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ fn log_sendmsg_error<B: AsPtr<u8>>(

#[cfg(test)]
mod tests {
use std::net::Ipv4Addr;

use super::*;

#[test]
Expand All @@ -137,38 +139,27 @@ mod tests {
}
#[test]
fn test_send_recv_msg() {
let saddr = "0.0.0.0:9901".parse().unwrap();
let saddr = "0.0.0.0:9902".parse().unwrap();
let a = sync::UdpSocket::bind(saddr).unwrap();
let b = sync::UdpSocket::bind("0.0.0.0:0").unwrap();
let send_port = b.local_addr().unwrap().port();
let send_addr = b.local_addr().unwrap().ip();
let buf = b"hello world";
// let src = Source::Interface(1);
let tr = Transmit::new(saddr, *buf);
let src = Source::Interface(1);
let tr = Transmit::new(saddr, *buf).src_ip(src);
b.send_msg(&UdpState::new(), tr).unwrap();
// recv
let mut r = [0; 1024];
let meta = a.recv_msg(&mut r).unwrap();
assert_eq!(buf[..], r[..11]);
// dst addr and b addr matches!
assert_eq!(meta.dst_local_ip, Some(send_addr));
// meta.ifindex
assert_eq!(send_port, meta.addr.port());
assert_eq!(meta.ifindex, 1);
assert!(matches!(
meta.dst_local_ip,
// dst_local_ip might be 127.0.0.1
Some(addr) if addr == send_addr || addr == IpAddr::V4(Ipv4Addr::LOCALHOST)
));
}
// #[test]
// fn test_send_recv_msg_if() {
// let addrs: Vec<_> = nix::ifaddrs::getifaddrs().unwrap().collect();
// dbg!(addrs);
// let saddr = "0.0.0.0:9901".parse().unwrap();
// let a = sync::UdpSocket::bind(saddr).unwrap();
// let b = sync::UdpSocket::bind("0.0.0.0:0").unwrap();
// let send_addr = b.local_addr().unwrap().ip();
// let buf = b"hello world";
// // let src = Source::Interface(1);
// let tr = Transmit::new(saddr, *buf);
// b.send_msg(&UdpState::new(), tr).unwrap();
// // recv
// let mut r = [0; 1024];
// let meta = a.recv_msg(&mut r).unwrap();
// assert_eq!(buf[..], r[..11]);
// // dst addr and b addr matches!
// assert_eq!(meta.dst_local_ip, Some(send_addr));
// }
}
12 changes: 8 additions & 4 deletions src/unix.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#![allow(clippy::unnecessary_cast)]
use std::{
io,
io::IoSliceMut,
mem::{self, MaybeUninit},
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6},
os::unix::io::AsRawFd,
ptr,
sync::atomic::AtomicUsize,
task::{Context, Poll},
time::Instant,
Expand Down Expand Up @@ -587,13 +587,13 @@ fn init(io: SockRef<'_>) -> io::Result<()> {
)?;
}
}
#[cfg(any(target_os = "macos"))]
#[cfg(target_os = "macos")]
{
if is_ipv4 {
set_socket_option(&*io, libc::IPPROTO_IP, libc::IP_PKTINFO, OPTION_ON)?;
}
}
#[cfg(any(target_os = "freebsd"))]
#[cfg(target_os = "freebsd")]
// IP_RECVDSTADDR == IP_SENDSRCADDR on FreeBSD
// macOS uses only IP_RECVDSTADDR, no IP_SENDSRCADDR on macOS
// macOS also supports IP_PKTINFO
Expand Down Expand Up @@ -679,6 +679,8 @@ fn send<B: AsPtr<u8>>(
last_send_error: &mut Instant,
transmits: &[Transmit<B>],
) -> io::Result<usize> {
use std::ptr;

let mut msgs: [libc::mmsghdr; BATCH_SIZE] = unsafe { mem::zeroed() };
let mut iovecs: [libc::iovec; BATCH_SIZE] = unsafe { mem::zeroed() };
let mut cmsgs = [cmsg::Aligned([0u8; CMSG_LEN]); BATCH_SIZE];
Expand All @@ -692,7 +694,7 @@ fn send<B: AsPtr<u8>>(
unsafe { MaybeUninit::uninit().assume_init() };
for (i, transmit) in transmits.iter().enumerate().take(BATCH_SIZE) {
let dst_addr = unsafe {
std::ptr::write(addrs[i].as_mut_ptr(), socket2::SockAddr::from(transmit.dst));
ptr::write(addrs[i].as_mut_ptr(), socket2::SockAddr::from(transmit.dst));
&*addrs[i].as_ptr()
};
prepare_msg(
Expand Down Expand Up @@ -834,6 +836,8 @@ fn send<B: AsPtr<u8>>(

#[cfg(not(any(target_os = "macos", target_os = "ios")))]
fn recv(io: SockRef<'_>, bufs: &mut [IoSliceMut<'_>], meta: &mut [RecvMeta]) -> io::Result<usize> {
use std::ptr;

let mut names = [MaybeUninit::<libc::sockaddr_storage>::uninit(); BATCH_SIZE];
let mut ctrls = [cmsg::Aligned(MaybeUninit::<[u8; CMSG_LEN]>::uninit()); BATCH_SIZE];
let mut hdrs = unsafe { mem::zeroed::<[libc::mmsghdr; BATCH_SIZE]>() };
Expand Down

0 comments on commit 8307b7e

Please sign in to comment.