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