rtci_shared/ifaces/mod.rs
1/// Platform-specific interface enumeration (FFI into the OS).
2pub mod ffi;
3pub use ffi::ifaces;
4/// The local IPv6 addresses the operating system flags as RFC 4941 temporary (privacy)
5/// addresses, for an ICE agent that may leave them unwrapped by mDNS
6/// (draft-ietf-mmusic-mdns-ice-candidates section 3.1.2.3).
7///
8/// Where it comes from: Linux and Android, `/proc/net/if_inet6` (`IFA_F_TEMPORARY`; shown on a
9/// hosted Linux runner with a kernel-made temporary address); macOS and iOS, the
10/// `SIOCGIFAFLAG_IN6` ioctl (`IN6_IFF_TEMPORARY`, from XNU's `netinet6/in6_var.h`; the request
11/// is shown to be taken, a temporary address was not observed on a runner). Windows:
12/// `ErrorKind::Unsupported`, no address is ever marked (no flag shown to tell a temporary
13/// address from the stable one, see the Windows implementation). Other systems:
14/// `ErrorKind::Unsupported`.
15///
16/// # Errors
17///
18/// The flags could not be read (the platform has no way, or the call failed).
19pub use ffi::temporary_ipv6_addresses;
20
21/// The addresses of `/proc/net/if_inet6` (one line per address: the address in 32 hex
22/// digits, the interface index, the prefix length, the scope, the flags, the interface name;
23/// all hex) whose flags carry `IFA_F_TEMPORARY` (0x01).
24#[cfg(any(test, target_os = "linux", target_os = "android"))]
25pub(crate) fn parse_proc_if_inet6(text: &str) -> Vec<::std::net::Ipv6Addr> {
26 const IFA_F_TEMPORARY: u32 = 0x01;
27 text.lines()
28 .filter_map(|line| {
29 let fields: Vec<&str> = line.split_whitespace().collect();
30 let (address, flags) = (fields.first()?, fields.get(4)?);
31 if address.len() != 32 {
32 return None;
33 }
34 let address = u128::from_str_radix(address, 16).ok()?;
35 let flags = u32::from_str_radix(flags, 16).ok()?;
36 (flags & IFA_F_TEMPORARY != 0).then(|| ::std::net::Ipv6Addr::from(address))
37 })
38 .collect()
39}
40
41#[cfg(test)]
42mod tests {
43 use super::*;
44
45 #[test]
46 fn proc_if_inet6_lines_flagged_temporary_are_the_temporary_addresses() {
47 // Lines as a Linux host with a stable (mngtmpaddr, 0x100) and a temporary (0x01)
48 // address on eth0 writes them; the second temporary one is deprecated too (0x21).
49 let text = "\
5000000000000000000000000000000001 01 80 10 80 lo
51fe80000000000000021122fffe334455 02 40 20 80 eth0
522a0012340000000102112233aabbccdd 02 40 00 100 eth0
532a001234000000014d2f1a2b3c4d5e6f 02 40 00 01 eth0
542a001234000000019a8b7c6d5e4f3a2b 02 40 00 21 eth0
55";
56 assert_eq!(
57 parse_proc_if_inet6(text),
58 vec![
59 "2a00:1234:0:1:4d2f:1a2b:3c4d:5e6f"
60 .parse::<::std::net::Ipv6Addr>()
61 .unwrap(),
62 "2a00:1234:0:1:9a8b:7c6d:5e4f:3a2b".parse().unwrap(),
63 ]
64 );
65 }
66
67 /// Windows marks nothing: no flag there has been shown to tell a temporary address from
68 /// the stable one, and a wrong guess would publish the stable address.
69 #[test]
70 fn windows_marks_no_address_temporary() {
71 let result = temporary_ipv6_addresses();
72 if cfg!(windows) {
73 assert!(
74 matches!(&result, Err(e) if e.kind() == std::io::ErrorKind::Unsupported),
75 "{result:?}"
76 );
77 }
78 }
79
80 #[test]
81 fn the_temporary_addresses_the_system_reports_are_among_its_interface_addresses() {
82 let temporary = match temporary_ipv6_addresses() {
83 Ok(temporary) => temporary,
84 // Not read on this system: nothing is marked.
85 // (On macOS this is where a wrong `SIOCGIFAFLAG_IN6` request would show: the
86 // kernel refuses it for the host's loopback `::1` at least.)
87 Err(e) if e.kind() == std::io::ErrorKind::Unsupported => {
88 if cfg!(any(target_os = "linux", target_os = "macos")) {
89 panic!("the flags are read on this system: {e}");
90 }
91 return;
92 }
93 Err(e) => panic!("temporary_ipv6_addresses failed: {e}"),
94 };
95 let all: Vec<::std::net::IpAddr> = ifaces()
96 .unwrap()
97 .into_iter()
98 .filter_map(|i| i.addr.map(|a| a.ip()))
99 .collect();
100 for address in &temporary {
101 assert!(
102 all.contains(&::std::net::IpAddr::V6(*address)),
103 "{address} is not an interface address"
104 );
105 assert!(!address.is_loopback() && !address.is_unicast_link_local());
106 }
107 }
108}
109
110#[derive(PartialEq, Eq, Debug, Clone)]
111/// The next hop configured for an interface address.
112#[non_exhaustive]
113pub enum NextHop {
114 /// The broadcast address of the attached network.
115 Broadcast(::std::net::SocketAddr),
116 /// The destination address, for point-to-point links.
117 Destination(::std::net::SocketAddr),
118}
119
120#[derive(PartialEq, Eq, Debug, Clone)]
121/// The address family or link type an [`Interface`] entry describes.
122#[non_exhaustive]
123pub enum Kind {
124 /// A raw packet-level (link layer) address.
125 Packet,
126 /// A link-layer address, such as a MAC address.
127 Link,
128 /// An IPv4 address.
129 Ipv4,
130 /// An IPv6 address.
131 Ipv6,
132 /// An address family this crate does not recognise, carrying the raw OS value.
133 Unknow(i32),
134}
135
136#[derive(Debug, Clone)]
137/// One address on one local network interface.
138///
139/// ICE gathering walks these to produce host candidates.
140pub struct Interface {
141 /// The OS name of the interface, such as `en0` or `eth0`.
142 pub name: String,
143 /// Which address family or link type this entry describes.
144 pub kind: Kind,
145 /// The address itself, if the OS reported one.
146 pub addr: Option<::std::net::SocketAddr>,
147 /// The netmask for [`Self::addr`], if the OS reported one.
148 pub mask: Option<::std::net::SocketAddr>,
149 /// The broadcast or point-to-point destination address, if any.
150 pub hop: Option<NextHop>,
151}