]>
code.octet-stream.net Git - netwatcher/blob - src/imp_mac.rs
1 use std
::{collections
::HashMap
, net
::IpAddr
};
5 use nix
::{ifaddrs
::getifaddrs
, net
::if_
::if_nametoindex
};
7 use crate::util
::format_mac
;
8 use crate::{Error
, IfIndex
, Interface
};
10 struct CandidateInterface
{
13 hw_addr
: Option
<String
>,
17 pub(crate) fn list_interfaces() -> Result
<HashMap
<IfIndex
, Interface
>, Error
> {
18 let addrs
= getifaddrs().map_err(|_
| Error
::Internal
)?
;
19 let mut candidates
= HashMap
::new();
22 let index
= if_nametoindex(addr
.inter
face
_name
.as_str()).map_err(|_
| Error
::Internal
)?
;
23 let candidate
= candidates
24 .entry(addr
.inter
face
_name
.clone())
25 .or_insert_with(|| CandidateInterface
{
26 name
: addr
.inter
face
_name
.clone(),
31 if let Some(a
) = addr
.address
{
32 if let Some(a
) = a
.as_link_addr() {
33 if let Some(raw_addr
) = a
.addr() {
34 candidate
.hw_addr
= Some(format_mac(&raw_addr
)?
);
37 if let Some(a
) = a
.as_sockaddr_in() {
38 candidate
.ips
.push(IpAddr
::V4(a
.ip
()));
40 if let Some(a
) = a
.as_sockaddr_in6() {
41 candidate
.ips
.push(IpAddr
::V6(a
.ip
()));
49 c
.hw_addr
.map(|hw_addr
| {
65 // The "objc2" project aims to provide bindings for all frameworks but Network.framework
66 // isn't ready yet so let's kick it old-school
68 struct nw_path_monitor
;
69 type nw_path_monitor_t
= *mut nw_path_monitor
;
71 type nw_path_t
= *mut nw_path
;
72 struct dispatch_queue
;
73 type dispatch_queue_t
= *mut dispatch_queue
;
74 const QOS_CLASS_BACKGROUND
: usize = 0x09;
76 #[link(name = "Network", kind = "framework")]
78 fn nw_path_monitor_create() -> nw_path_monitor_t
;
79 fn nw_path_monitor_set_update_handler(
80 monitor
: nw_path_monitor_t
,
81 update_handler
: &Block
<dyn
Fn(nw_path_t
)>,
83 fn nw_path_monitor_set_queue(monitor
: nw_path_monitor_t
, queue
: dispatch_queue_t
);
84 fn nw_path_monitor_start(monitor
: nw_path_monitor_t
);
85 fn nw_path_monitor_cancel(monitor
: nw_path_monitor_t
);
87 fn dispatch_get_global_queue(identifier
: usize, flag
: usize) -> dispatch_queue_t
;
92 use super::list_interfaces
;
96 let ifaces
= list_interfaces().unwrap
();
97 println
!("{:?}", ifaces
);