Skip to content

Commit 4cc9c98

Browse files
committed
Minor style fixes for active TCP candidate support
1 parent 1d502ca commit 4cc9c98

File tree

5 files changed

+64
-54
lines changed

5 files changed

+64
-54
lines changed

agent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ func (a *Agent) addPair(local, remote Candidate) *CandidatePair {
623623
a.log.Errorf("Failed to cast local candidate to CandidateHost")
624624
return nil
625625
}
626-
localCandidateHost.port = localAddress.Port // this causes a data race with candidateBase.Port()
626+
localCandidateHost.port = localAddress.Port // This causes a data race with candidateBase.Port()
627627
local.start(a, packetConn, a.startedCh)
628628
}
629629
p := newCandidatePair(local, remote, a.isControlling)

agent_active_tcp_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func TestAgentActiveTCP(t *testing.T) {
4848
listenIPAddress net.IP
4949
selectedPairNetworkType string
5050
}
51+
5152
testCases := []testCase{
5253
{
5354
name: "TCP4 connection",
@@ -56,29 +57,28 @@ func TestAgentActiveTCP(t *testing.T) {
5657
selectedPairNetworkType: tcp,
5758
},
5859
{
59-
name: "UDP is preferred over TCP4", // fails some time
60+
name: "UDP is preferred over TCP4", // This fails some time
6061
networkTypes: supportedNetworkTypes(),
6162
listenIPAddress: getLocalIPAddress(t, NetworkTypeTCP4),
6263
selectedPairNetworkType: udp,
6364
},
6465
}
6566

6667
if ipv6Available(t) {
67-
tcpv6Cases := []testCase{
68-
{
68+
testCases = append(testCases,
69+
testCase{
6970
name: "TCP6 connection",
7071
networkTypes: []NetworkType{NetworkTypeTCP6},
7172
listenIPAddress: getLocalIPAddress(t, NetworkTypeTCP6),
7273
selectedPairNetworkType: tcp,
7374
},
74-
{
75-
name: "UDP is preferred over TCP6", // fails some time
75+
testCase{
76+
name: "UDP is preferred over TCP6", // This fails some time
7677
networkTypes: supportedNetworkTypes(),
7778
listenIPAddress: getLocalIPAddress(t, NetworkTypeTCP6),
7879
selectedPairNetworkType: udp,
7980
},
80-
}
81-
testCases = append(testCases, tcpv6Cases...)
81+
)
8282
}
8383

8484
for _, testCase := range testCases {

candidate_base.go

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,44 @@ func (c *candidateBase) LocalPreference() uint16 {
188188
return defaultLocalPreference
189189
}
190190

191+
// TypePreference returns the type preference for this candidate
192+
func (c *candidateBase) TypePreference() uint16 {
193+
pref := c.Type().Preference()
194+
if pref == 0 {
195+
return 0
196+
}
197+
198+
if c.NetworkType().IsTCP() {
199+
var tcpPriorityOffset uint16 = defaultTCPPriorityOffset
200+
if c.agent() != nil {
201+
tcpPriorityOffset = c.agent().tcpPriorityOffset
202+
}
203+
204+
pref -= tcpPriorityOffset
205+
}
206+
207+
return pref
208+
}
209+
210+
// Priority computes the priority for this ICE Candidate
211+
// See: https://www.rfc-editor.org/rfc/rfc8445#section-5.1.2.1
212+
func (c *candidateBase) Priority() uint32 {
213+
if c.priorityOverride != 0 {
214+
return c.priorityOverride
215+
}
216+
217+
// The local preference MUST be an integer from 0 (lowest preference) to
218+
// 65535 (highest preference) inclusive. When there is only a single IP
219+
// address, this value SHOULD be set to 65535. If there are multiple
220+
// candidates for a particular component for a particular data stream
221+
// that have the same type, the local preference MUST be unique for each
222+
// one.
223+
224+
return (1<<24)*uint32(c.TypePreference()) +
225+
(1<<8)*uint32(c.LocalPreference()) +
226+
(1<<0)*uint32(256-c.Component())
227+
}
228+
191229
// RelatedAddress returns *CandidateRelatedAddress
192230
func (c *candidateBase) RelatedAddress() *CandidateRelatedAddress {
193231
return c.relatedAddress
@@ -343,29 +381,6 @@ func (c *candidateBase) writeTo(raw []byte, dst Candidate) (int, error) {
343381
return n, nil
344382
}
345383

346-
// Priority computes the priority for this ICE Candidate
347-
func (c *candidateBase) Priority() uint32 {
348-
if c.priorityOverride != 0 {
349-
return c.priorityOverride
350-
}
351-
352-
// The local preference MUST be an integer from 0 (lowest preference) to
353-
// 65535 (highest preference) inclusive. When there is only a single IP
354-
// address, this value SHOULD be set to 65535. If there are multiple
355-
// candidates for a particular component for a particular data stream
356-
// that have the same type, the local preference MUST be unique for each
357-
// one.
358-
359-
var tcpPriorityOffset uint16 = defaultTCPPriorityOffset
360-
if c.agent() != nil {
361-
tcpPriorityOffset = c.agent().tcpPriorityOffset
362-
}
363-
364-
return (1<<24)*uint32(c.Type().Preference(c.networkType, tcpPriorityOffset)) +
365-
(1<<8)*uint32(c.LocalPreference()) +
366-
uint32(256-c.Component())
367-
}
368-
369384
// Equal is used to compare two candidateBases
370385
func (c *candidateBase) Equal(other Candidate) bool {
371386
return c.NetworkType() == other.NetworkType() &&

candidatetype.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,19 @@ func (c CandidateType) String() string {
3838
// The RECOMMENDED values are 126 for host candidates, 100
3939
// for server reflexive candidates, 110 for peer reflexive candidates,
4040
// and 0 for relayed candidates.
41-
func (c CandidateType) Preference(networkType NetworkType, tcpOffset uint16) uint16 {
42-
var result uint16
41+
func (c CandidateType) Preference() uint16 {
4342
switch c {
4443
case CandidateTypeHost:
45-
result = 126
44+
return 126
4645
case CandidateTypePeerReflexive:
47-
result = 110
46+
return 110
4847
case CandidateTypeServerReflexive:
49-
result = 100
48+
return 100
5049
case CandidateTypeRelay, CandidateTypeUnspecified:
5150
return 0
5251
default:
5352
return 0
5453
}
55-
if networkType.IsTCP() {
56-
return result - tcpOffset
57-
}
58-
return result
5954
}
6055

6156
func containsCandidateType(candidateType CandidateType, candidateTypeList []CandidateType) bool {

gather.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const (
2525
stunGatherTimeout = time.Second * 5
2626
)
2727

28-
type connAndPort struct {
28+
type connConfig struct {
2929
conn net.PacketConn
3030
port int
3131
tcpType TCPType
@@ -161,18 +161,18 @@ func (a *Agent) gatherCandidatesLocal(ctx context.Context, networkTypes []Networ
161161
}
162162

163163
for network := range networks {
164-
var connectionConfigurations []connAndPort
164+
var connConfigs []connConfig
165165

166166
switch network {
167167
case tcp:
168168
// Handle ICE TCP active mode
169-
connectionConfigurations = append(connectionConfigurations, connAndPort{nil, 0, TCPTypeActive})
169+
connConfigs = append(connConfigs, connConfig{nil, 0, TCPTypeActive})
170170

171171
// Handle ICE TCP passive mode
172172
if a.tcpMux != nil {
173-
connectionConfigurations = a.getTCPMuxConnections(mappedIP, ip, network, connectionConfigurations)
173+
connConfigs = a.getTCPMuxConns(mappedIP, ip, network, connConfigs)
174174
}
175-
if len(connectionConfigurations) == 0 {
175+
if len(connConfigs) == 0 {
176176
// Didn't succeed with any, try the next network.
177177
continue
178178
}
@@ -186,36 +186,36 @@ func (a *Agent) gatherCandidatesLocal(ctx context.Context, networkTypes []Networ
186186
}
187187

188188
if udpConn, ok := conn.LocalAddr().(*net.UDPAddr); ok {
189-
connectionConfigurations = append(connectionConfigurations, connAndPort{conn, udpConn.Port, TCPTypeUnspecified})
189+
connConfigs = append(connConfigs, connConfig{conn, udpConn.Port, TCPTypeUnspecified})
190190
} else {
191191
a.log.Warnf("Failed to get port of UDPAddr from ListenUDPInPortRange: %s %s %s", network, ip, a.localUfrag)
192192
continue
193193
}
194194
}
195195

196-
for _, connectionConfiguration := range connectionConfigurations {
196+
for _, connConfig := range connConfigs {
197197
hostConfig := CandidateHostConfig{
198198
Network: network,
199199
Address: address,
200-
Port: connectionConfiguration.port,
200+
Port: connConfig.port,
201201
Component: ComponentRTP,
202-
TCPType: connectionConfiguration.tcpType,
202+
TCPType: connConfig.tcpType,
203203
}
204204

205205
c, err := NewCandidateHost(&hostConfig)
206206
if err != nil {
207-
closeConnAndLog(connectionConfiguration.conn, a.log, "failed to create host candidate: %s %s %d: %v", network, mappedIP, connectionConfiguration.port, err)
207+
closeConnAndLog(connConfig.conn, a.log, "failed to create host candidate: %s %s %d: %v", network, mappedIP, connConfig.port, err)
208208
continue
209209
}
210210

211211
if a.mDNSMode == MulticastDNSModeQueryAndGather {
212212
if err = c.setIP(ip); err != nil {
213-
closeConnAndLog(connectionConfiguration.conn, a.log, "failed to create host candidate: %s %s %d: %v", network, mappedIP, connectionConfiguration.port, err)
213+
closeConnAndLog(connConfig.conn, a.log, "failed to create host candidate: %s %s %d: %v", network, mappedIP, connConfig.port, err)
214214
continue
215215
}
216216
}
217217

218-
if err := a.addCandidate(ctx, c, connectionConfiguration.conn); err != nil {
218+
if err := a.addCandidate(ctx, c, connConfig.conn); err != nil {
219219
if closeErr := c.close(); closeErr != nil {
220220
a.log.Warnf("Failed to close candidate: %v", closeErr)
221221
}
@@ -226,7 +226,7 @@ func (a *Agent) gatherCandidatesLocal(ctx context.Context, networkTypes []Networ
226226
}
227227
}
228228

229-
func (a *Agent) getTCPMuxConnections(mappedIP net.IP, ip net.IP, network string, conns []connAndPort) []connAndPort {
229+
func (a *Agent) getTCPMuxConns(mappedIP net.IP, ip net.IP, network string, conns []connConfig) []connConfig {
230230
var muxConns []net.PacketConn
231231
if multi, ok := a.tcpMux.(AllConnsGetter); ok {
232232
a.log.Debugf("GetAllConns by ufrag: %s", a.localUfrag)
@@ -249,7 +249,7 @@ func (a *Agent) getTCPMuxConnections(mappedIP net.IP, ip net.IP, network string,
249249
// Extract the port for each PacketConn we got.
250250
for _, conn := range muxConns {
251251
if tcpConn, ok := conn.LocalAddr().(*net.TCPAddr); ok {
252-
conns = append(conns, connAndPort{conn, tcpConn.Port, TCPTypePassive})
252+
conns = append(conns, connConfig{conn, tcpConn.Port, TCPTypePassive})
253253
} else {
254254
a.log.Warnf("Failed to get port of connection from TCPMux: %s %s %s", network, ip, a.localUfrag)
255255
}

0 commit comments

Comments
 (0)