|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Peter Haag |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright notice, |
| 9 | + * this list of conditions and the following disclaimer. |
| 10 | + * * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer in the documentation |
| 12 | + * and/or other materials provided with the distribution. |
| 13 | + * * Neither the name of the author nor the names of its contributors may be |
| 14 | + * used to endorse or promote products derived from this software without |
| 15 | + * specific prior written permission. |
| 16 | + * |
| 17 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 21 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | + * POSSIBILITY OF SUCH DAMAGE. |
| 28 | + * |
| 29 | + */ |
| 30 | + |
| 31 | +#include <arpa/inet.h> |
| 32 | +#include <inttypes.h> |
| 33 | +#include <netinet/in.h> |
| 34 | +#include <stddef.h> |
| 35 | +#include <stdint.h> |
| 36 | +#include <stdio.h> |
| 37 | +#include <string.h> |
| 38 | +#include <sys/socket.h> |
| 39 | +#include <sys/types.h> |
| 40 | +#include <time.h> |
| 41 | + |
| 42 | +#include "nfxV3.h" |
| 43 | +#include "output_csv.h" |
| 44 | +#include "output_util.h" |
| 45 | +#include "util.h" |
| 46 | + |
| 47 | +#define IP_STRING_LEN (INET6_ADDRSTRLEN) |
| 48 | + |
| 49 | +// record counter |
| 50 | +static uint32_t recordCount; |
| 51 | + |
| 52 | +void csv_prolog_fast(void) { |
| 53 | + // empty prolog |
| 54 | + recordCount = 0; |
| 55 | + printf("cnt,af,firstSeen,lastSeen,proto,srcAddr,srcPort,dstAddr,dstPort,srcAS,dstAS,input,output,flags,srcTos,packets,bytes\n"); |
| 56 | +} // End of csv_prolog_fast |
| 57 | + |
| 58 | +void csv_epilog_fast(void) { |
| 59 | + // empty epilog |
| 60 | +} // End of csv_epilog_fast |
| 61 | + |
| 62 | +void csv_record_fast(FILE *stream, recordHandle_t *recordHandle, int tag) { |
| 63 | + EXgenericFlow_t *genericFlow = (EXgenericFlow_t *)recordHandle->extensionList[EXgenericFlowID]; |
| 64 | + EXipv4Flow_t *ipv4Flow = (EXipv4Flow_t *)recordHandle->extensionList[EXipv4FlowID]; |
| 65 | + EXipv6Flow_t *ipv6Flow = (EXipv6Flow_t *)recordHandle->extensionList[EXipv6FlowID]; |
| 66 | + EXflowMisc_t *flowMisc = (EXflowMisc_t *)recordHandle->extensionList[EXflowMiscID]; |
| 67 | + EXasRouting_t *asRouting = (EXasRouting_t *)recordHandle->extensionList[EXasRoutingID]; |
| 68 | + |
| 69 | + EXgenericFlow_t genericNull = {0}; |
| 70 | + if (!genericFlow) genericFlow = &genericNull; |
| 71 | + |
| 72 | + EXflowMisc_t miscNull = {0}; |
| 73 | + if (!flowMisc) flowMisc = &miscNull; |
| 74 | + |
| 75 | + EXasRouting_t asNULL = {0}; |
| 76 | + if (!asRouting) asRouting = &asNULL; |
| 77 | + |
| 78 | + int af = 0; |
| 79 | + char sa[IP_STRING_LEN], da[IP_STRING_LEN]; |
| 80 | + if (ipv4Flow) { |
| 81 | + af = PF_INET; |
| 82 | + uint32_t src = htonl(ipv4Flow->srcAddr); |
| 83 | + uint32_t dst = htonl(ipv4Flow->dstAddr); |
| 84 | + |
| 85 | + inet_ntop(AF_INET, &src, sa, sizeof(sa)); |
| 86 | + inet_ntop(AF_INET, &dst, da, sizeof(da)); |
| 87 | + } |
| 88 | + |
| 89 | + if (ipv6Flow) { |
| 90 | + af = PF_INET6; |
| 91 | + uint64_t src[2], dst[2]; |
| 92 | + src[0] = htonll(ipv6Flow->srcAddr[0]); |
| 93 | + src[1] = htonll(ipv6Flow->srcAddr[1]); |
| 94 | + dst[0] = htonll(ipv6Flow->dstAddr[0]); |
| 95 | + dst[1] = htonll(ipv6Flow->dstAddr[1]); |
| 96 | + |
| 97 | + inet_ntop(AF_INET6, &src, sa, sizeof(sa)); |
| 98 | + inet_ntop(AF_INET6, &dst, da, sizeof(da)); |
| 99 | + } |
| 100 | + |
| 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); |
| 105 | + |
| 106 | +} // End of csv_record_fast |
0 commit comments