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 | |
---|
52 | void iferr(libtrace_t *trace) |
---|
53 | { |
---|
54 | libtrace_err_t err = trace_get_err(trace); |
---|
55 | if (err.err_num==0) |
---|
56 | return; |
---|
57 | printf("Error: %s\n",err.problem); |
---|
58 | exit(1); |
---|
59 | } |
---|
60 | |
---|
61 | void iferrout(libtrace_out_t *trace) |
---|
62 | { |
---|
63 | libtrace_err_t err = trace_get_err_output(trace); |
---|
64 | if (err.err_num==0) |
---|
65 | return; |
---|
66 | printf("Error: %s\n",err.problem); |
---|
67 | exit(1); |
---|
68 | } |
---|
69 | |
---|
70 | char *lookup_uri(const char *type) |
---|
71 | { |
---|
72 | if (!strcmp(type,"erf")) |
---|
73 | return "erf:traces/100_packets.erf"; |
---|
74 | if (!strcmp(type,"pcap")) |
---|
75 | return "pcap:traces/100_packets.pcap"; |
---|
76 | if (!strcmp(type,"wtf")) |
---|
77 | return "wtf:traces/wed.wtf"; |
---|
78 | if (!strcmp(type,"rtclient")) |
---|
79 | return "rtclient:chasm"; |
---|
80 | if (!strcmp(type,"pcapfile")) |
---|
81 | return "pcapfile:traces/100_packets.pcap"; |
---|
82 | if (!strcmp(type,"legacyatm")) |
---|
83 | return "legacyatm:traces/legacyatm.gz"; |
---|
84 | if (!strcmp(type,"legacypos")) |
---|
85 | return "legacypos:traces/legacypos.gz"; |
---|
86 | if (!strcmp(type,"legacyeth")) |
---|
87 | return "legacyeth:traces/legacyeth.gz"; |
---|
88 | if (!strcmp(type, "duck")) |
---|
89 | return "duck:traces/100_packets.duck"; |
---|
90 | if (!strcmp(type, "tsh")) |
---|
91 | return "tsh:traces/10_packets.tsh.gz"; |
---|
92 | if (!strcmp(type, "sll1")) |
---|
93 | return "pcapfile:traces/sll.pcap.gz"; |
---|
94 | if (!strcmp(type, "sll2")) |
---|
95 | return "pcapfile:traces/100_sll.pcap"; |
---|
96 | return "unknown"; |
---|
97 | } |
---|
98 | |
---|
99 | char *lookup_out_uri(const char *type) { |
---|
100 | if (!strcmp(type,"erf")) |
---|
101 | return "erf:traces/100_packets.out.erf"; |
---|
102 | if (!strcmp(type,"pcap")) |
---|
103 | return "pcap:traces/100_packets.out.pcap"; |
---|
104 | if (!strcmp(type,"pcapfile")) |
---|
105 | return "pcapfile:traces/100_packets.out.pcap"; |
---|
106 | if (!strcmp(type,"wtf")) |
---|
107 | return "wtf:traces/wed.out.wtf"; |
---|
108 | if (!strcmp(type,"duck")) |
---|
109 | return "duck:traces/100_packets.out.duck"; |
---|
110 | return "unknown"; |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | static int length_changed(libtrace_packet_t *packet, |
---|
115 | libtrace_packet_t *packet2) { |
---|
116 | |
---|
117 | uint16_t cap_len_1 = trace_get_capture_length(packet); |
---|
118 | uint16_t cap_len_2 = trace_get_capture_length(packet2); |
---|
119 | uint16_t wlen_1 = trace_get_wire_length(packet); |
---|
120 | uint16_t wlen_2 = trace_get_wire_length(packet2); |
---|
121 | |
---|
122 | /* Special case where we demoted a packet */ |
---|
123 | if (trace_get_link_type(packet) == TRACE_TYPE_LINUX_SLL && |
---|
124 | trace_get_link_type(packet2) != TRACE_TYPE_LINUX_SLL) { |
---|
125 | |
---|
126 | if (cap_len_2 != cap_len_1 - sizeof(libtrace_sll_header_t)) |
---|
127 | return true; |
---|
128 | } else { |
---|
129 | |
---|
130 | if (cap_len_1 != cap_len_2 && cap_len_2 != wlen_2) |
---|
131 | return true; |
---|
132 | } |
---|
133 | |
---|
134 | if (wlen_1 != wlen_2) |
---|
135 | return true; |
---|
136 | |
---|
137 | return false; |
---|
138 | |
---|
139 | } |
---|
140 | |
---|
141 | int main(int argc, char *argv[]) { |
---|
142 | int psize = 0; |
---|
143 | int error = 0; |
---|
144 | int count = 0; |
---|
145 | int level = 0; |
---|
146 | int expected = 100; |
---|
147 | int tcpcount = 0; |
---|
148 | libtrace_t *trace,*trace2; |
---|
149 | libtrace_out_t *outtrace; |
---|
150 | libtrace_packet_t *packet,*packet2; |
---|
151 | const char *trace1name; |
---|
152 | const char *trace2name; |
---|
153 | |
---|
154 | trace = trace_create(lookup_uri(argv[1])); |
---|
155 | iferr(trace); |
---|
156 | |
---|
157 | if (strcmp(argv[1],"rtclient")==0) |
---|
158 | expected=101; |
---|
159 | |
---|
160 | outtrace = trace_create_output(lookup_out_uri(argv[2])); |
---|
161 | iferrout(outtrace); |
---|
162 | |
---|
163 | level=0; |
---|
164 | trace_config_output(outtrace,TRACE_OPTION_OUTPUT_COMPRESS,&level); |
---|
165 | if (trace_is_err_output(outtrace)) { |
---|
166 | trace_perror_output(outtrace,"WARNING: "); |
---|
167 | } |
---|
168 | |
---|
169 | trace_start(trace); |
---|
170 | iferr(trace); |
---|
171 | trace_start_output(outtrace); |
---|
172 | iferrout(outtrace); |
---|
173 | |
---|
174 | packet=trace_create_packet(); |
---|
175 | for (;;) { |
---|
176 | if ((psize = trace_read_packet(trace, packet)) <0) { |
---|
177 | error = 1; |
---|
178 | break; |
---|
179 | } |
---|
180 | if (psize == 0) { |
---|
181 | error = 0; |
---|
182 | break; |
---|
183 | } |
---|
184 | count ++; |
---|
185 | trace_write_packet(outtrace,packet); |
---|
186 | iferrout(outtrace); |
---|
187 | if (count>100) |
---|
188 | break; |
---|
189 | } |
---|
190 | trace_destroy_packet(packet); |
---|
191 | if (error == 0) { |
---|
192 | if (count != expected) { |
---|
193 | printf("failure: %d packets expected, %d seen\n",expected,count); |
---|
194 | error = 1; |
---|
195 | } |
---|
196 | } else { |
---|
197 | iferr(trace); |
---|
198 | } |
---|
199 | trace_destroy(trace); |
---|
200 | trace_destroy_output(outtrace); |
---|
201 | |
---|
202 | if (error) |
---|
203 | return error; |
---|
204 | |
---|
205 | /* Now read it back in again and check it's all kosher */ |
---|
206 | trace1name = lookup_uri(argv[1]); |
---|
207 | trace = trace_create(trace1name); |
---|
208 | iferr(trace); |
---|
209 | trace_start(trace); |
---|
210 | trace2name = lookup_out_uri(argv[2]); |
---|
211 | trace2 = trace_create(trace2name); |
---|
212 | iferr(trace2); |
---|
213 | trace_start(trace2); |
---|
214 | iferr(trace2); |
---|
215 | packet=trace_create_packet(); |
---|
216 | packet2=trace_create_packet(); |
---|
217 | count=0; |
---|
218 | tcpcount=0; |
---|
219 | while(trace_read_packet(trace,packet)>0) { |
---|
220 | int err; |
---|
221 | ++count; |
---|
222 | if ((err=trace_read_packet(trace2,packet2))<1) { |
---|
223 | printf("premature EOF on destination, %i from %s, %i from %s\n",count,lookup_uri(argv[1]),count-1,lookup_out_uri(argv[2])); |
---|
224 | iferr(trace2); |
---|
225 | error=1; |
---|
226 | break; |
---|
227 | } |
---|
228 | /* The capture length might be snapped down to the wire length */ |
---|
229 | if (length_changed(packet, packet2)) { |
---|
230 | printf("\t%s\t%s\n", |
---|
231 | trace1name, |
---|
232 | trace2name); |
---|
233 | printf("caplen\t%zd\t%zd\t%+zd\n", |
---|
234 | trace_get_capture_length(packet), |
---|
235 | trace_get_capture_length(packet2), |
---|
236 | trace_get_capture_length(packet2)-trace_get_capture_length(packet)); |
---|
237 | printf("wirelen\t%zd\t%zd\t%+zd\n", |
---|
238 | trace_get_wire_length(packet), |
---|
239 | trace_get_wire_length(packet2), |
---|
240 | trace_get_wire_length(packet2)-trace_get_wire_length(packet)); |
---|
241 | printf("link\t%d\t%d\n", |
---|
242 | trace_get_link_type(packet), |
---|
243 | trace_get_link_type(packet2)); |
---|
244 | abort(); |
---|
245 | } |
---|
246 | |
---|
247 | if (trace_get_tcp(packet)) { |
---|
248 | if (!trace_get_tcp(packet2)) { |
---|
249 | printf("trace corrupt -- expected tcp\n"); |
---|
250 | error=1; |
---|
251 | break; |
---|
252 | } |
---|
253 | ++tcpcount; |
---|
254 | } else { |
---|
255 | if (trace_get_tcp(packet2)) { |
---|
256 | printf("trace corrupt: unexpected tcp\n"); |
---|
257 | error=1; |
---|
258 | break; |
---|
259 | } |
---|
260 | } |
---|
261 | |
---|
262 | } |
---|
263 | if (trace_read_packet(trace2,packet2)>0) { |
---|
264 | printf("Extra packets after EOF\n"); |
---|
265 | error=1; |
---|
266 | } |
---|
267 | trace_destroy(trace); |
---|
268 | trace_destroy(trace2); |
---|
269 | trace_destroy_packet(packet); |
---|
270 | trace_destroy_packet(packet2); |
---|
271 | |
---|
272 | printf("tcpcount=%i\n",tcpcount); |
---|
273 | |
---|
274 | return error; |
---|
275 | } |
---|