From 24281d4049acc6cfa1c4a88245160191fd209230 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Fri, 19 Apr 2019 23:47:11 +0100 Subject: [PATCH] Fix Read, update sample --- cmd/yggdrasil/main.go | 4 ++-- src/yggdrasil/conn.go | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/cmd/yggdrasil/main.go b/cmd/yggdrasil/main.go index 2267d62..0e3aa35 100644 --- a/cmd/yggdrasil/main.go +++ b/cmd/yggdrasil/main.go @@ -283,7 +283,7 @@ func main() { } logger.Println("Accepted") for { - b := []byte{} + b := make([]byte, 100) if n, err := conn.Read(b); err != nil { logger.Errorln("Read failed:", err) time.Sleep(time.Second * 2) @@ -319,7 +319,7 @@ func main() { logger.Errorln("Write failed:", err) } else { logger.Println("Wrote", n, "bytes:", b) - b = b[:0] + b = make([]byte, 100) if n, err := conn.Read(b); err != nil { logger.Errorln("Read failed:", err) } else { diff --git a/src/yggdrasil/conn.go b/src/yggdrasil/conn.go index df45d12..b936d4f 100644 --- a/src/yggdrasil/conn.go +++ b/src/yggdrasil/conn.go @@ -88,8 +88,10 @@ func (c *Conn) Read(b []byte) (int, error) { util.PutBytes(bs) return errors.New("packet dropped due to decryption failure") } - b = b[:0] - b = append(b, bs...) + copy(b, bs) + if len(bs) < len(b) { + b = b[:len(bs)] + } c.session.updateNonce(&p.Nonce) c.session.time = time.Now() return nil