|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +use derive_more::{From, IsVariant}; |
| 4 | +use netlink_packet_core::{ |
| 5 | + parse_ip, DecodeError, DefaultNla, ErrorContext, Nla, NlaBuffer, Parseable, |
| 6 | +}; |
| 7 | +use std::net::IpAddr; |
| 8 | + |
| 9 | +use crate::constants::{ |
| 10 | + CTA_IP_V4_DST, CTA_IP_V4_SRC, CTA_IP_V6_DST, CTA_IP_V6_SRC, |
| 11 | +}; |
| 12 | + |
| 13 | +#[derive(Clone, Debug, PartialEq, Eq, From, IsVariant)] |
| 14 | +pub enum IPTuple { |
| 15 | + SourceAddress(IpAddr), |
| 16 | + #[from(ignore)] |
| 17 | + DestinationAddress(IpAddr), |
| 18 | + Other(DefaultNla), |
| 19 | +} |
| 20 | + |
| 21 | +pub const IPV4_LEN: usize = 4; |
| 22 | +pub const IPV6_LEN: usize = 16; |
| 23 | + |
| 24 | +// Helper function needed for implementing the Nla trait |
| 25 | +pub fn emit_ip(addr: &IpAddr, buf: &mut [u8]) { |
| 26 | + match addr { |
| 27 | + IpAddr::V4(ip) => { |
| 28 | + buf[..IPV4_LEN].copy_from_slice(ip.octets().as_slice()); |
| 29 | + } |
| 30 | + IpAddr::V6(ip) => { |
| 31 | + buf[..IPV6_LEN].copy_from_slice(ip.octets().as_slice()); |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl Nla for IPTuple { |
| 37 | + fn value_len(&self) -> usize { |
| 38 | + match self { |
| 39 | + IPTuple::SourceAddress(attr) => match *attr { |
| 40 | + IpAddr::V4(_) => IPV4_LEN, |
| 41 | + IpAddr::V6(_) => IPV6_LEN, |
| 42 | + }, |
| 43 | + IPTuple::DestinationAddress(attr) => match *attr { |
| 44 | + IpAddr::V4(_) => IPV4_LEN, |
| 45 | + IpAddr::V6(_) => IPV6_LEN, |
| 46 | + }, |
| 47 | + IPTuple::Other(attr) => attr.value_len(), |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + fn kind(&self) -> u16 { |
| 52 | + match self { |
| 53 | + IPTuple::SourceAddress(attr) => match *attr { |
| 54 | + IpAddr::V4(_) => CTA_IP_V4_SRC, |
| 55 | + IpAddr::V6(_) => CTA_IP_V6_SRC, |
| 56 | + }, |
| 57 | + IPTuple::DestinationAddress(attr) => match *attr { |
| 58 | + IpAddr::V4(_) => CTA_IP_V4_DST, |
| 59 | + IpAddr::V6(_) => CTA_IP_V6_DST, |
| 60 | + }, |
| 61 | + IPTuple::Other(attr) => attr.kind(), |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + fn emit_value(&self, buffer: &mut [u8]) { |
| 66 | + match self { |
| 67 | + IPTuple::SourceAddress(attr) => emit_ip(attr, buffer), |
| 68 | + IPTuple::DestinationAddress(attr) => emit_ip(attr, buffer), |
| 69 | + IPTuple::Other(attr) => attr.emit_value(buffer), |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | +impl<'buffer, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'buffer T>> |
| 74 | + for IPTuple |
| 75 | +{ |
| 76 | + fn parse(buf: &NlaBuffer<&'buffer T>) -> Result<Self, DecodeError> { |
| 77 | + let kind = buf.kind(); |
| 78 | + let payload = buf.value(); |
| 79 | + let nla = match kind { |
| 80 | + CTA_IP_V4_SRC | CTA_IP_V6_SRC => Self::SourceAddress( |
| 81 | + parse_ip(payload).context("invalid SourceAddress value")?, |
| 82 | + ), |
| 83 | + CTA_IP_V4_DST | CTA_IP_V6_DST => Self::DestinationAddress( |
| 84 | + parse_ip(payload) |
| 85 | + .context("invalid DestinationAddress value")?, |
| 86 | + ), |
| 87 | + _ => IPTuple::Other(DefaultNla::parse(buf)?), |
| 88 | + }; |
| 89 | + Ok(nla) |
| 90 | + } |
| 91 | +} |
0 commit comments