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
// Copyright 2015 Axel Rasmussen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::error::*;
use data_encoding::HEXLOWER_PERMISSIVE;
use serde::de::{Deserialize, Deserializer, Unexpected, Visitor};
use serde::ser::{Serialize, Serializer};
use std::cmp::Ordering;
use std::fmt;
use std::marker::PhantomData;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::str::FromStr;

struct ParseableVisitor<T: FromStr<Err = Error>> {
    phantom: PhantomData<T>,
}

impl<'de, T: FromStr<Err = Error>> Visitor<'de> for ParseableVisitor<T> {
    type Value = T;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("a parseable value")
    }

    fn visit_str<E: ::serde::de::Error>(self, v: &str) -> ::std::result::Result<Self::Value, E> {
        match v.parse::<T>() {
            Err(e) => Err(E::invalid_value(
                Unexpected::Str(v),
                &e.to_string().as_str(),
            )),
            Ok(addr) => Ok(addr),
        }
    }
}

impl<T: FromStr<Err = Error>> Default for ParseableVisitor<T> {
    fn default() -> Self {
        ParseableVisitor {
            phantom: PhantomData,
        }
    }
}

fn increment_ip_bytes(bytes: &mut [u8]) {
    for byte in bytes.iter_mut().rev() {
        match byte.checked_add(1) {
            None => *byte = 0,
            Some(new_byte) => {
                *byte = new_byte;
                return;
            }
        }
    }
}

/// Returns the IP address which immediately follows the given IP address. If
/// the increment overflowed (i.e., the given input IP was already the largest
/// possible IP address), None is returned instead.
pub fn increment_ip(ip: IpAddr) -> Option<IpAddr> {
    match ip {
        IpAddr::V4(ip) => {
            let mut bytes: [u8; 4] = ip.octets();
            increment_ip_bytes(&mut bytes);
            if bytes.iter().fold(true, |acc, byte| acc && *byte == 0) {
                return None;
            }
            Some(Ipv4Addr::from(bytes).into())
        }
        IpAddr::V6(ip) => {
            let mut bytes: [u8; 16] = ip.octets();
            increment_ip_bytes(&mut bytes);
            if bytes.iter().fold(true, |acc, byte| acc && *byte == 0) {
                return None;
            }
            Some(Ipv6Addr::from(bytes).into())
        }
    }
}

/// Compare the two given IP addresses, returning the ordering between them. If
/// the given IP addresses are not of the same type (V4/V6), returns None
/// instead.
pub fn compare_ips(a: IpAddr, b: IpAddr) -> Option<Ordering> {
    Some(match a {
        IpAddr::V4(a) => match b {
            IpAddr::V4(b) => a.cmp(&b),
            IpAddr::V6(_) => return None,
        },
        IpAddr::V6(a) => match b {
            IpAddr::V4(_) => return None,
            IpAddr::V6(b) => a.cmp(&b),
        },
    })
}

/// Return the smaller of the two IP addresses, or None if they cannot be
/// compared (see compare_ips).
pub fn min_ip(a: IpAddr, b: IpAddr) -> Option<IpAddr> {
    match compare_ips(a, b) {
        None => None,
        Some(ordering) => Some(match ordering {
            Ordering::Greater => b,
            _ => a,
        }),
    }
}

/// Return the larger of the two IP addresses, or None if they cannot be
/// compared (see compare_ips).
pub fn max_ip(a: IpAddr, b: IpAddr) -> Option<IpAddr> {
    match compare_ips(a, b) {
        None => None,
        Some(ordering) => Some(match ordering {
            Ordering::Less => b,
            _ => a,
        }),
    }
}

/// This structure denotes a hardware MAC address.
#[derive(Clone, Copy, Debug, Hash, Eq, Ord, PartialEq, PartialOrd)]
pub struct HardwareAddr {
    address: [u8; 6],
}

impl HardwareAddr {
    /// Return the 6 bytes which make up the MAC address as a slice.
    pub fn as_bytes(&self) -> &[u8] {
        &self.address
    }
}

impl fmt::Display for HardwareAddr {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
            self.address[0],
            self.address[1],
            self.address[2],
            self.address[3],
            self.address[4],
            self.address[5]
        )
    }
}

impl FromStr for HardwareAddr {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self> {
        // Common / valid MAC address formats are "xx:xx:xx:xx:xx:xx",
        // "xx-xx-xx-xx-xx-xx", and "xxxx.xxxx.xxxx" where "xx" is a hex byte.
        let normalized = s.replace(":", "");
        let normalized = normalized.replace("-", "");
        let normalized = normalized.replace(".", "");
        let address_vec = match HEXLOWER_PERMISSIVE.decode(normalized.as_bytes()) {
            Ok(data) => data,
            Err(e) => return Err(Error::HexDecode(e)),
        };
        if address_vec.len() != 6 {
            return Err(Error::InvalidArgument(format!(
                "invalid MAC address '{}', expected 6 bytes found {}",
                s,
                address_vec.len()
            )));
        }

        let mut address = [0_u8; 6];
        for (dst, src) in address.iter_mut().zip(address_vec.into_iter()) {
            *dst = src;
        }

        Ok(HardwareAddr { address: address })
    }
}

impl Serialize for HardwareAddr {
    fn serialize<S: Serializer>(&self, serializer: S) -> ::std::result::Result<S::Ok, S::Error> {
        serializer.serialize_str(self.to_string().as_str())
    }
}

impl<'de> Deserialize<'de> for HardwareAddr {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> ::std::result::Result<Self, D::Error> {
        deserializer.deserialize_str(ParseableVisitor::<HardwareAddr>::default())
    }
}

/// Apply the given mask to the given IP address bytes. See IpNet::apply_mask
/// for details on behavior.
fn apply_ip_mask_bytes(ip: &mut [u8], mask: &[u8], invert: bool, set: bool) {
    debug_assert!(ip.len() == mask.len());
    for (b, m) in ip.iter_mut().zip(mask.iter()) {
        let m: u8 = match invert {
            false => *m,
            true => !*m,
        };

        if set {
            *b |= m;
        } else {
            *b &= m;
        }
    }
}

/// Apply the given mask to the given IP address. See IpNet::apply_mask for
/// details on behavior.
fn apply_ip_mask(ip: IpAddr, mask: &[u8], invert: bool, set: bool) -> IpAddr {
    debug_assert!(mask.len() == 16);
    let mut bytes: [u8; 16] = match ip {
        IpAddr::V4(ip) => ip.to_ipv6_compatible().octets(),
        IpAddr::V6(ip) => ip.octets(),
    };
    apply_ip_mask_bytes(&mut bytes, mask, invert, set);
    let masked_ip = Ipv6Addr::from(bytes);
    match ip.is_ipv4() {
        false => IpAddr::V6(masked_ip),
        true => {
            let bytes = masked_ip.octets();
            IpAddr::V4(Ipv4Addr::new(bytes[12], bytes[13], bytes[14], bytes[15]))
        }
    }
}

/// An IpNet represents an IP network. Networks are typically identified in CIDR
/// notation, like (for example) "192.0.0.0/24".
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
pub struct IpNet {
    ip: IpAddr,
    mask: [u8; 16],
}

impl IpNet {
    /// Return the network IP. This address will have been normalized, such that
    /// any non-masked bits will have been turned off during construction.
    pub fn get_ip(&self) -> IpAddr {
        self.ip
    }

    /// Return the network mask, as a byte slice. This slice will always be 16
    /// bytes long, even if this is an IPv4 network.
    pub fn get_mask(&self) -> &[u8] {
        &self.mask
    }

    /// Apply this network's mask to the given IP address, returning the
    /// modified copy.
    ///
    /// The "default" behavior is when invert=false and set=false. In this
    /// case, any bits which are "0" in the mask are turned off in the given
    /// IP address' bytes.
    ///
    /// If invert=true, then each bit in the mask is flipped before it is
    /// applied - in other words, we apply the inverse mask.
    ///
    /// If set=true, then we switch from bitwise AND to bitwise OR, meaning
    /// instead of the behavior described above, any bits which are "1" in the
    /// mask are turned *on* in the IP address, and other bits are left
    /// unchanged.
    pub fn apply_mask(&self, ip: IpAddr, invert: bool, set: bool) -> IpAddr {
        apply_ip_mask(ip, &self.mask, invert, set)
    }

    /// Returns the number of "1" bits in this network's mask. Although the mask
    /// is always 16 bytes long, for IPv4 networks only the last 4 bytes of the
    /// mask are considered.
    pub fn get_one_bits(&self) -> usize {
        self.mask
            .iter()
            .skip(if self.ip.is_ipv4() { 12 } else { 0 })
            .fold(0_u32, |acc, &b| acc + b.count_ones()) as usize
    }

    /// Returns whether or not this network's mask is "canonical" - i.e., if all
    /// of its mask's "1" bits are contiguous (no "0" bits in-between them).
    pub fn is_canonical(&self) -> bool {
        let first_zero_bit = match self.mask.iter().position(|b| *b != 0xff_u8) {
            None => return true,
            Some(idx) => idx,
        };
        if self.mask[first_zero_bit].count_zeros() != self.mask[first_zero_bit].trailing_zeros() {
            return false;
        }
        self.mask
            .iter()
            .skip(first_zero_bit + 1)
            .fold(true, |acc, byte| acc && (*byte == 0x00_u8))
    }

    /// Return the netmask IP address for this network.
    pub fn netmask(&self) -> IpAddr {
        self.apply_mask(self.ip, false, true)
    }

    /// Return the broadcast IP address for this network.
    pub fn broadcast(&self) -> IpAddr {
        self.apply_mask(self.ip, true, true)
    }

    /// Return whether or not the given IP address is contained within this
    /// network. If strict=true, then the "network address" and "broadcast
    /// address" are both not considered to be "within" this network (this is
    /// useful if you want to check if the given IP address is actually usable
    /// for a host on the network).
    pub fn contains(&self, ip: IpAddr, strict: bool) -> bool {
        let contains = self.apply_mask(ip, false, false) == self.ip;
        match strict {
            false => contains,
            true => contains && ip != self.ip && ip != self.broadcast(),
        }
    }

    /// Increment the given IP address as if with increment_ip(), but returning
    /// None if the resulting IP address does not lie within this network. The
    /// "strict" parameter behaves as IpNet::contains() describes.
    pub fn increment_in(&self, ip: IpAddr, strict: bool) -> Option<IpAddr> {
        let next_ip = match increment_ip(ip) {
            None => return None,
            Some(ip) => ip,
        };
        match self.contains(next_ip, strict) {
            false => None,
            true => Some(next_ip),
        }
    }

    /// Return the first IP address which falls within this network.
    pub fn first(&self) -> Option<IpAddr> {
        increment_ip(self.ip)
    }

    /// Return the last non-broadcast IP address which falls within this
    /// network.
    pub fn last(&self) -> IpAddr {
        match self.broadcast() {
            IpAddr::V4(ip) => {
                let mut bytes = ip.octets();
                let idx = bytes.len() - 1;
                bytes[idx] -= 1;
                Ipv4Addr::from(bytes).into()
            }
            IpAddr::V6(ip) => {
                let mut bytes = ip.octets();
                let idx = bytes.len() - 1;
                bytes[idx] -= 1;
                Ipv6Addr::from(bytes).into()
            }
        }
    }
}

impl fmt::Display for IpNet {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{}/{}",
            self.ip,
            match self.is_canonical() {
                false => HEXLOWER_PERMISSIVE
                    .encode(&self.mask)
                    .chars()
                    .skip(if self.ip.is_ipv4() { 24 } else { 0 })
                    .collect::<String>(),
                true => self.get_one_bits().to_string(),
            }
        )
    }
}

impl FromStr for IpNet {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self> {
        let (ip, mask): (&str, &str) = s.split_at(match s.find('/') {
            None => {
                return Err(Error::InvalidArgument(format!(
                    "invalid IP network specifier '{}'",
                    s
                )));
            }
            Some(idx) => idx,
        });
        let ip: IpAddr = ip.parse()?;
        let mask: &str = &mask[1..];

        let mut mask_vec: Vec<u8> = vec![
            0xff_u8;
            match ip.is_ipv4() {
                false => 0,
                true => 12,
            }
        ];

        let mask_is_hex = (ip.is_ipv4() && mask.len() == 8) || (ip.is_ipv6() && mask.len() == 32);
        if mask_is_hex {
            mask_vec.extend(
                match HEXLOWER_PERMISSIVE.decode(mask.as_bytes()) {
                    Ok(data) => data,
                    Err(e) => return Err(Error::HexDecode(e)),
                }
                .into_iter(),
            );
        } else {
            let ones = u8::from_str_radix(mask, 10)?;
            let mut v = vec![0xff_u8; (ones / 8) as usize];
            let extra_ones = ones % 8;
            if extra_ones > 0 {
                v.push((0xff_u8 >> (8 - extra_ones)) << (8 - extra_ones));
            }
            mask_vec.extend(v.into_iter());
        }

        let mut mask = [0_u8; 16];
        for (dst, src) in mask.iter_mut().zip(mask_vec.into_iter()) {
            *dst = src;
        }

        Ok(IpNet {
            ip: apply_ip_mask(ip, &mask, false, false),
            mask: mask,
        })
    }
}

impl Serialize for IpNet {
    fn serialize<S: Serializer>(&self, serializer: S) -> ::std::result::Result<S::Ok, S::Error> {
        serializer.serialize_str(self.to_string().as_str())
    }
}

impl<'de> Deserialize<'de> for IpNet {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> ::std::result::Result<Self, D::Error> {
        deserializer.deserialize_str(ParseableVisitor::<IpNet>::default())
    }
}