Debian Codegraph @

src/interface.rs

raw symbols SCIP
1 use std::{
2 collections::HashMap,
3 net::{IpAddr, SocketAddr},
4 str::FromStr,
5 };
6
7 use super::cerr;
8
9 #[cfg(target_os = "linux")]
10 mod linux;
11 #[cfg(target_os = "linux")]
12 pub use linux::ChangeDetector;
13
14 // NOTE: this detection logic is not sharable with macos!
15 #[cfg(target_os = "freebsd")]
16 mod freebsd;
17 #[cfg(target_os = "freebsd")]
18 pub use freebsd::ChangeDetector;
19
20 #[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
21 mod fallback;
22 #[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
23 pub use fallback::ChangeDetector;
24
25 pub fn interfaces() -> std::io::Result<HashMap<InterfaceName, InterfaceData>> {
26 let mut elements = HashMap::default();
27
28 for data in InterfaceIterator::new()? {
29 let current: &mut InterfaceData = elements.entry(data.name).or_default();
30
31 current.socket_addrs.extend(data.socket_addr);
32 assert!(!(current.mac.is_some() && data.mac.is_some()));
33 current.mac = current.mac.or(data.mac);
34 }
35
36 Ok(elements)
37 }
38
39 #[derive(Default, Debug)]
40 pub struct InterfaceData {
41 socket_addrs: Vec<SocketAddr>,
42 mac: Option<[u8; 6]>,
43 }
44
45 impl InterfaceData {
46 pub fn has_ip_addr(&self, address: IpAddr) -> bool {
47 self.socket_addrs
48 .iter()
49 .any(|socket_addr| socket_addr.ip() == address)
50 }
51
52 pub fn ips(&self) -> impl Iterator<Item = IpAddr> + '_ {
53 self.socket_addrs.iter().map(|a| a.ip())
54 }
55
56 pub fn mac(&self) -> Option<[u8; 6]> {
57 self.mac
58 }
59 }
60
61 // Invariants:
62 // self.base always contains a pointer received from libc::getifaddrs that is not NULL. The region pointed to is never modified in rust code.
63 // self.next always contains either a pointer pointing to a valid ifaddr received from libc::getifaddrs or null.
64 //
65 // These invariants are setup by InterfaceIterator::new and guaranteed by drop and next, which are the only places these pointers are used.
66 struct InterfaceIterator {
67 base: *mut libc::ifaddrs,
68 next: *const libc::ifaddrs,
69 }
70
71 impl InterfaceIterator {
72 pub fn new() -> std::io::Result<Self> {
73 let mut addrs: *mut libc::ifaddrs = std::ptr::null_mut();
74
75 // Safety:
76 // addrs lives for the duration of the call to getifaddrs.
77 //
78 // Invariant preservation:
79 // we validate that the received address is not null, and
80 // by the guarantees from getifaddrs points to a valid
81 // ifaddr returned from getifaddrs
82 unsafe {
83 cerr(libc::getifaddrs(&mut addrs))?;
84
85 assert!(!addrs.is_null());
86
87 Ok(Self {
88 base: addrs,
89 next: addrs,
90 })
91 }
92 }
93 }
94
95 impl Drop for InterfaceIterator {
96 fn drop(&mut self) {
97 // Safety:
98 // By the invariants, self.base is guaranteed to point to a memory region allocated by getifaddrs
99 unsafe { libc::freeifaddrs(self.base) };
100 }
101 }
102
103 struct InterfaceDataInternal {
104 name: InterfaceName,
105 mac: Option<[u8; 6]>,
106 socket_addr: Option<SocketAddr>,
107 }
108
109 impl Iterator for InterfaceIterator {
110 type Item = InterfaceDataInternal;
111
112 fn next(&mut self) -> Option<<Self as Iterator>::Item> {
113 // Safety:
114 // By the invariants, self.next is guaranteed to be a valid pointer to an ifaddrs struct or null.
115 let ifaddr = unsafe { self.next.as_ref() }?;
116
117 // Invariant preservation
118 // By the guarantees given by getifaddrs, ifaddr.ifa_next is either null or points to a valid
119 // ifaddr.
120 self.next = ifaddr.ifa_next;
121
122 // Safety:
123 // getifaddrs guarantees that ifa_name is not null and points to a valid C string.
124 let ifname = unsafe { std::ffi::CStr::from_ptr(ifaddr.ifa_name) };
125 let name = match std::str::from_utf8(ifname.to_bytes()) {
126 Err(_) => unreachable!("interface names must be ascii"),
127 Ok(name) => InterfaceName::from_str(name).expect("name from os"),
128 };
129
130 // Safety:
131 // getifaddrs guarantees that ifa_addr either points to a valid address or is NULL.
132 let family = unsafe { ifaddr.ifa_addr.as_ref() }.map(|a| a.sa_family);
133
134 #[allow(unused)]
135 let mac: Option<[u8; 6]> = None;
136
137 #[cfg(target_os = "linux")]
138 // Safety: getifaddrs ensures that, if an address is present, it is valid. A valid address
139 // of type AF_PACKET is always reinterpret castable to sockaddr_ll, and we know an address
140 // is present since family is not None
141 let mac = if family == Some(libc::AF_PACKET as _) {
142 let sockaddr_ll: libc::sockaddr_ll =
143 unsafe { std::ptr::read_unaligned(ifaddr.ifa_addr as *const _) };
144
145 Some([
146 sockaddr_ll.sll_addr[0],
147 sockaddr_ll.sll_addr[1],
148 sockaddr_ll.sll_addr[2],
149 sockaddr_ll.sll_addr[3],
150 sockaddr_ll.sll_addr[4],
151 sockaddr_ll.sll_addr[5],
152 ])
153 } else {
154 None
155 };
156
157 #[cfg(any(target_os = "freebsd", target_os = "macos"))]
158 let mac = if family == Some(libc::AF_LINK as _) {
159 // Safety: getifaddrs ensures that, if an address is present, it is valid. A valid address
160 // of type AF_LINK is always reinterpret castable to sockaddr_ll, and we know an address
161 // is present since family is not None
162 let sockaddr_dl: libc::sockaddr_dl =
163 unsafe { std::ptr::read_unaligned(ifaddr.ifa_addr as *const _) };
164
165 // From sys/net/if_types.h in freebsd:
166 const IFT_ETHER: u8 = 0x6;
167
168 if sockaddr_dl.sdl_type == IFT_ETHER
169 && sockaddr_dl.sdl_nlen.saturating_add(6) as usize <= sockaddr_dl.sdl_data.len()
170 {
171 Some([
172 sockaddr_dl.sdl_data[sockaddr_dl.sdl_nlen as usize] as u8,
173 sockaddr_dl.sdl_data[sockaddr_dl.sdl_nlen as usize + 1] as u8,
174 sockaddr_dl.sdl_data[sockaddr_dl.sdl_nlen as usize + 2] as u8,
175 sockaddr_dl.sdl_data[sockaddr_dl.sdl_nlen as usize + 3] as u8,
176 sockaddr_dl.sdl_data[sockaddr_dl.sdl_nlen as usize + 4] as u8,
177 sockaddr_dl.sdl_data[sockaddr_dl.sdl_nlen as usize + 5] as u8,
178 ])
179 } else {
180 None
181 }
182 } else {
183 None
184 };
185
186 // Safety: ifaddr.ifa_addr is always either NULL, or by the guarantees of getifaddrs, points to a valid address.
187 let socket_addr = unsafe { sockaddr_to_socket_addr(ifaddr.ifa_addr) };
188
189 let data = InterfaceDataInternal {
190 name,
191 mac,
192 socket_addr,
193 };
194
195 Some(data)
196 }
197 }
198
199 #[derive(Clone, Copy, PartialEq, Eq, Hash)]
200 pub struct InterfaceName {
201 bytes: [u8; libc::IFNAMSIZ],
202 }
203
204 impl InterfaceName {
205 #[cfg(all(test, target_os = "linux"))]
206 pub const LOOPBACK: Self = Self {
207 bytes: *b"lo\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
208 };
209
210 #[cfg(all(test, any(target_os = "freebsd", target_os = "macos")))]
211 pub const LOOPBACK: Self = Self {
212 bytes: *b"lo0\0\0\0\0\0\0\0\0\0\0\0\0\0",
213 };
214
215 #[cfg(test)]
216 pub const INVALID: Self = Self {
217 bytes: *b"123412341234123\0",
218 };
219
220 pub fn as_str(&self) -> &str {
221 std::str::from_utf8(self.bytes.as_slice())
222 .unwrap_or_default()
223 .trim_end_matches('\0')
224 }
225
226 pub fn as_cstr(&self) -> &std::ffi::CStr {
227 // TODO: in rust 1.69.0, use
228 // std::ffi::CStr::from_bytes_until_nul(&self.bytes[..]).unwrap()
229
230 // it is an invariant of InterfaceName that the bytes are null-terminated
231 let first_null = self.bytes.iter().position(|b| *b == 0).unwrap();
232 std::ffi::CStr::from_bytes_with_nul(&self.bytes[..=first_null]).unwrap()
233 }
234
235 pub fn to_ifr_name(self) -> [libc::c_char; libc::IFNAMSIZ] {
236 let mut it = self.bytes.iter().copied();
237 [0; libc::IFNAMSIZ].map(|_| it.next().unwrap_or(0) as libc::c_char)
238 }
239
240 pub fn from_socket_addr(local_addr: SocketAddr) -> std::io::Result<Option<Self>> {
241 let matches_inferface = |interface: &InterfaceDataInternal| match interface.socket_addr {
242 None => false,
243 Some(address) => address.ip() == local_addr.ip(),
244 };
245
246 match InterfaceIterator::new()?.find(matches_inferface) {
247 Some(interface) => Ok(Some(interface.name)),
248 None => Ok(None),
249 }
250 }
251
252 pub fn get_index(&self) -> Option<libc::c_uint> {
253 // # SAFETY
254 //
255 // self lives for the duration of the call, and is null terminated.
256 match unsafe { libc::if_nametoindex(self.as_cstr().as_ptr()) } {
257 0 => None,
258 n => Some(n),
259 }
260 }
261 }
262
263 impl std::fmt::Debug for InterfaceName {
264 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
265 f.debug_tuple("InterfaceName")
266 .field(&self.as_str())
267 .finish()
268 }
269 }
270
271 impl std::fmt::Display for InterfaceName {
272 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
273 self.as_str().fmt(f)
274 }
275 }
276
277 impl std::str::FromStr for InterfaceName {
278 type Err = ();
279
280 fn from_str(s: &str) -> Result<Self, Self::Err> {
281 let mut bytes = [0; libc::IFNAMSIZ];
282
283 // >= so that we always retain a NUL byte at the end
284 if s.len() >= bytes.len() {
285 return Err(());
286 }
287
288 if s.is_empty() {
289 // this causes problems down the line when giving the interface name to tokio
290 return Err(());
291 }
292
293 let mut it = s.bytes();
294 bytes = bytes.map(|_| it.next().unwrap_or_default());
295
296 Ok(Self { bytes })
297 }
298 }
299
300 #[cfg(feature = "serde")]
301 impl<'de> serde::Deserialize<'de> for InterfaceName {
302 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
303 where
304 D: serde::Deserializer<'de>,
305 {
306 let s = String::deserialize(deserializer)?;
307 FromStr::from_str(&s).map_err(|_| serde::de::Error::custom("invalid interface name"))
308 }
309 }
310
311 /// Convert a libc::sockaddr to a rust std::net::SocketAddr
312 ///
313 /// # Safety
314 ///
315 /// This function assumes that sockaddr is either NULL or points to a valid address.
316 unsafe fn sockaddr_to_socket_addr(sockaddr: *const libc::sockaddr) -> Option<SocketAddr> {
317 // Most (but not all) of the fields in a socket addr are in network byte
318 // ordering. As such, when doing conversions here, we should start from the
319 // NATIVE byte representation, as this will actualy be the big-endian
320 // representation of the underlying value regardless of platform.
321
322 // Check for null pointers
323 if sockaddr.is_null() {
324 return None;
325 }
326
327 // Safety: by the previous check, sockaddr is not NULL and hence points to a valid address
328 match unsafe { (*sockaddr).sa_family as libc::c_int } {
329 libc::AF_INET => {
330 // SAFETY: we cast from a libc::sockaddr (alignment 2) to a libc::sockaddr_in (alignment 4)
331 // that means that the pointer is now potentially unaligned. We must used read_unaligned!
332 // However, the rest of the cast is safe as a valid AF_INET address is always reinterpret castable
333 // as a sockaddr_in
334 let inaddr: libc::sockaddr_in =
335 unsafe { std::ptr::read_unaligned(sockaddr as *const libc::sockaddr_in) };
336
337 let socketaddr = std::net::SocketAddrV4::new(
338 std::net::Ipv4Addr::from(inaddr.sin_addr.s_addr.to_ne_bytes()),
339 u16::from_be_bytes(inaddr.sin_port.to_ne_bytes()),
340 );
341
342 Some(std::net::SocketAddr::V4(socketaddr))
343 }
344 libc::AF_INET6 => {
345 // SAFETY: we cast from a libc::sockaddr (alignment 2) to a libc::sockaddr_in6 (alignment 4)
346 // that means that the pointer is now potentially unaligned. We must used read_unaligned!
347 // However, the cast is safe as a valid AF_INET6 address is always reinterpret catable as a sockaddr_in6
348 let inaddr: libc::sockaddr_in6 =
349 unsafe { std::ptr::read_unaligned(sockaddr as *const libc::sockaddr_in6) };
350
351 // Safety:
352 // sin_addr lives for the duration fo the call and matches type
353 let sin_addr = inaddr.sin6_addr.s6_addr;
354 let segment_bytes: [u8; 16] =
355 unsafe { std::ptr::read_unaligned(&sin_addr as *const _ as *const _) };
356
357 let socketaddr = std::net::SocketAddrV6::new(
358 std::net::Ipv6Addr::from(segment_bytes),
359 u16::from_be_bytes(inaddr.sin6_port.to_ne_bytes()),
360 inaddr.sin6_flowinfo, /* NOTE: Despite network byte order, no conversion is needed (see https://github.com/rust-lang/rust/issues/101605) */
361 inaddr.sin6_scope_id,
362 );
363
364 Some(std::net::SocketAddr::V6(socketaddr))
365 }
366 _ => None,
367 }
368 }
369
370 #[cfg(test)]
371 mod tests {
372 use std::net::Ipv4Addr;
373
374 use super::*;
375
376 #[test]
377 fn interface_name_from_string() {
378 assert!(InterfaceName::from_str("").is_err());
379 assert!(InterfaceName::from_str("a string that is too long").is_err());
380
381 let input = "enp0s31f6";
382 assert_eq!(InterfaceName::from_str(input).unwrap().as_str(), input);
383
384 let ifr_name = (*b"enp0s31f6\0\0\0\0\0\0\0").map(|b| b as libc::c_char);
385 assert_eq!(
386 InterfaceName::from_str(input).unwrap().to_ifr_name(),
387 ifr_name
388 );
389 }
390
391 #[test]
392 fn test_mac_address_iterator() {
393 let v: Vec<_> = InterfaceIterator::new()
394 .unwrap()
395 .filter_map(|d| d.mac)
396 .collect();
397
398 assert!(!v.is_empty());
399 }
400
401 #[test]
402 fn test_interface_name_iterator() {
403 let v: Vec<_> = InterfaceIterator::new().unwrap().map(|d| d.name).collect();
404
405 assert!(v.contains(&InterfaceName::LOOPBACK));
406 }
407
408 #[test]
409 fn test_socket_addr_iterator() {
410 let v: Vec<_> = InterfaceIterator::new()
411 .unwrap()
412 .filter_map(|d| d.socket_addr)
413 .collect();
414
415 let localhost_0 = SocketAddr::from((Ipv4Addr::LOCALHOST, 0));
416
417 assert!(v.contains(&localhost_0));
418 }
419
420 #[test]
421 fn interface_index_ipv4() {
422 assert!(InterfaceName::LOOPBACK.get_index().is_some());
423 }
424
425 #[test]
426 fn interface_index_ipv6() {
427 assert!(InterfaceName::LOOPBACK.get_index().is_some());
428 }
429
430 #[test]
431 fn interface_index_invalid() {
432 assert!(InterfaceName::INVALID.get_index().is_none());
433 }
434 }