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 | |
---|
32 | /** @file |
---|
33 | * |
---|
34 | * @brief Trace file processing library |
---|
35 | * |
---|
36 | * @author Daniel Lawson |
---|
37 | * @author Perry Lorier |
---|
38 | * |
---|
39 | */ |
---|
40 | #define _GNU_SOURCE |
---|
41 | #include "common.h" |
---|
42 | #include "config.h" |
---|
43 | #include <assert.h> |
---|
44 | #include <errno.h> |
---|
45 | #include <fcntl.h> |
---|
46 | #include <netdb.h> |
---|
47 | #include <stdio.h> |
---|
48 | #include <stdlib.h> |
---|
49 | #include <string.h> |
---|
50 | #include <sys/stat.h> |
---|
51 | #include <sys/types.h> |
---|
52 | |
---|
53 | #ifdef HAVE_LIMITS_H |
---|
54 | # include <limits.h> |
---|
55 | #endif |
---|
56 | |
---|
57 | #ifdef HAVE_SYS_LIMITS_H |
---|
58 | # include <sys/limits.h> |
---|
59 | #endif |
---|
60 | |
---|
61 | #include <sys/socket.h> |
---|
62 | #include <sys/un.h> |
---|
63 | #include <sys/mman.h> |
---|
64 | #include <unistd.h> |
---|
65 | #include <net/ethernet.h> |
---|
66 | #include <time.h> |
---|
67 | #include <sys/ioctl.h> |
---|
68 | |
---|
69 | #ifdef HAVE_STDINT_H |
---|
70 | # include <stdint.h> |
---|
71 | #else |
---|
72 | # error "Can't find stdint.h - you need to replace this" |
---|
73 | #endif |
---|
74 | |
---|
75 | #ifdef HAVE_STDDEF_H |
---|
76 | # include <stddef.h> |
---|
77 | #else |
---|
78 | # error "Can't find stddef.h - do you define ptrdiff_t elsewhere?" |
---|
79 | #endif |
---|
80 | |
---|
81 | #include "libtrace.h" |
---|
82 | #include "fifo.h" |
---|
83 | |
---|
84 | #if HAVE_PCAP_BPF_H |
---|
85 | # include <pcap-bpf.h> |
---|
86 | # ifndef HAVE_BPF |
---|
87 | # define HAVE_BPF 1 |
---|
88 | # endif |
---|
89 | #else |
---|
90 | # ifdef HAVE_NET_BPF_H |
---|
91 | # include <net/bpf.h> |
---|
92 | # ifndef HAVE_BPF |
---|
93 | # define HAVE_BPF 1 |
---|
94 | # endif |
---|
95 | # endif |
---|
96 | #endif |
---|
97 | |
---|
98 | #if HAVE_PCAP_H |
---|
99 | # include <pcap.h> |
---|
100 | # ifndef HAVE_PCAP |
---|
101 | # define HAVE_PCAP 1 |
---|
102 | # endif |
---|
103 | #endif |
---|
104 | |
---|
105 | #ifdef HAVE_ZLIB_H |
---|
106 | # include <zlib.h> |
---|
107 | # ifndef HAVE_ZLIB |
---|
108 | # define HAVE_ZLIB 1 |
---|
109 | # endif |
---|
110 | #endif |
---|
111 | |
---|
112 | |
---|
113 | #include "wag.h" |
---|
114 | |
---|
115 | #ifdef HAVE_DAG_API |
---|
116 | # include "dagnew.h" |
---|
117 | # include "dagapi.h" |
---|
118 | # ifndef DAGDEVICE |
---|
119 | # define DAGDEVICE 1 |
---|
120 | # endif |
---|
121 | #else |
---|
122 | # include "dagformat.h" |
---|
123 | #endif |
---|
124 | |
---|
125 | |
---|
126 | typedef enum {SOCKET, TRACE, STDIN, DEVICE, INTERFACE, RT } source_t; |
---|
127 | |
---|
128 | typedef enum {ERF, PCAP, PCAPINT, DAG, RTCLIENT, WAG, WAGINT } format_t; |
---|
129 | |
---|
130 | #if HAVE_BPF |
---|
131 | struct libtrace_filter_t { |
---|
132 | struct bpf_insn *filter; |
---|
133 | char * filterstring; |
---|
134 | }; |
---|
135 | #endif |
---|
136 | |
---|
137 | struct libtrace_t { |
---|
138 | format_t format; |
---|
139 | source_t sourcetype; |
---|
140 | union { |
---|
141 | struct { |
---|
142 | char *hostname; |
---|
143 | short port; |
---|
144 | } rt; |
---|
145 | char *path; |
---|
146 | char *interface; |
---|
147 | } conn_info; |
---|
148 | union { |
---|
149 | int fd; |
---|
150 | #if HAVE_ZLIB |
---|
151 | gzFile *file; |
---|
152 | #else |
---|
153 | FILE *file; |
---|
154 | #endif |
---|
155 | #if HAVE_PCAP |
---|
156 | pcap_t *pcap; |
---|
157 | #endif |
---|
158 | } input; |
---|
159 | struct fifo_t *fifo; |
---|
160 | struct { |
---|
161 | void *buf; |
---|
162 | unsigned bottom; |
---|
163 | unsigned top; |
---|
164 | unsigned diff; |
---|
165 | unsigned curr; |
---|
166 | unsigned offset; |
---|
167 | } dag; |
---|
168 | struct { |
---|
169 | void *buffer; |
---|
170 | int size; |
---|
171 | } packet; |
---|
172 | double last_ts; |
---|
173 | double start_ts; |
---|
174 | }; |
---|
175 | |
---|
176 | #define RP_BUFSIZE 65536 |
---|
177 | |
---|
178 | #define URI_PROTO_LINE 16 |
---|
179 | static int init_trace(struct libtrace_t **libtrace, char *uri) { |
---|
180 | char *scan = calloc(sizeof(char),URI_PROTO_LINE); |
---|
181 | char *uridata = 0; |
---|
182 | struct stat buf; |
---|
183 | |
---|
184 | // parse the URI to determine what sort of event we are dealing with |
---|
185 | |
---|
186 | // want snippet before the : to get the uri base type. |
---|
187 | |
---|
188 | if((uridata = strchr(uri,':')) == NULL) { |
---|
189 | // badly formed URI - needs a : |
---|
190 | return 0; |
---|
191 | } |
---|
192 | |
---|
193 | if ((*uridata - *uri) > URI_PROTO_LINE) { |
---|
194 | // badly formed URI - uri type is too long |
---|
195 | return 0; |
---|
196 | } |
---|
197 | strncpy(scan,uri, (uridata - uri)); |
---|
198 | |
---|
199 | if (!strncasecmp(scan,"erf",3)) { |
---|
200 | (*libtrace)->format=ERF; |
---|
201 | #if HAVE_PCAP |
---|
202 | } else if (!strncasecmp(scan,"pcapint",7)) { |
---|
203 | (*libtrace)->format=PCAPINT; |
---|
204 | } else if (!strncasecmp(scan,"pcap",4)) { |
---|
205 | (*libtrace)->format=PCAP; |
---|
206 | #else |
---|
207 | } else if (!strncasecmp(scan,"pcap",4)) { // also catches pcapint |
---|
208 | fprintf(stderr,"This version of libtrace has been compiled without PCAP support\n"); |
---|
209 | return 0; |
---|
210 | #endif |
---|
211 | |
---|
212 | #ifdef DAGDEVICE |
---|
213 | } else if (!strncasecmp(scan,"dag",3)) { |
---|
214 | (*libtrace)->format=DAG; |
---|
215 | #else |
---|
216 | } else if (!strncasecmp(scan,"dag",3)) { |
---|
217 | fprintf(stderr,"This version of libtrace has been compiled without DAG support\n"); |
---|
218 | return 0; |
---|
219 | #endif |
---|
220 | } else if (!strncasecmp(scan,"rtclient",7)) { |
---|
221 | (*libtrace)->format=RTCLIENT; |
---|
222 | } else if (!strncasecmp(scan,"wagint",6)) { |
---|
223 | (*libtrace)->format=WAGINT; |
---|
224 | } else if (!strncasecmp(scan,"wag",3)) { |
---|
225 | (*libtrace)->format=WAG; |
---|
226 | } else { |
---|
227 | //badly formed URI |
---|
228 | return 0; |
---|
229 | } |
---|
230 | |
---|
231 | // push uridata past the delimiter |
---|
232 | uridata++; |
---|
233 | |
---|
234 | // libtrace->format now contains the type of uri |
---|
235 | // libtrace->uridata contains the appropriate data for this |
---|
236 | |
---|
237 | switch((*libtrace)->format) { |
---|
238 | #if HAVE_PCAP |
---|
239 | case PCAPINT: |
---|
240 | #endif |
---|
241 | case WAGINT: |
---|
242 | /* Can have uridata of the following format |
---|
243 | * eth0 |
---|
244 | * etc |
---|
245 | */ |
---|
246 | // We basically assume this is correct. |
---|
247 | (*libtrace)->sourcetype = INTERFACE; |
---|
248 | (*libtrace)->conn_info.path = strdup(uridata); |
---|
249 | break; |
---|
250 | #if HAVE_PCAP |
---|
251 | case PCAP: |
---|
252 | #endif |
---|
253 | case ERF: |
---|
254 | case WAG: |
---|
255 | /* |
---|
256 | * Can have uridata of the following format |
---|
257 | * /path/to/socket (probably not PCAP) |
---|
258 | * /path/to/file |
---|
259 | * /path/to/file.gz (not PCAP) |
---|
260 | * /dev/device (use PCAPINT) |
---|
261 | * - |
---|
262 | */ |
---|
263 | if (!strncmp(uridata,"-",1)) { |
---|
264 | (*libtrace)->sourcetype = STDIN; |
---|
265 | } else { |
---|
266 | if (stat(uridata,&buf) == -1) { |
---|
267 | perror("stat"); |
---|
268 | return 0; |
---|
269 | } |
---|
270 | if (S_ISSOCK(buf.st_mode)) { |
---|
271 | (*libtrace)->sourcetype = SOCKET; |
---|
272 | } else if (S_ISCHR(buf.st_mode)) { |
---|
273 | (*libtrace)->sourcetype = DEVICE; |
---|
274 | } else { |
---|
275 | (*libtrace)->sourcetype = TRACE; |
---|
276 | } |
---|
277 | (*libtrace)->conn_info.path = strdup(uridata); |
---|
278 | } |
---|
279 | break; |
---|
280 | case DAG: |
---|
281 | #ifdef DAGDEVICE |
---|
282 | /* |
---|
283 | * Can have uridata of the following format: |
---|
284 | * /dev/device |
---|
285 | */ |
---|
286 | if (stat(uridata,&buf) == -1) { |
---|
287 | perror("stat"); |
---|
288 | return 0; |
---|
289 | } |
---|
290 | if (S_ISCHR(buf.st_mode)) { |
---|
291 | (*libtrace)->sourcetype = DEVICE; |
---|
292 | } else { |
---|
293 | fprintf(stderr,"%s isn't a valid char device, exiting\n",uridata); |
---|
294 | exit(1); |
---|
295 | } |
---|
296 | (*libtrace)->conn_info.path = strdup(uridata); |
---|
297 | #endif |
---|
298 | break; |
---|
299 | |
---|
300 | case RTCLIENT: |
---|
301 | /* |
---|
302 | * Can have the uridata in the format |
---|
303 | * hostname |
---|
304 | * hostname:port |
---|
305 | */ |
---|
306 | (*libtrace)->sourcetype = RT; |
---|
307 | if (strlen(uridata) == 0) { |
---|
308 | (*libtrace)->conn_info.rt.hostname = |
---|
309 | strdup("localhost"); |
---|
310 | (*libtrace)->conn_info.rt.port = |
---|
311 | COLLECTOR_PORT; |
---|
312 | break; |
---|
313 | } |
---|
314 | if ((scan = strchr(uridata,':')) == NULL) { |
---|
315 | (*libtrace)->conn_info.rt.hostname = |
---|
316 | strdup(uridata); |
---|
317 | (*libtrace)->conn_info.rt.port = |
---|
318 | COLLECTOR_PORT; |
---|
319 | } else { |
---|
320 | (*libtrace)->conn_info.rt.hostname = |
---|
321 | (char *)strndup(uridata,(scan - uridata)); |
---|
322 | |
---|
323 | (*libtrace)->conn_info.rt.port = |
---|
324 | atoi(++scan); |
---|
325 | } |
---|
326 | break; |
---|
327 | } |
---|
328 | |
---|
329 | |
---|
330 | (*libtrace)->fifo = create_fifo(1048576); |
---|
331 | assert( (*libtrace)->fifo); |
---|
332 | //(*libtrace)->packet.buffer = 0; |
---|
333 | //(*libtrace)->packet.size = 0; |
---|
334 | |
---|
335 | return 1; |
---|
336 | } |
---|
337 | |
---|
338 | /** Create a trace file from a URI |
---|
339 | * |
---|
340 | * @returns opaque pointer to a libtrace_t |
---|
341 | * |
---|
342 | * Valid URI's are: |
---|
343 | * erf:/path/to/erf/file |
---|
344 | * erf:/path/to/erf/file.gz |
---|
345 | * erf:/path/to/rtclient/socket |
---|
346 | * erf:- (stdin) |
---|
347 | * pcapint:pcapinterface (eg: pcapint:eth0) |
---|
348 | * pcap:/path/to/pcap/file |
---|
349 | * pcap:- |
---|
350 | * rtclient:hostname |
---|
351 | * rtclient:hostname:port |
---|
352 | * wag:- |
---|
353 | * wag:/path/to/wag/file |
---|
354 | * wag:/path/to/wag/file.gz |
---|
355 | * wag:/path/to/wag/socket |
---|
356 | * wagint:/dev/device |
---|
357 | * |
---|
358 | * URIs which have yet to be implemented are: |
---|
359 | * dag:/dev/dagcard |
---|
360 | * pcap:/path/to/pcap/socket |
---|
361 | * |
---|
362 | * If an error occured when attempting to open a trace, NULL is returned |
---|
363 | * and an error is output to stdout. |
---|
364 | */ |
---|
365 | struct libtrace_t *trace_create(char *uri) { |
---|
366 | struct libtrace_t *libtrace = malloc(sizeof(struct libtrace_t)); |
---|
367 | struct hostent *he; |
---|
368 | struct sockaddr_in remote; |
---|
369 | struct sockaddr_un unix_sock; |
---|
370 | #if HAVE_PCAP |
---|
371 | char errbuf[PCAP_ERRBUF_SIZE]; |
---|
372 | #endif |
---|
373 | |
---|
374 | if(init_trace(&libtrace,uri) == 0) { |
---|
375 | return 0; |
---|
376 | } |
---|
377 | |
---|
378 | switch(libtrace->sourcetype) { |
---|
379 | case RT: |
---|
380 | if ((he=gethostbyname(libtrace->conn_info.rt.hostname)) == NULL) { |
---|
381 | perror("gethostbyname"); |
---|
382 | return 0; |
---|
383 | } |
---|
384 | if ((libtrace->input.fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { |
---|
385 | perror("socket"); |
---|
386 | return 0; |
---|
387 | } |
---|
388 | |
---|
389 | remote.sin_family = AF_INET; |
---|
390 | remote.sin_port = htons(libtrace->conn_info.rt.port); |
---|
391 | remote.sin_addr = *((struct in_addr *)he->h_addr); |
---|
392 | bzero(&(remote.sin_zero), 8); |
---|
393 | |
---|
394 | if (connect(libtrace->input.fd, (struct sockaddr *)&remote, |
---|
395 | sizeof(struct sockaddr)) == -1) { |
---|
396 | perror("connect (inet)"); |
---|
397 | return 0; |
---|
398 | } |
---|
399 | break; |
---|
400 | case TRACE: |
---|
401 | #if HAVE_PCAP |
---|
402 | if (libtrace->format == PCAP) { |
---|
403 | if ((libtrace->input.pcap = pcap_open_offline(libtrace->conn_info.path, errbuf)) == NULL) { |
---|
404 | fprintf(stderr,"%s\n",errbuf); |
---|
405 | return 0; |
---|
406 | } |
---|
407 | } else { |
---|
408 | #else |
---|
409 | { |
---|
410 | #endif |
---|
411 | #if HAVE_ZLIB |
---|
412 | libtrace->input.file = gzopen(libtrace->conn_info.path, "r"); |
---|
413 | #else |
---|
414 | libtrace->input.file = fopen(libtrace->conn_info.path, "r"); |
---|
415 | #endif |
---|
416 | } |
---|
417 | break; |
---|
418 | case STDIN: |
---|
419 | #if HAVE_PCAP |
---|
420 | if (libtrace->format == PCAP) { |
---|
421 | libtrace->input.pcap = pcap_open_offline("-",errbuf); |
---|
422 | } else { |
---|
423 | #else |
---|
424 | { |
---|
425 | #endif |
---|
426 | #if HAVE_ZLIB |
---|
427 | libtrace->input.file = gzdopen(STDIN, "r"); |
---|
428 | #else |
---|
429 | libtrace->input.file = stdin; |
---|
430 | #endif |
---|
431 | } |
---|
432 | break; |
---|
433 | case SOCKET: |
---|
434 | /* Pcap doesn't work */ |
---|
435 | if (libtrace->format != PCAP) { |
---|
436 | if ((libtrace->input.fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { |
---|
437 | perror("socket"); |
---|
438 | return 0; |
---|
439 | } |
---|
440 | unix_sock.sun_family = AF_UNIX; |
---|
441 | bzero(unix_sock.sun_path,108); |
---|
442 | snprintf(unix_sock.sun_path,108,"%s",libtrace->conn_info.path); |
---|
443 | |
---|
444 | if (connect(libtrace->input.fd, (struct sockaddr *)&unix_sock, |
---|
445 | sizeof(struct sockaddr)) == -1) { |
---|
446 | perror("connect (unix)"); |
---|
447 | return 0; |
---|
448 | } |
---|
449 | } |
---|
450 | break; |
---|
451 | case DEVICE: |
---|
452 | case INTERFACE: |
---|
453 | switch (libtrace->format) { |
---|
454 | #if HAVE_PCAP |
---|
455 | case PCAPINT: |
---|
456 | case PCAP: |
---|
457 | libtrace->input.pcap = pcap_open_live( |
---|
458 | libtrace->conn_info.path, |
---|
459 | 4096, |
---|
460 | 1, |
---|
461 | 0, |
---|
462 | errbuf); |
---|
463 | break; |
---|
464 | #endif |
---|
465 | case WAGINT: |
---|
466 | case WAG: |
---|
467 | libtrace->input.fd = open( |
---|
468 | libtrace->conn_info.path, |
---|
469 | O_RDONLY); |
---|
470 | break; |
---|
471 | #ifdef DAGDEVICE |
---|
472 | case DAG: |
---|
473 | if((libtrace->input.fd = dag_open(libtrace->conn_info.path)) < 0) { |
---|
474 | fprintf(stderr,"Cannot open DAG %s: %m\n", libtrace->conn_info.path,errno); |
---|
475 | exit(0); |
---|
476 | } |
---|
477 | if((libtrace->dag.buf = dag_mmap(libtrace->input.fd)) == MAP_FAILED) { |
---|
478 | fprintf(stderr,"Cannot mmap DAG %s: %m\n", libtrace->conn_info.path,errno); |
---|
479 | exit(0); |
---|
480 | } |
---|
481 | if(dag_start(libtrace->input.fd) < 0) { |
---|
482 | fprintf(stderr,"Cannot start DAG %s: %m\n", libtrace->conn_info.path,errno); |
---|
483 | exit(0); |
---|
484 | } |
---|
485 | break; |
---|
486 | #endif |
---|
487 | default: |
---|
488 | fprintf(stderr,"Unknown format trace, hoping I can just read\n"); |
---|
489 | break; |
---|
490 | |
---|
491 | } |
---|
492 | break; |
---|
493 | default: |
---|
494 | fprintf(stderr,"Unsupported source type for libtrace, terminating (%i)\n",libtrace->sourcetype); |
---|
495 | exit(0); |
---|
496 | |
---|
497 | } |
---|
498 | return libtrace; |
---|
499 | } |
---|
500 | |
---|
501 | /** Close a trace file, freeing up any resources it may have been using |
---|
502 | * |
---|
503 | */ |
---|
504 | void trace_destroy(struct libtrace_t *libtrace) { |
---|
505 | assert(libtrace); |
---|
506 | #if HAVE_PCAP |
---|
507 | if (libtrace->format == PCAP || libtrace->format == PCAPINT) { |
---|
508 | pcap_close(libtrace->input.pcap); |
---|
509 | #else |
---|
510 | if (0) { |
---|
511 | #endif |
---|
512 | } else if (libtrace->sourcetype == SOCKET || libtrace->sourcetype == RT) { |
---|
513 | close(libtrace->input.fd); |
---|
514 | #ifdef DAGDEVICE |
---|
515 | } else if (libtrace->format == DAG) { |
---|
516 | dag_stop(libtrace->input.fd); |
---|
517 | #endif |
---|
518 | } else { |
---|
519 | #if HAVE_ZLIB |
---|
520 | gzclose(libtrace->input.file); |
---|
521 | #else |
---|
522 | fclose(libtrace->input.file); |
---|
523 | #endif |
---|
524 | } |
---|
525 | // need to free things! |
---|
526 | destroy_fifo(libtrace->fifo); |
---|
527 | free(libtrace); |
---|
528 | } |
---|
529 | |
---|
530 | static int trace_read(struct libtrace_t *libtrace, void *buffer, size_t len) { |
---|
531 | int numbytes; |
---|
532 | static short lctr = 0; |
---|
533 | struct dag_record_t *recptr = 0; |
---|
534 | int rlen; |
---|
535 | assert(libtrace); |
---|
536 | assert(len >= 0); |
---|
537 | |
---|
538 | if (buffer == 0) |
---|
539 | buffer = malloc(len); |
---|
540 | |
---|
541 | while(1) { |
---|
542 | switch(libtrace->sourcetype) { |
---|
543 | case SOCKET: |
---|
544 | case RT: |
---|
545 | |
---|
546 | #ifndef MSG_NOSIGNAL |
---|
547 | #define MSG_NOSIGNAL 0 |
---|
548 | #endif |
---|
549 | // read from the network |
---|
550 | if ((numbytes=recv(libtrace->input.fd, |
---|
551 | buffer, |
---|
552 | len, |
---|
553 | MSG_NOSIGNAL)) == -1) { |
---|
554 | if (errno == EINTR) { |
---|
555 | // ignore EINTR in case |
---|
556 | // a caller is using signals |
---|
557 | continue; |
---|
558 | } |
---|
559 | perror("recv"); |
---|
560 | return -1; |
---|
561 | } |
---|
562 | break; |
---|
563 | case DEVICE: |
---|
564 | switch(libtrace->format) { |
---|
565 | #ifdef DAGDEVICE |
---|
566 | case DAG: |
---|
567 | |
---|
568 | libtrace->dag.bottom = libtrace->dag.top; |
---|
569 | libtrace->dag.top = dag_offset( |
---|
570 | libtrace->input.fd, |
---|
571 | &(libtrace->dag.bottom), |
---|
572 | 0); |
---|
573 | libtrace->dag.diff = libtrace->dag.top - |
---|
574 | libtrace->dag.bottom; |
---|
575 | |
---|
576 | numbytes=libtrace->dag.diff; |
---|
577 | libtrace->dag.offset = 0; |
---|
578 | |
---|
579 | break; |
---|
580 | #endif |
---|
581 | default: |
---|
582 | if ((numbytes=read(libtrace->input.fd, |
---|
583 | buffer, |
---|
584 | len)) == -1) { |
---|
585 | perror("read"); |
---|
586 | return -1; |
---|
587 | } |
---|
588 | } |
---|
589 | break; |
---|
590 | default: |
---|
591 | #if HAVE_ZLIB |
---|
592 | if ((numbytes=gzread(libtrace->input.file, |
---|
593 | buffer, |
---|
594 | len)) == -1) { |
---|
595 | perror("gzread"); |
---|
596 | return -1; |
---|
597 | } |
---|
598 | #else |
---|
599 | if ((numbytes=fread(buffer,len,1,libtrace->input.file)) == 0 ) { |
---|
600 | if(feof(libtrace->input.file)) { |
---|
601 | return 0; |
---|
602 | } |
---|
603 | if(ferror(libtrace->input.file)) { |
---|
604 | perror("fread"); |
---|
605 | return -1; |
---|
606 | } |
---|
607 | return 0; |
---|
608 | } |
---|
609 | #endif |
---|
610 | } |
---|
611 | break; |
---|
612 | } |
---|
613 | return numbytes; |
---|
614 | |
---|
615 | } |
---|
616 | |
---|
617 | /** Read one packet from the trace into buffer |
---|
618 | * |
---|
619 | * @param libtrace the libtrace opaque pointer |
---|
620 | * @param packet the packet opaque pointer |
---|
621 | * @returns false if it failed to read a packet |
---|
622 | * |
---|
623 | */ |
---|
624 | int trace_read_packet(struct libtrace_t *libtrace, struct libtrace_packet_t *packet) { |
---|
625 | int numbytes; |
---|
626 | int size; |
---|
627 | char buf[RP_BUFSIZE]; |
---|
628 | #if HAVE_PCAP |
---|
629 | struct pcap_pkthdr pcaphdr; |
---|
630 | const u_char *pcappkt; |
---|
631 | #endif |
---|
632 | dag_record_t *erfptr; |
---|
633 | int read_required = 0; |
---|
634 | |
---|
635 | void *buffer = 0; |
---|
636 | if (!libtrace) { |
---|
637 | fprintf(stderr,"Oi! You called trace_read_packet() with a NULL libtrace parameter!\n"); |
---|
638 | } |
---|
639 | assert(libtrace); |
---|
640 | assert(packet); |
---|
641 | |
---|
642 | /* Store the trace we are reading from into the packet opaque |
---|
643 | * structure */ |
---|
644 | packet->trace = libtrace; |
---|
645 | |
---|
646 | buffer = packet->buffer; |
---|
647 | #if HAVE_PCAP |
---|
648 | /* PCAP gives us it's own per-packet interface. Let's use it */ |
---|
649 | if (libtrace->format == PCAP || libtrace->format == PCAPINT) { |
---|
650 | if ((pcappkt = pcap_next(libtrace->input.pcap, &pcaphdr)) == NULL) { |
---|
651 | return -1; |
---|
652 | } |
---|
653 | memcpy(buffer,&pcaphdr,sizeof(struct pcap_pkthdr)); |
---|
654 | memcpy(buffer + sizeof(struct pcap_pkthdr),pcappkt,pcaphdr.len); |
---|
655 | numbytes = pcaphdr.len; |
---|
656 | |
---|
657 | packet->size = numbytes + sizeof(struct pcap_pkthdr); |
---|
658 | return numbytes; |
---|
659 | } |
---|
660 | #endif |
---|
661 | |
---|
662 | /* If we're reading from an ERF input, it's an offline trace. We can make some assumptions */ |
---|
663 | if (libtrace->format == ERF) { |
---|
664 | void *buffer2 = buffer; |
---|
665 | int rlen; |
---|
666 | // read in the trace header |
---|
667 | if ((numbytes=gzread(libtrace->input.file, |
---|
668 | buffer, |
---|
669 | dag_record_size)) == -1) { |
---|
670 | perror("gzread"); |
---|
671 | return -1; |
---|
672 | } |
---|
673 | if (numbytes == 0) { |
---|
674 | return 0; |
---|
675 | } |
---|
676 | rlen = ntohs(((dag_record_t *)buffer)->rlen); |
---|
677 | size = rlen - dag_record_size; |
---|
678 | assert(size < LIBTRACE_PACKET_BUFSIZE); |
---|
679 | buffer2 = buffer + dag_record_size; |
---|
680 | |
---|
681 | // read in the rest of the packet |
---|
682 | if ((numbytes=gzread(libtrace->input.file, |
---|
683 | buffer2, |
---|
684 | size)) == -1) { |
---|
685 | perror("gzread"); |
---|
686 | return -1; |
---|
687 | } |
---|
688 | //if ((numbytes + dag_record_size) != rlen) { |
---|
689 | // printf("read %d wanted %d\n",numbytes +dag_record_size, rlen); |
---|
690 | //} |
---|
691 | packet->size = rlen; |
---|
692 | |
---|
693 | return rlen; |
---|
694 | } |
---|
695 | |
---|
696 | #ifdef DAGDEVICE |
---|
697 | if (libtrace->format == DAG) { |
---|
698 | if (libtrace->dag.diff == 0) { |
---|
699 | if ((numbytes = trace_read(libtrace,buf,RP_BUFSIZE)) <= 0) |
---|
700 | return numbytes; |
---|
701 | } |
---|
702 | // DAG always gives us whole packets. |
---|
703 | |
---|
704 | erfptr = (dag_record_t *) ((void *)libtrace->dag.buf + (libtrace->dag.bottom + libtrace->dag.offset)); |
---|
705 | size = ntohs(erfptr->rlen); |
---|
706 | |
---|
707 | if ( size > LIBTRACE_PACKET_BUFSIZE) { |
---|
708 | printf("%d\n",size); |
---|
709 | assert( size < LIBTRACE_PACKET_BUFSIZE); |
---|
710 | } |
---|
711 | |
---|
712 | // have to copy it out of the memory hole at this stage: |
---|
713 | memcpy(packet->buffer, erfptr, size); |
---|
714 | |
---|
715 | packet->size = size; |
---|
716 | libtrace->dag.offset += size; |
---|
717 | libtrace->dag.diff -= size; |
---|
718 | |
---|
719 | assert(libtrace->dag.diff >= 0); |
---|
720 | //assert(libtrace->dag.offset <= libtrace->dag.top); |
---|
721 | return (size); |
---|
722 | |
---|
723 | } |
---|
724 | #endif |
---|
725 | do { |
---|
726 | if (fifo_out_available(libtrace->fifo) == 0 || read_required) { |
---|
727 | if ((numbytes = trace_read(libtrace,buf,RP_BUFSIZE))<=0){ |
---|
728 | return numbytes; |
---|
729 | } |
---|
730 | assert(libtrace->fifo); |
---|
731 | fifo_write(libtrace->fifo,buf,numbytes); |
---|
732 | |
---|
733 | read_required = 0; |
---|
734 | } |
---|
735 | |
---|
736 | switch (libtrace->format) { |
---|
737 | case RTCLIENT: |
---|
738 | // only do this if we're reading from the RT interface |
---|
739 | if (fifo_out_read(libtrace->fifo, &packet->status, sizeof(int)) == 0) { |
---|
740 | read_required = 1; |
---|
741 | continue; |
---|
742 | } |
---|
743 | |
---|
744 | fifo_out_update(libtrace->fifo,sizeof(int)); |
---|
745 | |
---|
746 | /* FALL THRU */ |
---|
747 | case ERF: |
---|
748 | //case DAG: |
---|
749 | // read in the erf header |
---|
750 | if ((numbytes = fifo_out_read(libtrace->fifo, buffer, sizeof(dag_record_t))) == 0) { |
---|
751 | fifo_out_reset(libtrace->fifo); |
---|
752 | read_required = 1; |
---|
753 | continue; |
---|
754 | } |
---|
755 | |
---|
756 | size = ntohs(((dag_record_t *)buffer)->rlen); |
---|
757 | break; |
---|
758 | case WAG: |
---|
759 | if ((numbytes = fifo_out_read(libtrace->fifo, |
---|
760 | &size, |
---|
761 | sizeof(size))) |
---|
762 | == 0) { |
---|
763 | fifo_out_reset(libtrace->fifo); |
---|
764 | read_required = 1; |
---|
765 | continue; |
---|
766 | } |
---|
767 | size*=4; |
---|
768 | break; |
---|
769 | default: |
---|
770 | fprintf(stderr,"Unknown type in _read()\n"); |
---|
771 | assert(0); |
---|
772 | } |
---|
773 | |
---|
774 | assert(size < LIBTRACE_PACKET_BUFSIZE); |
---|
775 | |
---|
776 | // read in the full packet |
---|
777 | if ((numbytes = fifo_out_read(libtrace->fifo, buffer, size)) == 0) { |
---|
778 | fifo_out_reset(libtrace->fifo); |
---|
779 | read_required = 1; |
---|
780 | continue; |
---|
781 | } |
---|
782 | |
---|
783 | // got in our whole packet, so... |
---|
784 | fifo_out_update(libtrace->fifo,size); |
---|
785 | |
---|
786 | if (libtrace->sourcetype == SOCKET || libtrace->sourcetype == RT) { |
---|
787 | fifo_ack_update(libtrace->fifo,size + sizeof(int)); |
---|
788 | } else { |
---|
789 | fifo_ack_update(libtrace->fifo,size); |
---|
790 | } |
---|
791 | |
---|
792 | packet->size = numbytes; |
---|
793 | return numbytes; |
---|
794 | |
---|
795 | } while (1); |
---|
796 | } |
---|
797 | |
---|
798 | |
---|
799 | /** get a pointer to the link layer |
---|
800 | * @param libtrace a pointer to the trace object returned from gettrace |
---|
801 | * @param buffer a pointer to a filled in buffer |
---|
802 | * @param buflen a pointer to the size of the buffer |
---|
803 | * |
---|
804 | * @returns a pointer to the link layer, or NULL if there is no link layer |
---|
805 | * you should call trace_get_link_type() to find out what type of link layer this is |
---|
806 | */ |
---|
807 | void *trace_get_link(struct libtrace_packet_t *packet) { |
---|
808 | void *ethptr = 0; |
---|
809 | |
---|
810 | struct wag_event_t *event = (struct wag_event_t *)packet->buffer; |
---|
811 | struct wag_data_event_t *data_event; |
---|
812 | |
---|
813 | |
---|
814 | switch(packet->trace->format) { |
---|
815 | case ERF: |
---|
816 | case DAG: |
---|
817 | case RTCLIENT: |
---|
818 | if (trace_get_link_type(packet)==TRACE_TYPE_ETH) |
---|
819 | ethptr = ((uint8_t *)packet->buffer + |
---|
820 | dag_record_size + 2); |
---|
821 | else |
---|
822 | ethptr = ((uint8_t *)packet->buffer + |
---|
823 | dag_record_size + 2); |
---|
824 | break; |
---|
825 | #if HAVE_PCAP |
---|
826 | case PCAPINT: |
---|
827 | case PCAP: |
---|
828 | ethptr = (struct ether_header *)(packet->buffer + sizeof(struct pcap_pkthdr)); |
---|
829 | break; |
---|
830 | #endif |
---|
831 | case WAGINT: |
---|
832 | case WAG: |
---|
833 | switch (event->type) { |
---|
834 | case 0x0: |
---|
835 | data_event = (void*)&(event->payload); |
---|
836 | return data_event->data; |
---|
837 | default: |
---|
838 | fprintf(stderr,"Unknown WAG Event (0x%08x)\n",event->type); |
---|
839 | return NULL; |
---|
840 | } |
---|
841 | |
---|
842 | default: |
---|
843 | fprintf(stderr,"Dunno this trace format\n"); |
---|
844 | assert(0); |
---|
845 | } |
---|
846 | return ethptr; |
---|
847 | } |
---|
848 | |
---|
849 | /** get a pointer to the IP header (if any) |
---|
850 | * @param libtrace a pointer to the trace object returned from gettrace |
---|
851 | * @param buffer a pointer to a filled in buffer |
---|
852 | * @param buflen a pointer to the size of the buffer |
---|
853 | * |
---|
854 | * @returns a pointer to the IP header, or NULL if there is not an IP packet |
---|
855 | */ |
---|
856 | struct libtrace_ip *trace_get_ip(struct libtrace_packet_t *packet) { |
---|
857 | struct libtrace_ip *ipptr = 0; |
---|
858 | |
---|
859 | switch(trace_get_link_type(packet)) { |
---|
860 | case TRACE_TYPE_80211: |
---|
861 | { |
---|
862 | |
---|
863 | struct ieee_802_11_header *wifi = trace_get_link(packet); |
---|
864 | |
---|
865 | // Data packet? |
---|
866 | if (wifi->type != 2) { |
---|
867 | ipptr = NULL; |
---|
868 | } |
---|
869 | else { |
---|
870 | struct ieee_802_11_payload *eth = (void*)wifi->data; |
---|
871 | if (eth->type != 0x0008) { |
---|
872 | ipptr=NULL; |
---|
873 | } else { |
---|
874 | ipptr=(void*)eth->data; |
---|
875 | } |
---|
876 | } |
---|
877 | } |
---|
878 | break; |
---|
879 | case TRACE_TYPE_ETH: |
---|
880 | { |
---|
881 | struct ether_header *eth = |
---|
882 | trace_get_link(packet); |
---|
883 | if (ntohs(eth->ether_type)!=0x0800) { |
---|
884 | ipptr = NULL; |
---|
885 | } |
---|
886 | else { |
---|
887 | ipptr = ((void *)eth) + 14; |
---|
888 | } |
---|
889 | break; |
---|
890 | } |
---|
891 | case TRACE_TYPE_ATM: |
---|
892 | { |
---|
893 | struct atm_rec *atm = |
---|
894 | trace_get_link(packet); |
---|
895 | // TODO: Find out what ATM does, and return |
---|
896 | // NULL for non IP data |
---|
897 | // Presumably it uses the normal stuff |
---|
898 | ipptr = (void*)&atm->pload; |
---|
899 | break; |
---|
900 | } |
---|
901 | default: |
---|
902 | fprintf(stderr,"Don't understand link layer type %i in trace_get_ip()\n", |
---|
903 | trace_get_link_type(packet)); |
---|
904 | ipptr=NULL; |
---|
905 | break; |
---|
906 | } |
---|
907 | |
---|
908 | return ipptr; |
---|
909 | } |
---|
910 | |
---|
911 | #define SW_IP_OFFMASK 0xff1f |
---|
912 | |
---|
913 | /** get a pointer to the TCP header (if any) |
---|
914 | * @param libtrace a pointer to the trace object returned from gettrace |
---|
915 | * @param buffer a pointer to a filled in buffer |
---|
916 | * @param buflen a pointer to the size of the buffer |
---|
917 | * |
---|
918 | * @returns a pointer to the TCP header, or NULL if there is not a TCP packet |
---|
919 | */ |
---|
920 | struct libtrace_tcp *trace_get_tcp(struct libtrace_packet_t *packet) { |
---|
921 | struct libtrace_tcp *tcpptr = 0; |
---|
922 | struct libtrace_ip *ipptr = 0; |
---|
923 | |
---|
924 | if(!(ipptr = trace_get_ip(packet))) { |
---|
925 | return 0; |
---|
926 | } |
---|
927 | if ((ipptr->ip_p == 6) && ((ipptr->ip_off & SW_IP_OFFMASK) == 0)) { |
---|
928 | tcpptr = (struct libtrace_tcp *)((ptrdiff_t)ipptr + (ipptr->ip_hl * 4)); |
---|
929 | } |
---|
930 | return tcpptr; |
---|
931 | } |
---|
932 | |
---|
933 | /** get a pointer to the UDP header (if any) |
---|
934 | * @param libtrace a pointer to the trace object returned from gettrace |
---|
935 | * @param buffer a pointer to a filled in buffer |
---|
936 | * @param buflen a pointer to the size of the buffer |
---|
937 | * |
---|
938 | * @returns a pointer to the UDP header, or NULL if this is not a UDP packet |
---|
939 | */ |
---|
940 | struct libtrace_udp *trace_get_udp(struct libtrace_packet_t *packet) { |
---|
941 | struct libtrace_udp *udpptr = 0; |
---|
942 | struct libtrace_ip *ipptr = 0; |
---|
943 | |
---|
944 | if(!(ipptr = trace_get_ip(packet))) { |
---|
945 | return 0; |
---|
946 | } |
---|
947 | if ((ipptr->ip_p == 17) && ((ipptr->ip_off & SW_IP_OFFMASK) == 0)) { |
---|
948 | udpptr = (struct libtrace_udp *)((ptrdiff_t)ipptr + (ipptr->ip_hl * 4)); |
---|
949 | } |
---|
950 | return udpptr; |
---|
951 | } |
---|
952 | |
---|
953 | /** get a pointer to the ICMP header (if any) |
---|
954 | * @param libtrace a pointer to the trace object returned from gettrace |
---|
955 | * @param buffer a pointer to a filled in buffer |
---|
956 | * @param buflen a pointer to the size of the buffer |
---|
957 | * |
---|
958 | * @returns a pointer to the ICMP header, or NULL if this is not a ICMP packet |
---|
959 | */ |
---|
960 | struct libtrace_icmp *trace_get_icmp(struct libtrace_packet_t *packet) { |
---|
961 | struct libtrace_icmp *icmpptr = 0; |
---|
962 | struct libtrace_ip *ipptr = 0; |
---|
963 | |
---|
964 | if(!(ipptr = trace_get_ip(packet))) { |
---|
965 | return 0; |
---|
966 | } |
---|
967 | if ((ipptr->ip_p == 1)&& ((ipptr->ip_off & SW_IP_OFFMASK) == 0 )){ |
---|
968 | icmpptr = (struct libtrace_icmp *)((ptrdiff_t)ipptr + (ipptr->ip_hl * 4)); |
---|
969 | } |
---|
970 | return icmpptr; |
---|
971 | } |
---|
972 | |
---|
973 | /** Get the current time in DAG time format |
---|
974 | * @param libtrace the libtrace opaque pointer |
---|
975 | * @param buffer a pointer to a filled in buffer |
---|
976 | * @param buflen the length of the buffer |
---|
977 | * @returns a 64 bit timestamp in DAG ERF format (upper 32 bits are the seconds |
---|
978 | * past 1970-01-01, the lower 32bits are partial seconds) |
---|
979 | * @author Daniel Lawson |
---|
980 | */ |
---|
981 | uint64_t trace_get_erf_timestamp(struct libtrace_packet_t *packet) { |
---|
982 | uint64_t timestamp = 0; |
---|
983 | dag_record_t *erfptr = 0; |
---|
984 | struct pcap_pkthdr *pcapptr = 0; |
---|
985 | struct wag_event_t *wagptr = 0; |
---|
986 | switch (packet->trace->format) { |
---|
987 | case DAG: |
---|
988 | case ERF: |
---|
989 | case RTCLIENT: |
---|
990 | erfptr = (dag_record_t *)packet->buffer; |
---|
991 | timestamp = erfptr->ts; |
---|
992 | break; |
---|
993 | #if HAVE_PCAP |
---|
994 | case PCAPINT: |
---|
995 | case PCAP: |
---|
996 | pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
997 | timestamp = ((((uint64_t)pcapptr->ts.tv_sec) << 32) + \ |
---|
998 | (pcapptr->ts.tv_usec*UINT_MAX/1000000)); |
---|
999 | break; |
---|
1000 | #endif |
---|
1001 | case WAGINT: |
---|
1002 | case WAG: |
---|
1003 | wagptr = (struct wag_event_t *)packet->buffer; |
---|
1004 | timestamp = wagptr->timestamp_lo; |
---|
1005 | timestamp |= (uint64_t)wagptr->timestamp_hi<<32; |
---|
1006 | timestamp = ((timestamp%44000000)*(UINT_MAX/44000000)) |
---|
1007 | | ((timestamp/44000000)<<32); |
---|
1008 | break; |
---|
1009 | default: |
---|
1010 | fprintf(stderr,"Unknown format in trace_get_erf_timestamp\n"); |
---|
1011 | timestamp = 0; |
---|
1012 | } |
---|
1013 | return timestamp; |
---|
1014 | } |
---|
1015 | |
---|
1016 | /** Get the current time in struct timeval |
---|
1017 | * @param libtrace the libtrace opaque pointer |
---|
1018 | * @param buffer a pointer to a filled in buffer |
---|
1019 | * @param buflen the length of the buffer |
---|
1020 | * @returns time that this packet was seen in a struct timeval |
---|
1021 | * @author Daniel Lawson |
---|
1022 | * @author Perry Lorier |
---|
1023 | */ |
---|
1024 | struct timeval trace_get_timeval(struct libtrace_packet_t *packet) { |
---|
1025 | struct timeval tv; |
---|
1026 | #if HAVE_PCAP |
---|
1027 | struct pcap_pkthdr *pcapptr = 0; |
---|
1028 | #endif |
---|
1029 | uint64_t ts; |
---|
1030 | //uint32_t seconds; |
---|
1031 | switch (packet->trace->format) { |
---|
1032 | #if HAVE_PCAP |
---|
1033 | case PCAPINT: |
---|
1034 | case PCAP: |
---|
1035 | pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
1036 | tv = pcapptr->ts; |
---|
1037 | break; |
---|
1038 | #endif |
---|
1039 | case WAGINT: |
---|
1040 | case WAG: |
---|
1041 | case DAG: |
---|
1042 | case ERF: |
---|
1043 | case RTCLIENT: |
---|
1044 | default: |
---|
1045 | // FIXME: This isn't portable to big-endian machines |
---|
1046 | ts = trace_get_erf_timestamp(packet); |
---|
1047 | tv.tv_sec = ts >> 32; |
---|
1048 | ts = (1000000 * (ts & 0xffffffffULL)); |
---|
1049 | ts += (ts & 0x80000000ULL) << 1; |
---|
1050 | tv.tv_usec = ts >> 32; |
---|
1051 | if (tv.tv_usec >= 1000000) { |
---|
1052 | tv.tv_usec -= 1000000; |
---|
1053 | tv.tv_sec += 1; |
---|
1054 | } |
---|
1055 | break; |
---|
1056 | } |
---|
1057 | return tv; |
---|
1058 | } |
---|
1059 | |
---|
1060 | /** Get the current time in floating point seconds |
---|
1061 | * @param libtrace the libtrace opaque pointer |
---|
1062 | * @param buffer a pointer to a filled in buffer |
---|
1063 | * @param buflen the length of the buffer |
---|
1064 | * @returns time that this packet was seen in 64bit floating point seconds |
---|
1065 | * @author Perry Lorier |
---|
1066 | */ |
---|
1067 | double trace_get_seconds(struct libtrace_packet_t *packet) { |
---|
1068 | uint64_t ts; |
---|
1069 | ts = trace_get_erf_timestamp(packet); |
---|
1070 | return (ts>>32) + ((ts & UINT_MAX)*1.0 / UINT_MAX); |
---|
1071 | } |
---|
1072 | |
---|
1073 | /** Get the size of the packet in the trace |
---|
1074 | * @param packet the packet opaque pointer |
---|
1075 | * @returns the size of the packet in the trace |
---|
1076 | * @author Perry Lorier |
---|
1077 | * @note Due to this being a header capture, or anonymisation, this may not |
---|
1078 | * be the same size as the original packet. See trace_get_wire_length() for the |
---|
1079 | * original size of the packet. |
---|
1080 | * @note This can (and often is) different for different packets in a trace! |
---|
1081 | * @par |
---|
1082 | * This is sometimes called the "snaplen". |
---|
1083 | */ |
---|
1084 | int trace_get_capture_length(struct libtrace_packet_t *packet) { |
---|
1085 | dag_record_t *erfptr = 0; |
---|
1086 | #if HAVE_PCAP |
---|
1087 | struct pcap_pkthdr *pcapptr = 0; |
---|
1088 | #endif |
---|
1089 | struct wag_event_t *wag_event; |
---|
1090 | switch (packet->trace->format) { |
---|
1091 | case DAG: |
---|
1092 | case ERF: |
---|
1093 | case RTCLIENT: |
---|
1094 | erfptr = (dag_record_t *)packet->buffer; |
---|
1095 | return ntohs(erfptr->rlen); |
---|
1096 | #if HAVE_PCAP |
---|
1097 | case PCAPINT: |
---|
1098 | case PCAP: |
---|
1099 | pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
1100 | //return ntohs(pcapptr->caplen); |
---|
1101 | return pcapptr->caplen; |
---|
1102 | #endif |
---|
1103 | case WAGINT: |
---|
1104 | case WAG: |
---|
1105 | wag_event = (struct wag_event_t *)packet->buffer; |
---|
1106 | switch(wag_event->type) { |
---|
1107 | case 0: |
---|
1108 | return wag_event->length*4-( |
---|
1109 | sizeof(struct wag_event_t)+ |
---|
1110 | sizeof(struct wag_data_event_t) |
---|
1111 | ); |
---|
1112 | default: |
---|
1113 | assert(0); |
---|
1114 | } |
---|
1115 | default: |
---|
1116 | assert(0); |
---|
1117 | } |
---|
1118 | return -1; |
---|
1119 | } |
---|
1120 | |
---|
1121 | /** Get the size of the packet as it was seen on the wire. |
---|
1122 | * @param libtrace the libtrace opaque pointer |
---|
1123 | * @param buffer a pointer to a filled in buffer |
---|
1124 | * @param buflen the length of the buffer |
---|
1125 | * @returns the size of the packet as it was on the wire. |
---|
1126 | * @author Perry Lorier |
---|
1127 | * @author Daniel Lawson |
---|
1128 | * @note Due to the trace being a header capture, or anonymisation this may |
---|
1129 | * not be the same as the Capture Len. |
---|
1130 | */ |
---|
1131 | int trace_get_wire_length(struct libtrace_packet_t *packet){ |
---|
1132 | dag_record_t *erfptr = 0; |
---|
1133 | #if HAVE_PCAP |
---|
1134 | struct pcap_pkthdr *pcapptr = 0; |
---|
1135 | #endif |
---|
1136 | struct wag_event_t *wag_event = 0; |
---|
1137 | switch (packet->trace->format) { |
---|
1138 | case DAG: |
---|
1139 | case ERF: |
---|
1140 | case RTCLIENT: |
---|
1141 | erfptr = (dag_record_t *)packet->buffer; |
---|
1142 | return ntohs(erfptr->wlen); |
---|
1143 | break; |
---|
1144 | #if HAVE_PCAP |
---|
1145 | case PCAPINT: |
---|
1146 | case PCAP: |
---|
1147 | pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
1148 | return ntohs(pcapptr->len); |
---|
1149 | break; |
---|
1150 | #endif |
---|
1151 | case WAGINT: |
---|
1152 | case WAG: |
---|
1153 | wag_event = (struct wag_event_t *)packet->buffer; |
---|
1154 | switch(wag_event->type) { |
---|
1155 | case 0: |
---|
1156 | return ((struct wag_data_event_t *)(&wag_event->payload))->frame_length; |
---|
1157 | default: |
---|
1158 | assert(0); |
---|
1159 | } |
---|
1160 | } |
---|
1161 | return -1; |
---|
1162 | |
---|
1163 | } |
---|
1164 | |
---|
1165 | /** Get the type of the link layer |
---|
1166 | * @param libtrace the libtrace opaque pointer |
---|
1167 | * @param buffer a pointer to a filled in buffer |
---|
1168 | * @param buflen the length of the buffer |
---|
1169 | * @returns libtrace_linktype_t |
---|
1170 | * @author Perry Lorier |
---|
1171 | * @author Daniel Lawson |
---|
1172 | */ |
---|
1173 | libtrace_linktype_t trace_get_link_type(struct libtrace_packet_t *packet ) { |
---|
1174 | dag_record_t *erfptr = 0; |
---|
1175 | #if HAVE_PCAP |
---|
1176 | struct pcap_pkthdr *pcapptr = 0; |
---|
1177 | #endif |
---|
1178 | int linktype = 0; |
---|
1179 | switch (packet->trace->format) { |
---|
1180 | case DAG: |
---|
1181 | case ERF: |
---|
1182 | case RTCLIENT: |
---|
1183 | erfptr = (dag_record_t *)packet->buffer; |
---|
1184 | switch (erfptr->type) { |
---|
1185 | case TYPE_ETH: return TRACE_TYPE_ETH; |
---|
1186 | case TYPE_ATM: return TRACE_TYPE_ATM; |
---|
1187 | default: assert(0); |
---|
1188 | } |
---|
1189 | return erfptr->type; |
---|
1190 | |
---|
1191 | break; |
---|
1192 | #if HAVE_PCAP |
---|
1193 | case PCAPINT: |
---|
1194 | case PCAP: |
---|
1195 | pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
1196 | linktype = pcap_datalink(packet->trace->input.pcap); |
---|
1197 | switch (linktype) { |
---|
1198 | case 1: |
---|
1199 | return TRACE_TYPE_ETH; |
---|
1200 | case 11: |
---|
1201 | return TRACE_TYPE_ATM; |
---|
1202 | case DLT_IEEE802_11: |
---|
1203 | return TRACE_TYPE_80211; |
---|
1204 | } |
---|
1205 | break; |
---|
1206 | #endif |
---|
1207 | case WAGINT: |
---|
1208 | case WAG: |
---|
1209 | return TRACE_TYPE_80211; |
---|
1210 | } |
---|
1211 | return -1; |
---|
1212 | } |
---|
1213 | |
---|
1214 | /** Get the source MAC addres |
---|
1215 | * @param libtrace the libtrace opaque pointer |
---|
1216 | * @param buffer a pointer to a filled in buffer |
---|
1217 | * @param buflen the length of the buffer |
---|
1218 | * @returns a pointer to the source mac, (or NULL if there is no source MAC) |
---|
1219 | * @author Perry Lorier |
---|
1220 | */ |
---|
1221 | uint8_t *trace_get_source_mac(struct libtrace_packet_t *packet) { |
---|
1222 | void *link = trace_get_link(packet); |
---|
1223 | struct ieee_802_11_header *wifi = link; |
---|
1224 | struct ether_header *ethptr = link; |
---|
1225 | if (!link) |
---|
1226 | return NULL; |
---|
1227 | switch (trace_get_link_type(packet)) { |
---|
1228 | case TRACE_TYPE_80211: |
---|
1229 | return (uint8_t*)&wifi->mac2; |
---|
1230 | case TRACE_TYPE_ETH: |
---|
1231 | return (uint8_t*)ðptr->ether_shost; |
---|
1232 | default: |
---|
1233 | fprintf(stderr,"Not implemented\n"); |
---|
1234 | assert(0); |
---|
1235 | } |
---|
1236 | } |
---|
1237 | |
---|
1238 | /** Get the destination MAC addres |
---|
1239 | * @param libtrace the libtrace opaque pointer |
---|
1240 | * @param buffer a pointer to a filled in buffer |
---|
1241 | * @param buflen the length of the buffer |
---|
1242 | * @returns a pointer to the destination mac, (or NULL if there is no |
---|
1243 | * destination MAC) |
---|
1244 | * @author Perry Lorier |
---|
1245 | */ |
---|
1246 | uint8_t *trace_get_destination_mac(struct libtrace_packet_t *packet) { |
---|
1247 | void *link = trace_get_link(packet); |
---|
1248 | struct ieee_802_11_header *wifi = link; |
---|
1249 | struct ether_header *ethptr = link; |
---|
1250 | if (!link) |
---|
1251 | return NULL; |
---|
1252 | switch (trace_get_link_type(packet)) { |
---|
1253 | case TRACE_TYPE_80211: |
---|
1254 | return (uint8_t*)&wifi->mac1; |
---|
1255 | case TRACE_TYPE_ETH: |
---|
1256 | return (uint8_t*)ðptr->ether_dhost; |
---|
1257 | default: |
---|
1258 | fprintf(stderr,"Not implemented\n"); |
---|
1259 | assert(0); |
---|
1260 | } |
---|
1261 | } |
---|
1262 | |
---|
1263 | |
---|
1264 | /** process a libtrace event |
---|
1265 | * @param trace the libtrace opaque pointer |
---|
1266 | * @param packet the libtrace_packet opaque pointer |
---|
1267 | * @param fd a pointer to a file descriptor to listen on |
---|
1268 | * @param seconds a pointer the time in seconds since to the next event |
---|
1269 | * @returns |
---|
1270 | * TRACE_EVENT_IOWAIT Waiting on I/O on <fd> |
---|
1271 | * TRACE_EVENT_SLEEP Next event in <seconds> |
---|
1272 | * TRACE_EVENT_PACKET Packet arrived in <buffer> with size <size> |
---|
1273 | * FIXME currently keeps a copy of the packet inside the trace pointer, |
---|
1274 | * which in turn is stored inside the new packet object... |
---|
1275 | * @author Perry Lorier |
---|
1276 | */ |
---|
1277 | struct libtrace_eventobj_t trace_event(struct libtrace_t *trace, |
---|
1278 | struct libtrace_packet_t *packet) { |
---|
1279 | struct libtrace_eventobj_t event; |
---|
1280 | |
---|
1281 | if (!trace) { |
---|
1282 | fprintf(stderr,"You called trace_event() with a NULL trace object!\n"); |
---|
1283 | } |
---|
1284 | assert(trace); |
---|
1285 | assert(packet); |
---|
1286 | |
---|
1287 | /* Store the trace we are reading from into the packet opaque |
---|
1288 | * structure */ |
---|
1289 | packet->trace = trace; |
---|
1290 | |
---|
1291 | /* Is there a packet ready? */ |
---|
1292 | switch (trace->sourcetype) { |
---|
1293 | #if HAVE_PCAP |
---|
1294 | case INTERFACE: |
---|
1295 | { |
---|
1296 | int data; |
---|
1297 | event.fd = pcap_fileno(trace->input.pcap); |
---|
1298 | if(ioctl(event.fd,FIONREAD,&data)==-1){ |
---|
1299 | perror("ioctl(FIONREAD)"); |
---|
1300 | } |
---|
1301 | if (data>0) { |
---|
1302 | trace_read_packet(trace,packet); |
---|
1303 | event.type = TRACE_EVENT_PACKET; |
---|
1304 | return event; |
---|
1305 | } |
---|
1306 | event.type = TRACE_EVENT_IOWAIT; |
---|
1307 | return event; |
---|
1308 | } |
---|
1309 | #endif |
---|
1310 | case SOCKET: |
---|
1311 | case DEVICE: |
---|
1312 | case RT: |
---|
1313 | { |
---|
1314 | int data; |
---|
1315 | event.fd = trace->input.fd; |
---|
1316 | if(ioctl(event.fd,FIONREAD,&data)==-1){ |
---|
1317 | perror("ioctl(FIONREAD)"); |
---|
1318 | } |
---|
1319 | if (data>0) { |
---|
1320 | trace_read_packet(trace,packet); |
---|
1321 | event.type = TRACE_EVENT_PACKET; |
---|
1322 | return event; |
---|
1323 | } |
---|
1324 | event.type = TRACE_EVENT_IOWAIT; |
---|
1325 | return event; |
---|
1326 | } |
---|
1327 | case STDIN: |
---|
1328 | case TRACE: |
---|
1329 | { |
---|
1330 | double ts; |
---|
1331 | /* "Prime" the pump */ |
---|
1332 | if (!trace->packet.buffer) { |
---|
1333 | trace->packet.buffer = malloc(4096); |
---|
1334 | trace->packet.size= |
---|
1335 | trace_read_packet(trace,packet); |
---|
1336 | } |
---|
1337 | ts=trace_get_seconds(packet); |
---|
1338 | if (trace->last_ts!=0) { |
---|
1339 | event.seconds = ts - trace->last_ts; |
---|
1340 | if (event.seconds>time(NULL)-trace->start_ts) { |
---|
1341 | event.type = TRACE_EVENT_SLEEP; |
---|
1342 | return event; |
---|
1343 | } |
---|
1344 | |
---|
1345 | } |
---|
1346 | else { |
---|
1347 | trace->start_ts = time(NULL); |
---|
1348 | trace->last_ts = ts; |
---|
1349 | } |
---|
1350 | |
---|
1351 | packet->size = trace->packet.size; |
---|
1352 | memcpy(packet->buffer,trace->packet.buffer,trace->packet.size); |
---|
1353 | |
---|
1354 | free(trace->packet.buffer); |
---|
1355 | trace->packet.buffer = 0; |
---|
1356 | event.type = TRACE_EVENT_PACKET; |
---|
1357 | return event; |
---|
1358 | } |
---|
1359 | default: |
---|
1360 | assert(0); |
---|
1361 | } |
---|
1362 | assert(0); |
---|
1363 | } |
---|
1364 | |
---|
1365 | /** setup a BPF filter |
---|
1366 | * @param filterstring a char * containing the bpf filter string |
---|
1367 | * @returns opaque pointer pointer to a libtrace_filter_t object |
---|
1368 | * @author Daniel Lawson |
---|
1369 | */ |
---|
1370 | struct libtrace_filter_t *trace_bpf_setfilter(const char *filterstring) { |
---|
1371 | #if HAVE_BPF |
---|
1372 | struct libtrace_filter_t *filter = malloc(sizeof(struct libtrace_filter_t)); |
---|
1373 | filter->filterstring = strdup(filterstring); |
---|
1374 | filter->filter = 0; |
---|
1375 | return filter; |
---|
1376 | #else |
---|
1377 | fprintf(stderr,"This version of libtrace does not have bpf filter support\n"); |
---|
1378 | return 0; |
---|
1379 | #endif |
---|
1380 | } |
---|
1381 | |
---|
1382 | /** apply a BPF filter |
---|
1383 | * @param libtrace the libtrace opaque pointer |
---|
1384 | * @param filter the filter opaque pointer |
---|
1385 | * @param buffer a pointer to a filled buffer |
---|
1386 | * @param buflen the length of the buffer |
---|
1387 | * @returns 0 if the filter fails, 1 if it succeeds |
---|
1388 | * @author Daniel Lawson |
---|
1389 | */ |
---|
1390 | int trace_bpf_filter(struct libtrace_filter_t *filter, |
---|
1391 | struct libtrace_packet_t *packet) { |
---|
1392 | #if HAVE_BPF |
---|
1393 | void *linkptr = 0; |
---|
1394 | int clen = 0; |
---|
1395 | assert(filter); |
---|
1396 | assert(packet); |
---|
1397 | linkptr = trace_get_link(packet); |
---|
1398 | assert(linkptr); |
---|
1399 | clen = trace_get_capture_length(packet); |
---|
1400 | |
---|
1401 | |
---|
1402 | if (filter->filterstring && ! filter->filter) { |
---|
1403 | pcap_t *pcap; |
---|
1404 | struct bpf_program bpfprog; |
---|
1405 | |
---|
1406 | switch (trace_get_link_type(packet)) { |
---|
1407 | case TRACE_TYPE_ETH: |
---|
1408 | pcap = pcap_open_dead(DLT_EN10MB, 1500); |
---|
1409 | break; |
---|
1410 | default: |
---|
1411 | printf("only works for ETH at the moment\n"); |
---|
1412 | assert(0); |
---|
1413 | } |
---|
1414 | |
---|
1415 | // build filter |
---|
1416 | if (pcap_compile( pcap, &bpfprog, filter->filterstring, 1, 0)) { |
---|
1417 | printf("bpf compilation error: %s\n", |
---|
1418 | pcap_geterr(pcap)); |
---|
1419 | assert(0); |
---|
1420 | } |
---|
1421 | pcap_close(pcap); |
---|
1422 | filter->filter = bpfprog.bf_insns; |
---|
1423 | } |
---|
1424 | |
---|
1425 | assert(filter->filter); |
---|
1426 | return bpf_filter(filter->filter, linkptr, clen, clen); |
---|
1427 | #else |
---|
1428 | fprintf(stderr,"This version of libtrace does not have bpf filter support\n"); |
---|
1429 | return 0; |
---|
1430 | #endif |
---|
1431 | } |
---|
1432 | |
---|
1433 | /** Set the direction flag, if it has one |
---|
1434 | * @param packet the packet opaque pointer |
---|
1435 | * @param direction the new direction (0,1,2,3) |
---|
1436 | * @returns a signed value containing the direction flag, or -1 if this is not supported |
---|
1437 | * @author Daniel Lawson |
---|
1438 | */ |
---|
1439 | int8_t trace_set_direction(struct libtrace_packet_t *packet, int8_t direction) { |
---|
1440 | |
---|
1441 | dag_record_t *erfptr = 0; |
---|
1442 | assert(packet); |
---|
1443 | |
---|
1444 | switch(packet->trace->format) { |
---|
1445 | case DAG: |
---|
1446 | case ERF: |
---|
1447 | case RTCLIENT: |
---|
1448 | erfptr = (dag_record_t *)packet->buffer; |
---|
1449 | erfptr->flags.iface = direction; |
---|
1450 | break; |
---|
1451 | default: |
---|
1452 | direction = -1; |
---|
1453 | } |
---|
1454 | |
---|
1455 | return direction; |
---|
1456 | |
---|
1457 | |
---|
1458 | } |
---|
1459 | |
---|
1460 | /** Get the direction flag, if it has one |
---|
1461 | * @param libtrace the libtrace opaque pointer |
---|
1462 | * @param buffer a point to a fille in buffer |
---|
1463 | * @param buflen the length of the buffer |
---|
1464 | * @returns a signed value containing the direction flag, or -1 if this is not supported |
---|
1465 | * @author Daniel Lawson |
---|
1466 | */ |
---|
1467 | int8_t trace_get_direction(struct libtrace_packet_t *packet) { |
---|
1468 | |
---|
1469 | int8_t direction; |
---|
1470 | dag_record_t *erfptr = 0; |
---|
1471 | assert(packet); |
---|
1472 | |
---|
1473 | switch(packet->trace->format) { |
---|
1474 | case DAG: |
---|
1475 | case ERF: |
---|
1476 | case RTCLIENT: |
---|
1477 | erfptr = (dag_record_t *)packet->buffer; |
---|
1478 | direction = erfptr->flags.iface; |
---|
1479 | break; |
---|
1480 | default: |
---|
1481 | direction = -1; |
---|
1482 | } |
---|
1483 | |
---|
1484 | return direction; |
---|
1485 | |
---|
1486 | |
---|
1487 | } |
---|
1488 | |
---|
1489 | #define ROOT_SERVER(x) ((x) < 512) |
---|
1490 | #define ROOT_CLIENT(x) ((512 <= (x)) && ((x) < 1024)) |
---|
1491 | #define NONROOT_SERVER(x) ((x) >= 5000) |
---|
1492 | #define NONROOT_CLIENT(x) ((1024 <= (x)) && ((x) < 5000)) |
---|
1493 | #define DYNAMIC(x) ((49152 < (x)) && ((x) < 65535)) |
---|
1494 | #define SERVER(x) ROOT_SERVER(x) || NONROOT_SERVER(x) |
---|
1495 | #define CLIENT(x) ROOT_CLIENT(x) || NONROOT_CLIENT(x) |
---|
1496 | |
---|
1497 | |
---|
1498 | /** Attempt to deduce the 'server' port |
---|
1499 | * @param protocol the IP protocol (eg, 6 or 17 for TCP or UDP) |
---|
1500 | * @param source the TCP or UDP source port |
---|
1501 | * @param dest the TCP or UDP destination port |
---|
1502 | * @returns a hint as to which port is the server port |
---|
1503 | * @author Daniel Lawson |
---|
1504 | */ |
---|
1505 | int8_t trace_get_server_port(uint8_t protocol, uint16_t source, uint16_t dest) { |
---|
1506 | /* |
---|
1507 | * * If the ports are equal, return DEST |
---|
1508 | * * Check for well-known ports in the given protocol |
---|
1509 | * * Root server ports: 0 - 511 |
---|
1510 | * * Root client ports: 512 - 1023 |
---|
1511 | * * non-root client ports: 1024 - 4999 |
---|
1512 | * * non-root server ports: 5000+ |
---|
1513 | * * Check for static ranges: 1024 - 49151 |
---|
1514 | * * Check for dynamic ranges: 49152 - 65535 |
---|
1515 | * * flip a coin. |
---|
1516 | */ |
---|
1517 | |
---|
1518 | uint16_t server, client; |
---|
1519 | |
---|
1520 | /* equal */ |
---|
1521 | if (source == client) |
---|
1522 | return USE_DEST; |
---|
1523 | |
---|
1524 | /* root server port, 0 - 511 */ |
---|
1525 | if (ROOT_SERVER(source) && ROOT_SERVER(dest)) { |
---|
1526 | if (source < dest) |
---|
1527 | return USE_SOURCE; |
---|
1528 | return USE_DEST; |
---|
1529 | } |
---|
1530 | |
---|
1531 | if (ROOT_SERVER(source) && !ROOT_SERVER(dest)) |
---|
1532 | return USE_SOURCE; |
---|
1533 | if (!ROOT_SERVER(source) && ROOT_SERVER(dest)) |
---|
1534 | return USE_DEST; |
---|
1535 | |
---|
1536 | /* non-root server */ |
---|
1537 | if (NONROOT_SERVER(source) && NONROOT_SERVER(dest)) { |
---|
1538 | if (source < dest) |
---|
1539 | return USE_SOURCE; |
---|
1540 | return USE_DEST; |
---|
1541 | } |
---|
1542 | if (NONROOT_SERVER(source) && !NONROOT_SERVER(dest)) |
---|
1543 | return USE_SOURCE; |
---|
1544 | if (!NONROOT_SERVER(source) && NONROOT_SERVER(dest)) |
---|
1545 | return USE_DEST; |
---|
1546 | |
---|
1547 | /* root client */ |
---|
1548 | if (ROOT_CLIENT(source) && ROOT_CLIENT(dest)) { |
---|
1549 | if (source < dest) |
---|
1550 | return USE_SOURCE; |
---|
1551 | return USE_DEST; |
---|
1552 | } |
---|
1553 | if (ROOT_CLIENT(source) && !ROOT_CLIENT(dest)) { |
---|
1554 | /* prefer root-client over nonroot-client */ |
---|
1555 | if (NONROOT_CLIENT(dest)) |
---|
1556 | return USE_SOURCE; |
---|
1557 | return USE_DEST; |
---|
1558 | } |
---|
1559 | if (!ROOT_CLIENT(source) && ROOT_CLIENT(dest)) { |
---|
1560 | /* prefer root-client over nonroot-client */ |
---|
1561 | if (NONROOT_CLIENT(source)) |
---|
1562 | return USE_DEST; |
---|
1563 | return USE_SOURCE; |
---|
1564 | } |
---|
1565 | |
---|
1566 | /* nonroot client */ |
---|
1567 | if (NONROOT_CLIENT(source) && NONROOT_CLIENT(dest)) { |
---|
1568 | if (source < dest) |
---|
1569 | return USE_SOURCE; |
---|
1570 | return USE_DEST; |
---|
1571 | } |
---|
1572 | if (NONROOT_CLIENT(source) && !NONROOT_CLIENT(dest)) |
---|
1573 | return USE_DEST; |
---|
1574 | if (!NONROOT_CLIENT(source) && NONROOT_CLIENT(dest)) |
---|
1575 | return USE_SOURCE; |
---|
1576 | |
---|
1577 | /* dynamic range */ |
---|
1578 | if (DYNAMIC(source) && DYNAMIC(dest)) |
---|
1579 | if (source < dest) |
---|
1580 | return USE_SOURCE; |
---|
1581 | return USE_DEST; |
---|
1582 | if (DYNAMIC(source) && !DYNAMIC(dest)) |
---|
1583 | return USE_DEST; |
---|
1584 | if (!DYNAMIC(source) && DYNAMIC(dest)) |
---|
1585 | return USE_SOURCE; |
---|
1586 | /* |
---|
1587 | if (SERVER(source) && CLIENT(dest)) |
---|
1588 | return USE_SOURCE; |
---|
1589 | |
---|
1590 | if (SERVER(dest) && CLIENT(source)) |
---|
1591 | return USE_DEST; |
---|
1592 | if (ROOT_SERVER(source) && !ROOT_SERVER(dest)) |
---|
1593 | return USE_SOURCE; |
---|
1594 | if (ROOT_SERVER(dest) && !ROOT_SERVER(source)) |
---|
1595 | return USE_DEST; |
---|
1596 | */ |
---|
1597 | // failing that test... |
---|
1598 | if (source < dest) { |
---|
1599 | return USE_SOURCE; |
---|
1600 | } |
---|
1601 | return USE_DEST; |
---|
1602 | |
---|
1603 | } |
---|
1604 | |
---|
1605 | /** Truncate the packet at the suggested length |
---|
1606 | * @param packet the packet opaque pointer |
---|
1607 | * @param len the new length of the packet |
---|
1608 | * @returns the new length of the packet, or the original length of the |
---|
1609 | * packet if unchanged |
---|
1610 | * NOTE: len refers to the network-level payload of the packet, and not |
---|
1611 | * any capture headers included as well. For example, to truncate a packet |
---|
1612 | * after the IP header, set scan to sizeof(ethernet_header) + sizeof(ip_header) |
---|
1613 | * @author Daniel Lawson |
---|
1614 | */ |
---|
1615 | size_t trace_truncate_packet(struct libtrace_packet_t *packet, size_t size) { |
---|
1616 | dag_record_t *erfptr; |
---|
1617 | #if HAVE_PCAP |
---|
1618 | struct pcap_pkthdr *pcaphdr; |
---|
1619 | #endif |
---|
1620 | |
---|
1621 | assert(packet); |
---|
1622 | |
---|
1623 | if (size > packet->size) { |
---|
1624 | // can't make a packet larger |
---|
1625 | return packet->size; |
---|
1626 | } |
---|
1627 | switch (packet->trace->format) { |
---|
1628 | #if HAVE_PCAP |
---|
1629 | case PCAPINT: |
---|
1630 | case PCAP: |
---|
1631 | pcaphdr = (struct pcap_pkthdr *)packet->buffer; |
---|
1632 | pcaphdr->caplen = size + sizeof(struct pcap_pkthdr); |
---|
1633 | packet->size = pcaphdr->caplen; |
---|
1634 | break; |
---|
1635 | #endif |
---|
1636 | case ERF: |
---|
1637 | case DAG: |
---|
1638 | case RTCLIENT: |
---|
1639 | erfptr = (dag_record_t *)packet->buffer; |
---|
1640 | erfptr->rlen = ntohs(size + sizeof(dag_record_t)); |
---|
1641 | packet->size = size + sizeof(dag_record_t); |
---|
1642 | break; |
---|
1643 | case WAGINT: |
---|
1644 | case WAG: |
---|
1645 | // don't know how to do this? |
---|
1646 | break; |
---|
1647 | } |
---|
1648 | return packet->size; |
---|
1649 | } |
---|
1650 | |
---|