webrtc_turn/proto/
chandata.rs

1#[cfg(test)]
2mod chandata_test;
3
4use super::channum::*;
5use crate::errors::*;
6
7use util::Error;
8
9const PADDING: usize = 4;
10
11fn nearest_padded_value_length(l: usize) -> usize {
12    let mut n = PADDING * (l / PADDING);
13    if n < l {
14        n += PADDING;
15    }
16    n
17}
18
19const CHANNEL_DATA_LENGTH_SIZE: usize = 2;
20const CHANNEL_DATA_NUMBER_SIZE: usize = CHANNEL_DATA_LENGTH_SIZE;
21const CHANNEL_DATA_HEADER_SIZE: usize = CHANNEL_DATA_LENGTH_SIZE + CHANNEL_DATA_NUMBER_SIZE;
22
23// ChannelData represents The ChannelData Message.
24//
25// See RFC 5766 Section 11.4
26#[derive(Default, Debug)]
27pub struct ChannelData {
28    pub data: Vec<u8>, // can be subslice of Raw
29    pub number: ChannelNumber,
30    pub raw: Vec<u8>,
31}
32
33impl PartialEq for ChannelData {
34    fn eq(&self, other: &Self) -> bool {
35        self.data == other.data && self.number == other.number
36    }
37}
38
39impl ChannelData {
40    // grow ensures that internal buffer will fit v more bytes and
41    // increases it capacity if necessary.
42    //
43    // Similar to stun.Message.grow method.
44    fn grow(&mut self, v: usize) {
45        let n = self.raw.len() + v;
46        self.raw.extend_from_slice(&vec![0; n - self.raw.len()]);
47    }
48
49    // Reset resets Length, Data and Raw length.
50    pub fn reset(&mut self) {
51        self.raw.clear();
52        self.data.clear();
53    }
54
55    // Encode encodes ChannelData Message to Raw.
56    pub fn encode(&mut self) {
57        self.raw.clear();
58        self.write_header();
59        self.raw.extend_from_slice(&self.data);
60        let padded = nearest_padded_value_length(self.raw.len());
61        let bytes_to_add = padded - self.raw.len();
62        if bytes_to_add > 0 {
63            self.raw.extend_from_slice(&vec![0; bytes_to_add]);
64        }
65    }
66
67    // Decode decodes The ChannelData Message from Raw.
68    pub fn decode(&mut self) -> Result<(), Error> {
69        let buf = &self.raw;
70        if buf.len() < CHANNEL_DATA_HEADER_SIZE {
71            return Err(ERR_UNEXPECTED_EOF.to_owned());
72        }
73        let num = u16::from_be_bytes([buf[0], buf[1]]);
74        self.number = ChannelNumber(num);
75        if !self.number.valid() {
76            return Err(ERR_INVALID_CHANNEL_NUMBER.to_owned());
77        }
78        let l = u16::from_be_bytes([
79            buf[CHANNEL_DATA_NUMBER_SIZE],
80            buf[CHANNEL_DATA_NUMBER_SIZE + 1],
81        ]) as usize;
82        if l > buf[CHANNEL_DATA_HEADER_SIZE..].len() {
83            return Err(ERR_BAD_CHANNEL_DATA_LENGTH.to_owned());
84        }
85        self.data = buf[CHANNEL_DATA_HEADER_SIZE..CHANNEL_DATA_HEADER_SIZE + l].to_vec();
86
87        Ok(())
88    }
89
90    // WriteHeader writes channel number and length.
91    pub fn write_header(&mut self) {
92        if self.raw.len() < CHANNEL_DATA_HEADER_SIZE {
93            // Making WriteHeader call valid even when c.Raw
94            // is nil or len(c.Raw) is less than needed for header.
95            self.grow(CHANNEL_DATA_HEADER_SIZE);
96        }
97        self.raw[..CHANNEL_DATA_NUMBER_SIZE].copy_from_slice(&self.number.0.to_be_bytes());
98        self.raw[CHANNEL_DATA_NUMBER_SIZE..CHANNEL_DATA_HEADER_SIZE]
99            .copy_from_slice(&(self.data.len() as u16).to_be_bytes());
100    }
101
102    // is_channel_data returns true if buf looks like the ChannelData Message.
103    pub fn is_channel_data(buf: &[u8]) -> bool {
104        if buf.len() < CHANNEL_DATA_HEADER_SIZE {
105            return false;
106        }
107
108        if u16::from_be_bytes([
109            buf[CHANNEL_DATA_NUMBER_SIZE],
110            buf[CHANNEL_DATA_NUMBER_SIZE + 1],
111        ]) > buf[CHANNEL_DATA_HEADER_SIZE..].len() as u16
112        {
113            return false;
114        }
115
116        // Quick check for channel number.
117        let num = ChannelNumber(u16::from_be_bytes([buf[0], buf[1]]));
118        num.valid()
119    }
120}