]>
Commit | Line | Data |
---|---|---|
85aaf69f SL |
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 | ||
bd371182 | 11 | //! Networking primitives for TCP/UDP communication. |
85aaf69f | 12 | |
c34b1796 | 13 | #![stable(feature = "rust1", since = "1.0.0")] |
85aaf69f | 14 | |
85aaf69f | 15 | use io::{self, Error, ErrorKind}; |
d9579d0f | 16 | use sys_common::net as net_imp; |
85aaf69f | 17 | |
92a42be0 | 18 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f | 19 | pub use self::ip::{IpAddr, Ipv4Addr, Ipv6Addr, Ipv6MulticastScope}; |
92a42be0 | 20 | #[stable(feature = "rust1", since = "1.0.0")] |
c34b1796 | 21 | pub use self::addr::{SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs}; |
92a42be0 | 22 | #[stable(feature = "rust1", since = "1.0.0")] |
d9579d0f | 23 | pub use self::tcp::{TcpStream, TcpListener, Incoming}; |
92a42be0 | 24 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f | 25 | pub use self::udp::UdpSocket; |
92a42be0 | 26 | #[stable(feature = "rust1", since = "1.0.0")] |
c34b1796 | 27 | pub use self::parser::AddrParseError; |
85aaf69f SL |
28 | |
29 | mod ip; | |
30 | mod addr; | |
31 | mod tcp; | |
32 | mod udp; | |
33 | mod parser; | |
c30ab7b3 SL |
34 | #[cfg(test)] |
35 | mod test; | |
85aaf69f | 36 | |
5bcae85e SL |
37 | /// Possible values which can be passed to the [`shutdown`] method of |
38 | /// [`TcpStream`]. | |
39 | /// | |
40 | /// [`shutdown`]: struct.TcpStream.html#method.shutdown | |
41 | /// [`TcpStream`]: struct.TcpStream.html | |
9e0c209e | 42 | #[derive(Copy, Clone, PartialEq, Eq, Debug)] |
c34b1796 | 43 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f SL |
44 | pub enum Shutdown { |
45 | /// Indicates that the reading portion of this stream/socket should be shut | |
46 | /// down. All currently blocked and future reads will return `Ok(0)`. | |
c34b1796 | 47 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f SL |
48 | Read, |
49 | /// Indicates that the writing portion of this stream/socket should be shut | |
50 | /// down. All currently blocked and future writes will return an error. | |
c34b1796 | 51 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f SL |
52 | Write, |
53 | /// Shut down both the reading and writing portions of this stream. | |
54 | /// | |
55 | /// See `Shutdown::Read` and `Shutdown::Write` for more information. | |
c34b1796 AL |
56 | #[stable(feature = "rust1", since = "1.0.0")] |
57 | Both, | |
85aaf69f SL |
58 | } |
59 | ||
9346a6ac AL |
60 | #[doc(hidden)] |
61 | trait NetInt { | |
62 | fn from_be(i: Self) -> Self; | |
63 | fn to_be(&self) -> Self; | |
64 | } | |
65 | macro_rules! doit { | |
66 | ($($t:ident)*) => ($(impl NetInt for $t { | |
67 | fn from_be(i: Self) -> Self { <$t>::from_be(i) } | |
68 | fn to_be(&self) -> Self { <$t>::to_be(*self) } | |
69 | })*) | |
70 | } | |
71 | doit! { i8 i16 i32 i64 isize u8 u16 u32 u64 usize } | |
72 | ||
73 | fn hton<I: NetInt>(i: I) -> I { i.to_be() } | |
74 | fn ntoh<I: NetInt>(i: I) -> I { I::from_be(i) } | |
85aaf69f | 75 | |
c34b1796 | 76 | fn each_addr<A: ToSocketAddrs, F, T>(addr: A, mut f: F) -> io::Result<T> |
85aaf69f SL |
77 | where F: FnMut(&SocketAddr) -> io::Result<T> |
78 | { | |
79 | let mut last_err = None; | |
54a0048b | 80 | for addr in addr.to_socket_addrs()? { |
85aaf69f SL |
81 | match f(&addr) { |
82 | Ok(l) => return Ok(l), | |
83 | Err(e) => last_err = Some(e), | |
84 | } | |
85 | } | |
86 | Err(last_err.unwrap_or_else(|| { | |
87 | Error::new(ErrorKind::InvalidInput, | |
c34b1796 | 88 | "could not resolve to any addresses") |
85aaf69f SL |
89 | })) |
90 | } | |
91 | ||
92 | /// An iterator over `SocketAddr` values returned from a host lookup operation. | |
c34b1796 AL |
93 | #[unstable(feature = "lookup_host", reason = "unsure about the returned \ |
94 | iterator and returning socket \ | |
e9174d1e SL |
95 | addresses", |
96 | issue = "27705")] | |
85aaf69f SL |
97 | pub struct LookupHost(net_imp::LookupHost); |
98 | ||
c34b1796 AL |
99 | #[unstable(feature = "lookup_host", reason = "unsure about the returned \ |
100 | iterator and returning socket \ | |
e9174d1e SL |
101 | addresses", |
102 | issue = "27705")] | |
85aaf69f | 103 | impl Iterator for LookupHost { |
3157f602 XL |
104 | type Item = SocketAddr; |
105 | fn next(&mut self) -> Option<SocketAddr> { self.0.next() } | |
85aaf69f SL |
106 | } |
107 | ||
108 | /// Resolve the host specified by `host` as a number of `SocketAddr` instances. | |
109 | /// | |
110 | /// This method may perform a DNS query to resolve `host` and may also inspect | |
111 | /// system configuration to resolve the specified hostname. | |
112 | /// | |
3157f602 XL |
113 | /// The returned iterator will skip over any unknown addresses returned by the |
114 | /// operating system. | |
115 | /// | |
c34b1796 | 116 | /// # Examples |
85aaf69f SL |
117 | /// |
118 | /// ```no_run | |
c1a9b12d SL |
119 | /// #![feature(lookup_host)] |
120 | /// | |
85aaf69f SL |
121 | /// use std::net; |
122 | /// | |
123 | /// # fn foo() -> std::io::Result<()> { | |
124 | /// for host in try!(net::lookup_host("rust-lang.org")) { | |
3157f602 | 125 | /// println!("found address: {}", host); |
85aaf69f SL |
126 | /// } |
127 | /// # Ok(()) | |
128 | /// # } | |
129 | /// ``` | |
c34b1796 AL |
130 | #[unstable(feature = "lookup_host", reason = "unsure about the returned \ |
131 | iterator and returning socket \ | |
e9174d1e SL |
132 | addresses", |
133 | issue = "27705")] | |
85aaf69f SL |
134 | pub fn lookup_host(host: &str) -> io::Result<LookupHost> { |
135 | net_imp::lookup_host(host).map(LookupHost) | |
136 | } |