std/os/net/linux_ext/tcp.rs
1//! Linux and Android-specific tcp extensions to primitives in the [`std::net`] module.
2//!
3//! [`std::net`]: crate::net
4
5use crate::sealed::Sealed;
6use crate::sys_common::AsInner;
7use crate::{io, net};
8
9/// Os-specific extensions for [`TcpStream`]
10///
11/// [`TcpStream`]: net::TcpStream
12#[unstable(feature = "tcp_quickack", issue = "96256")]
13pub trait TcpStreamExt: Sealed {
14 /// Enable or disable `TCP_QUICKACK`.
15 ///
16 /// This flag causes Linux to eagerly send ACKs rather than delaying them.
17 /// Linux may reset this flag after further operations on the socket.
18 ///
19 /// See [`man 7 tcp`](https://man7.org/linux/man-pages/man7/tcp.7.html) and
20 /// [TCP delayed acknowledgement](https://en.wikipedia.org/wiki/TCP_delayed_acknowledgment)
21 /// for more information.
22 ///
23 /// # Examples
24 ///
25 /// ```no_run
26 /// #![feature(tcp_quickack)]
27 /// use std::net::TcpStream;
28 /// use std::os::linux::net::TcpStreamExt;
29 ///
30 /// let stream = TcpStream::connect("127.0.0.1:8080")
31 /// .expect("Couldn't connect to the server...");
32 /// stream.set_quickack(true).expect("set_quickack call failed");
33 /// ```
34 #[unstable(feature = "tcp_quickack", issue = "96256")]
35 fn set_quickack(&self, quickack: bool) -> io::Result<()>;
36
37 /// Gets the value of the `TCP_QUICKACK` option on this socket.
38 ///
39 /// For more information about this option, see [`TcpStreamExt::set_quickack`].
40 ///
41 /// # Examples
42 ///
43 /// ```no_run
44 /// #![feature(tcp_quickack)]
45 /// use std::net::TcpStream;
46 /// use std::os::linux::net::TcpStreamExt;
47 ///
48 /// let stream = TcpStream::connect("127.0.0.1:8080")
49 /// .expect("Couldn't connect to the server...");
50 /// stream.set_quickack(true).expect("set_quickack call failed");
51 /// assert_eq!(stream.quickack().unwrap_or(false), true);
52 /// ```
53 #[unstable(feature = "tcp_quickack", issue = "96256")]
54 fn quickack(&self) -> io::Result<bool>;
55
56 /// A socket listener will be awakened solely when data arrives.
57 ///
58 /// The `accept` argument set the delay in seconds until the
59 /// data is available to read, reducing the number of short lived
60 /// connections without data to process.
61 /// Contrary to other platforms `SO_ACCEPTFILTER` feature equivalent, there is
62 /// no necessity to set it after the `listen` call.
63 ///
64 /// See [`man 7 tcp`](https://man7.org/linux/man-pages/man7/tcp.7.html)
65 ///
66 /// # Examples
67 ///
68 /// ```no run
69 /// #![feature(tcp_deferaccept)]
70 /// use std::net::TcpStream;
71 /// use std::os::linux::net::TcpStreamExt;
72 ///
73 /// let stream = TcpStream::connect("127.0.0.1:8080")
74 /// .expect("Couldn't connect to the server...");
75 /// stream.set_deferaccept(1).expect("set_deferaccept call failed");
76 /// ```
77 #[unstable(feature = "tcp_deferaccept", issue = "119639")]
78 #[cfg(target_os = "linux")]
79 fn set_deferaccept(&self, accept: u32) -> io::Result<()>;
80
81 /// Gets the accept delay value (in seconds) of the `TCP_DEFER_ACCEPT` option.
82 ///
83 /// For more information about this option, see [`TcpStreamExt::set_deferaccept`].
84 ///
85 /// # Examples
86 ///
87 /// ```no_run
88 /// #![feature(tcp_deferaccept)]
89 /// use std::net::TcpStream;
90 /// use std::os::linux::net::TcpStreamExt;
91 ///
92 /// let stream = TcpStream::connect("127.0.0.1:8080")
93 /// .expect("Couldn't connect to the server...");
94 /// stream.set_deferaccept(1).expect("set_deferaccept call failed");
95 /// assert_eq!(stream.deferaccept().unwrap_or(0), 1);
96 /// ```
97 #[unstable(feature = "tcp_deferaccept", issue = "119639")]
98 #[cfg(target_os = "linux")]
99 fn deferaccept(&self) -> io::Result<u32>;
100}
101
102#[unstable(feature = "tcp_quickack", issue = "96256")]
103impl Sealed for net::TcpStream {}
104
105#[unstable(feature = "tcp_quickack", issue = "96256")]
106impl TcpStreamExt for net::TcpStream {
107 fn set_quickack(&self, quickack: bool) -> io::Result<()> {
108 self.as_inner().as_inner().set_quickack(quickack)
109 }
110
111 fn quickack(&self) -> io::Result<bool> {
112 self.as_inner().as_inner().quickack()
113 }
114
115 #[cfg(target_os = "linux")]
116 fn set_deferaccept(&self, accept: u32) -> io::Result<()> {
117 self.as_inner().as_inner().set_deferaccept(accept)
118 }
119
120 #[cfg(target_os = "linux")]
121 fn deferaccept(&self) -> io::Result<u32> {
122 self.as_inner().as_inner().deferaccept()
123 }
124}