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. */ |
---|
673 | |
---|
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 | /* Instead of pcap_next/pcap_next_ex, we do this ourselves |
---|
681 | * with a trivial callback function. This lets us |
---|
682 | * catch the same errors as pcap_next_ex, but removes |
---|
683 | * the requirement for libpcap >= 0.8.x |
---|
684 | */ |
---|
685 | while ((pcapbytes = pcap_dispatch(libtrace->input.pcap, |
---|
686 | 1, /* number of packets */ |
---|
687 | &trace_pcap_handler, |
---|
688 | (u_char *)packet)) == 0); |
---|
689 | |
---|
690 | if (pcapbytes < 0 ) { |
---|
691 | return -1; |
---|
692 | } |
---|
693 | return (packet->size - sizeof(struct pcap_pkthdr)); |
---|
694 | //memcpy(buffer,&pcaphdr,sizeof(struct pcap_pkthdr)); |
---|
695 | //numbytes = pcaphdr->len; |
---|
696 | //memcpy(buffer + sizeof(struct pcap_pkthdr),pcappkt,numbytes); |
---|
697 | |
---|
698 | //packet->size = numbytes + sizeof(struct pcap_pkthdr); |
---|
699 | //return numbytes; |
---|
700 | } |
---|
701 | #endif |
---|
702 | |
---|
703 | /* If we're reading from an ERF input, it's an offline trace. We can make some assumptions */ |
---|
704 | if (libtrace->format == ERF) { |
---|
705 | void *buffer2 = buffer; |
---|
706 | int rlen; |
---|
707 | // read in the trace header |
---|
708 | if ((numbytes=gzread(libtrace->input.file, |
---|
709 | buffer, |
---|
710 | dag_record_size)) == -1) { |
---|
711 | perror("gzread"); |
---|
712 | return -1; |
---|
713 | } |
---|
714 | if (numbytes == 0) { |
---|
715 | return 0; |
---|
716 | } |
---|
717 | rlen = ntohs(((dag_record_t *)buffer)->rlen); |
---|
718 | size = rlen - dag_record_size; |
---|
719 | assert(size < LIBTRACE_PACKET_BUFSIZE); |
---|
720 | buffer2 = buffer + dag_record_size; |
---|
721 | |
---|
722 | // read in the rest of the packet |
---|
723 | if ((numbytes=gzread(libtrace->input.file, |
---|
724 | buffer2, |
---|
725 | size)) == -1) { |
---|
726 | perror("gzread"); |
---|
727 | return -1; |
---|
728 | } |
---|
729 | //if ((numbytes + dag_record_size) != rlen) { |
---|
730 | // printf("read %d wanted %d\n",numbytes +dag_record_size, rlen); |
---|
731 | //} |
---|
732 | packet->size = rlen; |
---|
733 | |
---|
734 | return rlen; |
---|
735 | } |
---|
736 | |
---|
737 | #if HAVE_DAG |
---|
738 | if (libtrace->format == DAG) { |
---|
739 | if (libtrace->dag.diff == 0) { |
---|
740 | if ((numbytes = trace_read(libtrace,buf,RP_BUFSIZE)) <= 0) |
---|
741 | return numbytes; |
---|
742 | } |
---|
743 | // DAG always gives us whole packets. |
---|
744 | |
---|
745 | erfptr = (dag_record_t *) ((void *)libtrace->dag.buf + (libtrace->dag.bottom + libtrace->dag.offset)); |
---|
746 | size = ntohs(erfptr->rlen); |
---|
747 | |
---|
748 | if ( size > LIBTRACE_PACKET_BUFSIZE) { |
---|
749 | printf("%d\n",size); |
---|
750 | assert( size < LIBTRACE_PACKET_BUFSIZE); |
---|
751 | } |
---|
752 | |
---|
753 | // have to copy it out of the memory hole at this stage: |
---|
754 | memcpy(packet->buffer, erfptr, size); |
---|
755 | |
---|
756 | packet->size = size; |
---|
757 | libtrace->dag.offset += size; |
---|
758 | libtrace->dag.diff -= size; |
---|
759 | |
---|
760 | assert(libtrace->dag.diff >= 0); |
---|
761 | //assert(libtrace->dag.offset <= libtrace->dag.top); |
---|
762 | return (size); |
---|
763 | |
---|
764 | } |
---|
765 | #endif |
---|
766 | do { |
---|
767 | if (fifo_out_available(libtrace->fifo) == 0 || read_required) { |
---|
768 | if ((numbytes = trace_read(libtrace,buf,RP_BUFSIZE))<=0){ |
---|
769 | return numbytes; |
---|
770 | } |
---|
771 | assert(libtrace->fifo); |
---|
772 | fifo_write(libtrace->fifo,buf,numbytes); |
---|
773 | |
---|
774 | read_required = 0; |
---|
775 | } |
---|
776 | |
---|
777 | switch (libtrace->format) { |
---|
778 | case RTCLIENT: |
---|
779 | // only do this if we're reading from the RT interface |
---|
780 | if (fifo_out_read(libtrace->fifo, &packet->status, sizeof(int)) == 0) { |
---|
781 | read_required = 1; |
---|
782 | continue; |
---|
783 | } |
---|
784 | |
---|
785 | fifo_out_update(libtrace->fifo,sizeof(int)); |
---|
786 | |
---|
787 | /* FALL THRU */ |
---|
788 | case ERF: |
---|
789 | //case DAG: |
---|
790 | // read in the erf header |
---|
791 | if ((numbytes = fifo_out_read(libtrace->fifo, buffer, sizeof(dag_record_t))) == 0) { |
---|
792 | fifo_out_reset(libtrace->fifo); |
---|
793 | read_required = 1; |
---|
794 | continue; |
---|
795 | } |
---|
796 | |
---|
797 | size = ntohs(((dag_record_t *)buffer)->rlen); |
---|
798 | break; |
---|
799 | case WAG: |
---|
800 | if ((numbytes = fifo_out_read(libtrace->fifo, |
---|
801 | &size, |
---|
802 | sizeof(size))) |
---|
803 | == 0) { |
---|
804 | fifo_out_reset(libtrace->fifo); |
---|
805 | read_required = 1; |
---|
806 | continue; |
---|
807 | } |
---|
808 | size*=4; |
---|
809 | break; |
---|
810 | default: |
---|
811 | fprintf(stderr,"Unknown type in _read()\n"); |
---|
812 | assert(0); |
---|
813 | } |
---|
814 | |
---|
815 | assert(size < LIBTRACE_PACKET_BUFSIZE); |
---|
816 | |
---|
817 | // read in the full packet |
---|
818 | if ((numbytes = fifo_out_read(libtrace->fifo, buffer, size)) == 0) { |
---|
819 | fifo_out_reset(libtrace->fifo); |
---|
820 | read_required = 1; |
---|
821 | continue; |
---|
822 | } |
---|
823 | |
---|
824 | // got in our whole packet, so... |
---|
825 | fifo_out_update(libtrace->fifo,size); |
---|
826 | |
---|
827 | if (libtrace->sourcetype == SOCKET || libtrace->sourcetype == RT) { |
---|
828 | fifo_ack_update(libtrace->fifo,size + sizeof(int)); |
---|
829 | } else { |
---|
830 | fifo_ack_update(libtrace->fifo,size); |
---|
831 | } |
---|
832 | |
---|
833 | packet->size = numbytes; |
---|
834 | return numbytes; |
---|
835 | |
---|
836 | } while (1); |
---|
837 | } |
---|
838 | |
---|
839 | |
---|
840 | /** get a pointer to the link layer |
---|
841 | * @param packet a pointer to a libtrace_packet structure |
---|
842 | * |
---|
843 | * @returns a pointer to the link layer, or NULL if there is no link layer |
---|
844 | * you should call trace_get_link_type() to find out what type of link layer this is |
---|
845 | */ |
---|
846 | void *trace_get_link(const struct libtrace_packet_t *packet) { |
---|
847 | const void *ethptr = 0; |
---|
848 | dag_record_t *erfptr = 0; |
---|
849 | struct wag_event_t *event = (struct wag_event_t *)packet->buffer; |
---|
850 | struct wag_data_event_t *data_event; |
---|
851 | |
---|
852 | |
---|
853 | switch(packet->trace->format) { |
---|
854 | case ERF: |
---|
855 | case DAG: |
---|
856 | case RTCLIENT: |
---|
857 | erfptr = (dag_record_t *)packet->buffer; |
---|
858 | if (erfptr->flags.rxerror == 1) { |
---|
859 | return NULL; |
---|
860 | } |
---|
861 | if (trace_get_link_type(packet)==TRACE_TYPE_ETH) |
---|
862 | ethptr = ((uint8_t *)packet->buffer + |
---|
863 | dag_record_size + 2); |
---|
864 | else |
---|
865 | ethptr = ((uint8_t *)packet->buffer + |
---|
866 | dag_record_size + 2); |
---|
867 | break; |
---|
868 | #if HAVE_PCAP |
---|
869 | case PCAPINT: |
---|
870 | case PCAP: |
---|
871 | ethptr = (packet->buffer + sizeof(struct pcap_pkthdr)); |
---|
872 | break; |
---|
873 | #endif |
---|
874 | case WAGINT: |
---|
875 | case WAG: |
---|
876 | switch (event->type) { |
---|
877 | case 0x0: |
---|
878 | data_event = (void*)&(event->payload); |
---|
879 | return data_event->data; |
---|
880 | default: |
---|
881 | fprintf(stderr,"Unknown WAG Event (0x%08x)\n",event->type); |
---|
882 | return NULL; |
---|
883 | } |
---|
884 | |
---|
885 | default: |
---|
886 | fprintf(stderr,"Don't know this trace format\n"); |
---|
887 | assert(0); |
---|
888 | } |
---|
889 | return ethptr; |
---|
890 | } |
---|
891 | |
---|
892 | /** get a pointer to the IP header (if any) |
---|
893 | * @param packet a pointer to a libtrace_packet structure |
---|
894 | * |
---|
895 | * @returns a pointer to the IP header, or NULL if there is not an IP packet |
---|
896 | */ |
---|
897 | struct libtrace_ip *trace_get_ip(struct libtrace_packet_t *packet) { |
---|
898 | struct libtrace_ip *ipptr = 0; |
---|
899 | |
---|
900 | switch(trace_get_link_type(packet)) { |
---|
901 | case TRACE_TYPE_80211: |
---|
902 | { |
---|
903 | |
---|
904 | struct ieee_802_11_header *wifi = trace_get_link(packet); |
---|
905 | if (!wifi) { |
---|
906 | ipptr = NULL; |
---|
907 | break; |
---|
908 | } |
---|
909 | |
---|
910 | // Data packet? |
---|
911 | if (wifi->type != 2) { |
---|
912 | ipptr = NULL; |
---|
913 | } |
---|
914 | else { |
---|
915 | struct ieee_802_11_payload *eth = (void*)wifi->data; |
---|
916 | if (eth->type != 0x0008) { |
---|
917 | ipptr=NULL; |
---|
918 | } else { |
---|
919 | ipptr=(void*)eth->data; |
---|
920 | } |
---|
921 | } |
---|
922 | } |
---|
923 | break; |
---|
924 | case TRACE_TYPE_ETH: |
---|
925 | { |
---|
926 | struct ether_header *eth = |
---|
927 | trace_get_link(packet); |
---|
928 | if (!eth) { |
---|
929 | ipptr = NULL; |
---|
930 | break; |
---|
931 | } |
---|
932 | if (ntohs(eth->ether_type)!=0x0800) { |
---|
933 | ipptr = NULL; |
---|
934 | } |
---|
935 | else { |
---|
936 | ipptr = ((void *)eth) + 14; |
---|
937 | } |
---|
938 | break; |
---|
939 | } |
---|
940 | case TRACE_TYPE_NONE: |
---|
941 | ipptr = trace_get_link(packet); |
---|
942 | break; |
---|
943 | case TRACE_TYPE_LINUX_SLL: |
---|
944 | { |
---|
945 | struct trace_sll_header_t *sll; |
---|
946 | |
---|
947 | sll = trace_get_link(packet); |
---|
948 | if (!sll) { |
---|
949 | ipptr = NULL; |
---|
950 | break; |
---|
951 | } |
---|
952 | if (ntohs(sll->protocol)!=0x0800) { |
---|
953 | ipptr = NULL; |
---|
954 | } |
---|
955 | else { |
---|
956 | ipptr = ((void*)sll)+sizeof(*sll); |
---|
957 | } |
---|
958 | } |
---|
959 | break; |
---|
960 | case TRACE_TYPE_ATM: |
---|
961 | { |
---|
962 | struct atm_rec *atm = |
---|
963 | trace_get_link(packet); |
---|
964 | // TODO: Find out what ATM does, and return |
---|
965 | // NULL for non IP data |
---|
966 | // Presumably it uses the normal stuff |
---|
967 | if (!atm) { |
---|
968 | ipptr = NULL; |
---|
969 | break; |
---|
970 | } |
---|
971 | ipptr = (void*)&atm->pload; |
---|
972 | break; |
---|
973 | } |
---|
974 | default: |
---|
975 | fprintf(stderr,"Don't understand link layer type %i in trace_get_ip()\n", |
---|
976 | trace_get_link_type(packet)); |
---|
977 | ipptr=NULL; |
---|
978 | break; |
---|
979 | } |
---|
980 | |
---|
981 | return ipptr; |
---|
982 | } |
---|
983 | |
---|
984 | #define SW_IP_OFFMASK 0xff1f |
---|
985 | |
---|
986 | /** get a pointer to the TCP header (if any) |
---|
987 | * @param packet a pointer to a libtrace_packet structure |
---|
988 | * |
---|
989 | * @returns a pointer to the TCP header, or NULL if there is not a TCP packet |
---|
990 | */ |
---|
991 | struct libtrace_tcp *trace_get_tcp(struct libtrace_packet_t *packet) { |
---|
992 | struct libtrace_tcp *tcpptr = 0; |
---|
993 | struct libtrace_ip *ipptr = 0; |
---|
994 | |
---|
995 | if(!(ipptr = trace_get_ip(packet))) { |
---|
996 | return 0; |
---|
997 | } |
---|
998 | if ((ipptr->ip_p == 6) && ((ipptr->ip_off & SW_IP_OFFMASK) == 0)) { |
---|
999 | tcpptr = (struct libtrace_tcp *)((ptrdiff_t)ipptr + (ipptr->ip_hl * 4)); |
---|
1000 | } |
---|
1001 | return tcpptr; |
---|
1002 | } |
---|
1003 | |
---|
1004 | /** get a pointer to the TCP header (if any) given a pointer to the IP header |
---|
1005 | * @param ip The IP header |
---|
1006 | * @param[out] skipped An output variable of the number of bytes skipped |
---|
1007 | * |
---|
1008 | * @returns a pointer to the TCP header, or NULL if this is not a TCP packet |
---|
1009 | * |
---|
1010 | * Skipped can be NULL, in which case it will be ignored by the program. |
---|
1011 | */ |
---|
1012 | struct libtrace_tcp *get_tcp_from_ip(struct libtrace_ip *ip, int *skipped) |
---|
1013 | { |
---|
1014 | #define SW_IP_OFFMASK 0xff1f |
---|
1015 | struct libtrace_tcp *tcpptr = 0; |
---|
1016 | |
---|
1017 | if ((ip->ip_p == 6) && ((ip->ip_off & SW_IP_OFFMASK) == 0)) { |
---|
1018 | tcpptr = (struct libtrace_tcp *)((ptrdiff_t)ip+ (ip->ip_hl * 4)); |
---|
1019 | } |
---|
1020 | |
---|
1021 | if (skipped) |
---|
1022 | *skipped=(ip->ip_hl*4); |
---|
1023 | |
---|
1024 | return tcpptr; |
---|
1025 | } |
---|
1026 | |
---|
1027 | /** get a pointer to the UDP header (if any) |
---|
1028 | * @param packet a pointer to a libtrace_packet structure |
---|
1029 | * |
---|
1030 | * @returns a pointer to the UDP header, or NULL if this is not a UDP packet |
---|
1031 | */ |
---|
1032 | struct libtrace_udp *trace_get_udp(struct libtrace_packet_t *packet) { |
---|
1033 | struct libtrace_udp *udpptr = 0; |
---|
1034 | struct libtrace_ip *ipptr = 0; |
---|
1035 | |
---|
1036 | if(!(ipptr = trace_get_ip(packet))) { |
---|
1037 | return 0; |
---|
1038 | } |
---|
1039 | if ((ipptr->ip_p == 17) && ((ipptr->ip_off & SW_IP_OFFMASK) == 0)) { |
---|
1040 | udpptr = (struct libtrace_udp *)((ptrdiff_t)ipptr + (ipptr->ip_hl * 4)); |
---|
1041 | } |
---|
1042 | |
---|
1043 | return udpptr; |
---|
1044 | } |
---|
1045 | |
---|
1046 | /** get a pointer to the UDP header (if any) given a pointer to the IP header |
---|
1047 | * @param ip The IP header |
---|
1048 | * @param[out] skipped An output variable of the number of bytes skipped |
---|
1049 | * |
---|
1050 | * @returns a pointer to the UDP header, or NULL if this is not a UDP packet |
---|
1051 | * |
---|
1052 | * Skipped can be NULL, in which case it will be ignored by the program. |
---|
1053 | */ |
---|
1054 | struct libtrace_udp *get_udp_from_ip(struct libtrace_ip *ip, int *skipped) |
---|
1055 | { |
---|
1056 | struct libtrace_udp *udpptr = 0; |
---|
1057 | |
---|
1058 | if ((ip->ip_p == 6) && ((ip->ip_off & SW_IP_OFFMASK) == 0)) { |
---|
1059 | udpptr = (struct libtrace_udp *)((ptrdiff_t)ip+ (ip->ip_hl * 4)); |
---|
1060 | } |
---|
1061 | |
---|
1062 | if (skipped) |
---|
1063 | *skipped=(ip->ip_hl*4); |
---|
1064 | |
---|
1065 | return udpptr; |
---|
1066 | } |
---|
1067 | |
---|
1068 | |
---|
1069 | /** get a pointer to the ICMP header (if any) |
---|
1070 | * @param packet a pointer to a libtrace_packet structure |
---|
1071 | * |
---|
1072 | * @returns a pointer to the ICMP header, or NULL if this is not a ICMP packet |
---|
1073 | */ |
---|
1074 | struct libtrace_icmp *trace_get_icmp(struct libtrace_packet_t *packet) { |
---|
1075 | struct libtrace_icmp *icmpptr = 0; |
---|
1076 | struct libtrace_ip *ipptr = 0; |
---|
1077 | |
---|
1078 | if(!(ipptr = trace_get_ip(packet))) { |
---|
1079 | return 0; |
---|
1080 | } |
---|
1081 | if ((ipptr->ip_p == 1)&& ((ipptr->ip_off & SW_IP_OFFMASK) == 0 )){ |
---|
1082 | icmpptr = (struct libtrace_icmp *)((ptrdiff_t)ipptr + (ipptr->ip_hl * 4)); |
---|
1083 | } |
---|
1084 | return icmpptr; |
---|
1085 | } |
---|
1086 | |
---|
1087 | /** get a pointer to the ICMP header (if any) given a pointer to the IP header |
---|
1088 | * @param ip The IP header |
---|
1089 | * @param[out] skipped An output variable of the number of bytes skipped |
---|
1090 | * |
---|
1091 | * @returns a pointer to the ICMP header, or NULL if this is not a ICMP packet |
---|
1092 | * |
---|
1093 | * Skipped can be NULL, in which case it will be ignored by the program. |
---|
1094 | */ |
---|
1095 | struct libtrace_icmp *get_icmp_from_ip(struct libtrace_ip *ip, int *skipped) |
---|
1096 | { |
---|
1097 | struct libtrace_icmp *icmpptr = 0; |
---|
1098 | |
---|
1099 | if ((ip->ip_p == 6) && ((ip->ip_off & SW_IP_OFFMASK) == 0)) { |
---|
1100 | icmpptr = (struct libtrace_icmp *)((ptrdiff_t)ip+ (ip->ip_hl * 4)); |
---|
1101 | } |
---|
1102 | |
---|
1103 | if (skipped) |
---|
1104 | *skipped=(ip->ip_hl*4); |
---|
1105 | |
---|
1106 | return icmpptr; |
---|
1107 | } |
---|
1108 | /** parse an ip or tcp option |
---|
1109 | * @param[in,out] ptr the pointer to the current option |
---|
1110 | * @param[in,out] len the length of the remaining buffer |
---|
1111 | * @param[out] type the type of the option |
---|
1112 | * @param[out] optlen the length of the option |
---|
1113 | * @param[out] data the data of the option |
---|
1114 | * |
---|
1115 | * @returns bool true if there is another option (and the fields are filled in) |
---|
1116 | * or false if this was the last option. |
---|
1117 | * |
---|
1118 | * This updates ptr to point to the next option after this one, and updates |
---|
1119 | * len to be the number of bytes remaining in the options area. Type is updated |
---|
1120 | * to be the code of this option, and data points to the data of this option, |
---|
1121 | * with optlen saying how many bytes there are. |
---|
1122 | * |
---|
1123 | * @note Beware of fragmented packets. |
---|
1124 | * @author Perry Lorier |
---|
1125 | */ |
---|
1126 | int trace_get_next_option(unsigned char **ptr,int *len, |
---|
1127 | unsigned char *type, |
---|
1128 | unsigned char *optlen, |
---|
1129 | unsigned char **data) |
---|
1130 | { |
---|
1131 | if (*len<=0) |
---|
1132 | return 0; |
---|
1133 | *type=**ptr; |
---|
1134 | switch(*type) { |
---|
1135 | case 0: /* End of options */ |
---|
1136 | return 0; |
---|
1137 | case 1: /* Pad */ |
---|
1138 | (*ptr)++; |
---|
1139 | (*len)--; |
---|
1140 | return 1; |
---|
1141 | default: |
---|
1142 | *optlen = *(*ptr+1); |
---|
1143 | if (*optlen<2) |
---|
1144 | return 0; // I have no idea wtf is going on |
---|
1145 | // with these packets |
---|
1146 | (*len)-=*optlen; |
---|
1147 | (*data)=(*ptr+2); |
---|
1148 | (*ptr)+=*optlen; |
---|
1149 | if (*len<0) |
---|
1150 | return 0; |
---|
1151 | return 1; |
---|
1152 | } |
---|
1153 | assert(0); |
---|
1154 | } |
---|
1155 | |
---|
1156 | |
---|
1157 | /** Get the current time in DAG time format |
---|
1158 | * @param packet a pointer to a libtrace_packet structure |
---|
1159 | * @returns a 64 bit timestamp in DAG ERF format (upper 32 bits are the seconds |
---|
1160 | * past 1970-01-01, the lower 32bits are partial seconds) |
---|
1161 | * @author Daniel Lawson |
---|
1162 | */ |
---|
1163 | uint64_t trace_get_erf_timestamp(const struct libtrace_packet_t *packet) { |
---|
1164 | uint64_t timestamp = 0; |
---|
1165 | dag_record_t *erfptr = 0; |
---|
1166 | struct pcap_pkthdr *pcapptr = 0; |
---|
1167 | struct wag_event_t *wagptr = 0; |
---|
1168 | switch (packet->trace->format) { |
---|
1169 | case DAG: |
---|
1170 | case ERF: |
---|
1171 | case RTCLIENT: |
---|
1172 | erfptr = (dag_record_t *)packet->buffer; |
---|
1173 | timestamp = erfptr->ts; |
---|
1174 | break; |
---|
1175 | #if HAVE_PCAP |
---|
1176 | case PCAPINT: |
---|
1177 | case PCAP: |
---|
1178 | pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
1179 | timestamp = ((((uint64_t)pcapptr->ts.tv_sec) << 32) + \ |
---|
1180 | (pcapptr->ts.tv_usec*UINT_MAX/1000000)); |
---|
1181 | break; |
---|
1182 | #endif |
---|
1183 | case WAGINT: |
---|
1184 | case WAG: |
---|
1185 | wagptr = (struct wag_event_t *)packet->buffer; |
---|
1186 | timestamp = wagptr->timestamp_lo; |
---|
1187 | timestamp |= (uint64_t)wagptr->timestamp_hi<<32; |
---|
1188 | timestamp = ((timestamp%44000000)*(UINT_MAX/44000000)) |
---|
1189 | | ((timestamp/44000000)<<32); |
---|
1190 | break; |
---|
1191 | default: |
---|
1192 | fprintf(stderr,"Unknown format in trace_get_erf_timestamp\n"); |
---|
1193 | timestamp = 0; |
---|
1194 | } |
---|
1195 | return timestamp; |
---|
1196 | } |
---|
1197 | |
---|
1198 | /** Get the current time in struct timeval |
---|
1199 | * @param packet a pointer to a libtrace_packet structure |
---|
1200 | * |
---|
1201 | * @returns time that this packet was seen in a struct timeval |
---|
1202 | * @author Daniel Lawson |
---|
1203 | * @author Perry Lorier |
---|
1204 | */ |
---|
1205 | struct timeval trace_get_timeval(const struct libtrace_packet_t *packet) { |
---|
1206 | struct timeval tv; |
---|
1207 | #if HAVE_PCAP |
---|
1208 | struct pcap_pkthdr *pcapptr = 0; |
---|
1209 | #endif |
---|
1210 | uint64_t ts; |
---|
1211 | //uint32_t seconds; |
---|
1212 | switch (packet->trace->format) { |
---|
1213 | #if HAVE_PCAP |
---|
1214 | case PCAPINT: |
---|
1215 | case PCAP: |
---|
1216 | pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
1217 | tv = pcapptr->ts; |
---|
1218 | break; |
---|
1219 | #endif |
---|
1220 | case WAGINT: |
---|
1221 | case WAG: |
---|
1222 | case DAG: |
---|
1223 | case ERF: |
---|
1224 | case RTCLIENT: |
---|
1225 | default: |
---|
1226 | // FIXME: This isn't portable to big-endian machines |
---|
1227 | ts = trace_get_erf_timestamp(packet); |
---|
1228 | tv.tv_sec = ts >> 32; |
---|
1229 | ts = (1000000 * (ts & 0xffffffffULL)); |
---|
1230 | ts += (ts & 0x80000000ULL) << 1; |
---|
1231 | tv.tv_usec = ts >> 32; |
---|
1232 | if (tv.tv_usec >= 1000000) { |
---|
1233 | tv.tv_usec -= 1000000; |
---|
1234 | tv.tv_sec += 1; |
---|
1235 | } |
---|
1236 | break; |
---|
1237 | } |
---|
1238 | return tv; |
---|
1239 | } |
---|
1240 | |
---|
1241 | /** Get the current time in floating point seconds |
---|
1242 | * @param packet a pointer to a libtrace_packet structure |
---|
1243 | * @returns time that this packet was seen in 64bit floating point seconds |
---|
1244 | * @author Perry Lorier |
---|
1245 | */ |
---|
1246 | double trace_get_seconds(const struct libtrace_packet_t *packet) { |
---|
1247 | uint64_t ts; |
---|
1248 | ts = trace_get_erf_timestamp(packet); |
---|
1249 | return (ts>>32) + ((ts & UINT_MAX)*1.0 / UINT_MAX); |
---|
1250 | } |
---|
1251 | |
---|
1252 | /** Get the size of the packet in the trace |
---|
1253 | * @param packet the packet opaque pointer |
---|
1254 | * @returns the size of the packet in the trace |
---|
1255 | * @author Perry Lorier |
---|
1256 | * @note Due to this being a header capture, or anonymisation, this may not |
---|
1257 | * be the same size as the original packet. See trace_get_wire_length() for the |
---|
1258 | * original size of the packet. |
---|
1259 | * @note This can (and often is) different for different packets in a trace! |
---|
1260 | * @par |
---|
1261 | * This is sometimes called the "snaplen". |
---|
1262 | */ |
---|
1263 | int trace_get_capture_length(const struct libtrace_packet_t *packet) { |
---|
1264 | dag_record_t *erfptr = 0; |
---|
1265 | #if HAVE_PCAP |
---|
1266 | struct pcap_pkthdr *pcapptr = 0; |
---|
1267 | #endif |
---|
1268 | struct wag_event_t *wag_event; |
---|
1269 | switch (packet->trace->format) { |
---|
1270 | case DAG: |
---|
1271 | case ERF: |
---|
1272 | case RTCLIENT: |
---|
1273 | erfptr = (dag_record_t *)packet->buffer; |
---|
1274 | return ntohs(erfptr->rlen); |
---|
1275 | #if HAVE_PCAP |
---|
1276 | case PCAPINT: |
---|
1277 | case PCAP: |
---|
1278 | pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
1279 | //return ntohs(pcapptr->caplen); |
---|
1280 | return pcapptr->caplen; |
---|
1281 | #endif |
---|
1282 | case WAGINT: |
---|
1283 | case WAG: |
---|
1284 | wag_event = (struct wag_event_t *)packet->buffer; |
---|
1285 | switch(wag_event->type) { |
---|
1286 | case 0: |
---|
1287 | return wag_event->length*4-( |
---|
1288 | sizeof(struct wag_event_t)+ |
---|
1289 | sizeof(struct wag_data_event_t) |
---|
1290 | ); |
---|
1291 | default: |
---|
1292 | assert(0); |
---|
1293 | } |
---|
1294 | default: |
---|
1295 | assert(0); |
---|
1296 | } |
---|
1297 | return -1; |
---|
1298 | } |
---|
1299 | |
---|
1300 | /** Get the size of the packet as it was seen on the wire. |
---|
1301 | * @param packet a pointer to a libtrace_packet structure |
---|
1302 | * |
---|
1303 | * @returns the size of the packet as it was on the wire. |
---|
1304 | * @author Perry Lorier |
---|
1305 | * @author Daniel Lawson |
---|
1306 | * @note Due to the trace being a header capture, or anonymisation this may |
---|
1307 | * not be the same as the Capture Len. |
---|
1308 | */ |
---|
1309 | int trace_get_wire_length(const struct libtrace_packet_t *packet){ |
---|
1310 | dag_record_t *erfptr = 0; |
---|
1311 | #if HAVE_PCAP |
---|
1312 | struct pcap_pkthdr *pcapptr = 0; |
---|
1313 | #endif |
---|
1314 | struct wag_event_t *wag_event = 0; |
---|
1315 | switch (packet->trace->format) { |
---|
1316 | case DAG: |
---|
1317 | case ERF: |
---|
1318 | case RTCLIENT: |
---|
1319 | erfptr = (dag_record_t *)packet->buffer; |
---|
1320 | return ntohs(erfptr->wlen); |
---|
1321 | break; |
---|
1322 | #if HAVE_PCAP |
---|
1323 | case PCAPINT: |
---|
1324 | case PCAP: |
---|
1325 | pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
1326 | return ntohs(pcapptr->len); |
---|
1327 | break; |
---|
1328 | #endif |
---|
1329 | case WAGINT: |
---|
1330 | case WAG: |
---|
1331 | wag_event = (struct wag_event_t *)packet->buffer; |
---|
1332 | switch(wag_event->type) { |
---|
1333 | case 0: |
---|
1334 | return ((struct wag_data_event_t *)(&wag_event->payload))->frame_length; |
---|
1335 | default: |
---|
1336 | assert(0); |
---|
1337 | } |
---|
1338 | } |
---|
1339 | return -1; |
---|
1340 | |
---|
1341 | } |
---|
1342 | |
---|
1343 | /** Get the type of the link layer |
---|
1344 | * @param packet a pointer to a libtrace_packet structure |
---|
1345 | * @returns libtrace_linktype_t |
---|
1346 | * @author Perry Lorier |
---|
1347 | * @author Daniel Lawson |
---|
1348 | */ |
---|
1349 | libtrace_linktype_t trace_get_link_type(const struct libtrace_packet_t *packet ) { |
---|
1350 | dag_record_t *erfptr = 0; |
---|
1351 | #if HAVE_PCAP |
---|
1352 | struct pcap_pkthdr *pcapptr = 0; |
---|
1353 | #endif |
---|
1354 | int linktype = 0; |
---|
1355 | switch (packet->trace->format) { |
---|
1356 | case DAG: |
---|
1357 | case ERF: |
---|
1358 | case RTCLIENT: |
---|
1359 | erfptr = (dag_record_t *)packet->buffer; |
---|
1360 | switch (erfptr->type) { |
---|
1361 | case TYPE_ETH: return TRACE_TYPE_ETH; |
---|
1362 | case TYPE_ATM: return TRACE_TYPE_ATM; |
---|
1363 | default: assert(0); |
---|
1364 | } |
---|
1365 | return erfptr->type; |
---|
1366 | |
---|
1367 | break; |
---|
1368 | #if HAVE_PCAP |
---|
1369 | case PCAPINT: |
---|
1370 | case PCAP: |
---|
1371 | pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
1372 | linktype = pcap_datalink(packet->trace->input.pcap); |
---|
1373 | switch (linktype) { |
---|
1374 | case DLT_NULL: |
---|
1375 | return TRACE_TYPE_NONE; |
---|
1376 | case DLT_EN10MB: |
---|
1377 | return TRACE_TYPE_ETH; |
---|
1378 | case DLT_ATM_RFC1483: |
---|
1379 | return TRACE_TYPE_ATM; |
---|
1380 | case DLT_IEEE802_11: |
---|
1381 | return TRACE_TYPE_80211; |
---|
1382 | #ifdef DLT_LINUX_SLL |
---|
1383 | case DLT_LINUX_SLL: |
---|
1384 | return TRACE_TYPE_LINUX_SLL; |
---|
1385 | #endif |
---|
1386 | } |
---|
1387 | break; |
---|
1388 | #endif |
---|
1389 | case WAGINT: |
---|
1390 | case WAG: |
---|
1391 | return TRACE_TYPE_80211; |
---|
1392 | } |
---|
1393 | return -1; |
---|
1394 | } |
---|
1395 | |
---|
1396 | /** Get the source MAC addres |
---|
1397 | * @param packet a pointer to a libtrace_packet structure |
---|
1398 | * @returns a pointer to the source mac, (or NULL if there is no source MAC) |
---|
1399 | * @author Perry Lorier |
---|
1400 | */ |
---|
1401 | uint8_t *trace_get_source_mac(const struct libtrace_packet_t *packet) { |
---|
1402 | void *link = trace_get_link(packet); |
---|
1403 | struct ieee_802_11_header *wifi = link; |
---|
1404 | struct ether_header *ethptr = link; |
---|
1405 | if (!link) |
---|
1406 | return NULL; |
---|
1407 | switch (trace_get_link_type(packet)) { |
---|
1408 | case TRACE_TYPE_80211: |
---|
1409 | return (uint8_t*)&wifi->mac2; |
---|
1410 | case TRACE_TYPE_ETH: |
---|
1411 | return (uint8_t*)ðptr->ether_shost; |
---|
1412 | default: |
---|
1413 | fprintf(stderr,"Not implemented\n"); |
---|
1414 | assert(0); |
---|
1415 | } |
---|
1416 | } |
---|
1417 | |
---|
1418 | /** Get the destination MAC addres |
---|
1419 | * @param packet a libtrace_packet pointer |
---|
1420 | * @returns a pointer to the destination mac, (or NULL if there is no |
---|
1421 | * destination MAC) |
---|
1422 | * @author Perry Lorier |
---|
1423 | */ |
---|
1424 | uint8_t *trace_get_destination_mac(const struct libtrace_packet_t *packet) { |
---|
1425 | void *link = trace_get_link(packet); |
---|
1426 | struct ieee_802_11_header *wifi = link; |
---|
1427 | struct ether_header *ethptr = link; |
---|
1428 | if (!link) |
---|
1429 | return NULL; |
---|
1430 | switch (trace_get_link_type(packet)) { |
---|
1431 | case TRACE_TYPE_80211: |
---|
1432 | return (uint8_t*)&wifi->mac1; |
---|
1433 | case TRACE_TYPE_ETH: |
---|
1434 | return (uint8_t*)ðptr->ether_dhost; |
---|
1435 | default: |
---|
1436 | fprintf(stderr,"Not implemented\n"); |
---|
1437 | assert(0); |
---|
1438 | } |
---|
1439 | } |
---|
1440 | |
---|
1441 | |
---|
1442 | /** process a libtrace event |
---|
1443 | * @param trace the libtrace opaque pointer |
---|
1444 | * @param packet the libtrace_packet opaque pointer |
---|
1445 | * @returns |
---|
1446 | * TRACE_EVENT_IOWAIT Waiting on I/O on fd |
---|
1447 | * TRACE_EVENT_SLEEP Next event in seconds |
---|
1448 | * TRACE_EVENT_PACKET Packet arrived in buffer with size size |
---|
1449 | * FIXME currently keeps a copy of the packet inside the trace pointer, |
---|
1450 | * which in turn is stored inside the new packet object... |
---|
1451 | * @author Perry Lorier |
---|
1452 | */ |
---|
1453 | struct libtrace_eventobj_t trace_event(struct libtrace_t *trace, |
---|
1454 | struct libtrace_packet_t *packet) { |
---|
1455 | struct libtrace_eventobj_t event; |
---|
1456 | |
---|
1457 | if (!trace) { |
---|
1458 | fprintf(stderr,"You called trace_event() with a NULL trace object!\n"); |
---|
1459 | } |
---|
1460 | assert(trace); |
---|
1461 | assert(packet); |
---|
1462 | |
---|
1463 | /* Store the trace we are reading from into the packet opaque |
---|
1464 | * structure */ |
---|
1465 | packet->trace = trace; |
---|
1466 | |
---|
1467 | /* Is there a packet ready? */ |
---|
1468 | switch (trace->sourcetype) { |
---|
1469 | #if HAVE_PCAP |
---|
1470 | case INTERFACE: |
---|
1471 | { |
---|
1472 | int data; |
---|
1473 | event.fd = pcap_fileno(trace->input.pcap); |
---|
1474 | if(ioctl(event.fd,FIONREAD,&data)==-1){ |
---|
1475 | perror("ioctl(FIONREAD)"); |
---|
1476 | } |
---|
1477 | if (data>0) { |
---|
1478 | trace_read_packet(trace,packet); |
---|
1479 | event.type = TRACE_EVENT_PACKET; |
---|
1480 | return event; |
---|
1481 | } |
---|
1482 | event.type = TRACE_EVENT_IOWAIT; |
---|
1483 | return event; |
---|
1484 | } |
---|
1485 | #endif |
---|
1486 | case SOCKET: |
---|
1487 | case DEVICE: |
---|
1488 | case RT: |
---|
1489 | { |
---|
1490 | int data; |
---|
1491 | event.fd = trace->input.fd; |
---|
1492 | if(ioctl(event.fd,FIONREAD,&data)==-1){ |
---|
1493 | perror("ioctl(FIONREAD)"); |
---|
1494 | } |
---|
1495 | if (data>0) { |
---|
1496 | trace_read_packet(trace,packet); |
---|
1497 | event.type = TRACE_EVENT_PACKET; |
---|
1498 | return event; |
---|
1499 | } |
---|
1500 | event.type = TRACE_EVENT_IOWAIT; |
---|
1501 | return event; |
---|
1502 | } |
---|
1503 | case STDIN: |
---|
1504 | case TRACE: |
---|
1505 | { |
---|
1506 | double ts; |
---|
1507 | /* "Prime" the pump */ |
---|
1508 | if (!trace->packet.buffer) { |
---|
1509 | trace->packet.buffer = malloc(4096); |
---|
1510 | trace->packet.size= |
---|
1511 | trace_read_packet(trace,packet); |
---|
1512 | } |
---|
1513 | ts=trace_get_seconds(packet); |
---|
1514 | if (trace->last_ts!=0) { |
---|
1515 | event.seconds = ts - trace->last_ts; |
---|
1516 | if (event.seconds>time(NULL)-trace->start_ts) { |
---|
1517 | event.type = TRACE_EVENT_SLEEP; |
---|
1518 | return event; |
---|
1519 | } |
---|
1520 | |
---|
1521 | } |
---|
1522 | else { |
---|
1523 | trace->start_ts = time(NULL); |
---|
1524 | trace->last_ts = ts; |
---|
1525 | } |
---|
1526 | |
---|
1527 | packet->size = trace->packet.size; |
---|
1528 | memcpy(packet->buffer,trace->packet.buffer,trace->packet.size); |
---|
1529 | |
---|
1530 | free(trace->packet.buffer); |
---|
1531 | trace->packet.buffer = 0; |
---|
1532 | event.type = TRACE_EVENT_PACKET; |
---|
1533 | return event; |
---|
1534 | } |
---|
1535 | default: |
---|
1536 | assert(0); |
---|
1537 | } |
---|
1538 | assert(0); |
---|
1539 | } |
---|
1540 | |
---|
1541 | /** setup a BPF filter |
---|
1542 | * @param filterstring a char * containing the bpf filter string |
---|
1543 | * @returns opaque pointer pointer to a libtrace_filter_t object |
---|
1544 | * @author Daniel Lawson |
---|
1545 | */ |
---|
1546 | struct libtrace_filter_t *trace_bpf_setfilter(const char *filterstring) { |
---|
1547 | #if HAVE_BPF |
---|
1548 | struct libtrace_filter_t *filter = malloc(sizeof(struct libtrace_filter_t)); |
---|
1549 | filter->filterstring = strdup(filterstring); |
---|
1550 | filter->filter = 0; |
---|
1551 | return filter; |
---|
1552 | #else |
---|
1553 | fprintf(stderr,"This version of libtrace does not have bpf filter support\n"); |
---|
1554 | return 0; |
---|
1555 | #endif |
---|
1556 | } |
---|
1557 | |
---|
1558 | /** apply a BPF filter |
---|
1559 | * @param filter the filter opaque pointer |
---|
1560 | * @param packet the packet opaque pointer |
---|
1561 | * @returns 0 if the filter fails, 1 if it succeeds |
---|
1562 | * @author Daniel Lawson |
---|
1563 | */ |
---|
1564 | int trace_bpf_filter(struct libtrace_filter_t *filter, |
---|
1565 | struct libtrace_packet_t *packet) { |
---|
1566 | #if HAVE_BPF |
---|
1567 | void *linkptr = 0; |
---|
1568 | int clen = 0; |
---|
1569 | assert(filter); |
---|
1570 | assert(packet); |
---|
1571 | linkptr = trace_get_link(packet); |
---|
1572 | if (!linkptr) { |
---|
1573 | return 0; |
---|
1574 | } |
---|
1575 | |
---|
1576 | clen = trace_get_capture_length(packet); |
---|
1577 | |
---|
1578 | |
---|
1579 | if (filter->filterstring && ! filter->filter) { |
---|
1580 | pcap_t *pcap; |
---|
1581 | struct bpf_program bpfprog; |
---|
1582 | |
---|
1583 | switch (trace_get_link_type(packet)) { |
---|
1584 | case TRACE_TYPE_ETH: |
---|
1585 | pcap = pcap_open_dead(DLT_EN10MB, 1500); |
---|
1586 | break; |
---|
1587 | case TRACE_TYPE_LINUX_SLL: |
---|
1588 | pcap = pcap_open_dead(DLT_LINUX_SLL, 1500); |
---|
1589 | break; |
---|
1590 | default: |
---|
1591 | printf("only works for ETH and LINUX_SLL (ppp) at the moment\n"); |
---|
1592 | assert(0); |
---|
1593 | } |
---|
1594 | |
---|
1595 | // build filter |
---|
1596 | if (pcap_compile( pcap, &bpfprog, filter->filterstring, 1, 0)) { |
---|
1597 | printf("bpf compilation error: %s\n", |
---|
1598 | pcap_geterr(pcap)); |
---|
1599 | assert(0); |
---|
1600 | } |
---|
1601 | pcap_close(pcap); |
---|
1602 | filter->filter = bpfprog.bf_insns; |
---|
1603 | } |
---|
1604 | |
---|
1605 | assert(filter->filter); |
---|
1606 | return bpf_filter(filter->filter, linkptr, clen, clen); |
---|
1607 | #else |
---|
1608 | fprintf(stderr,"This version of libtrace does not have bpf filter support\n"); |
---|
1609 | return 0; |
---|
1610 | #endif |
---|
1611 | } |
---|
1612 | |
---|
1613 | /** Set the direction flag, if it has one |
---|
1614 | * @param packet the packet opaque pointer |
---|
1615 | * @param direction the new direction (0,1,2,3) |
---|
1616 | * @returns a signed value containing the direction flag, or -1 if this is not supported |
---|
1617 | * @author Daniel Lawson |
---|
1618 | */ |
---|
1619 | int8_t trace_set_direction(struct libtrace_packet_t *packet, int8_t direction) { |
---|
1620 | |
---|
1621 | dag_record_t *erfptr = 0; |
---|
1622 | assert(packet); |
---|
1623 | |
---|
1624 | switch(packet->trace->format) { |
---|
1625 | case DAG: |
---|
1626 | case ERF: |
---|
1627 | case RTCLIENT: |
---|
1628 | erfptr = (dag_record_t *)packet->buffer; |
---|
1629 | erfptr->flags.iface = direction; |
---|
1630 | break; |
---|
1631 | default: |
---|
1632 | direction = -1; |
---|
1633 | } |
---|
1634 | |
---|
1635 | return direction; |
---|
1636 | |
---|
1637 | |
---|
1638 | } |
---|
1639 | |
---|
1640 | /** Get the direction flag, if it has one |
---|
1641 | * @param packet a pointer to a libtrace_packet structure |
---|
1642 | * @returns a signed value containing the direction flag, or -1 if this is not supported |
---|
1643 | * The direction is defined as 0 for packets originating locally (ie, outbound) |
---|
1644 | * and 1 for packets originating remotely (ie, inbound). |
---|
1645 | * Other values are possible, which might be overloaded to mean special things |
---|
1646 | * for a special trace. |
---|
1647 | * @author Daniel Lawson |
---|
1648 | */ |
---|
1649 | int8_t trace_get_direction(const struct libtrace_packet_t *packet) { |
---|
1650 | |
---|
1651 | int8_t direction; |
---|
1652 | dag_record_t *erfptr = 0; |
---|
1653 | assert(packet); |
---|
1654 | direction = -1; |
---|
1655 | |
---|
1656 | switch(packet->trace->format) { |
---|
1657 | case DAG: |
---|
1658 | case ERF: |
---|
1659 | case RTCLIENT: |
---|
1660 | erfptr = (dag_record_t *)packet->buffer; |
---|
1661 | direction = erfptr->flags.iface; |
---|
1662 | break; |
---|
1663 | case PCAP: |
---|
1664 | case PCAPINT: |
---|
1665 | switch (trace_get_link_type(packet)) { |
---|
1666 | case TRACE_TYPE_LINUX_SLL: |
---|
1667 | { |
---|
1668 | struct trace_sll_header_t *sll; |
---|
1669 | sll = trace_get_link(packet); |
---|
1670 | if (!sll) { |
---|
1671 | return -1; |
---|
1672 | } |
---|
1673 | /* 0 == LINUX_SLL_HOST */ |
---|
1674 | /* the Waikato Capture point defines "packets |
---|
1675 | * originating locally" (ie, outbound), with a |
---|
1676 | * direction of 0, and "packets destined locally" |
---|
1677 | * (ie, inbound), with a direction of 1. |
---|
1678 | * This is kind-of-opposite to LINUX_SLL. |
---|
1679 | * We return consistent values here, however |
---|
1680 | * |
---|
1681 | * Note that in recent versions of pcap, you can |
---|
1682 | * use "inbound" and "outbound" on ppp in linux |
---|
1683 | */ |
---|
1684 | if (ntohs(sll->pkttype==0)) { |
---|
1685 | |
---|
1686 | direction = 1; |
---|
1687 | } |
---|
1688 | else { |
---|
1689 | direction = 0; |
---|
1690 | } |
---|
1691 | break; |
---|
1692 | } |
---|
1693 | default: |
---|
1694 | /* pass */ |
---|
1695 | break; |
---|
1696 | } |
---|
1697 | default: |
---|
1698 | /* pass */ |
---|
1699 | break; |
---|
1700 | } |
---|
1701 | |
---|
1702 | return direction; |
---|
1703 | |
---|
1704 | |
---|
1705 | } |
---|
1706 | |
---|
1707 | #define ROOT_SERVER(x) ((x) < 512) |
---|
1708 | #define ROOT_CLIENT(x) ((512 <= (x)) && ((x) < 1024)) |
---|
1709 | #define NONROOT_SERVER(x) ((x) >= 5000) |
---|
1710 | #define NONROOT_CLIENT(x) ((1024 <= (x)) && ((x) < 5000)) |
---|
1711 | #define DYNAMIC(x) ((49152 < (x)) && ((x) < 65535)) |
---|
1712 | #define SERVER(x) ROOT_SERVER(x) || NONROOT_SERVER(x) |
---|
1713 | #define CLIENT(x) ROOT_CLIENT(x) || NONROOT_CLIENT(x) |
---|
1714 | |
---|
1715 | |
---|
1716 | /* Attempt to deduce the 'server' port |
---|
1717 | * @param protocol the IP protocol (eg, 6 or 17 for TCP or UDP) |
---|
1718 | * @param source the TCP or UDP source port |
---|
1719 | * @param dest the TCP or UDP destination port |
---|
1720 | * @returns a hint as to which port is the server port |
---|
1721 | * @author Daniel Lawson |
---|
1722 | */ |
---|
1723 | int8_t trace_get_server_port(uint8_t protocol, uint16_t source, uint16_t dest) { |
---|
1724 | /* |
---|
1725 | * * If the ports are equal, return DEST |
---|
1726 | * * Check for well-known ports in the given protocol |
---|
1727 | * * Root server ports: 0 - 511 |
---|
1728 | * * Root client ports: 512 - 1023 |
---|
1729 | * * non-root client ports: 1024 - 4999 |
---|
1730 | * * non-root server ports: 5000+ |
---|
1731 | * * Check for static ranges: 1024 - 49151 |
---|
1732 | * * Check for dynamic ranges: 49152 - 65535 |
---|
1733 | * * flip a coin. |
---|
1734 | */ |
---|
1735 | |
---|
1736 | uint16_t server, client; |
---|
1737 | |
---|
1738 | /* equal */ |
---|
1739 | if (source == client) |
---|
1740 | return USE_DEST; |
---|
1741 | |
---|
1742 | /* root server port, 0 - 511 */ |
---|
1743 | if (ROOT_SERVER(source) && ROOT_SERVER(dest)) { |
---|
1744 | if (source < dest) |
---|
1745 | return USE_SOURCE; |
---|
1746 | return USE_DEST; |
---|
1747 | } |
---|
1748 | |
---|
1749 | if (ROOT_SERVER(source) && !ROOT_SERVER(dest)) |
---|
1750 | return USE_SOURCE; |
---|
1751 | if (!ROOT_SERVER(source) && ROOT_SERVER(dest)) |
---|
1752 | return USE_DEST; |
---|
1753 | |
---|
1754 | /* non-root server */ |
---|
1755 | if (NONROOT_SERVER(source) && NONROOT_SERVER(dest)) { |
---|
1756 | if (source < dest) |
---|
1757 | return USE_SOURCE; |
---|
1758 | return USE_DEST; |
---|
1759 | } |
---|
1760 | if (NONROOT_SERVER(source) && !NONROOT_SERVER(dest)) |
---|
1761 | return USE_SOURCE; |
---|
1762 | if (!NONROOT_SERVER(source) && NONROOT_SERVER(dest)) |
---|
1763 | return USE_DEST; |
---|
1764 | |
---|
1765 | /* root client */ |
---|
1766 | if (ROOT_CLIENT(source) && ROOT_CLIENT(dest)) { |
---|
1767 | if (source < dest) |
---|
1768 | return USE_SOURCE; |
---|
1769 | return USE_DEST; |
---|
1770 | } |
---|
1771 | if (ROOT_CLIENT(source) && !ROOT_CLIENT(dest)) { |
---|
1772 | /* prefer root-client over nonroot-client */ |
---|
1773 | if (NONROOT_CLIENT(dest)) |
---|
1774 | return USE_SOURCE; |
---|
1775 | return USE_DEST; |
---|
1776 | } |
---|
1777 | if (!ROOT_CLIENT(source) && ROOT_CLIENT(dest)) { |
---|
1778 | /* prefer root-client over nonroot-client */ |
---|
1779 | if (NONROOT_CLIENT(source)) |
---|
1780 | return USE_DEST; |
---|
1781 | return USE_SOURCE; |
---|
1782 | } |
---|
1783 | |
---|
1784 | /* nonroot client */ |
---|
1785 | if (NONROOT_CLIENT(source) && NONROOT_CLIENT(dest)) { |
---|
1786 | if (source < dest) |
---|
1787 | return USE_SOURCE; |
---|
1788 | return USE_DEST; |
---|
1789 | } |
---|
1790 | if (NONROOT_CLIENT(source) && !NONROOT_CLIENT(dest)) |
---|
1791 | return USE_DEST; |
---|
1792 | if (!NONROOT_CLIENT(source) && NONROOT_CLIENT(dest)) |
---|
1793 | return USE_SOURCE; |
---|
1794 | |
---|
1795 | /* dynamic range */ |
---|
1796 | if (DYNAMIC(source) && DYNAMIC(dest)) |
---|
1797 | if (source < dest) |
---|
1798 | return USE_SOURCE; |
---|
1799 | return USE_DEST; |
---|
1800 | if (DYNAMIC(source) && !DYNAMIC(dest)) |
---|
1801 | return USE_DEST; |
---|
1802 | if (!DYNAMIC(source) && DYNAMIC(dest)) |
---|
1803 | return USE_SOURCE; |
---|
1804 | /* |
---|
1805 | if (SERVER(source) && CLIENT(dest)) |
---|
1806 | return USE_SOURCE; |
---|
1807 | |
---|
1808 | if (SERVER(dest) && CLIENT(source)) |
---|
1809 | return USE_DEST; |
---|
1810 | if (ROOT_SERVER(source) && !ROOT_SERVER(dest)) |
---|
1811 | return USE_SOURCE; |
---|
1812 | if (ROOT_SERVER(dest) && !ROOT_SERVER(source)) |
---|
1813 | return USE_DEST; |
---|
1814 | */ |
---|
1815 | // failing that test... |
---|
1816 | if (source < dest) { |
---|
1817 | return USE_SOURCE; |
---|
1818 | } |
---|
1819 | return USE_DEST; |
---|
1820 | |
---|
1821 | } |
---|
1822 | |
---|
1823 | /** Truncate the packet at the suggested length |
---|
1824 | * @param packet the packet opaque pointer |
---|
1825 | * @param size the new length of the packet |
---|
1826 | * @returns the new length of the packet, or the original length of the |
---|
1827 | * packet if unchanged |
---|
1828 | * NOTE: len refers to the network-level payload of the packet, and not |
---|
1829 | * any capture headers included as well. For example, to truncate a packet |
---|
1830 | * after the IP header, set scan to sizeof(ethernet_header) + sizeof(ip_header) |
---|
1831 | * @author Daniel Lawson |
---|
1832 | */ |
---|
1833 | size_t trace_truncate_packet(struct libtrace_packet_t *packet, size_t size) { |
---|
1834 | dag_record_t *erfptr; |
---|
1835 | #if HAVE_PCAP |
---|
1836 | struct pcap_pkthdr *pcaphdr; |
---|
1837 | #endif |
---|
1838 | |
---|
1839 | assert(packet); |
---|
1840 | |
---|
1841 | if (size > packet->size) { |
---|
1842 | // can't make a packet larger |
---|
1843 | return packet->size; |
---|
1844 | } |
---|
1845 | switch (packet->trace->format) { |
---|
1846 | #if HAVE_PCAP |
---|
1847 | case PCAPINT: |
---|
1848 | case PCAP: |
---|
1849 | pcaphdr = (struct pcap_pkthdr *)packet->buffer; |
---|
1850 | pcaphdr->caplen = size + sizeof(struct pcap_pkthdr); |
---|
1851 | packet->size = pcaphdr->caplen; |
---|
1852 | break; |
---|
1853 | #endif |
---|
1854 | case ERF: |
---|
1855 | case DAG: |
---|
1856 | case RTCLIENT: |
---|
1857 | erfptr = (dag_record_t *)packet->buffer; |
---|
1858 | erfptr->rlen = ntohs(size + sizeof(dag_record_t)); |
---|
1859 | packet->size = size + sizeof(dag_record_t); |
---|
1860 | break; |
---|
1861 | case WAGINT: |
---|
1862 | case WAG: |
---|
1863 | // don't know how to do this? |
---|
1864 | break; |
---|
1865 | } |
---|
1866 | return packet->size; |
---|
1867 | } |
---|
1868 | |
---|