forked from pion/ice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcandidate_base.go
235 lines (196 loc) · 5.44 KB
/
candidate_base.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
package ice
import (
"fmt"
"net"
"sync/atomic"
"time"
"github.com/pion/logging"
"github.com/pion/stun"
)
type candidateBase struct {
id string
networkType NetworkType
candidateType CandidateType
component uint16
address string
port int
relatedAddress *CandidateRelatedAddress
resolvedAddr *net.UDPAddr
lastSent atomic.Value
lastReceived atomic.Value
conn net.PacketConn
currAgent *Agent
closeCh chan struct{}
closedCh chan struct{}
}
// ID returns Candidate ID
func (c *candidateBase) ID() string {
return c.id
}
// Address returns Candidate Address
func (c *candidateBase) Address() string {
return c.address
}
// Port returns Candidate Port
func (c *candidateBase) Port() int {
return c.port
}
// Type returns candidate type
func (c *candidateBase) Type() CandidateType {
return c.candidateType
}
// NetworkType returns candidate NetworkType
func (c *candidateBase) NetworkType() NetworkType {
return c.networkType
}
// Component returns candidate component
func (c *candidateBase) Component() uint16 {
return c.component
}
// LocalPreference returns the local preference for this candidate
func (c *candidateBase) LocalPreference() uint16 {
return defaultLocalPreference
}
// RelatedAddress returns *CandidateRelatedAddress
func (c *candidateBase) RelatedAddress() *CandidateRelatedAddress {
return c.relatedAddress
}
// start runs the candidate using the provided connection
func (c *candidateBase) start(a *Agent, conn net.PacketConn) {
c.currAgent = a
c.conn = conn
c.closeCh = make(chan struct{})
c.closedCh = make(chan struct{})
go c.recvLoop()
}
func (c *candidateBase) recvLoop() {
defer func() {
close(c.closedCh)
}()
log := c.agent().log
buffer := make([]byte, receiveMTU)
for {
n, srcAddr, err := c.conn.ReadFrom(buffer)
if err != nil {
return
}
handleInboundCandidateMsg(c, buffer[:n], srcAddr, log)
}
}
func handleInboundCandidateMsg(c Candidate, buffer []byte, srcAddr net.Addr, log logging.LeveledLogger) {
if stun.IsMessage(buffer) {
m := &stun.Message{
Raw: make([]byte, len(buffer)),
}
// Explicitly copy raw buffer so Message can own the memory.
copy(m.Raw, buffer)
if err := m.Decode(); err != nil {
log.Warnf("Failed to handle decode ICE from %s to %s: %v", c.addr(), srcAddr, err)
return
}
err := c.agent().run(func(agent *Agent) {
agent.handleInbound(m, c, srcAddr)
})
if err != nil {
log.Warnf("Failed to handle message: %v", err)
}
return
}
isValidRemoteCandidate := make(chan bool, 1)
err := c.agent().run(func(agent *Agent) {
isValidRemoteCandidate <- agent.noSTUNSeen(c, srcAddr)
})
if err != nil {
log.Warnf("Failed to handle message: %v", err)
} else if !<-isValidRemoteCandidate {
log.Warnf("Discarded message from %s, not a valid remote candidate", c.addr())
}
// NOTE This will return packetio.ErrFull if the buffer ever manages to fill up.
if _, err := c.agent().buffer.Write(buffer); err != nil {
log.Warnf("failed to write packet")
}
}
// close stops the recvLoop
func (c *candidateBase) close() error {
if c.conn != nil {
// Unblock recvLoop
close(c.closeCh)
// Close the conn
err := c.conn.Close()
if err != nil {
return err
}
// Wait until the recvLoop is closed
<-c.closedCh
}
return nil
}
func (c *candidateBase) writeTo(raw []byte, dst Candidate) (int, error) {
n, err := c.conn.WriteTo(raw, dst.addr())
if err != nil {
return n, fmt.Errorf("failed to send packet: %v", err)
}
c.seen(true)
return n, nil
}
// Priority computes the priority for this ICE Candidate
func (c *candidateBase) Priority() uint32 {
// The local preference MUST be an integer from 0 (lowest preference) to
// 65535 (highest preference) inclusive. When there is only a single IP
// address, this value SHOULD be set to 65535. If there are multiple
// candidates for a particular component for a particular data stream
// that have the same type, the local preference MUST be unique for each
// one.
return (1<<24)*uint32(c.Type().Preference()) +
(1<<8)*uint32(c.LocalPreference()) +
uint32(256-c.Component())
}
// Equal is used to compare two candidateBases
func (c *candidateBase) Equal(other Candidate) bool {
return c.NetworkType() == other.NetworkType() &&
c.Type() == other.Type() &&
c.Address() == other.Address() &&
c.Port() == other.Port() &&
c.RelatedAddress().Equal(other.RelatedAddress())
}
// String makes the candidateBase printable
func (c *candidateBase) String() string {
return fmt.Sprintf("%s %s:%d%s", c.Type(), c.Address(), c.Port(), c.relatedAddress)
}
// LastReceived returns a time.Time indicating the last time
// this candidate was received
func (c *candidateBase) LastReceived() time.Time {
lastReceived := c.lastReceived.Load()
if lastReceived == nil {
return time.Time{}
}
return lastReceived.(time.Time)
}
func (c *candidateBase) setLastReceived(t time.Time) {
c.lastReceived.Store(t)
}
// LastSent returns a time.Time indicating the last time
// this candidate was sent
func (c *candidateBase) LastSent() time.Time {
lastSent := c.lastSent.Load()
if lastSent == nil {
return time.Time{}
}
return lastSent.(time.Time)
}
func (c *candidateBase) setLastSent(t time.Time) {
c.lastSent.Store(t)
}
func (c *candidateBase) seen(outbound bool) {
if outbound {
c.setLastSent(time.Now())
} else {
c.setLastReceived(time.Now())
}
}
func (c *candidateBase) addr() *net.UDPAddr {
return c.resolvedAddr
}
func (c *candidateBase) agent() *Agent {
return c.currAgent
}