]>
git.proxmox.com Git - rustc.git/blob - src/libstd/net/ip.rs
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.
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.
11 #![unstable(feature = "ip", reason = "extra functionality has not been \
12 scrutinized to the level that it should \
20 use net
::{hton, ntoh}
;
21 use sys
::net
::netc
as c
;
22 use sys_common
::{AsInner, FromInner}
;
24 /// An IP address, either an IPv4 or IPv6 address.
25 #[stable(feature = "ip_addr", since = "1.7.0")]
26 #[derive(Copy, Clone, Eq, PartialEq, Debug, Hash, PartialOrd, Ord)]
28 /// Representation of an IPv4 address.
29 #[stable(feature = "ip_addr", since = "1.7.0")]
30 V4(#[stable(feature = "ip_addr", since = "1.7.0")] Ipv4Addr),
31 /// Representation of an IPv6 address.
32 #[stable(feature = "ip_addr", since = "1.7.0")]
33 V6(#[stable(feature = "ip_addr", since = "1.7.0")] Ipv6Addr),
36 /// Representation of an IPv4 address.
38 #[stable(feature = "rust1", since = "1.0.0")]
43 /// Representation of an IPv6 address.
45 #[stable(feature = "rust1", since = "1.0.0")]
50 #[allow(missing_docs)]
51 #[derive(Copy, PartialEq, Eq, Clone, Hash, Debug)]
52 pub enum Ipv6MulticastScope
{
63 /// Creates a new IPv4 address from four eight-bit octets.
65 /// The result will represent the IP address `a`.`b`.`c`.`d`.
66 #[stable(feature = "rust1", since = "1.0.0")]
67 pub fn new(a
: u8, b
: u8, c
: u8, d
: u8) -> Ipv4Addr
{
70 s_addr
: hton(((a
as u32) << 24) |
78 /// Returns the four eight-bit integers that make up this address.
79 #[stable(feature = "rust1", since = "1.0.0")]
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]
85 /// Returns true for the special 'unspecified' address 0.0.0.0.
86 pub fn is_unspecified(&self) -> bool
{
87 self.inner
.s_addr
== 0
90 /// Returns true if this is a loopback address (127.0.0.0/8).
92 /// This property is defined by RFC 6890
93 #[stable(since = "1.7.0", feature = "ip_17")]
94 pub fn is_loopback(&self) -> bool
{
95 self.octets()[0] == 127
98 /// Returns true if this is a private address.
100 /// The private address ranges are defined in RFC1918 and include:
105 #[stable(since = "1.7.0", feature = "ip_17")]
106 pub fn is_private(&self) -> bool
{
107 match (self.octets()[0], self.octets()[1]) {
109 (172, b
) if b
>= 16 && b
<= 31 => true,
115 /// Returns true if the address is link-local (169.254.0.0/16).
117 /// This property is defined by RFC 6890
118 #[stable(since = "1.7.0", feature = "ip_17")]
119 pub fn is_link_local(&self) -> bool
{
120 self.octets()[0] == 169 && self.octets()[1] == 254
123 /// Returns true if the address appears to be globally routable.
124 /// See [iana-ipv4-special-registry][ipv4-sr].
125 /// [ipv4-sr]: http://goo.gl/RaZ7lg
127 /// The following return false:
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)
134 /// - the unspecified address (0.0.0.0)
135 pub fn is_global(&self) -> bool
{
136 !self.is_private() && !self.is_loopback() && !self.is_link_local() &&
137 !self.is_broadcast() && !self.is_documentation() && !self.is_unspecified()
140 /// Returns true if this is a multicast address.
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")]
145 pub fn is_multicast(&self) -> bool
{
146 self.octets()[0] >= 224 && self.octets()[0] <= 239
149 /// Returns true if this is a broadcast address.
151 /// A broadcast address has all octets set to 255 as defined in RFC 919.
152 #[stable(since = "1.7.0", feature = "ip_17")]
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
158 /// Returns true if this address is in a range designated for documentation.
160 /// This is defined in RFC 5737:
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)
165 #[stable(since = "1.7.0", feature = "ip_17")]
166 pub fn is_documentation(&self) -> bool
{
167 match(self.octets()[0], self.octets()[1], self.octets()[2], self.octets()[3]) {
168 (192, 0, 2, _
) => true,
169 (198, 51, 100, _
) => true,
170 (203, 0, 113, _
) => true,
175 /// Converts this address to an IPv4-compatible IPv6 address.
177 /// a.b.c.d becomes ::a.b.c.d
178 #[stable(feature = "rust1", since = "1.0.0")]
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)
185 /// Converts this address to an IPv4-mapped IPv6 address.
187 /// a.b.c.d becomes ::ffff:a.b.c.d
188 #[stable(feature = "rust1", since = "1.0.0")]
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)
196 #[stable(feature = "rust1", since = "1.0.0")]
198 impl fmt
::Display
for IpAddr
{
199 fn fmt(&self, fmt
: &mut fmt
::Formatter
) -> fmt
::Result
{
201 IpAddr
::V4(ref a
) => a
.fmt(fmt
),
202 IpAddr
::V6(ref a
) => a
.fmt(fmt
),
207 #[stable(feature = "rust1", since = "1.0.0")]
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])
215 #[stable(feature = "rust1", since = "1.0.0")]
216 impl fmt
::Debug
for Ipv4Addr
{
217 fn fmt(&self, fmt
: &mut fmt
::Formatter
) -> fmt
::Result
{
218 fmt
::Display
::fmt(self, fmt
)
222 #[stable(feature = "rust1", since = "1.0.0")]
223 impl Clone
for Ipv4Addr
{
224 fn clone(&self) -> Ipv4Addr { *self }
227 #[stable(feature = "rust1", since = "1.0.0")]
228 impl PartialEq
for Ipv4Addr
{
229 fn eq(&self, other
: &Ipv4Addr
) -> bool
{
230 self.inner
.s_addr
== other
.inner
.s_addr
234 #[stable(feature = "rust1", since = "1.0.0")]
235 impl Eq
for Ipv4Addr {}
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
)
244 #[stable(feature = "rust1", since = "1.0.0")]
245 impl PartialOrd
for Ipv4Addr
{
246 fn partial_cmp(&self, other
: &Ipv4Addr
) -> Option
<Ordering
> {
247 Some(self.cmp(other
))
251 #[stable(feature = "rust1", since = "1.0.0")]
252 impl Ord
for Ipv4Addr
{
253 fn cmp(&self, other
: &Ipv4Addr
) -> Ordering
{
254 self.octets().cmp(&other
.octets())
258 impl AsInner
<c
::in_addr
> for Ipv4Addr
{
259 fn as_inner(&self) -> &c
::in_addr { &self.inner }
261 impl FromInner
<c
::in_addr
> for Ipv4Addr
{
262 fn from_inner(addr
: c
::in_addr
) -> Ipv4Addr
{
263 Ipv4Addr { inner: addr }
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)
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)
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])
290 /// Creates a new IPv6 address from eight 16-bit segments.
292 /// The result will represent the IP address a:b:c:d:e:f:g:h.
293 #[stable(feature = "rust1", since = "1.0.0")]
294 pub fn new(a
: u16, b
: u16, c
: u16, d
: u16, e
: u16, f
: u16, g
: u16,
295 h
: u16) -> Ipv6Addr
{
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 }
308 /// Returns the eight 16-bit segments that make up this address.
309 #[stable(feature = "rust1", since = "1.0.0")]
310 pub fn segments(&self) -> [u16; 8] {
311 let arr
= &self.inner
.s6_addr
;
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),
324 /// Returns true for the special 'unspecified' address ::.
326 /// This property is defined in RFC 6890.
327 #[stable(since = "1.7.0", feature = "ip_17")]
328 pub fn is_unspecified(&self) -> bool
{
329 self.segments() == [0, 0, 0, 0, 0, 0, 0, 0]
332 /// Returns true if this is a loopback address (::1).
334 /// This property is defined in RFC 6890.
335 #[stable(since = "1.7.0", feature = "ip_17")]
336 pub fn is_loopback(&self) -> bool
{
337 self.segments() == [0, 0, 0, 0, 0, 0, 0, 1]
340 /// Returns true if the address appears to be globally routable.
342 /// The following return false:
344 /// - the loopback address
345 /// - link-local, site-local, and unique local unicast addresses
346 /// - interface-, link-, realm-, admin- and site-local multicast addresses
347 pub fn is_global(&self) -> bool
{
348 match self.multicast_scope() {
349 Some(Ipv6MulticastScope
::Global
) => true,
350 None
=> self.is_unicast_global(),
355 /// Returns true if this is a unique local address (IPv6).
357 /// Unique local addresses are defined in RFC4193 and have the form fc00::/7.
358 pub fn is_unique_local(&self) -> bool
{
359 (self.segments()[0] & 0xfe00) == 0xfc00
362 /// Returns true if the address is unicast and link-local (fe80::/10).
363 pub fn is_unicast_link_local(&self) -> bool
{
364 (self.segments()[0] & 0xffc0) == 0xfe80
367 /// Returns true if this is a deprecated unicast site-local address (IPv6
369 pub fn is_unicast_site_local(&self) -> bool
{
370 (self.segments()[0] & 0xffc0) == 0xfec0
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)
379 /// Returns true if the address is a globally routable unicast address.
381 /// The following return false:
383 /// - the loopback address
384 /// - the link-local addresses
385 /// - the (deprecated) site-local addresses
386 /// - unique local addresses
387 /// - the unspecified address
388 /// - the address range reserved for documentation
389 pub fn is_unicast_global(&self) -> bool
{
391 && !self.is_loopback() && !self.is_unicast_link_local()
392 && !self.is_unicast_site_local() && !self.is_unique_local()
393 && !self.is_unspecified() && !self.is_documentation()
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
),
414 /// Returns true if this is a multicast address.
416 /// Multicast addresses have the form ff00::/8, and this property is defined
418 #[stable(since = "1.7.0", feature = "ip_17")]
419 pub fn is_multicast(&self) -> bool
{
420 (self.segments()[0] & 0xff00) == 0xff00
423 /// Converts this address to an IPv4 address. Returns None if this address is
424 /// neither IPv4-compatible or IPv4-mapped.
426 /// ::a.b.c.d and ::ffff:a.b.c.d become a.b.c.d
427 #[stable(feature = "rust1", since = "1.0.0")]
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))
438 /// Returns the sixteen eight-bit integers the IPv6 address consists of.
439 #[unstable(feature = "ipv6_to_octets", reason = "needs some testing",
441 pub fn octets(&self) -> [u8; 16] {
446 #[stable(feature = "rust1", since = "1.0.0")]
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
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)
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)
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;
472 if segments
[i
] == 0 {
473 if cur_span_len
== 0 {
479 if cur_span_len
> longest_span_len
{
480 longest_span_len
= cur_span_len
;
481 longest_span_at
= cur_span_at
;
489 (longest_span_at
, longest_span_len
)
492 let (zeros_at
, zeros_len
) = find_zero_slice(&self.segments());
495 fn fmt_subslice(segments
: &[u16], fmt
: &mut fmt
::Formatter
) -> fmt
::Result
{
496 if !segments
.is_empty() {
497 write
!(fmt
, "{:x}", segments
[0])?
;
498 for &seg
in &segments
[1..] {
499 write
!(fmt
, ":{:x}", seg
)?
;
505 fmt_subslice(&self.segments()[..zeros_at
], fmt
)?
;
506 fmt
.write_str("::")?
;
507 fmt_subslice(&self.segments()[zeros_at
+ zeros_len
..], fmt
)
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
)
518 #[stable(feature = "rust1", since = "1.0.0")]
519 impl fmt
::Debug
for Ipv6Addr
{
520 fn fmt(&self, fmt
: &mut fmt
::Formatter
) -> fmt
::Result
{
521 fmt
::Display
::fmt(self, fmt
)
525 #[stable(feature = "rust1", since = "1.0.0")]
526 impl Clone
for Ipv6Addr
{
527 fn clone(&self) -> Ipv6Addr { *self }
530 #[stable(feature = "rust1", since = "1.0.0")]
531 impl PartialEq
for Ipv6Addr
{
532 fn eq(&self, other
: &Ipv6Addr
) -> bool
{
533 self.inner
.s6_addr
== other
.inner
.s6_addr
537 #[stable(feature = "rust1", since = "1.0.0")]
538 impl Eq
for Ipv6Addr {}
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
)
547 #[stable(feature = "rust1", since = "1.0.0")]
548 impl PartialOrd
for Ipv6Addr
{
549 fn partial_cmp(&self, other
: &Ipv6Addr
) -> Option
<Ordering
> {
550 Some(self.cmp(other
))
554 #[stable(feature = "rust1", since = "1.0.0")]
555 impl Ord
for Ipv6Addr
{
556 fn cmp(&self, other
: &Ipv6Addr
) -> Ordering
{
557 self.segments().cmp(&other
.segments())
561 impl AsInner
<c
::in6_addr
> for Ipv6Addr
{
562 fn as_inner(&self) -> &c
::in6_addr { &self.inner }
564 impl FromInner
<c
::in6_addr
> for Ipv6Addr
{
565 fn from_inner(addr
: c
::in6_addr
) -> Ipv6Addr
{
566 Ipv6Addr { inner: addr }
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
)
579 // Tests for this module
584 use net
::Ipv6MulticastScope
::*;
585 use net
::test
::{tsa, sa6, sa4}
;
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());
594 let none
: Option
<Ipv4Addr
> = "256.0.0.1".parse().ok();
595 assert_eq
!(None
, none
);
597 let none
: Option
<Ipv4Addr
> = "255.0.0".parse().ok();
598 assert_eq
!(None
, none
);
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
);
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());
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());
615 assert_eq
!(Ok(Ipv6Addr
::new(0x2a02, 0x6b8, 0, 0, 0, 0, 0x11, 0x11)),
616 "2a02:6b8::11:11".parse());
619 let none
: Option
<Ipv6Addr
> = "::00000".parse().ok();
620 assert_eq
!(None
, none
);
622 let none
: Option
<Ipv6Addr
> = "1:2:3:4:5:6:7".parse().ok();
623 assert_eq
!(None
, none
);
625 let none
: Option
<Ipv6Addr
> = "1:2:3:4:5:6:7:8:9".parse().ok();
626 assert_eq
!(None
, none
);
628 let none
: Option
<Ipv6Addr
> = "1:2:::6:7:8".parse().ok();
629 assert_eq
!(None
, none
);
631 let none
: Option
<Ipv6Addr
> = "1:2::6::8".parse().ok();
632 assert_eq
!(None
, none
);
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());
647 let none
: Option
<Ipv4Addr
> = "::127.0.0.1:".parse().ok();
648 assert_eq
!(None
, none
);
650 let none
: Option
<Ipv6Addr
> = "1.2.3.4.5:127.0.0.1".parse().ok();
651 assert_eq
!(None
, none
);
653 let none
: Option
<Ipv6Addr
> = "1.2.3.4.5:6:7:127.0.0.1".parse().ok();
654 assert_eq
!(None
, none
);
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());
661 assert_eq
!(Ok(SocketAddrV4
::new(Ipv4Addr
::new(77, 88, 21, 11), 80)),
662 "77.88.21.11:80".parse());
663 assert_eq
!(Ok(sa6(Ipv6Addr
::new(0x2a02, 0x6b8, 0, 1, 0, 0, 0, 1), 53)),
664 "[2a02:6b8:0:1::1]:53".parse());
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());
668 assert_eq
!(Ok(sa6(Ipv6Addr
::new(0, 0, 0, 0, 0, 0, 0x7F00, 1), 22)),
669 "[::127.0.0.1]:22".parse());
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());
675 let none
: Option
<SocketAddr
> = "127.0.0.1".parse().ok();
676 assert_eq
!(None
, none
);
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
);
684 let none
: Option
<SocketAddr
> = "127.0.0.1:123456".parse().ok();
685 assert_eq
!(None
, none
);
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");
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");
698 // v6 address with no zero segments
699 assert_eq
!(Ipv6Addr
::new(8, 9, 10, 11, 12, 13, 14, 15).to_string(),
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());
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());
711 assert_eq
!("::", Ipv6Addr
::new(0, 0, 0, 0, 0, 0, 0, 0).to_string());
714 assert_eq
!("::1", Ipv6Addr
::new(0, 0, 0, 0, 0, 0, 0, 1).to_string());
717 assert_eq
!("1::", Ipv6Addr
::new(1, 0, 0, 0, 0, 0, 0, 0).to_string());
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());
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());
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());
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(),
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());
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
);
762 // address unspec loopbk privt linloc global multicast brdcast doc
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);
782 fn ipv6_properties() {
783 fn check(str_addr
: &str, octets
: &[u8; 16], unspec
: bool
, loopback
: bool
,
784 unique_local
: bool
, global
: bool
,
785 u_link_local
: bool
, u_site_local
: bool
, u_global
: bool
, u_doc
: bool
,
786 m_scope
: Option
<Ipv6MulticastScope
>) {
787 let ip
: Ipv6Addr
= str_addr
.parse().unwrap();
788 assert_eq
!(str_addr
, ip
.to_string());
789 assert_eq
!(&ip
.octets(), octets
);
790 assert_eq
!(Ipv6Addr
::from(*octets
), ip
);
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
);
799 assert_eq
!(ip
.is_documentation(), u_doc
);
800 assert_eq
!(ip
.multicast_scope(), m_scope
);
801 assert_eq
!(ip
.is_multicast(), m_scope
.is_some());
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
);
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
));
852 fn test_ipv4_to_int() {
853 let a
= Ipv4Addr
::new(127, 0, 0, 1);
854 assert_eq
!(u32::from(a
), 2130706433);
858 fn test_int_to_ipv4() {
859 let a
= Ipv4Addr
::new(127, 0, 0, 1);
860 assert_eq
!(Ipv4Addr
::from(2130706433), a
);
864 fn ipv4_from_u32_slice() {
865 assert_eq
!(Ipv4Addr
::from([127, 0, 0, 1]), Ipv4Addr
::new(127, 0, 0, 1))
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());