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 nlp_stat[3][65536] = {{{0,0}}} ; |
---|
37 | |
---|
38 | void nlp_per_packet(struct libtrace_packet_t *packet) |
---|
39 | { |
---|
40 | uint16_t ethertype; |
---|
41 | void *link; |
---|
42 | libtrace_direction_t dir = trace_get_direction(packet); |
---|
43 | |
---|
44 | link = trace_get_layer3(packet,ðertype,NULL); |
---|
45 | |
---|
46 | if (!link) |
---|
47 | return; |
---|
48 | |
---|
49 | if (dir != TRACE_DIR_INCOMING && dir != TRACE_DIR_OUTGOING) |
---|
50 | dir = TRACE_DIR_OTHER; |
---|
51 | |
---|
52 | nlp_stat[dir][ethertype].count++; |
---|
53 | nlp_stat[dir][ethertype].bytes+=trace_get_wire_length(packet); |
---|
54 | } |
---|
55 | |
---|
56 | void nlp_report(void){ |
---|
57 | int i,j; |
---|
58 | |
---|
59 | FILE *out = fopen("nlp.rpt", "w"); |
---|
60 | if (!out) { |
---|
61 | perror("fopen"); |
---|
62 | return; |
---|
63 | } |
---|
64 | |
---|
65 | /* Put some headings up for human-readability */ |
---|
66 | fprintf(out, "%-12s\t%10s\t%16s %16s\n", |
---|
67 | "NETWORK LAYER", |
---|
68 | "DIRECTION", |
---|
69 | "BYTES", |
---|
70 | "PACKETS"); |
---|
71 | for(i = 0; i < 65536; i++){ |
---|
72 | if (nlp_stat[0][i].count==0 && |
---|
73 | nlp_stat[1][i].count==0 && nlp_stat[2][i].count==0) |
---|
74 | continue; |
---|
75 | switch(i){ |
---|
76 | case 0x0800: |
---|
77 | fprintf(out, "%12s", "IPv4 |"); |
---|
78 | break; |
---|
79 | case 0x0806: |
---|
80 | fprintf(out, "%12s", "ARP |"); |
---|
81 | break; |
---|
82 | case 0x8137: |
---|
83 | fprintf(out, "%12s", "IPX |"); |
---|
84 | break; |
---|
85 | case 0x814C: |
---|
86 | fprintf(out, "%12s", "SNMP |"); |
---|
87 | break; |
---|
88 | case 0x86DD: |
---|
89 | fprintf(out, "%12s", "IPv6 |"); |
---|
90 | break; |
---|
91 | case 0x880B: |
---|
92 | fprintf(out, "%12s", "PPP |"); |
---|
93 | break; |
---|
94 | default: |
---|
95 | fprintf(out, "%10i |",i); |
---|
96 | } |
---|
97 | for(j=0;j<3;j++){ |
---|
98 | if (j != 0) { |
---|
99 | fprintf(out, "%12s", " |"); |
---|
100 | } |
---|
101 | |
---|
102 | switch (j) { |
---|
103 | case 0: |
---|
104 | fprintf(out, "\t%10s", "Outbound"); |
---|
105 | break; |
---|
106 | case 1: |
---|
107 | fprintf(out, "\t%10s", "Inbound"); |
---|
108 | break; |
---|
109 | case 2: |
---|
110 | fprintf(out, "\t%10s", "Unknown"); |
---|
111 | break; |
---|
112 | } |
---|
113 | |
---|
114 | fprintf(out, "\t%16" PRIu64 " %16" PRIu64 "\n", |
---|
115 | nlp_stat[j][i].bytes, |
---|
116 | nlp_stat[j][i].count); |
---|
117 | } |
---|
118 | } |
---|
119 | fclose(out); |
---|
120 | } |
---|