-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathfastbit_element.h
417 lines (359 loc) · 10.8 KB
/
fastbit_element.h
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/**
* \file fastbit_elemetn.h
* \author Petr Kramolis <[email protected]>
* \brief object wrapers for information elements.
*
* 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.
*
*/
#ifndef FASTBIT_ELEMENT
#define FASTBIT_ELEMENT
extern "C" {
#include <ipfixcol.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
}
#include <map>
#include <iostream>
#include <string>
#include <fastbit/ibis.h>
#include "fastbit.h"
#include "config_struct.h"
#define NFv9_CONVERSION_ENTERPRISE_NUMBER (~((uint32_t) 0))
const int IE_NAME_LENGTH = 32;
const int TYPE_NAME_LENGTH = 10;
/**
* \brief Class wrapper for information elements
*/
class element
{
protected:
struct fastbit_config *_config;
uint32_t _en; /* Enterprise number */
uint16_t _id; /* Field ID */
int _size;
enum ibis::TYPE_T _type;
/* Row name for this element, consisting of enterprise number and field id
* Example: e0id16, e20id50, ...
*/
char _name[IE_NAME_LENGTH];
uint32_t _filled; /* number of items are stored in buffer */
uint32_t _buf_max; /* maximum number of items buffer can hold */
char *_buffer; /* items buffer */
/**
* \brief Get method for element size
*
* @return element size
*/
int size() { return _size; }
/**
* \brief Set element name
*
* Element names are based on enterprise ID and element ID.
* Examples: e0id5, e5id10, ...
*
* @param en Enterprise ID
* @param id Field ID
* @param part Number of part (used for IPv6)
*/
void set_name(uint32_t en, uint16_t id, int part = -1);
/**
* \brief Copy data and change byte order
*
* This method simply copy data from src to dst
* in oposite byte order.
*
* @param dst pointer to destination memory where data should be writen (the memory MUST be preallocated)
* @param src pointer to source data for byte reorder
* @param size size of memory to copy & reorder from source data
* @param offset offset for destination memory (useful when reordering int16_t to int32_t etc)
*/
void byte_reorder(uint8_t *dst, uint8_t *src, int size, int offset = 0);
/**
* \brief Allocate memory for buffer
*
* @param count Number of elements that the buffer should hold
*/
void allocate_buffer(uint32_t count);
/**
* \brief Free memory for buffer
*/
void free_buffer();
/**
* \brief Append data to the buffer
* @param data
* @return 0 on success, 1 when buffer is full
*/
int append(void *data);
public:
virtual ~element() { free_buffer(); };
/**
* \brief Fill internal element value according to given data
*
* This method transforms data from ipfix record to internal
* value usable by fastbit. Number of bytes to read is specified by _size
* variable. This method converts endianity. Data can be accesed by value pointer.
*
* @param data pointer to input data (usualy ipfix element)
* @return size of the element read from the data
*/
virtual uint16_t fill(uint8_t *data) = 0;
/**
* \brief Flush buffer content to file
*
* @param path of the file to write to
* @return 0 on success, 1 otherwise
*/
virtual int flush(std::string path);
/**
* \brief Return string with par information for -part.txt FastBit file
*
* @return string with part information
*/
virtual std::string get_part_info();
/**
* \brief Get Name of the element
*
* @return Returns name of the element
*/
const char* getName() const;
};
class el_var_size : public element
{
public:
void *data;
el_var_size(struct fastbit_config *config = NULL, int size = 0, uint32_t en = 0, uint16_t id = 0,
uint32_t buf_size = RESERVED_SPACE);
/**
* \brief Fill internal element value according to given data
*
* This method transforms data from ipfix record to internal
* value usable by fastbit. Number of bytes to read is specified by _size
* variable. This method converts endianity. Data can be accesed by value pointer.
*
* @param data pointer to input data (usualy ipfix element)
* @return 0 on succes
* @return 1 on failure
*/
virtual uint16_t fill(uint8_t *data);
protected:
int set_type();
};
typedef union float_union
{
float float32;
double float64;
} float_u;
class el_float : public element
{
public:
float_u float_value;
el_float(struct fastbit_config *config = NULL, int size = 1, uint32_t en = 0, uint16_t id = 0,
uint32_t buf_size = RESERVED_SPACE);
/**
* \brief Fill internal element value according to given data
*
* This method transforms data from ipfix record to internal
* value usable by fastbit. Number of bytes to read is specified by _size
* variable. This method converts endianity. Data can be accesed by value pointer.
*
* @param data pointer to input data (usualy ipfix element)
* @return 0 on succes
* @return 1 on failure
*/
virtual uint16_t fill(uint8_t *data);
protected:
int set_type();
};
class el_text : public element
{
private:
bool _var_size;
uint16_t _true_size;
uint8_t _offset;
char *_sp_buffer;
uint32_t _sp_buffer_size;
uint32_t _sp_buffer_offset;
public:
el_text(struct fastbit_config *config, int size = 1, uint32_t en = 0, uint16_t id = 0,
uint32_t buf_size = RESERVED_SPACE);
virtual uint16_t fill(uint8_t *data);
virtual ~el_text();
/**
* \brief Overloaded flush function to write the sp buffer.
* Calls parent funtion flush
*
* @param path to write the file to
* @return 0 on success, 1 otherwise
*/
virtual int flush(std::string path);
protected:
int set_type() {
_type = ibis::TEXT;
return 0;
}
/**
* \brief Append string to buffer
* @param data Data to append
* @param size size of data to append
* @return 0 on success, 1 otherwise
*/
int append_str(void *data, uint16_t size);
};
class el_ipv6 : public element
{
public:
uint64_t ipv6_value;
el_ipv6(struct fastbit_config *config = NULL, int size = 1, uint32_t en = 0, uint16_t id = 0, int part = 0,
uint32_t buf_size = RESERVED_SPACE);
/**
* \brief fill internal element value according to given data
*
* This method transforms data from ipfix record to internal
* value usable by fastbit. Number of bytes to read is specified by _size
* variable. This method converts endianity. Data can be accesed by value pointer.
*
* @param data pointer to input data (usualy ipfix element)
* @return 0 on succes
* @return 1 on failure
*/
virtual uint16_t fill(uint8_t *data);
protected:
int set_type();
};
class el_blob : public element
{
public:
el_blob(struct fastbit_config *config = NULL, int size = 1, uint32_t en = 0, uint16_t id = 0,
uint32_t buf_size = RESERVED_SPACE);
virtual uint16_t fill(uint8_t *data);
virtual ~el_blob();
/**
* \brief Overloaded flush function to write the sp buffer
*
* Calls parent funtion flush
*
* @param path to write the file to
* @return 0 on success, 1 otherwise
*/
virtual int flush(std::string path);
protected:
bool _var_size;
uint16_t _true_size;
uint8_t uint_value;
char *_sp_buffer;
uint32_t _sp_buffer_size;
uint32_t _sp_buffer_offset;
int set_type(){
_type = ibis::BLOB;
return 0;
}
};
typedef union uinteger_union
{
uint8_t ubyte;
uint16_t ushort;
uint32_t uint;
uint64_t ulong;
} uint_u;
class el_uint : public element
{
public:
el_uint(struct fastbit_config *config, int size = 1, uint32_t en = 0, uint16_t id = 0,
uint32_t buf_size = RESERVED_SPACE);
/**
* \brief fill internal element value according to given data
*
* This method transforms data from ipfix record to internal
* value usable by fastbit. Number of bytes to read is specified by _size
* variable. This method converts endianity. Data can be accesed by value pointer.
*
* @param data pointer to input data (usualy ipfix element)
* @return 0 on succes
* @return 1 on failure
*/
virtual uint16_t fill(uint8_t *data);
protected:
uint_u uint_value;
uint16_t _real_size; /* Element size that was sent in template; may differ from storage _size */
int set_type();
};
class el_sint : public el_uint
{
public:
el_sint(struct fastbit_config *config, int size = 1, uint32_t en = 0, uint16_t id = 0,
uint32_t buf_size = RESERVED_SPACE);
protected:
int set_type();
};
class el_unknown : public element
{
protected:
bool _var_size;
/**
* \brief Append data to the buffer
* @param data
* @return 0 on success, 1 when buffer is full
*/
int append(void *data);
public:
el_unknown(struct fastbit_config *config, int size = 0, uint32_t en = 0, uint16_t id = 0, int part = 0,
uint32_t buf_size = 0);
/**
* \brief fill internal element value according to given data
*
* This method transforms data from ipfix record to internal
* value usable by fastbit. Number of bytes to read is specified by _size
* variable. This method converts endianity. Data can be accesed by value pointer.
*
* @param data pointer to input data (usualy ipfix element)
* @return size of the element read from the data
*/
virtual uint16_t fill(uint8_t *data);
/**
* \brief Flush buffer content to file
*
* @param path of the file to write to
* @return 0 on success, 1 otherwise
*/
virtual int flush(std::string path);
/**
* \brief Return string with par information for -part.txt FastBit file
*
* @return string with part information
*/
std::string get_part_info();
};
#endif