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