netutil.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package netutil implements network-related utility functions.
  15. package netutil
  16. import (
  17. "context"
  18. "net"
  19. "net/url"
  20. "reflect"
  21. "sort"
  22. "time"
  23. "github.com/coreos/etcd/pkg/types"
  24. "github.com/coreos/pkg/capnslog"
  25. )
  26. var (
  27. plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "pkg/netutil")
  28. // indirection for testing
  29. resolveTCPAddr = resolveTCPAddrDefault
  30. )
  31. const retryInterval = time.Second
  32. // taken from go's ResolveTCP code but uses configurable ctx
  33. func resolveTCPAddrDefault(ctx context.Context, addr string) (*net.TCPAddr, error) {
  34. host, port, serr := net.SplitHostPort(addr)
  35. if serr != nil {
  36. return nil, serr
  37. }
  38. portnum, perr := net.DefaultResolver.LookupPort(ctx, "tcp", port)
  39. if perr != nil {
  40. return nil, perr
  41. }
  42. var ips []net.IPAddr
  43. if ip := net.ParseIP(host); ip != nil {
  44. ips = []net.IPAddr{{IP: ip}}
  45. } else {
  46. // Try as a DNS name.
  47. ipss, err := net.DefaultResolver.LookupIPAddr(ctx, host)
  48. if err != nil {
  49. return nil, err
  50. }
  51. ips = ipss
  52. }
  53. // randomize?
  54. ip := ips[0]
  55. return &net.TCPAddr{IP: ip.IP, Port: portnum, Zone: ip.Zone}, nil
  56. }
  57. // resolveTCPAddrs is a convenience wrapper for net.ResolveTCPAddr.
  58. // resolveTCPAddrs return a new set of url.URLs, in which all DNS hostnames
  59. // are resolved.
  60. func resolveTCPAddrs(ctx context.Context, urls [][]url.URL) ([][]url.URL, error) {
  61. newurls := make([][]url.URL, 0)
  62. for _, us := range urls {
  63. nus := make([]url.URL, len(us))
  64. for i, u := range us {
  65. nu, err := url.Parse(u.String())
  66. if err != nil {
  67. return nil, err
  68. }
  69. nus[i] = *nu
  70. }
  71. for i, u := range nus {
  72. h, err := resolveURL(ctx, u)
  73. if err != nil {
  74. return nil, err
  75. }
  76. if h != "" {
  77. nus[i].Host = h
  78. }
  79. }
  80. newurls = append(newurls, nus)
  81. }
  82. return newurls, nil
  83. }
  84. func resolveURL(ctx context.Context, u url.URL) (string, error) {
  85. for ctx.Err() == nil {
  86. host, _, err := net.SplitHostPort(u.Host)
  87. if err != nil {
  88. plog.Errorf("could not parse url %s during tcp resolving", u.Host)
  89. return "", err
  90. }
  91. if host == "localhost" || net.ParseIP(host) != nil {
  92. return "", nil
  93. }
  94. tcpAddr, err := resolveTCPAddr(ctx, u.Host)
  95. if err == nil {
  96. plog.Infof("resolving %s to %s", u.Host, tcpAddr.String())
  97. return tcpAddr.String(), nil
  98. }
  99. plog.Warningf("failed resolving host %s (%v); retrying in %v", u.Host, err, retryInterval)
  100. select {
  101. case <-ctx.Done():
  102. plog.Errorf("could not resolve host %s", u.Host)
  103. return "", err
  104. case <-time.After(retryInterval):
  105. }
  106. }
  107. return "", ctx.Err()
  108. }
  109. // urlsEqual checks equality of url.URLS between two arrays.
  110. // This check pass even if an URL is in hostname and opposite is in IP address.
  111. func urlsEqual(ctx context.Context, a []url.URL, b []url.URL) bool {
  112. if len(a) != len(b) {
  113. return false
  114. }
  115. urls, err := resolveTCPAddrs(ctx, [][]url.URL{a, b})
  116. if err != nil {
  117. return false
  118. }
  119. a, b = urls[0], urls[1]
  120. sort.Sort(types.URLs(a))
  121. sort.Sort(types.URLs(b))
  122. for i := range a {
  123. if !reflect.DeepEqual(a[i], b[i]) {
  124. return false
  125. }
  126. }
  127. return true
  128. }
  129. func URLStringsEqual(ctx context.Context, a []string, b []string) bool {
  130. if len(a) != len(b) {
  131. return false
  132. }
  133. urlsA := make([]url.URL, 0)
  134. for _, str := range a {
  135. u, err := url.Parse(str)
  136. if err != nil {
  137. return false
  138. }
  139. urlsA = append(urlsA, *u)
  140. }
  141. urlsB := make([]url.URL, 0)
  142. for _, str := range b {
  143. u, err := url.Parse(str)
  144. if err != nil {
  145. return false
  146. }
  147. urlsB = append(urlsB, *u)
  148. }
  149. return urlsEqual(ctx, urlsA, urlsB)
  150. }
  151. func IsNetworkTimeoutError(err error) bool {
  152. nerr, ok := err.(net.Error)
  153. return ok && nerr.Timeout()
  154. }