]>
Commit | Line | Data |
---|---|---|
85aaf69f SL |
1 | // Copyright 2015 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
c34b1796 AL |
11 | #![unstable(feature = "ip", reason = "extra functionality has not been \ |
12 | scrutinized to the level that it should \ | |
e9174d1e SL |
13 | be stable", |
14 | issue = "27709")] | |
c34b1796 | 15 | |
85aaf69f | 16 | use cmp::Ordering; |
85aaf69f | 17 | use fmt; |
92a42be0 SL |
18 | use hash; |
19 | use mem; | |
85aaf69f | 20 | use net::{hton, ntoh}; |
92a42be0 SL |
21 | use sys::net::netc as c; |
22 | use sys_common::{AsInner, FromInner}; | |
85aaf69f | 23 | |
b039eaaf | 24 | /// An IP address, either an IPv4 or IPv6 address. |
9cc50fc6 | 25 | #[stable(feature = "ip_addr", since = "1.7.0")] |
c34b1796 AL |
26 | #[derive(Copy, Clone, Eq, PartialEq, Debug, Hash, PartialOrd, Ord)] |
27 | pub enum IpAddr { | |
28 | /// Representation of an IPv4 address. | |
9cc50fc6 | 29 | #[stable(feature = "ip_addr", since = "1.7.0")] |
7453a54e | 30 | V4(#[stable(feature = "ip_addr", since = "1.7.0")] Ipv4Addr), |
c34b1796 | 31 | /// Representation of an IPv6 address. |
9cc50fc6 | 32 | #[stable(feature = "ip_addr", since = "1.7.0")] |
7453a54e | 33 | V6(#[stable(feature = "ip_addr", since = "1.7.0")] Ipv6Addr), |
c34b1796 AL |
34 | } |
35 | ||
85aaf69f SL |
36 | /// Representation of an IPv4 address. |
37 | #[derive(Copy)] | |
c34b1796 | 38 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f | 39 | pub struct Ipv4Addr { |
92a42be0 | 40 | inner: c::in_addr, |
85aaf69f SL |
41 | } |
42 | ||
43 | /// Representation of an IPv6 address. | |
44 | #[derive(Copy)] | |
c34b1796 | 45 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f | 46 | pub struct Ipv6Addr { |
92a42be0 | 47 | inner: c::in6_addr, |
85aaf69f SL |
48 | } |
49 | ||
50 | #[allow(missing_docs)] | |
51 | #[derive(Copy, PartialEq, Eq, Clone, Hash, Debug)] | |
52 | pub enum Ipv6MulticastScope { | |
53 | InterfaceLocal, | |
54 | LinkLocal, | |
55 | RealmLocal, | |
56 | AdminLocal, | |
57 | SiteLocal, | |
58 | OrganizationLocal, | |
59 | Global | |
60 | } | |
61 | ||
85aaf69f | 62 | impl Ipv4Addr { |
9346a6ac | 63 | /// Creates a new IPv4 address from four eight-bit octets. |
85aaf69f | 64 | /// |
bd371182 | 65 | /// The result will represent the IP address `a`.`b`.`c`.`d`. |
c34b1796 | 66 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f SL |
67 | pub fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr { |
68 | Ipv4Addr { | |
92a42be0 | 69 | inner: c::in_addr { |
85aaf69f SL |
70 | s_addr: hton(((a as u32) << 24) | |
71 | ((b as u32) << 16) | | |
72 | ((c as u32) << 8) | | |
73 | (d as u32)), | |
74 | } | |
75 | } | |
76 | } | |
77 | ||
bd371182 | 78 | /// Returns the four eight-bit integers that make up this address. |
c34b1796 | 79 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f SL |
80 | pub fn octets(&self) -> [u8; 4] { |
81 | let bits = ntoh(self.inner.s_addr); | |
82 | [(bits >> 24) as u8, (bits >> 16) as u8, (bits >> 8) as u8, bits as u8] | |
83 | } | |
84 | ||
bd371182 | 85 | /// Returns true for the special 'unspecified' address 0.0.0.0. |
85aaf69f SL |
86 | pub fn is_unspecified(&self) -> bool { |
87 | self.inner.s_addr == 0 | |
88 | } | |
89 | ||
bd371182 | 90 | /// Returns true if this is a loopback address (127.0.0.0/8). |
9cc50fc6 SL |
91 | /// |
92 | /// This property is defined by RFC 6890 | |
93 | #[stable(since = "1.7.0", feature = "ip_17")] | |
85aaf69f SL |
94 | pub fn is_loopback(&self) -> bool { |
95 | self.octets()[0] == 127 | |
96 | } | |
97 | ||
98 | /// Returns true if this is a private address. | |
99 | /// | |
100 | /// The private address ranges are defined in RFC1918 and include: | |
101 | /// | |
102 | /// - 10.0.0.0/8 | |
103 | /// - 172.16.0.0/12 | |
104 | /// - 192.168.0.0/16 | |
9cc50fc6 | 105 | #[stable(since = "1.7.0", feature = "ip_17")] |
85aaf69f SL |
106 | pub fn is_private(&self) -> bool { |
107 | match (self.octets()[0], self.octets()[1]) { | |
108 | (10, _) => true, | |
109 | (172, b) if b >= 16 && b <= 31 => true, | |
110 | (192, 168) => true, | |
111 | _ => false | |
112 | } | |
113 | } | |
114 | ||
bd371182 | 115 | /// Returns true if the address is link-local (169.254.0.0/16). |
9cc50fc6 SL |
116 | /// |
117 | /// This property is defined by RFC 6890 | |
118 | #[stable(since = "1.7.0", feature = "ip_17")] | |
85aaf69f SL |
119 | pub fn is_link_local(&self) -> bool { |
120 | self.octets()[0] == 169 && self.octets()[1] == 254 | |
121 | } | |
122 | ||
123 | /// Returns true if the address appears to be globally routable. | |
54a0048b SL |
124 | /// See [iana-ipv4-special-registry][ipv4-sr]. |
125 | /// [ipv4-sr]: http://goo.gl/RaZ7lg | |
85aaf69f | 126 | /// |
d9579d0f AL |
127 | /// The following return false: |
128 | /// | |
129 | /// - private address (10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16) | |
130 | /// - the loopback address (127.0.0.0/8) | |
131 | /// - the link-local address (169.254.0.0/16) | |
132 | /// - the broadcast address (255.255.255.255/32) | |
133 | /// - test addresses used for documentation (192.0.2.0/24, 198.51.100.0/24 and 203.0.113.0/24) | |
54a0048b | 134 | /// - the unspecified address (0.0.0.0) |
85aaf69f | 135 | pub fn is_global(&self) -> bool { |
9346a6ac | 136 | !self.is_private() && !self.is_loopback() && !self.is_link_local() && |
54a0048b | 137 | !self.is_broadcast() && !self.is_documentation() && !self.is_unspecified() |
85aaf69f SL |
138 | } |
139 | ||
140 | /// Returns true if this is a multicast address. | |
141 | /// | |
9cc50fc6 SL |
142 | /// Multicast addresses have a most significant octet between 224 and 239, |
143 | /// and is defined by RFC 5771 | |
144 | #[stable(since = "1.7.0", feature = "ip_17")] | |
85aaf69f SL |
145 | pub fn is_multicast(&self) -> bool { |
146 | self.octets()[0] >= 224 && self.octets()[0] <= 239 | |
147 | } | |
148 | ||
9346a6ac AL |
149 | /// Returns true if this is a broadcast address. |
150 | /// | |
bd371182 | 151 | /// A broadcast address has all octets set to 255 as defined in RFC 919. |
9cc50fc6 | 152 | #[stable(since = "1.7.0", feature = "ip_17")] |
9346a6ac AL |
153 | pub fn is_broadcast(&self) -> bool { |
154 | self.octets()[0] == 255 && self.octets()[1] == 255 && | |
155 | self.octets()[2] == 255 && self.octets()[3] == 255 | |
156 | } | |
157 | ||
bd371182 | 158 | /// Returns true if this address is in a range designated for documentation. |
9346a6ac | 159 | /// |
d9579d0f AL |
160 | /// This is defined in RFC 5737: |
161 | /// | |
9346a6ac AL |
162 | /// - 192.0.2.0/24 (TEST-NET-1) |
163 | /// - 198.51.100.0/24 (TEST-NET-2) | |
164 | /// - 203.0.113.0/24 (TEST-NET-3) | |
9cc50fc6 | 165 | #[stable(since = "1.7.0", feature = "ip_17")] |
9346a6ac AL |
166 | pub fn is_documentation(&self) -> bool { |
167 | match(self.octets()[0], self.octets()[1], self.octets()[2], self.octets()[3]) { | |
c1a9b12d | 168 | (192, 0, 2, _) => true, |
9346a6ac | 169 | (198, 51, 100, _) => true, |
c1a9b12d | 170 | (203, 0, 113, _) => true, |
9346a6ac AL |
171 | _ => false |
172 | } | |
173 | } | |
174 | ||
bd371182 | 175 | /// Converts this address to an IPv4-compatible IPv6 address. |
85aaf69f SL |
176 | /// |
177 | /// a.b.c.d becomes ::a.b.c.d | |
c34b1796 | 178 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f SL |
179 | pub fn to_ipv6_compatible(&self) -> Ipv6Addr { |
180 | Ipv6Addr::new(0, 0, 0, 0, 0, 0, | |
181 | ((self.octets()[0] as u16) << 8) | self.octets()[1] as u16, | |
182 | ((self.octets()[2] as u16) << 8) | self.octets()[3] as u16) | |
183 | } | |
184 | ||
bd371182 | 185 | /// Converts this address to an IPv4-mapped IPv6 address. |
85aaf69f SL |
186 | /// |
187 | /// a.b.c.d becomes ::ffff:a.b.c.d | |
c34b1796 | 188 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f SL |
189 | pub fn to_ipv6_mapped(&self) -> Ipv6Addr { |
190 | Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, | |
191 | ((self.octets()[0] as u16) << 8) | self.octets()[1] as u16, | |
192 | ((self.octets()[2] as u16) << 8) | self.octets()[3] as u16) | |
193 | } | |
85aaf69f SL |
194 | } |
195 | ||
c34b1796 | 196 | #[stable(feature = "rust1", since = "1.0.0")] |
92a42be0 | 197 | #[allow(deprecated)] |
c34b1796 AL |
198 | impl fmt::Display for IpAddr { |
199 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | |
200 | match *self { | |
201 | IpAddr::V4(ref a) => a.fmt(fmt), | |
202 | IpAddr::V6(ref a) => a.fmt(fmt), | |
203 | } | |
204 | } | |
205 | } | |
206 | ||
207 | #[stable(feature = "rust1", since = "1.0.0")] | |
85aaf69f SL |
208 | impl fmt::Display for Ipv4Addr { |
209 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | |
210 | let octets = self.octets(); | |
211 | write!(fmt, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3]) | |
212 | } | |
213 | } | |
214 | ||
c34b1796 | 215 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f SL |
216 | impl fmt::Debug for Ipv4Addr { |
217 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | |
218 | fmt::Display::fmt(self, fmt) | |
219 | } | |
220 | } | |
221 | ||
c34b1796 | 222 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f SL |
223 | impl Clone for Ipv4Addr { |
224 | fn clone(&self) -> Ipv4Addr { *self } | |
225 | } | |
226 | ||
c34b1796 | 227 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f SL |
228 | impl PartialEq for Ipv4Addr { |
229 | fn eq(&self, other: &Ipv4Addr) -> bool { | |
230 | self.inner.s_addr == other.inner.s_addr | |
231 | } | |
232 | } | |
c34b1796 AL |
233 | |
234 | #[stable(feature = "rust1", since = "1.0.0")] | |
85aaf69f SL |
235 | impl Eq for Ipv4Addr {} |
236 | ||
85aaf69f SL |
237 | #[stable(feature = "rust1", since = "1.0.0")] |
238 | impl hash::Hash for Ipv4Addr { | |
239 | fn hash<H: hash::Hasher>(&self, s: &mut H) { | |
240 | self.inner.s_addr.hash(s) | |
241 | } | |
242 | } | |
243 | ||
c34b1796 | 244 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f SL |
245 | impl PartialOrd for Ipv4Addr { |
246 | fn partial_cmp(&self, other: &Ipv4Addr) -> Option<Ordering> { | |
247 | Some(self.cmp(other)) | |
248 | } | |
249 | } | |
250 | ||
c34b1796 | 251 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f SL |
252 | impl Ord for Ipv4Addr { |
253 | fn cmp(&self, other: &Ipv4Addr) -> Ordering { | |
92a42be0 | 254 | self.octets().cmp(&other.octets()) |
85aaf69f SL |
255 | } |
256 | } | |
257 | ||
92a42be0 SL |
258 | impl AsInner<c::in_addr> for Ipv4Addr { |
259 | fn as_inner(&self) -> &c::in_addr { &self.inner } | |
85aaf69f | 260 | } |
92a42be0 SL |
261 | impl FromInner<c::in_addr> for Ipv4Addr { |
262 | fn from_inner(addr: c::in_addr) -> Ipv4Addr { | |
85aaf69f SL |
263 | Ipv4Addr { inner: addr } |
264 | } | |
265 | } | |
266 | ||
d9579d0f AL |
267 | #[stable(feature = "ip_u32", since = "1.1.0")] |
268 | impl From<Ipv4Addr> for u32 { | |
269 | fn from(ip: Ipv4Addr) -> u32 { | |
270 | let ip = ip.octets(); | |
271 | ((ip[0] as u32) << 24) + ((ip[1] as u32) << 16) + ((ip[2] as u32) << 8) + (ip[3] as u32) | |
272 | } | |
273 | } | |
274 | ||
275 | #[stable(feature = "ip_u32", since = "1.1.0")] | |
276 | impl From<u32> for Ipv4Addr { | |
277 | fn from(ip: u32) -> Ipv4Addr { | |
278 | Ipv4Addr::new((ip >> 24) as u8, (ip >> 16) as u8, (ip >> 8) as u8, ip as u8) | |
279 | } | |
280 | } | |
281 | ||
54a0048b SL |
282 | #[stable(feature = "from_slice_v4", since = "1.9.0")] |
283 | impl From<[u8; 4]> for Ipv4Addr { | |
284 | fn from(octets: [u8; 4]) -> Ipv4Addr { | |
285 | Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3]) | |
286 | } | |
287 | } | |
288 | ||
85aaf69f | 289 | impl Ipv6Addr { |
9346a6ac | 290 | /// Creates a new IPv6 address from eight 16-bit segments. |
85aaf69f | 291 | /// |
bd371182 | 292 | /// The result will represent the IP address a:b:c:d:e:f:g:h. |
c34b1796 | 293 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f SL |
294 | pub fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16, |
295 | h: u16) -> Ipv6Addr { | |
92a42be0 SL |
296 | let mut addr: c::in6_addr = unsafe { mem::zeroed() }; |
297 | addr.s6_addr = [(a >> 8) as u8, a as u8, | |
298 | (b >> 8) as u8, b as u8, | |
299 | (c >> 8) as u8, c as u8, | |
300 | (d >> 8) as u8, d as u8, | |
301 | (e >> 8) as u8, e as u8, | |
302 | (f >> 8) as u8, f as u8, | |
303 | (g >> 8) as u8, g as u8, | |
304 | (h >> 8) as u8, h as u8]; | |
305 | Ipv6Addr { inner: addr } | |
85aaf69f SL |
306 | } |
307 | ||
bd371182 | 308 | /// Returns the eight 16-bit segments that make up this address. |
c34b1796 | 309 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f | 310 | pub fn segments(&self) -> [u16; 8] { |
92a42be0 SL |
311 | let arr = &self.inner.s6_addr; |
312 | [ | |
313 | (arr[0] as u16) << 8 | (arr[1] as u16), | |
314 | (arr[2] as u16) << 8 | (arr[3] as u16), | |
315 | (arr[4] as u16) << 8 | (arr[5] as u16), | |
316 | (arr[6] as u16) << 8 | (arr[7] as u16), | |
317 | (arr[8] as u16) << 8 | (arr[9] as u16), | |
318 | (arr[10] as u16) << 8 | (arr[11] as u16), | |
319 | (arr[12] as u16) << 8 | (arr[13] as u16), | |
320 | (arr[14] as u16) << 8 | (arr[15] as u16), | |
321 | ] | |
85aaf69f SL |
322 | } |
323 | ||
bd371182 | 324 | /// Returns true for the special 'unspecified' address ::. |
9cc50fc6 SL |
325 | /// |
326 | /// This property is defined in RFC 6890. | |
327 | #[stable(since = "1.7.0", feature = "ip_17")] | |
85aaf69f SL |
328 | pub fn is_unspecified(&self) -> bool { |
329 | self.segments() == [0, 0, 0, 0, 0, 0, 0, 0] | |
330 | } | |
331 | ||
bd371182 | 332 | /// Returns true if this is a loopback address (::1). |
9cc50fc6 SL |
333 | /// |
334 | /// This property is defined in RFC 6890. | |
335 | #[stable(since = "1.7.0", feature = "ip_17")] | |
85aaf69f SL |
336 | pub fn is_loopback(&self) -> bool { |
337 | self.segments() == [0, 0, 0, 0, 0, 0, 0, 1] | |
338 | } | |
339 | ||
340 | /// Returns true if the address appears to be globally routable. | |
341 | /// | |
d9579d0f AL |
342 | /// The following return false: |
343 | /// | |
344 | /// - the loopback address | |
345 | /// - link-local, site-local, and unique local unicast addresses | |
346 | /// - interface-, link-, realm-, admin- and site-local multicast addresses | |
85aaf69f SL |
347 | pub fn is_global(&self) -> bool { |
348 | match self.multicast_scope() { | |
349 | Some(Ipv6MulticastScope::Global) => true, | |
350 | None => self.is_unicast_global(), | |
351 | _ => false | |
352 | } | |
353 | } | |
354 | ||
bd371182 | 355 | /// Returns true if this is a unique local address (IPv6). |
85aaf69f | 356 | /// |
bd371182 | 357 | /// Unique local addresses are defined in RFC4193 and have the form fc00::/7. |
85aaf69f SL |
358 | pub fn is_unique_local(&self) -> bool { |
359 | (self.segments()[0] & 0xfe00) == 0xfc00 | |
360 | } | |
361 | ||
bd371182 | 362 | /// Returns true if the address is unicast and link-local (fe80::/10). |
85aaf69f SL |
363 | pub fn is_unicast_link_local(&self) -> bool { |
364 | (self.segments()[0] & 0xffc0) == 0xfe80 | |
365 | } | |
366 | ||
367 | /// Returns true if this is a deprecated unicast site-local address (IPv6 | |
bd371182 | 368 | /// fec0::/10). |
85aaf69f SL |
369 | pub fn is_unicast_site_local(&self) -> bool { |
370 | (self.segments()[0] & 0xffc0) == 0xfec0 | |
371 | } | |
372 | ||
54a0048b SL |
373 | /// Returns true if this is an address reserved for documentation |
374 | /// This is defined to be 2001:db8::/32 in RFC RFC 3849 | |
375 | pub fn is_documentation(&self) -> bool { | |
376 | (self.segments()[0] == 0x2001) && (self.segments()[1] == 0xdb8) | |
377 | } | |
378 | ||
bd371182 | 379 | /// Returns true if the address is a globally routable unicast address. |
85aaf69f | 380 | /// |
d9579d0f AL |
381 | /// The following return false: |
382 | /// | |
383 | /// - the loopback address | |
384 | /// - the link-local addresses | |
385 | /// - the (deprecated) site-local addresses | |
386 | /// - unique local addresses | |
54a0048b SL |
387 | /// - the unspecified address |
388 | /// - the address range reserved for documentation | |
85aaf69f SL |
389 | pub fn is_unicast_global(&self) -> bool { |
390 | !self.is_multicast() | |
391 | && !self.is_loopback() && !self.is_unicast_link_local() | |
392 | && !self.is_unicast_site_local() && !self.is_unique_local() | |
54a0048b | 393 | && !self.is_unspecified() && !self.is_documentation() |
85aaf69f SL |
394 | } |
395 | ||
396 | /// Returns the address's multicast scope if the address is multicast. | |
397 | pub fn multicast_scope(&self) -> Option<Ipv6MulticastScope> { | |
398 | if self.is_multicast() { | |
399 | match self.segments()[0] & 0x000f { | |
400 | 1 => Some(Ipv6MulticastScope::InterfaceLocal), | |
401 | 2 => Some(Ipv6MulticastScope::LinkLocal), | |
402 | 3 => Some(Ipv6MulticastScope::RealmLocal), | |
403 | 4 => Some(Ipv6MulticastScope::AdminLocal), | |
404 | 5 => Some(Ipv6MulticastScope::SiteLocal), | |
405 | 8 => Some(Ipv6MulticastScope::OrganizationLocal), | |
406 | 14 => Some(Ipv6MulticastScope::Global), | |
407 | _ => None | |
408 | } | |
409 | } else { | |
410 | None | |
411 | } | |
412 | } | |
413 | ||
414 | /// Returns true if this is a multicast address. | |
415 | /// | |
9cc50fc6 SL |
416 | /// Multicast addresses have the form ff00::/8, and this property is defined |
417 | /// by RFC 3956. | |
418 | #[stable(since = "1.7.0", feature = "ip_17")] | |
85aaf69f SL |
419 | pub fn is_multicast(&self) -> bool { |
420 | (self.segments()[0] & 0xff00) == 0xff00 | |
421 | } | |
422 | ||
9346a6ac | 423 | /// Converts this address to an IPv4 address. Returns None if this address is |
85aaf69f SL |
424 | /// neither IPv4-compatible or IPv4-mapped. |
425 | /// | |
426 | /// ::a.b.c.d and ::ffff:a.b.c.d become a.b.c.d | |
c34b1796 | 427 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f SL |
428 | pub fn to_ipv4(&self) -> Option<Ipv4Addr> { |
429 | match self.segments() { | |
430 | [0, 0, 0, 0, 0, f, g, h] if f == 0 || f == 0xffff => { | |
431 | Some(Ipv4Addr::new((g >> 8) as u8, g as u8, | |
432 | (h >> 8) as u8, h as u8)) | |
433 | }, | |
434 | _ => None | |
435 | } | |
436 | } | |
54a0048b SL |
437 | |
438 | /// Returns the sixteen eight-bit integers the IPv6 address consists of. | |
439 | #[unstable(feature = "ipv6_to_octets", reason = "needs some testing", | |
440 | issue = "32313")] | |
441 | pub fn octets(&self) -> [u8; 16] { | |
442 | self.inner.s6_addr | |
443 | } | |
85aaf69f SL |
444 | } |
445 | ||
c34b1796 | 446 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f SL |
447 | impl fmt::Display for Ipv6Addr { |
448 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | |
449 | match self.segments() { | |
450 | // We need special cases for :: and ::1, otherwise they're formatted | |
451 | // as ::0.0.0.[01] | |
452 | [0, 0, 0, 0, 0, 0, 0, 0] => write!(fmt, "::"), | |
453 | [0, 0, 0, 0, 0, 0, 0, 1] => write!(fmt, "::1"), | |
454 | // Ipv4 Compatible address | |
455 | [0, 0, 0, 0, 0, 0, g, h] => { | |
456 | write!(fmt, "::{}.{}.{}.{}", (g >> 8) as u8, g as u8, | |
457 | (h >> 8) as u8, h as u8) | |
458 | } | |
459 | // Ipv4-Mapped address | |
460 | [0, 0, 0, 0, 0, 0xffff, g, h] => { | |
461 | write!(fmt, "::ffff:{}.{}.{}.{}", (g >> 8) as u8, g as u8, | |
462 | (h >> 8) as u8, h as u8) | |
463 | }, | |
464 | _ => { | |
465 | fn find_zero_slice(segments: &[u16; 8]) -> (usize, usize) { | |
466 | let mut longest_span_len = 0; | |
467 | let mut longest_span_at = 0; | |
468 | let mut cur_span_len = 0; | |
469 | let mut cur_span_at = 0; | |
470 | ||
c34b1796 | 471 | for i in 0..8 { |
85aaf69f SL |
472 | if segments[i] == 0 { |
473 | if cur_span_len == 0 { | |
474 | cur_span_at = i; | |
475 | } | |
476 | ||
477 | cur_span_len += 1; | |
478 | ||
479 | if cur_span_len > longest_span_len { | |
480 | longest_span_len = cur_span_len; | |
481 | longest_span_at = cur_span_at; | |
482 | } | |
483 | } else { | |
484 | cur_span_len = 0; | |
485 | cur_span_at = 0; | |
486 | } | |
487 | } | |
488 | ||
489 | (longest_span_at, longest_span_len) | |
490 | } | |
491 | ||
492 | let (zeros_at, zeros_len) = find_zero_slice(&self.segments()); | |
493 | ||
494 | if zeros_len > 1 { | |
92a42be0 SL |
495 | fn fmt_subslice(segments: &[u16], fmt: &mut fmt::Formatter) -> fmt::Result { |
496 | if !segments.is_empty() { | |
54a0048b | 497 | write!(fmt, "{:x}", segments[0])?; |
92a42be0 | 498 | for &seg in &segments[1..] { |
54a0048b | 499 | write!(fmt, ":{:x}", seg)?; |
92a42be0 SL |
500 | } |
501 | } | |
502 | Ok(()) | |
85aaf69f SL |
503 | } |
504 | ||
54a0048b SL |
505 | fmt_subslice(&self.segments()[..zeros_at], fmt)?; |
506 | fmt.write_str("::")?; | |
92a42be0 | 507 | fmt_subslice(&self.segments()[zeros_at + zeros_len..], fmt) |
85aaf69f SL |
508 | } else { |
509 | let &[a, b, c, d, e, f, g, h] = &self.segments(); | |
510 | write!(fmt, "{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}", | |
511 | a, b, c, d, e, f, g, h) | |
512 | } | |
513 | } | |
514 | } | |
515 | } | |
516 | } | |
517 | ||
c34b1796 | 518 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f SL |
519 | impl fmt::Debug for Ipv6Addr { |
520 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | |
521 | fmt::Display::fmt(self, fmt) | |
522 | } | |
523 | } | |
524 | ||
c34b1796 | 525 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f SL |
526 | impl Clone for Ipv6Addr { |
527 | fn clone(&self) -> Ipv6Addr { *self } | |
528 | } | |
529 | ||
c34b1796 | 530 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f SL |
531 | impl PartialEq for Ipv6Addr { |
532 | fn eq(&self, other: &Ipv6Addr) -> bool { | |
533 | self.inner.s6_addr == other.inner.s6_addr | |
534 | } | |
535 | } | |
c34b1796 AL |
536 | |
537 | #[stable(feature = "rust1", since = "1.0.0")] | |
85aaf69f SL |
538 | impl Eq for Ipv6Addr {} |
539 | ||
85aaf69f SL |
540 | #[stable(feature = "rust1", since = "1.0.0")] |
541 | impl hash::Hash for Ipv6Addr { | |
542 | fn hash<H: hash::Hasher>(&self, s: &mut H) { | |
543 | self.inner.s6_addr.hash(s) | |
544 | } | |
545 | } | |
546 | ||
c34b1796 | 547 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f SL |
548 | impl PartialOrd for Ipv6Addr { |
549 | fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> { | |
550 | Some(self.cmp(other)) | |
551 | } | |
552 | } | |
553 | ||
c34b1796 | 554 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f SL |
555 | impl Ord for Ipv6Addr { |
556 | fn cmp(&self, other: &Ipv6Addr) -> Ordering { | |
92a42be0 | 557 | self.segments().cmp(&other.segments()) |
85aaf69f SL |
558 | } |
559 | } | |
560 | ||
92a42be0 SL |
561 | impl AsInner<c::in6_addr> for Ipv6Addr { |
562 | fn as_inner(&self) -> &c::in6_addr { &self.inner } | |
85aaf69f | 563 | } |
92a42be0 SL |
564 | impl FromInner<c::in6_addr> for Ipv6Addr { |
565 | fn from_inner(addr: c::in6_addr) -> Ipv6Addr { | |
85aaf69f SL |
566 | Ipv6Addr { inner: addr } |
567 | } | |
568 | } | |
9346a6ac | 569 | |
54a0048b SL |
570 | #[stable(feature = "ipv6_from_octets", since = "1.9.0")] |
571 | impl From<[u8; 16]> for Ipv6Addr { | |
572 | fn from(octets: [u8; 16]) -> Ipv6Addr { | |
573 | let mut inner: c::in6_addr = unsafe { mem::zeroed() }; | |
574 | inner.s6_addr = octets; | |
575 | Ipv6Addr::from_inner(inner) | |
576 | } | |
577 | } | |
578 | ||
9346a6ac AL |
579 | // Tests for this module |
580 | #[cfg(test)] | |
581 | mod tests { | |
582 | use prelude::v1::*; | |
9346a6ac AL |
583 | use net::*; |
584 | use net::Ipv6MulticastScope::*; | |
585 | use net::test::{tsa, sa6, sa4}; | |
586 | ||
587 | #[test] | |
588 | fn test_from_str_ipv4() { | |
589 | assert_eq!(Ok(Ipv4Addr::new(127, 0, 0, 1)), "127.0.0.1".parse()); | |
590 | assert_eq!(Ok(Ipv4Addr::new(255, 255, 255, 255)), "255.255.255.255".parse()); | |
591 | assert_eq!(Ok(Ipv4Addr::new(0, 0, 0, 0)), "0.0.0.0".parse()); | |
592 | ||
593 | // out of range | |
594 | let none: Option<Ipv4Addr> = "256.0.0.1".parse().ok(); | |
595 | assert_eq!(None, none); | |
596 | // too short | |
597 | let none: Option<Ipv4Addr> = "255.0.0".parse().ok(); | |
598 | assert_eq!(None, none); | |
599 | // too long | |
600 | let none: Option<Ipv4Addr> = "255.0.0.1.2".parse().ok(); | |
601 | assert_eq!(None, none); | |
602 | // no number between dots | |
603 | let none: Option<Ipv4Addr> = "255.0..1".parse().ok(); | |
604 | assert_eq!(None, none); | |
605 | } | |
606 | ||
607 | #[test] | |
608 | fn test_from_str_ipv6() { | |
609 | assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)), "0:0:0:0:0:0:0:0".parse()); | |
610 | assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), "0:0:0:0:0:0:0:1".parse()); | |
611 | ||
612 | assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), "::1".parse()); | |
613 | assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)), "::".parse()); | |
614 | ||
615 | assert_eq!(Ok(Ipv6Addr::new(0x2a02, 0x6b8, 0, 0, 0, 0, 0x11, 0x11)), | |
616 | "2a02:6b8::11:11".parse()); | |
617 | ||
618 | // too long group | |
619 | let none: Option<Ipv6Addr> = "::00000".parse().ok(); | |
620 | assert_eq!(None, none); | |
621 | // too short | |
622 | let none: Option<Ipv6Addr> = "1:2:3:4:5:6:7".parse().ok(); | |
623 | assert_eq!(None, none); | |
624 | // too long | |
625 | let none: Option<Ipv6Addr> = "1:2:3:4:5:6:7:8:9".parse().ok(); | |
626 | assert_eq!(None, none); | |
627 | // triple colon | |
628 | let none: Option<Ipv6Addr> = "1:2:::6:7:8".parse().ok(); | |
629 | assert_eq!(None, none); | |
630 | // two double colons | |
631 | let none: Option<Ipv6Addr> = "1:2::6::8".parse().ok(); | |
632 | assert_eq!(None, none); | |
633 | } | |
634 | ||
635 | #[test] | |
636 | fn test_from_str_ipv4_in_ipv6() { | |
637 | assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 49152, 545)), | |
638 | "::192.0.2.33".parse()); | |
639 | assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0xFFFF, 49152, 545)), | |
640 | "::FFFF:192.0.2.33".parse()); | |
641 | assert_eq!(Ok(Ipv6Addr::new(0x64, 0xff9b, 0, 0, 0, 0, 49152, 545)), | |
642 | "64:ff9b::192.0.2.33".parse()); | |
643 | assert_eq!(Ok(Ipv6Addr::new(0x2001, 0xdb8, 0x122, 0xc000, 0x2, 0x2100, 49152, 545)), | |
644 | "2001:db8:122:c000:2:2100:192.0.2.33".parse()); | |
645 | ||
646 | // colon after v4 | |
647 | let none: Option<Ipv4Addr> = "::127.0.0.1:".parse().ok(); | |
648 | assert_eq!(None, none); | |
649 | // not enough groups | |
650 | let none: Option<Ipv6Addr> = "1.2.3.4.5:127.0.0.1".parse().ok(); | |
651 | assert_eq!(None, none); | |
652 | // too many groups | |
653 | let none: Option<Ipv6Addr> = "1.2.3.4.5:6:7:127.0.0.1".parse().ok(); | |
654 | assert_eq!(None, none); | |
655 | } | |
656 | ||
657 | #[test] | |
658 | fn test_from_str_socket_addr() { | |
659 | assert_eq!(Ok(sa4(Ipv4Addr::new(77, 88, 21, 11), 80)), | |
660 | "77.88.21.11:80".parse()); | |
b039eaaf SL |
661 | assert_eq!(Ok(SocketAddrV4::new(Ipv4Addr::new(77, 88, 21, 11), 80)), |
662 | "77.88.21.11:80".parse()); | |
9346a6ac AL |
663 | assert_eq!(Ok(sa6(Ipv6Addr::new(0x2a02, 0x6b8, 0, 1, 0, 0, 0, 1), 53)), |
664 | "[2a02:6b8:0:1::1]:53".parse()); | |
b039eaaf SL |
665 | assert_eq!(Ok(SocketAddrV6::new(Ipv6Addr::new(0x2a02, 0x6b8, 0, 1, |
666 | 0, 0, 0, 1), 53, 0, 0)), | |
667 | "[2a02:6b8:0:1::1]:53".parse()); | |
9346a6ac AL |
668 | assert_eq!(Ok(sa6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x7F00, 1), 22)), |
669 | "[::127.0.0.1]:22".parse()); | |
b039eaaf SL |
670 | assert_eq!(Ok(SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, |
671 | 0x7F00, 1), 22, 0, 0)), | |
672 | "[::127.0.0.1]:22".parse()); | |
9346a6ac AL |
673 | |
674 | // without port | |
675 | let none: Option<SocketAddr> = "127.0.0.1".parse().ok(); | |
676 | assert_eq!(None, none); | |
677 | // without port | |
678 | let none: Option<SocketAddr> = "127.0.0.1:".parse().ok(); | |
679 | assert_eq!(None, none); | |
680 | // wrong brackets around v4 | |
681 | let none: Option<SocketAddr> = "[127.0.0.1]:22".parse().ok(); | |
682 | assert_eq!(None, none); | |
683 | // port out of range | |
684 | let none: Option<SocketAddr> = "127.0.0.1:123456".parse().ok(); | |
685 | assert_eq!(None, none); | |
686 | } | |
687 | ||
688 | #[test] | |
689 | fn ipv6_addr_to_string() { | |
690 | // ipv4-mapped address | |
691 | let a1 = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc000, 0x280); | |
692 | assert_eq!(a1.to_string(), "::ffff:192.0.2.128"); | |
693 | ||
694 | // ipv4-compatible address | |
695 | let a1 = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0xc000, 0x280); | |
696 | assert_eq!(a1.to_string(), "::192.0.2.128"); | |
697 | ||
698 | // v6 address with no zero segments | |
699 | assert_eq!(Ipv6Addr::new(8, 9, 10, 11, 12, 13, 14, 15).to_string(), | |
700 | "8:9:a:b:c:d:e:f"); | |
701 | ||
702 | // reduce a single run of zeros | |
703 | assert_eq!("ae::ffff:102:304", | |
704 | Ipv6Addr::new(0xae, 0, 0, 0, 0, 0xffff, 0x0102, 0x0304).to_string()); | |
705 | ||
706 | // don't reduce just a single zero segment | |
707 | assert_eq!("1:2:3:4:5:6:0:8", | |
708 | Ipv6Addr::new(1, 2, 3, 4, 5, 6, 0, 8).to_string()); | |
709 | ||
710 | // 'any' address | |
711 | assert_eq!("::", Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0).to_string()); | |
712 | ||
713 | // loopback address | |
714 | assert_eq!("::1", Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_string()); | |
715 | ||
716 | // ends in zeros | |
717 | assert_eq!("1::", Ipv6Addr::new(1, 0, 0, 0, 0, 0, 0, 0).to_string()); | |
718 | ||
719 | // two runs of zeros, second one is longer | |
720 | assert_eq!("1:0:0:4::8", Ipv6Addr::new(1, 0, 0, 4, 0, 0, 0, 8).to_string()); | |
721 | ||
722 | // two runs of zeros, equal length | |
723 | assert_eq!("1::4:5:0:0:8", Ipv6Addr::new(1, 0, 0, 4, 5, 0, 0, 8).to_string()); | |
724 | } | |
725 | ||
726 | #[test] | |
727 | fn ipv4_to_ipv6() { | |
728 | assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678), | |
729 | Ipv4Addr::new(0x12, 0x34, 0x56, 0x78).to_ipv6_mapped()); | |
730 | assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x1234, 0x5678), | |
731 | Ipv4Addr::new(0x12, 0x34, 0x56, 0x78).to_ipv6_compatible()); | |
732 | } | |
733 | ||
734 | #[test] | |
735 | fn ipv6_to_ipv4() { | |
736 | assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678).to_ipv4(), | |
737 | Some(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78))); | |
738 | assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x1234, 0x5678).to_ipv4(), | |
739 | Some(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78))); | |
740 | assert_eq!(Ipv6Addr::new(0, 0, 1, 0, 0, 0, 0x1234, 0x5678).to_ipv4(), | |
741 | None); | |
742 | } | |
743 | ||
744 | #[test] | |
745 | fn ipv4_properties() { | |
746 | fn check(octets: &[u8; 4], unspec: bool, loopback: bool, | |
747 | private: bool, link_local: bool, global: bool, | |
748 | multicast: bool, broadcast: bool, documentation: bool) { | |
749 | let ip = Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3]); | |
750 | assert_eq!(octets, &ip.octets()); | |
751 | ||
752 | assert_eq!(ip.is_unspecified(), unspec); | |
753 | assert_eq!(ip.is_loopback(), loopback); | |
754 | assert_eq!(ip.is_private(), private); | |
755 | assert_eq!(ip.is_link_local(), link_local); | |
756 | assert_eq!(ip.is_global(), global); | |
757 | assert_eq!(ip.is_multicast(), multicast); | |
758 | assert_eq!(ip.is_broadcast(), broadcast); | |
759 | assert_eq!(ip.is_documentation(), documentation); | |
760 | } | |
761 | ||
762 | // address unspec loopbk privt linloc global multicast brdcast doc | |
54a0048b SL |
763 | check(&[0, 0, 0, 0], true, false, false, false, false, false, false, false); |
764 | check(&[0, 0, 0, 1], false, false, false, false, true, false, false, false); | |
765 | check(&[0, 1, 0, 0], false, false, false, false, true, false, false, false); | |
766 | check(&[10, 9, 8, 7], false, false, true, false, false, false, false, false); | |
767 | check(&[127, 1, 2, 3], false, true, false, false, false, false, false, false); | |
768 | check(&[172, 31, 254, 253], false, false, true, false, false, false, false, false); | |
769 | check(&[169, 254, 253, 242], false, false, false, true, false, false, false, false); | |
770 | check(&[192, 0, 2, 183], false, false, false, false, false, false, false, true); | |
771 | check(&[192, 1, 2, 183], false, false, false, false, true, false, false, false); | |
772 | check(&[192, 168, 254, 253], false, false, true, false, false, false, false, false); | |
773 | check(&[198, 51, 100, 0], false, false, false, false, false, false, false, true); | |
774 | check(&[203, 0, 113, 0], false, false, false, false, false, false, false, true); | |
775 | check(&[203, 2, 113, 0], false, false, false, false, true, false, false, false); | |
776 | check(&[224, 0, 0, 0], false, false, false, false, true, true, false, false); | |
777 | check(&[239, 255, 255, 255], false, false, false, false, true, true, false, false); | |
778 | check(&[255, 255, 255, 255], false, false, false, false, false, false, true, false); | |
9346a6ac AL |
779 | } |
780 | ||
781 | #[test] | |
782 | fn ipv6_properties() { | |
54a0048b | 783 | fn check(str_addr: &str, octets: &[u8; 16], unspec: bool, loopback: bool, |
9346a6ac | 784 | unique_local: bool, global: bool, |
54a0048b | 785 | u_link_local: bool, u_site_local: bool, u_global: bool, u_doc: bool, |
9346a6ac AL |
786 | m_scope: Option<Ipv6MulticastScope>) { |
787 | let ip: Ipv6Addr = str_addr.parse().unwrap(); | |
788 | assert_eq!(str_addr, ip.to_string()); | |
54a0048b SL |
789 | assert_eq!(&ip.octets(), octets); |
790 | assert_eq!(Ipv6Addr::from(*octets), ip); | |
9346a6ac AL |
791 | |
792 | assert_eq!(ip.is_unspecified(), unspec); | |
793 | assert_eq!(ip.is_loopback(), loopback); | |
794 | assert_eq!(ip.is_unique_local(), unique_local); | |
795 | assert_eq!(ip.is_global(), global); | |
796 | assert_eq!(ip.is_unicast_link_local(), u_link_local); | |
797 | assert_eq!(ip.is_unicast_site_local(), u_site_local); | |
798 | assert_eq!(ip.is_unicast_global(), u_global); | |
54a0048b | 799 | assert_eq!(ip.is_documentation(), u_doc); |
9346a6ac AL |
800 | assert_eq!(ip.multicast_scope(), m_scope); |
801 | assert_eq!(ip.is_multicast(), m_scope.is_some()); | |
802 | } | |
803 | ||
54a0048b SL |
804 | // unspec loopbk uniqlo global unill unisl uniglo doc mscope |
805 | check("::", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | |
806 | true, false, false, false, false, false, false, false, None); | |
807 | check("::1", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], | |
808 | false, true, false, false, false, false, false, false, None); | |
809 | check("::0.0.0.2", &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2], | |
810 | false, false, false, true, false, false, true, false, None); | |
811 | check("1::", &[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | |
812 | false, false, false, true, false, false, true, false, None); | |
813 | check("fc00::", &[0xfc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | |
814 | false, false, true, false, false, false, false, false, None); | |
815 | check("fdff:ffff::", &[0xfd, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | |
816 | false, false, true, false, false, false, false, false, None); | |
817 | check("fe80:ffff::", &[0xfe, 0x80, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | |
818 | false, false, false, false, true, false, false, false, None); | |
819 | check("febf:ffff::", &[0xfe, 0xbf, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | |
820 | false, false, false, false, true, false, false, false, None); | |
821 | check("fec0::", &[0xfe, 0xc0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | |
822 | false, false, false, false, false, true, false, false, None); | |
823 | check("ff01::", &[0xff, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | |
824 | false, false, false, false, false, false, false, false, Some(InterfaceLocal)); | |
825 | check("ff02::", &[0xff, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | |
826 | false, false, false, false, false, false, false, false, Some(LinkLocal)); | |
827 | check("ff03::", &[0xff, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | |
828 | false, false, false, false, false, false, false, false, Some(RealmLocal)); | |
829 | check("ff04::", &[0xff, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | |
830 | false, false, false, false, false, false, false, false, Some(AdminLocal)); | |
831 | check("ff05::", &[0xff, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | |
832 | false, false, false, false, false, false, false, false, Some(SiteLocal)); | |
833 | check("ff08::", &[0xff, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | |
834 | false, false, false, false, false, false, false, false, Some(OrganizationLocal)); | |
835 | check("ff0e::", &[0xff, 0xe, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | |
836 | false, false, false, true, false, false, false, false, Some(Global)); | |
837 | check("2001:db8:85a3::8a2e:370:7334", | |
838 | &[0x20, 1, 0xd, 0xb8, 0x85, 0xa3, 0, 0, 0, 0, 0x8a, 0x2e, 3, 0x70, 0x73, 0x34], | |
839 | false, false, false, false, false, false, false, true, None); | |
840 | check("102:304:506:708:90a:b0c:d0e:f10", | |
841 | &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], | |
842 | false, false, false, true, false, false, true, false, None); | |
9346a6ac AL |
843 | } |
844 | ||
845 | #[test] | |
846 | fn to_socket_addr_socketaddr() { | |
847 | let a = sa4(Ipv4Addr::new(77, 88, 21, 11), 12345); | |
848 | assert_eq!(Ok(vec![a]), tsa(a)); | |
849 | } | |
d9579d0f AL |
850 | |
851 | #[test] | |
852 | fn test_ipv4_to_int() { | |
853 | let a = Ipv4Addr::new(127, 0, 0, 1); | |
854 | assert_eq!(u32::from(a), 2130706433); | |
855 | } | |
856 | ||
857 | #[test] | |
858 | fn test_int_to_ipv4() { | |
859 | let a = Ipv4Addr::new(127, 0, 0, 1); | |
860 | assert_eq!(Ipv4Addr::from(2130706433), a); | |
861 | } | |
92a42be0 | 862 | |
54a0048b SL |
863 | #[test] |
864 | fn ipv4_from_u32_slice() { | |
865 | assert_eq!(Ipv4Addr::from([127, 0, 0, 1]), Ipv4Addr::new(127, 0, 0, 1)) | |
866 | } | |
867 | ||
92a42be0 SL |
868 | #[test] |
869 | fn ord() { | |
870 | assert!(Ipv4Addr::new(100, 64, 3, 3) < Ipv4Addr::new(192, 0, 2, 2)); | |
871 | assert!("2001:db8:f00::1002".parse::<Ipv6Addr>().unwrap() < | |
872 | "2001:db8:f00::2001".parse::<Ipv6Addr>().unwrap()); | |
873 | } | |
9346a6ac | 874 | } |