candidate_host.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package ice
  4. import (
  5. "net"
  6. "strings"
  7. )
  8. // CandidateHost is a candidate of type host
  9. type CandidateHost struct {
  10. candidateBase
  11. network string
  12. }
  13. // CandidateHostConfig is the config required to create a new CandidateHost
  14. type CandidateHostConfig struct {
  15. CandidateID string
  16. Network string
  17. Address string
  18. Port int
  19. Component uint16
  20. Priority uint32
  21. Foundation string
  22. TCPType TCPType
  23. }
  24. // NewCandidateHost creates a new host candidate
  25. func NewCandidateHost(config *CandidateHostConfig) (*CandidateHost, error) {
  26. candidateID := config.CandidateID
  27. if candidateID == "" {
  28. candidateID = globalCandidateIDGenerator.Generate()
  29. }
  30. c := &CandidateHost{
  31. candidateBase: candidateBase{
  32. id: candidateID,
  33. address: config.Address,
  34. candidateType: CandidateTypeHost,
  35. component: config.Component,
  36. port: config.Port,
  37. tcpType: config.TCPType,
  38. foundationOverride: config.Foundation,
  39. priorityOverride: config.Priority,
  40. remoteCandidateCaches: map[AddrPort]Candidate{},
  41. },
  42. network: config.Network,
  43. }
  44. if !strings.HasSuffix(config.Address, ".local") {
  45. ip := net.ParseIP(config.Address)
  46. if ip == nil {
  47. return nil, ErrAddressParseFailed
  48. }
  49. if err := c.setIP(ip); err != nil {
  50. return nil, err
  51. }
  52. } else {
  53. // Until mDNS candidate is resolved assume it is UDPv4
  54. c.candidateBase.networkType = NetworkTypeUDP4
  55. }
  56. return c, nil
  57. }
  58. func (c *CandidateHost) setIP(ip net.IP) error {
  59. networkType, err := determineNetworkType(c.network, ip)
  60. if err != nil {
  61. return err
  62. }
  63. c.candidateBase.networkType = networkType
  64. c.candidateBase.resolvedAddr = createAddr(networkType, ip, c.port)
  65. return nil
  66. }