From 7c22d99dab368d8f7e1cb1b71bfe8f6dd695f18d Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Fri, 4 Jul 2014 19:29:02 -0700 Subject: [PATCH] local -> external --- peer/client.go | 2 +- peer/client_test.go | 2 +- peer/knownaddress.go | 5 +-- peer/listener.go | 82 +++++++++++++++++++++++--------------------- peer/server.go | 4 +-- 5 files changed, 50 insertions(+), 45 deletions(-) diff --git a/peer/client.go b/peer/client.go index 75d33a2e8..6d4be538f 100644 --- a/peer/client.go +++ b/peer/client.go @@ -207,7 +207,7 @@ func (c *Client) peerErrorHandler() { case <-c.quit: return case errPeer := <-c.erroredPeers: - // TODO do something + log.Infof("%v errored: %v", errPeer.peer, errPeer.err) c.StopPeer(errPeer.peer) return } diff --git a/peer/client_test.go b/peer/client_test.go index 31e8cb685..a2043f17c 100644 --- a/peer/client_test.go +++ b/peer/client_test.go @@ -26,7 +26,7 @@ func makeClientPair(t testing.TB, bufferSize int, chNames []String) (*Client, *C s1 := NewServer("tcp", ":8001", c1) // Dial the server & add the connection to c2. - s1laddr := s1.LocalAddress() + s1laddr := s1.ExternalAddress() conn, err := s1laddr.Dial() if err != nil { t.Fatalf("Could not connect to server address %v", s1laddr) diff --git a/peer/knownaddress.go b/peer/knownaddress.go index c18e9401b..0db057fe3 100644 --- a/peer/knownaddress.go +++ b/peer/knownaddress.go @@ -1,9 +1,10 @@ package peer import ( - . "github.com/tendermint/tendermint/binary" "io" "time" + + . "github.com/tendermint/tendermint/binary" ) /* @@ -19,7 +20,7 @@ type KnownAddress struct { LastAttempt Time LastSuccess Time NewRefs UInt16 - OldBucket Int16 // TODO init to -1 + OldBucket Int16 } func NewKnownAddress(addr *NetAddress, src *NetAddress) *KnownAddress { diff --git a/peer/listener.go b/peer/listener.go index fac97601f..0cc5b476a 100644 --- a/peer/listener.go +++ b/peer/listener.go @@ -2,23 +2,20 @@ package peer import ( "net" + "strconv" "sync/atomic" + . "github.com/tendermint/tendermint/binary" . "github.com/tendermint/tendermint/common" "github.com/tendermint/tendermint/peer/upnp" ) -const ( - // BUG(jae) Remove DEFAULT_PORT - DEFAULT_PORT = 8001 -) - /* Listener is part of a Server. */ type Listener interface { Connections() <-chan *Connection - LocalAddress() *NetAddress + ExternalAddress() *NetAddress Stop() } @@ -27,6 +24,7 @@ DefaultListener is an implementation that works on the golang network stack. */ type DefaultListener struct { listener net.Listener + extAddr *NetAddress connections chan *Connection stopped uint32 } @@ -36,6 +34,30 @@ const ( ) func NewDefaultListener(protocol string, listenAddr string) Listener { + listenHost, listenPortStr, err := net.SplitHostPort(listenAddr) + if err != nil { + panic(err) + } + listenPort, err := strconv.Atoi(listenPortStr) + if err != nil { + panic(err) + } + + // 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) + } + // Otherwise just use the local address... + if extAddr == nil { + extAddr = getNaiveExternalAddress(listenPort) + } + if extAddr == nil { + panic("Could not determine external address!") + } + + // Create listener listener, err := net.Listen(protocol, listenAddr) if err != nil { panic(err) @@ -43,6 +65,7 @@ func NewDefaultListener(protocol string, listenAddr string) Listener { dl := &DefaultListener{ listener: listener, + extAddr: extAddr, connections: make(chan *Connection, DEFAULT_BUFFERED_CONNECTIONS), } @@ -80,8 +103,8 @@ func (l *DefaultListener) Connections() <-chan *Connection { return l.connections } -func (l *DefaultListener) LocalAddress() *NetAddress { - return GetLocalAddress() +func (l *DefaultListener) ExternalAddress() *NetAddress { + return l.extAddr } func (l *DefaultListener) Stop() { @@ -90,54 +113,35 @@ func (l *DefaultListener) Stop() { } } -/* local address helpers */ - -func GetLocalAddress() *NetAddress { - laddr := GetUPNPLocalAddress() - if laddr != nil { - return laddr - } - - laddr = GetDefaultLocalAddress() - if laddr != nil { - return laddr - } - - panic("Could not determine local address") -} +/* external address helpers */ // UPNP external address discovery & port mapping -// TODO: more flexible internal & external ports -func GetUPNPLocalAddress() *NetAddress { - // XXX remove nil, create option for specifying address. - // removed because this takes too long. - return nil - log.Infof("Getting UPNP local address") +func getUPNPExternalAddress(externalPort, internalPort int) *NetAddress { + log.Infof("Getting UPNP external address") nat, err := upnp.Discover() if err != nil { - log.Infof("Could not get UPNP local address: %v", err) + log.Infof("Could not get UPNP extrernal address: %v", err) return nil } ext, err := nat.GetExternalAddress() if err != nil { - log.Infof("Could not get UPNP local address: %v", err) + log.Infof("Could not get UPNP external address: %v", err) return nil } - _, err = nat.AddPortMapping("tcp", DEFAULT_PORT, DEFAULT_PORT, "tendermint", 0) + externalPort, err = nat.AddPortMapping("tcp", externalPort, internalPort, "tendermint", 0) if err != nil { - log.Infof("Could not get UPNP local address: %v", err) + log.Infof("Could not get UPNP external address: %v", err) return nil } - log.Infof("Got UPNP local address: %v", ext) - return NewNetAddressIPPort(ext, DEFAULT_PORT) + log.Infof("Got UPNP external address: %v", ext) + return NewNetAddressIPPort(ext, UInt16(externalPort)) } -// Naive local IPv4 interface address detection -// TODO: use syscalls to get actual ourIP. http://pastebin.com/9exZG4rh -func GetDefaultLocalAddress() *NetAddress { +// TODO: use syscalls: http://pastebin.com/9exZG4rh +func getNaiveExternalAddress(port int) *NetAddress { addrs, err := net.InterfaceAddrs() if err != nil { Panicf("Unexpected error fetching interface addresses: %v", err) @@ -152,7 +156,7 @@ func GetDefaultLocalAddress() *NetAddress { if v4 == nil || v4[0] == 127 { continue } // loopback - return NewNetAddressIPPort(ipnet.IP, DEFAULT_PORT) + return NewNetAddressIPPort(ipnet.IP, UInt16(port)) } return nil } diff --git a/peer/server.go b/peer/server.go index bd8733961..f6fd032f0 100644 --- a/peer/server.go +++ b/peer/server.go @@ -17,8 +17,8 @@ func NewServer(protocol string, laddr string, c *Client) *Server { return s } -func (s *Server) LocalAddress() *NetAddress { - return s.listener.LocalAddress() +func (s *Server) ExternalAddress() *NetAddress { + return s.listener.ExternalAddress() } // meant to run in a goroutine