source: test/test-dir.c

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file was a566d9c3, checked in by Shane Alcock <salcock@…>, 9 years ago
  • Add some useful debugging output if test-dir fails
  • Property mode set to 100644
File size: 4.8 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/* This test is a bit more complicated.  It takes a pcap file (which doesn't
42 * have any direction tagging), then tags it (causing a promotion), then
43 * writes it to an erf file (which should demote it again).
44 */
45
46#include <stdio.h>
47#include <stdlib.h>
48#include <assert.h>
49#include <string.h>
50#include <sys/types.h>
51#include <time.h>
52#include <string.h>
53
54#include "libtrace.h"
55#include "libpacketdump.h"
56
57void iferr(libtrace_t *trace)
58{
59        libtrace_err_t err = trace_get_err(trace);
60        if (err.err_num==0)
61                return;
62        printf("Error: %s\n",err.problem);
63        exit(1);
64}
65
66void iferrout(libtrace_out_t *trace)
67{
68        libtrace_err_t err = trace_get_err_output(trace);
69        if (err.err_num==0)
70                return;
71        printf("Error: %s\n",err.problem);
72        exit(1);
73}
74
75char *lookup_uri(const char *type) 
76{
77        if (!strcmp(type,"erf"))
78                return "erf:traces/100_packets.erf";
79        if (!strcmp(type,"pcap"))
80                return "pcap:traces/100_packets.pcap";
81        if (!strcmp(type,"wtf"))
82                return "wtf:traces/wed.wtf";
83        if (!strcmp(type,"rtclient"))
84                return "rtclient:chasm";
85        if (!strcmp(type,"pcapfile"))
86                return "pcapfile:traces/100_packets.pcap";
87        if (!strcmp(type, "duck"))
88                return "duck:traces/100_packets.duck";
89        return "unknown";
90}
91
92char *lookup_out_uri(const char *type) {
93        if (!strcmp(type,"erf"))
94                return "erf:traces/100_packets.out.erf";
95        if (!strcmp(type,"pcap"))
96                return "pcap:traces/100_packets.out.pcap";
97        if (!strcmp(type,"wtf"))
98                return "wtf:traces/wed.out.wtf";
99        if (!strcmp(type,"duck"))
100                return "duck:traces/100_packets.out.duck";
101        return "unknown";
102}
103
104int main(int argc, char *argv[]) {
105        int psize = 0;
106        int error = 0;
107        int count = 0;
108        int level = 0;
109        int expected = 100;
110        int err;
111        libtrace_t *trace;
112        libtrace_out_t *outtrace;
113        libtrace_packet_t *packet;
114
115        trace = trace_create(lookup_uri("pcap"));
116        iferr(trace);
117
118        outtrace = trace_create_output(lookup_out_uri("erf"));
119        iferrout(outtrace);
120
121        level=0;
122        trace_config_output(outtrace,TRACE_OPTION_OUTPUT_COMPRESS,&level);
123        iferrout(outtrace);
124
125        trace_start(trace);
126        iferr(trace);
127        trace_start_output(outtrace);
128        iferrout(outtrace);
129       
130        packet=trace_create_packet();
131        for (;;) {
132                if ((psize = trace_read_packet(trace, packet)) <0) {
133                        error = 1;
134                        break;
135                }
136                if (psize == 0) {
137                        error = 0;
138                        break;
139                }
140                count ++;
141                /* Force promotion */
142                if (trace_get_source_port(packet)==80) {
143                        trace_set_direction(packet,TRACE_DIR_OUTGOING);
144                        assert(trace_get_direction(packet)==TRACE_DIR_OUTGOING);
145                        assert(trace_get_source_port(packet)==80);
146                }
147                else {
148                        trace_set_direction(packet,TRACE_DIR_INCOMING);
149                        assert(trace_get_direction(packet)==TRACE_DIR_INCOMING);
150                }
151                /* And then force demotion */
152                trace_write_packet(outtrace,packet);
153                iferrout(outtrace);
154                if (count>100)
155                        break;
156        }
157        trace_destroy_packet(packet);
158        if (error == 0) {
159                if (count != expected) {
160                        printf("failure: %d packets expected, %d seen\n",expected,count);
161                        error = 1;
162                }
163        } else {
164                iferr(trace);
165        }
166        trace_destroy(trace);
167        trace_destroy_output(outtrace);
168
169        if (error)
170                return error;
171
172        /* Now read it back in again and check it's all kosher */
173        trace = trace_create(lookup_out_uri("erf"));
174        iferr(trace);
175        trace_start(trace);
176        iferr(trace);
177        packet=trace_create_packet();
178        count=0;
179        while((err=trace_read_packet(trace,packet))>0) {
180                ++count;
181                if (!((trace_get_source_port(packet)==80)
182                        ^ (trace_get_direction(packet)==TRACE_DIR_INCOMING))) {
183
184                        trace_dump_packet(packet);
185                        printf("Direction tagging broken\n");
186                        error=1;
187                        break;
188                }
189
190        }
191        trace_destroy(trace);
192        trace_destroy_packet(packet);
193
194        return error;
195}
Note: See TracBrowser for help on using the repository browser.