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