1 | /* |
---|
2 | * This file is part of libtrace |
---|
3 | * |
---|
4 | * Copyright (c) 2007 The University of Waikato, Hamilton, New Zealand. |
---|
5 | * Authors: Daniel Lawson |
---|
6 | * Perry Lorier |
---|
7 | * Josef Vodanovich |
---|
8 | * |
---|
9 | * All rights reserved. |
---|
10 | * |
---|
11 | * This code has been developed by the University of Waikato WAND |
---|
12 | * research group. For further information please see http://www.wand.net.nz/ |
---|
13 | * |
---|
14 | * libtrace is free software; you can redistribute it and/or modify |
---|
15 | * it under the terms of the GNU General Public License as published by |
---|
16 | * the Free Software Foundation; either version 2 of the License, or |
---|
17 | * (at your option) any later version. |
---|
18 | * |
---|
19 | * libtrace is distributed in the hope that it will be useful, |
---|
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
22 | * GNU General Public License for more details. |
---|
23 | * |
---|
24 | * You should have received a copy of the GNU General Public License |
---|
25 | * along with libtrace; if not, write to the Free Software |
---|
26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
27 | * |
---|
28 | * $Id$ |
---|
29 | * |
---|
30 | */ |
---|
31 | |
---|
32 | /* This program takes a series of traces and bpf filters and outputs how many |
---|
33 | * bytes/packets |
---|
34 | */ |
---|
35 | |
---|
36 | #include <stdio.h> |
---|
37 | #include <stdlib.h> |
---|
38 | #include <assert.h> |
---|
39 | #include <string.h> |
---|
40 | #include <sys/time.h> |
---|
41 | #include <sys/types.h> |
---|
42 | |
---|
43 | #include <getopt.h> |
---|
44 | #include <inttypes.h> |
---|
45 | //#include "lt_inttypes.h" |
---|
46 | |
---|
47 | #include "libtrace.h" |
---|
48 | #include "tracereport.h" |
---|
49 | #include "report.h" |
---|
50 | |
---|
51 | struct libtrace_t *trace; |
---|
52 | uint32_t reports_required = 0; |
---|
53 | |
---|
54 | /* Process a trace, counting packets that match filter(s) */ |
---|
55 | void run_trace(char *uri, libtrace_filter_t *filter, int count) |
---|
56 | { |
---|
57 | struct libtrace_packet_t *packet = trace_create_packet(); |
---|
58 | |
---|
59 | trace = trace_create(uri); |
---|
60 | |
---|
61 | if (trace_is_err(trace)) { |
---|
62 | trace_perror(trace,"trace_create"); |
---|
63 | return; |
---|
64 | } |
---|
65 | |
---|
66 | if (filter) { |
---|
67 | trace_config(trace,TRACE_OPTION_FILTER,filter); |
---|
68 | } |
---|
69 | |
---|
70 | if (trace_start(trace)==-1) { |
---|
71 | trace_perror(trace,"trace_start"); |
---|
72 | return; |
---|
73 | } |
---|
74 | |
---|
75 | for (;;) { |
---|
76 | int psize; |
---|
77 | |
---|
78 | /* Not convinced we need this count business */ |
---|
79 | /* |
---|
80 | if (count--<1) |
---|
81 | break; |
---|
82 | */ |
---|
83 | if ((psize = trace_read_packet(trace, packet)) <1) { |
---|
84 | break; |
---|
85 | } |
---|
86 | if (reports_required & REPORT_TYPE_ERROR) |
---|
87 | error_per_packet(packet); |
---|
88 | if (reports_required & REPORT_TYPE_PORT) |
---|
89 | port_per_packet(packet); |
---|
90 | if (reports_required & REPORT_TYPE_PROTO) |
---|
91 | protocol_per_packet(packet); |
---|
92 | if (reports_required & REPORT_TYPE_TOS) |
---|
93 | tos_per_packet(packet); |
---|
94 | if (reports_required & REPORT_TYPE_TTL) |
---|
95 | ttl_per_packet(packet); |
---|
96 | if (reports_required & REPORT_TYPE_FLOW) |
---|
97 | flow_per_packet(packet); |
---|
98 | if (reports_required & REPORT_TYPE_TCPOPT) |
---|
99 | tcpopt_per_packet(packet); |
---|
100 | if (reports_required & REPORT_TYPE_NLP) |
---|
101 | nlp_per_packet(packet); |
---|
102 | if (reports_required & REPORT_TYPE_DIR) |
---|
103 | dir_per_packet(packet); |
---|
104 | /* |
---|
105 | if (reports_required & REPORT_TYPE_ECN) |
---|
106 | ecn_per_packet(packet); |
---|
107 | if (reports_required & REPORT_TYPE_TCPSEQ) |
---|
108 | tcpseg_per_packet(packet); |
---|
109 | */ |
---|
110 | } |
---|
111 | trace_destroy(trace); |
---|
112 | } |
---|
113 | |
---|
114 | void usage(char *argv0) |
---|
115 | { |
---|
116 | fprintf(stderr,"Usage:\n" |
---|
117 | "%s flags traceuri [traceuri...]\n" |
---|
118 | "-f --filter=bpf \tApply BPF filter. Can be specified multiple times\n" |
---|
119 | "-e --error Report packet errors (e.g. checksum failures, rxerrors)\n" |
---|
120 | "-F --flow Report flows\n" |
---|
121 | "-P --protocol Report transport protocols\n" |
---|
122 | "-p --port Report port numbers\n" |
---|
123 | "-T --tos Report IP TOS\n" |
---|
124 | "-O --tcpoptions \tReport TCP Options\n" |
---|
125 | "-n --nlp Report network layer protocols\n" |
---|
126 | "-d --direction Report direction\n" |
---|
127 | "-C --ecn Report TCP ECN information\n" |
---|
128 | "-s --tcpsegment \tReport TCP segment size\n" |
---|
129 | "-H --help Print libtrace runtime documentation\n" |
---|
130 | ,argv0); |
---|
131 | exit(1); |
---|
132 | } |
---|
133 | |
---|
134 | int main(int argc, char *argv[]) { |
---|
135 | |
---|
136 | int i; |
---|
137 | int opt; |
---|
138 | char *filterstring=NULL; |
---|
139 | |
---|
140 | libtrace_filter_t *filter = NULL;/*trace_bpf_setfilter(filterstring); */ |
---|
141 | |
---|
142 | while (1) { |
---|
143 | int option_index; |
---|
144 | struct option long_options[] = { |
---|
145 | { "filter", 1, 0, 'f' }, |
---|
146 | { "help", 0, 0, 'H' }, |
---|
147 | { "error", 0, 0, 'e' }, |
---|
148 | { "flow", 0, 0, 'F' }, |
---|
149 | { "protocol", 0, 0, 'P' }, |
---|
150 | { "port", 0, 0, 'p' }, |
---|
151 | { "tos", 0, 0, 'T' }, |
---|
152 | { "ttl", 0, 0, 't' }, |
---|
153 | { "tcpoptions", 0, 0, 'O' }, |
---|
154 | { "nlp", 0, 0, 'n' }, |
---|
155 | { "direction", 0, 0, 'd' }, |
---|
156 | { "ecn", 0, 0, 'C' }, |
---|
157 | { "tcpsegment", 0, 0, 's' }, |
---|
158 | { NULL, 0, 0, 0 } |
---|
159 | }; |
---|
160 | opt = getopt_long(argc, argv, "f:HeFPpTtOndCs", long_options, |
---|
161 | &option_index); |
---|
162 | if (opt == -1) |
---|
163 | break; |
---|
164 | |
---|
165 | switch (opt) { |
---|
166 | case 'C': |
---|
167 | reports_required |= REPORT_TYPE_ECN; |
---|
168 | break; |
---|
169 | case 'd': |
---|
170 | reports_required |= REPORT_TYPE_DIR; |
---|
171 | break; |
---|
172 | case 'e': |
---|
173 | reports_required |= REPORT_TYPE_ERROR; |
---|
174 | break; |
---|
175 | case 'F': |
---|
176 | reports_required |= REPORT_TYPE_FLOW; |
---|
177 | break; |
---|
178 | case 'f': |
---|
179 | filterstring = optarg; |
---|
180 | break; |
---|
181 | case 'H': |
---|
182 | usage(argv[0]); |
---|
183 | break; |
---|
184 | case 'n': |
---|
185 | reports_required |= REPORT_TYPE_NLP; |
---|
186 | break; |
---|
187 | case 'O': |
---|
188 | reports_required |= REPORT_TYPE_TCPOPT; |
---|
189 | break; |
---|
190 | case 'P': |
---|
191 | reports_required |= REPORT_TYPE_PROTO; |
---|
192 | break; |
---|
193 | case 'p': |
---|
194 | reports_required |= REPORT_TYPE_PORT; |
---|
195 | break; |
---|
196 | case 's': |
---|
197 | reports_required |= REPORT_TYPE_TCPSEG; |
---|
198 | break; |
---|
199 | case 'T': |
---|
200 | reports_required |= REPORT_TYPE_TOS; |
---|
201 | break; |
---|
202 | case 't': |
---|
203 | reports_required |= REPORT_TYPE_TTL; |
---|
204 | break; |
---|
205 | default: |
---|
206 | usage(argv[0]); |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | if (filterstring) { |
---|
211 | filter = trace_create_filter(filterstring); |
---|
212 | } |
---|
213 | |
---|
214 | |
---|
215 | for(i=optind;i<argc;++i) { |
---|
216 | /* This is handy for knowing how far through the traceset |
---|
217 | * we are - printing to stderr because we use stdout for |
---|
218 | * genuine output at the moment */ |
---|
219 | fprintf(stderr, "Reading from trace: %s\n", argv[i]); |
---|
220 | run_trace(argv[i],filter,(1<<30)); |
---|
221 | } |
---|
222 | |
---|
223 | if (reports_required & REPORT_TYPE_ERROR) |
---|
224 | error_report(); |
---|
225 | if (reports_required & REPORT_TYPE_FLOW) |
---|
226 | flow_report(); |
---|
227 | if (reports_required & REPORT_TYPE_TOS) |
---|
228 | tos_report(); |
---|
229 | if (reports_required & REPORT_TYPE_PROTO) |
---|
230 | protocol_report(); |
---|
231 | if (reports_required & REPORT_TYPE_PORT) |
---|
232 | port_report(); |
---|
233 | if (reports_required & REPORT_TYPE_TTL) |
---|
234 | ttl_report(); |
---|
235 | if (reports_required & REPORT_TYPE_TCPOPT) |
---|
236 | tcpopt_report(); |
---|
237 | if (reports_required & REPORT_TYPE_NLP) |
---|
238 | nlp_report(); |
---|
239 | if (reports_required & REPORT_TYPE_DIR) |
---|
240 | dir_report(); |
---|
241 | /* |
---|
242 | if (reports_required & REPORT_TYPE_ECN) |
---|
243 | ecn_report(); |
---|
244 | if (reports_required & REPORT_TYPE_TCPSEG) |
---|
245 | tcpseg_report(); |
---|
246 | */ |
---|
247 | return 0; |
---|
248 | } |
---|