Skip to content

Commit 9d3cdd9

Browse files
committed
Replace printf by local buffer and itoa. See #548
1 parent fa2785b commit 9d3cdd9

1 file changed

Lines changed: 51 additions & 4 deletions

File tree

src/output/output_csv_fast.c

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <stddef.h>
3535
#include <stdint.h>
3636
#include <stdio.h>
37+
#include <stdlib.h>
3738
#include <string.h>
3839
#include <sys/socket.h>
3940
#include <sys/types.h>
@@ -49,9 +50,35 @@
4950
// record counter
5051
static uint32_t recordCount;
5152

53+
#include "itoa.c"
54+
55+
#define AddString(s) \
56+
do { \
57+
size_t len = strlen(s); \
58+
memcpy(streamPtr, s, len); \
59+
streamPtr += len; \
60+
*streamPtr++ = ','; \
61+
} while (0)
62+
63+
#define AddU64(u64) \
64+
do { \
65+
streamPtr = itoa_u64((uint64_t)(u64), streamPtr); \
66+
*streamPtr++ = ','; \
67+
} while (0)
68+
69+
#define AddU32(u32) \
70+
do { \
71+
streamPtr = itoa_u32((uint32_t)(u32), streamPtr); \
72+
*streamPtr++ = ','; \
73+
} while (0)
74+
75+
static char *buff = NULL;
76+
5277
void csv_prolog_fast(void) {
5378
// empty prolog
5479
recordCount = 0;
80+
buff = malloc(4096);
81+
buff[0] = '\0';
5582
printf("cnt,af,firstSeen,lastSeen,proto,srcAddr,srcPort,dstAddr,dstPort,srcAS,dstAS,input,output,flags,srcTos,packets,bytes\n");
5683
} // End of csv_prolog_fast
5784

@@ -75,6 +102,8 @@ void csv_record_fast(FILE *stream, recordHandle_t *recordHandle, int tag) {
75102
EXasRouting_t asNULL = {0};
76103
if (!asRouting) asRouting = &asNULL;
77104

105+
char *streamPtr = buff;
106+
78107
int af = 0;
79108
char sa[IP_STRING_LEN], da[IP_STRING_LEN];
80109
if (ipv4Flow) {
@@ -98,9 +127,27 @@ void csv_record_fast(FILE *stream, recordHandle_t *recordHandle, int tag) {
98127
inet_ntop(AF_INET6, &dst, da, sizeof(da));
99128
}
100129

101-
fprintf(stream, "%u,%i,%" PRIu64 ",%" PRIu64 ",%u,%s,%u,%s,%u,%u,%u,%u,%u,%s,%u,%" PRIu64 ",%" PRIu64 "\n", ++recordCount, af,
102-
genericFlow->msecFirst, genericFlow->msecLast, genericFlow->proto, sa, genericFlow->srcPort, da, genericFlow->dstPort, asRouting->srcAS,
103-
asRouting->dstAS, flowMisc->input, flowMisc->output, FlagsString(genericFlow->proto == IPPROTO_TCP ? genericFlow->tcpFlags : 0),
104-
genericFlow->srcTos, genericFlow->inPackets, genericFlow->inBytes);
130+
AddU32(++recordCount);
131+
AddU32(af);
132+
AddU64(genericFlow->msecFirst);
133+
AddU64(genericFlow->msecLast);
134+
AddU32(genericFlow->proto);
135+
AddString(sa);
136+
AddU32(genericFlow->srcPort);
137+
AddString(da);
138+
AddU32(genericFlow->dstPort);
139+
AddU32(asRouting->srcAS);
140+
AddU32(asRouting->dstAS);
141+
AddU32(flowMisc->input);
142+
AddU32(flowMisc->output);
143+
AddString(FlagsString(genericFlow->proto == IPPROTO_TCP ? genericFlow->tcpFlags : 0));
144+
AddU32(genericFlow->srcTos);
145+
AddU64(genericFlow->inPackets);
146+
AddU64(genericFlow->inBytes);
147+
148+
*--streamPtr = '\n';
149+
*++streamPtr = '\0';
150+
151+
fputs(buff, stream);
105152

106153
} // End of csv_record_fast

0 commit comments

Comments
 (0)