M gorcon/gorcon.go +46 -11
@@ 3,12 3,13 @@ package gorcon
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 @@ const (
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 @@ func WriteCommand(w io.Writer, cmdtype C
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
}
M main.go +3 -42
@@ 6,7 6,6 @@ import "net"
import "./gorcon" // Relative imports! Woo hoo!
import "io"
import "bufio"
-import "encoding/binary"
var _ = fmt.Println
var _ = log.Println
@@ 18,44 17,6 @@ type response struct {
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 @@ func main() {
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 @@ func main() {
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)
}