Skip to content

Commit

Permalink
more gates
Browse files Browse the repository at this point in the history
  • Loading branch information
evanrittenhouse committed Dec 30, 2024
1 parent cfe6ffd commit b4316ec
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 32 deletions.
50 changes: 31 additions & 19 deletions dgram/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,30 @@ use std::net::SocketAddr;
use std::time::Instant;
use std::time::SystemTime;

use libc::in6_pktinfo;
use libc::in_pktinfo;
use libc::sockaddr_in;
use libc::sockaddr_in6;
use nix::sys::socket::ControlMessageOwned;
#[cfg(unix)]
mod unix {
use libc::in6_pktinfo;
use libc::in_pktinfo;
use libc::sockaddr_in;
use libc::sockaddr_in6;
pub(super) use nix::sys::socket::ControlMessageOwned;

#[derive(Debug)]
pub enum IpOrigDstAddr {
V4(sockaddr_in),
V6(sockaddr_in6),
}

#[cfg(unix)]
#[derive(Copy, Clone, Debug)]
pub enum IpPktInfo {
V4(in_pktinfo),
V6(in6_pktinfo),
}
}

#[cfg(unix)]
pub use unix::{IpOrigDstAddr, IpPktInfo};

/// Settings for handling control messages when sending data.
#[cfg(target_os = "linux")]
Expand Down Expand Up @@ -79,8 +98,10 @@ pub struct RecvData {
///
/// This can be either an IPv4 or IPv6 address, depending on whether
/// `IPV4_ORIGDSTADDR` or `IPV6_ORIGDSTADDR` was received.
#[cfg(unix)]
pub original_addr: Option<IpOrigDstAddr>,
cmsgs: Vec<ControlMessageOwned>,
#[cfg(unix)]
cmsgs: Vec<unix::ControlMessageOwned>,
}

impl RecvData {
Expand All @@ -93,7 +114,9 @@ impl RecvData {
metrics: None,
gro: None,
rx_time: None,
#[cfg(unix)]
original_addr: None,
#[cfg(unix)]
cmsgs: Vec::with_capacity(cmsg_space_len),
}
}
Expand All @@ -109,7 +132,8 @@ impl RecvData {
/// Returns the list of cmsgs which were returned from calling `recvmsg`. If
/// `recvmsg` was called with its [`RecvMsgCmsgSettings::store_cmsgs`]
/// field set to to `false`, this will return an empty slice.
pub fn cmsgs(&self) -> &[ControlMessageOwned] {
#[cfg(unix)]
pub fn cmsgs(&self) -> &[unix::ControlMessageOwned] {
&self.cmsgs
}
}
Expand All @@ -124,18 +148,6 @@ pub struct RecvMetrics {
pub udp_packets_dropped: u64,
}

#[derive(Debug)]
pub enum IpOrigDstAddr {
V4(sockaddr_in),
V6(sockaddr_in6),
}

#[derive(Copy, Clone, Debug)]
pub enum IpPktInfo {
V4(in_pktinfo),
V6(in6_pktinfo),
}

#[cfg(target_os = "linux")]
mod linux_imports {
pub(super) use crate::syscalls::recv_msg;
Expand Down
30 changes: 17 additions & 13 deletions dgram/src/socket_setup.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use std::io;
use std::os::fd::AsFd;

#[cfg(target_os = "linux")]
use super::linux_imports::*;

#[cfg(unix)]
mod unix {
pub(super) use std::io;
pub(super) use std::os::fd::AsFd;
}

/// Indicators of settings applied to a socket. These settings aren't "applied"
/// to a socket. Rather, the same (maximal) settings are always applied to a
/// socket, and this struct indicates which of those settings were successfully
Expand Down Expand Up @@ -31,7 +34,7 @@ impl SocketCapabilities {
/// which settings were successfully applied.
#[cfg(unix)]
pub fn apply_all_and_get_compatibility(
socket: &impl AsFd, max_send_udp_payload_size: usize,
socket: &impl unix::AsFd, max_send_udp_payload_size: usize,
) -> Self {
let fd = socket.as_fd();

Expand All @@ -52,8 +55,8 @@ pub fn set_gso_segment(sock: &impl AsFd, segment: usize) -> io::Result<()> {
Ok(())
}

#[cfg(not(target_os = "linux"))]
pub fn set_gso_segment(_: &impl AsFd, _: usize) -> io::Result<()> {
#[cfg(all(not(target_os = "linux"), unix))]
pub fn set_gso_segment(_: &impl unix::AsFd, _: usize) -> unix::io::Result<()> {
Err(std::io::Error::from(std::io::ErrorKind::Unsupported))
}

Expand All @@ -65,7 +68,8 @@ pub fn set_gro(sock: &impl AsFd) -> io::Result<()> {
}

#[cfg(not(target_os = "linux"))]
pub fn set_gro(_: &impl AsFd) -> io::Result<()> {
#[cfg(all(not(target_os = "linux"), unix))]
pub fn set_gro(_: &impl unix::AsFd) -> unix::io::Result<()> {
Err(std::io::Error::from(std::io::ErrorKind::Unsupported))
}

Expand All @@ -76,8 +80,8 @@ fn set_udp_rxq_ovfl(sock: &impl AsFd) -> io::Result<()> {
Ok(())
}

#[cfg(not(target_os = "linux"))]
fn set_udp_rxq_ovfl(_: &impl AsFd) -> io::Result<()> {
#[cfg(all(not(target_os = "linux"), unix))]
fn set_udp_rxq_ovfl(_: &impl unix::AsFd) -> unix::io::Result<()> {
Err(std::io::Error::from(std::io::ErrorKind::Unsupported))
}

Expand All @@ -93,8 +97,8 @@ pub fn set_tx_time(sock: &impl AsFd) -> io::Result<()> {
Ok(())
}

#[cfg(not(target_os = "linux"))]
pub fn set_tx_time(_: &impl AsFd) -> io::Result<()> {
#[cfg(all(not(target_os = "linux"), unix))]
pub fn set_tx_time(_: &impl unix::AsFd) -> unix::io::Result<()> {
Err(std::io::Error::from(std::io::ErrorKind::Unsupported))
}

Expand All @@ -105,7 +109,7 @@ pub fn set_rx_time(sock: &impl AsFd) -> io::Result<()> {
Ok(())
}

#[cfg(not(target_os = "linux"))]
pub fn set_rx_time(_: &impl AsFd) -> io::Result<()> {
#[cfg(all(not(target_os = "linux"), unix))]
pub fn set_rx_time(_: &impl unix::AsFd) -> unix::io::Result<()> {
Err(std::io::Error::from(std::io::ErrorKind::Unsupported))
}

0 comments on commit b4316ec

Please sign in to comment.