selection.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package ice
  4. import (
  5. "net"
  6. "time"
  7. "github.com/pion/logging"
  8. "github.com/pion/stun"
  9. )
  10. type pairCandidateSelector interface {
  11. Start()
  12. ContactCandidates()
  13. PingCandidate(local, remote Candidate)
  14. HandleSuccessResponse(m *stun.Message, local, remote Candidate, remoteAddr net.Addr)
  15. HandleBindingRequest(m *stun.Message, local, remote Candidate)
  16. }
  17. type controllingSelector struct {
  18. startTime time.Time
  19. agent *Agent
  20. nominatedPair *CandidatePair
  21. log logging.LeveledLogger
  22. }
  23. func (s *controllingSelector) Start() {
  24. s.startTime = time.Now()
  25. s.nominatedPair = nil
  26. }
  27. func (s *controllingSelector) isNominatable(c Candidate) bool {
  28. switch {
  29. case c.Type() == CandidateTypeHost:
  30. return time.Since(s.startTime).Nanoseconds() > s.agent.hostAcceptanceMinWait.Nanoseconds()
  31. case c.Type() == CandidateTypeServerReflexive:
  32. return time.Since(s.startTime).Nanoseconds() > s.agent.srflxAcceptanceMinWait.Nanoseconds()
  33. case c.Type() == CandidateTypePeerReflexive:
  34. return time.Since(s.startTime).Nanoseconds() > s.agent.prflxAcceptanceMinWait.Nanoseconds()
  35. case c.Type() == CandidateTypeRelay:
  36. return time.Since(s.startTime).Nanoseconds() > s.agent.relayAcceptanceMinWait.Nanoseconds()
  37. }
  38. s.log.Errorf("Invalid candidate type: %s", c.Type())
  39. return false
  40. }
  41. func (s *controllingSelector) ContactCandidates() {
  42. switch {
  43. case s.agent.getSelectedPair() != nil:
  44. if s.agent.validateSelectedPair() {
  45. s.log.Trace("checking keepalive")
  46. s.agent.checkKeepalive()
  47. }
  48. case s.nominatedPair != nil:
  49. s.nominatePair(s.nominatedPair)
  50. default:
  51. p := s.agent.getBestValidCandidatePair()
  52. if p != nil && s.isNominatable(p.Local) && s.isNominatable(p.Remote) {
  53. s.log.Tracef("Nominatable pair found, nominating (%s, %s)", p.Local.String(), p.Remote.String())
  54. p.nominated = true
  55. s.nominatedPair = p
  56. s.nominatePair(p)
  57. return
  58. }
  59. s.agent.pingAllCandidates()
  60. }
  61. }
  62. func (s *controllingSelector) nominatePair(pair *CandidatePair) {
  63. // The controlling agent MUST include the USE-CANDIDATE attribute in
  64. // order to nominate a candidate pair (Section 8.1.1). The controlled
  65. // agent MUST NOT include the USE-CANDIDATE attribute in a Binding
  66. // request.
  67. msg, err := stun.Build(stun.BindingRequest, stun.TransactionID,
  68. stun.NewUsername(s.agent.remoteUfrag+":"+s.agent.localUfrag),
  69. UseCandidate(),
  70. AttrControlling(s.agent.tieBreaker),
  71. PriorityAttr(pair.Local.Priority()),
  72. stun.NewShortTermIntegrity(s.agent.remotePwd),
  73. stun.Fingerprint,
  74. )
  75. if err != nil {
  76. s.log.Error(err.Error())
  77. return
  78. }
  79. s.log.Tracef("ping STUN (nominate candidate pair) from %s to %s", pair.Local.String(), pair.Remote.String())
  80. s.agent.sendBindingRequest(msg, pair.Local, pair.Remote)
  81. }
  82. func (s *controllingSelector) HandleBindingRequest(m *stun.Message, local, remote Candidate) {
  83. s.agent.sendBindingSuccess(m, local, remote)
  84. p := s.agent.findPair(local, remote)
  85. if p == nil {
  86. s.agent.addPair(local, remote)
  87. return
  88. }
  89. if p.state == CandidatePairStateSucceeded && s.nominatedPair == nil && s.agent.getSelectedPair() == nil {
  90. bestPair := s.agent.getBestAvailableCandidatePair()
  91. if bestPair == nil {
  92. s.log.Tracef("No best pair available")
  93. } else if bestPair.equal(p) && s.isNominatable(p.Local) && s.isNominatable(p.Remote) {
  94. s.log.Tracef("The candidate (%s, %s) is the best candidate available, marking it as nominated",
  95. p.Local.String(), p.Remote.String())
  96. s.nominatedPair = p
  97. s.nominatePair(p)
  98. }
  99. }
  100. }
  101. func (s *controllingSelector) HandleSuccessResponse(m *stun.Message, local, remote Candidate, remoteAddr net.Addr) {
  102. ok, pendingRequest := s.agent.handleInboundBindingSuccess(m.TransactionID)
  103. if !ok {
  104. s.log.Warnf("Discard message from (%s), unknown TransactionID 0x%x", remote, m.TransactionID)
  105. return
  106. }
  107. transactionAddr := pendingRequest.destination
  108. // Assert that NAT is not symmetric
  109. // https://tools.ietf.org/html/rfc8445#section-7.2.5.2.1
  110. if !addrEqual(transactionAddr, remoteAddr) {
  111. s.log.Debugf("Discard message: transaction source and destination does not match expected(%s), actual(%s)", transactionAddr, remote)
  112. return
  113. }
  114. s.log.Tracef("inbound STUN (SuccessResponse) from %s to %s", remote.String(), local.String())
  115. p := s.agent.findPair(local, remote)
  116. if p == nil {
  117. // This shouldn't happen
  118. s.log.Error("Success response from invalid candidate pair")
  119. return
  120. }
  121. p.state = CandidatePairStateSucceeded
  122. s.log.Tracef("Found valid candidate pair: %s", p)
  123. if pendingRequest.isUseCandidate && s.agent.getSelectedPair() == nil {
  124. s.agent.setSelectedPair(p)
  125. }
  126. }
  127. func (s *controllingSelector) PingCandidate(local, remote Candidate) {
  128. msg, err := stun.Build(stun.BindingRequest, stun.TransactionID,
  129. stun.NewUsername(s.agent.remoteUfrag+":"+s.agent.localUfrag),
  130. AttrControlling(s.agent.tieBreaker),
  131. PriorityAttr(local.Priority()),
  132. stun.NewShortTermIntegrity(s.agent.remotePwd),
  133. stun.Fingerprint,
  134. )
  135. if err != nil {
  136. s.log.Error(err.Error())
  137. return
  138. }
  139. s.agent.sendBindingRequest(msg, local, remote)
  140. }
  141. type controlledSelector struct {
  142. agent *Agent
  143. log logging.LeveledLogger
  144. }
  145. func (s *controlledSelector) Start() {
  146. }
  147. func (s *controlledSelector) ContactCandidates() {
  148. if s.agent.getSelectedPair() != nil {
  149. if s.agent.validateSelectedPair() {
  150. s.log.Trace("checking keepalive")
  151. s.agent.checkKeepalive()
  152. }
  153. } else {
  154. s.agent.pingAllCandidates()
  155. }
  156. }
  157. func (s *controlledSelector) PingCandidate(local, remote Candidate) {
  158. msg, err := stun.Build(stun.BindingRequest, stun.TransactionID,
  159. stun.NewUsername(s.agent.remoteUfrag+":"+s.agent.localUfrag),
  160. AttrControlled(s.agent.tieBreaker),
  161. PriorityAttr(local.Priority()),
  162. stun.NewShortTermIntegrity(s.agent.remotePwd),
  163. stun.Fingerprint,
  164. )
  165. if err != nil {
  166. s.log.Error(err.Error())
  167. return
  168. }
  169. s.agent.sendBindingRequest(msg, local, remote)
  170. }
  171. func (s *controlledSelector) HandleSuccessResponse(m *stun.Message, local, remote Candidate, remoteAddr net.Addr) {
  172. //nolint:godox
  173. // TODO according to the standard we should specifically answer a failed nomination:
  174. // https://tools.ietf.org/html/rfc8445#section-7.3.1.5
  175. // If the controlled agent does not accept the request from the
  176. // controlling agent, the controlled agent MUST reject the nomination
  177. // request with an appropriate error code response (e.g., 400)
  178. // [RFC5389].
  179. ok, pendingRequest := s.agent.handleInboundBindingSuccess(m.TransactionID)
  180. if !ok {
  181. s.log.Warnf("Discard message from (%s), unknown TransactionID 0x%x", remote, m.TransactionID)
  182. return
  183. }
  184. transactionAddr := pendingRequest.destination
  185. // Assert that NAT is not symmetric
  186. // https://tools.ietf.org/html/rfc8445#section-7.2.5.2.1
  187. if !addrEqual(transactionAddr, remoteAddr) {
  188. s.log.Debugf("Discard message: transaction source and destination does not match expected(%s), actual(%s)", transactionAddr, remote)
  189. return
  190. }
  191. s.log.Tracef("inbound STUN (SuccessResponse) from %s to %s", remote.String(), local.String())
  192. p := s.agent.findPair(local, remote)
  193. if p == nil {
  194. // This shouldn't happen
  195. s.log.Error("Success response from invalid candidate pair")
  196. return
  197. }
  198. p.state = CandidatePairStateSucceeded
  199. s.log.Tracef("Found valid candidate pair: %s", p)
  200. if p.nominateOnBindingSuccess {
  201. if selectedPair := s.agent.getSelectedPair(); selectedPair == nil ||
  202. (selectedPair != p && selectedPair.priority() <= p.priority()) {
  203. s.agent.setSelectedPair(p)
  204. } else if selectedPair != p {
  205. s.log.Tracef("ignore nominate new pair %s, already nominated pair %s", p, selectedPair)
  206. }
  207. }
  208. }
  209. func (s *controlledSelector) HandleBindingRequest(m *stun.Message, local, remote Candidate) {
  210. useCandidate := m.Contains(stun.AttrUseCandidate)
  211. p := s.agent.findPair(local, remote)
  212. if p == nil {
  213. p = s.agent.addPair(local, remote)
  214. }
  215. if useCandidate {
  216. // https://tools.ietf.org/html/rfc8445#section-7.3.1.5
  217. if p.state == CandidatePairStateSucceeded {
  218. // If the state of this pair is Succeeded, it means that the check
  219. // previously sent by this pair produced a successful response and
  220. // generated a valid pair (Section 7.2.5.3.2). The agent sets the
  221. // nominated flag value of the valid pair to true.
  222. if selectedPair := s.agent.getSelectedPair(); selectedPair == nil ||
  223. (selectedPair != p && selectedPair.priority() <= p.priority()) {
  224. s.agent.setSelectedPair(p)
  225. } else if selectedPair != p {
  226. s.log.Tracef("ignore nominate new pair %s, already nominated pair %s", p, selectedPair)
  227. }
  228. } else {
  229. // If the received Binding request triggered a new check to be
  230. // enqueued in the triggered-check queue (Section 7.3.1.4), once the
  231. // check is sent and if it generates a successful response, and
  232. // generates a valid pair, the agent sets the nominated flag of the
  233. // pair to true. If the request fails (Section 7.2.5.2), the agent
  234. // MUST remove the candidate pair from the valid list, set the
  235. // candidate pair state to Failed, and set the checklist state to
  236. // Failed.
  237. p.nominateOnBindingSuccess = true
  238. }
  239. }
  240. s.agent.sendBindingSuccess(m, local, remote)
  241. s.PingCandidate(local, remote)
  242. }
  243. type liteSelector struct {
  244. pairCandidateSelector
  245. }
  246. // A lite selector should not contact candidates
  247. func (s *liteSelector) ContactCandidates() {
  248. if _, ok := s.pairCandidateSelector.(*controllingSelector); ok {
  249. //nolint:godox
  250. // https://github.com/pion/ice/issues/96
  251. // TODO: implement lite controlling agent. For now falling back to full agent.
  252. // This only happens if both peers are lite. See RFC 8445 S6.1.1 and S6.2
  253. s.pairCandidateSelector.ContactCandidates()
  254. } else if v, ok := s.pairCandidateSelector.(*controlledSelector); ok {
  255. v.agent.validateSelectedPair()
  256. }
  257. }