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
#[cfg(unix)]
#[macro_use]
extern crate bitflags;
#[cfg(unix)]
#[macro_use]
extern crate lazy_static;

use std::{
    fmt,
    net,
    io,
};
use serde::Serialize;
use serde_json;

#[cfg(unix)]
mod unix;
#[cfg(windows)]
mod windows;

#[derive(Debug)]
pub enum IfCfgError {
    #[cfg(unix)]
    InterfacesError(unix::InterfacesError),
    IOError(io::Error),
    SerdeJsonError(serde_json::error::Error),
    Generic(&'static str),
}

impl fmt::Display for IfCfgError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match &self {
            #[cfg(unix)]
            IfCfgError::InterfacesError(err) => write!(f, "InterfacesError({})", err),
            IfCfgError::IOError(err) => write!(f, "IOError({})", err),
            IfCfgError::SerdeJsonError(err) => write!(f, "SerdeJsonError({})", err),
            IfCfgError::Generic(err) => write!(f, "Generic({})", err),
        }
    }
}

impl From<io::Error> for IfCfgError {
    fn from(err: io::Error) -> IfCfgError {
        IfCfgError::IOError(err)
    }
}

impl From<serde_json::error::Error> for IfCfgError {
    fn from(err: serde_json::error::Error) -> IfCfgError {
        IfCfgError::SerdeJsonError(err)
    }
}

pub type Result<T> = std::result::Result<T, IfCfgError>;

#[derive(Debug, Clone, Copy, Serialize)]
pub enum AddressFamily {
    IPv4,
    IPv6,
    Link,
    Packet,
    Unknown(i32),
}

#[derive(Debug, Clone, Copy, Serialize)]
pub enum Hops {
    Broadcast(net::SocketAddr),
    Destination(net::SocketAddr),
}

#[derive(Debug, Clone, Copy, Serialize)]
pub struct InterfaceAddress {
    pub address_family: AddressFamily,
    pub address: Option<net::SocketAddr>,
    pub mask: Option<net::SocketAddr>,
    pub hop: Option<Hops>,
}

#[derive(Debug, Serialize)]
pub struct IfCfg {
    pub name: String,
    pub mac: String,
    pub addresses: Vec<InterfaceAddress>,
    pub description: String,
}

impl IfCfg {
    pub fn get() -> Result<Vec<IfCfg>> {
        //! Gets the Interfaces from a computer
        //!
        //! ## Example
        //! ```rust
        //! use ifcfg::{IfCfg, Result};
        //!
        //! fn main() -> Result<()> {
        //!     let adapters = IfCfg::get().expect("could not get interfaces");
        //!     println!("{}", serde_json::to_string(&adapters)?);
        //!     Ok(())
        //! }
        //! ```
        let mut results = Vec::<IfCfg>::new();

        #[cfg(unix)]
        {
            let interfaces = unix::Interface::get_all()?;
            for interface in interfaces.iter() {
                results.push(interface.into());
            }
        }
        #[cfg(windows)]
        {
            let adapters = windows::Adapters::new()?;
            for adapter in adapters.iter() {
                results.push(adapter.into());
            }
        }
        Ok(results)
    }
}

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

    #[test]
    fn test_get_adapters () {
        let x = IfCfg::get();
        assert!(x.is_ok());
    }
}