1 | /* |
---|
2 | * This file is part of libtrace |
---|
3 | * |
---|
4 | * Copyright (c) 2004 The University of Waikato, Hamilton, New Zealand. |
---|
5 | * Authors: Daniel Lawson |
---|
6 | * Perry Lorier |
---|
7 | * |
---|
8 | * All rights reserved. |
---|
9 | * |
---|
10 | * This code has been developed by the University of Waikato WAND |
---|
11 | * research group. For further information please see http://www.wand.net.nz/ |
---|
12 | * |
---|
13 | * libtrace is free software; you can redistribute it and/or modify |
---|
14 | * it under the terms of the GNU General Public License as published by |
---|
15 | * the Free Software Foundation; either version 2 of the License, or |
---|
16 | * (at your option) any later version. |
---|
17 | * |
---|
18 | * libtrace is distributed in the hope that it will be useful, |
---|
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
21 | * GNU General Public License for more details. |
---|
22 | * |
---|
23 | * You should have received a copy of the GNU General Public License |
---|
24 | * along with libtrace; if not, write to the Free Software |
---|
25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
26 | * |
---|
27 | * $Id$ |
---|
28 | * |
---|
29 | */ |
---|
30 | |
---|
31 | |
---|
32 | /** @file |
---|
33 | * |
---|
34 | * @brief Trace file processing library |
---|
35 | * |
---|
36 | * @author Daniel Lawson |
---|
37 | * @author Perry Lorier |
---|
38 | * |
---|
39 | * @internal |
---|
40 | */ |
---|
41 | #define _GNU_SOURCE |
---|
42 | #include "common.h" |
---|
43 | #include "config.h" |
---|
44 | #include <assert.h> |
---|
45 | #include <errno.h> |
---|
46 | #include <fcntl.h> |
---|
47 | #include <netdb.h> |
---|
48 | #include <stdio.h> |
---|
49 | #include <stdlib.h> |
---|
50 | #include <string.h> |
---|
51 | #include <sys/stat.h> |
---|
52 | #include <sys/types.h> |
---|
53 | |
---|
54 | #ifdef HAVE_LIMITS_H |
---|
55 | # include <limits.h> |
---|
56 | #endif |
---|
57 | |
---|
58 | #ifdef HAVE_SYS_LIMITS_H |
---|
59 | # include <sys/limits.h> |
---|
60 | #endif |
---|
61 | |
---|
62 | #include <sys/socket.h> |
---|
63 | #include <sys/un.h> |
---|
64 | #include <sys/mman.h> |
---|
65 | #include <unistd.h> |
---|
66 | |
---|
67 | #ifdef HAVE_NET_IF_ARP_H |
---|
68 | # include <net/if_arp.h> |
---|
69 | #endif |
---|
70 | |
---|
71 | #ifdef HAVE_NET_IF_H |
---|
72 | # include <net/if.h> |
---|
73 | #endif |
---|
74 | |
---|
75 | #ifdef HAVE_NETINET_IN_H |
---|
76 | # include <netinet/in.h> |
---|
77 | #endif |
---|
78 | |
---|
79 | #ifdef HAVE_NET_ETHERNET_H |
---|
80 | # include <net/ethernet.h> |
---|
81 | #endif |
---|
82 | |
---|
83 | #ifdef HAVE_NETINET_IF_ETHER_H |
---|
84 | # include <netinet/if_ether.h> |
---|
85 | #endif |
---|
86 | |
---|
87 | #include <time.h> |
---|
88 | #include <sys/ioctl.h> |
---|
89 | |
---|
90 | #ifdef HAVE_INTTYPES_H |
---|
91 | # include <inttypes.h> |
---|
92 | #else |
---|
93 | # error "Can't find inttypes.h - this needs to be fixed" |
---|
94 | #endif |
---|
95 | |
---|
96 | #ifdef HAVE_STDDEF_H |
---|
97 | # include <stddef.h> |
---|
98 | #else |
---|
99 | # error "Can't find stddef.h - do you define ptrdiff_t elsewhere?" |
---|
100 | #endif |
---|
101 | |
---|
102 | #include "libtrace.h" |
---|
103 | #include "fifo.h" |
---|
104 | #include "libtrace_int.h" |
---|
105 | #include "parse_cmd.h" |
---|
106 | |
---|
107 | #if HAVE_PCAP_BPF_H |
---|
108 | # include <pcap-bpf.h> |
---|
109 | #else |
---|
110 | # ifdef HAVE_NET_BPF_H |
---|
111 | # include <net/bpf.h> |
---|
112 | # endif |
---|
113 | #endif |
---|
114 | |
---|
115 | #if HAVE_PCAP_H |
---|
116 | # include <pcap.h> |
---|
117 | # ifdef HAVE_PCAP_INT_H |
---|
118 | # include <pcap-int.h> |
---|
119 | # endif |
---|
120 | #endif |
---|
121 | |
---|
122 | #ifdef HAVE_ZLIB_H |
---|
123 | # include <zlib.h> |
---|
124 | #endif |
---|
125 | |
---|
126 | |
---|
127 | #include "wag.h" |
---|
128 | |
---|
129 | #ifdef HAVE_DAG_API |
---|
130 | # include "dagnew.h" |
---|
131 | # include "dagapi.h" |
---|
132 | #else |
---|
133 | # include "dagformat.h" |
---|
134 | #endif |
---|
135 | |
---|
136 | #include "libtrace_int.h" |
---|
137 | //#include "format/format_list.h" |
---|
138 | #include <err.h> |
---|
139 | |
---|
140 | #define MAXOPTS 1024 |
---|
141 | |
---|
142 | //typedef enum {SOCKET, TRACE, STDIN, DEVICE, INTERFACE, RT } source_t; |
---|
143 | |
---|
144 | //typedef enum {ERF, PCAP, PCAPINT, DAG, RTCLIENT, WAG, WAGINT } format_e_t; |
---|
145 | |
---|
146 | //typedef enum {RTSERVER, GZERF } output_t; |
---|
147 | #if HAVE_BPF |
---|
148 | /** A type encapsulating a bpf filter |
---|
149 | * This type covers the compiled bpf filter, as well as the original filter |
---|
150 | * string |
---|
151 | * |
---|
152 | */ |
---|
153 | struct libtrace_filter_t { |
---|
154 | struct bpf_insn *filter; |
---|
155 | char * filterstring; |
---|
156 | }; |
---|
157 | #endif |
---|
158 | |
---|
159 | struct libtrace_format_t **format_list = 0; |
---|
160 | int format_size = 0; |
---|
161 | int nformats = 0; |
---|
162 | |
---|
163 | void register_format(struct libtrace_format_t *f) { |
---|
164 | // fprintf(stderr,"Registering input format %s\n",f->name); |
---|
165 | if (format_list == 0) { |
---|
166 | format_size = 10; |
---|
167 | format_list = malloc(sizeof(struct libtrace_format_t *) * format_size); |
---|
168 | } else if (format_size == nformats) { |
---|
169 | format_size = format_size + 10; |
---|
170 | format_list = realloc(format_list, |
---|
171 | sizeof(struct libtrace_format_t *) * format_size); |
---|
172 | } |
---|
173 | format_list[nformats] = f; |
---|
174 | nformats++; |
---|
175 | } |
---|
176 | |
---|
177 | /** Prints help information for libtrace |
---|
178 | * |
---|
179 | * Function prints out some basic help information regarding libtrace, |
---|
180 | * and then prints out the help() function registered with each input module |
---|
181 | */ |
---|
182 | void trace_help() { |
---|
183 | int i = 0; |
---|
184 | printf("libtrace %s\n",PACKAGE_VERSION); |
---|
185 | for (i = 0; i < nformats; i++) { |
---|
186 | if (format_list[i]->help) { |
---|
187 | format_list[i]->help(); |
---|
188 | } |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | |
---|
193 | #define RP_BUFSIZE 65536 |
---|
194 | #define URI_PROTO_LINE 16 |
---|
195 | |
---|
196 | /** Initialises the data contained within the libtrace_out_t structure, based on the provided uri. |
---|
197 | * |
---|
198 | * @param libtrace the libtrace_out_t structure to initialise |
---|
199 | * @param uri the uri defining the output type and destination |
---|
200 | * @returns 1 if initialised successfully, 0 otherwise |
---|
201 | * |
---|
202 | * @author Shane Alcock |
---|
203 | * */ |
---|
204 | static int init_output(struct libtrace_out_t **libtrace, char *uri) { |
---|
205 | char *scan = calloc(sizeof(char),URI_PROTO_LINE); |
---|
206 | char *uridata = 0; |
---|
207 | int i; |
---|
208 | |
---|
209 | // parse the URI to determine what sort of event we are dealing with |
---|
210 | |
---|
211 | // want snippet before the : to get the uri base type. |
---|
212 | |
---|
213 | if((uridata = strchr(uri,':')) == NULL) { |
---|
214 | // badly formed URI - needs a : |
---|
215 | return 0; |
---|
216 | } |
---|
217 | |
---|
218 | if ((*uridata - *uri) > URI_PROTO_LINE) { |
---|
219 | // badly formed URI - uri type is too long |
---|
220 | return 0; |
---|
221 | } |
---|
222 | |
---|
223 | strncpy(scan,uri, (uridata - uri)); |
---|
224 | |
---|
225 | (*libtrace)->format = 0; |
---|
226 | for (i = 0; i < nformats; i++) { |
---|
227 | if (strlen(scan) == strlen(format_list[i]->name) && |
---|
228 | !strncasecmp(scan, |
---|
229 | format_list[i]->name, |
---|
230 | strlen(scan))) { |
---|
231 | (*libtrace)->format=format_list[i]; |
---|
232 | break; |
---|
233 | } |
---|
234 | } |
---|
235 | if ((*libtrace)->format == 0) { |
---|
236 | fprintf(stderr, |
---|
237 | "libtrace has no support for this format (%s)\n",scan); |
---|
238 | return 0; |
---|
239 | } |
---|
240 | |
---|
241 | // push uridata past the delimiter |
---|
242 | uridata++; |
---|
243 | (*libtrace)->uridata = strdup(uridata); |
---|
244 | |
---|
245 | |
---|
246 | // libtrace->format now contains the type of uri |
---|
247 | // libtrace->uridata contains the appropriate data for this |
---|
248 | |
---|
249 | if ((*libtrace)->format->init_output) { |
---|
250 | (*libtrace)->format->init_output( (*libtrace)); |
---|
251 | } else { |
---|
252 | fprintf(stderr, |
---|
253 | "No init_output function for format %s\n",scan); |
---|
254 | return 0; |
---|
255 | } |
---|
256 | |
---|
257 | |
---|
258 | (*libtrace)->fifo = create_fifo(1048576); |
---|
259 | assert( (*libtrace)->fifo); |
---|
260 | return 1; |
---|
261 | } |
---|
262 | |
---|
263 | /** Create a trace file from a URI |
---|
264 | * |
---|
265 | * @returns opaque pointer to a libtrace_t |
---|
266 | * |
---|
267 | * Valid URI's are: |
---|
268 | * erf:/path/to/erf/file |
---|
269 | * erf:/path/to/erf/file.gz |
---|
270 | * erf:/path/to/rtclient/socket |
---|
271 | * erf:- (stdin) |
---|
272 | * pcapint:pcapinterface (eg: pcapint:eth0) |
---|
273 | * pcap:/path/to/pcap/file |
---|
274 | * pcap:- |
---|
275 | * rtclient:hostname |
---|
276 | * rtclient:hostname:port |
---|
277 | * wag:- |
---|
278 | * wag:/path/to/wag/file |
---|
279 | * wag:/path/to/wag/file.gz |
---|
280 | * wag:/path/to/wag/socket |
---|
281 | * wagint:/dev/device |
---|
282 | * |
---|
283 | * URIs which have yet to be implemented are: |
---|
284 | * dag:/dev/dagcard |
---|
285 | * pcap:/path/to/pcap/socket |
---|
286 | * |
---|
287 | * If an error occured when attempting to open a trace, NULL is returned |
---|
288 | * and an error is output to stdout. |
---|
289 | */ |
---|
290 | struct libtrace_t *trace_create(char *uri) { |
---|
291 | struct libtrace_t *libtrace = malloc(sizeof(struct libtrace_t)); |
---|
292 | char *scan = calloc(sizeof(char),URI_PROTO_LINE); |
---|
293 | char *uridata = 0; |
---|
294 | int i = 0; |
---|
295 | struct stat buf; |
---|
296 | |
---|
297 | // parse the URI to determine what sort of event we are dealing with |
---|
298 | |
---|
299 | // want snippet before the : to get the uri base type. |
---|
300 | |
---|
301 | if((uridata = strchr(uri,':')) == NULL) { |
---|
302 | // badly formed URI - needs a : |
---|
303 | return 0; |
---|
304 | } |
---|
305 | |
---|
306 | if ((*uridata - *uri) > URI_PROTO_LINE) { |
---|
307 | // badly formed URI - uri type is too long |
---|
308 | return 0; |
---|
309 | } |
---|
310 | strncpy(scan,uri, (uridata - uri)); |
---|
311 | |
---|
312 | libtrace->event.tdelta = 0.0; |
---|
313 | |
---|
314 | |
---|
315 | libtrace->format = 0; |
---|
316 | for (i = 0; i < nformats; i++) { |
---|
317 | if (strlen(scan) == strlen(format_list[i]->name) && |
---|
318 | !strncasecmp(scan, |
---|
319 | format_list[i]->name, |
---|
320 | strlen(scan))) { |
---|
321 | libtrace->format=format_list[i]; |
---|
322 | break; |
---|
323 | } |
---|
324 | } |
---|
325 | if (libtrace->format == 0) { |
---|
326 | fprintf(stderr, |
---|
327 | "libtrace has no support for this format (%s)\n",scan); |
---|
328 | return 0; |
---|
329 | } |
---|
330 | |
---|
331 | // push uridata past the delimiter |
---|
332 | uridata++; |
---|
333 | libtrace->uridata = strdup(uridata); |
---|
334 | |
---|
335 | // libtrace->format now contains the type of uri |
---|
336 | // libtrace->uridata contains the appropriate data for this |
---|
337 | |
---|
338 | if (libtrace->format->init_input) { |
---|
339 | libtrace->format->init_input( libtrace); |
---|
340 | } else { |
---|
341 | fprintf(stderr, |
---|
342 | "No init function for format %s\n",scan); |
---|
343 | return 0; |
---|
344 | } |
---|
345 | |
---|
346 | |
---|
347 | libtrace->fifo = create_fifo(1048576); |
---|
348 | assert( libtrace->fifo); |
---|
349 | |
---|
350 | return libtrace; |
---|
351 | } |
---|
352 | |
---|
353 | /** Creates a libtrace_out_t structure and the socket / file through which output will be directed. |
---|
354 | * |
---|
355 | * @param uri the uri string describing the output format and the destination |
---|
356 | * @returns the newly created libtrace_out_t structure |
---|
357 | * |
---|
358 | * @author Shane Alcock |
---|
359 | * */ |
---|
360 | struct libtrace_out_t *trace_output_create(char *uri) { |
---|
361 | struct libtrace_out_t *libtrace = malloc(sizeof(struct libtrace_out_t)); |
---|
362 | |
---|
363 | char *scan = calloc(sizeof(char),URI_PROTO_LINE); |
---|
364 | char *uridata = 0; |
---|
365 | int i; |
---|
366 | |
---|
367 | // parse the URI to determine what sort of event we are dealing with |
---|
368 | |
---|
369 | // want snippet before the : to get the uri base type. |
---|
370 | |
---|
371 | if((uridata = strchr(uri,':')) == NULL) { |
---|
372 | // badly formed URI - needs a : |
---|
373 | return 0; |
---|
374 | } |
---|
375 | |
---|
376 | if ((*uridata - *uri) > URI_PROTO_LINE) { |
---|
377 | // badly formed URI - uri type is too long |
---|
378 | return 0; |
---|
379 | } |
---|
380 | |
---|
381 | strncpy(scan,uri, (uridata - uri)); |
---|
382 | |
---|
383 | libtrace->format = 0; |
---|
384 | for (i = 0; i < nformats; i++) { |
---|
385 | if (strlen(scan) == strlen(format_list[i]->name) && |
---|
386 | !strncasecmp(scan, |
---|
387 | format_list[i]->name, |
---|
388 | strlen(scan))) { |
---|
389 | libtrace->format=format_list[i]; |
---|
390 | break; |
---|
391 | } |
---|
392 | } |
---|
393 | if (libtrace->format == 0) { |
---|
394 | fprintf(stderr, |
---|
395 | "libtrace has no support for this format (%s)\n",scan); |
---|
396 | return 0; |
---|
397 | } |
---|
398 | |
---|
399 | // push uridata past the delimiter |
---|
400 | uridata++; |
---|
401 | libtrace->uridata = strdup(uridata); |
---|
402 | |
---|
403 | |
---|
404 | // libtrace->format now contains the type of uri |
---|
405 | // libtrace->uridata contains the appropriate data for this |
---|
406 | |
---|
407 | if (libtrace->format->init_output) { |
---|
408 | libtrace->format->init_output( libtrace); |
---|
409 | } else { |
---|
410 | fprintf(stderr, |
---|
411 | "No init_output function for format %s\n",scan); |
---|
412 | return 0; |
---|
413 | } |
---|
414 | |
---|
415 | |
---|
416 | libtrace->fifo = create_fifo(1048576); |
---|
417 | assert( libtrace->fifo); |
---|
418 | |
---|
419 | return libtrace; |
---|
420 | } |
---|
421 | |
---|
422 | /** Parses an output options string and calls the appropriate function to deal with output options. |
---|
423 | * |
---|
424 | * @param libtrace the output trace object to apply the options to |
---|
425 | * @param options the options string |
---|
426 | * @returns -1 if option configuration failed, 0 otherwise |
---|
427 | * |
---|
428 | * @author Shane Alcock |
---|
429 | */ |
---|
430 | int trace_output_config(struct libtrace_out_t *libtrace, char *options) { |
---|
431 | char *opt_string = 0; |
---|
432 | char *opt_argv[MAXOPTS]; |
---|
433 | int opt_argc = 0; |
---|
434 | |
---|
435 | assert(libtrace); |
---|
436 | |
---|
437 | if (!options) { |
---|
438 | printf("No options specified\n"); |
---|
439 | return 0; |
---|
440 | } |
---|
441 | |
---|
442 | asprintf(&opt_string, "%s %s", libtrace->format->name, options); |
---|
443 | parse_cmd(opt_string, &opt_argc, opt_argv, MAXOPTS); |
---|
444 | |
---|
445 | if (libtrace->format->config_output) |
---|
446 | return libtrace->format->config_output(libtrace, opt_argc, opt_argv); |
---|
447 | |
---|
448 | } |
---|
449 | |
---|
450 | /** Close a trace file, freeing up any resources it may have been using |
---|
451 | * |
---|
452 | */ |
---|
453 | void trace_destroy(struct libtrace_t *libtrace) { |
---|
454 | assert(libtrace); |
---|
455 | libtrace->format->fin_input(libtrace); |
---|
456 | // need to free things! |
---|
457 | destroy_fifo(libtrace->fifo); |
---|
458 | free(libtrace); |
---|
459 | } |
---|
460 | |
---|
461 | /** Close an output trace file, freeing up any resources it may have been using |
---|
462 | * |
---|
463 | * @param libtrace the output trace file to be destroyed |
---|
464 | * |
---|
465 | * @author Shane Alcock |
---|
466 | * */ |
---|
467 | void trace_output_destroy(struct libtrace_out_t *libtrace) { |
---|
468 | assert(libtrace); |
---|
469 | libtrace->format->fin_output(libtrace); |
---|
470 | destroy_fifo(libtrace->fifo); |
---|
471 | free(libtrace); |
---|
472 | } |
---|
473 | |
---|
474 | /** Read one packet from the trace into buffer |
---|
475 | * |
---|
476 | * @param libtrace the libtrace opaque pointer |
---|
477 | * @param packet the packet opaque pointer |
---|
478 | * @returns false if it failed to read a packet |
---|
479 | * |
---|
480 | */ |
---|
481 | int trace_read_packet(struct libtrace_t *libtrace, struct libtrace_packet_t *packet) { |
---|
482 | |
---|
483 | if (!libtrace) { |
---|
484 | fprintf(stderr,"Oi! You called trace_read_packet() with a NULL libtrace parameter!\n"); |
---|
485 | } |
---|
486 | assert(libtrace); |
---|
487 | assert(packet); |
---|
488 | |
---|
489 | /* Store the trace we are reading from into the packet opaque |
---|
490 | * structure */ |
---|
491 | packet->trace = libtrace; |
---|
492 | |
---|
493 | if (libtrace->format->read_packet) { |
---|
494 | return libtrace->format->read_packet(libtrace,packet); |
---|
495 | } |
---|
496 | } |
---|
497 | |
---|
498 | /** Writes a packet to the specified output |
---|
499 | * |
---|
500 | * @param libtrace describes the output format, destination, etc. |
---|
501 | * @param packet the packet to be written out |
---|
502 | * @returns the number of bytes written, -1 if write failed |
---|
503 | * |
---|
504 | * @author Shane Alcock |
---|
505 | * */ |
---|
506 | int trace_write_packet(struct libtrace_out_t *libtrace, struct libtrace_packet_t *packet) { |
---|
507 | assert(libtrace); |
---|
508 | assert(packet); |
---|
509 | |
---|
510 | if (libtrace->format->write_packet) { |
---|
511 | return libtrace->format->write_packet(libtrace, packet); |
---|
512 | } |
---|
513 | |
---|
514 | } |
---|
515 | |
---|
516 | /** get a pointer to the link layer |
---|
517 | * @param packet a pointer to a libtrace_packet structure |
---|
518 | * |
---|
519 | * @returns a pointer to the link layer, or NULL if there is no link layer |
---|
520 | * you should call trace_get_link_type() to find out what type of link layer this is |
---|
521 | */ |
---|
522 | void *trace_get_link(const struct libtrace_packet_t *packet) { |
---|
523 | const void *ethptr = 0; |
---|
524 | |
---|
525 | if (packet->trace->format->get_link) { |
---|
526 | ethptr = packet->trace->format->get_link(packet); |
---|
527 | } |
---|
528 | return (void *)ethptr; |
---|
529 | } |
---|
530 | |
---|
531 | /** get a pointer to the IP header (if any) |
---|
532 | * @param packet a pointer to a libtrace_packet structure |
---|
533 | * |
---|
534 | * @returns a pointer to the IP header, or NULL if there is not an IP packet |
---|
535 | */ |
---|
536 | struct libtrace_ip *trace_get_ip(const struct libtrace_packet_t *packet) { |
---|
537 | struct libtrace_ip *ipptr = 0; |
---|
538 | |
---|
539 | switch(trace_get_link_type(packet)) { |
---|
540 | case TRACE_TYPE_80211: |
---|
541 | { |
---|
542 | |
---|
543 | struct ieee_802_11_header *wifi = trace_get_link(packet); |
---|
544 | if (!wifi) { |
---|
545 | ipptr = NULL; |
---|
546 | break; |
---|
547 | } |
---|
548 | |
---|
549 | // Data packet? |
---|
550 | if (wifi->type != 2) { |
---|
551 | ipptr = NULL; |
---|
552 | } |
---|
553 | else { |
---|
554 | struct ieee_802_11_payload *eth = (void*)wifi->data; |
---|
555 | if (eth->type != 0x0008) { |
---|
556 | ipptr=NULL; |
---|
557 | } else { |
---|
558 | ipptr=(void*)eth->data; |
---|
559 | } |
---|
560 | } |
---|
561 | } |
---|
562 | break; |
---|
563 | case TRACE_TYPE_ETH: |
---|
564 | { |
---|
565 | struct ether_header *eth = |
---|
566 | trace_get_link(packet); |
---|
567 | if (!eth) { |
---|
568 | ipptr = NULL; |
---|
569 | break; |
---|
570 | } |
---|
571 | if (ntohs(eth->ether_type)!=0x0800) { |
---|
572 | ipptr = NULL; |
---|
573 | } |
---|
574 | else { |
---|
575 | ipptr = ((void *)eth) + 14; |
---|
576 | } |
---|
577 | break; |
---|
578 | } |
---|
579 | case TRACE_TYPE_NONE: |
---|
580 | ipptr = trace_get_link(packet); |
---|
581 | break; |
---|
582 | case TRACE_TYPE_LINUX_SLL: |
---|
583 | { |
---|
584 | struct trace_sll_header_t *sll; |
---|
585 | |
---|
586 | sll = trace_get_link(packet); |
---|
587 | if (!sll) { |
---|
588 | ipptr = NULL; |
---|
589 | break; |
---|
590 | } |
---|
591 | if (ntohs(sll->protocol)!=0x0800) { |
---|
592 | ipptr = NULL; |
---|
593 | } |
---|
594 | else { |
---|
595 | ipptr = ((void*)sll)+sizeof(*sll); |
---|
596 | } |
---|
597 | } |
---|
598 | break; |
---|
599 | case TRACE_TYPE_PFLOG: |
---|
600 | { |
---|
601 | struct trace_pflog_header_t *pflog; |
---|
602 | pflog = trace_get_link(packet); |
---|
603 | if (!pflog) { |
---|
604 | ipptr = NULL; |
---|
605 | break; |
---|
606 | } |
---|
607 | if (pflog->af != AF_INET) { |
---|
608 | ipptr = NULL; |
---|
609 | } else { |
---|
610 | ipptr = ((void*)pflog)+sizeof(*pflog); |
---|
611 | } |
---|
612 | } |
---|
613 | break; |
---|
614 | case TRACE_TYPE_ATM: |
---|
615 | { |
---|
616 | struct atm_rec *atm = |
---|
617 | trace_get_link(packet); |
---|
618 | // TODO: Find out what ATM does, and return |
---|
619 | // NULL for non IP data |
---|
620 | // Presumably it uses the normal stuff |
---|
621 | if (!atm) { |
---|
622 | ipptr = NULL; |
---|
623 | break; |
---|
624 | } |
---|
625 | ipptr = (void*)&atm->pload; |
---|
626 | break; |
---|
627 | } |
---|
628 | default: |
---|
629 | fprintf(stderr,"Don't understand link layer type %i in trace_get_ip()\n", |
---|
630 | trace_get_link_type(packet)); |
---|
631 | ipptr=NULL; |
---|
632 | break; |
---|
633 | } |
---|
634 | |
---|
635 | return ipptr; |
---|
636 | } |
---|
637 | |
---|
638 | #define SW_IP_OFFMASK 0xff1f |
---|
639 | |
---|
640 | /** get a pointer to the TCP header (if any) |
---|
641 | * @param packet a pointer to a libtrace_packet structure |
---|
642 | * |
---|
643 | * @returns a pointer to the TCP header, or NULL if there is not a TCP packet |
---|
644 | */ |
---|
645 | struct libtrace_tcp *trace_get_tcp(const struct libtrace_packet_t *packet) { |
---|
646 | struct libtrace_tcp *tcpptr = 0; |
---|
647 | struct libtrace_ip *ipptr = 0; |
---|
648 | |
---|
649 | if(!(ipptr = trace_get_ip(packet))) { |
---|
650 | return 0; |
---|
651 | } |
---|
652 | if ((ipptr->ip_p == 6) && ((ipptr->ip_off & SW_IP_OFFMASK) == 0)) { |
---|
653 | tcpptr = (struct libtrace_tcp *)((ptrdiff_t)ipptr + (ipptr->ip_hl * 4)); |
---|
654 | } |
---|
655 | return tcpptr; |
---|
656 | } |
---|
657 | |
---|
658 | /** get a pointer to the TCP header (if any) given a pointer to the IP header |
---|
659 | * @param ip The IP header |
---|
660 | * @param[out] skipped An output variable of the number of bytes skipped |
---|
661 | * |
---|
662 | * @returns a pointer to the TCP header, or NULL if this is not a TCP packet |
---|
663 | * |
---|
664 | * Skipped can be NULL, in which case it will be ignored by the program. |
---|
665 | */ |
---|
666 | struct libtrace_tcp *get_tcp_from_ip(const struct libtrace_ip *ip, int *skipped) |
---|
667 | { |
---|
668 | #define SW_IP_OFFMASK 0xff1f |
---|
669 | struct libtrace_tcp *tcpptr = 0; |
---|
670 | |
---|
671 | if ((ip->ip_p == 6) && ((ip->ip_off & SW_IP_OFFMASK) == 0)) { |
---|
672 | tcpptr = (struct libtrace_tcp *)((ptrdiff_t)ip+ (ip->ip_hl * 4)); |
---|
673 | } |
---|
674 | |
---|
675 | if (skipped) |
---|
676 | *skipped=(ip->ip_hl*4); |
---|
677 | |
---|
678 | return tcpptr; |
---|
679 | } |
---|
680 | |
---|
681 | /** get a pointer to the UDP header (if any) |
---|
682 | * @param packet a pointer to a libtrace_packet structure |
---|
683 | * |
---|
684 | * @returns a pointer to the UDP header, or NULL if this is not a UDP packet |
---|
685 | */ |
---|
686 | struct libtrace_udp *trace_get_udp(const struct libtrace_packet_t *packet) { |
---|
687 | struct libtrace_udp *udpptr = 0; |
---|
688 | struct libtrace_ip *ipptr = 0; |
---|
689 | |
---|
690 | if(!(ipptr = trace_get_ip(packet))) { |
---|
691 | return 0; |
---|
692 | } |
---|
693 | if ((ipptr->ip_p == 17) && ((ipptr->ip_off & SW_IP_OFFMASK) == 0)) { |
---|
694 | udpptr = (struct libtrace_udp *)((ptrdiff_t)ipptr + (ipptr->ip_hl * 4)); |
---|
695 | } |
---|
696 | |
---|
697 | return udpptr; |
---|
698 | } |
---|
699 | |
---|
700 | /** get a pointer to the UDP header (if any) given a pointer to the IP header |
---|
701 | * @param ip The IP header |
---|
702 | * @param[out] skipped An output variable of the number of bytes skipped |
---|
703 | * |
---|
704 | * @returns a pointer to the UDP header, or NULL if this is not a UDP packet |
---|
705 | * |
---|
706 | * Skipped can be NULL, in which case it will be ignored by the program. |
---|
707 | */ |
---|
708 | struct libtrace_udp *get_udp_from_ip(const struct libtrace_ip *ip, int *skipped) |
---|
709 | { |
---|
710 | struct libtrace_udp *udpptr = 0; |
---|
711 | |
---|
712 | if ((ip->ip_p == 6) && ((ip->ip_off & SW_IP_OFFMASK) == 0)) { |
---|
713 | udpptr = (struct libtrace_udp *)((ptrdiff_t)ip+ (ip->ip_hl * 4)); |
---|
714 | } |
---|
715 | |
---|
716 | if (skipped) |
---|
717 | *skipped=(ip->ip_hl*4); |
---|
718 | |
---|
719 | return udpptr; |
---|
720 | } |
---|
721 | |
---|
722 | |
---|
723 | /** get a pointer to the ICMP header (if any) |
---|
724 | * @param packet a pointer to a libtrace_packet structure |
---|
725 | * |
---|
726 | * @returns a pointer to the ICMP header, or NULL if this is not a ICMP packet |
---|
727 | */ |
---|
728 | struct libtrace_icmp *trace_get_icmp(const struct libtrace_packet_t *packet) { |
---|
729 | struct libtrace_icmp *icmpptr = 0; |
---|
730 | struct libtrace_ip *ipptr = 0; |
---|
731 | |
---|
732 | if(!(ipptr = trace_get_ip(packet))) { |
---|
733 | return 0; |
---|
734 | } |
---|
735 | if ((ipptr->ip_p == 1)&& ((ipptr->ip_off & SW_IP_OFFMASK) == 0 )){ |
---|
736 | icmpptr = (struct libtrace_icmp *)((ptrdiff_t)ipptr + (ipptr->ip_hl * 4)); |
---|
737 | } |
---|
738 | return icmpptr; |
---|
739 | } |
---|
740 | |
---|
741 | /** get a pointer to the ICMP header (if any) given a pointer to the IP header |
---|
742 | * @param ip The IP header |
---|
743 | * @param[out] skipped An output variable of the number of bytes skipped |
---|
744 | * |
---|
745 | * @returns a pointer to the ICMP header, or NULL if this is not a ICMP packet |
---|
746 | * |
---|
747 | * Skipped can be NULL, in which case it will be ignored by the program. |
---|
748 | */ |
---|
749 | struct libtrace_icmp *get_icmp_from_ip(struct libtrace_ip *ip, int *skipped) |
---|
750 | { |
---|
751 | struct libtrace_icmp *icmpptr = 0; |
---|
752 | |
---|
753 | if ((ip->ip_p == 6) && ((ip->ip_off & SW_IP_OFFMASK) == 0)) { |
---|
754 | icmpptr = (struct libtrace_icmp *)((ptrdiff_t)ip+ (ip->ip_hl * 4)); |
---|
755 | } |
---|
756 | |
---|
757 | if (skipped) |
---|
758 | *skipped=(ip->ip_hl*4); |
---|
759 | |
---|
760 | return icmpptr; |
---|
761 | } |
---|
762 | /** parse an ip or tcp option |
---|
763 | * @param[in,out] ptr the pointer to the current option |
---|
764 | * @param[in,out] len the length of the remaining buffer |
---|
765 | * @param[out] type the type of the option |
---|
766 | * @param[out] optlen the length of the option |
---|
767 | * @param[out] data the data of the option |
---|
768 | * |
---|
769 | * @returns bool true if there is another option (and the fields are filled in) |
---|
770 | * or false if this was the last option. |
---|
771 | * |
---|
772 | * This updates ptr to point to the next option after this one, and updates |
---|
773 | * len to be the number of bytes remaining in the options area. Type is updated |
---|
774 | * to be the code of this option, and data points to the data of this option, |
---|
775 | * with optlen saying how many bytes there are. |
---|
776 | * |
---|
777 | * @note Beware of fragmented packets. |
---|
778 | * @author Perry Lorier |
---|
779 | */ |
---|
780 | int trace_get_next_option(unsigned char **ptr,int *len, |
---|
781 | unsigned char *type, |
---|
782 | unsigned char *optlen, |
---|
783 | unsigned char **data) |
---|
784 | { |
---|
785 | if (*len<=0) |
---|
786 | return 0; |
---|
787 | *type=**ptr; |
---|
788 | switch(*type) { |
---|
789 | case 0: /* End of options */ |
---|
790 | return 0; |
---|
791 | case 1: /* Pad */ |
---|
792 | (*ptr)++; |
---|
793 | (*len)--; |
---|
794 | return 1; |
---|
795 | default: |
---|
796 | *optlen = *(*ptr+1); |
---|
797 | if (*optlen<2) |
---|
798 | return 0; // I have no idea wtf is going on |
---|
799 | // with these packets |
---|
800 | (*len)-=*optlen; |
---|
801 | (*data)=(*ptr+2); |
---|
802 | (*ptr)+=*optlen; |
---|
803 | if (*len<0) |
---|
804 | return 0; |
---|
805 | return 1; |
---|
806 | } |
---|
807 | assert(0); |
---|
808 | } |
---|
809 | |
---|
810 | |
---|
811 | /** Get the current time in DAG time format |
---|
812 | * @param packet a pointer to a libtrace_packet structure |
---|
813 | * @returns a 64 bit timestamp in DAG ERF format (upper 32 bits are the seconds |
---|
814 | * past 1970-01-01, the lower 32bits are partial seconds) |
---|
815 | * @author Daniel Lawson |
---|
816 | */ |
---|
817 | uint64_t trace_get_erf_timestamp(const struct libtrace_packet_t *packet) { |
---|
818 | uint64_t timestamp = 0; |
---|
819 | struct timeval ts; |
---|
820 | |
---|
821 | if (packet->trace->format->get_erf_timestamp) { |
---|
822 | timestamp = packet->trace->format->get_erf_timestamp(packet); |
---|
823 | } else if (packet->trace->format->get_timeval) { |
---|
824 | ts = packet->trace->format->get_timeval(packet); |
---|
825 | timestamp = ((((uint64_t)ts.tv_sec) << 32) + \ |
---|
826 | (((uint64_t)ts.tv_usec * UINT_MAX)/1000000)); |
---|
827 | } |
---|
828 | return timestamp; |
---|
829 | } |
---|
830 | |
---|
831 | /** Get the current time in struct timeval |
---|
832 | * @param packet a pointer to a libtrace_packet structure |
---|
833 | * |
---|
834 | * @returns time that this packet was seen in a struct timeval |
---|
835 | * @author Daniel Lawson |
---|
836 | * @author Perry Lorier |
---|
837 | */ |
---|
838 | struct timeval trace_get_timeval(const struct libtrace_packet_t *packet) { |
---|
839 | struct timeval tv; |
---|
840 | uint64_t ts = 0; |
---|
841 | |
---|
842 | if (packet->trace->format->get_timeval) { |
---|
843 | tv = packet->trace->format->get_timeval(packet); |
---|
844 | } else if (packet->trace->format->get_erf_timestamp) { |
---|
845 | ts = packet->trace->format->get_erf_timestamp(packet); |
---|
846 | #if __BYTE_ORDER == __BIG_ENDIAN |
---|
847 | tv.tv_sec = ts & 0xFFFFFFFF; |
---|
848 | #elif __BYTE_ORDER == __LITTLE_ENDIAN |
---|
849 | tv.tv_sec = ts >> 32; |
---|
850 | #else |
---|
851 | #error "What on earth are you running this on?" |
---|
852 | #endif |
---|
853 | ts = (1000000 * (ts & 0xffffffffULL)); |
---|
854 | ts += (ts & 0x80000000ULL) << 1; |
---|
855 | tv.tv_usec = ts >> 32; |
---|
856 | if (tv.tv_usec >= 1000000) { |
---|
857 | tv.tv_usec -= 1000000; |
---|
858 | tv.tv_sec += 1; |
---|
859 | } |
---|
860 | } |
---|
861 | |
---|
862 | return tv; |
---|
863 | } |
---|
864 | |
---|
865 | /** Get the current time in floating point seconds |
---|
866 | * @param packet a pointer to a libtrace_packet structure |
---|
867 | * @returns time that this packet was seen in 64bit floating point seconds |
---|
868 | * @author Perry Lorier |
---|
869 | */ |
---|
870 | double trace_get_seconds(const struct libtrace_packet_t *packet) { |
---|
871 | double seconds; |
---|
872 | uint64_t ts; |
---|
873 | |
---|
874 | if (packet->trace->format->get_seconds) { |
---|
875 | seconds = packet->trace->format->get_seconds(packet); |
---|
876 | } else if (packet->trace->format->get_erf_timestamp) { |
---|
877 | ts = packet->trace->format->get_erf_timestamp(packet); |
---|
878 | seconds = (ts>>32) + ((ts & UINT_MAX)*1.0 / UINT_MAX); |
---|
879 | } |
---|
880 | return seconds; |
---|
881 | } |
---|
882 | |
---|
883 | /** Get the size of the packet in the trace |
---|
884 | * @param packet the packet opaque pointer |
---|
885 | * @returns the size of the packet in the trace |
---|
886 | * @author Perry Lorier |
---|
887 | * @note Due to this being a header capture, or anonymisation, this may not |
---|
888 | * be the same size as the original packet. See trace_get_wire_length() for the |
---|
889 | * original size of the packet. |
---|
890 | * @note This can (and often is) different for different packets in a trace! |
---|
891 | * @par |
---|
892 | * This is sometimes called the "snaplen". |
---|
893 | */ |
---|
894 | int trace_get_capture_length(const struct libtrace_packet_t *packet) { |
---|
895 | |
---|
896 | if (packet->trace->format->get_capture_length) { |
---|
897 | return packet->trace->format->get_capture_length(packet); |
---|
898 | } |
---|
899 | return -1; |
---|
900 | } |
---|
901 | |
---|
902 | /** Get the size of the packet as it was seen on the wire. |
---|
903 | * @param packet a pointer to a libtrace_packet structure |
---|
904 | * |
---|
905 | * @returns the size of the packet as it was on the wire. |
---|
906 | * @author Perry Lorier |
---|
907 | * @author Daniel Lawson |
---|
908 | * @note Due to the trace being a header capture, or anonymisation this may |
---|
909 | * not be the same as the Capture Len. |
---|
910 | */ |
---|
911 | int trace_get_wire_length(const struct libtrace_packet_t *packet){ |
---|
912 | if (packet->trace->format->get_wire_length) { |
---|
913 | return packet->trace->format->get_wire_length(packet); |
---|
914 | } |
---|
915 | return -1; |
---|
916 | |
---|
917 | } |
---|
918 | |
---|
919 | /** Get the type of the link layer |
---|
920 | * @param packet a pointer to a libtrace_packet structure |
---|
921 | * @returns libtrace_linktype_t |
---|
922 | * @author Perry Lorier |
---|
923 | * @author Daniel Lawson |
---|
924 | */ |
---|
925 | libtrace_linktype_t trace_get_link_type(const struct libtrace_packet_t *packet ) { |
---|
926 | if (packet->trace->format->get_link_type) { |
---|
927 | return packet->trace->format->get_link_type(packet); |
---|
928 | } |
---|
929 | return -1; |
---|
930 | } |
---|
931 | |
---|
932 | /** Get the source MAC addres |
---|
933 | * @param packet a pointer to a libtrace_packet structure |
---|
934 | * @returns a pointer to the source mac, (or NULL if there is no source MAC) |
---|
935 | * @author Perry Lorier |
---|
936 | */ |
---|
937 | uint8_t *trace_get_source_mac(const struct libtrace_packet_t *packet) { |
---|
938 | void *link = trace_get_link(packet); |
---|
939 | struct ieee_802_11_header *wifi = link; |
---|
940 | struct ether_header *ethptr = link; |
---|
941 | if (!link) |
---|
942 | return NULL; |
---|
943 | switch (trace_get_link_type(packet)) { |
---|
944 | case TRACE_TYPE_80211: |
---|
945 | return (uint8_t*)&wifi->mac2; |
---|
946 | case TRACE_TYPE_ETH: |
---|
947 | return (uint8_t*)ðptr->ether_shost; |
---|
948 | default: |
---|
949 | fprintf(stderr,"Not implemented\n"); |
---|
950 | assert(0); |
---|
951 | } |
---|
952 | } |
---|
953 | |
---|
954 | /** Get the destination MAC addres |
---|
955 | * @param packet a libtrace_packet pointer |
---|
956 | * @returns a pointer to the destination mac, (or NULL if there is no |
---|
957 | * destination MAC) |
---|
958 | * @author Perry Lorier |
---|
959 | */ |
---|
960 | uint8_t *trace_get_destination_mac(const struct libtrace_packet_t *packet) { |
---|
961 | void *link = trace_get_link(packet); |
---|
962 | struct ieee_802_11_header *wifi = link; |
---|
963 | struct ether_header *ethptr = link; |
---|
964 | if (!link) |
---|
965 | return NULL; |
---|
966 | switch (trace_get_link_type(packet)) { |
---|
967 | case TRACE_TYPE_80211: |
---|
968 | return (uint8_t*)&wifi->mac1; |
---|
969 | case TRACE_TYPE_ETH: |
---|
970 | return (uint8_t*)ðptr->ether_dhost; |
---|
971 | default: |
---|
972 | fprintf(stderr,"Not implemented\n"); |
---|
973 | assert(0); |
---|
974 | } |
---|
975 | } |
---|
976 | |
---|
977 | |
---|
978 | /** process a libtrace event |
---|
979 | * @param trace the libtrace opaque pointer |
---|
980 | * @param packet the libtrace_packet opaque pointer |
---|
981 | * @returns |
---|
982 | * TRACE_EVENT_IOWAIT Waiting on I/O on fd |
---|
983 | * TRACE_EVENT_SLEEP Next event in seconds |
---|
984 | * TRACE_EVENT_PACKET Packet arrived in buffer with size size |
---|
985 | * TRACE_EVENT_TERMINATE Trace terminated (perhaps with an error condition) |
---|
986 | * FIXME currently keeps a copy of the packet inside the trace pointer, |
---|
987 | * which in turn is stored inside the new packet object... |
---|
988 | * @author Perry Lorier |
---|
989 | */ |
---|
990 | struct libtrace_eventobj_t trace_event(struct libtrace_t *trace, |
---|
991 | struct libtrace_packet_t *packet) { |
---|
992 | struct libtrace_eventobj_t event; |
---|
993 | |
---|
994 | if (!trace) { |
---|
995 | fprintf(stderr,"You called trace_event() with a NULL trace object!\n"); |
---|
996 | } |
---|
997 | assert(trace); |
---|
998 | assert(packet); |
---|
999 | |
---|
1000 | /* Store the trace we are reading from into the packet opaque |
---|
1001 | * structure */ |
---|
1002 | packet->trace = trace; |
---|
1003 | |
---|
1004 | /* Is there a packet ready? */ |
---|
1005 | switch (trace->sourcetype) { |
---|
1006 | #if HAVE_PCAP |
---|
1007 | case INTERFACE: |
---|
1008 | { |
---|
1009 | int data; |
---|
1010 | // XXX FIXME |
---|
1011 | //event.fd = pcap_fileno(trace->input.pcap); |
---|
1012 | if(ioctl(event.fd,FIONREAD,&data)==-1){ |
---|
1013 | perror("ioctl(FIONREAD)"); |
---|
1014 | } |
---|
1015 | if (data>0) { |
---|
1016 | event.size = trace_read_packet(trace,packet); |
---|
1017 | event.type = TRACE_EVENT_PACKET; |
---|
1018 | return event; |
---|
1019 | } |
---|
1020 | event.type = TRACE_EVENT_IOWAIT; |
---|
1021 | return event; |
---|
1022 | } |
---|
1023 | #endif |
---|
1024 | case SOCKET: |
---|
1025 | case DEVICE: |
---|
1026 | case RT: |
---|
1027 | { |
---|
1028 | int data; |
---|
1029 | // XXX FIXME |
---|
1030 | //event.fd = trace->input.fd; |
---|
1031 | if(ioctl(event.fd,FIONREAD,&data)==-1){ |
---|
1032 | perror("ioctl(FIONREAD)"); |
---|
1033 | } |
---|
1034 | if (data>0) { |
---|
1035 | event.size = trace_read_packet(trace,packet); |
---|
1036 | event.type = TRACE_EVENT_PACKET; |
---|
1037 | return event; |
---|
1038 | } |
---|
1039 | event.type = TRACE_EVENT_IOWAIT; |
---|
1040 | return event; |
---|
1041 | } |
---|
1042 | case STDIN: |
---|
1043 | case TRACE: |
---|
1044 | { |
---|
1045 | double ts; |
---|
1046 | double now; |
---|
1047 | struct timeval stv; |
---|
1048 | /* "Prime" the pump */ |
---|
1049 | if (!trace->event.packet.buffer) { |
---|
1050 | trace->event.packet.buffer = malloc(4096); |
---|
1051 | trace->event.packet.size= |
---|
1052 | trace_read_packet(trace,packet); |
---|
1053 | event.size = trace->event.packet.size; |
---|
1054 | if (trace->event.packet.size > 0 ) { |
---|
1055 | memcpy(trace->event.packet.buffer,packet->buffer,trace->event.packet.size); |
---|
1056 | } else { |
---|
1057 | // return here, the test for event.size will sort out the error |
---|
1058 | event.type = TRACE_EVENT_PACKET; |
---|
1059 | return event; |
---|
1060 | } |
---|
1061 | } |
---|
1062 | |
---|
1063 | ts=trace_get_seconds(packet); |
---|
1064 | if (trace->event.tdelta!=0) { |
---|
1065 | // Get the adjusted current time |
---|
1066 | gettimeofday(&stv, NULL); |
---|
1067 | now = stv.tv_sec + ((double)stv.tv_usec / 1000000.0); |
---|
1068 | now -= trace->event.tdelta; // adjust for trace delta |
---|
1069 | |
---|
1070 | |
---|
1071 | // if the trace timestamp is still in the future, |
---|
1072 | // return a SLEEP event, otherwise fire the packet |
---|
1073 | if (ts > now) { |
---|
1074 | event.seconds = ts - trace->event.trace_last_ts; |
---|
1075 | event.type = TRACE_EVENT_SLEEP; |
---|
1076 | return event; |
---|
1077 | } |
---|
1078 | } else { |
---|
1079 | gettimeofday(&stv, NULL); |
---|
1080 | // work out the difference between the start of trace replay, |
---|
1081 | // and the first packet in the trace |
---|
1082 | trace->event.tdelta = stv.tv_sec + ((double)stv.tv_usec / 1000000.0); |
---|
1083 | trace->event.tdelta -= ts; |
---|
1084 | |
---|
1085 | } |
---|
1086 | |
---|
1087 | // This is the first packet, so just fire away. |
---|
1088 | packet->size = trace->event.packet.size; |
---|
1089 | memcpy(packet->buffer,trace->event.packet.buffer,trace->event.packet.size); |
---|
1090 | |
---|
1091 | free(trace->event.packet.buffer); |
---|
1092 | trace->event.packet.buffer = 0; |
---|
1093 | event.type = TRACE_EVENT_PACKET; |
---|
1094 | |
---|
1095 | trace->event.trace_last_ts = ts; |
---|
1096 | |
---|
1097 | return event; |
---|
1098 | } |
---|
1099 | default: |
---|
1100 | assert(0); |
---|
1101 | } |
---|
1102 | assert(0); |
---|
1103 | } |
---|
1104 | |
---|
1105 | /** setup a BPF filter |
---|
1106 | * @param filterstring a char * containing the bpf filter string |
---|
1107 | * @returns opaque pointer pointer to a libtrace_filter_t object |
---|
1108 | * @author Daniel Lawson |
---|
1109 | */ |
---|
1110 | struct libtrace_filter_t *trace_bpf_setfilter(const char *filterstring) { |
---|
1111 | #if HAVE_BPF |
---|
1112 | struct libtrace_filter_t *filter = malloc(sizeof(struct libtrace_filter_t)); |
---|
1113 | filter->filterstring = strdup(filterstring); |
---|
1114 | filter->filter = 0; |
---|
1115 | return filter; |
---|
1116 | #else |
---|
1117 | fprintf(stderr,"This version of libtrace does not have bpf filter support\n"); |
---|
1118 | return 0; |
---|
1119 | #endif |
---|
1120 | } |
---|
1121 | |
---|
1122 | /** apply a BPF filter |
---|
1123 | * @param filter the filter opaque pointer |
---|
1124 | * @param packet the packet opaque pointer |
---|
1125 | * @returns 0 if the filter fails, 1 if it succeeds |
---|
1126 | * @author Daniel Lawson |
---|
1127 | */ |
---|
1128 | int trace_bpf_filter(struct libtrace_filter_t *filter, |
---|
1129 | const struct libtrace_packet_t *packet) { |
---|
1130 | #if HAVE_BPF |
---|
1131 | void *linkptr = 0; |
---|
1132 | int clen = 0; |
---|
1133 | assert(filter); |
---|
1134 | assert(packet); |
---|
1135 | linkptr = trace_get_link(packet); |
---|
1136 | if (!linkptr) { |
---|
1137 | return 0; |
---|
1138 | } |
---|
1139 | |
---|
1140 | clen = trace_get_capture_length(packet); |
---|
1141 | |
---|
1142 | |
---|
1143 | if (filter->filterstring && ! filter->filter) { |
---|
1144 | pcap_t *pcap; |
---|
1145 | struct bpf_program bpfprog; |
---|
1146 | |
---|
1147 | switch (trace_get_link_type(packet)) { |
---|
1148 | case TRACE_TYPE_ETH: |
---|
1149 | pcap = (pcap_t *)pcap_open_dead(DLT_EN10MB, 1500); |
---|
1150 | break; |
---|
1151 | #ifdef DLT_LINUX_SLL |
---|
1152 | case TRACE_TYPE_LINUX_SLL: |
---|
1153 | pcap = (pcap_t *)pcap_open_dead(DLT_LINUX_SLL, 1500); |
---|
1154 | break; |
---|
1155 | #endif |
---|
1156 | #ifdef DLT_PFLOG |
---|
1157 | case TRACE_TYPE_PFLOG: |
---|
1158 | pcap = (pcap_t *)pcap_open_dead(DLT_PFLOG, 1500); |
---|
1159 | break; |
---|
1160 | #endif |
---|
1161 | default: |
---|
1162 | printf("only works for ETH and LINUX_SLL (ppp) at the moment\n"); |
---|
1163 | assert(0); |
---|
1164 | } |
---|
1165 | |
---|
1166 | // build filter |
---|
1167 | if (pcap_compile( pcap, &bpfprog, filter->filterstring, 1, 0)) { |
---|
1168 | printf("bpf compilation error: %s: %s\n", |
---|
1169 | pcap_geterr(pcap),filter->filterstring); |
---|
1170 | assert(0); |
---|
1171 | } |
---|
1172 | pcap_close(pcap); |
---|
1173 | filter->filter = bpfprog.bf_insns; |
---|
1174 | } |
---|
1175 | |
---|
1176 | assert(filter->filter); |
---|
1177 | return bpf_filter(filter->filter, linkptr, clen, clen); |
---|
1178 | #else |
---|
1179 | fprintf(stderr,"This version of libtrace does not have bpf filter support\n"); |
---|
1180 | return 0; |
---|
1181 | #endif |
---|
1182 | } |
---|
1183 | |
---|
1184 | /** Set the direction flag, if it has one |
---|
1185 | * @param packet the packet opaque pointer |
---|
1186 | * @param direction the new direction (0,1,2,3) |
---|
1187 | * @returns a signed value containing the direction flag, or -1 if this is not supported |
---|
1188 | * @author Daniel Lawson |
---|
1189 | */ |
---|
1190 | int8_t trace_set_direction(struct libtrace_packet_t *packet, int8_t direction) { |
---|
1191 | assert(packet); |
---|
1192 | if (packet->trace->format->set_direction) { |
---|
1193 | return packet->trace->format->set_direction(packet,direction); |
---|
1194 | } |
---|
1195 | return -1; |
---|
1196 | } |
---|
1197 | |
---|
1198 | /** Get the direction flag, if it has one |
---|
1199 | * @param packet a pointer to a libtrace_packet structure |
---|
1200 | * @returns a signed value containing the direction flag, or -1 if this is not supported |
---|
1201 | * The direction is defined as 0 for packets originating locally (ie, outbound) |
---|
1202 | * and 1 for packets originating remotely (ie, inbound). |
---|
1203 | * Other values are possible, which might be overloaded to mean special things |
---|
1204 | * for a special trace. |
---|
1205 | * @author Daniel Lawson |
---|
1206 | */ |
---|
1207 | int8_t trace_get_direction(const struct libtrace_packet_t *packet) { |
---|
1208 | assert(packet); |
---|
1209 | if (packet->trace->format->get_direction) { |
---|
1210 | return packet->trace->format->get_direction(packet); |
---|
1211 | } |
---|
1212 | return -1; |
---|
1213 | } |
---|
1214 | |
---|
1215 | struct ports_t { |
---|
1216 | uint16_t src; |
---|
1217 | uint16_t dst; |
---|
1218 | }; |
---|
1219 | |
---|
1220 | /* Return the client port |
---|
1221 | */ |
---|
1222 | uint16_t trace_get_source_port(const struct libtrace_packet_t *packet) |
---|
1223 | { |
---|
1224 | struct libtrace_ip *ip = trace_get_ip(packet); |
---|
1225 | struct ports_t *port; |
---|
1226 | if (6 != ip->ip_p |
---|
1227 | && 17 != ip->ip_p) |
---|
1228 | return 0; |
---|
1229 | if (0 != (ip->ip_off & SW_IP_OFFMASK)) |
---|
1230 | return 0; |
---|
1231 | |
---|
1232 | port = (struct ports_t *)((ptrdiff_t)ip + (ip->ip_hl * 4)); |
---|
1233 | |
---|
1234 | return htons(port->src); |
---|
1235 | } |
---|
1236 | |
---|
1237 | /* Same as get_source_port except use the destination port */ |
---|
1238 | uint16_t trace_get_destination_port(const struct libtrace_packet_t *packet) |
---|
1239 | { |
---|
1240 | struct libtrace_ip *ip = trace_get_ip(packet); |
---|
1241 | struct ports_t *port; |
---|
1242 | |
---|
1243 | if (6 != ip->ip_p |
---|
1244 | && 17 != ip->ip_p) |
---|
1245 | return 0; |
---|
1246 | |
---|
1247 | if (0 != (ip->ip_off & SW_IP_OFFMASK)) |
---|
1248 | return 0; |
---|
1249 | |
---|
1250 | port = (struct ports_t *)((ptrdiff_t)ip + (ip->ip_hl * 4)); |
---|
1251 | |
---|
1252 | return htons(port->dst); |
---|
1253 | } |
---|
1254 | |
---|
1255 | #define ROOT_SERVER(x) ((x) < 512) |
---|
1256 | #define ROOT_CLIENT(x) ((512 <= (x)) && ((x) < 1024)) |
---|
1257 | #define NONROOT_SERVER(x) ((x) >= 5000) |
---|
1258 | #define NONROOT_CLIENT(x) ((1024 <= (x)) && ((x) < 5000)) |
---|
1259 | #define DYNAMIC(x) ((49152 < (x)) && ((x) < 65535)) |
---|
1260 | #define SERVER(x) ROOT_SERVER(x) || NONROOT_SERVER(x) |
---|
1261 | #define CLIENT(x) ROOT_CLIENT(x) || NONROOT_CLIENT(x) |
---|
1262 | |
---|
1263 | /* Attempt to deduce the 'server' port |
---|
1264 | * @param protocol the IP protocol (eg, 6 or 17 for TCP or UDP) |
---|
1265 | * @param source the TCP or UDP source port |
---|
1266 | * @param dest the TCP or UDP destination port |
---|
1267 | * @returns a hint as to which port is the server port |
---|
1268 | * @author Daniel Lawson |
---|
1269 | */ |
---|
1270 | int8_t trace_get_server_port(uint8_t protocol, uint16_t source, uint16_t dest) { |
---|
1271 | /* |
---|
1272 | * * If the ports are equal, return DEST |
---|
1273 | * * Check for well-known ports in the given protocol |
---|
1274 | * * Root server ports: 0 - 511 |
---|
1275 | * * Root client ports: 512 - 1023 |
---|
1276 | * * non-root client ports: 1024 - 4999 |
---|
1277 | * * non-root server ports: 5000+ |
---|
1278 | * * Check for static ranges: 1024 - 49151 |
---|
1279 | * * Check for dynamic ranges: 49152 - 65535 |
---|
1280 | * * flip a coin. |
---|
1281 | */ |
---|
1282 | |
---|
1283 | uint16_t server, client; |
---|
1284 | |
---|
1285 | /* equal */ |
---|
1286 | if (source == client) |
---|
1287 | return USE_DEST; |
---|
1288 | |
---|
1289 | /* root server port, 0 - 511 */ |
---|
1290 | if (ROOT_SERVER(source) && ROOT_SERVER(dest)) { |
---|
1291 | if (source < dest) |
---|
1292 | return USE_SOURCE; |
---|
1293 | return USE_DEST; |
---|
1294 | } |
---|
1295 | |
---|
1296 | if (ROOT_SERVER(source) && !ROOT_SERVER(dest)) |
---|
1297 | return USE_SOURCE; |
---|
1298 | if (!ROOT_SERVER(source) && ROOT_SERVER(dest)) |
---|
1299 | return USE_DEST; |
---|
1300 | |
---|
1301 | /* non-root server */ |
---|
1302 | if (NONROOT_SERVER(source) && NONROOT_SERVER(dest)) { |
---|
1303 | if (source < dest) |
---|
1304 | return USE_SOURCE; |
---|
1305 | return USE_DEST; |
---|
1306 | } |
---|
1307 | if (NONROOT_SERVER(source) && !NONROOT_SERVER(dest)) |
---|
1308 | return USE_SOURCE; |
---|
1309 | if (!NONROOT_SERVER(source) && NONROOT_SERVER(dest)) |
---|
1310 | return USE_DEST; |
---|
1311 | |
---|
1312 | /* root client */ |
---|
1313 | if (ROOT_CLIENT(source) && ROOT_CLIENT(dest)) { |
---|
1314 | if (source < dest) |
---|
1315 | return USE_SOURCE; |
---|
1316 | return USE_DEST; |
---|
1317 | } |
---|
1318 | if (ROOT_CLIENT(source) && !ROOT_CLIENT(dest)) { |
---|
1319 | /* prefer root-client over nonroot-client */ |
---|
1320 | if (NONROOT_CLIENT(dest)) |
---|
1321 | return USE_SOURCE; |
---|
1322 | return USE_DEST; |
---|
1323 | } |
---|
1324 | if (!ROOT_CLIENT(source) && ROOT_CLIENT(dest)) { |
---|
1325 | /* prefer root-client over nonroot-client */ |
---|
1326 | if (NONROOT_CLIENT(source)) |
---|
1327 | return USE_DEST; |
---|
1328 | return USE_SOURCE; |
---|
1329 | } |
---|
1330 | |
---|
1331 | /* nonroot client */ |
---|
1332 | if (NONROOT_CLIENT(source) && NONROOT_CLIENT(dest)) { |
---|
1333 | if (source < dest) |
---|
1334 | return USE_SOURCE; |
---|
1335 | return USE_DEST; |
---|
1336 | } |
---|
1337 | if (NONROOT_CLIENT(source) && !NONROOT_CLIENT(dest)) |
---|
1338 | return USE_DEST; |
---|
1339 | if (!NONROOT_CLIENT(source) && NONROOT_CLIENT(dest)) |
---|
1340 | return USE_SOURCE; |
---|
1341 | |
---|
1342 | /* dynamic range */ |
---|
1343 | if (DYNAMIC(source) && DYNAMIC(dest)) |
---|
1344 | if (source < dest) |
---|
1345 | return USE_SOURCE; |
---|
1346 | return USE_DEST; |
---|
1347 | if (DYNAMIC(source) && !DYNAMIC(dest)) |
---|
1348 | return USE_DEST; |
---|
1349 | if (!DYNAMIC(source) && DYNAMIC(dest)) |
---|
1350 | return USE_SOURCE; |
---|
1351 | /* |
---|
1352 | if (SERVER(source) && CLIENT(dest)) |
---|
1353 | return USE_SOURCE; |
---|
1354 | |
---|
1355 | if (SERVER(dest) && CLIENT(source)) |
---|
1356 | return USE_DEST; |
---|
1357 | if (ROOT_SERVER(source) && !ROOT_SERVER(dest)) |
---|
1358 | return USE_SOURCE; |
---|
1359 | if (ROOT_SERVER(dest) && !ROOT_SERVER(source)) |
---|
1360 | return USE_DEST; |
---|
1361 | */ |
---|
1362 | // failing that test... |
---|
1363 | if (source < dest) { |
---|
1364 | return USE_SOURCE; |
---|
1365 | } |
---|
1366 | return USE_DEST; |
---|
1367 | |
---|
1368 | } |
---|
1369 | |
---|
1370 | /** Truncate the packet at the suggested length |
---|
1371 | * @param packet the packet opaque pointer |
---|
1372 | * @param size the new length of the packet |
---|
1373 | * @returns the new length of the packet, or the original length of the |
---|
1374 | * packet if unchanged |
---|
1375 | * NOTE: len refers to the network-level payload of the packet, and not |
---|
1376 | * any capture headers included as well. For example, to truncate a packet |
---|
1377 | * after the IP header, set scan to sizeof(ethernet_header) + sizeof(ip_header) |
---|
1378 | * @author Daniel Lawson |
---|
1379 | */ |
---|
1380 | size_t trace_set_capture_length(struct libtrace_packet_t *packet, size_t size) { |
---|
1381 | assert(packet); |
---|
1382 | |
---|
1383 | if (size > packet->size) { |
---|
1384 | // can't make a packet larger |
---|
1385 | return packet->size; |
---|
1386 | } |
---|
1387 | if (packet->trace->format->truncate_packet) { |
---|
1388 | return packet->trace->format->truncate_packet(packet,size); |
---|
1389 | } |
---|
1390 | return -1; |
---|
1391 | } |
---|
1392 | |
---|