Skip to content

Commit 01bf5c5

Browse files
AdamZvarasedmicha
authored andcommitted
Modifier: update tests, rewrite header file
Move functions for modification of messages directly into header file. Fix modifier tests acordingly
1 parent b23d701 commit 01bf5c5

File tree

5 files changed

+158
-128
lines changed

5 files changed

+158
-128
lines changed

include/ipfixcol2/modifier.h

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -160,41 +160,62 @@ IPX_API int
160160
ipx_modifier_set_iemgr(ipx_modifier_t *mod, struct fds_iemgr *iemgr);
161161

162162
/**
163-
* \brief Filter data from parsed IPFIX data record and modify records template
163+
* \brief Remove fields from data record based on filter
164164
*
165-
* Modified record data is replaced in place and new template is created. Original
166-
* template in record is replaced with new template.
165+
* Filter is an array of integers for each field in record template. For each
166+
* non-zero value in filter, data field at same position is removed from message.
167167
*
168-
* \warning This function removes snapshot from data record, which needs to be set again!
168+
* \param[in,out] rec Original record
169+
* \param[in] filter Filter array
170+
*/
171+
IPX_API void
172+
ipfix_msg_remove_drecs(struct fds_drec *rec, const uint8_t *filter);
173+
174+
/**
175+
* \brief Append new fields to data record
169176
*
170-
* \param[in,out] rec Original/parsed data record
171-
* \param[in] filter Filter array
172-
* \return #IPX_OK on success
173-
* \return #IPX_ERR_ARG if any argument is NULL
174-
* \return #IPX_ERR_NOMEM on memory allocation error
177+
* \note Variable length fields will contain 1 or 3 length prefix octets
178+
* based on their length in output buffer. For length < 255 single prefix octet
179+
* is used. Otherwise 3 octets are used.
180+
*
181+
* \param[in,out] rec Original/modified record
182+
* \param[in] fields Field definitions
183+
* \param[in] output Output buffers containing new values
184+
* \param[in] arr_cnt Size of output buffer
185+
* \return #IPX_OK on success, otherwise #IPX_ERR_NOMEM
175186
*/
176187
IPX_API int
177-
ipx_modifier_filter(struct fds_drec *rec, uint8_t *filter);
188+
ipfix_msg_add_drecs(struct fds_drec *rec,
189+
const struct ipx_modifier_field *fields,
190+
const struct ipx_modifier_output *output,
191+
const size_t arr_cnt);
178192

179193
/**
180-
* \brief Append data to parsed IPFIX data record and modify records template
194+
* \brief Remove fields from template based on given filter
181195
*
182-
* Modified record data is replaced in place and new template is created. Original
183-
* template in record is replaced with new template.
196+
* \warning Only non-option templates are accepted.
184197
*
185-
* \warning This function removes snapshot from data record, which needs to be set again!
198+
* \param[in] tmplt Original template
199+
* \param[in] filter Filter array
200+
* \return Parsed modified template or NULL (memory allocation error)
201+
*/
202+
IPX_API struct fds_template *
203+
ipfix_template_remove_fields(const struct fds_template *tmplt, const uint8_t *filter);
204+
205+
/**
206+
* \brief Append new fields to template based on given output values
186207
*
187-
* \param[in,out] rec Original/parsed data record
188-
* \param[in] fields Array of new template fields
189-
* \param[in] buffers Output buffers
190-
* \param[in] fields_cnt Number of items in fields (and buffers)
191-
* \return #IPX_OK on success
192-
* \return #IPX_ERR_ARG if any argument is NULL
193-
* \return #IPX_ERR_NOMEM on memory allocation error
208+
* \param[in] tmplt Original template
209+
* \param[in] fields Array of new fields
210+
* \param[in] buffers Array of output values
211+
* \param[in] fields_cnt Number of items in fields (and buffers) array
212+
* \return Parsed modified template or NULL (memory allocation error)
194213
*/
195-
IPX_API int
196-
ipx_modifier_append(struct fds_drec *rec, const struct ipx_modifier_field *fields,
197-
const struct ipx_modifier_output *buffers, const size_t fields_cnt);
214+
IPX_API struct fds_template *
215+
ipfix_template_add_fields(const struct fds_template *tmplt,
216+
const struct ipx_modifier_field *fields,
217+
const struct ipx_modifier_output *buffers,
218+
const size_t fields_cnt);
198219

199220
/**
200221
* \brief Add new session context to modifier

src/core/modifier.c

Lines changed: 21 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -586,16 +586,7 @@ ipfix_template_add_field(const struct ipx_modifier_field field, uint16_t *raw)
586586
assert(false);
587587
}
588588

589-
/**
590-
* \brief Append new fields to template based on given output values
591-
*
592-
* \param[in] tmplt Original template
593-
* \param[in] fields Array of new fields
594-
* \param[in] buffers Array of output values
595-
* \param[in] fields_cnt Number of items in fields (and buffers) array
596-
* \return Parsed modified template or NULL (memory allocation error)
597-
*/
598-
static struct fds_template *
589+
struct fds_template *
599590
ipfix_template_add_fields(const struct fds_template *tmplt,
600591
const struct ipx_modifier_field *fields,
601592
const struct ipx_modifier_output *buffers,
@@ -636,20 +627,7 @@ ipfix_template_add_fields(const struct fds_template *tmplt,
636627
return new_tmplt;
637628
}
638629

639-
/**
640-
* \brief Append new fields to data record
641-
*
642-
* \note Variable length fields will contain 1 or 3 length prefix octets
643-
* based on their length in output buffer. For length < 255 single prefix octet
644-
* is used. Otherwise 3 octets are used.
645-
*
646-
* \param[in,out] rec Original/modified record
647-
* \param[in] fields Field definitions
648-
* \param[in] output Output buffers containing new values
649-
* \param[in] arr_cnt Size of output buffer
650-
* \return #IPX_OK on success, otherwise #IPX_ERR_NOMEM
651-
*/
652-
static int
630+
int
653631
ipfix_msg_add_drecs(struct fds_drec *rec,
654632
const struct ipx_modifier_field *fields,
655633
const struct ipx_modifier_output *output,
@@ -669,7 +647,7 @@ ipfix_msg_add_drecs(struct fds_drec *rec,
669647
if (output[i].length == IPX_MODIFIER_SKIP) {
670648
// Skip field
671649
continue;
672-
} else if (output[i].length < 0) {
650+
} else if (output[i].length <= 0) {
673651
// Dont copy memory from buffer, but keep this field
674652

675653
if (fields[i].length == FDS_IPFIX_VAR_IE_LEN) {
@@ -710,16 +688,7 @@ ipfix_msg_add_drecs(struct fds_drec *rec,
710688
return IPX_OK;
711689
}
712690

713-
/**
714-
* \brief Remove fields from template based on given filter
715-
*
716-
* \warning Only non-option templates are accepted.
717-
*
718-
* \param[in] tmplt Original template
719-
* \param[in] filter Filter array
720-
* \return Parsed modified template or NULL (memory allocation error)
721-
*/
722-
static struct fds_template *
691+
struct fds_template *
723692
ipfix_template_remove_fields(const struct fds_template *tmplt, const uint8_t *filter)
724693
{
725694
assert(tmplt->type == FDS_TYPE_TEMPLATE);
@@ -787,16 +756,7 @@ ipfix_template_remove_fields(const struct fds_template *tmplt, const uint8_t *fi
787756
return new_template;
788757
}
789758

790-
/**
791-
* \brief Remove fields from data record based on filter
792-
*
793-
* Filter is an array of integers for each field in record template. For each
794-
* non-zero value in filter, data field at same position is removed from message.
795-
*
796-
* \param[in,out] rec Original record
797-
* \param[in] filter Filter array
798-
*/
799-
static void
759+
void
800760
ipfix_msg_remove_drecs(struct fds_drec *rec, const uint8_t *filter)
801761
{
802762
uint16_t filter_size = rec->tmplt->fields_cnt_total;
@@ -825,7 +785,8 @@ ipfix_msg_remove_drecs(struct fds_drec *rec, const uint8_t *filter)
825785
rec_size -= it.field.size + length_octets;
826786
}
827787
prev_rec = it.field.data + it.field.size;
828-
assert(idx++ < filter_size);
788+
idx++;
789+
assert(idx <= filter_size);
829790
}
830791

831792
// Go through filter in reverse order and delete selected records
@@ -839,54 +800,6 @@ ipfix_msg_remove_drecs(struct fds_drec *rec, const uint8_t *filter)
839800
rec->size = rec_size;
840801
}
841802

842-
int
843-
ipx_modifier_filter(struct fds_drec *rec, uint8_t *filter)
844-
{
845-
if(rec == NULL || filter == NULL) {
846-
return IPX_ERR_ARG;
847-
}
848-
849-
// Modify template
850-
struct fds_template *tmplt = ipfix_template_remove_fields(rec->tmplt, filter);
851-
if (tmplt == NULL) {
852-
return IPX_ERR_NOMEM;
853-
}
854-
855-
// Modify record
856-
ipfix_msg_remove_drecs(rec, filter);
857-
rec->tmplt = tmplt;
858-
rec->snap = NULL;
859-
860-
return IPX_OK;
861-
}
862-
863-
int
864-
ipx_modifier_append(struct fds_drec *rec,
865-
const struct ipx_modifier_field *fields,
866-
const struct ipx_modifier_output *buffers,
867-
const size_t fields_cnt)
868-
{
869-
if (rec == NULL || fields == NULL || buffers == NULL) {
870-
return IPX_ERR_ARG;
871-
}
872-
873-
// Modify template
874-
struct fds_template *tmplt = ipfix_template_add_fields(rec->tmplt, fields, buffers, fields_cnt);
875-
if (tmplt == NULL) {
876-
return IPX_ERR_NOMEM;
877-
}
878-
879-
// Modify record
880-
if (ipfix_msg_add_drecs(rec, fields, buffers, fields_cnt) != IPX_OK) {
881-
fds_template_destroy(tmplt);
882-
return IPX_ERR_NOMEM;
883-
}
884-
rec->tmplt = tmplt;
885-
rec->snap = NULL;
886-
887-
return IPX_OK;
888-
}
889-
890803
/**
891804
* \brief Generate and set new ID for template
892805
*
@@ -976,7 +889,7 @@ template_store(ipx_modifier_t *mod, struct fds_drec *rec, ipx_msg_garbage_t **ga
976889
* \param[out] garbage Possible garbage created in this function
977890
* \return #IPX_OK on success, #IPX_ERR_NOMEM on memory allocation error
978891
*/
979-
int
892+
static int
980893
template_modify_and_store(struct fds_drec *rec, ipx_modifier_t *mod, struct ipx_modifier_output *output, uint8_t *filter, ipx_msg_garbage_t **garbage)
981894
{
982895
struct fds_template *new_tmplt = (struct fds_template *) rec->tmplt;
@@ -1022,7 +935,7 @@ template_modify_and_store(struct fds_drec *rec, ipx_modifier_t *mod, struct ipx_
1022935
* \param[out] new_output Array of valid values
1023936
* \param[in] output_cnt Number of output buffer elements
1024937
*/
1025-
void
938+
static inline void
1026939
output_buffer_to_array(struct ipx_modifier_output *output, uint8_t *new_output, size_t output_cnt)
1027940
{
1028941
for (size_t i = 0; i < output_cnt; ++i) {
@@ -1034,7 +947,17 @@ output_buffer_to_array(struct ipx_modifier_output *output, uint8_t *new_output,
1034947
}
1035948
}
1036949

1037-
int
950+
/**
951+
* \brief Update appended/filtered fields in template
952+
*
953+
* \param[in] rec Pointer to data record
954+
* \param[in] mod Modifier
955+
* \param[in] output Output buffers
956+
* \param[in] filter Filter array
957+
* \param[out] garbage Possible garbage created in this function
958+
* \return #IPX_OK on success, #IPX_ERR_NOMEM on memory allocation error
959+
*/
960+
static int
1038961
template_update(struct fds_drec *rec, ipx_modifier_t *mod, struct ipx_modifier_output *output, uint8_t *filter, ipx_msg_garbage_t **garbage)
1039962
{
1040963
const struct fds_template *tmplt = rec->tmplt;
@@ -1085,7 +1008,7 @@ template_update(struct fds_drec *rec, ipx_modifier_t *mod, struct ipx_modifier_o
10851008
* \param[in] filter Filter array
10861009
* \return #IPX_OK on success, #IPX_ERR_NOMEM on memory allocation error
10871010
*/
1088-
int
1011+
static int
10891012
fields_update(struct fds_drec *rec, ipx_modifier_t *mod, struct ipx_modifier_output *output, uint8_t *filter)
10901013
{
10911014
if (mod->cb_filter) {

tests/unit/core/modifier/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ set(AUX_TOOLS
77
)
88

99
# Register tests
10-
unit_tests_register_test(modifier_append_and_filter.cpp ${AUX_TOOLS})
10+
unit_tests_register_test(modifier_modify_message.cpp ${AUX_TOOLS})
1111
unit_tests_register_test(modifier_context.cpp ${AUX_TOOLS})

tests/unit/core/modifier/modifier_context.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ class Session {
7777
int adder_one(const struct fds_drec *rec, struct ipx_modifier_output output[], void *cb_data) {
7878
(void) rec, (void) cb_data;
7979
output[0].length = 4;
80+
output[1].length = IPX_MODIFIER_SKIP;
81+
output[2].length = IPX_MODIFIER_SKIP;
8082
return 0;
8183
}
8284

@@ -85,6 +87,7 @@ int adder_two(const struct fds_drec *rec, struct ipx_modifier_output output[], v
8587
(void) rec, (void) cb_data;
8688
output[0].length = 4;
8789
output[1].length = 4;
90+
output[2].length = IPX_MODIFIER_SKIP;
8891
return 0;
8992
}
9093

@@ -431,8 +434,8 @@ TEST_F(MixedSessions, createSameType) {
431434

432435
// Check order
433436
EXPECT_EQ(mod->sessions.ctx_valid, session_cnt);
434-
for (size_t i = 0; i < session_cnt; i++) {
435-
EXPECT_EQ(mod->sessions.ctx[i].session, sessions[i]->getSession(FDS_SESSION_TCP));
437+
for (size_t i = 0; i < session_cnt - 1; i++) {
438+
EXPECT_LT(mod->sessions.ctx[i].session, mod->sessions.ctx[i+1].session);
436439
}
437440

438441
// Free memory
@@ -495,8 +498,8 @@ TEST_F(MixedSessions, reachMaximumTemplateID) {
495498
// 257 because last template with ID 256 was still added ...
496499
findTemplate(FDS_OK, 256, 4);
497500
EXPECT_EQ(mod->curr_ctx->next_id, 257);
498-
modifyRecord(&adder_one);
499-
findTemplate(FDS_OK, 257, 3);
501+
modifyRecord(&adder_three);
502+
findTemplate(FDS_OK, 257, 5);
500503

501504
delete sessions;
502505
}

0 commit comments

Comments
 (0)