webrtc_turn/proto/
rsrvtoken.rs

1#[cfg(test)]
2mod rsrvtoken_test;
3
4use stun::attributes::*;
5use stun::checks::*;
6use stun::message::*;
7
8use util::Error;
9
10// ReservationToken represents RESERVATION-TOKEN attribute.
11//
12// The RESERVATION-TOKEN attribute contains a token that uniquely
13// identifies a relayed transport address being held in reserve by the
14// server. The server includes this attribute in a success response to
15// tell the client about the token, and the client includes this
16// attribute in a subsequent Allocate request to request the server use
17// that relayed transport address for the allocation.
18//
19// RFC 5766 Section 14.9
20#[derive(Debug, Default, PartialEq)]
21pub struct ReservationToken(pub Vec<u8>);
22
23const RESERVATION_TOKEN_SIZE: usize = 8; // 8 bytes
24
25impl Setter for ReservationToken {
26    // AddTo adds RESERVATION-TOKEN to message.
27    fn add_to(&self, m: &mut Message) -> Result<(), Error> {
28        check_size(ATTR_RESERVATION_TOKEN, self.0.len(), RESERVATION_TOKEN_SIZE)?;
29        m.add(ATTR_RESERVATION_TOKEN, &self.0);
30        Ok(())
31    }
32}
33
34impl Getter for ReservationToken {
35    // GetFrom decodes RESERVATION-TOKEN from message.
36    fn get_from(&mut self, m: &Message) -> Result<(), Error> {
37        let v = m.get(ATTR_RESERVATION_TOKEN)?;
38        check_size(ATTR_RESERVATION_TOKEN, v.len(), RESERVATION_TOKEN_SIZE)?;
39        self.0 = v;
40        Ok(())
41    }
42}