-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpacketacks.go
311 lines (243 loc) · 8.06 KB
/
packetacks.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package mqtt
import (
"bytes"
"fmt"
)
////////////////////Interface//////////////////////////////
type PacketAck interface {
Packet
//Variable Header
GetPacketId() uint16
SetPacketId(id uint16)
}
type PacketPuback interface {
PacketAck
}
type PacketPubrec interface {
PacketAck
}
type PacketPubrel interface {
PacketAck
}
type PacketPubcomp interface {
PacketAck
}
type PacketUnsuback interface {
PacketAck
}
type PacketSuback interface {
PacketAck
//Payload
GetReturnCodes() []byte
SetReturnCodes([]byte)
}
type PacketConnack interface {
PacketAck
//Variable Header
GetSPFlag() bool
SetSPFlag(b bool)
GetReturnCode() CONNACK_RETURNCODE
SetReturnCode(c CONNACK_RETURNCODE)
}
////////////////////Implementation////////////////////////
type packet_ack struct {
packet
packetId uint16
}
func NewPacketAcks(pt PacketType) *packet_ack {
if !(pt == PACKET_PUBACK || pt == PACKET_PUBREC || pt == PACKET_PUBREL || pt == PACKET_PUBCOMP || pt == PACKET_UNSUBACK) {
return nil
}
this := packet_ack{}
this.IBytizer = &this
this.IParser = &this
this.packetType = pt
if pt == PACKET_PUBREL || pt == PACKET_SUBSCRIBE || pt == PACKET_UNSUBSCRIBE {
this.packetFlag = 2
} else {
this.packetFlag = 0
}
return &this
}
func (this *packet_ack) IBytize() []byte {
var buffer bytes.Buffer
buffer.WriteByte((byte(this.packetType) << 4) | (this.packetFlag & 0x0F))
buffer.WriteByte(2)
buffer.WriteByte(byte(this.packetId >> 8))
buffer.WriteByte(byte(this.packetId & 0xFF))
return buffer.Bytes()
}
func (this *packet_ack) IParse(buffer []byte) error {
if buffer == nil || len(buffer) != 4 {
return fmt.Errorf("Invalid %s Control Packet Size %x\n", PACKET_TYPE_STRINGS[this.packetType], len(buffer))
}
if packetType := PacketType((buffer[0] >> 4) & 0x0F); packetType != this.packetType {
return fmt.Errorf("Invalid %s Control Packet Type %x\n", PACKET_TYPE_STRINGS[this.packetType], packetType)
}
if packetFlag := buffer[0] & 0x0F; packetFlag != this.packetFlag {
return fmt.Errorf("Invalid %s Control Packet Flags %x\n", PACKET_TYPE_STRINGS[this.packetType], packetFlag)
}
if buffer[1] != 2 {
return fmt.Errorf("Invalid %s Control Packet Remaining Length %x\n", PACKET_TYPE_STRINGS[this.packetType], buffer[1])
}
if this.packetId = ((uint16(buffer[2])) << 8) | uint16(buffer[3]); this.packetId == 0 {
return fmt.Errorf("Invalid %s Control Packet PacketId 0\n", PACKET_TYPE_STRINGS[this.packetType])
}
return nil
}
//Variable Header
func (this *packet_ack) GetPacketId() uint16 {
return this.packetId
}
func (this *packet_ack) SetPacketId(id uint16) {
this.packetId = id
}
/////////////////////
type packet_suback struct {
packet_ack
returnCodes []byte
}
func NewPacketSuback() *packet_suback {
this := packet_suback{}
this.IBytizer = &this
this.IParser = &this
this.packetType = PACKET_SUBACK
this.packetFlag = 0
return &this
}
func (this *packet_suback) IBytize() []byte {
var buffer bytes.Buffer
//Fixed Header
buffer.WriteByte((byte(this.packetType) << 4) | (this.packetFlag & 0x0F))
remainingLength := uint32(2 + len(this.returnCodes))
x, _ := this.EncodingRemainingLength(remainingLength)
buffer.Write(x)
//Variable Header
buffer.WriteByte(byte(this.packetId >> 8))
buffer.WriteByte(byte(this.packetId & 0xFF))
//Payload
buffer.Write(this.returnCodes)
return buffer.Bytes()
}
func (this *packet_suback) IParse(buffer []byte) error {
var err error
var bufferLength, remainingLength, consumedBytes uint32
bufferLength = uint32(len(buffer))
if buffer == nil || bufferLength < 5 {
return fmt.Errorf("Invalid %x Control Packet Size %x\n", PACKET_TYPE_STRINGS[this.packetType], bufferLength)
}
//Fixed Header
if packetType := PacketType((buffer[0] >> 4) & 0x0F); packetType != this.packetType {
return fmt.Errorf("Invalid %s Control Packet Type %x\n", PACKET_TYPE_STRINGS[this.packetType], packetType)
}
if packetFlag := buffer[0] & 0x0F; packetFlag != this.packetFlag {
return fmt.Errorf("Invalid %s Control Packet Flags %x\n", PACKET_TYPE_STRINGS[this.packetType], packetFlag)
}
if remainingLength, consumedBytes, err = this.DecodingRemainingLength(buffer[1:]); err != nil {
return fmt.Errorf("Invalid %s Control Packet DecodingRemainingLength %s\n", PACKET_TYPE_STRINGS[this.packetType], err.Error())
}
if consumedBytes += 1; bufferLength < consumedBytes+remainingLength {
return fmt.Errorf("Invalid %s Control Packet Remaining Length %x\n", PACKET_TYPE_STRINGS[this.packetType], remainingLength)
}
buffer = buffer[:consumedBytes+remainingLength]
bufferLength = consumedBytes + remainingLength
//Variable Header
if this.packetId = ((uint16(buffer[consumedBytes])) << 8) | uint16(buffer[consumedBytes+1]); this.packetId == 0 {
return fmt.Errorf("Invalid %s Control Packet PacketId 0\n", PACKET_TYPE_STRINGS[this.packetType])
}
if consumedBytes += 2; bufferLength < consumedBytes+1 {
return fmt.Errorf("Invalid %s Control Packet Must Have at least One Return Code\n", PACKET_TYPE_STRINGS[this.packetType])
}
//Payload
this.returnCodes = make([]byte, remainingLength-2)
copy(this.returnCodes, buffer[consumedBytes:consumedBytes+remainingLength-2])
for i := 0; i < int(remainingLength-2); i++ {
if !(this.returnCodes[i] <= 0x02 || this.returnCodes[i] == 0x80) {
return fmt.Errorf("Invalid %s Control Packet Return Code %02x\n", PACKET_TYPE_STRINGS[this.packetType], this.returnCodes[i])
}
}
return nil
}
//Payload
func (this *packet_suback) GetReturnCodes() []byte {
return this.returnCodes
}
func (this *packet_suback) SetReturnCodes(returnCodes []byte) {
this.returnCodes = make([]byte, len(returnCodes))
copy(this.returnCodes, returnCodes)
}
////////////////////Implementation////////////////////////
type CONNACK_RETURNCODE byte
const (
CONNACK_RETURNCODE_ACCEPTED CONNACK_RETURNCODE = iota
CONNACK_RETURNCODE_REFUSED_UNACCEPTABLE_PROTOCOL_VERSION
CONNACK_RETURNCODE_REFUSED_IDENTIFIER_REJECTED
CONNACK_RETURNCODE_REFUSED_SERVER_UNAVAILABLE
CONNACK_RETURNCODE_REFUSED_BAD_USERNAME_OR_PASSWORD
CONNACK_RETURNCODE_REFUSED_NOT_AUTHORIZED
)
type packet_connack struct {
packet_ack
spFlag byte
returnCode CONNACK_RETURNCODE
}
func NewPacketConnack() *packet_connack {
this := packet_connack{}
this.IBytizer = &this
this.IParser = &this
this.packetType = PACKET_CONNACK
this.packetFlag = 0
return &this
}
func (this *packet_connack) IBytize() []byte {
var buffer bytes.Buffer
//Fixed Header
buffer.WriteByte((byte(this.packetType) << 4) | (this.packetFlag & 0x0F))
buffer.WriteByte(2)
//Variable Header
buffer.WriteByte(byte(this.spFlag))
buffer.WriteByte(byte(this.returnCode))
return buffer.Bytes()
}
func (this *packet_connack) IParse(buffer []byte) error {
if buffer == nil || len(buffer) != 4 {
return fmt.Errorf("Invalid %s Control Packet Size %x\n", PACKET_TYPE_STRINGS[this.packetType], len(buffer))
}
//Fixed Header
if packetType := PacketType((buffer[0] >> 4) & 0x0F); packetType != this.packetType {
return fmt.Errorf("Invalid %s Control Packet Type %x\n", PACKET_TYPE_STRINGS[this.packetType], packetType)
}
if packetFlag := buffer[0] & 0x0F; packetFlag != this.packetFlag {
return fmt.Errorf("Invalid %s Control Packet Flags %x\n", PACKET_TYPE_STRINGS[this.packetType], packetFlag)
}
if buffer[1] != 2 {
return fmt.Errorf("Invalid %s Control Packet Remaining Length %x\n", PACKET_TYPE_STRINGS[this.packetType], buffer[1])
}
//Variable Header
if buffer[2]&0xFE != 0 {
return fmt.Errorf("Invalid %s Control Packet Acknowledge Flags %x\n", PACKET_TYPE_STRINGS[this.packetType], buffer[2])
}
this.spFlag = buffer[2] & 0x01
if buffer[3] > 0x05 {
return fmt.Errorf("Invalid %s Control Packet Return Code %x\n", PACKET_TYPE_STRINGS[this.packetType], buffer[3])
}
this.returnCode = CONNACK_RETURNCODE(buffer[3])
return nil
}
//Variable Header
func (this *packet_connack) GetSPFlag() bool {
return this.spFlag != 0
}
func (this *packet_connack) SetSPFlag(spFlag bool) {
if spFlag {
this.spFlag = 1
} else {
this.spFlag = 0
}
}
func (this *packet_connack) GetReturnCode() CONNACK_RETURNCODE {
return this.returnCode
}
func (this *packet_connack) SetReturnCode(c CONNACK_RETURNCODE) {
this.returnCode = c
}