|
53 | 53 | #include <unistd.h> |
54 | 54 | #include <stdarg.h> |
55 | 55 | #include <limits.h> |
| 56 | +#include <ctype.h> |
56 | 57 |
|
57 | 58 | /** Timeout configuration */ |
58 | 59 | enum cfg_timeout_mode { |
@@ -294,6 +295,63 @@ cfg_str2long(const char *str, long int *res) |
294 | 295 | return IPX_OK; |
295 | 296 | } |
296 | 297 |
|
| 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 | + |
297 | 355 | /** |
298 | 356 | * \brief Process \<trapIfcCommon\> node |
299 | 357 | * |
@@ -729,8 +787,9 @@ cfg_parse_params(ipx_ctx_t *ctx, fds_xml_ctx_t *root, struct conf_params *cfg) |
729 | 787 | case NODE_UNIREC_FMT: |
730 | 788 | // UniRec output format |
731 | 789 | 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) { |
734 | 793 | IPX_CTX_ERROR(ctx, "Unable to allocate memory (%s:%d)", __FILE__, __LINE__); |
735 | 794 | return IPX_ERR_NOMEM; |
736 | 795 | } |
@@ -780,7 +839,8 @@ cfg_validate(ipx_ctx_t *ctx, const struct conf_params *cfg) |
780 | 839 | rc = IPX_ERR_FORMAT; |
781 | 840 | } |
782 | 841 |
|
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) { |
784 | 844 | IPX_CTX_ERROR(ctx, "UniRec template is not specified!", '\0'); |
785 | 845 | rc = IPX_ERR_FORMAT; |
786 | 846 | } |
@@ -848,6 +908,7 @@ configuration_free(struct conf_params *cfg) |
848 | 908 | } |
849 | 909 |
|
850 | 910 | free(cfg->trap_ifc_spec); |
851 | | - free(cfg->unirec_format); |
| 911 | + free(cfg->unirec_fmt); |
| 912 | + free(cfg->unirec_spec); |
852 | 913 | free(cfg); |
853 | 914 | } |
0 commit comments