mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2024-12-24 08:47:47 +00:00
Initialise awdl.go from link.go, remove deadlock between awdl.create and link.create, other bits and pieces
This commit is contained in:
parent
c51a3340b1
commit
41a410f2a1
@ -18,6 +18,7 @@ type awdlInterface struct {
|
|||||||
shutdown chan bool
|
shutdown chan bool
|
||||||
peer *peer
|
peer *peer
|
||||||
link *linkInterface
|
link *linkInterface
|
||||||
|
stream stream
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *awdl) init(c *Core) error {
|
func (l *awdl) init(c *Core) error {
|
||||||
@ -41,6 +42,8 @@ func (l *awdl) create(fromAWDL chan []byte, toAWDL chan []byte, name string) (*a
|
|||||||
toAWDL: toAWDL,
|
toAWDL: toAWDL,
|
||||||
shutdown: make(chan bool),
|
shutdown: make(chan bool),
|
||||||
}
|
}
|
||||||
|
intf.stream.init()
|
||||||
|
go intf.handler()
|
||||||
l.mutex.Lock()
|
l.mutex.Lock()
|
||||||
l.interfaces[name] = &intf
|
l.interfaces[name] = &intf
|
||||||
l.mutex.Unlock()
|
l.mutex.Unlock()
|
||||||
@ -71,6 +74,9 @@ func (l *awdl) shutdown(identity string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ai *awdlInterface) handler() {
|
func (ai *awdlInterface) handler() {
|
||||||
|
inPacket := func(packet []byte) {
|
||||||
|
ai.link.fromlink <- packet
|
||||||
|
}
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ai.shutdown:
|
case <-ai.shutdown:
|
||||||
@ -78,7 +84,7 @@ func (ai *awdlInterface) handler() {
|
|||||||
case <-ai.link.shutdown:
|
case <-ai.link.shutdown:
|
||||||
return
|
return
|
||||||
case in := <-ai.fromAWDL:
|
case in := <-ai.fromAWDL:
|
||||||
ai.link.fromlink <- in
|
ai.stream.write(in, inPacket)
|
||||||
case out := <-ai.link.tolink:
|
case out := <-ai.link.tolink:
|
||||||
ai.toAWDL <- out
|
ai.toAWDL <- out
|
||||||
}
|
}
|
||||||
|
@ -199,8 +199,8 @@ func (c *Core) Start(nc *config.NodeConfig, log *log.Logger) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := c.awdl.init(c); err != nil {
|
if err := c.link.init(c); err != nil {
|
||||||
c.log.Println("Failed to start AWDL interface")
|
c.log.Println("Failed to start link interfaces")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@ type link struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type linkInterface struct {
|
type linkInterface struct {
|
||||||
|
name string
|
||||||
link *link
|
link *link
|
||||||
fromlink chan []byte
|
fromlink chan []byte
|
||||||
tolink chan []byte
|
tolink chan []byte
|
||||||
@ -32,55 +33,75 @@ func (l *link) init(c *Core) error {
|
|||||||
l.interfaces = make(map[string]*linkInterface)
|
l.interfaces = make(map[string]*linkInterface)
|
||||||
l.mutex.Unlock()
|
l.mutex.Unlock()
|
||||||
|
|
||||||
|
if err := l.core.awdl.init(c); err != nil {
|
||||||
|
l.core.log.Println("Failed to start AWDL interface")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *link) create(fromlink chan []byte, tolink chan []byte, name string) (*linkInterface, error) {
|
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{
|
intf := linkInterface{
|
||||||
|
name: name,
|
||||||
link: l,
|
link: l,
|
||||||
fromlink: fromlink,
|
fromlink: fromlink,
|
||||||
tolink: tolink,
|
tolink: tolink,
|
||||||
shutdown: make(chan bool),
|
shutdown: make(chan bool),
|
||||||
}
|
}
|
||||||
l.mutex.Lock()
|
l.interfaces[intf.name] = &intf
|
||||||
l.interfaces[name] = &intf
|
go intf.start()
|
||||||
l.mutex.Unlock()
|
return &intf, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (intf *linkInterface) start() {
|
||||||
myLinkPub, myLinkPriv := crypto.NewBoxKeys()
|
myLinkPub, myLinkPriv := crypto.NewBoxKeys()
|
||||||
meta := version_getBaseMetadata()
|
meta := version_getBaseMetadata()
|
||||||
meta.box = l.core.boxPub
|
meta.box = intf.link.core.boxPub
|
||||||
meta.sig = l.core.sigPub
|
meta.sig = intf.link.core.sigPub
|
||||||
meta.link = *myLinkPub
|
meta.link = *myLinkPub
|
||||||
metaBytes := meta.encode()
|
metaBytes := meta.encode()
|
||||||
tolink <- metaBytes
|
//intf.link.core.log.Println("start: intf.tolink <- metaBytes")
|
||||||
metaBytes = <-fromlink
|
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")
|
||||||
meta = version_metadata{}
|
meta = version_metadata{}
|
||||||
if !meta.decode(metaBytes) || !meta.check() {
|
if !meta.decode(metaBytes) || !meta.check() {
|
||||||
return nil, errors.New("Metadata decode failure")
|
intf.link.core.log.Println("Metadata decode failure")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
base := version_getBaseMetadata()
|
base := version_getBaseMetadata()
|
||||||
if meta.ver > base.ver || meta.ver == base.ver && meta.minorVer > base.minorVer {
|
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))
|
intf.link.core.log.Println("Failed to connect to node: " + intf.name + " version: " + fmt.Sprintf("%d.%d", meta.ver, meta.minorVer))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
shared := crypto.GetSharedKey(myLinkPriv, &meta.link)
|
shared := crypto.GetSharedKey(myLinkPriv, &meta.link)
|
||||||
//shared := crypto.GetSharedKey(&l.core.boxPriv, boxPubKey)
|
intf.peer = intf.link.core.peers.newPeer(&meta.box, &meta.sig, shared, intf.name)
|
||||||
intf.peer = l.core.peers.newPeer(&meta.box, &meta.sig, shared, name)
|
if intf.peer == nil {
|
||||||
if intf.peer != nil {
|
intf.link.mutex.Lock()
|
||||||
intf.peer.linkOut = make(chan []byte, 1) // protocol traffic
|
delete(intf.link.interfaces, intf.name)
|
||||||
intf.peer.out = func(msg []byte) {
|
intf.link.mutex.Unlock()
|
||||||
defer func() { recover() }()
|
return
|
||||||
intf.tolink <- msg
|
|
||||||
} // called by peer.sendPacket()
|
|
||||||
l.core.switchTable.idleIn <- intf.peer.port // notify switch that we're idle
|
|
||||||
intf.peer.close = func() {
|
|
||||||
close(intf.fromlink)
|
|
||||||
close(intf.tolink)
|
|
||||||
}
|
|
||||||
go intf.handler()
|
|
||||||
go intf.peer.linkLoop()
|
|
||||||
return &intf, nil
|
|
||||||
}
|
}
|
||||||
delete(l.interfaces, name)
|
intf.peer.linkOut = make(chan []byte, 1) // protocol traffic
|
||||||
return nil, errors.New("l.core.peers.newPeer failed")
|
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)
|
||||||
|
}
|
||||||
|
go intf.handler()
|
||||||
|
go intf.peer.linkLoop()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *link) getInterface(identity string) *linkInterface {
|
func (l *link) getInterface(identity string) *linkInterface {
|
||||||
|
@ -9,7 +9,6 @@ import (
|
|||||||
|
|
||||||
type stream struct {
|
type stream struct {
|
||||||
buffer []byte
|
buffer []byte
|
||||||
cursor int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const streamMsgSize = 2048 + 65535
|
const streamMsgSize = 2048 + 65535
|
||||||
@ -18,7 +17,6 @@ var streamMsg = [...]byte{0xde, 0xad, 0xb1, 0x75} // "dead bits"
|
|||||||
|
|
||||||
func (s *stream) init() {
|
func (s *stream) init() {
|
||||||
s.buffer = make([]byte, 2*streamMsgSize)
|
s.buffer = make([]byte, 2*streamMsgSize)
|
||||||
s.cursor = 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This reads from the channel into a []byte buffer for incoming messages. It
|
// This reads from the channel into a []byte buffer for incoming messages. It
|
||||||
@ -76,36 +74,3 @@ func stream_chopMsg(bs *[]byte) ([]byte, bool, error) {
|
|||||||
(*bs) = (*bs)[msgEnd:]
|
(*bs) = (*bs)[msgEnd:]
|
||||||
return msg, true, nil
|
return msg, true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
func (s *stream) chopMsg() ([]byte, bool, error) {
|
|
||||||
// Returns msg, ok, err
|
|
||||||
if len(s.buffer) < len(streamMsg) {
|
|
||||||
fmt.Println("*** too short")
|
|
||||||
return nil, false, nil
|
|
||||||
}
|
|
||||||
for idx := range streamMsg {
|
|
||||||
if s.buffer[idx] != streamMsg[idx] {
|
|
||||||
fmt.Println("*** bad message")
|
|
||||||
return nil, false, errors.New("bad message")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
msgLen, msgLenLen := wire_decode_uint64((s.buffer)[len(streamMsg):])
|
|
||||||
if msgLen > streamMsgSize {
|
|
||||||
fmt.Println("*** oversized message")
|
|
||||||
return nil, false, errors.New("oversized message")
|
|
||||||
}
|
|
||||||
msgBegin := len(streamMsg) + msgLenLen
|
|
||||||
msgEnd := msgBegin + int(msgLen)
|
|
||||||
if msgLenLen == 0 || len(s.buffer) < msgEnd {
|
|
||||||
// We don't have the full message
|
|
||||||
// Need to buffer this and wait for the rest to come in
|
|
||||||
fmt.Println("*** still waiting")
|
|
||||||
return nil, false, nil
|
|
||||||
}
|
|
||||||
msg := s.buffer[msgBegin:msgEnd]
|
|
||||||
s.buffer = s.buffer[msgEnd:]
|
|
||||||
fmt.Println("*** done")
|
|
||||||
return msg, true, nil
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user