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

Support openbsd #1863

Merged
merged 1 commit into from
May 21, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion quinn-udp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub struct RecvMeta {
pub ecn: Option<EcnCodepoint>,
/// The destination IP address which was encoded in this datagram
///
/// Populated on platforms: Windows, Linux, Android, FreeBSD, macOS, and iOS.
/// Populated on platforms: Windows, Linux, Android, FreeBSD, OpenBSD, macOS, and iOS.
pub dst_ip: Option<IpAddr>,
}

Expand Down
70 changes: 50 additions & 20 deletions quinn-udp/src/unix.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "openbsd")))]
use std::ptr;
use std::{
io::{self, IoSliceMut},
Expand All @@ -18,6 +18,14 @@ use super::{
cmsg, log_sendmsg_error, EcnCodepoint, RecvMeta, Transmit, UdpSockRef, IO_ERROR_LOG_INTERVAL,
};

// Defined in netinet6/in6.h on OpenBSD, this is not yet exported by the libc crate
// directly. See https://github.com/rust-lang/libc/issues/3704 for when we might be able to
// rely on this from the libc crate.
#[cfg(target_os = "openbsd")]
const IPV6_DONTFRAG: libc::c_int = 62;
#[cfg(not(target_os = "openbsd"))]
const IPV6_DONTFRAG: libc::c_int = libc::IPV6_DONTFRAG;

#[cfg(target_os = "freebsd")]
type IpTosTy = libc::c_uchar;
#[cfg(not(target_os = "freebsd"))]
Expand Down Expand Up @@ -48,6 +56,7 @@ impl UdpSocketState {
let mut cmsg_platform_space = 0;
if cfg!(target_os = "linux")
|| cfg!(target_os = "freebsd")
|| cfg!(target_os = "openbsd")
|| cfg!(target_os = "macos")
|| cfg!(target_os = "ios")
|| cfg!(target_os = "android")
Expand All @@ -73,6 +82,7 @@ impl UdpSocketState {

// mac and ios do not support IP_RECVTOS on dual-stack sockets :(
// older macos versions also don't have the flag and will error out if we don't ignore it
#[cfg(not(target_os = "openbsd"))]
if is_ipv4 || !io.only_v6()? {
if let Err(err) = set_socket_option(&*io, libc::IPPROTO_IP, libc::IP_RECVTOS, OPTION_ON)
{
Expand Down Expand Up @@ -108,7 +118,12 @@ impl UdpSocketState {
)?;
}
}
#[cfg(any(target_os = "freebsd", target_os = "macos", target_os = "ios"))]
#[cfg(any(
target_os = "freebsd",
target_od = "openbsd",
target_os = "macos",
target_os = "ios"
))]
{
if is_ipv4 {
// Set `may_fragment` to `true` if this option is not supported on the platform.
Expand All @@ -120,7 +135,12 @@ impl UdpSocketState {
)?;
}
}
#[cfg(any(target_os = "freebsd", target_os = "macos", target_os = "ios"))]
#[cfg(any(
target_os = "freebsd",
target_os = "openbsd",
target_os = "macos",
target_os = "ios"
))]
// IP_RECVDSTADDR == IP_SENDSRCADDR on FreeBSD
// macOS uses only IP_RECVDSTADDR, no IP_SENDSRCADDR on macOS
// macOS also supports IP_PKTINFO
Expand All @@ -138,12 +158,8 @@ impl UdpSocketState {
// kernel's path MTU guess, but actually disabling fragmentation requires this too. See
// __ip6_append_data in ip6_output.c.
// Set `may_fragment` to `true` if this option is not supported on the platform.
may_fragment |= !set_socket_option_supported(
&*io,
libc::IPPROTO_IPV6,
libc::IPV6_DONTFRAG,
OPTION_ON,
)?;
may_fragment |=
!set_socket_option_supported(&*io, libc::IPPROTO_IPV6, IPV6_DONTFRAG, OPTION_ON)?;
}

let now = Instant::now();
Expand Down Expand Up @@ -202,13 +218,13 @@ impl UdpSocketState {
}

/// Sets the flag indicating we got EINVAL error from `sendmsg` or `sendmmsg` syscall.
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "openbsd")))]
fn set_sendmsg_einval(&self) {
self.sendmsg_einval.store(true, Ordering::Relaxed)
}
}

#[cfg(not(any(target_os = "macos", target_os = "ios")))]
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "openbsd")))]
fn send(
#[allow(unused_variables)] // only used on Linux
state: &UdpSocketState,
Expand Down Expand Up @@ -293,7 +309,7 @@ fn send(
}
}

#[cfg(any(target_os = "macos", target_os = "ios"))]
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "openbsd"))]
Ralith marked this conversation as resolved.
Show resolved Hide resolved
fn send(state: &UdpSocketState, io: SockRef<'_>, transmit: &Transmit<'_>) -> io::Result<()> {
let mut hdr: libc::msghdr = unsafe { mem::zeroed() };
let mut iov: libc::iovec = unsafe { mem::zeroed() };
Expand All @@ -306,7 +322,7 @@ fn send(state: &UdpSocketState, io: SockRef<'_>, transmit: &Transmit<'_>) -> io:
&mut iov,
&mut ctrl,
// Only tested on macOS and iOS
cfg!(target_os = "macos") || cfg!(target_os = "ios"),
cfg!(target_os = "macos") || cfg!(target_os = "ios") || cfg!(target_os = "openbsd"),
state.sendmsg_einval(),
);
let n = unsafe { libc::sendmsg(io.as_raw_fd(), &hdr, 0) };
Expand Down Expand Up @@ -335,7 +351,7 @@ fn send(state: &UdpSocketState, io: SockRef<'_>, transmit: &Transmit<'_>) -> io:
Ok(())
}

#[cfg(not(any(target_os = "macos", target_os = "ios")))]
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "openbsd")))]
fn recv(io: SockRef<'_>, bufs: &mut [IoSliceMut<'_>], meta: &mut [RecvMeta]) -> io::Result<usize> {
let mut names = [MaybeUninit::<libc::sockaddr_storage>::uninit(); BATCH_SIZE];
let mut ctrls = [cmsg::Aligned(MaybeUninit::<[u8; CMSG_LEN]>::uninit()); BATCH_SIZE];
Expand Down Expand Up @@ -372,7 +388,7 @@ fn recv(io: SockRef<'_>, bufs: &mut [IoSliceMut<'_>], meta: &mut [RecvMeta]) ->
Ok(msg_count as usize)
}

#[cfg(any(target_os = "macos", target_os = "ios"))]
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "openbsd"))]
fn recv(io: SockRef<'_>, bufs: &mut [IoSliceMut<'_>], meta: &mut [RecvMeta]) -> io::Result<usize> {
let mut name = MaybeUninit::<libc::sockaddr_storage>::uninit();
let mut ctrl = cmsg::Aligned(MaybeUninit::<[u8; CMSG_LEN]>::uninit());
Expand Down Expand Up @@ -401,7 +417,7 @@ fn recv(io: SockRef<'_>, bufs: &mut [IoSliceMut<'_>], meta: &mut [RecvMeta]) ->
///
/// It uses [`libc::syscall`] instead of [`libc::recvmmsg`]
/// to avoid linking error on systems where libc does not contain `recvmmsg`.
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "openbsd")))]
unsafe fn recvmmsg_with_fallback(
sockfd: libc::c_int,
msgvec: *mut libc::mmsghdr,
Expand Down Expand Up @@ -442,7 +458,7 @@ unsafe fn recvmmsg_with_fallback(
/// Fallback implementation of `recvmmsg` using `recvmsg`
/// for systems which do not support `recvmmsg`
/// such as Linux <2.6.33.
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "openbsd")))]
unsafe fn recvmmsg_fallback(
sockfd: libc::c_int,
msgvec: *mut libc::mmsghdr,
Expand Down Expand Up @@ -524,7 +540,12 @@ fn prepare_msg(
};
encoder.push(libc::IPPROTO_IP, libc::IP_PKTINFO, pktinfo);
}
#[cfg(any(target_os = "freebsd", target_os = "macos", target_os = "ios"))]
#[cfg(any(
target_os = "freebsd",
target_os = "macos",
target_os = "ios",
target_os = "openbsd"
))]
{
if encode_src_ip {
let addr = libc::in_addr {
Expand Down Expand Up @@ -578,8 +599,12 @@ fn decode_recv(
let cmsg_iter = unsafe { cmsg::Iter::new(hdr) };
for cmsg in cmsg_iter {
match (cmsg.cmsg_level, cmsg.cmsg_type) {
(libc::IPPROTO_IP, libc::IP_TOS) => unsafe {
ecn_bits = cmsg::decode::<u8, libc::cmsghdr>(cmsg);
},
// FreeBSD uses IP_RECVTOS here, and we can be liberal because cmsgs are opt-in.
(libc::IPPROTO_IP, libc::IP_TOS) | (libc::IPPROTO_IP, libc::IP_RECVTOS) => unsafe {
#[cfg(not(target_os = "openbsd"))]
(libc::IPPROTO_IP, libc::IP_RECVTOS) => unsafe {
flub marked this conversation as resolved.
Show resolved Hide resolved
ecn_bits = cmsg::decode::<u8, libc::cmsghdr>(cmsg);
},
(libc::IPPROTO_IPV6, libc::IPV6_TCLASS) => unsafe {
Expand All @@ -601,7 +626,12 @@ fn decode_recv(
pktinfo.ipi_addr.s_addr.to_ne_bytes(),
)));
}
#[cfg(any(target_os = "freebsd", target_os = "macos", target_os = "ios"))]
#[cfg(any(
target_os = "freebsd",
target_os = "macos",
target_os = "ios",
target_os = "openbsd"
))]
(libc::IPPROTO_IP, libc::IP_RECVDSTADDR) => {
let in_addr = unsafe { cmsg::decode::<libc::in_addr, libc::cmsghdr>(cmsg) };
dst_ip = Some(IpAddr::V4(Ipv4Addr::from(in_addr.s_addr.to_ne_bytes())));
Expand Down
64 changes: 44 additions & 20 deletions quinn-udp/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#[cfg(not(target_os = "openbsd"))]
use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6};
use std::{
io::IoSliceMut,
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6, UdpSocket},
net::{IpAddr, Ipv4Addr, Ipv6Addr, UdpSocket},
slice,
};

Expand Down Expand Up @@ -31,6 +33,46 @@ fn basic() {

#[test]
fn ecn_v6() {
let send = Socket::from(UdpSocket::bind("[::1]:0").unwrap());
let recv = Socket::from(UdpSocket::bind("[::1]:0").unwrap());
for codepoint in [EcnCodepoint::Ect0, EcnCodepoint::Ect1] {
test_send_recv(
&send,
&recv,
Transmit {
destination: recv.local_addr().unwrap().as_socket().unwrap(),
ecn: Some(codepoint),
contents: b"hello",
segment_size: None,
src_ip: None,
},
);
}
}

#[test]
#[cfg(not(target_os = "openbsd"))]
fn ecn_v4() {
let send = Socket::from(UdpSocket::bind("127.0.0.1:0").unwrap());
let recv = Socket::from(UdpSocket::bind("127.0.0.1:0").unwrap());
for codepoint in [EcnCodepoint::Ect0, EcnCodepoint::Ect1] {
test_send_recv(
&send,
&recv,
Transmit {
destination: recv.local_addr().unwrap().as_socket().unwrap(),
ecn: Some(codepoint),
contents: b"hello",
segment_size: None,
src_ip: None,
},
);
}
}

#[test]
#[cfg(not(target_os = "openbsd"))]
fn ecn_v6_dualstack() {
let recv = socket2::Socket::new(
socket2::Domain::IPV6,
socket2::Type::DGRAM,
Expand Down Expand Up @@ -72,25 +114,7 @@ fn ecn_v6() {
}

#[test]
fn ecn_v4() {
let send = Socket::from(UdpSocket::bind("127.0.0.1:0").unwrap());
let recv = Socket::from(UdpSocket::bind("127.0.0.1:0").unwrap());
for codepoint in [EcnCodepoint::Ect0, EcnCodepoint::Ect1] {
test_send_recv(
&send,
&recv,
Transmit {
destination: recv.local_addr().unwrap().as_socket().unwrap(),
ecn: Some(codepoint),
contents: b"hello",
segment_size: None,
src_ip: None,
},
);
}
}

#[test]
#[cfg(not(target_os = "openbsd"))]
fn ecn_v4_mapped_v6() {
let send = socket2::Socket::new(
socket2::Domain::IPV6,
Expand Down
1 change: 1 addition & 0 deletions quinn/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ fn run_echo(args: EchoArgs) {
// platforms in the doc comment of `quinn_udp::RecvMeta::dst_ip`.
if cfg!(target_os = "linux")
|| cfg!(target_os = "freebsd")
|| cfg!(target_os = "openbsd")
|| cfg!(target_os = "macos")
|| cfg!(target_os = "windows")
{
Expand Down