Skip to content

Commit 67d123f

Browse files
committed
JSON output: minor code polishing
1 parent ceb1559 commit 67d123f

File tree

3 files changed

+26
-29
lines changed

3 files changed

+26
-29
lines changed

doc/data/configs/tcp2anon2json.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
<ignoreUnknown>true</ignoreUnknown>
4141
<ignoreOptions>false</ignoreOptions>
4242
<nonPrintableChar>true</nonPrintableChar>
43-
<detailedInfo>false</detailedInfo>
44-
<templateInfo>false</templateInfo>
43+
<detailedInfo>false</detailedInfo>
44+
<templateInfo>false</templateInfo>
4545

4646
<!-- Output methods -->
4747
<outputs>

src/plugins/output/json/src/Config.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -461,18 +461,18 @@ Config::parse_params(fds_xml_ctx_t *params)
461461
assert(content->type == FDS_OPTS_T_BOOL);
462462
format.split_biflow = content->val_bool;
463463
break;
464-
case FMT_DETAILEDINFO: // Add detailed information about each record
464+
case FMT_DETAILEDINFO: // Add detailed information about each record
465465
assert(content->type == FDS_OPTS_T_BOOL);
466466
format.detailed_info = content->val_bool;
467467
break;
468+
case FMT_TMPLTINFO: // Add template records
469+
assert(content->type == FDS_OPTS_T_BOOL);
470+
format.template_info = content->val_bool;
471+
break;
468472
case OUTPUT_LIST: // List of output plugin
469473
assert(content->type == FDS_OPTS_T_CONTEXT);
470474
parse_outputs(content->ptr_ctx);
471475
break;
472-
case FMT_TMPLTINFO: // Add template records
473-
assert(content->type == FDS_OPTS_T_BOOL);
474-
format.template_info = content->val_bool;
475-
break;
476476
default:
477477
throw std::invalid_argument("Unexpected element within <params>!");
478478
}

src/plugins/output/json/src/Storage.cpp

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ using namespace std;
4747
#include "Storage.hpp"
4848
#include <libfds.h>
4949

50-
/** Base size of the conversion buffer */
50+
/** Base size of the conversion buffer */
5151
#define BUFFER_BASE 4096
52+
/** Size of local conversion buffers (for snprintf) */
53+
#define LOCAL_BSIZE 64
5254

5355
Storage::Storage(const ipx_ctx_t *ctx, const struct cfg_format &fmt)
5456
: m_ctx(ctx), m_format(fmt)
@@ -150,9 +152,8 @@ Storage::output_add(Output *output)
150152
* \param[in] tset_iter (Options) Template set structure to convert
151153
* \param[in] set_id Id of the set
152154
* \param[in] hdr Message header of IPFIX record
153-
* \throw runtime_error if template parser failed
155+
* \throw runtime_error If template parser failed
154156
*/
155-
156157
void
157158
Storage::convert_tmplt_rec(struct fds_tset_iter *tset_iter, uint16_t set_id, fds_ipfix_msg_hdr* hdr)
158159
{
@@ -179,11 +180,11 @@ Storage::convert_tmplt_rec(struct fds_tset_iter *tset_iter, uint16_t set_id, fds
179180
}
180181

181182
// Printing out the header
182-
char field[64];
183-
snprintf(field, 64, "\"ipfix:templateId\":\"%" PRIu16 "\"", tmplt->id);
183+
char field[LOCAL_BSIZE];
184+
snprintf(field, LOCAL_BSIZE, "\"ipfix:templateId\":\"%" PRIu16 "\"", tmplt->id);
184185
buffer_append(field);
185186
if (set_id == FDS_IPFIX_SET_OPTS_TMPLT) {
186-
snprintf(field, 64, ",\"ipfix:scopeCount\":\"%" PRIu16 "\"", tmplt->fields_cnt_scope);
187+
snprintf(field, LOCAL_BSIZE, ",\"ipfix:scopeCount\":\"%" PRIu16 "\"", tmplt->fields_cnt_scope);
187188
buffer_append(field);
188189
}
189190

@@ -201,11 +202,11 @@ Storage::convert_tmplt_rec(struct fds_tset_iter *tset_iter, uint16_t set_id, fds
201202
buffer_append(",");
202203
}
203204
buffer_append("{");
204-
snprintf(field, 64, "\"ipfix:elementId\":\"%" PRIu16 "\"", current.id);
205+
snprintf(field, LOCAL_BSIZE, "\"ipfix:elementId\":\"%" PRIu16 "\"", current.id);
205206
buffer_append(field);
206-
snprintf(field, 64, ",\"ipfix:enterpriseId\":\"%" PRIu32 "\"", current.en);
207+
snprintf(field, LOCAL_BSIZE, ",\"ipfix:enterpriseId\":\"%" PRIu32 "\"", current.en);
207208
buffer_append(field);
208-
snprintf(field, 64, ",\"ipfix:fieldLength\":\"%" PRIu16 "\"", current.length);
209+
snprintf(field, LOCAL_BSIZE, ",\"ipfix:fieldLength\":\"%" PRIu16 "\"", current.length);
209210
buffer_append(field);
210211
buffer_append("}");
211212
}
@@ -344,17 +345,17 @@ void
344345
Storage::addDetailedInfo(fds_ipfix_msg_hdr *hdr)
345346
{
346347
// Array for formatting detailed info fields
347-
char field[64];
348-
snprintf(field, 64, ",\"ipfix:exportTime\":\"%" PRIu32 "\"", ntohl(hdr->export_time));
348+
char field[LOCAL_BSIZE];
349+
snprintf(field, LOCAL_BSIZE, ",\"ipfix:exportTime\":\"%" PRIu32 "\"", ntohl(hdr->export_time));
349350
buffer_append(field);
350351

351-
snprintf(field, 64, ",\"ipfix:seqNumber\":\"%" PRIu32 "\"", ntohl(hdr->seq_num));
352+
snprintf(field, LOCAL_BSIZE, ",\"ipfix:seqNumber\":\"%" PRIu32 "\"", ntohl(hdr->seq_num));
352353
buffer_append(field);
353354

354-
snprintf(field, 64, ",\"ipfix:odid\":\"%" PRIu32 "\"", ntohl(hdr->odid));
355+
snprintf(field, LOCAL_BSIZE, ",\"ipfix:odid\":\"%" PRIu32 "\"", ntohl(hdr->odid));
355356
buffer_append(field);
356357

357-
snprintf(field, 32, ",\"ipfix:msgLength\":\"%" PRIu16 "\"", ntohs(hdr->length));
358+
snprintf(field, LOCAL_BSIZE, ",\"ipfix:msgLength\":\"%" PRIu16 "\"", ntohs(hdr->length));
358359
buffer_append(field);
359360
}
360361

@@ -390,18 +391,14 @@ Storage::convert(struct fds_drec &rec, const fds_iemgr_t *iemgr, fds_ipfix_msg_h
390391
addDetailedInfo(hdr);
391392

392393
// Add template ID to JSON string
393-
char field[64];
394-
snprintf(field, 32, ",\"ipfix:templateId\":\"%" PRIu16 "\"", rec.tmplt->id);
394+
char field[LOCAL_BSIZE];
395+
snprintf(field, LOCAL_BSIZE, ",\"ipfix:templateId\":\"%" PRIu16 "\"", rec.tmplt->id);
395396
buffer_append(field);
396397

397398
// Append the record with '}' parenthesis removed before
398399
buffer_append("}");
399400
}
400401

401-
// Append the record with end of line character
402-
buffer_append("\n");
403-
404-
/* Note: additional information (e.g. ODID, Export Time, etc.) can be added here,
405-
* just use buffer_append() and buffer_reserve() to append and extend buffer, respectively.
406-
*/
402+
// Append the record with end of line character
403+
buffer_append("\n");
407404
}

0 commit comments

Comments
 (0)