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#[stable(feature = "tcp_quickack", since = "1.89.0")]
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 /// use std::net::TcpStream;
27 /// #[cfg(target_os = "linux")]
28 /// use std::os::linux::net::TcpStreamExt;
29 /// #[cfg(target_os = "android")]
30 /// use std::os::android::net::TcpStreamExt;
31 ///
32 /// let stream = TcpStream::connect("127.0.0.1:8080")
33 /// .expect("Couldn't connect to the server...");
34 /// stream.set_quickack(true).expect("set_quickack call failed");
35 /// ```
36 #[stable(feature = "tcp_quickack", since = "1.89.0")]
37 fn set_quickack(&self, quickack: bool) -> io::Result<()>;
38
39 /// Gets the value of the `TCP_QUICKACK` option on this socket.
40 ///
41 /// For more information about this option, see [`TcpStreamExt::set_quickack`].
42 ///
43 /// # Examples
44 ///
45 /// ```no_run
46 /// use std::net::TcpStream;
47 /// #[cfg(target_os = "linux")]
48 /// use std::os::linux::net::TcpStreamExt;
49 /// #[cfg(target_os = "android")]
50 /// use std::os::android::net::TcpStreamExt;
51 ///
52 /// let stream = TcpStream::connect("127.0.0.1:8080")
53 /// .expect("Couldn't connect to the server...");
54 /// stream.set_quickack(true).expect("set_quickack call failed");
55 /// assert_eq!(stream.quickack().unwrap_or(false), true);
56 /// ```
57 #[stable(feature = "tcp_quickack", since = "1.89.0")]
58 fn quickack(&self) -> io::Result<bool>;
59
60 /// A socket listener will be awakened solely when data arrives.
61 ///
62 /// The `accept` argument set the delay in seconds until the
63 /// data is available to read, reducing the number of short lived
64 /// connections without data to process.
65 /// Contrary to other platforms `SO_ACCEPTFILTER` feature equivalent, there is
66 /// no necessity to set it after the `listen` call.
67 ///
68 /// See [`man 7 tcp`](https://man7.org/linux/man-pages/man7/tcp.7.html)
69 ///
70 /// # Examples
71 ///
72 /// ```no run
73 /// #![feature(tcp_deferaccept)]
74 /// use std::net::TcpStream;
75 /// use std::os::linux::net::TcpStreamExt;
76 ///
77 /// let stream = TcpStream::connect("127.0.0.1:8080")
78 /// .expect("Couldn't connect to the server...");
79 /// stream.set_deferaccept(1).expect("set_deferaccept call failed");
80 /// ```
81 #[unstable(feature = "tcp_deferaccept", issue = "119639")]
82 #[cfg(target_os = "linux")]
83 fn set_deferaccept(&self, accept: u32) -> io::Result<()>;
84
85 /// Gets the accept delay value (in seconds) of the `TCP_DEFER_ACCEPT` option.
86 ///
87 /// For more information about this option, see [`TcpStreamExt::set_deferaccept`].
88 ///
89 /// # Examples
90 ///
91 /// ```no_run
92 /// #![feature(tcp_deferaccept)]
93 /// use std::net::TcpStream;
94 /// use std::os::linux::net::TcpStreamExt;
95 ///
96 /// let stream = TcpStream::connect("127.0.0.1:8080")
97 /// .expect("Couldn't connect to the server...");
98 /// stream.set_deferaccept(1).expect("set_deferaccept call failed");
99 /// assert_eq!(stream.deferaccept().unwrap_or(0), 1);
100 /// ```
101 #[unstable(feature = "tcp_deferaccept", issue = "119639")]
102 #[cfg(target_os = "linux")]
103 fn deferaccept(&self) -> io::Result<u32>;
104}
105
106#[stable(feature = "tcp_quickack", since = "1.89.0")]
107impl Sealed for net::TcpStream {}
108
109#[stable(feature = "tcp_quickack", since = "1.89.0")]
110impl TcpStreamExt for net::TcpStream {
111 fn set_quickack(&self, quickack: bool) -> io::Result<()> {
112 self.as_inner().as_inner().set_quickack(quickack)
113 }
114
115 fn quickack(&self) -> io::Result<bool> {
116 self.as_inner().as_inner().quickack()
117 }
118
119 #[cfg(target_os = "linux")]
120 fn set_deferaccept(&self, accept: u32) -> io::Result<()> {
121 self.as_inner().as_inner().set_deferaccept(accept)
122 }
123
124 #[cfg(target_os = "linux")]
125 fn deferaccept(&self) -> io::Result<u32> {
126 self.as_inner().as_inner().deferaccept()
127 }
128}