candidate_peer_reflexive.go 1.6 KB

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