From caf3a847e62eec0e7e38c89884416e786f4306e9 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Tue, 31 Dec 2013 14:00:25 -0800 Subject: [PATCH] Guard against a bad advertise address --- consul/config.go | 7 +++++++ consul/server.go | 21 ++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/consul/config.go b/consul/config.go index 1f40a1abf1..056f33e73f 100644 --- a/consul/config.go +++ b/consul/config.go @@ -5,6 +5,7 @@ import ( "github.com/hashicorp/raft" "github.com/hashicorp/serf/serf" "io" + "net" "os" ) @@ -39,6 +40,12 @@ type Config struct { // by the WAN and LAN RPCAddr string + // RPCAdvertise is the address that is advertised to other nodes for + // the RPC endpoint. This can differ from the RPC address, if for example + // the RPCAddr is unspecified "0.0.0.0:8300", but this address must be + // reachable + RPCAdvertise net.Addr + // SerfLANConfig is the configuration for the intra-dc serf SerfLANConfig *serf.Config diff --git a/consul/server.go b/consul/server.go index 63fa1e73b9..49b2e98edf 100644 --- a/consul/server.go +++ b/consul/server.go @@ -233,7 +233,26 @@ func (s *Server) setupRPC() error { return err } s.rpcListener = list - s.raftLayer = NewRaftLayer(s.rpcListener.Addr()) + + var advertise net.Addr + if s.config.RPCAdvertise != nil { + advertise = s.config.RPCAdvertise + } else { + advertise = s.rpcListener.Addr() + } + + // Verify that we have a usable advertise address + addr, ok := advertise.(*net.TCPAddr) + if !ok { + list.Close() + return fmt.Errorf("RPC advertise address is not a TCP Address: %v", addr) + } + if addr.IP.IsUnspecified() { + list.Close() + return fmt.Errorf("RPC advertise address is not advertisable: %v", addr) + } + + s.raftLayer = NewRaftLayer(advertise) go s.listen() return nil }