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

// use core::fmt::Debug;
//use std::ops::{Add, Sub};
use num::bigint::BigUint;

use num_traits::identities::Zero;
use num_traits::identities::One;
use core::ops::Shl;
use core::ops::Add;
use core::cmp::Ordering;
use core::cmp::Ord;
// use num_integer::Integer;
use num_traits::cast::ToPrimitive;
// use num_traits::cast::FromPrimitive;

use std::fmt;

pub struct Prefix {
    pub num: usize,
    pub ip_bits: ::ip_bits::IpBits,
    pub net_mask: BigUint,
    pub vt_from: fn(&Prefix, usize) -> Result<Prefix, String>,
    //pub vt_to_ip_str: &'static (Fn(&Vec<u16>) -> String)
}

impl Clone for Prefix {
    fn clone(&self) -> Prefix {
        Prefix {
            num: self.num,
            ip_bits: self.ip_bits.clone(),
            net_mask: self.net_mask.clone(),
            vt_from: self.vt_from
        }
    }
}

impl PartialEq for Prefix {
    fn eq(&self, other: &Self) -> bool {
        return self.ip_bits.version == other.ip_bits.version &&
          self.num == other.num;
    }
    fn ne(&self, other: &Self) -> bool {
        !self.eq(other)
    }
}

// funny
impl fmt::Debug for Prefix {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Prefix: {}", self.num)
    }
}


impl Eq for Prefix {}

impl Ord for Prefix {
    fn cmp(&self, oth: & Prefix) -> Ordering {
        if self.ip_bits.version < oth.ip_bits.version {
            Ordering::Less
        } else if self.ip_bits.version > oth.ip_bits.version {
            Ordering::Greater
        } else {
            if self.num < oth.num {
                Ordering::Less
            } else if self.num > oth.num {
                Ordering::Greater
            } else {
                Ordering::Equal
            }
        }
    }
}
impl PartialOrd for Prefix {
    fn partial_cmp(&self, other: &Prefix) -> Option<Ordering> {
        Some(self.cmp(other))
    }

}

impl Prefix {
    //#[allow(dead_code)]
    pub fn from(&self, num: usize) -> Result<Prefix, String>{
        return (self.vt_from)(self, num)
    }

    #[allow(dead_code)]
    pub fn to_ip_str(&self) -> String {
        return (self.ip_bits.vt_as_compressed_string)(&self.ip_bits, &self.netmask())
    }

    #[allow(dead_code)]
    pub fn size(&self) -> BigUint {
      return BigUint::one() << (self.ip_bits.bits-self.num.to_usize().unwrap())
    }

    pub fn new_netmask(prefix: usize, bits: usize) -> BigUint {
        let mut mask = BigUint::zero();
        let host_prefix = bits-prefix;
        for i in 0..prefix {
            mask = mask + (BigUint::one() << (host_prefix+i));
        }
        return mask
    }

    #[allow(dead_code)]
    //#[allow(unused_variables)]
    pub fn netmask(&self) -> BigUint {
        self.net_mask.clone()
    }

    #[allow(dead_code)]
    pub fn get_prefix(&self) -> usize {
        return self.num
    }

    ///  The hostmask is the contrary of the subnet mask,
    ///  as it shows the bits that can change within the
    ///  hosts
    ///
    ///    prefix = IPAddress::Prefix32.new 24
    ///
    ///    prefix.hostmask
    ///      ///  "0.0.0.255"
    ///
    #[allow(dead_code)]
    pub fn host_mask(&self) -> BigUint {
        let mut ret = BigUint::zero();
        for _ in 0..(self.ip_bits.bits-self.num) {
            ret = ret.shl(1).add(BigUint::one());
        }
        return ret;
    }

    ///
    ///  Returns the length of the host portion
    ///  of a netmask.
    ///
    ///    prefix = Prefix128.new 96
    ///
    ///    prefix.host_prefix
    ///      ///  128
    ///
    #[allow(dead_code)]
    pub fn host_prefix(&self) -> usize {
        return (self.ip_bits.bits) - self.num;
    }

    ///
    ///  Transforms the prefix into a string of bits
    ///  representing the netmask
    ///
    ///    prefix = IPAddress::Prefix128.new 64
    ///
    ///    prefix.bits
    ///      ///  "1111111111111111111111111111111111111111111111111111111111111111"
    ///          "0000000000000000000000000000000000000000000000000000000000000000"
    ///
    #[allow(dead_code)]
    pub fn bits(&self) -> String {
        return self.netmask().to_str_radix(2)
    }
    #[allow(dead_code)]
    pub fn to_s(&self) -> String {
        return format!("{}", self.get_prefix());
    }
    #[allow(dead_code)]
    pub fn to_i(&self) -> usize {
        return self.get_prefix();
    }

    #[allow(dead_code)]
    pub fn add_prefix(&self, other: &Prefix) -> Result<Prefix, String> {
        self.from(self.get_prefix() + other.get_prefix())
    }
    #[allow(dead_code)]
    pub fn add(&self, other: usize) -> Result<Prefix, String> {
        self.from(self.get_prefix() + other)
    }
    #[allow(dead_code)]
    pub fn sub_prefix(&self, other: &Prefix) -> Result<Prefix, String> {
        return self.sub(other.get_prefix());
    }
    #[allow(dead_code)]
    pub fn sub(&self, other: usize) -> Result<Prefix, String> {
        if other > self.get_prefix() {
            return self.from(other-self.get_prefix());
        }
        return self.from(self.get_prefix() - other);
    }

}