1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#[cfg(test)]
mod control_test;

use std::fmt;

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

/// Common helper for ICE-{CONTROLLED,CONTROLLING} and represents the so-called Tiebreaker number.
#[derive(Default, PartialEq, Eq, Debug, Copy, Clone)]
pub struct TieBreaker(pub u64);

pub(crate) const TIE_BREAKER_SIZE: usize = 8; // 64 bit

impl TieBreaker {
    /// Adds Tiebreaker value to m as t attribute.
    pub fn add_to_as(self, m: &mut Message, t: AttrType) -> Result<(), stun::Error> {
        let mut v = vec![0; TIE_BREAKER_SIZE];
        v.copy_from_slice(&self.0.to_be_bytes());
        m.add(t, &v);
        Ok(())
    }

    /// Decodes Tiebreaker value in message getting it as for t type.
    pub fn get_from_as(&mut self, m: &Message, t: AttrType) -> Result<(), stun::Error> {
        let v = m.get(t)?;
        check_size(t, v.len(), TIE_BREAKER_SIZE)?;
        self.0 = u64::from_be_bytes([v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]]);
        Ok(())
    }
}
/// Represents ICE-CONTROLLED attribute.
#[derive(Default, PartialEq, Eq, Debug, Copy, Clone)]
pub struct AttrControlled(pub u64);

impl Setter for AttrControlled {
    /// Adds ICE-CONTROLLED to message.
    fn add_to(&self, m: &mut Message) -> Result<(), stun::Error> {
        TieBreaker(self.0).add_to_as(m, ATTR_ICE_CONTROLLED)
    }
}

impl Getter for AttrControlled {
    /// Decodes ICE-CONTROLLED from message.
    fn get_from(&mut self, m: &Message) -> Result<(), stun::Error> {
        let mut t = TieBreaker::default();
        t.get_from_as(m, ATTR_ICE_CONTROLLED)?;
        self.0 = t.0;
        Ok(())
    }
}

/// Represents ICE-CONTROLLING attribute.
#[derive(Default, PartialEq, Eq, Debug, Copy, Clone)]
pub struct AttrControlling(pub u64);

impl Setter for AttrControlling {
    // add_to adds ICE-CONTROLLING to message.
    fn add_to(&self, m: &mut Message) -> Result<(), stun::Error> {
        TieBreaker(self.0).add_to_as(m, ATTR_ICE_CONTROLLING)
    }
}

impl Getter for AttrControlling {
    // get_from decodes ICE-CONTROLLING from message.
    fn get_from(&mut self, m: &Message) -> Result<(), stun::Error> {
        let mut t = TieBreaker::default();
        t.get_from_as(m, ATTR_ICE_CONTROLLING)?;
        self.0 = t.0;
        Ok(())
    }
}

/// Helper that wraps ICE-{CONTROLLED,CONTROLLING}.
#[derive(Default, PartialEq, Eq, Debug, Copy, Clone)]
pub struct AttrControl {
    role: Role,
    tie_breaker: TieBreaker,
}

impl Setter for AttrControl {
    // add_to adds ICE-CONTROLLED or ICE-CONTROLLING attribute depending on Role.
    fn add_to(&self, m: &mut Message) -> Result<(), stun::Error> {
        if self.role == Role::Controlling {
            self.tie_breaker.add_to_as(m, ATTR_ICE_CONTROLLING)
        } else {
            self.tie_breaker.add_to_as(m, ATTR_ICE_CONTROLLED)
        }
    }
}

impl Getter for AttrControl {
    // get_from decodes Role and Tiebreaker value from message.
    fn get_from(&mut self, m: &Message) -> Result<(), stun::Error> {
        if m.contains(ATTR_ICE_CONTROLLING) {
            self.role = Role::Controlling;
            return self.tie_breaker.get_from_as(m, ATTR_ICE_CONTROLLING);
        }
        if m.contains(ATTR_ICE_CONTROLLED) {
            self.role = Role::Controlled;
            return self.tie_breaker.get_from_as(m, ATTR_ICE_CONTROLLED);
        }

        Err(stun::Error::ErrAttributeNotFound)
    }
}

/// Represents ICE agent role, which can be controlling or controlled.
/// Possible ICE agent roles.
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub enum Role {
    Controlling,
    Controlled,
    Unspecified,
}

impl Default for Role {
    fn default() -> Self {
        Self::Controlling
    }
}

impl From<&str> for Role {
    fn from(raw: &str) -> Self {
        match raw {
            "controlling" => Self::Controlling,
            "controlled" => Self::Controlled,
            _ => Self::Unspecified,
        }
    }
}

impl fmt::Display for Role {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = match *self {
            Self::Controlling => "controlling",
            Self::Controlled => "controlled",
            Self::Unspecified => "unspecified",
        };
        write!(f, "{s}")
    }
}