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