client/server demo hkex.Dial(), hkex.Listen()/hl.Accept() with auto-KEx

This commit is contained in:
Russ Magee 2018-01-06 23:58:30 +00:00
parent 4dd121b10b
commit 60f2cb7e26
5 changed files with 100 additions and 26 deletions

Binary file not shown.

View File

@ -2,22 +2,18 @@ package main
import (
"fmt"
"net"
hkex "blitter.com/herradurakex"
)
func main() {
bareconn, err := net.Dial("tcp", "localhost:2000")
conn, err := hkex.Dial("tcp", "localhost:2000")
if err != nil {
// handle error
}
conn := hkex.NewHKExConn(&bareconn)
fmt.Printf("conn: %v\n", conn)
// fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n")
// status, err := bufio.NewReader(conn).ReadString('\n')
// _, err = bufio.NewReader(conn).ReadString('\n')
// fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n")
// status, err := bufio.NewReader(conn).ReadString('\n')
// _, err = bufio.NewReader(conn).ReadString('\n')
// ...
}

Binary file not shown.

View File

@ -4,14 +4,14 @@ import (
"fmt"
"io"
"log"
"net"
// "blitter.com/herradurakex"
//"net"
hkex "blitter.com/herradurakex"
)
func main() {
// Listen on TCP port 2000 on all available unicast and
// anycast IP addresses of the local system.
l, err := net.Listen("tcp", ":2000")
l, err := hkex.Listen("tcp", ":2000")
if err != nil {
log.Fatal(err)
}
@ -30,7 +30,7 @@ func main() {
// Handle the connection in a new goroutine.
// The loop then returns to accepting, so that
// multiple connections may be served concurrently.
go func(c net.Conn) {
go func(c hkex.HKExConn) {
// Echo all incoming data.
io.Copy(c, c)
fmt.Println("Client sent:%v\n", c)

View File

@ -47,7 +47,7 @@ func New(i int, p int) (h *HerraduraKEx) {
if i == 0 {
i = 256
}
if p == 0 {
if p == 0 {
p = 64
}
@ -143,33 +143,93 @@ func (h *HerraduraKEx) String() string {
h.PeerD.Text(16),
h.fa.Text(16))
}
/*---------------------------------------------------------------------*/
type HKExConn struct {
c net.Conn // which also implements io.Reader, io.Writer, ...
c net.Conn // which also implements io.Reader, io.Writer, ...
h *HerraduraKEx
}
// Return c coerced into a HKExConn (which implements interface net.Conn)
func NewHKExConn(c *net.Conn) (hc *HKExConn) {
//fmt.Println("** NewHKExConn wrapping net.Conn **")
hc = new(HKExConn)
hc.c = *c
hc.h = New(0,0)
d := big.NewInt(0)
_,err := fmt.Fscanln(hc.c, d)
// Dial as net.Dial(), but with implicit HKEx PeerD read on connect
func Dial(protocol string, ipport string) (hc *HKExConn, err error) {
c, err := net.Dial(protocol, ipport)
if err != nil {
//
return nil, err
}
hc = &HKExConn{c, New(0, 0)}
// Stub KEx
fmt.Fprintf(c, "0x%s\n", hc.h.d.Text(16))
d := big.NewInt(0)
_, err = fmt.Fscanln(c, d)
if err != nil {
return nil, err
}
hc.h.PeerD = d
fmt.Printf("** peerD:%s\n", hc.h.PeerD.Text(16))
fmt.Printf("**(c)** peerD:%s\n", hc.h.PeerD.Text(16))
hc.h.FA()
fmt.Printf("**(c)** FA:%s\n", hc.h.fa)
return
}
func (hc *HKExConn) Close() (err error) {
err = hc.c.Close()
fmt.Println("[Conn Closing]")
return
}
/*---------------------------------------------------------------------*/
type HKExListener struct {
l net.Listener
}
func Listen(protocol string, ipport string) (hl HKExListener, e error) {
l, err := net.Listen(protocol, ipport)
if err != nil {
return HKExListener{nil}, err
}
fmt.Println("[Listening]")
hl.l = l
return
}
func (hl *HKExListener) Close() {
hl.l.Close()
fmt.Println("[Listener Closed]")
}
func (hl *HKExListener) Accept() (hc HKExConn, err error) {
c, err := hl.l.Accept()
fmt.Println("[Accepted]")
if err != nil {
return HKExConn{nil, nil}, err
}
hc = HKExConn{c, New(0, 0)}
d := big.NewInt(0)
_, err = fmt.Fscanln(c, d)
if err != nil {
return hc, err
}
hc.h.PeerD = d
fmt.Printf("**(s)** peerD:%s\n", hc.h.PeerD.Text(16))
hc.h.FA()
fmt.Printf("**(s)** FA:%s\n", hc.h.fa)
// Stub KEx
fmt.Fprintf(c, "0x%s\n", hc.h.d.Text(16))
return
}
/*---------------------------------------------------------------------*/
func (hc HKExConn) Read(b []byte) (n int, err error) {
n, err = hc.c.Read(b)
if n > 0 {
//fmt.Println("** hc.Read() wraps c.Read() **")
fmt.Println("** hc.Read() wraps c.Read() **")
}
return
}
@ -181,3 +241,21 @@ func (hc HKExConn) Write(b []byte) (n int, err error) {
}
return
}
// Return c coerced into a HKExConn (which implements interface net.Conn)
// Only useful if one wants to convert an open connection later to HKEx
// (Use Dial() instead to start with HKEx automatically.)
func NewHKExConn(c *net.Conn) (hc *HKExConn) {
hc = new(HKExConn)
hc.c = *c
hc.h = New(0, 0)
d := big.NewInt(0)
_, err := fmt.Fscanln(hc.c, d)
if err != nil {
//
}
hc.h.PeerD = d
fmt.Printf("** peerD:%s\n", hc.h.PeerD.Text(16))
return
}