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 | |
---|
31 | #include "libtrace.h" |
---|
32 | #include "libtrace_int.h" |
---|
33 | #include "rtserver.h" |
---|
34 | #include "parse_cmd.h" |
---|
35 | |
---|
36 | #ifdef HAVE_INTTYPES_H |
---|
37 | # include <inttypes.h> |
---|
38 | #else |
---|
39 | # error "Can't find inttypes.h - this needs to be fixed" |
---|
40 | #endif |
---|
41 | |
---|
42 | #ifdef HAVE_STDDEF_H |
---|
43 | # include <stddef.h> |
---|
44 | #else |
---|
45 | # error "Can't find stddef.h - do you define ptrdiff_t elsewhere?" |
---|
46 | #endif |
---|
47 | #include <sys/types.h> |
---|
48 | #include <sys/socket.h> |
---|
49 | #include <sys/un.h> |
---|
50 | #include <sys/mman.h> |
---|
51 | #include <sys/stat.h> |
---|
52 | #include <unistd.h> |
---|
53 | #include <assert.h> |
---|
54 | #include <errno.h> |
---|
55 | #include <netdb.h> |
---|
56 | #include <fcntl.h> |
---|
57 | #include <getopt.h> |
---|
58 | |
---|
59 | /* Catch undefined O_LARGEFILE on *BSD etc */ |
---|
60 | #ifndef O_LARGEFILE |
---|
61 | # define O_LARGEFILE 0 |
---|
62 | #endif |
---|
63 | |
---|
64 | #define CONNINFO libtrace->format_data->conn_info |
---|
65 | #define INPUT libtrace->format_data->input |
---|
66 | #define DAG libtrace->format_data->dag |
---|
67 | struct libtrace_format_data_t { |
---|
68 | union { |
---|
69 | /** Information about rtclients */ |
---|
70 | struct { |
---|
71 | char *hostname; |
---|
72 | short port; |
---|
73 | } rt; |
---|
74 | char *path; /**< information for local sockets */ |
---|
75 | } conn_info; |
---|
76 | /** Information about the current state of the input device */ |
---|
77 | union { |
---|
78 | int fd; |
---|
79 | #if HAVE_ZLIB |
---|
80 | gzFile *file; |
---|
81 | #else |
---|
82 | FILE *file; |
---|
83 | #endif |
---|
84 | } input; |
---|
85 | |
---|
86 | struct { |
---|
87 | void *buf; |
---|
88 | unsigned bottom; |
---|
89 | unsigned top; |
---|
90 | unsigned diff; |
---|
91 | unsigned curr; |
---|
92 | unsigned offset; |
---|
93 | } dag; |
---|
94 | }; |
---|
95 | |
---|
96 | struct libtrace_format_data_out_t { |
---|
97 | union { |
---|
98 | struct { |
---|
99 | char *hostname; |
---|
100 | short port; |
---|
101 | } rt; |
---|
102 | char *path; |
---|
103 | } conn_info; |
---|
104 | |
---|
105 | union { |
---|
106 | struct { |
---|
107 | int level; |
---|
108 | } erf; |
---|
109 | |
---|
110 | } options; |
---|
111 | |
---|
112 | union { |
---|
113 | int fd; |
---|
114 | struct rtserver_t * rtserver; |
---|
115 | #if HAVE_ZLIB |
---|
116 | gzFile *file; |
---|
117 | #else |
---|
118 | FILE *file; |
---|
119 | #endif |
---|
120 | } output; |
---|
121 | }; |
---|
122 | |
---|
123 | #ifdef HAVE_DAG |
---|
124 | static int dag_init_input(struct libtrace_t *libtrace) { |
---|
125 | struct stat buf; |
---|
126 | libtrace->format_data = (struct libtrace_format_data_t *) |
---|
127 | malloc(sizeof(struct libtrace_format_data_t)); |
---|
128 | |
---|
129 | CONNINFO.path = libtrace->uridata; |
---|
130 | if (stat(CONNINFO.path,&buf) == -1) { |
---|
131 | perror("stat"); |
---|
132 | return 0; |
---|
133 | } |
---|
134 | if (S_ISCHR(buf.st_mode)) { |
---|
135 | // DEVICE |
---|
136 | if((INPUT.fd = |
---|
137 | dag_open(CONNINFO.path)) < 0) { |
---|
138 | fprintf(stderr,"Cannot open DAG %s: %m\n", |
---|
139 | CONNINFO.path,errno); |
---|
140 | exit(0); |
---|
141 | } |
---|
142 | if((DAG.buf = (void *) |
---|
143 | dag_mmap(INPUT.fd)) == MAP_FAILED) { |
---|
144 | fprintf(stderr,"Cannot mmap DAG %s: %m\n", |
---|
145 | CONNINFO.path,errno); |
---|
146 | exit(0); |
---|
147 | } |
---|
148 | if(dag_start(INPUT.fd) < 0) { |
---|
149 | fprintf(stderr,"Cannot start DAG %s: %m\n", |
---|
150 | CONNINFO.path,errno); |
---|
151 | exit(0); |
---|
152 | } |
---|
153 | } else { |
---|
154 | fprintf(stderr,"%s isn't a valid char device, exiting\n", |
---|
155 | CONNINFO.path); |
---|
156 | return 0; |
---|
157 | } |
---|
158 | } |
---|
159 | #endif |
---|
160 | |
---|
161 | static int erf_init_input(struct libtrace_t *libtrace) { |
---|
162 | struct stat buf; |
---|
163 | struct hostent *he; |
---|
164 | struct sockaddr_in remote; |
---|
165 | struct sockaddr_un unix_sock; |
---|
166 | libtrace->format_data = (struct libtrace_format_data_t *) |
---|
167 | malloc(sizeof(struct libtrace_format_data_t)); |
---|
168 | |
---|
169 | CONNINFO.path = libtrace->uridata; |
---|
170 | if (!strncmp(CONNINFO.path,"-",1)) { |
---|
171 | // STDIN |
---|
172 | #if HAVE_ZLIB |
---|
173 | INPUT.file = gzdopen(STDIN, "r"); |
---|
174 | #else |
---|
175 | INPUT.file = stdin; |
---|
176 | #endif |
---|
177 | |
---|
178 | } else { |
---|
179 | if (stat(CONNINFO.path,&buf) == -1 ) { |
---|
180 | perror("stat"); |
---|
181 | return 0; |
---|
182 | } |
---|
183 | if (S_ISSOCK(buf.st_mode)) { |
---|
184 | // SOCKET |
---|
185 | if ((INPUT.fd = socket( |
---|
186 | AF_UNIX, SOCK_STREAM, 0)) == -1) { |
---|
187 | perror("socket"); |
---|
188 | return 0; |
---|
189 | } |
---|
190 | unix_sock.sun_family = AF_UNIX; |
---|
191 | bzero(unix_sock.sun_path,108); |
---|
192 | snprintf(unix_sock.sun_path, |
---|
193 | 108,"%s" |
---|
194 | ,CONNINFO.path); |
---|
195 | |
---|
196 | if (connect(INPUT.fd, |
---|
197 | (struct sockaddr *)&unix_sock, |
---|
198 | sizeof(struct sockaddr)) == -1) { |
---|
199 | perror("connect (unix)"); |
---|
200 | return 0; |
---|
201 | } |
---|
202 | } else { |
---|
203 | // TRACE |
---|
204 | #if HAVE_ZLIB |
---|
205 | // using gzdopen means we can set O_LARGEFILE |
---|
206 | // ourselves. However, this way is messy and |
---|
207 | // we lose any error checking on "open" |
---|
208 | INPUT.file = |
---|
209 | gzdopen(open( |
---|
210 | CONNINFO.path, |
---|
211 | O_LARGEFILE), "r"); |
---|
212 | #else |
---|
213 | INPUT.file = |
---|
214 | fdopen(open( |
---|
215 | CONNINFO.path, |
---|
216 | O_LARGEFILE), "r"); |
---|
217 | #endif |
---|
218 | |
---|
219 | } |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | static int rtclient_init_input(struct libtrace_t *libtrace) { |
---|
224 | char *scan; |
---|
225 | char *uridata = libtrace->uridata; |
---|
226 | struct hostent *he; |
---|
227 | struct sockaddr_in remote; |
---|
228 | libtrace->format_data = (struct libtrace_format_data_t *) |
---|
229 | malloc(sizeof(struct libtrace_format_data_t)); |
---|
230 | |
---|
231 | if (strlen(uridata) == 0) { |
---|
232 | CONNINFO.rt.hostname = |
---|
233 | strdup("localhost"); |
---|
234 | CONNINFO.rt.port = |
---|
235 | COLLECTOR_PORT; |
---|
236 | } else { |
---|
237 | if ((scan = strchr(uridata,':')) == NULL) { |
---|
238 | CONNINFO.rt.hostname = |
---|
239 | strdup(uridata); |
---|
240 | CONNINFO.rt.port = |
---|
241 | COLLECTOR_PORT; |
---|
242 | } else { |
---|
243 | CONNINFO.rt.hostname = |
---|
244 | (char *)strndup(uridata, |
---|
245 | (scan - uridata)); |
---|
246 | CONNINFO.rt.port = |
---|
247 | atoi(++scan); |
---|
248 | } |
---|
249 | } |
---|
250 | |
---|
251 | if ((he=gethostbyname(CONNINFO.rt.hostname)) == NULL) { |
---|
252 | perror("gethostbyname"); |
---|
253 | return 0; |
---|
254 | } |
---|
255 | if ((INPUT.fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { |
---|
256 | perror("socket"); |
---|
257 | return 0; |
---|
258 | } |
---|
259 | |
---|
260 | remote.sin_family = AF_INET; |
---|
261 | remote.sin_port = htons(CONNINFO.rt.port); |
---|
262 | remote.sin_addr = *((struct in_addr *)he->h_addr); |
---|
263 | bzero(&(remote.sin_zero), 8); |
---|
264 | |
---|
265 | if (connect(INPUT.fd, (struct sockaddr *)&remote, |
---|
266 | sizeof(struct sockaddr)) == -1) { |
---|
267 | perror("connect (inet)"); |
---|
268 | return 0; |
---|
269 | } |
---|
270 | } |
---|
271 | |
---|
272 | static int erf_init_output(struct libtrace_out_t *libtrace) { |
---|
273 | char *filemode = 0; |
---|
274 | libtrace->format_data = (struct libtrace_format_data_out_t *) |
---|
275 | calloc(1,sizeof(struct libtrace_format_data_out_t)); |
---|
276 | |
---|
277 | libtrace->format_data->options.erf.level = 1; |
---|
278 | asprintf(&filemode,"wb%d",libtrace->format_data->options.erf.level); |
---|
279 | |
---|
280 | if (!strncmp(libtrace->uridata,"-",1)) { |
---|
281 | // STDOUT |
---|
282 | #if HAVE_ZLIB |
---|
283 | libtrace->format_data->output.file = gzdopen(dup(1), filemode); |
---|
284 | #else |
---|
285 | libtrace->format_data->output.file = stdout; |
---|
286 | #endif |
---|
287 | } |
---|
288 | else { |
---|
289 | // TRACE |
---|
290 | #if HAVE_ZLIB |
---|
291 | // using gzdopen means we can set O_LARGEFILE |
---|
292 | // ourselves. However, this way is messy and |
---|
293 | // we lose any error checking on "open" |
---|
294 | libtrace->format_data->output.file = gzdopen(open( |
---|
295 | libtrace->uridata, |
---|
296 | O_CREAT | O_LARGEFILE | O_WRONLY, |
---|
297 | S_IRUSR | S_IWUSR), filemode); |
---|
298 | #else |
---|
299 | libtrace->format_data->output.file = fdopen(open( |
---|
300 | libtrace->uridata, |
---|
301 | O_CREAT | O_LARGEFILE | O_WRONLY, |
---|
302 | S_IRUSR | S_IWUSR), "w"); |
---|
303 | #endif |
---|
304 | } |
---|
305 | |
---|
306 | |
---|
307 | } |
---|
308 | |
---|
309 | static int rtclient_init_output(struct libtrace_out_t *libtrace) { |
---|
310 | char * uridata = libtrace->uridata; |
---|
311 | char * scan; |
---|
312 | libtrace->format_data = (struct libtrace_format_data_out_t *) |
---|
313 | calloc(1,sizeof(struct libtrace_format_data_out_t)); |
---|
314 | // extract conn_info from uridata |
---|
315 | if (strlen(uridata) == 0) { |
---|
316 | libtrace->format_data->conn_info.rt.hostname = |
---|
317 | strdup("localhost"); |
---|
318 | libtrace->format_data->conn_info.rt.port = COLLECTOR_PORT; |
---|
319 | } |
---|
320 | else { |
---|
321 | if ((scan = strchr(uridata,':')) == NULL) { |
---|
322 | libtrace->format_data->conn_info.rt.hostname = |
---|
323 | strdup(uridata); |
---|
324 | libtrace->format_data->conn_info.rt.port = |
---|
325 | COLLECTOR_PORT; |
---|
326 | } else { |
---|
327 | libtrace->format_data->conn_info.rt.hostname = |
---|
328 | (char *)strndup(uridata, |
---|
329 | (scan - uridata)); |
---|
330 | libtrace->format_data->conn_info.rt.port = |
---|
331 | atoi(++scan); |
---|
332 | } |
---|
333 | } |
---|
334 | |
---|
335 | |
---|
336 | libtrace->format_data->output.rtserver = |
---|
337 | rtserver_create(libtrace->format_data->conn_info.rt.hostname, |
---|
338 | libtrace->format_data->conn_info.rt.port); |
---|
339 | if (!libtrace->format_data->output.rtserver) |
---|
340 | return 0; |
---|
341 | |
---|
342 | } |
---|
343 | |
---|
344 | static int erf_config_output(struct libtrace_out_t *libtrace, int argc, char *argv[]) { |
---|
345 | int opt; |
---|
346 | int level = libtrace->format_data->options.erf.level; |
---|
347 | optind = 1; |
---|
348 | |
---|
349 | while ((opt = getopt(argc, argv, "z:")) != EOF) { |
---|
350 | switch (opt) { |
---|
351 | case 'z': |
---|
352 | level = atoi(optarg); |
---|
353 | break; |
---|
354 | default: |
---|
355 | printf("Bad argument to erf: %s\n", opt); |
---|
356 | // maybe spit out some help here |
---|
357 | return -1; |
---|
358 | } |
---|
359 | } |
---|
360 | if (level != libtrace->format_data->options.erf.level) { |
---|
361 | if (level > 9 || level < 0) { |
---|
362 | // retarded level choice |
---|
363 | printf("Compression level must be between 0 and 9 inclusive - you selected %i \n", level); |
---|
364 | |
---|
365 | } else { |
---|
366 | libtrace->format_data->options.erf.level = level; |
---|
367 | return gzsetparams(libtrace->format_data->output.file, level, Z_DEFAULT_STRATEGY); |
---|
368 | } |
---|
369 | } |
---|
370 | return 0; |
---|
371 | |
---|
372 | } |
---|
373 | |
---|
374 | static int rtclient_config_output(struct libtrace_out_t *libtrace, int argc, char *argv[]) { |
---|
375 | return 0; |
---|
376 | } |
---|
377 | |
---|
378 | #ifdef HAVE_DAG |
---|
379 | static int dag_fin_input(struct libtrace_t *libtrace) { |
---|
380 | dag_stop(INPUT.fd); |
---|
381 | } |
---|
382 | #endif |
---|
383 | |
---|
384 | static int erf_fin_input(struct libtrace_t *libtrace) { |
---|
385 | #if HAVE_ZLIB |
---|
386 | gzclose(INPUT.file); |
---|
387 | #else |
---|
388 | fclose(INPUT.file); |
---|
389 | #endif |
---|
390 | } |
---|
391 | |
---|
392 | static int rtclient_fin_input(struct libtrace_t *libtrace) { |
---|
393 | close(INPUT.fd); |
---|
394 | } |
---|
395 | |
---|
396 | static int erf_fin_output(struct libtrace_out_t *libtrace) { |
---|
397 | #if HAVE_ZLIB |
---|
398 | gzclose(libtrace->format_data->output.file); |
---|
399 | #else |
---|
400 | fclose(libtrace->format_data->output.file); |
---|
401 | #endif |
---|
402 | } |
---|
403 | |
---|
404 | |
---|
405 | static int rtclient_fin_output(struct libtrace_out_t *libtrace) { |
---|
406 | rtserver_destroy(libtrace->format_data->output.rtserver); |
---|
407 | } |
---|
408 | |
---|
409 | #if HAVE_DAG |
---|
410 | static int dag_read(struct libtrace_t *libtrace, void *buffer, size_t len) { |
---|
411 | int numbytes; |
---|
412 | static short lctr = 0; |
---|
413 | struct dag_record_t *erfptr = 0; |
---|
414 | int rlen; |
---|
415 | |
---|
416 | if (buffer == 0) |
---|
417 | buffer = malloc(len); |
---|
418 | |
---|
419 | DAG.bottom = DAG.top; |
---|
420 | DAG.top = dag_offset( |
---|
421 | INPUT.fd, |
---|
422 | &(DAG.bottom), |
---|
423 | 0); |
---|
424 | DAG.diff = DAG.top - |
---|
425 | DAG.bottom; |
---|
426 | |
---|
427 | numbytes=DAG.diff; |
---|
428 | DAG.offset = 0; |
---|
429 | return numbytes; |
---|
430 | } |
---|
431 | #endif |
---|
432 | |
---|
433 | #if HAVE_DAG |
---|
434 | static int dag_read_packet(struct libtrace_t *libtrace, struct libtrace_packet_t *packet) { |
---|
435 | int numbytes; |
---|
436 | int size; |
---|
437 | char buf[RP_BUFSIZE]; |
---|
438 | dag_record_t *erfptr; |
---|
439 | void *buffer = packet->buffer; |
---|
440 | void *buffer2 = buffer; |
---|
441 | int rlen; |
---|
442 | |
---|
443 | if (libtrace->dag.diff == 0) { |
---|
444 | if ((numbytes = dag_read(libtrace,buf,RP_BUFSIZE)) <= 0) |
---|
445 | return numbytes; |
---|
446 | } |
---|
447 | |
---|
448 | //DAG always gives us whole packets |
---|
449 | erfptr = (dag_record_t *) ((void *)libtrace->dag.buf + |
---|
450 | (libtrace->dag.bottom + libtrace->dag.offset)); |
---|
451 | size = ntohs(erfptr->rlen); |
---|
452 | |
---|
453 | if ( size > LIBTRACE_PACKET_BUFSIZE) { |
---|
454 | printf("%d\n",size); |
---|
455 | assert( size < LIBTRACE_PACKET_BUFSIZE); |
---|
456 | } |
---|
457 | |
---|
458 | // have to copy it out of the memory hole at this stage: |
---|
459 | memcpy(packet->buffer, erfptr, size); |
---|
460 | |
---|
461 | packet->size = size; |
---|
462 | libtrace->dag.offset += size; |
---|
463 | libtrace->dag.diff -= size; |
---|
464 | |
---|
465 | assert(libtrace->dag.diff >= 0); |
---|
466 | |
---|
467 | return (size); |
---|
468 | } |
---|
469 | #endif |
---|
470 | |
---|
471 | static int erf_read_packet(struct libtrace_t *libtrace, struct libtrace_packet_t *packet) { |
---|
472 | int numbytes; |
---|
473 | int size; |
---|
474 | char buf[RP_BUFSIZE]; |
---|
475 | dag_record_t *erfptr; |
---|
476 | void *buffer = packet->buffer; |
---|
477 | void *buffer2 = buffer; |
---|
478 | int rlen; |
---|
479 | |
---|
480 | if ((numbytes=gzread(INPUT.file, |
---|
481 | buffer, |
---|
482 | dag_record_size)) == -1) { |
---|
483 | perror("gzread"); |
---|
484 | return -1; |
---|
485 | } |
---|
486 | if (numbytes == 0) { |
---|
487 | return 0; |
---|
488 | } |
---|
489 | rlen = ntohs(((dag_record_t *)buffer)->rlen); |
---|
490 | size = rlen - dag_record_size; |
---|
491 | assert(size < LIBTRACE_PACKET_BUFSIZE); |
---|
492 | buffer2 = buffer + dag_record_size; |
---|
493 | |
---|
494 | // read in the rest of the packet |
---|
495 | if ((numbytes=gzread(INPUT.file, |
---|
496 | buffer2, |
---|
497 | size)) == -1) { |
---|
498 | perror("gzread"); |
---|
499 | return -1; |
---|
500 | } |
---|
501 | packet->size = rlen; |
---|
502 | return rlen; |
---|
503 | } |
---|
504 | |
---|
505 | static int rtclient_read(struct libtrace_t *libtrace, void *buffer, size_t len) { |
---|
506 | int numbytes; |
---|
507 | static short lctr = 0; |
---|
508 | struct dag_record_t *erfptr = 0; |
---|
509 | int rlen; |
---|
510 | |
---|
511 | if (buffer == 0) |
---|
512 | buffer = malloc(len); |
---|
513 | while(1) { |
---|
514 | #ifndef MSG_NOSIGNAL |
---|
515 | # define MSG_NOSIGNAL 0 |
---|
516 | #endif |
---|
517 | if ((numbytes = recv(INPUT.fd, |
---|
518 | buffer, |
---|
519 | len, |
---|
520 | MSG_NOSIGNAL)) == -1) { |
---|
521 | if (errno == EINTR) { |
---|
522 | //ignore EINTR in case |
---|
523 | // a caller is using signals |
---|
524 | continue; |
---|
525 | } |
---|
526 | perror("recv"); |
---|
527 | return -1; |
---|
528 | } |
---|
529 | break; |
---|
530 | |
---|
531 | } |
---|
532 | return numbytes; |
---|
533 | } |
---|
534 | |
---|
535 | static int rtclient_read_packet(struct libtrace_t *libtrace, struct libtrace_packet_t *packet) { |
---|
536 | int numbytes; |
---|
537 | int size; |
---|
538 | char buf[RP_BUFSIZE]; |
---|
539 | dag_record_t *erfptr; |
---|
540 | int read_required = 0; |
---|
541 | |
---|
542 | void *buffer = 0; |
---|
543 | buffer = packet->buffer; |
---|
544 | |
---|
545 | do { |
---|
546 | if (fifo_out_available(libtrace->fifo) == 0 || read_required) { |
---|
547 | if ((numbytes = rtclient_read( |
---|
548 | libtrace,buf,RP_BUFSIZE))<=0) { |
---|
549 | return numbytes; |
---|
550 | } |
---|
551 | assert(libtrace->fifo); |
---|
552 | fifo_write(libtrace->fifo,buf,numbytes); |
---|
553 | read_required = 0; |
---|
554 | } |
---|
555 | // Read status byte |
---|
556 | if (fifo_out_read(libtrace->fifo, |
---|
557 | &packet->status, sizeof(int)) == 0) { |
---|
558 | read_required = 1; |
---|
559 | continue; |
---|
560 | } |
---|
561 | fifo_out_update(libtrace->fifo,sizeof(int)); |
---|
562 | |
---|
563 | // read in the ERF header |
---|
564 | if ((numbytes = fifo_out_read(libtrace->fifo, buffer, |
---|
565 | sizeof(dag_record_t))) == 0) { |
---|
566 | fifo_out_reset(libtrace->fifo); |
---|
567 | read_required = 1; |
---|
568 | continue; |
---|
569 | } |
---|
570 | size = ntohs(((dag_record_t *)buffer)->rlen); |
---|
571 | |
---|
572 | // read in the full packet |
---|
573 | if ((numbytes = fifo_out_read(libtrace->fifo, |
---|
574 | buffer, size)) == 0) { |
---|
575 | fifo_out_reset(libtrace->fifo); |
---|
576 | read_required = 1; |
---|
577 | continue; |
---|
578 | } |
---|
579 | |
---|
580 | // got in our whole packet, so... |
---|
581 | fifo_out_update(libtrace->fifo,size); |
---|
582 | |
---|
583 | fifo_ack_update(libtrace->fifo,size + sizeof(int)); |
---|
584 | |
---|
585 | packet->size = numbytes; |
---|
586 | return numbytes; |
---|
587 | } while(1); |
---|
588 | } |
---|
589 | |
---|
590 | static int erf_write_packet(struct libtrace_out_t *libtrace, struct libtrace_packet_t *packet) { |
---|
591 | int numbytes = 0; |
---|
592 | |
---|
593 | if ((numbytes = gzwrite(libtrace->format_data->output.file, packet->buffer, packet->size)) == 0) { |
---|
594 | perror("gzwrite"); |
---|
595 | return -1; |
---|
596 | } |
---|
597 | return numbytes; |
---|
598 | } |
---|
599 | |
---|
600 | static int rtclient_write_packet(struct libtrace_out_t *libtrace, struct libtrace_packet_t *packet) { |
---|
601 | int numbytes = 0; |
---|
602 | int size; |
---|
603 | int intsize = sizeof(int); |
---|
604 | char buf[RP_BUFSIZE]; |
---|
605 | void *buffer = &buf[intsize]; |
---|
606 | int write_required = 0; |
---|
607 | |
---|
608 | do { |
---|
609 | if (rtserver_checklisten(libtrace->format_data->output.rtserver) < 0) |
---|
610 | return -1; |
---|
611 | |
---|
612 | assert(libtrace->fifo); |
---|
613 | if (fifo_out_available(libtrace->fifo) == 0 || write_required) { |
---|
614 | // Packet added to fifo |
---|
615 | if ((numbytes = fifo_write(libtrace->fifo, packet->buffer, packet->size)) == 0) { |
---|
616 | // some error with the fifo |
---|
617 | perror("fifo_write"); |
---|
618 | return -1; |
---|
619 | } |
---|
620 | write_required = 0; |
---|
621 | } |
---|
622 | |
---|
623 | // Read from fifo and add protocol header |
---|
624 | if ((numbytes = fifo_out_read(libtrace->fifo, buffer, sizeof(dag_record_t))) == 0) { |
---|
625 | // failure reading in from fifo |
---|
626 | fifo_out_reset(libtrace->fifo); |
---|
627 | write_required = 1; |
---|
628 | continue; |
---|
629 | } |
---|
630 | size = ntohs(((dag_record_t *)buffer)->rlen); |
---|
631 | assert(size < LIBTRACE_PACKET_BUFSIZE); |
---|
632 | if ((numbytes = fifo_out_read(libtrace->fifo, buffer, size)) == 0) { |
---|
633 | // failure reading in from fifo |
---|
634 | fifo_out_reset(libtrace->fifo); |
---|
635 | write_required = 1; |
---|
636 | continue; |
---|
637 | } |
---|
638 | // Sort out the protocol header |
---|
639 | memcpy(buf, &packet->status, intsize); |
---|
640 | |
---|
641 | if ((numbytes = rtserver_sendclients(libtrace->format_data->output.rtserver, buf, size + sizeof(int))) < 0) { |
---|
642 | write_required = 0; |
---|
643 | continue; |
---|
644 | } |
---|
645 | |
---|
646 | |
---|
647 | fifo_out_update(libtrace->fifo, size); |
---|
648 | fifo_ack_update(libtrace->fifo, size); |
---|
649 | } while(1); |
---|
650 | } |
---|
651 | |
---|
652 | static void *erf_get_link(const struct libtrace_packet_t *packet) { |
---|
653 | const void *ethptr = 0; |
---|
654 | dag_record_t *erfptr = 0; |
---|
655 | erfptr = (dag_record_t *)packet->buffer; |
---|
656 | |
---|
657 | if (erfptr->flags.rxerror == 1) { |
---|
658 | return NULL; |
---|
659 | } |
---|
660 | ethptr = ((uint8_t *)packet->buffer + |
---|
661 | dag_record_size + 2); |
---|
662 | return (void *)ethptr; |
---|
663 | } |
---|
664 | |
---|
665 | static libtrace_linktype_t erf_get_link_type(const struct libtrace_packet_t *packet) { |
---|
666 | dag_record_t *erfptr = 0; |
---|
667 | erfptr = (dag_record_t *)packet->buffer; |
---|
668 | printf("%d\n",erfptr->type); |
---|
669 | switch (erfptr->type) { |
---|
670 | case TYPE_ETH: return TRACE_TYPE_ETH; |
---|
671 | case TYPE_ATM: return TRACE_TYPE_ATM; |
---|
672 | default: assert(0); |
---|
673 | } |
---|
674 | return erfptr->type; |
---|
675 | } |
---|
676 | |
---|
677 | static int8_t erf_get_direction(const struct libtrace_packet_t *packet) { |
---|
678 | dag_record_t *erfptr = 0; |
---|
679 | erfptr = (dag_record_t *)packet->buffer; |
---|
680 | return erfptr->flags.iface; |
---|
681 | } |
---|
682 | |
---|
683 | static int8_t erf_set_direction(const struct libtrace_packet_t *packet, int8_t direction) { |
---|
684 | dag_record_t *erfptr = 0; |
---|
685 | erfptr = (dag_record_t *)packet->buffer; |
---|
686 | erfptr->flags.iface = direction; |
---|
687 | return erfptr->flags.iface; |
---|
688 | } |
---|
689 | |
---|
690 | static uint64_t erf_get_erf_timestamp(const struct libtrace_packet_t *packet) { |
---|
691 | dag_record_t *erfptr = 0; |
---|
692 | erfptr = (dag_record_t *)packet->buffer; |
---|
693 | return erfptr->ts; |
---|
694 | } |
---|
695 | |
---|
696 | static int erf_get_capture_length(const struct libtrace_packet_t *packet) { |
---|
697 | dag_record_t *erfptr = 0; |
---|
698 | erfptr = (dag_record_t *)packet->buffer; |
---|
699 | return ntohs(erfptr->rlen); |
---|
700 | } |
---|
701 | |
---|
702 | static int erf_get_wire_length(const struct libtrace_packet_t *packet) { |
---|
703 | dag_record_t *erfptr = 0; |
---|
704 | erfptr = (dag_record_t *)packet->buffer; |
---|
705 | return ntohs(erfptr->wlen); |
---|
706 | } |
---|
707 | |
---|
708 | static size_t erf_set_capture_length(struct libtrace_packet_t *packet, const size_t size) { |
---|
709 | dag_record_t *erfptr = 0; |
---|
710 | assert(packet); |
---|
711 | if(size > packet->size) { |
---|
712 | // can't make a packet larger |
---|
713 | return packet->size; |
---|
714 | } |
---|
715 | erfptr = (dag_record_t *)packet->buffer; |
---|
716 | erfptr->rlen = ntohs(size + sizeof(dag_record_t)); |
---|
717 | packet->size = size + sizeof(dag_record_t); |
---|
718 | return packet->size; |
---|
719 | } |
---|
720 | |
---|
721 | static void dag_help() { |
---|
722 | printf("dag format module: $Revision$\n"); |
---|
723 | printf("Supported input URIs:\n"); |
---|
724 | printf("\tdag:/dev/dagn\n"); |
---|
725 | printf("\n"); |
---|
726 | printf("\te.g.: dag:/dev/dag0\n"); |
---|
727 | printf("\n"); |
---|
728 | printf("Supported output URIs:\n"); |
---|
729 | printf("\tnone\n"); |
---|
730 | printf("\n"); |
---|
731 | |
---|
732 | } |
---|
733 | |
---|
734 | static void erf_help() { |
---|
735 | printf("erf format module: $Revision$\n"); |
---|
736 | printf("Supported input URIs:\n"); |
---|
737 | printf("\terf:/path/to/file\t(uncompressed)\n"); |
---|
738 | printf("\terf:/path/to/file.gz\t(gzip-compressed)\n"); |
---|
739 | printf("\terf:-\t(stdin, either compressed or not)\n")/ |
---|
740 | printf("\terf:/path/to/socket\n"); |
---|
741 | printf("\n"); |
---|
742 | printf("\te.g.: erf:/tmp/trace\n"); |
---|
743 | printf("\n"); |
---|
744 | printf("Supported output URIs:\n"); |
---|
745 | printf("\tnone\n"); |
---|
746 | printf("\n"); |
---|
747 | |
---|
748 | } |
---|
749 | |
---|
750 | static void rtclient_help() { |
---|
751 | printf("rtclient format module\n"); |
---|
752 | printf("Supported input URIs:\n"); |
---|
753 | printf("\trtclient:hostname:port\n"); |
---|
754 | printf("\trtclient:port\n"); |
---|
755 | printf("\n"); |
---|
756 | printf("\te.g.: rtclient:localhost\n"); |
---|
757 | printf("\te.g.: rtclient:localhost:32500\n"); |
---|
758 | printf("\n"); |
---|
759 | printf("Supported output URIs:\n"); |
---|
760 | printf("\trtclient:port\n"); |
---|
761 | printf("\n"); |
---|
762 | printf("\te.g.: rtclient:32500\n"); |
---|
763 | printf("\n"); |
---|
764 | |
---|
765 | } |
---|
766 | |
---|
767 | |
---|
768 | static struct libtrace_format_t erf = { |
---|
769 | "erf", |
---|
770 | "$Id$", |
---|
771 | erf_init_input, /* init_input */ |
---|
772 | erf_init_output, /* init_output */ |
---|
773 | erf_config_output, /* config_output */ |
---|
774 | erf_fin_input, /* fin_input */ |
---|
775 | erf_fin_output, /* fin_output */ |
---|
776 | NULL, /* read */ |
---|
777 | erf_read_packet, /* read_packet */ |
---|
778 | erf_write_packet, /* write_packet */ |
---|
779 | erf_get_link, /* get_link */ |
---|
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 | erf_get_capture_length, /* get_capture_length */ |
---|
787 | erf_get_wire_length, /* get_wire_length */ |
---|
788 | erf_set_capture_length, /* set_capture_length */ |
---|
789 | erf_help /* help */ |
---|
790 | }; |
---|
791 | |
---|
792 | #ifdef HAVE_DAG |
---|
793 | static struct libtrace_format_t dag = { |
---|
794 | "dag", |
---|
795 | "$Id$", |
---|
796 | dag_init_input, /* init_input */ |
---|
797 | NULL, /* init_output */ |
---|
798 | NULL, /* config_output */ |
---|
799 | dag_fin_input, /* fin_input */ |
---|
800 | NULL, /* fin_output */ |
---|
801 | dag_read, /* read */ |
---|
802 | dag_read_packet, /* read_packet */ |
---|
803 | NULL, /* write_packet */ |
---|
804 | erf_get_link, /* get_link */ |
---|
805 | erf_get_link_type, /* get_link_type */ |
---|
806 | erf_get_direction, /* get_direction */ |
---|
807 | erf_set_direction, /* set_direction */ |
---|
808 | erf_get_erf_timestamp, /* get_erf_timestamp */ |
---|
809 | NULL, /* get_timeval */ |
---|
810 | NULL, /* get_seconds */ |
---|
811 | erf_get_capture_length, /* get_capture_length */ |
---|
812 | erf_get_wire_length, /* get_wire_length */ |
---|
813 | erf_set_capture_length, /* set_capture_length */ |
---|
814 | dag_help /* help */ |
---|
815 | }; |
---|
816 | #endif |
---|
817 | |
---|
818 | static struct libtrace_format_t rtclient = { |
---|
819 | "rtclient", |
---|
820 | "$Id$", |
---|
821 | rtclient_init_input, /* init_input */ |
---|
822 | rtclient_init_output, /* init_output */ |
---|
823 | rtclient_config_output, /* config_output */ |
---|
824 | rtclient_fin_input, /* fin_input */ |
---|
825 | rtclient_fin_output, /* fin_output */ |
---|
826 | rtclient_read, /* read */ |
---|
827 | rtclient_read_packet, /* read_packet */ |
---|
828 | rtclient_write_packet, /* write_packet */ |
---|
829 | erf_get_link, /* get_link */ |
---|
830 | erf_get_link_type, /* get_link_type */ |
---|
831 | erf_get_direction, /* get_direction */ |
---|
832 | erf_set_direction, /* set_direction */ |
---|
833 | erf_get_erf_timestamp, /* get_erf_timestamp */ |
---|
834 | NULL, /* get_timeval */ |
---|
835 | NULL, /* get_seconds */ |
---|
836 | erf_get_capture_length, /* get_capture_length */ |
---|
837 | erf_get_wire_length, /* get_wire_length */ |
---|
838 | erf_set_capture_length, /* set_capture_length */ |
---|
839 | rtclient_help /* help */ |
---|
840 | }; |
---|
841 | |
---|
842 | void __attribute__((constructor)) erf_constructor() { |
---|
843 | register_format(&erf); |
---|
844 | #ifdef HAVE_DAG |
---|
845 | register_format(&dag); |
---|
846 | #endif |
---|
847 | register_format(&rtclient); |
---|
848 | } |
---|