diff options
Diffstat (limited to 'vendor/github.com/hashicorp/memberlist')
5 files changed, 57 insertions, 42 deletions
diff --git a/vendor/github.com/hashicorp/memberlist/memberlist.go b/vendor/github.com/hashicorp/memberlist/memberlist.go index e1e38fc94..bdf333b43 100644 --- a/vendor/github.com/hashicorp/memberlist/memberlist.go +++ b/vendor/github.com/hashicorp/memberlist/memberlist.go @@ -308,23 +308,17 @@ func (m *Memberlist) tcpLookupIP(host string, defaultPort uint16) ([]ipPort, err // resolveAddr is used to resolve the address into an address, // port, and error. If no port is given, use the default func (m *Memberlist) resolveAddr(hostStr string) ([]ipPort, error) { - // Normalize the incoming string to host:port so we can apply Go's - // parser to it. - port := uint16(0) - if !hasPort(hostStr) { - hostStr += ":" + strconv.Itoa(m.config.BindPort) - } + // This captures the supplied port, or the default one. + hostStr = ensurePort(hostStr, m.config.BindPort) host, sport, err := net.SplitHostPort(hostStr) if err != nil { return nil, err } - - // This will capture the supplied port, or the default one added above. lport, err := strconv.ParseUint(sport, 10, 16) if err != nil { return nil, err } - port = uint16(lport) + port := uint16(lport) // If it looks like an IP address we are done. The SplitHostPort() above // will make sure the host part is in good shape for parsing, even for diff --git a/vendor/github.com/hashicorp/memberlist/memberlist_test.go b/vendor/github.com/hashicorp/memberlist/memberlist_test.go index ecda7fb55..ee2fc5d52 100644 --- a/vendor/github.com/hashicorp/memberlist/memberlist_test.go +++ b/vendor/github.com/hashicorp/memberlist/memberlist_test.go @@ -424,7 +424,7 @@ func TestMemberList_ResolveAddr_TCP_First(t *testing.T) { } port := uint16(m.config.BindPort) expected := []ipPort{ - ipPort{net.ParseIP("127.0.0.1").To4(), port}, + ipPort{net.ParseIP("127.0.0.1"), port}, ipPort{net.ParseIP("2001:db8:a0b:12f0::1"), port}, } if !reflect.DeepEqual(ips, expected) { diff --git a/vendor/github.com/hashicorp/memberlist/suspicion.go b/vendor/github.com/hashicorp/memberlist/suspicion.go index 5f573e1fc..f8aa9e20a 100644 --- a/vendor/github.com/hashicorp/memberlist/suspicion.go +++ b/vendor/github.com/hashicorp/memberlist/suspicion.go @@ -117,7 +117,7 @@ func (s *suspicion) Confirm(from string) bool { // stop the timer then we will call the timeout function directly from // here. n := atomic.AddInt32(&s.n, 1) - elapsed := time.Now().Sub(s.start) + elapsed := time.Since(s.start) remaining := remainingSuspicionTime(n, s.k, elapsed, s.min, s.max) if s.timer.Stop() { if remaining > 0 { diff --git a/vendor/github.com/hashicorp/memberlist/util.go b/vendor/github.com/hashicorp/memberlist/util.go index a4f926e3a..e2381a698 100644 --- a/vendor/github.com/hashicorp/memberlist/util.go +++ b/vendor/github.com/hashicorp/memberlist/util.go @@ -217,20 +217,6 @@ func decodeCompoundMessage(buf []byte) (trunc int, parts [][]byte, err error) { return } -// Given a string of the form "host", "host:port", -// "ipv6::addr" or "[ipv6::address]:port", -// return true if the string includes a port. -func hasPort(s string) bool { - last := strings.LastIndex(s, ":") - if last == -1 { - return false - } - if s[0] == '[' { - return s[last-1] == ']' - } - return strings.Index(s, ":") == last -} - // compressPayload takes an opaque input buffer, compresses it // and wraps it in a compress{} message that is encoded. func compressPayload(inp []byte) (*bytes.Buffer, error) { @@ -294,3 +280,31 @@ func decompressBuffer(c *compress) ([]byte, error) { func joinHostPort(host string, port uint16) string { return net.JoinHostPort(host, strconv.Itoa(int(port))) } + +// hasPort is given a string of the form "host", "host:port", "ipv6::address", +// or "[ipv6::address]:port", and returns true if the string includes a port. +func hasPort(s string) bool { + // IPv6 address in brackets. + if strings.LastIndex(s, "[") == 0 { + return strings.LastIndex(s, ":") > strings.LastIndex(s, "]") + } + + // Otherwise the presence of a single colon determines if there's a port + // since IPv6 addresses outside of brackets (count > 1) can't have a + // port. + return strings.Count(s, ":") == 1 +} + +// ensurePort makes sure the given string has a port number on it, otherwise it +// appends the given port as a default. +func ensurePort(s string, port int) string { + if hasPort(s) { + return s + } + + // If this is an IPv6 address, the join call will add another set of + // brackets, so we have to trim before we add the default port. + s = strings.Trim(s, "[]") + s = net.JoinHostPort(s, strconv.Itoa(port)) + return s +} diff --git a/vendor/github.com/hashicorp/memberlist/util_test.go b/vendor/github.com/hashicorp/memberlist/util_test.go index e1d8eba01..b7f2b4199 100644 --- a/vendor/github.com/hashicorp/memberlist/util_test.go +++ b/vendor/github.com/hashicorp/memberlist/util_test.go @@ -7,24 +7,31 @@ import ( "time" ) -func Test_hasPort(t *testing.T) { - cases := []struct { - s string - expected bool +func TestUtil_PortFunctions(t *testing.T) { + tests := []struct { + addr string + hasPort bool + ensurePort string }{ - {"", false}, - {":80", true}, - {"127.0.0.1", false}, - {"127.0.0.1:80", true}, - {"::1", false}, - {"2001:db8:a0b:12f0::1", false}, - {"[2001:db8:a0b:12f0::1]", false}, - {"[2001:db8:a0b:12f0::1]:80", true}, - } - for _, c := range cases { - if hasPort(c.s) != c.expected { - t.Fatalf("bad: '%s' hasPort was not %v", c.s, c.expected) - } + {"1.2.3.4", false, "1.2.3.4:8301"}, + {"1.2.3.4:1234", true, "1.2.3.4:1234"}, + {"2600:1f14:e22:1501:f9a:2e0c:a167:67e8", false, "[2600:1f14:e22:1501:f9a:2e0c:a167:67e8]:8301"}, + {"[2600:1f14:e22:1501:f9a:2e0c:a167:67e8]", false, "[2600:1f14:e22:1501:f9a:2e0c:a167:67e8]:8301"}, + {"[2600:1f14:e22:1501:f9a:2e0c:a167:67e8]:1234", true, "[2600:1f14:e22:1501:f9a:2e0c:a167:67e8]:1234"}, + {"localhost", false, "localhost:8301"}, + {"localhost:1234", true, "localhost:1234"}, + {"hashicorp.com", false, "hashicorp.com:8301"}, + {"hashicorp.com:1234", true, "hashicorp.com:1234"}, + } + for _, tt := range tests { + t.Run(tt.addr, func(t *testing.T) { + if got, want := hasPort(tt.addr), tt.hasPort; got != want { + t.Fatalf("got %v want %v", got, want) + } + if got, want := ensurePort(tt.addr, 8301), tt.ensurePort; got != want { + t.Fatalf("got %v want %v", got, want) + } + }) } } |