1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
//! Helpers for handling CREATE* cells.
use crate::circuit::celltypes::CreateResponse;
use crate::{Error, Result};
use tor_cell::chancell;
use tor_cell::chancell::msg::{AnyChanMsg, HandshakeType};
/// An object that can put a given handshake into a ChanMsg for a CREATE*
/// cell, and unwrap a CREATED* cell.
pub(crate) trait CreateHandshakeWrap {
/// Construct an appropriate ChanMsg to hold this kind of handshake.
fn to_chanmsg(&self, bytes: Vec<u8>) -> AnyChanMsg;
/// Decode a ChanMsg to an appropriate handshake value, checking
/// its type.
fn decode_chanmsg(&self, msg: CreateResponse) -> Result<Vec<u8>>;
}
/// A CreateHandshakeWrap that generates CREATE_FAST and handles CREATED_FAST.
pub(crate) struct CreateFastWrap;
impl CreateHandshakeWrap for CreateFastWrap {
fn to_chanmsg(&self, bytes: Vec<u8>) -> AnyChanMsg {
chancell::msg::CreateFast::new(bytes).into()
}
fn decode_chanmsg(&self, msg: CreateResponse) -> Result<Vec<u8>> {
use CreateResponse::*;
match msg {
CreatedFast(m) => Ok(m.into_handshake()),
Destroy(_) => Err(Error::CircRefused(
"Relay replied to CREATE_FAST with DESTROY.",
)),
_ => Err(Error::CircProto(format!(
"Relay replied to CREATE_FAST with unexpected cell: {}",
msg
))),
}
}
}
/// A CreateHandshakeWrap that generates CREATE2 and handles CREATED2
pub(crate) struct Create2Wrap {
/// The handshake type to put in the CREATE2 cell.
pub(crate) handshake_type: HandshakeType,
}
impl CreateHandshakeWrap for Create2Wrap {
fn to_chanmsg(&self, bytes: Vec<u8>) -> AnyChanMsg {
chancell::msg::Create2::new(self.handshake_type, bytes).into()
}
fn decode_chanmsg(&self, msg: CreateResponse) -> Result<Vec<u8>> {
use CreateResponse::*;
match msg {
Created2(m) => Ok(m.into_body()),
Destroy(_) => Err(Error::CircRefused("Relay replied to CREATE2 with DESTROY.")),
_ => Err(Error::CircProto(format!(
"Relay replied to CREATE2 with unexpected cell {}",
msg
))),
}
}
}
|