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