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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
//! Connection information.

use std::{
    fmt::{self, Display, Formatter},
    net::{IpAddr, Ipv4Addr, Ipv6Addr},
    str::FromStr,
};

use str_reader::StringReader;

use crate::{NetworkType, ParseError};

/// Connection information.
#[derive(Clone)]
pub struct ConnectionInfo {
    network_type: NetworkType,
    address: ConnectionAddress,
}

impl ConnectionInfo {
    /// Create a new connection info.
    #[inline]
    pub fn new<T>(network_type: NetworkType, address: T) -> Self
    where
        T: Into<ConnectionAddress>,
    {
        let address = address.into();

        Self {
            network_type,
            address,
        }
    }

    /// Get the network type.
    #[inline]
    pub fn network_type(&self) -> &NetworkType {
        &self.network_type
    }

    /// Get the connection address.
    #[inline]
    pub fn address(&self) -> &ConnectionAddress {
        &self.address
    }
}

impl Display for ConnectionInfo {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "{} {}", self.network_type, self.address)
    }
}

impl FromStr for ConnectionInfo {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut reader = StringReader::new(s);

        let network_type = reader.parse_word()?;
        let address = reader.as_str().parse()?;

        let res = Self {
            network_type,
            address,
        };

        Ok(res)
    }
}

/// Connection address.
#[derive(Clone)]
pub enum ConnectionAddress {
    IPv4(IPv4Address),
    IPv6(IPv6Address),
    Other(OtherAddress),
}

impl ConnectionAddress {
    /// Create unicast connection address from a given IP address.
    #[inline]
    pub fn unicast<A>(addr: A) -> Self
    where
        A: Into<IpAddr>,
    {
        match addr.into() {
            IpAddr::V4(addr) => Self::IPv4(IPv4Address::unicast(addr)),
            IpAddr::V6(addr) => Self::IPv6(IPv6Address::unicast(addr)),
        }
    }
}

impl Display for ConnectionAddress {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            Self::IPv4(addr) => write!(f, "IP4 {}", addr),
            Self::IPv6(addr) => write!(f, "IP6 {}", addr),
            Self::Other(addr) => write!(f, "{} {}", addr.address_type, addr),
        }
    }
}

impl FromStr for ConnectionAddress {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut reader = StringReader::new(s);

        let address_type = reader.read_word();
        let address = reader.as_str();

        let res = match address_type {
            "IP4" => Self::IPv4(address.parse()?),
            "IP6" => Self::IPv6(address.parse()?),
            _ => Self::Other(OtherAddress::new(address_type, address.trim())),
        };

        Ok(res)
    }
}

impl From<IPv4Address> for ConnectionAddress {
    #[inline]
    fn from(addr: IPv4Address) -> Self {
        Self::IPv4(addr)
    }
}

impl From<IPv6Address> for ConnectionAddress {
    #[inline]
    fn from(addr: IPv6Address) -> Self {
        Self::IPv6(addr)
    }
}

impl From<OtherAddress> for ConnectionAddress {
    #[inline]
    fn from(addr: OtherAddress) -> Self {
        Self::Other(addr)
    }
}

/// IPv4 connection address.
#[derive(Copy, Clone)]
pub struct IPv4Address {
    address: Ipv4Addr,
    ttl: Option<u8>,
    count: Option<u32>,
}

impl IPv4Address {
    /// Create a single unicast IPv4 connection address.
    #[inline]
    pub const fn unicast(address: Ipv4Addr) -> Self {
        Self {
            address,
            ttl: None,
            count: None,
        }
    }

    /// Create multicast IPv4 connection address(es).
    #[inline]
    pub const fn multicast(address: Ipv4Addr, ttl: u8, count: Option<u32>) -> Self {
        Self {
            address,
            ttl: Some(ttl),
            count,
        }
    }

    /// Get the IP address.
    #[inline]
    pub fn address(&self) -> Ipv4Addr {
        self.address
    }

    /// Get multicast TTL value.
    #[inline]
    pub fn ttl(&self) -> Option<u8> {
        self.ttl
    }

    /// Get number of addresses.
    #[inline]
    pub fn count(&self) -> Option<u32> {
        self.count
    }
}

impl Display for IPv4Address {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "{}", self.address)?;

        if let Some(ttl) = self.ttl {
            write!(f, "/{}", ttl)?;

            if let Some(count) = self.count {
                write!(f, "/{}", count)?;
            }
        }

        Ok(())
    }
}

impl FromStr for IPv4Address {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut reader = StringReader::new(s);

        let address = reader.parse_word()?;

        reader.skip_whitespace();

        let ttl = if reader.is_empty() {
            None
        } else {
            reader.match_char('/')?;

            Some(reader.read_u8()?)
        };

        reader.skip_whitespace();

        let count = if reader.is_empty() {
            None
        } else {
            reader.match_char('/')?;

            Some(reader.read_u32()?)
        };

        reader.skip_whitespace();

        if !reader.is_empty() {
            return Err(ParseError::plain());
        }

        let res = Self {
            address,
            ttl,
            count,
        };

        Ok(res)
    }
}

/// IPv6 connection address.
#[derive(Copy, Clone)]
pub struct IPv6Address {
    address: Ipv6Addr,
    count: Option<u32>,
}

impl IPv6Address {
    /// Create a single unicast IPv6 connection address.
    #[inline]
    pub const fn unicast(address: Ipv6Addr) -> Self {
        Self {
            address,
            count: None,
        }
    }

    /// Create multicast IPv6 connection address(es).
    #[inline]
    pub const fn multicast(address: Ipv6Addr, count: Option<u32>) -> Self {
        Self { address, count }
    }

    /// Get the IPv6 address.
    #[inline]
    pub fn address(&self) -> Ipv6Addr {
        self.address
    }

    /// Get number of addresses.
    #[inline]
    pub fn count(&self) -> Option<u32> {
        self.count
    }
}

impl Display for IPv6Address {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "{}", self.address)?;

        if let Some(count) = self.count {
            write!(f, "/{}", count)?;
        }

        Ok(())
    }
}

impl FromStr for IPv6Address {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut reader = StringReader::new(s);

        let address = reader.parse_word()?;

        reader.skip_whitespace();

        let count = if reader.is_empty() {
            None
        } else {
            reader.match_char('/')?;

            Some(reader.read_u32()?)
        };

        reader.skip_whitespace();

        if !reader.is_empty() {
            return Err(ParseError::plain());
        }

        let res = Self { address, count };

        Ok(res)
    }
}

/// Other connection address.
#[derive(Clone)]
pub struct OtherAddress {
    address_type: String,
    address: String,
}

impl OtherAddress {
    /// Create a new connection address that is not IPv4 or IPv6.
    #[inline]
    pub fn new<T, A>(address_type: T, address: A) -> Self
    where
        T: ToString,
        A: ToString,
    {
        Self {
            address_type: address_type.to_string(),
            address: address.to_string(),
        }
    }

    /// Get the type of the address.
    #[inline]
    pub fn address_type(&self) -> &str {
        &self.address_type
    }

    /// Get the address.
    #[inline]
    pub fn address(&self) -> &str {
        &self.address
    }
}

impl Display for OtherAddress {
    #[inline]
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        f.write_str(&self.address)
    }
}