candidate_server_reflexive.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package ice
  4. import "net"
  5. // CandidateServerReflexive ...
  6. type CandidateServerReflexive struct {
  7. candidateBase
  8. }
  9. // CandidateServerReflexiveConfig is the config required to create a new CandidateServerReflexive
  10. type CandidateServerReflexiveConfig struct {
  11. CandidateID string
  12. Network string
  13. Address string
  14. Port int
  15. Component uint16
  16. Priority uint32
  17. Foundation string
  18. RelAddr string
  19. RelPort int
  20. }
  21. // NewCandidateServerReflexive creates a new server reflective candidate
  22. func NewCandidateServerReflexive(config *CandidateServerReflexiveConfig) (*CandidateServerReflexive, error) {
  23. ip := net.ParseIP(config.Address)
  24. if ip == nil {
  25. return nil, ErrAddressParseFailed
  26. }
  27. networkType, err := determineNetworkType(config.Network, ip)
  28. if err != nil {
  29. return nil, err
  30. }
  31. candidateID := config.CandidateID
  32. if candidateID == "" {
  33. candidateID = globalCandidateIDGenerator.Generate()
  34. }
  35. return &CandidateServerReflexive{
  36. candidateBase: candidateBase{
  37. id: candidateID,
  38. networkType: networkType,
  39. candidateType: CandidateTypeServerReflexive,
  40. address: config.Address,
  41. port: config.Port,
  42. resolvedAddr: &net.UDPAddr{IP: ip, Port: config.Port},
  43. component: config.Component,
  44. foundationOverride: config.Foundation,
  45. priorityOverride: config.Priority,
  46. relatedAddress: &CandidateRelatedAddress{
  47. Address: config.RelAddr,
  48. Port: config.RelPort,
  49. },
  50. remoteCandidateCaches: map[AddrPort]Candidate{},
  51. },
  52. }, nil
  53. }