Skip to content

Commit

Permalink
Merge pull request #62 from CESNET/hutak-unirec-config-file
Browse files Browse the repository at this point in the history
Unirec output: introduce mappingFile parameter
  • Loading branch information
Lukas955 authored Mar 4, 2022
2 parents 6c0a579 + a929299 commit 8aa1a33
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 24 deletions.
4 changes: 4 additions & 0 deletions extra_plugins/output/unirec/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ Parameters
Specification of interface type and its parameters. For more details, see section
"Output interface types".

:``mappingFile``:
Path to configuration file with mapping IPFIX fields to UniRec fields. If the parameter is
not defined, the default configuration file is used. See section "UniRec configuration file".

Output interface types
----------------------
Exactly one of the following output type must be defined in the instance configuration of this
Expand Down
23 changes: 23 additions & 0 deletions extra_plugins/output/unirec/src/configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ enum cfg_timeout_mode {
CFG_TIMEOUT_HALF_WAIT = -3 /**< Block only if some client is connected */
};

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

/*
* <params>
* <mappingFile>/etc/ipfixcol2/unirec-elements.txt</mappingFile>
* <uniRecFormat>DST_IP,SRC_IP,BYTES,DST_PORT,?TCP_FLAGS,SRC_PORT,PROTOCOL</uniRecFormat>
* <splitBiflow>true</splitBiflow>
* <trapIfcCommon> <!-- optional -->
Expand Down Expand Up @@ -123,6 +126,7 @@ enum params_xml_nodes {
// Main parameters
NODE_UNIREC_FMT = 1,
NODE_BIFLOW_SPLIT,
NODE_MAPPING_FILE,
NODE_TRAP_COMMON,
NODE_TRAP_SPEC,
// TRAP common parameters
Expand Down Expand Up @@ -208,6 +212,7 @@ static const struct fds_xml_args args_params[] = {
FDS_OPTS_ROOT("params"),
FDS_OPTS_ELEM(NODE_UNIREC_FMT, "uniRecFormat", FDS_OPTS_T_STRING, 0),
FDS_OPTS_ELEM(NODE_BIFLOW_SPLIT, "splitBiflow", FDS_OPTS_T_BOOL, FDS_OPTS_P_OPT),
FDS_OPTS_ELEM(NODE_MAPPING_FILE, "mappingFile", FDS_OPTS_T_STRING, FDS_OPTS_P_OPT),
FDS_OPTS_NESTED(NODE_TRAP_COMMON, "trapIfcCommon", args_trap_common, FDS_OPTS_P_OPT),
FDS_OPTS_NESTED(NODE_TRAP_SPEC, "trapIfcSpec", args_trap_spec, 0),
FDS_OPTS_END
Expand Down Expand Up @@ -781,6 +786,13 @@ cfg_parse_params(ipx_ctx_t *ctx, fds_xml_ctx_t *root, struct conf_params *cfg)

// Set default values
cfg->biflow_split = true;
cfg->mapping_file = NULL;

rc = cfg_str_append(&cfg->mapping_file, "%s/%s", ipx_api_cfg_dir(), DEF_CONF_FILENAME);
if (rc != FDS_OK) {
IPX_CTX_ERROR(ctx, "Unable to allocate memory (%s:%d)", __FILE__, __LINE__);
return rc;
}

// Set default TRAP common parameters
struct ifc_common common;
Expand All @@ -807,6 +819,16 @@ cfg_parse_params(ipx_ctx_t *ctx, fds_xml_ctx_t *root, struct conf_params *cfg)
assert(content->type == FDS_OPTS_T_BOOL);
cfg->biflow_split = content->val_bool;
break;
case NODE_MAPPING_FILE:
// Mapping file
assert(content->type == FDS_OPTS_T_STRING);
free(cfg->mapping_file);
cfg->mapping_file = strdup(content->ptr_string);
if (cfg->mapping_file == NULL) {
IPX_CTX_ERROR(ctx, "Unable to allocate memory (%s:%d)", __FILE__, __LINE__);
return IPX_ERR_NOMEM;
}
break;
case NODE_TRAP_SPEC:
// TRAP output interface specifier
assert(content->type == FDS_OPTS_T_CONTEXT);
Expand Down Expand Up @@ -920,6 +942,7 @@ configuration_free(struct conf_params *cfg)
return;
}

free(cfg->mapping_file);
free(cfg->trap_ifc_spec);
free(cfg->unirec_fmt);
free(cfg->unirec_spec);
Expand Down
4 changes: 3 additions & 1 deletion extra_plugins/output/unirec/src/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@
* \brief Structure for a configuration parsed from XML
*/
struct conf_params {
/** Prepared TRAP interface specification string */
/** Path to IPFIX-to-UniRec mapping file */
char *mapping_file;
/** Prepared TRAP interface specification string */
char *trap_ifc_spec;
/**
* TRAP interface UniRec template
Expand Down
29 changes: 6 additions & 23 deletions extra_plugins/output/unirec/src/unirecplugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@
#include "configuration.h"
#include "map.h"

/** Filename of IPFIX-to-UniRec */
#define CONF_FILENAME "unirec-elements.txt"
/** Name of TRAP context that belongs to the plugin */
#define PLUGIN_TRAP_NAME "IPFIXcol2-UniRec"
/** Description of the TRAP context that belongs to the plugin */
Expand Down Expand Up @@ -100,41 +98,26 @@ struct conf_unirec {
/**
* \brief Get the IPFIX-to-UniRec conversion database
* \param ctx Plugin context
* \param file Path to a file with IPFIX-to-UniRec mapping
* \return Conversion table or NULL (an error has occurred)
*/
static map_t *
ipfix2unirec_db(ipx_ctx_t *ctx)
ipfix2unirec_db(ipx_ctx_t *ctx, const char *file)
{
const char *path = ipx_api_cfg_dir();
const size_t full_size = strlen(path) + strlen(CONF_FILENAME) + 2; // 2 = '/' + '\0'
char *full_path = malloc(full_size * sizeof(char));
if (!full_path) {
IPX_CTX_ERROR(ctx, "Unable to allocate memory (%s:%d)", __FILE__, __LINE__);
return NULL;
}

int ret_val = snprintf(full_path, full_size, "%s/%s", path, CONF_FILENAME);
if (ret_val < 0 || ((size_t) ret_val) >= full_size) {
IPX_CTX_ERROR(ctx, "Failed to generate a configuration path (internal error)", '\0');
free(full_path);
return NULL;
}

map_t *map = map_init(ipx_ctx_iemgr_get(ctx));
if (!map) {
IPX_CTX_ERROR(ctx, "Failed to initialize conversion map! (%s:%d)", __FILE__, __LINE__);
free(full_path);
return NULL;
}

if (map_load(map, full_path) != IPX_OK) {
IPX_CTX_INFO(ctx, "Loading IPFIX-to-UniRec mapping file '%s'", file);

if (map_load(map, file) != IPX_OK) {
IPX_CTX_ERROR(ctx, "Failed to initialize conversion database: %s", map_last_error(map));
map_destroy(map);
free(full_path);
return NULL;
}

free(full_path);
return map;
}

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

// Load IPFIX-to-UniRec conversion database
map_t *conv_db = ipfix2unirec_db(ctx);
map_t *conv_db = ipfix2unirec_db(ctx, parsed_params->mapping_file);
if (!conv_db) {
configuration_free(parsed_params);
free(conf);
Expand Down

0 comments on commit 8aa1a33

Please sign in to comment.