netutil.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. "net"
  18. "net/url"
  19. "reflect"
  20. "sort"
  21. "time"
  22. "golang.org/x/net/context"
  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 = net.ResolveTCPAddr
  30. )
  31. const retryInterval = time.Second
  32. // resolveTCPAddrs is a convenience wrapper for net.ResolveTCPAddr.
  33. // resolveTCPAddrs return a new set of url.URLs, in which all DNS hostnames
  34. // are resolved.
  35. func resolveTCPAddrs(ctx context.Context, urls [][]url.URL) ([][]url.URL, error) {
  36. newurls := make([][]url.URL, 0)
  37. for _, us := range urls {
  38. nus := make([]url.URL, len(us))
  39. for i, u := range us {
  40. nu, err := url.Parse(u.String())
  41. if err != nil {
  42. return nil, err
  43. }
  44. nus[i] = *nu
  45. }
  46. for i, u := range nus {
  47. h, err := resolveURL(ctx, u)
  48. if err != nil {
  49. return nil, err
  50. }
  51. if h != "" {
  52. nus[i].Host = h
  53. }
  54. }
  55. newurls = append(newurls, nus)
  56. }
  57. return newurls, nil
  58. }
  59. func resolveURL(ctx context.Context, u url.URL) (string, error) {
  60. for ctx.Err() == nil {
  61. host, _, err := net.SplitHostPort(u.Host)
  62. if err != nil {
  63. plog.Errorf("could not parse url %s during tcp resolving", u.Host)
  64. return "", err
  65. }
  66. if host == "localhost" || net.ParseIP(host) != nil {
  67. return "", nil
  68. }
  69. tcpAddr, err := resolveTCPAddr("tcp", u.Host)
  70. if err == nil {
  71. plog.Infof("resolving %s to %s", u.Host, tcpAddr.String())
  72. return tcpAddr.String(), nil
  73. }
  74. plog.Warningf("failed resolving host %s (%v); retrying in %v", u.Host, err, retryInterval)
  75. select {
  76. case <-ctx.Done():
  77. plog.Errorf("could not resolve host %s", u.Host)
  78. return "", err
  79. case <-time.After(retryInterval):
  80. }
  81. }
  82. return "", ctx.Err()
  83. }
  84. // urlsEqual checks equality of url.URLS between two arrays.
  85. // This check pass even if an URL is in hostname and opposite is in IP address.
  86. func urlsEqual(ctx context.Context, a []url.URL, b []url.URL) bool {
  87. if len(a) != len(b) {
  88. return false
  89. }
  90. urls, err := resolveTCPAddrs(ctx, [][]url.URL{a, b})
  91. if err != nil {
  92. return false
  93. }
  94. a, b = urls[0], urls[1]
  95. sort.Sort(types.URLs(a))
  96. sort.Sort(types.URLs(b))
  97. for i := range a {
  98. if !reflect.DeepEqual(a[i], b[i]) {
  99. return false
  100. }
  101. }
  102. return true
  103. }
  104. func URLStringsEqual(ctx context.Context, a []string, b []string) bool {
  105. if len(a) != len(b) {
  106. return false
  107. }
  108. urlsA := make([]url.URL, 0)
  109. for _, str := range a {
  110. u, err := url.Parse(str)
  111. if err != nil {
  112. return false
  113. }
  114. urlsA = append(urlsA, *u)
  115. }
  116. urlsB := make([]url.URL, 0)
  117. for _, str := range b {
  118. u, err := url.Parse(str)
  119. if err != nil {
  120. return false
  121. }
  122. urlsB = append(urlsB, *u)
  123. }
  124. return urlsEqual(ctx, urlsA, urlsB)
  125. }
  126. func IsNetworkTimeoutError(err error) bool {
  127. nerr, ok := err.(net.Error)
  128. return ok && nerr.Timeout()
  129. }