Skip to content

Commit

Permalink
Use copy instead of a new buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
lixin9311 committed Jun 28, 2018
1 parent c5d1416 commit cc5b55c
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions shadowstream/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,19 @@ func Unpack(dst, pkt []byte, s Cipher) ([]byte, error) {
type packetConn struct {
net.PacketConn
Cipher
wbuf []byte
rbuf []byte
buf []byte
sync.Mutex // write lock
}

// NewPacketConn wraps a net.PacketConn with stream cipher encryption/decryption.
func NewPacketConn(c net.PacketConn, ciph Cipher) net.PacketConn {
return &packetConn{PacketConn: c, Cipher: ciph, wbuf: make([]byte, 64*1024), rbuf: make([]byte, 64*1024)}
return &packetConn{PacketConn: c, Cipher: ciph, buf: make([]byte, 64*1024)}
}

func (c *packetConn) WriteTo(b []byte, addr net.Addr) (int, error) {
c.Lock()
defer c.Unlock()
buf, err := Pack(c.wbuf, b, c.Cipher)
buf, err := Pack(c.buf, b, c.Cipher)
if err != nil {
return 0, err
}
Expand All @@ -68,10 +67,14 @@ func (c *packetConn) WriteTo(b []byte, addr net.Addr) (int, error) {
}

func (c *packetConn) ReadFrom(b []byte) (int, net.Addr, error) {
n, addr, err := c.PacketConn.ReadFrom(c.rbuf)
n, addr, err := c.PacketConn.ReadFrom(b)
if err != nil {
return n, addr, err
}
b, err = Unpack(b, c.rbuf[:n], c.Cipher)
bb, err := Unpack(b[c.IVSize():], b[:n], c.Cipher)
if err != nil {
return n, addr, err
}
copy(b, bb)
return len(b), addr, err
}

0 comments on commit cc5b55c

Please sign in to comment.