Skip to main content

mctx_core/raw_ip/
capabilities.rs

1/// Raw-IP support level for one address family on the current platform.
2#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3pub enum RawIpCapability {
4    /// The platform has no supported transmit implementation for this family.
5    Unsupported,
6    /// The platform accepts a complete caller-provided IPv4 datagram through
7    /// an `IP_HDRINCL`-style socket.
8    FullIpDatagram,
9    /// The platform accepts the caller-provided IPv6 payload, but its network
10    /// stack rebuilds the IPv6 base header during transmit.
11    ///
12    /// The raw-IP API pins the source to the configured local bind address and
13    /// applies the supplied traffic class and hop limit. IPv6 flow-label and
14    /// transport-checksum behavior remain platform controlled.
15    KernelRebuiltIpv6Header,
16}
17
18/// Raw-IP transmit support exposed by the current platform.
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub struct RawIpCapabilities {
21    /// IPv4 raw-IP transmit behavior.
22    pub ipv4: RawIpCapability,
23    /// IPv6 raw-IP transmit behavior.
24    pub ipv6: RawIpCapability,
25}
26
27/// Returns the raw-IP capabilities compiled for the current platform.
28pub const fn raw_ip_capabilities() -> RawIpCapabilities {
29    #[cfg(any(target_os = "linux", target_os = "macos"))]
30    {
31        RawIpCapabilities {
32            ipv4: RawIpCapability::FullIpDatagram,
33            ipv6: RawIpCapability::KernelRebuiltIpv6Header,
34        }
35    }
36
37    #[cfg(windows)]
38    {
39        RawIpCapabilities {
40            ipv4: RawIpCapability::FullIpDatagram,
41            ipv6: RawIpCapability::Unsupported,
42        }
43    }
44
45    #[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
46    {
47        RawIpCapabilities {
48            ipv4: RawIpCapability::Unsupported,
49            ipv6: RawIpCapability::Unsupported,
50        }
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn capability_report_matches_the_compiled_platform() {
60        let capabilities = raw_ip_capabilities();
61
62        #[cfg(any(target_os = "linux", target_os = "macos"))]
63        assert_eq!(capabilities.ipv4, RawIpCapability::FullIpDatagram);
64        #[cfg(any(target_os = "linux", target_os = "macos"))]
65        assert_eq!(capabilities.ipv6, RawIpCapability::KernelRebuiltIpv6Header);
66
67        #[cfg(windows)]
68        assert_eq!(capabilities.ipv4, RawIpCapability::FullIpDatagram);
69        #[cfg(windows)]
70        assert_eq!(capabilities.ipv6, RawIpCapability::Unsupported);
71    }
72}