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.
16 use libc::{self, socklen_t, sa_family_t};
18 use net::{IpAddr, lookup_host, ntoh, hton};
20 use sys_common::{FromInner, AsInner, IntoInner};
23 /// Representation of a socket address for networking applications
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 {
34 V4(libc::sockaddr_in),
35 V6(libc::sockaddr_in6),
39 /// Creates a new socket address from the (ip, port) pair.
40 pub fn new(ip: IpAddr, port: u16) -> SocketAddr {
42 IpAddr::V4(ref ip) => {
43 Repr::V4(libc::sockaddr_in {
44 sin_family: libc::AF_INET as sa_family_t,
46 sin_addr: *ip.as_inner(),
47 .. unsafe { mem::zeroed() }
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() }
59 SocketAddr { repr: repr }
62 /// Gets the IP address associated with this socket address.
63 pub fn ip(&self) -> IpAddr {
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)),
70 /// Gets the port number associated with this socket address
71 pub fn port(&self) -> u16 {
73 Repr::V4(ref sa) => ntoh(sa.sin_port),
74 Repr::V6(ref sa) => ntoh(sa.sin6_port),
78 fn set_port(&mut self, port: u16) {
80 Repr::V4(ref mut sa) => sa.sin_port = hton(port),
81 Repr::V6(ref mut sa) => sa.sin6_port = hton(port),
86 #[stable(feature = "rust1", since = "1.0.0")]
87 impl fmt::Display for SocketAddr {
88 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
90 Repr::V4(_) => write!(f, "{}:{}", self.ip(), self.port()),
91 Repr::V6(_) => write!(f, "[{}]:{}", self.ip(), self.port()),
96 impl FromInner<libc::sockaddr_in> for SocketAddr {
97 fn from_inner(addr: libc::sockaddr_in) -> SocketAddr {
98 SocketAddr { repr: Repr::V4(addr) }
102 impl FromInner<libc::sockaddr_in6> for SocketAddr {
103 fn from_inner(addr: libc::sockaddr_in6) -> SocketAddr {
104 SocketAddr { repr: Repr::V6(addr) }
108 impl<'a> IntoInner<(*const libc::sockaddr, socklen_t)> for &'a SocketAddr {
109 fn into_inner(self) -> (*const libc::sockaddr, socklen_t) {
112 (a as *const _ as *const _, mem::size_of_val(a) as socklen_t)
115 (a as *const _ as *const _, mem::size_of_val(a) as socklen_t)
121 impl fmt::Debug for SocketAddr {
122 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
123 fmt::Display::fmt(self, fmt)
127 impl Clone for Repr {
128 fn clone(&self) -> Repr { *self }
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
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
151 impl<S: hash::Hasher + hash::Writer> hash::Hash<S> for Repr {
152 fn hash(&self, s: &mut S) {
155 (a.sin_family, a.sin_port, a.sin_addr.s_addr).hash(s)
158 (a.sin6_family, a.sin6_port, &a.sin6_addr.s6_addr,
159 a.sin6_flowinfo, a.sin6_scope_id).hash(s)
165 #[stable(feature = "rust1", since = "1.0.0")]
166 impl hash::Hash for Repr {
167 fn hash<H: hash::Hasher>(&self, s: &mut H) {
170 (a.sin_family, a.sin_port, a.sin_addr.s_addr).hash(s)
173 (a.sin6_family, a.sin6_port, &a.sin6_addr.s6_addr,
174 a.sin6_flowinfo, a.sin6_scope_id).hash(s)
180 /// A trait for objects which can be converted or resolved to one or more
181 /// `SocketAddr` values.
183 /// This trait is used for generic address resolution when constructing network
184 /// objects. By default it is implemented for the following types:
186 /// * `SocketAddr` - `to_socket_addrs` is identity function.
188 /// * `(IpAddr, u16)` - `to_socket_addrs` constructs `SocketAddr` trivially.
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
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.
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.
210 /// use std::net::{IpAddr, SocketAddr, TcpStream, UdpSocket, TcpListener};
213 /// let ip = IpAddr::new_v4(127, 0, 0, 1);
214 /// let port = 12345;
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");
225 /// // TcpListener::bind(), UdpSocket::bind() and UdpSocket::send_to()
226 /// // behave similarly
227 /// let tcp_l = TcpListener::bind("localhost:12345");
229 /// let mut udp_s = UdpSocket::bind(&("127.0.0.1", port)).unwrap();
230 /// udp_s.send_to(&[7], &(ip, 23451));
233 pub trait ToSocketAddrs {
234 /// Returned iterator over socket addresses which this type may correspond
236 type Iter: Iterator<Item=SocketAddr>;
238 /// Converts this object to an iterator of resolved `SocketAddr`s.
240 /// The returned iterator may not actually yield any values depending on the
241 /// outcome of any resolution performed.
243 /// Note that this function may block the current thread while resolution is
248 /// Any errors encountered during resolution will be returned as an `Err`.
249 fn to_socket_addrs(&self) -> io::Result<Self::Iter>;
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())
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())
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 })
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;
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()),
286 resolve_socket_addr(host, port)
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()),
300 macro_rules! try_opt {
301 ($e:expr, $msg:expr) => (
304 None => return Err(io::Error::new(io::ErrorKind::InvalidInput,
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)
319 impl<'a, T: ToSocketAddrs + ?Sized> ToSocketAddrs for &'a T {
321 fn to_socket_addrs(&self) -> io::Result<T::Iter> {
322 (**self).to_socket_addrs()
331 use net::Ipv6MulticastScope::*;
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());
340 let none: Option<IpAddr> = "256.0.0.1".parse().ok();
341 assert_eq!(None, none);
343 let none: Option<IpAddr> = "255.0.0".parse().ok();
344 assert_eq!(None, none);
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);
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());
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());
361 assert_eq!(Ok(Ipv6Addr::new(0x2a02, 0x6b8, 0, 0, 0, 0, 0x11, 0x11)),
362 "2a02:6b8::11:11".parse());
365 let none: Option<IpAddr> = "::00000".parse().ok();
366 assert_eq!(None, none);
368 let none: Option<IpAddr> = "1:2:3:4:5:6:7".parse().ok();
369 assert_eq!(None, none);
371 let none: Option<IpAddr> = "1:2:3:4:5:6:7:8:9".parse().ok();
372 assert_eq!(None, none);
374 let none: Option<IpAddr> = "1:2:::6:7:8".parse().ok();
375 assert_eq!(None, none);
377 let none: Option<IpAddr> = "1:2::6::8".parse().ok();
378 assert_eq!(None, none);
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());
393 let none: Option<IpAddr> = "::127.0.0.1:".parse().ok();
394 assert_eq!(None, none);
396 let none: Option<IpAddr> = "1.2.3.4.5:127.0.0.1".parse().ok();
397 assert_eq!(None, none);
399 let none: Option<IpAddr> = "1.2.3.4.5:6:7:127.0.0.1".parse().ok();
400 assert_eq!(None, none);
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());
413 let none: Option<SocketAddr> = "127.0.0.1".parse().ok();
414 assert_eq!(None, none);
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);
422 let none: Option<SocketAddr> = "127.0.0.1:123456".parse().ok();
423 assert_eq!(None, none);
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");
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");
436 // v6 address with no zero segments
437 assert_eq!(Ipv6Addr::new(8, 9, 10, 11, 12, 13, 14, 15).to_string(),
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());
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());
449 assert_eq!("::", Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0).to_string());
452 assert_eq!("::1", Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_string());
455 assert_eq!("1::", Ipv6Addr::new(1, 0, 0, 0, 0, 0, 0, 0).to_string());
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());
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());
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());
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(),
483 fn ipv4_properties() {
484 fn check(octets: &[u8; 4], unspec: bool, loopback: bool,
485 private: bool, link_local: bool, global: bool,
487 let ip = Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3]);
488 assert_eq!(octets, &ip.octets());
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);
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);
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());
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());
532 // unspec loopbk uniqlo global unill unisl uniglo mscope
534 true, false, false, true, false, false, true, None);
536 false, true, false, false, false, false, false, None);
538 false, false, false, true, false, false, true, None);
540 false, false, false, true, false, false, true, None);
542 false, false, true, false, false, false, false, None);
544 false, false, true, false, false, false, false, None);
546 false, false, false, false, true, false, false, None);
548 false, false, false, false, true, false, false, None);
550 false, false, false, false, false, true, false, None);
552 false, false, false, false, false, false, false, Some(InterfaceLocal));
554 false, false, false, false, false, false, false, Some(LinkLocal));
556 false, false, false, false, false, false, false, Some(RealmLocal));
558 false, false, false, false, false, false, false, Some(AdminLocal));
560 false, false, false, false, false, false, false, Some(SiteLocal));
562 false, false, false, false, false, false, false, Some(OrganizationLocal));
564 false, false, false, true, false, false, false, Some(Global));
567 fn tsa<A: ToSocketAddrs>(a: A) -> io::Result<Vec<SocketAddr>> {
568 Ok(try!(a.to_socket_addrs()).collect())
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));
578 fn to_socket_addr_ipaddr_u16() {
579 let a = IpAddr::new_v4(77, 88, 21, 11);
581 let e = SocketAddr::new(a, p);
582 assert_eq!(Ok(vec![e]), tsa((a, p)));
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)));
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)));
593 let a = SocketAddr::new(IpAddr::new_v4(127, 0, 0, 1), 23924);
594 assert!(tsa(("localhost", 23924u16)).unwrap().contains(&a));
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"));
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"));
605 let a = SocketAddr::new(IpAddr::new_v4(127, 0, 0, 1), 23924);
606 assert!(tsa("localhost:23924").unwrap().contains(&a));