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.
17 use sys_common::{AsInner, FromInner};
18 use net::{hton, ntoh};
20 /// Representation of an IPv4 address.
26 /// Representation of an IPv6 address.
29 inner: libc::in6_addr,
32 #[allow(missing_docs)]
33 #[derive(Copy, PartialEq, Eq, Clone, Hash, Debug)]
34 pub enum Ipv6MulticastScope {
44 /// Enumeration of possible IP addresses
45 #[derive(Copy, PartialEq, Eq, Clone, Hash, Debug)]
54 /// Create a new IpAddr that contains an IPv4 address.
56 /// The result will represent the IP address a.b.c.d
57 pub fn new_v4(a: u8, b: u8, c: u8, d: u8) -> IpAddr {
58 IpAddr::V4(Ipv4Addr::new(a, b, c, d))
61 /// Create a new IpAddr that contains an IPv6 address.
63 /// The result will represent the IP address a:b:c:d:e:f
64 pub fn new_v6(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16,
66 IpAddr::V6(Ipv6Addr::new(a, b, c, d, e, f, g, h))
70 #[stable(feature = "rust1", since = "1.0.0")]
71 impl fmt::Display for IpAddr {
72 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
74 IpAddr::V4(v4) => v4.fmt(f),
75 IpAddr::V6(v6) => v6.fmt(f)
81 /// Create a new IPv4 address from four eight-bit octets.
83 /// The result will represent the IP address a.b.c.d
84 pub fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
86 inner: libc::in_addr {
87 s_addr: hton(((a as u32) << 24) |
95 /// Returns the four eight-bit integers that make up this address
96 pub fn octets(&self) -> [u8; 4] {
97 let bits = ntoh(self.inner.s_addr);
98 [(bits >> 24) as u8, (bits >> 16) as u8, (bits >> 8) as u8, bits as u8]
101 /// Returns true for the special 'unspecified' address 0.0.0.0
102 pub fn is_unspecified(&self) -> bool {
103 self.inner.s_addr == 0
106 /// Returns true if this is a loopback address (127.0.0.0/8)
107 pub fn is_loopback(&self) -> bool {
108 self.octets()[0] == 127
111 /// Returns true if this is a private address.
113 /// The private address ranges are defined in RFC1918 and include:
118 pub fn is_private(&self) -> bool {
119 match (self.octets()[0], self.octets()[1]) {
121 (172, b) if b >= 16 && b <= 31 => true,
127 /// Returns true if the address is link-local (169.254.0.0/16)
128 pub fn is_link_local(&self) -> bool {
129 self.octets()[0] == 169 && self.octets()[1] == 254
132 /// Returns true if the address appears to be globally routable.
134 /// Non-globally-routable networks include the private networks (10.0.0.0/8,
135 /// 172.16.0.0/12 and 192.168.0.0/16), the loopback network (127.0.0.0/8),
136 /// and the link-local network (169.254.0.0/16).
137 pub fn is_global(&self) -> bool {
138 !self.is_private() && !self.is_loopback() && !self.is_link_local()
141 /// Returns true if this is a multicast address.
143 /// Multicast addresses have a most significant octet between 224 and 239.
144 pub fn is_multicast(&self) -> bool {
145 self.octets()[0] >= 224 && self.octets()[0] <= 239
148 /// Convert this address to an IPv4-compatible IPv6 address
150 /// a.b.c.d becomes ::a.b.c.d
151 pub fn to_ipv6_compatible(&self) -> Ipv6Addr {
152 Ipv6Addr::new(0, 0, 0, 0, 0, 0,
153 ((self.octets()[0] as u16) << 8) | self.octets()[1] as u16,
154 ((self.octets()[2] as u16) << 8) | self.octets()[3] as u16)
157 /// Convert this address to an IPv4-mapped IPv6 address
159 /// a.b.c.d becomes ::ffff:a.b.c.d
160 pub fn to_ipv6_mapped(&self) -> Ipv6Addr {
161 Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff,
162 ((self.octets()[0] as u16) << 8) | self.octets()[1] as u16,
163 ((self.octets()[2] as u16) << 8) | self.octets()[3] as u16)
168 impl fmt::Display for Ipv4Addr {
169 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
170 let octets = self.octets();
171 write!(fmt, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3])
175 impl fmt::Debug for Ipv4Addr {
176 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
177 fmt::Display::fmt(self, fmt)
181 impl Clone for Ipv4Addr {
182 fn clone(&self) -> Ipv4Addr { *self }
185 impl PartialEq for Ipv4Addr {
186 fn eq(&self, other: &Ipv4Addr) -> bool {
187 self.inner.s_addr == other.inner.s_addr
190 impl Eq for Ipv4Addr {}
192 impl<S: hash::Hasher + hash::Writer> hash::Hash<S> for Ipv4Addr {
193 fn hash(&self, s: &mut S) {
194 self.inner.s_addr.hash(s)
198 impl PartialOrd for Ipv4Addr {
199 fn partial_cmp(&self, other: &Ipv4Addr) -> Option<Ordering> {
200 Some(self.cmp(other))
204 impl Ord for Ipv4Addr {
205 fn cmp(&self, other: &Ipv4Addr) -> Ordering {
206 self.inner.s_addr.cmp(&other.inner.s_addr)
210 impl AsInner<libc::in_addr> for Ipv4Addr {
211 fn as_inner(&self) -> &libc::in_addr { &self.inner }
213 impl FromInner<libc::in_addr> for Ipv4Addr {
214 fn from_inner(addr: libc::in_addr) -> Ipv4Addr {
215 Ipv4Addr { inner: addr }
220 /// Create a new IPv6 address from eight 16-bit segments.
222 /// The result will represent the IP address a:b:c:d:e:f
223 pub fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16,
224 h: u16) -> Ipv6Addr {
226 inner: libc::in6_addr {
227 s6_addr: [hton(a), hton(b), hton(c), hton(d),
228 hton(e), hton(f), hton(g), hton(h)]
233 /// Return the eight 16-bit segments that make up this address
234 pub fn segments(&self) -> [u16; 8] {
235 [ntoh(self.inner.s6_addr[0]),
236 ntoh(self.inner.s6_addr[1]),
237 ntoh(self.inner.s6_addr[2]),
238 ntoh(self.inner.s6_addr[3]),
239 ntoh(self.inner.s6_addr[4]),
240 ntoh(self.inner.s6_addr[5]),
241 ntoh(self.inner.s6_addr[6]),
242 ntoh(self.inner.s6_addr[7])]
245 /// Returns true for the special 'unspecified' address ::
246 pub fn is_unspecified(&self) -> bool {
247 self.segments() == [0, 0, 0, 0, 0, 0, 0, 0]
250 /// Returns true if this is a loopback address (::1)
251 pub fn is_loopback(&self) -> bool {
252 self.segments() == [0, 0, 0, 0, 0, 0, 0, 1]
255 /// Returns true if the address appears to be globally routable.
257 /// Non-globally-routable networks include the loopback address; the
258 /// link-local, site-local, and unique local unicast addresses; and the
259 /// interface-, link-, realm-, admin- and site-local multicast addresses.
260 pub fn is_global(&self) -> bool {
261 match self.multicast_scope() {
262 Some(Ipv6MulticastScope::Global) => true,
263 None => self.is_unicast_global(),
268 /// Returns true if this is a unique local address (IPv6)
270 /// Unique local addresses are defined in RFC4193 and have the form fc00::/7
271 pub fn is_unique_local(&self) -> bool {
272 (self.segments()[0] & 0xfe00) == 0xfc00
275 /// Returns true if the address is unicast and link-local (fe80::/10)
276 pub fn is_unicast_link_local(&self) -> bool {
277 (self.segments()[0] & 0xffc0) == 0xfe80
280 /// Returns true if this is a deprecated unicast site-local address (IPv6
282 pub fn is_unicast_site_local(&self) -> bool {
283 (self.segments()[0] & 0xffc0) == 0xfec0
286 /// Returns true if the address is a globally routable unicast address
288 /// Non-globally-routable unicast addresses include the loopback address,
289 /// the link-local addresses, the deprecated site-local addresses and the
290 /// unique local addresses.
291 pub fn is_unicast_global(&self) -> bool {
293 && !self.is_loopback() && !self.is_unicast_link_local()
294 && !self.is_unicast_site_local() && !self.is_unique_local()
297 /// Returns the address's multicast scope if the address is multicast.
298 pub fn multicast_scope(&self) -> Option<Ipv6MulticastScope> {
299 if self.is_multicast() {
300 match self.segments()[0] & 0x000f {
301 1 => Some(Ipv6MulticastScope::InterfaceLocal),
302 2 => Some(Ipv6MulticastScope::LinkLocal),
303 3 => Some(Ipv6MulticastScope::RealmLocal),
304 4 => Some(Ipv6MulticastScope::AdminLocal),
305 5 => Some(Ipv6MulticastScope::SiteLocal),
306 8 => Some(Ipv6MulticastScope::OrganizationLocal),
307 14 => Some(Ipv6MulticastScope::Global),
315 /// Returns true if this is a multicast address.
317 /// Multicast addresses have the form ff00::/8.
318 pub fn is_multicast(&self) -> bool {
319 (self.segments()[0] & 0xff00) == 0xff00
322 /// Convert this address to an IPv4 address. Returns None if this address is
323 /// neither IPv4-compatible or IPv4-mapped.
325 /// ::a.b.c.d and ::ffff:a.b.c.d become a.b.c.d
326 pub fn to_ipv4(&self) -> Option<Ipv4Addr> {
327 match self.segments() {
328 [0, 0, 0, 0, 0, f, g, h] if f == 0 || f == 0xffff => {
329 Some(Ipv4Addr::new((g >> 8) as u8, g as u8,
330 (h >> 8) as u8, h as u8))
337 impl fmt::Display for Ipv6Addr {
338 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
339 match self.segments() {
340 // We need special cases for :: and ::1, otherwise they're formatted
342 [0, 0, 0, 0, 0, 0, 0, 0] => write!(fmt, "::"),
343 [0, 0, 0, 0, 0, 0, 0, 1] => write!(fmt, "::1"),
344 // Ipv4 Compatible address
345 [0, 0, 0, 0, 0, 0, g, h] => {
346 write!(fmt, "::{}.{}.{}.{}", (g >> 8) as u8, g as u8,
347 (h >> 8) as u8, h as u8)
349 // Ipv4-Mapped address
350 [0, 0, 0, 0, 0, 0xffff, g, h] => {
351 write!(fmt, "::ffff:{}.{}.{}.{}", (g >> 8) as u8, g as u8,
352 (h >> 8) as u8, h as u8)
355 fn find_zero_slice(segments: &[u16; 8]) -> (usize, usize) {
356 let mut longest_span_len = 0;
357 let mut longest_span_at = 0;
358 let mut cur_span_len = 0;
359 let mut cur_span_at = 0;
361 for i in range(0, 8) {
362 if segments[i] == 0 {
363 if cur_span_len == 0 {
369 if cur_span_len > longest_span_len {
370 longest_span_len = cur_span_len;
371 longest_span_at = cur_span_at;
379 (longest_span_at, longest_span_len)
382 let (zeros_at, zeros_len) = find_zero_slice(&self.segments());
385 fn fmt_subslice(segments: &[u16]) -> String {
388 .map(|&seg| format!("{:x}", seg))
389 .collect::<Vec<String>>()
394 write!(fmt, "{}::{}",
395 fmt_subslice(&self.segments()[..zeros_at]),
396 fmt_subslice(&self.segments()[zeros_at + zeros_len..]))
398 let &[a, b, c, d, e, f, g, h] = &self.segments();
399 write!(fmt, "{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}",
400 a, b, c, d, e, f, g, h)
407 impl fmt::Debug for Ipv6Addr {
408 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
409 fmt::Display::fmt(self, fmt)
413 impl Clone for Ipv6Addr {
414 fn clone(&self) -> Ipv6Addr { *self }
417 impl PartialEq for Ipv6Addr {
418 fn eq(&self, other: &Ipv6Addr) -> bool {
419 self.inner.s6_addr == other.inner.s6_addr
422 impl Eq for Ipv6Addr {}
424 impl<S: hash::Hasher + hash::Writer> hash::Hash<S> for Ipv6Addr {
425 fn hash(&self, s: &mut S) {
426 self.inner.s6_addr.hash(s)
430 impl PartialOrd for Ipv6Addr {
431 fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> {
432 Some(self.cmp(other))
436 impl Ord for Ipv6Addr {
437 fn cmp(&self, other: &Ipv6Addr) -> Ordering {
438 self.inner.s6_addr.cmp(&other.inner.s6_addr)
442 impl AsInner<libc::in6_addr> for Ipv6Addr {
443 fn as_inner(&self) -> &libc::in6_addr { &self.inner }
445 impl FromInner<libc::in6_addr> for Ipv6Addr {
446 fn from_inner(addr: libc::in6_addr) -> Ipv6Addr {
447 Ipv6Addr { inner: addr }