Skip to main content

rtc_turn/proto/
rsrvtoken.rs

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