open_net/api/network/
lan_ipv4.rs1use std::net::Ipv4Addr;
4use std::time::SystemTime;
5
6#[non_exhaustive]
8#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
9pub enum LanInterfaceKind {
10 Wifi,
11 Ethernet,
12 Bridge,
13 Tunnel,
14 Virtual,
15 Loopback,
16 #[default]
17 Unknown,
18}
19
20#[non_exhaustive]
22#[derive(Clone, Copy, Debug, PartialEq, Eq)]
23pub enum LanIpv4Completeness {
24 Complete,
27 Partial,
30}
31
32#[non_exhaustive]
36#[derive(Clone, Copy, Debug, PartialEq, Eq)]
37pub enum LanIpv4Exclusion {
38 InterfaceDown,
39 InterfaceStateUnknown,
40 PointToPoint,
41 PointToPointStateUnknown,
42 VirtualInterfaceNameHint,
43 PlatformVirtualOrTunnel,
44 LinkInactive,
45 Loopback,
46 LinkLocal,
47 Unspecified,
48 Broadcast,
49 Multicast,
50 ThisNetwork,
51 Reserved,
52 SharedAddressSpace,
53 BenchmarkRange,
54}
55
56#[non_exhaustive]
59#[derive(Clone, Copy, Debug, PartialEq, Eq)]
60pub enum LanIpv4RecommendationReason {
61 ActiveLanDefaultRoute,
62 ActiveWifiOrEthernet,
63 ActiveBridge,
64 LanLinkUnknown,
65 MetadataUnknown,
66 BridgeLinkUnknown,
67}
68
69#[derive(Clone, Debug, PartialEq, Eq)]
72pub struct LanIpv4Candidate {
73 pub(crate) interface_id: String,
74 pub(crate) interface_name: String,
75 pub(crate) interface_index: Option<u32>,
76 pub(crate) address: Ipv4Addr,
77 pub(crate) netmask: Ipv4Addr,
78 pub(crate) prefix_len: u8,
79 pub(crate) interface_kind: LanInterfaceKind,
80 pub(crate) oper_up: Option<bool>,
81 pub(crate) point_to_point: Option<bool>,
82 pub(crate) link_active: Option<bool>,
83 pub(crate) default_route: Option<bool>,
84 pub(crate) exclusions: Vec<LanIpv4Exclusion>,
85 pub(crate) recommendation: Option<LanIpv4RecommendationReason>,
86}
87
88impl LanIpv4Candidate {
89 pub fn interface_id(&self) -> &str {
92 &self.interface_id
93 }
94
95 pub fn interface_name(&self) -> &str {
96 &self.interface_name
97 }
98
99 pub fn interface_index(&self) -> Option<u32> {
101 self.interface_index
102 }
103
104 pub fn address(&self) -> Ipv4Addr {
105 self.address
106 }
107
108 pub fn netmask(&self) -> Ipv4Addr {
109 self.netmask
110 }
111
112 pub fn prefix_len(&self) -> u8 {
113 self.prefix_len
114 }
115
116 pub fn interface_kind(&self) -> LanInterfaceKind {
117 self.interface_kind
118 }
119
120 pub fn is_oper_up(&self) -> Option<bool> {
123 self.oper_up
124 }
125
126 pub fn is_point_to_point(&self) -> Option<bool> {
127 self.point_to_point
128 }
129
130 pub fn link_active(&self) -> Option<bool> {
132 self.link_active
133 }
134
135 pub fn is_default_route(&self) -> Option<bool> {
139 self.default_route
140 }
141
142 pub fn default_exclusions(&self) -> &[LanIpv4Exclusion] {
145 &self.exclusions
146 }
147
148 pub fn recommendation_reason(&self) -> Option<LanIpv4RecommendationReason> {
150 self.recommendation
151 }
152
153 pub fn is_recommended(&self) -> bool {
154 self.recommendation.is_some()
155 }
156}
157
158#[derive(Clone, Debug)]
161pub struct LanIpv4Snapshot {
162 pub(crate) candidates: Vec<LanIpv4Candidate>,
163 pub(crate) completeness: LanIpv4Completeness,
164 pub(crate) diagnostics: Vec<String>,
165 pub(crate) query_started_at: SystemTime,
166 pub(crate) observed_at: SystemTime,
167}
168
169impl LanIpv4Snapshot {
170 pub fn candidates(&self) -> &[LanIpv4Candidate] {
175 &self.candidates
176 }
177
178 pub fn recommended(&self) -> Option<&LanIpv4Candidate> {
183 self.candidates
184 .iter()
185 .find(|candidate| candidate.is_recommended())
186 }
187
188 pub fn completeness(&self) -> LanIpv4Completeness {
189 self.completeness
190 }
191
192 pub fn diagnostics(&self) -> &[String] {
194 &self.diagnostics
195 }
196
197 pub fn query_started_at(&self) -> SystemTime {
200 self.query_started_at
201 }
202
203 pub fn observed_at(&self) -> SystemTime {
206 self.observed_at
207 }
208}