]> git.lizzy.rs Git - rust.git/blob - src/libstd/net/ip.rs
Auto merge of #22517 - brson:relnotes, r=Gankro
[rust.git] / 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.
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
11 use prelude::v1::*;
12
13 use cmp::Ordering;
14 use hash;
15 use fmt;
16 use libc;
17 use sys_common::{AsInner, FromInner};
18 use net::{hton, ntoh};
19
20 /// Representation of an IPv4 address.
21 #[derive(Copy)]
22 pub struct Ipv4Addr {
23     inner: libc::in_addr,
24 }
25
26 /// Representation of an IPv6 address.
27 #[derive(Copy)]
28 pub struct Ipv6Addr {
29     inner: libc::in6_addr,
30 }
31
32 #[allow(missing_docs)]
33 #[derive(Copy, PartialEq, Eq, Clone, Hash, Debug)]
34 pub enum Ipv6MulticastScope {
35     InterfaceLocal,
36     LinkLocal,
37     RealmLocal,
38     AdminLocal,
39     SiteLocal,
40     OrganizationLocal,
41     Global
42 }
43
44 /// Enumeration of possible IP addresses
45 #[derive(Copy, PartialEq, Eq, Clone, Hash, Debug)]
46 pub enum IpAddr {
47     /// An IPv4 address.
48     V4(Ipv4Addr),
49     /// An IPv6 address.
50     V6(Ipv6Addr)
51 }
52
53 impl IpAddr {
54     /// Create a new IpAddr that contains an IPv4 address.
55     ///
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))
59     }
60
61     /// Create a new IpAddr that contains an IPv6 address.
62     ///
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,
65                   h: u16) -> IpAddr {
66         IpAddr::V6(Ipv6Addr::new(a, b, c, d, e, f, g, h))
67     }
68 }
69
70 #[stable(feature = "rust1", since = "1.0.0")]
71 impl fmt::Display for IpAddr {
72     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
73         match *self {
74             IpAddr::V4(v4) => v4.fmt(f),
75             IpAddr::V6(v6) => v6.fmt(f)
76         }
77     }
78 }
79
80 impl Ipv4Addr {
81     /// Create a new IPv4 address from four eight-bit octets.
82     ///
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 {
85         Ipv4Addr {
86             inner: libc::in_addr {
87                 s_addr: hton(((a as u32) << 24) |
88                              ((b as u32) << 16) |
89                              ((c as u32) <<  8) |
90                               (d as u32)),
91             }
92         }
93     }
94
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]
99     }
100
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
104     }
105
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
109     }
110
111     /// Returns true if this is a private address.
112     ///
113     /// The private address ranges are defined in RFC1918 and include:
114     ///
115     ///  - 10.0.0.0/8
116     ///  - 172.16.0.0/12
117     ///  - 192.168.0.0/16
118     pub fn is_private(&self) -> bool {
119         match (self.octets()[0], self.octets()[1]) {
120             (10, _) => true,
121             (172, b) if b >= 16 && b <= 31 => true,
122             (192, 168) => true,
123             _ => false
124         }
125     }
126
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
130     }
131
132     /// Returns true if the address appears to be globally routable.
133     ///
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()
139     }
140
141     /// Returns true if this is a multicast address.
142     ///
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
146     }
147
148     /// Convert this address to an IPv4-compatible IPv6 address
149     ///
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)
155     }
156
157     /// Convert this address to an IPv4-mapped IPv6 address
158     ///
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)
164     }
165
166 }
167
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])
172     }
173 }
174
175 impl fmt::Debug for Ipv4Addr {
176     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
177         fmt::Display::fmt(self, fmt)
178     }
179 }
180
181 impl Clone for Ipv4Addr {
182     fn clone(&self) -> Ipv4Addr { *self }
183 }
184
185 impl PartialEq for Ipv4Addr {
186     fn eq(&self, other: &Ipv4Addr) -> bool {
187         self.inner.s_addr == other.inner.s_addr
188     }
189 }
190 impl Eq for Ipv4Addr {}
191
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)
195     }
196 }
197
198 impl PartialOrd for Ipv4Addr {
199     fn partial_cmp(&self, other: &Ipv4Addr) -> Option<Ordering> {
200         Some(self.cmp(other))
201     }
202 }
203
204 impl Ord for Ipv4Addr {
205     fn cmp(&self, other: &Ipv4Addr) -> Ordering {
206         self.inner.s_addr.cmp(&other.inner.s_addr)
207     }
208 }
209
210 impl AsInner<libc::in_addr> for Ipv4Addr {
211     fn as_inner(&self) -> &libc::in_addr { &self.inner }
212 }
213 impl FromInner<libc::in_addr> for Ipv4Addr {
214     fn from_inner(addr: libc::in_addr) -> Ipv4Addr {
215         Ipv4Addr { inner: addr }
216     }
217 }
218
219 impl Ipv6Addr {
220     /// Create a new IPv6 address from eight 16-bit segments.
221     ///
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 {
225         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)]
229             }
230         }
231     }
232
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])]
243     }
244
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]
248     }
249
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]
253     }
254
255     /// Returns true if the address appears to be globally routable.
256     ///
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(),
264             _ => false
265         }
266     }
267
268     /// Returns true if this is a unique local address (IPv6)
269     ///
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
273     }
274
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
278     }
279
280     /// Returns true if this is a deprecated unicast site-local address (IPv6
281     /// fec0::/10)
282     pub fn is_unicast_site_local(&self) -> bool {
283         (self.segments()[0] & 0xffc0) == 0xfec0
284     }
285
286     /// Returns true if the address is a globally routable unicast address
287     ///
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 {
292         !self.is_multicast()
293             && !self.is_loopback() && !self.is_unicast_link_local()
294             && !self.is_unicast_site_local() && !self.is_unique_local()
295     }
296
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),
308                 _ => None
309             }
310         } else {
311             None
312         }
313     }
314
315     /// Returns true if this is a multicast address.
316     ///
317     /// Multicast addresses have the form ff00::/8.
318     pub fn is_multicast(&self) -> bool {
319         (self.segments()[0] & 0xff00) == 0xff00
320     }
321
322     /// Convert this address to an IPv4 address. Returns None if this address is
323     /// neither IPv4-compatible or IPv4-mapped.
324     ///
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))
331             },
332             _ => None
333         }
334     }
335 }
336
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
341             // as ::0.0.0.[01]
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)
348             }
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)
353             },
354             _ => {
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;
360
361                     for i in range(0, 8) {
362                         if segments[i] == 0 {
363                             if cur_span_len == 0 {
364                                 cur_span_at = i;
365                             }
366
367                             cur_span_len += 1;
368
369                             if cur_span_len > longest_span_len {
370                                 longest_span_len = cur_span_len;
371                                 longest_span_at = cur_span_at;
372                             }
373                         } else {
374                             cur_span_len = 0;
375                             cur_span_at = 0;
376                         }
377                     }
378
379                     (longest_span_at, longest_span_len)
380                 }
381
382                 let (zeros_at, zeros_len) = find_zero_slice(&self.segments());
383
384                 if zeros_len > 1 {
385                     fn fmt_subslice(segments: &[u16]) -> String {
386                         segments
387                             .iter()
388                             .map(|&seg| format!("{:x}", seg))
389                             .collect::<Vec<String>>()
390                             .as_slice()
391                             .connect(":")
392                     }
393
394                     write!(fmt, "{}::{}",
395                            fmt_subslice(&self.segments()[..zeros_at]),
396                            fmt_subslice(&self.segments()[zeros_at + zeros_len..]))
397                 } else {
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)
401                 }
402             }
403         }
404     }
405 }
406
407 impl fmt::Debug for Ipv6Addr {
408     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
409         fmt::Display::fmt(self, fmt)
410     }
411 }
412
413 impl Clone for Ipv6Addr {
414     fn clone(&self) -> Ipv6Addr { *self }
415 }
416
417 impl PartialEq for Ipv6Addr {
418     fn eq(&self, other: &Ipv6Addr) -> bool {
419         self.inner.s6_addr == other.inner.s6_addr
420     }
421 }
422 impl Eq for Ipv6Addr {}
423
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)
427     }
428 }
429
430 impl PartialOrd for Ipv6Addr {
431     fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> {
432         Some(self.cmp(other))
433     }
434 }
435
436 impl Ord for Ipv6Addr {
437     fn cmp(&self, other: &Ipv6Addr) -> Ordering {
438         self.inner.s6_addr.cmp(&other.inner.s6_addr)
439     }
440 }
441
442 impl AsInner<libc::in6_addr> for Ipv6Addr {
443     fn as_inner(&self) -> &libc::in6_addr { &self.inner }
444 }
445 impl FromInner<libc::in6_addr> for Ipv6Addr {
446     fn from_inner(addr: libc::in6_addr) -> Ipv6Addr {
447         Ipv6Addr { inner: addr }
448     }
449 }