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