sysinfo/unix/
network_helper.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use crate::common::MacAddr;
4use std::ptr::null_mut;
5
6/// This iterator yields an interface name and address.
7pub(crate) struct InterfaceAddressIterator {
8    /// Pointer to the current `ifaddrs` struct.
9    ifap: *mut libc::ifaddrs,
10    /// Pointer to the first element in linked list.
11    buf: *mut libc::ifaddrs,
12}
13
14impl Iterator for InterfaceAddressIterator {
15    type Item = (String, MacAddr);
16
17    fn next(&mut self) -> Option<Self::Item> {
18        unsafe {
19            while !self.ifap.is_null() {
20                // advance the pointer until a MAC address is found
21                let ifap = self.ifap;
22                self.ifap = (*ifap).ifa_next;
23
24                if let Some(addr) = parse_interface_address(ifap) {
25                    let ifa_name = (*ifap).ifa_name;
26                    if ifa_name.is_null() {
27                        continue;
28                    }
29                    // libc::IFNAMSIZ + 6
30                    // This size refers to ./apple/network.rs:75
31                    let mut name = vec![0u8; libc::IFNAMSIZ + 6];
32                    libc::strcpy(name.as_mut_ptr() as _, (*ifap).ifa_name);
33                    name.set_len(libc::strlen((*ifap).ifa_name));
34                    let name = String::from_utf8_unchecked(name);
35
36                    return Some((name, addr));
37                }
38            }
39            None
40        }
41    }
42}
43
44impl Drop for InterfaceAddressIterator {
45    fn drop(&mut self) {
46        unsafe {
47            libc::freeifaddrs(self.buf);
48        }
49    }
50}
51
52#[cfg(any(target_os = "macos", target_os = "freebsd", target_os = "ios"))]
53impl From<&libc::sockaddr_dl> for MacAddr {
54    fn from(value: &libc::sockaddr_dl) -> Self {
55        let sdl_data = value.sdl_data;
56        // interface name length, NO trailing 0
57        let sdl_nlen = value.sdl_nlen as usize;
58        // make sure that it is never out of bound
59        if sdl_nlen + 5 < 12 {
60            MacAddr([
61                sdl_data[sdl_nlen] as u8,
62                sdl_data[sdl_nlen + 1] as u8,
63                sdl_data[sdl_nlen + 2] as u8,
64                sdl_data[sdl_nlen + 3] as u8,
65                sdl_data[sdl_nlen + 4] as u8,
66                sdl_data[sdl_nlen + 5] as u8,
67            ])
68        } else {
69            MacAddr::UNSPECIFIED
70        }
71    }
72}
73
74#[cfg(any(target_os = "macos", target_os = "freebsd", target_os = "ios"))]
75unsafe fn parse_interface_address(ifap: *const libc::ifaddrs) -> Option<MacAddr> {
76    let sock_addr = (*ifap).ifa_addr;
77    if sock_addr.is_null() {
78        return None;
79    }
80    match (*sock_addr).sa_family as libc::c_int {
81        libc::AF_LINK => {
82            let addr = sock_addr as *const libc::sockaddr_dl;
83            Some(MacAddr::from(&*addr))
84        }
85        _ => None,
86    }
87}
88
89#[cfg(any(target_os = "linux", target_os = "android"))]
90unsafe fn parse_interface_address(ifap: *const libc::ifaddrs) -> Option<MacAddr> {
91    use libc::sockaddr_ll;
92
93    let sock_addr = (*ifap).ifa_addr;
94    if sock_addr.is_null() {
95        return None;
96    }
97    match (*sock_addr).sa_family as libc::c_int {
98        libc::AF_PACKET => {
99            let addr = sock_addr as *const sockaddr_ll;
100            // Take the first 6 bytes
101            let [addr @ .., _, _] = (*addr).sll_addr;
102            Some(MacAddr(addr))
103        }
104        _ => None,
105    }
106}
107
108/// Return an iterator on (interface_name, address) pairs
109pub(crate) fn get_interface_address() -> Result<InterfaceAddressIterator, String> {
110    let mut ifap = null_mut();
111    unsafe {
112        if retry_eintr!(libc::getifaddrs(&mut ifap)) == 0 && !ifap.is_null() {
113            Ok(InterfaceAddressIterator { ifap, buf: ifap })
114        } else {
115            Err("failed to call getifaddrs()".to_string())
116        }
117    }
118}