2019-01-04 17:14:40 +00:00
|
|
|
package yggdrasil
|
|
|
|
|
|
|
|
import (
|
2019-01-05 12:06:45 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-01-04 17:23:37 +00:00
|
|
|
"sync"
|
2019-01-05 12:06:45 +00:00
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
2019-01-04 17:23:37 +00:00
|
|
|
|
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
|
2019-01-05 12:06:45 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/util"
|
2019-01-04 17:14:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type awdl struct {
|
2019-01-04 17:23:37 +00:00
|
|
|
core *Core
|
|
|
|
mutex sync.RWMutex // protects interfaces below
|
|
|
|
interfaces map[string]*awdlInterface
|
2019-01-04 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type awdlInterface struct {
|
2019-01-04 17:23:37 +00:00
|
|
|
awdl *awdl
|
2019-01-05 12:06:45 +00:00
|
|
|
fromAWDL chan []byte
|
|
|
|
toAWDL chan []byte
|
2019-01-04 17:23:37 +00:00
|
|
|
shutdown chan bool
|
|
|
|
peer *peer
|
2019-01-04 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *awdl) init(c *Core) error {
|
2019-01-04 17:23:37 +00:00
|
|
|
l.core = c
|
|
|
|
l.mutex.Lock()
|
|
|
|
l.interfaces = make(map[string]*awdlInterface)
|
|
|
|
l.mutex.Unlock()
|
2019-01-04 17:14:40 +00:00
|
|
|
|
2019-01-04 17:23:37 +00:00
|
|
|
return nil
|
2019-01-04 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 22:57:37 +00:00
|
|
|
func (l *awdl) create(fromAWDL chan []byte, toAWDL chan []byte /*boxPubKey *crypto.BoxPubKey, sigPubKey *crypto.SigPubKey*/, name string) (*awdlInterface, error) {
|
2019-01-04 17:23:37 +00:00
|
|
|
intf := awdlInterface{
|
2019-01-05 23:00:49 +00:00
|
|
|
awdl: l,
|
2019-01-06 14:12:10 +00:00
|
|
|
fromAWDL: fromAWDL,
|
|
|
|
toAWDL: toAWDL,
|
2019-01-04 17:23:37 +00:00
|
|
|
shutdown: make(chan bool),
|
|
|
|
}
|
2019-01-13 22:57:37 +00:00
|
|
|
l.mutex.Lock()
|
|
|
|
l.interfaces[name] = &intf
|
|
|
|
l.mutex.Unlock()
|
|
|
|
myLinkPub, myLinkPriv := crypto.NewBoxKeys()
|
|
|
|
meta := version_getBaseMetadata()
|
|
|
|
meta.box = l.core.boxPub
|
|
|
|
meta.sig = l.core.sigPub
|
|
|
|
meta.link = *myLinkPub
|
|
|
|
metaBytes := meta.encode()
|
|
|
|
l.core.log.Println("toAWDL <- metaBytes")
|
|
|
|
toAWDL <- metaBytes
|
|
|
|
l.core.log.Println("metaBytes = <-fromAWDL")
|
|
|
|
metaBytes = <-fromAWDL
|
|
|
|
l.core.log.Println("version_metadata{}")
|
|
|
|
meta = version_metadata{}
|
|
|
|
if !meta.decode(metaBytes) || !meta.check() {
|
|
|
|
return nil, errors.New("Metadata decode failure")
|
|
|
|
}
|
|
|
|
l.core.log.Println("version_getBaseMetadata{}")
|
|
|
|
base := version_getBaseMetadata()
|
|
|
|
if meta.ver > base.ver || meta.ver == base.ver && meta.minorVer > base.minorVer {
|
|
|
|
return nil, errors.New("Failed to connect to node: " + name + " version: " + fmt.Sprintf("%d.%d", meta.ver, meta.minorVer))
|
|
|
|
}
|
|
|
|
l.core.log.Println("crypto.GetSharedKey")
|
|
|
|
shared := crypto.GetSharedKey(myLinkPriv, &meta.link)
|
|
|
|
//shared := crypto.GetSharedKey(&l.core.boxPriv, boxPubKey)
|
|
|
|
l.core.log.Println("l.core.peers.newPeer")
|
|
|
|
intf.peer = l.core.peers.newPeer(&meta.box, &meta.sig, shared, name)
|
2019-01-04 17:23:37 +00:00
|
|
|
if intf.peer != nil {
|
2019-01-05 12:06:45 +00:00
|
|
|
intf.peer.linkOut = make(chan []byte, 1) // protocol traffic
|
2019-01-04 17:23:37 +00:00
|
|
|
intf.peer.out = func(msg []byte) {
|
|
|
|
defer func() { recover() }()
|
2019-01-05 12:06:45 +00:00
|
|
|
intf.toAWDL <- msg
|
|
|
|
} // called by peer.sendPacket()
|
|
|
|
l.core.switchTable.idleIn <- intf.peer.port // notify switch that we're idle
|
2019-01-05 00:52:41 +00:00
|
|
|
intf.peer.close = func() {
|
2019-01-05 12:06:45 +00:00
|
|
|
close(intf.fromAWDL)
|
|
|
|
close(intf.toAWDL)
|
2019-01-05 00:52:41 +00:00
|
|
|
}
|
2019-01-06 14:12:10 +00:00
|
|
|
go intf.handler()
|
|
|
|
go intf.peer.linkLoop()
|
2019-01-05 12:06:45 +00:00
|
|
|
return &intf, nil
|
2019-01-04 17:23:37 +00:00
|
|
|
}
|
2019-01-13 22:57:37 +00:00
|
|
|
delete(l.interfaces, name)
|
2019-01-05 12:06:45 +00:00
|
|
|
return nil, errors.New("l.core.peers.newPeer failed")
|
2019-01-04 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *awdl) getInterface(identity string) *awdlInterface {
|
2019-01-04 17:23:37 +00:00
|
|
|
l.mutex.RLock()
|
|
|
|
defer l.mutex.RUnlock()
|
|
|
|
if intf, ok := l.interfaces[identity]; ok {
|
|
|
|
return intf
|
|
|
|
}
|
|
|
|
return nil
|
2019-01-04 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
2019-01-05 12:06:45 +00:00
|
|
|
func (l *awdl) shutdown(identity string) error {
|
2019-01-04 17:23:37 +00:00
|
|
|
if intf, ok := l.interfaces[identity]; ok {
|
|
|
|
intf.shutdown <- true
|
|
|
|
l.core.peers.removePeer(intf.peer.port)
|
|
|
|
l.mutex.Lock()
|
|
|
|
delete(l.interfaces, identity)
|
|
|
|
l.mutex.Unlock()
|
2019-01-05 12:06:45 +00:00
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return errors.New(fmt.Sprintf("Interface '%s' doesn't exist or already shutdown", identity))
|
2019-01-04 17:23:37 +00:00
|
|
|
}
|
2019-01-04 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ai *awdlInterface) handler() {
|
2019-01-05 12:06:45 +00:00
|
|
|
send := func(msg []byte) {
|
|
|
|
ai.toAWDL <- msg
|
|
|
|
atomic.AddUint64(&ai.peer.bytesSent, uint64(len(msg)))
|
|
|
|
util.PutBytes(msg)
|
|
|
|
}
|
2019-01-04 17:23:37 +00:00
|
|
|
for {
|
2019-01-05 12:06:45 +00:00
|
|
|
timerInterval := tcp_ping_interval
|
2019-01-04 17:41:03 +00:00
|
|
|
timer := time.NewTimer(timerInterval)
|
2019-01-05 12:06:45 +00:00
|
|
|
defer timer.Stop()
|
2019-01-04 17:23:37 +00:00
|
|
|
select {
|
|
|
|
case p := <-ai.peer.linkOut:
|
2019-01-05 12:06:45 +00:00
|
|
|
send(p)
|
2019-01-04 17:41:03 +00:00
|
|
|
continue
|
|
|
|
default:
|
|
|
|
}
|
2019-01-05 12:06:45 +00:00
|
|
|
timer.Stop()
|
2019-01-04 17:41:03 +00:00
|
|
|
select {
|
|
|
|
case <-timer.C:
|
|
|
|
default:
|
|
|
|
}
|
2019-01-05 12:06:45 +00:00
|
|
|
timer.Reset(timerInterval)
|
2019-01-04 17:41:03 +00:00
|
|
|
select {
|
2019-01-05 12:06:45 +00:00
|
|
|
case _ = <-timer.C:
|
2019-01-05 21:59:07 +00:00
|
|
|
send([]byte{})
|
2019-01-05 12:06:45 +00:00
|
|
|
case p := <-ai.peer.linkOut:
|
|
|
|
send(p)
|
|
|
|
continue
|
|
|
|
case r := <-ai.fromAWDL:
|
2019-01-04 17:23:37 +00:00
|
|
|
ai.peer.handlePacket(r)
|
2019-01-05 12:06:45 +00:00
|
|
|
ai.awdl.core.switchTable.idleIn <- ai.peer.port
|
2019-01-04 17:23:37 +00:00
|
|
|
case <-ai.shutdown:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-01-04 17:14:40 +00:00
|
|
|
}
|