Skip to content
Open
Show file tree
Hide file tree
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
13 changes: 13 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,16 @@ jobs:
run: |
. $HOME/.cargo/env
cargo hack test --target ${{ matrix.target }} --feature-powerset
openbsd:
name: OpenBSD/x86_64
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- name: Test on OpenBSD
uses: vmactions/openbsd-vm@v1
with:
release: "8.0"
prepare: |
pkg_add rust # `rustup` has no toolchain for OpenBSD
run: |
cargo test && cargo test --features all
20 changes: 8 additions & 12 deletions src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::os::fd::{FromRawFd, IntoRawFd};
use std::os::windows::io::{FromRawSocket, IntoRawSocket};
use std::time::Duration;

use crate::sys::{self, c_int, getsockopt, setsockopt, Bool};
use crate::sys::{self, c_int, getsockopt, setsockopt, Bool, IpMulticastOpt};
#[cfg(all(unix, not(any(target_os = "redox", target_os = "horizon"))))]
use crate::MsgHdrMut;
use crate::{Domain, Protocol, SockAddr, TcpKeepalive, Type};
Expand Down Expand Up @@ -1562,7 +1562,7 @@ impl Socket {
/// [`set_multicast_loop_v4`]: Socket::set_multicast_loop_v4
pub fn multicast_loop_v4(&self) -> io::Result<bool> {
unsafe {
getsockopt::<c_int>(self.as_raw(), sys::IPPROTO_IP, sys::IP_MULTICAST_LOOP)
getsockopt::<IpMulticastOpt>(self.as_raw(), sys::IPPROTO_IP, sys::IP_MULTICAST_LOOP)
.map(|loop_v4| loop_v4 != 0)
}
}
Expand All @@ -1577,7 +1577,7 @@ impl Socket {
self.as_raw(),
sys::IPPROTO_IP,
sys::IP_MULTICAST_LOOP,
loop_v4 as c_int,
IpMulticastOpt::from(loop_v4),
)
}
}
Expand All @@ -1589,7 +1589,7 @@ impl Socket {
/// [`set_multicast_ttl_v4`]: Socket::set_multicast_ttl_v4
pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
unsafe {
getsockopt::<c_int>(self.as_raw(), sys::IPPROTO_IP, sys::IP_MULTICAST_TTL)
getsockopt::<IpMulticastOpt>(self.as_raw(), sys::IPPROTO_IP, sys::IP_MULTICAST_TTL)
.map(|ttl| ttl as u32)
}
}
Expand All @@ -1602,14 +1602,10 @@ impl Socket {
///
/// Note that this may not have any affect on IPv6 sockets.
pub fn set_multicast_ttl_v4(&self, ttl: u32) -> io::Result<()> {
unsafe {
setsockopt(
self.as_raw(),
sys::IPPROTO_IP,
sys::IP_MULTICAST_TTL,
ttl as c_int,
)
}
let ttl = IpMulticastOpt::try_from(ttl).map_err(|_| {
io::Error::new(io::ErrorKind::InvalidInput, "multicast TTL out of range")
})?;
unsafe { setsockopt(self.as_raw(), sys::IPPROTO_IP, sys::IP_MULTICAST_TTL, ttl) }
}

/// Get the value of the `IP_TTL` option for this socket.
Expand Down
15 changes: 15 additions & 0 deletions src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,21 @@ pub(crate) use libc::{TCP_KEEPCNT, TCP_KEEPINTVL};
// See this type in the Windows file.
pub(crate) type Bool = c_int;

#[cfg(any(
target_os = "illumos",
target_os = "netbsd",
target_os = "openbsd",
target_os = "solaris",
))]
pub(crate) type IpMulticastOpt = libc::c_uchar;
#[cfg(not(any(
target_os = "illumos",
target_os = "netbsd",
target_os = "openbsd",
target_os = "solaris",
)))]
pub(crate) type IpMulticastOpt = c_int;

#[cfg(any(
target_os = "ios",
target_os = "visionos",
Expand Down
2 changes: 2 additions & 0 deletions src/sys/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ pub(crate) use windows_sys::Win32::Networking::WinSock::{
pub(crate) const IPPROTO_IP: c_int = windows_sys::Win32::Networking::WinSock::IPPROTO_IP as c_int;
pub(crate) const SOL_SOCKET: c_int = windows_sys::Win32::Networking::WinSock::SOL_SOCKET as c_int;

pub(crate) type IpMulticastOpt = c_int;

// This is so we can special case MaybeUninit::assume_init for Bool.
// See Bool for why.
pub(crate) trait GetsockoptOutput: Sized {
Expand Down
51 changes: 51 additions & 0 deletions tests/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1648,6 +1648,57 @@ test!(IPv4 multicast_all_v4, set_multicast_all_v4(false));
#[cfg(all(feature = "all", target_os = "linux"))]
test!(IPv6 multicast_all_v6, set_multicast_all_v6(false));

#[test]
fn multicast_loop_v4() {
let socket = Socket::new(Domain::IPV4, Type::DGRAM, None).unwrap();

let initial = socket
.multicast_loop_v4()
.expect("failed to get initial value");

assert_ne!(initial, false, "initial value and argument are the same");

socket
.set_multicast_loop_v4(false)
.expect("failed to set option");

let got = socket.multicast_loop_v4().expect("failed to get value");

assert_eq!(got, false, "set and get values differ");
}

#[test]
fn multicast_ttl_v4() {
let socket = Socket::new(Domain::IPV4, Type::DGRAM, None).unwrap();

let initial = socket
.multicast_ttl_v4()
.expect("failed to get initial value");

assert_ne!(initial, 255, "initial value and argument are the same");

socket
.set_multicast_ttl_v4(255)
.expect("failed to set option");

let got = socket.multicast_ttl_v4().expect("failed to get value");

assert_eq!(got, 255, "set and get values differ");
}

#[test]
fn multicast_ttl_v4_out_of_range() {
let socket = Socket::new(Domain::IPV4, Type::DGRAM, None).unwrap();

for ttl in [256, u32::MAX] {
match socket.set_multicast_ttl_v4(ttl) {
Ok(()) => panic!("unexpected success for {}", ttl),
Err(ref err) if err.kind() == io::ErrorKind::InvalidInput => {}
Err(err) => panic!("unexpected error {}", err),
}
}
}

#[test]
#[cfg(not(any(
target_os = "haiku",
Expand Down
Loading