agent: use helper for INADDR_ANY

This commit is contained in:
Frank Schroeder 2017-05-10 09:48:43 +02:00 committed by Frank Schröder
parent f8913b4c77
commit e6c6f8cedd
3 changed files with 23 additions and 3 deletions

View File

@ -305,7 +305,7 @@ func (a *Agent) consulConfig() (*consul.Config, error) {
} }
a.config.AdvertiseAddr = ipStr a.config.AdvertiseAddr = ipStr
case a.config.BindAddr != "0.0.0.0" && a.config.BindAddr != "" && a.config.BindAddr != "[::]": case a.config.BindAddr != "" && !isAddrANY(a.config.BindAddr):
a.config.AdvertiseAddr = a.config.BindAddr a.config.AdvertiseAddr = a.config.BindAddr
default: default:

View File

@ -371,12 +371,12 @@ func (c *Command) readConfig() *Config {
return nil return nil
} }
if config.AdvertiseAddr == "0.0.0.0" || config.AdvertiseAddr == "::" || config.AdvertiseAddr == "[::]" { if isAddrANY(config.AdvertiseAddr) {
c.UI.Error("Advertise address cannot be " + config.AdvertiseAddr) c.UI.Error("Advertise address cannot be " + config.AdvertiseAddr)
return nil return nil
} }
if config.AdvertiseAddrWan == "0.0.0.0" || config.AdvertiseAddrWan == "::" || config.AdvertiseAddrWan == "[::]" { if isAddrANY(config.AdvertiseAddrWan) {
c.UI.Error("Advertise WAN address cannot be " + config.AdvertiseAddrWan) c.UI.Error("Advertise WAN address cannot be " + config.AdvertiseAddrWan)
return nil return nil
} }

View File

@ -1968,3 +1968,23 @@ func (d dirEnts) Less(i, j int) bool {
func (d dirEnts) Swap(i, j int) { func (d dirEnts) Swap(i, j int) {
d[i], d[j] = d[j], d[i] d[i], d[j] = d[j], d[i]
} }
// isAddrANY 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 isAddrANY(ip interface{}) bool {
if ip == nil {
return false
}
var ips string
switch x := ip.(type) {
case net.IP:
ips = x.String()
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 == "[::]"
}