@@ -70,22 +70,23 @@ IPFIXOutput::should_start_new_file(std::time_t current_time)
7070 return false ;
7171 }
7272
73- uint32_t time_difference = current_time - file_start_time;
74- if (time_difference >= config->window_size ) {
75- return true ;
76- } else {
77- return false ;
78- }
73+ return (current_time >= file_start_time + time_t (config->window_size ));
7974}
8075
8176/* *
82- * @ brief Create a new output file
83- * @ param[in] current_time Current export time
84- * @ throw runtime_error if the file cannot be created
77+ * \ brief Create a new output file
78+ * \ param[in] current_time Current export time
79+ * \ throw runtime_error if the file cannot be created
8580 */
8681void
8782IPFIXOutput::new_file (const std::time_t current_time)
8883{
84+ // Require (Options) Templates definitions to be added (only if this is not the first file)
85+ bool add_tmplts = (output_file != nullptr );
86+
87+ // Close the previous file, if exists
88+ close_file ();
89+
8990 // Get the timestamp of the file to create
9091 if (config->align_windows ) {
9192 // Round down to the nearest multiple of window size
@@ -98,7 +99,7 @@ IPFIXOutput::new_file(const std::time_t current_time)
9899 std::tm *ts_res = config->use_localtime
99100 ? localtime_r (&file_start_time, &ts_now)
100101 : gmtime_r (&file_start_time, &ts_now);
101- if (ts_res == NULL ) {
102+ if (ts_res == nullptr ) {
102103 // An error has occurred
103104 const char *err_str;
104105 ipx_strerror (errno, err_str);
@@ -141,7 +142,7 @@ IPFIXOutput::new_file(const std::time_t current_time)
141142
142143 // Consider all Templates as undefined
143144 for (auto &odid_pair : odid_contexts) {
144- odid_pair.second .needs_to_write_templates = true ;
145+ odid_pair.second .needs_to_write_templates = add_tmplts ;
145146 }
146147
147148 IPX_CTX_INFO (plugin_context, " New output file created: %s" , filename);
@@ -221,7 +222,7 @@ write_templates_cb(const struct fds_template *tmplt, void *data)
221222 assert (tmplt->type == FDS_TYPE_TEMPLATE || tmplt->type == FDS_TYPE_TEMPLATE_OPTS);
222223
223224 auto *ctx = reinterpret_cast <struct write_templates_aux *>(data);
224- constexpr size_t MSG_SIZE = 1400 ; // Create only IPFIX Message with at most this length // TODO
225+ constexpr size_t MSG_SIZE = 1400 ; // Create only IPFIX Message with at most this length
225226
226227 // First, check if the template can fit into the current message
227228 size_t size_needed = 0 ;
@@ -317,13 +318,15 @@ IPFIXOutput::get_odid(uint32_t odid, const ipx_session *session)
317318{
318319 auto *odid_ctx = &odid_contexts[odid];
319320 if (odid_ctx->session == nullptr ) {
320- // Grant right to this Transport Session to write into thw file with the given ODID...
321+ // Grant this Transport Session access to write into the file with the given ODID...
321322 odid_ctx->session = session;
323+ IPX_CTX_INFO (plugin_context, " [ODID: %" PRIu32 " ] '%s' has been granted access to write to "
324+ " the file with the given ODID." , odid, session->ident );
322325 return odid_ctx;
323326 }
324327
325328 if (odid_ctx->session == session) {
326- // This is already known Transport Session with right to write into the file
329+ // This is already known Transport Session with access to write into the file
327330 return odid_ctx;
328331 }
329332
@@ -376,7 +379,6 @@ IPFIXOutput::on_ipfix_message(ipx_msg_ipfix *message)
376379 assert (time_now >= 0 && sizeof (time_now) >= sizeof (msg_etime));
377380
378381 if (should_start_new_file (time_now)) {
379- close_file ();
380382 new_file (time_now); // This will make sure that templates will be written to the file
381383 }
382384
@@ -390,13 +392,13 @@ IPFIXOutput::on_ipfix_message(ipx_msg_ipfix *message)
390392
391393 // Write all (Options) Templates, if required
392394 if (tsnap != nullptr && odid_context->needs_to_write_templates ) {
393- uint32_t new_sn = (config->skip_unknown_datasets ) ? odid_context->sequence_number : msg_seq ;
395+ uint32_t new_sn = (config->preserve_original ) ? msg_seq : odid_context->sequence_number ;
394396 write_templates (tsnap, msg_odid, msg_etime, new_sn);
395397 odid_context->needs_to_write_templates = false ;
396398 }
397399
398400 // If we don't have to look for unknown Data Sets, just copy the whole message -> FAST PATH
399- if (! config->skip_unknown_datasets ) {
401+ if (config->preserve_original ) {
400402 std::fwrite (msg_hdr, msg_size, 1 , output_file);
401403 return ;
402404 }
@@ -477,7 +479,7 @@ IPFIXOutput::remove_session(const struct ipx_session *session)
477479{
478480 auto it = odid_contexts.begin ();
479481 while (it != odid_contexts.end ()) {
480- // Has this session right to write to the file?
482+ // Has this session access to write to the file?
481483 struct odid_context_s &ctx = it->second ;
482484 if (ctx.session != session) {
483485 // Remove it from colliding sessions, if present
@@ -488,7 +490,7 @@ IPFIXOutput::remove_session(const struct ipx_session *session)
488490
489491 // This is the Transport Session with rights to write to the file with given ODID
490492 if (ctx.colliding_sessions .empty ()) {
491- // Only session in the context -> remove whole context
493+ // Only session in the context -> remove the whole context
492494 it = odid_contexts.erase (it);
493495 continue ;
494496 }
@@ -516,7 +518,7 @@ IPFIXOutput::on_session_message(ipx_msg_session *message)
516518
517519 case IPX_MSG_SESSION_CLOSE:
518520 // Free up ODIDs used by this session allowing them to be used again
519- remove_session (session); // TODO: consider blocking ODID until new file is started + reset seqnum!
521+ remove_session (session);
520522 break ;
521523 }
522524}
0 commit comments