1
//! Code to handle incoming cells on a channel.
2
//!
3
//! The role of this code is to run in a separate asynchronous task,
4
//! and routes cells to the right circuits.
5
//!
6
//! TODO: I have zero confidence in the close-and-cleanup behavior here,
7
//! or in the error handling behavior.
8

            
9
use super::circmap::{CircEnt, CircMap};
10
use crate::circuit::CircuitRxSender;
11
use crate::client::circuit::halfcirc::HalfCirc;
12
use crate::client::circuit::padding::{
13
    PaddingController, PaddingEvent, PaddingEventStream, SendPadding, StartBlocking,
14
};
15
use crate::util::err::ReactorError;
16
use crate::{Error, HopNum, Result};
17
use tor_async_utils::SinkPrepareExt as _;
18
use tor_async_utils::oneshot_broadcast;
19
use tor_cell::chancell::ChanMsg;
20
use tor_cell::chancell::msg::{Destroy, DestroyReason, Padding, PaddingNegotiate};
21
use tor_cell::chancell::{AnyChanCell, CircId, msg::AnyChanMsg};
22
use tor_error::debug_report;
23
use tor_rtcompat::{DynTimeProvider, Runtime};
24

            
25
#[cfg_attr(not(target_os = "linux"), allow(unused))]
26
use tor_error::error_report;
27
#[cfg_attr(not(target_os = "linux"), allow(unused))]
28
use tor_rtcompat::StreamOps;
29

            
30
use futures::channel::mpsc;
31
use oneshot_fused_workaround as oneshot;
32

            
33
use futures::Sink;
34
use futures::StreamExt as _;
35
use futures::sink::SinkExt;
36
use futures::stream::Stream;
37
use futures::{select, select_biased};
38
use tor_error::internal;
39

            
40
use std::fmt;
41
use std::pin::Pin;
42
use std::sync::Arc;
43

            
44
use crate::channel::{ChannelDetails, CloseInfo, kist::KistParams, padding, params::*, unique_id};
45
use crate::circuit::celltypes::CreateResponse;
46
use tracing::{debug, instrument, trace};
47

            
48
#[cfg(feature = "relay")]
49
use {
50
    crate::channel::Channel,
51
    crate::circuit::celltypes::CreateRequest,
52
    crate::relay::channel::create_handler::{CreateRequestHandler, RelayCircComponents},
53
    std::sync::Weak,
54
    tor_llcrypto::pk::ed25519::Ed25519Identity,
55
    tor_llcrypto::pk::rsa::RsaIdentity,
56
};
57

            
58
/// A boxed trait object that can provide `ChanCell`s.
59
pub(super) type BoxedChannelStream =
60
    Box<dyn Stream<Item = std::result::Result<AnyChanCell, Error>> + Send + Unpin + 'static>;
61
/// A boxed trait object that can sink `ChanCell`s.
62
pub(super) type BoxedChannelSink =
63
    Box<dyn Sink<AnyChanCell, Error = Error> + Send + Unpin + 'static>;
64
/// A boxed trait object that can provide additional `StreamOps` on a `BoxedChannelStream`.
65
pub(super) type BoxedChannelStreamOps = Box<dyn StreamOps + Send + Unpin + 'static>;
66
/// The type of a oneshot channel used to inform reactor users of the result of an operation.
67
pub(super) type ReactorResultChannel<T> = oneshot::Sender<Result<T>>;
68

            
69
cfg_if::cfg_if! {
70
    if #[cfg(feature = "circ-padding")] {
71
        use crate::util::sink_blocker::{SinkBlocker, CountingPolicy};
72
        /// Type used by a channel reactor to send cells to the network.
73
        pub(super) type ChannelOutputSink = SinkBlocker<BoxedChannelSink, CountingPolicy>;
74
    } else {
75
        /// Type used by a channel reactor to send cells to the network.
76
        pub(super) type ChannelOutputSink = BoxedChannelSink;
77
    }
78
}
79

            
80
/// A message telling the channel reactor to do something.
81
#[cfg_attr(docsrs, doc(cfg(feature = "testing")))]
82
#[derive(Debug)]
83
#[allow(unreachable_pub)] // Only `pub` with feature `testing`; otherwise, visible in crate
84
#[allow(clippy::exhaustive_enums, private_interfaces)]
85
pub enum CtrlMsg {
86
    /// Shut down the reactor.
87
    Shutdown,
88
    /// Tell the reactor that a given circuit has gone away.
89
    CloseCircuit(CircId),
90
    /// Allocate a new circuit in this channel's circuit map, generating an ID for it
91
    /// and registering senders for messages received for the circuit.
92
    AllocateCircuit {
93
        /// Channel to send the circuit's `CreateResponse` down.
94
        created_sender: oneshot::Sender<CreateResponse>,
95
        /// Channel to send other messages from this circuit down.
96
        sender: CircuitRxSender,
97
        /// Oneshot channel to send the new circuit's identifiers down.
98
        tx: ReactorResultChannel<(
99
            CircId,
100
            crate::circuit::UniqId,
101
            PaddingController,
102
            PaddingEventStream,
103
        )>,
104
    },
105
    /// Enable/disable/reconfigure channel padding
106
    ///
107
    /// The sender of these messages is responsible for the optimisation of
108
    /// ensuring that "no-change" messages are elided.
109
    /// (This is implemented in `ChannelsParamsUpdatesBuilder`.)
110
    ///
111
    /// These updates are done via a control message to avoid adding additional branches to the
112
    /// main reactor `select!`.
113
    ConfigUpdate(Arc<ChannelPaddingInstructionsUpdates>),
114
    /// Enable/disable/reconfigure KIST.
115
    ///
116
    /// Like in the case of `ConfigUpdate`,
117
    /// the sender of these messages is responsible for the optimisation of
118
    /// ensuring that "no-change" messages are elided.
119
    KistConfigUpdate(KistParams),
120
    /// Change the current padding implementation to the one provided.
121
    #[cfg(feature = "circ-padding-manual")]
122
    SetChannelPadder {
123
        /// The padder to install, or None to remove any existing padder.
124
        padder: Option<crate::client::CircuitPadder>,
125
        /// A oneshot channel to use in reporting the outcome.
126
        sender: oneshot::Sender<Result<()>>,
127
    },
128
}
129

            
130
/// Object to handle incoming cells and background tasks on a channel.
131
///
132
/// This type is returned when you finish a channel; you need to spawn a
133
/// new task that calls `run()` on it.
134
#[must_use = "If you don't call run() on a reactor, the channel won't work."]
135
pub struct Reactor<R: Runtime> {
136
    /// Underlying runtime we use for generating sleep futures and telling time.
137
    pub(super) runtime: R,
138
    /// A receiver for control messages from `Channel` objects.
139
    pub(super) control: mpsc::UnboundedReceiver<CtrlMsg>,
140
    /// A oneshot sender that is used to alert other tasks when this reactor is
141
    /// finally dropped.
142
    pub(super) reactor_closed_tx: oneshot_broadcast::Sender<Result<CloseInfo>>,
143
    /// A receiver for cells to be sent on this reactor's sink.
144
    ///
145
    /// `Channel` objects have a sender that can send cells here.
146
    pub(super) cells: super::CellRx,
147
    /// A Stream from which we can read `ChanCell`s.
148
    ///
149
    /// This should be backed by a TLS connection if you want it to be secure.
150
    pub(super) input: futures::stream::Fuse<BoxedChannelStream>,
151
    /// A Sink to which we can write `ChanCell`s.
152
    ///
153
    /// This should also be backed by a TLS connection if you want it to be secure.
154
    pub(super) output: ChannelOutputSink,
155
    /// A handler for setting stream options on the underlying stream.
156
    #[cfg_attr(not(target_os = "linux"), allow(unused))]
157
    pub(super) streamops: BoxedChannelStreamOps,
158
    /// A handler and associated data for CREATE2/CREATE_FAST messages,
159
    /// if this channel should handle them.
160
    #[cfg(feature = "relay")]
161
    pub(super) create_request_handler: Option<CreateRequestHandlerAndData>,
162
    /// Timer tracking when to generate channel padding.
163
    ///
164
    /// Note that this is _distinct_ from the experimental maybenot-based padding
165
    /// implemented with padding_ctrl and padding_stream.
166
    /// This is the existing per-channel padding
167
    /// in the tor protocol used to resist netflow attacks.
168
    pub(super) padding_timer: Pin<Box<padding::Timer<R>>>,
169
    /// Outgoing cells introduced at the channel reactor
170
    pub(super) special_outgoing: SpecialOutgoing,
171
    /// A map from circuit ID to Sinks on which we can deliver cells.
172
    pub(super) circs: CircMap,
173
    /// A unique identifier for this channel.
174
    pub(super) unique_id: super::UniqId,
175
    /// Information shared with the frontend
176
    pub(super) details: Arc<ChannelDetails>,
177
    /// Context for allocating unique circuit log identifiers.
178
    pub(super) circ_unique_id_ctx: unique_id::CircUniqIdContext,
179
    /// A padding controller to which padding-related events should be reported.
180
    ///
181
    /// (This is used for experimental maybenot-based padding.)
182
    //
183
    // TODO: It would be good to use S here instead of DynTimeProvider,
184
    // but we still need the latter for the clones of padding_ctrl that we hand out
185
    // inside ChannelSender.
186
    pub(super) padding_ctrl: PaddingController<DynTimeProvider>,
187
    /// An event stream telling us about padding-related events.
188
    ///
189
    /// (This is used for experimental maybenot-based padding.)
190
    pub(super) padding_event_stream: PaddingEventStream<DynTimeProvider>,
191
    /// If present, the current rules for blocking the output based on the padding framework.
192
    pub(super) padding_blocker: Option<StartBlocking>,
193
    /// What link protocol is the channel using?
194
    #[allow(dead_code)] // We don't support protocols where this would matter
195
    pub(super) link_protocol: u16,
196
}
197

            
198
/// Outgoing cells introduced at the channel reactor
199
#[derive(Default, Debug, Clone)]
200
pub(super) struct SpecialOutgoing {
201
    /// If we must send a `PaddingNegotiate`, this is present.
202
    padding_negotiate: Option<PaddingNegotiate>,
203
    /// A number of pending PADDING cells that we have to send, once there is space.
204
    n_padding: u16,
205
}
206

            
207
impl SpecialOutgoing {
208
    /// Do we have a special cell to send?
209
    ///
210
    /// Called by the reactor before looking for cells from the reactor's clients.
211
    /// The returned message *must* be sent by the caller, not dropped!
212
    #[must_use = "SpecialOutgoing::next()'s return value must be actually sent"]
213
2758
    fn next(&mut self) -> Option<AnyChanCell> {
214
        // If this gets more cases, consider making SpecialOutgoing into a #[repr(C)]
215
        // enum, so that we can fast-path the usual case of "no special message to send".
216
2758
        if let Some(p) = self.padding_negotiate.take() {
217
            return Some(p.into());
218
2758
        }
219
2758
        if self.n_padding > 0 {
220
            self.n_padding -= 1;
221
            return Some(Padding::new().into());
222
2758
        }
223
2758
        None
224
2758
    }
225

            
226
    /// Try to queue a padding cell to be sent.
227
    fn queue_padding_cell(&mut self) {
228
        self.n_padding = self.n_padding.saturating_add(1);
229
    }
230
}
231

            
232
/// Allows us to just say debug!("{}: Reactor did a thing", &self, ...)
233
///
234
/// There is no risk of confusion because no-one would try to print a
235
/// Reactor for some other reason.
236
impl<R: Runtime> fmt::Display for Reactor<R> {
237
800
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
238
800
        fmt::Display::fmt(&self.unique_id, f)
239
800
    }
240
}
241

            
242
impl<R: Runtime> Reactor<R> {
243
    /// Launch the reactor, and run until the channel closes or we
244
    /// encounter an error.
245
    ///
246
    /// Once this function returns, the channel is dead, and can't be
247
    /// used again.
248
    #[instrument(level = "trace", skip_all)]
249
247
    pub async fn run(mut self) -> Result<()> {
250
        trace!(channel_id = %self, "Running reactor");
251
        let result: Result<()> = loop {
252
            match self.run_once().await {
253
                Ok(()) => (),
254
                Err(ReactorError::Shutdown) => break Ok(()),
255
                Err(ReactorError::Err(e)) => break Err(e),
256
            }
257
        };
258

            
259
        // Log that the reactor stopped, possibly with the associated error as a report.
260
        // May log at a higher level depending on the error kind.
261
        const MSG: &str = "Reactor stopped";
262
        match &result {
263
            Ok(()) => debug!(channel_id = %self, "{MSG}"),
264
            Err(e) => debug_report!(e, channel_id = %self, "{MSG}"),
265
        }
266

            
267
        // Inform any waiters that the channel has closed.
268
        let close_msg = result.as_ref().map_err(Clone::clone).map(|()| CloseInfo);
269
        self.reactor_closed_tx.send(close_msg);
270
        result
271
202
    }
272

            
273
    /// Helper for run(): handles only one action.
274
    #[instrument(level = "trace", skip_all)]
275
2905
    async fn run_once(&mut self) -> std::result::Result<(), ReactorError> {
276
        select! {
277

            
278
            // See if the output sink can have cells written to it yet.
279
            // If so, see if we have to-be-transmitted cells.
280
2709
            ret = self.output.prepare_send_from(async {
281
                // This runs if we will be able to write, so try to obtain a cell:
282

            
283
2709
                if let Some(l) = self.special_outgoing.next() {
284
                    // See reasoning below.
285
                    // eprintln!("PADDING - SENDING NEOGIATION: {:?}", &l);
286
                    self.padding_timer.as_mut().note_cell_sent();
287
                    return Some((l, None));
288
2709
                }
289

            
290
2709
                select_biased! {
291
2709
                    n = self.cells.next() => {
292
                        // Note transmission on *input* to the reactor, not ultimate
293
                        // transmission.  Ideally we would tap into the TCP stream at the far
294
                        // end of our TLS or perhaps during encoding on entry to the TLS, but
295
                        // both of those would involve quite some plumbing.  Doing it here in
296
                        // the reactor avoids additional inter-task communication, mutexes,
297
                        // etc.  (And there is no real difference between doing it here on
298
                        // input, to just below, on enquieing into the `sendable`.)
299
                        //
300
                        // Padding is sent when the output channel is idle, and the effect of
301
                        // buffering is just that we might sent it a little early because we
302
                        // measure idleness when we last put something into the output layers.
303
                        //
304
                        // We can revisit this if measurement shows it to be bad in practice.
305
                        //
306
                        // (We in any case need padding that we generate when idle to make it
307
                        // through to the output promptly, or it will be late and ineffective.)
308
2239
                        self.padding_timer.as_mut().note_cell_sent();
309
2239
                        n
310
                    },
311
2709
                    p = self.padding_timer.as_mut().next() => {
312
                        // eprintln!("PADDING - SENDING PADDING: {:?}", &p);
313

            
314
                        // Note that we treat padding from the padding_timer as a normal cell,
315
                        // since it doesn't have a padding machine.
316
                        self.padding_ctrl.queued_data(HopNum::from(0));
317

            
318
                        self.padding_timer.as_mut().note_cell_sent();
319
                        Some((p.into(), None))
320
                    },
321
                }
322
2239
            }) => {
323
                self.padding_ctrl.flushed_channel_cell();
324
                let (queued, sendable) = ret?;
325
                let (msg, cell_padding_info) = queued.ok_or(ReactorError::Shutdown)?;
326
                // Tell the relevant circuit padder that this cell is getting flushed.
327
                // Note that, technically, it won't go onto the network for a while longer:
328
                // it has to go through the TLS buffer, and the kernel TCP buffer.
329
                // We've got to live with that.
330
                // TODO: conceivably we could defer this even longer, but it would take
331
                // some tricky hacking!
332
                if let (Some(cell_padding_info), Some(circid)) = (cell_padding_info, msg.circid()) {
333
                    self.circs.note_cell_flushed(circid, cell_padding_info);
334
                }
335
                sendable.send(msg)?;
336
            }
337

            
338
            ret = self.control.next() => {
339
                let ctrl = match ret {
340
                    None | Some(CtrlMsg::Shutdown) => return Err(ReactorError::Shutdown),
341
                    Some(x) => x,
342
                };
343
                self.handle_control(ctrl).await?;
344
            }
345

            
346
            ret = self.padding_event_stream.next() => {
347
                let event = ret.ok_or_else(|| Error::from(internal!("Padding event stream was exhausted")))?;
348
                self.handle_padding_event(event).await?;
349
            }
350

            
351
            ret = self.input.next() => {
352
                let item = ret
353
                    .ok_or(ReactorError::Shutdown)??;
354
                crate::note_incoming_traffic();
355
                self.handle_cell(item).await?;
356
            }
357

            
358
        }
359
        Ok(()) // Run again.
360
2879
    }
361

            
362
    /// Handle a CtrlMsg other than Shutdown.
363
    #[instrument(level = "trace", skip(self))] // Intentionally omitting skip_all, msg is useful and not sensitive
364
138
    async fn handle_control(&mut self, msg: CtrlMsg) -> Result<()> {
365
        trace!(
366
            channel_id = %self,
367
            msg = ?msg,
368
            "reactor received control message"
369
        );
370

            
371
        match msg {
372
            CtrlMsg::Shutdown => panic!(), // was handled in reactor loop.
373
            CtrlMsg::CloseCircuit(id) => self.outbound_destroy_circ(id).await?,
374
            CtrlMsg::AllocateCircuit {
375
                created_sender,
376
                sender,
377
                tx,
378
            } => {
379
                let mut rng = rand::rng();
380
                let my_unique_id = self.unique_id;
381
                let circ_unique_id = self.circ_unique_id_ctx.next(my_unique_id);
382
                // NOTE: This is a very weird place to be calling new_padding, but:
383
                //  - we need to do it here or earlier, so we can add it as part of the CircEnt to
384
                //    our map.
385
                //  - We need to do it at some point where we have a runtime, which implies in a
386
                //    reactor.
387
                //
388
                // TODO circpad: We might want to lazy-allocate this somehow, or try harder to make
389
                // it a no-op when we aren't padding on a particular circuit.
390
                let (padding_ctrl, padding_stream) = crate::client::circuit::padding::new_padding(
391
                    // TODO: avoid using DynTimeProvider at some point, and re-parameterize for efficiency.
392
                    DynTimeProvider::new(self.runtime.clone()),
393
                );
394
                let ret: Result<_> = self
395
                    .circs
396
                    .add_origin_ent(&mut rng, created_sender, sender, padding_ctrl.clone())
397
51
                    .map(|id| (id, circ_unique_id, padding_ctrl, padding_stream));
398
                let _ = tx.send(ret); // don't care about other side going away
399
                self.update_disused_since();
400
            }
401
            CtrlMsg::ConfigUpdate(updates) => {
402
                if self.link_protocol == 4 {
403
                    // Link protocol 4 does not permit sending, or negotiating, link padding.
404
                    // We test for == 4 so that future updates to handshake.rs LINK_PROTOCOLS
405
                    // keep doing padding things.
406
                    return Ok(());
407
                }
408

            
409
                let ChannelPaddingInstructionsUpdates {
410
                    // List all the fields explicitly; that way the compiler will warn us
411
                    // if one is added and we fail to handle it here.
412
                    padding_enable,
413
                    padding_parameters,
414
                    padding_negotiate,
415
                } = &*updates;
416
                if let Some(parameters) = padding_parameters {
417
                    self.padding_timer.as_mut().reconfigure(parameters)?;
418
                }
419
                if let Some(enable) = padding_enable {
420
                    if *enable {
421
                        self.padding_timer.as_mut().enable();
422
                    } else {
423
                        self.padding_timer.as_mut().disable();
424
                    }
425
                }
426
                if let Some(padding_negotiate) = padding_negotiate {
427
                    // This replaces any previous PADDING_NEGOTIATE cell that we were
428
                    // told to send, but which we didn't manage to send yet.
429
                    // It doesn't make sense to queue them up.
430
                    self.special_outgoing.padding_negotiate = Some(padding_negotiate.clone());
431
                }
432
            }
433
            CtrlMsg::KistConfigUpdate(kist) => self.apply_kist_params(&kist),
434
            #[cfg(feature = "circ-padding-manual")]
435
            CtrlMsg::SetChannelPadder { padder, sender } => {
436
                self.padding_ctrl
437
                    .install_padder_padding_at_hop(HopNum::from(0), padder);
438
                let _ignore = sender.send(Ok(()));
439
            }
440
        }
441
        Ok(())
442
138
    }
443

            
444
    /// Take the padding action described in `action`.
445
    ///
446
    /// (With circuit padding disabled, PaddingEvent can't be constructed.)
447
    #[cfg(not(feature = "circ-padding"))]
448
    #[allow(clippy::unused_async)] // for symmetry with the version below
449
    async fn handle_padding_event(&mut self, action: PaddingEvent) -> Result<()> {
450
        void::unreachable(action.0)
451
    }
452

            
453
    /// Take the padding action described in `action`.
454
    #[cfg(feature = "circ-padding")]
455
    async fn handle_padding_event(&mut self, action: PaddingEvent) -> Result<()> {
456
        use PaddingEvent as PE;
457
        match action {
458
            PE::SendPadding(send_padding) => {
459
                self.handle_send_padding(send_padding).await?;
460
            }
461
            PE::StartBlocking(start_blocking) => {
462
                if self.output.is_unlimited() {
463
                    self.output.set_blocked();
464
                }
465
                self.padding_blocker = Some(start_blocking);
466
            }
467
            PE::StopBlocking => {
468
                self.output.set_unlimited();
469
            }
470
        }
471
        Ok(())
472
    }
473

            
474
    /// Send the padding described in `padding`.
475
    #[cfg(feature = "circ-padding")]
476
    async fn handle_send_padding(&mut self, padding: SendPadding) -> Result<()> {
477
        // TODO circpad: This is somewhat duplicative of the logic in `Circuit::send_padding` and
478
        // `Circuit::padding_disposition`.  It might be good to unify them at some point.
479
        // For now (Oct 2025), though, they have slightly different inputs and behaviors.
480

            
481
        use crate::client::circuit::padding::{Bypass::*, Replace::*};
482
        // multihop padding belongs in circuit padders, not here.
483
        let hop = HopNum::from(0);
484
        assert_eq!(padding.hop, hop);
485

            
486
        // If true, there is blocking, but we are allowed to bypass it.
487
        let blocking_bypassed = matches!(
488
            (&self.padding_blocker, padding.may_bypass_block()),
489
            (
490
                Some(StartBlocking {
491
                    is_bypassable: true
492
                }),
493
                BypassBlocking
494
            )
495
        );
496
        // If true, there is blocking, and we can't bypass it.
497
        let this_padding_blocked = self.padding_blocker.is_some() && !blocking_bypassed;
498

            
499
        if padding.may_replace_with_data() == Replaceable {
500
            if self.output_is_full().await? {
501
                // When the output buffer is full,
502
                // we _always_ treat it as satisfying our replaceable padding.
503
                //
504
                // TODO circpad: It would be better to check whether
505
                // the output has any bytes at all, but futures_codec doesn't seem to give us a
506
                // way to check that.  If we manage to do so in the future, we should change the
507
                // logic in this function.
508
                self.padding_ctrl
509
                    .replaceable_padding_already_queued(hop, padding);
510
                return Ok(());
511
            } else if self.cells.approx_count() > 0 {
512
                // We can replace the padding with outbound cells!
513
                if this_padding_blocked {
514
                    // In the blocked case, we just declare that the pending data _is_ the queued padding.
515
                    self.padding_ctrl
516
                        .replaceable_padding_already_queued(hop, padding);
517
                } else {
518
                    // Otherwise we report that queued data _became_ padding,
519
                    // and we allow it to pass any blocking that's present.
520
                    self.padding_ctrl.queued_data_as_padding(hop, padding);
521
                    if blocking_bypassed {
522
                        self.output.allow_n_additional_items(1);
523
                    }
524
                }
525
                return Ok(());
526
            } else {
527
                // There's nothing to replace this with, so fall through.
528
            }
529
        }
530

            
531
        // There's no replacement, so we queue unconditionally.
532
        self.special_outgoing.queue_padding_cell();
533
        self.padding_ctrl.queued_padding(hop, padding);
534
        if blocking_bypassed {
535
            self.output.allow_n_additional_items(1);
536
        }
537

            
538
        Ok(())
539
    }
540

            
541
    /// Return true if the output stream is full.
542
    ///
543
    /// We use this in circuit padding to implement replaceable padding.
544
    //
545
    // TODO circpad: We'd rather check whether there is any data at all queued in self.output,
546
    // but futures_codec doesn't give us a way to do that.
547
    #[cfg(feature = "circ-padding")]
548
    async fn output_is_full(&mut self) -> Result<bool> {
549
        use futures::future::poll_fn;
550
        use std::task::Poll;
551
        // We use poll_fn to get a cx that we can pass to poll_ready_unpin.
552
        poll_fn(|cx| {
553
            Poll::Ready(match self.output.poll_ready_unpin(cx) {
554
                // If if's ready to send, it isn't full.
555
                Poll::Ready(Ok(())) => Ok(false),
556
                // If it isn't ready to send, it's full.
557
                Poll::Pending => Ok(true),
558
                // Propagate errors:
559
                Poll::Ready(Err(e)) => Err(e),
560
            })
561
        })
562
        .await
563
    }
564

            
565
    /// Helper: process a cell on a channel.  Most cell types get ignored
566
    /// or rejected; a few get delivered to circuits.
567
    #[instrument(level = "trace", skip_all)]
568
318
    async fn handle_cell(&mut self, cell: AnyChanCell) -> Result<()> {
569
        let (circid, msg) = cell.into_circid_and_msg();
570
        use AnyChanMsg::*;
571

            
572
        match msg {
573
            Relay(_) | Padding(_) | Vpadding(_) => {} // too frequent to log.
574
            _ => trace!(
575
                channel_id = %self,
576
                "received {} for {}",
577
                msg.cmd(),
578
                CircId::get_or_zero(circid)
579
            ),
580
        }
581

            
582
        // Report the message to the padding controller.
583
        match msg {
584
            Padding(_) | Vpadding(_) => {
585
                // We always accept channel padding, even if we haven't negotiated any.
586
                let _always_acceptable = self.padding_ctrl.decrypted_padding(HopNum::from(0));
587
            }
588
            _ => self.padding_ctrl.decrypted_data(HopNum::from(0)),
589
        }
590

            
591
        match msg {
592
            // These are allowed, and need to be handled.
593
            Relay(_) => self.deliver_relay(circid, msg).await,
594

            
595
            // The 'if' guard is important as we should not consider this branch if we're not
596
            // supposed to handle CREATE* cells (and therefore RELAY_EARLY),
597
            // regardless of whether the "relay" feature is set.
598
            #[cfg(feature = "relay")]
599
            RelayEarly(_) if self.create_request_handler.is_some() => {
600
                self.deliver_relay(circid, msg).await
601
            }
602

            
603
            Destroy(_) => self.deliver_destroy(circid, msg).await,
604

            
605
            // The 'if' guard is important as we should not consider this branch if we're not
606
            // supposed to handle CREATE* cells, regardless of whether the "relay" feature is set.
607
            // We should instead fall through to the wildcard pattern.
608
            //
609
            // Clients that enable the "relay" feature, and outgoing channels for bridges,
610
            // will not have a handler set.
611
            #[cfg(feature = "relay")]
612
            CreateFast(msg) if self.create_request_handler.is_some() => {
613
                self.handle_create(circid, CreateRequest::CreateFast(msg))
614
                    .await
615
            }
616
            #[cfg(feature = "relay")]
617
            Create2(msg) if self.create_request_handler.is_some() => {
618
                self.handle_create(circid, CreateRequest::Create2(msg))
619
                    .await
620
            }
621

            
622
            CreatedFast(_) | Created2(_) => self.deliver_created(circid, msg),
623

            
624
            // From padding-spec:
625
            //
626
            // > Clients and bridges MUST reject padding negotiation messages from relays,
627
            // > and close the channel if they receive one.
628
            //
629
            // Same guard as CREATE*, only the relay side of a channel handles this. With
630
            // a create handler, it implies we are a relay.
631
            #[cfg(feature = "relay")]
632
            PaddingNegotiate(msg) if self.create_request_handler.is_some() => {
633
                self.handle_padding_negotiate(&msg)
634
            }
635

            
636
            // These are always ignored.
637
            Padding(_) | Vpadding(_) => Ok(()),
638

            
639
            // Unexpected or unhandled cell.
640
            _ => Err(Error::ChanProto(format!("Unexpected cell: {msg:?}"))),
641
        }
642
318
    }
643

            
644
    /// Give the RELAY (or possibly RELAY_EARLY) cell `msg` to the appropriate circuit.
645
214
    async fn deliver_relay(&mut self, circid: Option<CircId>, msg: AnyChanMsg) -> Result<()> {
646
214
        let Some(circid) = circid else {
647
            return Err(Error::ChanProto("Relay cell without circuit ID".into()));
648
        };
649

            
650
214
        let Some(mut ent) = self.circs.get_mut(circid) else {
651
12
            trace!(channel_id = %self, "Relay cell for nonexistent circuit {}", circid);
652
            // Silently drop the RELAY cell, as per the spec:
653
            //
654
            // > When a node receives a RELAY or RELAY_EARLY cell, it checks the cell’s circID and
655
            // > determines whether it has a corresponding circuit along that connection.
656
            // > If not, the node drops the cell.
657
            //
658
            // See https://spec.torproject.org/tor-spec/routing-relay-cells.html#circuit-id-checks
659
12
            drop(msg);
660
12
            return Ok(());
661
        };
662

            
663
202
        match &mut *ent {
664
            CircEnt::OpenOrigin { cell_sender: s, .. } => {
665
                // There's an open circuit; we can give it the RELAY cell.
666
40
                if s.send(msg).await.is_err() {
667
                    drop(ent);
668
                    // The circuit's receiver went away, so we should destroy the circuit.
669
                    self.outbound_destroy_circ(circid).await?;
670
40
                }
671
40
                Ok(())
672
            }
673
            #[cfg(feature = "relay")]
674
            CircEnt::OpenRelay { cell_sender: s, .. } => {
675
                // There's an open circuit; we can give it the RELAY cell.
676
                if s.send(msg).await.is_err() {
677
                    drop(ent);
678
                    // The circuit's receiver went away, so we should destroy the circuit.
679
                    // We send a DESTROY on our own channel, and the circuit reactor should have
680
                    // taken care of sending a DESTROY on the other channel.
681
                    self.outbound_destroy_circ(circid).await?;
682
                }
683
                Ok(())
684
            }
685
6
            CircEnt::Opening { .. } => Err(Error::ChanProto(
686
6
                "Relay cell on pending circuit before CREATED* received".into(),
687
6
            )),
688
156
            CircEnt::DestroySent(hc) => hc.receive_cell(),
689
        }
690
214
    }
691

            
692
    /// Handle a [`PaddingNegotiate`] cell in `msg` sent by a client.
693
    #[cfg(feature = "relay")]
694
    fn handle_padding_negotiate(&mut self, msg: &PaddingNegotiate) -> Result<()> {
695
        match msg.command() {
696
            tor_cell::chancell::msg::PaddingNegotiateCmd::START => {
697
                // TODO(relay): Use the consensus values instead of the spec defaults.
698
                let parameters = padding::Parameters::default_padding()
699
                    .raised_to_requested(msg.ito_low_ms(), msg.ito_high_ms());
700
                self.padding_timer.as_mut().reconfigure(&parameters)?;
701
                self.padding_timer.as_mut().enable();
702
                debug!(
703
                    channel_id = %self,
704
                    low_ms = parameters.low.as_millis(),
705
                    high_ms = parameters.high.as_millis(),
706
                    "Channel padding started"
707
                );
708
            }
709
            tor_cell::chancell::msg::PaddingNegotiateCmd::STOP => {
710
                self.padding_timer.as_mut().disable();
711
                debug!(channel_id = %self, "Channel padding stopped");
712
            }
713
            cmd => {
714
                return Err(Error::ChanProto(format!(
715
                    "Unknown PADDING_NEGOTIATE command: {cmd}"
716
                )));
717
            }
718
        }
719
        Ok(())
720
    }
721

            
722
    /// Handle a CREATE* cell `msg`.
723
    #[cfg(feature = "relay")]
724
15
    async fn handle_create(&mut self, circid: Option<CircId>, msg: CreateRequest) -> Result<()> {
725
15
        let Some(ref create_request_handler) = self.create_request_handler else {
726
            // We should have checked this in an 'if' guard in 'handle_cell()'.
727
            return Err(internal!("Called 'deliver_relay()', but handler isn't set").into());
728
        };
729

            
730
15
        let Some(circid) = circid else {
731
            let err = format!("Received {} cell without circuit ID", msg.cmd());
732
            return Err(Error::ChanProto(err));
733
        };
734

            
735
15
        let Some(chan) = create_request_handler.channel.upgrade() else {
736
            // This can happen if the last `Arc<Channel>` was dropped before the reactor had a
737
            // chance to notice.
738
            // We'll just try to reject the new circuit request and let the reactor shut down
739
            // normally, rather than return an error.
740
            let destroy = Destroy::new(DestroyReason::NONE);
741
            let destroy = AnyChanCell::new(Some(circid), destroy.into());
742

            
743
            debug!(
744
                "Unable to upgrade weak `Channel` while handling {}; sending {}",
745
                msg.cmd(),
746
                destroy.msg().cmd(),
747
            );
748
            return self.send_cell(destroy).await;
749
        };
750

            
751
        // Allocate an internal circuit ID, regardless of if the create fails or not.
752
        // We expect that this will not overflow since it would require an attacker to send
753
        // 500*2^32 bytes (~2 TiB) worth of cells.
754
15
        let circ_uniq_id = self.circ_unique_id_ctx.next(self.unique_id);
755

            
756
        // Build the relay circuit.
757
15
        let create_result = create_request_handler
758
15
            .handler
759
15
            .handle_create(
760
15
                &self.runtime,
761
15
                &chan,
762
15
                &create_request_handler.our_ed25519_id,
763
15
                &create_request_handler.our_rsa_id,
764
15
                circid,
765
15
                &msg,
766
15
                &self.details.memquota,
767
15
                circ_uniq_id,
768
15
            )
769
15
            .await;
770

            
771
        // Add the circuit to the circuit map.
772
15
        let response = match create_result {
773
            Ok((response, components)) => {
774
                let RelayCircComponents {
775
                    circ,
776
                    sender,
777
                    padding_ctrl,
778
8
                } = components;
779

            
780
8
                if let Err(reason) = self.circs.add_relay_ent(circid, circ, sender, padding_ctrl) {
781
                    debug!("Unable to add circuit map entry for incoming circuit: {reason}");
782
                    CreateResponse::Destroy(Destroy::new(reason))
783
                } else {
784
8
                    response
785
                }
786
            }
787
7
            Err(destroy) => CreateResponse::Destroy(destroy),
788
        };
789

            
790
15
        let response = AnyChanCell::new(Some(circid), response.into());
791
15
        self.send_cell(response).await
792
15
    }
793

            
794
    /// Handle a CREATED{_FAST,2} cell by passing it on to the appropriate
795
    /// circuit, if that circuit is waiting for one.
796
50
    fn deliver_created(&mut self, circid: Option<CircId>, msg: AnyChanMsg) -> Result<()> {
797
50
        let Some(circid) = circid else {
798
6
            return Err(Error::ChanProto("'Created' cell without circuit ID".into()));
799
        };
800

            
801
44
        let Some(target) = self.circs.advance_from_opening(circid) else {
802
6
            trace!(channel_id = %self, "Unexpected CREATED* cell not on opening circuit {}", circid);
803
            // Silently drop the cell: we can't easily distinguish cells with bogus CircIds
804
            // from cells arriving on already-closed circuits,
805
            // so we err on the side of keeping the channel open.
806
            //
807
            // See https://gitlab.torproject.org/tpo/core/arti/-/work_items/2655#note_3447841
808
6
            drop(msg);
809
6
            return Ok(());
810
        };
811

            
812
38
        let created = msg.try_into()?;
813
        // TODO(nickm) I think that this one actually means the other side
814
        // is closed. See arti#269.
815
38
        target.send(created).map_err(|_| {
816
            Error::from(internal!(
817
                "Circuit queue rejected created message. Is it closing?"
818
            ))
819
        })
820
50
    }
821

            
822
    /// Handle a DESTROY cell by removing the corresponding circuit
823
    /// from the map, and passing the destroy cell onward to the circuit.
824
39
    async fn deliver_destroy(&mut self, circid: Option<CircId>, msg: AnyChanMsg) -> Result<()> {
825
39
        let Some(circid) = circid else {
826
            return Err(Error::ChanProto("'Destroy' cell without circuit ID".into()));
827
        };
828

            
829
        /// Helper to send DESTROY cell.
830
14
        async fn send_destroy(mut sender: CircuitRxSender, msg: AnyChanMsg) -> Result<()> {
831
14
            sender
832
14
                .send(msg)
833
14
                .await
834
                // TODO(nickm) I think that this one actually means the other side
835
                // is closed. See arti#269.
836
                .map_err(|_| internal!("open circuit wasn't interested in destroy cell?").into())
837
14
        }
838

            
839
        // Remove the circuit from the map: nothing more can be done with it.
840
39
        let entry = self.circs.remove(circid);
841
39
        self.update_disused_since();
842
39
        match entry {
843
            // If the circuit is waiting for CREATED, tell it that it
844
            // won't get one.
845
            Some(CircEnt::Opening {
846
                create_response_sender,
847
                ..
848
            }) => {
849
13
                trace!(channel_id = %self, "Passing destroy to pending circuit {}", circid);
850
13
                create_response_sender
851
13
                    .send(msg.try_into()?)
852
                    // TODO(nickm) I think that this one actually means the other side
853
                    // is closed. See arti#269.
854
                    .map_err(|_| {
855
                        internal!("pending circuit wasn't interested in destroy cell?").into()
856
                    })
857
            }
858
            // It's an open origin circuit: tell it that it got a DESTROY cell.
859
            Some(CircEnt::OpenOrigin { cell_sender, .. }) => {
860
10
                trace!(channel_id = %self, "Passing destroy to open origin circuit {}", circid);
861
10
                send_destroy(cell_sender, msg).await
862
            }
863
            // It's an open relay circuit: tell it that it got a DESTROY cell.
864
            #[cfg(feature = "relay")]
865
            Some(CircEnt::OpenRelay { cell_sender, .. }) => {
866
4
                trace!(channel_id = %self, "Passing destroy to open relay circuit {}", circid);
867
4
                send_destroy(cell_sender, msg).await
868
            }
869
            // We've sent a destroy; we can leave this circuit removed.
870
6
            Some(CircEnt::DestroySent(_)) => Ok(()),
871
            // Got a DESTROY cell for a circuit we don't have.
872
            None => {
873
6
                trace!(channel_id = %self, "Destroy for nonexistent circuit {}", circid);
874
                // Silently drop the cell: we can't easily distinguish cells with bogus CircIds
875
                // from cells arriving on already-closed circuits,
876
                // so we err on the side of keeping the channel open:
877
                //
878
                // See https://gitlab.torproject.org/tpo/core/arti/-/work_items/2655#note_3447841
879
6
                drop(msg);
880
6
                Ok(())
881
            }
882
        }
883
39
    }
884

            
885
    /// Helper: send a cell on the outbound sink.
886
44
    async fn send_cell(&mut self, cell: AnyChanCell) -> Result<()> {
887
44
        self.output.send(cell).await?;
888
42
        Ok(())
889
44
    }
890

            
891
    /// Called when a circuit goes away: sends a DESTROY cell and removes
892
    /// the circuit.
893
    ///
894
    /// No-op if the circuit has already gone away,
895
    /// (for example if we have already received DESTROY).
896
87
    async fn outbound_destroy_circ(&mut self, id: CircId) -> Result<()> {
897
87
        if !self.circs.is_open(id) {
898
58
            return Ok(());
899
29
        }
900

            
901
29
        trace!(channel_id = %self, "Circuit {} is gone; sending DESTROY", id);
902
        // Remove the circuit's entry from the map: nothing more
903
        // can be done with it.
904
        // TODO: It would be great to have a tighter upper bound for
905
        // the number of relay cells we'll receive.
906
29
        self.circs.destroy_sent(id, HalfCirc::new(3000));
907
29
        self.update_disused_since();
908
29
        let destroy = Destroy::new(DestroyReason::NONE).into();
909
29
        let cell = AnyChanCell::new(Some(id), destroy);
910
29
        self.send_cell(cell).await?;
911

            
912
27
        Ok(())
913
87
    }
914

            
915
    /// Update disused timestamp with current time if this channel is no longer used
916
119
    fn update_disused_since(&self) {
917
119
        if self.circs.open_ent_count() == 0 {
918
            // Update disused_since if it still indicates that the channel is in use
919
68
            self.details.unused_since.update_if_none();
920
        } else {
921
            // Mark this channel as in use
922
51
            self.details.unused_since.clear();
923
        }
924
119
    }
925

            
926
    /// Use the new KIST parameters.
927
    #[cfg(target_os = "linux")]
928
    fn apply_kist_params(&self, params: &KistParams) {
929
        use super::kist::KistMode;
930

            
931
        let set_tcp_notsent_lowat = |v: u32| {
932
            if let Err(e) = self.streamops.set_tcp_notsent_lowat(v) {
933
                // This is bad, but not fatal: not setting the KIST options
934
                // comes with a performance penalty, but we don't have to crash.
935
                error_report!(e, "Failed to set KIST socket options");
936
            }
937
        };
938

            
939
        match params.kist_enabled() {
940
            KistMode::TcpNotSentLowat => set_tcp_notsent_lowat(params.tcp_notsent_lowat()),
941
            KistMode::Disabled => set_tcp_notsent_lowat(u32::MAX),
942
        }
943
    }
944

            
945
    /// Use the new KIST parameters.
946
    #[cfg(not(target_os = "linux"))]
947
    fn apply_kist_params(&self, params: &KistParams) {
948
        use super::kist::KistMode;
949

            
950
        if params.kist_enabled() != KistMode::Disabled {
951
            tracing::warn!("KIST not currently supported on non-linux platforms");
952
        }
953
    }
954
}
955

            
956
/// If the channel is configured to handle CREATE* requests,
957
/// this contains anything that is needed solely for this purpose.
958
#[cfg(feature = "relay")]
959
pub(super) struct CreateRequestHandlerAndData {
960
    /// A handler for CREATE2/CREATE_FAST messages.
961
    pub(super) handler: Arc<CreateRequestHandler>,
962
    /// The [`Channel`] associated with this reactor.
963
    ///
964
    /// We don't want the channel reactor to access its `Channel` directly
965
    /// (shared data should use its [`ChannelDetails`] instead),
966
    /// but we need it to pass it to new circuit reactors,
967
    /// so we store a copy here.
968
    pub(super) channel: Weak<Channel>,
969
    /// Our Ed25519 identity for ntor-v3 handshakes.
970
    pub(super) our_ed25519_id: Ed25519Identity,
971
    /// Our RSA identity for ntor handshakes.
972
    pub(super) our_rsa_id: RsaIdentity,
973
}
974

            
975
#[cfg(test)]
976
pub(crate) mod test {
977
    #![allow(clippy::unwrap_used)]
978
    use super::*;
979
    use crate::channel::{Canonicity, ChannelMode, ClosedUnexpectedly, UniqId};
980
    use crate::circuit::test::fake_mpsc;
981
    use crate::client::circuit::CircParameters;
982
    use crate::client::circuit::padding::new_padding;
983
    use crate::peer::{PeerAddr, PeerInfo};
984
    use crate::util::{DummyTimeoutEstimator, fake_mq};
985
    use futures::sink::SinkExt;
986
    use futures::stream::StreamExt;
987
    use tor_cell::chancell::msg;
988
    use tor_linkspec::{OwnedChanTarget, RelayIdsBuilder};
989
    use tor_rtcompat::SpawnExt;
990
    use tor_rtcompat::{DynTimeProvider, NoOpStreamOpsHandle, Runtime};
991

            
992
    pub(crate) type CodecResult = std::result::Result<AnyChanCell, Error>;
993

            
994
    pub(crate) fn new_reactor<R: Runtime>(
995
        runtime: R,
996
        mode: ChannelMode,
997
    ) -> (
998
        Arc<crate::channel::Channel>,
999
        Reactor<R>,
        mpsc::Receiver<AnyChanCell>,
        mpsc::Sender<CodecResult>,
    ) {
        let link_protocol = 4;
        let (send1, recv1) = mpsc::channel(32);
        let (send2, recv2) = mpsc::channel(32);
        let unique_id = UniqId::new();
        let ed = [6; 32].into();
        let rsa = [10; 20].into();
        let dummy_target = OwnedChanTarget::builder()
            .ed_identity(ed)
            .rsa_identity(rsa)
            .build()
            .unwrap();
        let mut peer_ids = RelayIdsBuilder::default();
        peer_ids.ed_identity(ed);
        peer_ids.rsa_identity(rsa);
        let peer_info = PeerInfo::new(PeerAddr::UNSPECIFIED, peer_ids.build().unwrap());
        let send1 = send1.sink_map_err(|e| {
            trace!("got sink error: {:?}", e);
            Error::CellDecodeErr {
                object: "reactor test",
                err: tor_cell::Error::ChanProto("dummy message".into()),
            }
        });
        let stream_ops = NoOpStreamOpsHandle::default();
        let (chan, reactor) = crate::channel::Channel::new(
            mode,
            link_protocol,
            Box::new(send1),
            Box::new(recv2),
            Box::new(stream_ops),
            unique_id,
            dummy_target,
            safelog::MaybeSensitive::not_sensitive(peer_info),
            crate::ClockSkew::None,
            runtime,
            fake_mq(),
            Canonicity::new_canonical(),
        )
        .expect("channel create failed");
        (chan, reactor, recv1, send2)
    }
    // Try shutdown from inside run_once..
    #[test]
    fn shutdown() {
        tor_rtcompat::test_with_all_runtimes!(|rt| async move {
            let (chan, mut reactor, _output, _input) = new_reactor(rt, ChannelMode::Client);
            chan.terminate();
            let r = reactor.run_once().await;
            assert!(matches!(r, Err(ReactorError::Shutdown)));
        });
    }
    // Try shutdown while reactor is running.
    #[test]
    fn shutdown2() {
        tor_rtcompat::test_with_all_runtimes!(|rt| async move {
            // TODO: Ask a rust person if this is how to do this.
            use futures::future::FutureExt;
            use futures::join;
            let (chan, reactor, _output, _input) = new_reactor(rt, ChannelMode::Client);
            // Let's get the reactor running...
            let run_reactor = reactor.run().map(|x| x.is_ok()).shared();
            let rr = run_reactor.clone();
            let exit_then_check = async {
                assert!(rr.peek().is_none());
                // ... and terminate the channel while that's happening.
                chan.terminate();
            };
            let (rr_s, _) = join!(run_reactor, exit_then_check);
            // Now let's see. The reactor should not _still_ be running.
            assert!(rr_s);
        });
    }
    #[test]
    fn new_circ_closed() {
        tor_rtcompat::test_with_all_runtimes!(|rt| async move {
            let (chan, mut reactor, mut output, _input) =
                new_reactor(rt.clone(), ChannelMode::Client);
            assert!(chan.duration_unused().is_some()); // unused yet
            let (ret, reac) = futures::join!(
                chan.new_tunnel(Arc::new(DummyTimeoutEstimator)),
                reactor.run_once()
            );
            let (pending, circr) = ret.unwrap();
            rt.spawn(async {
                let _ignore = circr.run().await;
            })
            .unwrap();
            assert!(reac.is_ok());
            let id = pending.peek_circid();
            let ent = reactor.circs.get_mut(id);
            assert!(matches!(*ent.unwrap(), CircEnt::Opening { .. }));
            assert!(chan.duration_unused().is_none()); // in use
            // Now drop the circuit; this should tell the reactor to remove
            // the circuit from the map.
            drop(pending);
            reactor.run_once().await.unwrap();
            let ent = reactor.circs.get_mut(id);
            assert!(matches!(*ent.unwrap(), CircEnt::DestroySent(_)));
            let cell = output.next().await.unwrap();
            assert_eq!(cell.circid(), Some(id));
            assert!(matches!(cell.msg(), AnyChanMsg::Destroy(_)));
            assert!(chan.duration_unused().is_some()); // unused again
        });
    }
    // Test proper delivery of a created cell that doesn't make a channel
    #[test]
    #[ignore] // See bug #244: re-enable this test once it passes reliably.
    fn new_circ_create_failure() {
        use std::time::Duration;
        use tor_rtcompat::SleepProvider;
        tor_rtcompat::test_with_all_runtimes!(|rt| async move {
            let (chan, mut reactor, mut output, mut input) =
                new_reactor(rt.clone(), ChannelMode::Client);
            let (ret, reac) = futures::join!(
                chan.new_tunnel(Arc::new(DummyTimeoutEstimator)),
                reactor.run_once()
            );
            let (pending, circr) = ret.unwrap();
            rt.spawn(async {
                let _ignore = circr.run().await;
            })
            .unwrap();
            assert!(reac.is_ok());
            let circparams = CircParameters::default();
            let id = pending.peek_circid();
            let ent = reactor.circs.get_mut(id);
            assert!(matches!(*ent.unwrap(), CircEnt::Opening { .. }));
            #[allow(clippy::clone_on_copy)]
            let rtc = rt.clone();
            let send_response = async {
                rtc.sleep(Duration::from_millis(100)).await;
                trace!("sending createdfast");
                // We'll get a bad handshake result from this createdfast cell.
                let created_cell = AnyChanCell::new(Some(id), msg::CreatedFast::new(*b"x").into());
                input.send(Ok(created_cell)).await.unwrap();
                reactor.run_once().await.unwrap();
            };
            let (circ, _) = futures::join!(pending.create_firsthop_fast(circparams), send_response);
            // Make sure statuses are as expected.
            assert!(matches!(circ.err().unwrap(), Error::BadCircHandshakeAuth));
            reactor.run_once().await.unwrap();
            // Make sure that the createfast cell got sent
            let cell_sent = output.next().await.unwrap();
            assert!(matches!(cell_sent.msg(), msg::AnyChanMsg::CreateFast(_)));
            // But the next run if the reactor will make the circuit get closed.
            let ent = reactor.circs.get_mut(id);
            assert!(matches!(*ent.unwrap(), CircEnt::DestroySent(_)));
        });
    }
    // Try incoming cells that shouldn't arrive on channels.
    #[test]
    fn bad_cells() {
        tor_rtcompat::test_with_all_runtimes!(|rt| async move {
            let (_chan, mut reactor, _output, mut input) = new_reactor(rt, ChannelMode::Client);
            // Created2 cells for nonexistent circuits are dropped
            let created2_cell = msg::Created2::new(*b"hihi").into();
            input
                .send(Ok(AnyChanCell::new(CircId::new(7), created2_cell)))
                .await
                .unwrap();
            reactor.run_once().await.unwrap();
            // Relay cells on a circuit we've never heard of are dropped
            let relay_cell = msg::Relay::new(b"abc").into();
            input
                .send(Ok(AnyChanCell::new(CircId::new(4), relay_cell)))
                .await
                .unwrap();
            reactor.run_once().await.unwrap();
            // There used to be tests here for other types, but now that we only
            // accept OpenClientChanCell, we know that the codec can't even try
            // to give us e.g. VERSIONS or CREATE.
        });
    }
    #[test]
    fn deliver_relay() {
        tor_rtcompat::test_with_all_runtimes!(|rt| async move {
            use oneshot_fused_workaround as oneshot;
            let (_chan, mut reactor, _output, mut input) =
                new_reactor(rt.clone(), ChannelMode::Client);
            let (padding_ctrl, _padding_stream) = new_padding(DynTimeProvider::new(rt));
            let (_circ_stream_7, mut circ_stream_13) = {
                let (snd1, _rcv1) = oneshot::channel();
                let (snd2, rcv2) = fake_mpsc(64);
                reactor.circs.put_unchecked(
                    CircId::new(7).unwrap(),
                    CircEnt::Opening {
                        create_response_sender: snd1,
                        cell_sender: snd2,
                        padding_ctrl: padding_ctrl.clone(),
                    },
                );
                let (snd3, rcv3) = fake_mpsc(64);
                reactor.circs.put_unchecked(
                    CircId::new(13).unwrap(),
                    CircEnt::OpenOrigin {
                        cell_sender: snd3,
                        padding_ctrl,
                    },
                );
                reactor.circs.put_unchecked(
                    CircId::new(23).unwrap(),
                    CircEnt::DestroySent(HalfCirc::new(25)),
                );
                (rcv2, rcv3)
            };
            // If a relay cell is sent on an open channel, the correct circuit
            // should get it.
            let relaycell: AnyChanMsg = msg::Relay::new(b"do you suppose").into();
            input
                .send(Ok(AnyChanCell::new(CircId::new(13), relaycell.clone())))
                .await
                .unwrap();
            reactor.run_once().await.unwrap();
            let got = circ_stream_13.next().await.unwrap();
            assert!(matches!(got, AnyChanMsg::Relay(_)));
            // If a relay cell is sent on an opening channel, that's an error.
            input
                .send(Ok(AnyChanCell::new(CircId::new(7), relaycell.clone())))
                .await
                .unwrap();
            let e = reactor.run_once().await.unwrap_err().unwrap_err();
            assert_eq!(
                format!("{}", e),
                "Channel protocol violation: Relay cell on pending circuit before CREATED* received"
            );
            // If a relay cell is sent on a non-existent circuit, it will be dropped.
            input
                .send(Ok(AnyChanCell::new(CircId::new(101), relaycell.clone())))
                .await
                .unwrap();
            reactor.run_once().await.unwrap();
            // It's fine to get a relay cell on a DestroySent channel: that happens
            // when the other side hasn't noticed the Destroy yet.
            // We can do this 25 more times according to our setup:
            for _ in 0..25 {
                input
                    .send(Ok(AnyChanCell::new(CircId::new(23), relaycell.clone())))
                    .await
                    .unwrap();
                reactor.run_once().await.unwrap(); // should be fine.
            }
            // This one will fail.
            input
                .send(Ok(AnyChanCell::new(CircId::new(23), relaycell.clone())))
                .await
                .unwrap();
            let e = reactor.run_once().await.unwrap_err().unwrap_err();
            assert_eq!(
                format!("{}", e),
                "Channel protocol violation: Too many cells received on destroyed circuit"
            );
        });
    }
    #[test]
    fn deliver_destroy() {
        tor_rtcompat::test_with_all_runtimes!(|rt| async move {
            use crate::circuit::celltypes::*;
            use oneshot_fused_workaround as oneshot;
            let (_chan, mut reactor, _output, mut input) =
                new_reactor(rt.clone(), ChannelMode::Client);
            let (padding_ctrl, _padding_stream) = new_padding(DynTimeProvider::new(rt));
            let (circ_oneshot_7, mut circ_stream_13) = {
                let (snd1, rcv1) = oneshot::channel();
                let (snd2, _rcv2) = fake_mpsc(64);
                reactor.circs.put_unchecked(
                    CircId::new(7).unwrap(),
                    CircEnt::Opening {
                        create_response_sender: snd1,
                        cell_sender: snd2,
                        padding_ctrl: padding_ctrl.clone(),
                    },
                );
                let (snd3, rcv3) = fake_mpsc(64);
                reactor.circs.put_unchecked(
                    CircId::new(13).unwrap(),
                    CircEnt::OpenOrigin {
                        cell_sender: snd3,
                        padding_ctrl: padding_ctrl.clone(),
                    },
                );
                reactor.circs.put_unchecked(
                    CircId::new(23).unwrap(),
                    CircEnt::DestroySent(HalfCirc::new(25)),
                );
                (rcv1, rcv3)
            };
            // Destroying an opening circuit is fine.
            let destroycell: AnyChanMsg = msg::Destroy::new(0.into()).into();
            input
                .send(Ok(AnyChanCell::new(CircId::new(7), destroycell.clone())))
                .await
                .unwrap();
            reactor.run_once().await.unwrap();
            let msg = circ_oneshot_7.await;
            assert!(matches!(msg, Ok(CreateResponse::Destroy(_))));
            // Destroying an open circuit is fine.
            input
                .send(Ok(AnyChanCell::new(CircId::new(13), destroycell.clone())))
                .await
                .unwrap();
            reactor.run_once().await.unwrap();
            let msg = circ_stream_13.next().await.unwrap();
            assert!(matches!(msg, AnyChanMsg::Destroy(_)));
            // Destroying a DestroySent circuit is fine.
            input
                .send(Ok(AnyChanCell::new(CircId::new(23), destroycell.clone())))
                .await
                .unwrap();
            reactor.run_once().await.unwrap();
            // Destroying a nonexistent circuit is not an error (the DESTROY is dropped).
            input
                .send(Ok(AnyChanCell::new(CircId::new(101), destroycell.clone())))
                .await
                .unwrap();
            reactor.run_once().await.unwrap();
        });
    }
    #[test]
    fn closing_if_reactor_dropped() {
        tor_rtcompat::test_with_all_runtimes!(|rt| async move {
            let (chan, reactor, _output, _input) = new_reactor(rt, ChannelMode::Client);
            assert!(!chan.is_closing());
            drop(reactor);
            assert!(chan.is_closing());
            assert!(matches!(
                chan.wait_for_close().await,
                Err(ClosedUnexpectedly::ReactorDropped),
            ));
        });
    }
    #[test]
    fn closing_if_reactor_shutdown() {
        tor_rtcompat::test_with_all_runtimes!(|rt| async move {
            let (chan, reactor, _output, _input) = new_reactor(rt, ChannelMode::Client);
            assert!(!chan.is_closing());
            chan.terminate();
            assert!(!chan.is_closing());
            let r = reactor.run().await;
            assert!(r.is_ok());
            assert!(chan.is_closing());
            assert!(chan.wait_for_close().await.is_ok());
        });
    }
    #[test]
    fn reactor_error_wait_for_close() {
        tor_rtcompat::test_with_all_runtimes!(|rt| async move {
            let (chan, reactor, _output, mut input) = new_reactor(rt, ChannelMode::Client);
            // force an error by sending created2 cell without a CircId
            let created2_cell = msg::Created2::new(*b"hihi").into();
            input
                .send(Ok(AnyChanCell::new(None, created2_cell)))
                .await
                .unwrap();
            // `reactor.run()` should return an error
            let run_error = reactor.run().await.unwrap_err();
            // `chan.wait_for_close()` should return the same error
            let Err(ClosedUnexpectedly::ReactorError(wait_error)) = chan.wait_for_close().await
            else {
                panic!("Expected a 'ReactorError'");
            };
            // `Error` doesn't implement `PartialEq`, so best we can do is to compare the strings
            assert_eq!(run_error.to_string(), wait_error.to_string());
        });
    }
}