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 "libtrace.h" |
---|
33 | #include "tracereport.h" |
---|
34 | #include "report.h" |
---|
35 | |
---|
36 | static stat_t ecn_stat[3][4] = {{{0,0}}} ; |
---|
37 | |
---|
38 | void ecn_per_packet(struct libtrace_packet_t *packet) |
---|
39 | { |
---|
40 | struct libtrace_ip *ip = trace_get_ip(packet); |
---|
41 | libtrace_direction_t dir = trace_get_direction(packet); |
---|
42 | int ecn; |
---|
43 | |
---|
44 | if (!ip) |
---|
45 | return; |
---|
46 | |
---|
47 | if (dir != TRACE_DIR_INCOMING && dir != TRACE_DIR_OUTGOING) |
---|
48 | dir = TRACE_DIR_OTHER; |
---|
49 | |
---|
50 | ecn = ip->ip_tos & 0x2; |
---|
51 | ecn_stat[dir][ecn].count++; |
---|
52 | ecn_stat[dir][ecn].bytes+=trace_get_wire_length(packet); |
---|
53 | } |
---|
54 | |
---|
55 | void ecn_report(void) |
---|
56 | { |
---|
57 | int i,j; |
---|
58 | int total = 0; |
---|
59 | |
---|
60 | FILE *out = fopen("ecn.rpt", "w"); |
---|
61 | if (!out) { |
---|
62 | perror("fopen"); |
---|
63 | return; |
---|
64 | } |
---|
65 | |
---|
66 | /* Put some headings up for human-readability */ |
---|
67 | fprintf(out, "%-12s\t%10s\t%16s %16s\n", |
---|
68 | "ECN", |
---|
69 | "DIRECTION", |
---|
70 | "BYTES", |
---|
71 | "PACKETS"); |
---|
72 | |
---|
73 | for(i=0;i<4;++i) { |
---|
74 | if (ecn_stat[0][i].count==0 && |
---|
75 | ecn_stat[1][i].count==0 && ecn_stat[2][i].count==0) |
---|
76 | continue; |
---|
77 | switch(i){ |
---|
78 | case 1: |
---|
79 | fprintf(out, "%12s", "ECN CAPE |"); |
---|
80 | break; |
---|
81 | case 2: |
---|
82 | fprintf(out, "%12s", "ECN CAPE |"); |
---|
83 | break; |
---|
84 | case 3: |
---|
85 | fprintf(out, "%12s", "CONG EXP |"); |
---|
86 | break; |
---|
87 | default: |
---|
88 | fprintf(out, "%12s", "NO ECN |"); |
---|
89 | } |
---|
90 | |
---|
91 | for(j=0;j<3;j++){ |
---|
92 | if (j != 0) { |
---|
93 | fprintf(out, "%12s", " |"); |
---|
94 | } |
---|
95 | |
---|
96 | switch (j) { |
---|
97 | case 0: |
---|
98 | fprintf(out, "\t%10s", "Outbound"); |
---|
99 | break; |
---|
100 | case 1: |
---|
101 | fprintf(out, "\t%10s", "Inbound"); |
---|
102 | break; |
---|
103 | case 2: |
---|
104 | fprintf(out, "\t%10s", "Unknown"); |
---|
105 | break; |
---|
106 | } |
---|
107 | fprintf(out, "\t%16" PRIu64 " %16" PRIu64 "\n", |
---|
108 | ecn_stat[j][i].bytes, |
---|
109 | ecn_stat[j][i].count); |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | for(i=0;i<3;i++){ |
---|
114 | for(j=1;j<4;j++) |
---|
115 | total += ecn_stat[i][j].count; |
---|
116 | } |
---|
117 | fprintf(out, "%s: %i\n", "Total ECN", total); |
---|
118 | fclose(out); |
---|
119 | } |
---|