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 DATA(x) ((struct erf_format_data_t *)x->format_data) |
---|
75 | #define DATAOUT(x) ((struct erf_format_data_out_t *)x->format_data) |
---|
76 | |
---|
77 | #define CONNINFO DATA(libtrace)->conn_info |
---|
78 | #define INPUT DATA(libtrace)->input |
---|
79 | #define OUTPUT DATAOUT(libtrace)->output |
---|
80 | #if HAVE_DAG |
---|
81 | #define DAG DATA(libtrace)->dag |
---|
82 | #endif |
---|
83 | #define OPTIONS DATAOUT(libtrace)->options |
---|
84 | struct erf_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 | struct { |
---|
98 | enum { INDEX_UNKNOWN=0, INDEX_NONE, INDEX_EXISTS } exists; |
---|
99 | LIBTRACE_FILE index; |
---|
100 | off_t index_len; |
---|
101 | } seek; |
---|
102 | |
---|
103 | #if HAVE_DAG |
---|
104 | struct { |
---|
105 | void *buf; |
---|
106 | unsigned bottom; |
---|
107 | unsigned top; |
---|
108 | unsigned diff; |
---|
109 | unsigned curr; |
---|
110 | unsigned offset; |
---|
111 | } dag; |
---|
112 | #endif |
---|
113 | }; |
---|
114 | |
---|
115 | struct erf_format_data_out_t { |
---|
116 | union { |
---|
117 | struct { |
---|
118 | char *hostname; |
---|
119 | short port; |
---|
120 | } rt; |
---|
121 | char *path; |
---|
122 | } conn_info; |
---|
123 | |
---|
124 | union { |
---|
125 | struct { |
---|
126 | int level; |
---|
127 | int fileflag; |
---|
128 | } erf; |
---|
129 | |
---|
130 | } options; |
---|
131 | |
---|
132 | union { |
---|
133 | int fd; |
---|
134 | struct rtserver_t * rtserver; |
---|
135 | #if HAVE_ZLIB |
---|
136 | gzFile *file; |
---|
137 | #else |
---|
138 | int file; |
---|
139 | #endif |
---|
140 | } output; |
---|
141 | }; |
---|
142 | |
---|
143 | /** Structure holding status information for a packet */ |
---|
144 | typedef struct libtrace_packet_status { |
---|
145 | uint8_t type; |
---|
146 | uint8_t reserved; |
---|
147 | uint16_t message; |
---|
148 | } libtrace_packet_status_t; |
---|
149 | |
---|
150 | typedef struct erf_index_t { |
---|
151 | uint64_t timestamp; |
---|
152 | uint64_t offset; |
---|
153 | } erf_index_t; |
---|
154 | |
---|
155 | #ifdef HAVE_DAG |
---|
156 | static int dag_init_input(struct libtrace_t *libtrace) { |
---|
157 | struct stat buf; |
---|
158 | |
---|
159 | libtrace->format_data = (struct erf_format_data_t *) |
---|
160 | malloc(sizeof(struct erf_format_data_t)); |
---|
161 | if (stat(libtrace->uridata, &buf) == -1) { |
---|
162 | trace_set_err(libtrace,errno,"stat(%s)",libtrace->uridata); |
---|
163 | return -1; |
---|
164 | } |
---|
165 | if (S_ISCHR(buf.st_mode)) { |
---|
166 | /* DEVICE */ |
---|
167 | if((INPUT.fd = dag_open(libtrace->uridata)) < 0) { |
---|
168 | trace_set_err(libtrace,errno,"Cannot open DAG %s", |
---|
169 | libtrace->uridata); |
---|
170 | return -1; |
---|
171 | } |
---|
172 | if((DAG.buf = (void *)dag_mmap(INPUT.fd)) == MAP_FAILED) { |
---|
173 | trace_set_err(libtrace,errno,"Cannot mmap DAG %s", |
---|
174 | libtrace->uridata); |
---|
175 | return -1; |
---|
176 | } |
---|
177 | } else { |
---|
178 | trace_set_err(libtrace,errno,"Not a valid dag device: %s", |
---|
179 | libtrace->uridata); |
---|
180 | return -1; |
---|
181 | } |
---|
182 | return 0; |
---|
183 | } |
---|
184 | |
---|
185 | #endif |
---|
186 | |
---|
187 | /* Dag erf ether packets have a 2 byte padding before the packet |
---|
188 | * so that the ip header is aligned on a 32 bit boundary. |
---|
189 | */ |
---|
190 | static int erf_get_padding(const libtrace_packet_t *packet) |
---|
191 | { |
---|
192 | dag_record_t *erfptr = (dag_record_t *)packet->header; |
---|
193 | switch(erfptr->type) { |
---|
194 | case TYPE_ETH: return 2; |
---|
195 | default: return 0; |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | static int erf_get_framing_length(const libtrace_packet_t *packet) |
---|
200 | { |
---|
201 | return dag_record_size + erf_get_padding(packet); |
---|
202 | } |
---|
203 | |
---|
204 | |
---|
205 | static int erf_init_input(libtrace_t *libtrace) |
---|
206 | { |
---|
207 | libtrace->format_data = malloc(sizeof(struct erf_format_data_t)); |
---|
208 | |
---|
209 | return 0; /* success */ |
---|
210 | } |
---|
211 | |
---|
212 | static int erf_start_input(libtrace_t *libtrace) |
---|
213 | { |
---|
214 | if (INPUT.file) |
---|
215 | return 0; /* success */ |
---|
216 | |
---|
217 | INPUT.file = trace_open_file(libtrace); |
---|
218 | |
---|
219 | if (!INPUT.file) |
---|
220 | return -1; |
---|
221 | |
---|
222 | return 0; /* success */ |
---|
223 | } |
---|
224 | |
---|
225 | /* Binary search through the index to find the closest point before |
---|
226 | * the packet. Consider in future having a btree index perhaps? |
---|
227 | */ |
---|
228 | static int erf_fast_seek_start(libtrace_t *libtrace,uint64_t erfts) |
---|
229 | { |
---|
230 | size_t max_off = DATA(libtrace)->seek.index_len/sizeof(erf_index_t); |
---|
231 | size_t min_off = 0; |
---|
232 | off_t current; |
---|
233 | erf_index_t record; |
---|
234 | do { |
---|
235 | current=(max_off+min_off)>>2; |
---|
236 | |
---|
237 | LIBTRACE_SEEK(DATA(libtrace)->seek.index, |
---|
238 | current*sizeof(record), |
---|
239 | SEEK_SET); |
---|
240 | LIBTRACE_READ(DATA(libtrace)->seek.index, |
---|
241 | &record,sizeof(record)); |
---|
242 | if (record.timestamp < erfts) { |
---|
243 | min_off=current; |
---|
244 | } |
---|
245 | if (record.timestamp > erfts) { |
---|
246 | max_off=current; |
---|
247 | } |
---|
248 | if (record.timestamp == erfts) |
---|
249 | break; |
---|
250 | } while(min_off<max_off); |
---|
251 | |
---|
252 | /* If we've passed it, seek backwards. This loop shouldn't |
---|
253 | * execute more than twice. |
---|
254 | */ |
---|
255 | do { |
---|
256 | LIBTRACE_SEEK(DATA(libtrace)->seek.index, |
---|
257 | current*sizeof(record),SEEK_SET); |
---|
258 | LIBTRACE_READ(DATA(libtrace)->seek.index, |
---|
259 | &record,sizeof(record)); |
---|
260 | current--; |
---|
261 | } while(record.timestamp>erfts); |
---|
262 | |
---|
263 | /* We've found our location in the trace, now use it. */ |
---|
264 | LIBTRACE_SEEK(INPUT.file,record.offset,SEEK_SET); |
---|
265 | |
---|
266 | return 0; /* success */ |
---|
267 | } |
---|
268 | |
---|
269 | /* There is no index. Seek through the entire trace from the start, nice |
---|
270 | * and slowly. |
---|
271 | */ |
---|
272 | static int erf_slow_seek_start(libtrace_t *libtrace,uint64_t erfts) |
---|
273 | { |
---|
274 | if (INPUT.file) { |
---|
275 | LIBTRACE_CLOSE(INPUT.file); |
---|
276 | } |
---|
277 | INPUT.file = trace_open_file(libtrace); |
---|
278 | if (!INPUT.file) |
---|
279 | return -1; |
---|
280 | return 0; |
---|
281 | } |
---|
282 | |
---|
283 | static int erf_seek_erf(libtrace_t *libtrace,uint64_t erfts) |
---|
284 | { |
---|
285 | libtrace_packet_t *packet; |
---|
286 | off_t off = 0; |
---|
287 | |
---|
288 | if (DATA(libtrace)->seek.exists==INDEX_UNKNOWN) { |
---|
289 | char buffer[PATH_MAX]; |
---|
290 | snprintf(buffer,sizeof(buffer),"%s.idx",libtrace->uridata); |
---|
291 | DATA(libtrace)->seek.index=LIBTRACE_OPEN(buffer,"r"); |
---|
292 | if (DATA(libtrace)->seek.index) { |
---|
293 | DATA(libtrace)->seek.exists=INDEX_EXISTS; |
---|
294 | } |
---|
295 | else { |
---|
296 | DATA(libtrace)->seek.exists=INDEX_NONE; |
---|
297 | } |
---|
298 | } |
---|
299 | |
---|
300 | /* If theres an index, use it to find the nearest packet that isn't |
---|
301 | * after the time we're looking for. If there is no index we need |
---|
302 | * to seek slowly through the trace from the beginning. Sigh. |
---|
303 | */ |
---|
304 | switch(DATA(libtrace)->seek.exists) { |
---|
305 | case INDEX_EXISTS: |
---|
306 | erf_fast_seek_start(libtrace,erfts); |
---|
307 | break; |
---|
308 | case INDEX_NONE: |
---|
309 | erf_slow_seek_start(libtrace,erfts); |
---|
310 | break; |
---|
311 | case INDEX_UNKNOWN: |
---|
312 | assert(0); |
---|
313 | break; |
---|
314 | } |
---|
315 | |
---|
316 | /* Now seek forward looking for the correct timestamp */ |
---|
317 | packet=trace_create_packet(); |
---|
318 | do { |
---|
319 | trace_read_packet(libtrace,packet); |
---|
320 | if (trace_get_erf_timestamp(packet)==erfts) |
---|
321 | break; |
---|
322 | off=LIBTRACE_TELL(INPUT.file); |
---|
323 | } while(trace_get_erf_timestamp(packet)<erfts); |
---|
324 | |
---|
325 | LIBTRACE_SEEK(INPUT.file,off,SEEK_SET); |
---|
326 | |
---|
327 | return 0; |
---|
328 | } |
---|
329 | |
---|
330 | static int rtclient_init_input(libtrace_t *libtrace) { |
---|
331 | char *scan; |
---|
332 | libtrace->format_data = malloc(sizeof(struct erf_format_data_t)); |
---|
333 | |
---|
334 | |
---|
335 | if (strlen(libtrace->uridata) == 0) { |
---|
336 | CONNINFO.rt.hostname = |
---|
337 | strdup("localhost"); |
---|
338 | CONNINFO.rt.port = |
---|
339 | COLLECTOR_PORT; |
---|
340 | } else { |
---|
341 | if ((scan = strchr(libtrace->uridata,':')) == NULL) { |
---|
342 | CONNINFO.rt.hostname = |
---|
343 | strdup(libtrace->uridata); |
---|
344 | CONNINFO.rt.port = |
---|
345 | COLLECTOR_PORT; |
---|
346 | } else { |
---|
347 | CONNINFO.rt.hostname = |
---|
348 | (char *)strndup(libtrace->uridata, |
---|
349 | (scan - libtrace->uridata)); |
---|
350 | CONNINFO.rt.port = |
---|
351 | atoi(++scan); |
---|
352 | } |
---|
353 | } |
---|
354 | |
---|
355 | return 0; /* success */ |
---|
356 | } |
---|
357 | |
---|
358 | static int rtclient_start_input(libtrace_t *libtrace) |
---|
359 | { |
---|
360 | struct hostent *he; |
---|
361 | struct sockaddr_in remote; |
---|
362 | if ((he=gethostbyname(CONNINFO.rt.hostname)) == NULL) { |
---|
363 | trace_set_err(libtrace,errno,"failed to resolve %s", |
---|
364 | CONNINFO.rt.hostname); |
---|
365 | return -1; |
---|
366 | } |
---|
367 | if ((INPUT.fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { |
---|
368 | trace_set_err(libtrace,errno,"socket(AF_INET,SOCK_STREAM)"); |
---|
369 | return -1; |
---|
370 | } |
---|
371 | |
---|
372 | remote.sin_family = AF_INET; |
---|
373 | remote.sin_port = htons(CONNINFO.rt.port); |
---|
374 | remote.sin_addr = *((struct in_addr *)he->h_addr); |
---|
375 | bzero(&(remote.sin_zero), 8); |
---|
376 | |
---|
377 | if (connect(INPUT.fd, (struct sockaddr *)&remote, |
---|
378 | sizeof(struct sockaddr)) == -1) { |
---|
379 | trace_set_err(libtrace,errno,"connect(%s)", |
---|
380 | CONNINFO.rt.hostname); |
---|
381 | return -1; |
---|
382 | } |
---|
383 | return 0; /* success */ |
---|
384 | } |
---|
385 | |
---|
386 | static int rtclient_pause_input(libtrace_t *libtrace) |
---|
387 | { |
---|
388 | close(INPUT.fd); |
---|
389 | return 0; /* success */ |
---|
390 | } |
---|
391 | |
---|
392 | static int erf_init_output(libtrace_out_t *libtrace) { |
---|
393 | libtrace->format_data = calloc(1,sizeof(struct erf_format_data_out_t)); |
---|
394 | |
---|
395 | OPTIONS.erf.level = 0; |
---|
396 | OPTIONS.erf.fileflag = O_CREAT | O_LARGEFILE | O_WRONLY; |
---|
397 | OUTPUT.file = 0; |
---|
398 | |
---|
399 | return 0; |
---|
400 | } |
---|
401 | |
---|
402 | static int erf_config_output(libtrace_out_t *libtrace, trace_option_t option, void *value) { |
---|
403 | |
---|
404 | switch (option) { |
---|
405 | case TRACE_OPTION_OUTPUT_COMPRESS: |
---|
406 | OPTIONS.erf.level = *(int*)value; |
---|
407 | return 0; |
---|
408 | case TRACE_OPTION_OUTPUT_FILEFLAGS: |
---|
409 | OPTIONS.erf.fileflag = *(int*)value; |
---|
410 | return 0; |
---|
411 | default: |
---|
412 | /* Unknown option */ |
---|
413 | trace_set_err_out(libtrace,TRACE_ERR_UNKNOWN_OPTION, |
---|
414 | "Unknown option"); |
---|
415 | return -1; |
---|
416 | } |
---|
417 | } |
---|
418 | |
---|
419 | |
---|
420 | #ifdef HAVE_DAG |
---|
421 | static int dag_pause_input(libtrace_t *libtrace) { |
---|
422 | dag_stop(INPUT.fd); |
---|
423 | return 0; /* success */ |
---|
424 | } |
---|
425 | |
---|
426 | static int dag_fin_input(libtrace_t *libtrace) { |
---|
427 | /* dag pause input implicitly called to cleanup before this */ |
---|
428 | dag_close(INPUT.fd); |
---|
429 | free(libtrace->format_data); |
---|
430 | return 0; /* success */ |
---|
431 | } |
---|
432 | #endif |
---|
433 | |
---|
434 | static int rtclient_fin_input(libtrace_t *libtrace) { |
---|
435 | free(CONNINFO.rt.hostname); |
---|
436 | close(INPUT.fd); |
---|
437 | free(libtrace->format_data); |
---|
438 | return 0; |
---|
439 | } |
---|
440 | |
---|
441 | static int erf_fin_input(libtrace_t *libtrace) { |
---|
442 | LIBTRACE_CLOSE(INPUT.file); |
---|
443 | free(libtrace->format_data); |
---|
444 | return 0; |
---|
445 | } |
---|
446 | |
---|
447 | static int erf_fin_output(libtrace_out_t *libtrace) { |
---|
448 | LIBTRACE_CLOSE(OUTPUT.file); |
---|
449 | free(libtrace->format_data); |
---|
450 | return 0; |
---|
451 | } |
---|
452 | |
---|
453 | #if HAVE_DAG |
---|
454 | static int dag_read(libtrace_t *libtrace, int block_flag) { |
---|
455 | |
---|
456 | if (DAG.diff != 0) |
---|
457 | return DAG.diff; |
---|
458 | |
---|
459 | DAG.bottom = DAG.top; |
---|
460 | |
---|
461 | DAG.top = dag_offset( |
---|
462 | INPUT.fd, |
---|
463 | &(DAG.bottom), |
---|
464 | block_flag); |
---|
465 | |
---|
466 | DAG.diff = DAG.top - DAG.bottom; |
---|
467 | |
---|
468 | DAG.offset = 0; |
---|
469 | return DAG.diff; |
---|
470 | } |
---|
471 | |
---|
472 | /* FIXME: dag_read_packet shouldn't update the pointers, dag_fin_packet |
---|
473 | * should do that. |
---|
474 | */ |
---|
475 | static int dag_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { |
---|
476 | int numbytes; |
---|
477 | int size; |
---|
478 | dag_record_t *erfptr; |
---|
479 | |
---|
480 | if (packet->buf_control == TRACE_CTRL_PACKET) { |
---|
481 | packet->buf_control = TRACE_CTRL_EXTERNAL; |
---|
482 | free(packet->buffer); |
---|
483 | packet->buffer = 0; |
---|
484 | } |
---|
485 | |
---|
486 | packet->type = RT_DATA_ERF; |
---|
487 | |
---|
488 | if ((numbytes = dag_read(libtrace,0)) < 0) |
---|
489 | return numbytes; |
---|
490 | assert(numbytes>0); |
---|
491 | |
---|
492 | /*DAG always gives us whole packets */ |
---|
493 | erfptr = (dag_record_t *) ((char *)DAG.buf + |
---|
494 | (DAG.bottom + DAG.offset)); |
---|
495 | size = ntohs(erfptr->rlen); |
---|
496 | |
---|
497 | assert( size >= dag_record_size ); |
---|
498 | assert( size < LIBTRACE_PACKET_BUFSIZE); |
---|
499 | |
---|
500 | packet->buffer = erfptr; |
---|
501 | packet->header = erfptr; |
---|
502 | if (((dag_record_t *)packet->buffer)->flags.rxerror == 1) { |
---|
503 | packet->payload = NULL; |
---|
504 | } else { |
---|
505 | packet->payload = (char*)packet->buffer |
---|
506 | + erf_get_framing_length(packet); |
---|
507 | } |
---|
508 | |
---|
509 | DAG.offset += size; |
---|
510 | DAG.diff -= size; |
---|
511 | |
---|
512 | return (size); |
---|
513 | } |
---|
514 | |
---|
515 | static int dag_start_input(libtrace_t *libtrace) { |
---|
516 | if(dag_start(INPUT.fd) < 0) { |
---|
517 | trace_set_err(libtrace,errno,"Cannot start DAG %s", |
---|
518 | libtrace->uridata); |
---|
519 | return -1; |
---|
520 | } |
---|
521 | /* dags appear to have a bug where if you call dag_start after |
---|
522 | * calling dag_stop, and at least one packet has arrived, bad things |
---|
523 | * happen. flush the memory hole |
---|
524 | */ |
---|
525 | while(dag_read(libtrace,1)!=0) |
---|
526 | DAG.diff=0; |
---|
527 | return 0; |
---|
528 | } |
---|
529 | #endif |
---|
530 | |
---|
531 | static int erf_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { |
---|
532 | int numbytes; |
---|
533 | int size; |
---|
534 | void *buffer2 = packet->buffer; |
---|
535 | int rlen; |
---|
536 | |
---|
537 | if (!packet->buffer || packet->buf_control == TRACE_CTRL_EXTERNAL) { |
---|
538 | packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE); |
---|
539 | packet->buf_control = TRACE_CTRL_PACKET; |
---|
540 | } |
---|
541 | |
---|
542 | packet->header = packet->buffer; |
---|
543 | packet->type = RT_DATA_ERF; |
---|
544 | |
---|
545 | if ((numbytes=LIBTRACE_READ(INPUT.file, |
---|
546 | packet->buffer, |
---|
547 | dag_record_size)) == -1) { |
---|
548 | trace_set_err(libtrace,errno,"read(%s)", |
---|
549 | libtrace->uridata); |
---|
550 | return -1; |
---|
551 | } |
---|
552 | if (numbytes == 0) { |
---|
553 | return 0; |
---|
554 | } |
---|
555 | |
---|
556 | rlen = ntohs(((dag_record_t *)packet->buffer)->rlen); |
---|
557 | buffer2 = (char*)packet->buffer + dag_record_size; |
---|
558 | size = rlen - dag_record_size; |
---|
559 | assert(size < LIBTRACE_PACKET_BUFSIZE); |
---|
560 | |
---|
561 | |
---|
562 | /* Unknown/corrupt */ |
---|
563 | assert(((dag_record_t *)packet->buffer)->type < 10); |
---|
564 | |
---|
565 | /* read in the rest of the packet */ |
---|
566 | if ((numbytes=LIBTRACE_READ(INPUT.file, |
---|
567 | buffer2, |
---|
568 | size)) != size) { |
---|
569 | trace_set_err(libtrace,errno, "read(%s)", libtrace->uridata); |
---|
570 | return -1; |
---|
571 | } |
---|
572 | if (((dag_record_t *)packet->buffer)->flags.rxerror == 1) { |
---|
573 | packet->payload = NULL; |
---|
574 | } else { |
---|
575 | packet->payload = (char*)packet->buffer + erf_get_framing_length(packet); |
---|
576 | } |
---|
577 | return rlen; |
---|
578 | } |
---|
579 | |
---|
580 | static int rtclient_read(libtrace_t *libtrace, void *buffer, size_t len) { |
---|
581 | int numbytes; |
---|
582 | |
---|
583 | while(1) { |
---|
584 | #ifndef MSG_NOSIGNAL |
---|
585 | # define MSG_NOSIGNAL 0 |
---|
586 | #endif |
---|
587 | if ((numbytes = recv(INPUT.fd, |
---|
588 | buffer, |
---|
589 | len, |
---|
590 | MSG_NOSIGNAL)) == -1) { |
---|
591 | if (errno == EINTR) { |
---|
592 | /*ignore EINTR in case |
---|
593 | *a caller is using signals |
---|
594 | */ |
---|
595 | continue; |
---|
596 | } |
---|
597 | trace_set_err(libtrace,errno,"recv(%s)", |
---|
598 | libtrace->uridata); |
---|
599 | return -1; |
---|
600 | } |
---|
601 | break; |
---|
602 | |
---|
603 | } |
---|
604 | return numbytes; |
---|
605 | } |
---|
606 | |
---|
607 | static int rtclient_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { |
---|
608 | int numbytes = 0; |
---|
609 | char buf[RP_BUFSIZE]; |
---|
610 | int read_required = 0; |
---|
611 | |
---|
612 | void *buffer = 0; |
---|
613 | |
---|
614 | if (packet->buf_control == TRACE_CTRL_EXTERNAL || !packet->buffer) { |
---|
615 | packet->buf_control = TRACE_CTRL_PACKET; |
---|
616 | packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE); |
---|
617 | } |
---|
618 | |
---|
619 | buffer = packet->buffer; |
---|
620 | packet->header = packet->buffer; |
---|
621 | |
---|
622 | packet->type = RT_DATA_ERF; |
---|
623 | |
---|
624 | |
---|
625 | do { |
---|
626 | libtrace_packet_status_t status; |
---|
627 | int size; |
---|
628 | if (tracefifo_out_available(libtrace->fifo) == 0 |
---|
629 | || read_required) { |
---|
630 | if ((numbytes = rtclient_read( |
---|
631 | libtrace,buf,RP_BUFSIZE))<=0) { |
---|
632 | return numbytes; |
---|
633 | } |
---|
634 | tracefifo_write(libtrace->fifo,buf,numbytes); |
---|
635 | read_required = 0; |
---|
636 | } |
---|
637 | /* Read status byte */ |
---|
638 | if (tracefifo_out_read(libtrace->fifo, |
---|
639 | &status, sizeof(uint32_t)) == 0) { |
---|
640 | read_required = 1; |
---|
641 | continue; |
---|
642 | } |
---|
643 | tracefifo_out_update(libtrace->fifo,sizeof(uint32_t)); |
---|
644 | /* Read in packet size */ |
---|
645 | if (tracefifo_out_read(libtrace->fifo, |
---|
646 | &size, sizeof(uint32_t)) == 0) { |
---|
647 | tracefifo_out_reset(libtrace->fifo); |
---|
648 | read_required = 1; |
---|
649 | continue; |
---|
650 | } |
---|
651 | tracefifo_out_update(libtrace->fifo, sizeof(uint32_t)); |
---|
652 | |
---|
653 | if (status.type == 2 /* RT_MSG */) { |
---|
654 | /* Need to skip this packet as it is a message packet */ |
---|
655 | tracefifo_out_update(libtrace->fifo, size); |
---|
656 | tracefifo_ack_update(libtrace->fifo, size + |
---|
657 | sizeof(uint32_t) + |
---|
658 | sizeof(libtrace_packet_status_t)); |
---|
659 | continue; |
---|
660 | } |
---|
661 | |
---|
662 | /* read in the full packet */ |
---|
663 | if ((numbytes = tracefifo_out_read(libtrace->fifo, |
---|
664 | buffer, size)) == 0) { |
---|
665 | tracefifo_out_reset(libtrace->fifo); |
---|
666 | read_required = 1; |
---|
667 | continue; |
---|
668 | } |
---|
669 | |
---|
670 | /* got in our whole packet, so... */ |
---|
671 | tracefifo_out_update(libtrace->fifo,size); |
---|
672 | |
---|
673 | tracefifo_ack_update(libtrace->fifo,size + |
---|
674 | sizeof(uint32_t) + |
---|
675 | sizeof(libtrace_packet_status_t)); |
---|
676 | |
---|
677 | if (((dag_record_t *)buffer)->flags.rxerror == 1) { |
---|
678 | packet->payload = NULL; |
---|
679 | } else { |
---|
680 | packet->payload = (char*)packet->buffer + erf_get_framing_length(packet); |
---|
681 | } |
---|
682 | return numbytes; |
---|
683 | } while(1); |
---|
684 | } |
---|
685 | |
---|
686 | static int erf_dump_packet(libtrace_out_t *libtrace, |
---|
687 | dag_record_t *erfptr, int pad, void *buffer, size_t size) { |
---|
688 | int numbytes = 0; |
---|
689 | assert(size<=65536); |
---|
690 | /* FIXME: Shouldn't this return != dag_record_size+pad on error? */ |
---|
691 | if ((numbytes = LIBTRACE_WRITE(OUTPUT.file, erfptr, dag_record_size + pad)) == 0) { |
---|
692 | trace_set_err_out(libtrace,errno, |
---|
693 | "write(%s)",libtrace->uridata); |
---|
694 | return -1; |
---|
695 | } |
---|
696 | |
---|
697 | if ((numbytes=LIBTRACE_WRITE(OUTPUT.file, buffer, size)) == 0) { |
---|
698 | trace_set_err_out(libtrace,errno, |
---|
699 | "write(%s)",libtrace->uridata); |
---|
700 | return -1; |
---|
701 | } |
---|
702 | |
---|
703 | return numbytes + pad + dag_record_size; |
---|
704 | } |
---|
705 | |
---|
706 | static int erf_start_output(libtrace_out_t *libtrace) |
---|
707 | { |
---|
708 | OUTPUT.file = trace_open_file_out(libtrace, |
---|
709 | OPTIONS.erf.level, |
---|
710 | OPTIONS.erf.fileflag); |
---|
711 | if (!OUTPUT.file) { |
---|
712 | return -1; |
---|
713 | } |
---|
714 | return 0; |
---|
715 | } |
---|
716 | |
---|
717 | static int erf_write_packet(libtrace_out_t *libtrace, |
---|
718 | const libtrace_packet_t *packet) |
---|
719 | { |
---|
720 | int numbytes = 0; |
---|
721 | int pad = 0; |
---|
722 | dag_record_t *dag_hdr = (dag_record_t *)packet->header; |
---|
723 | void *payload = packet->payload; |
---|
724 | |
---|
725 | assert(OUTPUT.file); |
---|
726 | |
---|
727 | pad = erf_get_padding(packet); |
---|
728 | |
---|
729 | /* If we've had an rxerror, we have no payload to write - fix rlen to |
---|
730 | * be the correct length */ |
---|
731 | if (payload == NULL) { |
---|
732 | dag_hdr->rlen = htons(dag_record_size + pad); |
---|
733 | } |
---|
734 | |
---|
735 | if (libtrace->format == &erf |
---|
736 | #if HAVE_DAG |
---|
737 | || libtrace->format == &dag |
---|
738 | #endif |
---|
739 | ) { |
---|
740 | numbytes = erf_dump_packet(libtrace, |
---|
741 | (dag_record_t *)packet->buffer, |
---|
742 | pad, |
---|
743 | payload, |
---|
744 | trace_get_capture_length(packet) |
---|
745 | ); |
---|
746 | } else { |
---|
747 | dag_record_t erfhdr; |
---|
748 | int type; |
---|
749 | /* convert format - build up a new erf header */ |
---|
750 | /* Timestamp */ |
---|
751 | erfhdr.ts = trace_get_erf_timestamp(packet); |
---|
752 | type=libtrace_to_erf_type(trace_get_link_type(packet)); |
---|
753 | if (type==(char)-1) { |
---|
754 | trace_set_err_out(libtrace,TRACE_ERR_BAD_PACKET, |
---|
755 | "No erf type for packet"); |
---|
756 | return -1; |
---|
757 | } |
---|
758 | erfhdr.type = type; |
---|
759 | /* Flags. Can't do this */ |
---|
760 | memset(&erfhdr.flags,1,sizeof(erfhdr.flags)); |
---|
761 | /* Packet length (rlen includes format overhead) */ |
---|
762 | erfhdr.rlen = trace_get_capture_length(packet) |
---|
763 | + erf_get_framing_length(packet); |
---|
764 | /* loss counter. Can't do this */ |
---|
765 | erfhdr.lctr = 0; |
---|
766 | /* Wire length */ |
---|
767 | erfhdr.wlen = trace_get_wire_length(packet); |
---|
768 | |
---|
769 | /* Write it out */ |
---|
770 | numbytes = erf_dump_packet(libtrace, |
---|
771 | &erfhdr, |
---|
772 | pad, |
---|
773 | payload, |
---|
774 | trace_get_capture_length(packet)); |
---|
775 | } |
---|
776 | return numbytes; |
---|
777 | } |
---|
778 | |
---|
779 | static libtrace_linktype_t erf_get_link_type(const libtrace_packet_t *packet) { |
---|
780 | dag_record_t *erfptr = 0; |
---|
781 | erfptr = (dag_record_t *)packet->header; |
---|
782 | return erf_type_to_libtrace(erfptr->type); |
---|
783 | } |
---|
784 | |
---|
785 | static int8_t erf_get_direction(const libtrace_packet_t *packet) { |
---|
786 | dag_record_t *erfptr = 0; |
---|
787 | erfptr = (dag_record_t *)packet->header; |
---|
788 | return erfptr->flags.iface; |
---|
789 | } |
---|
790 | |
---|
791 | static int8_t erf_set_direction(const libtrace_packet_t *packet, int8_t direction) { |
---|
792 | dag_record_t *erfptr = 0; |
---|
793 | erfptr = (dag_record_t *)packet->header; |
---|
794 | erfptr->flags.iface = direction; |
---|
795 | return erfptr->flags.iface; |
---|
796 | } |
---|
797 | |
---|
798 | static uint64_t erf_get_erf_timestamp(const libtrace_packet_t *packet) { |
---|
799 | dag_record_t *erfptr = 0; |
---|
800 | erfptr = (dag_record_t *)packet->header; |
---|
801 | return erfptr->ts; |
---|
802 | } |
---|
803 | |
---|
804 | static int erf_get_capture_length(const libtrace_packet_t *packet) { |
---|
805 | dag_record_t *erfptr = 0; |
---|
806 | erfptr = (dag_record_t *)packet->header; |
---|
807 | return (ntohs(erfptr->rlen) - erf_get_framing_length(packet)); |
---|
808 | } |
---|
809 | |
---|
810 | static int erf_get_wire_length(const libtrace_packet_t *packet) { |
---|
811 | dag_record_t *erfptr = 0; |
---|
812 | erfptr = (dag_record_t *)packet->header; |
---|
813 | return ntohs(erfptr->wlen); |
---|
814 | } |
---|
815 | |
---|
816 | static size_t erf_set_capture_length(libtrace_packet_t *packet, size_t size) { |
---|
817 | dag_record_t *erfptr = 0; |
---|
818 | assert(packet); |
---|
819 | if(size > trace_get_capture_length(packet)) { |
---|
820 | /* can't make a packet larger */ |
---|
821 | return trace_get_capture_length(packet); |
---|
822 | } |
---|
823 | erfptr = (dag_record_t *)packet->header; |
---|
824 | erfptr->rlen = htons(size + erf_get_framing_length(packet)); |
---|
825 | return trace_get_capture_length(packet); |
---|
826 | } |
---|
827 | |
---|
828 | static int rtclient_get_fd(const libtrace_t *libtrace) { |
---|
829 | return INPUT.fd; |
---|
830 | } |
---|
831 | |
---|
832 | #ifdef HAVE_DAG |
---|
833 | libtrace_eventobj_t trace_event_dag(libtrace_t *trace, libtrace_packet_t *packet) { |
---|
834 | libtrace_eventobj_t event = {0,0,0.0,0}; |
---|
835 | int dag_fd; |
---|
836 | int data; |
---|
837 | |
---|
838 | if (trace->format->get_fd) { |
---|
839 | dag_fd = trace->format->get_fd(trace); |
---|
840 | } else { |
---|
841 | dag_fd = 0; |
---|
842 | } |
---|
843 | |
---|
844 | data = dag_read(trace, DAGF_NONBLOCK); |
---|
845 | |
---|
846 | if (data > 0) { |
---|
847 | event.size = trace_read_packet(trace,packet); |
---|
848 | event.type = TRACE_EVENT_PACKET; |
---|
849 | return event; |
---|
850 | } |
---|
851 | event.type = TRACE_EVENT_SLEEP; |
---|
852 | event.seconds = 0.0001; |
---|
853 | return event; |
---|
854 | } |
---|
855 | #endif |
---|
856 | |
---|
857 | #if HAVE_DAG |
---|
858 | static void dag_help() { |
---|
859 | printf("dag format module: $Revision$\n"); |
---|
860 | printf("Supported input URIs:\n"); |
---|
861 | printf("\tdag:/dev/dagn\n"); |
---|
862 | printf("\n"); |
---|
863 | printf("\te.g.: dag:/dev/dag0\n"); |
---|
864 | printf("\n"); |
---|
865 | printf("Supported output URIs:\n"); |
---|
866 | printf("\tnone\n"); |
---|
867 | printf("\n"); |
---|
868 | } |
---|
869 | #endif |
---|
870 | |
---|
871 | static void erf_help() { |
---|
872 | printf("erf format module: $Revision$\n"); |
---|
873 | printf("Supported input URIs:\n"); |
---|
874 | printf("\terf:/path/to/file\t(uncompressed)\n"); |
---|
875 | printf("\terf:/path/to/file.gz\t(gzip-compressed)\n"); |
---|
876 | printf("\terf:-\t(stdin, either compressed or not)\n"); |
---|
877 | printf("\terf:/path/to/socket\n"); |
---|
878 | printf("\n"); |
---|
879 | printf("\te.g.: erf:/tmp/trace\n"); |
---|
880 | printf("\n"); |
---|
881 | printf("Supported output URIs:\n"); |
---|
882 | printf("\terf:path/to/file\t(uncompressed)\n"); |
---|
883 | printf("\terf:/path/to/file.gz\t(gzip-compressed)\n"); |
---|
884 | printf("\terf:-\t(stdout, either compressed or not)\n"); |
---|
885 | printf("\n"); |
---|
886 | printf("\te.g.: erf:/tmp/trace\n"); |
---|
887 | printf("\n"); |
---|
888 | printf("Supported output options:\n"); |
---|
889 | printf("\t-z\tSpecify the gzip compression, ranging from 0 (uncompressed) to 9 - defaults to 1\n"); |
---|
890 | printf("\n"); |
---|
891 | |
---|
892 | |
---|
893 | } |
---|
894 | |
---|
895 | static void rtclient_help() { |
---|
896 | printf("rtclient format module: $Revision$\n"); |
---|
897 | printf("DEPRECATED - use rt module instead\n"); |
---|
898 | printf("Supported input URIs:\n"); |
---|
899 | printf("\trtclient:host:port\n"); |
---|
900 | printf("\n"); |
---|
901 | printf("\te.g.:rtclient:localhost:3435\n"); |
---|
902 | printf("\n"); |
---|
903 | printf("Supported output URIs:\n"); |
---|
904 | printf("\tnone\n"); |
---|
905 | printf("\n"); |
---|
906 | } |
---|
907 | |
---|
908 | static struct libtrace_format_t erf = { |
---|
909 | "erf", |
---|
910 | "$Id$", |
---|
911 | TRACE_FORMAT_ERF, |
---|
912 | erf_init_input, /* init_input */ |
---|
913 | NULL, /* config_input */ |
---|
914 | erf_start_input, /* start_input */ |
---|
915 | NULL, /* pause_input */ |
---|
916 | erf_init_output, /* init_output */ |
---|
917 | erf_config_output, /* config_output */ |
---|
918 | erf_start_output, /* start_output */ |
---|
919 | erf_fin_input, /* fin_input */ |
---|
920 | erf_fin_output, /* fin_output */ |
---|
921 | erf_read_packet, /* read_packet */ |
---|
922 | NULL, /* fin_packet */ |
---|
923 | erf_write_packet, /* write_packet */ |
---|
924 | erf_get_link_type, /* get_link_type */ |
---|
925 | erf_get_direction, /* get_direction */ |
---|
926 | erf_set_direction, /* set_direction */ |
---|
927 | erf_get_erf_timestamp, /* get_erf_timestamp */ |
---|
928 | NULL, /* get_timeval */ |
---|
929 | NULL, /* get_seconds */ |
---|
930 | erf_seek_erf, /* seek_erf */ |
---|
931 | NULL, /* seek_timeval */ |
---|
932 | NULL, /* seek_seconds */ |
---|
933 | erf_get_capture_length, /* get_capture_length */ |
---|
934 | erf_get_wire_length, /* get_wire_length */ |
---|
935 | erf_get_framing_length, /* get_framing_length */ |
---|
936 | erf_set_capture_length, /* set_capture_length */ |
---|
937 | NULL, /* get_fd */ |
---|
938 | trace_event_trace, /* trace_event */ |
---|
939 | erf_help, /* help */ |
---|
940 | NULL /* next pointer */ |
---|
941 | }; |
---|
942 | |
---|
943 | #ifdef HAVE_DAG |
---|
944 | static struct libtrace_format_t dag = { |
---|
945 | "dag", |
---|
946 | "$Id$", |
---|
947 | TRACE_FORMAT_ERF, |
---|
948 | dag_init_input, /* init_input */ |
---|
949 | NULL, /* config_input */ |
---|
950 | dag_start_input, /* start_input */ |
---|
951 | dag_pause_input, /* pause_input */ |
---|
952 | NULL, /* init_output */ |
---|
953 | NULL, /* config_output */ |
---|
954 | NULL, /* start_output */ |
---|
955 | dag_fin_input, /* fin_input */ |
---|
956 | NULL, /* fin_output */ |
---|
957 | dag_read_packet, /* read_packet */ |
---|
958 | NULL, /* fin_packet */ |
---|
959 | NULL, /* write_packet */ |
---|
960 | erf_get_link_type, /* get_link_type */ |
---|
961 | erf_get_direction, /* get_direction */ |
---|
962 | erf_set_direction, /* set_direction */ |
---|
963 | erf_get_erf_timestamp, /* get_erf_timestamp */ |
---|
964 | NULL, /* get_timeval */ |
---|
965 | NULL, /* get_seconds */ |
---|
966 | NULL, /* seek_erf */ |
---|
967 | NULL, /* seek_timeval */ |
---|
968 | NULL, /* seek_seconds */ |
---|
969 | erf_get_capture_length, /* get_capture_length */ |
---|
970 | erf_get_wire_length, /* get_wire_length */ |
---|
971 | erf_get_framing_length, /* get_framing_length */ |
---|
972 | erf_set_capture_length, /* set_capture_length */ |
---|
973 | NULL, /* get_fd */ |
---|
974 | trace_event_dag, /* trace_event */ |
---|
975 | dag_help, /* help */ |
---|
976 | NULL /* next pointer */ |
---|
977 | }; |
---|
978 | #endif |
---|
979 | |
---|
980 | static struct libtrace_format_t rtclient = { |
---|
981 | "rtclient", |
---|
982 | "$Id$", |
---|
983 | TRACE_FORMAT_ERF, |
---|
984 | rtclient_init_input, /* init_input */ |
---|
985 | NULL, /* config_input */ |
---|
986 | rtclient_start_input, /* start_input */ |
---|
987 | rtclient_pause_input, /* pause_input */ |
---|
988 | NULL, /* init_output */ |
---|
989 | NULL, /* config_output */ |
---|
990 | NULL, /* start_output */ |
---|
991 | rtclient_fin_input, /* fin_input */ |
---|
992 | NULL, /* fin_output */ |
---|
993 | rtclient_read_packet, /* read_packet */ |
---|
994 | NULL, /* fin_packet */ |
---|
995 | NULL, /* write_packet */ |
---|
996 | erf_get_link_type, /* get_link_type */ |
---|
997 | erf_get_direction, /* get_direction */ |
---|
998 | erf_set_direction, /* set_direction */ |
---|
999 | erf_get_erf_timestamp, /* get_erf_timestamp */ |
---|
1000 | NULL, /* get_timeval */ |
---|
1001 | NULL, /* get_seconds */ |
---|
1002 | NULL, /* seek_erf */ |
---|
1003 | NULL, /* seek_timeval */ |
---|
1004 | NULL, /* seek_seconds */ |
---|
1005 | erf_get_capture_length, /* get_capture_length */ |
---|
1006 | erf_get_wire_length, /* get_wire_length */ |
---|
1007 | erf_get_framing_length, /* get_framing_length */ |
---|
1008 | erf_set_capture_length, /* set_capture_length */ |
---|
1009 | rtclient_get_fd, /* get_fd */ |
---|
1010 | trace_event_device, /* trace_event */ |
---|
1011 | rtclient_help, /* help */ |
---|
1012 | NULL /* next pointer */ |
---|
1013 | }; |
---|
1014 | |
---|
1015 | void __attribute__((constructor)) erf_constructor() { |
---|
1016 | register_format(&rtclient); |
---|
1017 | register_format(&erf); |
---|
1018 | #ifdef HAVE_DAG |
---|
1019 | register_format(&dag); |
---|
1020 | #endif |
---|
1021 | } |
---|