Skip to content

Commit 87fd692

Browse files
committed
Support route lookup
Quick and dirty patch to support looking up a route to a destination in addition to dumping all routes
1 parent 5e9f368 commit 87fd692

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/route/get.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// SPDX-License-Identifier: MIT
22

3+
use std::net::IpAddr;
4+
35
use futures::{
46
future::{self, Either},
57
stream::{StreamExt, TryStream},
@@ -8,8 +10,8 @@ use futures::{
810

911
use netlink_packet_core::{NetlinkMessage, NLM_F_DUMP, NLM_F_REQUEST};
1012
use netlink_packet_route::{
11-
RouteMessage, RtnlMessage, AF_INET, AF_INET6, RTN_UNSPEC, RTPROT_UNSPEC,
12-
RT_SCOPE_UNIVERSE, RT_TABLE_UNSPEC,
13+
route::Nla, RouteMessage, RtnlMessage, AF_INET, AF_INET6, RTN_UNSPEC,
14+
RTPROT_UNSPEC, RT_SCOPE_UNIVERSE, RT_TABLE_UNSPEC,
1315
};
1416

1517
use crate::{try_rtnl, Error, Handle};
@@ -65,14 +67,30 @@ impl RouteGetRequest {
6567
&mut self.message
6668
}
6769

70+
pub fn to(mut self, ip: IpAddr) -> Self {
71+
match ip {
72+
IpAddr::V4(v4) => {
73+
self.message.nlas.push(Nla::Destination(v4.octets().into()));
74+
self.message.header.destination_prefix_length = 32;
75+
}
76+
IpAddr::V6(v6) => {
77+
self.message.nlas.push(Nla::Destination(v6.octets().into()));
78+
self.message.header.destination_prefix_length = 128;
79+
}
80+
}
81+
self
82+
}
83+
6884
pub fn execute(self) -> impl TryStream<Ok = RouteMessage, Error = Error> {
6985
let RouteGetRequest {
7086
mut handle,
7187
message,
7288
} = self;
7389

90+
let should_dump = message.nlas.is_empty();
7491
let mut req = NetlinkMessage::from(RtnlMessage::GetRoute(message));
75-
req.header.flags = NLM_F_REQUEST | NLM_F_DUMP;
92+
req.header.flags =
93+
NLM_F_REQUEST | if should_dump { NLM_F_DUMP } else { 0 };
7694

7795
match handle.request(req) {
7896
Ok(response) => Either::Left(

0 commit comments

Comments
 (0)