Skip to content

Commit 4430f30

Browse files
jaroslavhcejkato2
authored andcommitted
unirec: translate, configure can now compile
- copied more files from previous iteration of ipfix
1 parent 173c7e8 commit 4430f30

File tree

3 files changed

+146
-16
lines changed

3 files changed

+146
-16
lines changed

extra_plugins/output/unirec/configuration.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ enum params_xml_nodes {
7878
/** Definition of the \<params\> node */
7979
static const struct fds_xml_args args_params[] = {
8080
FDS_OPTS_ROOT("params"),
81-
FDS_OPTS_ELEM(NODE_TRAP_IFC_TYPE, "trapIfcType", FDS_OPTS_T_STRING, 0),
81+
FDS_OPTS_ELEM(NODE_TRAP_IFC_TYPE, "trapIfcType", FDS_OPTS_T_INT, 0),
8282
FDS_OPTS_ELEM(NODE_TRAP_IFC_SOCKET, "trapIfcSocket", FDS_OPTS_T_STRING, 0),
8383
FDS_OPTS_ELEM(NODE_TRAP_IFC_TIMEOUT, "trapIfcTimeout", FDS_OPTS_T_STRING, 0),
8484
FDS_OPTS_ELEM(NODE_TRAP_IFC_FLUSH_TIMEOUT, "trapIfcFlushTimeout", FDS_OPTS_T_STRING, 0),
8585
FDS_OPTS_ELEM(NODE_TRAP_IFC_BUFFER_SWITCH, "trapIfcBufferSwitch", FDS_OPTS_T_STRING, 0),
86-
FDS_OPTS_ELEM(NODE_UNIREC FORMAT, "UniRecFormat", FDS_OPTS_T_STRING, 0),
86+
FDS_OPTS_ELEM(NODE_UNIREC_FORMAT, "UniRecFormat", FDS_OPTS_T_STRING, 0),
8787
FDS_OPTS_END
8888
};
8989

@@ -98,7 +98,7 @@ configuration_validate(ipx_ctx_t *ctx, const struct conf_params *cfg)
9898
{
9999
int ret_code = IPX_OK;
100100

101-
if (!cfg.trap_ifc_type) {
101+
if (!cfg->trap_ifc_type) {
102102
IPX_CTX_ERROR(ctx, "Trap interface type is not set.", '\0');
103103
ret_code = IPX_ERR_FORMAT;
104104
}
@@ -129,8 +129,8 @@ configuration_parse_root(ipx_ctx_t *ctx, fds_xml_ctx_t *root, struct conf_params
129129
while(fds_xml_next(root, &content) != FDS_EOC) {
130130
switch (content->id) {
131131
case NODE_TRAP_IFC_TYPE:
132-
assert(content->type == FDS_OPTS_T_STRING);
133-
cnf->trap_ifc_type = content->ptr_string;
132+
assert(content->type == FDS_OPTS_T_INT);
133+
cnf->trap_ifc_type = content->val_int;
134134
break;
135135
case NODE_TRAP_IFC_SOCKET:
136136
assert(content->type == FDS_OPTS_T_STRING);
@@ -164,7 +164,7 @@ configuration_parse_root(ipx_ctx_t *ctx, fds_xml_ctx_t *root, struct conf_params
164164
return IPX_ERR_NOMEM;
165165
}
166166
break;
167-
case NODE_TRAP_IFC_UNIREC_FORMAT:
167+
case NODE_UNIREC_FORMAT:
168168
assert(content->type == FDS_OPTS_T_STRING);
169169
cnf->unirec_format = strdup(content->ptr_string);
170170
if(cnf->unirec_format == NULL) {
@@ -263,9 +263,9 @@ configuration_free(struct conf_params *config)
263263
}
264264

265265
if (config->unirec_format) {
266-
free(c->unirec_format);
267-
c->unirec_format = NULL;
266+
free(config->unirec_format);
267+
config->unirec_format = NULL;
268268
}
269269

270-
free(c);
270+
free(config);
271271
}

extra_plugins/output/unirec/translator.c

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,92 @@ struct translator_table_rec
102102
translator_func func;
103103
};
104104

105+
//***************************** COPIED FROM OLD PLUGIN *****************************
106+
// Path to unirec elements config file
107+
const char *UNIREC_ELEMENTS_FILE = "unirec-elements.txt"; //TODO is this where it should be located
108+
109+
// Possible UniRec data types
110+
const char *unirec_data_types_str[] = {
111+
"string", /*UR_TYPE_STRING*/
112+
"bytes", /*UR_TYPE_BYTES*/
113+
"char", /*UR_TYPE_CHAR*/
114+
"uint8", /*UR_TYPE_UINT8*/
115+
"int8", /*UR_TYPE_INT8*/
116+
"uint16", /*UR_TYPE_UINT16*/
117+
"int16", /*UR_TYPE_INT16*/
118+
"uint32", /*UR_TYPE_UINT32*/
119+
"int32", /*UR_TYPE_INT32*/
120+
"uint64", /*UR_TYPE_UINT64*/
121+
"int64", /*UR_TYPE_INT64*/
122+
"float", /*UR_TYPE_FLOAT*/
123+
"double", /*UR_TYPE_DOUBLE*/
124+
"ipaddr", /*UR_TYPE_IP*/
125+
"time", /*UR_TYPE_TIME*/
126+
};
127+
128+
/**
129+
* \brief Creates IPFIX id from string
130+
*
131+
* @param ipfixToken String in eXXidYY format, where XX is enterprise number and YY is element ID
132+
* @return Returns id that is used to compare field against IPFIX template
133+
*/
134+
static ipfixElement ipfix_from_string(char *ipfixToken)
135+
{
136+
ipfixElement element;
137+
char *endptr;
138+
139+
element.en = strtol(ipfixToken + 1, &endptr, 10);
140+
element.id = strtol(endptr + 2, (char **) NULL, 10);
141+
142+
return element;
143+
}
144+
145+
static int8_t checkUnirecType(const char *type)
146+
{
147+
int i;
148+
for (i = 0; i < UNIREC_DATA_TYPES_COUNT; i++) {
149+
if (strcmp(type, unirec_data_types_str[i]) == 0) {
150+
return i;
151+
}
152+
}
153+
return -1;
154+
}
155+
156+
/**
157+
* \brief Convert ipfix element id to unirec type for faster processing ipfix messages
158+
* \param ipfix_el Ipfix element structure.
159+
* \return One value from enum `unirecFieldEnum`.
160+
*/
161+
static int8_t getUnirecFieldTypeFromIpfixId(ipfixElement ipfix_el)
162+
{
163+
uint16_t id = ipfix_el.id;
164+
uint32_t en = ipfix_el.en;
165+
166+
if ((en == 0 && (id == 8 || id == 12)) ||
167+
(en == 39499 && id== 40) ||
168+
(en == 0 && (id == 27 || id == 28)) ||
169+
(en == 39499 && id == 41)) {
170+
// IP or INVEA_SIP_RTP_IP
171+
return UNIREC_FIELD_IP;
172+
} else if (en == 0 && id == 2) {
173+
// Packets
174+
return UNIREC_FIELD_PACKET;
175+
} else if (en == 0 && (id == 152 || id == 153)) {
176+
// Timestamps
177+
return UNIREC_FIELD_TS;
178+
} else if (en == 0 && id == 10) {
179+
// DIR_BIT_FIELD
180+
return UNIREC_FIELD_DBF;
181+
} else if (en == 0 && id == 405) {
182+
// LINK_BIT_FIELD
183+
return UNIREC_FIELD_LBF;
184+
} else {
185+
// Other
186+
return UNIREC_FIELD_OTHER;
187+
}
188+
}
189+
190+
105191
/**
106192
* \brief Loads all available elements from configuration file
107193
* @return List of UniRec elements on success, NULL otherwise
@@ -118,14 +204,14 @@ static unirecField *load_elements()
118204
/* Open the file */
119205
uef = fopen(UNIREC_ELEMENTS_FILE, "r");
120206
if (uef == NULL) {
121-
IPX_CTX_ERROR(ctx, "Unable to open: %s", UNIREC_ELEMENTS_FILE);
207+
//TODO unable to open message
122208
return NULL;
123209
}
124210

125211
/* Init buffer */
126212
line = malloc(lineSize);
127213
if (line == NULL ){
128-
IPX_CTX_ERROR(ctx, "Unable to allocate memory for buffer.");
214+
//TODO memory allocation message
129215
fclose(uef);
130216
return NULL;
131217
}
@@ -144,7 +230,7 @@ static unirecField *load_elements()
144230
/* Create new element structure, make space for ipfixElCount ipfix elements and NULL */
145231
currentField = malloc(sizeof(unirecField));
146232
if (currentField == NULL ){
147-
IPX_CTX_ERROR(ctx, "Unable to allocate memory for field.");
233+
//TODO memory allocation message
148234
fclose(uef);
149235
free(line);
150236
return NULL;
@@ -165,7 +251,7 @@ static unirecField *load_elements()
165251
case 1:
166252
currentField->unirec_type = checkUnirecType(token);
167253
if (currentField->unirec_type < 0) {
168-
MSG_ERROR(msg_module, "Unknown UniRec data type \"%s\" of field \"%s\"", token, currentField->name);
254+
//MSG_ERROR(msg_module, "Unknown UniRec data type \"%s\" of field \"%s\"", token, currentField->name);
169255
fclose(uef);
170256
free(line);
171257
return NULL;
@@ -215,6 +301,7 @@ static unirecField *load_elements()
215301

216302
return fields;
217303
}
304+
//********************** END OF COPIED FROM OLD PLUGIN *****************************
218305

219306
/**
220307
* \brief Global translator table

extra_plugins/output/unirec/translator.h

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,55 @@
4343
#include <ipfixcol2.h>
4444
#include <unirec/unirec.h>
4545

46-
// Path to unirec elements config file
47-
const char *UNIREC_ELEMENTS_FILE = "unirec-elements.txt"; //TODO is this where it should be located
48-
4946
/** Internal definition of translator */
5047
typedef struct translator_s translator_t;
5148

49+
//***************************** COPIED FROM OLD PLUGIN *****************************
50+
51+
#define UNIREC_DATA_TYPES_COUNT 15 ///< Count of UniRec data types
52+
53+
enum unirecFieldEnum {
54+
UNIREC_FIELD_OTHER,
55+
UNIREC_FIELD_IP,
56+
UNIREC_FIELD_PACKET,
57+
UNIREC_FIELD_TS,
58+
UNIREC_FIELD_DBF,
59+
UNIREC_FIELD_LBF
60+
};
61+
62+
/** struct for ipfix elements' 2 ids */
63+
typedef struct ipfixElement {
64+
uint16_t id;
65+
uint32_t en;
66+
} ipfixElement;
67+
68+
/** linked list of UnirecFields */
69+
typedef struct unirecField {
70+
char *name;
71+
int ur_id;
72+
int8_t type; /**< Used for faster processing, possible value are in `unirefFieldEnum` */
73+
int8_t unirec_type; /**< Used for generating data format string */
74+
int8_t size;
75+
int8_t required;
76+
int8_t *required_ar; /**< Array of interfaces numbers where this element is required */
77+
int8_t *included_ar; /**< Array of interfaces numbers where this element is included */
78+
uint16_t *offset_ar;
79+
struct unirecField *next;
80+
struct unirecField *nextIfc;
81+
void *value; /**< Pointer to value of the field */
82+
uint16_t valueSize; /**< Size of the value */
83+
uint8_t valueFilled; /**< Is the value filled? */
84+
85+
86+
87+
88+
/**< Number of ipfix elements */
89+
int ipfixCount;
90+
/**< https://tools.ietf.org/html/rfc5101#section-3.2 with masked size and EN for IANA elements */
91+
ipfixElement ipfix[1];
92+
} unirecField;
93+
//***************************** END OF COPIED FROM OLD PLUGIN *****************************
94+
5295
/**
5396
* \brief Create a new instance of a translator
5497
* \return Pointer to the instance or NULL.

0 commit comments

Comments
 (0)