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