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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
use std::fmt;
use std::net::IpAddr;
use std::str::FromStr;
use std::vec::IntoIter;

use crate::errors::NetAddsError;
use crate::iter::{IntoSmartIterator, IpAddrSmartIterator, Ipv4AddrSmartIterator, Ipv6AddrSmartIterator};

mod v4;
pub use v4::*;

mod v6;
pub use v6::*;

/// An IP address range, either IPv4 or IPv6.
///
/// This enum can contain either an [`Ipv4AddrRange`] or an [`Ipv6AddrRange`], see their
/// respective documentation for more details.
///
/// The size of an `IpAddrRange` struct may vary depending on the target operating system.
///
/// # Textual representation
///
/// `IpAddrRange` provides a [`FromStr`] implementation. The two parts are divided by `-`.
/// For IPv4, the two parts must contains an IPv4.
/// For IPv6, the two parts must contains an IPv6.
///
/// [`FromStr`]: std::str::FromStr
///
/// # Examples
///
/// ```
/// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
///
/// use net_adds::{IpAddrRange, Ipv4AddrRange, Ipv6AddrRange};
///
/// let range = IpAddrRange::V4(Ipv4AddrRange::new(Ipv4Addr::new(192, 168, 0, 0), Ipv4Addr::new(192, 168, 0, 255)));
///
/// assert_eq!(Ok(range), "192.168.0.0-192.168.0.255".parse());
///
/// let range = IpAddrRange::V6(Ipv6AddrRange::new(Ipv6Addr::from(0x1), Ipv6Addr::from(0xFFFF)));
///
/// assert_eq!(Ok(range), "::1-::ffff".parse());
/// ```
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum IpAddrRange {
    V4(Ipv4AddrRange),
    V6(Ipv6AddrRange)
}

impl IpAddrRange {
    /// Returns the first ip.
    pub fn start (self) -> IpAddr {
        match self {
            IpAddrRange::V4(v4) => IpAddr::V4(v4.start()),
            IpAddrRange::V6(v6) => IpAddr::V6(v6.start())
        }
    }

    /// Returns the last ip.
    pub fn end (self) -> IpAddr {
        match self {
            IpAddrRange::V4(v4) => IpAddr::V4(v4.end()),
            IpAddrRange::V6(v6) => IpAddr::V6(v6.end())
        }
    }

    /// Returns all ips included in the range.
    ///
    /// # Examples:
    ///
    /// ```
    /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
    ///
    /// use net_adds::{IpAddrRange, Ipv4AddrRange, Ipv6AddrRange};
    ///
    /// let a = Ipv4Addr::new(0, 0, 0, 0);
    /// let b = Ipv4Addr::new(0, 0, 0, 1);
    /// let c = Ipv4Addr::new(0, 0, 0, 2);
    ///
    /// assert_eq!(IpAddrRange::V4(Ipv4AddrRange::new(a, c)).all(), vec![
    ///     IpAddr::V4(a),
    ///     IpAddr::V4(b),
    ///     IpAddr::V4(c)
    /// ]);
    ///
    /// let a = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
    /// let b = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
    /// let c = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 2);
    ///
    /// assert_eq!(IpAddrRange::V6(Ipv6AddrRange::new(c, a)).all(), vec![
    ///     IpAddr::V6(c),
    ///     IpAddr::V6(b),
    ///     IpAddr::V6(a)
    /// ]);
    /// ```
    pub fn all (&self) -> Vec<IpAddr> {
        match self {
            IpAddrRange::V4(rv4) => rv4.all().into_iter().map(IpAddr::V4).collect(),
            IpAddrRange::V6(rv6) => rv6.all().into_iter().map(IpAddr::V6).collect()
        }
    }

    /// Returns the number of ip's included in the range.
    ///
    /// # Examples:
    ///
    /// ```
    /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
    ///
    /// use net_adds::{IpAddrRange, Ipv4AddrRange, Ipv6AddrRange};
    ///
    /// let range = IpAddrRange::V4(Ipv4AddrRange::new(Ipv4Addr::from(0), Ipv4Addr::from(0xA)));
    /// assert_eq!(range.size(), 11);
    ///
    /// let range = IpAddrRange::V6(Ipv6AddrRange::new(Ipv6Addr::from(0), Ipv6Addr::from(0xA)));
    /// assert_eq!(range.size(), 11);
    /// ```
    pub fn size (&self) -> u128 {
        match self {
            IpAddrRange::V4(v4) => u128::from(v4.size()),
            IpAddrRange::V6(v6) => v6.size()
        }
    }

    /// Returns true if the ip argument is included in the range, else returns false.
    ///
    /// Panic if IPv4 and IPv6 are mixed.
    ///
    /// # Examples:
    ///
    /// ```
    /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
    ///
    /// use net_adds::{IpAddrRange, Ipv4AddrRange, Ipv6AddrRange};
    ///
    /// let range = IpAddrRange::V4(Ipv4AddrRange::new(Ipv4Addr::from(0), Ipv4Addr::from(0xA)));
    ///
    /// assert!(range.has(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0))));
    /// assert!(range.has(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 5))));
    /// assert!(range.has(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 10))));
    ///
    /// assert!(!range.has(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 11))));
    ///
    /// let range = IpAddrRange::V6(Ipv6AddrRange::new(Ipv6Addr::from(0), Ipv6Addr::from(0xA)));
    ///
    /// assert!(range.has(IpAddr::V6(Ipv6Addr::from(0x1))));
    /// assert!(range.has(IpAddr::V6(Ipv6Addr::from(0x5))));
    /// assert!(range.has(IpAddr::V6(Ipv6Addr::from(0xA))));
    ///
    /// assert!(!range.has(IpAddr::V6(Ipv6Addr::from(0xFFFF))));
    /// ```
    pub fn has (&self, ip: IpAddr) -> bool {
        match (self, ip) {
            (IpAddrRange::V4(v4), IpAddr::V4(ipv4)) => v4.has(ipv4),
            (IpAddrRange::V6(v6), IpAddr::V6(ipv6)) => v6.has(ipv6),
            _ => panic!("cannot mix IPv4 and IPv6 to check if range includes ip")
        }
    }

    /// Returns true if the range contains IPv4, else return false.
    ///
    /// # Examples:
    ///
    /// ```
    /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
    ///
    /// use net_adds::{IpAddrRange, Ipv4AddrRange, Ipv6AddrRange};
    ///
    /// let a = Ipv4Addr::new(0, 0, 0, 0);
    /// let b = Ipv4Addr::new(0, 0, 0, 2);
    ///
    /// assert_eq!(IpAddrRange::V4(Ipv4AddrRange::new(a, b)).is_ipv4(), true);
    ///
    /// let a = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
    /// let b = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 2);
    ///
    /// assert_eq!(IpAddrRange::V6(Ipv6AddrRange::new(a, b)).is_ipv4(), false);
    /// ```
    pub fn is_ipv4 (&self) -> bool {
        match self {
            IpAddrRange::V4(_) => true,
            IpAddrRange::V6(_) => false
        }
    }

    /// Returns true if the range contains IPv6, else return false.
    ///
    /// # Examples:
    ///
    /// ```
    /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
    ///
    /// use net_adds::{IpAddrRange, Ipv4AddrRange, Ipv6AddrRange};
    ///
    /// let a = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
    /// let b = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 2);
    ///
    /// assert_eq!(IpAddrRange::V6(Ipv6AddrRange::new(a, b)).is_ipv6(), true);
    ///
    /// let a = Ipv4Addr::new(0, 0, 0, 0);
    /// let b = Ipv4Addr::new(0, 0, 0, 2);
    ///
    /// assert_eq!(IpAddrRange::V4(Ipv4AddrRange::new(a, b)).is_ipv6(), false);
    /// ```
    pub fn is_ipv6 (&self) -> bool {
        match self {
            IpAddrRange::V4(_) => false,
            IpAddrRange::V6(_) => true
        }
    }
}

impl fmt::Display for IpAddrRange {
    fn fmt (&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            IpAddrRange::V4(v4) => write!(f, "V4({})", v4),
            IpAddrRange::V6(v6) => write!(f, "V6({})", v6)
        }
    }
}

impl From<Ipv4AddrRange> for IpAddrRange {
    /// Create an `IpAddrRange::V4` from an `Ipv4AddrRange`.
    ///
    /// # Examples:
    ///
    /// ```
    /// use std::net::Ipv4Addr;
    ///
    /// use net_adds::{IpAddrRange, Ipv4AddrRange};
    ///
    /// let a = Ipv4Addr::new(0, 0, 0, 0);
    /// let b = Ipv4Addr::new(0, 0, 0, 10);
    ///
    /// assert_eq!(IpAddrRange::from(Ipv4AddrRange::new(a, b)), IpAddrRange::V4(Ipv4AddrRange::new(a, b)));
    /// ```
    fn from (range: Ipv4AddrRange) -> IpAddrRange {
        IpAddrRange::V4(range)
    }
}

impl From<Ipv6AddrRange> for IpAddrRange {
    /// Create an `IpAddrRange::V6` from an `Ipv6AddrRange`.
    ///
    /// # Examples:
    ///
    /// ```
    /// use std::net::Ipv6Addr;
    ///
    /// use net_adds::{IpAddrRange, Ipv6AddrRange};
    ///
    /// let a = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
    /// let b = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0xa);
    /// let range = Ipv6AddrRange::new(a, b);
    ///
    /// assert_eq!(IpAddrRange::from(range), IpAddrRange::V6(range));
    /// ```
    fn from (range: Ipv6AddrRange) -> IpAddrRange {
        IpAddrRange::V6(range)
    }
}

impl FromStr for IpAddrRange {
    type Err = NetAddsError;

    /// Parse a string as `IpAddrRange`.
    ///
    /// If the string representation is not valid return an `NetAddsError::RangeAddrParse(RangeAddrParseError)`.
    ///
    /// # Examples:
    ///
    /// ```
    /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
    ///
    /// use net_adds::{IpAddrRange, Ipv4AddrRange, Ipv6AddrRange};
    ///
    /// let a = Ipv4Addr::new(192, 168, 0, 0);
    /// let b = Ipv4Addr::new(192, 168, 0, 255);
    /// let range = IpAddrRange::V4(Ipv4AddrRange::new(a, b));
    ///
    /// assert_eq!("192.168.0.0-192.168.0.255".parse(), Ok(range));
    ///
    /// let a = Ipv6Addr::new(0xFFFF, 0, 0, 0, 0, 0, 0, 0xFF);
    /// let b = Ipv6Addr::new(0xFFFF, 0, 0, 0, 0, 0, 0, 0xFFFF);
    /// let range = IpAddrRange::V6(Ipv6AddrRange::new(a, b));
    ///
    /// assert_eq!("ffff::ff-ffff::ffff".parse(), Ok(range));
    /// ```
    fn from_str (s: &str) -> Result<Self, Self::Err> {
        Ipv4AddrRange::from_str(s)
            .map(IpAddrRange::V4)
            .or_else(move |_| Ipv6AddrRange::from_str(s).map(IpAddrRange::V6))
    }
}

impl IntoIterator for IpAddrRange {
    type Item = IpAddr;
    type IntoIter = IntoIter<Self::Item>;

    /// Create a `IpAddr` iterator.
    ///
    /// # Examples:
    ///
    /// ```
    /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
    ///
    /// use net_adds::{IpAddrRange, Ipv4AddrRange, Ipv6AddrRange};
    ///
    /// let a = Ipv4Addr::new(0, 0, 0, 0);
    /// let b = Ipv4Addr::new(0, 0, 0, 1);
    /// let mut iter = IpAddrRange::V4(Ipv4AddrRange::new(a, b)).into_iter();
    ///
    /// assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0))));
    /// assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 1))));
    /// assert_eq!(iter.next(), None);
    ///
    /// let a = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
    /// let b = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
    /// let mut iter = IpAddrRange::V6(Ipv6AddrRange::new(a, b)).into_iter();
    ///
    /// assert_eq!(iter.next(), Some(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0))));
    /// assert_eq!(iter.next(), Some(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1))));
    /// assert_eq!(iter.next(), None);
    /// ```
    fn into_iter (self) -> Self::IntoIter {
        self.all().into_iter()
    }
}

impl IntoSmartIterator for IpAddrRange {
    type Item = IpAddr;
    type IntoSmartIter = IpAddrSmartIterator;

    /// Create a smart `IpAddr` iterator.
    ///
    /// # Examples:
    ///
    /// ```
    /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
    ///
    /// use net_adds::{IpAddrRange, Ipv4AddrRange, Ipv6AddrRange, IntoSmartIterator};
    ///
    /// let a = Ipv4Addr::new(0, 0, 0, 0);
    /// let b = Ipv4Addr::new(0, 0, 0, 1);
    /// let mut iter = IpAddrRange::V4(Ipv4AddrRange::new(a, b)).into_smart_iter();
    ///
    /// assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0))));
    /// assert_eq!(iter.next(), Some(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 1))));
    /// assert_eq!(iter.next(), None);
    ///
    /// let a = Ipv6Addr::from(0x0);
    /// let b = Ipv6Addr::from(0x1);
    /// let mut iter = IpAddrRange::V6(Ipv6AddrRange::new(a, b)).into_smart_iter();
    ///
    /// assert_eq!(iter.next(), Some(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0))));
    /// assert_eq!(iter.next(), Some(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1))));
    /// assert_eq!(iter.next(), None);
    /// ```
    fn into_smart_iter (self) -> Self::IntoSmartIter {
        match self {
            IpAddrRange::V4(v4) => {
                IpAddrSmartIterator::V4(Ipv4AddrSmartIterator::new(v4.start(), v4.end()))
            },
            IpAddrRange::V6(v6) => {
                IpAddrSmartIterator::V6(Ipv6AddrSmartIterator::new(v6.start(), v6.end()))
            }
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RangeAddrParseError ();

impl std::error::Error for RangeAddrParseError {}

impl fmt::Display for RangeAddrParseError {
    fn fmt (&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", "invalid IP address range syntax")
    }
}

#[cfg(test)]
mod test {
    use std::net::{Ipv4Addr, Ipv6Addr};

    use crate::{IpAddrRange, Ipv4AddrRange, Ipv6AddrRange, NetAddsError, RangeAddrParseError};

    #[test]
    fn from_str () {
        // IPv4
        let range = IpAddrRange::V4(Ipv4AddrRange::new(Ipv4Addr::new(0, 0, 0, 1), Ipv4Addr::new(0, 0, 0, 10)));
        assert_eq!("0.0.0.1-0.0.0.10".parse(), Ok(range));

        let range = IpAddrRange::V4(Ipv4AddrRange::new(Ipv4Addr::new(0, 0, 0, 0), Ipv4Addr::new(255, 255, 255, 255)));
        assert_eq!("0.0.0.0-255.255.255.255".parse(), Ok(range));

        let range = IpAddrRange::V4(Ipv4AddrRange::new(Ipv4Addr::new(255, 255, 255, 255), Ipv4Addr::new(0, 0, 0, 0)));
        assert_eq!("255.255.255.255-0.0.0.0".parse(), Ok(range));

        let err = Err(NetAddsError::RangeAddrParse(RangeAddrParseError()));

        // one ip is out of range.
        assert_eq!(err, "256.0.0.1-255.0.0.1".parse::<IpAddrRange>());

        // only one ip provided.
        assert_eq!(err, "127.0.0.1".parse::<IpAddrRange>());

        // to many ip provided.
        assert_eq!(err, "255.0.0.1-255.0.0.10-255.0.0.20".parse::<IpAddrRange>());

        // no ip before `-`.
        assert_eq!(err, "-127.0.0.1".parse::<IpAddrRange>());

        // no ip after `-`.
        assert_eq!(err, "127.0.0.1-".parse::<IpAddrRange>());

        // IPv6
        let a = Ipv6Addr::from(0x1);
        let b = Ipv6Addr::from(0xA);
        let range = IpAddrRange::V6(Ipv6AddrRange::new(a, b));
        assert_eq!(Ok(range), "::1-0:0:0:0:0:0:0:a".parse());
        assert_eq!(Ok(range), "0:0:0:0:0:0:0:1-::a".parse());
        assert_eq!(Ok(range), "::1-::a".parse());
        assert_eq!(Ok(range), "0:0:0:0:0:0:0:1-0:0:0:0:0:0:0:a".parse());

        let a = Ipv6Addr::new(0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF);
        let b = Ipv6Addr::from(0);
        let range = Ipv6AddrRange::new(a, b);
        assert_eq!(Ok(range), "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff-0:0:0:0:0:0:0:0".parse());
        assert_eq!(Ok(range), "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff-::".parse());

        let err = Err(NetAddsError::RangeAddrParse(RangeAddrParseError()));

        // one ip is out of range (invalid char "z").
        assert_eq!(err, "::fz-::fffe".parse::<Ipv6AddrRange>());

        // only one ip provided.
        assert_eq!(err, "0:0:0:0:0:0:0:0".parse::<Ipv6AddrRange>());

        // to many ip provided.
        assert_eq!(err, "::-::1-::2".parse::<Ipv6AddrRange>());

        // no ip before `-`.
        assert_eq!(err, "-::1".parse::<Ipv6AddrRange>());

        // no ip after `-`.
        assert_eq!(err, "::1-".parse::<Ipv6AddrRange>());
    }
}