1 //! Networking primitives for TCP/UDP communication.
3 //! This module provides networking functionality for the Transmission Control and User
4 //! Datagram Protocols, as well as types for IP and socket addresses.
8 //! * [`TcpListener`] and [`TcpStream`] provide functionality for communication over TCP
9 //! * [`UdpSocket`] provides functionality for communication over UDP
10 //! * [`IpAddr`] represents IP addresses of either IPv4 or IPv6; [`Ipv4Addr`] and
11 //! [`Ipv6Addr`] are respectively IPv4 and IPv6 addresses
12 //! * [`SocketAddr`] represents socket addresses of either IPv4 or IPv6; [`SocketAddrV4`]
13 //! and [`SocketAddrV6`] are respectively IPv4 and IPv6 socket addresses
14 //! * [`ToSocketAddrs`] is a trait that used for generic address resolution when interacting
15 //! with networking objects like [`TcpListener`], [`TcpStream`] or [`UdpSocket`]
16 //! * Other types are return or parameter types for various methods in this module
18 //! [`IpAddr`]: ../../std/net/enum.IpAddr.html
19 //! [`Ipv4Addr`]: ../../std/net/struct.Ipv4Addr.html
20 //! [`Ipv6Addr`]: ../../std/net/struct.Ipv6Addr.html
21 //! [`SocketAddr`]: ../../std/net/enum.SocketAddr.html
22 //! [`SocketAddrV4`]: ../../std/net/struct.SocketAddrV4.html
23 //! [`SocketAddrV6`]: ../../std/net/struct.SocketAddrV6.html
24 //! [`TcpListener`]: ../../std/net/struct.TcpListener.html
25 //! [`TcpStream`]: ../../std/net/struct.TcpStream.html
26 //! [`ToSocketAddrs`]: ../../std/net/trait.ToSocketAddrs.html
27 //! [`UdpSocket`]: ../../std/net/struct.UdpSocket.html
29 #![stable(feature = "rust1", since = "1.0.0")]
31 use crate::io::{self, Error, ErrorKind};
33 #[stable(feature = "rust1", since = "1.0.0")]
34 pub use self::ip::{IpAddr, Ipv4Addr, Ipv6Addr, Ipv6MulticastScope};
35 #[stable(feature = "rust1", since = "1.0.0")]
36 pub use self::addr::{SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs};
37 #[stable(feature = "rust1", since = "1.0.0")]
38 pub use self::tcp::{TcpStream, TcpListener, Incoming};
39 #[stable(feature = "rust1", since = "1.0.0")]
40 pub use self::udp::UdpSocket;
41 #[stable(feature = "rust1", since = "1.0.0")]
42 pub use self::parser::AddrParseError;
52 /// Possible values which can be passed to the [`shutdown`] method of
55 /// [`shutdown`]: struct.TcpStream.html#method.shutdown
56 /// [`TcpStream`]: struct.TcpStream.html
57 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
58 #[stable(feature = "rust1", since = "1.0.0")]
60 /// The reading portion of the [`TcpStream`] should be shut down.
62 /// All currently blocked and future [reads] will return [`Ok(0)`].
64 /// [`TcpStream`]: ../../std/net/struct.TcpStream.html
65 /// [reads]: ../../std/io/trait.Read.html
66 /// [`Ok(0)`]: ../../std/result/enum.Result.html#variant.Ok
67 #[stable(feature = "rust1", since = "1.0.0")]
69 /// The writing portion of the [`TcpStream`] should be shut down.
71 /// All currently blocked and future [writes] will return an error.
73 /// [`TcpStream`]: ../../std/net/struct.TcpStream.html
74 /// [writes]: ../../std/io/trait.Write.html
75 #[stable(feature = "rust1", since = "1.0.0")]
77 /// Both the reading and the writing portions of the [`TcpStream`] should be shut down.
79 /// See [`Shutdown::Read`] and [`Shutdown::Write`] for more information.
81 /// [`TcpStream`]: ../../std/net/struct.TcpStream.html
82 /// [`Shutdown::Read`]: #variant.Read
83 /// [`Shutdown::Write`]: #variant.Write
84 #[stable(feature = "rust1", since = "1.0.0")]
90 fn from_be(i: Self) -> Self;
91 fn to_be(&self) -> Self;
94 ($($t:ident)*) => ($(impl NetInt for $t {
95 fn from_be(i: Self) -> Self { <$t>::from_be(i) }
96 fn to_be(&self) -> Self { <$t>::to_be(*self) }
99 doit! { i8 i16 i32 i64 isize u8 u16 u32 u64 usize }
101 fn hton<I: NetInt>(i: I) -> I { i.to_be() }
102 fn ntoh<I: NetInt>(i: I) -> I { I::from_be(i) }
104 fn each_addr<A: ToSocketAddrs, F, T>(addr: A, mut f: F) -> io::Result<T>
105 where F: FnMut(io::Result<&SocketAddr>) -> io::Result<T>
107 let addrs = match addr.to_socket_addrs() {
109 Err(e) => return f(Err(e))
111 let mut last_err = None;
114 Ok(l) => return Ok(l),
115 Err(e) => last_err = Some(e),
118 Err(last_err.unwrap_or_else(|| {
119 Error::new(ErrorKind::InvalidInput,
120 "could not resolve to any addresses")