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(libtrace,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(libtrace,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(libtrace,errno,"Cannot mmap DAG %s", |
---|
165 | libtrace->uridata); |
---|
166 | return 0; |
---|
167 | } |
---|
168 | if(dag_start(INPUT.fd) < 0) { |
---|
169 | trace_set_err(libtrace,errno,"Cannot start DAG %s", |
---|
170 | libtrace->uridata); |
---|
171 | return 0; |
---|
172 | } |
---|
173 | } else { |
---|
174 | trace_set_err(libtrace,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(libtrace,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(libtrace,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(libtrace,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_out(libtrace,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 | free(libtrace->format_data); |
---|
302 | } |
---|
303 | #endif |
---|
304 | |
---|
305 | static int rtclient_fin_input(struct libtrace_t *libtrace) { |
---|
306 | free(CONNINFO.rt.hostname); |
---|
307 | close(INPUT.fd); |
---|
308 | free(libtrace->format_data); |
---|
309 | return 0; |
---|
310 | } |
---|
311 | |
---|
312 | static int erf_fin_input(struct libtrace_t *libtrace) { |
---|
313 | LIBTRACE_CLOSE(INPUT.file); |
---|
314 | free(libtrace->format_data); |
---|
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 | return 0; |
---|
322 | } |
---|
323 | |
---|
324 | #if HAVE_DAG |
---|
325 | static int dag_read(struct libtrace_t *libtrace, int block_flag) { |
---|
326 | int numbytes; |
---|
327 | static short lctr = 0; |
---|
328 | struct dag_record_t *erfptr = 0; |
---|
329 | int rlen; |
---|
330 | |
---|
331 | if (DAG.diff != 0) |
---|
332 | return DAG.diff; |
---|
333 | |
---|
334 | DAG.bottom = DAG.top; |
---|
335 | DAG.top = dag_offset( |
---|
336 | INPUT.fd, |
---|
337 | &(DAG.bottom), |
---|
338 | block_flag); |
---|
339 | DAG.diff = DAG.top - |
---|
340 | DAG.bottom; |
---|
341 | |
---|
342 | numbytes=DAG.diff; |
---|
343 | DAG.offset = 0; |
---|
344 | return numbytes; |
---|
345 | } |
---|
346 | #endif |
---|
347 | |
---|
348 | #if HAVE_DAG |
---|
349 | static int dag_read_packet(struct libtrace_t *libtrace, struct libtrace_packet_t *packet) { |
---|
350 | int numbytes; |
---|
351 | int size; |
---|
352 | dag_record_t *erfptr; |
---|
353 | void *buffer = packet->buffer; |
---|
354 | void *buffer2 = buffer; |
---|
355 | int rlen; |
---|
356 | |
---|
357 | if (packet->buf_control == TRACE_CTRL_PACKET) { |
---|
358 | packet->buf_control = TRACE_CTRL_EXTERNAL; |
---|
359 | free(packet->buffer); |
---|
360 | packet->buffer = 0; |
---|
361 | } |
---|
362 | |
---|
363 | packet->type = RT_DATA_ERF; |
---|
364 | |
---|
365 | if ((numbytes = dag_read(libtrace,0)) <= 0) |
---|
366 | return numbytes; |
---|
367 | |
---|
368 | /*DAG always gives us whole packets */ |
---|
369 | erfptr = (dag_record_t *) ((void *)DAG.buf + |
---|
370 | (DAG.bottom + DAG.offset)); |
---|
371 | size = ntohs(erfptr->rlen); |
---|
372 | |
---|
373 | if ( size > LIBTRACE_PACKET_BUFSIZE) { |
---|
374 | assert( size < LIBTRACE_PACKET_BUFSIZE); |
---|
375 | } |
---|
376 | |
---|
377 | packet->buffer = erfptr; |
---|
378 | packet->header = erfptr; |
---|
379 | if (((dag_record_t *)packet->buffer)->flags.rxerror == 1) { |
---|
380 | packet->payload = NULL; |
---|
381 | } else { |
---|
382 | packet->payload = packet->buffer + erf_get_framing_length(packet); |
---|
383 | } |
---|
384 | |
---|
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 || packet->buf_control == TRACE_CTRL_EXTERNAL) { |
---|
401 | packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE); |
---|
402 | packet->buf_control = TRACE_CTRL_PACKET; |
---|
403 | } |
---|
404 | |
---|
405 | packet->header = packet->buffer; |
---|
406 | packet->type = RT_DATA_ERF; |
---|
407 | |
---|
408 | if ((numbytes=LIBTRACE_READ(INPUT.file, |
---|
409 | packet->buffer, |
---|
410 | dag_record_size)) == -1) { |
---|
411 | trace_set_err(libtrace,errno,"read(%s)", |
---|
412 | libtrace->uridata); |
---|
413 | return -1; |
---|
414 | } |
---|
415 | if (numbytes == 0) { |
---|
416 | return 0; |
---|
417 | } |
---|
418 | |
---|
419 | rlen = ntohs(((dag_record_t *)packet->buffer)->rlen); |
---|
420 | buffer2 = (char*)packet->buffer + dag_record_size; |
---|
421 | size = rlen - dag_record_size; |
---|
422 | assert(size < LIBTRACE_PACKET_BUFSIZE); |
---|
423 | |
---|
424 | |
---|
425 | /* Unknown/corrupt */ |
---|
426 | assert(((dag_record_t *)packet->buffer)->type < 10); |
---|
427 | |
---|
428 | /* read in the rest of the packet */ |
---|
429 | if ((numbytes=LIBTRACE_READ(INPUT.file, |
---|
430 | buffer2, |
---|
431 | size)) != size) { |
---|
432 | trace_set_err(libtrace,errno, "read(%s)", libtrace->uridata); |
---|
433 | return -1; |
---|
434 | } |
---|
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 | while(1) { |
---|
447 | #ifndef MSG_NOSIGNAL |
---|
448 | # define MSG_NOSIGNAL 0 |
---|
449 | #endif |
---|
450 | if ((numbytes = recv(INPUT.fd, |
---|
451 | buffer, |
---|
452 | len, |
---|
453 | MSG_NOSIGNAL)) == -1) { |
---|
454 | if (errno == EINTR) { |
---|
455 | /*ignore EINTR in case |
---|
456 | *a caller is using signals |
---|
457 | */ |
---|
458 | continue; |
---|
459 | } |
---|
460 | trace_set_err(libtrace,errno,"recv(%s)", |
---|
461 | libtrace->uridata); |
---|
462 | return -1; |
---|
463 | } |
---|
464 | break; |
---|
465 | |
---|
466 | } |
---|
467 | return numbytes; |
---|
468 | } |
---|
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 | if (packet->buf_control == TRACE_CTRL_EXTERNAL || !packet->buffer) { |
---|
478 | packet->buf_control = TRACE_CTRL_PACKET; |
---|
479 | packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE); |
---|
480 | } |
---|
481 | |
---|
482 | buffer = packet->buffer; |
---|
483 | packet->header = packet->buffer; |
---|
484 | |
---|
485 | packet->type = RT_DATA_ERF; |
---|
486 | |
---|
487 | |
---|
488 | do { |
---|
489 | struct libtrace_packet_status status; |
---|
490 | int size; |
---|
491 | if (tracefifo_out_available(libtrace->fifo) == 0 |
---|
492 | || 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 | &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 == 2 /* RT_MSG */) { |
---|
517 | /* Need to skip this packet as it is a message packet */ |
---|
518 | tracefifo_out_update(libtrace->fifo, size); |
---|
519 | tracefifo_ack_update(libtrace->fifo, 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, 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,size); |
---|
535 | |
---|
536 | tracefifo_ack_update(libtrace->fifo,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(libtrace_out_t *libtrace, |
---|
550 | dag_record_t *erfptr, int pad, void *buffer, size_t size) { |
---|
551 | int numbytes = 0; |
---|
552 | assert(size>=0 && size<=65536); |
---|
553 | /* FIXME: Shouldn't this return != dag_record_size+pad on error? */ |
---|
554 | if ((numbytes = LIBTRACE_WRITE(OUTPUT.file, erfptr, dag_record_size + pad)) == 0) { |
---|
555 | trace_set_err_out(libtrace,errno, |
---|
556 | "write(%s)",libtrace->uridata); |
---|
557 | return -1; |
---|
558 | } |
---|
559 | |
---|
560 | if ((numbytes=LIBTRACE_WRITE(OUTPUT.file, buffer, size)) == 0) { |
---|
561 | trace_set_err_out(libtrace,errno, |
---|
562 | "write(%s)",libtrace->uridata); |
---|
563 | return -1; |
---|
564 | } |
---|
565 | |
---|
566 | return numbytes + pad + dag_record_size; |
---|
567 | } |
---|
568 | |
---|
569 | static int erf_start_output(libtrace_out_t *libtrace) |
---|
570 | { |
---|
571 | OUTPUT.file = trace_open_file_out(libtrace, |
---|
572 | OPTIONS.erf.level, |
---|
573 | OPTIONS.erf.fileflag); |
---|
574 | if (!OUTPUT.file) { |
---|
575 | return -1; |
---|
576 | } |
---|
577 | return 0; |
---|
578 | } |
---|
579 | |
---|
580 | static int erf_write_packet(libtrace_out_t *libtrace, |
---|
581 | const libtrace_packet_t *packet) |
---|
582 | { |
---|
583 | int numbytes = 0; |
---|
584 | int pad = 0; |
---|
585 | dag_record_t *dag_hdr = (dag_record_t *)packet->header; |
---|
586 | void *payload = packet->payload; |
---|
587 | |
---|
588 | assert(OUTPUT.file); |
---|
589 | |
---|
590 | pad = erf_get_padding(packet); |
---|
591 | |
---|
592 | /* If we've had an rxerror, we have no payload to write - fix rlen to |
---|
593 | * be the correct length */ |
---|
594 | if (payload == NULL) { |
---|
595 | dag_hdr->rlen = htons(dag_record_size + pad); |
---|
596 | } |
---|
597 | |
---|
598 | if (libtrace->format == &erf |
---|
599 | #if HAVE_DAG |
---|
600 | || libtrace->format == &dag |
---|
601 | #endif |
---|
602 | ) { |
---|
603 | numbytes = erf_dump_packet(libtrace, |
---|
604 | (dag_record_t *)packet->buffer, |
---|
605 | pad, |
---|
606 | payload, |
---|
607 | trace_get_capture_length(packet) |
---|
608 | ); |
---|
609 | } else { |
---|
610 | dag_record_t erfhdr; |
---|
611 | /* convert format - build up a new erf header */ |
---|
612 | /* Timestamp */ |
---|
613 | erfhdr.ts = trace_get_erf_timestamp(packet); |
---|
614 | erfhdr.type = libtrace_to_erf_type(trace_get_link_type(packet)); |
---|
615 | /* Flags. Can't do this */ |
---|
616 | memset(&erfhdr.flags,1,sizeof(erfhdr.flags)); |
---|
617 | /* Packet length (rlen includes format overhead) */ |
---|
618 | erfhdr.rlen = trace_get_capture_length(packet) |
---|
619 | + erf_get_framing_length(packet); |
---|
620 | /* loss counter. Can't do this */ |
---|
621 | erfhdr.lctr = 0; |
---|
622 | /* Wire length */ |
---|
623 | erfhdr.wlen = trace_get_wire_length(packet); |
---|
624 | |
---|
625 | /* Write it out */ |
---|
626 | numbytes = erf_dump_packet(libtrace, |
---|
627 | &erfhdr, |
---|
628 | pad, |
---|
629 | payload, |
---|
630 | trace_get_capture_length(packet)); |
---|
631 | } |
---|
632 | return numbytes; |
---|
633 | } |
---|
634 | |
---|
635 | static libtrace_linktype_t erf_get_link_type(const struct libtrace_packet_t *packet) { |
---|
636 | dag_record_t *erfptr = 0; |
---|
637 | erfptr = (dag_record_t *)packet->header; |
---|
638 | return erf_type_to_libtrace(erfptr->type); |
---|
639 | } |
---|
640 | |
---|
641 | static int8_t erf_get_direction(const struct libtrace_packet_t *packet) { |
---|
642 | dag_record_t *erfptr = 0; |
---|
643 | erfptr = (dag_record_t *)packet->header; |
---|
644 | return erfptr->flags.iface; |
---|
645 | } |
---|
646 | |
---|
647 | static int8_t erf_set_direction(const struct libtrace_packet_t *packet, int8_t direction) { |
---|
648 | dag_record_t *erfptr = 0; |
---|
649 | erfptr = (dag_record_t *)packet->header; |
---|
650 | erfptr->flags.iface = direction; |
---|
651 | return erfptr->flags.iface; |
---|
652 | } |
---|
653 | |
---|
654 | static uint64_t erf_get_erf_timestamp(const struct libtrace_packet_t *packet) { |
---|
655 | dag_record_t *erfptr = 0; |
---|
656 | erfptr = (dag_record_t *)packet->header; |
---|
657 | return erfptr->ts; |
---|
658 | } |
---|
659 | |
---|
660 | static int erf_get_capture_length(const struct libtrace_packet_t *packet) { |
---|
661 | dag_record_t *erfptr = 0; |
---|
662 | erfptr = (dag_record_t *)packet->header; |
---|
663 | return (ntohs(erfptr->rlen) - erf_get_framing_length(packet)); |
---|
664 | } |
---|
665 | |
---|
666 | static int erf_get_wire_length(const struct libtrace_packet_t *packet) { |
---|
667 | dag_record_t *erfptr = 0; |
---|
668 | erfptr = (dag_record_t *)packet->header; |
---|
669 | return ntohs(erfptr->wlen); |
---|
670 | } |
---|
671 | |
---|
672 | static size_t erf_set_capture_length(struct libtrace_packet_t *packet, size_t size) { |
---|
673 | dag_record_t *erfptr = 0; |
---|
674 | assert(packet); |
---|
675 | if(size > trace_get_capture_length(packet)) { |
---|
676 | /* can't make a packet larger */ |
---|
677 | return trace_get_capture_length(packet); |
---|
678 | } |
---|
679 | erfptr = (dag_record_t *)packet->header; |
---|
680 | erfptr->rlen = htons(size + erf_get_framing_length(packet)); |
---|
681 | printf("Set length: %i\n", trace_get_capture_length(packet)); |
---|
682 | return trace_get_capture_length(packet); |
---|
683 | } |
---|
684 | |
---|
685 | static int rtclient_get_fd(const libtrace_t *trace) { |
---|
686 | return trace->format_data->input.fd; |
---|
687 | } |
---|
688 | |
---|
689 | static int erf_get_fd(const libtrace_t *trace) { |
---|
690 | return trace->format_data->input.fd; |
---|
691 | } |
---|
692 | |
---|
693 | #ifdef HAVE_DAG |
---|
694 | struct libtrace_eventobj_t trace_event_dag(struct libtrace_t *trace, struct libtrace_packet_t *packet) { |
---|
695 | struct libtrace_eventobj_t event = {0,0,0.0,0}; |
---|
696 | int dag_fd; |
---|
697 | int data; |
---|
698 | |
---|
699 | if (trace->format->get_fd) { |
---|
700 | dag_fd = trace->format->get_fd(trace); |
---|
701 | } else { |
---|
702 | dag_fd = 0; |
---|
703 | } |
---|
704 | |
---|
705 | data = dag_read(trace, DAGF_NONBLOCK); |
---|
706 | |
---|
707 | if (data > 0) { |
---|
708 | event.size = trace_read_packet(trace,packet); |
---|
709 | event.type = TRACE_EVENT_PACKET; |
---|
710 | return event; |
---|
711 | } |
---|
712 | event.type = TRACE_EVENT_SLEEP; |
---|
713 | event.seconds = 0.0001; |
---|
714 | return event; |
---|
715 | } |
---|
716 | #endif |
---|
717 | |
---|
718 | #if HAVE_DAG |
---|
719 | static void dag_help() { |
---|
720 | printf("dag format module: $Revision$\n"); |
---|
721 | printf("Supported input URIs:\n"); |
---|
722 | printf("\tdag:/dev/dagn\n"); |
---|
723 | printf("\n"); |
---|
724 | printf("\te.g.: dag:/dev/dag0\n"); |
---|
725 | printf("\n"); |
---|
726 | printf("Supported output URIs:\n"); |
---|
727 | printf("\tnone\n"); |
---|
728 | printf("\n"); |
---|
729 | } |
---|
730 | #endif |
---|
731 | |
---|
732 | static void erf_help() { |
---|
733 | printf("erf format module: $Revision$\n"); |
---|
734 | printf("Supported input URIs:\n"); |
---|
735 | printf("\terf:/path/to/file\t(uncompressed)\n"); |
---|
736 | printf("\terf:/path/to/file.gz\t(gzip-compressed)\n"); |
---|
737 | printf("\terf:-\t(stdin, either compressed or not)\n"); |
---|
738 | printf("\terf:/path/to/socket\n"); |
---|
739 | printf("\n"); |
---|
740 | printf("\te.g.: erf:/tmp/trace\n"); |
---|
741 | printf("\n"); |
---|
742 | printf("Supported output URIs:\n"); |
---|
743 | printf("\terf:path/to/file\t(uncompressed)\n"); |
---|
744 | printf("\terf:/path/to/file.gz\t(gzip-compressed)\n"); |
---|
745 | printf("\terf:-\t(stdout, either compressed or not)\n"); |
---|
746 | printf("\n"); |
---|
747 | printf("\te.g.: erf:/tmp/trace\n"); |
---|
748 | printf("\n"); |
---|
749 | printf("Supported output options:\n"); |
---|
750 | printf("\t-z\tSpecify the gzip compression, ranging from 0 (uncompressed) to 9 - defaults to 1\n"); |
---|
751 | printf("\n"); |
---|
752 | |
---|
753 | |
---|
754 | } |
---|
755 | |
---|
756 | static void rtclient_help() { |
---|
757 | printf("rtclient format module: $Revision$\n"); |
---|
758 | printf("DEPRECATED - use rt module instead\n"); |
---|
759 | printf("Supported input URIs:\n"); |
---|
760 | printf("\trtclient:host:port\n"); |
---|
761 | printf("\n"); |
---|
762 | printf("\te.g.:rtclient:localhost:3435\n"); |
---|
763 | printf("\n"); |
---|
764 | printf("Supported output URIs:\n"); |
---|
765 | printf("\tnone\n"); |
---|
766 | printf("\n"); |
---|
767 | } |
---|
768 | |
---|
769 | static struct libtrace_format_t erf = { |
---|
770 | "erf", |
---|
771 | "$Id$", |
---|
772 | TRACE_FORMAT_ERF, |
---|
773 | erf_init_input, /* init_input */ |
---|
774 | NULL, /* config_input */ |
---|
775 | erf_start_input, /* start_input */ |
---|
776 | NULL, /* pause_input */ |
---|
777 | erf_init_output, /* init_output */ |
---|
778 | erf_config_output, /* config_output */ |
---|
779 | erf_start_output, /* start_output */ |
---|
780 | erf_fin_input, /* fin_input */ |
---|
781 | erf_fin_output, /* fin_output */ |
---|
782 | erf_read_packet, /* read_packet */ |
---|
783 | erf_write_packet, /* write_packet */ |
---|
784 | erf_get_link_type, /* get_link_type */ |
---|
785 | erf_get_direction, /* get_direction */ |
---|
786 | erf_set_direction, /* set_direction */ |
---|
787 | erf_get_erf_timestamp, /* get_erf_timestamp */ |
---|
788 | NULL, /* get_timeval */ |
---|
789 | NULL, /* get_seconds */ |
---|
790 | NULL, /* seek_erf */ |
---|
791 | NULL, /* seek_timeval */ |
---|
792 | NULL, /* seek_seconds */ |
---|
793 | erf_get_capture_length, /* get_capture_length */ |
---|
794 | erf_get_wire_length, /* get_wire_length */ |
---|
795 | erf_get_framing_length, /* get_framing_length */ |
---|
796 | erf_set_capture_length, /* set_capture_length */ |
---|
797 | erf_get_fd, /* get_fd */ |
---|
798 | trace_event_trace, /* trace_event */ |
---|
799 | erf_help /* help */ |
---|
800 | }; |
---|
801 | |
---|
802 | #ifdef HAVE_DAG |
---|
803 | static struct libtrace_format_t dag = { |
---|
804 | "dag", |
---|
805 | "$Id$", |
---|
806 | TRACE_FORMAT_ERF, |
---|
807 | dag_init_input, /* init_input */ |
---|
808 | NULL, /* config_input */ |
---|
809 | dag_start_input, /* start_input */ |
---|
810 | NULL, /* pause_input */ |
---|
811 | NULL, /* init_output */ |
---|
812 | NULL, /* config_output */ |
---|
813 | NULL, /* start_output */ |
---|
814 | dag_fin_input, /* fin_input */ |
---|
815 | NULL, /* fin_output */ |
---|
816 | dag_read_packet, /* read_packet */ |
---|
817 | NULL, /* write_packet */ |
---|
818 | erf_get_link_type, /* get_link_type */ |
---|
819 | erf_get_direction, /* get_direction */ |
---|
820 | erf_set_direction, /* set_direction */ |
---|
821 | erf_get_erf_timestamp, /* get_erf_timestamp */ |
---|
822 | NULL, /* get_timeval */ |
---|
823 | NULL, /* get_seconds */ |
---|
824 | NULL, /* seek_erf */ |
---|
825 | NULL, /* seek_timeval */ |
---|
826 | NULL, /* seek_seconds */ |
---|
827 | erf_get_capture_length, /* get_capture_length */ |
---|
828 | erf_get_wire_length, /* get_wire_length */ |
---|
829 | erf_get_framing_length, /* get_framing_length */ |
---|
830 | erf_set_capture_length, /* set_capture_length */ |
---|
831 | NULL, /* get_fd */ |
---|
832 | trace_event_dag, /* trace_event */ |
---|
833 | dag_help /* help */ |
---|
834 | }; |
---|
835 | #endif |
---|
836 | |
---|
837 | static struct libtrace_format_t rtclient = { |
---|
838 | "rtclient", |
---|
839 | "$Id$", |
---|
840 | TRACE_FORMAT_ERF, |
---|
841 | rtclient_init_input, /* init_input */ |
---|
842 | NULL, /* config_input */ |
---|
843 | NULL, /* start_input */ |
---|
844 | NULL, /* pause_input */ |
---|
845 | NULL, /* init_output */ |
---|
846 | NULL, /* config_output */ |
---|
847 | NULL, /* start_output */ |
---|
848 | rtclient_fin_input, /* fin_input */ |
---|
849 | NULL, /* fin_output */ |
---|
850 | rtclient_read_packet, /* read_packet */ |
---|
851 | NULL, /* write_packet */ |
---|
852 | erf_get_link_type, /* get_link_type */ |
---|
853 | erf_get_direction, /* get_direction */ |
---|
854 | erf_set_direction, /* set_direction */ |
---|
855 | erf_get_erf_timestamp, /* get_erf_timestamp */ |
---|
856 | NULL, /* get_timeval */ |
---|
857 | NULL, /* get_seconds */ |
---|
858 | NULL, /* seek_erf */ |
---|
859 | NULL, /* seek_timeval */ |
---|
860 | NULL, /* seek_seconds */ |
---|
861 | erf_get_capture_length, /* get_capture_length */ |
---|
862 | erf_get_wire_length, /* get_wire_length */ |
---|
863 | erf_get_framing_length, /* get_framing_length */ |
---|
864 | erf_set_capture_length, /* set_capture_length */ |
---|
865 | rtclient_get_fd, /* get_fd */ |
---|
866 | trace_event_device, /* trace_event */ |
---|
867 | rtclient_help /* help */ |
---|
868 | }; |
---|
869 | |
---|
870 | void __attribute__((constructor)) erf_constructor() { |
---|
871 | register_format(&rtclient); |
---|
872 | register_format(&erf); |
---|
873 | #ifdef HAVE_DAG |
---|
874 | register_format(&dag); |
---|
875 | #endif |
---|
876 | } |
---|