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
use crate::{IfEvent, IpNet, Ipv4Net, Ipv6Net};
use fnv::FnvHashSet;
use futures::channel::mpsc::UnboundedReceiver;
use futures::future::Either;
use futures::stream::{Stream, TryStreamExt};
use rtnetlink::constants::{RTMGRP_IPV4_IFADDR, RTMGRP_IPV6_IFADDR};
use rtnetlink::packet::address::nlas::Nla;
use rtnetlink::packet::{AddressMessage, RtnlMessage};
use rtnetlink::proto::{Connection, NetlinkMessage, NetlinkPayload};
use rtnetlink::sys::{AsyncSocket, SmolSocket, SocketAddr};
use std::collections::VecDeque;
use std::future::Future;
use std::io::{Error, ErrorKind, Result};
use std::net::{Ipv4Addr, Ipv6Addr};
use std::ops::DerefMut;
use std::pin::Pin;
use std::task::{Context, Poll};
pub struct IfWatcher {
conn: Connection<RtnlMessage, SmolSocket>,
messages: UnboundedReceiver<(NetlinkMessage<RtnlMessage>, SocketAddr)>,
addrs: FnvHashSet<IpNet>,
queue: VecDeque<IfEvent>,
}
impl std::fmt::Debug for IfWatcher {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("IfWatcher")
.field("addrs", &self.addrs)
.finish_non_exhaustive()
}
}
impl IfWatcher {
pub async fn new() -> Result<Self> {
let (mut conn, handle, messages) = rtnetlink::new_connection_with_socket::<SmolSocket>()?;
let groups = RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR;
let addr = SocketAddr::new(0, groups);
conn.socket_mut().socket_mut().bind(&addr)?;
let mut stream = handle.address().get().execute();
let mut addrs = FnvHashSet::default();
let mut queue = VecDeque::default();
loop {
let fut = futures::future::select(conn, stream.try_next());
match fut.await {
Either::Left(_) => {
return Err(std::io::Error::new(
ErrorKind::BrokenPipe,
"rtnetlink socket closed",
))
}
Either::Right((x, c)) => {
conn = c;
match x {
Ok(Some(msg)) => {
for net in iter_nets(msg) {
if addrs.insert(net) {
queue.push_back(IfEvent::Up(net));
}
}
}
Ok(None) => break,
Err(err) => return Err(Error::new(ErrorKind::Other, err)),
}
}
}
}
Ok(Self {
conn,
messages,
addrs,
queue,
})
}
pub fn iter(&self) -> impl Iterator<Item = &IpNet> {
self.addrs.iter()
}
fn add_address(&mut self, msg: AddressMessage) {
for net in iter_nets(msg) {
if self.addrs.insert(net) {
self.queue.push_back(IfEvent::Up(net));
}
}
}
fn rem_address(&mut self, msg: AddressMessage) {
for net in iter_nets(msg) {
if self.addrs.remove(&net) {
self.queue.push_back(IfEvent::Down(net));
}
}
}
}
impl Future for IfWatcher {
type Output = Result<IfEvent>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
log::trace!("polling IfWatcher {:p}", self.deref_mut());
if Pin::new(&mut self.conn).poll(cx).is_ready() {
return Poll::Ready(Err(std::io::Error::new(
ErrorKind::BrokenPipe,
"rtnetlink socket closed",
)));
}
while let Poll::Ready(Some((message, _))) = Pin::new(&mut self.messages).poll_next(cx) {
match message.payload {
NetlinkPayload::Error(err) => return Poll::Ready(Err(err.to_io())),
NetlinkPayload::InnerMessage(msg) => match msg {
RtnlMessage::NewAddress(msg) => self.add_address(msg),
RtnlMessage::DelAddress(msg) => self.rem_address(msg),
_ => {}
},
_ => {}
}
}
if let Some(event) = self.queue.pop_front() {
return Poll::Ready(Ok(event));
}
Poll::Pending
}
}
fn iter_nets(msg: AddressMessage) -> impl Iterator<Item = IpNet> {
let prefix = msg.header.prefix_len;
let family = msg.header.family;
msg.nlas.into_iter().filter_map(move |nla| {
if let Nla::Address(octets) = nla {
match family {
2 => {
let mut addr = [0; 4];
addr.copy_from_slice(&octets);
Some(IpNet::V4(
Ipv4Net::new(Ipv4Addr::from(addr), prefix).unwrap(),
))
}
10 => {
let mut addr = [0; 16];
addr.copy_from_slice(&octets);
Some(IpNet::V6(
Ipv6Net::new(Ipv6Addr::from(addr), prefix).unwrap(),
))
}
_ => None,
}
} else {
None
}
})
}