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$ |
---|
28 | * |
---|
29 | */ |
---|
30 | #define _GNU_SOURCE |
---|
31 | |
---|
32 | #include "config.h" |
---|
33 | #include "common.h" |
---|
34 | #include "libtrace.h" |
---|
35 | #include "libtrace_int.h" |
---|
36 | #include "format_helper.h" |
---|
37 | |
---|
38 | #include <sys/stat.h> |
---|
39 | #include <assert.h> |
---|
40 | #include <errno.h> |
---|
41 | #include <fcntl.h> |
---|
42 | #include <stdio.h> |
---|
43 | #include <string.h> |
---|
44 | #include <stdlib.h> |
---|
45 | |
---|
46 | #ifdef WIN32 |
---|
47 | # include <io.h> |
---|
48 | # include <share.h> |
---|
49 | # define snprintf sprintf_s |
---|
50 | #endif |
---|
51 | |
---|
52 | |
---|
53 | /* Catch undefined O_LARGEFILE on *BSD etc */ |
---|
54 | #ifndef O_LARGEFILE |
---|
55 | # define O_LARGEFILE 0 |
---|
56 | #endif |
---|
57 | |
---|
58 | #define DATA(x) ((struct legacy_format_data_t *)x->format_data) |
---|
59 | |
---|
60 | #define INPUT DATA(libtrace)->input |
---|
61 | |
---|
62 | struct legacy_format_data_t { |
---|
63 | union { |
---|
64 | int fd; |
---|
65 | libtrace_io_t *file; |
---|
66 | } input; |
---|
67 | }; |
---|
68 | |
---|
69 | static int legacyeth_get_framing_length(const libtrace_packet_t *packet UNUSED) |
---|
70 | { |
---|
71 | return sizeof(legacy_ether_t); |
---|
72 | } |
---|
73 | |
---|
74 | static int legacypos_get_framing_length(const libtrace_packet_t *packet UNUSED) |
---|
75 | { |
---|
76 | return sizeof(legacy_pos_t); |
---|
77 | } |
---|
78 | |
---|
79 | static int legacyatm_get_framing_length(const libtrace_packet_t *packet UNUSED) |
---|
80 | { |
---|
81 | return sizeof(legacy_cell_t); |
---|
82 | } |
---|
83 | |
---|
84 | static int erf_init_input(libtrace_t *libtrace) |
---|
85 | { |
---|
86 | libtrace->format_data = malloc(sizeof(struct legacy_format_data_t)); |
---|
87 | |
---|
88 | DATA(libtrace)->input.file = NULL; |
---|
89 | |
---|
90 | return 0; |
---|
91 | } |
---|
92 | |
---|
93 | static int erf_start_input(libtrace_t *libtrace) |
---|
94 | { |
---|
95 | if (DATA(libtrace)->input.file) |
---|
96 | return 0; |
---|
97 | |
---|
98 | DATA(libtrace)->input.file = trace_open_file(libtrace); |
---|
99 | |
---|
100 | if (DATA(libtrace)->input.file) |
---|
101 | return 0; |
---|
102 | |
---|
103 | return -1; |
---|
104 | } |
---|
105 | |
---|
106 | static int erf_fin_input(libtrace_t *libtrace) { |
---|
107 | libtrace_io_close(INPUT.file); |
---|
108 | free(libtrace->format_data); |
---|
109 | return 0; |
---|
110 | } |
---|
111 | |
---|
112 | static int legacy_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { |
---|
113 | int numbytes; |
---|
114 | void *buffer; |
---|
115 | |
---|
116 | if (!packet->buffer || packet->buf_control == TRACE_CTRL_EXTERNAL) { |
---|
117 | packet->buf_control = TRACE_CTRL_PACKET; |
---|
118 | packet->buffer=malloc(LIBTRACE_PACKET_BUFSIZE); |
---|
119 | } |
---|
120 | buffer = packet->buffer; |
---|
121 | |
---|
122 | switch(libtrace->format->type) { |
---|
123 | case TRACE_FORMAT_LEGACY_ATM: |
---|
124 | packet->type = RT_DATA_LEGACY_ATM; |
---|
125 | break; |
---|
126 | case TRACE_FORMAT_LEGACY_POS: |
---|
127 | packet->type = RT_DATA_LEGACY_POS; |
---|
128 | break; |
---|
129 | case TRACE_FORMAT_LEGACY_ETH: |
---|
130 | packet->type = RT_DATA_LEGACY_ETH; |
---|
131 | break; |
---|
132 | default: |
---|
133 | assert(0); |
---|
134 | } |
---|
135 | |
---|
136 | if ((numbytes=libtrace_io_read(INPUT.file, |
---|
137 | buffer, |
---|
138 | 64)) != 64) { |
---|
139 | if (numbytes!=0) { |
---|
140 | trace_set_err(libtrace,errno,"read(%s)",libtrace->uridata); |
---|
141 | } |
---|
142 | return numbytes; |
---|
143 | } |
---|
144 | |
---|
145 | packet->header = packet->buffer; |
---|
146 | packet->payload = (void*)((char*)packet->buffer + |
---|
147 | libtrace->format->get_framing_length(packet)); |
---|
148 | |
---|
149 | return 64; |
---|
150 | |
---|
151 | } |
---|
152 | |
---|
153 | static libtrace_linktype_t legacypos_get_link_type(const libtrace_packet_t *packet UNUSED) { |
---|
154 | return TRACE_TYPE_POS; |
---|
155 | } |
---|
156 | |
---|
157 | static libtrace_linktype_t legacyatm_get_link_type(const libtrace_packet_t *packet UNUSED) { |
---|
158 | return TRACE_TYPE_ATM; |
---|
159 | } |
---|
160 | |
---|
161 | static libtrace_linktype_t legacyeth_get_link_type(const libtrace_packet_t *packet UNUSED) { |
---|
162 | return TRACE_TYPE_ETH; |
---|
163 | } |
---|
164 | |
---|
165 | static int legacy_get_capture_length(const libtrace_packet_t *packet UNUSED) { |
---|
166 | return 64; |
---|
167 | } |
---|
168 | |
---|
169 | static int legacypos_get_wire_length(const libtrace_packet_t *packet) { |
---|
170 | legacy_pos_t *lpos = (legacy_pos_t *)packet->header; |
---|
171 | assert(ntohl(lpos->wlen)>0); |
---|
172 | return ntohl(lpos->wlen); |
---|
173 | } |
---|
174 | |
---|
175 | static int legacyatm_get_wire_length(const libtrace_packet_t *packet UNUSED) { |
---|
176 | return 53; |
---|
177 | } |
---|
178 | |
---|
179 | static int legacyeth_get_wire_length(const libtrace_packet_t *packet) { |
---|
180 | legacy_ether_t *leth = (legacy_ether_t *)packet->header; |
---|
181 | return ntohs(leth->wlen); |
---|
182 | } |
---|
183 | |
---|
184 | static uint64_t legacy_get_erf_timestamp(const libtrace_packet_t *packet) |
---|
185 | { |
---|
186 | legacy_ether_t *legacy = (legacy_ether_t*)packet->header; |
---|
187 | return legacy->ts; |
---|
188 | } |
---|
189 | |
---|
190 | static void legacypos_help(void) { |
---|
191 | printf("legacypos format module: $Revision$\n"); |
---|
192 | printf("Supported input URIs:\n"); |
---|
193 | printf("\tlegacypos:/path/to/file\t(uncompressed)\n"); |
---|
194 | printf("\tlegacypos:/path/to/file.gz\t(gzip-compressed)\n"); |
---|
195 | printf("\tlegacypos:-\t(stdin, either compressed or not)\n"); |
---|
196 | printf("\n"); |
---|
197 | printf("\te.g.: legacypos:/tmp/trace.gz\n"); |
---|
198 | printf("\n"); |
---|
199 | } |
---|
200 | |
---|
201 | static void legacyatm_help(void) { |
---|
202 | printf("legacyatm format module: $Revision$\n"); |
---|
203 | printf("Supported input URIs:\n"); |
---|
204 | printf("\tlegacyatm:/path/to/file\t(uncompressed)\n"); |
---|
205 | printf("\tlegacyatm:/path/to/file.gz\t(gzip-compressed)\n"); |
---|
206 | printf("\tlegacyatm:-\t(stdin, either compressed or not)\n"); |
---|
207 | printf("\n"); |
---|
208 | printf("\te.g.: legacyatm:/tmp/trace.gz\n"); |
---|
209 | printf("\n"); |
---|
210 | } |
---|
211 | |
---|
212 | static void legacyeth_help(void) { |
---|
213 | printf("legacyeth format module: $Revision$\n"); |
---|
214 | printf("Supported input URIs:\n"); |
---|
215 | printf("\tlegacyeth:/path/to/file\t(uncompressed)\n"); |
---|
216 | printf("\tlegacyeth:/path/to/file.gz\t(gzip-compressed)\n"); |
---|
217 | printf("\tlegacyeth:-\t(stdin, either compressed or not)\n"); |
---|
218 | printf("\n"); |
---|
219 | printf("\te.g.: legacyeth:/tmp/trace.gz\n"); |
---|
220 | printf("\n"); |
---|
221 | } |
---|
222 | |
---|
223 | static struct libtrace_format_t legacyatm = { |
---|
224 | "legacyatm", |
---|
225 | "$Id$", |
---|
226 | TRACE_FORMAT_LEGACY_ATM, |
---|
227 | erf_init_input, /* init_input */ |
---|
228 | NULL, /* config_input */ |
---|
229 | erf_start_input, /* start_input */ |
---|
230 | NULL, /* pause_input */ |
---|
231 | NULL, /* init_output */ |
---|
232 | NULL, /* config_output */ |
---|
233 | NULL, /* start_output */ |
---|
234 | erf_fin_input, /* fin_input */ |
---|
235 | NULL, /* fin_output */ |
---|
236 | legacy_read_packet, /* read_packet */ |
---|
237 | NULL, /* fin_packet */ |
---|
238 | NULL, /* write_packet */ |
---|
239 | legacyatm_get_link_type, /* get_link_type */ |
---|
240 | NULL, /* get_direction */ |
---|
241 | NULL, /* set_direction */ |
---|
242 | legacy_get_erf_timestamp, /* get_erf_timestamp */ |
---|
243 | NULL, /* get_timeval */ |
---|
244 | NULL, /* get_seconds */ |
---|
245 | NULL, /* seek_erf */ |
---|
246 | NULL, /* seek_timeval */ |
---|
247 | NULL, /* seek_seconds */ |
---|
248 | legacy_get_capture_length, /* get_capture_length */ |
---|
249 | legacyatm_get_wire_length, /* get_wire_length */ |
---|
250 | legacyatm_get_framing_length, /* get_framing_length */ |
---|
251 | NULL, /* set_capture_length */ |
---|
252 | NULL, /* get_fd */ |
---|
253 | trace_event_trace, /* trace_event */ |
---|
254 | legacyatm_help, /* help */ |
---|
255 | NULL /* next pointer */ |
---|
256 | }; |
---|
257 | |
---|
258 | static struct libtrace_format_t legacyeth = { |
---|
259 | "legacyeth", |
---|
260 | "$Id$", |
---|
261 | TRACE_FORMAT_LEGACY_ETH, |
---|
262 | erf_init_input, /* init_input */ |
---|
263 | NULL, /* config_input */ |
---|
264 | erf_start_input, /* start_input */ |
---|
265 | NULL, /* pause_input */ |
---|
266 | NULL, /* init_output */ |
---|
267 | NULL, /* config_output */ |
---|
268 | NULL, /* start_output */ |
---|
269 | erf_fin_input, /* fin_input */ |
---|
270 | NULL, /* fin_output */ |
---|
271 | legacy_read_packet, /* read_packet */ |
---|
272 | NULL, /* fin_packet */ |
---|
273 | NULL, /* write_packet */ |
---|
274 | legacyeth_get_link_type, /* get_link_type */ |
---|
275 | NULL, /* get_direction */ |
---|
276 | NULL, /* set_direction */ |
---|
277 | legacy_get_erf_timestamp, /* get_erf_timestamp */ |
---|
278 | NULL, /* get_timeval */ |
---|
279 | NULL, /* get_seconds */ |
---|
280 | NULL, /* seek_erf */ |
---|
281 | NULL, /* seek_timeval */ |
---|
282 | NULL, /* seek_seconds */ |
---|
283 | legacy_get_capture_length, /* get_capture_length */ |
---|
284 | legacyeth_get_wire_length, /* get_wire_length */ |
---|
285 | legacyeth_get_framing_length, /* get_framing_length */ |
---|
286 | NULL, /* set_capture_length */ |
---|
287 | NULL, /* get_fd */ |
---|
288 | trace_event_trace, /* trace_event */ |
---|
289 | legacyeth_help, /* help */ |
---|
290 | NULL /* next pointer */ |
---|
291 | }; |
---|
292 | |
---|
293 | static struct libtrace_format_t legacypos = { |
---|
294 | "legacypos", |
---|
295 | "$Id$", |
---|
296 | TRACE_FORMAT_LEGACY_POS, |
---|
297 | erf_init_input, /* init_input */ |
---|
298 | NULL, /* config_input */ |
---|
299 | erf_start_input, /* start_input */ |
---|
300 | NULL, /* pause_input */ |
---|
301 | NULL, /* init_output */ |
---|
302 | NULL, /* config_output */ |
---|
303 | NULL, /* start_output */ |
---|
304 | erf_fin_input, /* fin_input */ |
---|
305 | NULL, /* fin_output */ |
---|
306 | legacy_read_packet, /* read_packet */ |
---|
307 | NULL, /* fin_packet */ |
---|
308 | NULL, /* write_packet */ |
---|
309 | legacypos_get_link_type, /* get_link_type */ |
---|
310 | NULL, /* get_direction */ |
---|
311 | NULL, /* set_direction */ |
---|
312 | legacy_get_erf_timestamp, /* get_erf_timestamp */ |
---|
313 | NULL, /* get_timeval */ |
---|
314 | NULL, /* get_seconds */ |
---|
315 | NULL, /* seek_erf */ |
---|
316 | NULL, /* seek_timeval */ |
---|
317 | NULL, /* seek_seconds */ |
---|
318 | legacy_get_capture_length, /* get_capture_length */ |
---|
319 | legacypos_get_wire_length, /* get_wire_length */ |
---|
320 | legacypos_get_framing_length, /* get_framing_length */ |
---|
321 | NULL, /* set_capture_length */ |
---|
322 | NULL, /* get_fd */ |
---|
323 | trace_event_trace, /* trace_event */ |
---|
324 | legacypos_help, /* help */ |
---|
325 | NULL, /* next pointer */ |
---|
326 | }; |
---|
327 | |
---|
328 | |
---|
329 | void legacy_constructor(void) { |
---|
330 | register_format(&legacypos); |
---|
331 | register_format(&legacyeth); |
---|
332 | register_format(&legacyatm); |
---|
333 | } |
---|