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