Moved mutex to front of Conn struct

This commit is contained in:
Russ Magee 2018-05-04 23:39:19 -07:00
parent 70448dda08
commit 64e511c3c5

View file

@ -46,6 +46,7 @@ type WinSize struct {
// Conn is a HKex connection - a drop-in replacement for net.Conn // Conn is a HKex connection - a drop-in replacement for net.Conn
type Conn struct { type Conn struct {
m *sync.Mutex
c net.Conn // which also implements io.Reader, io.Writer, ... c net.Conn // which also implements io.Reader, io.Writer, ...
h *HerraduraKEx h *HerraduraKEx
cipheropts uint32 // post-KEx cipher/hmac options cipheropts uint32 // post-KEx cipher/hmac options
@ -54,7 +55,6 @@ type Conn struct {
Rows uint16 Rows uint16
Cols uint16 Cols uint16
Rwmut *sync.Mutex
chaff bool chaff bool
chaffMsecsMin int //msecs min interval chaffMsecsMin int //msecs min interval
chaffMsecsMax int //msecs max 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 return nil, err
} }
// Init hkexnet.Conn hc over net.Conn c // 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...) hc.applyConnExtensions(extensions...)
// Send hkexnet.Conn parameters to remote side // Send hkexnet.Conn parameters to remote side
@ -274,15 +274,14 @@ func (hl HKExListener) Accept() (hc Conn, err error) {
// Open raw Conn c // Open raw Conn c
c, err := hl.l.Accept() c, err := hl.l.Accept()
if err != nil { 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} r: nil, w: nil}
return hc, err return hc, err
} }
log.Println("[Accepted]") log.Println("[Accepted]")
hc = Conn{c: c, h: New(0, 0), WinCh: make(chan WinSize, 1), hc = Conn{m: &sync.Mutex{}, c: c, h: New(0, 0), WinCh: make(chan WinSize, 1),
Rwmut: &sync.Mutex{}, dBuf: new(bytes.Buffer)}
dBuf: new(bytes.Buffer)}
// Read in hkexnet.Conn parameters over raw Conn c // Read in hkexnet.Conn parameters over raw Conn c
// d is value for Herradura key exchange // 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 // Would be nice to determine if the mutex scope
// could be tightened. // could be tightened.
c.Rwmut.Lock() c.m.Lock()
{ {
log.Printf(" :>ptext:\r\n%s\r\n", hex.Dump(b)) 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 { if err != nil {
//panic(err) //panic(err)