source: test/test-decode.c @ e7246a0

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

Add a test to prove that simplistic protocol decodes are working

  • Property mode set to 100644
File size: 3.7 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-rtclient.c,v 1.2 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#include <stdio.h>
41#include <stdlib.h>
42#include <assert.h>
43#include <string.h>
44#include <sys/types.h>
45#include <time.h>
46#include <string.h>
47
48#include "dagformat.h"
49#include "libtrace.h"
50
51void iferr(libtrace_t *trace)
52{
53        libtrace_err_t err = trace_get_err(trace);
54        if (err.err_num==0)
55                return;
56        printf("Error: %s\n",err.problem);
57        exit(1);
58}
59
60const char *lookup_uri(const char *type) {
61        if (strchr(type,':'))
62                return type;
63        if (!strcmp(type,"erf"))
64                return "erf:traces/100_packets.erf";
65        if (!strcmp(type,"pcap"))
66                return "pcap:traces/100_packets.pcap";
67        if (!strcmp(type,"wtf"))
68                return "wtf:traces/wed.wtf";
69        if (!strcmp(type,"rtclient"))
70                return "rtclient:chasm";
71        if (!strcmp(type,"pcapfile"))
72                return "pcapfile:traces/100_packets.pcap";
73        if (!strcmp(type, "duck"))
74                return "duck:traces/100_packets.duck";
75        return "unknown";
76}
77
78int main(int argc, char *argv[]) {
79        int psize = 0;
80        int error = 0;
81        int count = 0;
82        int level = 0;
83        int expected = 100;
84        libtrace_t *trace;
85        libtrace_packet_t *packet;
86        libtrace_filter_t *filter_tcp = trace_create_filter("tcp");
87        libtrace_filter_t *filter_udp = trace_create_filter("udp");
88        libtrace_filter_t *filter_icmp = trace_create_filter("icmp");
89
90        if (argc<2) {
91                fprintf(stderr,"usage: %s type\n",argv[0]);
92                return 1;
93        }
94
95        trace = trace_create(lookup_uri(argv[1]));
96        iferr(trace);
97
98        if (strcmp(argv[1],"rtclient")==0) expected=101;
99       
100        level=0;
101
102        trace_start(trace);
103        iferr(trace);
104       
105        packet=trace_create_packet();
106        for (;;) {
107                if ((psize = trace_read_packet(trace, packet)) <0) {
108                        error = 1;
109                        iferr(trace);
110                        break;
111                }
112                if (psize == 0) {
113                        error = 0;
114                        break;
115                }
116               
117                if ((trace_get_tcp(packet)!=NULL) ^ (trace_apply_filter(filter_tcp,packet)>0)) {
118                        error=1;
119                        printf("tcp problem\n");
120                        break;
121                }
122                if ((trace_get_udp(packet)!=NULL) ^ (trace_apply_filter(filter_udp,packet)>0)) {
123                        error=1;
124                        printf("udp problem\n");
125                        break;
126                }
127                if ((trace_get_icmp(packet)!=NULL) ^ (trace_apply_filter(filter_icmp,packet)>0)) {
128                        error=1;
129                        printf("icmp problem\n");
130                        break;
131                }
132                count ++;
133                if (count>100)
134                        break;
135        }
136        trace_destroy_packet(packet);
137        if (error == 0) {
138                if (count == expected) {
139                        printf("success: %d packets read\n",expected);
140                } else {
141                        printf("failure: %d packets expected, %d seen\n",expected,count);
142                        error = 1;
143                }
144        } else {
145                iferr(trace);
146        }
147        trace_destroy(trace);
148        return error;
149}
Note: See TracBrowser for help on using the repository browser.