-
Notifications
You must be signed in to change notification settings - Fork 160
/
icecontrol.go
90 lines (74 loc) · 2.56 KB
/
icecontrol.go
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
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package ice
import (
"encoding/binary"
"github.com/pion/stun/v3"
)
// tiebreaker is common helper for ICE-{CONTROLLED,CONTROLLING}
// and represents the so-called tiebreaker number.
type tiebreaker uint64
const tiebreakerSize = 8 // 64 bit
// AddToAs adds tiebreaker value to m as t attribute.
func (a tiebreaker) AddToAs(m *stun.Message, t stun.AttrType) error {
v := make([]byte, tiebreakerSize)
binary.BigEndian.PutUint64(v, uint64(a))
m.Add(t, v)
return nil
}
// GetFromAs decodes tiebreaker value in message getting it as for t type.
func (a *tiebreaker) GetFromAs(m *stun.Message, t stun.AttrType) error {
v, err := m.Get(t)
if err != nil {
return err
}
if err = stun.CheckSize(t, len(v), tiebreakerSize); err != nil {
return err
}
*a = tiebreaker(binary.BigEndian.Uint64(v))
return nil
}
// AttrControlled represents ICE-CONTROLLED attribute.
type AttrControlled uint64
// AddTo adds ICE-CONTROLLED to message.
func (c AttrControlled) AddTo(m *stun.Message) error {
return tiebreaker(c).AddToAs(m, stun.AttrICEControlled)
}
// GetFrom decodes ICE-CONTROLLED from message.
func (c *AttrControlled) GetFrom(m *stun.Message) error {
return (*tiebreaker)(c).GetFromAs(m, stun.AttrICEControlled)
}
// AttrControlling represents ICE-CONTROLLING attribute.
type AttrControlling uint64
// AddTo adds ICE-CONTROLLING to message.
func (c AttrControlling) AddTo(m *stun.Message) error {
return tiebreaker(c).AddToAs(m, stun.AttrICEControlling)
}
// GetFrom decodes ICE-CONTROLLING from message.
func (c *AttrControlling) GetFrom(m *stun.Message) error {
return (*tiebreaker)(c).GetFromAs(m, stun.AttrICEControlling)
}
// AttrControl is helper that wraps ICE-{CONTROLLED,CONTROLLING}.
type AttrControl struct {
Role Role
Tiebreaker uint64
}
// AddTo adds ICE-CONTROLLED or ICE-CONTROLLING attribute depending on Role.
func (c AttrControl) AddTo(m *stun.Message) error {
if c.Role == Controlling {
return tiebreaker(c.Tiebreaker).AddToAs(m, stun.AttrICEControlling)
}
return tiebreaker(c.Tiebreaker).AddToAs(m, stun.AttrICEControlled)
}
// GetFrom decodes Role and Tiebreaker value from message.
func (c *AttrControl) GetFrom(m *stun.Message) error {
if m.Contains(stun.AttrICEControlling) {
c.Role = Controlling
return (*tiebreaker)(&c.Tiebreaker).GetFromAs(m, stun.AttrICEControlling)
}
if m.Contains(stun.AttrICEControlled) {
c.Role = Controlled
return (*tiebreaker)(&c.Tiebreaker).GetFromAs(m, stun.AttrICEControlled)
}
return stun.ErrAttributeNotFound
}