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