]> git.lizzy.rs Git - rust.git/blob - src/libstd/net/mod.rs
Auto merge of #67731 - matthewjasper:drop-in-place-reclimit, r=eddyb
[rust.git] / src / libstd / net / mod.rs
1 //! Networking primitives for TCP/UDP communication.
2 //!
3 //! This module provides networking functionality for the Transmission Control and User
4 //! Datagram Protocols, as well as types for IP and socket addresses.
5 //!
6 //! # Organization
7 //!
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
17 //!
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
28
29 #![stable(feature = "rust1", since = "1.0.0")]
30
31 use crate::io::{self, Error, ErrorKind};
32
33 #[stable(feature = "rust1", since = "1.0.0")]
34 pub use self::addr::{SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs};
35 #[stable(feature = "rust1", since = "1.0.0")]
36 pub use self::ip::{IpAddr, Ipv4Addr, Ipv6Addr, Ipv6MulticastScope};
37 #[stable(feature = "rust1", since = "1.0.0")]
38 pub use self::parser::AddrParseError;
39 #[stable(feature = "rust1", since = "1.0.0")]
40 pub use self::tcp::{Incoming, TcpListener, TcpStream};
41 #[stable(feature = "rust1", since = "1.0.0")]
42 pub use self::udp::UdpSocket;
43
44 mod addr;
45 mod ip;
46 mod parser;
47 mod tcp;
48 #[cfg(test)]
49 mod test;
50 mod udp;
51
52 /// Possible values which can be passed to the [`shutdown`] method of
53 /// [`TcpStream`].
54 ///
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")]
59 pub enum Shutdown {
60     /// The reading portion of the [`TcpStream`] should be shut down.
61     ///
62     /// All currently blocked and future [reads] will return [`Ok(0)`].
63     ///
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")]
68     Read,
69     /// The writing portion of the [`TcpStream`] should be shut down.
70     ///
71     /// All currently blocked and future [writes] will return an error.
72     ///
73     /// [`TcpStream`]: ../../std/net/struct.TcpStream.html
74     /// [writes]: ../../std/io/trait.Write.html
75     #[stable(feature = "rust1", since = "1.0.0")]
76     Write,
77     /// Both the reading and the writing portions of the [`TcpStream`] should be shut down.
78     ///
79     /// See [`Shutdown::Read`] and [`Shutdown::Write`] for more information.
80     ///
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")]
85     Both,
86 }
87
88 #[inline]
89 const fn htons(i: u16) -> u16 {
90     i.to_be()
91 }
92 #[inline]
93 const fn ntohs(i: u16) -> u16 {
94     u16::from_be(i)
95 }
96
97 fn each_addr<A: ToSocketAddrs, F, T>(addr: A, mut f: F) -> io::Result<T>
98 where
99     F: FnMut(io::Result<&SocketAddr>) -> io::Result<T>,
100 {
101     let addrs = match addr.to_socket_addrs() {
102         Ok(addrs) => addrs,
103         Err(e) => return f(Err(e)),
104     };
105     let mut last_err = None;
106     for addr in addrs {
107         match f(Ok(&addr)) {
108             Ok(l) => return Ok(l),
109             Err(e) => last_err = Some(e),
110         }
111     }
112     Err(last_err.unwrap_or_else(|| {
113         Error::new(ErrorKind::InvalidInput, "could not resolve to any addresses")
114     }))
115 }