From f52955ee0fd17f5adb7960f9602e1e498c056c9c Mon Sep 17 00:00:00 2001 From: Arceliar Date: Sun, 4 Aug 2019 14:18:59 -0500 Subject: [PATCH] WARNING: CRYPTO DISABLED while speeding up stream writeMsg --- src/crypto/crypto.go | 2 ++ src/util/util.go | 9 +++++++++ src/yggdrasil/router.go | 1 - src/yggdrasil/stream.go | 27 ++++++++++++++++----------- src/yggdrasil/switch.go | 2 +- 5 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/crypto/crypto.go b/src/crypto/crypto.go index 75736ba7..14b12186 100644 --- a/src/crypto/crypto.go +++ b/src/crypto/crypto.go @@ -172,6 +172,7 @@ func BoxOpen(shared *BoxSharedKey, boxed []byte, nonce *BoxNonce) ([]byte, bool) { out := util.GetBytes() + return append(out, boxed...), true s := (*[BoxSharedKeyLen]byte)(shared) n := (*[BoxNonceLen]byte)(nonce) unboxed, success := box.OpenAfterPrecomputation(out, boxed, n, s) @@ -184,6 +185,7 @@ func BoxSeal(shared *BoxSharedKey, unboxed []byte, nonce *BoxNonce) ([]byte, *Bo } nonce.Increment() out := util.GetBytes() + return append(out, unboxed...), nonce s := (*[BoxSharedKeyLen]byte)(shared) n := (*[BoxNonceLen]byte)(nonce) boxed := box.SealAfterPrecomputation(out, unboxed, n, s) diff --git a/src/util/util.go b/src/util/util.go index 7f03ad05..b5e6ccc6 100644 --- a/src/util/util.go +++ b/src/util/util.go @@ -34,6 +34,15 @@ func PutBytes(bs []byte) { byteStore.Put(bs) } +// Gets a slice of the appropriate length, reusing existing slice capacity when possible +func ResizeBytes(bs []byte, length int) []byte { + if cap(bs) >= length { + return bs[:length] + } else { + return make([]byte, length) + } +} + // This is a workaround to go's broken timer implementation func TimerStop(t *time.Timer) bool { stopped := t.Stop() diff --git a/src/yggdrasil/router.go b/src/yggdrasil/router.go index 4eaa56d8..2df7684f 100644 --- a/src/yggdrasil/router.go +++ b/src/yggdrasil/router.go @@ -127,7 +127,6 @@ func (r *router) mainLoop() { r.core.switchTable.doMaintenance() r.core.dht.doMaintenance() r.core.sessions.cleanup() - util.GetBytes() // To slowly drain things } case f := <-r.admin: f() diff --git a/src/yggdrasil/stream.go b/src/yggdrasil/stream.go index 4d73844f..30dd9244 100644 --- a/src/yggdrasil/stream.go +++ b/src/yggdrasil/stream.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "io" + "net" "github.com/yggdrasil-network/yggdrasil-go/src/util" ) @@ -12,10 +13,11 @@ import ( var _ = linkInterfaceMsgIO(&stream{}) type stream struct { - rwc io.ReadWriteCloser - inputBuffer []byte // Incoming packet stream - frag [2 * streamMsgSize]byte // Temporary data read off the underlying rwc, on its way to the inputBuffer - outputBuffer [2 * streamMsgSize]byte // Temporary data about to be written to the rwc + rwc io.ReadWriteCloser + inputBuffer []byte // Incoming packet stream + frag [2 * streamMsgSize]byte // Temporary data read off the underlying rwc, on its way to the inputBuffer + //outputBuffer [2 * streamMsgSize]byte // Temporary data about to be written to the rwc + outputBuffer net.Buffers } func (s *stream) close() error { @@ -35,14 +37,17 @@ func (s *stream) init(rwc io.ReadWriteCloser) { // writeMsg writes a message with stream padding, and is *not* thread safe. func (s *stream) writeMsg(bs []byte) (int, error) { buf := s.outputBuffer[:0] - buf = append(buf, streamMsg[:]...) - buf = wire_put_uint64(uint64(len(bs)), buf) - padLen := len(buf) - buf = append(buf, bs...) + buf = append(buf, streamMsg[:]) + l := wire_put_uint64(uint64(len(bs)), util.GetBytes()) + defer util.PutBytes(l) + buf = append(buf, l) + padLen := len(buf[0]) + len(buf[1]) + buf = append(buf, bs) + totalLen := padLen + len(bs) var bn int - for bn < len(buf) { - n, err := s.rwc.Write(buf[bn:]) - bn += n + for bn < totalLen { + n, err := buf.WriteTo(s.rwc) + bn += int(n) if err != nil { l := bn - padLen if l < 0 { diff --git a/src/yggdrasil/switch.go b/src/yggdrasil/switch.go index 1bc40501..0e11593f 100644 --- a/src/yggdrasil/switch.go +++ b/src/yggdrasil/switch.go @@ -820,7 +820,7 @@ func (t *switchTable) doWorker() { select { case bs := <-t.toRouter: buf = append(buf, bs) - for len(buf) > 32 { + for len(buf) > 32768 { // FIXME realistically don't drop anything, just for testing util.PutBytes(buf[0]) buf = buf[1:] }