forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amqp_parser.go
338 lines (293 loc) · 8.81 KB
/
amqp_parser.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package amqp
import (
"encoding/binary"
"time"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/packetbeat/procs"
)
func (amqp *amqpPlugin) amqpMessageParser(s *amqpStream) (ok bool, complete bool) {
for s.parseOffset < len(s.data) {
if len(s.data[s.parseOffset:]) < 8 {
logp.Warn("AMQP message smaller than a frame, waiting for more data")
return true, false
}
yes, version := isProtocolHeader(s.data[s.parseOffset:])
if yes {
debugf("Client header detected, version %d.%d.%d",
version[0], version[1], version[2])
s.parseOffset += 8
}
f, err := readFrameHeader(s.data[s.parseOffset:])
if err {
//incorrect header
return false, false
} else if f == nil {
//header not complete
return true, false
}
switch f.Type {
case methodType:
ok, complete = amqp.decodeMethodFrame(s, f.content)
case headerType:
ok = amqp.decodeHeaderFrame(s, f.content)
case bodyType:
ok, complete = s.decodeBodyFrame(f.content)
case heartbeatType:
detailedf("Heartbeat frame received")
default:
logp.Warn("Received unknown AMQP frame")
return false, false
}
// cast should be safe because f.size should not be bigger than tcp.TCP_MAX_DATA_IN_STREAM
s.parseOffset += 8 + int(f.size)
if !ok {
return false, false
} else if complete {
return true, true
}
}
return ok, complete
}
func (s *amqpStream) prepareForNewMessage() {
s.message = nil
}
func isProtocolHeader(data []byte) (isHeader bool, version string) {
if (string(data[:4]) == "AMQP") && data[4] == 0 {
return true, string(data[5:8])
}
return false, ""
}
//func to read a frame header and check if it is valid and complete
func readFrameHeader(data []byte) (ret *amqpFrame, err bool) {
var frame amqpFrame
frame.size = binary.BigEndian.Uint32(data[3:7])
if len(data) < int(frame.size)+8 {
logp.Warn("Frame shorter than declared size, waiting for more data")
return nil, false
}
if data[frame.size+7] != frameEndOctet {
logp.Warn("Missing frame end octet in frame, discarding it")
return nil, true
}
frame.Type = frameType(data[0])
if frame.size == 0 {
//frame content is nil with hearbeat frames
frame.content = nil
} else {
frame.content = data[7 : frame.size+7]
}
return &frame, false
}
/*
The Method Payload, according to official doc :
0 2 4
+----------+-----------+-------------- - -
| class-id | method-id | arguments...
+----------+-----------+-------------- - -
short short ...
*/
func (amqp *amqpPlugin) decodeMethodFrame(s *amqpStream, buf []byte) (bool, bool) {
if len(buf) < 4 {
logp.Warn("Method frame too small, waiting for more data")
return true, false
}
class := codeClass(binary.BigEndian.Uint16(buf[0:2]))
method := codeMethod(binary.BigEndian.Uint16(buf[2:4]))
arguments := buf[4:]
s.message.parseArguments = amqp.parseArguments
s.message.bodySize = uint64(len(buf[4:]))
debugf("Received frame of class %d and method %d", class, method)
fn, exists := amqp.methodMap[class][method]
if !exists {
logp.Debug("amqpdetailed", "Received unknown or not supported method")
return false, false
}
return fn(s.message, arguments)
}
/*
Structure of a content header, according to official doc :
0 2 4 12 14
+----------+--------+-----------+----------------+------------- - -
| class-id | weight | body size | property flags | property list...
+----------+--------+-----------+----------------+------------- - -
short short long long short remainder...
*/
func (amqp *amqpPlugin) decodeHeaderFrame(s *amqpStream, buf []byte) bool {
if len(buf) < 14 {
logp.Warn("Header frame too small, waiting for mode data")
return true
}
s.message.bodySize = binary.BigEndian.Uint64(buf[4:12])
debugf("Received Header frame. A message of %d bytes is expected", s.message.bodySize)
if amqp.parseHeaders == true {
err := getMessageProperties(s, buf[12:])
if err {
return false
}
}
return true
}
/*
Structure of a body frame, according to official doc :
+-----------------------+ +-----------+
| Opaque binary payload | | frame-end |
+-----------------------+ +-----------+
*/
func (s *amqpStream) decodeBodyFrame(buf []byte) (ok bool, complete bool) {
s.message.body = append(s.message.body, buf...)
debugf("A body frame of %d bytes long has been transmitted",
len(buf))
//is the message complete ? If yes, let's publish it
complete = uint64(len(s.message.body)) >= s.message.bodySize
return true, complete
}
func hasProperty(prop, flag byte) bool {
return (prop & flag) == flag
}
//function to get message content-type and content-encoding
func getMessageProperties(s *amqpStream, data []byte) bool {
m := s.message
//properties are coded in the two first bytes
prop1 := data[0]
prop2 := data[1]
var offset uint32 = 2
//while last bit set, we have another property flag field
for lastbit := 1; data[lastbit]&1 == 1; {
lastbit += 2
offset += 2
}
if hasProperty(prop1, contentTypeProp) {
contentType, next, err := getShortString(data, offset+1, uint32(data[offset]))
if err {
logp.Warn("Failed to get content type in header frame")
return true
}
m.fields["content-type"] = contentType
offset = next
}
if hasProperty(prop1, contentEncodingProp) {
contentEncoding, next, err := getShortString(data, offset+1, uint32(data[offset]))
if err {
logp.Warn("Failed to get content encoding in header frame")
return true
}
m.fields["content-encoding"] = contentEncoding
offset = next
}
if hasProperty(prop1, headersProp) {
headers := common.MapStr{}
next, err, exists := getTable(headers, data, offset)
if !err && exists {
m.fields["headers"] = headers
} else if err {
logp.Warn("Failed to get headers")
return true
}
offset = next
}
if hasProperty(prop1, deliveryModeProp) {
if data[offset] == 1 {
m.fields["delivery-mode"] = "non-persistent"
} else if data[offset] == 2 {
m.fields["delivery-mode"] = "persistent"
}
offset++
}
if hasProperty(prop1, priorityProp) {
m.fields["priority"] = data[offset]
offset++
}
if hasProperty(prop1, correlationIDProp) {
correlationID, next, err := getShortString(data, offset+1, uint32(data[offset]))
if err {
logp.Warn("Failed to get correlation-id in header frame")
return true
}
m.fields["correlation-id"] = correlationID
offset = next
}
if hasProperty(prop1, replyToProp) {
replyTo, next, err := getShortString(data, offset+1, uint32(data[offset]))
if err {
logp.Warn("Failed to get reply-to in header frame")
return true
}
m.fields["reply-to"] = replyTo
offset = next
}
if hasProperty(prop1, expirationProp) {
expiration, next, err := getShortString(data, offset+1, uint32(data[offset]))
if err {
logp.Warn("Failed to get expiration in header frame")
return true
}
m.fields["expiration"] = expiration
offset = next
}
if hasProperty(prop2, messageIDProp) {
messageID, next, err := getShortString(data, offset+1, uint32(data[offset]))
if err {
logp.Warn("Failed to get message id in header frame")
return true
}
m.fields["message-id"] = messageID
offset = next
}
if hasProperty(prop2, timestampProp) {
t := time.Unix(int64(binary.BigEndian.Uint64(data[offset:offset+8])), 0)
m.fields["timestamp"] = t.Format(amqpTimeLayout)
offset += 8
}
if hasProperty(prop2, typeProp) {
msgType, next, err := getShortString(data, offset+1, uint32(data[offset]))
if err {
logp.Warn("Failed to get message type in header frame")
return true
}
m.fields["type"] = msgType
offset = next
}
if hasProperty(prop2, userIDProp) {
userID, next, err := getShortString(data, offset+1, uint32(data[offset]))
if err {
logp.Warn("Failed to get user id in header frame")
return true
}
m.fields["user-id"] = userID
offset = next
}
if hasProperty(prop2, appIDProp) {
appID, _, err := getShortString(data, offset+1, uint32(data[offset]))
if err {
logp.Warn("Failed to get app-id in header frame")
return true
}
m.fields["app-id"] = appID
}
return false
}
func (amqp *amqpPlugin) handleAmqp(m *amqpMessage, tcptuple *common.TCPTuple, dir uint8) {
if amqp.mustHideCloseMethod(m) {
return
}
debugf("A message is ready to be handled")
m.tcpTuple = *tcptuple
m.direction = dir
m.cmdlineTuple = procs.ProcWatcher.FindProcessesTuple(tcptuple.IPPort())
if m.method == "basic.publish" {
amqp.handlePublishing(m)
} else if m.method == "basic.deliver" || m.method == "basic.return" ||
m.method == "basic.get-ok" {
amqp.handleDelivering(m)
} else if m.isRequest == true {
amqp.handleAmqpRequest(m)
} else if m.isRequest == false {
amqp.handleAmqpResponse(m)
}
}
func (amqp *amqpPlugin) mustHideCloseMethod(m *amqpMessage) bool {
return amqp.hideConnectionInformation == true &&
(m.method == "connection.close" || m.method == "channel.close") &&
getReplyCode(m.fields) < uint16(300)
}