Skip to content

Commit 4c2b2da

Browse files
AdamZvarasedmicha
authored andcommitted
Message builder: initial version
1 parent 667e999 commit 4c2b2da

File tree

5 files changed

+608
-0
lines changed

5 files changed

+608
-0
lines changed

include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ set(SUB_HEADERS
1414
ipfixcol2/message_garbage.h
1515
ipfixcol2/message_ipfix.h
1616
ipfixcol2/message_session.h
17+
ipfixcol2/message_builder.h
1718
ipfixcol2/modifier.h
1819
ipfixcol2/plugins.h
1920
ipfixcol2/session.h

include/ipfixcol2.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#include <ipfixcol2/message_garbage.h>
6161
#include <ipfixcol2/message_session.h>
6262
#include <ipfixcol2/message_ipfix.h>
63+
#include <ipfixcol2/message_builder.h>
6364

6465
#include <ipfixcol2/modifier.h>
6566
#include <ipfixcol2/plugins.h>
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/**
2+
* \file message_builder.h
3+
* \author Adam Zvara <[email protected]>
4+
* \brief Message builder (header file)
5+
* \date 2023
6+
*/
7+
8+
#ifndef IPX_MESSAGE_BUILDER_H
9+
#define IPX_MESSAGE_BUILDER_H
10+
11+
#include <ipfixcol2/api.h>
12+
13+
/** Internal declaration of message builder */
14+
typedef struct ipx_msg_builder ipx_msg_builder_t;
15+
16+
/**
17+
* \brief Create new message builder
18+
*
19+
* \return Pointer to new message builder, otherwise NULL (memory allocation error)
20+
*/
21+
IPX_API ipx_msg_builder_t *
22+
ipx_msg_builder_create();
23+
24+
/**
25+
* \brief Destroy message builder
26+
*
27+
* \warning This function does NOT free message pointer allocated in builder.
28+
* Raw message is usually freed by calling ipx_msg_ipfix_destroy() on created
29+
* wrapper or user can call ipx_msg_builder_free_raw() BEFORE destroying builder.
30+
*
31+
* \param[in] builder Message builder
32+
*/
33+
IPX_API void
34+
ipx_msg_builder_destroy(ipx_msg_builder_t *builder);
35+
36+
/**
37+
* \brief Free memory allocated for raw message in builder
38+
*
39+
* \param[in] builder Message builder
40+
*/
41+
IPX_API void
42+
ipx_msg_builder_free_raw(ipx_msg_builder_t *builder);
43+
44+
/**
45+
* \brief Get current maximum length of IPFIX message in builder
46+
*
47+
* \param[in] builder Message builder
48+
* \return Maximum allowed size of IPFIX message in builder or 0 (builder is NULL)
49+
*/
50+
IPX_API size_t
51+
ipx_msg_builder_get_maxlength(const ipx_msg_builder_t *builder);
52+
53+
/**
54+
* \brief Set maximum length of new IPFIX message in builder
55+
*
56+
* \param[in] builder Message builder
57+
* \param[in] new_length New maximum length of IPFIX message
58+
*/
59+
IPX_API void
60+
ipx_msg_builder_set_maxlength(ipx_msg_builder_t *builder, const size_t new_length);
61+
62+
/**
63+
* \brief Start creating new IPFIX message
64+
*
65+
* This function allocates memory for new raw message and copies original message
66+
* header into it. It MUST be called before trying to add sets/records into
67+
* message builder.
68+
*
69+
* \note If hints are not given (e.g hints == 0), maxbytes is used as
70+
* allocation size for raw message
71+
*
72+
* \param[in] builder Pointer to message builder
73+
* \param[in] hdr Original raw message header
74+
* \param[in] maxbytes Maximum length of new message (without IPFIX header)
75+
* \param[in] hints Allocation size of new raw message
76+
* \return #IPX_OK on success
77+
* \return #IPX_ERR_ARG on invalid argument (NULL pointers or maxbytes < IPFIX header length)
78+
* \return #IPX_ERR_NOMEM on failed memory allocation
79+
*/
80+
IPX_API int
81+
ipx_msg_builder_start(ipx_msg_builder_t *builder, const struct fds_ipfix_msg_hdr *hdr,
82+
const uint32_t maxbytes, const uint32_t hints);
83+
84+
/**
85+
* \brief Add new set to IPFIX message
86+
*
87+
* \warning When message length exceeds maxbytes, ipx_msg_builder_end needs to be called
88+
* to create wrapper around message!
89+
*
90+
* \param[in] builder Pointer to builder
91+
* \param[in] id ID of new set
92+
* \return #IPX_OK on success
93+
* \return #IPX_ERR_DENIED when message length would exceed maxbytes
94+
* \return #IPX_ERR_NOMEM on memory allocation error
95+
*/
96+
IPX_API int
97+
ipx_msg_builder_add_set(struct ipx_msg_builder *builder, const uint16_t id);
98+
99+
/**
100+
* \brief Add new data record to IPFIX message
101+
*
102+
* \warning When message length exceeds maxbytes, ipx_msg_builder_end needs to be called
103+
* to create wrapper around message!
104+
* \note New set is added automatically, there is no need to call add_set before calling
105+
* this function
106+
*
107+
* \param[in] builder Pointer to message builder
108+
* \param[in] record Pointer to data record
109+
* \return #IPX_OK on success
110+
* \return #IPX_ERR_DENIED when message length would exceed maxbytes
111+
* \return #IPX_ERR_NOMEM on memory allocation error
112+
*/
113+
IPX_API int
114+
ipx_msg_builder_add_drec(ipx_msg_builder_t *builder, const struct fds_drec *record);
115+
116+
/**
117+
* \brief Create wrapper around raw IPFIX message in message builder
118+
*
119+
* \param[in] builder Pointer to message builder
120+
* \param[in] plugin_ctx Plugin context
121+
* \param[in] msg_ctx Message context (from original IPFIX message)
122+
* \return Pointer to allocated IPFIX wrapper or NULL
123+
*/
124+
IPX_API ipx_msg_ipfix_t *
125+
ipx_msg_builder_end(const ipx_msg_builder_t *builder, const ipx_ctx_t *plugin_ctx,
126+
const struct ipx_msg_ctx *msg_ctx);
127+
128+
#endif

src/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ set(CORE_SOURCE
4444
message_session.c
4545
message_terminate.c
4646
message_terminate.h
47+
message_builder.c
4748
modifier.c
4849
modifier.h
4950
odid_range.c

0 commit comments

Comments
 (0)