netutil.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2015 CoreOS, Inc.
  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
  15. import (
  16. "net"
  17. "net/url"
  18. "reflect"
  19. "sort"
  20. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/pkg/capnslog"
  21. "github.com/coreos/etcd/pkg/types"
  22. )
  23. var (
  24. plog = capnslog.NewPackageLogger("github.com/coreos/etcd/pkg", "netutil")
  25. // indirection for testing
  26. resolveTCPAddr = net.ResolveTCPAddr
  27. )
  28. // resolveTCPAddrs is a convenience wrapper for net.ResolveTCPAddr.
  29. // resolveTCPAddrs return a new set of url.URLs, in which all DNS hostnames
  30. // are resolved.
  31. func resolveTCPAddrs(urls [][]url.URL) ([][]url.URL, error) {
  32. newurls := make([][]url.URL, 0)
  33. for _, us := range urls {
  34. nus := make([]url.URL, len(us))
  35. for i, u := range us {
  36. nu, err := url.Parse(u.String())
  37. if err != nil {
  38. return nil, err
  39. }
  40. nus[i] = *nu
  41. }
  42. for i, u := range nus {
  43. host, _, err := net.SplitHostPort(u.Host)
  44. if err != nil {
  45. plog.Errorf("could not parse url %s during tcp resolving", u.Host)
  46. return nil, err
  47. }
  48. if host == "localhost" {
  49. continue
  50. }
  51. if net.ParseIP(host) != nil {
  52. continue
  53. }
  54. tcpAddr, err := resolveTCPAddr("tcp", u.Host)
  55. if err != nil {
  56. plog.Errorf("could not resolve host %s", u.Host)
  57. return nil, err
  58. }
  59. plog.Infof("resolving %s to %s", u.Host, tcpAddr.String())
  60. nus[i].Host = tcpAddr.String()
  61. }
  62. newurls = append(newurls, nus)
  63. }
  64. return newurls, nil
  65. }
  66. // urlsEqual checks equality of url.URLS between two arrays.
  67. // This check pass even if an URL is in hostname and opposite is in IP address.
  68. func urlsEqual(a []url.URL, b []url.URL) bool {
  69. if len(a) != len(b) {
  70. return false
  71. }
  72. urls, err := resolveTCPAddrs([][]url.URL{a, b})
  73. if err != nil {
  74. return false
  75. }
  76. a, b = urls[0], urls[1]
  77. sort.Sort(types.URLs(a))
  78. sort.Sort(types.URLs(b))
  79. for i := range a {
  80. if !reflect.DeepEqual(a[i], b[i]) {
  81. return false
  82. }
  83. }
  84. return true
  85. }
  86. func URLStringsEqual(a []string, b []string) bool {
  87. if len(a) != len(b) {
  88. return false
  89. }
  90. urlsA := make([]url.URL, 0)
  91. for _, str := range a {
  92. u, err := url.Parse(str)
  93. if err != nil {
  94. return false
  95. }
  96. urlsA = append(urlsA, *u)
  97. }
  98. urlsB := make([]url.URL, 0)
  99. for _, str := range b {
  100. u, err := url.Parse(str)
  101. if err != nil {
  102. return false
  103. }
  104. urlsB = append(urlsB, *u)
  105. }
  106. return urlsEqual(urlsA, urlsB)
  107. }