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
use core::fmt;
use core::str::FromStr;

use super::{impl_try_from_any, Address, Prefix, PrefixLength};
use crate::{
    any,
    error::Error,
    fmt::AddressDisplay,
    traits::{self, primitive::Address as _, Afi, Prefix as _},
    Ipv4, Ipv6,
};

#[allow(clippy::wildcard_imports)]
mod private {
    use super::*;

    /// An IP interface, consisting of am address and prefix length.
    #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
    pub struct Interface<A: Afi> {
        address: Address<A>,
        length: PrefixLength<A>,
    }

    impl<A: Afi> Interface<A> {
        /// Construct a new [`Interface<A>`] from an address and prefix length.
        pub const fn new(address: Address<A>, length: PrefixLength<A>) -> Self {
            Self { address, length }
        }

        /// Get the address of this interface.
        pub const fn address(&self) -> Address<A> {
            self.address
        }

        /// Get the prefix length of this interface.
        pub const fn length(&self) -> PrefixLength<A> {
            self.length
        }
    }
}

pub use self::private::Interface;

impl<A: Afi> traits::Interface for Interface<A> {
    type Address = Address<A>;
    type Prefix = Prefix<A>;
    type PrefixLength = PrefixLength<A>;

    fn network(&self) -> Self::Address {
        self.trunc().network()
    }

    fn addr(&self) -> Self::Address {
        self.address()
    }

    fn trunc(&self) -> Self::Prefix {
        Self::Prefix::from(*self)
    }

    fn prefix_len(&self) -> Self::PrefixLength {
        self.length()
    }

    fn broadcast(&self) -> Self::Address {
        let prefix = self.trunc();
        prefix.network() | prefix.hostmask()
    }
}

impl<A: Afi> From<Address<A>> for Interface<A> {
    fn from(addr: Address<A>) -> Self {
        Self::new(addr, PrefixLength::MAX)
    }
}

impl<A: Afi> FromStr for Interface<A> {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        A::Primitive::parse_prefix(s).and_then(|(addr, len)| {
            Ok(Self::new(
                Address::new(addr),
                PrefixLength::from_primitive(len)?,
            ))
        })
    }
}

impl_try_from_any! {
    any::Interface {
        any::Interface::Ipv4 => Interface<Ipv4>,
        any::Interface::Ipv6 => Interface<Ipv6>,
    }
}

impl<A: Afi> fmt::Display for Interface<A>
where
    A::Primitive: AddressDisplay<A>,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}/{}", self.address(), self.length())
    }
}

#[cfg(feature = "ipnet")]
impl From<ipnet::Ipv4Net> for Interface<Ipv4> {
    fn from(net: ipnet::Ipv4Net) -> Self {
        let address = net.addr().into();
        let length = PrefixLength::from_primitive(net.prefix_len())
            .expect("we trusted `ipnet` to enforce length bounds");
        Self::new(address, length)
    }
}

#[cfg(feature = "ipnet")]
impl From<ipnet::Ipv6Net> for Interface<Ipv6> {
    fn from(net: ipnet::Ipv6Net) -> Self {
        let address = net.addr().into();
        let length = PrefixLength::from_primitive(net.prefix_len())
            .expect("we trusted `ipnet` to enforce length bounds");
        Self::new(address, length)
    }
}

#[cfg(any(test, feature = "arbitrary"))]
use proptest::{
    arbitrary::{any_with, Arbitrary, ParamsFor, StrategyFor},
    strategy::{BoxedStrategy, Strategy},
};

#[cfg(any(test, feature = "arbitrary"))]
impl<A: Afi> Arbitrary for Interface<A>
where
    Address<A>: Arbitrary,
    StrategyFor<Address<A>>: 'static,
    PrefixLength<A>: Arbitrary,
    StrategyFor<PrefixLength<A>>: 'static,
{
    type Parameters = ParamsFor<(Address<A>, PrefixLength<A>)>;
    type Strategy = BoxedStrategy<Self>;

    fn arbitrary_with(params: Self::Parameters) -> Self::Strategy {
        (
            any_with::<Address<A>>(params.0),
            any_with::<PrefixLength<A>>(params.1),
        )
            .prop_map(|(address, length)| Self::new(address, length))
            .boxed()
    }
}