Skip to content
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
104 changes: 102 additions & 2 deletions src/addr/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,24 @@ use netlink_packet_core::{
NetlinkMessage, NLM_F_ACK, NLM_F_CREATE, NLM_F_EXCL, NLM_F_REPLACE,
NLM_F_REQUEST,
};
use netlink_packet_route::{address::AddressMessage, RouteNetlinkMessage};
use netlink_packet_route::{
address::{
AddressFlags, AddressMessage, AddressProtocol, AddressScope, CacheInfo,
},
RouteNetlinkMessage,
};

use crate::{addr::AddressMessageBuilder, try_nl, Error, Handle};
use crate::{
addr::{
builder::{
set_anycast, set_broadcast, set_cache_info, set_flags, set_label,
set_peer, set_preferred_lft, set_priority, set_protocol, set_scope,
set_valid_lft,
},
AddressMessageBuilder,
},
try_nl, Error, Handle,
};

/// A request to create a new address. This is equivalent to the `ip address
/// add` commands.
Expand Down Expand Up @@ -44,6 +59,91 @@ impl AddressAddRequest {
}
}

/// Sets the address scope (`ifa_scope`), e.g. `AddressScope::Link`.
pub fn scope(mut self, scope: AddressScope) -> Self {
set_scope(&mut self.message, scope);
self
}

/// Sets the address flags like the `nodad`, `optimistic`, `home`,
/// `mngtmpaddr`, `noprefixroute` and `autojoin` options of
/// `ip address add`.
///
/// The lower 8 bits are stored in the `ifa_flags` field of the message
/// header. The full 32-bit value is sent as the `IFA_FLAGS` attribute
/// when any bit does not fit, like iproute2 does.
pub fn flags(mut self, flags: AddressFlags) -> Self {
set_flags(&mut self.message, flags);
self
}

/// Sets the peer address (`IFA_ADDRESS`) for a point-to-point address.
pub fn peer(mut self, peer: IpAddr) -> Self {
set_peer(&mut self.message, peer);
self
}

/// Sets the broadcast address (`IFA_BROADCAST`).
///
/// Only IPv4 addresses support broadcast.
pub fn broadcast(mut self, broadcast: Ipv4Addr) -> Self {
set_broadcast(&mut self.message, broadcast);
self
}

/// Sets the anycast address (`IFA_ANYCAST`).
///
/// Only IPv6 addresses support anycast.
pub fn anycast(mut self, anycast: Ipv6Addr) -> Self {
set_anycast(&mut self.message, anycast);
self
}

/// Sets the interface label (`IFA_LABEL`).
pub fn label(mut self, label: impl Into<String>) -> Self {
set_label(&mut self.message, label.into());
self
}

/// Sets the route priority (`IFA_RT_PRIORITY`), which is the `metric`
/// option of `ip address add`.
pub fn priority(mut self, priority: u32) -> Self {
set_priority(&mut self.message, priority);
self
}

/// Sets the cache information (`IFA_CACHEINFO`).
pub fn cache_info(mut self, cache_info: CacheInfo) -> Self {
set_cache_info(&mut self.message, cache_info);
self
}

/// Sets the valid lifetime (`ifa_valid` of `IFA_CACHEINFO`).
///
/// When no [AddressAddRequest::cache_info] is set, the preferred lifetime
/// is set to `u32::MAX` (infinite) like iproute2 does for the omitted
/// `preferred_lft`.
pub fn valid_lft(mut self, valid_lft: u32) -> Self {
set_valid_lft(&mut self.message, valid_lft);
self
}

/// Sets the preferred lifetime (`ifa_preferred` of `IFA_CACHEINFO`).
///
/// When no [AddressAddRequest::cache_info] is set, the valid lifetime is
/// set to `u32::MAX` (infinite) like iproute2 does for the omitted
/// `valid_lft`.
pub fn preferred_lft(mut self, preferred_lft: u32) -> Self {
set_preferred_lft(&mut self.message, preferred_lft);
self
}

/// Sets the address protocol (`IFA_PROTO`).
pub fn protocol(mut self, protocol: AddressProtocol) -> Self {
set_protocol(&mut self.message, protocol);
self
}

/// Replace existing matching address.
pub fn replace(self) -> Self {
Self {
Expand Down
Loading
Loading