1 | /* |
---|
2 | * This file is part of libtrace |
---|
3 | * |
---|
4 | * Copyright (c) 2007 The University of Waikato, Hamilton, New Zealand. |
---|
5 | * Authors: Richard Sanger |
---|
6 | * |
---|
7 | * All rights reserved. |
---|
8 | * |
---|
9 | * This code has been developed by the University of Waikato WAND |
---|
10 | * research group. For further information please see http://www.wand.net.nz/ |
---|
11 | * |
---|
12 | * libtrace is free software; you can redistribute it and/or modify |
---|
13 | * it under the terms of the GNU General Public License as published by |
---|
14 | * the Free Software Foundation; either version 2 of the License, or |
---|
15 | * (at your option) any later version. |
---|
16 | * |
---|
17 | * libtrace is distributed in the hope that it will be useful, |
---|
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
20 | * GNU General Public License for more details. |
---|
21 | * |
---|
22 | * You should have received a copy of the GNU General Public License |
---|
23 | * along with libtrace; if not, write to the Free Software |
---|
24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
25 | * |
---|
26 | * $Id$ |
---|
27 | * |
---|
28 | */ |
---|
29 | |
---|
30 | |
---|
31 | #include <stdio.h> |
---|
32 | #include <stdlib.h> |
---|
33 | #include <assert.h> |
---|
34 | #include <string.h> |
---|
35 | #include <sys/time.h> |
---|
36 | #include <sys/types.h> |
---|
37 | #include <time.h> |
---|
38 | #include <math.h> |
---|
39 | #include <signal.h> |
---|
40 | #include <unistd.h> |
---|
41 | |
---|
42 | #include <netinet/in.h> |
---|
43 | #include <netinet/in_systm.h> |
---|
44 | #include <netinet/tcp.h> |
---|
45 | #include <netinet/ip.h> |
---|
46 | #include <netinet/ip_icmp.h> |
---|
47 | #include <arpa/inet.h> |
---|
48 | #include <sys/socket.h> |
---|
49 | #include "dagformat.h" |
---|
50 | #include "libtrace.h" |
---|
51 | |
---|
52 | #define ERROR(mesg, ...) { \ |
---|
53 | err = 1; \ |
---|
54 | fprintf(stderr, "%s Error: " mesg, uri_read, __VA_ARGS__); \ |
---|
55 | } |
---|
56 | |
---|
57 | static const char *uri_read; |
---|
58 | static libtrace_t *trace_read; |
---|
59 | |
---|
60 | static const char *lookup_uri_write(const char *type) |
---|
61 | { |
---|
62 | if (!strcmp(type, "int")) |
---|
63 | return "int:veth0"; |
---|
64 | if (!strcmp(type, "ring")) |
---|
65 | return "ring:veth0"; |
---|
66 | if (!strcmp(type, "pcapint")) |
---|
67 | return "pcapint:veth0"; |
---|
68 | if (!strncmp(type, "dpdk:", sizeof("dpdk:"))) |
---|
69 | return type; |
---|
70 | return "unknown"; |
---|
71 | } |
---|
72 | |
---|
73 | static const char *lookup_uri_read(const char *type) |
---|
74 | { |
---|
75 | if (!strcmp(type, "int")) |
---|
76 | return "int:veth1"; |
---|
77 | if (!strcmp(type, "ring")) |
---|
78 | return "ring:veth1"; |
---|
79 | if (!strcmp(type, "pcapint")) |
---|
80 | return "pcapint:veth1"; |
---|
81 | if (!strncmp(type, "dpdk:", sizeof("dpdk:"))) |
---|
82 | return type; |
---|
83 | return "unknown"; |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | /** |
---|
88 | * Source packet we modify this every write see build_packet |
---|
89 | */ |
---|
90 | static unsigned char buffer1[] = { |
---|
91 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, /* Dest Mac */ |
---|
92 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x06, /* Src Mac */ |
---|
93 | 0x01, 0x01, /* Ethertype = Experimental */ |
---|
94 | 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, /* payload */ |
---|
95 | }; |
---|
96 | |
---|
97 | static unsigned char buffer2[] = { |
---|
98 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, /* Dest Mac */ |
---|
99 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x06, /* Src Mac */ |
---|
100 | 0x01, 0x01, /* Ethertype = Experimental */ |
---|
101 | 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, /* payload */ |
---|
102 | 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, /* payload */ |
---|
103 | 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, /* payload */ |
---|
104 | 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, /* payload */ |
---|
105 | 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, /* payload */ |
---|
106 | 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, /* payload */ |
---|
107 | 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, /* payload */ |
---|
108 | 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, /* payload */ |
---|
109 | 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, /* payload */ |
---|
110 | 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, /* payload */ |
---|
111 | }; |
---|
112 | |
---|
113 | |
---|
114 | static void signal_handler(int signal) |
---|
115 | { |
---|
116 | if (signal == SIGALRM) { |
---|
117 | fprintf(stderr, "!!!Failed due to Timeout!!!\n"); |
---|
118 | exit(-1); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * Verifies a packet matches with what we expected |
---|
124 | */ |
---|
125 | static int verify_packet(libtrace_packet_t *packet, size_t expected_size) |
---|
126 | { |
---|
127 | int err = 0; |
---|
128 | |
---|
129 | // Verify wirelen - Wirelen includes checksum of 4 bytes |
---|
130 | if (trace_get_wire_length(packet) != expected_size + 4) { |
---|
131 | ERROR("trace_get_wire_length() incorrect, read %zu expected %zu\n", |
---|
132 | trace_get_wire_length(packet), expected_size + 4); |
---|
133 | } |
---|
134 | // Verify caplen is no more than we asked for, this is allowed |
---|
135 | // to include the CRC sum but should still be snapped off if applicable |
---|
136 | if (trace_get_capture_length(packet) == MIN(expected_size, 30)) { |
---|
137 | // Good |
---|
138 | } else if (trace_get_capture_length(packet) == MIN(expected_size+4, 30)) { |
---|
139 | // Good |
---|
140 | } else { |
---|
141 | ERROR("trace_get_capture_length() incorrect, read %zu expected %zu (or %zu)\n", |
---|
142 | trace_get_capture_length(packet), MIN(expected_size, 30), MIN(expected_size+4, 30)); |
---|
143 | } |
---|
144 | return err; |
---|
145 | } |
---|
146 | |
---|
147 | static void iferr_out(libtrace_out_t *trace) |
---|
148 | { |
---|
149 | libtrace_err_t err = trace_get_err_output(trace); |
---|
150 | if (err.err_num == 0) |
---|
151 | return; |
---|
152 | printf("Error: %s\n", err.problem); |
---|
153 | exit(1); |
---|
154 | } |
---|
155 | |
---|
156 | static void iferr(libtrace_t *trace) |
---|
157 | { |
---|
158 | libtrace_err_t err = trace_get_err(trace); |
---|
159 | if (err.err_num == 0) |
---|
160 | return; |
---|
161 | printf("Error: %s\n", err.problem); |
---|
162 | exit(1); |
---|
163 | } |
---|
164 | |
---|
165 | int main(int argc, char *argv[]) |
---|
166 | { |
---|
167 | libtrace_out_t *trace_write; |
---|
168 | libtrace_packet_t *packet; |
---|
169 | int psize; |
---|
170 | int err = 0; |
---|
171 | int opt; |
---|
172 | |
---|
173 | if (argc < 3) { |
---|
174 | fprintf(stderr, "usage: %s type(write) type(read)\n", argv[0]); |
---|
175 | return 1; |
---|
176 | } |
---|
177 | |
---|
178 | signal(SIGALRM, signal_handler); |
---|
179 | // Timeout after 5 seconds |
---|
180 | alarm(5); |
---|
181 | |
---|
182 | trace_write = trace_create_output(lookup_uri_write(argv[1])); |
---|
183 | iferr_out(trace_write); |
---|
184 | uri_read = lookup_uri_read(argv[2]); |
---|
185 | trace_read = trace_create(uri_read); |
---|
186 | iferr(trace_read); |
---|
187 | |
---|
188 | // Set snaplen to 30 bytes |
---|
189 | opt = 30; |
---|
190 | if (trace_config(trace_read, TRACE_OPTION_SNAPLEN, &opt) != 0) |
---|
191 | iferr(trace_read); |
---|
192 | |
---|
193 | trace_start_output(trace_write); |
---|
194 | iferr_out(trace_write); |
---|
195 | trace_start(trace_read); |
---|
196 | iferr(trace_read); |
---|
197 | |
---|
198 | packet = trace_create_packet(); |
---|
199 | |
---|
200 | // Write out buffer1 and buffer2 (only buffer 2 should be snapped) |
---|
201 | trace_construct_packet(packet, TRACE_TYPE_ETH, buffer1, sizeof(buffer1)); |
---|
202 | if (trace_write_packet(trace_write, packet) == -1) { |
---|
203 | iferr_out(trace_write); |
---|
204 | } |
---|
205 | trace_construct_packet(packet, TRACE_TYPE_ETH, buffer2, sizeof(buffer2)); |
---|
206 | if (trace_write_packet(trace_write, packet) == -1) { |
---|
207 | iferr_out(trace_write); |
---|
208 | } |
---|
209 | trace_destroy_packet(packet); |
---|
210 | trace_destroy_output(trace_write); |
---|
211 | |
---|
212 | // Now read the 2 packets back in |
---|
213 | packet = trace_create_packet(); |
---|
214 | |
---|
215 | if ((psize = trace_read_packet(trace_read, packet)) < 0) { |
---|
216 | iferr(trace_read); |
---|
217 | // EOF we shouldn't hit this with a live format |
---|
218 | fprintf(stderr, "Error: looks like we lost some packets!\n"); |
---|
219 | err = 1; |
---|
220 | } else { |
---|
221 | err |= verify_packet(packet, sizeof(buffer1)); |
---|
222 | if ((psize = trace_read_packet(trace_read, packet)) < 0) { |
---|
223 | iferr(trace_read); |
---|
224 | // EOF we shouldn't hit this with a live format |
---|
225 | fprintf(stderr, "Error: looks like we lost some packets!\n"); |
---|
226 | err = 1; |
---|
227 | } else { |
---|
228 | err |= verify_packet(packet, sizeof(buffer2)); |
---|
229 | } |
---|
230 | } |
---|
231 | |
---|
232 | trace_destroy_packet(packet); |
---|
233 | trace_destroy(trace_read); |
---|
234 | |
---|
235 | return err; |
---|
236 | } |
---|