aboutsummaryrefslogtreecommitdiffstats
path: root/src/route_monitor/linux.rs
blob: a08cff2e02219849805b5dc0cdff6ebb0544c79b (plain) (blame)
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
use failure::Error;
use futures::stream::Stream;
use futures::{Async, Poll};
use pnetlink::socket::NetlinkProtocol;
use pnetlink::packet::route::RtMsgPacket;
use pnetlink::tokio::{NetlinkSocket, NetlinkCodec};
use tokio_core::reactor::Handle;
use tokio_io::{AsyncRead, codec::Framed};
use std::io;

pub struct RouteListener {
    inner: Framed<NetlinkSocket, NetlinkCodec>,
}

impl RouteListener {
    pub fn bind(handle: &Handle) -> io::Result<Self> {
        let sock = NetlinkSocket::bind(NetlinkProtocol::Route, 0, handle)?;

        Ok(RouteListener {
            inner: AsyncRead::framed(sock, NetlinkCodec {});
        })
    }
}

impl Stream for RouteListener {
    type Item = ();
    type Error = Error;

    fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
        match self.inner.poll() {
            Ok(Async::Ready(Some(packet)))
        }
    }
}