1 | /* |
---|
2 | * |
---|
3 | * Copyright (c) 2007-2016 The University of Waikato, Hamilton, New Zealand. |
---|
4 | * All rights reserved. |
---|
5 | * |
---|
6 | * This file is part of libtrace. |
---|
7 | * |
---|
8 | * This code has been developed by the University of Waikato WAND |
---|
9 | * research group. For further information please see http://www.wand.net.nz/ |
---|
10 | * |
---|
11 | * libtrace is free software; you can redistribute it and/or modify |
---|
12 | * it under the terms of the GNU Lesser General Public License as published by |
---|
13 | * the Free Software Foundation; either version 3 of the License, or |
---|
14 | * (at your option) any later version. |
---|
15 | * |
---|
16 | * libtrace is distributed in the hope that it will be useful, |
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 | * GNU Lesser General Public License for more details. |
---|
20 | * |
---|
21 | * You should have received a copy of the GNU Lesser General Public License |
---|
22 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | * |
---|
24 | * |
---|
25 | */ |
---|
26 | |
---|
27 | |
---|
28 | #include <netdb.h> |
---|
29 | #include <inttypes.h> |
---|
30 | #include <lt_inttypes.h> |
---|
31 | #include <stdio.h> |
---|
32 | #include <stdlib.h> |
---|
33 | #include "libtrace.h" |
---|
34 | #include "tracereport.h" |
---|
35 | #include "contain.h" |
---|
36 | #include "report.h" |
---|
37 | |
---|
38 | static uint64_t flow_count=0; |
---|
39 | |
---|
40 | struct fivetuple_t { |
---|
41 | uint32_t ipa; |
---|
42 | uint32_t ipb; |
---|
43 | uint16_t porta; |
---|
44 | uint16_t portb; |
---|
45 | uint8_t prot; |
---|
46 | }; |
---|
47 | |
---|
48 | static int fivetuplecmp(struct fivetuple_t a, struct fivetuple_t b) |
---|
49 | { |
---|
50 | if (a.porta != b.porta) return a.porta-b.porta; |
---|
51 | if (a.portb != b.portb) return a.portb-b.portb; |
---|
52 | if (a.ipa != b.ipa) return a.ipa-b.ipa; |
---|
53 | if (a.ipb != b.ipb) return a.ipb-b.ipb; |
---|
54 | return a.prot - b.prot; |
---|
55 | } |
---|
56 | |
---|
57 | static int flowset_cmp(const splay *a, const splay *b); |
---|
58 | SET_CREATE(flowset,struct fivetuple_t,fivetuplecmp) |
---|
59 | |
---|
60 | void flow_per_packet(struct libtrace_packet_t *packet) |
---|
61 | { |
---|
62 | struct libtrace_ip *ip = trace_get_ip(packet); |
---|
63 | struct fivetuple_t ft; |
---|
64 | if (!ip) |
---|
65 | return; |
---|
66 | ft.ipa=ip->ip_src.s_addr; |
---|
67 | ft.ipb=ip->ip_dst.s_addr; |
---|
68 | ft.porta=trace_get_source_port(packet); |
---|
69 | ft.portb=trace_get_destination_port(packet); |
---|
70 | ft.prot = 0; |
---|
71 | |
---|
72 | if (!SET_CONTAINS(flowset,ft)) { |
---|
73 | SET_INSERT(flowset,ft); |
---|
74 | flow_count++; |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | void flow_report(void) |
---|
79 | { |
---|
80 | FILE *out = fopen("flows.rpt", "w"); |
---|
81 | if (!out) { |
---|
82 | perror("fopen"); |
---|
83 | return; |
---|
84 | } |
---|
85 | fprintf(out, "Flows: %" PRIu64 "\n",flow_count); |
---|
86 | fclose(out); |
---|
87 | } |
---|