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
|
|
|
)
|
|
|
|
|
2020-05-23 10:23:55 -05:00
|
|
|
type links 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-09-19 19:15:59 -05:00
|
|
|
stopped chan struct{}
|
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
|
|
|
}
|
|
|
|
|
2020-05-23 10:23:55 -05:00
|
|
|
type linkMsgIO interface {
|
2019-01-21 21:27:52 -06:00
|
|
|
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 {
|
2020-05-16 17:07:47 -05:00
|
|
|
lname string
|
2020-05-23 10:23:55 -05:00
|
|
|
links *links
|
2019-08-26 18:37:38 -05:00
|
|
|
peer *peer
|
2020-05-23 10:23:55 -05:00
|
|
|
msgIO linkMsgIO
|
2019-08-26 18:37:38 -05:00
|
|
|
info linkInfo
|
|
|
|
incoming bool
|
|
|
|
force bool
|
|
|
|
closed chan struct{}
|
|
|
|
reader linkReader // Reads packets, notifies this linkInterface, passes packets to switch
|
|
|
|
writer linkWriter // Writes packets, notifies this linkInterface
|
|
|
|
phony.Inbox // Protects the below
|
|
|
|
sendTimer *time.Timer // Fires to signal that sending is blocked
|
|
|
|
keepAliveTimer *time.Timer // Fires to send keep-alive traffic
|
|
|
|
stallTimer *time.Timer // Fires to signal that no incoming traffic (including keep-alive) has been seen
|
|
|
|
closeTimer *time.Timer // Fires when the link has been idle so long we need to close it
|
2020-05-17 13:23:15 -05:00
|
|
|
isSending bool // True between a notifySending and a notifySent
|
2020-05-16 18:56:04 -05:00
|
|
|
blocked bool // True if we've blocked the peer in the switch
|
2019-01-04 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
2020-05-23 10:23:55 -05:00
|
|
|
func (l *links) 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-09-19 19:15:59 -05:00
|
|
|
l.stopped = make(chan struct{})
|
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
|
|
|
}
|
|
|
|
|
2020-05-23 10:23:55 -05:00
|
|
|
func (l *links) reconfigure() {
|
2019-08-28 19:31:04 +01:00
|
|
|
l.tcp.reconfigure()
|
2019-08-25 12:10:59 -05:00
|
|
|
}
|
|
|
|
|
2020-05-23 10:23:55 -05:00
|
|
|
func (l *links) call(uri string, sintf string) error {
|
2019-03-04 22:45:35 +00:00
|
|
|
u, err := url.Parse(uri)
|
|
|
|
if err != nil {
|
2019-09-18 15:01:19 +01:00
|
|
|
return fmt.Errorf("peer %s is not correctly formatted (%s)", uri, err)
|
2019-03-04 22:45:35 +00:00
|
|
|
}
|
|
|
|
pathtokens := strings.Split(strings.Trim(u.Path, "/"), "/")
|
|
|
|
switch u.Scheme {
|
|
|
|
case "tcp":
|
2019-10-23 17:26:35 +01:00
|
|
|
l.tcp.call(u.Host, nil, sintf, nil)
|
2019-03-04 22:45:35 +00:00
|
|
|
case "socks":
|
2019-10-23 17:26:35 +01:00
|
|
|
l.tcp.call(pathtokens[0], u.Host, sintf, nil)
|
|
|
|
case "tls":
|
|
|
|
l.tcp.call(u.Host, nil, sintf, l.tcp.tls.forDialer)
|
2019-03-04 22:45:35 +00:00
|
|
|
default:
|
|
|
|
return errors.New("unknown call scheme: " + u.Scheme)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-23 10:23:55 -05:00
|
|
|
func (l *links) listen(uri string) error {
|
2019-03-04 22:45:35 +00:00
|
|
|
u, err := url.Parse(uri)
|
|
|
|
if err != nil {
|
2019-09-18 15:01:19 +01:00
|
|
|
return fmt.Errorf("listener %s is not correctly formatted (%s)", uri, err)
|
2019-03-04 22:45:35 +00:00
|
|
|
}
|
|
|
|
switch u.Scheme {
|
|
|
|
case "tcp":
|
2019-10-23 17:26:35 +01:00
|
|
|
_, err := l.tcp.listen(u.Host, nil)
|
|
|
|
return err
|
|
|
|
case "tls":
|
|
|
|
_, err := l.tcp.listen(u.Host, l.tcp.tls.forListener)
|
2019-03-06 11:06:13 +00:00
|
|
|
return err
|
2019-03-04 22:45:35 +00:00
|
|
|
default:
|
|
|
|
return errors.New("unknown listen scheme: " + u.Scheme)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-23 10:23:55 -05:00
|
|
|
func (l *links) create(msgIO linkMsgIO, name, linkType, local, remote string, incoming, force bool) (*linkInterface, error) {
|
2019-11-29 11:45:02 +02:00
|
|
|
// Technically anything unique would work for names, but let's pick something human readable, just for debugging
|
2019-01-19 00:14:10 +00:00
|
|
|
intf := linkInterface{
|
2020-05-16 17:07:47 -05:00
|
|
|
lname: name,
|
2020-05-23 10:23:55 -05:00
|
|
|
links: l,
|
2019-01-21 23:08:50 -06:00
|
|
|
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-08-25 22:55:17 -05:00
|
|
|
intf.writer.intf = &intf
|
2020-05-17 08:34:22 -05:00
|
|
|
intf.writer.worker = make(chan [][]byte, 1)
|
2019-08-25 22:55:17 -05:00
|
|
|
intf.reader.intf = &intf
|
|
|
|
intf.reader.err = make(chan error)
|
2019-01-19 12:19:24 +00:00
|
|
|
return &intf, nil
|
|
|
|
}
|
|
|
|
|
2020-05-23 10:23:55 -05:00
|
|
|
func (l *links) stop() error {
|
2019-09-19 19:15:59 -05:00
|
|
|
close(l.stopped)
|
2019-09-18 16:32:22 +01:00
|
|
|
if err := l.tcp.stop(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return 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
|
2020-05-17 08:34:22 -05:00
|
|
|
go func() {
|
|
|
|
for bss := range intf.writer.worker {
|
|
|
|
intf.msgIO.writeMsgs(bss)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
defer intf.writer.Act(nil, func() {
|
|
|
|
intf.writer.closed = true
|
|
|
|
close(intf.writer.worker)
|
|
|
|
})
|
2019-01-21 23:08:50 -06:00
|
|
|
myLinkPub, myLinkPriv := crypto.NewBoxKeys()
|
|
|
|
meta := version_getBaseMetadata()
|
2020-05-23 10:23:55 -05:00
|
|
|
meta.box = intf.links.core.boxPub
|
|
|
|
meta.sig = intf.links.core.sigPub
|
2019-01-21 23:08:50 -06:00
|
|
|
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 {
|
2020-05-23 10:23:55 -05:00
|
|
|
intf.links.core.log.Errorln("Failed to connect to node: " + intf.lname + " 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
|
2020-05-23 10:23:55 -05:00
|
|
|
if intf.incoming && !intf.force && !intf.links.core.peers.isAllowedEncryptionPublicKey(&meta.box) {
|
|
|
|
intf.links.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
|
2020-05-23 10:23:55 -05:00
|
|
|
intf.links.mutex.Lock()
|
|
|
|
if oldIntf, isIn := intf.links.interfaces[intf.info]; isIn {
|
|
|
|
intf.links.mutex.Unlock()
|
2019-01-22 21:16:41 -06:00
|
|
|
// 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.
|
2020-05-23 10:23:55 -05:00
|
|
|
intf.links.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{})
|
2020-05-23 10:23:55 -05:00
|
|
|
intf.links.interfaces[intf.info] = intf
|
2019-01-22 21:48:43 -06:00
|
|
|
defer func() {
|
2020-05-23 10:23:55 -05:00
|
|
|
intf.links.mutex.Lock()
|
|
|
|
delete(intf.links.interfaces, intf.info)
|
|
|
|
intf.links.mutex.Unlock()
|
2019-08-25 22:55:17 -05:00
|
|
|
close(intf.closed)
|
2019-01-22 21:48:43 -06:00
|
|
|
}()
|
2020-05-23 10:23:55 -05:00
|
|
|
intf.links.core.log.Debugln("DEBUG: registered interface for", intf.name)
|
2019-01-22 21:16:41 -06:00
|
|
|
}
|
2020-05-23 10:23:55 -05:00
|
|
|
intf.links.mutex.Unlock()
|
2019-01-22 21:16:41 -06:00
|
|
|
// Create peer
|
2019-01-21 23:08:50 -06:00
|
|
|
shared := crypto.GetSharedKey(myLinkPriv, &meta.link)
|
2020-05-23 10:23:55 -05:00
|
|
|
phony.Block(&intf.links.core.peers, func() {
|
2020-05-02 11:16:11 -05:00
|
|
|
// FIXME don't use phony.Block, it's bad practice, even if it's safe here
|
2020-05-23 10:23:55 -05:00
|
|
|
intf.peer = intf.links.core.peers._newPeer(&meta.box, &meta.sig, shared, intf)
|
2020-05-02 11:16:11 -05:00
|
|
|
})
|
|
|
|
if intf.peer == nil {
|
|
|
|
return errors.New("failed to create peer")
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
// More cleanup can go here
|
|
|
|
intf.peer.Act(nil, intf.peer._removeSelf)
|
|
|
|
}()
|
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)
|
2020-05-23 10:23:55 -05:00
|
|
|
intf.links.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-08-25 22:55:17 -05:00
|
|
|
// Start things
|
2019-08-25 17:00:02 -05:00
|
|
|
go intf.peer.start()
|
2020-05-16 18:56:04 -05:00
|
|
|
intf.Act(nil, intf._notifyIdle)
|
2019-08-27 19:43:54 -05:00
|
|
|
intf.reader.Act(nil, intf.reader._read)
|
2019-08-25 22:55:17 -05:00
|
|
|
// Wait for the reader to finish
|
2019-09-19 19:15:59 -05:00
|
|
|
// TODO find a way to do this without keeping live goroutines around
|
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
|
|
|
go func() {
|
|
|
|
select {
|
2020-05-23 10:23:55 -05:00
|
|
|
case <-intf.links.stopped:
|
2019-09-19 19:15:59 -05:00
|
|
|
intf.msgIO.close()
|
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
}()
|
2019-08-25 22:55:17 -05:00
|
|
|
err = <-intf.reader.err
|
2019-09-19 19:15:59 -05:00
|
|
|
// TODO don't report an error if it's just a 'use of closed network connection'
|
2019-08-25 22:55:17 -05:00
|
|
|
if err != nil {
|
2020-05-23 10:23:55 -05:00
|
|
|
intf.links.core.log.Infof("Disconnected %s: %s, source %s; error: %s",
|
2019-02-24 14:48:16 -06:00
|
|
|
strings.ToUpper(intf.info.linkType), themString, intf.info.local, err)
|
2019-08-25 22:55:17 -05:00
|
|
|
} else {
|
2020-05-23 10:23:55 -05:00
|
|
|
intf.links.core.log.Infof("Disconnected %s: %s, source %s",
|
2019-02-24 14:48:16 -06:00
|
|
|
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
|
|
|
}
|
2019-08-25 22:19:20 -05:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2020-05-16 17:07:47 -05:00
|
|
|
// linkInterface needs to match the peerInterface type needed by the peers
|
|
|
|
|
|
|
|
func (intf *linkInterface) out(bss [][]byte) {
|
|
|
|
intf.Act(nil, func() {
|
|
|
|
// nil to prevent it from blocking if the link is somehow frozen
|
|
|
|
// this is safe because another packet won't be sent until the link notifies
|
|
|
|
// the peer that it's ready for one
|
2020-05-23 10:08:23 -05:00
|
|
|
intf.writer.sendFrom(nil, bss)
|
2020-05-16 17:07:47 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (intf *linkInterface) linkOut(bs []byte) {
|
|
|
|
intf.Act(nil, func() {
|
|
|
|
// nil to prevent it from blocking if the link is somehow frozen
|
|
|
|
// FIXME this is hypothetically not safe, the peer shouldn't be sending
|
|
|
|
// additional packets until this one finishes, otherwise this could leak
|
|
|
|
// memory if writing happens slower than link packets are generated...
|
|
|
|
// that seems unlikely, so it's a lesser evil than deadlocking for now
|
2020-05-23 10:08:23 -05:00
|
|
|
intf.writer.sendFrom(nil, [][]byte{bs})
|
2020-05-16 17:07:47 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (intf *linkInterface) notifyQueued(seq uint64) {
|
|
|
|
// This is the part where we want non-nil 'from' fields
|
|
|
|
intf.Act(intf.peer, func() {
|
2020-05-17 13:23:15 -05:00
|
|
|
if intf.isSending {
|
2020-05-16 17:07:47 -05:00
|
|
|
intf.peer.dropFromQueue(intf, seq)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (intf *linkInterface) close() {
|
|
|
|
intf.Act(nil, func() { intf.msgIO.close() })
|
|
|
|
}
|
|
|
|
|
|
|
|
func (intf *linkInterface) name() string {
|
|
|
|
return intf.lname
|
|
|
|
}
|
|
|
|
|
|
|
|
func (intf *linkInterface) local() string {
|
|
|
|
return intf.info.local
|
|
|
|
}
|
|
|
|
|
|
|
|
func (intf *linkInterface) remote() string {
|
|
|
|
return intf.info.remote
|
|
|
|
}
|
|
|
|
|
|
|
|
func (intf *linkInterface) interfaceType() string {
|
|
|
|
return intf.info.linkType
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2019-08-25 22:19:20 -05:00
|
|
|
const (
|
2019-08-26 18:37:38 -05:00
|
|
|
sendTime = 1 * time.Second // How long to wait before deciding a send is blocked
|
|
|
|
keepAliveTime = 2 * time.Second // How long to wait before sending a keep-alive response if we have no real traffic to send
|
|
|
|
stallTime = 6 * time.Second // How long to wait for response traffic before deciding the connection has stalled
|
|
|
|
closeTime = 2 * switch_timeout // How long to wait before closing the link
|
2019-08-25 22:19:20 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// notify the intf that we're currently sending
|
2020-05-23 10:08:23 -05:00
|
|
|
func (intf *linkInterface) notifySending(size int) {
|
2019-08-27 19:43:54 -05:00
|
|
|
intf.Act(&intf.writer, func() {
|
2020-05-17 13:23:15 -05:00
|
|
|
intf.isSending = true
|
2019-08-26 18:37:38 -05:00
|
|
|
intf.sendTimer = time.AfterFunc(sendTime, intf.notifyBlockedSend)
|
|
|
|
intf._cancelStallTimer()
|
2019-08-25 22:55:17 -05:00
|
|
|
})
|
2019-08-25 22:19:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// we just sent something, so cancel any pending timer to send keep-alive traffic
|
2019-08-26 18:37:38 -05:00
|
|
|
func (intf *linkInterface) _cancelStallTimer() {
|
|
|
|
if intf.stallTimer != nil {
|
|
|
|
intf.stallTimer.Stop()
|
|
|
|
intf.stallTimer = nil
|
|
|
|
}
|
2019-08-25 22:19:20 -05:00
|
|
|
}
|
|
|
|
|
2019-09-25 17:53:25 -05:00
|
|
|
// This gets called from a time.AfterFunc, and notifies the switch that we appear
|
|
|
|
// to have gotten blocked on a write, so the switch should start routing traffic
|
|
|
|
// through other links, if alternatives exist
|
2019-08-25 22:19:20 -05:00
|
|
|
func (intf *linkInterface) notifyBlockedSend() {
|
2019-09-25 17:53:25 -05:00
|
|
|
intf.Act(nil, func() {
|
2020-05-16 18:56:04 -05:00
|
|
|
if intf.sendTimer != nil && !intf.blocked {
|
2019-08-25 22:55:17 -05:00
|
|
|
//As far as we know, we're still trying to send, and the timer fired.
|
2020-05-16 18:56:04 -05:00
|
|
|
intf.blocked = true
|
2020-05-23 10:23:55 -05:00
|
|
|
intf.links.core.switchTable.blockPeer(intf, intf.peer.port)
|
2019-08-25 22:55:17 -05:00
|
|
|
}
|
|
|
|
})
|
2019-08-25 22:19:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// notify the intf that we've finished sending, returning the peer to the switch
|
2020-05-23 10:08:23 -05:00
|
|
|
func (intf *linkInterface) notifySent(size int) {
|
2019-08-27 19:43:54 -05:00
|
|
|
intf.Act(&intf.writer, func() {
|
2020-05-23 10:08:23 -05:00
|
|
|
if intf.sendTimer != nil {
|
|
|
|
intf.sendTimer.Stop()
|
|
|
|
intf.sendTimer = nil
|
|
|
|
}
|
|
|
|
if intf.keepAliveTimer != nil {
|
|
|
|
// TODO? unset this when we start sending, not when we finish...
|
|
|
|
intf.keepAliveTimer.Stop()
|
|
|
|
intf.keepAliveTimer = nil
|
2019-08-25 22:55:17 -05:00
|
|
|
}
|
2020-05-23 10:08:23 -05:00
|
|
|
intf._notifyIdle()
|
2020-05-17 13:23:15 -05:00
|
|
|
intf.isSending = false
|
2019-08-25 22:55:17 -05:00
|
|
|
if size > 0 && intf.stallTimer == nil {
|
|
|
|
intf.stallTimer = time.AfterFunc(stallTime, intf.notifyStalled)
|
|
|
|
}
|
|
|
|
})
|
2019-08-25 22:19:20 -05:00
|
|
|
}
|
|
|
|
|
2020-05-16 18:56:04 -05:00
|
|
|
// Notify the peer that we're ready for more traffic
|
2020-04-03 00:32:26 -05:00
|
|
|
func (intf *linkInterface) _notifyIdle() {
|
2020-05-23 10:08:23 -05:00
|
|
|
intf.peer.Act(intf, intf.peer._handleIdle)
|
2019-08-25 22:19:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the peer as stalled, to prevent them from returning to the switch until a read succeeds
|
|
|
|
func (intf *linkInterface) notifyStalled() {
|
2019-08-27 19:43:54 -05:00
|
|
|
intf.Act(nil, func() { // Sent from a time.AfterFunc
|
2020-05-16 18:56:04 -05:00
|
|
|
if intf.stallTimer != nil && !intf.blocked {
|
2019-08-26 18:37:38 -05:00
|
|
|
intf.stallTimer.Stop()
|
2019-08-25 22:55:17 -05:00
|
|
|
intf.stallTimer = nil
|
2020-05-16 18:56:04 -05:00
|
|
|
intf.blocked = true
|
2020-05-23 10:23:55 -05:00
|
|
|
intf.links.core.switchTable.blockPeer(intf, intf.peer.port)
|
2019-08-25 22:55:17 -05:00
|
|
|
}
|
|
|
|
})
|
2019-08-25 22:19:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// reset the close timer
|
2019-08-26 18:37:38 -05:00
|
|
|
func (intf *linkInterface) notifyReading() {
|
2019-08-27 19:43:54 -05:00
|
|
|
intf.Act(&intf.reader, func() {
|
2019-08-25 22:55:17 -05:00
|
|
|
if intf.closeTimer != nil {
|
|
|
|
intf.closeTimer.Stop()
|
|
|
|
}
|
|
|
|
intf.closeTimer = time.AfterFunc(closeTime, func() { intf.msgIO.close() })
|
|
|
|
})
|
2019-08-25 22:19:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// wake up the link if it was stalled, and (if size > 0) prepare to send keep-alive traffic
|
2019-08-26 18:37:38 -05:00
|
|
|
func (intf *linkInterface) notifyRead(size int) {
|
2019-08-27 19:43:54 -05:00
|
|
|
intf.Act(&intf.reader, func() {
|
2019-08-25 22:55:17 -05:00
|
|
|
if intf.stallTimer != nil {
|
|
|
|
intf.stallTimer.Stop()
|
|
|
|
intf.stallTimer = nil
|
|
|
|
}
|
2020-05-23 10:08:23 -05:00
|
|
|
if size > 0 && intf.keepAliveTimer == nil {
|
|
|
|
intf.keepAliveTimer = time.AfterFunc(keepAliveTime, intf.notifyDoKeepAlive)
|
2019-08-25 22:55:17 -05:00
|
|
|
}
|
2020-05-16 18:56:04 -05:00
|
|
|
if intf.blocked {
|
|
|
|
intf.blocked = false
|
2020-05-23 10:23:55 -05:00
|
|
|
intf.links.core.switchTable.unblockPeer(intf, intf.peer.port)
|
2020-05-16 18:56:04 -05:00
|
|
|
}
|
2019-08-25 22:55:17 -05:00
|
|
|
})
|
2019-08-25 22:19:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// We need to send keep-alive traffic now
|
|
|
|
func (intf *linkInterface) notifyDoKeepAlive() {
|
2019-08-27 19:43:54 -05:00
|
|
|
intf.Act(nil, func() { // Sent from a time.AfterFunc
|
2019-08-26 18:37:38 -05:00
|
|
|
if intf.stallTimer != nil {
|
|
|
|
intf.stallTimer.Stop()
|
|
|
|
intf.stallTimer = nil
|
2020-05-23 10:08:23 -05:00
|
|
|
intf.writer.sendFrom(nil, [][]byte{nil}) // Empty keep-alive traffic
|
2019-08-25 22:55:17 -05:00
|
|
|
}
|
|
|
|
})
|
2019-08-25 22:19:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type linkWriter struct {
|
2019-08-25 22:55:17 -05:00
|
|
|
phony.Inbox
|
2020-05-17 08:22:02 -05:00
|
|
|
intf *linkInterface
|
|
|
|
worker chan [][]byte
|
2020-05-17 08:34:22 -05:00
|
|
|
closed bool
|
2019-08-25 22:19:20 -05:00
|
|
|
}
|
|
|
|
|
2020-05-23 10:08:23 -05:00
|
|
|
func (w *linkWriter) sendFrom(from phony.Actor, bss [][]byte) {
|
2019-08-27 19:43:54 -05:00
|
|
|
w.Act(from, func() {
|
2020-05-17 08:34:22 -05:00
|
|
|
if w.closed {
|
|
|
|
return
|
|
|
|
}
|
2019-08-25 22:55:17 -05:00
|
|
|
var size int
|
|
|
|
for _, bs := range bss {
|
|
|
|
size += len(bs)
|
|
|
|
}
|
2020-05-23 10:08:23 -05:00
|
|
|
w.intf.notifySending(size)
|
2020-05-17 08:34:22 -05:00
|
|
|
w.worker <- bss
|
2020-05-23 10:08:23 -05:00
|
|
|
w.intf.notifySent(size)
|
2019-08-25 22:55:17 -05:00
|
|
|
})
|
2019-08-25 22:19:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type linkReader struct {
|
2019-08-25 22:55:17 -05:00
|
|
|
phony.Inbox
|
|
|
|
intf *linkInterface
|
|
|
|
err chan error
|
2019-08-25 22:19:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *linkReader) _read() {
|
2019-08-26 18:37:38 -05:00
|
|
|
r.intf.notifyReading()
|
2019-08-25 22:55:17 -05:00
|
|
|
msg, err := r.intf.msgIO.readMsg()
|
2019-08-26 18:37:38 -05:00
|
|
|
r.intf.notifyRead(len(msg))
|
2019-08-25 22:55:17 -05:00
|
|
|
if len(msg) > 0 {
|
|
|
|
r.intf.peer.handlePacketFrom(r, msg)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
r.err <- err
|
2019-08-25 22:19:20 -05:00
|
|
|
}
|
2019-08-25 22:55:17 -05:00
|
|
|
close(r.err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Now try to read again
|
2019-08-27 19:43:54 -05:00
|
|
|
r.Act(nil, r._read)
|
2019-08-25 22:19:20 -05:00
|
|
|
}
|