source: test/test-convert.c @ e111394

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since e111394 was 10cf089, checked in by Perry Lorier <perry@…>, 11 years ago

Be nicer about debug messages
Deal with packets that are snapped to capture == wirelength

  • Property mode set to 100644
File size: 6.3 KB
Line 
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
52void 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
61void 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
70char *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        return "unknown";
93}
94
95char *lookup_out_uri(const char *type) {
96        if (!strcmp(type,"erf"))
97                return "erf:traces/100_packets.out.erf";
98        if (!strcmp(type,"pcap"))
99                return "pcap:traces/100_packets.out.pcap";
100        if (!strcmp(type,"pcapfile"))
101                return "pcapfile:traces/100_packets.out.pcap";
102        if (!strcmp(type,"wtf"))
103                return "wtf:traces/wed.out.wtf";
104        if (!strcmp(type,"duck"))
105                return "duck:traces/100_packets.out.duck";
106        return "unknown";
107}
108
109int main(int argc, char *argv[]) {
110        int psize = 0;
111        int error = 0;
112        int count = 0;
113        int level = 0;
114        int expected = 100;
115        int tcpcount = 0;
116        libtrace_t *trace,*trace2;
117        libtrace_out_t *outtrace;
118        libtrace_packet_t *packet,*packet2;
119        const char *trace1name;
120        const char *trace2name;
121
122        trace = trace_create(lookup_uri(argv[1]));
123        iferr(trace);
124
125        if (strcmp(argv[1],"rtclient")==0)
126                expected=101;
127
128        outtrace = trace_create_output(lookup_out_uri(argv[2]));
129        iferrout(outtrace);
130
131        level=0;
132        trace_config_output(outtrace,TRACE_OPTION_OUTPUT_COMPRESS,&level);
133        if (trace_is_err_output(outtrace)) {
134                trace_perror_output(outtrace,"WARNING: ");
135        }
136
137        trace_start(trace);
138        iferr(trace);
139        trace_start_output(outtrace);
140        iferrout(outtrace);
141       
142        packet=trace_create_packet();
143        for (;;) {
144                if ((psize = trace_read_packet(trace, packet)) <0) {
145                        error = 1;
146                        break;
147                }
148                if (psize == 0) {
149                        error = 0;
150                        break;
151                }
152                count ++;
153                trace_write_packet(outtrace,packet);
154                iferrout(outtrace);
155                if (count>100)
156                        break;
157        }
158        trace_destroy_packet(packet);
159        if (error == 0) {
160                if (count != expected) {
161                        printf("failure: %d packets expected, %d seen\n",expected,count);
162                        error = 1;
163                }
164        } else {
165                iferr(trace);
166        }
167        trace_destroy(trace);
168        trace_destroy_output(outtrace);
169
170        if (error)
171                return error;
172
173        /* Now read it back in again and check it's all kosher */
174        trace1name = lookup_uri(argv[1]);
175        trace = trace_create(trace1name);
176        iferr(trace);
177        trace_start(trace);
178        trace2name = lookup_out_uri(argv[2]);
179        trace2 = trace_create(trace2name);
180        iferr(trace2);
181        trace_start(trace2);
182        iferr(trace2);
183        packet=trace_create_packet();
184        packet2=trace_create_packet();
185        count=0;
186        tcpcount=0;
187        while(trace_read_packet(trace,packet)>0) {
188                int err;
189                ++count;
190                if ((err=trace_read_packet(trace2,packet2))<1) {
191                        printf("premature EOF on destination, %i from %s, %i from %s\n",count,lookup_uri(argv[1]),count-1,lookup_out_uri(argv[2]));
192                        iferr(trace2);
193                        error=1;
194                        break;
195                }
196                /* The capture length might be snapped down to the wire length */
197                if (
198                ((  trace_get_capture_length(packet) != trace_get_capture_length(packet2)
199                 && trace_get_capture_length(packet2) != trace_get_wire_length(packet2)))
200                 || (trace_get_wire_length(packet) != trace_get_wire_length(packet2))) {
201                        printf("\t%s\t%s\n",
202                                trace1name,
203                                trace2name);
204                        printf("caplen\t%zd\t%zd\t%+zd\n",
205                                trace_get_capture_length(packet),
206                                trace_get_capture_length(packet2),
207                                trace_get_capture_length(packet2)-trace_get_capture_length(packet));
208                        printf("wirelen\t%zd\t%zd\t%+zd\n",
209                                trace_get_wire_length(packet),
210                                trace_get_wire_length(packet2),
211                                trace_get_wire_length(packet2)-trace_get_wire_length(packet));
212                        printf("link\t%d\t%d\n",
213                                trace_get_link_type(packet),
214                                trace_get_link_type(packet2));
215                        abort();
216                }
217       
218                if (trace_get_tcp(packet)) {
219                        if (!trace_get_tcp(packet2)) {
220                                printf("trace corrupt -- expected tcp\n");
221                                error=1;
222                                break;
223                        }
224                        ++tcpcount;
225                } else {
226                        if (trace_get_tcp(packet2)) {
227                                printf("trace corrupt: unexpected tcp\n");
228                                error=1;
229                                break;
230                        }
231                }
232
233        }
234        if (trace_read_packet(trace2,packet2)>0) {
235                printf("Extra packets after EOF\n");
236                error=1;
237        }
238        trace_destroy(trace);
239        trace_destroy(trace2);
240        trace_destroy_packet(packet);
241        trace_destroy_packet(packet2);
242
243        printf("tcpcount=%i\n",tcpcount);
244
245        return error;
246}
Note: See TracBrowser for help on using the repository browser.