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 | /* |
---|
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 | #include <time.h> |
---|
43 | |
---|
44 | #include <netinet/in.h> |
---|
45 | #include <netinet/in_systm.h> |
---|
46 | #include <netinet/tcp.h> |
---|
47 | #include <netinet/ip.h> |
---|
48 | #include <netinet/ip_icmp.h> |
---|
49 | #include <arpa/inet.h> |
---|
50 | #include <sys/socket.h> |
---|
51 | #include <getopt.h> |
---|
52 | #include <inttypes.h> |
---|
53 | |
---|
54 | #include "libtrace.h" |
---|
55 | |
---|
56 | #ifndef PRIu64 |
---|
57 | #define PRIu64 "llu" |
---|
58 | #endif |
---|
59 | |
---|
60 | struct libtrace_t *trace; |
---|
61 | |
---|
62 | struct filter_t { |
---|
63 | char *expr; |
---|
64 | struct libtrace_filter_t *filter; |
---|
65 | uint64_t count; |
---|
66 | uint64_t bytes; |
---|
67 | } *filters = NULL; |
---|
68 | int filter_count=0; |
---|
69 | uint64_t totcount; |
---|
70 | uint64_t totbytes; |
---|
71 | |
---|
72 | /* Process a trace, counting packets that match filter(s) */ |
---|
73 | void run_trace(char *uri) |
---|
74 | { |
---|
75 | struct libtrace_packet_t *packet = trace_create_packet(); |
---|
76 | int i; |
---|
77 | uint64_t count = 0; |
---|
78 | uint64_t bytes = 0; |
---|
79 | |
---|
80 | fprintf(stderr,"%s:\n",uri); |
---|
81 | |
---|
82 | trace = trace_create(uri); |
---|
83 | |
---|
84 | for (;;) { |
---|
85 | int psize; |
---|
86 | if ((psize = trace_read_packet(trace, packet)) <1) { |
---|
87 | break; |
---|
88 | } |
---|
89 | |
---|
90 | for(i=0;i<filter_count;++i) { |
---|
91 | if(trace_apply_filter(filters[i].filter,packet)) { |
---|
92 | ++filters[i].count; |
---|
93 | filters[i].bytes+=trace_get_wire_length(packet); |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | ++count; |
---|
98 | bytes+=trace_get_wire_length(packet); |
---|
99 | } |
---|
100 | |
---|
101 | for(i=0;i<filter_count;++i) { |
---|
102 | printf("%30s:\t%12"PRIu64"\t%12"PRIu64"\t%7.03f\n",filters[i].expr,filters[i].count,filters[i].bytes,filters[i].count*100.0/count); |
---|
103 | filters[i].bytes=0; |
---|
104 | filters[i].count=0; |
---|
105 | } |
---|
106 | printf("%30s:\t%12"PRIu64"\t%12" PRIu64 "\n","Total",count,bytes); |
---|
107 | totcount+=count; |
---|
108 | totbytes+=bytes; |
---|
109 | |
---|
110 | trace_destroy(trace); |
---|
111 | } |
---|
112 | |
---|
113 | void usage(char *argv0) |
---|
114 | { |
---|
115 | fprintf(stderr,"Usage: %s [--filter|-f bpf ]... libtraceuri...\n",argv0); |
---|
116 | } |
---|
117 | |
---|
118 | int main(int argc, char *argv[]) { |
---|
119 | |
---|
120 | int i; |
---|
121 | |
---|
122 | while(1) { |
---|
123 | int option_index; |
---|
124 | struct option long_options[] = { |
---|
125 | { "filter", 1, 0, 'f' }, |
---|
126 | { NULL, 0, 0, 0 }, |
---|
127 | }; |
---|
128 | |
---|
129 | int c=getopt_long(argc, argv, "f:", |
---|
130 | long_options, &option_index); |
---|
131 | |
---|
132 | if (c==-1) |
---|
133 | break; |
---|
134 | |
---|
135 | switch (c) { |
---|
136 | case 'f': |
---|
137 | ++filter_count; |
---|
138 | filters=realloc(filters,filter_count*sizeof(struct filter_t)); |
---|
139 | filters[filter_count-1].expr=strdup(optarg); |
---|
140 | filters[filter_count-1].filter=trace_create_filter(optarg); |
---|
141 | filters[filter_count-1].count=0; |
---|
142 | filters[filter_count-1].bytes=0; |
---|
143 | break; |
---|
144 | default: |
---|
145 | fprintf(stderr,"Unknown option: %c\n",c); |
---|
146 | usage(argv[0]); |
---|
147 | return 1; |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | printf("%-30s\t%12s\t%12s\t%7s\n","filter","count","bytes","%"); |
---|
152 | for(i=optind;i<argc;++i) { |
---|
153 | run_trace(argv[i]); |
---|
154 | } |
---|
155 | if (optind+1<argc) { |
---|
156 | printf("Grand total:\n"); |
---|
157 | printf("%30s:\t%12"PRIu64"\t%12" PRIu64 "\n","Total",totcount,totbytes); |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | return 0; |
---|
162 | } |
---|