]> git.lizzy.rs Git - rust.git/blob - library/std/src/net/mod.rs
2669f4dbf3068b9603f58098dc7c07efedffccb1
[rust.git] / library / std / src / 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 #![stable(feature = "rust1", since = "1.0.0")]
19
20 use crate::io::{self, Error, ErrorKind};
21
22 #[stable(feature = "rust1", since = "1.0.0")]
23 pub use self::addr::{SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs};
24 #[stable(feature = "rust1", since = "1.0.0")]
25 pub use self::ip::{IpAddr, Ipv4Addr, Ipv6Addr, Ipv6MulticastScope};
26 #[stable(feature = "rust1", since = "1.0.0")]
27 pub use self::parser::AddrParseError;
28 #[unstable(feature = "tcplistener_into_incoming", issue = "88339")]
29 pub use self::tcp::IntoIncoming;
30 #[stable(feature = "rust1", since = "1.0.0")]
31 pub use self::tcp::{Incoming, TcpListener, TcpStream};
32 #[stable(feature = "rust1", since = "1.0.0")]
33 pub use self::udp::UdpSocket;
34
35 mod addr;
36 mod ip;
37 mod parser;
38 mod tcp;
39 #[cfg(test)]
40 mod test;
41 mod udp;
42
43 /// Possible values which can be passed to the [`TcpStream::shutdown`] method.
44 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
45 #[stable(feature = "rust1", since = "1.0.0")]
46 pub enum Shutdown {
47     /// The reading portion of the [`TcpStream`] should be shut down.
48     ///
49     /// All currently blocked and future [reads] will return <code>[Ok]\(0)</code>.
50     ///
51     /// [reads]: crate::io::Read "io::Read"
52     #[stable(feature = "rust1", since = "1.0.0")]
53     Read,
54     /// The writing portion of the [`TcpStream`] should be shut down.
55     ///
56     /// All currently blocked and future [writes] will return an error.
57     ///
58     /// [writes]: crate::io::Write "io::Write"
59     #[stable(feature = "rust1", since = "1.0.0")]
60     Write,
61     /// Both the reading and the writing portions of the [`TcpStream`] should be shut down.
62     ///
63     /// See [`Shutdown::Read`] and [`Shutdown::Write`] for more information.
64     #[stable(feature = "rust1", since = "1.0.0")]
65     Both,
66 }
67
68 #[inline]
69 const fn htons(i: u16) -> u16 {
70     i.to_be()
71 }
72 #[inline]
73 const fn ntohs(i: u16) -> u16 {
74     u16::from_be(i)
75 }
76
77 fn each_addr<A: ToSocketAddrs, F, T>(addr: A, mut f: F) -> io::Result<T>
78 where
79     F: FnMut(io::Result<&SocketAddr>) -> io::Result<T>,
80 {
81     let addrs = match addr.to_socket_addrs() {
82         Ok(addrs) => addrs,
83         Err(e) => return f(Err(e)),
84     };
85     let mut last_err = None;
86     for addr in addrs {
87         match f(Ok(&addr)) {
88             Ok(l) => return Ok(l),
89             Err(e) => last_err = Some(e),
90         }
91     }
92     Err(last_err.unwrap_or_else(|| {
93         Error::new_const(ErrorKind::InvalidInput, &"could not resolve to any addresses")
94     }))
95 }