Dial() and Accept() again conform to net.Dial(), net.Accept() return signature

This commit is contained in:
Russ Magee 2018-09-29 12:15:53 -07:00
parent e57d97d3e6
commit cd9f7914e0
3 changed files with 13 additions and 13 deletions

View File

@ -251,17 +251,17 @@ func HKExAcceptSetup(c net.Conn, hc *Conn) (err error) {
// "C_AES_256" | "C_TWOFISH_128" // "C_AES_256" | "C_TWOFISH_128"
// //
// "H_SHA256" // "H_SHA256"
func Dial(protocol string, ipport string, extensions ...string) (hc *Conn, err error) { func Dial(protocol string, ipport string, extensions ...string) (hc Conn, err error) {
// Open raw Conn c // Open raw Conn c
c, err := net.Dial(protocol, ipport) c, err := net.Dial(protocol, ipport)
if err != nil { if err != nil {
return nil, err return hc, err
} }
// Init hkexnet.Conn hc over net.Conn c // Init hkexnet.Conn hc over net.Conn c
// NOTE: kex default of KEX_HERRADURA may be overridden by // NOTE: kex default of KEX_HERRADURA may be overridden by
// future extension args to applyConnExtensions(), which is // future extension args to applyConnExtensions(), which is
// called prior to Dial() // called prior to Dial()
hc = &Conn{m: &sync.Mutex{}, c: c, closeStat: new(CSOType), h: hkex.New(0, 0), dBuf: new(bytes.Buffer), totBytes: new(uint64), totPackets: new(uint64)} hc = Conn{m: &sync.Mutex{}, c: c, closeStat: new(CSOType), h: hkex.New(0, 0), dBuf: new(bytes.Buffer), totBytes: new(uint64), totPackets: new(uint64)}
hc.applyConnExtensions(extensions...) hc.applyConnExtensions(extensions...)
// TODO: Factor out ALL params following this to helpers for // TODO: Factor out ALL params following this to helpers for
@ -274,19 +274,19 @@ func Dial(protocol string, ipport string, extensions ...string) (hc *Conn, err e
// Perform Key Exchange according to client-request algorithm // Perform Key Exchange according to client-request algorithm
switch hc.kex { switch hc.kex {
case KEX_HERRADURA: case KEX_HERRADURA:
if HKExDialSetup(c, hc) != nil { if HKExDialSetup(c, &hc) != nil {
return hc, nil return hc, nil
} }
case KEX_FOO: case KEX_FOO:
// For testing: set up as HKEx anyway, but server via Accept() should // For testing: set up as HKEx anyway, but server via Accept() should
// reject as invalid. // reject as invalid.
//if FooKExDialSetup(c, hc) != nil { //if FooKExDialSetup(c, hc) != nil {
if HKExDialSetup(c, hc) != nil { if HKExDialSetup(c, &hc) != nil {
return hc, nil return hc, nil
} }
default: default:
log.Printf("Invalid kex alg (%d), rejecting\n", hc.kex) log.Printf("Invalid kex alg (%d), rejecting\n", hc.kex)
return nil, errors.New("Invalid kex alg") return hc, errors.New("Invalid kex alg")
} }
return return
} }
@ -389,17 +389,17 @@ func (hl HKExListener) Addr() net.Addr {
// Accept a client connection, conforming to net.Listener.Accept() // Accept a client connection, conforming to net.Listener.Accept()
// //
// See go doc net.Listener.Accept // See go doc net.Listener.Accept
func (hl *HKExListener) Accept() (hc *Conn, err error) { 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{m: &sync.Mutex{}, c: nil, h: nil, closeStat: new(CSOType), cipheropts: 0, opts: 0, hc := Conn{m: &sync.Mutex{}, c: nil, h: nil, closeStat: new(CSOType), cipheropts: 0, opts: 0,
r: nil, w: nil, totBytes: new(uint64), totPackets: new(uint64)} r: nil, w: nil, totBytes: new(uint64), totPackets: new(uint64)}
return hc, err return hc, err
} }
log.Println("[Accepted]") log.Println("[Accepted]")
hc = &Conn{ /*kex: from client,*/ m: &sync.Mutex{}, c: c, h: hkex.New(0, 0), closeStat: new(CSOType), WinCh: make(chan WinSize, 1), hc = Conn{ /*kex: from client,*/ m: &sync.Mutex{}, c: c, h: hkex.New(0, 0), closeStat: new(CSOType), WinCh: make(chan WinSize, 1),
dBuf: new(bytes.Buffer), totBytes: new(uint64), totPackets: new(uint64)} dBuf: new(bytes.Buffer), totBytes: new(uint64), totPackets: new(uint64)}
// TODO: Factor out ALL params following this to helpers for // TODO: Factor out ALL params following this to helpers for
@ -415,7 +415,7 @@ func (hl *HKExListener) Accept() (hc *Conn, err error) {
switch kexAlg { switch kexAlg {
case KEX_HERRADURA: case KEX_HERRADURA:
log.Printf("[KEx alg %d accepted]\n", kexAlg) log.Printf("[KEx alg %d accepted]\n", kexAlg)
if HKExAcceptSetup(c, hc) != nil { if HKExAcceptSetup(c, &hc) != nil {
return hc, nil return hc, nil
} }
default: default:

View File

@ -613,9 +613,9 @@ func main() {
} }
if shellMode { if shellMode {
doShellMode(isInteractive, conn, oldState, rec) doShellMode(isInteractive, &conn, oldState, rec)
} else { // copyMode } else { // copyMode
_, s := doCopyMode(conn, pathIsDest, fileArgs, rec) _, s := doCopyMode(&conn, pathIsDest, fileArgs, rec)
rec.SetStatus(s) rec.SetStatus(s)
} }

View File

@ -606,7 +606,7 @@ func main() {
log.Println("[Bad hkexsh.Session]") log.Println("[Bad hkexsh.Session]")
} }
return return
}(conn) }(&conn)
} // Accept() success } // Accept() success
} //endfor } //endfor
log.Println("[Exiting]") log.Println("[Exiting]")