source: test/test-dir.c @ 0ff6ddf

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

Add demotion of packets from LINUX_SLL to ethernet when writing ERF packets.
We don't need the extra header to store the direction information, and erf
doesn't have a type for linux-sll /anyway/.

This also adds a test which takes a packet, promotes it and demotes it again
which should catch direction related bugs

Closes: #21

  • Property mode set to 100644
File size: 4.6 KB
Line 
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: 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 "dagformat.h"
55#include "libtrace.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        libtrace_t *trace;
111        libtrace_out_t *outtrace;
112        libtrace_packet_t *packet;
113
114        trace = trace_create(lookup_uri("pcap"));
115        iferr(trace);
116
117        outtrace = trace_create_output(lookup_out_uri("erf"));
118        iferrout(outtrace);
119
120        level=0;
121        trace_config_output(outtrace,TRACE_OPTION_OUTPUT_COMPRESS,&level);
122        iferrout(outtrace);
123
124        trace_start(trace);
125        iferr(trace);
126        trace_start_output(outtrace);
127        iferrout(outtrace);
128       
129        packet=trace_create_packet();
130        for (;;) {
131                if ((psize = trace_read_packet(trace, packet)) <0) {
132                        error = 1;
133                        break;
134                }
135                if (psize == 0) {
136                        error = 0;
137                        break;
138                }
139                count ++;
140                /* Force promotion */
141                if (trace_get_source_port(packet)==80) {
142                        trace_set_direction(packet,TRACE_DIR_OUTGOING);
143                }
144                else {
145                        trace_set_direction(packet,TRACE_DIR_INCOMING);
146                }
147                /* And then force demotion */
148                trace_write_packet(outtrace,packet);
149                iferrout(outtrace);
150                if (count>100)
151                        break;
152        }
153        trace_destroy_packet(packet);
154        if (error == 0) {
155                if (count != expected) {
156                        printf("failure: %d packets expected, %d seen\n",expected,count);
157                        error = 1;
158                }
159        } else {
160                iferr(trace);
161        }
162        trace_destroy(trace);
163        trace_destroy_output(outtrace);
164
165        if (error)
166                return error;
167
168        /* Now read it back in again and check it's all kosher */
169        trace = trace_create(lookup_out_uri("erf"));
170        iferr(trace);
171        trace_start(trace);
172        iferr(trace);
173        packet=trace_create_packet();
174        count=0;
175        int err;
176        while((err=trace_read_packet(trace,packet))>0) {
177                ++count;
178                if (!((trace_get_source_port(packet)==80)
179                        ^ (trace_get_direction(packet)==TRACE_DIR_INCOMING))) {
180
181                        printf("Direction tagging broken\n");
182                        error=1;
183                        break;
184                }
185
186        }
187        trace_destroy(trace);
188        trace_destroy_packet(packet);
189
190        return error;
191}
Note: See TracBrowser for help on using the repository browser.