From ea322125255708992e4d1cedba092f3e0da75084 Mon Sep 17 00:00:00 2001 From: Frank Schroeder Date: Wed, 10 May 2017 09:48:43 +0200 Subject: [PATCH] agent: use helper for INADDR_ANY --- command/agent/agent.go | 2 +- command/agent/command.go | 4 ++-- command/agent/config.go | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/command/agent/agent.go b/command/agent/agent.go index ba15e3851a07..497bd51ba584 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -305,7 +305,7 @@ func (a *Agent) consulConfig() (*consul.Config, error) { } a.config.AdvertiseAddr = ipStr - case a.config.BindAddr != "0.0.0.0" && a.config.BindAddr != "" && a.config.BindAddr != "[::]": + case a.config.BindAddr != "" && !inaddr_any(a.config.BindAddr): a.config.AdvertiseAddr = a.config.BindAddr default: diff --git a/command/agent/command.go b/command/agent/command.go index 4326ab4306ba..e1650330ca3a 100644 --- a/command/agent/command.go +++ b/command/agent/command.go @@ -361,12 +361,12 @@ func (c *Command) readConfig() *Config { return nil } - if config.AdvertiseAddr == "0.0.0.0" || config.AdvertiseAddr == "::" || config.AdvertiseAddr == "[::]" { + if inaddr_any(config.AdvertiseAddr) { c.UI.Error("Advertise address cannot be " + config.AdvertiseAddr) return nil } - if config.AdvertiseAddrWan == "0.0.0.0" || config.AdvertiseAddrWan == "::" || config.AdvertiseAddrWan == "[::]" { + if inaddr_any(config.AdvertiseAddrWan) { c.UI.Error("Advertise WAN address cannot be " + config.AdvertiseAddrWan) return nil } diff --git a/command/agent/config.go b/command/agent/config.go index aea7d685fdaa..c6303718665c 100644 --- a/command/agent/config.go +++ b/command/agent/config.go @@ -1976,3 +1976,21 @@ func (d dirEnts) Less(i, j int) bool { func (d dirEnts) Swap(i, j int) { d[i], d[j] = d[j], d[i] } + +// inaddr_any checks if the given ip address is an IPv4 or IPv6 ANY address. ip +// can be either a *net.IP or a string. It panics on another type. +func inaddr_any(ip interface{}) bool { + if ip == nil { + return false + } + var ips string + switch x := ip.(type) { + case *net.IP: + ips = x.String() + case string: + ips = x + default: + panic(fmt.Sprintf("invalid type: %T", ip)) + } + return ips == "0.0.0.0" || ips == "::" || ips == "[::]" +}