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 stun::attributes::*;
use stun::checks::*;
use stun::message::*;
use std::fmt;
#[derive(Default, PartialEq, Debug, Copy, Clone)]
pub struct TieBreaker(pub u64);
pub(crate) const TIE_BREAKER_SIZE: usize = 8;
impl TieBreaker {
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 as u64).to_be_bytes());
m.add(t, &v);
Ok(())
}
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(())
}
}
#[derive(Default, PartialEq, Debug, Copy, Clone)]
pub struct AttrControlled(pub u64);
impl Setter for AttrControlled {
fn add_to(&self, m: &mut Message) -> Result<(), stun::Error> {
TieBreaker(self.0).add_to_as(m, ATTR_ICE_CONTROLLED)
}
}
impl Getter for AttrControlled {
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(())
}
}
#[derive(Default, PartialEq, Debug, Copy, Clone)]
pub struct AttrControlling(pub u64);
impl Setter for AttrControlling {
fn add_to(&self, m: &mut Message) -> Result<(), stun::Error> {
TieBreaker(self.0).add_to_as(m, ATTR_ICE_CONTROLLING)
}
}
impl Getter for AttrControlling {
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(())
}
}
#[derive(Default, PartialEq, Debug, Copy, Clone)]
pub struct AttrControl {
role: Role,
tie_breaker: TieBreaker,
}
impl Setter for AttrControl {
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 {
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)
}
}
#[derive(PartialEq, 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)
}
}