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

link/stream refactoring bugfixes and gofmt

This commit is contained in:
Arceliar 2019-01-19 16:37:45 -06:00
parent 41a410f2a1
commit c8e1be0f73
4 changed files with 28 additions and 30 deletions

View File

@ -35,22 +35,22 @@ func main() {
} }
var encryptionKeys []keySet var encryptionKeys []keySet
for i := 0; i < *numHosts + 1; i++ { for i := 0; i < *numHosts+1; i++ {
encryptionKeys = append(encryptionKeys, newBoxKey()) encryptionKeys = append(encryptionKeys, newBoxKey())
} }
encryptionKeys = sortKeySetArray(encryptionKeys) encryptionKeys = sortKeySetArray(encryptionKeys)
for i := 0; i < *keyTries - *numHosts - 1; i++ { for i := 0; i < *keyTries-*numHosts-1; i++ {
encryptionKeys[0] = newBoxKey(); encryptionKeys[0] = newBoxKey()
encryptionKeys = bubbleUpTo(encryptionKeys, 0) encryptionKeys = bubbleUpTo(encryptionKeys, 0)
} }
var signatureKeys []keySet var signatureKeys []keySet
for i := 0; i < *numHosts + 1; i++ { for i := 0; i < *numHosts+1; i++ {
signatureKeys = append(signatureKeys, newSigKey()) signatureKeys = append(signatureKeys, newSigKey())
} }
signatureKeys = sortKeySetArray(signatureKeys) signatureKeys = sortKeySetArray(signatureKeys)
for i := 0; i < *keyTries - *numHosts - 1; i++ { for i := 0; i < *keyTries-*numHosts-1; i++ {
signatureKeys[0] = newSigKey(); signatureKeys[0] = newSigKey()
signatureKeys = bubbleUpTo(signatureKeys, 0) signatureKeys = bubbleUpTo(signatureKeys, 0)
} }
@ -112,11 +112,11 @@ func sortKeySetArray(sets []keySet) []keySet {
} }
func bubbleUpTo(sets []keySet, num int) []keySet { func bubbleUpTo(sets []keySet, num int) []keySet {
for i := 0; i < len(sets) - num - 1; i++ { for i := 0; i < len(sets)-num-1; i++ {
if isBetter(sets[i + 1].id, sets[i].id) { if isBetter(sets[i+1].id, sets[i].id) {
var tmp = sets[i] var tmp = sets[i]
sets[i] = sets[i + 1] sets[i] = sets[i+1]
sets[i + 1] = tmp sets[i+1] = tmp
} }
} }
return sets return sets

View File

@ -42,7 +42,10 @@ 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() inPacket := func(packet []byte) {
intf.link.fromlink <- packet
}
intf.stream.init(inPacket)
go intf.handler() go intf.handler()
l.mutex.Lock() l.mutex.Lock()
l.interfaces[name] = &intf l.interfaces[name] = &intf
@ -74,9 +77,6 @@ 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:
@ -84,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.stream.write(in, inPacket) ai.stream.handleInput(in)
case out := <-ai.link.tolink: case out := <-ai.link.tolink:
ai.toAWDL <- out ai.toAWDL <- out
} }

View File

@ -8,15 +8,16 @@ import (
) )
type stream struct { type stream struct {
buffer []byte inputBuffer []byte
handlePacket func([]byte)
} }
const streamMsgSize = 2048 + 65535 const streamMsgSize = 2048 + 65535
var streamMsg = [...]byte{0xde, 0xad, 0xb1, 0x75} // "dead bits" var streamMsg = [...]byte{0xde, 0xad, 0xb1, 0x75} // "dead bits"
func (s *stream) init() { func (s *stream) init(in func([]byte)) {
s.buffer = make([]byte, 2*streamMsgSize) s.handlePacket = in
} }
// 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
@ -24,11 +25,10 @@ func (s *stream) init() {
// to the peer struct via the provided `in func([]byte)` argument. Then it // to the peer struct via the provided `in func([]byte)` argument. Then it
// shifts the incomplete fragments of data forward so future reads won't // shifts the incomplete fragments of data forward so future reads won't
// overwrite it. // overwrite it.
func (s *stream) write(bs []byte, in func([]byte)) error { func (s *stream) handleInput(bs []byte) error {
frag := s.buffer[:0] if len(bs) > 0 {
if n := len(bs); n > 0 { s.inputBuffer = append(s.inputBuffer, bs...)
frag = append(frag, bs[:n]...) msg, ok, err2 := stream_chopMsg(&s.inputBuffer)
msg, ok, err2 := stream_chopMsg(&frag)
if err2 != nil { if err2 != nil {
return fmt.Errorf("message error: %v", err2) return fmt.Errorf("message error: %v", err2)
} }
@ -37,8 +37,9 @@ func (s *stream) write(bs []byte, in func([]byte)) error {
return nil return nil
} }
newMsg := append(util.GetBytes(), msg...) newMsg := append(util.GetBytes(), msg...)
in(newMsg) s.inputBuffer = append(s.inputBuffer[:0], s.inputBuffer...)
util.Yield() s.handlePacket(newMsg)
util.Yield() // Make sure we give up control to the scheduler
} }
return nil return nil
} }

View File

@ -378,9 +378,6 @@ func (iface *tcpInterface) handler(sock net.Conn, incoming bool) {
// E.g. over different interfaces // E.g. over different interfaces
p := iface.core.peers.newPeer(&meta.box, &meta.sig, crypto.GetSharedKey(myLinkPriv, &meta.link), sock.RemoteAddr().String()) p := iface.core.peers.newPeer(&meta.box, &meta.sig, crypto.GetSharedKey(myLinkPriv, &meta.link), sock.RemoteAddr().String())
p.linkOut = make(chan []byte, 1) p.linkOut = make(chan []byte, 1)
in := func(bs []byte) {
p.handlePacket(bs)
}
out := make(chan []byte, 1) out := make(chan []byte, 1)
defer close(out) defer close(out)
go func() { go func() {
@ -443,7 +440,7 @@ func (iface *tcpInterface) handler(sock net.Conn, incoming bool) {
themAddrString := net.IP(themAddr[:]).String() themAddrString := net.IP(themAddr[:]).String()
themString := fmt.Sprintf("%s@%s", themAddrString, them) themString := fmt.Sprintf("%s@%s", themAddrString, them)
iface.core.log.Printf("Connected: %s, source: %s", themString, us) iface.core.log.Printf("Connected: %s, source: %s", themString, us)
iface.stream.init() iface.stream.init(p.handlePacket)
bs := make([]byte, 2*streamMsgSize) bs := make([]byte, 2*streamMsgSize)
var n int var n int
for { for {
@ -455,7 +452,7 @@ func (iface *tcpInterface) handler(sock net.Conn, incoming bool) {
break break
} }
if n > 0 { if n > 0 {
iface.stream.write(bs[:n], in) iface.stream.handleInput(bs[:n])
} }
} }
if err == nil { if err == nil {