165 lines
3.9 KiB
Go
Raw Normal View History

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"
)
type link struct {
2019-01-04 17:23:37 +00:00
core *Core
mutex sync.RWMutex // protects interfaces below
interfaces map[string]*linkInterface
}
type linkInterface struct {
name string
link *link
fromlink chan []byte
tolink chan []byte
2019-01-04 17:23:37 +00:00
shutdown chan bool
peer *peer
stream stream
}
func (l *link) init(c *Core) error {
2019-01-04 17:23:37 +00:00
l.core = c
l.mutex.Lock()
l.interfaces = make(map[string]*linkInterface)
2019-01-04 17:23:37 +00:00
l.mutex.Unlock()
if err := l.core.awdl.init(c); err != nil {
l.core.log.Println("Failed to start AWDL interface")
return err
}
2019-01-04 17:23:37 +00:00
return nil
}
func (l *link) create(fromlink chan []byte, tolink chan []byte, name string) (*linkInterface, error) {
l.mutex.Lock()
defer l.mutex.Unlock()
if _, ok := l.interfaces[name]; ok {
return nil, errors.New("Interface with this name already exists")
}
intf := linkInterface{
name: name,
link: l,
fromlink: fromlink,
tolink: tolink,
2019-01-04 17:23:37 +00:00
shutdown: make(chan bool),
}
l.interfaces[intf.name] = &intf
go intf.start()
return &intf, nil
}
func (intf *linkInterface) start() {
2019-01-13 22:57:37 +00:00
myLinkPub, myLinkPriv := crypto.NewBoxKeys()
meta := version_getBaseMetadata()
meta.box = intf.link.core.boxPub
meta.sig = intf.link.core.sigPub
2019-01-13 22:57:37 +00:00
meta.link = *myLinkPub
metaBytes := meta.encode()
//intf.link.core.log.Println("start: intf.tolink <- metaBytes")
intf.tolink <- metaBytes
//intf.link.core.log.Println("finish: intf.tolink <- metaBytes")
//intf.link.core.log.Println("start: metaBytes = <-intf.fromlink")
metaBytes = <-intf.fromlink
//intf.link.core.log.Println("finish: metaBytes = <-intf.fromlink")
2019-01-13 22:57:37 +00:00
meta = version_metadata{}
if !meta.decode(metaBytes) || !meta.check() {
intf.link.core.log.Println("Metadata decode failure")
return
2019-01-13 22:57:37 +00:00
}
base := version_getBaseMetadata()
if meta.ver > base.ver || meta.ver == base.ver && meta.minorVer > base.minorVer {
intf.link.core.log.Println("Failed to connect to node: " + intf.name + " version: " + fmt.Sprintf("%d.%d", meta.ver, meta.minorVer))
return
2019-01-13 22:57:37 +00:00
}
shared := crypto.GetSharedKey(myLinkPriv, &meta.link)
intf.peer = intf.link.core.peers.newPeer(&meta.box, &meta.sig, shared, intf.name)
if intf.peer == nil {
intf.link.mutex.Lock()
delete(intf.link.interfaces, intf.name)
intf.link.mutex.Unlock()
return
}
intf.peer.linkOut = make(chan []byte, 1) // protocol traffic
intf.peer.out = func(msg []byte) {
defer func() { recover() }()
intf.tolink <- msg
} // called by peer.sendPacket()
intf.link.core.switchTable.idleIn <- intf.peer.port // notify switch that we're idle
intf.peer.close = func() {
close(intf.fromlink)
close(intf.tolink)
2019-01-04 17:23:37 +00:00
}
go intf.handler()
go intf.peer.linkLoop()
}
func (l *link) getInterface(identity string) *linkInterface {
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
}
func (l *link) 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 fmt.Errorf("interface '%s' doesn't exist or already shutdown", identity)
2019-01-04 17:23:37 +00:00
}
}
func (ai *linkInterface) handler() {
2019-01-05 12:06:45 +00:00
send := func(msg []byte) {
ai.tolink <- msg
2019-01-05 12:06:45 +00:00
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
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)
continue
default:
}
2019-01-05 12:06:45 +00:00
timer.Stop()
select {
case <-timer.C:
default:
}
2019-01-05 12:06:45 +00:00
timer.Reset(timerInterval)
select {
2019-01-05 12:06:45 +00:00
case _ = <-timer.C:
send([]byte{})
2019-01-05 12:06:45 +00:00
case p := <-ai.peer.linkOut:
send(p)
continue
case r := <-ai.fromlink:
2019-01-04 17:23:37 +00:00
ai.peer.handlePacket(r)
ai.link.core.switchTable.idleIn <- ai.peer.port
2019-01-04 17:23:37 +00:00
case <-ai.shutdown:
return
}
}
}