-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacket.go
161 lines (154 loc) · 4.98 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
157
158
159
160
161
package mqtt
import (
"encoding/binary"
)
//packet
type packet struct {
fixedHeader []byte
variableHeader []byte
payload []byte
}
//ConnectOptions These correspond to the connect flags
type ConnectOptions struct {
Username string
Password string
WillRetain bool
WillQOS QOS
WillFlag bool
CleanSession bool
KeepAlive uint16
WillTopic string
WillMessage string
ClientId string
}
//FormulateMQTTOutputData
func (pkt *packet) FormulateMQTTOutputData() []byte {
var s []byte
s = append(s, pkt.fixedHeader...)
s = append(s, pkt.variableHeader...)
s = append(s, pkt.payload...)
return s
}
func (pkt *packet) configureConnectPackets(options *ConnectOptions) {
pkt.fixedHeader = append(pkt.fixedHeader, ControlPktConnect)
//Protocol Name
pkt.variableHeader = append(pkt.variableHeader, 0x0)
pkt.variableHeader = append(pkt.variableHeader, byte(len("MQTT")))
pkt.variableHeader = append(pkt.variableHeader, []byte("MQTT")...)
//Protocol Level
pkt.variableHeader = append(pkt.variableHeader, byte(MqttProtocolLvl))
//Connect Flags
connectFlag := byte(0)
if options.CleanSession {
connectFlag |= FlagCleanSessionEnabled
}
if options.WillFlag {
connectFlag |= FlagWillFlagEnabled
if options.WillQOS == 0 {
connectFlag |= 0x00
}
if options.WillQOS == QOS1 {
connectFlag |= FlagWillQOS1
}
if options.WillQOS == QOS2 {
connectFlag |= FlagWillQOS2
}
if options.WillRetain {
connectFlag |= FlagWillRetain
}
}
if options.Username != "" {
connectFlag |= FlagUsername
if options.Password != "" {
connectFlag |= FlagPassword
}
}
pkt.variableHeader = append(pkt.variableHeader, connectFlag)
//KeepAlive bytes
var keepAlive uint16 = options.KeepAlive
k := make([]byte, 2)
binary.BigEndian.PutUint16(k, keepAlive)
pkt.variableHeader = append(pkt.variableHeader, k...)
//configure the payload
pkt.SetConnectPayload(options)
length := len(pkt.variableHeader) + len(pkt.payload)
remLength := encodeRemainingLength(uint64(length))
pkt.fixedHeader = append(pkt.fixedHeader, remLength...)
}
func (pkt *packet) SetConnectPayload(options *ConnectOptions) {
var data uint16 = uint16(len(options.ClientId))
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, data)
pkt.payload = append(pkt.payload, b...)
pkt.payload = append(pkt.payload, []byte(options.ClientId)...)
if options.WillFlag {
data = uint16(len(options.WillTopic))
binary.BigEndian.PutUint16(b, data)
pkt.payload = append(pkt.payload, b...)
pkt.payload = append(pkt.payload, []byte(options.WillTopic)...)
data = uint16(len(options.WillMessage))
binary.BigEndian.PutUint16(b, data)
pkt.payload = append(pkt.payload, b...)
pkt.payload = append(pkt.payload, []byte(options.WillMessage)...)
}
if len(options.Username) > 0 {
data = uint16(len(options.Username))
binary.BigEndian.PutUint16(b, data)
pkt.payload = append(pkt.payload, b...)
pkt.payload = append(pkt.payload, []byte(options.Username)...)
if len(options.Password) > 0 {
data = uint16(len(options.Password))
binary.BigEndian.PutUint16(b, data)
pkt.payload = append(pkt.payload, b...)
pkt.payload = append(pkt.payload, []byte(options.Password)...)
}
}
}
func (pkt *packet) configureSubscribePackets(topic string, qos QOS) {
pkt.fixedHeader = append(pkt.fixedHeader, ControlPktSubscribe|0x02)
var packetId uint16 = PacketIdentifier
k := make([]byte, 2)
binary.BigEndian.PutUint16(k, packetId)
pkt.variableHeader = append(pkt.variableHeader, k...)
n := make([]byte, 2)
var topicLen uint16 = uint16(len(topic))
binary.BigEndian.PutUint16(n, topicLen)
pkt.payload = append(pkt.payload, n...)
pkt.payload = append(pkt.payload, []byte(topic)...)
pkt.payload = append(pkt.payload, byte(qos))
length := len(pkt.variableHeader) + len(pkt.payload)
remLength := encodeRemainingLength(uint64(length))
pkt.fixedHeader = append(pkt.fixedHeader, remLength...)
}
func (pkt *packet) configurePingRequest() {
pkt.fixedHeader = append(pkt.fixedHeader, ControlPktPingReq)
pkt.fixedHeader = append(pkt.fixedHeader, 0)
}
func (pkt *packet) configurePublish(topic string, message string, dup bool, qos QOS) {
fixed := ControlPktPublish
if dup {
fixed |= 0x08
}
if qos == QOS1 {
fixed |= 0x02
} else if qos == QOS2 {
fixed |= 0x06
}
pkt.fixedHeader = append(pkt.fixedHeader, byte(fixed))
k := make([]byte, 2)
binary.BigEndian.PutUint16(k, uint16(len(topic)))
pkt.variableHeader = append(pkt.variableHeader, k...)
pkt.variableHeader = append(pkt.variableHeader, []byte(topic)...)
k = make([]byte, 2)
var packetId uint16 = PacketIdentifier
binary.BigEndian.PutUint16(k, packetId)
pkt.variableHeader = append(pkt.variableHeader, k...)
pkt.variableHeader = append(pkt.variableHeader, []byte(message)...)
length := len(pkt.variableHeader) + len(pkt.payload)
remLength := encodeRemainingLength(uint64(length))
pkt.fixedHeader = append(pkt.fixedHeader, remLength...)
}
func (pkt *packet) configureDisconnect() {
pkt.fixedHeader = append(pkt.fixedHeader, ControlPktDisconnect)
pkt.fixedHeader = append(pkt.fixedHeader, 0)
}