Skip to content

Commit

Permalink
IPFIX input: initialial version IPFIX File reader
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas955 committed Apr 24, 2020
1 parent c9c2ed1 commit 7715585
Show file tree
Hide file tree
Showing 7 changed files with 590 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/plugins/input/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# List of input plugins to build and install
add_subdirectory(dummy)
add_subdirectory(ipfix)
add_subdirectory(tcp)
add_subdirectory(udp)
28 changes: 28 additions & 0 deletions src/plugins/input/ipfix/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Create a linkable module
add_library(ipfix-input MODULE
ipfix.c
config.c
config.h
)

install(
TARGETS ipfix-input
LIBRARY DESTINATION "${INSTALL_DIR_LIB}/ipfixcol2/"
)

if (ENABLE_DOC_MANPAGE)
# Build a manual page
set(SRC_FILE "${CMAKE_CURRENT_SOURCE_DIR}/doc/ipfixcol2-ipfix-input.7.rst")
set(DST_FILE "${CMAKE_CURRENT_BINARY_DIR}/ipfixcol2-ipfix-input.7")

add_custom_command(TARGET ipfix-input PRE_BUILD
COMMAND ${RST2MAN_EXECUTABLE} --syntax-highlight=none ${SRC_FILE} ${DST_FILE}
DEPENDS ${SRC_FILE}
VERBATIM
)

install(
FILES "${DST_FILE}"
DESTINATION "${INSTALL_DIR_MAN}/man7"
)
endif()
23 changes: 23 additions & 0 deletions src/plugins/input/ipfix/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
IPFIX file (input plugin)
=========================

TODO

Example configuration
---------------------

.. code-block:: xml
<input>
<name>IPFIX input</name>
<plugin>ipfix</plugin>
<params>
<dirPath>4739</dirPath>
<fileName></fileName>
</params>
</input>
Parameters
----------

TODO
158 changes: 158 additions & 0 deletions src/plugins/input/ipfix/config.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/**
* \file src/plugins/input/tcp/config.c
* \author Lukas Hutak <[email protected]>
* \brief Configuration parser of TCP input plugin (source file)
* \date 2018
*/

/* Copyright (C) 2018 CESNET, z.s.p.o.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of the Company nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* ALTERNATIVELY, provided that this notice is retained in full, this
* product may be distributed under the terms of the GNU General Public
* License (GPL) version 2 or later, in which case the provisions
* of the GPL apply INSTEAD OF those given above.
*
* This software is provided ``as is'', and any express or implied
* warranties, including, but not limited to, the implied warranties of
* merchantability and fitness for a particular purpose are disclaimed.
* In no event shall the company or contributors be liable for any
* direct, indirect, incidental, special, exemplary, or consequential
* damages (including, but not limited to, procurement of substitute
* goods or services; loss of use, data, or profits; or business
* interruption) however caused and on any theory of liability, whether
* in contract, strict liability, or tort (including negligence or
* otherwise) arising in any way out of the use of this software, even
* if advised of the possibility of such damage.
*
*/

#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include "config.h"

/*
* <params>
* <path>...</path> // required, exactly once
* </params>
*/

/** XML nodes */
enum params_xml_nodes {
NODE_PATH = 1
};

/** Definition of the \<params\> node */
static const struct fds_xml_args args_params[] = {
FDS_OPTS_ROOT("params"),
FDS_OPTS_ELEM(NODE_PATH, "path", FDS_OPTS_T_STRING, 0),
FDS_OPTS_END
};

/**
* \brief Process \<params\> node
* \param[in] ctx Plugin context
* \param[in] root XML context to process
* \param[in] cfg Parsed configuration
* \return #IPX_OK on success
* \return #IPX_ERR_FORMAT in case of failure
*/
static int
config_parser_root(ipx_ctx_t *ctx, fds_xml_ctx_t *root, struct ipfix_config *cfg)
{
const struct fds_xml_cont *content;
while (fds_xml_next(root, &content) != FDS_EOC) {
switch (content->id) {
case NODE_PATH:
// File(s) path
assert(content->type == FDS_OPTS_T_STRING);
cfg->path = strdup(content->ptr_string);
break;
default:
// Internal error
assert(false);
}
}

if (!cfg->path) {
IPX_CTX_ERROR(ctx, "Memory allocation error (%s:%d)", __FILE__, __LINE__);
}

return IPX_OK;
}

/**
* \brief Set default parameters of the configuration
* \param[in] cfg Configuration
*/
static void
config_default_set(struct ipfix_config *cfg)
{
cfg->path = NULL;
}

struct ipfix_config *
config_parse(ipx_ctx_t *ctx, const char *params)
{
struct ipfix_config *cfg = calloc(1, sizeof(*cfg));
if (!cfg) {
IPX_CTX_ERROR(ctx, "Memory allocation error (%s:%d)", __FILE__, __LINE__);
return NULL;
}

// Set default parameters
config_default_set(cfg);

// Create an XML parser
fds_xml_t *parser = fds_xml_create();
if (!parser) {
IPX_CTX_ERROR(ctx, "Memory allocation error (%s:%d)", __FILE__, __LINE__);
config_destroy(cfg);
return NULL;
}

if (fds_xml_set_args(parser, args_params) != IPX_OK) {
IPX_CTX_ERROR(ctx, "Failed to parse the description of an XML document!", '\0');
fds_xml_destroy(parser);
config_destroy(cfg);
return NULL;
}

fds_xml_ctx_t *params_ctx = fds_xml_parse_mem(parser, params, true);
if (params_ctx == NULL) {
IPX_CTX_ERROR(ctx, "Failed to parse the configuration: %s", fds_xml_last_err(parser));
fds_xml_destroy(parser);
config_destroy(cfg);
return NULL;
}

// Parse parameters
int rc = config_parser_root(ctx, params_ctx, cfg);
fds_xml_destroy(parser);
if (rc != IPX_OK) {
config_destroy(cfg);
return NULL;
}

return cfg;
}

void
config_destroy(struct ipfix_config *cfg)
{
free(cfg->path);
free(cfg);
}
72 changes: 72 additions & 0 deletions src/plugins/input/ipfix/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* \file src/plugins/input/tcp/config.c
* \author Lukas Hutak <[email protected]>
* \brief Configuration parser of TCP input plugin (source file)
* \date 2018
*/

/* Copyright (C) 2018 CESNET, z.s.p.o.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of the Company nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* ALTERNATIVELY, provided that this notice is retained in full, this
* product may be distributed under the terms of the GNU General Public
* License (GPL) version 2 or later, in which case the provisions
* of the GPL apply INSTEAD OF those given above.
*
* This software is provided ``as is'', and any express or implied
* warranties, including, but not limited to, the implied warranties of
* merchantability and fitness for a particular purpose are disclaimed.
* In no event shall the company or contributors be liable for any
* direct, indirect, incidental, special, exemplary, or consequential
* damages (including, but not limited to, procurement of substitute
* goods or services; loss of use, data, or profits; or business
* interruption) however caused and on any theory of liability, whether
* in contract, strict liability, or tort (including negligence or
* otherwise) arising in any way out of the use of this software, even
* if advised of the possibility of such damage.
*
*/

#ifndef CONFIG_H
#define CONFIG_H

#include <ipfixcol2.h>
#include "stdint.h"


/** Configuration of a instance of the IPFIX plugin */
struct ipfix_config {
/** Local port */
const char *path;
};

/**
* @brief Parse configuration of the plugin
* @param[in] ctx Instance context
* @param[in] params XML parameters
* @return Pointer to the parse configuration of the instance on success
* @return NULL if arguments are not valid or if a memory allocation error has occurred
*/
struct ipfix_config *
config_parse(ipx_ctx_t *ctx, const char *params);

/**
* @brief Destroy parsed configuration
* @param[in] cfg Parsed configuration
*/
void
config_destroy(struct ipfix_config *cfg);

#endif // CONFIG_H
20 changes: 20 additions & 0 deletions src/plugins/input/ipfix/doc/ipfixcol2-ipfix-input.7.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
=======================
ipfixcol2-ipfix-input
=======================

-------------------------
IPFIX File (input plugin)
-------------------------

:Author: Lukas Hutak ([email protected])
:Date: 2020-04-23
:Copyright: Copyright © 2020 CESNET, z.s.p.o.
:Version: 2.0
:Manual section: 7
:Manual group: IPFIXcol collector

Description
-----------

.. include:: ../README.rst
:start-line: 3
Loading

0 comments on commit 7715585

Please sign in to comment.