net.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package ice
  4. import (
  5. "net"
  6. "github.com/pion/logging"
  7. "github.com/pion/transport/v2"
  8. )
  9. // The conditions of invalidation written below are defined in
  10. // https://tools.ietf.org/html/rfc8445#section-5.1.1.1
  11. func isSupportedIPv6(ip net.IP) bool {
  12. if len(ip) != net.IPv6len ||
  13. isZeros(ip[0:12]) || // !(IPv4-compatible IPv6)
  14. ip[0] == 0xfe && ip[1]&0xc0 == 0xc0 || // !(IPv6 site-local unicast)
  15. ip.IsLinkLocalUnicast() ||
  16. ip.IsLinkLocalMulticast() {
  17. return false
  18. }
  19. return true
  20. }
  21. func isZeros(ip net.IP) bool {
  22. for i := 0; i < len(ip); i++ {
  23. if ip[i] != 0 {
  24. return false
  25. }
  26. }
  27. return true
  28. }
  29. func localInterfaces(n transport.Net, interfaceFilter func(string) bool, ipFilter func(net.IP) bool, networkTypes []NetworkType, includeLoopback bool) ([]net.IP, error) { //nolint:gocognit
  30. ips := []net.IP{}
  31. ifaces, err := n.Interfaces()
  32. if err != nil {
  33. return ips, err
  34. }
  35. var IPv4Requested, IPv6Requested bool
  36. for _, typ := range networkTypes {
  37. if typ.IsIPv4() {
  38. IPv4Requested = true
  39. }
  40. if typ.IsIPv6() {
  41. IPv6Requested = true
  42. }
  43. }
  44. for _, iface := range ifaces {
  45. if iface.Flags&net.FlagUp == 0 {
  46. continue // Interface down
  47. }
  48. if (iface.Flags&net.FlagLoopback != 0) && !includeLoopback {
  49. continue // Loopback interface
  50. }
  51. if interfaceFilter != nil && !interfaceFilter(iface.Name) {
  52. continue
  53. }
  54. addrs, err := iface.Addrs()
  55. if err != nil {
  56. continue
  57. }
  58. for _, addr := range addrs {
  59. var ip net.IP
  60. switch addr := addr.(type) {
  61. case *net.IPNet:
  62. ip = addr.IP
  63. case *net.IPAddr:
  64. ip = addr.IP
  65. }
  66. if ip == nil || (ip.IsLoopback() && !includeLoopback) {
  67. continue
  68. }
  69. if ipv4 := ip.To4(); ipv4 == nil {
  70. if !IPv6Requested {
  71. continue
  72. } else if !isSupportedIPv6(ip) {
  73. continue
  74. }
  75. } else if !IPv4Requested {
  76. continue
  77. }
  78. if ipFilter != nil && !ipFilter(ip) {
  79. continue
  80. }
  81. ips = append(ips, ip)
  82. }
  83. }
  84. return ips, nil
  85. }
  86. func listenUDPInPortRange(n transport.Net, log logging.LeveledLogger, portMax, portMin int, network string, lAddr *net.UDPAddr) (transport.UDPConn, error) {
  87. if (lAddr.Port != 0) || ((portMin == 0) && (portMax == 0)) {
  88. return n.ListenUDP(network, lAddr)
  89. }
  90. var i, j int
  91. i = portMin
  92. if i == 0 {
  93. i = 1
  94. }
  95. j = portMax
  96. if j == 0 {
  97. j = 0xFFFF
  98. }
  99. if i > j {
  100. return nil, ErrPort
  101. }
  102. portStart := globalMathRandomGenerator.Intn(j-i+1) + i
  103. portCurrent := portStart
  104. for {
  105. lAddr = &net.UDPAddr{IP: lAddr.IP, Port: portCurrent}
  106. c, e := n.ListenUDP(network, lAddr)
  107. if e == nil {
  108. return c, e //nolint:nilerr
  109. }
  110. log.Debugf("Failed to listen %s: %v", lAddr.String(), e)
  111. portCurrent++
  112. if portCurrent > j {
  113. portCurrent = i
  114. }
  115. if portCurrent == portStart {
  116. break
  117. }
  118. }
  119. return nil, ErrPort
  120. }