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