netutil.go 2.9 KB

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