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.
11 //! Networking primitives for TCP/UDP communication.
13 //! This module provides networking functionality for the Transmission Control and User
14 //! Datagram Protocols, as well as types for IP and socket addresses.
18 //! * [`TcpListener`] and [`TcpStream`] provide functionality for communication over TCP
19 //! * [`UdpSocket`] provides functionality for communication over UDP
20 //! * [`IpAddr`] represents IP addresses of either IPv4 or IPv6; [`Ipv4Addr`] and
21 //! [`Ipv6Addr`] are respectively IPv4 and IPv6 addresses
22 //! * [`SocketAddr`] represents socket addresses of either IPv4 or IPv6; [`SocketAddrV4`]
23 //! and [`SocketAddrV6`] are respectively IPv4 and IPv6 socket addresses
24 //! * [`ToSocketAddrs`] is a trait that used for generic address resolution when interacting
25 //! with networking objects like [`TcpListener`], [`TcpStream`] or [`UdpSocket`]
26 //! * Other types are return or parameter types for various methods in this module
28 //! [`IpAddr`]: ../../std/net/enum.IpAddr.html
29 //! [`Ipv4Addr`]: ../../std/net/struct.Ipv4Addr.html
30 //! [`Ipv6Addr`]: ../../std/net/struct.Ipv6Addr.html
31 //! [`SocketAddr`]: ../../std/net/enum.SocketAddr.html
32 //! [`SocketAddrV4`]: ../../std/net/struct.SocketAddrV4.html
33 //! [`SocketAddrV6`]: ../../std/net/struct.SocketAddrV6.html
34 //! [`TcpListener`]: ../../std/net/struct.TcpListener.html
35 //! [`TcpStream`]: ../../std/net/struct.TcpStream.html
36 //! [`ToSocketAddrs`]: ../../std/net/trait.ToSocketAddrs.html
37 //! [`UdpSocket`]: ../../std/net/struct.UdpSocket.html
39 #![stable(feature = "rust1", since = "1.0.0")]
41 use io::{self, Error, ErrorKind};
43 #[stable(feature = "rust1", since = "1.0.0")]
44 pub use self::ip::{IpAddr, Ipv4Addr, Ipv6Addr, Ipv6MulticastScope};
45 #[stable(feature = "rust1", since = "1.0.0")]
46 pub use self::addr::{SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs};
47 #[stable(feature = "rust1", since = "1.0.0")]
48 pub use self::tcp::{TcpStream, TcpListener, Incoming};
49 #[stable(feature = "rust1", since = "1.0.0")]
50 pub use self::udp::UdpSocket;
51 #[stable(feature = "rust1", since = "1.0.0")]
52 pub use self::parser::AddrParseError;
62 /// Possible values which can be passed to the [`shutdown`] method of
65 /// [`shutdown`]: struct.TcpStream.html#method.shutdown
66 /// [`TcpStream`]: struct.TcpStream.html
67 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
68 #[stable(feature = "rust1", since = "1.0.0")]
70 /// The reading portion of the [`TcpStream`] should be shut down.
72 /// All currently blocked and future [reads] will return [`Ok(0)`].
74 /// [`TcpStream`]: ../../std/net/struct.TcpStream.html
75 /// [reads]: ../../std/io/trait.Read.html
76 /// [`Ok(0)`]: ../../std/result/enum.Result.html#variant.Ok
77 #[stable(feature = "rust1", since = "1.0.0")]
79 /// The writing portion of the [`TcpStream`] should be shut down.
81 /// All currently blocked and future [writes] will return an error.
83 /// [`TcpStream`]: ../../std/net/struct.TcpStream.html
84 /// [writes]: ../../std/io/trait.Write.html
85 #[stable(feature = "rust1", since = "1.0.0")]
87 /// Both the reading and the writing portions of the [`TcpStream`] should be shut down.
89 /// See [`Shutdown::Read`] and [`Shutdown::Write`] for more information.
91 /// [`TcpStream`]: ../../std/net/struct.TcpStream.html
92 /// [`Shutdown::Read`]: #variant.Read
93 /// [`Shutdown::Write`]: #variant.Write
94 #[stable(feature = "rust1", since = "1.0.0")]
100 fn from_be(i: Self) -> Self;
101 fn to_be(&self) -> Self;
104 ($($t:ident)*) => ($(impl NetInt for $t {
105 fn from_be(i: Self) -> Self { <$t>::from_be(i) }
106 fn to_be(&self) -> Self { <$t>::to_be(*self) }
109 doit! { i8 i16 i32 i64 isize u8 u16 u32 u64 usize }
111 fn hton<I: NetInt>(i: I) -> I { i.to_be() }
112 fn ntoh<I: NetInt>(i: I) -> I { I::from_be(i) }
114 fn each_addr<A: ToSocketAddrs, F, T>(addr: A, mut f: F) -> io::Result<T>
115 where F: FnMut(io::Result<&SocketAddr>) -> io::Result<T>
117 let addrs = match addr.to_socket_addrs() {
119 Err(e) => return f(Err(e))
121 let mut last_err = None;
124 Ok(l) => return Ok(l),
125 Err(e) => last_err = Some(e),
128 Err(last_err.unwrap_or_else(|| {
129 Error::new(ErrorKind::InvalidInput,
130 "could not resolve to any addresses")