Debian Codegraph @

src/interface.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{
2 collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#,
3 netrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/::{IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#, SocketAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/socket_addr/SocketAddr#},
4 strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#,
5 };
6
7 use superrust-analyzer cargo timestamped-socket 0.2.2 crate/::cerrrust-analyzer cargo timestamped-socket 0.2.2 cerr().;
8
9 #[cfg(target_os = "linux")]
10 mod linuxrust-analyzer cargo timestamped-socket 0.2.2 interface/linux/;
11 #[cfg(target_os = "linux")]
12 pub use linuxrust-analyzer cargo timestamped-socket 0.2.2 interface/linux/::ChangeDetectorrust-analyzer cargo timestamped-socket 0.2.2 interface/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 interfacesrust-analyzer cargo timestamped-socket 0.2.2 interface/interfaces().() -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<InterfaceNamerust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceName#, InterfaceDatarust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceData#>> {
26 let mut elements = HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#::defaultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S>`][Default]default().();
27
28 for data in InterfaceIteratorrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceIterator#::newrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceIterator]new().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). {
29 let current: &mut InterfaceDatarust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceData# = elements.entryrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]entry().(data.namerust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceDataInternal#name.).or_defaultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`Entry<'a, K, V>`]or_default().();
30
31 current.socket_addrsrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceData#socket_addrs..extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(data.socket_addrrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceDataInternal#socket_addr.);
32 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().(current.macrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceData#mac..is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() && data.macrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceDataInternal#mac..is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().()));
33 current.macrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceData#mac. = current.macrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceData#mac..orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]or().(data.macrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceDataInternal#mac.);
34 }
35
36 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(elements)
37 }
38
39 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
40 pub struct InterfaceDatarust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceData# {
41 socket_addrsrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceData#socket_addrs.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<SocketAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/socket_addr/SocketAddr#>,
42 macrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceData#mac.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<[u8; 6]>,
43 }
44
45 impl InterfaceDatarust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceData# {
46 pub fn has_ip_addrrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceData]has_ip_addr().(&self, address: IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#) -> bool {
47 self.socket_addrsrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceData#socket_addrs.
48 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
49 .anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]any().(|socket_addr| socket_addr.iprust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/socket_addr/impl#[SocketAddr]ip().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/impl#[IpAddr][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/impl#[IpAddr][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/impl#[IpAddr][`PartialEq<Self>`]eq().address)
50 }
51
52 pub fn ipsrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceData]ips().(&self) -> impl Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#> + '_ {
53 self.socket_addrsrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceData#socket_addrs..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|a| a.iprust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/socket_addr/impl#[SocketAddr]ip().())
54 }
55
56 pub fn macrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceData]mac().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<[u8; 6]> {
57 self.macrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceData#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 InterfaceIteratorrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceIterator# {
67 baserust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceIterator#base.: *mut libcrust-analyzer cargo libc 0.2.152 crate/::ifaddrsrust-analyzer cargo libc 0.2.152 unix/linux_like/ifaddrs#,
68 nextrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceIterator#next.: *const libcrust-analyzer cargo libc 0.2.152 crate/::ifaddrsrust-analyzer cargo libc 0.2.152 unix/linux_like/ifaddrs#,
69 }
70
71 impl InterfaceIteratorrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceIterator# {
72 pub fn newrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceIterator]new().() -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Selfrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceIterator]> {
73 let mut addrs: *mut libcrust-analyzer cargo libc 0.2.152 crate/::ifaddrsrust-analyzer cargo libc 0.2.152 unix/linux_like/ifaddrs# = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::null_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core 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 cerrrust-analyzer cargo timestamped-socket 0.2.2 cerr().(libcrust-analyzer cargo libc 0.2.152 crate/::getifaddrsrust-analyzer cargo libc 0.2.152 getifaddrs().(&mut addrs))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
84
85 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().addrs.is_nullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]is_null().());
86
87 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceIterator] {
88 baserust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceIterator#base.: addrs,
89 nextrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceIterator#next.: addrs,
90 })
91 }
92 }
93 }
94
95 impl Droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/drop/Drop# for InterfaceIteratorrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceIterator# {
96 fn droprust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceIterator][Drop]drop().(&mut self) {
97 // Safety:
98 // By the invariants, self.base is guaranteed to point to a memory region allocated by getifaddrs
99 unsafe { libcrust-analyzer cargo libc 0.2.152 crate/::freeifaddrsrust-analyzer cargo libc 0.2.152 freeifaddrs().(self.baserust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceIterator#base.) };
100 }
101 }
102
103 struct InterfaceDataInternalrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceDataInternal# {
104 namerust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceDataInternal#name.: InterfaceNamerust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceName#,
105 macrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceDataInternal#mac.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<[u8; 6]>,
106 socket_addrrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceDataInternal#socket_addr.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<SocketAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/socket_addr/SocketAddr#>,
107 }
108
109 impl Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# for InterfaceIteratorrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceIterator# {
110 type Itemrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceIterator][Iterator]Item# = InterfaceDataInternalrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceDataInternal#;
111
112 fn nextrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceIterator][Iterator]next().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<<Selfrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceIterator][Iterator] as Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#>::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/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.nextrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceIterator#next..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/const_ptr/impl#[`*const T`]as_ref().() }?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().;
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.nextrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceIterator#next. = ifaddr.ifa_nextrust-analyzer cargo libc 0.2.152 unix/linux_like/ifaddrs#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 { stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr#::from_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]from_ptr().(ifaddr.ifa_namerust-analyzer cargo libc 0.2.152 unix/linux_like/ifaddrs#ifa_name.) };
125 let name = match stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::from_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8().(ifname.to_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]to_bytes().()) {
126 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => unreachablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/unreachable!!("interface names must be ascii"),
127 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(name) => InterfaceNamerust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceName#::from_strrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName][FromStr]from_str().(name).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]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_addrrust-analyzer cargo libc 0.2.152 unix/linux_like/ifaddrs#ifa_addr..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]as_ref().() }.maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|a| a.sa_familyrust-analyzer cargo libc 0.2.152 unix/linux_like/sockaddr#sa_family.);
133
134 #[allow(unused)]
135 let mac: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<[u8; 6]> = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#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 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq().Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(libcrust-analyzer cargo libc 0.2.152 crate/::AF_PACKETrust-analyzer cargo libc 0.2.152 unix/linux_like/AF_PACKET. as _) {
142 let sockaddr_ll: libcrust-analyzer cargo libc 0.2.152 crate/::sockaddr_llrust-analyzer cargo libc 0.2.152 unix/linux_like/sockaddr_ll# =
143 unsafe { stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::read_unalignedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/read_unaligned().(ifaddr.ifa_addrrust-analyzer cargo libc 0.2.152 unix/linux_like/ifaddrs#ifa_addr. as *const _) };
144
145 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#([
146 sockaddr_ll.sll_addrrust-analyzer cargo libc 0.2.152 unix/linux_like/sockaddr_ll#sll_addr.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().,
147 sockaddr_ll.sll_addrrust-analyzer cargo libc 0.2.152 unix/linux_like/sockaddr_ll#sll_addr.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().,
148 sockaddr_ll.sll_addrrust-analyzer cargo libc 0.2.152 unix/linux_like/sockaddr_ll#sll_addr.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().,
149 sockaddr_ll.sll_addrrust-analyzer cargo libc 0.2.152 unix/linux_like/sockaddr_ll#sll_addr.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().3]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().,
150 sockaddr_ll.sll_addrrust-analyzer cargo libc 0.2.152 unix/linux_like/sockaddr_ll#sll_addr.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().4]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().,
151 sockaddr_ll.sll_addrrust-analyzer cargo libc 0.2.152 unix/linux_like/sockaddr_ll#sll_addr.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().5]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().,
152 ])
153 } else {
154 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
155 };
156
157 #[cfg(any(target_os = "freebsd", target_os = "macos"))]
158 let mac = if family == Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(libcrust-analyzer cargo libc 0.2.152 crate/::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: libcrust-analyzer cargo libc 0.2.152 crate/::sockaddr_dl =
163 unsafe { stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::read_unalignedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core 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 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#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 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
181 }
182 } else {
183 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#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_addrrust-analyzer cargo timestamped-socket 0.2.2 interface/sockaddr_to_socket_addr().(ifaddr.ifa_addrrust-analyzer cargo libc 0.2.152 unix/linux_like/ifaddrs#ifa_addr.) };
188
189 let data = InterfaceDataInternalrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceDataInternal# {
190 namerust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceDataInternal#name.,
191 macrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceDataInternal#mac.,
192 socket_addrrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceDataInternal#socket_addr.,
193 };
194
195 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(data)
196 }
197 }
198
199 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:)]
200 pub struct InterfaceNamerust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceName# {
201 bytesrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceName#bytes.: [u8; libcrust-analyzer cargo libc 0.2.152 crate/::IFNAMSIZrust-analyzer cargo libc 0.2.152 unix/IFNAMSIZ.],
202 }
203
204 impl InterfaceNamerust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceName# {
205 #[cfg(all(test, target_os = "linux"))]
206 pub const LOOPBACKrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName]LOOPBACK.: Selfrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName] = Selfrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName] {
207 bytesrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceName#bytes.: *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().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: Selfrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName] = Selfrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName] {
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 INVALIDrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName]INVALID.: Selfrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName] = Selfrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName] {
217 bytesrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceName#bytes.: *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().b"123412341234123\0",
218 };
219
220 pub fn as_strrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName]as_str().(&self) -> &str {
221 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::from_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8().(self.bytesrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceName#bytes..as_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`]as_slice().())
222 .unwrap_or_defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or_default().()
223 .trim_end_matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_end_matches().('\0')
224 }
225
226 pub fn as_cstrrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName]as_cstr().(&self) -> &stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/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.bytesrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceName#bytes..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().positionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]position().(|b| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().b rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().0).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
232 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr#::from_bytes_with_nulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]from_bytes_with_nul().(&self.bytesrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceName#bytes.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()...=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeToInclusive#first_null]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()
233 }
234
235 pub fn to_ifr_namerust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName]to_ifr_name().(self) -> [libcrust-analyzer cargo libc 0.2.152 crate/::c_charrust-analyzer cargo libc 0.2.152 unix/linux_like/linux/gnu/b64/x86_64/c_char#; libcrust-analyzer cargo libc 0.2.152 crate/::IFNAMSIZrust-analyzer cargo libc 0.2.152 unix/IFNAMSIZ.] {
236 let mut it = self.bytesrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceName#bytes..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().copiedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#copied().();
237 [0; libcrust-analyzer cargo libc 0.2.152 crate/::IFNAMSIZrust-analyzer cargo libc 0.2.152 unix/IFNAMSIZ.].maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`]map().(|_| it.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/copied/impl#[`Copied<I>`][Iterator]next().().unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(0) as libcrust-analyzer cargo libc 0.2.152 crate/::c_charrust-analyzer cargo libc 0.2.152 unix/linux_like/linux/gnu/b64/x86_64/c_char#)
238 }
239
240 pub fn from_socket_addrrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName]from_socket_addr().(local_addr: SocketAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/socket_addr/SocketAddr#) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName]>> {
241 let matches_inferface = |interface: &InterfaceDataInternalrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceDataInternal#| match interface.socket_addrrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceDataInternal#socket_addr. {
242 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => false,
243 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(address) => address.iprust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/socket_addr/impl#[SocketAddr]ip().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/impl#[IpAddr][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/impl#[IpAddr][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/impl#[IpAddr][`PartialEq<Self>`]eq().local_addr.iprust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/socket_addr/impl#[SocketAddr]ip().(),
244 };
245
246 match InterfaceIteratorrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceIterator#::newrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceIterator]new().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#find().(matches_inferface) {
247 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(interface) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(interface.namerust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceDataInternal#name.)),
248 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#),
249 }
250 }
251
252 pub fn get_indexrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName]get_index().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<libcrust-analyzer cargo libc 0.2.152 crate/::c_uintrust-analyzer cargo libc 0.2.152 unix/c_uint#> {
253 // # SAFETY
254 //
255 // self lives for the duration of the call, and is null terminated.
256 match unsafe { libcrust-analyzer cargo libc 0.2.152 crate/::if_nametoindexrust-analyzer cargo libc 0.2.152 if_nametoindex().(self.as_cstrrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName]as_cstr().().as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]as_ptr().()) } {
257 0 => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
258 n => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(n),
259 }
260 }
261 }
262
263 impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for InterfaceNamerust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceName# {
264 fn fmtrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName][Debug]fmt().(&self, f: &mut stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
265 f.debug_tuplerust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]debug_tuple().("InterfaceName")
266 .fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugTuple<'a, 'b>`]field().(&self.as_strrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName]as_str().())
267 .finishrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugTuple<'a, 'b>`]finish().()
268 }
269 }
270
271 impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for InterfaceNamerust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceName# {
272 fn fmtrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName][Display]fmt().(&self, f: &mut stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
273 self.as_strrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName]as_str().().fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[str][Display]fmt().(f)
274 }
275 }
276
277 impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr# for InterfaceNamerust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceName# {
278 type Errrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName][FromStr]Err# = ();
279
280 fn from_strrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName][FromStr]from_str().(s: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName][FromStr], Selfrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName][FromStr]::Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#Err#> {
281 let mut bytes = [0; libcrust-analyzer cargo libc 0.2.152 crate/::IFNAMSIZrust-analyzer cargo libc 0.2.152 unix/IFNAMSIZ.];
282
283 // >= so that we always retain a NUL byte at the end
284 if s.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() {
285 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(());
286 }
287
288 if s.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() {
289 // this causes problems down the line when giving the interface name to tokio
290 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(());
291 }
292
293 let mut it = s.bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]bytes().();
294 bytes = bytes.maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`]map().(|_| it.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Bytes<'_>`][Iterator]next().().unwrap_or_defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_default().());
295
296 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName][FromStr] { bytesrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceName#bytes. })
297 }
298 }
299
300 #[cfg(feature = "serde")]
301 impl<'de> serderust-analyzer cargo serde 1.0.188 crate/::Deserializerust-analyzer cargo serde 1.0.188 de/Deserialize#<'de> for InterfaceNamerust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceName# {
302 fn deserializerust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName][`Deserialize<'de>`]deserialize().<D>(deserializer: D) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName][`Deserialize<'de>`], D::Errorrust-analyzer cargo serde 1.0.188 de/Deserializer#Error#>
303 where
304 D: serderust-analyzer cargo serde 1.0.188 crate/::Deserializerrust-analyzer cargo serde 1.0.188 de/Deserializer#<'de>,
305 {
306 let s = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::deserializerust-analyzer cargo serde 1.0.188 de/impls/impl#[String][`Deserialize<'de>`]deserialize().(deserializer)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
307 FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#::from_strrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName][FromStr]from_str().(&s).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|_| serderust-analyzer cargo serde 1.0.188 crate/::derust-analyzer cargo serde 1.0.188 de/::Errorrust-analyzer cargo serde 1.0.188 de/Error#::customrust-analyzer cargo serde 1.0.188 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_addrrust-analyzer cargo timestamped-socket 0.2.2 interface/sockaddr_to_socket_addr().(sockaddr: *const libcrust-analyzer cargo libc 0.2.152 crate/::sockaddrrust-analyzer cargo libc 0.2.152 unix/linux_like/sockaddr#) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<SocketAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/socket_addr/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_nullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/const_ptr/impl#[`*const T`]is_null().() {
324 return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
325 }
326
327 // Safety: by the previous check, sockaddr is not NULL and hence points to a valid address
328 match unsafe { (*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().sockaddr).sa_familyrust-analyzer cargo libc 0.2.152 unix/linux_like/sockaddr#sa_family. as libcrust-analyzer cargo libc 0.2.152 crate/::c_intrust-analyzer cargo libc 0.2.152 unix/c_int# } {
329 libcrust-analyzer cargo libc 0.2.152 crate/::AF_INETrust-analyzer cargo libc 0.2.152 unix/linux_like/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: libcrust-analyzer cargo libc 0.2.152 crate/::sockaddr_inrust-analyzer cargo libc 0.2.152 unix/linux_like/sockaddr_in# =
335 unsafe { stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::read_unalignedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/read_unaligned().(sockaddr as *const libcrust-analyzer cargo libc 0.2.152 crate/::sockaddr_inrust-analyzer cargo libc 0.2.152 unix/linux_like/sockaddr_in#) };
336
337 let socketaddr = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::netrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/::SocketAddrV4rust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/socket_addr/SocketAddrV4#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/socket_addr/impl#[SocketAddrV4]new().(
338 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::netrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/::Ipv4Addrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/Ipv4Addr#::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#from().(inaddr.sin_addrrust-analyzer cargo libc 0.2.152 unix/linux_like/sockaddr_in#sin_addr..s_addrrust-analyzer cargo libc 0.2.152 unix/linux_like/in_addr#s_addr..to_ne_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]to_ne_bytes().()),
339 u16::from_be_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u16]from_be_bytes().(inaddr.sin_portrust-analyzer cargo libc 0.2.152 unix/linux_like/sockaddr_in#sin_port..to_ne_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u16]to_ne_bytes().()),
340 );
341
342 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::netrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/::SocketAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/socket_addr/SocketAddr#::V4rust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/socket_addr/SocketAddr#V4#(socketaddr))
343 }
344 libcrust-analyzer cargo libc 0.2.152 crate/::AF_INET6rust-analyzer cargo libc 0.2.152 unix/linux_like/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: libcrust-analyzer cargo libc 0.2.152 crate/::sockaddr_in6rust-analyzer cargo libc 0.2.152 unix/linux_like/sockaddr_in6# =
349 unsafe { stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::read_unalignedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/read_unaligned().(sockaddr as *const libcrust-analyzer cargo libc 0.2.152 crate/::sockaddr_in6rust-analyzer cargo libc 0.2.152 unix/linux_like/sockaddr_in6#) };
350
351 // Safety:
352 // sin_addr lives for the duration fo the call and matches type
353 let sin_addr = inaddr.sin6_addrrust-analyzer cargo libc 0.2.152 unix/linux_like/sockaddr_in6#sin6_addr..s6_addrrust-analyzer cargo libc 0.2.152 unix/align/in6_addr#s6_addr.;
354 let segment_bytes: [u8; 16] =
355 unsafe { stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::read_unalignedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/read_unaligned().(&sin_addr as *const _ as *const _) };
356
357 let socketaddr = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::netrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/::SocketAddrV6rust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/socket_addr/SocketAddrV6#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/socket_addr/impl#[SocketAddrV6]new().(
358 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::netrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/::Ipv6Addrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/Ipv6Addr#::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/impl#[Ipv6Addr][`From<[u8; 16]>`]from().(segment_bytes),
359 u16::from_be_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u16]from_be_bytes().(inaddr.sin6_portrust-analyzer cargo libc 0.2.152 unix/linux_like/sockaddr_in6#sin6_port..to_ne_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u16]to_ne_bytes().()),
360 inaddr.sin6_flowinforust-analyzer cargo libc 0.2.152 unix/linux_like/sockaddr_in6#sin6_flowinfo., /* NOTE: Despite network byte order, no conversion is needed (see https://github.com/rust-lang/rust/issues/101605) */
361 inaddr.sin6_scope_idrust-analyzer cargo libc 0.2.152 unix/linux_like/sockaddr_in6#sin6_scope_id.,
362 );
363
364 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::netrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/::SocketAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/socket_addr/SocketAddr#::V6rust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/socket_addr/SocketAddr#V6#(socketaddr))
365 }
366 _ => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
367 }
368 }
369
370 #[cfg(test)]
371 mod testsrust-analyzer cargo timestamped-socket 0.2.2 interface/tests/ {
372 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::netrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/::Ipv4Addrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/Ipv4Addr#;
373
374 use superrust-analyzer cargo timestamped-socket 0.2.2 interface/::*;
375
376 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
377 fn interface_name_from_stringrust-analyzer cargo timestamped-socket 0.2.2 interface/tests/interface_name_from_string().() {
378 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(InterfaceNamerust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceName#::from_strrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName][FromStr]from_str().("").is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().());
379 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(InterfaceNamerust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceName#::from_strrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName][FromStr]from_str().("a string that is too long").is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().());
380
381 let input = "enp0s31f6";
382 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(InterfaceNamerust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceName#::from_strrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName][FromStr]from_str().(input).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().as_strrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName]as_str().(), input);
383
384 let ifr_name = (*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().b"enp0s31f6\0\0\0\0\0\0\0").maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`]map().(|b| b as libcrust-analyzer cargo libc 0.2.152 crate/::c_charrust-analyzer cargo libc 0.2.152 unix/linux_like/linux/gnu/b64/x86_64/c_char#);
385 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
386 InterfaceNamerust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceName#::from_strrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName][FromStr]from_str().(input).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().to_ifr_namerust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName]to_ifr_name().(),
387 ifr_name
388 );
389 }
390
391 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
392 fn test_mac_address_iteratorrust-analyzer cargo timestamped-socket 0.2.2 interface/tests/test_mac_address_iterator().() {
393 let v: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = InterfaceIteratorrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceIterator#::newrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceIterator]new().()
394 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()
395 .filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(|d| d.macrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceDataInternal#mac.)
396 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
397
398 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().v.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().());
399 }
400
401 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
402 fn test_interface_name_iteratorrust-analyzer cargo timestamped-socket 0.2.2 interface/tests/test_interface_name_iterator().() {
403 let v: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = InterfaceIteratorrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceIterator#::newrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceIterator]new().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|d| d.namerust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceDataInternal#name.).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
404
405 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(v.containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]contains().(&InterfaceNamerust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceName#::LOOPBACKrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName]LOOPBACK.));
406 }
407
408 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
409 fn test_socket_addr_iteratorrust-analyzer cargo timestamped-socket 0.2.2 interface/tests/test_socket_addr_iterator().() {
410 let v: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = InterfaceIteratorrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceIterator#::newrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceIterator]new().()
411 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()
412 .filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(|d| d.socket_addrrust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceDataInternal#socket_addr.)
413 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
414
415 let localhost_0 = SocketAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/socket_addr/SocketAddr#::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/socket_addr/impl#[SocketAddr][`From<(I, u16)>`]from().((Ipv4Addrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/Ipv4Addr#::LOCALHOSTrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/impl#[Ipv4Addr]LOCALHOST., 0));
416
417 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(v.containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]contains().(&localhost_0));
418 }
419
420 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
421 fn interface_index_ipv4rust-analyzer cargo timestamped-socket 0.2.2 interface/tests/interface_index_ipv4().() {
422 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(InterfaceNamerust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceName#::LOOPBACKrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName]LOOPBACK..get_indexrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName]get_index().().is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().());
423 }
424
425 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
426 fn interface_index_ipv6rust-analyzer cargo timestamped-socket 0.2.2 interface/tests/interface_index_ipv6().() {
427 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(InterfaceNamerust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceName#::LOOPBACKrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName]LOOPBACK..get_indexrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName]get_index().().is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().());
428 }
429
430 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
431 fn interface_index_invalidrust-analyzer cargo timestamped-socket 0.2.2 interface/tests/interface_index_invalid().() {
432 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(InterfaceNamerust-analyzer cargo timestamped-socket 0.2.2 interface/InterfaceName#::INVALIDrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName]INVALID..get_indexrust-analyzer cargo timestamped-socket 0.2.2 interface/impl#[InterfaceName]get_index().().is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().());
433 }
434 }