tendermint/peer/listener.go

163 lines
3.3 KiB
Go
Raw Normal View History

2014-06-18 20:48:32 -07:00
package peer
import (
2014-07-01 14:50:24 -07:00
"net"
2014-07-04 19:29:02 -07:00
"strconv"
2014-07-01 14:50:24 -07:00
"sync/atomic"
2014-07-04 19:29:02 -07:00
. "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common"
"github.com/tendermint/tendermint/peer/upnp"
2014-06-18 20:48:32 -07:00
)
/*
Listener is part of a Server.
*/
2014-06-24 17:28:40 -07:00
type Listener interface {
2014-07-01 14:50:24 -07:00
Connections() <-chan *Connection
2014-07-04 19:29:02 -07:00
ExternalAddress() *NetAddress
2014-07-01 14:50:24 -07:00
Stop()
2014-06-24 17:28:40 -07:00
}
/*
DefaultListener is an implementation that works on the golang network stack.
*/
2014-06-24 17:28:40 -07:00
type DefaultListener struct {
2014-07-01 14:50:24 -07:00
listener net.Listener
2014-07-04 19:29:02 -07:00
extAddr *NetAddress
2014-07-01 14:50:24 -07:00
connections chan *Connection
stopped uint32
2014-06-18 20:48:32 -07:00
}
2014-06-24 17:28:40 -07:00
const (
2014-07-05 23:50:06 -07:00
NumBufferedConnections = 10
2014-06-24 17:28:40 -07:00
)
func NewDefaultListener(protocol string, listenAddr string) Listener {
2014-07-04 19:29:02 -07:00
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
2014-07-01 14:50:24 -07:00
listener, err := net.Listen(protocol, listenAddr)
if err != nil {
panic(err)
}
2014-06-18 20:48:32 -07:00
2014-07-01 14:50:24 -07:00
dl := &DefaultListener{
listener: listener,
2014-07-04 19:29:02 -07:00
extAddr: extAddr,
2014-07-05 23:50:06 -07:00
connections: make(chan *Connection, NumBufferedConnections),
2014-07-01 14:50:24 -07:00
}
2014-06-18 20:48:32 -07:00
2014-07-01 14:50:24 -07:00
go dl.listenHandler()
2014-06-18 20:48:32 -07:00
2014-07-01 14:50:24 -07:00
return dl
2014-06-18 20:48:32 -07:00
}
2014-06-25 21:37:20 -07:00
func (l *DefaultListener) listenHandler() {
2014-07-01 14:50:24 -07:00
for {
conn, err := l.listener.Accept()
if atomic.LoadUint32(&l.stopped) == 1 {
break // go to cleanup
2014-07-01 14:50:24 -07:00
}
// listener wasn't stopped,
// yet we encountered an error.
if err != nil {
panic(err)
}
c := NewConnection(conn)
l.connections <- c
}
// cleanup
close(l.connections)
for _ = range l.connections {
// drain
}
2014-06-18 20:48:32 -07:00
}
2014-06-25 21:37:20 -07:00
func (l *DefaultListener) Connections() <-chan *Connection {
2014-07-01 14:50:24 -07:00
return l.connections
2014-06-18 20:48:32 -07:00
}
2014-07-04 19:29:02 -07:00
func (l *DefaultListener) ExternalAddress() *NetAddress {
return l.extAddr
2014-06-24 17:28:40 -07:00
}
2014-06-25 21:37:20 -07:00
func (l *DefaultListener) Stop() {
2014-07-01 14:50:24 -07:00
if atomic.CompareAndSwapUint32(&l.stopped, 0, 1) {
l.listener.Close()
}
2014-06-18 20:48:32 -07:00
}
2014-07-04 19:29:02 -07:00
/* external address helpers */
// UPNP external address discovery & port mapping
2014-07-04 19:29:02 -07:00
func getUPNPExternalAddress(externalPort, internalPort int) *NetAddress {
log.Infof("Getting UPNP external address")
nat, err := upnp.Discover()
2014-07-01 14:50:24 -07:00
if err != nil {
2014-07-04 19:29:02 -07:00
log.Infof("Could not get UPNP extrernal address: %v", err)
2014-07-01 14:50:24 -07:00
return nil
}
ext, err := nat.GetExternalAddress()
if err != nil {
2014-07-04 19:29:02 -07:00
log.Infof("Could not get UPNP external address: %v", err)
2014-07-01 14:50:24 -07:00
return nil
}
2014-07-04 19:29:02 -07:00
externalPort, err = nat.AddPortMapping("tcp", externalPort, internalPort, "tendermint", 0)
2014-07-01 14:50:24 -07:00
if err != nil {
2014-07-04 19:29:02 -07:00
log.Infof("Could not get UPNP external address: %v", err)
2014-07-01 14:50:24 -07:00
return nil
}
2014-07-04 19:29:02 -07:00
log.Infof("Got UPNP external address: %v", ext)
return NewNetAddressIPPort(ext, UInt16(externalPort))
}
2014-07-04 19:29:02 -07:00
// TODO: use syscalls: http://pastebin.com/9exZG4rh
func getNaiveExternalAddress(port int) *NetAddress {
2014-07-01 14:50:24 -07:00
addrs, err := net.InterfaceAddrs()
if err != nil {
Panicf("Unexpected error fetching interface addresses: %v", err)
}
for _, a := range addrs {
ipnet, ok := a.(*net.IPNet)
if !ok {
continue
}
v4 := ipnet.IP.To4()
if v4 == nil || v4[0] == 127 {
continue
} // loopback
2014-07-04 19:29:02 -07:00
return NewNetAddressIPPort(ipnet.IP, UInt16(port))
2014-07-01 14:50:24 -07:00
}
return nil
}