forked from CESNET/ipfixcol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Translator.cpp
354 lines (317 loc) · 9 KB
/
Translator.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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/**
* \file Translator.cpp
* \author Michal Kozubik <[email protected]>
* \brief Translator for JSON storage plugin
*
* Copyright (C) 2015 CESNET, z.s.p.o.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of the Company nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* ALTERNATIVELY, provided that this notice is retained in full, this
* product may be distributed under the terms of the GNU General Public
* License (GPL) version 2 or later, in which case the provisions
* of the GPL apply INSTEAD OF those given above.
*
* This software is provided ``as is, and any express or implied
* warranties, including, but not limited to, the implied warranties of
* merchantability and fitness for a particular purpose are disclaimed.
* In no event shall the company or contributors be liable for any
* direct, indirect, incidental, special, exemplary, or consequential
* damages (including, but not limited to, procurement of substitute
* goods or services; loss of use, data, or profits; or business
* interruption) however caused and on any theory of liability, whether
* in contract, strict liability, or tort (including negligence or
* otherwise) arising in any way out of the use of this software, even
* if advised of the possibility of such damage.
*
*/
#include "Translator.h"
#include "protocols.h"
#include <arpa/inet.h>
#include <vector>
#include "Storage.h"
// #include "itostr.h"
#include "branchlut2.h"
/**
* \brief Format flags 16bits
*/
const char *Translator::formatFlags16(uint16_t flags)
{
flags = ntohs(flags);
return formatFlags8((uint8_t) flags);
}
/**
* \brief Format flags 8bits
*/
const char *Translator::formatFlags8(uint8_t flags)
{
buffer[0] = '"';
buffer[1] = flags & 0x20 ? 'U' : '.';
buffer[2] = flags & 0x10 ? 'A' : '.';
buffer[3] = flags & 0x08 ? 'P' : '.';
buffer[4] = flags & 0x04 ? 'R' : '.';
buffer[5] = flags & 0x02 ? 'S' : '.';
buffer[6] = flags & 0x01 ? 'F' : '.';
buffer[7] = '"';
buffer[8] = '\0';
return buffer;
}
/**
* \brief Constructor
*/
Translator::Translator()
{
buffer = new char[Translator::BUFF_SIZE];
}
Translator::~Translator()
{
delete[] buffer;
}
/**
* \brief Format IPv4
*/
const char *Translator::formatIPv4(uint32_t addr, uint16_t *ret_len)
{
char *ret = NULL;
ret = u32toa_branchlut2(((uint8_t *) &addr)[0], buffer);
ret++[0] = '.';
ret = u32toa_branchlut2(((uint8_t *) &addr)[1], ret);
ret++[0] = '.';
ret = u32toa_branchlut2(((uint8_t *) &addr)[2], ret);
ret++[0] = '.';
ret = u32toa_branchlut2(((uint8_t *) &addr)[3], ret);
*ret_len = ret - buffer;
return buffer;
}
/**
* \brief Format IPv6
*/
const char *Translator::formatIPv6(uint8_t* addr)
{
inet_ntop(AF_INET6, (struct in6_addr *) addr, buffer, INET6_ADDRSTRLEN);
return buffer;
}
/**
* \brief Format timestamp
*/
const char *Translator::formatMac(uint8_t* addr)
{
snprintf(buffer, BUFF_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x",
addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
return buffer;
}
/**
* \brief Format protocol
*/
const char *Translator::formatProtocol(uint8_t proto)
{
snprintf(buffer, BUFF_SIZE, "\"%s\"", protocols[proto]);
return buffer;
}
/**
* \brief Format timestamp
*/
const char *Translator::formatTimestamp(uint64_t tstamp, t_units units, struct json_conf * config)
{
if(config->timestamp) {
tstamp = be64toh(tstamp);
/* Convert to milliseconds */
switch (units) {
case t_units::SEC:
tstamp *= 1000;
break;
case t_units::MICROSEC:
tstamp /= 1000;
break;
case t_units::NANOSEC:
tstamp /= 1000000;
break;
default: /* MILLI is default */
break;
}
timesec = tstamp / 1000;
msec = tstamp % 1000;
tm = localtime(×ec);
strftime(buffer, 21, "\"%FT%T", tm);
/* append miliseconds */
sprintf(&(buffer[20]), ".%03u\"", (const unsigned int) msec);
} else {
tstamp = be64toh(tstamp);
sprintf(buffer, "%" PRIu64 , tstamp);
}
return buffer;
}
/**
* \brief Conversion of unsigned int
*/
const char *Translator::toUnsigned(uint16_t length, uint16_t *ret_len, uint8_t *data_record,
uint16_t offset, const ipfix_element_t * element, struct json_conf * config)
{
// Calculate the length of returned string here to avoid unnecessary strlen later
const char *ret = NULL;
*ret_len = 0;
if(length == BYTE1) {
// 1 byte
if(element->en == 0 && element->id == 6 && config->tcpFlags) {
// Formated TCP flags
ret = formatFlags8(read8(data_record + offset));
*ret_len = 8;
return ret;
} else if (element->en == 0 && element->id == 4 && !config->protocol) {
// Formated protocol identification (TCP, UDP, ICMP,...)
ret = (formatProtocol(read8(data_record + offset)));
*ret_len = strlen(ret);
return ret;
} else {
// Other elements
ret = u32toa_branchlut2(read8(data_record + offset), buffer);
}
} else if(length == BYTE2) {
// 2 bytes
if (element->en == 0 && element->id == 6 && config->tcpFlags) {
// Formated TCP flags
ret = formatFlags16(read16(data_record + offset));
*ret_len = 8;
return ret;
} else {
// Other elements
ret = u32toa_branchlut2(ntohs(read16(data_record + offset)), buffer);
}
} else if(length == BYTE4) {
// 4 bytes
ret = u32toa_branchlut2(ntohl(read32(data_record + offset)), buffer);
} else if(length == BYTE8) {
// 8 bytes
ret = u64toa_branchlut2(be64toh(read64(data_record + offset)), buffer);
} else {
// Other sizes
*ret_len = snprintf(buffer, BUFF_SIZE, "%s", "\"unknown\"");
return buffer;
}
*ret_len = ret - buffer;
return buffer;
}
/**
* \brief Conversion of signed int
*/
const char *Translator::toSigned(uint16_t length, uint16_t *ret_len, uint8_t *data_record, uint16_t offset)
{
// Calculate the length of returned string here to avoid unnecessary strlen later
const char *ret = NULL;
*ret_len = 0;
if(length == BYTE1) {
// 1 byte
ret = i32toa_branchlut2(read8(data_record + offset), buffer);
} else if(length == BYTE2) {
// 2 bytes
ret = i32toa_branchlut2(ntohs(read16(data_record + offset)), buffer);
} else if(length == BYTE4) {
// 4 bytes
ret = i32toa_branchlut2(ntohl(read32(data_record + offset)), buffer);
} else if(length == BYTE8) {
// 8 bytes
ret = i64toa_branchlut2(be64toh(read64(data_record + offset)), buffer);
} else {
*ret_len = snprintf(buffer, BUFF_SIZE, "\"unknown\"");
return buffer;
}
*ret_len = ret - buffer;
return buffer;
}
/**
* \brief Conversion of float
*/
const char *Translator::toFloat(uint16_t length, uint16_t *ret_len, uint8_t *data_record, uint16_t offset)
{
if(length == BYTE4)
*ret_len = snprintf(buffer, BUFF_SIZE, "%f", (float) ntohl(read32(data_record + offset)));
else if(length == BYTE8)
*ret_len = snprintf(buffer, BUFF_SIZE, "%lf", (double) be64toh(read64(data_record + offset)));
else
*ret_len = snprintf(buffer, BUFF_SIZE, "\"unknown\"");
return buffer;
}
/**
* \brief Convert string to JSON format
*/
const char *Translator::escapeString(uint16_t length, const uint8_t *field,
const json_conf *config)
{
uint32_t idx_output = 0;
#define ESCAPE_CHAR(ch) { \
buffer[idx_output++] = '\\'; \
buffer[idx_output++] = ch; \
}
// Beginning of the string
buffer[idx_output++] = '"';
for (uint32_t i = 0; i < length; ++i) {
// All characters from the extended part of ASCII must be escaped
if (field[i] > 0x7F) {
snprintf(&buffer[idx_output], 7, "\\u00%02x", field[i]);
idx_output += 6;
continue;
}
/*
* Based on RFC 4627 (Section: 2.5. Strings):
* Control characters (i.e. 0x00 - 0x1F), '"' and '\' must be escaped
* using "\"", "\\" or "\uXXXX" where "XXXX" is a hexa value.
*/
if (field[i] > 0x1F && field[i] != '"' && field[i] != '\\') {
// Copy to the output buffer
buffer[idx_output++] = field[i];
continue;
}
// Copy as escaped character
switch(field[i]) {
case '\\': // Reverse solidus
ESCAPE_CHAR('\\');
continue;
case '\"': // Quotation
ESCAPE_CHAR('\"');
continue;
default:
break;
}
if (config->whiteSpaces == false) {
// Skip white space characters
continue;
}
switch(field[i]) {
case '\t': // Tabulator
ESCAPE_CHAR('t');
break;
case '\n': // New line
ESCAPE_CHAR('n');
break;
case '\b': // Backspace
ESCAPE_CHAR('b');
break;
case '\f': // Form feed
ESCAPE_CHAR('f');
break;
case '\r': // Return
ESCAPE_CHAR('r');
break;
default: // "\uXXXX"
snprintf(&buffer[idx_output], 7, "\\u00%02x", field[i]);
idx_output += 6;
break;
}
}
#undef ESCAPE_CHAR
// End of the string
buffer[idx_output++] = '"';
buffer[idx_output] = '\0';
return buffer;
}