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