turn_server/codec/channel_data.rs
1use bytes::{BufMut, BytesMut};
2
3use super::Error;
4
5/// The ChannelData Message
6///
7/// The ChannelData message is used to carry application data between the
8/// client and the server.
9///
10/// The Channel Number field specifies the number of the channel on which
11/// the data is traveling, and thus, the address of the peer that is
12/// sending or is to receive the data.
13///
14/// The Length field specifies the length in bytes of the application
15/// data field (i.e., it does not include the size of the ChannelData
16/// header). Note that 0 is a valid length.
17///
18/// The Application Data field carries the data the client is trying to
19/// send to the peer, or that the peer is sending to the client.
20pub struct ChannelData<'a> {
21 bytes: &'a [u8],
22 number: u16,
23}
24
25impl<'a> ChannelData<'a> {
26 pub fn new(number: u16, bytes: &'a [u8]) -> Self {
27 Self { number, bytes }
28 }
29
30 pub fn number(&self) -> u16 {
31 self.number
32 }
33
34 pub fn bytes(&self) -> &'a [u8] {
35 self.bytes
36 }
37
38 /// # Test
39 ///
40 /// ```
41 /// use bytes::{BufMut, BytesMut};
42 /// use turn_server::codec::channel_data::ChannelData;
43 ///
44 /// let data: [u8; 4] = [0x40, 0x00, 0x00, 0x40];
45 /// let mut bytes = BytesMut::with_capacity(1500);
46 ///
47 /// ChannelData::new(16384, &data[..]).encode(&mut bytes);
48 ///
49 /// let size = ChannelData::message_size(&bytes[..], false).unwrap();
50 ///
51 /// assert_eq!(size, 8);
52 /// ```
53 pub fn message_size(bytes: &[u8], is_tcp: bool) -> Result<usize, Error> {
54 if bytes.len() < 4 {
55 return Err(Error::InvalidInput);
56 }
57
58 if !(1..3).contains(&(bytes[0] >> 6)) {
59 return Err(Error::InvalidInput);
60 }
61
62 let mut size = (u16::from_be_bytes(bytes[2..4].try_into()?) + 4) as usize;
63 if is_tcp && (size % 4) > 0 {
64 size += 4 - (size % 4);
65 }
66
67 Ok(size)
68 }
69
70 /// # Test
71 ///
72 /// ```
73 /// use bytes::{BufMut, BytesMut};
74 /// use turn_server::codec::channel_data::ChannelData;
75 ///
76 /// let data: [u8; 4] = [0x40, 0x00, 0x00, 0x40];
77 /// let mut bytes = BytesMut::with_capacity(1500);
78 ///
79 /// ChannelData::new(16384, &data[..]).encode(&mut bytes);
80 ///
81 /// let ret = ChannelData::decode(&bytes[..]).unwrap();
82 ///
83 /// assert_eq!(ret.number(), 16384);
84 /// assert_eq!(ret.bytes(), &data[..]);
85 /// ```
86 pub fn encode(self, bytes: &mut BytesMut) {
87 bytes.clear();
88 bytes.put_u16(self.number);
89 bytes.put_u16(self.bytes.len() as u16);
90 bytes.extend_from_slice(self.bytes);
91 }
92
93 /// # Test
94 ///
95 /// ```
96 /// use bytes::{BufMut, BytesMut};
97 /// use turn_server::codec::channel_data::ChannelData;
98 ///
99 /// let data: [u8; 4] = [0x40, 0x00, 0x00, 0x40];
100 /// let mut bytes = BytesMut::with_capacity(1500);
101 ///
102 /// ChannelData::new(16384, &data[..]).encode(&mut bytes);
103 ///
104 /// let ret = ChannelData::decode(&bytes[..]).unwrap();
105 ///
106 /// assert_eq!(ret.number(), 16384);
107 /// assert_eq!(ret.bytes(), &data[..]);
108 /// ```
109 pub fn decode(bytes: &'a [u8]) -> Result<Self, Error> {
110 if bytes.len() < 4 {
111 return Err(Error::InvalidInput);
112 }
113
114 let number = u16::from_be_bytes(bytes[..2].try_into()?);
115 if !(0x4000..0xFFFF).contains(&number) {
116 return Err(Error::InvalidInput);
117 }
118
119 let size = u16::from_be_bytes(bytes[2..4].try_into()?) as usize;
120 if size > bytes.len() - 4 {
121 return Err(Error::InvalidInput);
122 }
123
124 Ok(Self {
125 bytes: &bytes[4..],
126 number,
127 })
128 }
129}