-
Notifications
You must be signed in to change notification settings - Fork 0
/
gelfserver.cpp
149 lines (136 loc) · 4.82 KB
/
gelfserver.cpp
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
#include "gelfserver.h"
#include <QJsonDocument>
#include <QUdpSocket>
#include <QDebug>
#include <zlib.h>
#define GZIP_CHUNK_SIZE 32 * 1024
GELFServer::GELFServer(QObject *parent) : QObject(parent), udp_socket(new QUdpSocket(this))
{
udp_socket->bind(QHostAddress::LocalHost, 12201);
connect(udp_socket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
}
void GELFServer::readPendingDatagrams()
{
while (udp_socket->hasPendingDatagrams()) {
QByteArray datagram, data;
QHostAddress sender;
quint16 senderPort;
// Recieve datagram
datagram.resize(udp_socket->pendingDatagramSize());
udp_socket->readDatagram(
datagram.data(),
datagram.size(),
&sender,
&senderPort
);
// check type
if (datagram[0] == (char)0x1e && datagram[1] == (char)0x0f) {
qWarning() << this
<< "Datagram from" << sender
<< "port" << senderPort
<< "Chunked messages is not yet supported.";
continue;
}
// Decompress payload
if (!decompress(datagram, data)) {
qWarning() << this
<< "Datagram from" << sender
<< "port" << senderPort
<< "Failed to decompress data" << data;
continue;
}
// Parse message json
QJsonDocument message_document = QJsonDocument::fromJson(data);
if (!message_document.isObject()) {
qWarning() << this << "Inavlid GELF message: root is not object";
return;
}
// Validate GELF structure
QJsonObject message_object = message_document.object();
QStringList keys = message_object.keys();
if (
!keys.contains("version")
|| !keys.contains("host")
|| !keys.contains("short_message")
|| !keys.contains("full_message")
|| !keys.contains("timestamp")
|| !keys.contains("level")
) {
qWarning() << this
<< "Ivalid GELF"
<< "from" << sender
<< "port" << senderPort
<< "keys" << keys;
}
// emit message
emit message(message_object);
}
}
bool GELFServer::decompress(QByteArray &input, QByteArray &output)
{
if (input.length() > 0) {
// Prepare inflater status
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
// Initialize inflater
int ret = inflateInit2(&strm, 32 + MAX_WBITS);
if (ret != Z_OK)
return false;
// Extract pointer to input data
char *input_data = input.data();
int input_data_left = input.length();
// Decompress data until available
do {
// Determine current chunk size
int chunk_size = qMin(GZIP_CHUNK_SIZE, input_data_left);
// Check for termination
if (chunk_size <= 0)
break;
// Set inflater references
strm.next_in = (unsigned char *)input_data;
strm.avail_in = chunk_size;
// Update interval variables
input_data += chunk_size;
input_data_left -= chunk_size;
// Inflate chunk and cumulate output
do {
// Declare vars
char out[GZIP_CHUNK_SIZE];
// Set inflater references
strm.next_out = (unsigned char *)out;
strm.avail_out = GZIP_CHUNK_SIZE;
// Try to inflate chunk
ret = inflate(&strm, Z_NO_FLUSH);
switch (ret) {
case Z_NEED_DICT:
qWarning() << this << "Z_NEED_DICT";
ret = Z_DATA_ERROR;
case Z_DATA_ERROR:
qWarning() << this << "Z_DATA_ERROR";
case Z_MEM_ERROR:
qWarning() << this << "Z_MEM_ERROR";
case Z_STREAM_ERROR:
qWarning() << this << "Z_STREAM_ERROR";
// Clean-up
inflateEnd(&strm);
// Return
return false;
}
// Determine decompressed size
int have = (GZIP_CHUNK_SIZE - strm.avail_out);
// Cumulate result
if (have > 0) output.append((char *)out, have);
} while (strm.avail_out == 0);
} while (ret != Z_STREAM_END);
// Clean-up
inflateEnd(&strm);
// Return
return (ret == Z_STREAM_END);
} else {
return true;
}
}