Skip to content

Commit a929299

Browse files
committed
Unirec output: introduce mappingFile parameter
1 parent 6c0a579 commit a929299

File tree

4 files changed

+36
-24
lines changed

4 files changed

+36
-24
lines changed

extra_plugins/output/unirec/README.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ Parameters
123123
Specification of interface type and its parameters. For more details, see section
124124
"Output interface types".
125125

126+
:``mappingFile``:
127+
Path to configuration file with mapping IPFIX fields to UniRec fields. If the parameter is
128+
not defined, the default configuration file is used. See section "UniRec configuration file".
129+
126130
Output interface types
127131
----------------------
128132
Exactly one of the following output type must be defined in the instance configuration of this

extra_plugins/output/unirec/src/configuration.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ enum cfg_timeout_mode {
6363
CFG_TIMEOUT_HALF_WAIT = -3 /**< Block only if some client is connected */
6464
};
6565

66+
/** Filename of IPFIX-to-UniRec */
67+
#define DEF_CONF_FILENAME "unirec-elements.txt"
6668
/** Default maximum number of connections over TCP/TCP-TLS/Unix */
6769
#define DEF_MAX_CONNECTIONS 64
6870
/** Default output interface timeout */
@@ -84,6 +86,7 @@ struct ifc_common {
8486

8587
/*
8688
* <params>
89+
* <mappingFile>/etc/ipfixcol2/unirec-elements.txt</mappingFile>
8790
* <uniRecFormat>DST_IP,SRC_IP,BYTES,DST_PORT,?TCP_FLAGS,SRC_PORT,PROTOCOL</uniRecFormat>
8891
* <splitBiflow>true</splitBiflow>
8992
* <trapIfcCommon> <!-- optional -->
@@ -123,6 +126,7 @@ enum params_xml_nodes {
123126
// Main parameters
124127
NODE_UNIREC_FMT = 1,
125128
NODE_BIFLOW_SPLIT,
129+
NODE_MAPPING_FILE,
126130
NODE_TRAP_COMMON,
127131
NODE_TRAP_SPEC,
128132
// TRAP common parameters
@@ -208,6 +212,7 @@ static const struct fds_xml_args args_params[] = {
208212
FDS_OPTS_ROOT("params"),
209213
FDS_OPTS_ELEM(NODE_UNIREC_FMT, "uniRecFormat", FDS_OPTS_T_STRING, 0),
210214
FDS_OPTS_ELEM(NODE_BIFLOW_SPLIT, "splitBiflow", FDS_OPTS_T_BOOL, FDS_OPTS_P_OPT),
215+
FDS_OPTS_ELEM(NODE_MAPPING_FILE, "mappingFile", FDS_OPTS_T_STRING, FDS_OPTS_P_OPT),
211216
FDS_OPTS_NESTED(NODE_TRAP_COMMON, "trapIfcCommon", args_trap_common, FDS_OPTS_P_OPT),
212217
FDS_OPTS_NESTED(NODE_TRAP_SPEC, "trapIfcSpec", args_trap_spec, 0),
213218
FDS_OPTS_END
@@ -781,6 +786,13 @@ cfg_parse_params(ipx_ctx_t *ctx, fds_xml_ctx_t *root, struct conf_params *cfg)
781786

782787
// Set default values
783788
cfg->biflow_split = true;
789+
cfg->mapping_file = NULL;
790+
791+
rc = cfg_str_append(&cfg->mapping_file, "%s/%s", ipx_api_cfg_dir(), DEF_CONF_FILENAME);
792+
if (rc != FDS_OK) {
793+
IPX_CTX_ERROR(ctx, "Unable to allocate memory (%s:%d)", __FILE__, __LINE__);
794+
return rc;
795+
}
784796

785797
// Set default TRAP common parameters
786798
struct ifc_common common;
@@ -807,6 +819,16 @@ cfg_parse_params(ipx_ctx_t *ctx, fds_xml_ctx_t *root, struct conf_params *cfg)
807819
assert(content->type == FDS_OPTS_T_BOOL);
808820
cfg->biflow_split = content->val_bool;
809821
break;
822+
case NODE_MAPPING_FILE:
823+
// Mapping file
824+
assert(content->type == FDS_OPTS_T_STRING);
825+
free(cfg->mapping_file);
826+
cfg->mapping_file = strdup(content->ptr_string);
827+
if (cfg->mapping_file == NULL) {
828+
IPX_CTX_ERROR(ctx, "Unable to allocate memory (%s:%d)", __FILE__, __LINE__);
829+
return IPX_ERR_NOMEM;
830+
}
831+
break;
810832
case NODE_TRAP_SPEC:
811833
// TRAP output interface specifier
812834
assert(content->type == FDS_OPTS_T_CONTEXT);
@@ -920,6 +942,7 @@ configuration_free(struct conf_params *cfg)
920942
return;
921943
}
922944

945+
free(cfg->mapping_file);
923946
free(cfg->trap_ifc_spec);
924947
free(cfg->unirec_fmt);
925948
free(cfg->unirec_spec);

extra_plugins/output/unirec/src/configuration.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@
4646
* \brief Structure for a configuration parsed from XML
4747
*/
4848
struct conf_params {
49-
/** Prepared TRAP interface specification string */
49+
/** Path to IPFIX-to-UniRec mapping file */
50+
char *mapping_file;
51+
/** Prepared TRAP interface specification string */
5052
char *trap_ifc_spec;
5153
/**
5254
* TRAP interface UniRec template

extra_plugins/output/unirec/src/unirecplugin.c

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@
4848
#include "configuration.h"
4949
#include "map.h"
5050

51-
/** Filename of IPFIX-to-UniRec */
52-
#define CONF_FILENAME "unirec-elements.txt"
5351
/** Name of TRAP context that belongs to the plugin */
5452
#define PLUGIN_TRAP_NAME "IPFIXcol2-UniRec"
5553
/** Description of the TRAP context that belongs to the plugin */
@@ -100,41 +98,26 @@ struct conf_unirec {
10098
/**
10199
* \brief Get the IPFIX-to-UniRec conversion database
102100
* \param ctx Plugin context
101+
* \param file Path to a file with IPFIX-to-UniRec mapping
103102
* \return Conversion table or NULL (an error has occurred)
104103
*/
105104
static map_t *
106-
ipfix2unirec_db(ipx_ctx_t *ctx)
105+
ipfix2unirec_db(ipx_ctx_t *ctx, const char *file)
107106
{
108-
const char *path = ipx_api_cfg_dir();
109-
const size_t full_size = strlen(path) + strlen(CONF_FILENAME) + 2; // 2 = '/' + '\0'
110-
char *full_path = malloc(full_size * sizeof(char));
111-
if (!full_path) {
112-
IPX_CTX_ERROR(ctx, "Unable to allocate memory (%s:%d)", __FILE__, __LINE__);
113-
return NULL;
114-
}
115-
116-
int ret_val = snprintf(full_path, full_size, "%s/%s", path, CONF_FILENAME);
117-
if (ret_val < 0 || ((size_t) ret_val) >= full_size) {
118-
IPX_CTX_ERROR(ctx, "Failed to generate a configuration path (internal error)", '\0');
119-
free(full_path);
120-
return NULL;
121-
}
122-
123107
map_t *map = map_init(ipx_ctx_iemgr_get(ctx));
124108
if (!map) {
125109
IPX_CTX_ERROR(ctx, "Failed to initialize conversion map! (%s:%d)", __FILE__, __LINE__);
126-
free(full_path);
127110
return NULL;
128111
}
129112

130-
if (map_load(map, full_path) != IPX_OK) {
113+
IPX_CTX_INFO(ctx, "Loading IPFIX-to-UniRec mapping file '%s'", file);
114+
115+
if (map_load(map, file) != IPX_OK) {
131116
IPX_CTX_ERROR(ctx, "Failed to initialize conversion database: %s", map_last_error(map));
132117
map_destroy(map);
133-
free(full_path);
134118
return NULL;
135119
}
136120

137-
free(full_path);
138121
return map;
139122
}
140123

@@ -317,7 +300,7 @@ ipx_plugin_init(ipx_ctx_t *ctx, const char *params)
317300
conf->params = parsed_params;
318301

319302
// Load IPFIX-to-UniRec conversion database
320-
map_t *conv_db = ipfix2unirec_db(ctx);
303+
map_t *conv_db = ipfix2unirec_db(ctx, parsed_params->mapping_file);
321304
if (!conv_db) {
322305
configuration_free(parsed_params);
323306
free(conf);

0 commit comments

Comments
 (0)