Skip to main content

tor_rtcompat/
network.rs

1//! Definitions for types used with [`NetStreamProvider`](crate::NetStreamProvider).
2
3/// Options to use when initializing a listening socket.
4///
5/// This may include both options that affect the listening,
6/// and options that will apply to any individual accepted connection streams.
7///
8/// It can include options set with `setsockopt`,
9/// as well as options that influence higher layers (eg, the runtime).
10///
11/// For established streams that are accepted from a listener,
12/// you can use [`StreamOps`](crate::StreamOps) to perform additional operations
13/// or to configure additional options.
14#[derive(Copy, Clone, Debug, PartialEq, Eq, derive_builder::Builder, amplify::Getters)]
15#[non_exhaustive]
16pub struct CommonListenOptions {
17    /// Value set for `SO_SNDBUF` on the listening socket.
18    #[builder(default)]
19    pub(crate) send_buffer_size: Option<usize>,
20
21    /// Value set for `SO_RCVBUF` on the listening socket.
22    #[builder(default)]
23    pub(crate) recv_buffer_size: Option<usize>,
24}
25
26impl CommonListenOptions {
27    /// Returns a builder for this [`CommonListenOptions`].
28    pub fn builder() -> CommonListenOptionsBuilder {
29        Default::default()
30    }
31}
32
33// We want to make sure that the defaults are set to the defaults that the builder uses.
34#[allow(clippy::derivable_impls)]
35impl Default for CommonListenOptions {
36    fn default() -> Self {
37        // This needs to match the result of `Self::builder().build().unwrap()`,
38        // which is tested by the `builder_defaults()` test below.
39        Self {
40            send_buffer_size: None,
41            recv_buffer_size: None,
42        }
43    }
44}
45
46/// Options to use when initializing a TCP listening socket.
47///
48/// See [`CommonListenOptions`] for more information.
49#[derive(Copy, Clone, Debug, PartialEq, Eq, derive_builder::Builder, amplify::Getters)]
50#[non_exhaustive]
51pub struct TcpListenOptions {
52    /// Options that are common for all socket types.
53    #[builder(sub_builder)]
54    pub(crate) common: CommonListenOptions,
55}
56
57impl TcpListenOptions {
58    /// Returns a builder for this [`TcpListenOptions`].
59    pub fn builder() -> TcpListenOptionsBuilder {
60        Default::default()
61    }
62}
63
64// We want to make sure that the defaults are set to the defaults that the builder uses.
65#[allow(clippy::derivable_impls)]
66impl Default for TcpListenOptions {
67    fn default() -> Self {
68        // This needs to match the result of `Self::builder().build().unwrap()`,
69        // which is tested by the `builder_defaults()` test below.
70        Self {
71            common: CommonListenOptions::default(),
72        }
73    }
74}
75
76/// Options to use when initializing a unix stream listening socket.
77// TODO: We should support at least the options in `CommonListenOptions`.
78#[derive(Copy, Clone, Debug, PartialEq, Eq, derive_builder::Builder, amplify::Getters)]
79#[non_exhaustive]
80pub struct UnixListenOptions {}
81
82impl UnixListenOptions {
83    /// Returns a builder for this [`UnixListenOptions`].
84    pub fn builder() -> UnixListenOptionsBuilder {
85        Default::default()
86    }
87}
88
89// We want to make sure that the defaults are set to the defaults that the builder uses.
90#[allow(clippy::derivable_impls)]
91impl Default for UnixListenOptions {
92    fn default() -> Self {
93        // This needs to match the result of `Self::builder().build().unwrap()`,
94        // which is tested by the `builder_defaults()` test below.
95        Self {}
96    }
97}
98
99/// Options to use when connecting a socket.
100///
101/// This may include both options that affect the connection attempt,
102/// and options that will apply to the resulting connection stream.
103///
104/// It can include options set with `setsockopt`,
105/// as well as options that influence higher layers (eg, the runtime).
106///
107/// For established streams,
108/// you can use [`StreamOps`](crate::StreamOps) to perform additional operations
109/// or to configure additional options.
110#[derive(Copy, Clone, Debug, PartialEq, Eq, derive_builder::Builder, amplify::Getters)]
111#[non_exhaustive]
112pub struct CommonConnectOptions {
113    /// Value set for `SO_SNDBUF` on the socket.
114    #[builder(default)]
115    pub(crate) send_buffer_size: Option<usize>,
116
117    /// Value set for `SO_RCVBUF` on the socket.
118    #[builder(default)]
119    pub(crate) recv_buffer_size: Option<usize>,
120}
121
122impl CommonConnectOptions {
123    /// Returns a builder for this [`CommonConnectOptions`].
124    pub fn builder() -> CommonConnectOptionsBuilder {
125        Default::default()
126    }
127}
128
129// We want to make sure that the defaults are set to the defaults that the builder uses.
130#[allow(clippy::derivable_impls)]
131impl Default for CommonConnectOptions {
132    fn default() -> Self {
133        // This needs to match the result of `Self::builder().build().unwrap()`,
134        // which is tested by the `builder_defaults()` test below.
135        Self {
136            send_buffer_size: None,
137            recv_buffer_size: None,
138        }
139    }
140}
141
142/// Options to use when connecting a TCP socket.
143///
144/// See [`CommonConnectOptions`] for more information.
145#[derive(Copy, Clone, Debug, PartialEq, Eq, derive_builder::Builder, amplify::Getters)]
146#[non_exhaustive]
147pub struct TcpConnectOptions {
148    /// Options that are common for all socket types.
149    #[builder(sub_builder)]
150    pub(crate) common: CommonConnectOptions,
151}
152
153impl TcpConnectOptions {
154    /// Returns a builder for this [`TcpConnectOptions`].
155    pub fn builder() -> TcpConnectOptionsBuilder {
156        Default::default()
157    }
158}
159
160// We want to make sure that the defaults are set to the defaults that the builder uses.
161#[allow(clippy::derivable_impls)]
162impl Default for TcpConnectOptions {
163    fn default() -> Self {
164        // This needs to match the result of `Self::builder().build().unwrap()`,
165        // which is tested by the `builder_defaults()` test below.
166        Self {
167            common: CommonConnectOptions::default(),
168        }
169    }
170}
171
172/// Options to use when connecting a unix stream socket.
173// TODO: We should support at least the options in `CommonConnectOptions`.
174#[derive(Copy, Clone, Debug, PartialEq, Eq, derive_builder::Builder, amplify::Getters)]
175#[non_exhaustive]
176pub struct UnixConnectOptions {}
177
178impl UnixConnectOptions {
179    /// Returns a builder for this [`UnixConnectOptions`].
180    pub fn builder() -> UnixConnectOptionsBuilder {
181        Default::default()
182    }
183}
184
185// We want to make sure that the defaults are set to the defaults that the builder uses.
186#[allow(clippy::derivable_impls)]
187impl Default for UnixConnectOptions {
188    fn default() -> Self {
189        // This needs to match the result of `Self::builder().build().unwrap()`,
190        // which is tested by the `builder_defaults()` test below.
191        Self {}
192    }
193}
194
195#[cfg(test)]
196mod test {
197    // @@ begin test lint list maintained by maint/add_warning @@
198    #![allow(clippy::bool_assert_comparison)]
199    #![allow(clippy::clone_on_copy)]
200    #![allow(clippy::dbg_macro)]
201    #![allow(clippy::mixed_attributes_style)]
202    #![allow(clippy::print_stderr)]
203    #![allow(clippy::print_stdout)]
204    #![allow(clippy::single_char_pattern)]
205    #![allow(clippy::unwrap_used)]
206    #![allow(clippy::unchecked_time_subtraction)]
207    #![allow(clippy::useless_vec)]
208    #![allow(clippy::needless_pass_by_value)]
209    #![allow(clippy::string_slice)] // See arti#2571
210    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
211
212    use super::*;
213
214    #[test]
215    fn builder_defaults() {
216        // Ensure that the builder default matches the type's default.
217        macro_rules! check {
218            ($type:tt) => {
219                assert_eq!($type::builder().build().unwrap(), $type::default());
220            };
221        }
222
223        check!(CommonListenOptions);
224        check!(TcpListenOptions);
225        check!(UnixListenOptions);
226
227        check!(CommonConnectOptions);
228        check!(TcpConnectOptions);
229        check!(UnixConnectOptions);
230    }
231}