1
//! Implementation for encoding and decoding of ChanCells.
2

            
3
use super::{CELL_DATA_LEN, ChanCell};
4
use crate::Error;
5
use crate::chancell::{ChanCmd, ChanMsg, CircId};
6
use tor_bytes::{self, Reader, Writer};
7
use tor_error::internal;
8

            
9
use bytes::BytesMut;
10

            
11
/// This object can be used to encode and decode channel cells.
12
///
13
/// NOTE: only link protocol versions 3 and higher are supported.
14
/// VERSIONS cells are not supported via the encoder/decoder, since
15
/// VERSIONS cells always use a two-byte circuit-ID for backwards
16
/// compatibility with protocol versions < 4.
17
///
18
/// The implemented format is one of the following:
19
///
20
/// Variable-length cells (since protocol versions 2 and 3 respectively):
21
/// ```ignore
22
///     u32 circid;
23
///     u8 command;
24
///     u16 len;
25
///     u8 body[len];
26
/// ```
27
///
28
/// Fixed-width cells (since protocol version 1 and 4 respectively):
29
/// ```ignore
30
///     u32 circid;
31
///     u8 command;
32
///     u8 body[509];
33
/// ```
34
pub struct ChannelCodec {
35
    #[allow(dead_code)] // We don't support any link versions where this matters
36
    /// The link protocol version being used for this channel.
37
    ///
38
    /// (We don't currently support any versions of the link protocol
39
    /// where this version matters, but for protocol versions below 4, it would
40
    /// affect the length of the circuit ID.)
41
    link_version: u16,
42
}
43

            
44
impl ChannelCodec {
45
    /// Create a new ChannelCodec with a given link protocol version
46
4611
    pub fn new(link_version: u16) -> Self {
47
4611
        ChannelCodec { link_version }
48
4611
    }
49

            
50
    /// Return the link protocol version of this codec.
51
742
    pub fn link_version(&self) -> u16 {
52
742
        self.link_version
53
742
    }
54

            
55
    /// Write the given cell into the provided BytesMut object.
56
87
    pub fn write_cell<M: ChanMsg>(
57
87
        &mut self,
58
87
        item: ChanCell<M>,
59
87
        dst: &mut BytesMut,
60
87
    ) -> crate::Result<()> {
61
87
        let ChanCell { circid, msg } = item;
62
87
        let cmd = msg.cmd();
63
87
        dst.write_u32(CircId::get_or_zero(circid));
64
87
        dst.write_u8(cmd.into());
65

            
66
        // this is typically 5, but not always
67
        // (for example if we were given a non-empty `dst`)
68
87
        let pos = dst.len();
69

            
70
        // now write the cell body and handle the length.
71
87
        if cmd.is_var_cell() {
72
16
            dst.write_u16(0);
73
16
            msg.encode_onto(dst)?;
74
16
            let len = dst.len() - pos - 2;
75
16
            if len > u16::MAX as usize {
76
                return Err(Error::Internal(internal!("ran out of space for varcell")));
77
16
            }
78
            // go back and set the length.
79
16
            *(<&mut [u8; 2]>::try_from(&mut dst[pos..pos + 2])
80
16
                .expect("two-byte slice was not two bytes!?")) = (len as u16).to_be_bytes();
81
        } else {
82
71
            msg.encode_onto(dst)?;
83
71
            let len = dst.len() - pos;
84
71
            if len > CELL_DATA_LEN {
85
                return Err(Error::Internal(internal!("ran out of space for cell")));
86
71
            }
87
            // pad to end of fixed-length cell
88
71
            dst.write_zeros(CELL_DATA_LEN - len);
89
        }
90
87
        Ok(())
91
87
    }
92

            
93
    /// Try to decode a cell from the provided BytesMut object.
94
    ///
95
    /// On a definite decoding error, return Err(_).  On a cell that might
96
    /// just be truncated, return Ok(None).
97
1000
    pub fn decode_cell<M: ChanMsg>(
98
1000
        &mut self,
99
1000
        src: &mut BytesMut,
100
1000
    ) -> crate::Result<Option<ChanCell<M>>> {
101
        /// Wrap `be` as an appropriate type.
102
212
        fn wrap_err(be: tor_bytes::Error) -> crate::Error {
103
212
            crate::Error::BytesErr {
104
212
                err: be,
105
212
                parsed: "channel cell",
106
212
            }
107
212
        }
108

            
109
1000
        if src.len() < 7 {
110
            // Smallest possible command: varcell with len 0
111
199
            return Ok(None);
112
801
        }
113
801
        let cmd: ChanCmd = src[4].into();
114
801
        let varcell = cmd.is_var_cell();
115
801
        let cell_len: usize = if varcell {
116
549
            let msg_len = u16::from_be_bytes(
117
549
                src[5..7]
118
549
                    .try_into()
119
549
                    .expect("Two-byte slice was not two bytes long!?"),
120
            );
121
549
            msg_len as usize + 7
122
        } else {
123
252
            514
124
        };
125
801
        if src.len() < cell_len {
126
518
            return Ok(None);
127
283
        }
128

            
129
283
        let cell = src.split_to(cell_len).freeze();
130
        //trace!("{:?} cell body ({}) is {:?}", cmd, cell.len(), &cell[..]);
131
283
        let mut r = Reader::from_bytes(&cell);
132
283
        let circid: Option<CircId> = CircId::new(r.take_u32().map_err(wrap_err)?);
133
283
        r.advance(if varcell { 3 } else { 1 }).map_err(wrap_err)?;
134
283
        let msg = M::decode_from_reader(cmd, &mut r).map_err(wrap_err)?;
135

            
136
275
        if !cmd.accepts_circid_val(circid) {
137
4
            return Err(Error::ChanProto(format!(
138
4
                "Invalid circuit ID {} for cell command {}",
139
4
                CircId::get_or_zero(circid),
140
4
                cmd
141
4
            )));
142
271
        }
143
271
        Ok(Some(ChanCell { circid, msg }))
144
1000
    }
145
}