|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +use std::{ |
| 4 | + marker::PhantomData, |
| 5 | + net::{Ipv4Addr, Ipv6Addr}, |
| 6 | +}; |
| 7 | + |
| 8 | +use netlink_packet_route::{ |
| 9 | + address::{AddressAttribute, AddressMessage}, |
| 10 | + AddressFamily, |
| 11 | +}; |
| 12 | + |
| 13 | +#[derive(Debug)] |
| 14 | +/// Helper struct for building [AddressMessage]. |
| 15 | +pub struct AddressMessageBuilder<T> { |
| 16 | + message: AddressMessage, |
| 17 | + _phantom: PhantomData<T>, |
| 18 | +} |
| 19 | + |
| 20 | +impl<T> AddressMessageBuilder<T> { |
| 21 | + /// Create a new [AddressMessageBuilder] without specifying the address family. |
| 22 | + fn new_no_address_family() -> Self { |
| 23 | + AddressMessageBuilder { |
| 24 | + message: AddressMessage::default(), |
| 25 | + _phantom: PhantomData, |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + /// Sets the interface index. |
| 30 | + pub fn index(mut self, index: u32) -> Self { |
| 31 | + self.message.header.index = index; |
| 32 | + self |
| 33 | + } |
| 34 | + |
| 35 | + /// Builds [AddressMessage]. |
| 36 | + pub fn build(self) -> AddressMessage { |
| 37 | + self.message |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +impl AddressMessageBuilder<Ipv4Addr> { |
| 42 | + /// Create a new [AddressMessageBuilder] for IPv4 addresses. |
| 43 | + pub fn new() -> Self { |
| 44 | + let mut builder = Self::new_no_address_family(); |
| 45 | + builder.message.header.family = AddressFamily::Inet; |
| 46 | + builder |
| 47 | + } |
| 48 | + |
| 49 | + /// Sets the address and prefix length. |
| 50 | + pub fn address(mut self, address: Ipv4Addr, prefix_len: u8) -> Self { |
| 51 | + self.message.header.prefix_len = prefix_len; |
| 52 | + |
| 53 | + if !address.is_multicast() { |
| 54 | + self.message |
| 55 | + .attributes |
| 56 | + .push(AddressAttribute::Address(address.into())); |
| 57 | + |
| 58 | + // The IFA_LOCAL address can be set to the same value as IFA_ADDRESS. |
| 59 | + self.message |
| 60 | + .attributes |
| 61 | + .push(AddressAttribute::Local(address.into())); |
| 62 | + |
| 63 | + // Set the IFA_BROADCAST address as well. |
| 64 | + if prefix_len == 32 { |
| 65 | + self.message |
| 66 | + .attributes |
| 67 | + .push(AddressAttribute::Broadcast(address)); |
| 68 | + } else { |
| 69 | + let ip_addr = u32::from(address); |
| 70 | + let brd = Ipv4Addr::from( |
| 71 | + (0xffff_ffff_u32) >> u32::from(prefix_len) | ip_addr, |
| 72 | + ); |
| 73 | + self.message |
| 74 | + .attributes |
| 75 | + .push(AddressAttribute::Broadcast(brd)); |
| 76 | + }; |
| 77 | + } |
| 78 | + |
| 79 | + self |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +impl AddressMessageBuilder<Ipv6Addr> { |
| 84 | + /// Create a new [AddressMessageBuilder] for IPv6 addresses. |
| 85 | + pub fn new() -> Self { |
| 86 | + let mut builder = Self::new_no_address_family(); |
| 87 | + builder.message.header.family = AddressFamily::Inet6; |
| 88 | + builder |
| 89 | + } |
| 90 | + |
| 91 | + /// Sets the address and prefix length. |
| 92 | + pub fn address(mut self, address: Ipv6Addr, prefix_len: u8) -> Self { |
| 93 | + self.message.header.prefix_len = prefix_len; |
| 94 | + |
| 95 | + if address.is_multicast() { |
| 96 | + self.message |
| 97 | + .attributes |
| 98 | + .push(AddressAttribute::Multicast(address)); |
| 99 | + } else { |
| 100 | + self.message |
| 101 | + .attributes |
| 102 | + .push(AddressAttribute::Address(address.into())); |
| 103 | + |
| 104 | + // The IFA_LOCAL address can be set to the same value as IFA_ADDRESS. |
| 105 | + self.message |
| 106 | + .attributes |
| 107 | + .push(AddressAttribute::Local(address.into())); |
| 108 | + } |
| 109 | + |
| 110 | + self |
| 111 | + } |
| 112 | +} |
0 commit comments