Skip to content

Commit 0ec65cf

Browse files
committed
UniRec output: added "string_trimmed" type for string trimming during conversion
1 parent 411a4aa commit 0ec65cf

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

extra_plugins/output/unirec/src/map.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
#define DEF_SIZE 32
5353
/** Size of error buffer */
5454
#define ERR_SIZE 256
55+
/** Special type of UniRec string field that is trimmed before conversion from IPFIX */
56+
#define TYPE_STRING_TRIM "string_trimmed"
5557

5658
/** Internal structure of the mapping database */
5759
struct map_s {
@@ -234,13 +236,14 @@ map_elem_get_internal(const char *elem)
234236
* \param[in] ur_name UniRec name
235237
* \param[in] ur_type UniRec type
236238
* \param[in] ur_type_str UniRec type string
239+
* \param[in] cflags Additional conversion flags
237240
* \param[in] ie_defs Definition of IPFIX IEs (comma separated list of definitions)
238241
* \param[in] line_id Line ID (just for error messages)
239242
* \return #IPX_OK on success
240243
* \return #IPX_ERR_NOMEM or #IPX_ERR_FORMAT on failure
241244
*/
242245
static int
243-
map_load_line_ie_defs(map_t *map, char *ur_name, int ur_type, char *ur_type_str,
246+
map_load_line_ie_defs(map_t *map, char *ur_name, int ur_type, char *ur_type_str, uint32_t cflags,
244247
const char *ie_defs, size_t line_id)
245248
{
246249
char *defs_cpy = strdup(ie_defs);
@@ -264,6 +267,7 @@ map_load_line_ie_defs(map_t *map, char *ur_name, int ur_type, char *ur_type_str,
264267
rec.unirec.name = ur_name;
265268
rec.unirec.type = ur_type;
266269
rec.unirec.type_str = ur_type_str;
270+
rec.unirec.flags = cflags;
267271

268272
// Process IPFIX fields
269273
char *subsave_ptr = NULL;
@@ -323,6 +327,7 @@ map_load_line(map_t *map, const char *line, size_t line_id)
323327
int rc = IPX_OK;
324328
char *ur_name = NULL;
325329
char *ur_type_str = NULL;
330+
uint32_t conv_flags = 0;
326331
ur_field_type_t ur_type;
327332

328333
char *line_cpy = strdup(line);
@@ -356,7 +361,15 @@ map_load_line(map_t *map, const char *line, size_t line_id)
356361
goto end;
357362
}
358363

359-
int type = ur_get_field_type_from_str(token);
364+
int type;
365+
if (strcmp(token, TYPE_STRING_TRIM) == 0) {
366+
// Special version of string
367+
type = UR_TYPE_STRING;
368+
conv_flags |= MAP_FLAGS_STR_TRIM;
369+
} else {
370+
type = ur_get_field_type_from_str(token);
371+
}
372+
360373
if (type == UR_E_INVALID_TYPE) {
361374
snprintf(map->err_buffer, ERR_SIZE, "Line %zu: Invalid type '%s' of UniRec field '%s'",
362375
line_id, token, ur_name);
@@ -382,7 +395,7 @@ map_load_line(map_t *map, const char *line, size_t line_id)
382395

383396
ptrdiff_t offset = token - line_cpy;
384397
const char *ie_defs = line + offset;
385-
rc = map_load_line_ie_defs(map, ur_name, ur_type, ur_type_str, ie_defs, line_id);
398+
rc = map_load_line_ie_defs(map, ur_name, ur_type, ur_type_str, conv_flags, ie_defs, line_id);
386399

387400
end:
388401
free(ur_type_str);

extra_plugins/output/unirec/src/map.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ enum MAP_SRC {
6161
MAP_SRC_INTERNAL_DBF
6262
};
6363

64+
enum MAP_FLAGS {
65+
/** Perform trim of an IPFIX string (convert only characters before the first '\0') */
66+
MAP_FLAGS_STR_TRIM = (1 << 0)
67+
};
68+
6469
/** IPFIX-to-UniRec mapping record */
6570
struct map_rec {
6671
struct {
@@ -85,6 +90,11 @@ struct map_rec {
8590
ur_field_type_t type;
8691
/** Data type (string, for log!) */
8792
char *type_str;
93+
/**
94+
* \brief Additional conversion flags
95+
* \note See ::MAP_FLAGS
96+
*/
97+
uint32_t flags;
8898
} unirec;
8999
};
90100

extra_plugins/output/unirec/src/translator.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,22 @@ translate_bytes(translator_t *trans, const struct translator_rec *rec,
316316
return 0;
317317
}
318318

319+
/**
320+
* \brief Convert IPFIX string to trimmed UniRec string
321+
*
322+
* The function will copy only characters up to the first occurrence of '\0' (excluding).
323+
* \copydetails translate_uint()
324+
*/
325+
static int
326+
translator_string_trim(translator_t *trans, const struct translator_rec *rec,
327+
const struct fds_drec_field *field)
328+
{
329+
ur_field_id_t ur_id = rec->unirec.id;
330+
size_t copy_len = strnlen((const char *) field->data, field->size);
331+
ur_set_var(trans->record.ur_tmplt, trans->record.data, ur_id, field->data, copy_len);
332+
return 0;
333+
}
334+
319335
/**
320336
* \brief Convert IPFIX boolean to UniRec char/(un)signed integer
321337
* \copydetails translate_uint()
@@ -699,6 +715,11 @@ translator_get_func(ipx_ctx_t *ctx, const struct map_rec *rec)
699715

700716
switch (type_ur) {
701717
case UR_TYPE_STRING:
718+
// String array
719+
if (type_ipx == FDS_ET_STRING && (rec->unirec.flags & MAP_FLAGS_STR_TRIM) != 0) {
720+
return translator_string_trim;
721+
}
722+
// Fall through
702723
case UR_TYPE_BYTES:
703724
// String/byte array
704725
if (type_ipx == FDS_ET_STRING || type_ipx == FDS_ET_OCTET_ARRAY) {

extra_plugins/output/unirec/src/unirecplugin.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ IPX_API struct ipx_plugin_info ipx_plugin_info = {
7878
// Configuration flags (reserved for future use)
7979
.flags = 0,
8080
// Plugin version string (like "1.2.3")
81-
.version = "2.0.0",
81+
.version = "2.1.0",
8282
// Minimal IPFIXcol version string (like "1.2.3")
8383
.ipx_min = "2.0.0"
8484
};

0 commit comments

Comments
 (0)