1use cfg_if::cfg_if;
7#[cfg(apple_targets)]
8use std::convert::TryFrom;
9use std::ffi;
10use std::iter::Iterator;
11use std::mem;
12use std::option::Option;
13
14use crate::net::if_::*;
15use crate::sys::socket::{SockaddrLike, SockaddrStorage};
16use crate::{Errno, Result};
17
18#[derive(Clone, Debug, Eq, Hash, PartialEq)]
20pub struct InterfaceAddress {
21 pub interface_name: String,
23 pub flags: InterfaceFlags,
25 pub address: Option<SockaddrStorage>,
27 pub netmask: Option<SockaddrStorage>,
29 pub broadcast: Option<SockaddrStorage>,
31 pub destination: Option<SockaddrStorage>,
33}
34
35cfg_if! {
36 if #[cfg(any(linux_android, target_os = "emscripten", target_os = "fuchsia"))] {
37 fn get_ifu_from_sockaddr(info: &libc::ifaddrs) -> *const libc::sockaddr {
38 info.ifa_ifu
39 }
40 } else {
41 fn get_ifu_from_sockaddr(info: &libc::ifaddrs) -> *const libc::sockaddr {
42 info.ifa_dstaddr
43 }
44 }
45}
46
47#[cfg(apple_targets)]
57unsafe fn workaround_xnu_bug(info: &libc::ifaddrs) -> Option<SockaddrStorage> {
58 let src_sock = info.ifa_netmask;
59 if src_sock.is_null() {
60 return None;
61 }
62
63 let mut dst_sock = mem::MaybeUninit::<libc::sockaddr_storage>::zeroed();
64
65 let dst_sock = unsafe {
66 std::ptr::copy_nonoverlapping(
68 src_sock as *const u8,
69 dst_sock.as_mut_ptr().cast(),
70 (*src_sock).sa_len.into(),
71 );
72
73 (*dst_sock.as_mut_ptr()).ss_len =
75 u8::try_from(mem::size_of::<libc::sockaddr_storage>()).unwrap();
76 dst_sock.assume_init()
77 };
78
79 let dst_sock_ptr =
80 &dst_sock as *const libc::sockaddr_storage as *const libc::sockaddr;
81
82 unsafe { SockaddrStorage::from_raw(dst_sock_ptr, None) }
83}
84
85impl InterfaceAddress {
86 fn from_libc_ifaddrs(info: &libc::ifaddrs) -> InterfaceAddress {
88 let ifname = unsafe { ffi::CStr::from_ptr(info.ifa_name) };
89 let address = unsafe { SockaddrStorage::from_raw(info.ifa_addr, None) };
90 #[cfg(apple_targets)]
91 let netmask = unsafe { workaround_xnu_bug(info) };
92 #[cfg(not(apple_targets))]
93 let netmask =
94 unsafe { SockaddrStorage::from_raw(info.ifa_netmask, None) };
95 let mut addr = InterfaceAddress {
96 interface_name: ifname.to_string_lossy().to_string(),
97 flags: InterfaceFlags::from_bits_truncate(
98 info.ifa_flags as IflagsType,
99 ),
100 address,
101 netmask,
102 broadcast: None,
103 destination: None,
104 };
105
106 let ifu = get_ifu_from_sockaddr(info);
107 if addr.flags.contains(InterfaceFlags::IFF_POINTOPOINT) {
108 addr.destination = unsafe { SockaddrStorage::from_raw(ifu, None) };
109 } else if addr.flags.contains(InterfaceFlags::IFF_BROADCAST) {
110 addr.broadcast = unsafe { SockaddrStorage::from_raw(ifu, None) };
111 }
112
113 addr
114 }
115}
116
117#[derive(Debug, Eq, Hash, PartialEq)]
123pub struct InterfaceAddressIterator {
124 base: *mut libc::ifaddrs,
125 next: *mut libc::ifaddrs,
126}
127
128impl Drop for InterfaceAddressIterator {
129 fn drop(&mut self) {
130 unsafe { libc::freeifaddrs(self.base) };
131 }
132}
133
134impl Iterator for InterfaceAddressIterator {
135 type Item = InterfaceAddress;
136 fn next(&mut self) -> Option<<Self as Iterator>::Item> {
137 match unsafe { self.next.as_ref() } {
138 Some(ifaddr) => {
139 self.next = ifaddr.ifa_next;
140 Some(InterfaceAddress::from_libc_ifaddrs(ifaddr))
141 }
142 None => None,
143 }
144 }
145}
146
147pub fn getifaddrs() -> Result<InterfaceAddressIterator> {
173 let mut addrs = mem::MaybeUninit::<*mut libc::ifaddrs>::uninit();
174 unsafe {
175 Errno::result(libc::getifaddrs(addrs.as_mut_ptr())).map(|_| {
176 InterfaceAddressIterator {
177 base: addrs.assume_init(),
178 next: addrs.assume_init(),
179 }
180 })
181 }
182}
183
184#[cfg(test)]
185mod tests {
186 use super::*;
187
188 #[test]
190 fn test_getifaddrs() {
191 let _ = getifaddrs();
192 }
193
194 #[test]
197 fn test_getifaddrs_netmask_correct() {
198 let addrs = getifaddrs().unwrap();
199 for iface in addrs {
200 let sock = if let Some(sock) = iface.netmask {
201 sock
202 } else {
203 continue;
204 };
205 if sock.family() == Some(crate::sys::socket::AddressFamily::Inet) {
206 let _ = sock.as_sockaddr_in().unwrap();
207 return;
208 } else if sock.family()
209 == Some(crate::sys::socket::AddressFamily::Inet6)
210 {
211 let _ = sock.as_sockaddr_in6().unwrap();
212 return;
213 }
214 }
215 panic!("No address?");
216 }
217}