5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-23 00:51:35 +00:00

Adapt AWDL to link

This commit is contained in:
Neil Alexander 2019-01-23 15:08:19 +00:00
parent 7b2460662d
commit 9c6cf50684
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944
3 changed files with 51 additions and 54 deletions

View File

@ -1,7 +1,7 @@
package yggdrasil package yggdrasil
import ( import (
//"fmt" "errors"
"sync" "sync"
) )
@ -12,13 +12,31 @@ type awdl struct {
} }
type awdlInterface struct { type awdlInterface struct {
awdl *awdl link *linkInterface
rwc awdlReadWriteCloser
peer *peer
stream stream
}
type awdlReadWriteCloser struct {
fromAWDL chan []byte fromAWDL chan []byte
toAWDL chan []byte toAWDL chan []byte
shutdown chan bool }
peer *peer
link *linkInterface func (c awdlReadWriteCloser) Read(p []byte) (n int, err error) {
stream stream p = <-c.fromAWDL
return len(p), nil
}
func (c awdlReadWriteCloser) Write(p []byte) (n int, err error) {
c.toAWDL <- p
return len(p), nil
}
func (c awdlReadWriteCloser) Close() error {
close(c.fromAWDL)
close(c.toAWDL)
return nil
} }
func (l *awdl) init(c *Core) error { func (l *awdl) init(c *Core) error {
@ -30,27 +48,26 @@ func (l *awdl) init(c *Core) error {
return nil return nil
} }
/* temporarily disabled while getting the TCP side to work func (l *awdl) create(fromAWDL chan []byte, toAWDL chan []byte, name, local, remote string) (*awdlInterface, error) {
func (l *awdl) create(fromAWDL chan []byte, toAWDL chan []byte, name string) (*awdlInterface, error) { rwc := awdlReadWriteCloser{
link, err := l.core.link.create(fromAWDL, toAWDL, name) fromAWDL: fromAWDL,
toAWDL: toAWDL,
}
s := stream{}
s.init(rwc, nil)
link, err := l.core.link.create(&s, name, "awdl", local, remote)
if err != nil { if err != nil {
return nil, err return nil, err
} }
intf := awdlInterface{ intf := awdlInterface{
awdl: l, link: link,
link: link, rwc: rwc,
fromAWDL: fromAWDL,
toAWDL: toAWDL,
shutdown: make(chan bool),
} }
inPacket := func(packet []byte) { intf.stream.init(intf.rwc, nil)
intf.link.fromlink <- packet
}
intf.stream.init(nil, inPacket) // FIXME nil = ReadWriteCloser
go intf.handler()
l.mutex.Lock() l.mutex.Lock()
l.interfaces[name] = &intf l.interfaces[name] = &intf
l.mutex.Unlock() l.mutex.Unlock()
go link.handler()
return &intf, nil return &intf, nil
} }
@ -64,31 +81,13 @@ func (l *awdl) getInterface(identity string) *awdlInterface {
} }
func (l *awdl) shutdown(identity string) error { func (l *awdl) shutdown(identity string) error {
if err := l.core.link.shutdown(identity); err != nil {
return err
}
if intf, ok := l.interfaces[identity]; ok { if intf, ok := l.interfaces[identity]; ok {
intf.shutdown <- true close(intf.link.closed)
intf.rwc.Close()
l.mutex.Lock() l.mutex.Lock()
delete(l.interfaces, identity) delete(l.interfaces, identity)
l.mutex.Unlock() l.mutex.Unlock()
return nil return nil
} }
return fmt.Errorf("interface '%s' doesn't exist or already shutdown", identity) return errors.New("Interface not found or already closed")
} }
func (ai *awdlInterface) handler() {
for {
select {
case <-ai.shutdown:
return
case <-ai.link.shutdown:
return
case in := <-ai.fromAWDL:
ai.stream.handleInput(in)
case out := <-ai.link.tolink:
ai.toAWDL <- out
}
}
}
*/

View File

@ -87,10 +87,12 @@ func (intf *linkInterface) handler() error {
if err != nil { if err != nil {
return err return err
} }
intf.link.core.log.Println("Sent my metadata")
metaBytes, err = intf.msgIO._recvMetaBytes() metaBytes, err = intf.msgIO._recvMetaBytes()
if err != nil { if err != nil {
return err return err
} }
intf.link.core.log.Println("Received their metadata")
meta = version_metadata{} meta = version_metadata{}
if !meta.decode(metaBytes) || !meta.check() { if !meta.decode(metaBytes) || !meta.check() {
return errors.New("failed to decode metadata") return errors.New("failed to decode metadata")
@ -100,6 +102,7 @@ func (intf *linkInterface) handler() error {
intf.link.core.log.Println("Failed to connect to node: " + intf.name + " version: " + fmt.Sprintf("%d.%d", meta.ver, meta.minorVer)) intf.link.core.log.Println("Failed to connect to node: " + intf.name + " version: " + fmt.Sprintf("%d.%d", meta.ver, meta.minorVer))
return errors.New("failed to connect: wrong version") return errors.New("failed to connect: wrong version")
} }
intf.link.core.log.Println("Do we have a link already?")
// Check if we already have a link to this node // Check if we already have a link to this node
intf.info.box = meta.box intf.info.box = meta.box
intf.info.sig = meta.sig intf.info.sig = meta.sig
@ -124,6 +127,7 @@ func (intf *linkInterface) handler() error {
intf.link.core.log.Println("DEBUG: registered interface for", intf.name) intf.link.core.log.Println("DEBUG: registered interface for", intf.name)
} }
intf.link.mutex.Unlock() intf.link.mutex.Unlock()
intf.link.core.log.Println("Create peer")
// Create peer // Create peer
shared := crypto.GetSharedKey(myLinkPriv, &meta.link) shared := crypto.GetSharedKey(myLinkPriv, &meta.link)
intf.peer = intf.link.core.peers.newPeer(&meta.box, &meta.sig, shared, intf.name) intf.peer = intf.link.core.peers.newPeer(&meta.box, &meta.sig, shared, intf.name)
@ -145,6 +149,7 @@ func (intf *linkInterface) handler() error {
intf.peer.close = func() { intf.msgIO.close() } intf.peer.close = func() { intf.msgIO.close() }
go intf.peer.linkLoop() go intf.peer.linkLoop()
// Start the writer // Start the writer
intf.link.core.log.Println("Start writer")
go func() { go func() {
interval := 4 * time.Second interval := 4 * time.Second
timer := time.NewTimer(interval) timer := time.NewTimer(interval)
@ -190,6 +195,7 @@ func (intf *linkInterface) handler() error {
}() }()
intf.link.core.switchTable.idleIn <- intf.peer.port // notify switch that we're idle intf.link.core.switchTable.idleIn <- intf.peer.port // notify switch that we're idle
// Run reader loop // Run reader loop
intf.link.core.log.Println("Start reader")
for { for {
msg, err := intf.msgIO.readMsg() msg, err := intf.msgIO.readMsg()
if len(msg) > 0 { if len(msg) > 0 {

View File

@ -29,22 +29,14 @@ func (nsl MobileLogger) Write(p []byte) (n int, err error) {
return len(p), nil return len(p), nil
} }
func (c *Core) AWDLCreateInterface(name string) error { func (c *Core) AWDLCreateInterface(name, local, remote string) error {
fromAWDL := make(chan []byte, 32) fromAWDL := make(chan []byte, 32)
toAWDL := make(chan []byte, 32) toAWDL := make(chan []byte, 32)
if intf, err := c.awdl.create(fromAWDL, toAWDL, name, local, remote); err != nil || intf == nil {
if intf, err := c.awdl.create(fromAWDL, toAWDL, name); err == nil { c.log.Println("c.awdl.create:", err)
if intf != nil {
c.log.Println(err)
return err
} else {
c.log.Println("c.awdl.create didn't return an interface")
return errors.New("c.awdl.create didn't return an interface")
}
} else {
c.log.Println(err)
return err return err
} }
return nil
} }
func (c *Core) AWDLShutdownInterface(name string) error { func (c *Core) AWDLShutdownInterface(name string) error {
@ -53,7 +45,7 @@ func (c *Core) AWDLShutdownInterface(name string) error {
func (c *Core) AWDLRecvPacket(identity string) ([]byte, error) { func (c *Core) AWDLRecvPacket(identity string) ([]byte, error) {
if intf := c.awdl.getInterface(identity); intf != nil { if intf := c.awdl.getInterface(identity); intf != nil {
return <-intf.toAWDL, nil return <-intf.rwc.toAWDL, nil
} }
return nil, errors.New("AWDLRecvPacket identity not known: " + identity) return nil, errors.New("AWDLRecvPacket identity not known: " + identity)
} }
@ -61,7 +53,7 @@ func (c *Core) AWDLRecvPacket(identity string) ([]byte, error) {
func (c *Core) AWDLSendPacket(identity string, buf []byte) error { func (c *Core) AWDLSendPacket(identity string, buf []byte) error {
packet := append(util.GetBytes(), buf[:]...) packet := append(util.GetBytes(), buf[:]...)
if intf := c.awdl.getInterface(identity); intf != nil { if intf := c.awdl.getInterface(identity); intf != nil {
intf.fromAWDL <- packet intf.rwc.fromAWDL <- packet
return nil return nil
} }
return errors.New("AWDLSendPacket identity not known: " + identity) return errors.New("AWDLSendPacket identity not known: " + identity)