Skip to content

Commit 488edf8

Browse files
committed
Unirec output plugin: improved processing of an UniRec template in configuration parser
1 parent 3b6878e commit 488edf8

File tree

2 files changed

+69
-5
lines changed

2 files changed

+69
-5
lines changed

extra_plugins/output/unirec/configuration.c

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include <unistd.h>
5454
#include <stdarg.h>
5555
#include <limits.h>
56+
#include <ctype.h>
5657

5758
/** Timeout configuration */
5859
enum cfg_timeout_mode {
@@ -294,6 +295,63 @@ cfg_str2long(const char *str, long int *res)
294295
return IPX_OK;
295296
}
296297

298+
/**
299+
* \brief Remove all whitespace characters from the UniRec template specification
300+
* \note The returned string must be freed by user when no longer needed
301+
* \param[in] raw Pointer to the string to sanitize
302+
* \return New sanitized string or NULL (memory allocation error)
303+
*/
304+
static char *
305+
cfg_str_sanitize(const char *raw)
306+
{
307+
char *res = strdup(raw);
308+
if (!res) {
309+
return NULL;
310+
}
311+
312+
size_t w_idx;
313+
for (w_idx = 0; (*raw) != '\0'; ++raw) {
314+
if (isspace((int) *raw)) {
315+
continue;
316+
}
317+
res[w_idx++] = *raw;
318+
}
319+
320+
res[w_idx] = '\0';
321+
return res;
322+
}
323+
324+
/**
325+
* \brief Sanitize UniRec template
326+
*
327+
* The function removes all whitespace characters and question marks
328+
* \note The returned string must be freed by user when no longer needed
329+
* \param[in] raw Template string to sanitize
330+
* \return New sanitized string or NULL (memory allocation error)
331+
*/
332+
static char *
333+
cfg_ur_tmplt_sanitize(const char *raw)
334+
{
335+
// Remove all whitespaces
336+
char *res = cfg_str_sanitize(raw);
337+
if (!res) {
338+
return NULL;
339+
}
340+
341+
// Remove questions marks
342+
char *r_ptr, *w_ptr;
343+
for (r_ptr = w_ptr = res; (*r_ptr) != '\0'; r_ptr++) {
344+
if (*r_ptr == '?') {
345+
continue;
346+
}
347+
348+
*(w_ptr++) = *r_ptr;
349+
}
350+
351+
*w_ptr = '\0';
352+
return res;
353+
}
354+
297355
/**
298356
* \brief Process \<trapIfcCommon\> node
299357
*
@@ -729,8 +787,9 @@ cfg_parse_params(ipx_ctx_t *ctx, fds_xml_ctx_t *root, struct conf_params *cfg)
729787
case NODE_UNIREC_FMT:
730788
// UniRec output format
731789
assert(content->type == FDS_OPTS_T_STRING);
732-
cfg->unirec_format = strdup(content->ptr_string);
733-
if(cfg->unirec_format == NULL) {
790+
cfg->unirec_fmt = cfg_ur_tmplt_sanitize(content->ptr_string);
791+
cfg->unirec_spec = cfg_str_sanitize(content->ptr_string);
792+
if (cfg->unirec_fmt == NULL || cfg->unirec_spec == NULL) {
734793
IPX_CTX_ERROR(ctx, "Unable to allocate memory (%s:%d)", __FILE__, __LINE__);
735794
return IPX_ERR_NOMEM;
736795
}
@@ -780,7 +839,8 @@ cfg_validate(ipx_ctx_t *ctx, const struct conf_params *cfg)
780839
rc = IPX_ERR_FORMAT;
781840
}
782841

783-
if (!cfg->unirec_format || strlen(cfg->unirec_format) == 0) {
842+
if (!cfg->unirec_fmt || strlen(cfg->unirec_fmt) == 0
843+
|| !cfg->unirec_spec || strlen(cfg->unirec_spec) == 0) {
784844
IPX_CTX_ERROR(ctx, "UniRec template is not specified!", '\0');
785845
rc = IPX_ERR_FORMAT;
786846
}
@@ -848,6 +908,7 @@ configuration_free(struct conf_params *cfg)
848908
}
849909

850910
free(cfg->trap_ifc_spec);
851-
free(cfg->unirec_format);
911+
free(cfg->unirec_fmt);
912+
free(cfg->unirec_spec);
852913
free(cfg);
853914
}

extra_plugins/output/unirec/configuration.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,11 @@ struct conf_params {
5656
* Elements marked with '?' are optional and might not be filled (e.g. TCP_FLAGS)
5757
* For example, "DST_IP,SRC_IP,BYTES,DST_PORT,?TCP_FLAGS,SRC_PORT,PROTOCOL".
5858
* All fields must be contained in unirec-elements.txt
59+
* \note All whitespaces has been removed
5960
*/
60-
char *unirec_format;
61+
char *unirec_spec;
62+
/** The same as \ref conf_params.unirec_spec, however, question marks has been removed */
63+
char *unirec_fmt;
6164
};
6265

6366
/**

0 commit comments

Comments
 (0)