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-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 | #include "libpacketdump.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 | const char *lookup_uri(const char *type) { |
---|
62 | if (strchr(type,':') || strchr(type,'/')) |
---|
63 | return type; |
---|
64 | if (!strcmp(type,"erf")) |
---|
65 | return "erf:traces/100_packets.erf"; |
---|
66 | if (!strcmp(type,"rawerf")) |
---|
67 | return "rawerf:traces/100_packets.erf"; |
---|
68 | if (!strcmp(type,"pcap")) |
---|
69 | return "pcap:traces/100_packets.pcap"; |
---|
70 | if (!strcmp(type,"pcapng")) |
---|
71 | return "pcap:traces/100_packets.pcapng"; |
---|
72 | if (!strcmp(type,"wtf")) |
---|
73 | return "wtf:traces/wed.wtf"; |
---|
74 | if (!strcmp(type,"rtclient")) |
---|
75 | return "rtclient:chasm"; |
---|
76 | if (!strcmp(type,"pcapfile")) |
---|
77 | return "pcapfile:traces/100_packets.pcap"; |
---|
78 | if (!strcmp(type,"pcapfilens")) |
---|
79 | return "pcapfile:traces/100_packetsns.pcap"; |
---|
80 | if (!strcmp(type, "duck")) |
---|
81 | return "duck:traces/100_packets.duck"; |
---|
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, "tsh")) |
---|
89 | return "tsh:traces/10_packets.tsh.gz"; |
---|
90 | return type; |
---|
91 | } |
---|
92 | |
---|
93 | int main(int argc, char *argv[]) { |
---|
94 | int psize = 0; |
---|
95 | int error = 0; |
---|
96 | int count = 0; |
---|
97 | int level = 0; |
---|
98 | int expected = 100; |
---|
99 | libtrace_t *trace; |
---|
100 | libtrace_packet_t *packet; |
---|
101 | libtrace_filter_t *filter_tcp = trace_create_filter("tcp"); |
---|
102 | libtrace_filter_t *filter_udp = |
---|
103 | trace_create_filter("udp and ip[6:2] & 0x1fff = 0"); |
---|
104 | libtrace_filter_t *filter_icmp = trace_create_filter("icmp"); |
---|
105 | |
---|
106 | if (argc<2) { |
---|
107 | fprintf(stderr,"usage: %s type\n",argv[0]); |
---|
108 | return 1; |
---|
109 | } |
---|
110 | |
---|
111 | trace = trace_create(lookup_uri(argv[1])); |
---|
112 | iferr(trace); |
---|
113 | |
---|
114 | if (strcmp(argv[1],"rtclient")==0) expected=101; |
---|
115 | |
---|
116 | level=0; |
---|
117 | |
---|
118 | trace_start(trace); |
---|
119 | iferr(trace); |
---|
120 | |
---|
121 | packet=trace_create_packet(); |
---|
122 | for (;;) { |
---|
123 | uint8_t proto; |
---|
124 | bool lt_tcp; |
---|
125 | bool bpf_tcp; |
---|
126 | if ((psize = trace_read_packet(trace, packet)) <0) { |
---|
127 | error = 1; |
---|
128 | iferr(trace); |
---|
129 | break; |
---|
130 | } |
---|
131 | if (psize == 0) { |
---|
132 | error = 0; |
---|
133 | break; |
---|
134 | } |
---|
135 | |
---|
136 | lt_tcp = trace_get_transport(packet,&proto,NULL) && proto == 6; |
---|
137 | bpf_tcp = trace_apply_filter(filter_tcp,packet)>0; |
---|
138 | |
---|
139 | if (lt_tcp != bpf_tcp) { |
---|
140 | error=1; |
---|
141 | printf("tcp problem\n"); |
---|
142 | if (trace_get_tcp(packet)) { |
---|
143 | printf(" libtrace thinks this is a tcp packet\n"); |
---|
144 | } |
---|
145 | else { |
---|
146 | printf(" libtrace doesn't think this is a tcp packet\n"); |
---|
147 | } |
---|
148 | if (trace_apply_filter(filter_tcp,packet)) { |
---|
149 | printf(" bpf thinks this is a tcp packet\n"); |
---|
150 | } |
---|
151 | else { |
---|
152 | printf(" bpf doesn't think this is a tcp packet\n"); |
---|
153 | } |
---|
154 | trace_dump_packet(packet); |
---|
155 | break; |
---|
156 | } |
---|
157 | if ((trace_get_udp(packet)!=NULL) ^ (trace_apply_filter(filter_udp,packet)>0)) { |
---|
158 | error=1; |
---|
159 | printf("udp problem\n"); |
---|
160 | if (trace_get_udp(packet)) { |
---|
161 | printf(" libtrace thinks this is a udp packet\n"); |
---|
162 | } |
---|
163 | else { |
---|
164 | printf(" libtrace doesn't think this is a udp packet\n"); |
---|
165 | } |
---|
166 | if (trace_apply_filter(filter_udp,packet)) { |
---|
167 | printf(" bpf thinks this is a udp packet\n"); |
---|
168 | } |
---|
169 | else { |
---|
170 | printf(" bpf doesn't think this is a udp packet\n"); |
---|
171 | } |
---|
172 | trace_dump_packet(packet); |
---|
173 | break; |
---|
174 | } |
---|
175 | if ((trace_get_icmp(packet)!=NULL) ^ (trace_apply_filter(filter_icmp,packet)>0)) { |
---|
176 | error=1; |
---|
177 | printf("icmp problem\n"); |
---|
178 | trace_dump_packet(packet); |
---|
179 | break; |
---|
180 | } |
---|
181 | count ++; |
---|
182 | if (count>100) |
---|
183 | break; |
---|
184 | } |
---|
185 | trace_destroy_packet(packet); |
---|
186 | if (error == 0) { |
---|
187 | if (count == expected) { |
---|
188 | printf("success: %d packets read\n",expected); |
---|
189 | } else { |
---|
190 | printf("failure: %d packets expected, %d seen\n",expected,count); |
---|
191 | error = 1; |
---|
192 | } |
---|
193 | } else { |
---|
194 | iferr(trace); |
---|
195 | } |
---|
196 | trace_destroy(trace); |
---|
197 | return error; |
---|
198 | } |
---|