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