]>
code.octet-stream.net Git - netwatcher/blob - src/imp_mac.rs
1 use std
::{collections
::HashMap
, net
::IpAddr
};
3 use nix
::{ifaddrs
::getifaddrs
, net
::if_
::if_nametoindex
};
5 use crate::util
::format_mac
;
6 use crate::{Error
, IfIndex
, Interface
};
8 struct CandidateInterface
{
11 hw_addr
: Option
<String
>,
15 pub(crate) fn list_interfaces() -> Result
<HashMap
<IfIndex
, Interface
>, Error
> {
16 let addrs
= getifaddrs().map_err(|_
| Error
::Internal
)?
;
17 let mut candidates
= HashMap
::new();
20 let index
= if_nametoindex(addr
.inter
face
_name
.as_str()).map_err(|_
| Error
::Internal
)?
;
21 let candidate
= candidates
22 .entry(addr
.inter
face
_name
.clone())
23 .or_insert_with(|| CandidateInterface
{
24 name
: addr
.inter
face
_name
.clone(),
29 if let Some(a
) = addr
.address
{
30 if let Some(a
) = a
.as_link_addr() {
31 if let Some(raw_addr
) = a
.addr() {
32 candidate
.hw_addr
= Some(format_mac(&raw_addr
)?
);
35 if let Some(a
) = a
.as_sockaddr_in() {
36 candidate
.ips
.push(IpAddr
::V4(a
.ip
()));
38 if let Some(a
) = a
.as_sockaddr_in6() {
39 candidate
.ips
.push(IpAddr
::V6(a
.ip
()));
47 c
.hw_addr
.map(|hw_addr
| {
65 use super::list_interfaces
;
69 let ifaces
= list_interfaces().unwrap
();
70 println
!("{:?}", ifaces
);