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 is 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//! Rust disables inheritance of socket objects to child processes by default when possible. For
19//! example, through the use of the `CLOEXEC` flag in UNIX systems or the `HANDLE_FLAG_INHERIT`
20//! flag on Windows.
21
22#![stable(feature = "rust1", since = "1.0.0")]
23
24use crate::io::{self, ErrorKind};
25
26#[stable(feature = "rust1", since = "1.0.0")]
27pub use self::ip_addr::{IpAddr, Ipv4Addr, Ipv6Addr, Ipv6MulticastScope};
28#[stable(feature = "rust1", since = "1.0.0")]
29pub use self::socket_addr::{SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs};
30#[unstable(feature = "tcplistener_into_incoming", issue = "88339")]
31pub use self::tcp::IntoIncoming;
32#[stable(feature = "rust1", since = "1.0.0")]
33pub use self::tcp::{Incoming, TcpListener, TcpStream};
34#[stable(feature = "rust1", since = "1.0.0")]
35pub use self::udp::UdpSocket;
36#[stable(feature = "rust1", since = "1.0.0")]
37pub use core::net::AddrParseError;
38
39mod ip_addr;
40mod socket_addr;
41mod tcp;
42#[cfg(test)]
43pub(crate) mod test;
44mod udp;
45
46/// Possible values which can be passed to the [`TcpStream::shutdown`] method.
47#[derive(Copy, Clone, PartialEq, Eq, Debug)]
48#[stable(feature = "rust1", since = "1.0.0")]
49pub enum Shutdown {
50 /// The reading portion of the [`TcpStream`] should be shut down.
51 ///
52 /// All currently blocked and future [reads] will return <code>[Ok]\(0)</code>.
53 ///
54 /// [reads]: crate::io::Read "io::Read"
55 #[stable(feature = "rust1", since = "1.0.0")]
56 Read,
57 /// The writing portion of the [`TcpStream`] should be shut down.
58 ///
59 /// All currently blocked and future [writes] will return an error.
60 ///
61 /// [writes]: crate::io::Write "io::Write"
62 #[stable(feature = "rust1", since = "1.0.0")]
63 Write,
64 /// Both the reading and the writing portions of the [`TcpStream`] should be shut down.
65 ///
66 /// See [`Shutdown::Read`] and [`Shutdown::Write`] for more information.
67 #[stable(feature = "rust1", since = "1.0.0")]
68 Both,
69}
70
71fn each_addr<A: ToSocketAddrs, F, T>(addr: A, mut f: F) -> io::Result<T>
72where
73 F: FnMut(io::Result<&SocketAddr>) -> io::Result<T>,
74{
75 let addrs: ::Iter = match addr.to_socket_addrs() {
76 Ok(addrs: ::Iter) => addrs,
77 Err(e: Error) => return f(Err(e)),
78 };
79 let mut last_err: Option = None;
80 for addr: SocketAddr in addrs {
81 match f(Ok(&addr)) {
82 Ok(l: T) => return Ok(l),
83 Err(e: Error) => last_err = Some(e),
84 }
85 }
86 Err(last_err.unwrap_or_else(|| {
87 io::const_io_error!(ErrorKind::InvalidInput, "could not resolve to any addresses")
88 }))
89}
90