MCPcopy Create free account
hub / github.com/pion/ice / LocalPreference

Method LocalPreference

candidate_base.go:145–223  ·  view source on GitHub ↗

LocalPreference returns the local preference for this candidate.

()

Source from the content-addressed store, hash-verified

143
144// LocalPreference returns the local preference for this candidate.
145func (c *candidateBase) LocalPreference() uint16 { //nolint:cyclop
146 if c.candidateType == CandidateTypeRelay {
147 return c.relayLocalPreference
148 }
149
150 if c.NetworkType().IsTCP() {
151 // RFC 6544, section 4.2
152 //
153 // In Section 4.1.2.1 of [RFC5245], a recommended formula for UDP ICE
154 // candidate prioritization is defined. For TCP candidates, the same
155 // formula and candidate type preferences SHOULD be used, and the
156 // RECOMMENDED type preferences for the new candidate types defined in
157 // this document (see Section 5) are 105 for NAT-assisted candidates and
158 // 75 for UDP-tunneled candidates.
159 //
160 // (...)
161 //
162 // With TCP candidates, the local preference part of the recommended
163 // priority formula is updated to also include the directionality
164 // (active, passive, or simultaneous-open) of the TCP connection. The
165 // RECOMMENDED local preference is then defined as:
166 //
167 // local preference = (2^13) * direction-pref + other-pref
168 //
169 // The direction-pref MUST be between 0 and 7 (both inclusive), with 7
170 // being the most preferred. The other-pref MUST be between 0 and 8191
171 // (both inclusive), with 8191 being the most preferred. It is
172 // RECOMMENDED that the host, UDP-tunneled, and relayed TCP candidates
173 // have the direction-pref assigned as follows: 6 for active, 4 for
174 // passive, and 2 for S-O. For the NAT-assisted and server reflexive
175 // candidates, the RECOMMENDED values are: 6 for S-O, 4 for active, and
176 // 2 for passive.
177 //
178 // (...)
179 //
180 // If any two candidates have the same type-preference and direction-
181 // pref, they MUST have a unique other-pref. With this specification,
182 // this usually only happens with multi-homed hosts, in which case
183 // other-pref is the preference for the particular IP address from which
184 // the candidate was obtained. When there is only a single IP address,
185 // this value SHOULD be set to the maximum allowed value (8191).
186 var otherPref uint16 = 8191
187
188 directionPref := func() uint16 {
189 switch c.Type() {
190 case CandidateTypeHost, CandidateTypeRelay:
191 switch c.tcpType {
192 case TCPTypeActive:
193 return 6
194 case TCPTypePassive:
195 return 4
196 case TCPTypeSimultaneousOpen:
197 return 2
198 case TCPTypeUnspecified:
199 return 0
200 }
201 case CandidateTypePeerReflexive, CandidateTypeServerReflexive:
202 switch c.tcpType {

Callers 1

PriorityMethod · 0.95

Calls 3

NetworkTypeMethod · 0.95
TypeMethod · 0.95
IsTCPMethod · 0.80

Tested by

no test coverage detected