diff --git a/config/config.go b/config/config.go index be0270e91..715068c81 100644 --- a/config/config.go +++ b/config/config.go @@ -21,8 +21,7 @@ var Config Config_ func initFlags(printHelp *bool) { flag.BoolVar(printHelp, "help", false, "Print this help message.") - flag.StringVar(&Config.IP, "ip", Config.IP, "Listen IP. (0.0.0.0 means any)") - flag.IntVar(&Config.Port, "port", Config.Port, "Listen port. (0 means any)") + flag.StringVar(&Config.LAddr, "laddr", Config.LAddr, "Listen address. (0.0.0.0:0 means any interface, any port)") flag.StringVar(&Config.Seed, "seed", Config.Seed, "Address of seed node") } @@ -64,9 +63,8 @@ func init() { /* Default configuration */ var defaultConfig = Config_{ - IP: "0.0.0.0", - Port: 8770, - Seed: "", + LAddr: "0.0.0.0:0", + Seed: "", Db: DbConfig{ Type: "level", Dir: AppDir + "/data", @@ -77,8 +75,7 @@ var defaultConfig = Config_{ /* Configuration types */ type Config_ struct { - IP string - Port int + LAddr string Seed string Db DbConfig Twilio TwilioConfig @@ -98,11 +95,11 @@ type DbConfig struct { } func (cfg *Config_) validate() error { - if cfg.IP == "" { - return errors.New("IP must be set") + if cfg.LAddr == "" { + cfg.LAddr = defaultConfig.LAddr } - if cfg.Port == 0 { - return errors.New("Port must be set") + if cfg.Seed == "" { + cfg.Seed = defaultConfig.Seed } if cfg.Db.Type == "" { return errors.New("Db.Type must be set") diff --git a/log.go b/log.go index 9b31067df..fc2ca879d 100644 --- a/log.go +++ b/log.go @@ -10,7 +10,7 @@ var log seelog.LoggerInterface func init() { // TODO: replace with configuration file in the ~/.tendermint directory. config := ` - + diff --git a/main.go b/main.go index 8c4cbb05c..75251b52a 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "os" "os/signal" "time" @@ -176,7 +175,8 @@ func main() { // Create & start node n := NewNode() - l := p2p.NewDefaultListener("tcp", fmt.Sprintf("%v:%v", config.Config.IP, config.Config.Port)) + log.Warnf(">> %v", config.Config.LAddr) + l := p2p.NewDefaultListener("tcp", config.Config.LAddr) n.AddListener(l) n.Start() diff --git a/p2p/listener.go b/p2p/listener.go index 357ec1e57..2335f07ad 100644 --- a/p2p/listener.go +++ b/p2p/listener.go @@ -32,38 +32,48 @@ type DefaultListener struct { const ( numBufferedConnections = 10 + defaultExternalPort = 8770 ) -func NewDefaultListener(protocol string, listenAddr string) Listener { - listenHost, listenPortStr, err := net.SplitHostPort(listenAddr) +func splitHostPort(addr string) (host string, port int) { + host, portStr, err := net.SplitHostPort(addr) if err != nil { panic(err) } - listenPort, err := strconv.Atoi(listenPortStr) + port, err = strconv.Atoi(portStr) if err != nil { panic(err) } + return host, port +} + +func NewDefaultListener(protocol string, lAddr string) Listener { + // Local listen IP & port + lAddrIP, lAddrPort := splitHostPort(lAddr) + + // Create listener + listener, err := net.Listen(protocol, lAddr) + if err != nil { + panic(err) + } + // Actual listener local IP & port + listenerIP, listenerPort := splitHostPort(listener.Addr().String()) + log.Infof("Local listener: %v:%v", listenerIP, listenerPort) // Determine external address... var extAddr *NetAddress - // If the listenHost is INADDR_ANY, try UPnP - if listenHost == "" || listenHost == "0.0.0.0" { - extAddr = getUPNPExternalAddress(listenPort, listenPort) + // If the lAddrIP is INADDR_ANY, try UPnP + if lAddrIP == "" || lAddrIP == "0.0.0.0" { + extAddr = getUPNPExternalAddress(lAddrPort, listenerPort) } // Otherwise just use the local address... if extAddr == nil { - extAddr = getNaiveExternalAddress(listenPort) + extAddr = getNaiveExternalAddress(listenerPort) } if extAddr == nil { panic("Could not determine external address!") } - // Create listener - listener, err := net.Listen(protocol, listenAddr) - if err != nil { - panic(err) - } - dl := &DefaultListener{ listener: listener, extAddr: extAddr, @@ -135,6 +145,11 @@ func getUPNPExternalAddress(externalPort, internalPort int) *NetAddress { return nil } + // UPnP can't seem to get the external port, so let's just be explicit. + if externalPort == 0 { + externalPort = defaultExternalPort + } + externalPort, err = nat.AddPortMapping("tcp", externalPort, internalPort, "tendermint", 0) if err != nil { log.Infof("Could not get UPNP external address: %v", err)