tendermint/p2p/listener.go

184 lines
3.9 KiB
Go
Raw Normal View History

2014-07-07 20:03:50 -07:00
package p2p
2014-06-18 20:48:32 -07:00
import (
2014-07-10 22:19:58 -07:00
"fmt"
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"
. "github.com/tendermint/tendermint/common"
2014-07-08 00:02:04 -07:00
"github.com/tendermint/tendermint/p2p/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 {
Connections() <-chan net.Conn
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 of Listener.
*/
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
connections chan net.Conn
2014-07-01 14:50:24 -07:00
stopped uint32
2014-06-18 20:48:32 -07:00
}
2014-06-24 17:28:40 -07:00
const (
2014-07-09 14:32:45 -07:00
numBufferedConnections = 10
defaultExternalPort = 8770
2014-06-24 17:28:40 -07:00
)
func splitHostPort(addr string) (host string, port int) {
host, portStr, err := net.SplitHostPort(addr)
2014-07-04 19:29:02 -07:00
if err != nil {
panic(err)
}
port, err = strconv.Atoi(portStr)
2014-07-04 19:29:02 -07:00
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())
2014-07-14 16:15:13 -07:00
log.Debug("Local listener: %v:%v", listenerIP, listenerPort)
2014-07-04 19:29:02 -07:00
// Determine external address...
var extAddr *NetAddress
// If the lAddrIP is INADDR_ANY, try UPnP
if lAddrIP == "" || lAddrIP == "0.0.0.0" {
extAddr = getUPNPExternalAddress(lAddrPort, listenerPort)
2014-07-04 19:29:02 -07:00
}
// Otherwise just use the local address...
if extAddr == nil {
extAddr = getNaiveExternalAddress(listenerPort)
2014-07-04 19:29:02 -07:00
}
if extAddr == nil {
panic("Could not determine external address!")
}
2014-07-01 14:50:24 -07:00
dl := &DefaultListener{
listener: listener,
2014-07-04 19:29:02 -07:00
extAddr: extAddr,
connections: make(chan net.Conn, numBufferedConnections),
2014-07-01 14:50:24 -07:00
}
2014-06-18 20:48:32 -07:00
2014-08-10 16:35:08 -07:00
go dl.listenRoutine()
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-07-15 15:54:33 -07:00
// TODO: prevent abuse, esp a bunch of connections coming from the same IP range.
2014-08-10 16:35:08 -07:00
func (l *DefaultListener) listenRoutine() {
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)
}
l.connections <- conn
2014-07-01 14:50:24 -07:00
}
// Cleanup
2014-07-01 14:50:24 -07:00
close(l.connections)
for _ = range l.connections {
// Drain
2014-07-01 14:50:24 -07:00
}
2014-06-18 20:48:32 -07:00
}
// A channel of inbound connections.
// It gets closed when the listener closes.
func (l *DefaultListener) Connections() <-chan net.Conn {
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-10 22:19:58 -07:00
func (l *DefaultListener) String() string {
return fmt.Sprintf("Listener(@%v)", l.extAddr)
}
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 {
2014-07-14 16:15:13 -07:00
log.Debug("Getting UPNP external address")
nat, err := upnp.Discover()
2014-07-01 14:50:24 -07:00
if err != nil {
2014-07-14 16:15:13 -07:00
log.Debug("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-14 16:15:13 -07:00
log.Debug("Could not get UPNP external address: %v", err)
2014-07-01 14:50:24 -07:00
return nil
}
// UPnP can't seem to get the external port, so let's just be explicit.
if externalPort == 0 {
externalPort = defaultExternalPort
}
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-14 16:15:13 -07:00
log.Debug("Could not get UPNP external address: %v", err)
2014-07-01 14:50:24 -07:00
return nil
}
2014-07-14 16:15:13 -07:00
log.Debug("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
return NewNetAddressIPPort(ipnet.IP, uint16(port))
2014-07-01 14:50:24 -07:00
}
return nil
}