psocket/net/mod.rs
1
2
3use std::fmt;
4use std::io::{self, Error, ErrorKind};
5use sys_common::net as net_imp;
6
7pub use self::tcp::{TcpSocket, Incoming};
8pub use self::udp::UdpSocket;
9
10pub use self::ip::{IpAddr, Ipv4Addr, Ipv6Addr, Ipv6MulticastScope};
11pub use self::addr::{SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs};
12// pub use self::udp::UdpSocket;
13
14mod ip;
15mod addr;
16mod parser;
17mod tcp;
18mod udp;
19
20/// Possible values which can be passed to the [`shutdown`] method of
21/// [`TcpSocket`].
22///
23/// [`shutdown`]: struct.TcpSocket.html#method.shutdown
24/// [`TcpSocket`]: struct.TcpSocket.html
25#[derive(Copy, Clone, PartialEq, Eq, Debug)]
26pub enum Shutdown {
27 /// The reading portion of the [`TcpSocket`] should be shut down.
28 ///
29 /// All currently blocked and future [reads] will return [`Ok(0)`].
30 ///
31 /// [`TcpSocket`]: ../../pscoket/net/struct.TcpSocket.html
32 /// [reads]: ../../std/io/trait.Read.html
33 /// [`Ok(0)`]: ../../std/result/enum.Result.html#variant.Ok
34 Read,
35 /// The writing portion of the [`TcpSocket`] should be shut down.
36 ///
37 /// All currently blocked and future [writes] will return an error.
38 ///
39 /// [`TcpSocket`]: ../../pscoket/net/struct.TcpSocket.html
40 /// [writes]: ../../std/io/trait.Write.html
41 Write,
42 /// Both the reading and the writing portions of the [`TcpSocket`] should be shut down.
43 ///
44 /// See [`Shutdown::Read`] and [`Shutdown::Write`] for more information.
45 ///
46 /// [`TcpSocket`]: ../../pscoket/net/struct.TcpSocket.html
47 /// [`Shutdown::Read`]: #variant.Read
48 /// [`Shutdown::Write`]: #variant.Write
49 Both,
50}
51
52#[doc(hidden)]
53trait NetInt {
54 fn from_be(i: Self) -> Self;
55 fn to_be(&self) -> Self;
56}
57macro_rules! doit {
58 ($($t:ident)*) => ($(impl NetInt for $t {
59 fn from_be(i: Self) -> Self { <$t>::from_be(i) }
60 fn to_be(&self) -> Self { <$t>::to_be(*self) }
61 })*)
62}
63doit! { i8 i16 i32 i64 isize u8 u16 u32 u64 usize }
64
65fn hton<I: NetInt>(i: I) -> I { i.to_be() }
66fn ntoh<I: NetInt>(i: I) -> I { I::from_be(i) }
67
68fn each_addr<A: ToSocketAddrs, F, T>(addr: A, mut f: F) -> io::Result<T>
69 where F: FnMut(&SocketAddr) -> io::Result<T>
70{
71 let mut last_err = None;
72 for addr in addr.to_socket_addrs()? {
73 match f(&addr) {
74 Ok(l) => return Ok(l),
75 Err(e) => last_err = Some(e),
76 }
77 }
78 Err(last_err.unwrap_or_else(|| {
79 Error::new(ErrorKind::InvalidInput,
80 "could not resolve to any addresses")
81 }))
82}
83
84/// An iterator over `SocketAddr` values returned from a host lookup operation.
85pub struct LookupHost(net_imp::LookupHost);
86
87impl Iterator for LookupHost {
88 type Item = SocketAddr;
89 fn next(&mut self) -> Option<SocketAddr> { self.0.next() }
90}
91
92impl fmt::Debug for LookupHost {
93 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
94 f.pad("LookupHost { .. }")
95 }
96}
97
98/// Resolve the host specified by `host` as a number of `SocketAddr` instances.
99///
100/// This method may perform a DNS query to resolve `host` and may also inspect
101/// system configuration to resolve the specified hostname.
102///
103/// The returned iterator will skip over any unknown addresses returned by the
104/// operating system.
105///
106/// # Examples
107///
108/// ```no_run
109/// use psocket::net;
110///
111/// # fn foo() -> std::io::Result<()> {
112/// for host in net::lookup_host("rust-lang.org")? {
113/// println!("found address: {}", host);
114/// }
115/// # Ok(())
116/// # }
117/// ```
118pub fn lookup_host(host: &str) -> io::Result<LookupHost> {
119 net_imp::lookup_host(host).map(LookupHost)
120}
121
122#[cfg(test)]
123mod test;