xref: /webrtc/turn/src/proto/reqtrans.rs (revision ffe74184)
1 #[cfg(test)]
2 mod reqtrans_test;
3 
4 use super::*;
5 
6 use std::fmt;
7 use stun::attributes::*;
8 use stun::checks::*;
9 use stun::message::*;
10 
11 // RequestedTransport represents REQUESTED-TRANSPORT attribute.
12 //
13 // This attribute is used by the client to request a specific transport
14 // protocol for the allocated transport address. RFC 5766 only allows the use of
15 // codepoint 17 (User Datagram protocol).
16 //
17 // RFC 5766 Section 14.7
18 #[derive(Default, Debug, PartialEq, Eq)]
19 pub struct RequestedTransport {
20     pub protocol: Protocol,
21 }
22 
23 impl fmt::Display for RequestedTransport {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result24     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25         write!(f, "protocol: {}", self.protocol)
26     }
27 }
28 
29 const REQUESTED_TRANSPORT_SIZE: usize = 4;
30 
31 impl Setter for RequestedTransport {
32     // AddTo adds REQUESTED-TRANSPORT to message.
add_to(&self, m: &mut Message) -> Result<(), stun::Error>33     fn add_to(&self, m: &mut Message) -> Result<(), stun::Error> {
34         let mut v = vec![0; REQUESTED_TRANSPORT_SIZE];
35         v[0] = self.protocol.0;
36         // b[1:4] is RFFU = 0.
37         // The RFFU field MUST be set to zero on transmission and MUST be
38         // ignored on reception. It is reserved for future uses.
39         m.add(ATTR_REQUESTED_TRANSPORT, &v);
40         Ok(())
41     }
42 }
43 
44 impl Getter for RequestedTransport {
45     // GetFrom decodes REQUESTED-TRANSPORT from message.
get_from(&mut self, m: &Message) -> Result<(), stun::Error>46     fn get_from(&mut self, m: &Message) -> Result<(), stun::Error> {
47         let v = m.get(ATTR_REQUESTED_TRANSPORT)?;
48 
49         check_size(ATTR_REQUESTED_TRANSPORT, v.len(), REQUESTED_TRANSPORT_SIZE)?;
50         self.protocol = Protocol(v[0]);
51         Ok(())
52     }
53 }
54