]> git.lizzy.rs Git - rust.git/blob - src/libstd/net/addr.rs
Rollup merge of #31969 - brson:relnotes, r=alexcrichton
[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 mem;
17 use net::{lookup_host, ntoh, hton, IpAddr, Ipv4Addr, Ipv6Addr};
18 use option;
19 use sys::net::netc as c;
20 use sys_common::{FromInner, AsInner, IntoInner};
21 use vec;
22 use iter;
23 use slice;
24
25 /// Representation of a socket address for networking applications.
26 ///
27 /// A socket address can either represent the IPv4 or IPv6 protocol and is
28 /// paired with at least a port number as well. Each protocol may have more
29 /// specific information about the address available to it as well.
30 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
31 #[stable(feature = "rust1", since = "1.0.0")]
32 pub enum SocketAddr {
33     /// An IPv4 socket address which is a (ip, port) combination.
34     #[stable(feature = "rust1", since = "1.0.0")]
35     V4(#[stable(feature = "rust1", since = "1.0.0")] SocketAddrV4),
36     /// An IPv6 socket address
37     #[stable(feature = "rust1", since = "1.0.0")]
38     V6(#[stable(feature = "rust1", since = "1.0.0")] SocketAddrV6),
39 }
40
41 /// An IPv4 socket address which is a (ip, port) combination.
42 #[derive(Copy)]
43 #[stable(feature = "rust1", since = "1.0.0")]
44 pub struct SocketAddrV4 { inner: c::sockaddr_in }
45
46 /// An IPv6 socket address.
47 #[derive(Copy)]
48 #[stable(feature = "rust1", since = "1.0.0")]
49 pub struct SocketAddrV6 { inner: c::sockaddr_in6 }
50
51 impl SocketAddr {
52     /// Creates a new socket address from the (ip, port) pair.
53     #[stable(feature = "ip_addr", since = "1.7.0")]
54     pub fn new(ip: IpAddr, port: u16) -> SocketAddr {
55         match ip {
56             IpAddr::V4(a) => SocketAddr::V4(SocketAddrV4::new(a, port)),
57             IpAddr::V6(a) => SocketAddr::V6(SocketAddrV6::new(a, port, 0, 0)),
58         }
59     }
60
61     /// Returns the IP address associated with this socket address.
62     #[stable(feature = "ip_addr", since = "1.7.0")]
63     pub fn ip(&self) -> IpAddr {
64         match *self {
65             SocketAddr::V4(ref a) => IpAddr::V4(*a.ip()),
66             SocketAddr::V6(ref a) => IpAddr::V6(*a.ip()),
67         }
68     }
69
70     /// Change the IP address associated with this socket address.
71     #[unstable(feature = "sockaddr_setters", reason = "recent addition", issue = "31572")]
72     pub fn set_ip(&mut self, new_ip: IpAddr) {
73         // `match (*self, new_ip)` would have us mutate a copy of self only to throw it away.
74         match (self, new_ip) {
75             (&mut SocketAddr::V4(ref mut a), IpAddr::V4(new_ip)) => a.set_ip(new_ip),
76             (&mut SocketAddr::V6(ref mut a), IpAddr::V6(new_ip)) => a.set_ip(new_ip),
77             (self_, new_ip) => *self_ = Self::new(new_ip, self_.port()),
78         }
79     }
80
81     /// Returns the port number associated with this socket address.
82     #[stable(feature = "rust1", since = "1.0.0")]
83     pub fn port(&self) -> u16 {
84         match *self {
85             SocketAddr::V4(ref a) => a.port(),
86             SocketAddr::V6(ref a) => a.port(),
87         }
88     }
89
90     /// Change the port number associated with this socket address.
91     #[unstable(feature = "sockaddr_setters", reason = "recent addition", issue = "31572")]
92     pub fn set_port(&mut self, new_port: u16) {
93         match *self {
94             SocketAddr::V4(ref mut a) => a.set_port(new_port),
95             SocketAddr::V6(ref mut a) => a.set_port(new_port),
96         }
97     }
98 }
99
100 impl SocketAddrV4 {
101     /// Creates a new socket address from the (ip, port) pair.
102     #[stable(feature = "rust1", since = "1.0.0")]
103     pub fn new(ip: Ipv4Addr, port: u16) -> SocketAddrV4 {
104         SocketAddrV4 {
105             inner: c::sockaddr_in {
106                 sin_family: c::AF_INET as c::sa_family_t,
107                 sin_port: hton(port),
108                 sin_addr: *ip.as_inner(),
109                 .. unsafe { mem::zeroed() }
110             },
111         }
112     }
113
114     /// Returns the IP address associated with this socket address.
115     #[stable(feature = "rust1", since = "1.0.0")]
116     pub fn ip(&self) -> &Ipv4Addr {
117         unsafe {
118             &*(&self.inner.sin_addr as *const c::in_addr as *const Ipv4Addr)
119         }
120     }
121
122     /// Change the IP address associated with this socket address.
123     #[unstable(feature = "sockaddr_setters", reason = "recent addition", issue = "31572")]
124     pub fn set_ip(&mut self, new_ip: Ipv4Addr) { self.inner.sin_addr = *new_ip.as_inner() }
125
126     /// Returns the port number associated with this socket address.
127     #[stable(feature = "rust1", since = "1.0.0")]
128     pub fn port(&self) -> u16 { ntoh(self.inner.sin_port) }
129
130     /// Change the port number associated with this socket address.
131     #[unstable(feature = "sockaddr_setters", reason = "recent addition", issue = "31572")]
132     pub fn set_port(&mut self, new_port: u16) { self.inner.sin_port = hton(new_port) }
133 }
134
135 impl SocketAddrV6 {
136     /// Creates a new socket address from the ip/port/flowinfo/scope_id
137     /// components.
138     #[stable(feature = "rust1", since = "1.0.0")]
139     pub fn new(ip: Ipv6Addr, port: u16, flowinfo: u32, scope_id: u32)
140                -> SocketAddrV6 {
141         SocketAddrV6 {
142             inner: c::sockaddr_in6 {
143                 sin6_family: c::AF_INET6 as c::sa_family_t,
144                 sin6_port: hton(port),
145                 sin6_addr: *ip.as_inner(),
146                 sin6_flowinfo: hton(flowinfo),
147                 sin6_scope_id: hton(scope_id),
148                 .. unsafe { mem::zeroed() }
149             },
150         }
151     }
152
153     /// Returns the IP address associated with this socket address.
154     #[stable(feature = "rust1", since = "1.0.0")]
155     pub fn ip(&self) -> &Ipv6Addr {
156         unsafe {
157             &*(&self.inner.sin6_addr as *const c::in6_addr as *const Ipv6Addr)
158         }
159     }
160
161     /// Change the IP address associated with this socket address.
162     #[unstable(feature = "sockaddr_setters", reason = "recent addition", issue = "31572")]
163     pub fn set_ip(&mut self, new_ip: Ipv6Addr) { self.inner.sin6_addr = *new_ip.as_inner() }
164
165     /// Returns the port number associated with this socket address.
166     #[stable(feature = "rust1", since = "1.0.0")]
167     pub fn port(&self) -> u16 { ntoh(self.inner.sin6_port) }
168
169     /// Change the port number associated with this socket address.
170     #[unstable(feature = "sockaddr_setters", reason = "recent addition", issue = "31572")]
171     pub fn set_port(&mut self, new_port: u16) { self.inner.sin6_port = hton(new_port) }
172
173     /// Returns the flow information associated with this address,
174     /// corresponding to the `sin6_flowinfo` field in C.
175     #[stable(feature = "rust1", since = "1.0.0")]
176     pub fn flowinfo(&self) -> u32 { ntoh(self.inner.sin6_flowinfo) }
177
178     /// Change the flow information associated with this socket address.
179     #[unstable(feature = "sockaddr_setters", reason = "recent addition", issue = "31572")]
180     pub fn set_flowinfo(&mut self, new_flowinfo: u32) {
181         self.inner.sin6_flowinfo = hton(new_flowinfo)
182     }
183
184     /// Returns the scope ID associated with this address,
185     /// corresponding to the `sin6_scope_id` field in C.
186     #[stable(feature = "rust1", since = "1.0.0")]
187     pub fn scope_id(&self) -> u32 { ntoh(self.inner.sin6_scope_id) }
188
189     /// Change the scope ID associated with this socket address.
190     #[unstable(feature = "sockaddr_setters", reason = "recent addition", issue = "31572")]
191     pub fn set_scope_id(&mut self, new_scope_id: u32) {
192         self.inner.sin6_scope_id = hton(new_scope_id)
193     }
194 }
195
196 impl FromInner<c::sockaddr_in> for SocketAddrV4 {
197     fn from_inner(addr: c::sockaddr_in) -> SocketAddrV4 {
198         SocketAddrV4 { inner: addr }
199     }
200 }
201
202 impl FromInner<c::sockaddr_in6> for SocketAddrV6 {
203     fn from_inner(addr: c::sockaddr_in6) -> SocketAddrV6 {
204         SocketAddrV6 { inner: addr }
205     }
206 }
207
208 impl<'a> IntoInner<(*const c::sockaddr, c::socklen_t)> for &'a SocketAddr {
209     fn into_inner(self) -> (*const c::sockaddr, c::socklen_t) {
210         match *self {
211             SocketAddr::V4(ref a) => {
212                 (a as *const _ as *const _, mem::size_of_val(a) as c::socklen_t)
213             }
214             SocketAddr::V6(ref a) => {
215                 (a as *const _ as *const _, mem::size_of_val(a) as c::socklen_t)
216             }
217         }
218     }
219 }
220
221 #[stable(feature = "rust1", since = "1.0.0")]
222 impl fmt::Display for SocketAddr {
223     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
224         match *self {
225             SocketAddr::V4(ref a) => a.fmt(f),
226             SocketAddr::V6(ref a) => a.fmt(f),
227         }
228     }
229 }
230
231 #[stable(feature = "rust1", since = "1.0.0")]
232 impl fmt::Display for SocketAddrV4 {
233     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
234         write!(f, "{}:{}", self.ip(), self.port())
235     }
236 }
237
238 #[stable(feature = "rust1", since = "1.0.0")]
239 impl fmt::Debug for SocketAddrV4 {
240     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
241         fmt::Display::fmt(self, fmt)
242     }
243 }
244
245 #[stable(feature = "rust1", since = "1.0.0")]
246 impl fmt::Display for SocketAddrV6 {
247     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
248         write!(f, "[{}]:{}", self.ip(), self.port())
249     }
250 }
251
252 #[stable(feature = "rust1", since = "1.0.0")]
253 impl fmt::Debug for SocketAddrV6 {
254     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
255         fmt::Display::fmt(self, fmt)
256     }
257 }
258
259 #[stable(feature = "rust1", since = "1.0.0")]
260 impl Clone for SocketAddrV4 {
261     fn clone(&self) -> SocketAddrV4 { *self }
262 }
263 #[stable(feature = "rust1", since = "1.0.0")]
264 impl Clone for SocketAddrV6 {
265     fn clone(&self) -> SocketAddrV6 { *self }
266 }
267
268 #[stable(feature = "rust1", since = "1.0.0")]
269 impl PartialEq for SocketAddrV4 {
270     fn eq(&self, other: &SocketAddrV4) -> bool {
271         self.inner.sin_port == other.inner.sin_port &&
272             self.inner.sin_addr.s_addr == other.inner.sin_addr.s_addr
273     }
274 }
275 #[stable(feature = "rust1", since = "1.0.0")]
276 impl PartialEq for SocketAddrV6 {
277     fn eq(&self, other: &SocketAddrV6) -> bool {
278         self.inner.sin6_port == other.inner.sin6_port &&
279             self.inner.sin6_addr.s6_addr == other.inner.sin6_addr.s6_addr &&
280             self.inner.sin6_flowinfo == other.inner.sin6_flowinfo &&
281             self.inner.sin6_scope_id == other.inner.sin6_scope_id
282     }
283 }
284 #[stable(feature = "rust1", since = "1.0.0")]
285 impl Eq for SocketAddrV4 {}
286 #[stable(feature = "rust1", since = "1.0.0")]
287 impl Eq for SocketAddrV6 {}
288
289 #[stable(feature = "rust1", since = "1.0.0")]
290 impl hash::Hash for SocketAddrV4 {
291     fn hash<H: hash::Hasher>(&self, s: &mut H) {
292         (self.inner.sin_port, self.inner.sin_addr.s_addr).hash(s)
293     }
294 }
295 #[stable(feature = "rust1", since = "1.0.0")]
296 impl hash::Hash for SocketAddrV6 {
297     fn hash<H: hash::Hasher>(&self, s: &mut H) {
298         (self.inner.sin6_port, &self.inner.sin6_addr.s6_addr,
299          self.inner.sin6_flowinfo, self.inner.sin6_scope_id).hash(s)
300     }
301 }
302
303 /// A trait for objects which can be converted or resolved to one or more
304 /// `SocketAddr` values.
305 ///
306 /// This trait is used for generic address resolution when constructing network
307 /// objects.  By default it is implemented for the following types:
308 ///
309 ///  * `SocketAddr`, `SocketAddrV4`, `SocketAddrV6` - `to_socket_addrs` is
310 ///    identity function.
311 ///
312 ///  * `(IpvNAddr, u16)` - `to_socket_addrs` constructs `SocketAddr` trivially.
313 ///
314 ///  * `(&str, u16)` - the string should be either a string representation of an
315 ///    IP address expected by `FromStr` implementation for `IpvNAddr` or a host
316 ///    name.
317 ///
318 ///  * `&str` - the string should be either a string representation of a
319 ///    `SocketAddr` as expected by its `FromStr` implementation or a string like
320 ///    `<host_name>:<port>` pair where `<port>` is a `u16` value.
321 ///
322 /// This trait allows constructing network objects like `TcpStream` or
323 /// `UdpSocket` easily with values of various types for the bind/connection
324 /// address. It is needed because sometimes one type is more appropriate than
325 /// the other: for simple uses a string like `"localhost:12345"` is much nicer
326 /// than manual construction of the corresponding `SocketAddr`, but sometimes
327 /// `SocketAddr` value is *the* main source of the address, and converting it to
328 /// some other type (e.g. a string) just for it to be converted back to
329 /// `SocketAddr` in constructor methods is pointless.
330 ///
331 /// Some examples:
332 ///
333 /// ```no_run
334 /// use std::net::{SocketAddrV4, TcpStream, UdpSocket, TcpListener, Ipv4Addr};
335 ///
336 /// fn main() {
337 ///     let ip = Ipv4Addr::new(127, 0, 0, 1);
338 ///     let port = 12345;
339 ///
340 ///     // The following lines are equivalent modulo possible "localhost" name
341 ///     // resolution differences
342 ///     let tcp_s = TcpStream::connect(SocketAddrV4::new(ip, port));
343 ///     let tcp_s = TcpStream::connect((ip, port));
344 ///     let tcp_s = TcpStream::connect(("127.0.0.1", port));
345 ///     let tcp_s = TcpStream::connect(("localhost", port));
346 ///     let tcp_s = TcpStream::connect("127.0.0.1:12345");
347 ///     let tcp_s = TcpStream::connect("localhost:12345");
348 ///
349 ///     // TcpListener::bind(), UdpSocket::bind() and UdpSocket::send_to()
350 ///     // behave similarly
351 ///     let tcp_l = TcpListener::bind("localhost:12345");
352 ///
353 ///     let mut udp_s = UdpSocket::bind(("127.0.0.1", port)).unwrap();
354 ///     udp_s.send_to(&[7], (ip, 23451)).unwrap();
355 /// }
356 /// ```
357 #[stable(feature = "rust1", since = "1.0.0")]
358 pub trait ToSocketAddrs {
359     /// Returned iterator over socket addresses which this type may correspond
360     /// to.
361     #[stable(feature = "rust1", since = "1.0.0")]
362     type Iter: Iterator<Item=SocketAddr>;
363
364     /// Converts this object to an iterator of resolved `SocketAddr`s.
365     ///
366     /// The returned iterator may not actually yield any values depending on the
367     /// outcome of any resolution performed.
368     ///
369     /// Note that this function may block the current thread while resolution is
370     /// performed.
371     ///
372     /// # Errors
373     ///
374     /// Any errors encountered during resolution will be returned as an `Err`.
375     #[stable(feature = "rust1", since = "1.0.0")]
376     fn to_socket_addrs(&self) -> io::Result<Self::Iter>;
377 }
378
379 #[stable(feature = "rust1", since = "1.0.0")]
380 impl ToSocketAddrs for SocketAddr {
381     type Iter = option::IntoIter<SocketAddr>;
382     fn to_socket_addrs(&self) -> io::Result<option::IntoIter<SocketAddr>> {
383         Ok(Some(*self).into_iter())
384     }
385 }
386
387 #[stable(feature = "rust1", since = "1.0.0")]
388 impl ToSocketAddrs for SocketAddrV4 {
389     type Iter = option::IntoIter<SocketAddr>;
390     fn to_socket_addrs(&self) -> io::Result<option::IntoIter<SocketAddr>> {
391         SocketAddr::V4(*self).to_socket_addrs()
392     }
393 }
394
395 #[stable(feature = "rust1", since = "1.0.0")]
396 impl ToSocketAddrs for SocketAddrV6 {
397     type Iter = option::IntoIter<SocketAddr>;
398     fn to_socket_addrs(&self) -> io::Result<option::IntoIter<SocketAddr>> {
399         SocketAddr::V6(*self).to_socket_addrs()
400     }
401 }
402
403 #[stable(feature = "rust1", since = "1.0.0")]
404 impl ToSocketAddrs for (IpAddr, u16) {
405     type Iter = option::IntoIter<SocketAddr>;
406     fn to_socket_addrs(&self) -> io::Result<option::IntoIter<SocketAddr>> {
407         let (ip, port) = *self;
408         match ip {
409             IpAddr::V4(ref a) => (*a, port).to_socket_addrs(),
410             IpAddr::V6(ref a) => (*a, port).to_socket_addrs(),
411         }
412     }
413 }
414
415 #[stable(feature = "rust1", since = "1.0.0")]
416 impl ToSocketAddrs for (Ipv4Addr, u16) {
417     type Iter = option::IntoIter<SocketAddr>;
418     fn to_socket_addrs(&self) -> io::Result<option::IntoIter<SocketAddr>> {
419         let (ip, port) = *self;
420         SocketAddrV4::new(ip, port).to_socket_addrs()
421     }
422 }
423
424 #[stable(feature = "rust1", since = "1.0.0")]
425 impl ToSocketAddrs for (Ipv6Addr, u16) {
426     type Iter = option::IntoIter<SocketAddr>;
427     fn to_socket_addrs(&self) -> io::Result<option::IntoIter<SocketAddr>> {
428         let (ip, port) = *self;
429         SocketAddrV6::new(ip, port, 0, 0).to_socket_addrs()
430     }
431 }
432
433 fn resolve_socket_addr(s: &str, p: u16) -> io::Result<vec::IntoIter<SocketAddr>> {
434     let ips = try!(lookup_host(s));
435     let v: Vec<_> = try!(ips.map(|a| {
436         a.map(|mut a| {
437             a.set_port(p);
438             a
439         })
440     }).collect());
441     Ok(v.into_iter())
442 }
443
444 #[stable(feature = "rust1", since = "1.0.0")]
445 impl<'a> ToSocketAddrs for (&'a str, u16) {
446     type Iter = vec::IntoIter<SocketAddr>;
447     fn to_socket_addrs(&self) -> io::Result<vec::IntoIter<SocketAddr>> {
448         let (host, port) = *self;
449
450         // try to parse the host as a regular IP address first
451         if let Ok(addr) = host.parse::<Ipv4Addr>() {
452             let addr = SocketAddrV4::new(addr, port);
453             return Ok(vec![SocketAddr::V4(addr)].into_iter())
454         }
455         if let Ok(addr) = host.parse::<Ipv6Addr>() {
456             let addr = SocketAddrV6::new(addr, port, 0, 0);
457             return Ok(vec![SocketAddr::V6(addr)].into_iter())
458         }
459
460         resolve_socket_addr(host, port)
461     }
462 }
463
464 // accepts strings like 'localhost:12345'
465 #[stable(feature = "rust1", since = "1.0.0")]
466 impl ToSocketAddrs for str {
467     type Iter = vec::IntoIter<SocketAddr>;
468     fn to_socket_addrs(&self) -> io::Result<vec::IntoIter<SocketAddr>> {
469         // try to parse as a regular SocketAddr first
470         if let Some(addr) = self.parse().ok() {
471             return Ok(vec![addr].into_iter());
472         }
473
474         macro_rules! try_opt {
475             ($e:expr, $msg:expr) => (
476                 match $e {
477                     Some(r) => r,
478                     None => return Err(io::Error::new(io::ErrorKind::InvalidInput,
479                                                       $msg)),
480                 }
481             )
482         }
483
484         // split the string by ':' and convert the second part to u16
485         let mut parts_iter = self.rsplitn(2, ':');
486         let port_str = try_opt!(parts_iter.next(), "invalid socket address");
487         let host = try_opt!(parts_iter.next(), "invalid socket address");
488         let port: u16 = try_opt!(port_str.parse().ok(), "invalid port value");
489         resolve_socket_addr(host, port)
490     }
491 }
492
493 #[stable(feature = "slice_to_socket_addrs", since = "1.8.0")]
494 impl<'a> ToSocketAddrs for &'a [SocketAddr] {
495     type Iter = iter::Cloned<slice::Iter<'a, SocketAddr>>;
496
497     fn to_socket_addrs(&self) -> io::Result<Self::Iter> {
498         Ok(self.iter().cloned())
499     }
500 }
501
502 #[stable(feature = "rust1", since = "1.0.0")]
503 impl<'a, T: ToSocketAddrs + ?Sized> ToSocketAddrs for &'a T {
504     type Iter = T::Iter;
505     fn to_socket_addrs(&self) -> io::Result<T::Iter> {
506         (**self).to_socket_addrs()
507     }
508 }
509
510 #[cfg(test)]
511 mod tests {
512     use prelude::v1::*;
513     use net::*;
514     use net::test::{tsa, sa6, sa4};
515
516     #[test]
517     fn to_socket_addr_ipaddr_u16() {
518         let a = Ipv4Addr::new(77, 88, 21, 11);
519         let p = 12345;
520         let e = SocketAddr::V4(SocketAddrV4::new(a, p));
521         assert_eq!(Ok(vec![e]), tsa((a, p)));
522     }
523
524     #[test]
525     fn to_socket_addr_str_u16() {
526         let a = sa4(Ipv4Addr::new(77, 88, 21, 11), 24352);
527         assert_eq!(Ok(vec![a]), tsa(("77.88.21.11", 24352)));
528
529         let a = sa6(Ipv6Addr::new(0x2a02, 0x6b8, 0, 1, 0, 0, 0, 1), 53);
530         assert_eq!(Ok(vec![a]), tsa(("2a02:6b8:0:1::1", 53)));
531
532         let a = sa4(Ipv4Addr::new(127, 0, 0, 1), 23924);
533         assert!(tsa(("localhost", 23924)).unwrap().contains(&a));
534     }
535
536     #[test]
537     fn to_socket_addr_str() {
538         let a = sa4(Ipv4Addr::new(77, 88, 21, 11), 24352);
539         assert_eq!(Ok(vec![a]), tsa("77.88.21.11:24352"));
540
541         let a = sa6(Ipv6Addr::new(0x2a02, 0x6b8, 0, 1, 0, 0, 0, 1), 53);
542         assert_eq!(Ok(vec![a]), tsa("[2a02:6b8:0:1::1]:53"));
543
544         let a = sa4(Ipv4Addr::new(127, 0, 0, 1), 23924);
545         assert!(tsa("localhost:23924").unwrap().contains(&a));
546     }
547
548     // FIXME: figure out why this fails on openbsd and bitrig and fix it
549     #[test]
550     #[cfg(not(any(windows, target_os = "openbsd", target_os = "bitrig")))]
551     fn to_socket_addr_str_bad() {
552         assert!(tsa("1200::AB00:1234::2552:7777:1313:34300").is_err());
553     }
554
555     #[test]
556     fn set_ip() {
557         fn ip4(low: u8) -> Ipv4Addr { Ipv4Addr::new(77, 88, 21, low) }
558         fn ip6(low: u16) -> Ipv6Addr { Ipv6Addr::new(0x2a02, 0x6b8, 0, 1, 0, 0, 0, low) }
559
560         let mut v4 = SocketAddrV4::new(ip4(11), 80);
561         assert_eq!(v4.ip(), &ip4(11));
562         v4.set_ip(ip4(12));
563         assert_eq!(v4.ip(), &ip4(12));
564
565         let mut addr = SocketAddr::V4(v4);
566         assert_eq!(addr.ip(), IpAddr::V4(ip4(12)));
567         addr.set_ip(IpAddr::V4(ip4(13)));
568         assert_eq!(addr.ip(), IpAddr::V4(ip4(13)));
569         addr.set_ip(IpAddr::V6(ip6(14)));
570         assert_eq!(addr.ip(), IpAddr::V6(ip6(14)));
571
572         let mut v6 = SocketAddrV6::new(ip6(1), 80, 0, 0);
573         assert_eq!(v6.ip(), &ip6(1));
574         v6.set_ip(ip6(2));
575         assert_eq!(v6.ip(), &ip6(2));
576
577         let mut addr = SocketAddr::V6(v6);
578         assert_eq!(addr.ip(), IpAddr::V6(ip6(2)));
579         addr.set_ip(IpAddr::V6(ip6(3)));
580         assert_eq!(addr.ip(), IpAddr::V6(ip6(3)));
581         addr.set_ip(IpAddr::V4(ip4(4)));
582         assert_eq!(addr.ip(), IpAddr::V4(ip4(4)));
583     }
584
585     #[test]
586     fn set_port() {
587         let mut v4 = SocketAddrV4::new(Ipv4Addr::new(77, 88, 21, 11), 80);
588         assert_eq!(v4.port(), 80);
589         v4.set_port(443);
590         assert_eq!(v4.port(), 443);
591
592         let mut addr = SocketAddr::V4(v4);
593         assert_eq!(addr.port(), 443);
594         addr.set_port(8080);
595         assert_eq!(addr.port(), 8080);
596
597         let mut v6 = SocketAddrV6::new(Ipv6Addr::new(0x2a02, 0x6b8, 0, 1, 0, 0, 0, 1), 80, 0, 0);
598         assert_eq!(v6.port(), 80);
599         v6.set_port(443);
600         assert_eq!(v6.port(), 443);
601
602         let mut addr = SocketAddr::V6(v6);
603         assert_eq!(addr.port(), 443);
604         addr.set_port(8080);
605         assert_eq!(addr.port(), 8080);
606     }
607
608     #[test]
609     fn set_flowinfo() {
610         let mut v6 = SocketAddrV6::new(Ipv6Addr::new(0x2a02, 0x6b8, 0, 1, 0, 0, 0, 1), 80, 10, 0);
611         assert_eq!(v6.flowinfo(), 10);
612         v6.set_flowinfo(20);
613         assert_eq!(v6.flowinfo(), 20);
614     }
615
616     #[test]
617     fn set_scope_id() {
618         let mut v6 = SocketAddrV6::new(Ipv6Addr::new(0x2a02, 0x6b8, 0, 1, 0, 0, 0, 1), 80, 0, 10);
619         assert_eq!(v6.scope_id(), 10);
620         v6.set_scope_id(20);
621         assert_eq!(v6.scope_id(), 20);
622     }
623 }