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 | * |
---|
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: test-pcap-to-erf.c,v 1.3 2006/02/27 03:41:12 perry Exp $ |
---|
28 | * |
---|
29 | */ |
---|
30 | #ifndef WIN32 |
---|
31 | # include <sys/time.h> |
---|
32 | # include <netinet/in.h> |
---|
33 | # include <netinet/in_systm.h> |
---|
34 | # include <netinet/tcp.h> |
---|
35 | # include <netinet/ip.h> |
---|
36 | # include <netinet/ip_icmp.h> |
---|
37 | # include <arpa/inet.h> |
---|
38 | # include <sys/socket.h> |
---|
39 | #endif |
---|
40 | |
---|
41 | #include <stdio.h> |
---|
42 | #include <stdlib.h> |
---|
43 | #include <assert.h> |
---|
44 | #include <string.h> |
---|
45 | #include <sys/types.h> |
---|
46 | #include <time.h> |
---|
47 | #include <string.h> |
---|
48 | |
---|
49 | #include "dagformat.h" |
---|
50 | #include "libtrace.h" |
---|
51 | #include "libpacketdump.h" |
---|
52 | |
---|
53 | void iferr(libtrace_t *trace) |
---|
54 | { |
---|
55 | libtrace_err_t err = trace_get_err(trace); |
---|
56 | if (err.err_num==0) |
---|
57 | return; |
---|
58 | printf("Error: %s\n",err.problem); |
---|
59 | exit(1); |
---|
60 | } |
---|
61 | |
---|
62 | void iferrout(libtrace_out_t *trace) |
---|
63 | { |
---|
64 | libtrace_err_t err = trace_get_err_output(trace); |
---|
65 | if (err.err_num==0) |
---|
66 | return; |
---|
67 | printf("Error: %s\n",err.problem); |
---|
68 | exit(1); |
---|
69 | } |
---|
70 | |
---|
71 | char *lookup_uri(const char *type) |
---|
72 | { |
---|
73 | if (!strcmp(type,"erf")) |
---|
74 | return "erf:traces/100_packets.erf"; |
---|
75 | if (!strcmp(type,"pcap")) |
---|
76 | return "pcap:traces/100_packets.pcap"; |
---|
77 | if (!strcmp(type,"wtf")) |
---|
78 | return "wtf:traces/wed.wtf"; |
---|
79 | if (!strcmp(type,"rtclient")) |
---|
80 | return "rtclient:chasm"; |
---|
81 | if (!strcmp(type,"pcapfile")) |
---|
82 | return "pcapfile:traces/100_packets.pcap"; |
---|
83 | if (!strcmp(type,"legacyatm")) |
---|
84 | return "legacyatm:traces/legacyatm.gz"; |
---|
85 | if (!strcmp(type,"legacypos")) |
---|
86 | return "legacypos:traces/legacypos.gz"; |
---|
87 | if (!strcmp(type,"legacyeth")) |
---|
88 | return "legacyeth:traces/legacyeth.gz"; |
---|
89 | if (!strcmp(type, "duck")) |
---|
90 | return "duck:traces/100_packets.duck"; |
---|
91 | if (!strcmp(type, "tsh")) |
---|
92 | return "tsh:traces/10_packets.tsh.gz"; |
---|
93 | return "unknown"; |
---|
94 | } |
---|
95 | |
---|
96 | char *lookup_out_uri(const char *type) { |
---|
97 | if (!strcmp(type,"erf")) |
---|
98 | return "erf:traces/100_packets.out.erf"; |
---|
99 | if (!strcmp(type,"pcap")) |
---|
100 | return "pcap:traces/100_packets.out.pcap"; |
---|
101 | if (!strcmp(type,"pcapfile")) |
---|
102 | return "pcapfile:traces/100_packets.out.pcap"; |
---|
103 | if (!strcmp(type,"wtf")) |
---|
104 | return "wtf:traces/wed.out.wtf"; |
---|
105 | if (!strcmp(type,"duck")) |
---|
106 | return "duck:traces/100_packets.out.duck"; |
---|
107 | return "unknown"; |
---|
108 | } |
---|
109 | |
---|
110 | int convert_fail(libtrace_packet_t *p1, libtrace_packet_t *p2) { |
---|
111 | |
---|
112 | libtrace_linktype_t l1, l2; |
---|
113 | |
---|
114 | int cap1, cap2, wire1, wire2; |
---|
115 | |
---|
116 | l1 = trace_get_link_type(p1); |
---|
117 | l2 = trace_get_link_type(p2); |
---|
118 | |
---|
119 | cap1 = trace_get_capture_length(p1); |
---|
120 | cap2 = trace_get_capture_length(p2); |
---|
121 | |
---|
122 | wire1 = trace_get_wire_length(p1); |
---|
123 | wire2 = trace_get_wire_length(p2); |
---|
124 | |
---|
125 | |
---|
126 | /* If the capture lengths are not the same, it may be because the |
---|
127 | * packet has been truncated - the important thing then is that the |
---|
128 | * wire lengths match */ |
---|
129 | if (cap1 != cap2) { |
---|
130 | |
---|
131 | /* Check for truncation */ |
---|
132 | if (cap2 == wire2) |
---|
133 | return 0; |
---|
134 | |
---|
135 | /* Check that the wire length is unchanged */ |
---|
136 | if (wire1 == wire2) |
---|
137 | return 0; |
---|
138 | |
---|
139 | /* There is no matching DLT for ATM, so we have to demote the |
---|
140 | * packet to LLCSNAP when converting to pcap. The demotion |
---|
141 | * loses 4 bytes of ATM header, but we need to allow for that |
---|
142 | */ |
---|
143 | if (l1 == TRACE_TYPE_ATM && l2 == TRACE_TYPE_LLCSNAP) { |
---|
144 | if (wire1 - wire2 == 4) |
---|
145 | return 0; |
---|
146 | } |
---|
147 | |
---|
148 | return 1; |
---|
149 | } |
---|
150 | |
---|
151 | return 0; |
---|
152 | } |
---|
153 | |
---|
154 | int main(int argc, char *argv[]) { |
---|
155 | int psize = 0; |
---|
156 | int error = 0; |
---|
157 | uint64_t count = 0; |
---|
158 | int level = 0; |
---|
159 | int expected = 100; |
---|
160 | int tcpcount = 0; |
---|
161 | libtrace_t *trace,*trace2; |
---|
162 | libtrace_out_t *outtrace; |
---|
163 | libtrace_packet_t *packet,*packet2; |
---|
164 | const char *trace1name; |
---|
165 | const char *trace2name; |
---|
166 | |
---|
167 | trace = trace_create(argv[1]); |
---|
168 | iferr(trace); |
---|
169 | |
---|
170 | if (strcmp(argv[1],"rtclient")==0) |
---|
171 | expected=101; |
---|
172 | |
---|
173 | outtrace = trace_create_output(argv[2]); |
---|
174 | iferrout(outtrace); |
---|
175 | |
---|
176 | level=0; |
---|
177 | trace_config_output(outtrace,TRACE_OPTION_OUTPUT_COMPRESS,&level); |
---|
178 | if (trace_is_err_output(outtrace)) { |
---|
179 | trace_perror_output(outtrace,"WARNING: "); |
---|
180 | } |
---|
181 | |
---|
182 | trace_start(trace); |
---|
183 | iferr(trace); |
---|
184 | trace_start_output(outtrace); |
---|
185 | iferrout(outtrace); |
---|
186 | |
---|
187 | packet=trace_create_packet(); |
---|
188 | for (;;) { |
---|
189 | if ((psize = trace_read_packet(trace, packet)) <0) { |
---|
190 | error = 1; |
---|
191 | break; |
---|
192 | } |
---|
193 | if (psize == 0) { |
---|
194 | error = 0; |
---|
195 | break; |
---|
196 | } |
---|
197 | count ++; |
---|
198 | trace_write_packet(outtrace,packet); |
---|
199 | iferrout(outtrace); |
---|
200 | } |
---|
201 | trace_destroy_packet(packet); |
---|
202 | if (error != 0) { |
---|
203 | iferr(trace); |
---|
204 | } |
---|
205 | trace_destroy(trace); |
---|
206 | trace_destroy_output(outtrace); |
---|
207 | |
---|
208 | if (error) |
---|
209 | return error; |
---|
210 | |
---|
211 | /* Now read it back in again and check it's all kosher */ |
---|
212 | trace1name = argv[1]; |
---|
213 | trace = trace_create(trace1name); |
---|
214 | iferr(trace); |
---|
215 | trace_start(trace); |
---|
216 | trace2name = (argv[2]); |
---|
217 | trace2 = trace_create(trace2name); |
---|
218 | iferr(trace2); |
---|
219 | trace_start(trace2); |
---|
220 | iferr(trace2); |
---|
221 | packet=trace_create_packet(); |
---|
222 | packet2=trace_create_packet(); |
---|
223 | count=0; |
---|
224 | tcpcount=0; |
---|
225 | while(trace_read_packet(trace,packet)>0) { |
---|
226 | int err; |
---|
227 | ++count; |
---|
228 | if ((err=trace_read_packet(trace2,packet2))<1) { |
---|
229 | printf("premature EOF on destination, %lu from %s, %i from %s\n",count,lookup_uri(argv[1]),count-1,lookup_out_uri(argv[2])); |
---|
230 | iferr(trace2); |
---|
231 | error=1; |
---|
232 | break; |
---|
233 | } |
---|
234 | /* The capture length might be snapped down to the wire length */ |
---|
235 | if (convert_fail(packet, packet2)) { |
---|
236 | printf("\t%s\t%s\n", |
---|
237 | trace1name, |
---|
238 | trace2name); |
---|
239 | printf("packet\t%lu\n", count); |
---|
240 | printf("caplen\t%zd\t%zd\t%+zd\n", |
---|
241 | trace_get_capture_length(packet), |
---|
242 | trace_get_capture_length(packet2), |
---|
243 | trace_get_capture_length(packet2)-trace_get_capture_length(packet)); |
---|
244 | printf("wirelen\t%zd\t%zd\t%+zd\n", |
---|
245 | trace_get_wire_length(packet), |
---|
246 | trace_get_wire_length(packet2), |
---|
247 | trace_get_wire_length(packet2)-trace_get_wire_length(packet)); |
---|
248 | printf("link\t%d\t%d\n", |
---|
249 | trace_get_link_type(packet), |
---|
250 | trace_get_link_type(packet2)); |
---|
251 | abort(); |
---|
252 | } |
---|
253 | |
---|
254 | if (trace_get_tcp(packet)) { |
---|
255 | if (!trace_get_tcp(packet2)) { |
---|
256 | printf("trace corrupt -- expected tcp\n"); |
---|
257 | |
---|
258 | //trace_dump_packet(packet); |
---|
259 | trace_hexdump_packet(packet); |
---|
260 | printf("\n"); |
---|
261 | //trace_dump_packet(packet2); |
---|
262 | trace_hexdump_packet(packet2); |
---|
263 | |
---|
264 | |
---|
265 | error=1; |
---|
266 | break; |
---|
267 | } |
---|
268 | ++tcpcount; |
---|
269 | } else { |
---|
270 | if (trace_get_tcp(packet2)) { |
---|
271 | printf("trace corrupt: unexpected tcp\n"); |
---|
272 | error=1; |
---|
273 | break; |
---|
274 | } |
---|
275 | } |
---|
276 | |
---|
277 | } |
---|
278 | if (count != 0 && trace_read_packet(trace2,packet2)>0) { |
---|
279 | printf("Extra packets after EOF\n"); |
---|
280 | error=1; |
---|
281 | } |
---|
282 | trace_destroy(trace); |
---|
283 | trace_destroy(trace2); |
---|
284 | trace_destroy_packet(packet); |
---|
285 | trace_destroy_packet(packet2); |
---|
286 | |
---|
287 | //printf("tcpcount=%i\n",tcpcount); |
---|
288 | |
---|
289 | return error; |
---|
290 | } |
---|