rtci-turn 0.5.0

Sans-I/O TURN client (RFC 8656, RFC 6062 TCP allocations) for WebRTC
Documentation
#[cfg(test)]
mod channnum_test;

use std::fmt;

use stun::attributes::*;
use stun::checks::*;
use stun::message::*;

use shared::error::Result;

// 16 bits of uint + 16 bits of RFFU = 0.
const CHANNEL_NUMBER_SIZE: usize = 4;

// See https://www.rfc-editor.org/rfc/rfc8656#section-12:
//
// 0x4000 through 0x4FFF: These values are the allowed channel
// numbers (4,096 possible values).
/// The lowest channel number a client may bind.
///
/// The range is chosen so ChannelData can be told apart from STUN messages on the same port.
pub const MIN_CHANNEL_NUMBER: u16 = 0x4000;
/// The highest channel number a client may bind.
pub const MAX_CHANNEL_NUMBER: u16 = 0x4FFF;

/// `ChannelNumber` represents `CHANNEL-NUMBER` attribute. Encoded as `u16`.
///
/// The `CHANNEL-NUMBER` attribute contains the number of the channel.
///
/// [RFC 8656 Section 18.1](https://www.rfc-editor.org/rfc/rfc8656#section-18.1).
#[derive(Default, Eq, PartialEq, Debug, Copy, Clone, Hash)]
pub struct ChannelNumber(pub u16);

impl fmt::Display for ChannelNumber {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl Setter for ChannelNumber {
    /// Adds `CHANNEL-NUMBER` to message.
    fn add_to(&self, m: &mut Message) -> Result<()> {
        let mut v = vec![0; CHANNEL_NUMBER_SIZE];
        v[..2].copy_from_slice(&self.0.to_be_bytes());
        // v[2:4] are zeroes (RFFU = 0)
        m.add(ATTR_CHANNEL_NUMBER, &v);
        Ok(())
    }
}

impl Getter for ChannelNumber {
    /// Decodes `CHANNEL-NUMBER` from message.
    fn get_from(&mut self, m: &Message) -> Result<()> {
        let v = m.get(ATTR_CHANNEL_NUMBER)?;

        check_size(ATTR_CHANNEL_NUMBER, v.len(), CHANNEL_NUMBER_SIZE)?;

        //_ = v[CHANNEL_NUMBER_SIZE-1] // asserting length
        self.0 = u16::from_be_bytes([v[0], v[1]]);
        // v[2:4] is RFFU and equals to 0.
        Ok(())
    }
}

impl ChannelNumber {
    /// Returns true if c in `[0x4000, 0x4FFF]`.
    fn is_channel_number_valid(&self) -> bool {
        self.0 >= MIN_CHANNEL_NUMBER && self.0 <= MAX_CHANNEL_NUMBER
    }

    /// returns `true` if channel number has correct value that complies
    /// [RFC 8656 Section 12](https://www.rfc-editor.org/rfc/rfc8656#section-12) range.
    pub fn valid(&self) -> bool {
        self.is_channel_number_valid()
    }
}