From 705b914d008f4917a4fee8ea5bdd0d875d930367 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Wed, 23 Jan 2019 19:42:33 +0000 Subject: [PATCH] Move awdl into link --- src/yggdrasil/awdl.go | 50 ++++++++++++++++++------------------- src/yggdrasil/core.go | 3 +-- src/yggdrasil/link.go | 3 ++- src/yggdrasil/mobile_ios.go | 10 ++++---- 4 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/yggdrasil/awdl.go b/src/yggdrasil/awdl.go index 56104ee..e9e57a6 100644 --- a/src/yggdrasil/awdl.go +++ b/src/yggdrasil/awdl.go @@ -7,13 +7,13 @@ import ( ) type awdl struct { - core *Core + link *link mutex sync.RWMutex // protects interfaces below interfaces map[string]*awdlInterface } type awdlInterface struct { - link *linkInterface + linkif *linkInterface rwc awdlReadWriteCloser peer *peer stream stream @@ -45,53 +45,53 @@ func (c awdlReadWriteCloser) Close() error { return nil } -func (l *awdl) init(c *Core) error { - l.core = c - l.mutex.Lock() - l.interfaces = make(map[string]*awdlInterface) - l.mutex.Unlock() +func (a *awdl) init(l *link) error { + a.link = l + a.mutex.Lock() + a.interfaces = make(map[string]*awdlInterface) + a.mutex.Unlock() return nil } -func (l *awdl) create(name, local, remote string) (*awdlInterface, error) { +func (a *awdl) create(name, local, remote string) (*awdlInterface, error) { rwc := awdlReadWriteCloser{ fromAWDL: make(chan []byte, 1), toAWDL: make(chan []byte, 1), } s := stream{} s.init(rwc) - link, err := l.core.link.create(&s, name, "awdl", local, remote) + linkif, err := a.link.create(&s, name, "awdl", local, remote) if err != nil { return nil, err } intf := awdlInterface{ - link: link, - rwc: rwc, + linkif: linkif, + rwc: rwc, } - l.mutex.Lock() - l.interfaces[name] = &intf - l.mutex.Unlock() - go intf.link.handler() + a.mutex.Lock() + a.interfaces[name] = &intf + a.mutex.Unlock() + go intf.linkif.handler() return &intf, nil } -func (l *awdl) getInterface(identity string) *awdlInterface { - l.mutex.RLock() - defer l.mutex.RUnlock() - if intf, ok := l.interfaces[identity]; ok { +func (a *awdl) getInterface(identity string) *awdlInterface { + a.mutex.RLock() + defer a.mutex.RUnlock() + if intf, ok := a.interfaces[identity]; ok { return intf } return nil } -func (l *awdl) shutdown(identity string) error { - if intf, ok := l.interfaces[identity]; ok { - close(intf.link.closed) +func (a *awdl) shutdown(identity string) error { + if intf, ok := a.interfaces[identity]; ok { + close(intf.linkif.closed) intf.rwc.Close() - l.mutex.Lock() - delete(l.interfaces, identity) - l.mutex.Unlock() + a.mutex.Lock() + delete(a.interfaces, identity) + a.mutex.Unlock() return nil } return errors.New("Interface not found or already closed") diff --git a/src/yggdrasil/core.go b/src/yggdrasil/core.go index 7090561..4a1aba3 100644 --- a/src/yggdrasil/core.go +++ b/src/yggdrasil/core.go @@ -44,8 +44,7 @@ type Core struct { searches searches multicast multicast tcp tcpInterface - link link // TODO: not sure if this wants to be here? - awdl awdl + link link log *log.Logger } diff --git a/src/yggdrasil/link.go b/src/yggdrasil/link.go index 8b833f5..99044ce 100644 --- a/src/yggdrasil/link.go +++ b/src/yggdrasil/link.go @@ -15,6 +15,7 @@ type link struct { core *Core mutex sync.RWMutex // protects interfaces below interfaces map[linkInfo]*linkInterface + awdl awdl // AWDL interface support } type linkInfo struct { @@ -49,7 +50,7 @@ func (l *link) init(c *Core) error { l.interfaces = make(map[linkInfo]*linkInterface) l.mutex.Unlock() - if err := l.core.awdl.init(c); err != nil { + if err := l.awdl.init(l); err != nil { l.core.log.Println("Failed to start AWDL interface") return err } diff --git a/src/yggdrasil/mobile_ios.go b/src/yggdrasil/mobile_ios.go index 5e0d4aa..c2ec63b 100644 --- a/src/yggdrasil/mobile_ios.go +++ b/src/yggdrasil/mobile_ios.go @@ -30,19 +30,19 @@ func (nsl MobileLogger) Write(p []byte) (n int, err error) { } func (c *Core) AWDLCreateInterface(name, local, remote string) error { - if intf, err := c.awdl.create(name, local, remote); err != nil || intf == nil { - c.log.Println("c.awdl.create:", err) + if intf, err := c.link.awdl.create(name, local, remote); err != nil || intf == nil { + c.log.Println("c.link.awdl.create:", err) return err } return nil } func (c *Core) AWDLShutdownInterface(name string) error { - return c.awdl.shutdown(name) + return c.link.awdl.shutdown(name) } func (c *Core) AWDLRecvPacket(identity string) ([]byte, error) { - if intf := c.awdl.getInterface(identity); intf != nil { + if intf := c.link.awdl.getInterface(identity); intf != nil { read, ok := <-intf.rwc.toAWDL if !ok { return nil, errors.New("AWDLRecvPacket: channel closed") @@ -54,7 +54,7 @@ func (c *Core) AWDLRecvPacket(identity string) ([]byte, error) { func (c *Core) AWDLSendPacket(identity string, buf []byte) error { packet := append(util.GetBytes(), buf[:]...) - if intf := c.awdl.getInterface(identity); intf != nil { + if intf := c.link.awdl.getInterface(identity); intf != nil { intf.rwc.fromAWDL <- packet return nil }