mirror of
https://github.com/bolkedebruin/rdpgw.git
synced 2025-08-17 14:03:50 +02:00
37 lines
922 B
Go
37 lines
922 B
Go
package protocol
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"errors"
|
|
"io"
|
|
)
|
|
|
|
func createPacket(pktType uint16, data []byte) (packet []byte) {
|
|
size := len(data) + 8
|
|
buf := new(bytes.Buffer)
|
|
|
|
binary.Write(buf, binary.LittleEndian, uint16(pktType))
|
|
binary.Write(buf, binary.LittleEndian, uint16(0)) // reserved
|
|
binary.Write(buf, binary.LittleEndian, uint32(size))
|
|
buf.Write(data)
|
|
|
|
return buf.Bytes()
|
|
}
|
|
|
|
func readHeader(data []byte) (packetType uint16, size uint32, packet []byte, err error) {
|
|
// header needs to be 8 min
|
|
if len(data) < 8 {
|
|
return 0, 0, nil, errors.New("header too short, fragment likely")
|
|
}
|
|
r := bytes.NewReader(data)
|
|
binary.Read(r, binary.LittleEndian, &packetType)
|
|
r.Seek(4, io.SeekStart)
|
|
binary.Read(r, binary.LittleEndian, &size)
|
|
if len(data) < int(size) {
|
|
return packetType, size, data[8:], errors.New("data incomplete, fragment received")
|
|
}
|
|
return packetType, size, data[8:], nil
|
|
}
|
|
|
|
|