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
use std::fmt;
use addr::{IpAddrVersion, IpAddr};
pub use self::IpNetwork::*;
pub mod ipv4;
pub mod ipv6;
#[derive(Copy, Clone, Show, PartialEq, Eq, PartialOrd,
Ord, Hash, RustcEncodable, RustcDecodable)]
pub enum IpNetwork {
Ipv4Network(ipv4::IpNetwork),
Ipv6Network(ipv6::IpNetwork),
}
macro_rules! mirror(
($addr:expr, $net:ident => $value:expr) => ({
match $addr {
Ipv4Network(ref $net) => $value,
Ipv6Network(ref $net) => $value,
}
});
(ip: $addr:expr, $net:ident => $value:expr) => ({
use addr::IpAddr::*;
match $addr {
Ipv4Network(ref $net) => Ipv4Addr($value),
Ipv6Network(ref $net) => Ipv6Addr($value),
}
});
(net: $addr:expr, $net:ident => $value:expr) => ({
match $addr {
Ipv4Network(ref $net) => Ipv4Network($value),
Ipv6Network(ref $net) => Ipv6Network($value),
}
});
);
impl IpNetwork {
pub fn version(&self) -> IpAddrVersion {
mirror!(*self, net => net.version())
}
pub fn address(&self) -> IpAddr {
mirror!(ip: *self, net => net.address())
}
pub fn broadcast_address(&self) -> IpAddr {
mirror!(ip: *self, net => net.broadcast_address())
}
pub fn prefix(&self) -> uint {
mirror!(*self, net => net.prefix())
}
pub fn host_prefix(&self) -> uint {
mirror!(*self, net => net.host_prefix())
}
pub fn num_addresses(&self) -> uint {
mirror!(*self, net => net.num_addresses())
}
pub fn mask(&self) -> IpAddr {
mirror!(ip: *self, net => net.mask())
}
pub fn range(&self) -> (IpAddr, IpAddr) {
use addr::IpAddr::*;
match *self {
Ipv4Network(ref net) => {
let (start, stop) = net.range();
(Ipv4Addr(start), Ipv4Addr(stop))
}
Ipv6Network(ref net) => {
let (start, stop) = net.range();
(Ipv6Addr(start), Ipv6Addr(stop))
}
}
}
pub fn contains(&self, ip: IpAddr) -> bool {
use addr::IpAddr::*;
match (*self, ip) {
(Ipv4Network(ref net), Ipv4Addr(ip)) => net.contains(ip),
(Ipv6Network(ref net), Ipv6Addr(ip)) => net.contains(ip),
_ => false,
}
}
pub fn overlaps(&self, other: IpNetwork) -> bool {
match (*self, other) {
(Ipv4Network(ref neta), Ipv4Network(netb)) => neta.overlaps(netb),
(Ipv6Network(ref neta), Ipv6Network(netb)) => neta.overlaps(netb),
_ => false,
}
}
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 fmt::String for IpNetwork {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
mirror!(*self, net => net.fmt(f))
}
}
#[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
}
}
}