From 9d3cdd998232beffda8a5a086eb29b46eabd3fca Mon Sep 17 00:00:00 2001 From: Peter Haag Date: Mon, 29 Jul 2024 13:31:11 +0200 Subject: [PATCH] Replace printf by local buffer and itoa. See #548 --- src/output/output_csv_fast.c | 55 +++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/src/output/output_csv_fast.c b/src/output/output_csv_fast.c index 1b609c0d..1bcf0c1d 100644 --- a/src/output/output_csv_fast.c +++ b/src/output/output_csv_fast.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -49,9 +50,35 @@ // record counter static uint32_t recordCount; +#include "itoa.c" + +#define AddString(s) \ + do { \ + size_t len = strlen(s); \ + memcpy(streamPtr, s, len); \ + streamPtr += len; \ + *streamPtr++ = ','; \ + } while (0) + +#define AddU64(u64) \ + do { \ + streamPtr = itoa_u64((uint64_t)(u64), streamPtr); \ + *streamPtr++ = ','; \ + } while (0) + +#define AddU32(u32) \ + do { \ + streamPtr = itoa_u32((uint32_t)(u32), streamPtr); \ + *streamPtr++ = ','; \ + } while (0) + +static char *buff = NULL; + void csv_prolog_fast(void) { // empty prolog recordCount = 0; + buff = malloc(4096); + buff[0] = '\0'; printf("cnt,af,firstSeen,lastSeen,proto,srcAddr,srcPort,dstAddr,dstPort,srcAS,dstAS,input,output,flags,srcTos,packets,bytes\n"); } // End of csv_prolog_fast @@ -75,6 +102,8 @@ void csv_record_fast(FILE *stream, recordHandle_t *recordHandle, int tag) { EXasRouting_t asNULL = {0}; if (!asRouting) asRouting = &asNULL; + char *streamPtr = buff; + int af = 0; char sa[IP_STRING_LEN], da[IP_STRING_LEN]; if (ipv4Flow) { @@ -98,9 +127,27 @@ void csv_record_fast(FILE *stream, recordHandle_t *recordHandle, int tag) { inet_ntop(AF_INET6, &dst, da, sizeof(da)); } - fprintf(stream, "%u,%i,%" PRIu64 ",%" PRIu64 ",%u,%s,%u,%s,%u,%u,%u,%u,%u,%s,%u,%" PRIu64 ",%" PRIu64 "\n", ++recordCount, af, - genericFlow->msecFirst, genericFlow->msecLast, genericFlow->proto, sa, genericFlow->srcPort, da, genericFlow->dstPort, asRouting->srcAS, - asRouting->dstAS, flowMisc->input, flowMisc->output, FlagsString(genericFlow->proto == IPPROTO_TCP ? genericFlow->tcpFlags : 0), - genericFlow->srcTos, genericFlow->inPackets, genericFlow->inBytes); + AddU32(++recordCount); + AddU32(af); + AddU64(genericFlow->msecFirst); + AddU64(genericFlow->msecLast); + AddU32(genericFlow->proto); + AddString(sa); + AddU32(genericFlow->srcPort); + AddString(da); + AddU32(genericFlow->dstPort); + AddU32(asRouting->srcAS); + AddU32(asRouting->dstAS); + AddU32(flowMisc->input); + AddU32(flowMisc->output); + AddString(FlagsString(genericFlow->proto == IPPROTO_TCP ? genericFlow->tcpFlags : 0)); + AddU32(genericFlow->srcTos); + AddU64(genericFlow->inPackets); + AddU64(genericFlow->inBytes); + + *--streamPtr = '\n'; + *++streamPtr = '\0'; + + fputs(buff, stream); } // End of csv_record_fast