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 "format_erf.h" |
---|
38 | |
---|
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 PATH_MAX _MAX_PATH |
---|
50 | #else |
---|
51 | # include <netdb.h> |
---|
52 | # ifndef PATH_MAX |
---|
53 | # define PATH_MAX 4096 |
---|
54 | # endif |
---|
55 | # include <sys/ioctl.h> |
---|
56 | #endif |
---|
57 | |
---|
58 | |
---|
59 | #define COLLECTOR_PORT 3435 |
---|
60 | |
---|
61 | static struct libtrace_format_t erfformat; |
---|
62 | |
---|
63 | #define DATA(x) ((struct erf_format_data_t *)x->format_data) |
---|
64 | #define DATAOUT(x) ((struct erf_format_data_out_t *)x->format_data) |
---|
65 | |
---|
66 | #define INPUT DATA(libtrace)->input |
---|
67 | #define OUTPUT DATAOUT(libtrace)->output |
---|
68 | #define OPTIONS DATAOUT(libtrace)->options |
---|
69 | struct erf_format_data_t { |
---|
70 | |
---|
71 | union { |
---|
72 | int fd; |
---|
73 | libtrace_io_t *file; |
---|
74 | } input; |
---|
75 | |
---|
76 | struct { |
---|
77 | enum { INDEX_UNKNOWN=0, INDEX_NONE, INDEX_EXISTS } exists; |
---|
78 | libtrace_io_t *index; |
---|
79 | off_t index_len; |
---|
80 | } seek; |
---|
81 | |
---|
82 | }; |
---|
83 | |
---|
84 | struct erf_format_data_out_t { |
---|
85 | union { |
---|
86 | struct { |
---|
87 | int level; |
---|
88 | int fileflag; |
---|
89 | } erf; |
---|
90 | |
---|
91 | } options; |
---|
92 | |
---|
93 | union { |
---|
94 | int fd; |
---|
95 | struct rtserver_t * rtserver; |
---|
96 | libtrace_io_t *file; |
---|
97 | } output; |
---|
98 | }; |
---|
99 | |
---|
100 | /** Structure holding status information for a packet */ |
---|
101 | typedef struct libtrace_packet_status { |
---|
102 | uint8_t type; |
---|
103 | uint8_t reserved; |
---|
104 | uint16_t message; |
---|
105 | } libtrace_packet_status_t; |
---|
106 | |
---|
107 | typedef struct erf_index_t { |
---|
108 | uint64_t timestamp; |
---|
109 | uint64_t offset; |
---|
110 | } erf_index_t; |
---|
111 | |
---|
112 | |
---|
113 | /* Dag erf ether packets have a 2 byte padding before the packet |
---|
114 | * so that the ip header is aligned on a 32 bit boundary. |
---|
115 | */ |
---|
116 | static int erf_get_padding(const libtrace_packet_t *packet) |
---|
117 | { |
---|
118 | if (packet->trace->format->type==TRACE_FORMAT_ERF) { |
---|
119 | dag_record_t *erfptr = (dag_record_t *)packet->header; |
---|
120 | switch(erfptr->type) { |
---|
121 | case TYPE_ETH: return 2; |
---|
122 | default: return 0; |
---|
123 | } |
---|
124 | } |
---|
125 | else { |
---|
126 | switch(trace_get_link_type(packet)) { |
---|
127 | case TYPE_ETH: return 2; |
---|
128 | default: return 0; |
---|
129 | } |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | int erf_get_framing_length(const libtrace_packet_t *packet) |
---|
134 | { |
---|
135 | return dag_record_size + erf_get_padding(packet); |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | static int erf_init_input(libtrace_t *libtrace) |
---|
140 | { |
---|
141 | libtrace->format_data = malloc(sizeof(struct erf_format_data_t)); |
---|
142 | |
---|
143 | INPUT.file = 0; |
---|
144 | |
---|
145 | return 0; /* success */ |
---|
146 | } |
---|
147 | |
---|
148 | static int erf_start_input(libtrace_t *libtrace) |
---|
149 | { |
---|
150 | if (INPUT.file) |
---|
151 | return 0; /* success */ |
---|
152 | |
---|
153 | INPUT.file = trace_open_file(libtrace); |
---|
154 | |
---|
155 | if (!INPUT.file) |
---|
156 | return -1; |
---|
157 | |
---|
158 | return 0; /* success */ |
---|
159 | } |
---|
160 | |
---|
161 | /* Binary search through the index to find the closest point before |
---|
162 | * the packet. Consider in future having a btree index perhaps? |
---|
163 | */ |
---|
164 | static int erf_fast_seek_start(libtrace_t *libtrace,uint64_t erfts) |
---|
165 | { |
---|
166 | size_t max_off = DATA(libtrace)->seek.index_len/sizeof(erf_index_t); |
---|
167 | size_t min_off = 0; |
---|
168 | off_t current; |
---|
169 | erf_index_t record; |
---|
170 | do { |
---|
171 | current=(max_off+min_off)>>2; |
---|
172 | |
---|
173 | libtrace_io_seek(DATA(libtrace)->seek.index, |
---|
174 | (int64_t)(current*sizeof(record)), |
---|
175 | SEEK_SET); |
---|
176 | libtrace_io_read(DATA(libtrace)->seek.index, |
---|
177 | &record,sizeof(record)); |
---|
178 | if (record.timestamp < erfts) { |
---|
179 | min_off=current; |
---|
180 | } |
---|
181 | if (record.timestamp > erfts) { |
---|
182 | max_off=current; |
---|
183 | } |
---|
184 | if (record.timestamp == erfts) |
---|
185 | break; |
---|
186 | } while(min_off<max_off); |
---|
187 | |
---|
188 | /* If we've passed it, seek backwards. This loop shouldn't |
---|
189 | * execute more than twice. |
---|
190 | */ |
---|
191 | do { |
---|
192 | libtrace_io_seek(DATA(libtrace)->seek.index, |
---|
193 | (int64_t)(current*sizeof(record)),SEEK_SET); |
---|
194 | libtrace_io_read(DATA(libtrace)->seek.index, |
---|
195 | &record,sizeof(record)); |
---|
196 | current--; |
---|
197 | } while(record.timestamp>erfts); |
---|
198 | |
---|
199 | /* We've found our location in the trace, now use it. */ |
---|
200 | libtrace_io_seek(INPUT.file,(int64_t) record.offset,SEEK_SET); |
---|
201 | |
---|
202 | return 0; /* success */ |
---|
203 | } |
---|
204 | |
---|
205 | /* There is no index. Seek through the entire trace from the start, nice |
---|
206 | * and slowly. |
---|
207 | */ |
---|
208 | static int erf_slow_seek_start(libtrace_t *libtrace,uint64_t erfts UNUSED) |
---|
209 | { |
---|
210 | if (INPUT.file) { |
---|
211 | libtrace_io_close(INPUT.file); |
---|
212 | } |
---|
213 | INPUT.file = trace_open_file(libtrace); |
---|
214 | if (!INPUT.file) |
---|
215 | return -1; |
---|
216 | return 0; |
---|
217 | } |
---|
218 | |
---|
219 | static int erf_seek_erf(libtrace_t *libtrace,uint64_t erfts) |
---|
220 | { |
---|
221 | libtrace_packet_t *packet; |
---|
222 | off_t off = 0; |
---|
223 | |
---|
224 | if (DATA(libtrace)->seek.exists==INDEX_UNKNOWN) { |
---|
225 | char buffer[PATH_MAX]; |
---|
226 | snprintf(buffer,sizeof(buffer),"%s.idx",libtrace->uridata); |
---|
227 | DATA(libtrace)->seek.index=libtrace_io_open(buffer,"rb"); |
---|
228 | if (DATA(libtrace)->seek.index) { |
---|
229 | DATA(libtrace)->seek.exists=INDEX_EXISTS; |
---|
230 | } |
---|
231 | else { |
---|
232 | DATA(libtrace)->seek.exists=INDEX_NONE; |
---|
233 | } |
---|
234 | } |
---|
235 | |
---|
236 | /* If theres an index, use it to find the nearest packet that isn't |
---|
237 | * after the time we're looking for. If there is no index we need |
---|
238 | * to seek slowly through the trace from the beginning. Sigh. |
---|
239 | */ |
---|
240 | switch(DATA(libtrace)->seek.exists) { |
---|
241 | case INDEX_EXISTS: |
---|
242 | erf_fast_seek_start(libtrace,erfts); |
---|
243 | break; |
---|
244 | case INDEX_NONE: |
---|
245 | erf_slow_seek_start(libtrace,erfts); |
---|
246 | break; |
---|
247 | case INDEX_UNKNOWN: |
---|
248 | assert(0); |
---|
249 | break; |
---|
250 | } |
---|
251 | |
---|
252 | /* Now seek forward looking for the correct timestamp */ |
---|
253 | packet=trace_create_packet(); |
---|
254 | do { |
---|
255 | trace_read_packet(libtrace,packet); |
---|
256 | if (trace_get_erf_timestamp(packet)==erfts) |
---|
257 | break; |
---|
258 | off=libtrace_io_tell(INPUT.file); |
---|
259 | } while(trace_get_erf_timestamp(packet)<erfts); |
---|
260 | |
---|
261 | libtrace_io_seek(INPUT.file,off,SEEK_SET); |
---|
262 | |
---|
263 | return 0; |
---|
264 | } |
---|
265 | |
---|
266 | static int erf_init_output(libtrace_out_t *libtrace) { |
---|
267 | libtrace->format_data = malloc(sizeof(struct erf_format_data_out_t)); |
---|
268 | |
---|
269 | OPTIONS.erf.level = 0; |
---|
270 | OPTIONS.erf.fileflag = O_CREAT | O_WRONLY; |
---|
271 | OUTPUT.file = 0; |
---|
272 | |
---|
273 | return 0; |
---|
274 | } |
---|
275 | |
---|
276 | static int erf_config_output(libtrace_out_t *libtrace, trace_option_output_t option, |
---|
277 | void *value) { |
---|
278 | |
---|
279 | switch (option) { |
---|
280 | case TRACE_OPTION_OUTPUT_COMPRESS: |
---|
281 | OPTIONS.erf.level = *(int*)value; |
---|
282 | return 0; |
---|
283 | case TRACE_OPTION_OUTPUT_FILEFLAGS: |
---|
284 | OPTIONS.erf.fileflag = *(int*)value; |
---|
285 | return 0; |
---|
286 | default: |
---|
287 | /* Unknown option */ |
---|
288 | trace_set_err_out(libtrace,TRACE_ERR_UNKNOWN_OPTION, |
---|
289 | "Unknown option"); |
---|
290 | return -1; |
---|
291 | } |
---|
292 | } |
---|
293 | |
---|
294 | |
---|
295 | |
---|
296 | static int erf_fin_input(libtrace_t *libtrace) { |
---|
297 | if (INPUT.file) |
---|
298 | libtrace_io_close(INPUT.file); |
---|
299 | free(libtrace->format_data); |
---|
300 | return 0; |
---|
301 | } |
---|
302 | |
---|
303 | static int erf_fin_output(libtrace_out_t *libtrace) { |
---|
304 | libtrace_io_close(OUTPUT.file); |
---|
305 | free(libtrace->format_data); |
---|
306 | return 0; |
---|
307 | } |
---|
308 | |
---|
309 | |
---|
310 | static int erf_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { |
---|
311 | int numbytes; |
---|
312 | unsigned int size; |
---|
313 | void *buffer2 = packet->buffer; |
---|
314 | unsigned int rlen; |
---|
315 | |
---|
316 | if (!packet->buffer || packet->buf_control == TRACE_CTRL_EXTERNAL) { |
---|
317 | packet->buffer = malloc((size_t)LIBTRACE_PACKET_BUFSIZE); |
---|
318 | packet->buf_control = TRACE_CTRL_PACKET; |
---|
319 | if (!packet->buffer) { |
---|
320 | trace_set_err(libtrace, errno, |
---|
321 | "Cannot allocate memory"); |
---|
322 | return -1; |
---|
323 | } |
---|
324 | } |
---|
325 | |
---|
326 | |
---|
327 | |
---|
328 | packet->header = packet->buffer; |
---|
329 | packet->type = TRACE_RT_DATA_ERF; |
---|
330 | |
---|
331 | if ((numbytes=libtrace_io_read(INPUT.file, |
---|
332 | packet->buffer, |
---|
333 | (size_t)dag_record_size)) == -1) { |
---|
334 | trace_set_err(libtrace,errno,"read(%s)", |
---|
335 | libtrace->uridata); |
---|
336 | return -1; |
---|
337 | } |
---|
338 | /* EOF */ |
---|
339 | if (numbytes == 0) { |
---|
340 | return 0; |
---|
341 | } |
---|
342 | |
---|
343 | rlen = ntohs(((dag_record_t *)packet->buffer)->rlen); |
---|
344 | buffer2 = (char*)packet->buffer + dag_record_size; |
---|
345 | size = rlen - dag_record_size; |
---|
346 | |
---|
347 | assert(size < LIBTRACE_PACKET_BUFSIZE); |
---|
348 | |
---|
349 | /* Unknown/corrupt */ |
---|
350 | assert(((dag_record_t *)packet->buffer)->type < 10); |
---|
351 | |
---|
352 | /* read in the rest of the packet */ |
---|
353 | if ((numbytes=libtrace_io_read(INPUT.file, |
---|
354 | buffer2, |
---|
355 | (size_t)size)) != (int)size) { |
---|
356 | if (numbytes==-1) { |
---|
357 | trace_set_err(libtrace,errno, "read(%s)", libtrace->uridata); |
---|
358 | return -1; |
---|
359 | } |
---|
360 | trace_set_err(libtrace,EIO,"Truncated packet (wanted %d, got %d)", size, numbytes); |
---|
361 | /* Failed to read the full packet? must be EOF */ |
---|
362 | return -1; |
---|
363 | } |
---|
364 | if (((dag_record_t *)packet->buffer)->flags.rxerror == 1) { |
---|
365 | packet->payload = NULL; |
---|
366 | } else { |
---|
367 | packet->payload = (char*)packet->buffer + erf_get_framing_length(packet); |
---|
368 | } |
---|
369 | return rlen; |
---|
370 | } |
---|
371 | |
---|
372 | static int erf_dump_packet(libtrace_out_t *libtrace, |
---|
373 | dag_record_t *erfptr, unsigned int pad, void *buffer) { |
---|
374 | int numbytes = 0; |
---|
375 | int size; |
---|
376 | |
---|
377 | if ((numbytes = |
---|
378 | libtrace_io_write(OUTPUT.file, |
---|
379 | erfptr, |
---|
380 | (size_t)(dag_record_size + pad))) |
---|
381 | != (int)(dag_record_size+pad)) { |
---|
382 | trace_set_err_out(libtrace,errno, |
---|
383 | "write(%s)",libtrace->uridata); |
---|
384 | return -1; |
---|
385 | } |
---|
386 | |
---|
387 | size=ntohs(erfptr->rlen)-(dag_record_size+pad); |
---|
388 | numbytes=libtrace_io_write(OUTPUT.file, buffer, (size_t)size); |
---|
389 | if (numbytes != size) { |
---|
390 | trace_set_err_out(libtrace,errno, |
---|
391 | "write(%s)",libtrace->uridata); |
---|
392 | return -1; |
---|
393 | } |
---|
394 | return numbytes + pad + dag_record_size; |
---|
395 | } |
---|
396 | |
---|
397 | static int erf_start_output(libtrace_out_t *libtrace) |
---|
398 | { |
---|
399 | OUTPUT.file = trace_open_file_out(libtrace, |
---|
400 | OPTIONS.erf.level, |
---|
401 | OPTIONS.erf.fileflag); |
---|
402 | if (!OUTPUT.file) { |
---|
403 | return -1; |
---|
404 | } |
---|
405 | return 0; |
---|
406 | } |
---|
407 | |
---|
408 | static bool find_compatible_linktype(libtrace_out_t *libtrace, |
---|
409 | libtrace_packet_t *packet) |
---|
410 | { |
---|
411 | /* Keep trying to simplify the packet until we can find |
---|
412 | * something we can do with it */ |
---|
413 | do { |
---|
414 | char type=libtrace_to_erf_type(trace_get_link_type(packet)); |
---|
415 | |
---|
416 | /* Success */ |
---|
417 | if (type != (char)-1) |
---|
418 | return true; |
---|
419 | |
---|
420 | if (!demote_packet(packet)) { |
---|
421 | trace_set_err_out(libtrace, |
---|
422 | TRACE_ERR_NO_CONVERSION, |
---|
423 | "No erf type for packet (%i)", |
---|
424 | trace_get_link_type(packet)); |
---|
425 | return false; |
---|
426 | } |
---|
427 | |
---|
428 | } while(1); |
---|
429 | |
---|
430 | return true; |
---|
431 | } |
---|
432 | |
---|
433 | static int erf_write_packet(libtrace_out_t *libtrace, |
---|
434 | libtrace_packet_t *packet) |
---|
435 | { |
---|
436 | int numbytes = 0; |
---|
437 | unsigned int pad = 0; |
---|
438 | dag_record_t *dag_hdr = (dag_record_t *)packet->header; |
---|
439 | void *payload = packet->payload; |
---|
440 | |
---|
441 | assert(OUTPUT.file); |
---|
442 | |
---|
443 | if (!packet->header) { |
---|
444 | /*trace_set_err_output(libtrace, TRACE_ERR_BAD_PACKET, |
---|
445 | "Packet has no header - probably an RT packet"); |
---|
446 | */ |
---|
447 | return -1; |
---|
448 | } |
---|
449 | |
---|
450 | pad = erf_get_padding(packet); |
---|
451 | |
---|
452 | /* If we've had an rxerror, we have no payload to write - fix |
---|
453 | * rlen to be the correct length |
---|
454 | */ |
---|
455 | /* I Think this is bogus, we should somehow figure out |
---|
456 | * a way to write out the payload even if it is gibberish -- Perry */ |
---|
457 | if (payload == NULL) { |
---|
458 | dag_hdr->rlen = htons(dag_record_size + pad); |
---|
459 | |
---|
460 | } |
---|
461 | |
---|
462 | if (packet->type == TRACE_RT_DATA_ERF) { |
---|
463 | numbytes = erf_dump_packet(libtrace, |
---|
464 | (dag_record_t *)packet->header, |
---|
465 | pad, |
---|
466 | payload |
---|
467 | ); |
---|
468 | } else { |
---|
469 | dag_record_t erfhdr; |
---|
470 | /* convert format - build up a new erf header */ |
---|
471 | /* Timestamp */ |
---|
472 | erfhdr.ts = bswap_host_to_le64(trace_get_erf_timestamp(packet)); |
---|
473 | |
---|
474 | /* Flags. Can't do this */ |
---|
475 | memset(&erfhdr.flags,1,sizeof(erfhdr.flags)); |
---|
476 | if (trace_get_direction(packet)!=~0U) |
---|
477 | erfhdr.flags.iface = trace_get_direction(packet); |
---|
478 | |
---|
479 | if (!find_compatible_linktype(libtrace,packet)) |
---|
480 | return -1; |
---|
481 | |
---|
482 | payload=packet->payload; |
---|
483 | pad = erf_get_padding(packet); |
---|
484 | |
---|
485 | erfhdr.type = libtrace_to_erf_type(trace_get_link_type(packet)); |
---|
486 | |
---|
487 | /* Packet length (rlen includes format overhead) */ |
---|
488 | assert(trace_get_capture_length(packet)>0 |
---|
489 | && trace_get_capture_length(packet)<=65536); |
---|
490 | assert(erf_get_framing_length(packet)>0 |
---|
491 | && trace_get_framing_length(packet)<=65536); |
---|
492 | assert( |
---|
493 | trace_get_capture_length(packet)+erf_get_framing_length(packet)>0 |
---|
494 | &&trace_get_capture_length(packet)+erf_get_framing_length(packet)<=65536); |
---|
495 | erfhdr.rlen = htons(trace_get_capture_length(packet) |
---|
496 | + erf_get_framing_length(packet)); |
---|
497 | /* loss counter. Can't do this */ |
---|
498 | erfhdr.lctr = 0; |
---|
499 | /* Wire length, does not include padding! */ |
---|
500 | erfhdr.wlen = htons(trace_get_wire_length(packet)); |
---|
501 | |
---|
502 | /* Write it out */ |
---|
503 | numbytes = erf_dump_packet(libtrace, |
---|
504 | &erfhdr, |
---|
505 | pad, |
---|
506 | payload); |
---|
507 | } |
---|
508 | return numbytes; |
---|
509 | } |
---|
510 | |
---|
511 | libtrace_linktype_t erf_get_link_type(const libtrace_packet_t *packet) { |
---|
512 | dag_record_t *erfptr = 0; |
---|
513 | erfptr = (dag_record_t *)packet->header; |
---|
514 | return erf_type_to_libtrace(erfptr->type); |
---|
515 | } |
---|
516 | |
---|
517 | libtrace_direction_t erf_get_direction(const libtrace_packet_t *packet) { |
---|
518 | dag_record_t *erfptr = 0; |
---|
519 | erfptr = (dag_record_t *)packet->header; |
---|
520 | return erfptr->flags.iface; |
---|
521 | } |
---|
522 | |
---|
523 | libtrace_direction_t erf_set_direction(libtrace_packet_t *packet, libtrace_direction_t direction) { |
---|
524 | dag_record_t *erfptr = 0; |
---|
525 | erfptr = (dag_record_t *)packet->header; |
---|
526 | erfptr->flags.iface = direction; |
---|
527 | return erfptr->flags.iface; |
---|
528 | } |
---|
529 | |
---|
530 | uint64_t erf_get_erf_timestamp(const libtrace_packet_t *packet) { |
---|
531 | dag_record_t *erfptr = 0; |
---|
532 | erfptr = (dag_record_t *)packet->header; |
---|
533 | return bswap_le_to_host64(erfptr->ts); |
---|
534 | } |
---|
535 | |
---|
536 | int erf_get_capture_length(const libtrace_packet_t *packet) { |
---|
537 | dag_record_t *erfptr = 0; |
---|
538 | int caplen; |
---|
539 | if (packet->payload == NULL) |
---|
540 | return 0; |
---|
541 | |
---|
542 | erfptr = (dag_record_t *)packet->header; |
---|
543 | caplen = ntohs(erfptr->rlen) - erf_get_framing_length(packet); |
---|
544 | if (ntohs(erfptr->wlen) < caplen) |
---|
545 | return ntohs(erfptr->wlen); |
---|
546 | |
---|
547 | return (ntohs(erfptr->rlen) - erf_get_framing_length(packet)); |
---|
548 | } |
---|
549 | |
---|
550 | int erf_get_wire_length(const libtrace_packet_t *packet) { |
---|
551 | dag_record_t *erfptr = 0; |
---|
552 | erfptr = (dag_record_t *)packet->header; |
---|
553 | return ntohs(erfptr->wlen); |
---|
554 | } |
---|
555 | |
---|
556 | size_t erf_set_capture_length(libtrace_packet_t *packet, size_t size) { |
---|
557 | dag_record_t *erfptr = 0; |
---|
558 | assert(packet); |
---|
559 | if(size > trace_get_capture_length(packet)) { |
---|
560 | /* can't make a packet larger */ |
---|
561 | return trace_get_capture_length(packet); |
---|
562 | } |
---|
563 | erfptr = (dag_record_t *)packet->header; |
---|
564 | erfptr->rlen = htons(size + erf_get_framing_length(packet)); |
---|
565 | return trace_get_capture_length(packet); |
---|
566 | } |
---|
567 | |
---|
568 | static void erf_help(void) { |
---|
569 | printf("erf format module: $Revision$\n"); |
---|
570 | printf("Supported input URIs:\n"); |
---|
571 | printf("\terf:/path/to/file\t(uncompressed)\n"); |
---|
572 | printf("\terf:/path/to/file.gz\t(gzip-compressed)\n"); |
---|
573 | printf("\terf:-\t(stdin, either compressed or not)\n"); |
---|
574 | printf("\terf:/path/to/socket\n"); |
---|
575 | printf("\n"); |
---|
576 | printf("\te.g.: erf:/tmp/trace\n"); |
---|
577 | printf("\n"); |
---|
578 | printf("Supported output URIs:\n"); |
---|
579 | printf("\terf:path/to/file\t(uncompressed)\n"); |
---|
580 | printf("\terf:/path/to/file.gz\t(gzip-compressed)\n"); |
---|
581 | printf("\terf:-\t(stdout, either compressed or not)\n"); |
---|
582 | printf("\n"); |
---|
583 | printf("\te.g.: erf:/tmp/trace\n"); |
---|
584 | printf("\n"); |
---|
585 | printf("Supported output options:\n"); |
---|
586 | printf("\t-z\tSpecify the gzip compression, ranging from 0 (uncompressed) to 9 - defaults to 1\n"); |
---|
587 | printf("\n"); |
---|
588 | |
---|
589 | |
---|
590 | } |
---|
591 | |
---|
592 | static struct libtrace_format_t erfformat = { |
---|
593 | "erf", |
---|
594 | "$Id$", |
---|
595 | TRACE_FORMAT_ERF, |
---|
596 | erf_init_input, /* init_input */ |
---|
597 | NULL, /* config_input */ |
---|
598 | erf_start_input, /* start_input */ |
---|
599 | NULL, /* pause_input */ |
---|
600 | erf_init_output, /* init_output */ |
---|
601 | erf_config_output, /* config_output */ |
---|
602 | erf_start_output, /* start_output */ |
---|
603 | erf_fin_input, /* fin_input */ |
---|
604 | erf_fin_output, /* fin_output */ |
---|
605 | erf_read_packet, /* read_packet */ |
---|
606 | NULL, /* fin_packet */ |
---|
607 | erf_write_packet, /* write_packet */ |
---|
608 | erf_get_link_type, /* get_link_type */ |
---|
609 | erf_get_direction, /* get_direction */ |
---|
610 | erf_set_direction, /* set_direction */ |
---|
611 | erf_get_erf_timestamp, /* get_erf_timestamp */ |
---|
612 | NULL, /* get_timeval */ |
---|
613 | NULL, /* get_seconds */ |
---|
614 | erf_seek_erf, /* seek_erf */ |
---|
615 | NULL, /* seek_timeval */ |
---|
616 | NULL, /* seek_seconds */ |
---|
617 | erf_get_capture_length, /* get_capture_length */ |
---|
618 | erf_get_wire_length, /* get_wire_length */ |
---|
619 | erf_get_framing_length, /* get_framing_length */ |
---|
620 | erf_set_capture_length, /* set_capture_length */ |
---|
621 | NULL, /* get_fd */ |
---|
622 | trace_event_trace, /* trace_event */ |
---|
623 | erf_help, /* help */ |
---|
624 | NULL /* next pointer */ |
---|
625 | }; |
---|
626 | |
---|
627 | |
---|
628 | void erf_constructor(void) { |
---|
629 | register_format(&erfformat); |
---|
630 | } |
---|