# HG changeset patch # User sqwishy # Date 1391321387 28800 # Sat Feb 01 22:09:47 2014 -0800 # Node ID 034f7268bbd6c0c45968c5813fa1e2c2feefe367 # Parent cda543e75b9c75da8b4a97192592780ec79176d8 reading responses and writing them and stuff I guess diff --git a/gorcon/gorcon.go b/gorcon/gorcon.go --- a/gorcon/gorcon.go +++ b/gorcon/gorcon.go @@ -3,12 +3,13 @@ import "encoding/binary" import "fmt" import "io" -import "bytes" import "log" var _ = fmt.Println var _ = log.Println +var InvalidResponse = fmt.Errorf("Invalid response") + const ( SERVERDATA_AUTH = CmdType(3) SERVERDATA_AUTH_RESPONSE = CmdType(2) @@ -18,12 +19,21 @@ type CmdType int32 -type packetHeader struct{ +type packetHeader struct { Size int32 Id int32 Type int32 } +type packet struct { + *packetHeader + Body []byte +} + +func (p *packet) String() string { + return fmt.Sprintf("&{packetHeader:%+v Body:%v}", p.packetHeader, p.Body) +} + func WriteCommandString(w io.Writer, cmdtype CmdType, body string) error { return WriteCommand(w, cmdtype, []byte(body)) } @@ -38,18 +48,43 @@ Id: int32(0), Type: int32(cmdtype), } - // TODO is this buffer shit neccessary? - var buf bytes.Buffer - if err := binary.Write(&buf, binary.LittleEndian, p); err != nil { + if err := binary.Write(w, binary.LittleEndian, p); err != nil { return err } - if _, err := buf.Write(body); err != nil { + if _, err := w.Write(body); err != nil { + return err + } + // 16 bits here for our terminal characters + // 8 bits for the message terminal, and another 8 because body won't/shouldn't have one + if err := binary.Write(w, binary.LittleEndian, int16(0)); err != nil { return err } - // 16 bits here since []byte doesn't add a terminal character - if err := binary.Write(&buf, binary.LittleEndian, int16(0)); err != nil { - return err + return nil +} + +func ReadResp(r io.Reader, ) (p *packet, err error) { + p = &packet{ + packetHeader: &packetHeader{}, + } + if err = binary.Read(r, binary.LittleEndian, &p.Size); err != nil { + return + } + if p.Size < 10 { + err = InvalidResponse + return } - _, err := io.Copy(w, &buf) - return err + if err = binary.Read(r, binary.LittleEndian, &p.Id); err != nil { + return + } + if err = binary.Read(r, binary.LittleEndian, &p.Type); err != nil { + return + } + p.Body = make([]byte, p.Size - 10) + if err = binary.Read(r, binary.LittleEndian, &p.Body); err != nil { + return + } + if _, err = r.Read(make([]byte, 2)); err != nil { + return + } + return } diff --git a/main.go b/main.go --- a/main.go +++ b/main.go @@ -6,7 +6,6 @@ import "./gorcon" // Relative imports! Woo hoo! import "io" import "bufio" -import "encoding/binary" var _ = fmt.Println var _ = log.Println @@ -18,44 +17,6 @@ Body []byte } -func readResp(r io.Reader, ) (*response, error) { - var size int32 - if err := binary.Read(r, binary.LittleEndian, &size); err != nil { - return nil, err - } - log.Println("size: ", size) - //if size < 10 { - // return nil, "invalid response data, oh noes!" - //} - - var id int32 - if err := binary.Read(r, binary.LittleEndian, &id); err != nil { - return nil, err - } - - var cmdtype gorcon.CmdType - if err := binary.Read(r, binary.LittleEndian, &cmdtype); err != nil { - return nil, err - } - - body := make([]byte, size - 10) - if err := binary.Read(r, binary.LittleEndian, body); err != nil { - return nil, err - } - - var end int16 - err := binary.Read(r, binary.LittleEndian, &end) - if err != nil { - return nil, err - } - - return &response{ - Id: id, - Type: cmdtype, - Body: body, - }, nil -} - func main() { log.Println("Hello, world!") @@ -69,14 +30,14 @@ go func() { r := bufio.NewReader(conn) for { - resp, err := readResp(r) + resp, err := gorcon.ReadResp(r) if err != nil { if err == io.EOF { return } log.Panicln(err) } - log.Println("<<< ", resp) + log.Printf("<<< %+v\n", resp) } defer func() { done<-true @@ -85,7 +46,7 @@ log.Println(conn) - err = gorcon.WriteCommandString(conn, gorcon.SERVERDATA_AUTH, "passwrd") + err = gorcon.WriteCommandString(conn, gorcon.SERVERDATA_AUTH, "hunter2") if err != nil { log.Panicln(err) }