Skip to content

Commit

Permalink
agent: use helper for INADDR_ANY
Browse files Browse the repository at this point in the history
  • Loading branch information
magiconair committed May 10, 2017
1 parent f8913b4 commit e6c6f8c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion command/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" && !isAddrANY(a.config.BindAddr):
a.config.AdvertiseAddr = a.config.BindAddr

default:
Expand Down
4 changes: 2 additions & 2 deletions command/agent/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,12 +371,12 @@ func (c *Command) readConfig() *Config {
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)
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)
return nil
}
Expand Down
20 changes: 20 additions & 0 deletions command/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1968,3 +1968,23 @@ func (d dirEnts) Less(i, j int) bool {
func (d dirEnts) Swap(i, j int) {
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 == "[::]"
}

0 comments on commit e6c6f8c

Please sign in to comment.