forked from gortc/ice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pair.go
256 lines (225 loc) · 5.89 KB
/
pair.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package ice
import (
"bytes"
"fmt"
"net"
)
func min(a, b int64) int64 {
if a < b {
return a
}
return b
}
func max(a, b int64) int64 {
if a > b {
return a
}
return b
}
// PairPriority computes Pair Priority as in RFC 8445 Section 6.1.2.3.
func PairPriority(controlling, controlled int) int64 {
var (
g = int64(controlling)
d = int64(controlled)
)
// pair priority = 2^32*MIN(G,D) + 2*MAX(G,D) + (G>D?1:0)
v := (1<<32)*min(g, d) + 2*max(g, d)
if g > d {
v++
}
return v
}
// Pair wraps two candidates, one is local, other is remote.
type Pair struct {
Local Candidate `json:"local"`
Remote Candidate `json:"remote"`
Priority int64 `json:"priority"`
Foundation []byte `json:"foundation"`
State PairState `json:"state"`
Nominated bool `json:"nominated"`
ComponentID int `json:"component_id"`
}
// Equal returns true if pair p equals to pair b.
func (p *Pair) Equal(b *Pair) bool {
if p.ComponentID != b.ComponentID {
return false
}
if p.Nominated != b.Nominated {
return false
}
if p.State != b.State {
return false
}
if p.Priority != b.Priority {
return false
}
if !p.Local.Equal(&b.Local) {
return false
}
if !p.Remote.Equal(&b.Remote) {
return false
}
if !bytes.Equal(p.Foundation, b.Foundation) {
return false
}
return true
}
// PairState as defined in RFC 8445 Section 6.1.2.6.
type PairState byte
// In returns true if s in states list.
func (s PairState) In(states ...PairState) bool {
for _, st := range states {
if st == s {
return true
}
}
return false
}
// UnmarshalText implements TextUnmarshaler.
func (s *PairState) UnmarshalText(text []byte) error {
for k, v := range pairStateToStr {
if string(text) == v {
*s = k
return nil
}
}
return fmt.Errorf("unknown pair state value %q", text)
}
// MarshalText implements TextMarshaler.
func (s PairState) MarshalText() ([]byte, error) {
return []byte(s.String()), nil
}
var pairStateToStr = map[PairState]string{
PairWaiting: "Waiting",
PairInProgress: "In-Progress",
PairSucceeded: "Succeeded",
PairFailed: "Failed",
PairFrozen: "Frozen",
}
func (s PairState) String() string { return pairStateToStr[s] }
const (
// PairFrozen state: A check for this pair has not been sent, and it cannot
// be sent until the pair is unfrozen and moved into the Waiting state.
PairFrozen PairState = iota
// PairInProgress state: A check has been sent for this pair, but the
// transaction is in progress.
PairInProgress
// PairSucceeded state: A check has been sent for this pair, and it produced
// a successful result.
PairSucceeded
// PairFailed state: A check has been sent for this pair, and it failed (a
// response to the check was never received, or a failure response was
// received).
PairFailed
// PairWaiting state: A check has not been sent for this pair, but the pair
// is not Frozen.
PairWaiting
)
// SetFoundation sets foundation, the combination of candidates foundations.
func (p *Pair) SetFoundation() {
f := make([]byte, foundationLength*2)
copy(f[:foundationLength], p.Local.Foundation)
copy(f[foundationLength:], p.Remote.Foundation)
p.Foundation = f
}
// SetPriority calculates and sets pair priority based on role and candidates.
func (p *Pair) SetPriority(role Role) {
var (
controlling = p.Local.Priority
controlled = p.Remote.Priority
)
if role == Controlled {
controlling, controlled = controlled, controlling
}
p.Priority = PairPriority(controlling, controlled)
}
// Pairs is ordered slice of Pair elements.
type Pairs []Pair
func (p Pairs) Len() int { return len(p) }
func (p Pairs) Less(i, j int) bool {
if p[i].Priority == p[j].Priority {
return p[i].ComponentID < p[j].ComponentID
}
return p[i].Priority > p[j].Priority
}
func (p Pairs) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func sameFamily(a, b net.IP) bool {
return len(a.To4()) == len(b.To4())
}
// NewPairs pairs each local candidate with each remote candidate for the same
// component of the same data stream with the same IP address family. Candidates
// should be sorted by priority in descending order, which is default order for
// the Candidates type. Populates only Local, Remote and ComponentID fields of Pair.
//
// See RFC 8445 Section 6.1.2.2.
func NewPairs(local, remote Candidates) Pairs {
p := make(Pairs, 0, 100)
for l := range local {
for r := range remote {
// Same data stream.
if local[l].ComponentID != remote[r].ComponentID {
continue
}
ipL, ipR := local[l].Addr.IP, remote[r].Addr.IP
// Same IP address family.
if !sameFamily(ipL, ipR) {
continue
}
if ipL.To4() == nil && ipL.IsLinkLocalUnicast() {
// IPv6 link-local addresses MUST NOT be paired with other
// than link-local addresses.
if !ipR.IsLinkLocalUnicast() {
continue
}
}
pair := Pair{
Local: local[l],
Remote: remote[r],
ComponentID: local[l].ComponentID,
}
pair.SetFoundation()
p = append(p, pair)
}
}
return p
}
const maxPairFoundationBytes = 64
type foundationKey [maxPairFoundationBytes]byte
type foundationSet map[foundationKey]struct{}
func getFoundationKey(f []byte) foundationKey {
k := foundationKey{}
copy(k[:], f)
return k
}
func assertFoundationLength(f []byte) {
if len(f) > maxPairFoundationBytes {
panic("length of foundation is greater that maximum")
}
}
func (s foundationSet) Contains(f []byte) bool {
assertFoundationLength(f)
_, ok := s[getFoundationKey(f)]
return ok
}
func (s foundationSet) Add(f []byte) {
assertFoundationLength(f)
s[getFoundationKey(f)] = struct{}{}
}
type pairKey struct {
LocalIP [net.IPv6len]byte
RemoteIP [net.IPv6len]byte
LocalPort int
RemotePort int
}
func (k pairKey) Equal(p *Pair) bool {
b := getPairKey(p)
return k == b
}
func getPairKey(p *Pair) pairKey {
k := pairKey{}
copy(k.LocalIP[:], p.Local.Addr.IP)
copy(k.RemoteIP[:], p.Remote.Addr.IP)
k.LocalPort = p.Local.Addr.Port
k.RemotePort = p.Remote.Addr.Port
return k
}