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 | #include <stdarg.h> |
---|
54 | |
---|
55 | #ifdef HAVE_LIMITS_H |
---|
56 | # include <limits.h> |
---|
57 | #endif |
---|
58 | |
---|
59 | #ifdef HAVE_SYS_LIMITS_H |
---|
60 | # include <sys/limits.h> |
---|
61 | #endif |
---|
62 | |
---|
63 | #include <sys/socket.h> |
---|
64 | #include <sys/un.h> |
---|
65 | #include <sys/mman.h> |
---|
66 | #include <unistd.h> |
---|
67 | |
---|
68 | #ifdef HAVE_NET_IF_ARP_H |
---|
69 | # include <net/if_arp.h> |
---|
70 | #endif |
---|
71 | |
---|
72 | #ifdef HAVE_NET_IF_H |
---|
73 | # include <net/if.h> |
---|
74 | #endif |
---|
75 | |
---|
76 | #ifdef HAVE_NETINET_IN_H |
---|
77 | # include <netinet/in.h> |
---|
78 | #endif |
---|
79 | |
---|
80 | #ifdef HAVE_NET_ETHERNET_H |
---|
81 | # include <net/ethernet.h> |
---|
82 | #endif |
---|
83 | |
---|
84 | #ifdef HAVE_NETINET_IF_ETHER_H |
---|
85 | # include <netinet/if_ether.h> |
---|
86 | #endif |
---|
87 | |
---|
88 | #include <time.h> |
---|
89 | #include <sys/ioctl.h> |
---|
90 | |
---|
91 | #ifdef HAVE_INTTYPES_H |
---|
92 | # include <inttypes.h> |
---|
93 | #else |
---|
94 | # error "Can't find inttypes.h - this needs to be fixed" |
---|
95 | #endif |
---|
96 | |
---|
97 | #ifdef HAVE_STDDEF_H |
---|
98 | # include <stddef.h> |
---|
99 | #else |
---|
100 | # error "Can't find stddef.h - do you define ptrdiff_t elsewhere?" |
---|
101 | #endif |
---|
102 | |
---|
103 | #include "libtrace.h" |
---|
104 | #include "fifo.h" |
---|
105 | #include "libtrace_int.h" |
---|
106 | #include "parse_cmd.h" |
---|
107 | |
---|
108 | #if HAVE_PCAP_BPF_H |
---|
109 | # include <pcap-bpf.h> |
---|
110 | #else |
---|
111 | # ifdef HAVE_NET_BPF_H |
---|
112 | # include <net/bpf.h> |
---|
113 | # endif |
---|
114 | #endif |
---|
115 | |
---|
116 | #include "libtrace_int.h" |
---|
117 | #include "format_helper.h" |
---|
118 | #include "rt_protocol.h" |
---|
119 | #include <err.h> |
---|
120 | |
---|
121 | #define MAXOPTS 1024 |
---|
122 | |
---|
123 | |
---|
124 | struct trace_err_t trace_err; |
---|
125 | |
---|
126 | struct libtrace_format_t **format_list = 0; |
---|
127 | int format_size = 0; |
---|
128 | int nformats = 0; |
---|
129 | |
---|
130 | /* strncpy is not assured to copy the final \0, so we |
---|
131 | * will use our own one that does |
---|
132 | */ |
---|
133 | static void xstrncpy(char *dest, const char *src, size_t n) |
---|
134 | { |
---|
135 | strncpy(dest,src,n); |
---|
136 | dest[n]='\0'; |
---|
137 | } |
---|
138 | |
---|
139 | static char *xstrndup(const char *src,size_t n) |
---|
140 | { |
---|
141 | char *ret=malloc(n+1); |
---|
142 | xstrncpy(ret,src,n); |
---|
143 | return ret; |
---|
144 | } |
---|
145 | |
---|
146 | void register_format(struct libtrace_format_t *f) { |
---|
147 | if (format_list == 0) { |
---|
148 | format_size = 10; |
---|
149 | format_list = malloc( |
---|
150 | sizeof(struct libtrace_format_t *) * |
---|
151 | format_size |
---|
152 | ); |
---|
153 | } else if (format_size == nformats) { |
---|
154 | format_size = format_size + 10; |
---|
155 | format_list = realloc(format_list, |
---|
156 | sizeof(struct libtrace_format_t *) * |
---|
157 | format_size); |
---|
158 | } |
---|
159 | format_list[nformats] = f; |
---|
160 | /* Now, verify things */ |
---|
161 | #if 0 |
---|
162 | if (format_list[nformats]->init_input) { |
---|
163 | #define REQUIRE(x) \ |
---|
164 | if (!format_list[nformats]->x) \ |
---|
165 | fprintf(stderr,"%s: Input format should provide " #x "\n",format_list[nformats]->name) |
---|
166 | REQUIRE(read_packet); |
---|
167 | REQUIRE(start_input); |
---|
168 | REQUIRE(config_input); |
---|
169 | REQUIRE(pause_input); |
---|
170 | REQUIRE(fin_input); |
---|
171 | REQUIRE(get_link_type); |
---|
172 | REQUIRE(get_capture_length); |
---|
173 | REQUIRE(get_wire_length); |
---|
174 | REQUIRE(get_framing_length); |
---|
175 | REQUIRE(trace_event); |
---|
176 | if (!format_list[nformats]->get_erf_timestamp |
---|
177 | && !format_list[nformats]->get_seconds |
---|
178 | && !format_list[nformats]->get_timeval) { |
---|
179 | fprintf(stderr,"%s: A trace format capable of input, should provide at least one of\n" |
---|
180 | "get_erf_timestamp, get_seconds or trace_timeval\n",format_list[nformats]->name); |
---|
181 | } |
---|
182 | if (format_list[nformats]->trace_event==trace_event_device) { |
---|
183 | REQUIRE(get_fd); |
---|
184 | } |
---|
185 | else { |
---|
186 | if (format_list[nformats]->get_fd) { |
---|
187 | fprintf(stderr,"%s: Unnecessary get_fd\n", |
---|
188 | format_list[nformats]->name); |
---|
189 | } |
---|
190 | } |
---|
191 | #undef REQUIRE |
---|
192 | } |
---|
193 | else { |
---|
194 | #define REQUIRE(x) \ |
---|
195 | if (format_list[nformats]->x) \ |
---|
196 | fprintf(stderr,"%s: Non Input format shouldn't need " #x "\n",format_list[nformats]->name) |
---|
197 | REQUIRE(read_packet); |
---|
198 | REQUIRE(start_input); |
---|
199 | REQUIRE(pause_input); |
---|
200 | REQUIRE(fin_input); |
---|
201 | REQUIRE(get_link_type); |
---|
202 | REQUIRE(get_capture_length); |
---|
203 | REQUIRE(get_wire_length); |
---|
204 | REQUIRE(get_framing_length); |
---|
205 | REQUIRE(trace_event); |
---|
206 | REQUIRE(get_seconds); |
---|
207 | REQUIRE(get_timeval); |
---|
208 | REQUIRE(get_erf_timestamp); |
---|
209 | #undef REQUIRE |
---|
210 | } |
---|
211 | if (format_list[nformats]->init_output) { |
---|
212 | #define REQUIRE(x) \ |
---|
213 | if (!format_list[nformats]->x) \ |
---|
214 | fprintf(stderr,"%s: Output format should provide " #x "\n",format_list[nformats]->name) |
---|
215 | REQUIRE(write_packet); |
---|
216 | REQUIRE(start_output); |
---|
217 | REQUIRE(config_output); |
---|
218 | REQUIRE(fin_output); |
---|
219 | #undef REQUIRE |
---|
220 | } |
---|
221 | else { |
---|
222 | #define REQUIRE(x) \ |
---|
223 | if (format_list[nformats]->x) \ |
---|
224 | fprintf(stderr,"%s: Non Output format shouldn't need " #x "\n",format_list[nformats]->name) |
---|
225 | REQUIRE(write_packet); |
---|
226 | REQUIRE(start_output); |
---|
227 | REQUIRE(config_output); |
---|
228 | REQUIRE(fin_output); |
---|
229 | #undef REQUIRE |
---|
230 | } |
---|
231 | #endif |
---|
232 | nformats++; |
---|
233 | } |
---|
234 | |
---|
235 | /* Prints help information for libtrace |
---|
236 | * |
---|
237 | * Function prints out some basic help information regarding libtrace, |
---|
238 | * and then prints out the help() function registered with each input module |
---|
239 | */ |
---|
240 | void trace_help() { |
---|
241 | int i = 0; |
---|
242 | printf("libtrace %s\n",PACKAGE_VERSION); |
---|
243 | for (i = 0; i < nformats; i++) { |
---|
244 | if (format_list[i]->help) { |
---|
245 | format_list[i]->help(); |
---|
246 | } |
---|
247 | } |
---|
248 | } |
---|
249 | |
---|
250 | #define RP_BUFSIZE 65536 |
---|
251 | #define URI_PROTO_LINE 16 |
---|
252 | |
---|
253 | /* Gets the name of the output format for a given output trace. |
---|
254 | * |
---|
255 | * @params libtrace the output trace to get the name of the format for |
---|
256 | * @returns callee-owned null-terminated char* containing the output format |
---|
257 | * |
---|
258 | */ |
---|
259 | SIMPLE_FUNCTION |
---|
260 | char *trace_get_output_format(const struct libtrace_out_t *libtrace) { |
---|
261 | char * format = libtrace->format->name; |
---|
262 | |
---|
263 | return format; |
---|
264 | } |
---|
265 | |
---|
266 | /* Create a trace file from a URI |
---|
267 | * |
---|
268 | * @params char * containing a valid libtrace URI |
---|
269 | * @returns opaque pointer to a libtrace_t |
---|
270 | * |
---|
271 | * Valid URI's are: |
---|
272 | * erf:/path/to/erf/file |
---|
273 | * erf:/path/to/erf/file.gz |
---|
274 | * erf:/path/to/rtclient/socket |
---|
275 | * erf:- (stdin) |
---|
276 | * dag:/dev/dagcard |
---|
277 | * pcapint:pcapinterface (eg: pcapint:eth0) |
---|
278 | * pcap:/path/to/pcap/file |
---|
279 | * pcap:- |
---|
280 | * rtclient:hostname |
---|
281 | * rtclient:hostname:port |
---|
282 | * wag:- |
---|
283 | * wag:/path/to/wag/file |
---|
284 | * wag:/path/to/wag/file.gz |
---|
285 | * wag:/path/to/wag/socket |
---|
286 | * |
---|
287 | * If an error occured when attempting to open a trace, NULL is returned |
---|
288 | * and an error is output to stdout. |
---|
289 | */ |
---|
290 | struct libtrace_t *trace_create(const char *uri) { |
---|
291 | struct libtrace_t *libtrace = malloc(sizeof(struct libtrace_t)); |
---|
292 | char *scan = 0; |
---|
293 | const char *uridata = 0; |
---|
294 | int i = 0; |
---|
295 | |
---|
296 | trace_err.err_num = TRACE_ERR_NOERROR; |
---|
297 | libtrace->format=NULL; |
---|
298 | |
---|
299 | /* parse the URI to determine what sort of event we are dealing with */ |
---|
300 | if ((uridata = trace_parse_uri(uri, &scan)) == 0) { |
---|
301 | trace_set_err(libtrace,TRACE_ERR_BAD_FORMAT,"Bad uri format (%s)",uri); |
---|
302 | return libtrace; |
---|
303 | } |
---|
304 | |
---|
305 | libtrace->event.tdelta = 0.0; |
---|
306 | libtrace->filter = NULL; |
---|
307 | libtrace->snaplen = 0; |
---|
308 | libtrace->started=false; |
---|
309 | |
---|
310 | for (i = 0; i < nformats; i++) { |
---|
311 | if (strlen(scan) == strlen(format_list[i]->name) && |
---|
312 | strncasecmp(scan, |
---|
313 | format_list[i]->name, |
---|
314 | strlen(scan)) == 0) { |
---|
315 | libtrace->format=format_list[i]; |
---|
316 | break; |
---|
317 | } |
---|
318 | } |
---|
319 | if (libtrace->format == 0) { |
---|
320 | trace_set_err(libtrace, TRACE_ERR_BAD_FORMAT, |
---|
321 | "Unknown format (%s)",scan); |
---|
322 | return 0; |
---|
323 | } |
---|
324 | |
---|
325 | libtrace->uridata = strdup(uridata); |
---|
326 | /* libtrace->format now contains the type of uri |
---|
327 | * libtrace->uridata contains the appropriate data for this |
---|
328 | */ |
---|
329 | |
---|
330 | if (libtrace->format->init_input) { |
---|
331 | int err=libtrace->format->init_input(libtrace); |
---|
332 | assert (err==-1 || err==0); |
---|
333 | if (err==-1) { |
---|
334 | /* init_input should call trace_set_err to set |
---|
335 | * the error message |
---|
336 | */ |
---|
337 | return libtrace; |
---|
338 | } |
---|
339 | } else { |
---|
340 | trace_set_err(libtrace,TRACE_ERR_NO_INIT, |
---|
341 | "Format does not support input (%s)",scan); |
---|
342 | return libtrace; |
---|
343 | } |
---|
344 | |
---|
345 | |
---|
346 | libtrace->fifo = create_tracefifo(1048576); |
---|
347 | if (!libtrace->fifo) { |
---|
348 | trace_set_err(libtrace,ENOMEM,"Could not allocate memory for fifo"); |
---|
349 | free(scan); |
---|
350 | return libtrace; |
---|
351 | } |
---|
352 | assert(libtrace->fifo); |
---|
353 | free(scan); |
---|
354 | trace_set_err(libtrace,0,""); |
---|
355 | return libtrace; |
---|
356 | } |
---|
357 | |
---|
358 | /* Creates a "dummy" trace file that has only the format type set. |
---|
359 | * |
---|
360 | * @returns opaque pointer to a (sparsely initialised) libtrace_t |
---|
361 | * |
---|
362 | * IMPORTANT: Do not attempt to call trace_read_packet or other such functions |
---|
363 | * with the dummy trace. Its intended purpose is to act as a packet->trace for |
---|
364 | * libtrace_packet_t's that are not associated with a libtrace_t structure. |
---|
365 | */ |
---|
366 | struct libtrace_t * trace_create_dead (const char *uri) { |
---|
367 | struct libtrace_t *libtrace = malloc(sizeof(struct libtrace_t)); |
---|
368 | char *scan = calloc(sizeof(char),URI_PROTO_LINE); |
---|
369 | char *uridata; |
---|
370 | int i; |
---|
371 | |
---|
372 | trace_err.err_num = TRACE_ERR_NOERROR; |
---|
373 | |
---|
374 | if((uridata = strchr(uri,':')) == NULL) { |
---|
375 | xstrncpy(scan, uri, strlen(uri)); |
---|
376 | } else { |
---|
377 | xstrncpy(scan,uri, (uridata - uri)); |
---|
378 | } |
---|
379 | |
---|
380 | libtrace->format = 0; |
---|
381 | |
---|
382 | for (i = 0; i < nformats; i++) { |
---|
383 | if (strlen(scan) == strlen(format_list[i]->name) && |
---|
384 | !strncasecmp(scan, |
---|
385 | format_list[i]->name, |
---|
386 | strlen(scan))) { |
---|
387 | libtrace->format=format_list[i]; |
---|
388 | break; |
---|
389 | } |
---|
390 | } |
---|
391 | if (libtrace->format == 0) { |
---|
392 | trace_set_err(libtrace,TRACE_ERR_BAD_FORMAT, |
---|
393 | "Unknown format (%s)",scan); |
---|
394 | return 0; |
---|
395 | } |
---|
396 | |
---|
397 | free(scan); |
---|
398 | return libtrace; |
---|
399 | |
---|
400 | } |
---|
401 | |
---|
402 | /* Creates a trace output file from a URI. |
---|
403 | * |
---|
404 | * @param uri the uri string describing the output format and destination |
---|
405 | * @returns opaque pointer to a libtrace_output_t |
---|
406 | * |
---|
407 | * If an error occured when attempting to open the output trace, NULL is |
---|
408 | * returned and trace_errno is set. |
---|
409 | */ |
---|
410 | |
---|
411 | libtrace_out_t *trace_create_output(const char *uri) { |
---|
412 | struct libtrace_out_t *libtrace = malloc(sizeof(struct libtrace_out_t)); |
---|
413 | |
---|
414 | char *scan = 0; |
---|
415 | const char *uridata = 0; |
---|
416 | int i; |
---|
417 | |
---|
418 | trace_err.err_num = TRACE_ERR_NOERROR; |
---|
419 | /* parse the URI to determine what sort of event we are dealing with */ |
---|
420 | |
---|
421 | if ((uridata = trace_parse_uri(uri, &scan)) == 0) { |
---|
422 | trace_set_err_out(libtrace,TRACE_ERR_BAD_FORMAT,"Bad uri format (%s)",uri); |
---|
423 | return libtrace; |
---|
424 | } |
---|
425 | |
---|
426 | |
---|
427 | libtrace->format = 0; |
---|
428 | for (i = 0; i < nformats; i++) { |
---|
429 | if (strlen(scan) == strlen(format_list[i]->name) && |
---|
430 | !strncasecmp(scan, |
---|
431 | format_list[i]->name, |
---|
432 | strlen(scan))) { |
---|
433 | libtrace->format=format_list[i]; |
---|
434 | break; |
---|
435 | } |
---|
436 | } |
---|
437 | if (libtrace->format == 0) { |
---|
438 | trace_set_err_out(libtrace,TRACE_ERR_BAD_FORMAT, |
---|
439 | "Unknown output format (%s)",scan); |
---|
440 | return 0; |
---|
441 | } |
---|
442 | libtrace->uridata = strdup(uridata); |
---|
443 | |
---|
444 | |
---|
445 | /* libtrace->format now contains the type of uri |
---|
446 | * libtrace->uridata contains the appropriate data for this |
---|
447 | */ |
---|
448 | |
---|
449 | if (libtrace->format->init_output) { |
---|
450 | /* 0 on success, -1 on failure */ |
---|
451 | switch(libtrace->format->init_output(libtrace)) { |
---|
452 | case -1: /* failure */ |
---|
453 | free(libtrace); |
---|
454 | return 0; |
---|
455 | case 0: /* success */ |
---|
456 | break; |
---|
457 | default: |
---|
458 | assert(!"init_output() should return -1 for failure, or 0 for success"); |
---|
459 | } |
---|
460 | } else { |
---|
461 | trace_set_err_out(libtrace,TRACE_ERR_NO_INIT_OUT, |
---|
462 | "Format does not support writing (%s)",scan); |
---|
463 | return libtrace; |
---|
464 | } |
---|
465 | |
---|
466 | |
---|
467 | free(scan); |
---|
468 | libtrace->started=false; |
---|
469 | return libtrace; |
---|
470 | } |
---|
471 | |
---|
472 | /* Start a trace |
---|
473 | * @param libtrace the input trace to start |
---|
474 | * @returns 0 on success |
---|
475 | * |
---|
476 | * This does the work associated with actually starting up |
---|
477 | * the trace. it may fail. |
---|
478 | */ |
---|
479 | int trace_start(struct libtrace_t *libtrace) |
---|
480 | { |
---|
481 | assert(libtrace); |
---|
482 | if (libtrace->format->start_input) { |
---|
483 | int ret=libtrace->format->start_input(libtrace); |
---|
484 | if (ret < 0) { |
---|
485 | return ret; |
---|
486 | } |
---|
487 | } |
---|
488 | |
---|
489 | libtrace->started=true; |
---|
490 | return 0; |
---|
491 | } |
---|
492 | |
---|
493 | int trace_start_output(libtrace_out_t *libtrace) |
---|
494 | { |
---|
495 | assert(libtrace); |
---|
496 | if (libtrace->format->start_output) { |
---|
497 | int ret=libtrace->format->start_output(libtrace); |
---|
498 | if (ret < 0) { |
---|
499 | return ret; |
---|
500 | } |
---|
501 | } |
---|
502 | |
---|
503 | libtrace->started=false; |
---|
504 | return 0; |
---|
505 | } |
---|
506 | |
---|
507 | int trace_pause(libtrace_t *libtrace) |
---|
508 | { |
---|
509 | assert(libtrace); |
---|
510 | assert(libtrace->started && "BUG: Called trace_pause without calling trace_start first"); |
---|
511 | if (libtrace->format->pause_input) |
---|
512 | libtrace->format->pause_input(libtrace); |
---|
513 | libtrace->started=false; |
---|
514 | return 0; |
---|
515 | } |
---|
516 | |
---|
517 | int trace_config(libtrace_t *libtrace, |
---|
518 | trace_option_t option, |
---|
519 | void *value) |
---|
520 | { |
---|
521 | int ret; |
---|
522 | if (libtrace->format->config_input) { |
---|
523 | ret=libtrace->format->config_input(libtrace,option,value); |
---|
524 | if (ret==0) |
---|
525 | return 0; |
---|
526 | } |
---|
527 | switch(option) { |
---|
528 | case TRACE_OPTION_SNAPLEN: |
---|
529 | libtrace->snaplen=*(int*)value; |
---|
530 | break; |
---|
531 | case TRACE_OPTION_FILTER: |
---|
532 | libtrace->filter=value; |
---|
533 | break; |
---|
534 | case TRACE_OPTION_PROMISC: |
---|
535 | trace_set_err(libtrace,TRACE_ERR_OPTION_UNAVAIL, |
---|
536 | "Promisc mode is not supported by this format module"); |
---|
537 | return -1; |
---|
538 | default: |
---|
539 | trace_set_err(libtrace,TRACE_ERR_UNKNOWN_OPTION, |
---|
540 | "Unknown option %i", option); |
---|
541 | return -1; |
---|
542 | } |
---|
543 | return 0; |
---|
544 | } |
---|
545 | |
---|
546 | /* Parses an output options string and calls the appropriate function to deal with output options. |
---|
547 | * |
---|
548 | * @param libtrace the output trace object to apply the options to |
---|
549 | * @param options the options string |
---|
550 | * @returns -1 if option configuration failed, 0 otherwise |
---|
551 | * |
---|
552 | * @author Shane Alcock |
---|
553 | */ |
---|
554 | int trace_config_output(struct libtrace_out_t *libtrace, |
---|
555 | trace_option_output_t option, |
---|
556 | void *value) { |
---|
557 | if (libtrace->format->config_output) { |
---|
558 | return libtrace->format->config_output(libtrace, option, value); |
---|
559 | } |
---|
560 | return -1; |
---|
561 | } |
---|
562 | |
---|
563 | /* Close a trace file, freeing up any resources it may have been using |
---|
564 | * |
---|
565 | */ |
---|
566 | void trace_destroy(struct libtrace_t *libtrace) { |
---|
567 | assert(libtrace); |
---|
568 | if (libtrace->started && libtrace->format->pause_input) |
---|
569 | libtrace->format->pause_input(libtrace); |
---|
570 | libtrace->format->fin_input(libtrace); |
---|
571 | /* need to free things! */ |
---|
572 | free(libtrace->uridata); |
---|
573 | destroy_tracefifo(libtrace->fifo); |
---|
574 | free(libtrace); |
---|
575 | } |
---|
576 | |
---|
577 | |
---|
578 | void trace_destroy_dead(struct libtrace_t *libtrace) { |
---|
579 | assert(libtrace); |
---|
580 | free(libtrace); |
---|
581 | } |
---|
582 | /* Close an output trace file, freeing up any resources it may have been using |
---|
583 | * |
---|
584 | * @param libtrace the output trace file to be destroyed |
---|
585 | * |
---|
586 | * @author Shane Alcock |
---|
587 | * */ |
---|
588 | void trace_destroy_output(struct libtrace_out_t *libtrace) { |
---|
589 | assert(libtrace); |
---|
590 | libtrace->format->fin_output(libtrace); |
---|
591 | free(libtrace->uridata); |
---|
592 | free(libtrace); |
---|
593 | } |
---|
594 | |
---|
595 | libtrace_packet_t *trace_create_packet() { |
---|
596 | libtrace_packet_t *packet = calloc(1,sizeof(libtrace_packet_t)); |
---|
597 | packet->buf_control=TRACE_CTRL_PACKET; |
---|
598 | return packet; |
---|
599 | } |
---|
600 | |
---|
601 | libtrace_packet_t *trace_copy_packet(const libtrace_packet_t *packet) { |
---|
602 | libtrace_packet_t *dest = malloc(sizeof(libtrace_packet_t)); |
---|
603 | dest->trace=packet->trace; |
---|
604 | dest->buffer=malloc( |
---|
605 | trace_get_framing_length(packet) |
---|
606 | +trace_get_capture_length(packet)); |
---|
607 | dest->header=dest->buffer; |
---|
608 | dest->payload=(void*) |
---|
609 | ((char*)dest->buffer+trace_get_framing_length(packet)); |
---|
610 | dest->size=packet->size; |
---|
611 | dest->type=packet->type; |
---|
612 | dest->buf_control=TRACE_CTRL_PACKET; |
---|
613 | memcpy(dest->header,packet->header,trace_get_framing_length(packet)); |
---|
614 | memcpy(dest->payload,packet->payload,trace_get_capture_length(packet)); |
---|
615 | |
---|
616 | return dest; |
---|
617 | } |
---|
618 | |
---|
619 | /** Destroy a packet object |
---|
620 | * |
---|
621 | * sideeffect: sets packet to NULL |
---|
622 | */ |
---|
623 | void trace_destroy_packet(struct libtrace_packet_t **packet) { |
---|
624 | if ((*packet)->buf_control == TRACE_CTRL_PACKET) { |
---|
625 | free((*packet)->buffer); |
---|
626 | } |
---|
627 | free((*packet)); |
---|
628 | packet = NULL; |
---|
629 | } |
---|
630 | |
---|
631 | /* Read one packet from the trace into buffer |
---|
632 | * |
---|
633 | * @param libtrace the libtrace opaque pointer |
---|
634 | * @param packet the packet opaque pointer |
---|
635 | * @returns 0 on EOF, negative value on error |
---|
636 | * |
---|
637 | */ |
---|
638 | int trace_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { |
---|
639 | |
---|
640 | assert(libtrace && "You called trace_read_packet() with a NULL libtrace parameter!\n"); |
---|
641 | assert(libtrace->started && "BUG: You must call libtrace_start() before trace_read_packet()\n"); |
---|
642 | assert(packet); |
---|
643 | assert((packet->buf_control==TRACE_CTRL_PACKET || packet->buf_control==TRACE_CTRL_EXTERNAL)&& |
---|
644 | "BUG: You must allocate a packet using packet_create()"); |
---|
645 | |
---|
646 | /* Store the trace we are reading from into the packet opaque |
---|
647 | * structure */ |
---|
648 | packet->trace = libtrace; |
---|
649 | |
---|
650 | |
---|
651 | if (libtrace->format->read_packet) { |
---|
652 | do { |
---|
653 | packet->size=libtrace->format->read_packet(libtrace,packet); |
---|
654 | if (packet->size==(size_t)-1 || packet->size==0) |
---|
655 | return packet->size; |
---|
656 | if (libtrace->filter) { |
---|
657 | /* If the filter doesn't match, read another |
---|
658 | * packet |
---|
659 | */ |
---|
660 | if (!trace_bpf_filter(libtrace->filter,packet)){ |
---|
661 | continue; |
---|
662 | } |
---|
663 | } |
---|
664 | if (libtrace->snaplen>0) { |
---|
665 | /* Snap the packet */ |
---|
666 | trace_set_capture_length(packet, |
---|
667 | libtrace->snaplen); |
---|
668 | } |
---|
669 | |
---|
670 | return packet->size; |
---|
671 | } while(1); |
---|
672 | } |
---|
673 | packet->size=-1; |
---|
674 | return -1; |
---|
675 | } |
---|
676 | |
---|
677 | /* Writes a packet to the specified output |
---|
678 | * |
---|
679 | * @param libtrace describes the output format, destination, etc. |
---|
680 | * @param packet the packet to be written out |
---|
681 | * @returns the number of bytes written, -1 if write failed |
---|
682 | * |
---|
683 | * @author Shane Alcock |
---|
684 | * */ |
---|
685 | int trace_write_packet(struct libtrace_out_t *libtrace, const struct libtrace_packet_t *packet) { |
---|
686 | assert(libtrace); |
---|
687 | assert(packet); |
---|
688 | /* Verify the packet is valid */ |
---|
689 | assert(packet->size<65536); |
---|
690 | assert(packet->size>0); |
---|
691 | |
---|
692 | if (libtrace->format->write_packet) { |
---|
693 | return libtrace->format->write_packet(libtrace, packet); |
---|
694 | } |
---|
695 | return -1; |
---|
696 | } |
---|
697 | |
---|
698 | void *trace_get_link(const struct libtrace_packet_t *packet) { |
---|
699 | return (void *)packet->payload; |
---|
700 | } |
---|
701 | |
---|
702 | /* |
---|
703 | typedef struct legacy_framing { |
---|
704 | uint64_t ts; |
---|
705 | uint32_t crc; |
---|
706 | uint32_t header; |
---|
707 | uint32_t data[12]; // pad to 64 bytes |
---|
708 | } legacy_framing_t; |
---|
709 | */ |
---|
710 | |
---|
711 | /* get a pointer to the IP header (if any) |
---|
712 | * @param packet a pointer to a libtrace_packet structure |
---|
713 | * |
---|
714 | * @returns a pointer to the IP header, or NULL if there is not an IP packet |
---|
715 | */ |
---|
716 | struct libtrace_ip *trace_get_ip(const struct libtrace_packet_t *packet) { |
---|
717 | struct libtrace_ip *ipptr = 0; |
---|
718 | |
---|
719 | switch(trace_get_link_type(packet)) { |
---|
720 | case TRACE_TYPE_80211_PRISM: |
---|
721 | { |
---|
722 | struct ieee_802_11_header *wifi = |
---|
723 | (void*)( |
---|
724 | (char*)trace_get_link(packet)+144); |
---|
725 | if (!wifi) { |
---|
726 | ipptr = NULL; |
---|
727 | break; |
---|
728 | } |
---|
729 | |
---|
730 | /* Data packet? */ |
---|
731 | if (wifi->type != 2) { |
---|
732 | ipptr = NULL; |
---|
733 | } |
---|
734 | else { |
---|
735 | struct ieee_802_11_payload *eth = |
---|
736 | (void*)((char*)wifi+sizeof(struct ieee_802_11_header)); |
---|
737 | ipptr = NULL; |
---|
738 | |
---|
739 | if (ntohs(eth->type) == 0x0800) { |
---|
740 | ipptr=(void*) |
---|
741 | ((char*)eth + sizeof(*eth)); |
---|
742 | } else if (ntohs(eth->type) == 0x8100) { |
---|
743 | struct libtrace_8021q *vlanhdr = |
---|
744 | (struct libtrace_8021q *)eth; |
---|
745 | if (ntohs(vlanhdr->vlan_ether_type) |
---|
746 | == 0x0800) { |
---|
747 | ipptr=(void*)( |
---|
748 | (char*)eth+sizeof(*vlanhdr)); |
---|
749 | } |
---|
750 | } |
---|
751 | } |
---|
752 | } |
---|
753 | break; |
---|
754 | case TRACE_TYPE_80211: |
---|
755 | { |
---|
756 | |
---|
757 | struct ieee_802_11_header *wifi = trace_get_link(packet); |
---|
758 | if (!wifi) { |
---|
759 | ipptr = NULL; |
---|
760 | break; |
---|
761 | } |
---|
762 | |
---|
763 | /* Data packet? */ |
---|
764 | if (wifi->type != 2) { |
---|
765 | ipptr = NULL; |
---|
766 | } |
---|
767 | else { |
---|
768 | struct ieee_802_11_payload *eth = |
---|
769 | (void*)((char*)wifi+sizeof(struct ieee_802_11_header)); |
---|
770 | ipptr = NULL; |
---|
771 | |
---|
772 | if (ntohs(eth->type) == 0x0800) { |
---|
773 | ipptr=(void*)((char*)eth + sizeof(*eth)); |
---|
774 | } else if (ntohs(eth->type) == 0x8100) { |
---|
775 | struct libtrace_8021q *vlanhdr = |
---|
776 | (struct libtrace_8021q *)eth; |
---|
777 | if (ntohs(vlanhdr->vlan_ether_type) |
---|
778 | == 0x0800) { |
---|
779 | ipptr=(void*)((char*)eth + |
---|
780 | sizeof(*vlanhdr)); |
---|
781 | } |
---|
782 | } |
---|
783 | } |
---|
784 | } |
---|
785 | break; |
---|
786 | case TRACE_TYPE_ETH: |
---|
787 | case TRACE_TYPE_LEGACY_ETH: |
---|
788 | { |
---|
789 | struct libtrace_ether *eth = |
---|
790 | trace_get_link(packet); |
---|
791 | if (!eth) { |
---|
792 | ipptr = NULL; |
---|
793 | break; |
---|
794 | } |
---|
795 | ipptr = NULL; |
---|
796 | |
---|
797 | if (ntohs(eth->ether_type)==0x0800) { |
---|
798 | ipptr = (void*)((char *)eth + sizeof(*eth)); |
---|
799 | } else if (ntohs(eth->ether_type) == 0x8100) { |
---|
800 | struct libtrace_8021q *vlanhdr = |
---|
801 | (struct libtrace_8021q *)eth; |
---|
802 | if (ntohs(vlanhdr->vlan_ether_type) |
---|
803 | == 0x0800) { |
---|
804 | ipptr = (void*)((char *)eth + |
---|
805 | sizeof(*vlanhdr)); |
---|
806 | } |
---|
807 | } |
---|
808 | break; |
---|
809 | } |
---|
810 | case TRACE_TYPE_NONE: |
---|
811 | ipptr = trace_get_link(packet); |
---|
812 | break; |
---|
813 | case TRACE_TYPE_LINUX_SLL: |
---|
814 | { |
---|
815 | struct trace_sll_header_t *sll; |
---|
816 | |
---|
817 | sll = trace_get_link(packet); |
---|
818 | if (!sll) { |
---|
819 | ipptr = NULL; |
---|
820 | break; |
---|
821 | } |
---|
822 | if (ntohs(sll->protocol)!=0x0800) { |
---|
823 | ipptr = NULL; |
---|
824 | } |
---|
825 | else { |
---|
826 | ipptr = (void*)((char*)sll+ |
---|
827 | sizeof(*sll)); |
---|
828 | } |
---|
829 | } |
---|
830 | break; |
---|
831 | case TRACE_TYPE_PFLOG: |
---|
832 | { |
---|
833 | struct trace_pflog_header_t *pflog; |
---|
834 | pflog = trace_get_link(packet); |
---|
835 | if (!pflog) { |
---|
836 | ipptr = NULL; |
---|
837 | break; |
---|
838 | } |
---|
839 | if (pflog->af != AF_INET) { |
---|
840 | ipptr = NULL; |
---|
841 | } else { |
---|
842 | ipptr = (void*)((char*)pflog+ |
---|
843 | sizeof(*pflog)); |
---|
844 | } |
---|
845 | } |
---|
846 | break; |
---|
847 | case TRACE_TYPE_LEGACY_POS: |
---|
848 | { |
---|
849 | /* 64 byte capture. */ |
---|
850 | struct libtrace_pos *pos = |
---|
851 | trace_get_link(packet); |
---|
852 | if (ntohs(pos->ether_type) == 0x0800) { |
---|
853 | ipptr=(void*)((char *)pos+sizeof(*pos)); |
---|
854 | } else { |
---|
855 | ipptr=NULL; |
---|
856 | } |
---|
857 | break; |
---|
858 | |
---|
859 | } |
---|
860 | case TRACE_TYPE_LEGACY_ATM: |
---|
861 | case TRACE_TYPE_ATM: |
---|
862 | { |
---|
863 | /* 64 byte capture. */ |
---|
864 | struct libtrace_llcsnap *llc = |
---|
865 | trace_get_link(packet); |
---|
866 | |
---|
867 | /* advance the llc ptr +4 into the link layer. |
---|
868 | * need to check what is in these 4 bytes. |
---|
869 | * don't have time! |
---|
870 | */ |
---|
871 | llc = (void*)((char *)llc + 4); |
---|
872 | if (ntohs(llc->type) == 0x0800) { |
---|
873 | ipptr=(void*)((char*)llc+sizeof(*llc)); |
---|
874 | } else { |
---|
875 | ipptr = NULL; |
---|
876 | } |
---|
877 | break; |
---|
878 | } |
---|
879 | default: |
---|
880 | fprintf(stderr,"Don't understand link layer type %i in trace_get_ip()\n", |
---|
881 | trace_get_link_type(packet)); |
---|
882 | ipptr=NULL; |
---|
883 | break; |
---|
884 | } |
---|
885 | |
---|
886 | return ipptr; |
---|
887 | } |
---|
888 | |
---|
889 | #define SW_IP_OFFMASK 0xff1f |
---|
890 | |
---|
891 | /* Gets a pointer to the transport layer header (if any) |
---|
892 | * @param packet a pointer to a libtrace_packet structure |
---|
893 | * |
---|
894 | * @returns a pointer to the transport layer header, or NULL if there is no header |
---|
895 | */ |
---|
896 | void *trace_get_transport(const struct libtrace_packet_t *packet) { |
---|
897 | void *trans_ptr = 0; |
---|
898 | struct libtrace_ip *ipptr = 0; |
---|
899 | |
---|
900 | if (!(ipptr = trace_get_ip(packet))) { |
---|
901 | return 0; |
---|
902 | } |
---|
903 | |
---|
904 | if ((ipptr->ip_off & SW_IP_OFFMASK) == 0) { |
---|
905 | trans_ptr = (void *)((ptrdiff_t)ipptr + (ipptr->ip_hl * 4)); |
---|
906 | } |
---|
907 | return trans_ptr; |
---|
908 | } |
---|
909 | |
---|
910 | /* Gets a pointer to the transport layer header (if any) given a pointer to the |
---|
911 | * IP header |
---|
912 | * @param ip The IP Header |
---|
913 | * @param[out] skipped An output variable of the number of bytes skipped |
---|
914 | * |
---|
915 | * @returns a pointer to the transport layer header, or NULL if there is no header |
---|
916 | * |
---|
917 | * Skipped can be NULL, in which case it will be ignored |
---|
918 | */ |
---|
919 | void *trace_get_transport_from_ip(const libtrace_ip_t *ip, int *skipped) { |
---|
920 | void *trans_ptr = 0; |
---|
921 | |
---|
922 | if ((ip->ip_off & SW_IP_OFFMASK) == 0) { |
---|
923 | trans_ptr = (void *)((ptrdiff_t)ip + (ip->ip_hl * 4)); |
---|
924 | } |
---|
925 | |
---|
926 | if (skipped) |
---|
927 | *skipped = (ip->ip_hl*4); |
---|
928 | |
---|
929 | return trans_ptr; |
---|
930 | } |
---|
931 | |
---|
932 | /* get a pointer to the TCP header (if any) |
---|
933 | * @param packet a pointer to a libtrace_packet structure |
---|
934 | * |
---|
935 | * @returns a pointer to the TCP header, or NULL if there is not a TCP packet |
---|
936 | */ |
---|
937 | libtrace_tcp_t *trace_get_tcp(const libtrace_packet_t *packet) { |
---|
938 | struct libtrace_tcp *tcpptr = 0; |
---|
939 | struct libtrace_ip *ipptr = 0; |
---|
940 | |
---|
941 | if(!(ipptr = trace_get_ip(packet))) { |
---|
942 | return 0; |
---|
943 | } |
---|
944 | if (ipptr->ip_p == 6) { |
---|
945 | tcpptr = (struct libtrace_tcp *)trace_get_transport_from_ip(ipptr, 0); |
---|
946 | } |
---|
947 | return tcpptr; |
---|
948 | } |
---|
949 | |
---|
950 | /* get a pointer to the TCP header (if any) given a pointer to the IP header |
---|
951 | * @param ip The IP header |
---|
952 | * @param[out] skipped An output variable of the number of bytes skipped |
---|
953 | * |
---|
954 | * @returns a pointer to the TCP header, or NULL if this is not a TCP packet |
---|
955 | * |
---|
956 | * Skipped can be NULL, in which case it will be ignored by the program. |
---|
957 | */ |
---|
958 | libtrace_tcp_t *trace_get_tcp_from_ip(const libtrace_ip_t *ip, int *skipped) |
---|
959 | { |
---|
960 | struct libtrace_tcp *tcpptr = 0; |
---|
961 | |
---|
962 | if (ip->ip_p == 6) { |
---|
963 | tcpptr = (struct libtrace_tcp *)trace_get_transport_from_ip(ip, skipped); |
---|
964 | } |
---|
965 | |
---|
966 | return tcpptr; |
---|
967 | } |
---|
968 | |
---|
969 | /* get a pointer to the UDP header (if any) |
---|
970 | * @param packet a pointer to a libtrace_packet structure |
---|
971 | * |
---|
972 | * @returns a pointer to the UDP header, or NULL if this is not a UDP packet |
---|
973 | */ |
---|
974 | struct libtrace_udp *trace_get_udp(const struct libtrace_packet_t *packet) { |
---|
975 | struct libtrace_udp *udpptr = 0; |
---|
976 | struct libtrace_ip *ipptr = 0; |
---|
977 | |
---|
978 | if(!(ipptr = trace_get_ip(packet))) { |
---|
979 | return 0; |
---|
980 | } |
---|
981 | if (ipptr->ip_p == 17) { |
---|
982 | udpptr = (struct libtrace_udp *)trace_get_transport_from_ip(ipptr, 0); |
---|
983 | } |
---|
984 | |
---|
985 | return udpptr; |
---|
986 | } |
---|
987 | |
---|
988 | /* get a pointer to the UDP header (if any) given a pointer to the IP header |
---|
989 | * @param ip The IP header |
---|
990 | * @param[out] skipped An output variable of the number of bytes skipped |
---|
991 | * |
---|
992 | * @returns a pointer to the UDP header, or NULL if this is not a UDP packet |
---|
993 | * |
---|
994 | * Skipped can be NULL, in which case it will be ignored by the program. |
---|
995 | */ |
---|
996 | struct libtrace_udp *trace_get_udp_from_ip(const struct libtrace_ip *ip, int *skipped) |
---|
997 | { |
---|
998 | struct libtrace_udp *udpptr = 0; |
---|
999 | |
---|
1000 | if (ip->ip_p == 17) { |
---|
1001 | udpptr = (struct libtrace_udp *)trace_get_transport_from_ip(ip, skipped); |
---|
1002 | } |
---|
1003 | |
---|
1004 | return udpptr; |
---|
1005 | } |
---|
1006 | |
---|
1007 | |
---|
1008 | /* get a pointer to the ICMP header (if any) |
---|
1009 | * @param packet a pointer to a libtrace_packet structure |
---|
1010 | * |
---|
1011 | * @returns a pointer to the ICMP header, or NULL if this is not a ICMP packet |
---|
1012 | */ |
---|
1013 | struct libtrace_icmp *trace_get_icmp(const struct libtrace_packet_t *packet) { |
---|
1014 | struct libtrace_icmp *icmpptr = 0; |
---|
1015 | struct libtrace_ip *ipptr = 0; |
---|
1016 | |
---|
1017 | if(!(ipptr = trace_get_ip(packet))) { |
---|
1018 | return 0; |
---|
1019 | } |
---|
1020 | if (ipptr->ip_p == 1){ |
---|
1021 | icmpptr = (struct libtrace_icmp *)trace_get_transport_from_ip(ipptr, 0); |
---|
1022 | } |
---|
1023 | return icmpptr; |
---|
1024 | } |
---|
1025 | |
---|
1026 | /* get a pointer to the ICMP header (if any) given a pointer to the IP header |
---|
1027 | * @param ip The IP header |
---|
1028 | * @param[out] skipped An output variable of the number of bytes skipped |
---|
1029 | * |
---|
1030 | * @returns a pointer to the ICMP header, or NULL if this is not a ICMP packet |
---|
1031 | * |
---|
1032 | * Skipped can be NULL, in which case it will be ignored by the program. |
---|
1033 | */ |
---|
1034 | struct libtrace_icmp *trace_get_icmp_from_ip(const struct libtrace_ip *ip, int *skipped) |
---|
1035 | { |
---|
1036 | struct libtrace_icmp *icmpptr = 0; |
---|
1037 | |
---|
1038 | if (ip->ip_p == 1) { |
---|
1039 | icmpptr = (struct libtrace_icmp *)trace_get_transport_from_ip(ip, skipped); |
---|
1040 | } |
---|
1041 | |
---|
1042 | return icmpptr; |
---|
1043 | } |
---|
1044 | /* parse an ip or tcp option |
---|
1045 | * @param[in,out] ptr the pointer to the current option |
---|
1046 | * @param[in,out] len the length of the remaining buffer |
---|
1047 | * @param[out] type the type of the option |
---|
1048 | * @param[out] optlen the length of the option |
---|
1049 | * @param[out] data the data of the option |
---|
1050 | * |
---|
1051 | * @returns bool true if there is another option (and the fields are filled in) |
---|
1052 | * or false if this was the last option. |
---|
1053 | * |
---|
1054 | * This updates ptr to point to the next option after this one, and updates |
---|
1055 | * len to be the number of bytes remaining in the options area. Type is updated |
---|
1056 | * to be the code of this option, and data points to the data of this option, |
---|
1057 | * with optlen saying how many bytes there are. |
---|
1058 | * |
---|
1059 | * @note Beware of fragmented packets. |
---|
1060 | * @author Perry Lorier |
---|
1061 | */ |
---|
1062 | int trace_get_next_option(unsigned char **ptr,int *len, |
---|
1063 | unsigned char *type, |
---|
1064 | unsigned char *optlen, |
---|
1065 | unsigned char **data) |
---|
1066 | { |
---|
1067 | if (*len<=0) |
---|
1068 | return 0; |
---|
1069 | *type=**ptr; |
---|
1070 | switch(*type) { |
---|
1071 | case 0: /* End of options */ |
---|
1072 | return 0; |
---|
1073 | case 1: /* Pad */ |
---|
1074 | (*ptr)++; |
---|
1075 | (*len)--; |
---|
1076 | return 1; |
---|
1077 | default: |
---|
1078 | *optlen = *(*ptr+1); |
---|
1079 | if (*optlen<2) |
---|
1080 | return 0; /* I have no idea wtf is going on |
---|
1081 | * with these packets |
---|
1082 | */ |
---|
1083 | (*len)-=*optlen; |
---|
1084 | (*data)=(*ptr+2); |
---|
1085 | (*ptr)+=*optlen; |
---|
1086 | if (*len<0) |
---|
1087 | return 0; |
---|
1088 | return 1; |
---|
1089 | } |
---|
1090 | assert(0); |
---|
1091 | } |
---|
1092 | |
---|
1093 | |
---|
1094 | /* Get the current time in DAG time format |
---|
1095 | * @param packet a pointer to a libtrace_packet structure |
---|
1096 | * @returns a 64 bit timestamp in DAG ERF format (upper 32 bits are the seconds |
---|
1097 | * past 1970-01-01, the lower 32bits are partial seconds) |
---|
1098 | * @author Daniel Lawson |
---|
1099 | */ |
---|
1100 | uint64_t trace_get_erf_timestamp(const libtrace_packet_t *packet) { |
---|
1101 | uint64_t timestamp = 0; |
---|
1102 | double seconds = 0.0; |
---|
1103 | struct timeval ts; |
---|
1104 | |
---|
1105 | assert(packet->size>0 && packet->size<65536); |
---|
1106 | |
---|
1107 | if (packet->trace->format->get_erf_timestamp) { |
---|
1108 | /* timestamp -> timestamp */ |
---|
1109 | timestamp = packet->trace->format->get_erf_timestamp(packet); |
---|
1110 | } else if (packet->trace->format->get_timeval) { |
---|
1111 | /* timeval -> timestamp */ |
---|
1112 | ts = packet->trace->format->get_timeval(packet); |
---|
1113 | timestamp = ((((uint64_t)ts.tv_sec) << 32) + \ |
---|
1114 | (((uint64_t)ts.tv_usec * UINT_MAX)/1000000)); |
---|
1115 | } else if (packet->trace->format->get_seconds) { |
---|
1116 | /* seconds -> timestamp */ |
---|
1117 | seconds = packet->trace->format->get_seconds(packet); |
---|
1118 | timestamp = ((uint64_t)((uint32_t)seconds) << 32) + \ |
---|
1119 | (( seconds - (uint32_t)seconds ) * UINT_MAX); |
---|
1120 | } |
---|
1121 | return timestamp; |
---|
1122 | } |
---|
1123 | |
---|
1124 | /* Get the current time in struct timeval |
---|
1125 | * @param packet a pointer to a libtrace_packet structure |
---|
1126 | * |
---|
1127 | * @returns time that this packet was seen in a struct timeval |
---|
1128 | * @author Daniel Lawson |
---|
1129 | * @author Perry Lorier |
---|
1130 | */ |
---|
1131 | struct timeval trace_get_timeval(const libtrace_packet_t *packet) { |
---|
1132 | struct timeval tv; |
---|
1133 | uint64_t ts = 0; |
---|
1134 | double seconds = 0.0; |
---|
1135 | assert(packet->size>0 && packet->size<65536); |
---|
1136 | if (packet->trace->format->get_timeval) { |
---|
1137 | /* timeval -> timeval */ |
---|
1138 | tv = packet->trace->format->get_timeval(packet); |
---|
1139 | } else if (packet->trace->format->get_erf_timestamp) { |
---|
1140 | /* timestamp -> timeval */ |
---|
1141 | ts = packet->trace->format->get_erf_timestamp(packet); |
---|
1142 | #if __BYTE_ORDER == __BIG_ENDIAN |
---|
1143 | tv.tv_sec = ts & 0xFFFFFFFF; |
---|
1144 | #elif __BYTE_ORDER == __LITTLE_ENDIAN |
---|
1145 | tv.tv_sec = ts >> 32; |
---|
1146 | #else |
---|
1147 | #error "What on earth are you running this on?" |
---|
1148 | #endif |
---|
1149 | tv.tv_usec = ((ts&0xFFFFFFFF)*1000000)>>32; |
---|
1150 | if (tv.tv_usec >= 1000000) { |
---|
1151 | tv.tv_usec -= 1000000; |
---|
1152 | tv.tv_sec += 1; |
---|
1153 | } |
---|
1154 | } else if (packet->trace->format->get_seconds) { |
---|
1155 | /* seconds -> timeval */ |
---|
1156 | seconds = packet->trace->format->get_seconds(packet); |
---|
1157 | tv.tv_sec = (uint32_t)seconds; |
---|
1158 | tv.tv_usec = (uint32_t)(((seconds - tv.tv_sec) * 1000000)/UINT_MAX); |
---|
1159 | } |
---|
1160 | |
---|
1161 | return tv; |
---|
1162 | } |
---|
1163 | |
---|
1164 | /* Get the current time in floating point seconds |
---|
1165 | * @param packet a pointer to a libtrace_packet structure |
---|
1166 | * @returns time that this packet was seen in 64bit floating point seconds |
---|
1167 | * @author Perry Lorier |
---|
1168 | */ |
---|
1169 | double trace_get_seconds(const struct libtrace_packet_t *packet) { |
---|
1170 | double seconds = 0.0; |
---|
1171 | uint64_t ts = 0; |
---|
1172 | struct timeval tv; |
---|
1173 | |
---|
1174 | assert(packet->size>0 && packet->size<65536); |
---|
1175 | |
---|
1176 | if (packet->trace->format->get_seconds) { |
---|
1177 | /* seconds->seconds */ |
---|
1178 | seconds = packet->trace->format->get_seconds(packet); |
---|
1179 | } else if (packet->trace->format->get_erf_timestamp) { |
---|
1180 | /* timestamp -> seconds */ |
---|
1181 | ts = packet->trace->format->get_erf_timestamp(packet); |
---|
1182 | seconds = (ts>>32) + ((ts & UINT_MAX)*1.0 / UINT_MAX); |
---|
1183 | } else if (packet->trace->format->get_timeval) { |
---|
1184 | /* timeval -> seconds */ |
---|
1185 | tv = packet->trace->format->get_timeval(packet); |
---|
1186 | seconds = tv.tv_sec + ((tv.tv_usec * UINT_MAX * 1.0)/1000000); |
---|
1187 | } |
---|
1188 | |
---|
1189 | return seconds; |
---|
1190 | } |
---|
1191 | |
---|
1192 | size_t trace_get_capture_length(const libtrace_packet_t *packet) { |
---|
1193 | |
---|
1194 | assert(packet->size>0 && packet->size<65536); |
---|
1195 | |
---|
1196 | if (packet->trace->format->get_capture_length) { |
---|
1197 | return packet->trace->format->get_capture_length(packet); |
---|
1198 | } |
---|
1199 | return -1; |
---|
1200 | } |
---|
1201 | |
---|
1202 | /* Get the size of the packet as it was seen on the wire. |
---|
1203 | * @param packet a pointer to a libtrace_packet structure |
---|
1204 | * |
---|
1205 | * @returns the size of the packet as it was on the wire. |
---|
1206 | * @author Perry Lorier |
---|
1207 | * @author Daniel Lawson |
---|
1208 | * @note Due to the trace being a header capture, or anonymisation this may |
---|
1209 | * not be the same as the Capture Len. |
---|
1210 | */ |
---|
1211 | size_t trace_get_wire_length(const libtrace_packet_t *packet){ |
---|
1212 | assert(packet->size>0 && packet->size<65536); |
---|
1213 | |
---|
1214 | if (packet->trace->format->get_wire_length) { |
---|
1215 | return packet->trace->format->get_wire_length(packet); |
---|
1216 | } |
---|
1217 | return -1; |
---|
1218 | |
---|
1219 | } |
---|
1220 | |
---|
1221 | /* Get the length of the capture framing headers. |
---|
1222 | * @param packet the packet opaque pointer |
---|
1223 | * @returns the size of the packet as it was on the wire. |
---|
1224 | * @author Perry Lorier |
---|
1225 | * @author Daniel Lawson |
---|
1226 | * @note this length corresponds to the difference between the size of a |
---|
1227 | * captured packet in memory, and the captured length of the packet |
---|
1228 | */ |
---|
1229 | SIMPLE_FUNCTION |
---|
1230 | size_t trace_get_framing_length(const libtrace_packet_t *packet) { |
---|
1231 | if (packet->trace->format->get_framing_length) { |
---|
1232 | return packet->trace->format->get_framing_length(packet); |
---|
1233 | } |
---|
1234 | return -1; |
---|
1235 | } |
---|
1236 | |
---|
1237 | |
---|
1238 | /* Get the type of the link layer |
---|
1239 | * @param packet a pointer to a libtrace_packet structure |
---|
1240 | * @returns libtrace_linktype_t |
---|
1241 | * @author Perry Lorier |
---|
1242 | * @author Daniel Lawson |
---|
1243 | */ |
---|
1244 | libtrace_linktype_t trace_get_link_type(const libtrace_packet_t *packet ) { |
---|
1245 | if (packet->trace->format->get_link_type) { |
---|
1246 | return packet->trace->format->get_link_type(packet); |
---|
1247 | } |
---|
1248 | return -1; |
---|
1249 | } |
---|
1250 | |
---|
1251 | /* Get the source MAC addres |
---|
1252 | * @param packet a pointer to a libtrace_packet structure |
---|
1253 | * @returns a pointer to the source mac, (or NULL if there is no source MAC) |
---|
1254 | * @author Perry Lorier |
---|
1255 | */ |
---|
1256 | uint8_t *trace_get_source_mac(const struct libtrace_packet_t *packet) { |
---|
1257 | void *link = trace_get_link(packet); |
---|
1258 | struct ieee_802_11_header *wifi = link; |
---|
1259 | struct libtrace_ether *ethptr = link; |
---|
1260 | if (!link) |
---|
1261 | return NULL; |
---|
1262 | switch (trace_get_link_type(packet)) { |
---|
1263 | case TRACE_TYPE_80211: |
---|
1264 | return (uint8_t*)&wifi->mac2; |
---|
1265 | case TRACE_TYPE_ETH: |
---|
1266 | return (uint8_t*)ðptr->ether_shost; |
---|
1267 | default: |
---|
1268 | fprintf(stderr,"Not implemented\n"); |
---|
1269 | assert(0); |
---|
1270 | } |
---|
1271 | } |
---|
1272 | |
---|
1273 | /* Get the destination MAC addres |
---|
1274 | * @param packet a libtrace_packet pointer |
---|
1275 | * @returns a pointer to the destination mac, (or NULL if there is no |
---|
1276 | * destination MAC) |
---|
1277 | * @author Perry Lorier |
---|
1278 | */ |
---|
1279 | uint8_t *trace_get_destination_mac(const struct libtrace_packet_t *packet) { |
---|
1280 | void *link = trace_get_link(packet); |
---|
1281 | struct ieee_802_11_header *wifi = link; |
---|
1282 | struct libtrace_ether *ethptr = link; |
---|
1283 | if (!link) |
---|
1284 | return NULL; |
---|
1285 | switch (trace_get_link_type(packet)) { |
---|
1286 | case TRACE_TYPE_80211: |
---|
1287 | return (uint8_t*)&wifi->mac1; |
---|
1288 | case TRACE_TYPE_ETH: |
---|
1289 | return (uint8_t*)ðptr->ether_dhost; |
---|
1290 | default: |
---|
1291 | fprintf(stderr,"Not implemented\n"); |
---|
1292 | assert(0); |
---|
1293 | } |
---|
1294 | } |
---|
1295 | |
---|
1296 | |
---|
1297 | /* process a libtrace event |
---|
1298 | * @param trace the libtrace opaque pointer |
---|
1299 | * @param packet the libtrace_packet opaque pointer |
---|
1300 | * @returns |
---|
1301 | * TRACE_EVENT_IOWAIT Waiting on I/O on fd |
---|
1302 | * TRACE_EVENT_SLEEP Next event in seconds |
---|
1303 | * TRACE_EVENT_PACKET Packet arrived in buffer with size size |
---|
1304 | * TRACE_EVENT_TERMINATE Trace terminated (perhaps with an error condition) |
---|
1305 | * FIXME currently keeps a copy of the packet inside the trace pointer, |
---|
1306 | * which in turn is stored inside the new packet object... |
---|
1307 | * @author Perry Lorier |
---|
1308 | */ |
---|
1309 | struct libtrace_eventobj_t trace_event(struct libtrace_t *trace, |
---|
1310 | struct libtrace_packet_t *packet) { |
---|
1311 | struct libtrace_eventobj_t event = {0,0,0.0,0}; |
---|
1312 | |
---|
1313 | if (!trace) { |
---|
1314 | fprintf(stderr,"You called trace_event() with a NULL trace object!\n"); |
---|
1315 | } |
---|
1316 | assert(trace); |
---|
1317 | assert(packet); |
---|
1318 | |
---|
1319 | /* Store the trace we are reading from into the packet opaque |
---|
1320 | * structure */ |
---|
1321 | packet->trace = trace; |
---|
1322 | |
---|
1323 | if (packet->trace->format->trace_event) { |
---|
1324 | return packet->trace->format->trace_event(trace,packet); |
---|
1325 | } else { |
---|
1326 | return event; |
---|
1327 | } |
---|
1328 | |
---|
1329 | } |
---|
1330 | |
---|
1331 | /* setup a BPF filter |
---|
1332 | * @param filterstring a char * containing the bpf filter string |
---|
1333 | * @returns opaque pointer pointer to a libtrace_filter_t object |
---|
1334 | * @author Daniel Lawson |
---|
1335 | */ |
---|
1336 | struct libtrace_filter_t *trace_bpf_setfilter(const char *filterstring) { |
---|
1337 | #if HAVE_BPF |
---|
1338 | struct libtrace_filter_t *filter = malloc(sizeof(struct libtrace_filter_t)); |
---|
1339 | filter->filterstring = strdup(filterstring); |
---|
1340 | filter->flag = 0; |
---|
1341 | return filter; |
---|
1342 | #else |
---|
1343 | fprintf(stderr,"This version of libtrace does not have bpf filter support\n"); |
---|
1344 | return 0; |
---|
1345 | #endif |
---|
1346 | } |
---|
1347 | |
---|
1348 | /* compile a bpf filter, now we know what trace it's on |
---|
1349 | * @internal |
---|
1350 | * |
---|
1351 | * @returns -1 on error, 0 on success |
---|
1352 | */ |
---|
1353 | int trace_bpf_compile(libtrace_filter_t *filter, |
---|
1354 | const libtrace_packet_t *packet ) { |
---|
1355 | #if HAVE_BPF |
---|
1356 | void *linkptr = 0; |
---|
1357 | assert(filter); |
---|
1358 | |
---|
1359 | /* If this isn't a real packet, then fail */ |
---|
1360 | linkptr = trace_get_link(packet); |
---|
1361 | if (!linkptr) { |
---|
1362 | trace_set_err(packet->trace, |
---|
1363 | TRACE_ERR_BAD_PACKET,"Packet has no payload"); |
---|
1364 | return -1; |
---|
1365 | } |
---|
1366 | |
---|
1367 | if (filter->filterstring && ! filter->flag) { |
---|
1368 | pcap_t *pcap; |
---|
1369 | libtrace_linktype_t linktype=trace_get_link_type(packet); |
---|
1370 | if (linktype==(libtrace_linktype_t)-1) { |
---|
1371 | trace_set_err(packet->trace,TRACE_ERR_BAD_PACKET, |
---|
1372 | "Packet has an unknown linktype"); |
---|
1373 | return -1; |
---|
1374 | } |
---|
1375 | if (libtrace_to_pcap_dlt(linktype) == -1) { |
---|
1376 | trace_set_err(packet->trace,TRACE_ERR_BAD_PACKET, |
---|
1377 | "Unknown pcap equivilent linktype"); |
---|
1378 | return -1; |
---|
1379 | } |
---|
1380 | pcap=(pcap_t *)pcap_open_dead( |
---|
1381 | libtrace_to_pcap_dlt(linktype), |
---|
1382 | 1500); |
---|
1383 | /* build filter */ |
---|
1384 | if (pcap_compile( pcap, &filter->filter, filter->filterstring, |
---|
1385 | 1, 0)) { |
---|
1386 | pcap_close(pcap); |
---|
1387 | trace_set_err(packet->trace,TRACE_ERR_BAD_PACKET, |
---|
1388 | "Packet has no payload"); |
---|
1389 | return -1; |
---|
1390 | } |
---|
1391 | pcap_close(pcap); |
---|
1392 | filter->flag=1; |
---|
1393 | } |
---|
1394 | return 0; |
---|
1395 | #else |
---|
1396 | assert(!"This should never be called when BPF not enabled"); |
---|
1397 | #endif |
---|
1398 | } |
---|
1399 | |
---|
1400 | int trace_bpf_filter(struct libtrace_filter_t *filter, |
---|
1401 | const struct libtrace_packet_t *packet) { |
---|
1402 | #if HAVE_BPF |
---|
1403 | void *linkptr = 0; |
---|
1404 | int clen = 0; |
---|
1405 | assert(filter); |
---|
1406 | assert(packet); |
---|
1407 | linkptr = trace_get_link(packet); |
---|
1408 | if (!linkptr) { |
---|
1409 | return 0; |
---|
1410 | } |
---|
1411 | |
---|
1412 | /* We need to compile it now, because before we didn't know what the |
---|
1413 | * link type was |
---|
1414 | */ |
---|
1415 | if (trace_bpf_compile(filter,packet)==-1) |
---|
1416 | return -1; |
---|
1417 | |
---|
1418 | clen = trace_get_capture_length(packet); |
---|
1419 | |
---|
1420 | assert(filter->flag); |
---|
1421 | return bpf_filter(filter->filter.bf_insns, linkptr, clen, clen); |
---|
1422 | #else |
---|
1423 | fprintf(stderr,"This version of libtrace does not have bpf filter support\n"); |
---|
1424 | return 0; |
---|
1425 | #endif |
---|
1426 | } |
---|
1427 | |
---|
1428 | /* Set the direction flag, if it has one |
---|
1429 | * @param packet the packet opaque pointer |
---|
1430 | * @param direction the new direction (0,1,2,3) |
---|
1431 | * @returns a signed value containing the direction flag, or -1 if this is not supported |
---|
1432 | * @author Daniel Lawson |
---|
1433 | */ |
---|
1434 | int8_t trace_set_direction(struct libtrace_packet_t *packet, int8_t direction) { |
---|
1435 | assert(packet); |
---|
1436 | assert(packet->size>0 && packet->size<65536); |
---|
1437 | if (packet->trace->format->set_direction) { |
---|
1438 | return packet->trace->format->set_direction(packet,direction); |
---|
1439 | } |
---|
1440 | return -1; |
---|
1441 | } |
---|
1442 | |
---|
1443 | /* Get the direction flag, if it has one |
---|
1444 | * @param packet a pointer to a libtrace_packet structure |
---|
1445 | * @returns a signed value containing the direction flag, or -1 if this is not supported |
---|
1446 | * The direction is defined as 0 for packets originating locally (ie, outbound) |
---|
1447 | * and 1 for packets originating remotely (ie, inbound). |
---|
1448 | * Other values are possible, which might be overloaded to mean special things |
---|
1449 | * for a special trace. |
---|
1450 | * @author Daniel Lawson |
---|
1451 | */ |
---|
1452 | int8_t trace_get_direction(const struct libtrace_packet_t *packet) { |
---|
1453 | assert(packet); |
---|
1454 | assert(packet->size>0 && packet->size<65536); |
---|
1455 | if (packet->trace->format->get_direction) { |
---|
1456 | return packet->trace->format->get_direction(packet); |
---|
1457 | } |
---|
1458 | return -1; |
---|
1459 | } |
---|
1460 | |
---|
1461 | struct ports_t { |
---|
1462 | uint16_t src; |
---|
1463 | uint16_t dst; |
---|
1464 | }; |
---|
1465 | |
---|
1466 | /* Return the client port |
---|
1467 | */ |
---|
1468 | uint16_t trace_get_source_port(const struct libtrace_packet_t *packet) |
---|
1469 | { |
---|
1470 | struct libtrace_ip *ip = trace_get_ip(packet); |
---|
1471 | struct ports_t *port; |
---|
1472 | if (6 != ip->ip_p |
---|
1473 | && 17 != ip->ip_p) |
---|
1474 | return 0; |
---|
1475 | if (0 != (ip->ip_off & SW_IP_OFFMASK)) |
---|
1476 | return 0; |
---|
1477 | |
---|
1478 | port = (struct ports_t *)((ptrdiff_t)ip + (ip->ip_hl * 4)); |
---|
1479 | |
---|
1480 | return ntohs(port->src); |
---|
1481 | } |
---|
1482 | |
---|
1483 | /* Same as get_source_port except use the destination port */ |
---|
1484 | uint16_t trace_get_destination_port(const struct libtrace_packet_t *packet) |
---|
1485 | { |
---|
1486 | struct libtrace_ip *ip = trace_get_ip(packet); |
---|
1487 | struct ports_t *port; |
---|
1488 | |
---|
1489 | if (6 != ip->ip_p |
---|
1490 | && 17 != ip->ip_p) |
---|
1491 | return 0; |
---|
1492 | |
---|
1493 | if (0 != (ip->ip_off & SW_IP_OFFMASK)) |
---|
1494 | return 0; |
---|
1495 | |
---|
1496 | port = (struct ports_t *)((ptrdiff_t)ip + (ip->ip_hl * 4)); |
---|
1497 | |
---|
1498 | return ntohs(port->dst); |
---|
1499 | } |
---|
1500 | |
---|
1501 | #define ROOT_SERVER(x) ((x) < 512) |
---|
1502 | #define ROOT_CLIENT(x) ((512 <= (x)) && ((x) < 1024)) |
---|
1503 | #define NONROOT_SERVER(x) ((x) >= 5000) |
---|
1504 | #define NONROOT_CLIENT(x) ((1024 <= (x)) && ((x) < 5000)) |
---|
1505 | #define DYNAMIC(x) ((49152 < (x)) && ((x) < 65535)) |
---|
1506 | #define SERVER(x) ROOT_SERVER(x) || NONROOT_SERVER(x) |
---|
1507 | #define CLIENT(x) ROOT_CLIENT(x) || NONROOT_CLIENT(x) |
---|
1508 | |
---|
1509 | /* Attempt to deduce the 'server' port |
---|
1510 | * @param protocol the IP protocol (eg, 6 or 17 for TCP or UDP) |
---|
1511 | * @param source the TCP or UDP source port |
---|
1512 | * @param dest the TCP or UDP destination port |
---|
1513 | * @returns a hint as to which port is the server port |
---|
1514 | * @author Daniel Lawson |
---|
1515 | */ |
---|
1516 | int8_t trace_get_server_port(uint8_t protocol __attribute__((unused)), uint16_t source, uint16_t dest) { |
---|
1517 | /* |
---|
1518 | * * If the ports are equal, return DEST |
---|
1519 | * * Check for well-known ports in the given protocol |
---|
1520 | * * Root server ports: 0 - 511 |
---|
1521 | * * Root client ports: 512 - 1023 |
---|
1522 | * * non-root client ports: 1024 - 4999 |
---|
1523 | * * non-root server ports: 5000+ |
---|
1524 | * * Check for static ranges: 1024 - 49151 |
---|
1525 | * * Check for dynamic ranges: 49152 - 65535 |
---|
1526 | * * flip a coin. |
---|
1527 | */ |
---|
1528 | |
---|
1529 | /* equal */ |
---|
1530 | if (source == dest) |
---|
1531 | return USE_DEST; |
---|
1532 | |
---|
1533 | /* root server port, 0 - 511 */ |
---|
1534 | if (ROOT_SERVER(source) && ROOT_SERVER(dest)) { |
---|
1535 | if (source < dest) |
---|
1536 | return USE_SOURCE; |
---|
1537 | return USE_DEST; |
---|
1538 | } |
---|
1539 | |
---|
1540 | if (ROOT_SERVER(source) && !ROOT_SERVER(dest)) |
---|
1541 | return USE_SOURCE; |
---|
1542 | if (!ROOT_SERVER(source) && ROOT_SERVER(dest)) |
---|
1543 | return USE_DEST; |
---|
1544 | |
---|
1545 | /* non-root server */ |
---|
1546 | if (NONROOT_SERVER(source) && NONROOT_SERVER(dest)) { |
---|
1547 | if (source < dest) |
---|
1548 | return USE_SOURCE; |
---|
1549 | return USE_DEST; |
---|
1550 | } |
---|
1551 | if (NONROOT_SERVER(source) && !NONROOT_SERVER(dest)) |
---|
1552 | return USE_SOURCE; |
---|
1553 | if (!NONROOT_SERVER(source) && NONROOT_SERVER(dest)) |
---|
1554 | return USE_DEST; |
---|
1555 | |
---|
1556 | /* root client */ |
---|
1557 | if (ROOT_CLIENT(source) && ROOT_CLIENT(dest)) { |
---|
1558 | if (source < dest) |
---|
1559 | return USE_SOURCE; |
---|
1560 | return USE_DEST; |
---|
1561 | } |
---|
1562 | if (ROOT_CLIENT(source) && !ROOT_CLIENT(dest)) { |
---|
1563 | /* prefer root-client over nonroot-client */ |
---|
1564 | if (NONROOT_CLIENT(dest)) |
---|
1565 | return USE_SOURCE; |
---|
1566 | return USE_DEST; |
---|
1567 | } |
---|
1568 | if (!ROOT_CLIENT(source) && ROOT_CLIENT(dest)) { |
---|
1569 | /* prefer root-client over nonroot-client */ |
---|
1570 | if (NONROOT_CLIENT(source)) |
---|
1571 | return USE_DEST; |
---|
1572 | return USE_SOURCE; |
---|
1573 | } |
---|
1574 | |
---|
1575 | /* nonroot client */ |
---|
1576 | if (NONROOT_CLIENT(source) && NONROOT_CLIENT(dest)) { |
---|
1577 | if (source < dest) |
---|
1578 | return USE_SOURCE; |
---|
1579 | return USE_DEST; |
---|
1580 | } |
---|
1581 | if (NONROOT_CLIENT(source) && !NONROOT_CLIENT(dest)) |
---|
1582 | return USE_DEST; |
---|
1583 | if (!NONROOT_CLIENT(source) && NONROOT_CLIENT(dest)) |
---|
1584 | return USE_SOURCE; |
---|
1585 | |
---|
1586 | /* dynamic range */ |
---|
1587 | if (DYNAMIC(source) && DYNAMIC(dest)) |
---|
1588 | if (source < dest) |
---|
1589 | return USE_SOURCE; |
---|
1590 | return USE_DEST; |
---|
1591 | if (DYNAMIC(source) && !DYNAMIC(dest)) |
---|
1592 | return USE_DEST; |
---|
1593 | if (!DYNAMIC(source) && DYNAMIC(dest)) |
---|
1594 | return USE_SOURCE; |
---|
1595 | /* |
---|
1596 | if (SERVER(source) && CLIENT(dest)) |
---|
1597 | return USE_SOURCE; |
---|
1598 | |
---|
1599 | if (SERVER(dest) && CLIENT(source)) |
---|
1600 | return USE_DEST; |
---|
1601 | if (ROOT_SERVER(source) && !ROOT_SERVER(dest)) |
---|
1602 | return USE_SOURCE; |
---|
1603 | if (ROOT_SERVER(dest) && !ROOT_SERVER(source)) |
---|
1604 | return USE_DEST; |
---|
1605 | */ |
---|
1606 | /* failing that test... */ |
---|
1607 | if (source < dest) { |
---|
1608 | return USE_SOURCE; |
---|
1609 | } |
---|
1610 | return USE_DEST; |
---|
1611 | |
---|
1612 | } |
---|
1613 | |
---|
1614 | /* Truncate the packet at the suggested length |
---|
1615 | * @param packet the packet opaque pointer |
---|
1616 | * @param size the new length of the packet |
---|
1617 | * @returns the new size of the packet |
---|
1618 | * @note size and the return size refer to the network-level payload of the |
---|
1619 | * packet, and do not include any capture headers. For example, to truncate a |
---|
1620 | * packet after the IP header, set size to sizeof(ethernet_header) + |
---|
1621 | * sizeof(ip_header) |
---|
1622 | * @note If the original network-level payload is smaller than size, then the |
---|
1623 | * original size is returned and the packet is left unchanged. |
---|
1624 | * @author Daniel Lawson |
---|
1625 | */ |
---|
1626 | size_t trace_set_capture_length(struct libtrace_packet_t *packet, size_t size) { |
---|
1627 | assert(packet); |
---|
1628 | assert(packet->size>0 && packet->size<65536); |
---|
1629 | |
---|
1630 | if (packet->trace->format->set_capture_length) { |
---|
1631 | int caplen=packet->trace->format->set_capture_length(packet,size); |
---|
1632 | if (caplen!=-1) { |
---|
1633 | packet->size=trace_get_framing_length(packet)+caplen; |
---|
1634 | } |
---|
1635 | return caplen; |
---|
1636 | } |
---|
1637 | |
---|
1638 | return -1; |
---|
1639 | } |
---|
1640 | |
---|
1641 | const char * trace_parse_uri(const char *uri, char **format) { |
---|
1642 | const char *uridata = 0; |
---|
1643 | |
---|
1644 | if((uridata = strchr(uri,':')) == NULL) { |
---|
1645 | /* badly formed URI - needs a : */ |
---|
1646 | return 0; |
---|
1647 | } |
---|
1648 | |
---|
1649 | if ((uridata - uri) > URI_PROTO_LINE) { |
---|
1650 | /* badly formed URI - uri type is too long */ |
---|
1651 | return 0; |
---|
1652 | } |
---|
1653 | |
---|
1654 | *format=xstrndup(uri, (uridata - uri)); |
---|
1655 | |
---|
1656 | /* push uridata past the delimiter */ |
---|
1657 | uridata++; |
---|
1658 | |
---|
1659 | return uridata; |
---|
1660 | } |
---|
1661 | |
---|
1662 | enum base_format_t trace_get_format(struct libtrace_packet_t *packet) |
---|
1663 | { |
---|
1664 | assert(packet); |
---|
1665 | |
---|
1666 | return packet->trace->format->type; |
---|
1667 | } |
---|
1668 | |
---|
1669 | libtrace_err_t trace_get_err(libtrace_t *trace) |
---|
1670 | { |
---|
1671 | libtrace_err_t err = trace->err; |
---|
1672 | trace->err.err_num = 0; /* "OK" */ |
---|
1673 | trace->err.problem[0]='\0'; |
---|
1674 | return err; |
---|
1675 | } |
---|
1676 | |
---|
1677 | libtrace_err_t trace_get_err_output(libtrace_out_t *trace) |
---|
1678 | { |
---|
1679 | libtrace_err_t err = trace->err; |
---|
1680 | trace->err.err_num = 0; /* "OK" */ |
---|
1681 | trace->err.problem[0]='\0'; |
---|
1682 | return err; |
---|
1683 | } |
---|
1684 | |
---|
1685 | int trace_seek_erf_timestamp(libtrace_t *trace, uint64_t ts) |
---|
1686 | { |
---|
1687 | if (trace->format->seek_erf) { |
---|
1688 | return trace->format->seek_erf(trace,ts); |
---|
1689 | } |
---|
1690 | else { |
---|
1691 | if (trace->format->seek_timeval) { |
---|
1692 | struct timeval tv; |
---|
1693 | #if __BYTE_ORDER == __BIG_ENDIAN |
---|
1694 | tv.tv_sec = ts & 0xFFFFFFFF; |
---|
1695 | #elif __BYTE_ORDER == __LITTLE_ENDIAN |
---|
1696 | tv.tv_sec = ts >> 32; |
---|
1697 | #else |
---|
1698 | #error "What on earth are you running this on?" |
---|
1699 | #endif |
---|
1700 | tv.tv_usec = ((ts&0xFFFFFFFF)*1000000)>>32; |
---|
1701 | if (tv.tv_usec >= 1000000) { |
---|
1702 | tv.tv_usec -= 1000000; |
---|
1703 | tv.tv_sec += 1; |
---|
1704 | } |
---|
1705 | return trace->format->seek_timeval(trace,tv); |
---|
1706 | } |
---|
1707 | if (trace->format->seek_seconds) { |
---|
1708 | double seconds = |
---|
1709 | (ts>>32) + ((ts & UINT_MAX)*1.0 / UINT_MAX); |
---|
1710 | return trace->format->seek_seconds(trace,seconds); |
---|
1711 | } |
---|
1712 | trace_set_err(trace, |
---|
1713 | TRACE_ERR_OPTION_UNAVAIL, |
---|
1714 | "Feature unimplemented"); |
---|
1715 | return -1; |
---|
1716 | } |
---|
1717 | } |
---|
1718 | |
---|
1719 | int trace_seek_seconds(libtrace_t *trace, double seconds) |
---|
1720 | { |
---|
1721 | if (trace->format->seek_seconds) { |
---|
1722 | return trace->format->seek_seconds(trace,seconds); |
---|
1723 | } |
---|
1724 | else { |
---|
1725 | if (trace->format->seek_timeval) { |
---|
1726 | struct timeval tv; |
---|
1727 | tv.tv_sec = (uint32_t)seconds; |
---|
1728 | tv.tv_usec = (uint32_t)(((seconds - tv.tv_sec) * 1000000)/UINT_MAX); |
---|
1729 | return trace->format->seek_timeval(trace,tv); |
---|
1730 | } |
---|
1731 | if (trace->format->seek_erf) { |
---|
1732 | uint64_t timestamp = |
---|
1733 | ((uint64_t)((uint32_t)seconds) << 32) + \ |
---|
1734 | (( seconds - (uint32_t)seconds ) * UINT_MAX); |
---|
1735 | return trace->format->seek_erf(trace,timestamp); |
---|
1736 | } |
---|
1737 | trace_set_err(trace, |
---|
1738 | TRACE_ERR_OPTION_UNAVAIL, |
---|
1739 | "Feature unimplemented"); |
---|
1740 | return -1; |
---|
1741 | } |
---|
1742 | } |
---|
1743 | |
---|
1744 | int trace_seek_timeval(libtrace_t *trace, struct timeval tv) |
---|
1745 | { |
---|
1746 | if (trace->format->seek_timeval) { |
---|
1747 | return trace->format->seek_timeval(trace,tv); |
---|
1748 | } |
---|
1749 | else { |
---|
1750 | if (trace->format->seek_erf) { |
---|
1751 | uint64_t timestamp = ((((uint64_t)tv.tv_sec) << 32) + \ |
---|
1752 | (((uint64_t)tv.tv_usec * UINT_MAX)/1000000)); |
---|
1753 | return trace->format->seek_erf(trace,timestamp); |
---|
1754 | } |
---|
1755 | if (trace->format->seek_seconds) { |
---|
1756 | double seconds = tv.tv_sec + ((tv.tv_usec * UINT_MAX * 1.0)/1000000); |
---|
1757 | return trace->format->seek_seconds(trace,seconds); |
---|
1758 | } |
---|
1759 | trace_set_err(trace, |
---|
1760 | TRACE_ERR_OPTION_UNAVAIL, |
---|
1761 | "Feature unimplemented"); |
---|
1762 | return -1; |
---|
1763 | } |
---|
1764 | } |
---|
1765 | |
---|