tor_proto/stream/params.rs
1//! Declares a type to configure new streams.
2
3use tor_cell::relaycell::msg::{BeginFlags, IpVersionPreference};
4
5/// A set of preferences used to declare how a new stream should be opened.
6#[derive(Clone, Debug, Default)]
7pub struct StreamParameters {
8 /// Preferred IP version to use.
9 ip_version: IpVersionPreference,
10 /// True if we are requesting an optimistic stream.
11 optimistic: bool,
12 /// True if we are suppressing hostnames
13 suppress_hostname: bool,
14 /// True if we are suppressing flags.
15 suppress_begin_flags: bool,
16}
17
18impl StreamParameters {
19 /// Create a new [`StreamParameters`] using default settings.
20 pub fn new() -> Self {
21 Self::default()
22 }
23
24 /// Configure this `StreamParameters` to include no flags in any
25 /// `BEGIN` message it generates.
26 ///
27 /// This is used to implement onion services. It may make other
28 /// operations inoperable.
29 pub fn suppress_begin_flags(&mut self) -> &mut Self {
30 self.suppress_begin_flags = true;
31 self
32 }
33
34 /// Configure this `StreamParameters` to suppress hostnames in the
35 /// BEGIN messages.
36 ///
37 /// This is used to implement onion services. It will make other
38 /// kinds of connections not work.
39 pub fn suppress_hostname(&mut self) -> &mut Self {
40 self.suppress_hostname = true;
41 self
42 }
43
44 /// Configure which IP version (IPv4 or IPv6) you'd like to request,
45 /// if you're connecting to a hostname.
46 ///
47 /// The default is to allow either version, but to prefer IPv4.
48 pub fn ip_version(&mut self, preference: IpVersionPreference) -> &mut Self {
49 self.ip_version = preference;
50 self
51 }
52
53 /// Configure whether the stream should be opened "optimistically."
54 ///
55 /// By default, streams are not "optimistic". When you call
56 /// [`ClientCirc::begin_stream()`](crate::tunnel::circuit::ClientCirc::begin_stream),
57 /// the function won't give you a stream until the exit node has
58 /// confirmed that it has successfully opened a connection to your
59 /// target address. It's safer to wait in this way, but it is slower:
60 /// it takes an entire round trip to get your confirmation.
61 ///
62 /// If a stream _is_ configured to be "optimistic", then
63 /// `ClientCirc::begin_stream()` will return the stream
64 /// immediately, without waiting for an answer from the exit. You
65 /// can start sending data on the stream right away, though of
66 /// course this data will be lost if the connection is not
67 /// actually successful.
68 pub fn optimistic(&mut self, optimistic: bool) -> &mut Self {
69 self.optimistic = optimistic;
70 self
71 }
72
73 /// Crate-internal: Return true if the stream is optimistic.
74 pub(crate) fn is_optimistic(&self) -> bool {
75 self.optimistic
76 }
77
78 /// Crate-internal: Get a set of [`BeginFlags`] for this stream.
79 pub(crate) fn begin_flags(&self) -> BeginFlags {
80 if self.suppress_begin_flags {
81 0.into()
82 } else {
83 self.ip_version.into()
84 }
85 }
86
87 /// Crate-internal: Return true if we are suppressing hostnames.
88 pub(crate) fn suppressing_hostname(&self) -> bool {
89 self.suppress_hostname
90 }
91}