]> git.lizzy.rs Git - rust.git/blob - src/libstd/net/addr.rs
Auto merge of #22541 - Manishearth:rollup, r=Gankro
[rust.git] / src / libstd / net / addr.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 fmt;
14 use hash;
15 use io;
16 use libc::{self, socklen_t, sa_family_t};
17 use mem;
18 use net::{IpAddr, lookup_host, ntoh, hton};
19 use option;
20 use sys_common::{FromInner, AsInner, IntoInner};
21 use vec;
22
23 /// Representation of a socket address for networking applications
24 ///
25 /// A socket address consists of at least an (ip, port) pair and may also
26 /// contain other information depending on the protocol.
27 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
28 pub struct SocketAddr {
29     repr: Repr,
30 }
31
32 #[derive(Copy)]
33 enum Repr {
34     V4(libc::sockaddr_in),
35     V6(libc::sockaddr_in6),
36 }
37
38 impl SocketAddr {
39     /// Creates a new socket address from the (ip, port) pair.
40     pub fn new(ip: IpAddr, port: u16) -> SocketAddr {
41         let repr = match ip {
42             IpAddr::V4(ref ip) => {
43                 Repr::V4(libc::sockaddr_in {
44                     sin_family: libc::AF_INET as sa_family_t,
45                     sin_port: hton(port),
46                     sin_addr: *ip.as_inner(),
47                     .. unsafe { mem::zeroed() }
48                 })
49             }
50             IpAddr::V6(ref ip) => {
51                 Repr::V6(libc::sockaddr_in6 {
52                     sin6_family: libc::AF_INET6 as sa_family_t,
53                     sin6_port: hton(port),
54                     sin6_addr: *ip.as_inner(),
55                     .. unsafe { mem::zeroed() }
56                 })
57             }
58         };
59         SocketAddr { repr: repr }
60     }
61
62     /// Gets the IP address associated with this socket address.
63     pub fn ip(&self) -> IpAddr {
64         match self.repr {
65             Repr::V4(ref sa) => IpAddr::V4(FromInner::from_inner(sa.sin_addr)),
66             Repr::V6(ref sa) => IpAddr::V6(FromInner::from_inner(sa.sin6_addr)),
67         }
68     }
69
70     /// Gets the port number associated with this socket address
71     pub fn port(&self) -> u16 {
72         match self.repr {
73             Repr::V4(ref sa) => ntoh(sa.sin_port),
74             Repr::V6(ref sa) => ntoh(sa.sin6_port),
75         }
76     }
77
78     fn set_port(&mut self, port: u16) {
79         match self.repr {
80             Repr::V4(ref mut sa) => sa.sin_port = hton(port),
81             Repr::V6(ref mut sa) => sa.sin6_port = hton(port),
82         }
83     }
84 }
85
86 #[stable(feature = "rust1", since = "1.0.0")]
87 impl fmt::Display for SocketAddr {
88     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
89         match self.repr {
90             Repr::V4(_) => write!(f, "{}:{}", self.ip(), self.port()),
91             Repr::V6(_) => write!(f, "[{}]:{}", self.ip(), self.port()),
92         }
93     }
94 }
95
96 impl FromInner<libc::sockaddr_in> for SocketAddr {
97     fn from_inner(addr: libc::sockaddr_in) -> SocketAddr {
98         SocketAddr { repr: Repr::V4(addr) }
99     }
100 }
101
102 impl FromInner<libc::sockaddr_in6> for SocketAddr {
103     fn from_inner(addr: libc::sockaddr_in6) -> SocketAddr {
104         SocketAddr { repr: Repr::V6(addr) }
105     }
106 }
107
108 impl<'a> IntoInner<(*const libc::sockaddr, socklen_t)> for &'a SocketAddr {
109     fn into_inner(self) -> (*const libc::sockaddr, socklen_t) {
110         match self.repr {
111             Repr::V4(ref a) => {
112                 (a as *const _ as *const _, mem::size_of_val(a) as socklen_t)
113             }
114             Repr::V6(ref a) => {
115                 (a as *const _ as *const _, mem::size_of_val(a) as socklen_t)
116             }
117         }
118     }
119 }
120
121 impl fmt::Debug for SocketAddr {
122     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
123         fmt::Display::fmt(self, fmt)
124     }
125 }
126
127 impl Clone for Repr {
128     fn clone(&self) -> Repr { *self }
129 }
130
131 impl PartialEq for Repr {
132     fn eq(&self, other: &Repr) -> bool {
133         match (*self, *other) {
134             (Repr::V4(ref a), Repr::V4(ref b)) => {
135                 a.sin_port == b.sin_port &&
136                     a.sin_addr.s_addr == b.sin_addr.s_addr
137             }
138             (Repr::V6(ref a), Repr::V6(ref b)) => {
139                 a.sin6_port == b.sin6_port &&
140                     a.sin6_addr.s6_addr == b.sin6_addr.s6_addr &&
141                     a.sin6_flowinfo == b.sin6_flowinfo &&
142                     a.sin6_scope_id == b.sin6_scope_id
143             }
144             _ => false,
145         }
146     }
147 }
148 impl Eq for Repr {}
149
150 #[cfg(stage0)]
151 impl<S: hash::Hasher + hash::Writer> hash::Hash<S> for Repr {
152     fn hash(&self, s: &mut S) {
153         match *self {
154             Repr::V4(ref a) => {
155                 (a.sin_family, a.sin_port, a.sin_addr.s_addr).hash(s)
156             }
157             Repr::V6(ref a) => {
158                 (a.sin6_family, a.sin6_port, &a.sin6_addr.s6_addr,
159                  a.sin6_flowinfo, a.sin6_scope_id).hash(s)
160             }
161         }
162     }
163 }
164 #[cfg(not(stage0))]
165 #[stable(feature = "rust1", since = "1.0.0")]
166 impl hash::Hash for Repr {
167     fn hash<H: hash::Hasher>(&self, s: &mut H) {
168         match *self {
169             Repr::V4(ref a) => {
170                 (a.sin_family, a.sin_port, a.sin_addr.s_addr).hash(s)
171             }
172             Repr::V6(ref a) => {
173                 (a.sin6_family, a.sin6_port, &a.sin6_addr.s6_addr,
174                  a.sin6_flowinfo, a.sin6_scope_id).hash(s)
175             }
176         }
177     }
178 }
179
180 /// A trait for objects which can be converted or resolved to one or more
181 /// `SocketAddr` values.
182 ///
183 /// This trait is used for generic address resolution when constructing network
184 /// objects.  By default it is implemented for the following types:
185 ///
186 ///  * `SocketAddr` - `to_socket_addrs` is identity function.
187 ///
188 ///  * `(IpAddr, u16)` - `to_socket_addrs` constructs `SocketAddr` trivially.
189 ///
190 ///  * `(&str, u16)` - the string should be either a string representation of an
191 ///    IP address expected by `FromStr` implementation for `IpAddr` or a host
192 ///    name.
193 ///
194 ///  * `&str` - the string should be either a string representation of a
195 ///    `SocketAddr` as expected by its `FromStr` implementation or a string like
196 ///    `<host_name>:<port>` pair where `<port>` is a `u16` value.
197 ///
198 /// This trait allows constructing network objects like `TcpStream` or
199 /// `UdpSocket` easily with values of various types for the bind/connection
200 /// address. It is needed because sometimes one type is more appropriate than
201 /// the other: for simple uses a string like `"localhost:12345"` is much nicer
202 /// than manual construction of the corresponding `SocketAddr`, but sometimes
203 /// `SocketAddr` value is *the* main source of the address, and converting it to
204 /// some other type (e.g. a string) just for it to be converted back to
205 /// `SocketAddr` in constructor methods is pointless.
206 ///
207 /// Some examples:
208 ///
209 /// ```no_run
210 /// use std::net::{IpAddr, SocketAddr, TcpStream, UdpSocket, TcpListener};
211 ///
212 /// fn main() {
213 ///     let ip = IpAddr::new_v4(127, 0, 0, 1);
214 ///     let port = 12345;
215 ///
216 ///     // The following lines are equivalent modulo possible "localhost" name
217 ///     // resolution differences
218 ///     let tcp_s = TcpStream::connect(&SocketAddr::new(ip, port));
219 ///     let tcp_s = TcpStream::connect(&(ip, port));
220 ///     let tcp_s = TcpStream::connect(&("127.0.0.1", port));
221 ///     let tcp_s = TcpStream::connect(&("localhost", port));
222 ///     let tcp_s = TcpStream::connect("127.0.0.1:12345");
223 ///     let tcp_s = TcpStream::connect("localhost:12345");
224 ///
225 ///     // TcpListener::bind(), UdpSocket::bind() and UdpSocket::send_to()
226 ///     // behave similarly
227 ///     let tcp_l = TcpListener::bind("localhost:12345");
228 ///
229 ///     let mut udp_s = UdpSocket::bind(&("127.0.0.1", port)).unwrap();
230 ///     udp_s.send_to(&[7], &(ip, 23451));
231 /// }
232 /// ```
233 pub trait ToSocketAddrs {
234     /// Returned iterator over socket addresses which this type may correspond
235     /// to.
236     type Iter: Iterator<Item=SocketAddr>;
237
238     /// Converts this object to an iterator of resolved `SocketAddr`s.
239     ///
240     /// The returned iterator may not actually yield any values depending on the
241     /// outcome of any resolution performed.
242     ///
243     /// Note that this function may block the current thread while resolution is
244     /// performed.
245     ///
246     /// # Errors
247     ///
248     /// Any errors encountered during resolution will be returned as an `Err`.
249     fn to_socket_addrs(&self) -> io::Result<Self::Iter>;
250 }
251
252 impl ToSocketAddrs for SocketAddr {
253     type Iter = option::IntoIter<SocketAddr>;
254     fn to_socket_addrs(&self) -> io::Result<option::IntoIter<SocketAddr>> {
255         Ok(Some(*self).into_iter())
256     }
257 }
258
259 impl ToSocketAddrs for (IpAddr, u16) {
260     type Iter = option::IntoIter<SocketAddr>;
261     fn to_socket_addrs(&self) -> io::Result<option::IntoIter<SocketAddr>> {
262         let (ip, port) = *self;
263         Ok(Some(SocketAddr::new(ip, port)).into_iter())
264     }
265 }
266
267 fn resolve_socket_addr(s: &str, p: u16) -> io::Result<vec::IntoIter<SocketAddr>> {
268     let ips = try!(lookup_host(s));
269     let v: Vec<_> = try!(ips.map(|a| {
270         a.map(|mut a| { a.set_port(p); a })
271     }).collect());
272     Ok(v.into_iter())
273 }
274
275 impl<'a> ToSocketAddrs for (&'a str, u16) {
276     type Iter = vec::IntoIter<SocketAddr>;
277     fn to_socket_addrs(&self) -> io::Result<vec::IntoIter<SocketAddr>> {
278         let (host, port) = *self;
279
280         // try to parse the host as a regular IpAddr first
281         match host.parse().ok() {
282             Some(addr) => return Ok(vec![SocketAddr::new(addr, port)].into_iter()),
283             None => {}
284         }
285
286         resolve_socket_addr(host, port)
287     }
288 }
289
290 // accepts strings like 'localhost:12345'
291 impl ToSocketAddrs for str {
292     type Iter = vec::IntoIter<SocketAddr>;
293     fn to_socket_addrs(&self) -> io::Result<vec::IntoIter<SocketAddr>> {
294         // try to parse as a regular SocketAddr first
295         match self.parse().ok() {
296             Some(addr) => return Ok(vec![addr].into_iter()),
297             None => {}
298         }
299
300         macro_rules! try_opt {
301             ($e:expr, $msg:expr) => (
302                 match $e {
303                     Some(r) => r,
304                     None => return Err(io::Error::new(io::ErrorKind::InvalidInput,
305                                                       $msg, None)),
306                 }
307             )
308         }
309
310         // split the string by ':' and convert the second part to u16
311         let mut parts_iter = self.rsplitn(2, ':');
312         let port_str = try_opt!(parts_iter.next(), "invalid socket address");
313         let host = try_opt!(parts_iter.next(), "invalid socket address");
314         let port: u16 = try_opt!(port_str.parse().ok(), "invalid port value");
315         resolve_socket_addr(host, port)
316     }
317 }
318
319 impl<'a, T: ToSocketAddrs + ?Sized> ToSocketAddrs for &'a T {
320     type Iter = T::Iter;
321     fn to_socket_addrs(&self) -> io::Result<T::Iter> {
322         (**self).to_socket_addrs()
323     }
324 }
325
326 #[cfg(test)]
327 mod tests {
328     use prelude::v1::*;
329     use io;
330     use net::*;
331     use net::Ipv6MulticastScope::*;
332
333     #[test]
334     fn test_from_str_ipv4() {
335         assert_eq!(Ok(Ipv4Addr::new(127, 0, 0, 1)), "127.0.0.1".parse());
336         assert_eq!(Ok(Ipv4Addr::new(255, 255, 255, 255)), "255.255.255.255".parse());
337         assert_eq!(Ok(Ipv4Addr::new(0, 0, 0, 0)), "0.0.0.0".parse());
338
339         // out of range
340         let none: Option<IpAddr> = "256.0.0.1".parse().ok();
341         assert_eq!(None, none);
342         // too short
343         let none: Option<IpAddr> = "255.0.0".parse().ok();
344         assert_eq!(None, none);
345         // too long
346         let none: Option<IpAddr> = "255.0.0.1.2".parse().ok();
347         assert_eq!(None, none);
348         // no number between dots
349         let none: Option<IpAddr> = "255.0..1".parse().ok();
350         assert_eq!(None, none);
351     }
352
353     #[test]
354     fn test_from_str_ipv6() {
355         assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)), "0:0:0:0:0:0:0:0".parse());
356         assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), "0:0:0:0:0:0:0:1".parse());
357
358         assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), "::1".parse());
359         assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)), "::".parse());
360
361         assert_eq!(Ok(Ipv6Addr::new(0x2a02, 0x6b8, 0, 0, 0, 0, 0x11, 0x11)),
362                 "2a02:6b8::11:11".parse());
363
364         // too long group
365         let none: Option<IpAddr> = "::00000".parse().ok();
366         assert_eq!(None, none);
367         // too short
368         let none: Option<IpAddr> = "1:2:3:4:5:6:7".parse().ok();
369         assert_eq!(None, none);
370         // too long
371         let none: Option<IpAddr> = "1:2:3:4:5:6:7:8:9".parse().ok();
372         assert_eq!(None, none);
373         // triple colon
374         let none: Option<IpAddr> = "1:2:::6:7:8".parse().ok();
375         assert_eq!(None, none);
376         // two double colons
377         let none: Option<IpAddr> = "1:2::6::8".parse().ok();
378         assert_eq!(None, none);
379     }
380
381     #[test]
382     fn test_from_str_ipv4_in_ipv6() {
383         assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 49152, 545)),
384                 "::192.0.2.33".parse());
385         assert_eq!(Ok(Ipv6Addr::new(0, 0, 0, 0, 0, 0xFFFF, 49152, 545)),
386                 "::FFFF:192.0.2.33".parse());
387         assert_eq!(Ok(Ipv6Addr::new(0x64, 0xff9b, 0, 0, 0, 0, 49152, 545)),
388                 "64:ff9b::192.0.2.33".parse());
389         assert_eq!(Ok(Ipv6Addr::new(0x2001, 0xdb8, 0x122, 0xc000, 0x2, 0x2100, 49152, 545)),
390                 "2001:db8:122:c000:2:2100:192.0.2.33".parse());
391
392         // colon after v4
393         let none: Option<IpAddr> = "::127.0.0.1:".parse().ok();
394         assert_eq!(None, none);
395         // not enough groups
396         let none: Option<IpAddr> = "1.2.3.4.5:127.0.0.1".parse().ok();
397         assert_eq!(None, none);
398         // too many groups
399         let none: Option<IpAddr> = "1.2.3.4.5:6:7:127.0.0.1".parse().ok();
400         assert_eq!(None, none);
401     }
402
403     #[test]
404     fn test_from_str_socket_addr() {
405         assert_eq!(Ok(SocketAddr::new(IpAddr::new_v4(77, 88, 21, 11), 80)),
406                 "77.88.21.11:80".parse());
407         assert_eq!(Ok(SocketAddr::new(IpAddr::new_v6(0x2a02, 0x6b8, 0, 1, 0, 0, 0, 1), 53)),
408                 "[2a02:6b8:0:1::1]:53".parse());
409         assert_eq!(Ok(SocketAddr::new(IpAddr::new_v6(0, 0, 0, 0, 0, 0, 0x7F00, 1), 22)),
410                 "[::127.0.0.1]:22".parse());
411
412         // without port
413         let none: Option<SocketAddr> = "127.0.0.1".parse().ok();
414         assert_eq!(None, none);
415         // without port
416         let none: Option<SocketAddr> = "127.0.0.1:".parse().ok();
417         assert_eq!(None, none);
418         // wrong brackets around v4
419         let none: Option<SocketAddr> = "[127.0.0.1]:22".parse().ok();
420         assert_eq!(None, none);
421         // port out of range
422         let none: Option<SocketAddr> = "127.0.0.1:123456".parse().ok();
423         assert_eq!(None, none);
424     }
425
426     #[test]
427     fn ipv6_addr_to_string() {
428         // ipv4-mapped address
429         let a1 = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc000, 0x280);
430         assert_eq!(a1.to_string(), "::ffff:192.0.2.128");
431
432         // ipv4-compatible address
433         let a1 = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0xc000, 0x280);
434         assert_eq!(a1.to_string(), "::192.0.2.128");
435
436         // v6 address with no zero segments
437         assert_eq!(Ipv6Addr::new(8, 9, 10, 11, 12, 13, 14, 15).to_string(),
438                    "8:9:a:b:c:d:e:f");
439
440         // reduce a single run of zeros
441         assert_eq!("ae::ffff:102:304",
442                    Ipv6Addr::new(0xae, 0, 0, 0, 0, 0xffff, 0x0102, 0x0304).to_string());
443
444         // don't reduce just a single zero segment
445         assert_eq!("1:2:3:4:5:6:0:8",
446                    Ipv6Addr::new(1, 2, 3, 4, 5, 6, 0, 8).to_string());
447
448         // 'any' address
449         assert_eq!("::", Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0).to_string());
450
451         // loopback address
452         assert_eq!("::1", Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_string());
453
454         // ends in zeros
455         assert_eq!("1::", Ipv6Addr::new(1, 0, 0, 0, 0, 0, 0, 0).to_string());
456
457         // two runs of zeros, second one is longer
458         assert_eq!("1:0:0:4::8", Ipv6Addr::new(1, 0, 0, 4, 0, 0, 0, 8).to_string());
459
460         // two runs of zeros, equal length
461         assert_eq!("1::4:5:0:0:8", Ipv6Addr::new(1, 0, 0, 4, 5, 0, 0, 8).to_string());
462     }
463
464     #[test]
465     fn ipv4_to_ipv6() {
466         assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678),
467                    Ipv4Addr::new(0x12, 0x34, 0x56, 0x78).to_ipv6_mapped());
468         assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x1234, 0x5678),
469                    Ipv4Addr::new(0x12, 0x34, 0x56, 0x78).to_ipv6_compatible());
470     }
471
472     #[test]
473     fn ipv6_to_ipv4() {
474         assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678).to_ipv4(),
475                    Some(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78)));
476         assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0x1234, 0x5678).to_ipv4(),
477                    Some(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78)));
478         assert_eq!(Ipv6Addr::new(0, 0, 1, 0, 0, 0, 0x1234, 0x5678).to_ipv4(),
479                    None);
480     }
481
482     #[test]
483     fn ipv4_properties() {
484         fn check(octets: &[u8; 4], unspec: bool, loopback: bool,
485                  private: bool, link_local: bool, global: bool,
486                  multicast: bool) {
487             let ip = Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3]);
488             assert_eq!(octets, &ip.octets());
489
490             assert_eq!(ip.is_unspecified(), unspec);
491             assert_eq!(ip.is_loopback(), loopback);
492             assert_eq!(ip.is_private(), private);
493             assert_eq!(ip.is_link_local(), link_local);
494             assert_eq!(ip.is_global(), global);
495             assert_eq!(ip.is_multicast(), multicast);
496         }
497
498         //    address                unspec loopbk privt  linloc global multicast
499         check(&[0, 0, 0, 0],         true,  false, false, false, true,  false);
500         check(&[0, 0, 0, 1],         false, false, false, false, true,  false);
501         check(&[1, 0, 0, 0],         false, false, false, false, true,  false);
502         check(&[10, 9, 8, 7],        false, false, true,  false, false, false);
503         check(&[127, 1, 2, 3],       false, true,  false, false, false, false);
504         check(&[172, 31, 254, 253],  false, false, true,  false, false,  false);
505         check(&[169, 254, 253, 242], false, false, false, true,  false, false);
506         check(&[192, 168, 254, 253], false, false, true,  false, false, false);
507         check(&[224, 0, 0, 0],       false, false, false, false, true,  true);
508         check(&[239, 255, 255, 255], false, false, false, false, true,  true);
509         check(&[255, 255, 255, 255], false, false, false, false, true,  false);
510     }
511
512     #[test]
513     fn ipv6_properties() {
514         fn check(str_addr: &str, unspec: bool, loopback: bool,
515                  unique_local: bool, global: bool,
516                  u_link_local: bool, u_site_local: bool, u_global: bool,
517                  m_scope: Option<Ipv6MulticastScope>) {
518             let ip: Ipv6Addr = str_addr.parse().ok().unwrap();
519             assert_eq!(str_addr, ip.to_string());
520
521             assert_eq!(ip.is_unspecified(), unspec);
522             assert_eq!(ip.is_loopback(), loopback);
523             assert_eq!(ip.is_unique_local(), unique_local);
524             assert_eq!(ip.is_global(), global);
525             assert_eq!(ip.is_unicast_link_local(), u_link_local);
526             assert_eq!(ip.is_unicast_site_local(), u_site_local);
527             assert_eq!(ip.is_unicast_global(), u_global);
528             assert_eq!(ip.multicast_scope(), m_scope);
529             assert_eq!(ip.is_multicast(), m_scope.is_some());
530         }
531
532         //    unspec loopbk uniqlo global unill  unisl  uniglo mscope
533         check("::",
534               true,  false, false, true,  false, false, true,  None);
535         check("::1",
536               false, true,  false, false, false, false, false, None);
537         check("::0.0.0.2",
538               false, false, false, true,  false, false, true,  None);
539         check("1::",
540               false, false, false, true,  false, false, true,  None);
541         check("fc00::",
542               false, false, true,  false, false, false, false, None);
543         check("fdff:ffff::",
544               false, false, true,  false, false, false, false, None);
545         check("fe80:ffff::",
546               false, false, false, false, true,  false, false, None);
547         check("febf:ffff::",
548               false, false, false, false, true,  false, false, None);
549         check("fec0::",
550               false, false, false, false, false, true,  false, None);
551         check("ff01::",
552               false, false, false, false, false, false, false, Some(InterfaceLocal));
553         check("ff02::",
554               false, false, false, false, false, false, false, Some(LinkLocal));
555         check("ff03::",
556               false, false, false, false, false, false, false, Some(RealmLocal));
557         check("ff04::",
558               false, false, false, false, false, false, false, Some(AdminLocal));
559         check("ff05::",
560               false, false, false, false, false, false, false, Some(SiteLocal));
561         check("ff08::",
562               false, false, false, false, false, false, false, Some(OrganizationLocal));
563         check("ff0e::",
564               false, false, false, true,  false, false, false, Some(Global));
565     }
566
567     fn tsa<A: ToSocketAddrs>(a: A) -> io::Result<Vec<SocketAddr>> {
568         Ok(try!(a.to_socket_addrs()).collect())
569     }
570
571     #[test]
572     fn to_socket_addr_socketaddr() {
573         let a = SocketAddr::new(IpAddr::new_v4(77, 88, 21, 11), 12345);
574         assert_eq!(Ok(vec![a]), tsa(a));
575     }
576
577     #[test]
578     fn to_socket_addr_ipaddr_u16() {
579         let a = IpAddr::new_v4(77, 88, 21, 11);
580         let p = 12345u16;
581         let e = SocketAddr::new(a, p);
582         assert_eq!(Ok(vec![e]), tsa((a, p)));
583     }
584
585     #[test]
586     fn to_socket_addr_str_u16() {
587         let a = SocketAddr::new(IpAddr::new_v4(77, 88, 21, 11), 24352);
588         assert_eq!(Ok(vec![a]), tsa(("77.88.21.11", 24352u16)));
589
590         let a = SocketAddr::new(IpAddr::new_v6(0x2a02, 0x6b8, 0, 1, 0, 0, 0, 1), 53);
591         assert_eq!(Ok(vec![a]), tsa(("2a02:6b8:0:1::1", 53)));
592
593         let a = SocketAddr::new(IpAddr::new_v4(127, 0, 0, 1), 23924);
594         assert!(tsa(("localhost", 23924u16)).unwrap().contains(&a));
595     }
596
597     #[test]
598     fn to_socket_addr_str() {
599         let a = SocketAddr::new(IpAddr::new_v4(77, 88, 21, 11), 24352);
600         assert_eq!(Ok(vec![a]), tsa("77.88.21.11:24352"));
601
602         let a = SocketAddr::new(IpAddr::new_v6(0x2a02, 0x6b8, 0, 1, 0, 0, 0, 1), 53);
603         assert_eq!(Ok(vec![a]), tsa("[2a02:6b8:0:1::1]:53"));
604
605         let a = SocketAddr::new(IpAddr::new_v4(127, 0, 0, 1), 23924);
606         assert!(tsa("localhost:23924").unwrap().contains(&a));
607     }
608 }