5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-29 21:21:35 +00:00
This commit is contained in:
Neil Alexander 2019-01-04 17:23:37 +00:00
parent f29a098488
commit 3878197a59
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944

View File

@ -1,86 +1,87 @@
package yggdrasil package yggdrasil
import ( import (
"sync" "sync"
"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
) )
type awdl struct { type awdl struct {
core *Core core *Core
mutex sync.RWMutex // protects interfaces below mutex sync.RWMutex // protects interfaces below
interfaces map[string]*awdlInterface interfaces map[string]*awdlInterface
} }
type awdlInterface struct { type awdlInterface struct {
awdl *awdl awdl *awdl
recv <-chan []byte // traffic received from the network recv <-chan []byte // traffic received from the network
send chan<- []byte // traffic to send to the network send chan<- []byte // traffic to send to the network
shutdown chan bool shutdown chan bool
peer *peer peer *peer
} }
func (l *awdl) init(c *Core) error { func (l *awdl) init(c *Core) error {
l.core = c l.core = c
l.mutex.Lock() l.mutex.Lock()
l.interfaces = make(map[string]*awdlInterface) l.interfaces = make(map[string]*awdlInterface)
l.mutex.Unlock() l.mutex.Unlock()
return nil return nil
} }
func (l *awdl) create(boxPubKey *crypto.BoxPubKey, sigPubKey *crypto.SigPubKey, name string) *awdlInterface { func (l *awdl) create(boxPubKey *crypto.BoxPubKey, sigPubKey *crypto.SigPubKey, name string) *awdlInterface {
shared := crypto.GetSharedKey(&l.core.boxPriv, boxPubKey) shared := crypto.GetSharedKey(&l.core.boxPriv, boxPubKey)
intf := awdlInterface{ intf := awdlInterface{
recv: make(<-chan []byte), recv: make(<-chan []byte),
send: make(chan<- []byte), send: make(chan<- []byte),
shutdown: make(chan bool), shutdown: make(chan bool),
peer: l.core.peers.newPeer(boxPubKey, sigPubKey, shared, name), peer: l.core.peers.newPeer(boxPubKey, sigPubKey, shared, name),
} }
if intf.peer != nil { if intf.peer != nil {
l.mutex.Lock() l.mutex.Lock()
l.interfaces[name] = &intf l.interfaces[name] = &intf
l.mutex.Unlock() l.mutex.Unlock()
intf.peer.linkOut = make(chan []byte, 1) // protocol traffic intf.peer.linkOut = make(chan []byte, 1)
intf.peer.out = func(msg []byte) { intf.peer.out = func(msg []byte) {
defer func() { recover() }() defer func() { recover() }()
intf.send <- msg intf.send <- msg
} }
go intf.handler() go intf.handler()
l.core.switchTable.idleIn <- intf.peer.port l.core.switchTable.idleIn <- intf.peer.port
return &intf return &intf
} }
return nil return nil
} }
func (l *awdl) getInterface(identity string) *awdlInterface { func (l *awdl) getInterface(identity string) *awdlInterface {
l.mutex.RLock() l.mutex.RLock()
defer l.mutex.RUnlock() defer l.mutex.RUnlock()
if intf, ok := l.interfaces[identity]; ok { if intf, ok := l.interfaces[identity]; ok {
return intf return intf
} }
return nil return nil
} }
func (l *awdl) shutdown(identity string) { func (l *awdl) shutdown(identity string) {
if intf, ok := l.interfaces[identity]; ok { if intf, ok := l.interfaces[identity]; ok {
intf.shutdown <- true intf.shutdown <- true
l.core.peers.removePeer(intf.peer.port) l.core.peers.removePeer(intf.peer.port)
l.mutex.Lock() l.mutex.Lock()
delete(l.interfaces, identity) delete(l.interfaces, identity)
l.mutex.Unlock() l.mutex.Unlock()
} }
} }
func (ai *awdlInterface) handler() { func (ai *awdlInterface) handler() {
for { for {
select { select {
case p := <-ai.peer.linkOut: case p := <-ai.peer.linkOut:
ai.send <- p ai.send <- p
case r := <-ai.recv: // traffic received from AWDL case r := <-ai.recv: // traffic received from AWDL
ai.peer.handlePacket(r) ai.peer.handlePacket(r)
case <-ai.shutdown: case <-ai.shutdown:
return return
default: default:
} }
} }
} }