iptrie/prefix/
cover.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use std::cmp::Ordering;
use ipnet::{Ipv4Net,Ipv6Net};
use std::net::{Ipv4Addr,Ipv6Addr};
use crate::*;

#[doc(hidden)]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum IpPrefixCoverage { NoCover, WiderRange, SameRange }

impl IpPrefixCoverage {
    #[inline] fn is_wider(&self) -> bool { *self == IpPrefixCoverage::WiderRange }
    #[inline] fn is_same(&self) -> bool { *self == IpPrefixCoverage::SameRange }
    #[inline] fn is_covering(&self) -> bool { *self != IpPrefixCoverage::NoCover }
}

/// A trait to check the prefix coverage
pub trait IpPrefixCovering<P>
{
    /// Checks the coverage of this prefix against a set of adress
    ///
    /// * `SameRange` means that the two prefixes are equivalent
    /// * `WiderRange` means that this prefix includes the other
    /// * `NoCover` groups all the other cases
    ///
    /// # Difference with `PartialEq`
    /// Two prefixes could be different but equivalent regarding to
    /// the range of addresses they cover.
    /// ```
    /// # use iptrie::*;
    /// use ipnet::Ipv4Net;
    /// let a = "1.1.1.1/16".parse::<Ipv4Net>().unwrap();
    /// let b = "1.1.2.2/16".parse::<Ipv4Net>().unwrap();
    /// assert!( a != b ); // since host addr are different
    /// assert! (a.covers_equally(&b) ); // but prefixes are equivalent
    /// ```
    #[doc(hidden)]
    fn covering(&self, other: &P) -> IpPrefixCoverage;

    /// Checks the coverage of this prefix against a set of addresses
    ///
    /// # Difference with `PartialEq`
    /// Two prefixes could be different but equivalent regarding to
    /// the range of addresses they cover.
    /// ```
    /// # use iptrie::*;
    /// use ipnet::Ipv4Net;
    /// let a = "1.1.1.1/16".parse::<Ipv4Net>().unwrap();
    /// let b = "1.1.2.2/16".parse::<Ipv4Net>().unwrap();
    /// assert!( a != b ); // host addr are different
    /// assert! (a.covers(&b) && b.covers(&a)); // but prefixes are equivalent
    /// ```
    #[inline]
    fn covers(&self, other: &P) -> bool{ self.covering(other).is_covering() }

    #[inline] #[doc(hidden)]
    fn covers_striclty(&self, other: &P) -> bool{ self.covering(other).is_wider() }
    #[inline] #[doc(hidden)]
    fn covers_equally(&self, other: &P) -> bool{ self.covering(other).is_same() }
}

impl<P:IpPrefix> IpPrefixCovering<Self> for P
{
    #[inline]
    fn covering(&self, other: &Self) -> IpPrefixCoverage {
        if other.bitslot() & self.bitmask() != self.bitslot_trunc() {
            IpPrefixCoverage::NoCover
        } else {
            match self.len().cmp(&other.len()) {
                Ordering::Less => IpPrefixCoverage::WiderRange,
                Ordering::Equal => IpPrefixCoverage::SameRange,
                Ordering::Greater => IpPrefixCoverage::NoCover
            }
        }
    }
}

// coverage impl. between prefixes with same slots and same addr type
macro_rules! ipcover {
    ($self:ty, $other:ty) => {
        impl IpPrefixCovering<$other> for $self {
            #[inline]
            fn covering(&self, other: &$other) -> IpPrefixCoverage {
                if other.bitslot() & self.bitmask() != self.bitslot_trunc() {
                    IpPrefixCoverage::NoCover
                } else {
                    match self.len().cmp(&other.len()) {
                        Ordering::Less => IpPrefixCoverage::WiderRange,
                        Ordering::Equal => IpPrefixCoverage::SameRange,
                        Ordering::Greater => IpPrefixCoverage::NoCover
                    }
                }
            }
        }
    }
}

ipcover!(Ipv4Prefix, Ipv4Net);
ipcover!(Ipv4Prefix, Ipv4Addr);

ipcover!(Ipv4Net, Ipv4Prefix);
ipcover!(Ipv4Net, Ipv4Addr);

ipcover!(Ipv6Prefix, Ipv6Net);
ipcover!(Ipv6Prefix, Ipv6Addr);

ipcover!(Ipv6Net, Ipv6Prefix);
ipcover!(Ipv6Net, Ipv6Addr);




// coverage impl. relative to IpPrefix56 or IpNetAddr
// (which has a slot shorter than the others)
macro_rules! ipcover_for_ipv6_on_u64 {
    ($short:ty, $other:ty) => {
        impl IpPrefixCovering<$other> for $short {
            #[inline]
            fn covering(&self, other: &$other) -> IpPrefixCoverage {
                if ((other.bitslot() >> 64) as u64) & self.bitmask() != self.bitslot_trunc() {
                    IpPrefixCoverage::NoCover
                } else {
                    match self.len().cmp(&other.len()) {
                        Ordering::Less => IpPrefixCoverage::WiderRange,
                        Ordering::Equal => IpPrefixCoverage::SameRange,
                        Ordering::Greater => IpPrefixCoverage::NoCover
                    }
                }
            }
        }
    }
}

ipcover_for_ipv6_on_u64!(Ipv6NetRouting, Ipv6Prefix);
ipcover_for_ipv6_on_u64!(Ipv6NetRouting, Ipv6Net);
ipcover_for_ipv6_on_u64!(Ipv6NetRouting, Ipv6Addr);


macro_rules! ipcover_of_ipv6_on_u64 {
    ($self:ty, $short:ty) => {
        impl IpPrefixCovering<$short> for $self {
            #[inline]
            fn covering(&self, other: &$short) -> IpPrefixCoverage {
                if ((other.bitslot() as u128) << 64) & self.bitmask() != self.bitslot_trunc() {
                    IpPrefixCoverage::NoCover
                } else {
                    match self.len().cmp(&other.len()) {
                        Ordering::Less => IpPrefixCoverage::WiderRange,
                        Ordering::Equal => IpPrefixCoverage::SameRange,
                        Ordering::Greater => IpPrefixCoverage::NoCover
                    }
                }
            }
        }
    }
}

ipcover_of_ipv6_on_u64!(Ipv6Prefix, Ipv6NetRouting);
ipcover_of_ipv6_on_u64!(Ipv6Net, Ipv6NetRouting);




// Equality between prefix...
macro_rules! ipprefix_eq {
    ($self:ty, $other:ty) => {
        impl PartialEq<$other> for $self {
            #[inline]
            fn eq(&self, other: &$other) -> bool {
                self.covers_equally(other)
            }
        }
    }
}

ipprefix_eq!(Ipv6Net,Ipv6NetRouting);
ipprefix_eq!(Ipv6Net,Ipv6Prefix);

ipprefix_eq!(Ipv6NetRouting,Ipv6Net);
ipprefix_eq!(Ipv6NetRouting,Ipv6Prefix);

ipprefix_eq!(Ipv6Prefix,Ipv6Net);
ipprefix_eq!(Ipv6Prefix,Ipv6NetRouting);