candidate_host.go 1.6 KB

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