From 64e511c3c565410c153b9b5bcab793c80339194e Mon Sep 17 00:00:00 2001 From: Russ Magee Date: Fri, 4 May 2018 23:39:19 -0700 Subject: [PATCH] Moved mutex to front of Conn struct --- hkexnet.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/hkexnet.go b/hkexnet.go index cb3f4c2..e22aec1 100644 --- a/hkexnet.go +++ b/hkexnet.go @@ -46,6 +46,7 @@ type WinSize struct { // Conn is a HKex connection - a drop-in replacement for net.Conn type Conn struct { + m *sync.Mutex c net.Conn // which also implements io.Reader, io.Writer, ... h *HerraduraKEx cipheropts uint32 // post-KEx cipher/hmac options @@ -54,7 +55,6 @@ type Conn struct { Rows uint16 Cols uint16 - Rwmut *sync.Mutex chaff bool chaffMsecsMin int //msecs min interval chaffMsecsMax int //msecs max interval @@ -147,7 +147,7 @@ func Dial(protocol string, ipport string, extensions ...string) (hc *Conn, err e return nil, err } // Init hkexnet.Conn hc over net.Conn c - hc = &Conn{c: c, h: New(0, 0), Rwmut: &sync.Mutex{}, dBuf: new(bytes.Buffer)} + hc = &Conn{m: &sync.Mutex{}, c: c, h: New(0, 0), dBuf: new(bytes.Buffer)} hc.applyConnExtensions(extensions...) // Send hkexnet.Conn parameters to remote side @@ -274,15 +274,14 @@ func (hl HKExListener) Accept() (hc Conn, err error) { // Open raw Conn c c, err := hl.l.Accept() if err != nil { - hc := Conn{c: nil, h: nil, cipheropts: 0, opts: 0, Rwmut: &sync.Mutex{}, + hc := Conn{m: &sync.Mutex{}, c: nil, h: nil, cipheropts: 0, opts: 0, r: nil, w: nil} return hc, err } log.Println("[Accepted]") - hc = Conn{c: c, h: New(0, 0), WinCh: make(chan WinSize, 1), - Rwmut: &sync.Mutex{}, - dBuf: new(bytes.Buffer)} + hc = Conn{m: &sync.Mutex{}, c: c, h: New(0, 0), WinCh: make(chan WinSize, 1), + dBuf: new(bytes.Buffer)} // Read in hkexnet.Conn parameters over raw Conn c // d is value for Herradura key exchange @@ -454,7 +453,7 @@ func (c Conn) WritePacket(b []byte, op byte) (n int, err error) { // // Would be nice to determine if the mutex scope // could be tightened. - c.Rwmut.Lock() + c.m.Lock() { log.Printf(" :>ptext:\r\n%s\r\n", hex.Dump(b)) @@ -490,7 +489,7 @@ func (c Conn) WritePacket(b []byte, op byte) (n int, err error) { } } } - c.Rwmut.Unlock() + c.m.Unlock() if err != nil { //panic(err)