1 | /* |
---|
2 | * This file is part of libtrace |
---|
3 | * |
---|
4 | * Copyright (c) 2004 The University of Waikato, Hamilton, New Zealand. |
---|
5 | * Authors: Daniel Lawson |
---|
6 | * Perry Lorier |
---|
7 | * |
---|
8 | * All rights reserved. |
---|
9 | * |
---|
10 | * This code has been developed by the University of Waikato WAND |
---|
11 | * research group. For further information please see http://www.wand.net.nz/ |
---|
12 | * |
---|
13 | * libtrace is free software; you can redistribute it and/or modify |
---|
14 | * it under the terms of the GNU General Public License as published by |
---|
15 | * the Free Software Foundation; either version 2 of the License, or |
---|
16 | * (at your option) any later version. |
---|
17 | * |
---|
18 | * libtrace is distributed in the hope that it will be useful, |
---|
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
21 | * GNU General Public License for more details. |
---|
22 | * |
---|
23 | * You should have received a copy of the GNU General Public License |
---|
24 | * along with libtrace; if not, write to the Free Software |
---|
25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
26 | * |
---|
27 | * $Id$ |
---|
28 | * |
---|
29 | */ |
---|
30 | |
---|
31 | /* This program takes a series of traces and bpf filters and outputs how many |
---|
32 | * bytes/packets |
---|
33 | */ |
---|
34 | |
---|
35 | #include <stdio.h> |
---|
36 | #include <stdlib.h> |
---|
37 | #include <assert.h> |
---|
38 | #include <string.h> |
---|
39 | #include <sys/time.h> |
---|
40 | #include <sys/types.h> |
---|
41 | #include <time.h> |
---|
42 | |
---|
43 | #include <netinet/in.h> |
---|
44 | #include <netinet/in_systm.h> |
---|
45 | #include <netinet/tcp.h> |
---|
46 | #include <netinet/ip.h> |
---|
47 | #include <netinet/ip_icmp.h> |
---|
48 | #include <arpa/inet.h> |
---|
49 | #include <sys/socket.h> |
---|
50 | #include <getopt.h> |
---|
51 | #include <inttypes.h> |
---|
52 | #include "lt_inttypes.h" |
---|
53 | |
---|
54 | #include "libtrace.h" |
---|
55 | #include "tracereport.h" |
---|
56 | #include "report.h" |
---|
57 | |
---|
58 | struct libtrace_t *trace; |
---|
59 | |
---|
60 | /* Process a trace, counting packets that match filter(s) */ |
---|
61 | static void run_trace(char *uri, libtrace_filter_t *filter, int count) |
---|
62 | { |
---|
63 | struct libtrace_packet_t *packet = trace_create_packet(); |
---|
64 | |
---|
65 | fprintf(stderr,"%s:\n",uri); |
---|
66 | |
---|
67 | trace = trace_create(uri); |
---|
68 | if (trace_is_err(trace)) { |
---|
69 | trace_perror(trace,"trace_create"); |
---|
70 | return; |
---|
71 | } |
---|
72 | |
---|
73 | if (filter) { |
---|
74 | trace_config(trace,TRACE_OPTION_FILTER,filter); |
---|
75 | } |
---|
76 | |
---|
77 | if (trace_start(trace)==-1) { |
---|
78 | trace_perror(trace,"trace_start"); |
---|
79 | return; |
---|
80 | } |
---|
81 | |
---|
82 | for (;;) { |
---|
83 | int psize; |
---|
84 | if (count--<1) |
---|
85 | break; |
---|
86 | if ((psize = trace_read_packet(trace, packet)) <1) { |
---|
87 | break; |
---|
88 | } |
---|
89 | |
---|
90 | error_per_packet(packet); |
---|
91 | port_per_packet(packet); |
---|
92 | protocol_per_packet(packet); |
---|
93 | tos_per_packet(packet); |
---|
94 | ttl_per_packet(packet); |
---|
95 | flow_per_packet(packet); |
---|
96 | dir_per_packet(packet); |
---|
97 | |
---|
98 | } |
---|
99 | |
---|
100 | trace_destroy(trace); |
---|
101 | } |
---|
102 | |
---|
103 | static void usage(char *argv0) |
---|
104 | { |
---|
105 | fprintf(stderr,"Usage:\n" |
---|
106 | "%s flags traceuri [traceuri...]\n" |
---|
107 | "-f --filter Apply BPF filter. Can be specified multiple times\n" |
---|
108 | "-H --libtrace-help Print libtrace runtime documentation\n" |
---|
109 | ,argv0); |
---|
110 | exit(1); |
---|
111 | } |
---|
112 | |
---|
113 | int main(int argc, char *argv[]) { |
---|
114 | |
---|
115 | libtrace_filter_t *filter = NULL; |
---|
116 | |
---|
117 | if (argc<2) |
---|
118 | usage(argv[0]); |
---|
119 | |
---|
120 | while(1) { |
---|
121 | int option_index; |
---|
122 | struct option long_options[] = { |
---|
123 | { "filter", 1, 0, 'f' }, |
---|
124 | { "libtrace-help", 0, 0, 'H' }, |
---|
125 | {NULL, 0, 0, 0 }, |
---|
126 | }; |
---|
127 | int c = getopt_long(argc, argv, "f:H", |
---|
128 | long_options, &option_index); |
---|
129 | if (c == -1) |
---|
130 | break; |
---|
131 | switch(c) { |
---|
132 | case 'f': |
---|
133 | if (filter != NULL) { |
---|
134 | fprintf(stderr,"You can only have one filter\n"); |
---|
135 | usage(argv[0]); |
---|
136 | } |
---|
137 | filter=trace_create_filter(optarg); |
---|
138 | break; |
---|
139 | case 'H': |
---|
140 | trace_help(); |
---|
141 | exit(1); |
---|
142 | break; |
---|
143 | default: |
---|
144 | printf("Unknown option: %c\n", c); |
---|
145 | usage(argv[0]); |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | while(optind < argc) { |
---|
150 | run_trace(argv[optind++],filter,(1<<30)); |
---|
151 | } |
---|
152 | |
---|
153 | error_report(); |
---|
154 | flow_report(); |
---|
155 | tos_report(); |
---|
156 | protocol_report(); |
---|
157 | port_report(); |
---|
158 | ttl_report(); |
---|
159 | dir_report(); |
---|
160 | |
---|
161 | return 0; |
---|
162 | } |
---|