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