From d46db695494d462cd53c84f973af44135a543245 Mon Sep 17 00:00:00 2001 From: Ossi Herrala Date: Mon, 10 Feb 2025 12:16:25 +0200 Subject: [PATCH] Add Ipv4Pair and Ipv6Pair --- luomu-common/src/addr_pair.rs | 68 +++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/luomu-common/src/addr_pair.rs b/luomu-common/src/addr_pair.rs index 21e8ec5..76ec554 100644 --- a/luomu-common/src/addr_pair.rs +++ b/luomu-common/src/addr_pair.rs @@ -155,6 +155,74 @@ impl IPPair { } } +/// A pair of IPv4 addresses. Useful for IPv4 only protocols like ARP. +#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)] +pub struct Ipv4Pair { + src: Source, + dst: Destination, +} + +impl AddrPair for Ipv4Pair { + fn new(src: Source, dst: Destination) -> Self { + Self { src, dst } + } + + fn source(&self) -> Source { + self.src + } + + fn destination(&self) -> Destination { + self.dst + } + + fn flip(&self) -> Self { + Self { + src: self.dst.flip(), + dst: self.src.flip(), + } + } +} + +impl From for IPPair { + fn from(ip_pair: Ipv4Pair) -> Self { + IPPair::new_v4(ip_pair.source(), ip_pair.destination()) + } +} + +/// A pair of IPv6 addresses. +#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)] +pub struct Ipv6Pair { + src: Source, + dst: Destination, +} + +impl AddrPair for Ipv6Pair { + fn new(src: Source, dst: Destination) -> Self { + Self { src, dst } + } + + fn source(&self) -> Source { + self.src + } + + fn destination(&self) -> Destination { + self.dst + } + + fn flip(&self) -> Self { + Self { + src: self.dst.flip(), + dst: self.src.flip(), + } + } +} + +impl From for IPPair { + fn from(ip_pair: Ipv6Pair) -> Self { + IPPair::new_v6(ip_pair.source(), ip_pair.destination()) + } +} + /// TCP and UDP use 16 bit port numbers and protocol implementations need to /// handle both source and destination port numbers together. This keeps both /// port numbers.