add magic header to gaurantee packet fitness

This commit is contained in:
Zack Scholl 2020-11-23 12:41:30 -08:00
parent 5978896936
commit 6b18337afe
1 changed files with 14 additions and 0 deletions

View File

@ -84,6 +84,7 @@ func (c *Comm) Write(b []byte) (n int, err error) {
fmt.Println("binary.Write failed:", err)
}
tmpCopy := append(header.Bytes(), b...)
tmpCopy = append([]byte("croc"), tmpCopy...)
n, err = c.connection.Write(tmpCopy)
if err != nil {
err = fmt.Errorf("connection.Write failed: %w", err)
@ -111,6 +112,19 @@ func (c *Comm) Read() (buf []byte, numBytes int, bs []byte, err error) {
log.Debugf("initial read error: %v", err)
return
}
if !bytes.Equal(header, []byte("croc")) {
err = fmt.Errorf("bad packet")
log.Debugf("no croc header: %+v", err)
return
}
// read until we get 4 bytes for the header
header = make([]byte, 4)
_, err = io.ReadFull(c.connection, header)
if err != nil {
log.Debugf("initial read error: %v", err)
return
}
var numBytesUint32 uint32
rbuf := bytes.NewReader(header)