net.go 2.7 KB

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