-
Notifications
You must be signed in to change notification settings - Fork 1
/
packet.go
156 lines (133 loc) · 2.9 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package inform
import (
"errors"
"github.com/ZAP-Quebec/unifi-inform/binary"
messages "github.com/ZAP-Quebec/unifi-inform/data"
)
const (
MAGIC_NUMBER uint32 = 1414414933
INFORM_VERSION uint32 = 0
DATA_VERSION uint32 = 1
ENCRYPT_FLAG uint16 = 1
ZLIB_FLAG uint16 = 2
SNAPPY_FLAG uint16 = 4
)
var (
NIL_IV IV = []byte{
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
}
)
type Packet struct {
ap messages.MacAddr
flags uint16
key messages.Key
Msg messages.Message
}
func NewPacket(ap messages.MacAddr, msg messages.Message, k messages.Key) *Packet {
flags := SNAPPY_FLAG
if k != nil {
flags = flags | ENCRYPT_FLAG
}
return &Packet{
ap: ap,
key: k,
Msg: msg,
flags: flags,
}
}
func (p Packet) IsEncrypted() bool {
return p.flags&ENCRYPT_FLAG == ENCRYPT_FLAG
}
func (p Packet) IsZLib() bool {
return p.flags&ZLIB_FLAG == ZLIB_FLAG
}
func (p Packet) IsSnappy() bool {
return p.flags&SNAPPY_FLAG == SNAPPY_FLAG
}
func (p Packet) Marshal() (result []byte, err error) {
msg := p.Msg.Marshal()
if p.IsZLib() {
msg, err = CompressZLib(msg)
if err != nil {
return
}
} else if p.IsSnappy() {
msg, err = CompressSnappy(msg)
if err != nil {
return
}
}
iv := NIL_IV
if p.IsEncrypted() {
iv, err = GenerateIV()
if err != nil {
return
}
msg, err = Encrypt(iv, p.key, msg)
if err != nil {
return
}
}
if len(p.ap) != 6 {
return nil, errors.New("Invalid length of MAC address")
}
b := binary.NewBuffer(uint(40 + len(msg)))
b.WriteUInt32BE(0, MAGIC_NUMBER)
b.WriteUInt32BE(4, INFORM_VERSION)
b.Write(8, p.ap)
b.WriteUInt16BE(14, p.flags)
b.Write(16, iv)
b.WriteUInt32BE(32, DATA_VERSION)
b.WriteUInt32BE(36, uint32(len(msg)))
b.Write(40, msg)
return b, nil
}
func (p *Packet) Unmarshal(data []byte, keyFetcher func(messages.MacAddr) (messages.Key, error)) (err error) {
b := binary.Buffer(data)
if len(data) < 40 {
return errors.New("Invalid packet length.")
}
dataLength := uint(b.ReadUInt32BE(36))
if uint(len(data)) < dataLength+40 {
return errors.New("Invalid packet length.")
}
if b.ReadUInt32BE(0) != MAGIC_NUMBER {
return errors.New("Invalid magic number at start of packet.")
}
if b.ReadUInt32BE(4) != INFORM_VERSION {
return errors.New("Unkwown inform version.")
}
if b.ReadUInt32BE(32) != DATA_VERSION {
return errors.New("Unkwown data version.")
}
p.ap = b.Read(8, 14)
p.flags = b.ReadUInt16BE(14)
msg := b.Read(40, 40+dataLength)
if p.IsEncrypted() {
iv := IV(b.Read(16, 32))
p.key, err = keyFetcher(p.ap)
if err != nil {
return err
}
msg, err = Decrypt(iv, p.key, msg)
if err != nil {
return err
}
}
if p.IsZLib() {
msg, err = DecompressZLib(msg)
if err != nil {
return err
}
} else if p.IsSnappy() {
msg, err = DecompressSnappy(msg)
if err != nil {
return err
}
}
p.Msg, err = messages.Unmarshal(msg)
return err
}