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