|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use super::c_int; |
| 5 | +use s2n_quic_core::inet::IpV4Address; |
| 6 | + |
| 7 | +#[cfg(s2n_quic_platform_pktinfo)] |
| 8 | +mod pktinfo_enabled { |
| 9 | + use super::*; |
| 10 | + use crate::message::cmsg; |
| 11 | + use libc::{IPPROTO_IP, IP_PKTINFO}; |
| 12 | + |
| 13 | + pub const LEVEL: Option<c_int> = Some(IPPROTO_IP as _); |
| 14 | + pub const TYPE: Option<c_int> = Some(IP_PKTINFO as _); |
| 15 | + pub const SOCKOPT: Option<(c_int, c_int)> = Some((IPPROTO_IP as _, IP_PKTINFO as _)); |
| 16 | + pub const CMSG_SPACE: usize = crate::message::cmsg::size_of_cmsg::<Cmsg>(); |
| 17 | + |
| 18 | + pub type Cmsg = libc::in_pktinfo; |
| 19 | + |
| 20 | + #[inline] |
| 21 | + pub const fn is_match(level: c_int, ty: c_int) -> bool { |
| 22 | + level == IPPROTO_IP as c_int && ty == IP_PKTINFO as c_int |
| 23 | + } |
| 24 | + |
| 25 | + /// # Safety |
| 26 | + /// |
| 27 | + /// * The provided bytes must be aligned to `cmsghdr` |
| 28 | + #[inline] |
| 29 | + pub unsafe fn decode(bytes: &[u8]) -> Option<(IpV4Address, u32)> { |
| 30 | + let pkt_info = cmsg::decode::value_from_bytes::<Cmsg>(bytes)?; |
| 31 | + |
| 32 | + // read from both fields in case only one is set and not the other |
| 33 | + // |
| 34 | + // from https://man7.org/linux/man-pages/man7/ip.7.html: |
| 35 | + // |
| 36 | + // > ipi_spec_dst is the local address |
| 37 | + // > of the packet and ipi_addr is the destination address in |
| 38 | + // > the packet header. |
| 39 | + let local_address = match (pkt_info.ipi_addr.s_addr, pkt_info.ipi_spec_dst.s_addr) { |
| 40 | + (0, v) => v.to_ne_bytes(), |
| 41 | + (v, _) => v.to_ne_bytes(), |
| 42 | + }; |
| 43 | + |
| 44 | + let address = IpV4Address::new(local_address); |
| 45 | + let interface = pkt_info.ipi_ifindex as _; |
| 46 | + |
| 47 | + Some((address, interface)) |
| 48 | + } |
| 49 | + |
| 50 | + #[inline] |
| 51 | + pub fn encode(addr: &IpV4Address, local_interface: Option<u32>) -> Cmsg { |
| 52 | + let mut pkt_info = unsafe { core::mem::zeroed::<Cmsg>() }; |
| 53 | + pkt_info.ipi_spec_dst.s_addr = u32::from_ne_bytes((*addr).into()); |
| 54 | + if let Some(interface) = local_interface { |
| 55 | + pkt_info.ipi_ifindex = interface as _; |
| 56 | + } |
| 57 | + pkt_info |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +#[cfg(any(not(s2n_quic_platform_pktinfo), test))] |
| 62 | +mod pktinfo_disabled { |
| 63 | + #![cfg_attr(test, allow(dead_code))] |
| 64 | + use super::*; |
| 65 | + |
| 66 | + pub const LEVEL: Option<c_int> = None; |
| 67 | + pub const TYPE: Option<c_int> = None; |
| 68 | + pub const SOCKOPT: Option<(c_int, c_int)> = None; |
| 69 | + pub const CMSG_SPACE: usize = 0; |
| 70 | + |
| 71 | + pub type Cmsg = c_int; |
| 72 | + |
| 73 | + #[inline] |
| 74 | + pub const fn is_match(level: c_int, ty: c_int) -> bool { |
| 75 | + let _ = level; |
| 76 | + let _ = ty; |
| 77 | + false |
| 78 | + } |
| 79 | + |
| 80 | + /// # Safety |
| 81 | + /// |
| 82 | + /// * The provided bytes must be aligned to `cmsghdr` |
| 83 | + pub unsafe fn decode(bytes: &[u8]) -> Option<(IpV4Address, u32)> { |
| 84 | + let _ = bytes; |
| 85 | + None |
| 86 | + } |
| 87 | + |
| 88 | + #[inline] |
| 89 | + pub fn encode(addr: &IpV4Address, local_interface: Option<u32>) -> Cmsg { |
| 90 | + let _ = addr; |
| 91 | + let _ = local_interface; |
| 92 | + unimplemented!("this platform does not support pktinfo") |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +mod pktinfo_impl { |
| 97 | + #[cfg(not(s2n_quic_platform_pktinfo))] |
| 98 | + pub use super::pktinfo_disabled::*; |
| 99 | + #[cfg(s2n_quic_platform_pktinfo)] |
| 100 | + pub use super::pktinfo_enabled::*; |
| 101 | +} |
| 102 | + |
| 103 | +pub use pktinfo_impl::*; |
| 104 | + |
| 105 | +pub const IS_SUPPORTED: bool = cfg!(s2n_quic_platform_pktinfo); |
0 commit comments