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 | static struct libtrace_format_t erf; |
---|
69 | static struct libtrace_format_t rtclient; |
---|
70 | #if HAVE_DAG |
---|
71 | static struct libtrace_format_t dag; |
---|
72 | #endif |
---|
73 | |
---|
74 | #define CONNINFO libtrace->format_data->conn_info |
---|
75 | #define INPUT libtrace->format_data->input |
---|
76 | #define OUTPUT libtrace->format_data->output |
---|
77 | #if HAVE_DAG |
---|
78 | #define DAG libtrace->format_data->dag |
---|
79 | #endif |
---|
80 | #define OPTIONS libtrace->format_data->options |
---|
81 | struct libtrace_format_data_t { |
---|
82 | union { |
---|
83 | struct { |
---|
84 | char *hostname; |
---|
85 | short port; |
---|
86 | } rt; |
---|
87 | } conn_info; |
---|
88 | |
---|
89 | union { |
---|
90 | int fd; |
---|
91 | LIBTRACE_FILE file; |
---|
92 | } input; |
---|
93 | |
---|
94 | #if HAVE_DAG |
---|
95 | struct { |
---|
96 | void *buf; |
---|
97 | unsigned bottom; |
---|
98 | unsigned top; |
---|
99 | unsigned diff; |
---|
100 | unsigned curr; |
---|
101 | unsigned offset; |
---|
102 | } dag; |
---|
103 | #endif |
---|
104 | }; |
---|
105 | |
---|
106 | struct libtrace_format_data_out_t { |
---|
107 | union { |
---|
108 | struct { |
---|
109 | char *hostname; |
---|
110 | short port; |
---|
111 | } rt; |
---|
112 | char *path; |
---|
113 | } conn_info; |
---|
114 | |
---|
115 | |
---|
116 | |
---|
117 | union { |
---|
118 | struct { |
---|
119 | int level; |
---|
120 | int fileflag; |
---|
121 | } erf; |
---|
122 | |
---|
123 | } options; |
---|
124 | |
---|
125 | union { |
---|
126 | int fd; |
---|
127 | struct rtserver_t * rtserver; |
---|
128 | #if HAVE_ZLIB |
---|
129 | gzFile *file; |
---|
130 | #else |
---|
131 | int file; |
---|
132 | #endif |
---|
133 | } output; |
---|
134 | }; |
---|
135 | |
---|
136 | /** Structure holding status information for a packet */ |
---|
137 | typedef struct libtrace_packet_status { |
---|
138 | uint8_t type; |
---|
139 | uint8_t reserved; |
---|
140 | uint16_t message; |
---|
141 | } libtrace_packet_status_t; |
---|
142 | |
---|
143 | |
---|
144 | #ifdef HAVE_DAG |
---|
145 | static int dag_init_input(struct libtrace_t *libtrace) { |
---|
146 | libtrace->format_data = (struct libtrace_format_data_t *) |
---|
147 | malloc(sizeof(struct libtrace_format_data_t)); |
---|
148 | } |
---|
149 | |
---|
150 | static int dag_start_input(struct libtrace_t *libtrace) { |
---|
151 | struct stat buf; |
---|
152 | if (stat(libtrace->uridata, &buf) == -1) { |
---|
153 | trace_set_err(errno,"stat(%s)",libtrace->uridata); |
---|
154 | return 0; |
---|
155 | } |
---|
156 | if (S_ISCHR(buf.st_mode)) { |
---|
157 | /* DEVICE */ |
---|
158 | if((INPUT.fd = dag_open(libtrace->uridata)) < 0) { |
---|
159 | trace_set_err(errno,"Cannot open DAG %s", |
---|
160 | libtrace->uridata); |
---|
161 | return 0; |
---|
162 | } |
---|
163 | if((DAG.buf = (void *)dag_mmap(INPUT.fd)) == MAP_FAILED) { |
---|
164 | trace_set_err(errno,"Cannot mmap DAG %s", |
---|
165 | libtrace->uridata); |
---|
166 | return 0; |
---|
167 | } |
---|
168 | if(dag_start(INPUT.fd) < 0) { |
---|
169 | trace_set_err(errno,"Cannot start DAG %s", |
---|
170 | libtrace->uridata); |
---|
171 | return 0; |
---|
172 | } |
---|
173 | } else { |
---|
174 | trace_set_err(errno,"Not a valid dag device: %s", |
---|
175 | libtrace->uridata); |
---|
176 | return 0; |
---|
177 | } |
---|
178 | return 1; |
---|
179 | } |
---|
180 | #endif |
---|
181 | |
---|
182 | /* Dag erf ether packets have a 2 byte padding before the packet |
---|
183 | * so that the ip header is aligned on a 32 bit boundary. |
---|
184 | */ |
---|
185 | static int erf_get_padding(const struct libtrace_packet_t *packet) |
---|
186 | { |
---|
187 | switch(trace_get_link_type(packet)) { |
---|
188 | case TRACE_TYPE_ETH: return 2; |
---|
189 | default: return 0; |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | static int erf_get_framing_length(const struct libtrace_packet_t *packet) |
---|
194 | { |
---|
195 | return dag_record_size + erf_get_padding(packet); |
---|
196 | } |
---|
197 | |
---|
198 | |
---|
199 | static int erf_init_input(struct libtrace_t *libtrace) |
---|
200 | { |
---|
201 | libtrace->format_data = (struct libtrace_format_data_t *) |
---|
202 | malloc(sizeof(struct libtrace_format_data_t)); |
---|
203 | |
---|
204 | } |
---|
205 | |
---|
206 | static int erf_start_input(struct libtrace_t *libtrace) |
---|
207 | { |
---|
208 | libtrace->format_data->input.file = trace_open_file(libtrace); |
---|
209 | |
---|
210 | if (libtrace->format_data->input.file) |
---|
211 | return 1; |
---|
212 | |
---|
213 | return 0; |
---|
214 | } |
---|
215 | |
---|
216 | static int rtclient_init_input(struct libtrace_t *libtrace) { |
---|
217 | char *scan; |
---|
218 | char *uridata = libtrace->uridata; |
---|
219 | struct hostent *he; |
---|
220 | struct sockaddr_in remote; |
---|
221 | libtrace->format_data = (struct libtrace_format_data_t *) |
---|
222 | malloc(sizeof(struct libtrace_format_data_t)); |
---|
223 | |
---|
224 | |
---|
225 | if (strlen(uridata) == 0) { |
---|
226 | CONNINFO.rt.hostname = |
---|
227 | strdup("localhost"); |
---|
228 | CONNINFO.rt.port = |
---|
229 | COLLECTOR_PORT; |
---|
230 | } else { |
---|
231 | if ((scan = strchr(uridata,':')) == NULL) { |
---|
232 | CONNINFO.rt.hostname = |
---|
233 | strdup(uridata); |
---|
234 | CONNINFO.rt.port = |
---|
235 | COLLECTOR_PORT; |
---|
236 | } else { |
---|
237 | CONNINFO.rt.hostname = |
---|
238 | (char *)strndup(uridata, |
---|
239 | (scan - uridata)); |
---|
240 | CONNINFO.rt.port = |
---|
241 | atoi(++scan); |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | if ((he=gethostbyname(CONNINFO.rt.hostname)) == NULL) { |
---|
246 | trace_set_err(errno,"failed to resolve %s", |
---|
247 | CONNINFO.rt.hostname); |
---|
248 | return 0; |
---|
249 | } |
---|
250 | if ((INPUT.fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { |
---|
251 | trace_set_err(errno,"socket(AF_INET,SOCK_STREAM)"); |
---|
252 | return 0; |
---|
253 | } |
---|
254 | |
---|
255 | remote.sin_family = AF_INET; |
---|
256 | remote.sin_port = htons(CONNINFO.rt.port); |
---|
257 | remote.sin_addr = *((struct in_addr *)he->h_addr); |
---|
258 | bzero(&(remote.sin_zero), 8); |
---|
259 | |
---|
260 | if (connect(INPUT.fd, (struct sockaddr *)&remote, |
---|
261 | sizeof(struct sockaddr)) == -1) { |
---|
262 | trace_set_err(errno,"connect(%s)", |
---|
263 | CONNINFO.rt.hostname); |
---|
264 | return 0; |
---|
265 | } |
---|
266 | return 1; |
---|
267 | } |
---|
268 | |
---|
269 | static int erf_init_output(struct libtrace_out_t *libtrace) { |
---|
270 | libtrace->format_data = (struct libtrace_format_data_out_t *) |
---|
271 | calloc(1,sizeof(struct libtrace_format_data_out_t)); |
---|
272 | |
---|
273 | OPTIONS.erf.level = 0; |
---|
274 | OPTIONS.erf.fileflag = O_CREAT | O_LARGEFILE | O_WRONLY; |
---|
275 | OUTPUT.file = 0; |
---|
276 | |
---|
277 | return 0; |
---|
278 | } |
---|
279 | |
---|
280 | static int erf_config_output(struct libtrace_out_t *libtrace, trace_option_t option, void *value) { |
---|
281 | |
---|
282 | switch (option) { |
---|
283 | case TRACE_OPTION_OUTPUT_COMPRESS: |
---|
284 | OPTIONS.erf.level = *(int*)value; |
---|
285 | return 0; |
---|
286 | case TRACE_OPTION_OUTPUT_FILEFLAGS: |
---|
287 | OPTIONS.erf.fileflag = *(int*)value; |
---|
288 | return 0; |
---|
289 | default: |
---|
290 | /* Unknown option */ |
---|
291 | trace_set_err(TRACE_ERR_UNKNOWN_OPTION, |
---|
292 | "Unknown option"); |
---|
293 | return -1; |
---|
294 | } |
---|
295 | } |
---|
296 | |
---|
297 | |
---|
298 | #ifdef HAVE_DAG |
---|
299 | static int dag_fin_input(struct libtrace_t *libtrace) { |
---|
300 | dag_stop(INPUT.fd); |
---|
301 | } |
---|
302 | #endif |
---|
303 | |
---|
304 | static int rtclient_fin_input(struct libtrace_t *libtrace) { |
---|
305 | close(INPUT.fd); |
---|
306 | return 0; |
---|
307 | } |
---|
308 | |
---|
309 | static int erf_fin_input(struct libtrace_t *libtrace) { |
---|
310 | LIBTRACE_CLOSE(INPUT.file); |
---|
311 | free(libtrace->format_data); |
---|
312 | return 0; |
---|
313 | } |
---|
314 | |
---|
315 | static int erf_fin_output(struct libtrace_out_t *libtrace) { |
---|
316 | LIBTRACE_CLOSE(OUTPUT.file); |
---|
317 | free(libtrace->format_data); |
---|
318 | |
---|
319 | return 0; |
---|
320 | } |
---|
321 | |
---|
322 | #if HAVE_DAG |
---|
323 | static int dag_read(struct libtrace_t *libtrace, int block_flag) { |
---|
324 | int numbytes; |
---|
325 | static short lctr = 0; |
---|
326 | struct dag_record_t *erfptr = 0; |
---|
327 | int rlen; |
---|
328 | |
---|
329 | if (DAG.diff != 0) |
---|
330 | return DAG.diff; |
---|
331 | |
---|
332 | DAG.bottom = DAG.top; |
---|
333 | DAG.top = dag_offset( |
---|
334 | INPUT.fd, |
---|
335 | &(DAG.bottom), |
---|
336 | block_flag); |
---|
337 | DAG.diff = DAG.top - |
---|
338 | DAG.bottom; |
---|
339 | |
---|
340 | numbytes=DAG.diff; |
---|
341 | DAG.offset = 0; |
---|
342 | return numbytes; |
---|
343 | } |
---|
344 | #endif |
---|
345 | |
---|
346 | #if HAVE_DAG |
---|
347 | static int dag_read_packet(struct libtrace_t *libtrace, struct libtrace_packet_t *packet) { |
---|
348 | int numbytes; |
---|
349 | int size; |
---|
350 | dag_record_t *erfptr; |
---|
351 | void *buffer = packet->buffer; |
---|
352 | void *buffer2 = buffer; |
---|
353 | int rlen; |
---|
354 | |
---|
355 | if (packet->buf_control == TRACE_CTRL_PACKET) { |
---|
356 | packet->buf_control = TRACE_CTRL_EXTERNAL; |
---|
357 | free(packet->buffer); |
---|
358 | packet->buffer = 0; |
---|
359 | } |
---|
360 | |
---|
361 | if ((numbytes = dag_read(libtrace,0)) <= 0) |
---|
362 | return numbytes; |
---|
363 | |
---|
364 | /*DAG always gives us whole packets */ |
---|
365 | erfptr = (dag_record_t *) ((void *)DAG.buf + |
---|
366 | (DAG.bottom + DAG.offset)); |
---|
367 | size = ntohs(erfptr->rlen); |
---|
368 | |
---|
369 | if ( size > LIBTRACE_PACKET_BUFSIZE) { |
---|
370 | assert( size < LIBTRACE_PACKET_BUFSIZE); |
---|
371 | } |
---|
372 | |
---|
373 | packet->buffer = erfptr; |
---|
374 | packet->header = erfptr; |
---|
375 | if (((dag_record_t *)packet->buffer)->flags.rxerror == 1) { |
---|
376 | packet->payload = NULL; |
---|
377 | } else { |
---|
378 | packet->payload = packet->buffer + erf_get_framing_length(packet); |
---|
379 | } |
---|
380 | |
---|
381 | packet->size = size; |
---|
382 | DAG.offset += size; |
---|
383 | DAG.diff -= size; |
---|
384 | |
---|
385 | assert(DAG.diff >= 0); |
---|
386 | |
---|
387 | return (size); |
---|
388 | } |
---|
389 | #endif |
---|
390 | |
---|
391 | static int erf_read_packet(struct libtrace_t *libtrace, struct libtrace_packet_t *packet) { |
---|
392 | int numbytes; |
---|
393 | int size; |
---|
394 | void *buffer2 = packet->buffer; |
---|
395 | int rlen; |
---|
396 | |
---|
397 | if (!packet->buffer || packet->buf_control == TRACE_CTRL_EXTERNAL) { |
---|
398 | packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE); |
---|
399 | packet->buf_control = TRACE_CTRL_PACKET; |
---|
400 | } |
---|
401 | |
---|
402 | packet->header = packet->buffer; |
---|
403 | |
---|
404 | if ((numbytes=LIBTRACE_READ(INPUT.file, |
---|
405 | packet->buffer, |
---|
406 | dag_record_size)) == -1) { |
---|
407 | trace_set_err(errno,"read(%s)", |
---|
408 | packet->trace->uridata); |
---|
409 | return -1; |
---|
410 | } |
---|
411 | if (numbytes == 0) { |
---|
412 | return 0; |
---|
413 | } |
---|
414 | |
---|
415 | rlen = ntohs(((dag_record_t *)packet->buffer)->rlen); |
---|
416 | buffer2 = (char*)packet->buffer + dag_record_size; |
---|
417 | size = rlen - dag_record_size; |
---|
418 | assert(size < LIBTRACE_PACKET_BUFSIZE); |
---|
419 | |
---|
420 | |
---|
421 | /* Unknown/corrupt */ |
---|
422 | assert(((dag_record_t *)packet->buffer)->type < 10); |
---|
423 | |
---|
424 | /* read in the rest of the packet */ |
---|
425 | if ((numbytes=LIBTRACE_READ(INPUT.file, |
---|
426 | buffer2, |
---|
427 | size)) != size) { |
---|
428 | trace_set_err(errno, "read(%s)", packet->trace->uridata); |
---|
429 | return -1; |
---|
430 | } |
---|
431 | packet->size = rlen; |
---|
432 | if (((dag_record_t *)packet->buffer)->flags.rxerror == 1) { |
---|
433 | packet->payload = NULL; |
---|
434 | } else { |
---|
435 | packet->payload = (char*)packet->buffer + erf_get_framing_length(packet); |
---|
436 | } |
---|
437 | return rlen; |
---|
438 | } |
---|
439 | |
---|
440 | static int rtclient_read(struct libtrace_t *libtrace, void *buffer, size_t len) { |
---|
441 | int numbytes; |
---|
442 | |
---|
443 | while(1) { |
---|
444 | #ifndef MSG_NOSIGNAL |
---|
445 | # define MSG_NOSIGNAL 0 |
---|
446 | #endif |
---|
447 | if ((numbytes = recv(INPUT.fd, |
---|
448 | buffer, |
---|
449 | len, |
---|
450 | MSG_NOSIGNAL)) == -1) { |
---|
451 | if (errno == EINTR) { |
---|
452 | /*ignore EINTR in case |
---|
453 | *a caller is using signals |
---|
454 | */ |
---|
455 | continue; |
---|
456 | } |
---|
457 | trace_set_err(errno,"recv(%s)", |
---|
458 | libtrace->uridata); |
---|
459 | return -1; |
---|
460 | } |
---|
461 | break; |
---|
462 | |
---|
463 | } |
---|
464 | return numbytes; |
---|
465 | } |
---|
466 | |
---|
467 | #define RT_DATA 1 |
---|
468 | #define RT_MSG 2 |
---|
469 | |
---|
470 | static int rtclient_read_packet(struct libtrace_t *libtrace, struct libtrace_packet_t *packet) { |
---|
471 | int numbytes = 0; |
---|
472 | char buf[RP_BUFSIZE]; |
---|
473 | int read_required = 0; |
---|
474 | |
---|
475 | void *buffer = 0; |
---|
476 | |
---|
477 | packet->trace = libtrace; |
---|
478 | |
---|
479 | if (packet->buf_control == TRACE_CTRL_EXTERNAL || !packet->buffer) { |
---|
480 | packet->buf_control = TRACE_CTRL_PACKET; |
---|
481 | packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE); |
---|
482 | } |
---|
483 | |
---|
484 | buffer = packet->buffer; |
---|
485 | packet->header = packet->buffer; |
---|
486 | |
---|
487 | |
---|
488 | do { |
---|
489 | struct libtrace_packet_status status; |
---|
490 | if (tracefifo_out_available(libtrace->fifo) == 0 || read_required) { |
---|
491 | if ((numbytes = rtclient_read( |
---|
492 | libtrace,buf,RP_BUFSIZE))<=0) { |
---|
493 | return numbytes; |
---|
494 | } |
---|
495 | tracefifo_write(libtrace->fifo,buf,numbytes); |
---|
496 | read_required = 0; |
---|
497 | } |
---|
498 | /* Read status byte */ |
---|
499 | if (tracefifo_out_read(libtrace->fifo, |
---|
500 | &status, sizeof(uint32_t)) == 0) { |
---|
501 | read_required = 1; |
---|
502 | continue; |
---|
503 | } |
---|
504 | tracefifo_out_update(libtrace->fifo,sizeof(uint32_t)); |
---|
505 | /* Read in packet size */ |
---|
506 | if (tracefifo_out_read(libtrace->fifo, |
---|
507 | &packet->size, sizeof(uint32_t)) == 0) { |
---|
508 | tracefifo_out_reset(libtrace->fifo); |
---|
509 | read_required = 1; |
---|
510 | continue; |
---|
511 | } |
---|
512 | tracefifo_out_update(libtrace->fifo, sizeof(uint32_t)); |
---|
513 | |
---|
514 | if (status.type == RT_MSG) { |
---|
515 | /* Need to skip this packet as it is a message packet */ |
---|
516 | tracefifo_out_update(libtrace->fifo, packet->size); |
---|
517 | tracefifo_ack_update(libtrace->fifo, packet->size + |
---|
518 | sizeof(uint32_t) + |
---|
519 | sizeof(libtrace_packet_status_t)); |
---|
520 | continue; |
---|
521 | } |
---|
522 | |
---|
523 | /* read in the full packet */ |
---|
524 | if ((numbytes = tracefifo_out_read(libtrace->fifo, |
---|
525 | buffer, packet->size)) == 0) { |
---|
526 | tracefifo_out_reset(libtrace->fifo); |
---|
527 | read_required = 1; |
---|
528 | continue; |
---|
529 | } |
---|
530 | |
---|
531 | /* got in our whole packet, so... */ |
---|
532 | tracefifo_out_update(libtrace->fifo,packet->size); |
---|
533 | |
---|
534 | tracefifo_ack_update(libtrace->fifo,packet->size + |
---|
535 | sizeof(uint32_t) + |
---|
536 | sizeof(libtrace_packet_status_t)); |
---|
537 | |
---|
538 | if (((dag_record_t *)buffer)->flags.rxerror == 1) { |
---|
539 | packet->payload = NULL; |
---|
540 | } else { |
---|
541 | packet->payload = (char*)packet->buffer + erf_get_framing_length(packet); |
---|
542 | } |
---|
543 | return numbytes; |
---|
544 | } while(1); |
---|
545 | } |
---|
546 | |
---|
547 | static int erf_dump_packet(libtrace_out_t *libtrace, |
---|
548 | dag_record_t *erfptr, int pad, void *buffer, size_t size) { |
---|
549 | int numbytes = 0; |
---|
550 | assert(size>=0 && size<=65536); |
---|
551 | /* FIXME: Shouldn't this return != dag_record_size+pad on error? */ |
---|
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 | return -1; |
---|
572 | } |
---|
573 | return 0; |
---|
574 | } |
---|
575 | |
---|
576 | static int erf_write_packet(libtrace_out_t *libtrace, |
---|
577 | const libtrace_packet_t *packet) |
---|
578 | { |
---|
579 | int numbytes = 0; |
---|
580 | int pad = 0; |
---|
581 | dag_record_t *dag_hdr = (dag_record_t *)packet->header; |
---|
582 | void *payload = packet->payload; |
---|
583 | |
---|
584 | assert(OUTPUT.file); |
---|
585 | |
---|
586 | pad = erf_get_padding(packet); |
---|
587 | |
---|
588 | /* If we've had an rxerror, we have no payload to write - fix rlen to |
---|
589 | * be the correct length */ |
---|
590 | if (payload == NULL) { |
---|
591 | dag_hdr->rlen = htons(dag_record_size + pad); |
---|
592 | } |
---|
593 | |
---|
594 | if (packet->trace->format == &erf |
---|
595 | #if HAVE_DAG |
---|
596 | || packet->trace->format == &dag |
---|
597 | #endif |
---|
598 | ) { |
---|
599 | numbytes = erf_dump_packet(libtrace, |
---|
600 | (dag_record_t *)packet->buffer, |
---|
601 | pad, |
---|
602 | payload, |
---|
603 | trace_get_capture_length(packet) |
---|
604 | ); |
---|
605 | } else { |
---|
606 | dag_record_t erfhdr; |
---|
607 | /* convert format - build up a new erf header */ |
---|
608 | /* Timestamp */ |
---|
609 | erfhdr.ts = trace_get_erf_timestamp(packet); |
---|
610 | erfhdr.type = libtrace_to_erf_type(trace_get_link_type(packet)); |
---|
611 | /* Flags. Can't do this */ |
---|
612 | memset(&erfhdr.flags,1,sizeof(erfhdr.flags)); |
---|
613 | /* Packet length (rlen includes format overhead) */ |
---|
614 | erfhdr.rlen = trace_get_capture_length(packet) |
---|
615 | + 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: $Revision$\n"); |
---|
754 | printf("DEPRECATED - use rt module instead\n"); |
---|
755 | printf("Supported input URIs:\n"); |
---|
756 | printf("\trtclient:host:port\n"); |
---|
757 | printf("\n"); |
---|
758 | printf("\te.g.:rtclient:localhost:3435\n"); |
---|
759 | printf("\n"); |
---|
760 | printf("Supported output URIs:\n"); |
---|
761 | printf("\tnone\n"); |
---|
762 | printf("\n"); |
---|
763 | } |
---|
764 | |
---|
765 | static struct libtrace_format_t erf = { |
---|
766 | "erf", |
---|
767 | "$Id$", |
---|
768 | TRACE_FORMAT_ERF, |
---|
769 | erf_init_input, /* init_input */ |
---|
770 | NULL, /* config_input */ |
---|
771 | erf_start_input, /* start_input */ |
---|
772 | NULL, /* pause_input */ |
---|
773 | erf_init_output, /* init_output */ |
---|
774 | erf_config_output, /* config_output */ |
---|
775 | erf_start_output, /* start_output */ |
---|
776 | erf_fin_input, /* fin_input */ |
---|
777 | erf_fin_output, /* fin_output */ |
---|
778 | erf_read_packet, /* read_packet */ |
---|
779 | erf_write_packet, /* write_packet */ |
---|
780 | erf_get_link_type, /* get_link_type */ |
---|
781 | erf_get_direction, /* get_direction */ |
---|
782 | erf_set_direction, /* set_direction */ |
---|
783 | erf_get_erf_timestamp, /* get_erf_timestamp */ |
---|
784 | NULL, /* get_timeval */ |
---|
785 | NULL, /* get_seconds */ |
---|
786 | NULL, /* seek_erf */ |
---|
787 | NULL, /* seek_timeval */ |
---|
788 | NULL, /* seek_seconds */ |
---|
789 | erf_get_capture_length, /* get_capture_length */ |
---|
790 | erf_get_wire_length, /* get_wire_length */ |
---|
791 | erf_get_framing_length, /* get_framing_length */ |
---|
792 | erf_set_capture_length, /* set_capture_length */ |
---|
793 | erf_get_fd, /* get_fd */ |
---|
794 | trace_event_trace, /* trace_event */ |
---|
795 | erf_help /* help */ |
---|
796 | }; |
---|
797 | |
---|
798 | #ifdef HAVE_DAG |
---|
799 | static struct libtrace_format_t dag = { |
---|
800 | "dag", |
---|
801 | "$Id$", |
---|
802 | TRACE_FORMAT_ERF, |
---|
803 | dag_init_input, /* init_input */ |
---|
804 | NULL, /* config_input */ |
---|
805 | dag_start_input, /* start_input */ |
---|
806 | NULL, /* pause_input */ |
---|
807 | NULL, /* init_output */ |
---|
808 | NULL, /* config_output */ |
---|
809 | NULL, /* start_output */ |
---|
810 | dag_fin_input, /* fin_input */ |
---|
811 | NULL, /* fin_output */ |
---|
812 | dag_read_packet, /* read_packet */ |
---|
813 | NULL, /* write_packet */ |
---|
814 | erf_get_link_type, /* get_link_type */ |
---|
815 | erf_get_direction, /* get_direction */ |
---|
816 | erf_set_direction, /* set_direction */ |
---|
817 | erf_get_erf_timestamp, /* get_erf_timestamp */ |
---|
818 | NULL, /* get_timeval */ |
---|
819 | NULL, /* get_seconds */ |
---|
820 | NULL, /* seek_erf */ |
---|
821 | NULL, /* seek_timeval */ |
---|
822 | NULL, /* seek_seconds */ |
---|
823 | erf_get_capture_length, /* get_capture_length */ |
---|
824 | erf_get_wire_length, /* get_wire_length */ |
---|
825 | erf_get_framing_length, /* get_framing_length */ |
---|
826 | erf_set_capture_length, /* set_capture_length */ |
---|
827 | NULL, /* get_fd */ |
---|
828 | trace_event_dag, /* trace_event */ |
---|
829 | dag_help /* help */ |
---|
830 | }; |
---|
831 | #endif |
---|
832 | |
---|
833 | static struct libtrace_format_t rtclient = { |
---|
834 | "rtclient", |
---|
835 | "$Id$", |
---|
836 | TRACE_FORMAT_ERF, |
---|
837 | rtclient_init_input, /* init_input */ |
---|
838 | NULL, /* config_input */ |
---|
839 | NULL, /* start_input */ |
---|
840 | NULL, /* pause_input */ |
---|
841 | NULL, /* init_output */ |
---|
842 | NULL, /* config_output */ |
---|
843 | NULL, /* start_output */ |
---|
844 | rtclient_fin_input, /* fin_input */ |
---|
845 | NULL, /* fin_output */ |
---|
846 | rtclient_read_packet, /* read_packet */ |
---|
847 | NULL, /* write_packet */ |
---|
848 | erf_get_link_type, /* get_link_type */ |
---|
849 | erf_get_direction, /* get_direction */ |
---|
850 | erf_set_direction, /* set_direction */ |
---|
851 | erf_get_erf_timestamp, /* get_erf_timestamp */ |
---|
852 | NULL, /* get_timeval */ |
---|
853 | NULL, /* get_seconds */ |
---|
854 | NULL, /* seek_erf */ |
---|
855 | NULL, /* seek_timeval */ |
---|
856 | NULL, /* seek_seconds */ |
---|
857 | erf_get_capture_length, /* get_capture_length */ |
---|
858 | erf_get_wire_length, /* get_wire_length */ |
---|
859 | erf_get_framing_length, /* get_framing_length */ |
---|
860 | erf_set_capture_length, /* set_capture_length */ |
---|
861 | rtclient_get_fd, /* get_fd */ |
---|
862 | trace_event_device, /* trace_event */ |
---|
863 | rtclient_help /* help */ |
---|
864 | }; |
---|
865 | |
---|
866 | void __attribute__((constructor)) erf_constructor() { |
---|
867 | register_format(&rtclient); |
---|
868 | register_format(&erf); |
---|
869 | #ifdef HAVE_DAG |
---|
870 | register_format(&dag); |
---|
871 | #endif |
---|
872 | } |
---|