mirror of
https://github.com/fluencelabs/tendermint
synced 2025-05-29 14:11:21 +00:00
local -> external
This commit is contained in:
parent
0f973c29ca
commit
7c22d99dab
@ -207,7 +207,7 @@ func (c *Client) peerErrorHandler() {
|
|||||||
case <-c.quit:
|
case <-c.quit:
|
||||||
return
|
return
|
||||||
case errPeer := <-c.erroredPeers:
|
case errPeer := <-c.erroredPeers:
|
||||||
// TODO do something
|
log.Infof("%v errored: %v", errPeer.peer, errPeer.err)
|
||||||
c.StopPeer(errPeer.peer)
|
c.StopPeer(errPeer.peer)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ func makeClientPair(t testing.TB, bufferSize int, chNames []String) (*Client, *C
|
|||||||
s1 := NewServer("tcp", ":8001", c1)
|
s1 := NewServer("tcp", ":8001", c1)
|
||||||
|
|
||||||
// Dial the server & add the connection to c2.
|
// Dial the server & add the connection to c2.
|
||||||
s1laddr := s1.LocalAddress()
|
s1laddr := s1.ExternalAddress()
|
||||||
conn, err := s1laddr.Dial()
|
conn, err := s1laddr.Dial()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Could not connect to server address %v", s1laddr)
|
t.Fatalf("Could not connect to server address %v", s1laddr)
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
package peer
|
package peer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
. "github.com/tendermint/tendermint/binary"
|
|
||||||
"io"
|
"io"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
. "github.com/tendermint/tendermint/binary"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -19,7 +20,7 @@ type KnownAddress struct {
|
|||||||
LastAttempt Time
|
LastAttempt Time
|
||||||
LastSuccess Time
|
LastSuccess Time
|
||||||
NewRefs UInt16
|
NewRefs UInt16
|
||||||
OldBucket Int16 // TODO init to -1
|
OldBucket Int16
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewKnownAddress(addr *NetAddress, src *NetAddress) *KnownAddress {
|
func NewKnownAddress(addr *NetAddress, src *NetAddress) *KnownAddress {
|
||||||
|
@ -2,23 +2,20 @@ package peer
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
"strconv"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
|
. "github.com/tendermint/tendermint/binary"
|
||||||
. "github.com/tendermint/tendermint/common"
|
. "github.com/tendermint/tendermint/common"
|
||||||
"github.com/tendermint/tendermint/peer/upnp"
|
"github.com/tendermint/tendermint/peer/upnp"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
// BUG(jae) Remove DEFAULT_PORT
|
|
||||||
DEFAULT_PORT = 8001
|
|
||||||
)
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Listener is part of a Server.
|
Listener is part of a Server.
|
||||||
*/
|
*/
|
||||||
type Listener interface {
|
type Listener interface {
|
||||||
Connections() <-chan *Connection
|
Connections() <-chan *Connection
|
||||||
LocalAddress() *NetAddress
|
ExternalAddress() *NetAddress
|
||||||
Stop()
|
Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,6 +24,7 @@ DefaultListener is an implementation that works on the golang network stack.
|
|||||||
*/
|
*/
|
||||||
type DefaultListener struct {
|
type DefaultListener struct {
|
||||||
listener net.Listener
|
listener net.Listener
|
||||||
|
extAddr *NetAddress
|
||||||
connections chan *Connection
|
connections chan *Connection
|
||||||
stopped uint32
|
stopped uint32
|
||||||
}
|
}
|
||||||
@ -36,6 +34,30 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func NewDefaultListener(protocol string, listenAddr string) Listener {
|
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)
|
listener, err := net.Listen(protocol, listenAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -43,6 +65,7 @@ func NewDefaultListener(protocol string, listenAddr string) Listener {
|
|||||||
|
|
||||||
dl := &DefaultListener{
|
dl := &DefaultListener{
|
||||||
listener: listener,
|
listener: listener,
|
||||||
|
extAddr: extAddr,
|
||||||
connections: make(chan *Connection, DEFAULT_BUFFERED_CONNECTIONS),
|
connections: make(chan *Connection, DEFAULT_BUFFERED_CONNECTIONS),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,8 +103,8 @@ func (l *DefaultListener) Connections() <-chan *Connection {
|
|||||||
return l.connections
|
return l.connections
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *DefaultListener) LocalAddress() *NetAddress {
|
func (l *DefaultListener) ExternalAddress() *NetAddress {
|
||||||
return GetLocalAddress()
|
return l.extAddr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *DefaultListener) Stop() {
|
func (l *DefaultListener) Stop() {
|
||||||
@ -90,54 +113,35 @@ func (l *DefaultListener) Stop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* local address helpers */
|
/* external 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")
|
|
||||||
}
|
|
||||||
|
|
||||||
// UPNP external address discovery & port mapping
|
// UPNP external address discovery & port mapping
|
||||||
// TODO: more flexible internal & external ports
|
func getUPNPExternalAddress(externalPort, internalPort int) *NetAddress {
|
||||||
func GetUPNPLocalAddress() *NetAddress {
|
log.Infof("Getting UPNP external address")
|
||||||
// XXX remove nil, create option for specifying address.
|
|
||||||
// removed because this takes too long.
|
|
||||||
return nil
|
|
||||||
log.Infof("Getting UPNP local address")
|
|
||||||
nat, err := upnp.Discover()
|
nat, err := upnp.Discover()
|
||||||
if err != nil {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
ext, err := nat.GetExternalAddress()
|
ext, err := nat.GetExternalAddress()
|
||||||
if err != nil {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = nat.AddPortMapping("tcp", DEFAULT_PORT, DEFAULT_PORT, "tendermint", 0)
|
externalPort, err = nat.AddPortMapping("tcp", externalPort, internalPort, "tendermint", 0)
|
||||||
if err != nil {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("Got UPNP local address: %v", ext)
|
log.Infof("Got UPNP external address: %v", ext)
|
||||||
return NewNetAddressIPPort(ext, DEFAULT_PORT)
|
return NewNetAddressIPPort(ext, UInt16(externalPort))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Naive local IPv4 interface address detection
|
// TODO: use syscalls: http://pastebin.com/9exZG4rh
|
||||||
// TODO: use syscalls to get actual ourIP. http://pastebin.com/9exZG4rh
|
func getNaiveExternalAddress(port int) *NetAddress {
|
||||||
func GetDefaultLocalAddress() *NetAddress {
|
|
||||||
addrs, err := net.InterfaceAddrs()
|
addrs, err := net.InterfaceAddrs()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Panicf("Unexpected error fetching interface addresses: %v", err)
|
Panicf("Unexpected error fetching interface addresses: %v", err)
|
||||||
@ -152,7 +156,7 @@ func GetDefaultLocalAddress() *NetAddress {
|
|||||||
if v4 == nil || v4[0] == 127 {
|
if v4 == nil || v4[0] == 127 {
|
||||||
continue
|
continue
|
||||||
} // loopback
|
} // loopback
|
||||||
return NewNetAddressIPPort(ipnet.IP, DEFAULT_PORT)
|
return NewNetAddressIPPort(ipnet.IP, UInt16(port))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -17,8 +17,8 @@ func NewServer(protocol string, laddr string, c *Client) *Server {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) LocalAddress() *NetAddress {
|
func (s *Server) ExternalAddress() *NetAddress {
|
||||||
return s.listener.LocalAddress()
|
return s.listener.ExternalAddress()
|
||||||
}
|
}
|
||||||
|
|
||||||
// meant to run in a goroutine
|
// meant to run in a goroutine
|
||||||
|
Loading…
x
Reference in New Issue
Block a user