use futures::stream::StreamExt;
use std::{
marker::PhantomData,
net::{Ipv4Addr, Ipv6Addr},
};
use netlink_packet_route::{
constants::*,
nlas::route::Nla,
NetlinkMessage,
RouteMessage,
RtnlMessage,
};
use crate::{try_nl, Error, Handle};
pub struct RouteAddRequest<T = ()> {
handle: Handle,
message: RouteMessage,
replace: bool,
_phantom: PhantomData<T>,
}
impl<T> RouteAddRequest<T> {
pub(crate) fn new(handle: Handle) -> Self {
let mut message = RouteMessage::default();
message.header.table = RT_TABLE_MAIN;
message.header.protocol = RTPROT_STATIC;
message.header.scope = RT_SCOPE_UNIVERSE;
message.header.kind = RTN_UNICAST;
RouteAddRequest {
handle,
message,
replace: false,
_phantom: Default::default(),
}
}
pub fn input_interface(mut self, index: u32) -> Self {
self.message.nlas.push(Nla::Iif(index));
self
}
pub fn output_interface(mut self, index: u32) -> Self {
self.message.nlas.push(Nla::Oif(index));
self
}
pub fn table(mut self, table: u8) -> Self {
self.message.header.table = table;
self
}
pub fn protocol(mut self, protocol: u8) -> Self {
self.message.header.protocol = protocol;
self
}
pub fn scope(mut self, scope: u8) -> Self {
self.message.header.scope = scope;
self
}
pub fn kind(mut self, kind: u8) -> Self {
self.message.header.kind = kind;
self
}
pub fn v4(mut self) -> RouteAddRequest<Ipv4Addr> {
self.message.header.address_family = AF_INET as u8;
RouteAddRequest {
handle: self.handle,
message: self.message,
replace: false,
_phantom: Default::default(),
}
}
pub fn v6(mut self) -> RouteAddRequest<Ipv6Addr> {
self.message.header.address_family = AF_INET6 as u8;
RouteAddRequest {
handle: self.handle,
message: self.message,
replace: false,
_phantom: Default::default(),
}
}
pub fn replace(self) -> Self {
Self {
replace: true,
..self
}
}
pub async fn execute(self) -> Result<(), Error> {
let RouteAddRequest {
mut handle,
message,
replace,
..
} = self;
let mut req = NetlinkMessage::from(RtnlMessage::NewRoute(message));
let replace = if replace { NLM_F_REPLACE } else { NLM_F_EXCL };
req.header.flags = NLM_F_REQUEST | NLM_F_ACK | replace | NLM_F_CREATE;
let mut response = handle.request(req)?;
while let Some(message) = response.next().await {
try_nl!(message);
}
Ok(())
}
pub fn message_mut(&mut self) -> &mut RouteMessage {
&mut self.message
}
}
impl RouteAddRequest<Ipv4Addr> {
pub fn source_prefix(mut self, addr: Ipv4Addr, prefix_length: u8) -> Self {
self.message.header.source_prefix_length = prefix_length;
let src = addr.octets().to_vec();
self.message.nlas.push(Nla::Source(src));
self
}
pub fn pref_source(mut self, addr: Ipv4Addr) -> Self {
let src = addr.octets().to_vec();
self.message.nlas.push(Nla::PrefSource(src));
self
}
pub fn destination_prefix(mut self, addr: Ipv4Addr, prefix_length: u8) -> Self {
self.message.header.destination_prefix_length = prefix_length;
let dst = addr.octets().to_vec();
self.message.nlas.push(Nla::Destination(dst));
self
}
pub fn gateway(mut self, addr: Ipv4Addr) -> Self {
let gtw = addr.octets().to_vec();
self.message.nlas.push(Nla::Gateway(gtw));
self
}
}
impl RouteAddRequest<Ipv6Addr> {
pub fn source_prefix(mut self, addr: Ipv6Addr, prefix_length: u8) -> Self {
self.message.header.source_prefix_length = prefix_length;
let src = addr.octets().to_vec();
self.message.nlas.push(Nla::Source(src));
self
}
pub fn pref_source(mut self, addr: Ipv6Addr) -> Self {
let src = addr.octets().to_vec();
self.message.nlas.push(Nla::PrefSource(src));
self
}
pub fn destination_prefix(mut self, addr: Ipv6Addr, prefix_length: u8) -> Self {
self.message.header.destination_prefix_length = prefix_length;
let dst = addr.octets().to_vec();
self.message.nlas.push(Nla::Destination(dst));
self
}
pub fn gateway(mut self, addr: Ipv6Addr) -> Self {
let gtw = addr.octets().to_vec();
self.message.nlas.push(Nla::Gateway(gtw));
self
}
}