Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat support addr label #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion examples/add_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ async fn add_address(
if let Some(link) = links.try_next().await? {
handle
.address()
.add(link.header.index, ip.ip(), ip.prefix())
.add(
link.header.index,
ip.ip(),
ip.prefix(),
Some(format!("{}:vip", link_name)),
)
.execute()
.await?
}
Expand Down
29 changes: 28 additions & 1 deletion examples/get_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,30 @@

use futures::stream::TryStreamExt;
use rtnetlink::{new_connection, Error, Handle};
use std::env;

#[tokio::main]
async fn main() -> Result<(), ()> {
let args: Vec<String> = env::args().collect();
if args.len() != 2 {
return Ok(());
}

let link_name = &args[1];

let (connection, handle, _) = new_connection().unwrap();
tokio::spawn(connection);

let link = "lo".to_string();
println!("dumping address for link \"{link}\"");

if let Err(e) = dump_addresses(handle, link).await {
if let Err(e) = dump_addresses(handle.clone(), link).await {
eprintln!("{e}");
}

let label = format!("{link_name}:vip");
println!("\ndumping address with label \"{label}\"");
if let Err(e) = dump_addresses_with_label(handle, &label).await {
eprintln!("{e}");
}

Expand All @@ -35,3 +49,16 @@ async fn dump_addresses(handle: Handle, link: String) -> Result<(), Error> {
Ok(())
}
}

async fn dump_addresses_with_label(
handle: Handle,
label: &str,
) -> Result<(), Error> {
let mut addrs = handle.address().get().set_label_filter(label).execute();

while let Some(msg) = addrs.try_next().await? {
println!("{msg:?}");
}

Ok(())
}
5 changes: 5 additions & 0 deletions src/addr/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ impl AddressAddRequest {
index: u32,
address: IpAddr,
prefix_len: u8,
label: Option<String>,
) -> Self {
let mut message = AddressMessage::default();

Expand All @@ -54,6 +55,10 @@ impl AddressAddRequest {
} else {
message.nlas.push(Nla::Address(address_vec.clone()));

if let Some(l) = label {
message.nlas.push(Nla::Label(l));
}

// for IPv4 the IFA_LOCAL address can be set to the same value as
// IFA_ADDRESS
message.nlas.push(Nla::Local(address_vec.clone()));
Expand Down
35 changes: 32 additions & 3 deletions src/addr/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ impl AddressGetRequest {
self.filter_builder.address = Some(address);
self
}

pub fn set_label_filter(mut self, label: &str) -> Self {
self.filter_builder.label = Some(label.to_string());
self
}
}

// The reason for having filters, is that we cannot retrieve addresses
Expand All @@ -84,6 +89,7 @@ struct AddressFilterBuilder {
index: Option<u32>,
address: Option<IpAddr>,
prefix_len: Option<u8>,
label: Option<String>,
}

impl AddressFilterBuilder {
Expand All @@ -108,11 +114,13 @@ impl AddressFilterBuilder {
}

if let Some(address) = self.address {
let mut is_match: bool = false;

for nla in msg.nlas.iter() {
if let Unspec(x) | Address(x) | Local(x) | Multicast(x)
| Anycast(x) = nla
{
let is_match = match address {
is_match = match address {
IpAddr::V4(address) => {
x[..] == address.octets()[..]
}
Expand All @@ -121,12 +129,33 @@ impl AddressFilterBuilder {
}
};
if is_match {
return true;
break;
}
}
}

if !is_match {
return false;
}
}

if let Some(ref label) = self.label {
let mut is_match: bool = false;

for nla in msg.nlas.iter() {
if let Label(l) = nla {
if label == l {
is_match = true;
break;
}
}
}
return false;

if !is_match {
return false;
}
}

true
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/addr/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,15 @@ impl AddressHandle {
index: u32,
address: IpAddr,
prefix_len: u8,
label: Option<String>,
) -> AddressAddRequest {
AddressAddRequest::new(self.0.clone(), index, address, prefix_len)
AddressAddRequest::new(
self.0.clone(),
index,
address,
prefix_len,
label,
)
}

/// Delete the given address
Expand Down