tendermint/p2p/listener.go

213 lines
4.7 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"
"time"
2015-04-01 17:30:16 -07:00
. "github.com/tendermint/tendermint/common"
"github.com/tendermint/tendermint/p2p/upnp"
2014-06-18 20:48:32 -07:00
)
2014-06-24 17:28:40 -07:00
type Listener interface {
Connections() <-chan net.Conn
2015-04-22 19:45:36 -07:00
InternalAddress() *NetAddress
2014-07-04 19:29:02 -07:00
ExternalAddress() *NetAddress
String() string
Stop() bool
2014-06-24 17:28:40 -07:00
}
2014-12-23 19:31:24 -08:00
// Implements Listener
2014-06-24 17:28:40 -07:00
type DefaultListener struct {
BaseService
2014-07-01 14:50:24 -07:00
listener net.Listener
2015-04-22 19:45:36 -07:00
intAddr *NetAddress
2014-07-04 19:29:02 -07:00
extAddr *NetAddress
connections chan net.Conn
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
tryListenSeconds = 5
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 {
2015-07-19 23:42:52 +00:00
PanicSanity(err)
2014-07-04 19:29:02 -07:00
}
port, err = strconv.Atoi(portStr)
2014-07-04 19:29:02 -07:00
if err != nil {
2015-07-19 23:42:52 +00:00
PanicSanity(err)
2014-07-04 19:29:02 -07:00
}
return host, port
}
func NewDefaultListener(protocol string, lAddr string) Listener {
// Local listen IP & port
lAddrIP, lAddrPort := splitHostPort(lAddr)
// Create listener
var listener net.Listener
var err error
for i := 0; i < tryListenSeconds; i++ {
listener, err = net.Listen(protocol, lAddr)
if err == nil {
break
} else if i < tryListenSeconds-1 {
time.Sleep(time.Second * 1)
}
}
if err != nil {
2015-07-19 23:42:52 +00:00
PanicCrisis(err)
}
// Actual listener local IP & port
listenerIP, listenerPort := splitHostPort(listener.Addr().String())
2015-07-19 21:49:13 +00:00
log.Info("Local listener", "ip", listenerIP, "port", listenerPort)
2014-07-04 19:29:02 -07:00
2015-04-22 19:45:36 -07:00
// Determine internal address...
var intAddr *NetAddress = NewNetAddressString(lAddr)
2014-07-04 19:29:02 -07:00
// Determine external address...
var extAddr *NetAddress
if !config.GetBool("skip_upnp") {
// If the lAddrIP is INADDR_ANY, try UPnP
if lAddrIP == "" || lAddrIP == "0.0.0.0" {
extAddr = getUPNPExternalAddress(lAddrPort, listenerPort)
2014-12-17 01:37:13 -08:00
}
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 {
2015-07-19 23:42:52 +00:00
PanicCrisis("Could not determine external address!")
2014-07-04 19:29:02 -07:00
}
2014-07-01 14:50:24 -07:00
dl := &DefaultListener{
listener: listener,
2015-04-22 19:45:36 -07:00
intAddr: intAddr,
2014-07-04 19:29:02 -07:00
extAddr: extAddr,
connections: make(chan net.Conn, numBufferedConnections),
2014-07-01 14:50:24 -07:00
}
dl.BaseService = *NewBaseService(log, "DefaultListener", dl)
dl.Start() // Started upon construction
return dl
}
2014-06-18 20:48:32 -07:00
func (l *DefaultListener) OnStart() error {
2015-07-21 18:31:01 -07:00
l.BaseService.OnStart()
go l.listenRoutine()
return nil
}
2014-06-18 20:48:32 -07:00
2015-07-21 18:31:01 -07:00
func (l *DefaultListener) OnStop() {
l.BaseService.OnStop()
l.listener.Close()
2014-06-18 20:48:32 -07:00
}
2015-07-12 17:54:34 +00:00
// Accept connections and pass on the channel
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 !l.IsRunning() {
break // Go to cleanup
2014-07-01 14:50:24 -07:00
}
// listener wasn't stopped,
// yet we encountered an error.
if err != nil {
2015-07-19 23:42:52 +00:00
PanicCrisis(err)
2014-07-01 14:50:24 -07:00
}
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
}
2015-04-22 19:45:36 -07:00
func (l *DefaultListener) InternalAddress() *NetAddress {
return l.intAddr
}
2014-07-04 19:29:02 -07:00
func (l *DefaultListener) ExternalAddress() *NetAddress {
return l.extAddr
2014-06-24 17:28:40 -07:00
}
2015-04-23 18:33:20 -07:00
// NOTE: The returned listener is already Accept()'ing.
// So it's not suitable to pass into http.Serve().
func (l *DefaultListener) NetListener() net.Listener {
return l.listener
}
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 {
2015-07-19 21:49:13 +00:00
log.Info("Getting UPNP external address")
nat, err := upnp.Discover()
2014-07-01 14:50:24 -07:00
if err != nil {
2015-07-19 21:49:13 +00:00
log.Info("Could not perform UPNP discover", "error", err)
2014-07-01 14:50:24 -07:00
return nil
}
ext, err := nat.GetExternalAddress()
if err != nil {
2015-07-19 21:49:13 +00:00
log.Info("Could not get UPNP external address", "error", 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 {
2015-07-19 21:49:13 +00:00
log.Info("Could not add UPNP port mapping", "error", err)
2014-07-01 14:50:24 -07:00
return nil
}
2015-07-19 21:49:13 +00:00
log.Info("Got UPNP external address", "address", 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 {
2015-07-19 23:42:52 +00:00
PanicCrisis(Fmt("Could not fetch interface addresses: %v", err))
2014-07-01 14:50:24 -07:00
}
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
}