2019-01-04 17:14:40 +00:00
|
|
|
package yggdrasil
|
|
|
|
|
|
|
|
import (
|
2019-01-31 23:29:18 +00:00
|
|
|
"encoding/hex"
|
2019-01-05 12:06:45 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-02-24 14:48:16 -06:00
|
|
|
"io"
|
2019-01-31 23:18:02 +00:00
|
|
|
"net"
|
2019-03-04 22:45:35 +00:00
|
|
|
"net/url"
|
2019-01-31 23:18:02 +00:00
|
|
|
"strings"
|
2019-01-04 17:23:37 +00:00
|
|
|
"sync"
|
2019-03-04 17:09:48 +00:00
|
|
|
|
2019-01-21 23:08:50 -06:00
|
|
|
//"sync/atomic"
|
2019-01-05 12:06:45 +00:00
|
|
|
"time"
|
2019-01-04 17:23:37 +00:00
|
|
|
|
2019-01-31 23:18:02 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
2019-01-04 17:23:37 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
|
2019-01-22 21:16:41 -06:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/util"
|
2019-08-24 16:27:12 -05:00
|
|
|
|
|
|
|
"github.com/Arceliar/phony"
|
2019-01-04 17:14:40 +00:00
|
|
|
)
|
|
|
|
|
2019-01-19 00:14:10 +00:00
|
|
|
type link struct {
|
2019-08-25 12:10:59 -05:00
|
|
|
core *Core
|
|
|
|
mutex sync.RWMutex // protects interfaces below
|
|
|
|
interfaces map[linkInfo]*linkInterface
|
|
|
|
tcp tcp // TCP interface support
|
2019-02-02 22:18:55 -06:00
|
|
|
// TODO timeout (to remove from switch), read from config.ReadTimeout
|
2019-01-22 21:16:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type linkInfo struct {
|
|
|
|
box crypto.BoxPubKey // Their encryption key
|
|
|
|
sig crypto.SigPubKey // Their signing key
|
|
|
|
linkType string // Type of link, e.g. TCP, AWDL
|
|
|
|
local string // Local name or address
|
|
|
|
remote string // Remote name or address
|
2019-01-04 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 21:27:52 -06:00
|
|
|
type linkInterfaceMsgIO interface {
|
|
|
|
readMsg() ([]byte, error)
|
2019-08-18 12:17:54 -05:00
|
|
|
writeMsgs([][]byte) (int, error)
|
2019-01-21 21:27:52 -06:00
|
|
|
close() error
|
|
|
|
// These are temporary workarounds to stream semantics
|
|
|
|
_sendMetaBytes([]byte) error
|
|
|
|
_recvMetaBytes() ([]byte, error)
|
|
|
|
}
|
|
|
|
|
2019-01-19 00:14:10 +00:00
|
|
|
type linkInterface struct {
|
2019-02-01 00:02:17 +00:00
|
|
|
name string
|
|
|
|
link *link
|
|
|
|
peer *peer
|
|
|
|
msgIO linkInterfaceMsgIO
|
|
|
|
info linkInfo
|
|
|
|
incoming bool
|
|
|
|
force bool
|
|
|
|
closed chan struct{}
|
2019-01-04 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
2019-01-19 00:14:10 +00:00
|
|
|
func (l *link) init(c *Core) error {
|
2019-01-04 17:23:37 +00:00
|
|
|
l.core = c
|
|
|
|
l.mutex.Lock()
|
2019-01-22 21:16:41 -06:00
|
|
|
l.interfaces = make(map[linkInfo]*linkInterface)
|
2019-01-04 17:23:37 +00:00
|
|
|
l.mutex.Unlock()
|
2019-01-04 17:14:40 +00:00
|
|
|
|
2019-03-04 17:09:48 +00:00
|
|
|
if err := l.tcp.init(l); err != nil {
|
|
|
|
c.log.Errorln("Failed to start TCP interface")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-04 17:23:37 +00:00
|
|
|
return nil
|
2019-01-04 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
2019-08-25 12:10:59 -05:00
|
|
|
func (l *link) reconfigure(e chan error) {
|
|
|
|
defer close(e)
|
|
|
|
tcpResponse := make(chan error)
|
|
|
|
go l.tcp.reconfigure(tcpResponse)
|
|
|
|
for err := range tcpResponse {
|
|
|
|
e <- err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 22:45:35 +00:00
|
|
|
func (l *link) call(uri string, sintf string) error {
|
|
|
|
u, err := url.Parse(uri)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pathtokens := strings.Split(strings.Trim(u.Path, "/"), "/")
|
|
|
|
switch u.Scheme {
|
|
|
|
case "tcp":
|
|
|
|
l.tcp.call(u.Host, nil, sintf)
|
|
|
|
case "socks":
|
2019-03-08 10:26:46 +00:00
|
|
|
l.tcp.call(pathtokens[0], u.Host, sintf)
|
2019-03-04 22:45:35 +00:00
|
|
|
default:
|
|
|
|
return errors.New("unknown call scheme: " + u.Scheme)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *link) listen(uri string) error {
|
|
|
|
u, err := url.Parse(uri)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch u.Scheme {
|
|
|
|
case "tcp":
|
2019-03-06 11:06:13 +00:00
|
|
|
_, err := l.tcp.listen(u.Host)
|
|
|
|
return err
|
2019-03-04 22:45:35 +00:00
|
|
|
default:
|
|
|
|
return errors.New("unknown listen scheme: " + u.Scheme)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-01 00:02:17 +00:00
|
|
|
func (l *link) create(msgIO linkInterfaceMsgIO, name, linkType, local, remote string, incoming, force bool) (*linkInterface, error) {
|
2019-01-22 21:16:41 -06:00
|
|
|
// Technically anything unique would work for names, but lets pick something human readable, just for debugging
|
2019-01-19 00:14:10 +00:00
|
|
|
intf := linkInterface{
|
2019-01-21 23:08:50 -06:00
|
|
|
name: name,
|
|
|
|
link: l,
|
|
|
|
msgIO: msgIO,
|
2019-01-22 21:16:41 -06:00
|
|
|
info: linkInfo{
|
|
|
|
linkType: linkType,
|
|
|
|
local: local,
|
|
|
|
remote: remote,
|
|
|
|
},
|
2019-02-01 00:02:17 +00:00
|
|
|
incoming: incoming,
|
|
|
|
force: force,
|
2019-01-04 17:23:37 +00:00
|
|
|
}
|
2019-01-19 12:19:24 +00:00
|
|
|
return &intf, nil
|
|
|
|
}
|
|
|
|
|
2019-01-21 23:08:50 -06:00
|
|
|
func (intf *linkInterface) handler() error {
|
|
|
|
// TODO split some of this into shorter functions, so it's easier to read, and for the FIXME duplicate peer issue mentioned later
|
|
|
|
myLinkPub, myLinkPriv := crypto.NewBoxKeys()
|
|
|
|
meta := version_getBaseMetadata()
|
|
|
|
meta.box = intf.link.core.boxPub
|
|
|
|
meta.sig = intf.link.core.sigPub
|
|
|
|
meta.link = *myLinkPub
|
|
|
|
metaBytes := meta.encode()
|
|
|
|
// TODO timeouts on send/recv (goroutine for send/recv, channel select w/ timer)
|
2019-02-26 21:07:56 -06:00
|
|
|
var err error
|
|
|
|
if !util.FuncTimeout(func() { err = intf.msgIO._sendMetaBytes(metaBytes) }, 30*time.Second) {
|
|
|
|
return errors.New("timeout on metadata send")
|
|
|
|
}
|
2019-01-21 23:08:50 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-26 21:07:56 -06:00
|
|
|
if !util.FuncTimeout(func() { metaBytes, err = intf.msgIO._recvMetaBytes() }, 30*time.Second) {
|
|
|
|
return errors.New("timeout on metadata recv")
|
|
|
|
}
|
2019-01-21 23:08:50 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
meta = version_metadata{}
|
|
|
|
if !meta.decode(metaBytes) || !meta.check() {
|
|
|
|
return errors.New("failed to decode metadata")
|
|
|
|
}
|
|
|
|
base := version_getBaseMetadata()
|
|
|
|
if meta.ver > base.ver || meta.ver == base.ver && meta.minorVer > base.minorVer {
|
2019-01-27 20:56:10 +00:00
|
|
|
intf.link.core.log.Errorln("Failed to connect to node: " + intf.name + " version: " + fmt.Sprintf("%d.%d", meta.ver, meta.minorVer))
|
2019-01-21 23:08:50 -06:00
|
|
|
return errors.New("failed to connect: wrong version")
|
|
|
|
}
|
2019-01-31 23:29:18 +00:00
|
|
|
// Check if we're authorized to connect to this key / IP
|
2019-03-12 15:01:27 +00:00
|
|
|
if intf.incoming && !intf.force && !intf.link.core.peers.isAllowedEncryptionPublicKey(&meta.box) {
|
2019-03-12 16:03:02 +00:00
|
|
|
intf.link.core.log.Warnf("%s connection from %s forbidden: AllowedEncryptionPublicKeys does not contain key %s",
|
2019-01-31 23:47:20 +00:00
|
|
|
strings.ToUpper(intf.info.linkType), intf.info.remote, hex.EncodeToString(meta.box[:]))
|
|
|
|
intf.msgIO.close()
|
|
|
|
return nil
|
2019-01-31 23:29:18 +00:00
|
|
|
}
|
2019-01-22 21:16:41 -06:00
|
|
|
// Check if we already have a link to this node
|
|
|
|
intf.info.box = meta.box
|
|
|
|
intf.info.sig = meta.sig
|
|
|
|
intf.link.mutex.Lock()
|
|
|
|
if oldIntf, isIn := intf.link.interfaces[intf.info]; isIn {
|
|
|
|
intf.link.mutex.Unlock()
|
|
|
|
// FIXME we should really return an error and let the caller block instead
|
2019-01-22 21:53:39 -06:00
|
|
|
// That lets them do things like close connections on its own, avoid printing a connection message in the first place, etc.
|
2019-01-27 20:56:10 +00:00
|
|
|
intf.link.core.log.Debugln("DEBUG: found existing interface for", intf.name)
|
2019-01-22 21:53:39 -06:00
|
|
|
intf.msgIO.close()
|
2019-08-13 18:49:49 -05:00
|
|
|
if !intf.incoming {
|
|
|
|
// Block outgoing connection attempts until the existing connection closes
|
|
|
|
<-oldIntf.closed
|
|
|
|
}
|
2019-01-22 21:16:41 -06:00
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
intf.closed = make(chan struct{})
|
|
|
|
intf.link.interfaces[intf.info] = intf
|
2019-01-22 21:48:43 -06:00
|
|
|
defer func() {
|
|
|
|
intf.link.mutex.Lock()
|
|
|
|
delete(intf.link.interfaces, intf.info)
|
|
|
|
intf.link.mutex.Unlock()
|
|
|
|
close(intf.closed)
|
|
|
|
}()
|
2019-01-27 20:56:10 +00:00
|
|
|
intf.link.core.log.Debugln("DEBUG: registered interface for", intf.name)
|
2019-01-22 21:16:41 -06:00
|
|
|
}
|
|
|
|
intf.link.mutex.Unlock()
|
|
|
|
// Create peer
|
2019-01-21 23:08:50 -06:00
|
|
|
shared := crypto.GetSharedKey(myLinkPriv, &meta.link)
|
2019-03-04 20:33:08 +00:00
|
|
|
intf.peer = intf.link.core.peers.newPeer(&meta.box, &meta.sig, shared, intf, func() { intf.msgIO.close() })
|
2019-01-21 23:08:50 -06:00
|
|
|
if intf.peer == nil {
|
|
|
|
return errors.New("failed to create peer")
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
// More cleanup can go here
|
|
|
|
intf.link.core.peers.removePeer(intf.peer.port)
|
|
|
|
}()
|
|
|
|
// Finish setting up the peer struct
|
2019-08-18 12:17:54 -05:00
|
|
|
out := make(chan [][]byte, 1)
|
2019-01-21 23:08:50 -06:00
|
|
|
defer close(out)
|
2019-08-18 12:17:54 -05:00
|
|
|
intf.peer.out = func(msgs [][]byte) {
|
2019-01-21 23:08:50 -06:00
|
|
|
defer func() { recover() }()
|
2019-08-18 12:17:54 -05:00
|
|
|
out <- msgs
|
2019-01-21 23:08:50 -06:00
|
|
|
}
|
2019-01-22 18:24:15 -06:00
|
|
|
intf.peer.linkOut = make(chan []byte, 1)
|
2019-02-01 00:02:17 +00:00
|
|
|
themAddr := address.AddrForNodeID(crypto.GetNodeID(&intf.info.box))
|
|
|
|
themAddrString := net.IP(themAddr[:]).String()
|
|
|
|
themString := fmt.Sprintf("%s@%s", themAddrString, intf.info.remote)
|
2019-01-31 23:18:02 +00:00
|
|
|
intf.link.core.log.Infof("Connected %s: %s, source %s",
|
2019-02-03 15:50:25 -06:00
|
|
|
strings.ToUpper(intf.info.linkType), themString, intf.info.local)
|
2019-01-31 23:18:02 +00:00
|
|
|
// Start the link loop
|
2019-01-21 23:08:50 -06:00
|
|
|
go intf.peer.linkLoop()
|
|
|
|
// Start the writer
|
2019-01-30 20:58:23 -06:00
|
|
|
signalReady := make(chan struct{}, 1)
|
2019-02-02 22:41:51 -06:00
|
|
|
signalSent := make(chan bool, 1)
|
2019-02-02 22:18:55 -06:00
|
|
|
sendAck := make(chan struct{}, 1)
|
2019-03-09 18:08:26 -06:00
|
|
|
sendBlocked := time.NewTimer(time.Second)
|
|
|
|
defer util.TimerStop(sendBlocked)
|
|
|
|
util.TimerStop(sendBlocked)
|
2019-01-21 23:08:50 -06:00
|
|
|
go func() {
|
2019-01-30 20:58:23 -06:00
|
|
|
defer close(signalReady)
|
2019-02-02 22:18:55 -06:00
|
|
|
defer close(signalSent)
|
2019-01-21 23:08:50 -06:00
|
|
|
interval := 4 * time.Second
|
2019-02-02 22:18:55 -06:00
|
|
|
tcpTimer := time.NewTimer(interval) // used for backwards compat with old tcp
|
|
|
|
defer util.TimerStop(tcpTimer)
|
2019-08-18 12:17:54 -05:00
|
|
|
send := func(bss [][]byte) {
|
2019-03-09 18:08:26 -06:00
|
|
|
sendBlocked.Reset(time.Second)
|
2019-08-18 12:17:54 -05:00
|
|
|
size, _ := intf.msgIO.writeMsgs(bss)
|
2019-03-09 18:08:26 -06:00
|
|
|
util.TimerStop(sendBlocked)
|
2019-02-02 22:18:55 -06:00
|
|
|
select {
|
2019-08-18 12:17:54 -05:00
|
|
|
case signalSent <- size > 0:
|
2019-02-02 22:18:55 -06:00
|
|
|
default:
|
2019-01-21 23:08:50 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
// First try to send any link protocol traffic
|
|
|
|
select {
|
|
|
|
case msg := <-intf.peer.linkOut:
|
2019-08-18 12:17:54 -05:00
|
|
|
send([][]byte{msg})
|
2019-01-21 23:08:50 -06:00
|
|
|
continue
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
// No protocol traffic to send, so reset the timer
|
2019-02-02 22:18:55 -06:00
|
|
|
util.TimerStop(tcpTimer)
|
|
|
|
tcpTimer.Reset(interval)
|
2019-01-21 23:08:50 -06:00
|
|
|
// Now block until something is ready or the timer triggers keepalive traffic
|
|
|
|
select {
|
2019-02-02 22:18:55 -06:00
|
|
|
case <-tcpTimer.C:
|
2019-03-06 12:07:33 +00:00
|
|
|
intf.link.core.log.Tracef("Sending (legacy) keep-alive to %s: %s, source %s",
|
2019-02-02 22:18:55 -06:00
|
|
|
strings.ToUpper(intf.info.linkType), themString, intf.info.local)
|
2019-08-18 12:17:54 -05:00
|
|
|
send([][]byte{nil})
|
2019-02-02 22:18:55 -06:00
|
|
|
case <-sendAck:
|
2019-03-06 12:07:33 +00:00
|
|
|
intf.link.core.log.Tracef("Sending ack to %s: %s, source %s",
|
2019-02-02 22:18:55 -06:00
|
|
|
strings.ToUpper(intf.info.linkType), themString, intf.info.local)
|
2019-08-18 12:17:54 -05:00
|
|
|
send([][]byte{nil})
|
2019-01-21 23:08:50 -06:00
|
|
|
case msg := <-intf.peer.linkOut:
|
2019-08-18 12:17:54 -05:00
|
|
|
send([][]byte{msg})
|
|
|
|
case msgs, ok := <-out:
|
2019-01-21 23:08:50 -06:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2019-08-18 12:17:54 -05:00
|
|
|
send(msgs)
|
|
|
|
for _, msg := range msgs {
|
|
|
|
util.PutBytes(msg)
|
|
|
|
}
|
2019-01-30 20:58:23 -06:00
|
|
|
select {
|
|
|
|
case signalReady <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
2019-03-06 12:07:33 +00:00
|
|
|
//intf.link.core.log.Tracef("Sending packet to %s: %s, source %s",
|
2019-02-23 00:07:00 -06:00
|
|
|
// strings.ToUpper(intf.info.linkType), themString, intf.info.local)
|
2019-01-30 20:58:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
//intf.link.core.switchTable.idleIn <- intf.peer.port // notify switch that we're idle
|
|
|
|
// Used to enable/disable activity in the switch
|
2019-02-02 22:18:55 -06:00
|
|
|
signalAlive := make(chan bool, 1) // True = real packet, false = keep-alive
|
2019-01-30 20:58:23 -06:00
|
|
|
defer close(signalAlive)
|
2019-02-24 14:48:16 -06:00
|
|
|
ret := make(chan error, 1) // How we signal the return value when multiple goroutines are involved
|
2019-01-30 20:58:23 -06:00
|
|
|
go func() {
|
|
|
|
var isAlive bool
|
|
|
|
var isReady bool
|
2019-02-02 22:41:51 -06:00
|
|
|
var sendTimerRunning bool
|
|
|
|
var recvTimerRunning bool
|
2019-02-24 13:24:55 -06:00
|
|
|
recvTime := 6 * time.Second // TODO set to ReadTimeout from the config, reset if it gets changed
|
|
|
|
closeTime := 2 * switch_timeout // TODO or maybe this makes more sense for ReadTimeout?...
|
2019-02-02 22:41:51 -06:00
|
|
|
sendTime := time.Second
|
|
|
|
sendTimer := time.NewTimer(sendTime)
|
|
|
|
defer util.TimerStop(sendTimer)
|
|
|
|
recvTimer := time.NewTimer(recvTime)
|
|
|
|
defer util.TimerStop(recvTimer)
|
2019-02-24 13:24:55 -06:00
|
|
|
closeTimer := time.NewTimer(closeTime)
|
|
|
|
defer util.TimerStop(closeTimer)
|
2019-01-30 20:58:23 -06:00
|
|
|
for {
|
2019-02-23 00:07:00 -06:00
|
|
|
//intf.link.core.log.Debugf("State of %s: %s, source %s :: isAlive %t isReady %t sendTimerRunning %t recvTimerRunning %t",
|
|
|
|
// strings.ToUpper(intf.info.linkType), themString, intf.info.local,
|
|
|
|
// isAlive, isReady, sendTimerRunning, recvTimerRunning)
|
2019-01-30 20:58:23 -06:00
|
|
|
select {
|
2019-02-02 22:18:55 -06:00
|
|
|
case gotMsg, ok := <-signalAlive:
|
2019-01-30 20:58:23 -06:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2019-02-24 13:24:55 -06:00
|
|
|
util.TimerStop(closeTimer)
|
2019-02-24 14:48:16 -06:00
|
|
|
closeTimer.Reset(closeTime)
|
2019-02-15 19:35:10 -06:00
|
|
|
util.TimerStop(recvTimer)
|
|
|
|
recvTimerRunning = false
|
|
|
|
isAlive = true
|
|
|
|
if !isReady {
|
|
|
|
// (Re-)enable in the switch
|
2019-08-25 10:36:09 -05:00
|
|
|
intf.link.core.switchTable.RecvFrom(nil, func() {
|
2019-08-24 15:22:46 -05:00
|
|
|
intf.link.core.switchTable._idleIn(intf.peer.port)
|
|
|
|
})
|
2019-02-15 19:35:10 -06:00
|
|
|
isReady = true
|
2019-01-30 20:58:23 -06:00
|
|
|
}
|
2019-02-02 22:41:51 -06:00
|
|
|
if gotMsg && !sendTimerRunning {
|
|
|
|
// We got a message
|
|
|
|
// Start a timer, if it expires then send a 0-sized ack to let them know we're alive
|
|
|
|
util.TimerStop(sendTimer)
|
|
|
|
sendTimer.Reset(sendTime)
|
|
|
|
sendTimerRunning = true
|
2019-02-02 22:18:55 -06:00
|
|
|
}
|
2019-02-15 19:35:10 -06:00
|
|
|
if !gotMsg {
|
2019-03-06 12:07:33 +00:00
|
|
|
intf.link.core.log.Tracef("Received ack from %s: %s, source %s",
|
2019-02-15 19:35:10 -06:00
|
|
|
strings.ToUpper(intf.info.linkType), themString, intf.info.local)
|
|
|
|
}
|
2019-02-02 22:41:51 -06:00
|
|
|
case sentMsg, ok := <-signalSent:
|
2019-02-02 22:18:55 -06:00
|
|
|
// Stop any running ack timer
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2019-02-02 22:41:51 -06:00
|
|
|
util.TimerStop(sendTimer)
|
|
|
|
sendTimerRunning = false
|
|
|
|
if sentMsg && !recvTimerRunning {
|
|
|
|
// We sent a message
|
|
|
|
// Start a timer, if it expires and we haven't gotten any return traffic (including a 0-sized ack), then assume there's a problem
|
|
|
|
util.TimerStop(recvTimer)
|
|
|
|
recvTimer.Reset(recvTime)
|
|
|
|
recvTimerRunning = true
|
2019-02-02 22:18:55 -06:00
|
|
|
}
|
2019-01-30 20:58:23 -06:00
|
|
|
case _, ok := <-signalReady:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2019-02-15 19:35:10 -06:00
|
|
|
if !isAlive {
|
2019-01-30 20:58:23 -06:00
|
|
|
// Disable in the switch
|
|
|
|
isReady = false
|
|
|
|
} else {
|
|
|
|
// Keep enabled in the switch
|
2019-08-25 10:36:09 -05:00
|
|
|
intf.link.core.switchTable.RecvFrom(nil, func() {
|
2019-08-24 15:22:46 -05:00
|
|
|
intf.link.core.switchTable._idleIn(intf.peer.port)
|
|
|
|
})
|
2019-02-15 19:35:10 -06:00
|
|
|
isReady = true
|
2019-01-21 23:08:50 -06:00
|
|
|
}
|
2019-03-09 18:08:26 -06:00
|
|
|
case <-sendBlocked.C:
|
|
|
|
// We blocked while trying to send something
|
|
|
|
isReady = false
|
|
|
|
intf.link.core.switchTable.blockPeer(intf.peer.port)
|
2019-02-02 22:41:51 -06:00
|
|
|
case <-sendTimer.C:
|
|
|
|
// We haven't sent anything, so signal a send of a 0 packet to let them know we're alive
|
|
|
|
select {
|
|
|
|
case sendAck <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
case <-recvTimer.C:
|
|
|
|
// We haven't received anything, so assume there's a problem and don't return this node to the switch until they start responding
|
2019-01-30 20:58:23 -06:00
|
|
|
isAlive = false
|
2019-03-07 21:36:12 -06:00
|
|
|
intf.link.core.switchTable.blockPeer(intf.peer.port)
|
2019-02-24 13:24:55 -06:00
|
|
|
case <-closeTimer.C:
|
|
|
|
// We haven't received anything in a really long time, so things have died at the switch level and then some...
|
|
|
|
// Just close the connection at this point...
|
2019-02-24 14:48:16 -06:00
|
|
|
select {
|
|
|
|
case ret <- errors.New("timeout"):
|
|
|
|
default:
|
|
|
|
}
|
2019-02-24 13:24:55 -06:00
|
|
|
intf.msgIO.close()
|
2019-01-21 23:08:50 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
// Run reader loop
|
2019-08-25 10:36:09 -05:00
|
|
|
var helper phony.Inbox
|
2019-08-24 16:27:12 -05:00
|
|
|
done := make(chan struct{})
|
|
|
|
var helperFunc func()
|
|
|
|
helperFunc = func() {
|
|
|
|
// The helper reads in a loop and sends to the peer
|
|
|
|
// It loops by sending itself a message, which can be delayed by backpressure
|
|
|
|
// So if the peer is busy, backpressure will pause reading until the peer catches up
|
2019-02-24 14:48:16 -06:00
|
|
|
msg, err := intf.msgIO.readMsg()
|
2019-01-21 23:08:50 -06:00
|
|
|
if len(msg) > 0 {
|
2019-08-24 12:55:49 -05:00
|
|
|
// TODO rewrite this if the link becomes an actor
|
2019-08-24 16:27:12 -05:00
|
|
|
intf.peer.handlePacketFrom(&helper, msg)
|
2019-01-21 23:08:50 -06:00
|
|
|
}
|
|
|
|
if err != nil {
|
2019-02-24 14:48:16 -06:00
|
|
|
if err != io.EOF {
|
|
|
|
select {
|
|
|
|
case ret <- err:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
2019-08-24 16:27:12 -05:00
|
|
|
close(done)
|
|
|
|
return
|
2019-01-21 23:08:50 -06:00
|
|
|
}
|
2019-01-30 20:58:23 -06:00
|
|
|
select {
|
2019-02-02 22:18:55 -06:00
|
|
|
case signalAlive <- len(msg) > 0:
|
2019-01-30 20:58:23 -06:00
|
|
|
default:
|
|
|
|
}
|
2019-08-24 16:27:12 -05:00
|
|
|
// Now try to read again
|
2019-08-25 10:36:09 -05:00
|
|
|
helper.RecvFrom(nil, helperFunc)
|
2019-01-21 23:08:50 -06:00
|
|
|
}
|
2019-08-24 16:27:12 -05:00
|
|
|
// Start the read loop
|
2019-08-25 10:36:09 -05:00
|
|
|
helper.RecvFrom(nil, helperFunc)
|
2019-08-24 16:27:12 -05:00
|
|
|
<-done // Wait for the helper to exit
|
2019-01-21 23:08:50 -06:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2019-02-24 12:59:30 -06:00
|
|
|
// Remember to set `err` to something useful before returning
|
2019-02-24 14:48:16 -06:00
|
|
|
select {
|
|
|
|
case err = <-ret:
|
|
|
|
intf.link.core.log.Infof("Disconnected %s: %s, source %s; error: %s",
|
|
|
|
strings.ToUpper(intf.info.linkType), themString, intf.info.local, err)
|
|
|
|
default:
|
|
|
|
err = nil
|
|
|
|
intf.link.core.log.Infof("Disconnected %s: %s, source %s",
|
|
|
|
strings.ToUpper(intf.info.linkType), themString, intf.info.local)
|
|
|
|
}
|
2019-02-24 12:59:30 -06:00
|
|
|
return err
|
2019-01-21 23:08:50 -06:00
|
|
|
}
|