heim_net/
nic.rs

1use std::fmt;
2use std::net;
3
4use heim_common::prelude::*;
5
6use crate::sys;
7
8/// Network interface address.
9#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
10pub enum Address {
11    /// IPv4 Internet protocols
12    Inet(net::SocketAddr),
13
14    /// IPv6 Internet protocols
15    Inet6(net::SocketAddr),
16
17    /// Link level interface
18    Link(macaddr::MacAddr),
19
20    #[doc(hidden)]
21    __Nonexhaustive,
22}
23
24// TODO: Consider implement `Address::to_family()` method
25// which will return the `libc::c_int` value with a corresponding value
26// for the current address member (ex. `AF_INET` or `AF_PACKET`)
27// Do not forget that it is OS-dependant.
28
29/// Network interface device.
30pub struct Nic(sys::Nic);
31
32wrap!(Nic, sys::Nic);
33
34impl Nic {
35    /// Returns NIC name.
36    pub fn name(&self) -> &str {
37        self.as_ref().name()
38    }
39
40    /// Returns primary NIC address.
41    pub fn address(&self) -> Address {
42        self.as_ref().address()
43    }
44
45    /// Returns netmask address if available.
46    pub fn netmask(&self) -> Option<Address> {
47        self.as_ref().netmask()
48    }
49
50    /// Returns destination address if available.
51    pub fn destination(&self) -> Option<Address> {
52        self.as_ref().destination()
53    }
54
55    /// Returns `bool` indicating whether interface is up and running.
56    pub fn is_up(&self) -> bool {
57        self.as_ref().is_up()
58    }
59
60    /// Returns `bool` indicating whether interface is loopback.
61    pub fn is_loopback(&self) -> bool {
62        self.as_ref().is_loopback()
63    }
64
65    /// Returns `bool` indicating whether interface is multicast.
66    pub fn is_multicast(&self) -> bool {
67        self.as_ref().is_multicast()
68    }
69}
70
71impl fmt::Debug for Nic {
72    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
73        f.debug_struct("Nic")
74            .field("name", &self.name())
75            .field("address", &self.address())
76            .field("netmask", &self.netmask())
77            .field("destination", &self.destination())
78            .field("is_up", &self.is_up())
79            .field("is_loopback", &self.is_loopback())
80            .field("is_multicast", &self.is_multicast())
81            .finish()
82    }
83}
84
85/// Returns stream which yields [Network Interface Cards].
86///
87/// [Network Interface Cards]: struct.Nic.html
88pub fn nic() -> impl Stream<Item = Result<Nic>> {
89    sys::nic().map_ok(Into::into)
90}