getifs 0.6.0

Cross-platform enumeration of network interfaces and their MTU, gateway, multicast, and local/private/public IP addresses.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
use std::io;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

use ipnet::{IpNet, Ipv4Net, Ipv6Net};
use smallvec_wrapper::SmallVec;
use smol_str::SmolStr;

use super::os;

macro_rules! routev_impl {
  ($kind:literal) => {
    paste::paste! {
      #[doc = "An IP" $kind " entry from the kernel routing table."]
      #[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
      pub struct [<Ip $kind Route>] {
        index: u32,
        destination: [<Ip $kind Net>],
        gateway: Option<[<Ip $kind Addr>]>,
      }

      impl core::fmt::Display for [<Ip $kind Route>] {
        fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
          match self.gateway {
            Some(gw) => write!(f, "{} via {} ({})", self.destination, gw, self.index),
            None => write!(f, "{} ({})", self.destination, self.index),
          }
        }
      }

      impl [<Ip $kind Route>] {
        #[doc = "Creates a new IP" $kind " route entry."]
        #[inline]
        pub const fn new(
          index: u32,
          destination: [<Ip $kind Net>],
          gateway: Option<[<Ip $kind Addr>]>,
        ) -> Self {
          Self { index, destination, gateway }
        }

        /// Returns the output interface index for this route.
        #[inline]
        pub const fn index(&self) -> u32 {
          self.index
        }

        /// Returns the output interface name.
        ///
        /// This method invokes `if_indextoname` internally.
        pub fn name(&self) -> io::Result<SmolStr> {
          crate::idx_to_name::ifindex_to_name(self.index)
        }

        /// Returns the destination network of this route.
        ///
        /// A default route has prefix length `0` (`0.0.0.0/0` or `::/0`).
        #[inline]
        pub const fn destination(&self) -> &[<Ip $kind Net>] {
          &self.destination
        }

        /// Returns the next-hop gateway, or `None` for a directly-connected
        /// (link-scope) route.
        #[inline]
        pub const fn gateway(&self) -> Option<[<Ip $kind Addr>]> {
          self.gateway
        }

        /// Returns `true` if this is a default route.
        #[inline]
        pub const fn is_default(&self) -> bool {
          self.destination.prefix_len() == 0
        }
      }
    }
  };
}

routev_impl!("v4");
routev_impl!("v6");

impl From<Ipv4Route> for IpRoute {
  #[inline]
  fn from(value: Ipv4Route) -> Self {
    Self::V4(value)
  }
}

impl From<Ipv6Route> for IpRoute {
  #[inline]
  fn from(value: Ipv6Route) -> Self {
    Self::V6(value)
  }
}

/// An entry from the kernel routing table.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum IpRoute {
  /// An IPv4 route.
  V4(Ipv4Route),
  /// An IPv6 route.
  V6(Ipv6Route),
}

impl core::fmt::Display for IpRoute {
  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    match self {
      Self::V4(r) => write!(f, "{r}"),
      Self::V6(r) => write!(f, "{r}"),
    }
  }
}

impl IpRoute {
  /// Returns the output interface index.
  #[inline]
  pub const fn index(&self) -> u32 {
    match self {
      Self::V4(r) => r.index(),
      Self::V6(r) => r.index(),
    }
  }

  /// Returns the output interface name.
  ///
  /// This method invokes `if_indextoname` internally.
  pub fn name(&self) -> io::Result<SmolStr> {
    crate::idx_to_name::ifindex_to_name(self.index())
  }

  /// Returns the destination network of this route.
  #[inline]
  pub const fn destination(&self) -> IpNet {
    match self {
      Self::V4(r) => IpNet::V4(*r.destination()),
      Self::V6(r) => IpNet::V6(*r.destination()),
    }
  }

  /// Returns the next-hop gateway, or `None` for a directly-connected route.
  #[inline]
  pub const fn gateway(&self) -> Option<IpAddr> {
    match self {
      Self::V4(r) => match r.gateway() {
        Some(ip) => Some(IpAddr::V4(ip)),
        None => None,
      },
      Self::V6(r) => match r.gateway() {
        Some(ip) => Some(IpAddr::V6(ip)),
        None => None,
      },
    }
  }

  /// Returns `true` if this is a default route.
  #[inline]
  pub const fn is_default(&self) -> bool {
    match self {
      Self::V4(r) => r.is_default(),
      Self::V6(r) => r.is_default(),
    }
  }
}

/// Returns the **unicast and local** entries from the kernel routing
/// table (both IPv4 and IPv6). Other route classes are intentionally
/// excluded — the [`IpRoute`] type only models a single (`destination`,
/// `gateway`, `index`) tuple, so route kinds without a usable
/// next-hop interface are filtered out at the platform layer:
///
/// - **Linux**: only `RTN_UNICAST` and `RTN_LOCAL` rows from the
///   three built-in RPDB tables — `RT_TABLE_MAIN`, `RT_TABLE_LOCAL`,
///   and `RT_TABLE_DEFAULT` — are emitted. Routes from custom policy
///   tables (selected via `ip rule` with fwmark, iif, uid, etc.),
///   TOS-specific rows (`rtm_tos != 0`), source-constrained rows
///   (`rtm_src_len != 0` or `RTA_SRC` set), and blackhole /
///   unreachable / prohibit / broadcast / multicast / nat types are
///   dropped — they can't be represented faithfully as a single
///   (oif, gw) tuple, and surfacing them would mislead callers into
///   using a route the kernel would not consult for ordinary
///   traffic. ECMP routes (`RTA_MULTIPATH`) are decoded into one
///   [`IpRoute`] per nexthop. Routes that reference a separate nexthop
///   object via `RTA_NH_ID` (the `ip nexthop`-managed indirection
///   added in Linux 5.3) are resolved against an up-front
///   `RTM_GETNEXTHOP` dump: leaves emit one route, groups fan out to
///   one route per member (group-of-groups is rare and skipped).
///   Blackhole nexthops are filtered.
///
///   Cross-family next-hop routes (the kernel's `RTA_VIA` encoding —
///   e.g. an IPv4 route via an IPv6 link-local gateway) are dropped:
///   [`IpRoute`] only models a same-family `(destination, gateway)`
///   pair, and emitting such a route with `gateway = None` would
///   misrepresent it as on-link. Hosts that route via an `RTA_VIA`
///   default will see no default in [`route_table`] / [`route_ipv4_table`]
///   / [`route_ipv6_table`]; reach for `rtnetlink` directly there.
///
///   Best-effort: hosts running unconstrained custom `ip rule`
///   policies ahead of `main` (multi-WAN / mwan3 / advanced VPN
///   setups) may have outbound traffic routed through a custom table
///   that this API does not enumerate — do not use this output to
///   predict the kernel's actual forwarding decision on policy-routed
///   hosts; reach for `rtnetlink` and `RTM_GETRULE` directly there.
/// - **BSD-like / macOS**: only routes with `RTF_UP` and a usable
///   destination are emitted; AF_LINK gateways surface as
///   `gateway = None`.
/// - **Windows**: only families whose `GetIpForwardTable2` call
///   succeeds are included; an `ERROR_NOT_FOUND` for one family is
///   treated as an empty table for that family rather than a failure.
///
/// ## Platform notes
///
/// `parse_addrs` decodes both the full `sockaddr_in[6]` form and the
/// BSD compact form (`sa_family = AF_INET[6]` but `sa_len <
/// size_of::<sockaddr_in[6]>()`) that NetBSD and OpenBSD emit for
/// netmasks. AF_LINK gateways (no IP equivalent) decode to
/// `gateway = None`. A `parse_addrs` error is treated as a malformed
/// message and surfaced to the caller rather than being swallowed.
///
/// ## Example
///
/// ```rust
/// use getifs::route_table;
///
/// for route in route_table().unwrap() {
///   println!("{route}");
/// }
/// ```
pub fn route_table() -> io::Result<SmallVec<IpRoute>> {
  os::route_table_by_filter(|_| true)
}

/// Returns the IPv4 unicast/local entries from the kernel routing
/// table. See [`route_table`] for the exact set of route kinds
/// included and the platform notes.
///
/// ## Example
///
/// ```rust
/// use getifs::route_ipv4_table;
///
/// for route in route_ipv4_table().unwrap() {
///   println!("{route}");
/// }
/// ```
pub fn route_ipv4_table() -> io::Result<SmallVec<Ipv4Route>> {
  os::route_ipv4_table_by_filter(|_| true)
}

/// Returns the IPv6 unicast/local entries from the kernel routing
/// table. See [`route_table`] for the exact set of route kinds
/// included and the platform notes.
///
/// ## Example
///
/// ```rust
/// use getifs::route_ipv6_table;
///
/// for route in route_ipv6_table().unwrap() {
///   println!("{route}");
/// }
/// ```
pub fn route_ipv6_table() -> io::Result<SmallVec<Ipv6Route>> {
  os::route_ipv6_table_by_filter(|_| true)
}

/// Returns routing-table entries that match the given filter. Only
/// the unicast/local route classes (see [`route_table`]) are visible
/// to the filter; non-unicast kinds are excluded at the platform
/// layer before `f` runs.
///
/// ## Example
///
/// ```rust
/// use getifs::{route_table_by_filter, IpRoute};
///
/// // Only default routes
/// let defaults = route_table_by_filter(|r| r.is_default()).unwrap();
/// for route in defaults {
///   println!("{route}");
/// }
/// ```
pub fn route_table_by_filter<F>(f: F) -> io::Result<SmallVec<IpRoute>>
where
  F: FnMut(&IpRoute) -> bool,
{
  os::route_table_by_filter(f)
}

/// Returns IPv4 routing-table entries that match the given filter.
///
/// ## Example
///
/// ```rust
/// use getifs::route_ipv4_table_by_filter;
///
/// // Only routes with a gateway
/// let with_gw = route_ipv4_table_by_filter(|r| r.gateway().is_some()).unwrap();
/// for route in with_gw {
///   println!("{route}");
/// }
/// ```
pub fn route_ipv4_table_by_filter<F>(f: F) -> io::Result<SmallVec<Ipv4Route>>
where
  F: FnMut(&Ipv4Route) -> bool,
{
  os::route_ipv4_table_by_filter(f)
}

/// Returns IPv6 routing-table entries that match the given filter.
///
/// ## Example
///
/// ```rust
/// use getifs::route_ipv6_table_by_filter;
///
/// let v6 = route_ipv6_table_by_filter(|_| true).unwrap();
/// for route in v6 {
///   println!("{route}");
/// }
/// ```
pub fn route_ipv6_table_by_filter<F>(f: F) -> io::Result<SmallVec<Ipv6Route>>
where
  F: FnMut(&Ipv6Route) -> bool,
{
  os::route_ipv6_table_by_filter(f)
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn route_v4_basic() {
    let dst = Ipv4Net::new(Ipv4Addr::new(10, 0, 0, 0), 8).unwrap();
    let gw = Some(Ipv4Addr::new(10, 0, 0, 1));
    let r = Ipv4Route::new(2, dst, gw);
    assert_eq!(r.index(), 2);
    assert_eq!(r.destination(), &dst);
    assert_eq!(r.gateway(), gw);
    assert!(!r.is_default());
    // Don't assert on `r.name()` — the previous version called
    // `name().is_ok()` with a hard-coded index of 2, which fails on
    // hosts (Windows runners, some macOS / container CIs) where no
    // interface happens to be at that index. The constructor under
    // test doesn't depend on that lookup; this is a unit test, not
    // an integration test.

    let default = Ipv4Route::new(0, Ipv4Net::new(Ipv4Addr::UNSPECIFIED, 0).unwrap(), None);
    assert!(default.is_default());
    assert!(default.gateway().is_none());
  }

  #[test]
  fn route_v6_basic() {
    let dst = Ipv6Net::new(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0), 32).unwrap();
    let gw = Some(Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 1));
    let r = Ipv6Route::new(3, dst, gw);
    assert_eq!(r.index(), 3);
    assert_eq!(r.destination(), &dst);
    assert_eq!(r.gateway(), gw);
    assert!(!r.is_default());
  }

  #[test]
  fn route_enum_dispatch() {
    let v4 = Ipv4Route::new(
      1,
      Ipv4Net::new(Ipv4Addr::new(192, 168, 1, 0), 24).unwrap(),
      None,
    );
    let r: IpRoute = v4.into();
    assert_eq!(r.index(), 1);
    assert!(r.gateway().is_none());
    assert!(matches!(r.destination(), IpNet::V4(_)));
    assert!(!r.is_default());

    let v6 = Ipv6Route::new(
      1,
      Ipv6Net::new(Ipv6Addr::UNSPECIFIED, 0).unwrap(),
      Some(Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 1)),
    );
    let r: IpRoute = v6.into();
    assert!(r.is_default());
    assert!(matches!(r.destination(), IpNet::V6(_)));
    assert_eq!(
      r.gateway(),
      Some(IpAddr::V6(Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 1))),
    );
  }

  // The union `route_table` walks both AF_INET and AF_INET6 on BSD;
  // NetBSD's CI VM hits the `ENOMEM` v6 dump path documented at
  // `route_v6_table_returns` below, and `family_unavailable_to_empty`
  // only collapses unsupported-family errnos (not `ENOMEM`). Gate on
  // NetBSD for the same reason — propagating the kernel errno is
  // correct library behavior; the smoke test just gets skipped.
  #[cfg(not(target_os = "netbsd"))]
  #[test]
  fn route_table_returns() {
    let routes = route_table().unwrap();
    for r in &routes {
      // exercise the accessors so this test catches accidental panics
      // (e.g. from invalid prefix lengths) coming back from the kernel.
      let _ = r.index();
      let _ = r.destination();
      let _ = r.gateway();
      let _ = r.is_default();
      let _ = format!("{r}");
    }
  }

  #[cfg(not(target_os = "netbsd"))]
  #[test]
  fn route_table_filter_default_only() {
    let defaults = route_table_by_filter(|r| r.is_default()).unwrap();
    for r in &defaults {
      assert!(
        r.is_default(),
        "got non-default route through is_default filter: {r}"
      );
    }
  }

  #[test]
  fn route_v4_table_returns() {
    let routes = route_ipv4_table().unwrap();
    for r in &routes {
      let _ = r.index();
      let _ = r.destination();
      let _ = r.gateway();
    }
  }

  // NetBSD's CI VM has a v6 routing stack whose `NET_RT_DUMP` sysctl
  // can return `ENOMEM` rather than an empty dump (the OS-side
  // `sysctl_dorouttable` allocator behavior — not a real out-of-memory
  // condition we can do anything about). Propagating the errno is
  // correct library behavior, so the smoke test just gets gated off
  // on the platform.
  #[cfg(not(target_os = "netbsd"))]
  #[test]
  fn route_v6_table_returns() {
    let routes = route_ipv6_table().unwrap();
    for r in &routes {
      let _ = r.index();
      let _ = r.destination();
      let _ = r.gateway();
    }
  }
}