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