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 | #define RT_MSG 2 |
---|
33 | #define RT_DATA 1 |
---|
34 | |
---|
35 | #include "config.h" |
---|
36 | #include "common.h" |
---|
37 | #include "libtrace.h" |
---|
38 | #include "libtrace_int.h" |
---|
39 | #include "format_helper.h" |
---|
40 | #include "parse_cmd.h" |
---|
41 | |
---|
42 | #ifdef HAVE_INTTYPES_H |
---|
43 | # include <inttypes.h> |
---|
44 | #else |
---|
45 | # error "Can't find inttypes.h - this needs to be fixed" |
---|
46 | #endif |
---|
47 | |
---|
48 | #ifdef HAVE_STDDEF_H |
---|
49 | # include <stddef.h> |
---|
50 | #else |
---|
51 | # error "Can't find stddef.h - do you define ptrdiff_t elsewhere?" |
---|
52 | #endif |
---|
53 | #include <sys/types.h> |
---|
54 | #include <sys/socket.h> |
---|
55 | #include <sys/un.h> |
---|
56 | #include <sys/mman.h> |
---|
57 | #include <sys/stat.h> |
---|
58 | #include <unistd.h> |
---|
59 | #include <assert.h> |
---|
60 | #include <errno.h> |
---|
61 | #include <netdb.h> |
---|
62 | #include <fcntl.h> |
---|
63 | #include <getopt.h> |
---|
64 | #include <stdio.h> |
---|
65 | #include <string.h> |
---|
66 | #include <stdlib.h> |
---|
67 | |
---|
68 | |
---|
69 | #define COLLECTOR_PORT 3435 |
---|
70 | |
---|
71 | static struct libtrace_format_t erf; |
---|
72 | static struct libtrace_format_t rtclient; |
---|
73 | #if HAVE_DAG |
---|
74 | static struct libtrace_format_t dag; |
---|
75 | #endif |
---|
76 | |
---|
77 | #define CONNINFO libtrace->format_data->conn_info |
---|
78 | #define INPUT libtrace->format_data->input |
---|
79 | #define OUTPUT libtrace->format_data->output |
---|
80 | #if HAVE_DAG |
---|
81 | #define DAG libtrace->format_data->dag |
---|
82 | #endif |
---|
83 | #define OPTIONS libtrace->format_data->options |
---|
84 | struct libtrace_format_data_t { |
---|
85 | union { |
---|
86 | struct { |
---|
87 | char *hostname; |
---|
88 | short port; |
---|
89 | } rt; |
---|
90 | } conn_info; |
---|
91 | |
---|
92 | union { |
---|
93 | int fd; |
---|
94 | LIBTRACE_FILE file; |
---|
95 | } input; |
---|
96 | |
---|
97 | #if HAVE_DAG |
---|
98 | struct { |
---|
99 | void *buf; |
---|
100 | unsigned bottom; |
---|
101 | unsigned top; |
---|
102 | unsigned diff; |
---|
103 | unsigned curr; |
---|
104 | unsigned offset; |
---|
105 | } dag; |
---|
106 | #endif |
---|
107 | }; |
---|
108 | |
---|
109 | struct libtrace_format_data_out_t { |
---|
110 | union { |
---|
111 | struct { |
---|
112 | char *hostname; |
---|
113 | short port; |
---|
114 | } rt; |
---|
115 | char *path; |
---|
116 | } conn_info; |
---|
117 | |
---|
118 | |
---|
119 | |
---|
120 | union { |
---|
121 | struct { |
---|
122 | int level; |
---|
123 | int fileflag; |
---|
124 | } erf; |
---|
125 | |
---|
126 | } options; |
---|
127 | |
---|
128 | union { |
---|
129 | int fd; |
---|
130 | struct rtserver_t * rtserver; |
---|
131 | #if HAVE_ZLIB |
---|
132 | gzFile *file; |
---|
133 | #else |
---|
134 | int file; |
---|
135 | #endif |
---|
136 | } output; |
---|
137 | }; |
---|
138 | |
---|
139 | /** Structure holding status information for a packet */ |
---|
140 | typedef struct libtrace_packet_status { |
---|
141 | uint8_t type; |
---|
142 | uint8_t reserved; |
---|
143 | uint16_t message; |
---|
144 | } libtrace_packet_status_t; |
---|
145 | |
---|
146 | |
---|
147 | #ifdef HAVE_DAG |
---|
148 | static int dag_init_input(struct libtrace_t *libtrace) { |
---|
149 | libtrace->format_data = (struct libtrace_format_data_t *) |
---|
150 | malloc(sizeof(struct libtrace_format_data_t)); |
---|
151 | } |
---|
152 | |
---|
153 | static int dag_start_input(struct libtrace_t *libtrace) { |
---|
154 | struct stat buf; |
---|
155 | if (stat(packet->trace->uridata) == -1) { |
---|
156 | trace_set_err(errno,"stat(%s)",libtrace->uridata); |
---|
157 | return 0; |
---|
158 | } |
---|
159 | if (S_ISCHR(buf.st_mode)) { |
---|
160 | /* DEVICE */ |
---|
161 | if((INPUT.fd = dag_open(packet->trace->uridata)) < 0) { |
---|
162 | trace_set_err(errno,"Cannot open DAG %s", |
---|
163 | packet->trace->uridata); |
---|
164 | return 0; |
---|
165 | } |
---|
166 | if((DAG.buf = (void *)dag_mmap(INPUT.fd)) == MAP_FAILED) { |
---|
167 | trace_set_err(errno,"Cannot mmap DAG %s", |
---|
168 | packet->trace->uridata); |
---|
169 | return 0; |
---|
170 | } |
---|
171 | if(dag_start(INPUT.fd) < 0) { |
---|
172 | trace_set_err(errno,"Cannot start DAG %s", |
---|
173 | packet->trace->uridata); |
---|
174 | return 0; |
---|
175 | } |
---|
176 | } else { |
---|
177 | trace_set_err(errno,"Not a valid dag device: %s", |
---|
178 | packet->trace->uridata); |
---|
179 | return 0; |
---|
180 | } |
---|
181 | return 1; |
---|
182 | } |
---|
183 | #endif |
---|
184 | |
---|
185 | /* Dag erf ether packets have a 2 byte padding before the packet |
---|
186 | * so that the ip header is aligned on a 32 bit boundary. |
---|
187 | */ |
---|
188 | static int erf_get_padding(const struct libtrace_packet_t *packet) |
---|
189 | { |
---|
190 | switch(trace_get_link_type(packet)) { |
---|
191 | case TRACE_TYPE_ETH: return 2; |
---|
192 | default: return 0; |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | static int erf_get_framing_length(const struct libtrace_packet_t *packet) |
---|
197 | { |
---|
198 | return dag_record_size + erf_get_padding(packet); |
---|
199 | } |
---|
200 | |
---|
201 | |
---|
202 | static int erf_init_input(struct libtrace_t *libtrace) |
---|
203 | { |
---|
204 | libtrace->format_data = (struct libtrace_format_data_t *) |
---|
205 | malloc(sizeof(struct libtrace_format_data_t)); |
---|
206 | |
---|
207 | } |
---|
208 | |
---|
209 | static int erf_start_input(struct libtrace_t *libtrace) |
---|
210 | { |
---|
211 | libtrace->format_data->input.file = trace_open_file(libtrace); |
---|
212 | |
---|
213 | if (libtrace->format_data->input.file) |
---|
214 | return 1; |
---|
215 | |
---|
216 | return 0; |
---|
217 | } |
---|
218 | |
---|
219 | static int rtclient_init_input(struct libtrace_t *libtrace) { |
---|
220 | char *scan; |
---|
221 | char *uridata = libtrace->uridata; |
---|
222 | struct hostent *he; |
---|
223 | struct sockaddr_in remote; |
---|
224 | libtrace->format_data = (struct libtrace_format_data_t *) |
---|
225 | malloc(sizeof(struct libtrace_format_data_t)); |
---|
226 | |
---|
227 | |
---|
228 | if (strlen(uridata) == 0) { |
---|
229 | CONNINFO.rt.hostname = |
---|
230 | strdup("localhost"); |
---|
231 | CONNINFO.rt.port = |
---|
232 | COLLECTOR_PORT; |
---|
233 | } else { |
---|
234 | if ((scan = strchr(uridata,':')) == NULL) { |
---|
235 | CONNINFO.rt.hostname = |
---|
236 | strdup(uridata); |
---|
237 | CONNINFO.rt.port = |
---|
238 | COLLECTOR_PORT; |
---|
239 | } else { |
---|
240 | CONNINFO.rt.hostname = |
---|
241 | (char *)strndup(uridata, |
---|
242 | (scan - uridata)); |
---|
243 | CONNINFO.rt.port = |
---|
244 | atoi(++scan); |
---|
245 | } |
---|
246 | } |
---|
247 | |
---|
248 | if ((he=gethostbyname(CONNINFO.rt.hostname)) == NULL) { |
---|
249 | trace_set_err(errno,"failed to resolve %s", |
---|
250 | CONNINFO.rt.hostname); |
---|
251 | return 0; |
---|
252 | } |
---|
253 | if ((INPUT.fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { |
---|
254 | trace_set_err(errno,"socket(AF_INET,SOCK_STREAM)"); |
---|
255 | return 0; |
---|
256 | } |
---|
257 | |
---|
258 | remote.sin_family = AF_INET; |
---|
259 | remote.sin_port = htons(CONNINFO.rt.port); |
---|
260 | remote.sin_addr = *((struct in_addr *)he->h_addr); |
---|
261 | bzero(&(remote.sin_zero), 8); |
---|
262 | |
---|
263 | if (connect(INPUT.fd, (struct sockaddr *)&remote, |
---|
264 | sizeof(struct sockaddr)) == -1) { |
---|
265 | trace_set_err(errno,"connect(%s)", |
---|
266 | CONNINFO.rt.hostname); |
---|
267 | return 0; |
---|
268 | } |
---|
269 | return 1; |
---|
270 | } |
---|
271 | |
---|
272 | static int erf_init_output(struct libtrace_out_t *libtrace) { |
---|
273 | libtrace->format_data = (struct libtrace_format_data_out_t *) |
---|
274 | calloc(1,sizeof(struct libtrace_format_data_out_t)); |
---|
275 | |
---|
276 | OPTIONS.erf.level = 0; |
---|
277 | OPTIONS.erf.fileflag = O_CREAT | O_LARGEFILE | O_WRONLY; |
---|
278 | OUTPUT.file = 0; |
---|
279 | |
---|
280 | return 1; |
---|
281 | } |
---|
282 | |
---|
283 | static int erf_config_output(struct libtrace_out_t *libtrace, trace_option_t option, void *value) { |
---|
284 | |
---|
285 | switch (option) { |
---|
286 | case TRACE_OPTION_OUTPUT_COMPRESS: |
---|
287 | OPTIONS.erf.level = *(int*)value; |
---|
288 | return 0; |
---|
289 | case TRACE_OPTION_OUTPUT_FILEFLAGS: |
---|
290 | OPTIONS.erf.fileflag = *(int*)value; |
---|
291 | return 0; |
---|
292 | default: |
---|
293 | /* Unknown option */ |
---|
294 | trace_set_err(TRACE_ERR_UNKNOWN_OPTION, |
---|
295 | "Unknown option"); |
---|
296 | return -1; |
---|
297 | } |
---|
298 | } |
---|
299 | |
---|
300 | |
---|
301 | #ifdef HAVE_DAG |
---|
302 | static int dag_fin_input(struct libtrace_t *libtrace) { |
---|
303 | dag_stop(INPUT.fd); |
---|
304 | } |
---|
305 | #endif |
---|
306 | |
---|
307 | static int erf_fin_input(struct libtrace_t *libtrace) { |
---|
308 | LIBTRACE_CLOSE(INPUT.file); |
---|
309 | free(libtrace->format_data); |
---|
310 | return 0; |
---|
311 | } |
---|
312 | |
---|
313 | static int rtclient_fin_input(struct libtrace_t *libtrace) { |
---|
314 | close(INPUT.fd); |
---|
315 | return 0; |
---|
316 | } |
---|
317 | |
---|
318 | static int erf_fin_output(struct libtrace_out_t *libtrace) { |
---|
319 | LIBTRACE_CLOSE(OUTPUT.file); |
---|
320 | free(libtrace->format_data); |
---|
321 | |
---|
322 | return 0; |
---|
323 | } |
---|
324 | |
---|
325 | #if HAVE_DAG |
---|
326 | static int dag_read(struct libtrace_t *libtrace, int block_flag) { |
---|
327 | int numbytes; |
---|
328 | static short lctr = 0; |
---|
329 | struct dag_record_t *erfptr = 0; |
---|
330 | int rlen; |
---|
331 | |
---|
332 | if (DAG.diff != 0) |
---|
333 | return DAG.diff; |
---|
334 | |
---|
335 | DAG.bottom = DAG.top; |
---|
336 | DAG.top = dag_offset( |
---|
337 | INPUT.fd, |
---|
338 | &(DAG.bottom), |
---|
339 | block_flag); |
---|
340 | DAG.diff = DAG.top - |
---|
341 | DAG.bottom; |
---|
342 | |
---|
343 | numbytes=DAG.diff; |
---|
344 | DAG.offset = 0; |
---|
345 | return numbytes; |
---|
346 | } |
---|
347 | #endif |
---|
348 | |
---|
349 | #if HAVE_DAG |
---|
350 | static int dag_read_packet(struct libtrace_t *libtrace, struct libtrace_packet_t *packet) { |
---|
351 | int numbytes; |
---|
352 | int size; |
---|
353 | dag_record_t *erfptr; |
---|
354 | void *buffer = packet->buffer; |
---|
355 | void *buffer2 = buffer; |
---|
356 | int rlen; |
---|
357 | |
---|
358 | if (packet->buf_control == PACKET) { |
---|
359 | packet->buf_control = EXTERNAL; |
---|
360 | free(packet->buffer); |
---|
361 | packet->buffer = 0; |
---|
362 | } |
---|
363 | |
---|
364 | if ((numbytes = dag_read(libtrace,0)) <= 0) |
---|
365 | return numbytes; |
---|
366 | |
---|
367 | /*DAG always gives us whole packets */ |
---|
368 | erfptr = (dag_record_t *) ((void *)DAG.buf + |
---|
369 | (DAG.bottom + DAG.offset)); |
---|
370 | size = ntohs(erfptr->rlen); |
---|
371 | |
---|
372 | if ( size > LIBTRACE_PACKET_BUFSIZE) { |
---|
373 | assert( size < LIBTRACE_PACKET_BUFSIZE); |
---|
374 | } |
---|
375 | |
---|
376 | packet->buffer = erfptr; |
---|
377 | packet->header = erfptr; |
---|
378 | if (((dag_record_t *)buffer)->flags.rxerror == 1) { |
---|
379 | packet->payload = NULL; |
---|
380 | } else { |
---|
381 | packet->payload = packet->buffer + erf_get_framing_length(packet); |
---|
382 | } |
---|
383 | |
---|
384 | packet->size = size; |
---|
385 | DAG.offset += size; |
---|
386 | DAG.diff -= size; |
---|
387 | |
---|
388 | assert(DAG.diff >= 0); |
---|
389 | |
---|
390 | return (size); |
---|
391 | } |
---|
392 | #endif |
---|
393 | |
---|
394 | static int erf_read_packet(struct libtrace_t *libtrace, struct libtrace_packet_t *packet) { |
---|
395 | int numbytes; |
---|
396 | int size; |
---|
397 | void *buffer2 = packet->buffer; |
---|
398 | int rlen; |
---|
399 | |
---|
400 | if (!packet->buffer) { |
---|
401 | packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE); |
---|
402 | packet->buf_control = PACKET; |
---|
403 | } |
---|
404 | |
---|
405 | packet->header = packet->buffer; |
---|
406 | |
---|
407 | if ((numbytes=LIBTRACE_READ(INPUT.file, |
---|
408 | packet->buffer, |
---|
409 | dag_record_size)) == -1) { |
---|
410 | trace_set_err(errno,"read(%s)", |
---|
411 | packet->trace->uridata); |
---|
412 | return -1; |
---|
413 | } |
---|
414 | if (numbytes == 0) { |
---|
415 | return 0; |
---|
416 | } |
---|
417 | |
---|
418 | rlen = ntohs(((dag_record_t *)packet->buffer)->rlen); |
---|
419 | buffer2 = (char*)packet->buffer + dag_record_size; |
---|
420 | size = rlen - dag_record_size; |
---|
421 | assert(size < LIBTRACE_PACKET_BUFSIZE); |
---|
422 | |
---|
423 | |
---|
424 | /* Unknown/corrupt */ |
---|
425 | assert(((dag_record_t *)packet->buffer)->type < 10); |
---|
426 | |
---|
427 | /* read in the rest of the packet */ |
---|
428 | if ((numbytes=LIBTRACE_READ(INPUT.file, |
---|
429 | buffer2, |
---|
430 | size)) != size) { |
---|
431 | trace_set_err(errno, "read(%s)", packet->trace->uridata); |
---|
432 | return -1; |
---|
433 | } |
---|
434 | packet->size = rlen; |
---|
435 | if (((dag_record_t *)packet->buffer)->flags.rxerror == 1) { |
---|
436 | packet->payload = NULL; |
---|
437 | } else { |
---|
438 | packet->payload = (char*)packet->buffer + erf_get_framing_length(packet); |
---|
439 | } |
---|
440 | return rlen; |
---|
441 | } |
---|
442 | |
---|
443 | static int rtclient_read(struct libtrace_t *libtrace, void *buffer, size_t len) { |
---|
444 | int numbytes; |
---|
445 | |
---|
446 | if (buffer == 0) |
---|
447 | buffer = malloc(len); |
---|
448 | while(1) { |
---|
449 | #ifndef MSG_NOSIGNAL |
---|
450 | # define MSG_NOSIGNAL 0 |
---|
451 | #endif |
---|
452 | if ((numbytes = recv(INPUT.fd, |
---|
453 | buffer, |
---|
454 | len, |
---|
455 | MSG_NOSIGNAL)) == -1) { |
---|
456 | if (errno == EINTR) { |
---|
457 | /*ignore EINTR in case |
---|
458 | *a caller is using signals |
---|
459 | */ |
---|
460 | continue; |
---|
461 | } |
---|
462 | trace_set_err(errno,"recv(%s)", |
---|
463 | libtrace->uridata); |
---|
464 | return -1; |
---|
465 | } |
---|
466 | break; |
---|
467 | |
---|
468 | } |
---|
469 | return numbytes; |
---|
470 | } |
---|
471 | |
---|
472 | static int rtclient_read_packet(struct libtrace_t *libtrace, struct libtrace_packet_t *packet) { |
---|
473 | int numbytes = 0; |
---|
474 | char buf[RP_BUFSIZE]; |
---|
475 | int read_required = 0; |
---|
476 | |
---|
477 | void *buffer = 0; |
---|
478 | |
---|
479 | packet->trace = libtrace; |
---|
480 | |
---|
481 | if (packet->buf_control == EXTERNAL) { |
---|
482 | packet->buf_control = PACKET; |
---|
483 | packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE); |
---|
484 | } |
---|
485 | |
---|
486 | buffer = packet->buffer; |
---|
487 | packet->header = packet->buffer; |
---|
488 | |
---|
489 | |
---|
490 | do { |
---|
491 | struct libtrace_packet_status status; |
---|
492 | if (tracefifo_out_available(libtrace->fifo) == 0 || read_required) { |
---|
493 | if ((numbytes = rtclient_read( |
---|
494 | libtrace,buf,RP_BUFSIZE))<=0) { |
---|
495 | return numbytes; |
---|
496 | } |
---|
497 | tracefifo_write(libtrace->fifo,buf,numbytes); |
---|
498 | read_required = 0; |
---|
499 | } |
---|
500 | /* Read status byte */ |
---|
501 | if (tracefifo_out_read(libtrace->fifo, |
---|
502 | &status, sizeof(uint32_t)) == 0) { |
---|
503 | read_required = 1; |
---|
504 | continue; |
---|
505 | } |
---|
506 | tracefifo_out_update(libtrace->fifo,sizeof(uint32_t)); |
---|
507 | /* Read in packet size */ |
---|
508 | if (tracefifo_out_read(libtrace->fifo, |
---|
509 | &packet->size, sizeof(uint32_t)) == 0) { |
---|
510 | tracefifo_out_reset(libtrace->fifo); |
---|
511 | read_required = 1; |
---|
512 | continue; |
---|
513 | } |
---|
514 | tracefifo_out_update(libtrace->fifo, sizeof(uint32_t)); |
---|
515 | |
---|
516 | if (status.type == RT_MSG) { |
---|
517 | /* Need to skip this packet as it is a message packet */ |
---|
518 | tracefifo_out_update(libtrace->fifo, packet->size); |
---|
519 | tracefifo_ack_update(libtrace->fifo, packet->size + |
---|
520 | sizeof(uint32_t) + |
---|
521 | sizeof(libtrace_packet_status_t)); |
---|
522 | continue; |
---|
523 | } |
---|
524 | |
---|
525 | /* read in the full packet */ |
---|
526 | if ((numbytes = tracefifo_out_read(libtrace->fifo, |
---|
527 | buffer, packet->size)) == 0) { |
---|
528 | tracefifo_out_reset(libtrace->fifo); |
---|
529 | read_required = 1; |
---|
530 | continue; |
---|
531 | } |
---|
532 | |
---|
533 | /* got in our whole packet, so... */ |
---|
534 | tracefifo_out_update(libtrace->fifo,packet->size); |
---|
535 | |
---|
536 | tracefifo_ack_update(libtrace->fifo,packet->size + |
---|
537 | sizeof(uint32_t) + |
---|
538 | sizeof(libtrace_packet_status_t)); |
---|
539 | |
---|
540 | if (((dag_record_t *)buffer)->flags.rxerror == 1) { |
---|
541 | packet->payload = NULL; |
---|
542 | } else { |
---|
543 | packet->payload = (char*)packet->buffer + erf_get_framing_length(packet); |
---|
544 | } |
---|
545 | return numbytes; |
---|
546 | } while(1); |
---|
547 | } |
---|
548 | |
---|
549 | static int erf_dump_packet(struct libtrace_out_t *libtrace, |
---|
550 | dag_record_t *erfptr, int pad, void *buffer, size_t size) { |
---|
551 | int numbytes = 0; |
---|
552 | if ((numbytes = LIBTRACE_WRITE(OUTPUT.file, erfptr, dag_record_size + pad)) == 0) { |
---|
553 | trace_set_err(errno,"write(%s)",libtrace->uridata); |
---|
554 | return -1; |
---|
555 | } |
---|
556 | |
---|
557 | if ((numbytes=LIBTRACE_WRITE(OUTPUT.file, buffer, size)) == 0) { |
---|
558 | trace_set_err(errno,"write(%s)",libtrace->uridata); |
---|
559 | return -1; |
---|
560 | } |
---|
561 | |
---|
562 | return numbytes + pad + dag_record_size; |
---|
563 | } |
---|
564 | |
---|
565 | static int erf_start_output(libtrace_out_t *libtrace) |
---|
566 | { |
---|
567 | OUTPUT.file = trace_open_file_out(libtrace, |
---|
568 | OPTIONS.erf.level, |
---|
569 | OPTIONS.erf.fileflag); |
---|
570 | if (!OUTPUT.file) { |
---|
571 | printf("%s\n",trace_err.problem); |
---|
572 | return -1; |
---|
573 | } |
---|
574 | return 0; |
---|
575 | } |
---|
576 | |
---|
577 | static int erf_write_packet(libtrace_out_t *libtrace, |
---|
578 | const libtrace_packet_t *packet) |
---|
579 | { |
---|
580 | int numbytes = 0; |
---|
581 | dag_record_t erfhdr; |
---|
582 | int pad = 0; |
---|
583 | dag_record_t *dag_hdr = (dag_record_t *)packet->header; |
---|
584 | void *payload = packet->payload; |
---|
585 | |
---|
586 | assert(OUTPUT.file); |
---|
587 | |
---|
588 | pad = erf_get_padding(packet); |
---|
589 | |
---|
590 | /* If we've had an rxerror, we have no payload to write - fix rlen to |
---|
591 | * be the correct length */ |
---|
592 | if (payload == NULL) { |
---|
593 | dag_hdr->rlen = htons(dag_record_size + pad); |
---|
594 | } |
---|
595 | |
---|
596 | if (packet->trace->format == &erf || |
---|
597 | #if HAVE_DAG |
---|
598 | packet->trace->format == &dag || |
---|
599 | #endif |
---|
600 | packet->trace->format == &rtclient ) { |
---|
601 | numbytes = erf_dump_packet(libtrace, |
---|
602 | (dag_record_t *)packet->buffer, |
---|
603 | pad, |
---|
604 | payload, |
---|
605 | trace_get_capture_length(packet) |
---|
606 | ); |
---|
607 | } else { |
---|
608 | /* convert format - build up a new erf header */ |
---|
609 | /* Timestamp */ |
---|
610 | erfhdr.ts = trace_get_erf_timestamp(packet); |
---|
611 | erfhdr.type = libtrace_to_erf_type(trace_get_link_type(packet)); |
---|
612 | /* Flags. Can't do this */ |
---|
613 | memset(&erfhdr.flags,1,1); |
---|
614 | /* Packet length (rlen includes format overhead) */ |
---|
615 | erfhdr.rlen = trace_get_capture_length(packet) + erf_get_framing_length(packet); |
---|
616 | /* loss counter. Can't do this */ |
---|
617 | erfhdr.lctr = 0; |
---|
618 | /* Wire length */ |
---|
619 | erfhdr.wlen = trace_get_wire_length(packet); |
---|
620 | |
---|
621 | /* Write it out */ |
---|
622 | numbytes = erf_dump_packet(libtrace, |
---|
623 | &erfhdr, |
---|
624 | pad, |
---|
625 | payload, |
---|
626 | trace_get_capture_length(packet)); |
---|
627 | } |
---|
628 | return numbytes; |
---|
629 | } |
---|
630 | |
---|
631 | static libtrace_linktype_t erf_get_link_type(const struct libtrace_packet_t *packet) { |
---|
632 | dag_record_t *erfptr = 0; |
---|
633 | erfptr = (dag_record_t *)packet->header; |
---|
634 | return erf_type_to_libtrace(erfptr->type); |
---|
635 | } |
---|
636 | |
---|
637 | static int8_t erf_get_direction(const struct libtrace_packet_t *packet) { |
---|
638 | dag_record_t *erfptr = 0; |
---|
639 | erfptr = (dag_record_t *)packet->header; |
---|
640 | return erfptr->flags.iface; |
---|
641 | } |
---|
642 | |
---|
643 | static int8_t erf_set_direction(const struct libtrace_packet_t *packet, int8_t direction) { |
---|
644 | dag_record_t *erfptr = 0; |
---|
645 | erfptr = (dag_record_t *)packet->header; |
---|
646 | erfptr->flags.iface = direction; |
---|
647 | return erfptr->flags.iface; |
---|
648 | } |
---|
649 | |
---|
650 | static uint64_t erf_get_erf_timestamp(const struct libtrace_packet_t *packet) { |
---|
651 | dag_record_t *erfptr = 0; |
---|
652 | erfptr = (dag_record_t *)packet->header; |
---|
653 | return erfptr->ts; |
---|
654 | } |
---|
655 | |
---|
656 | static int erf_get_capture_length(const struct libtrace_packet_t *packet) { |
---|
657 | dag_record_t *erfptr = 0; |
---|
658 | erfptr = (dag_record_t *)packet->header; |
---|
659 | return (ntohs(erfptr->rlen) - erf_get_framing_length(packet)); |
---|
660 | } |
---|
661 | |
---|
662 | static int erf_get_wire_length(const struct libtrace_packet_t *packet) { |
---|
663 | dag_record_t *erfptr = 0; |
---|
664 | erfptr = (dag_record_t *)packet->header; |
---|
665 | return ntohs(erfptr->wlen); |
---|
666 | } |
---|
667 | |
---|
668 | static size_t erf_set_capture_length(struct libtrace_packet_t *packet, size_t size) { |
---|
669 | dag_record_t *erfptr = 0; |
---|
670 | assert(packet); |
---|
671 | if((size + erf_get_framing_length(packet)) > packet->size) { |
---|
672 | /* can't make a packet larger */ |
---|
673 | return (packet->size - erf_get_framing_length(packet)); |
---|
674 | } |
---|
675 | erfptr = (dag_record_t *)packet->header; |
---|
676 | erfptr->rlen = htons(size + erf_get_framing_length(packet)); |
---|
677 | packet->size = size + erf_get_framing_length(packet); |
---|
678 | return packet->size; |
---|
679 | } |
---|
680 | |
---|
681 | static int rtclient_get_fd(const struct libtrace_packet_t *packet) { |
---|
682 | return packet->trace->format_data->input.fd; |
---|
683 | } |
---|
684 | |
---|
685 | static int erf_get_fd(const struct libtrace_packet_t *packet) { |
---|
686 | return packet->trace->format_data->input.fd; |
---|
687 | } |
---|
688 | |
---|
689 | #ifdef HAVE_DAG |
---|
690 | struct libtrace_eventobj_t trace_event_dag(struct libtrace_t *trace, struct libtrace_packet_t *packet) { |
---|
691 | struct libtrace_eventobj_t event = {0,0,0.0,0}; |
---|
692 | int dag_fd; |
---|
693 | int data; |
---|
694 | |
---|
695 | if (packet->trace->format->get_fd) { |
---|
696 | dag_fd = packet->trace->format->get_fd(packet); |
---|
697 | } else { |
---|
698 | dag_fd = 0; |
---|
699 | } |
---|
700 | |
---|
701 | data = dag_read(trace, DAGF_NONBLOCK); |
---|
702 | |
---|
703 | if (data > 0) { |
---|
704 | event.size = trace_read_packet(trace,packet); |
---|
705 | event.type = TRACE_EVENT_PACKET; |
---|
706 | return event; |
---|
707 | } |
---|
708 | event.type = TRACE_EVENT_SLEEP; |
---|
709 | event.seconds = 0.0001; |
---|
710 | return event; |
---|
711 | } |
---|
712 | #endif |
---|
713 | |
---|
714 | #if HAVE_DAG |
---|
715 | static void dag_help() { |
---|
716 | printf("dag format module: $Revision$\n"); |
---|
717 | printf("Supported input URIs:\n"); |
---|
718 | printf("\tdag:/dev/dagn\n"); |
---|
719 | printf("\n"); |
---|
720 | printf("\te.g.: dag:/dev/dag0\n"); |
---|
721 | printf("\n"); |
---|
722 | printf("Supported output URIs:\n"); |
---|
723 | printf("\tnone\n"); |
---|
724 | printf("\n"); |
---|
725 | } |
---|
726 | #endif |
---|
727 | |
---|
728 | static void erf_help() { |
---|
729 | printf("erf format module: $Revision$\n"); |
---|
730 | printf("Supported input URIs:\n"); |
---|
731 | printf("\terf:/path/to/file\t(uncompressed)\n"); |
---|
732 | printf("\terf:/path/to/file.gz\t(gzip-compressed)\n"); |
---|
733 | printf("\terf:-\t(stdin, either compressed or not)\n"); |
---|
734 | printf("\terf:/path/to/socket\n"); |
---|
735 | printf("\n"); |
---|
736 | printf("\te.g.: erf:/tmp/trace\n"); |
---|
737 | printf("\n"); |
---|
738 | printf("Supported output URIs:\n"); |
---|
739 | printf("\terf:path/to/file\t(uncompressed)\n"); |
---|
740 | printf("\terf:/path/to/file.gz\t(gzip-compressed)\n"); |
---|
741 | printf("\terf:-\t(stdout, either compressed or not)\n"); |
---|
742 | printf("\n"); |
---|
743 | printf("\te.g.: erf:/tmp/trace\n"); |
---|
744 | printf("\n"); |
---|
745 | printf("Supported output options:\n"); |
---|
746 | printf("\t-z\tSpecify the gzip compression, ranging from 0 (uncompressed) to 9 - defaults to 1\n"); |
---|
747 | printf("\n"); |
---|
748 | |
---|
749 | |
---|
750 | } |
---|
751 | |
---|
752 | static void rtclient_help() { |
---|
753 | printf("rtclient format module\n"); |
---|
754 | printf("Supported input URIs:\n"); |
---|
755 | printf("\trtclient:hostname:port\n"); |
---|
756 | printf("\trtclient:hostname (connects on default port)\n"); |
---|
757 | printf("\n"); |
---|
758 | printf("\te.g.: rtclient:localhost\n"); |
---|
759 | printf("\te.g.: rtclient:localhost:32500\n"); |
---|
760 | printf("\n"); |
---|
761 | printf("Supported output URIs:\n"); |
---|
762 | printf("\trtclient: \t(will output on default port on all available IP addresses) \n"); |
---|
763 | printf("\trtclient:hostname:port\n"); |
---|
764 | printf("\trtclient:port\n"); |
---|
765 | printf("\n"); |
---|
766 | printf("\te.g.: rtclient:32500\n"); |
---|
767 | printf("\te.g.: rtclient:\n"); |
---|
768 | printf("\n"); |
---|
769 | |
---|
770 | } |
---|
771 | |
---|
772 | static struct libtrace_format_t erf = { |
---|
773 | "erf", |
---|
774 | "$Id$", |
---|
775 | "erf", |
---|
776 | erf_init_input, /* init_input */ |
---|
777 | NULL, /* config_input */ |
---|
778 | erf_start_input, /* start_input */ |
---|
779 | NULL, /* pause_input */ |
---|
780 | erf_init_output, /* init_output */ |
---|
781 | erf_config_output, /* config_output */ |
---|
782 | erf_start_output, /* start_output */ |
---|
783 | erf_fin_input, /* fin_input */ |
---|
784 | erf_fin_output, /* fin_output */ |
---|
785 | erf_read_packet, /* read_packet */ |
---|
786 | erf_write_packet, /* write_packet */ |
---|
787 | erf_get_link_type, /* get_link_type */ |
---|
788 | erf_get_direction, /* get_direction */ |
---|
789 | erf_set_direction, /* set_direction */ |
---|
790 | erf_get_erf_timestamp, /* get_erf_timestamp */ |
---|
791 | NULL, /* get_timeval */ |
---|
792 | NULL, /* get_seconds */ |
---|
793 | NULL, /* seek_erf */ |
---|
794 | NULL, /* seek_timeval */ |
---|
795 | NULL, /* seek_seconds */ |
---|
796 | erf_get_capture_length, /* get_capture_length */ |
---|
797 | erf_get_wire_length, /* get_wire_length */ |
---|
798 | erf_get_framing_length, /* get_framing_length */ |
---|
799 | erf_set_capture_length, /* set_capture_length */ |
---|
800 | erf_get_fd, /* get_fd */ |
---|
801 | trace_event_trace, /* trace_event */ |
---|
802 | erf_help /* help */ |
---|
803 | }; |
---|
804 | |
---|
805 | #ifdef HAVE_DAG |
---|
806 | static struct libtrace_format_t dag = { |
---|
807 | "dag", |
---|
808 | "$Id$", |
---|
809 | "erf", |
---|
810 | dag_init_input, /* init_input */ |
---|
811 | NULL, /* config_input */ |
---|
812 | dag_start_input, /* start_input */ |
---|
813 | NULL, /* init_output */ |
---|
814 | NULL, /* config_output */ |
---|
815 | NULL, /* start_output */ |
---|
816 | dag_fin_input, /* fin_input */ |
---|
817 | NULL, /* fin_output */ |
---|
818 | dag_read_packet, /* read_packet */ |
---|
819 | NULL, /* write_packet */ |
---|
820 | erf_get_link_type, /* get_link_type */ |
---|
821 | erf_get_direction, /* get_direction */ |
---|
822 | erf_set_direction, /* set_direction */ |
---|
823 | erf_get_erf_timestamp, /* get_erf_timestamp */ |
---|
824 | NULL, /* get_timeval */ |
---|
825 | NULL, /* get_seconds */ |
---|
826 | erf_get_capture_length, /* get_capture_length */ |
---|
827 | erf_get_wire_length, /* get_wire_length */ |
---|
828 | erf_get_framing_length, /* get_framing_length */ |
---|
829 | erf_set_capture_length, /* set_capture_length */ |
---|
830 | NULL, /* get_fd */ |
---|
831 | trace_event_dag, /* trace_event */ |
---|
832 | dag_help /* help */ |
---|
833 | }; |
---|
834 | #endif |
---|
835 | |
---|
836 | static struct libtrace_format_t rtclient = { |
---|
837 | "rtclient", |
---|
838 | "$Id$", |
---|
839 | "erf", |
---|
840 | rtclient_init_input, /* init_input */ |
---|
841 | NULL, /* config_input */ |
---|
842 | NULL, /* start_input */ |
---|
843 | NULL, /* pause_input */ |
---|
844 | NULL, /* init_output */ |
---|
845 | NULL, /* config_output */ |
---|
846 | NULL, /* start_output */ |
---|
847 | rtclient_fin_input, /* fin_input */ |
---|
848 | NULL, /* fin_output */ |
---|
849 | rtclient_read_packet, /* read_packet */ |
---|
850 | NULL, /* write_packet */ |
---|
851 | erf_get_link_type, /* get_link_type */ |
---|
852 | erf_get_direction, /* get_direction */ |
---|
853 | erf_set_direction, /* set_direction */ |
---|
854 | erf_get_erf_timestamp, /* get_erf_timestamp */ |
---|
855 | NULL, /* get_timeval */ |
---|
856 | NULL, /* get_seconds */ |
---|
857 | NULL, /* seek_erf */ |
---|
858 | NULL, /* seek_timeval */ |
---|
859 | NULL, /* seek_seconds */ |
---|
860 | erf_get_capture_length, /* get_capture_length */ |
---|
861 | erf_get_wire_length, /* get_wire_length */ |
---|
862 | erf_get_framing_length, /* get_framing_length */ |
---|
863 | erf_set_capture_length, /* set_capture_length */ |
---|
864 | rtclient_get_fd, /* get_fd */ |
---|
865 | trace_event_device, /* trace_event */ |
---|
866 | rtclient_help /* help */ |
---|
867 | }; |
---|
868 | |
---|
869 | void __attribute__((constructor)) erf_constructor() { |
---|
870 | register_format(&erf); |
---|
871 | #ifdef HAVE_DAG |
---|
872 | register_format(&dag); |
---|
873 | #endif |
---|
874 | } |
---|