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 prot_stat[3][256] = {{{0,0}}} ; |
---|
37 | static bool suppress[3] = {true,true,true}; |
---|
38 | |
---|
39 | void protocol_per_packet(struct libtrace_packet_t *packet) |
---|
40 | { |
---|
41 | uint8_t proto; |
---|
42 | libtrace_direction_t dir = trace_get_direction(packet); |
---|
43 | |
---|
44 | if (trace_get_transport(packet,&proto,NULL)==NULL) |
---|
45 | return; |
---|
46 | |
---|
47 | if (dir != TRACE_DIR_INCOMING && dir != TRACE_DIR_OUTGOING) |
---|
48 | dir = TRACE_DIR_OTHER; |
---|
49 | |
---|
50 | prot_stat[dir][proto].count++; |
---|
51 | prot_stat[dir][proto].bytes+=trace_get_wire_length(packet); |
---|
52 | suppress[dir] = false; |
---|
53 | } |
---|
54 | |
---|
55 | void protocol_report(void) |
---|
56 | { |
---|
57 | int i,j; |
---|
58 | FILE *out = fopen("protocol.rpt", "w"); |
---|
59 | if (!out) { |
---|
60 | perror("fopen"); |
---|
61 | return; |
---|
62 | } |
---|
63 | fprintf(out, "%-16s\t%10s\t%16s %16s\n", |
---|
64 | "PROTOCOL", |
---|
65 | "DIRECTION", |
---|
66 | "BYTES", |
---|
67 | "PACKETS"); |
---|
68 | |
---|
69 | setprotoent(1); |
---|
70 | for(i=0;i<256;++i) { |
---|
71 | struct protoent *prot; |
---|
72 | if (prot_stat[0][i].count==0 && |
---|
73 | prot_stat[1][i].count==0 && prot_stat[2][i].count==0) |
---|
74 | continue; |
---|
75 | prot = getprotobynumber(i); |
---|
76 | if (prot) { |
---|
77 | fprintf(out, "%16s",prot->p_name); |
---|
78 | } |
---|
79 | else { |
---|
80 | fprintf(out, "%16i:",i); |
---|
81 | } |
---|
82 | for (j=0; j < 3; j++) { |
---|
83 | if (j != 0) { |
---|
84 | fprintf(out, "%16s", " "); |
---|
85 | } |
---|
86 | switch (j) { |
---|
87 | case 0: |
---|
88 | fprintf(out, "\t%10s", "Outbound"); |
---|
89 | break; |
---|
90 | case 1: |
---|
91 | fprintf(out, "\t%10s", "Inbound"); |
---|
92 | break; |
---|
93 | case 2: |
---|
94 | fprintf(out, "\t%10s", "Unknown"); |
---|
95 | break; |
---|
96 | } |
---|
97 | |
---|
98 | fprintf(out, "\t%16" PRIu64 " %16" PRIu64 "\n", |
---|
99 | prot_stat[j][i].bytes, |
---|
100 | prot_stat[j][i].count); |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | setprotoent(0); |
---|
105 | fclose(out); |
---|
106 | } |
---|