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 | #include <stdio.h> |
---|
32 | #include <stdlib.h> |
---|
33 | #include <string.h> |
---|
34 | #include <assert.h> |
---|
35 | #include <string.h> |
---|
36 | #include <sys/time.h> |
---|
37 | #include <time.h> |
---|
38 | |
---|
39 | #include <netinet/in.h> |
---|
40 | #include <netinet/tcp.h> |
---|
41 | #include <netinet/ip.h> |
---|
42 | #include <arpa/inet.h> |
---|
43 | #include <sys/socket.h> |
---|
44 | |
---|
45 | #include <getopt.h> |
---|
46 | |
---|
47 | #include "libtrace.h" |
---|
48 | #include "dagformat.h" |
---|
49 | //#include "utils.h" |
---|
50 | #include "getdate.h" |
---|
51 | |
---|
52 | |
---|
53 | struct libtrace_t *trace; |
---|
54 | struct libtrace_filter_t *filter; |
---|
55 | |
---|
56 | |
---|
57 | static void parse_cmdline(int argc, char **argv); |
---|
58 | static void usage(); |
---|
59 | |
---|
60 | |
---|
61 | static char *inputuri = 0; |
---|
62 | static char *outputfilename = 0; |
---|
63 | static char *starttime = 0; |
---|
64 | static char *finishtime = 0; |
---|
65 | static char *filterstring = 0; |
---|
66 | static char *prog = 0; |
---|
67 | static uint64_t count = 0; |
---|
68 | static time_t stv = 0; |
---|
69 | static time_t ftv = 0; |
---|
70 | static FILE *outfile = 0; |
---|
71 | |
---|
72 | int main(int argc, char *argv[]) { |
---|
73 | |
---|
74 | int psize = 0; |
---|
75 | time_t ts = 0; |
---|
76 | uint64_t number = 0; |
---|
77 | struct libtrace_packet_t *packet = trace_create_packet(); |
---|
78 | parse_cmdline(argc,argv); |
---|
79 | |
---|
80 | // set up times |
---|
81 | if (starttime) { |
---|
82 | stv = get_date(starttime,NULL); |
---|
83 | fprintf(stderr,"Start time: %lu\n",stv); |
---|
84 | } |
---|
85 | if (finishtime) { |
---|
86 | ftv = get_date(finishtime,NULL); |
---|
87 | fprintf(stderr,"End time: %lu\n",ftv); |
---|
88 | } |
---|
89 | |
---|
90 | // setup output files |
---|
91 | if (outputfilename == 0) { |
---|
92 | outfile = stdout; |
---|
93 | } else { |
---|
94 | if ((outfile = fopen(outputfilename,"wb")) == 0) { |
---|
95 | perror("fopen"); |
---|
96 | exit(0); |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | if (inputuri == 0) { |
---|
101 | usage(); |
---|
102 | exit(0); |
---|
103 | } |
---|
104 | // set up input files |
---|
105 | if ((trace = trace_create(inputuri)) == 0) { |
---|
106 | fprintf(stderr,"trace_create(%s) failed. Bad/nonexistant URI?\n",inputuri); |
---|
107 | exit(0); |
---|
108 | } |
---|
109 | |
---|
110 | if (filterstring) { |
---|
111 | filter = trace_bpf_setfilter(filterstring); |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | for (;;) { |
---|
116 | if ((psize = trace_read_packet(trace, &packet)) == -1) { |
---|
117 | // terminate |
---|
118 | break; |
---|
119 | } |
---|
120 | |
---|
121 | |
---|
122 | if (filter && !trace_bpf_filter(filter,&packet)) { |
---|
123 | continue; |
---|
124 | } |
---|
125 | |
---|
126 | ts = (time_t)trace_get_seconds(&packet); |
---|
127 | |
---|
128 | if (count > 0) { |
---|
129 | number ++; |
---|
130 | fwrite(packet.buffer,psize,1,outfile); |
---|
131 | if (number > count) { |
---|
132 | fprintf(stderr,"Maximum number of packets reached\n"); |
---|
133 | break; |
---|
134 | } |
---|
135 | // carry on the loop |
---|
136 | continue; |
---|
137 | } |
---|
138 | if (stv == 0 || ts > stv) { |
---|
139 | if (ftv == 0 || ts <= ftv) { |
---|
140 | fwrite(packet.buffer,psize,1,outfile); |
---|
141 | } |
---|
142 | } |
---|
143 | if (ts > ftv && ftv != 0) { |
---|
144 | fprintf(stderr,"Packet timestamp (%lu) exceeds maximum time stamp (%lu)\n",ts,ftv); |
---|
145 | break; |
---|
146 | } |
---|
147 | |
---|
148 | } |
---|
149 | trace_destroy(trace); |
---|
150 | fclose(outfile); |
---|
151 | return 0; |
---|
152 | } |
---|
153 | |
---|
154 | static void parse_cmdline(int argc, char **argv) { |
---|
155 | int opt; |
---|
156 | prog = strdup(argv[0]); |
---|
157 | while((opt = getopt(argc, argv, "hi:o:s:e:f:c:")) != EOF) { |
---|
158 | switch(opt) { |
---|
159 | case 'h': |
---|
160 | usage(); |
---|
161 | /* never returns */ |
---|
162 | case 'i': |
---|
163 | inputuri = optarg; |
---|
164 | break; |
---|
165 | case 'o': |
---|
166 | outputfilename = optarg; |
---|
167 | break; |
---|
168 | case 's': |
---|
169 | starttime = optarg; |
---|
170 | break; |
---|
171 | case 'e': |
---|
172 | finishtime = optarg; |
---|
173 | break; |
---|
174 | case 'f': |
---|
175 | filterstring = optarg; |
---|
176 | break; |
---|
177 | case 'c': |
---|
178 | if (starttime || finishtime) { |
---|
179 | printf("Can't have start/end time and a packet count, ignoring count\n"); |
---|
180 | } else { |
---|
181 | count = atoi(optarg); |
---|
182 | } |
---|
183 | break; |
---|
184 | default: |
---|
185 | usage(); |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | } |
---|
190 | static void usage() { |
---|
191 | printf("usage: %s [-h] [-i inputuri] [-o outputfilename] [[-s starttime] [-e endtime]]|[-c count] [-f filterstring]\n",prog); |
---|
192 | printf("\n"); |
---|
193 | printf("-h\t\tshow this usage message.\n"); |
---|
194 | printf("-i file\tinput filename\n"); |
---|
195 | printf("-o file\toutput filename\n"); |
---|
196 | printf("-s start\ttime to start output at\n"); |
---|
197 | printf("-e end\ttime to stop output at\n"); |
---|
198 | printf("-c count\tnumber of packets to output\n"); |
---|
199 | printf("-f filter\tbpf filter to apply\n"); |
---|
200 | printf("\n"); |
---|
201 | printf(" This will read over a DAG trace file <inputfile>, and will output all records between <start> and <finish> times to a new DAG-format file, <outputfile>. The inputfile can be gzip-compressed, however the output wont be.\n If the input or output files are not specified, they will default to stdin and standard out, respectively. If the start and finish times are not specified, they will default to the start of the trace and the end of the trace, respectively.\n"); |
---|
202 | printf(" The start and finish times are in the following format: \"YYYY/MM/DD HH:MM:SS\", and should be given in your local timezome (they are internally converted to UTC)\n"); |
---|
203 | exit(0); |
---|
204 | } |
---|
205 | |
---|