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 local IPv6 addresses the operating system flags as deprecated (RFC 4862 section 5.5.4:
22/// past their preferred lifetime; a temporary address is deprecated once a newer one replaces
23/// it, RFC 8981 section 3.4), for an ICE agent that discards deprecated temporary addresses not
24/// in use (RFC 8835 section 3.3).
25///
26/// Where it comes from: Linux and Android, `/proc/net/if_inet6` (`IFA_F_DEPRECATED`, 0x20, in
27/// `linux/if_addr.h`); macOS and iOS, the `SIOCGIFAFLAG_IN6` ioctl (`IN6_IFF_DEPRECATED`,
28/// 0x0010, XNU `netinet6/in6_var.h`). Windows and other systems: `ErrorKind::Unsupported`.
29///
30/// # Errors
31///
32/// The flags could not be read (the platform has no way, or the call failed).
33pub use ffi::deprecated_ipv6_addresses;
34
35/// The addresses of `/proc/net/if_inet6` (one line per address: the address in 32 hex
36/// digits, the interface index, the prefix length, the scope, the flags, the interface name;
37/// all hex) whose flags carry `IFA_F_TEMPORARY` (0x01).
38#[cfg(any(test, target_os = "linux", target_os = "android"))]
39pub(crate) fn parse_proc_if_inet6(text: &str) -> Vec<::std::net::Ipv6Addr> {
40 const IFA_F_TEMPORARY: u32 = 0x01;
41 parse_proc_if_inet6_flag(text, IFA_F_TEMPORARY)
42}
43
44/// The addresses of `/proc/net/if_inet6` whose flags carry `flag`.
45#[cfg(any(test, target_os = "linux", target_os = "android"))]
46pub(crate) fn parse_proc_if_inet6_flag(text: &str, flag: u32) -> Vec<::std::net::Ipv6Addr> {
47 text.lines()
48 .filter_map(|line| {
49 let fields: Vec<&str> = line.split_whitespace().collect();
50 let (address, flags) = (fields.first()?, fields.get(4)?);
51 if address.len() != 32 {
52 return None;
53 }
54 let address = u128::from_str_radix(address, 16).ok()?;
55 let flags = u32::from_str_radix(flags, 16).ok()?;
56 (flags & flag != 0).then(|| ::std::net::Ipv6Addr::from(address))
57 })
58 .collect()
59}
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64
65 #[test]
66 fn proc_if_inet6_lines_flagged_temporary_are_the_temporary_addresses() {
67 // Lines as a Linux host with a stable (mngtmpaddr, 0x100) and a temporary (0x01)
68 // address on eth0 writes them; the second temporary one is deprecated too (0x21).
69 let text = "\
7000000000000000000000000000000001 01 80 10 80 lo
71fe80000000000000021122fffe334455 02 40 20 80 eth0
722a0012340000000102112233aabbccdd 02 40 00 100 eth0
732a001234000000014d2f1a2b3c4d5e6f 02 40 00 01 eth0
742a001234000000019a8b7c6d5e4f3a2b 02 40 00 21 eth0
75";
76 assert_eq!(
77 parse_proc_if_inet6(text),
78 vec![
79 "2a00:1234:0:1:4d2f:1a2b:3c4d:5e6f"
80 .parse::<::std::net::Ipv6Addr>()
81 .unwrap(),
82 "2a00:1234:0:1:9a8b:7c6d:5e4f:3a2b".parse().unwrap(),
83 ]
84 );
85 }
86
87 /// `IFA_F_DEPRECATED` (0x20): the second temporary address of the lines above.
88 #[test]
89 fn proc_if_inet6_lines_flagged_deprecated_are_the_deprecated_addresses() {
90 let text = "\
912a001234000000014d2f1a2b3c4d5e6f 02 40 00 01 eth0
922a001234000000019a8b7c6d5e4f3a2b 02 40 00 21 eth0
93";
94 assert_eq!(
95 parse_proc_if_inet6_flag(text, 0x20),
96 vec![
97 "2a00:1234:0:1:9a8b:7c6d:5e4f:3a2b"
98 .parse::<::std::net::Ipv6Addr>()
99 .unwrap()
100 ]
101 );
102 }
103
104 /// Windows marks nothing: no flag there has been shown to tell a temporary address from
105 /// the stable one, and a wrong guess would publish the stable address.
106 #[test]
107 fn windows_marks_no_address_temporary() {
108 let result = temporary_ipv6_addresses();
109 if cfg!(windows) {
110 assert!(
111 matches!(&result, Err(e) if e.kind() == std::io::ErrorKind::Unsupported),
112 "{result:?}"
113 );
114 }
115 }
116
117 #[test]
118 fn the_temporary_addresses_the_system_reports_are_among_its_interface_addresses() {
119 let temporary = match temporary_ipv6_addresses() {
120 Ok(temporary) => temporary,
121 // Not read on this system: nothing is marked.
122 // (On macOS this is where a wrong `SIOCGIFAFLAG_IN6` request would show: the
123 // kernel refuses it for the host's loopback `::1` at least.)
124 Err(e) if e.kind() == std::io::ErrorKind::Unsupported => {
125 if cfg!(any(target_os = "linux", target_os = "macos")) {
126 panic!("the flags are read on this system: {e}");
127 }
128 return;
129 }
130 Err(e) => panic!("temporary_ipv6_addresses failed: {e}"),
131 };
132 let all: Vec<::std::net::IpAddr> = ifaces()
133 .unwrap()
134 .into_iter()
135 .filter_map(|i| i.addr.map(|a| a.ip()))
136 .collect();
137 for address in &temporary {
138 assert!(
139 all.contains(&::std::net::IpAddr::V6(*address)),
140 "{address} is not an interface address"
141 );
142 assert!(!address.is_loopback() && !address.is_unicast_link_local());
143 }
144 }
145}
146
147#[derive(PartialEq, Eq, Debug, Clone)]
148/// The next hop configured for an interface address.
149#[non_exhaustive]
150pub enum NextHop {
151 /// The broadcast address of the attached network.
152 Broadcast(::std::net::SocketAddr),
153 /// The destination address, for point-to-point links.
154 Destination(::std::net::SocketAddr),
155}
156
157#[derive(PartialEq, Eq, Debug, Clone)]
158/// The address family or link type an [`Interface`] entry describes.
159#[non_exhaustive]
160pub enum Kind {
161 /// A raw packet-level (link layer) address.
162 Packet,
163 /// A link-layer address, such as a MAC address.
164 Link,
165 /// An IPv4 address.
166 Ipv4,
167 /// An IPv6 address.
168 Ipv6,
169 /// An address family this crate does not recognise, carrying the raw OS value.
170 Unknow(i32),
171}
172
173#[derive(Debug, Clone)]
174/// One address on one local network interface.
175///
176/// ICE gathering walks these to produce host candidates.
177pub struct Interface {
178 /// The OS name of the interface, such as `en0` or `eth0`.
179 pub name: String,
180 /// Which address family or link type this entry describes.
181 pub kind: Kind,
182 /// The address itself, if the OS reported one.
183 pub addr: Option<::std::net::SocketAddr>,
184 /// The netmask for [`Self::addr`], if the OS reported one.
185 pub mask: Option<::std::net::SocketAddr>,
186 /// The broadcast or point-to-point destination address, if any.
187 pub hop: Option<NextHop>,
188}