-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet.go
36 lines (29 loc) · 958 Bytes
/
packet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package samil
import (
"encoding/binary"
)
var advertisement = forgePacket([3]byte{0, 64, 2}, []byte("I AM SERVER"))
var model = forgePacket([3]byte{1, 3, 2}, nil)
var data = forgePacket([3]byte{1, 2, 2}, nil)
// Start and end are both the last two digits of a year number.
// I.e. 05, 07 means 2005 and 2007.
func historyPacket(start, end int) []byte {
payload := []byte{byte(start), byte(end)}
return forgePacket([3]byte{6, 1, 2}, payload)
}
func forgePacket(header [3]byte, payload []byte) []byte {
request := make([]byte, 2+3+2+len(payload)+2)
copy(request[0:2], []byte{0x55, 0xaa})
copy(request[2:5], header[:])
binary.BigEndian.PutUint16(request[5:7], uint16(len(payload)))
copy(request[7:7+len(payload)], payload)
checksum := uint16(checksum(request[:]))
binary.BigEndian.PutUint16(request[7+len(payload):], checksum)
return request
}
func checksum(packet []byte) (sum int) {
for _, b := range packet {
sum += int(b)
}
return
}