Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

agent: use bind over advertise for src #3028

Merged
merged 2 commits into from
May 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions 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 Expand Up @@ -440,9 +440,10 @@ func (a *Agent) consulConfig() (*consul.Config, error) {
}

// set the src address for outgoing rpc connections
// to RPCAdvertise with port 0 so that outgoing
// connections use a random port.
base.RPCSrcAddr = &net.TCPAddr{IP: base.RPCAdvertise.IP}
// Use port 0 so that outgoing connections use a random port.
if !isAddrANY(base.RPCAddr.IP) {
base.RPCSrcAddr = &net.TCPAddr{IP: base.RPCAddr.IP}
}

// Format the build string
revision := a.config.Revision
Expand Down
4 changes: 2 additions & 2 deletions command/agent/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,12 +361,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 @@ -1976,3 +1976,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 == "[::]"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Realized that there's https://golang.org/pkg/net/#IP.IsUnspecified which looks to be doing the same thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspected as much but couldn't find it. Thx

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't work with [::]. I'll use my helper for now.

}