candidate_peer_reflexive.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Package ice ...
  2. //nolint:dupl
  3. package ice
  4. import "net"
  5. // CandidatePeerReflexive ...
  6. type CandidatePeerReflexive struct {
  7. candidateBase
  8. }
  9. // CandidatePeerReflexiveConfig is the config required to create a new CandidatePeerReflexive
  10. type CandidatePeerReflexiveConfig 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. // NewCandidatePeerReflexive creates a new peer reflective candidate
  22. func NewCandidatePeerReflexive(config *CandidatePeerReflexiveConfig) (*CandidatePeerReflexive, 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. candidateIDGenerator := newCandidateIDGenerator()
  33. if candidateID == "" {
  34. candidateID = candidateIDGenerator.Generate()
  35. }
  36. return &CandidatePeerReflexive{
  37. candidateBase: candidateBase{
  38. id: candidateID,
  39. networkType: networkType,
  40. candidateType: CandidateTypePeerReflexive,
  41. address: config.Address,
  42. port: config.Port,
  43. resolvedAddr: createAddr(networkType, ip, config.Port),
  44. component: config.Component,
  45. foundationOverride: config.Foundation,
  46. priorityOverride: config.Priority,
  47. relatedAddress: &CandidateRelatedAddress{
  48. Address: config.RelAddr,
  49. Port: config.RelPort,
  50. },
  51. },
  52. }, nil
  53. }