forked from CESNET/ipfixcol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStorage.cpp
384 lines (333 loc) · 10.3 KB
/
Storage.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/**
* \file Storage.cpp
* \author Michal Kozubik <[email protected]>
* \brief Class 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.
*
*/
/**
* @todo We are not sure if conversion for type BOOLEAN is in little or big endian -> how to translate it to json. It is necessary to solve it.
*/
extern "C" {
#include <ipfixcol.h>
#include <ipfixcol/profiles.h>
#include <string.h>
//#include <ipfix_element.h>
}
#include "Storage.h"
#include <stdexcept>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <map>
static const char *msg_module = "json_storage";
#define READ_BYTE_ARR(_dst_, _src_, _len_) \
do {\
for (int i = 0; i < (_len_); ++i) { \
(_dst_)[i] = read8((_src_) + i); \
} \
} while(0)
/**
* \brief Constructor
*/
Storage::Storage()
{
/* Allocate space for buffers */
record.reserve(4096);
buffer.reserve(BUFF_SIZE);
}
Storage::~Storage()
{
for (Output *output: outputs) {
delete output;
}
}
/**
* \brief Send data record
*/
void Storage::sendData() const
{
for (Output *output: outputs) {
output->ProcessDataRecord(record);
}
}
/**
* \brief Store data sets
*/
void Storage::storeDataSets(const ipfix_message* ipfix_msg, struct json_conf * config)
{
/* Iterate through all data records */
for (int i = 0; i < ipfix_msg->data_records_count; ++i) {
storeDataRecord(&(ipfix_msg->metadata[i]), config);
}
}
/**
* \brief Get real field length
*/
uint16_t Storage::realLength(uint16_t length, uint8_t *data_record, uint16_t &offset) const
{
/* Static length */
if (length != VAR_IE_LENGTH) {
return length;
}
/* Variable length */
length = static_cast<int>(read8(data_record + offset));
offset++;
if (length == 255) {
length = ntohs(read16(data_record + offset));
offset += 2;
}
return length;
}
/**
* \brief Read string field
*/
void Storage::readString(uint16_t& length, uint8_t *data_record, uint16_t &offset)
{
/* Get string length */
length = realLength(length, data_record, offset);
/* Read string and replace white spaces by \notation (\n, \t, ...) */
unsigned long int index = 0;
unsigned long int index2 = 0;
const char * pointer = (const char *)(data_record + offset);
stringWithEscseq[index2++] = '"';
for(index = 0; index != length; index++) {
switch(pointer[index]) {
case TABULATOR: stringWithEscseq[index2++] = '\\'; stringWithEscseq[index2++] = 't'; break;
case NEWLINE: stringWithEscseq[index2++] = '\\'; stringWithEscseq[index2++] = 'n'; break;
case QUOTATION: stringWithEscseq[index2++] = '\\'; stringWithEscseq[index2++] = '\"'; break;
case REVERSESOLIDUS: stringWithEscseq[index2++] = '\\'; stringWithEscseq[index2++] = '\\'; break;
case SOLIDUS: stringWithEscseq[index2++] = '\\'; stringWithEscseq[index2++] = '/'; break;
case BACKSPACE: stringWithEscseq[index2++] = '\\'; stringWithEscseq[index2++] = 'b'; break;
case FORMFEED: stringWithEscseq[index2++] = '\\'; stringWithEscseq[index2++] = 'f'; break;
case RETURN: stringWithEscseq[index2++] = '\\'; stringWithEscseq[index2++] = 'r'; break;
default: stringWithEscseq[index2++] = pointer[index]; break;
}
}
stringWithEscseq[index2++] = '"';
stringWithEscseq[index2] = '\0';
record.append((const char *) stringWithEscseq);
}
/**
* \brief Read raw data from record
*/
void Storage::readRawData(uint16_t &length, uint8_t* data_record, uint16_t &offset)
{
record += '"';
/* Read raw value */
switch (length) {
case 1:
sprintf(buffer.data(), "%" PRIu16, static_cast<int>(read8(data_record + offset)));
break;
case 2:
sprintf(buffer.data(), "%" PRIu16, ntohs(read16(data_record + offset)));
break;
case 4:
sprintf(buffer.data(), "%" PRIu32, ntohl(read32(data_record + offset)));
break;
case 8:
sprintf(buffer.data(), "%" PRIu64, be64toh(read64(data_record + offset)));
break;
default:
length = this->realLength(length, data_record, offset);
if (length * 2 > buffer.capacity()) {
buffer.reserve(length * 2 + 1);
}
for (int i = 0; i < length; i++) {
sprintf(buffer.data() + i * 2, "%02x", (data_record + offset)[i]);
}
record += "0x";
}
record += buffer.data();
record += '"';
}
/**
* \brief Create raw name for unknown elements
*/
std::string Storage::rawName(uint32_t en, uint16_t id) const
{
std::ostringstream ss;
ss << "e" << en << "id" << id;
return ss.str();
}
/**
* \brief Store data record
*/
void Storage::storeDataRecord(struct metadata *mdata, struct json_conf * config)
{
std::string tmp_name;
offset = 0;
record.clear();
record += "{\"@type\": \"ipfix.entry\", ";
struct ipfix_template *templ = mdata->record.templ;
uint8_t *data_record = (uint8_t*) mdata->record.record;
/* get all fields */
uint16_t added = 0;
for (uint16_t count = 0, index = 0; count < templ->field_count; ++count, ++index) {
/* Get Enterprise number and ID */
id = templ->fields[index].ie.id;
length = templ->fields[index].ie.length;
enterprise = 0;
if (id & 0x8000) {
id &= 0x7fff;
enterprise = templ->fields[++index].enterprise_number;
}
/* Get element informations */
const ipfix_element_t * element = get_element_by_id(id, enterprise);
if (element == NULL) {
// Element not found
if (config->ignoreUnknown) {
offset += length;
continue;
}
tmp_name = rawName(enterprise, id);
MSG_DEBUG(msg_module, "Unknown element (%s)", tmp_name.c_str());
}
if (added > 0) {
record += ", ";
}
record += "\"ipfix.";
record += (element != NULL) ? element->name : tmp_name;
record += "\": ";
switch ((element != NULL) ? element->type : ET_UNASSIGNED) {
case ET_UNSIGNED_8:
case ET_UNSIGNED_16:
case ET_UNSIGNED_32:
case ET_UNSIGNED_64:
record += translator.toUnsigned(&length, data_record, offset, element, config);
break;
case ET_SIGNED_8:
case ET_SIGNED_16:
case ET_SIGNED_32:
case ET_SIGNED_64:
record += translator.toSigned(&length, data_record, offset);
break;
case ET_FLOAT_32:
case ET_FLOAT_64:
record += translator.toFloat(&length, data_record, offset);
break;
case ET_IPV4_ADDRESS:
record += '"';
record += translator.formatIPv4(read32(data_record + offset));
record += '"';
break;
case ET_IPV6_ADDRESS:
READ_BYTE_ARR(addr6, data_record + offset, IPV6_LEN);
record += '"';
record += translator.formatIPv6(addr6);
record += '"';
break;
case ET_MAC_ADDRESS:
READ_BYTE_ARR(addrMac, data_record + offset, MAC_LEN);
break;
case ET_DATE_TIME_SECONDS:
record += translator.formatTimestamp(read64(data_record + offset), t_units::SEC, config);
break;
case ET_DATE_TIME_MILLISECONDS:
record += translator.formatTimestamp(read64(data_record + offset), t_units::MILLISEC, config);
break;
case ET_DATE_TIME_MICROSECONDS:
translator.formatTimestamp(read64(data_record + offset), t_units::MICROSEC, config);
break;
case ET_DATE_TIME_NANOSECONDS:
translator.formatTimestamp(read64(data_record + offset), t_units::NANOSEC, config);
break;
case ET_STRING:
readString(length, data_record, offset);
break;
case ET_BOOLEAN:
readRawData(length, data_record, offset);
break;
case ET_UNASSIGNED:
readRawData(length, data_record, offset);
break;
default:
readRawData(length, data_record, offset);
break;
}
offset += length;
added++;
}
/* Store metadata */
if (processMetadata) {
record += ", \"ipfix.metadata\": {";
storeMetadata(mdata);
record += "}";
}
record += "}\n";
sendData();
}
/**
* \brief Store metadata information
*/
void Storage::storeMetadata(metadata* mdata)
{
std::stringstream ss;
/* Geolocation info */
ss << "\"srcAS\": \"" << mdata->srcAS << "\", ";
ss << "\"dstAS\": \"" << mdata->dstAS << "\", ";
ss << "\"srcCountry\": \"" << mdata->srcCountry << "\", ";
ss << "\"dstCountry\": \"" << mdata->dstCountry << "\", ";
ss << "\"srcName\": \"" << mdata->srcName << "\", ";
ss << "\"dstName\": \"" << mdata->dstName << "\", ";
record += ss.str();
/* Profiles */
record += "\"profiles\": [";
if (mdata->channels) {
// Get name of root profile
void *profile_ptr = NULL;
void *prev_profile_ptr = NULL;
const char *root_profile_name;
profile_ptr = channel_get_profile(mdata->channels[0]);
while (profile_ptr != NULL) {
prev_profile_ptr = profile_ptr;
profile_ptr = profile_get_parent(profile_ptr);
}
root_profile_name = profile_get_name(prev_profile_ptr);
// Process all channels
for (int i = 0; mdata->channels[i] != 0; ++i) {
if (i > 0) {
record += ", ";
}
record += "{\"profile\": \"";
record += root_profile_name;
record += "/";
record += profile_get_path(channel_get_profile(mdata->channels[i]));
record += "\", \"channel\": \"";
record += channel_get_name(mdata->channels[i]);
record += "\"}";
}
}
record += "]";
}