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