5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-12-26 12:25:41 +00:00

Add contexts

This commit is contained in:
Neil Alexander 2019-01-06 14:12:10 +00:00
parent 2034c9eab9
commit 6efac9a377
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944
2 changed files with 56 additions and 8 deletions

View File

@ -34,14 +34,37 @@ func (l *awdl) init(c *Core) error {
return nil
}
func (l *awdl) create(boxPubKey *crypto.BoxPubKey, sigPubKey *crypto.SigPubKey, name string) (*awdlInterface, error) {
func (l *awdl) create(fromAWDL chan []byte, toAWDL chan []byte, boxPubKey *crypto.BoxPubKey, sigPubKey *crypto.SigPubKey, name string) (*awdlInterface, error) {
/*
myLinkPub, myLinkPriv := crypto.NewBoxKeys()
meta := version_getBaseMetadata()
meta.box = l.core.boxPub
meta.sig = l.core.sigPub
meta.link = *myLinkPub
metaBytes := meta.encode()
l.core.log.Println("toAWDL <- metaBytes")
toAWDL <- metaBytes
l.core.log.Println("metaBytes = <-fromAWDL")
metaBytes = <-fromAWDL
l.core.log.Println("version_metadata{}")
meta = version_metadata{}
if !meta.decode(metaBytes) || !meta.check() {
return nil, errors.New("Metadata decode failure")
}
base := version_getBaseMetadata()
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))
}
shared := crypto.GetSharedKey(myLinkPriv, &meta.link)
*/
shared := crypto.GetSharedKey(&l.core.boxPriv, boxPubKey)
intf := awdlInterface{
awdl: l,
fromAWDL: make(chan []byte, 32),
toAWDL: make(chan []byte, 32),
fromAWDL: fromAWDL,
toAWDL: toAWDL,
shutdown: make(chan bool),
peer: l.core.peers.newPeer(boxPubKey, sigPubKey, shared, name),
//peer: l.core.peers.newPeer(&meta.box, &meta.sig, shared, name),
}
if intf.peer != nil {
l.mutex.Lock()
@ -57,8 +80,8 @@ func (l *awdl) create(boxPubKey *crypto.BoxPubKey, sigPubKey *crypto.SigPubKey,
close(intf.fromAWDL)
close(intf.toAWDL)
}
go intf.handler() // start listening for packets from switch
go intf.peer.linkLoop() // start link loop
go intf.handler()
go intf.peer.linkLoop()
return &intf, nil
}
return nil, errors.New("l.core.peers.newPeer failed")

View File

@ -98,29 +98,47 @@ func (c *Core) RouterSendPacket(buf []byte) error {
}
func (c *Core) AWDLCreateInterface(boxPubKey string, sigPubKey string, name string) error {
fromAWDL := make(chan []byte, 32)
toAWDL := make(chan []byte, 32)
var boxPub crypto.BoxPubKey
var sigPub crypto.SigPubKey
boxPubHex, err := hex.DecodeString(boxPubKey)
if err != nil {
c.log.Println(err)
return err
}
sigPubHex, err := hex.DecodeString(sigPubKey)
if err != nil {
c.log.Println(err)
return err
}
copy(boxPub[:], boxPubHex)
copy(sigPub[:], sigPubHex)
if intf, err := c.awdl.create(&boxPub, &sigPub, name); err == nil {
if intf, err := c.awdl.create(fromAWDL, toAWDL, &boxPub, &sigPub, name); err == nil {
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
}
}
func (c *Core) AWDLCreateInterfaceFromContext(context []byte, name string) error {
if len(context) < crypto.BoxPubKeyLen+crypto.SigPubKeyLen {
return errors.New("Not enough bytes in context")
}
boxPubKey := hex.EncodeToString(context[:crypto.BoxPubKeyLen])
sigPubKey := hex.EncodeToString(context[crypto.BoxPubKeyLen:])
return c.AWDLCreateInterface(boxPubKey, sigPubKey, name)
}
func (c *Core) AWDLShutdownInterface(name string) error {
return c.awdl.shutdown(name)
}
@ -129,7 +147,7 @@ func (c *Core) AWDLRecvPacket(identity string) ([]byte, error) {
if intf := c.awdl.getInterface(identity); intf != nil {
return <-intf.toAWDL, nil
}
return nil, errors.New("identity not known: " + identity)
return nil, errors.New("AWDLRecvPacket identity not known: " + identity)
}
func (c *Core) AWDLSendPacket(identity string, buf []byte) error {
@ -138,5 +156,12 @@ func (c *Core) AWDLSendPacket(identity string, buf []byte) error {
intf.fromAWDL <- packet
return nil
}
return errors.New("identity not known: " + identity)
return errors.New("AWDLSendPacket identity not known: " + identity)
}
func (c *Core) AWDLConnectionContext() []byte {
var context []byte
context = append(context, c.boxPub[:]...)
context = append(context, c.sigPub[:]...)
return context
}