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
use std::cmp::Ordering;
use std::fmt;
use addr::ipv4::{IpAddr, MAX_PREFIXLEN};
use addr::{IpAddrVersion, Ipv4};
#[derive(Copy, Clone, Show, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
pub struct IpNetwork(pub IpAddr, pub uint);
impl IpNetwork {
pub fn version(&self) -> IpAddrVersion {
Ipv4
}
pub fn address(&self) -> IpAddr {
let &IpNetwork(addr, _) = self;
addr & self.mask()
}
pub fn broadcast_address(&self) -> IpAddr {
self.address() | !self.mask()
}
pub fn prefix(&self) -> uint {
let &IpNetwork(_, prefix) = self;
prefix
}
pub fn host_prefix(&self) -> uint {
MAX_PREFIXLEN - self.prefix()
}
pub fn num_addresses(&self) -> uint {
use std::num::Int;
2u.pow(self.host_prefix())
}
pub fn mask(&self) -> IpAddr {
IpAddr::with_prefixlen(self.prefix())
}
pub fn range(&self) -> (IpAddr, IpAddr) {
(self.address(), self.broadcast_address())
}
pub fn contains(&self, ip: IpAddr) -> bool {
let (start, stop) = self.range();
start <= ip && ip <= stop
}
pub fn overlaps(&self, other: IpNetwork) -> bool {
other.contains(self.address()) || other.contains(self.broadcast_address())
|| self.contains(other.address()) || self.contains(other.broadcast_address())
}
pub fn iter(&self) -> Hosts {
let (start, stop) = self.range();
Hosts {
state: start,
stop: stop,
}
}
pub fn hosts_iter(&self) -> Hosts {
let (start, stop) = self.range();
Hosts {
state: start + 1,
stop: stop - 1,
}
}
}
impl PartialOrd for IpNetwork {
fn partial_cmp(&self, other: &IpNetwork) -> Option<Ordering> {
self.address().partial_cmp(&other.address())
}
}
impl Ord for IpNetwork {
fn cmp(&self, other: &IpNetwork) -> Ordering {
self.address().cmp(&other.address())
}
}
impl fmt::String for IpNetwork {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}/{}", self.address(), self.prefix())
}
}
#[derive(Copy, Clone)]
pub struct Hosts {
state: IpAddr,
stop: IpAddr,
}
impl Iterator for Hosts {
type Item = IpAddr;
fn next(&mut self) -> Option<IpAddr> {
if self.state <= self.stop {
let result = self.state;
self.state = self.state + 1;
Some(result)
} else {
None
}
}
}
impl DoubleEndedIterator for Hosts {
fn next_back(&mut self) -> Option<IpAddr> {
if self.stop >= self.state {
self.stop = self.stop - 1;
Some(self.stop)
} else {
None
}
}
}
#[cfg(test)]
mod test {
use super::IpNetwork;
use addr::ipv4::IpAddr;
#[test]
fn test_mask() {
let net = IpNetwork(IpAddr(127, 0, 0, 1), 24);
assert_eq!(net.mask(), IpAddr(255, 255, 255, 0));
}
#[test]
fn test_broadcast() {
let net = IpNetwork(IpAddr(127, 0, 0, 1), 24);
assert_eq!(net.broadcast_address(), IpAddr(127, 0, 0, 255));
}
#[test]
fn test_num_addresses() {
assert_eq!(IpNetwork(IpAddr(127, 0, 0, 1), 24).num_addresses(), 256);
assert_eq!(IpNetwork(IpAddr(127, 0, 0, 1), 0).num_addresses(), ::std::u32::MAX as uint + 1);
}
#[test]
fn test_iter() {
let net = IpNetwork(IpAddr(127, 0, 0, 1), 24);
assert_eq!(net.iter().count(), net.num_addresses());
assert_eq!(net.iter().rev().count(), net.num_addresses());
assert_eq!(net.hosts_iter().count(), net.num_addresses() - 2);
assert_eq!(net.hosts_iter().rev().count(), net.num_addresses() - 2);
}
#[test]
fn test_contains() {
let net = IpNetwork(IpAddr(127, 0, 0, 1), 24);
assert!(net.contains(IpAddr(127, 0, 0, 25)));
assert!(!net.contains(IpAddr(128, 0, 0, 25)));
}
#[test]
fn test_overlaps() {
let net1 = IpNetwork(IpAddr(127, 0, 0, 1), 24);
let net2 = IpNetwork(IpAddr(127, 0, 0, 1), 16);
let net3 = IpNetwork(IpAddr(128, 0, 0, 1), 16);
assert!(net1.overlaps(net1));
assert!(net1.overlaps(net2));
assert!(!net1.overlaps(net3));
}
}