1 | /* |
---|
2 | * This file is part of libtrace |
---|
3 | * |
---|
4 | * Copyright (c) 2007 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 | #ifdef HAVE_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 | free(libtrace); |
---|
625 | } |
---|
626 | /* Close an output trace file, freeing up any resources it may have been using |
---|
627 | * |
---|
628 | * @param libtrace the output trace file to be destroyed |
---|
629 | * |
---|
630 | * @author Shane Alcock |
---|
631 | * */ |
---|
632 | DLLEXPORT void trace_destroy_output(libtrace_out_t *libtrace) |
---|
633 | { |
---|
634 | assert(libtrace); |
---|
635 | if (libtrace->format && libtrace->format->fin_output) |
---|
636 | libtrace->format->fin_output(libtrace); |
---|
637 | if (libtrace->uridata) |
---|
638 | free(libtrace->uridata); |
---|
639 | free(libtrace); |
---|
640 | } |
---|
641 | |
---|
642 | DLLEXPORT libtrace_packet_t *trace_create_packet(void) |
---|
643 | { |
---|
644 | libtrace_packet_t *packet = |
---|
645 | (libtrace_packet_t*)calloc((size_t)1,sizeof(libtrace_packet_t)); |
---|
646 | |
---|
647 | packet->buf_control=TRACE_CTRL_PACKET; |
---|
648 | packet->capture_length=-1; |
---|
649 | return packet; |
---|
650 | } |
---|
651 | |
---|
652 | DLLEXPORT libtrace_packet_t *trace_copy_packet(const libtrace_packet_t *packet) { |
---|
653 | libtrace_packet_t *dest = |
---|
654 | (libtrace_packet_t *)malloc(sizeof(libtrace_packet_t)); |
---|
655 | if (!dest) { |
---|
656 | printf("out of memory constructing packet\n"); |
---|
657 | abort(); |
---|
658 | } |
---|
659 | dest->trace=packet->trace; |
---|
660 | dest->buffer=malloc(65536); |
---|
661 | if (!dest->buffer) { |
---|
662 | printf("out of memory allocating buffer memory\n"); |
---|
663 | abort(); |
---|
664 | } |
---|
665 | dest->header=dest->buffer; |
---|
666 | dest->payload=(void*) |
---|
667 | ((char*)dest->buffer+trace_get_framing_length(packet)); |
---|
668 | dest->type=packet->type; |
---|
669 | dest->buf_control=TRACE_CTRL_PACKET; |
---|
670 | /* Reset the cache - better to recalculate than try to convert |
---|
671 | * the values over to the new packet */ |
---|
672 | dest->capture_length = -1; |
---|
673 | dest->l3_header = NULL; |
---|
674 | dest->l3_ethertype = 0; |
---|
675 | memcpy(dest->header,packet->header,trace_get_framing_length(packet)); |
---|
676 | memcpy(dest->payload,packet->payload,trace_get_capture_length(packet)); |
---|
677 | |
---|
678 | return dest; |
---|
679 | } |
---|
680 | |
---|
681 | /** Destroy a packet object |
---|
682 | * |
---|
683 | * sideeffect: sets packet to NULL |
---|
684 | */ |
---|
685 | DLLEXPORT void trace_destroy_packet(libtrace_packet_t *packet) { |
---|
686 | if (packet->buf_control == TRACE_CTRL_PACKET) { |
---|
687 | free(packet->buffer); |
---|
688 | } |
---|
689 | packet->buf_control=(buf_control_t)'\0'; |
---|
690 | /* an "bad" value to force an assert |
---|
691 | * if this packet is ever reused |
---|
692 | */ |
---|
693 | free(packet); |
---|
694 | } |
---|
695 | |
---|
696 | /* Read one packet from the trace into buffer |
---|
697 | * |
---|
698 | * @param libtrace the libtrace opaque pointer |
---|
699 | * @param packet the packet opaque pointer |
---|
700 | * @returns 0 on EOF, negative value on error |
---|
701 | * |
---|
702 | */ |
---|
703 | DLLEXPORT int trace_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { |
---|
704 | |
---|
705 | assert(libtrace && "You called trace_read_packet() with a NULL libtrace parameter!\n"); |
---|
706 | if (trace_is_err(libtrace)) |
---|
707 | return -1; |
---|
708 | if (!libtrace->started) { |
---|
709 | trace_set_err(libtrace,TRACE_ERR_BAD_STATE,"You must call libtrace_start() before trace_read_packet()\n"); |
---|
710 | return -1; |
---|
711 | } |
---|
712 | if (!(packet->buf_control==TRACE_CTRL_PACKET || packet->buf_control==TRACE_CTRL_EXTERNAL)) { |
---|
713 | trace_set_err(libtrace,TRACE_ERR_BAD_STATE,"Packet passed to trace_read_packet() is invalid\n"); |
---|
714 | return -1; |
---|
715 | } |
---|
716 | assert(packet); |
---|
717 | |
---|
718 | /* Store the trace we are reading from into the packet opaque |
---|
719 | * structure */ |
---|
720 | packet->trace = libtrace; |
---|
721 | |
---|
722 | /* Finalise the packet, freeing any resources the format module |
---|
723 | * may have allocated it |
---|
724 | */ |
---|
725 | if (libtrace->format->fin_packet) { |
---|
726 | libtrace->format->fin_packet(packet); |
---|
727 | } |
---|
728 | |
---|
729 | /* Clear the packet cache */ |
---|
730 | packet->capture_length = -1; |
---|
731 | packet->l3_header = NULL; |
---|
732 | packet->l3_ethertype = 0; |
---|
733 | |
---|
734 | if (libtrace->format->read_packet) { |
---|
735 | do { |
---|
736 | size_t ret; |
---|
737 | ret=libtrace->format->read_packet(libtrace,packet); |
---|
738 | if (ret==(size_t)-1 || ret==0) { |
---|
739 | return ret; |
---|
740 | } |
---|
741 | if (libtrace->filter) { |
---|
742 | /* If the filter doesn't match, read another |
---|
743 | * packet |
---|
744 | */ |
---|
745 | if (!trace_apply_filter(libtrace->filter,packet)){ |
---|
746 | continue; |
---|
747 | } |
---|
748 | } |
---|
749 | if (libtrace->snaplen>0) { |
---|
750 | /* Snap the packet */ |
---|
751 | trace_set_capture_length(packet, |
---|
752 | libtrace->snaplen); |
---|
753 | } |
---|
754 | return ret; |
---|
755 | } while(1); |
---|
756 | } |
---|
757 | trace_set_err(libtrace,TRACE_ERR_UNSUPPORTED,"This format does not support reading packets\n"); |
---|
758 | return ~0U; |
---|
759 | } |
---|
760 | |
---|
761 | /* Writes a packet to the specified output |
---|
762 | * |
---|
763 | * @param libtrace describes the output format, destination, etc. |
---|
764 | * @param packet the packet to be written out |
---|
765 | * @returns the number of bytes written, -1 if write failed |
---|
766 | * |
---|
767 | * @author Shane Alcock |
---|
768 | * */ |
---|
769 | DLLEXPORT int trace_write_packet(libtrace_out_t *libtrace, libtrace_packet_t *packet) { |
---|
770 | assert(libtrace); |
---|
771 | assert(packet); |
---|
772 | /* Verify the packet is valid */ |
---|
773 | if (!libtrace->started) { |
---|
774 | trace_set_err_out(libtrace,TRACE_ERR_BAD_STATE, |
---|
775 | "Trace is not started before trace_write_packet"); |
---|
776 | return -1; |
---|
777 | } |
---|
778 | |
---|
779 | if (libtrace->format->write_packet) { |
---|
780 | return libtrace->format->write_packet(libtrace, packet); |
---|
781 | } |
---|
782 | trace_set_err_out(libtrace,TRACE_ERR_UNSUPPORTED, |
---|
783 | "This format does not support writing packets"); |
---|
784 | return -1; |
---|
785 | } |
---|
786 | |
---|
787 | DLLEXPORT void *trace_get_packet_buffer(const libtrace_packet_t *packet, |
---|
788 | libtrace_linktype_t *linktype, uint32_t *remaining) { |
---|
789 | assert(packet != NULL); |
---|
790 | if (linktype) *linktype = trace_get_link_type(packet); |
---|
791 | if (remaining) *remaining = trace_get_capture_length(packet); |
---|
792 | return (void *) packet->payload; |
---|
793 | } |
---|
794 | |
---|
795 | DLLEXPORT void *trace_get_link(const libtrace_packet_t *packet) { |
---|
796 | return (void *)packet->payload; |
---|
797 | } |
---|
798 | |
---|
799 | /* Get the current time in DAG time format |
---|
800 | * @param packet a pointer to a libtrace_packet structure |
---|
801 | * @returns a 64 bit timestamp in DAG ERF format (upper 32 bits are the seconds |
---|
802 | * past 1970-01-01, the lower 32bits are partial seconds) |
---|
803 | * @author Daniel Lawson |
---|
804 | */ |
---|
805 | DLLEXPORT uint64_t trace_get_erf_timestamp(const libtrace_packet_t *packet) { |
---|
806 | uint64_t timestamp = 0; |
---|
807 | double seconds = 0.0; |
---|
808 | struct timeval ts; |
---|
809 | |
---|
810 | if (packet->trace->format->get_erf_timestamp) { |
---|
811 | /* timestamp -> timestamp */ |
---|
812 | timestamp = packet->trace->format->get_erf_timestamp(packet); |
---|
813 | } else if (packet->trace->format->get_timeval) { |
---|
814 | /* timeval -> timestamp */ |
---|
815 | ts = packet->trace->format->get_timeval(packet); |
---|
816 | timestamp = ((((uint64_t)ts.tv_sec) << 32) + \ |
---|
817 | (((uint64_t)ts.tv_usec * UINT_MAX)/1000000)); |
---|
818 | } else if (packet->trace->format->get_seconds) { |
---|
819 | /* seconds -> timestamp */ |
---|
820 | seconds = packet->trace->format->get_seconds(packet); |
---|
821 | timestamp = ((uint64_t)((uint32_t)seconds) << 32) + \ |
---|
822 | (uint64_t)(( seconds - (uint32_t)seconds ) * UINT_MAX); |
---|
823 | } |
---|
824 | return timestamp; |
---|
825 | } |
---|
826 | |
---|
827 | /* Get the current time in struct timeval |
---|
828 | * @param packet a pointer to a libtrace_packet structure |
---|
829 | * |
---|
830 | * @returns time that this packet was seen in a struct timeval |
---|
831 | * @author Daniel Lawson |
---|
832 | * @author Perry Lorier |
---|
833 | */ |
---|
834 | DLLEXPORT struct timeval trace_get_timeval(const libtrace_packet_t *packet) { |
---|
835 | struct timeval tv; |
---|
836 | uint64_t ts = 0; |
---|
837 | double seconds = 0.0; |
---|
838 | if (packet->trace->format->get_timeval) { |
---|
839 | /* timeval -> timeval */ |
---|
840 | tv = packet->trace->format->get_timeval(packet); |
---|
841 | } else if (packet->trace->format->get_erf_timestamp) { |
---|
842 | /* timestamp -> timeval */ |
---|
843 | ts = packet->trace->format->get_erf_timestamp(packet); |
---|
844 | #if __BYTE_ORDER == __BIG_ENDIAN |
---|
845 | tv.tv_sec = ts & 0xFFFFFFFF; |
---|
846 | #elif __BYTE_ORDER == __LITTLE_ENDIAN |
---|
847 | tv.tv_sec = ts >> 32; |
---|
848 | #else |
---|
849 | #error "What on earth are you running this on?" |
---|
850 | #endif |
---|
851 | tv.tv_usec = ((ts&0xFFFFFFFF)*1000000)>>32; |
---|
852 | if (tv.tv_usec >= 1000000) { |
---|
853 | tv.tv_usec -= 1000000; |
---|
854 | tv.tv_sec += 1; |
---|
855 | } |
---|
856 | } else if (packet->trace->format->get_seconds) { |
---|
857 | /* seconds -> timeval */ |
---|
858 | seconds = packet->trace->format->get_seconds(packet); |
---|
859 | tv.tv_sec = (uint32_t)seconds; |
---|
860 | tv.tv_usec = (uint32_t)(((seconds - tv.tv_sec) * 1000000)/UINT_MAX); |
---|
861 | } |
---|
862 | else { |
---|
863 | tv.tv_sec=-1; |
---|
864 | tv.tv_usec=-1; |
---|
865 | } |
---|
866 | |
---|
867 | return tv; |
---|
868 | } |
---|
869 | |
---|
870 | /* Get the current time in floating point seconds |
---|
871 | * @param packet a pointer to a libtrace_packet structure |
---|
872 | * @returns time that this packet was seen in 64bit floating point seconds |
---|
873 | * @author Perry Lorier |
---|
874 | */ |
---|
875 | DLLEXPORT double trace_get_seconds(const libtrace_packet_t *packet) { |
---|
876 | double seconds = 0.0; |
---|
877 | uint64_t ts = 0; |
---|
878 | struct timeval tv; |
---|
879 | |
---|
880 | if (packet->trace->format->get_seconds) { |
---|
881 | /* seconds->seconds */ |
---|
882 | seconds = packet->trace->format->get_seconds(packet); |
---|
883 | } else if (packet->trace->format->get_erf_timestamp) { |
---|
884 | /* timestamp -> seconds */ |
---|
885 | ts = packet->trace->format->get_erf_timestamp(packet); |
---|
886 | seconds = (ts>>32) + ((ts & UINT_MAX)*1.0 / UINT_MAX); |
---|
887 | } else if (packet->trace->format->get_timeval) { |
---|
888 | /* timeval -> seconds */ |
---|
889 | tv = packet->trace->format->get_timeval(packet); |
---|
890 | seconds = tv.tv_sec + ((tv.tv_usec * 1.0) / 1000000); |
---|
891 | } |
---|
892 | |
---|
893 | return seconds; |
---|
894 | } |
---|
895 | |
---|
896 | DLLEXPORT size_t trace_get_capture_length(const libtrace_packet_t *packet) |
---|
897 | { |
---|
898 | /* Cache the capture length */ |
---|
899 | if (packet->capture_length == -1) { |
---|
900 | if (!packet->trace->format->get_capture_length) |
---|
901 | return ~0U; |
---|
902 | /* Cast away constness because this is "just" a cache */ |
---|
903 | ((libtrace_packet_t*)packet)->capture_length = |
---|
904 | packet->trace->format->get_capture_length(packet); |
---|
905 | } |
---|
906 | |
---|
907 | return packet->capture_length; |
---|
908 | } |
---|
909 | |
---|
910 | /* Get the size of the packet as it was seen on the wire. |
---|
911 | * @param packet a pointer to a libtrace_packet structure |
---|
912 | * |
---|
913 | * @returns the size of the packet as it was on the wire. |
---|
914 | * @author Perry Lorier |
---|
915 | * @author Daniel Lawson |
---|
916 | * @note Due to the trace being a header capture, or anonymisation this may |
---|
917 | * not be the same as the Capture Len. |
---|
918 | */ |
---|
919 | DLLEXPORT size_t trace_get_wire_length(const libtrace_packet_t *packet){ |
---|
920 | if (packet->trace->format->get_wire_length) { |
---|
921 | return packet->trace->format->get_wire_length(packet); |
---|
922 | } |
---|
923 | return ~0U; |
---|
924 | |
---|
925 | } |
---|
926 | |
---|
927 | /* Get the length of the capture framing headers. |
---|
928 | * @param packet the packet opaque pointer |
---|
929 | * @returns the size of the packet as it was on the wire. |
---|
930 | * @author Perry Lorier |
---|
931 | * @author Daniel Lawson |
---|
932 | * @note this length corresponds to the difference between the size of a |
---|
933 | * captured packet in memory, and the captured length of the packet |
---|
934 | */ |
---|
935 | DLLEXPORT SIMPLE_FUNCTION |
---|
936 | size_t trace_get_framing_length(const libtrace_packet_t *packet) { |
---|
937 | if (packet->trace->format->get_framing_length) { |
---|
938 | return packet->trace->format->get_framing_length(packet); |
---|
939 | } |
---|
940 | return ~0U; |
---|
941 | } |
---|
942 | |
---|
943 | |
---|
944 | /* Get the type of the link layer |
---|
945 | * @param packet a pointer to a libtrace_packet structure |
---|
946 | * @returns libtrace_linktype_t |
---|
947 | * @author Perry Lorier |
---|
948 | * @author Daniel Lawson |
---|
949 | */ |
---|
950 | DLLEXPORT libtrace_linktype_t trace_get_link_type(const libtrace_packet_t *packet ) { |
---|
951 | if (packet->trace->format->get_link_type) { |
---|
952 | return packet->trace->format->get_link_type(packet); |
---|
953 | } |
---|
954 | return (libtrace_linktype_t)-1; |
---|
955 | } |
---|
956 | |
---|
957 | /* process a libtrace event |
---|
958 | * @param trace the libtrace opaque pointer |
---|
959 | * @param packet the libtrace_packet opaque pointer |
---|
960 | * @returns |
---|
961 | * TRACE_EVENT_IOWAIT Waiting on I/O on fd |
---|
962 | * TRACE_EVENT_SLEEP Next event in seconds |
---|
963 | * TRACE_EVENT_PACKET Packet arrived in buffer with size size |
---|
964 | * TRACE_EVENT_TERMINATE Trace terminated (perhaps with an error condition) |
---|
965 | * FIXME currently keeps a copy of the packet inside the trace pointer, |
---|
966 | * which in turn is stored inside the new packet object... |
---|
967 | * @author Perry Lorier |
---|
968 | */ |
---|
969 | DLLEXPORT libtrace_eventobj_t trace_event(libtrace_t *trace, |
---|
970 | libtrace_packet_t *packet) { |
---|
971 | libtrace_eventobj_t event = {TRACE_EVENT_IOWAIT,0,0.0,0}; |
---|
972 | |
---|
973 | if (!trace) { |
---|
974 | fprintf(stderr,"You called trace_event() with a NULL trace object!\n"); |
---|
975 | } |
---|
976 | assert(trace); |
---|
977 | assert(packet); |
---|
978 | |
---|
979 | /* Store the trace we are reading from into the packet opaque |
---|
980 | * structure */ |
---|
981 | packet->trace = trace; |
---|
982 | |
---|
983 | if (packet->trace->format->trace_event) { |
---|
984 | return packet->trace->format->trace_event(trace,packet); |
---|
985 | } else { |
---|
986 | return event; |
---|
987 | } |
---|
988 | |
---|
989 | } |
---|
990 | |
---|
991 | /** Setup a BPF filter based on pre-compiled byte-code. |
---|
992 | * @param bf_insns A pointer to the start of the byte-code |
---|
993 | * @param bf_len The number of BPF instructions |
---|
994 | * @returns an opaque pointer to a libtrace_filter_t object |
---|
995 | * @note The supplied byte-code is not checked for correctness. |
---|
996 | * @author Scott Raynel |
---|
997 | */ |
---|
998 | DLLEXPORT libtrace_filter_t * |
---|
999 | trace_create_filter_from_bytecode(void *bf_insns, unsigned int bf_len) |
---|
1000 | { |
---|
1001 | #ifndef HAVE_BPF |
---|
1002 | fprintf(stderr, "This version of libtrace does not have BPF support\n"); |
---|
1003 | return NULL; |
---|
1004 | #else |
---|
1005 | struct libtrace_filter_t *filter = (struct libtrace_filter_t *) |
---|
1006 | malloc(sizeof(struct libtrace_filter_t)); |
---|
1007 | filter->filter.bf_insns = (struct bpf_insn *) |
---|
1008 | malloc(sizeof(struct bpf_insn) * bf_len); |
---|
1009 | |
---|
1010 | memcpy(filter->filter.bf_insns, bf_insns, |
---|
1011 | bf_len * sizeof(struct bpf_insn)); |
---|
1012 | |
---|
1013 | filter->filter.bf_len = bf_len; |
---|
1014 | filter->filterstring = NULL; |
---|
1015 | /* "flag" indicates that the filter member is valid */ |
---|
1016 | filter->flag = 1; |
---|
1017 | |
---|
1018 | return filter; |
---|
1019 | #endif |
---|
1020 | } |
---|
1021 | |
---|
1022 | /* setup a BPF filter |
---|
1023 | * @param filterstring a char * containing the bpf filter string |
---|
1024 | * @returns opaque pointer pointer to a libtrace_filter_t object |
---|
1025 | * @author Daniel Lawson |
---|
1026 | */ |
---|
1027 | DLLEXPORT libtrace_filter_t *trace_create_filter(const char *filterstring) { |
---|
1028 | #ifdef HAVE_BPF |
---|
1029 | libtrace_filter_t *filter = (libtrace_filter_t*) |
---|
1030 | malloc(sizeof(libtrace_filter_t)); |
---|
1031 | filter->filterstring = strdup(filterstring); |
---|
1032 | filter->flag = 0; |
---|
1033 | return filter; |
---|
1034 | #else |
---|
1035 | fprintf(stderr,"This version of libtrace does not have bpf filter support\n"); |
---|
1036 | return NULL; |
---|
1037 | #endif |
---|
1038 | } |
---|
1039 | |
---|
1040 | DLLEXPORT void trace_destroy_filter(libtrace_filter_t *filter) |
---|
1041 | { |
---|
1042 | #ifdef HAVE_BPF |
---|
1043 | free(filter->filterstring); |
---|
1044 | if (filter->flag) |
---|
1045 | pcap_freecode(&filter->filter); |
---|
1046 | free(filter); |
---|
1047 | #else |
---|
1048 | |
---|
1049 | #endif |
---|
1050 | } |
---|
1051 | |
---|
1052 | /* compile a bpf filter, now we know what trace it's on |
---|
1053 | * @internal |
---|
1054 | * |
---|
1055 | * @returns -1 on error, 0 on success |
---|
1056 | */ |
---|
1057 | int trace_bpf_compile(libtrace_filter_t *filter, |
---|
1058 | const libtrace_packet_t *packet ) { |
---|
1059 | #ifdef HAVE_BPF |
---|
1060 | void *linkptr = 0; |
---|
1061 | libtrace_linktype_t linktype; |
---|
1062 | assert(filter); |
---|
1063 | |
---|
1064 | /* If this isn't a real packet, then fail */ |
---|
1065 | linkptr = trace_get_packet_buffer(packet,&linktype,NULL); |
---|
1066 | if (!linkptr) { |
---|
1067 | trace_set_err(packet->trace, |
---|
1068 | TRACE_ERR_BAD_PACKET,"Packet has no payload"); |
---|
1069 | return -1; |
---|
1070 | } |
---|
1071 | |
---|
1072 | if (filter->filterstring && ! filter->flag) { |
---|
1073 | pcap_t *pcap = NULL; |
---|
1074 | if (linktype==(libtrace_linktype_t)-1) { |
---|
1075 | trace_set_err(packet->trace, |
---|
1076 | TRACE_ERR_BAD_PACKET, |
---|
1077 | "Packet has an unknown linktype"); |
---|
1078 | return -1; |
---|
1079 | } |
---|
1080 | if (libtrace_to_pcap_dlt(linktype) == ~1U) { |
---|
1081 | trace_set_err(packet->trace,TRACE_ERR_BAD_PACKET, |
---|
1082 | "Unknown pcap equivalent linktype"); |
---|
1083 | return -1; |
---|
1084 | } |
---|
1085 | pcap=(pcap_t *)pcap_open_dead( |
---|
1086 | (int)libtrace_to_pcap_dlt(linktype), |
---|
1087 | 1500U); |
---|
1088 | /* build filter */ |
---|
1089 | assert(pcap); |
---|
1090 | if (pcap_compile( pcap, &filter->filter, filter->filterstring, |
---|
1091 | 1, 0)) { |
---|
1092 | trace_set_err(packet->trace,TRACE_ERR_BAD_PACKET, |
---|
1093 | "Unable to compile the filter \"%s\": %s", |
---|
1094 | filter->filterstring, |
---|
1095 | pcap_geterr(pcap)); |
---|
1096 | pcap_close(pcap); |
---|
1097 | return -1; |
---|
1098 | } |
---|
1099 | pcap_close(pcap); |
---|
1100 | filter->flag=1; |
---|
1101 | } |
---|
1102 | return 0; |
---|
1103 | #else |
---|
1104 | assert(!"Internal bug: This never be called when BPF not enabled"); |
---|
1105 | trace_set_err(packet->trace,TRACE_ERR_OPTION_UNAVAIL, |
---|
1106 | "Feature unavailable"); |
---|
1107 | return -1; |
---|
1108 | #endif |
---|
1109 | } |
---|
1110 | |
---|
1111 | DLLEXPORT int trace_apply_filter(libtrace_filter_t *filter, |
---|
1112 | const libtrace_packet_t *packet) { |
---|
1113 | #ifdef HAVE_BPF |
---|
1114 | void *linkptr = 0; |
---|
1115 | uint32_t clen = 0; |
---|
1116 | bool free_packet_needed = false; |
---|
1117 | int ret; |
---|
1118 | libtrace_packet_t *packet_copy = (libtrace_packet_t*)packet; |
---|
1119 | |
---|
1120 | assert(filter); |
---|
1121 | assert(packet); |
---|
1122 | |
---|
1123 | if (libtrace_to_pcap_dlt(trace_get_link_type(packet))==~0U) { |
---|
1124 | /* Copy the packet, as we don't want to trash the one we |
---|
1125 | * were passed in |
---|
1126 | */ |
---|
1127 | packet_copy=trace_copy_packet(packet); |
---|
1128 | free_packet_needed=true; |
---|
1129 | while (libtrace_to_pcap_dlt(trace_get_link_type(packet_copy))== |
---|
1130 | ~0U) { |
---|
1131 | if (!demote_packet(packet_copy)) { |
---|
1132 | trace_set_err(packet->trace, |
---|
1133 | TRACE_ERR_NO_CONVERSION, |
---|
1134 | "pcap does not support this format"); |
---|
1135 | if (free_packet_needed) { |
---|
1136 | trace_destroy_packet(packet_copy); |
---|
1137 | } |
---|
1138 | return -1; |
---|
1139 | } |
---|
1140 | } |
---|
1141 | } |
---|
1142 | |
---|
1143 | linkptr = trace_get_packet_buffer(packet_copy,NULL,&clen); |
---|
1144 | if (!linkptr) { |
---|
1145 | if (free_packet_needed) { |
---|
1146 | trace_destroy_packet(packet_copy); |
---|
1147 | } |
---|
1148 | return 0; |
---|
1149 | } |
---|
1150 | |
---|
1151 | /* We need to compile it now, because before we didn't know what the |
---|
1152 | * link type was |
---|
1153 | */ |
---|
1154 | if (trace_bpf_compile(filter,packet_copy)==-1) { |
---|
1155 | if (free_packet_needed) { |
---|
1156 | trace_destroy_packet(packet_copy); |
---|
1157 | } |
---|
1158 | return -1; |
---|
1159 | } |
---|
1160 | |
---|
1161 | assert(filter->flag); |
---|
1162 | ret=bpf_filter(filter->filter.bf_insns,(u_char*)linkptr,(unsigned int)clen,(unsigned int)clen); |
---|
1163 | if (free_packet_needed) { |
---|
1164 | trace_destroy_packet(packet_copy); |
---|
1165 | } |
---|
1166 | return ret; |
---|
1167 | #else |
---|
1168 | fprintf(stderr,"This version of libtrace does not have bpf filter support\n"); |
---|
1169 | return 0; |
---|
1170 | #endif |
---|
1171 | } |
---|
1172 | |
---|
1173 | /* Set the direction flag, if it has one |
---|
1174 | * @param packet the packet opaque pointer |
---|
1175 | * @param direction the new direction (0,1,2,3) |
---|
1176 | * @returns a signed value containing the direction flag, or -1 if this is not supported |
---|
1177 | */ |
---|
1178 | DLLEXPORT libtrace_direction_t trace_set_direction(libtrace_packet_t *packet, |
---|
1179 | libtrace_direction_t direction) |
---|
1180 | { |
---|
1181 | assert(packet); |
---|
1182 | if (packet->trace->format->set_direction) { |
---|
1183 | return packet->trace->format->set_direction(packet,direction); |
---|
1184 | } |
---|
1185 | return (libtrace_direction_t)~0U; |
---|
1186 | } |
---|
1187 | |
---|
1188 | /* Get the direction flag, if it has one |
---|
1189 | * @param packet a pointer to a libtrace_packet structure |
---|
1190 | * @returns a signed value containing the direction flag, or -1 if this is not supported |
---|
1191 | * The direction is defined as 0 for packets originating locally (ie, outbound) |
---|
1192 | * and 1 for packets originating remotely (ie, inbound). |
---|
1193 | * Other values are possible, which might be overloaded to mean special things |
---|
1194 | * for a special trace. |
---|
1195 | * @author Daniel Lawson |
---|
1196 | */ |
---|
1197 | DLLEXPORT libtrace_direction_t trace_get_direction(const libtrace_packet_t *packet) |
---|
1198 | { |
---|
1199 | assert(packet); |
---|
1200 | if (packet->trace->format->get_direction) { |
---|
1201 | return packet->trace->format->get_direction(packet); |
---|
1202 | } |
---|
1203 | return (libtrace_direction_t)~0U; |
---|
1204 | } |
---|
1205 | |
---|
1206 | #define ROOT_SERVER(x) ((x) < 512) |
---|
1207 | #define ROOT_CLIENT(x) ((512 <= (x)) && ((x) < 1024)) |
---|
1208 | #define NONROOT_SERVER(x) ((x) >= 5000) |
---|
1209 | #define NONROOT_CLIENT(x) ((1024 <= (x)) && ((x) < 5000)) |
---|
1210 | #define DYNAMIC(x) ((49152 < (x)) && ((x) < 65535)) |
---|
1211 | #define SERVER(x) ROOT_SERVER(x) || NONROOT_SERVER(x) |
---|
1212 | #define CLIENT(x) ROOT_CLIENT(x) || NONROOT_CLIENT(x) |
---|
1213 | |
---|
1214 | /* Attempt to deduce the 'server' port |
---|
1215 | * @param protocol the IP protocol (eg, 6 or 17 for TCP or UDP) |
---|
1216 | * @param source the TCP or UDP source port |
---|
1217 | * @param dest the TCP or UDP destination port |
---|
1218 | * @returns a hint as to which port is the server port |
---|
1219 | */ |
---|
1220 | DLLEXPORT int8_t trace_get_server_port(UNUSED uint8_t protocol, |
---|
1221 | uint16_t source, uint16_t dest) |
---|
1222 | { |
---|
1223 | /* |
---|
1224 | * * If the ports are equal, return DEST |
---|
1225 | * * Check for well-known ports in the given protocol |
---|
1226 | * * Root server ports: 0 - 511 |
---|
1227 | * * Root client ports: 512 - 1023 |
---|
1228 | * * non-root client ports: 1024 - 4999 |
---|
1229 | * * non-root server ports: 5000+ |
---|
1230 | * * Check for static ranges: 1024 - 49151 |
---|
1231 | * * Check for dynamic ranges: 49152 - 65535 |
---|
1232 | * * flip a coin. |
---|
1233 | */ |
---|
1234 | |
---|
1235 | /* equal */ |
---|
1236 | if (source == dest) |
---|
1237 | return USE_DEST; |
---|
1238 | |
---|
1239 | /* root server port, 0 - 511 */ |
---|
1240 | if (ROOT_SERVER(source) && ROOT_SERVER(dest)) { |
---|
1241 | if (source < dest) |
---|
1242 | return USE_SOURCE; |
---|
1243 | return USE_DEST; |
---|
1244 | } |
---|
1245 | |
---|
1246 | if (ROOT_SERVER(source) && !ROOT_SERVER(dest)) |
---|
1247 | return USE_SOURCE; |
---|
1248 | if (!ROOT_SERVER(source) && ROOT_SERVER(dest)) |
---|
1249 | return USE_DEST; |
---|
1250 | |
---|
1251 | /* non-root server */ |
---|
1252 | if (NONROOT_SERVER(source) && NONROOT_SERVER(dest)) { |
---|
1253 | if (source < dest) |
---|
1254 | return USE_SOURCE; |
---|
1255 | return USE_DEST; |
---|
1256 | } |
---|
1257 | if (NONROOT_SERVER(source) && !NONROOT_SERVER(dest)) |
---|
1258 | return USE_SOURCE; |
---|
1259 | if (!NONROOT_SERVER(source) && NONROOT_SERVER(dest)) |
---|
1260 | return USE_DEST; |
---|
1261 | |
---|
1262 | /* root client */ |
---|
1263 | if (ROOT_CLIENT(source) && ROOT_CLIENT(dest)) { |
---|
1264 | if (source < dest) |
---|
1265 | return USE_SOURCE; |
---|
1266 | return USE_DEST; |
---|
1267 | } |
---|
1268 | if (ROOT_CLIENT(source) && !ROOT_CLIENT(dest)) { |
---|
1269 | /* prefer root-client over nonroot-client */ |
---|
1270 | if (NONROOT_CLIENT(dest)) |
---|
1271 | return USE_SOURCE; |
---|
1272 | return USE_DEST; |
---|
1273 | } |
---|
1274 | if (!ROOT_CLIENT(source) && ROOT_CLIENT(dest)) { |
---|
1275 | /* prefer root-client over nonroot-client */ |
---|
1276 | if (NONROOT_CLIENT(source)) |
---|
1277 | return USE_DEST; |
---|
1278 | return USE_SOURCE; |
---|
1279 | } |
---|
1280 | |
---|
1281 | /* nonroot client */ |
---|
1282 | if (NONROOT_CLIENT(source) && NONROOT_CLIENT(dest)) { |
---|
1283 | if (source < dest) |
---|
1284 | return USE_SOURCE; |
---|
1285 | return USE_DEST; |
---|
1286 | } |
---|
1287 | if (NONROOT_CLIENT(source) && !NONROOT_CLIENT(dest)) |
---|
1288 | return USE_DEST; |
---|
1289 | if (!NONROOT_CLIENT(source) && NONROOT_CLIENT(dest)) |
---|
1290 | return USE_SOURCE; |
---|
1291 | |
---|
1292 | /* dynamic range */ |
---|
1293 | if (DYNAMIC(source) && DYNAMIC(dest)) { |
---|
1294 | if (source < dest) |
---|
1295 | return USE_SOURCE; |
---|
1296 | return USE_DEST; |
---|
1297 | } |
---|
1298 | if (DYNAMIC(source) && !DYNAMIC(dest)) |
---|
1299 | return USE_DEST; |
---|
1300 | if (!DYNAMIC(source) && DYNAMIC(dest)) |
---|
1301 | return USE_SOURCE; |
---|
1302 | /* |
---|
1303 | if (SERVER(source) && CLIENT(dest)) |
---|
1304 | return USE_SOURCE; |
---|
1305 | |
---|
1306 | if (SERVER(dest) && CLIENT(source)) |
---|
1307 | return USE_DEST; |
---|
1308 | if (ROOT_SERVER(source) && !ROOT_SERVER(dest)) |
---|
1309 | return USE_SOURCE; |
---|
1310 | if (ROOT_SERVER(dest) && !ROOT_SERVER(source)) |
---|
1311 | return USE_DEST; |
---|
1312 | */ |
---|
1313 | /* failing that test... */ |
---|
1314 | if (source < dest) { |
---|
1315 | return USE_SOURCE; |
---|
1316 | } |
---|
1317 | return USE_DEST; |
---|
1318 | |
---|
1319 | } |
---|
1320 | |
---|
1321 | /* Truncate the packet at the suggested length |
---|
1322 | * @param packet the packet opaque pointer |
---|
1323 | * @param size the new length of the packet |
---|
1324 | * @returns the new size of the packet |
---|
1325 | * @note size and the return size refer to the network-level payload of the |
---|
1326 | * packet, and do not include any capture headers. For example, to truncate a |
---|
1327 | * packet after the IP header, set size to sizeof(ethernet_header) + |
---|
1328 | * sizeof(ip_header) |
---|
1329 | * @note If the original network-level payload is smaller than size, then the |
---|
1330 | * original size is returned and the packet is left unchanged. |
---|
1331 | * @author Daniel Lawson |
---|
1332 | */ |
---|
1333 | DLLEXPORT size_t trace_set_capture_length(libtrace_packet_t *packet, size_t size) { |
---|
1334 | assert(packet); |
---|
1335 | |
---|
1336 | if (packet->trace->format->set_capture_length) { |
---|
1337 | return packet->trace->format->set_capture_length(packet,size); |
---|
1338 | } |
---|
1339 | |
---|
1340 | return ~0U; |
---|
1341 | } |
---|
1342 | |
---|
1343 | DLLEXPORT const char * trace_parse_uri(const char *uri, char **format) { |
---|
1344 | const char *uridata = 0; |
---|
1345 | |
---|
1346 | if((uridata = strchr(uri,':')) == NULL) { |
---|
1347 | /* badly formed URI - needs a : */ |
---|
1348 | return 0; |
---|
1349 | } |
---|
1350 | |
---|
1351 | if ((unsigned)(uridata - uri) > URI_PROTO_LINE) { |
---|
1352 | /* badly formed URI - uri type is too long */ |
---|
1353 | return 0; |
---|
1354 | } |
---|
1355 | |
---|
1356 | *format=xstrndup(uri, (size_t)(uridata - uri)); |
---|
1357 | |
---|
1358 | /* push uridata past the delimiter */ |
---|
1359 | uridata++; |
---|
1360 | |
---|
1361 | return uridata; |
---|
1362 | } |
---|
1363 | |
---|
1364 | enum base_format_t trace_get_format(libtrace_packet_t *packet) |
---|
1365 | { |
---|
1366 | assert(packet); |
---|
1367 | |
---|
1368 | return packet->trace->format->type; |
---|
1369 | } |
---|
1370 | |
---|
1371 | DLLEXPORT libtrace_err_t trace_get_err(libtrace_t *trace) |
---|
1372 | { |
---|
1373 | libtrace_err_t err = trace->err; |
---|
1374 | trace->err.err_num = 0; /* "OK" */ |
---|
1375 | trace->err.problem[0]='\0'; |
---|
1376 | return err; |
---|
1377 | } |
---|
1378 | |
---|
1379 | DLLEXPORT bool trace_is_err(libtrace_t *trace) |
---|
1380 | { |
---|
1381 | return trace->err.err_num != 0; |
---|
1382 | } |
---|
1383 | |
---|
1384 | DLLEXPORT void trace_perror(libtrace_t *trace,const char *msg,...) |
---|
1385 | { |
---|
1386 | char buf[256]; |
---|
1387 | va_list va; |
---|
1388 | va_start(va,msg); |
---|
1389 | vsnprintf(buf,sizeof(buf),msg,va); |
---|
1390 | va_end(va); |
---|
1391 | if(trace->err.err_num) { |
---|
1392 | fprintf(stderr,"%s(%s): %s\n", |
---|
1393 | buf,trace->uridata,trace->err.problem); |
---|
1394 | } else { |
---|
1395 | fprintf(stderr,"%s(%s): No error\n", |
---|
1396 | buf,trace->uridata); |
---|
1397 | } |
---|
1398 | trace->err.err_num = 0; /* "OK" */ |
---|
1399 | trace->err.problem[0]='\0'; |
---|
1400 | } |
---|
1401 | |
---|
1402 | DLLEXPORT libtrace_err_t trace_get_err_output(libtrace_out_t *trace) |
---|
1403 | { |
---|
1404 | libtrace_err_t err = trace->err; |
---|
1405 | trace->err.err_num = TRACE_ERR_NOERROR; /* "OK" */ |
---|
1406 | trace->err.problem[0]='\0'; |
---|
1407 | return err; |
---|
1408 | } |
---|
1409 | |
---|
1410 | DLLEXPORT bool trace_is_err_output(libtrace_out_t *trace) |
---|
1411 | { |
---|
1412 | return trace->err.err_num != 0; |
---|
1413 | } |
---|
1414 | |
---|
1415 | DLLEXPORT void trace_perror_output(libtrace_out_t *trace,const char *msg,...) |
---|
1416 | { |
---|
1417 | char buf[256]; |
---|
1418 | va_list va; |
---|
1419 | va_start(va,msg); |
---|
1420 | vsnprintf(buf,sizeof(buf),msg,va); |
---|
1421 | va_end(va); |
---|
1422 | if(trace->err.err_num) { |
---|
1423 | fprintf(stderr,"%s(%s): %s\n", |
---|
1424 | buf, |
---|
1425 | trace->uridata?trace->uridata:"no uri", |
---|
1426 | trace->err.problem); |
---|
1427 | } else { |
---|
1428 | fprintf(stderr,"%s(%s): No error\n",buf,trace->uridata); |
---|
1429 | } |
---|
1430 | trace->err.err_num = TRACE_ERR_NOERROR; /* "OK" */ |
---|
1431 | trace->err.problem[0]='\0'; |
---|
1432 | } |
---|
1433 | |
---|
1434 | DLLEXPORT int trace_seek_erf_timestamp(libtrace_t *trace, uint64_t ts) |
---|
1435 | { |
---|
1436 | if (trace->format->seek_erf) { |
---|
1437 | return trace->format->seek_erf(trace,ts); |
---|
1438 | } |
---|
1439 | else { |
---|
1440 | if (trace->format->seek_timeval) { |
---|
1441 | struct timeval tv; |
---|
1442 | #if __BYTE_ORDER == __BIG_ENDIAN |
---|
1443 | tv.tv_sec = ts & 0xFFFFFFFF; |
---|
1444 | #elif __BYTE_ORDER == __LITTLE_ENDIAN |
---|
1445 | tv.tv_sec = ts >> 32; |
---|
1446 | #else |
---|
1447 | #error "What on earth are you running this on?" |
---|
1448 | #endif |
---|
1449 | tv.tv_usec = ((ts&0xFFFFFFFF)*1000000)>>32; |
---|
1450 | if (tv.tv_usec >= 1000000) { |
---|
1451 | tv.tv_usec -= 1000000; |
---|
1452 | tv.tv_sec += 1; |
---|
1453 | } |
---|
1454 | return trace->format->seek_timeval(trace,tv); |
---|
1455 | } |
---|
1456 | if (trace->format->seek_seconds) { |
---|
1457 | double seconds = |
---|
1458 | (ts>>32) + ((ts & UINT_MAX)*1.0 / UINT_MAX); |
---|
1459 | return trace->format->seek_seconds(trace,seconds); |
---|
1460 | } |
---|
1461 | trace_set_err(trace, |
---|
1462 | TRACE_ERR_OPTION_UNAVAIL, |
---|
1463 | "Feature unimplemented"); |
---|
1464 | return -1; |
---|
1465 | } |
---|
1466 | } |
---|
1467 | |
---|
1468 | DLLEXPORT int trace_seek_seconds(libtrace_t *trace, double seconds) |
---|
1469 | { |
---|
1470 | if (trace->format->seek_seconds) { |
---|
1471 | return trace->format->seek_seconds(trace,seconds); |
---|
1472 | } |
---|
1473 | else { |
---|
1474 | if (trace->format->seek_timeval) { |
---|
1475 | struct timeval tv; |
---|
1476 | tv.tv_sec = (uint32_t)seconds; |
---|
1477 | tv.tv_usec = (uint32_t)(((seconds - tv.tv_sec) * 1000000)/UINT_MAX); |
---|
1478 | return trace->format->seek_timeval(trace,tv); |
---|
1479 | } |
---|
1480 | if (trace->format->seek_erf) { |
---|
1481 | uint64_t timestamp = |
---|
1482 | ((uint64_t)((uint32_t)seconds) << 32) + \ |
---|
1483 | (uint64_t)(( seconds - (uint32_t)seconds ) * UINT_MAX); |
---|
1484 | return trace->format->seek_erf(trace,timestamp); |
---|
1485 | } |
---|
1486 | trace_set_err(trace, |
---|
1487 | TRACE_ERR_OPTION_UNAVAIL, |
---|
1488 | "Feature unimplemented"); |
---|
1489 | return -1; |
---|
1490 | } |
---|
1491 | } |
---|
1492 | |
---|
1493 | DLLEXPORT int trace_seek_timeval(libtrace_t *trace, struct timeval tv) |
---|
1494 | { |
---|
1495 | if (trace->format->seek_timeval) { |
---|
1496 | return trace->format->seek_timeval(trace,tv); |
---|
1497 | } |
---|
1498 | else { |
---|
1499 | if (trace->format->seek_erf) { |
---|
1500 | uint64_t timestamp = ((((uint64_t)tv.tv_sec) << 32) + \ |
---|
1501 | (((uint64_t)tv.tv_usec * UINT_MAX)/1000000)); |
---|
1502 | return trace->format->seek_erf(trace,timestamp); |
---|
1503 | } |
---|
1504 | if (trace->format->seek_seconds) { |
---|
1505 | double seconds = tv.tv_sec + ((tv.tv_usec * 1.0)/1000000); |
---|
1506 | return trace->format->seek_seconds(trace,seconds); |
---|
1507 | } |
---|
1508 | trace_set_err(trace, |
---|
1509 | TRACE_ERR_OPTION_UNAVAIL, |
---|
1510 | "Feature unimplemented"); |
---|
1511 | return -1; |
---|
1512 | } |
---|
1513 | } |
---|
1514 | |
---|
1515 | DLLEXPORT char *trace_ether_ntoa(const uint8_t *addr, char *buf) |
---|
1516 | { |
---|
1517 | static char staticbuf[18]={0,}; |
---|
1518 | if (!buf) |
---|
1519 | buf=staticbuf; |
---|
1520 | snprintf(buf,(size_t)18,"%02x:%02x:%02x:%02x:%02x:%02x", |
---|
1521 | addr[0],addr[1],addr[2], |
---|
1522 | addr[3],addr[4],addr[5]); |
---|
1523 | return buf; |
---|
1524 | } |
---|
1525 | |
---|
1526 | DLLEXPORT uint8_t *trace_ether_aton(const char *buf, uint8_t *addr) |
---|
1527 | { |
---|
1528 | uint8_t *buf2 = addr; |
---|
1529 | unsigned int tmp[6]; |
---|
1530 | static uint8_t staticaddr[6]; |
---|
1531 | if (!buf2) |
---|
1532 | buf2=staticaddr; |
---|
1533 | sscanf(buf,"%x:%x:%x:%x:%x:%x", |
---|
1534 | &tmp[0],&tmp[1],&tmp[2], |
---|
1535 | &tmp[3],&tmp[4],&tmp[5]); |
---|
1536 | buf2[0]=tmp[0]; buf2[1]=tmp[1]; buf2[2]=tmp[2]; |
---|
1537 | buf2[3]=tmp[3]; buf2[4]=tmp[4]; buf2[5]=tmp[5]; |
---|
1538 | return buf2; |
---|
1539 | } |
---|
1540 | |
---|
1541 | DLLEXPORT |
---|
1542 | void trace_construct_packet(libtrace_packet_t *packet, |
---|
1543 | libtrace_linktype_t linktype, |
---|
1544 | const void *data, |
---|
1545 | uint16_t len) |
---|
1546 | { |
---|
1547 | size_t size; |
---|
1548 | static libtrace_t *deadtrace=NULL; |
---|
1549 | libtrace_pcapfile_pkt_hdr_t hdr; |
---|
1550 | #ifdef WIN32 |
---|
1551 | struct _timeb tstruct; |
---|
1552 | #else |
---|
1553 | struct timeval tv; |
---|
1554 | #endif |
---|
1555 | |
---|
1556 | if (NULL == deadtrace) |
---|
1557 | deadtrace=trace_create_dead("pcapfile"); |
---|
1558 | |
---|
1559 | #ifdef WIN32 |
---|
1560 | _ftime(&tstruct); |
---|
1561 | hdr.ts_sec=tstruct.time; |
---|
1562 | hdr.ts_usec=tstruct.millitm * 1000; |
---|
1563 | #else |
---|
1564 | gettimeofday(&tv,NULL); |
---|
1565 | hdr.ts_sec=tv.tv_sec; |
---|
1566 | hdr.ts_usec=tv.tv_usec; |
---|
1567 | #endif |
---|
1568 | |
---|
1569 | hdr.caplen=len; |
---|
1570 | hdr.wirelen=len; |
---|
1571 | |
---|
1572 | packet->trace=deadtrace; |
---|
1573 | size=len+sizeof(hdr); |
---|
1574 | if (packet->buf_control==TRACE_CTRL_PACKET) { |
---|
1575 | packet->buffer=realloc(packet->buffer,size); |
---|
1576 | } |
---|
1577 | else { |
---|
1578 | packet->buffer=malloc(size); |
---|
1579 | } |
---|
1580 | packet->buf_control=TRACE_CTRL_PACKET; |
---|
1581 | packet->header=packet->buffer; |
---|
1582 | packet->payload=(void*)((char*)packet->buffer+sizeof(hdr)); |
---|
1583 | memcpy(packet->header,&hdr,sizeof(hdr)); |
---|
1584 | memcpy(packet->payload,data,(size_t)len); |
---|
1585 | packet->type=pcap_linktype_to_rt(libtrace_to_pcap_linktype(linktype)); |
---|
1586 | } |
---|