1 | /* |
---|
2 | * This file is part of libtrace |
---|
3 | * |
---|
4 | * Copyright (c) 2007,2008,2009,2010 The University of Waikato, Hamilton, |
---|
5 | * New Zealand. |
---|
6 | * |
---|
7 | * Authors: Daniel Lawson |
---|
8 | * Perry Lorier |
---|
9 | * Shane Alcock |
---|
10 | * |
---|
11 | * All rights reserved. |
---|
12 | * |
---|
13 | * This code has been developed by the University of Waikato WAND |
---|
14 | * research group. For further information please see http://www.wand.net.nz/ |
---|
15 | * |
---|
16 | * libtrace is free software; you can redistribute it and/or modify |
---|
17 | * it under the terms of the GNU General Public License as published by |
---|
18 | * the Free Software Foundation; either version 2 of the License, or |
---|
19 | * (at your option) any later version. |
---|
20 | * |
---|
21 | * libtrace is distributed in the hope that it will be useful, |
---|
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
24 | * GNU General Public License for more details. |
---|
25 | * |
---|
26 | * You should have received a copy of the GNU General Public License |
---|
27 | * along with libtrace; if not, write to the Free Software |
---|
28 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
29 | * |
---|
30 | * $Id$ |
---|
31 | * |
---|
32 | */ |
---|
33 | |
---|
34 | |
---|
35 | #define _GNU_SOURCE |
---|
36 | #include "common.h" |
---|
37 | #include "config.h" |
---|
38 | #include <assert.h> |
---|
39 | #include <errno.h> |
---|
40 | #include <fcntl.h> |
---|
41 | #include <stdio.h> |
---|
42 | #include <stdlib.h> |
---|
43 | #include <string.h> |
---|
44 | #include <sys/stat.h> |
---|
45 | #include <sys/types.h> |
---|
46 | #ifndef WIN32 |
---|
47 | #include <sys/socket.h> |
---|
48 | #endif |
---|
49 | #include <stdarg.h> |
---|
50 | #include <sys/param.h> |
---|
51 | |
---|
52 | #ifdef HAVE_LIMITS_H |
---|
53 | # include <limits.h> |
---|
54 | #endif |
---|
55 | |
---|
56 | #ifdef HAVE_SYS_LIMITS_H |
---|
57 | # include <sys/limits.h> |
---|
58 | #endif |
---|
59 | |
---|
60 | #ifdef HAVE_NET_IF_ARP_H |
---|
61 | # include <net/if_arp.h> |
---|
62 | #endif |
---|
63 | |
---|
64 | #ifdef HAVE_NET_IF_H |
---|
65 | # include <net/if.h> |
---|
66 | #endif |
---|
67 | |
---|
68 | #ifdef HAVE_NETINET_IN_H |
---|
69 | # include <netinet/in.h> |
---|
70 | #endif |
---|
71 | |
---|
72 | #ifdef HAVE_NET_ETHERNET_H |
---|
73 | # include <net/ethernet.h> |
---|
74 | #endif |
---|
75 | |
---|
76 | #ifdef HAVE_NETINET_IF_ETHER_H |
---|
77 | # include <netinet/if_ether.h> |
---|
78 | #endif |
---|
79 | |
---|
80 | #include <time.h> |
---|
81 | #ifdef WIN32 |
---|
82 | #include <sys/timeb.h> |
---|
83 | #endif |
---|
84 | |
---|
85 | #include "libtrace.h" |
---|
86 | #include "libtrace_int.h" |
---|
87 | |
---|
88 | #ifdef HAVE_PCAP_BPF_H |
---|
89 | # include <pcap-bpf.h> |
---|
90 | #else |
---|
91 | # ifdef HAVE_NET_BPF_H |
---|
92 | # include <net/bpf.h> |
---|
93 | # endif |
---|
94 | #endif |
---|
95 | |
---|
96 | |
---|
97 | #include "libtrace_int.h" |
---|
98 | #include "format_helper.h" |
---|
99 | #include "rt_protocol.h" |
---|
100 | |
---|
101 | #define MAXOPTS 1024 |
---|
102 | |
---|
103 | /* This file contains much of the implementation of the libtrace API itself. */ |
---|
104 | |
---|
105 | static struct libtrace_format_t *formats_list = NULL; |
---|
106 | |
---|
107 | volatile int libtrace_halt = 0; |
---|
108 | |
---|
109 | /* strncpy is not assured to copy the final \0, so we |
---|
110 | * will use our own one that does |
---|
111 | */ |
---|
112 | static void xstrncpy(char *dest, const char *src, size_t n) |
---|
113 | { |
---|
114 | strncpy(dest,src,n); |
---|
115 | dest[n]='\0'; |
---|
116 | } |
---|
117 | |
---|
118 | static char *xstrndup(const char *src,size_t n) |
---|
119 | { |
---|
120 | char *ret=(char*)malloc(n+1); |
---|
121 | if (ret==NULL) { |
---|
122 | fprintf(stderr,"Out of memory"); |
---|
123 | exit(EXIT_FAILURE); |
---|
124 | } |
---|
125 | xstrncpy(ret,src,n); |
---|
126 | return ret; |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | /* call all the constructors if they haven't yet all been called */ |
---|
131 | static void trace_init(void) |
---|
132 | { |
---|
133 | if (!formats_list) { |
---|
134 | duck_constructor(); |
---|
135 | erf_constructor(); |
---|
136 | tsh_constructor(); |
---|
137 | legacy_constructor(); |
---|
138 | atmhdr_constructor(); |
---|
139 | linuxnative_constructor(); |
---|
140 | #ifdef HAVE_LIBPCAP |
---|
141 | pcap_constructor(); |
---|
142 | #endif |
---|
143 | bpf_constructor(); |
---|
144 | pcapfile_constructor(); |
---|
145 | rt_constructor(); |
---|
146 | #ifdef HAVE_DAG |
---|
147 | dag_constructor(); |
---|
148 | #endif |
---|
149 | #ifdef HAVE_DPDK |
---|
150 | dpdk_constructor(); |
---|
151 | #endif |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | /* Prints help information for libtrace |
---|
156 | * |
---|
157 | * Function prints out some basic help information regarding libtrace, |
---|
158 | * and then prints out the help() function registered with each input module |
---|
159 | */ |
---|
160 | DLLEXPORT void trace_help(void) { |
---|
161 | struct libtrace_format_t *tmp; |
---|
162 | trace_init(); |
---|
163 | printf("libtrace %s\n\n",PACKAGE_VERSION); |
---|
164 | printf("Following this are a list of the format modules supported in this build of libtrace\n\n"); |
---|
165 | for(tmp=formats_list;tmp;tmp=tmp->next) { |
---|
166 | if (tmp->help) |
---|
167 | tmp->help(); |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | #define URI_PROTO_LINE 16U |
---|
172 | |
---|
173 | /* Try to guess which format module is appropriate for a given trace file or |
---|
174 | * device */ |
---|
175 | static void guess_format(libtrace_t *libtrace, const char *filename) |
---|
176 | { |
---|
177 | struct libtrace_format_t *tmp; |
---|
178 | |
---|
179 | /* Try and guess based on filename */ |
---|
180 | for(tmp = formats_list; tmp; tmp=tmp->next) { |
---|
181 | if (tmp->probe_filename && tmp->probe_filename(filename)) { |
---|
182 | libtrace->format = tmp; |
---|
183 | libtrace->uridata = strdup(filename); |
---|
184 | return; |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | libtrace->io = wandio_create(filename); |
---|
189 | if (!libtrace->io) |
---|
190 | return; |
---|
191 | |
---|
192 | /* Try and guess based on file magic */ |
---|
193 | for(tmp = formats_list; tmp; tmp=tmp->next) { |
---|
194 | if (tmp->probe_magic && tmp->probe_magic(libtrace->io)) { |
---|
195 | libtrace->format = tmp; |
---|
196 | libtrace->uridata = strdup(filename); |
---|
197 | return; |
---|
198 | } |
---|
199 | } |
---|
200 | |
---|
201 | /* No formats matched -- make sure we clean up the IO object we |
---|
202 | * used to probe the file magic */ |
---|
203 | wandio_destroy(libtrace->io); |
---|
204 | return; |
---|
205 | } |
---|
206 | |
---|
207 | /* Creates an input trace from a URI |
---|
208 | * |
---|
209 | * @params char * containing a valid libtrace URI |
---|
210 | * @returns opaque pointer to a libtrace_t |
---|
211 | * |
---|
212 | * Some valid URI's are: |
---|
213 | * erf:/path/to/erf/file |
---|
214 | * erf:/path/to/erf/file.gz |
---|
215 | * erf:- (stdin) |
---|
216 | * dag:/dev/dagcard |
---|
217 | * pcapint:pcapinterface (eg: pcapint:eth0) |
---|
218 | * pcapfile:/path/to/pcap/file |
---|
219 | * pcapfile:- |
---|
220 | * int:interface (eg: int:eth0) only on Linux |
---|
221 | * rt:hostname |
---|
222 | * rt:hostname:port |
---|
223 | * |
---|
224 | * If an error occured when attempting to open a trace, NULL is returned |
---|
225 | * and an error is output to stdout. |
---|
226 | */ |
---|
227 | DLLEXPORT libtrace_t *trace_create(const char *uri) { |
---|
228 | libtrace_t *libtrace = |
---|
229 | (libtrace_t *)malloc(sizeof(libtrace_t)); |
---|
230 | char *scan = 0; |
---|
231 | const char *uridata = 0; |
---|
232 | |
---|
233 | trace_init(); |
---|
234 | |
---|
235 | assert(uri && "Passing NULL to trace_create makes me a very sad program"); |
---|
236 | |
---|
237 | if (!libtrace) { |
---|
238 | /* Out of memory */ |
---|
239 | return NULL; |
---|
240 | } |
---|
241 | |
---|
242 | libtrace->err.err_num = TRACE_ERR_NOERROR; |
---|
243 | libtrace->format=NULL; |
---|
244 | |
---|
245 | libtrace->event.tdelta = 0.0; |
---|
246 | libtrace->event.packet = NULL; |
---|
247 | libtrace->event.psize = 0; |
---|
248 | libtrace->event.trace_last_ts = 0.0; |
---|
249 | libtrace->event.waiting = false; |
---|
250 | libtrace->filter = NULL; |
---|
251 | libtrace->snaplen = 0; |
---|
252 | libtrace->started=false; |
---|
253 | libtrace->uridata = NULL; |
---|
254 | libtrace->io = NULL; |
---|
255 | libtrace->filtered_packets = 0; |
---|
256 | libtrace->accepted_packets = 0; |
---|
257 | |
---|
258 | /* Parse the URI to determine what sort of trace we are dealing with */ |
---|
259 | if ((uridata = trace_parse_uri(uri, &scan)) == 0) { |
---|
260 | /* Could not parse the URI nicely */ |
---|
261 | guess_format(libtrace,uri); |
---|
262 | if (!libtrace->format) { |
---|
263 | trace_set_err(libtrace,TRACE_ERR_BAD_FORMAT,"Unable to guess format (%s)",uri); |
---|
264 | return libtrace; |
---|
265 | } |
---|
266 | } |
---|
267 | else { |
---|
268 | struct libtrace_format_t *tmp; |
---|
269 | |
---|
270 | /* Find a format that matches the first part of the URI */ |
---|
271 | for (tmp=formats_list;tmp;tmp=tmp->next) { |
---|
272 | if (strlen(scan) == strlen(tmp->name) && |
---|
273 | strncasecmp(scan, tmp->name, strlen(scan)) == 0 |
---|
274 | ) { |
---|
275 | libtrace->format=tmp; |
---|
276 | break; |
---|
277 | } |
---|
278 | } |
---|
279 | |
---|
280 | if (libtrace->format == 0) { |
---|
281 | trace_set_err(libtrace, TRACE_ERR_BAD_FORMAT, |
---|
282 | "Unknown format (%s)",scan); |
---|
283 | return libtrace; |
---|
284 | } |
---|
285 | |
---|
286 | libtrace->uridata = strdup(uridata); |
---|
287 | } |
---|
288 | /* libtrace->format now contains the type of uri |
---|
289 | * libtrace->uridata contains the appropriate data for this |
---|
290 | */ |
---|
291 | |
---|
292 | /* Call the init_input function for the matching capture format */ |
---|
293 | if (libtrace->format->init_input) { |
---|
294 | int err=libtrace->format->init_input(libtrace); |
---|
295 | assert (err==-1 || err==0); |
---|
296 | if (err==-1) { |
---|
297 | /* init_input should call trace_set_err to set |
---|
298 | * the error message |
---|
299 | */ |
---|
300 | return libtrace; |
---|
301 | } |
---|
302 | } else { |
---|
303 | trace_set_err(libtrace,TRACE_ERR_UNSUPPORTED, |
---|
304 | "Format does not support input (%s)",scan); |
---|
305 | return libtrace; |
---|
306 | } |
---|
307 | |
---|
308 | if (scan) |
---|
309 | free(scan); |
---|
310 | libtrace->err.err_num=TRACE_ERR_NOERROR; |
---|
311 | libtrace->err.problem[0]='\0'; |
---|
312 | return libtrace; |
---|
313 | } |
---|
314 | |
---|
315 | /* Creates a "dummy" trace file that has only the format type set. |
---|
316 | * |
---|
317 | * @returns opaque pointer to a (sparsely initialised) libtrace_t |
---|
318 | * |
---|
319 | * IMPORTANT: Do not attempt to call trace_read_packet or other such functions |
---|
320 | * with the dummy trace. Its intended purpose is to act as a packet->trace for |
---|
321 | * libtrace_packet_t's that are not associated with a libtrace_t structure. |
---|
322 | */ |
---|
323 | DLLEXPORT libtrace_t * trace_create_dead (const char *uri) { |
---|
324 | libtrace_t *libtrace = (libtrace_t *) malloc(sizeof(libtrace_t)); |
---|
325 | char *scan = (char *)calloc(sizeof(char),URI_PROTO_LINE); |
---|
326 | char *uridata; |
---|
327 | struct libtrace_format_t *tmp; |
---|
328 | |
---|
329 | trace_init(); |
---|
330 | |
---|
331 | libtrace->err.err_num = TRACE_ERR_NOERROR; |
---|
332 | |
---|
333 | if((uridata = strchr(uri,':')) == NULL) { |
---|
334 | xstrncpy(scan, uri, strlen(uri)); |
---|
335 | } else { |
---|
336 | xstrncpy(scan,uri, (size_t)(uridata - uri)); |
---|
337 | } |
---|
338 | |
---|
339 | libtrace->err.err_num = TRACE_ERR_NOERROR; |
---|
340 | libtrace->format=NULL; |
---|
341 | |
---|
342 | libtrace->event.tdelta = 0.0; |
---|
343 | libtrace->event.packet = NULL; |
---|
344 | libtrace->event.psize = 0; |
---|
345 | libtrace->event.trace_last_ts = 0.0; |
---|
346 | libtrace->filter = NULL; |
---|
347 | libtrace->snaplen = 0; |
---|
348 | libtrace->started=false; |
---|
349 | libtrace->uridata = NULL; |
---|
350 | libtrace->io = NULL; |
---|
351 | libtrace->filtered_packets = 0; |
---|
352 | |
---|
353 | for(tmp=formats_list;tmp;tmp=tmp->next) { |
---|
354 | if (strlen(scan) == strlen(tmp->name) && |
---|
355 | !strncasecmp(scan, |
---|
356 | tmp->name, |
---|
357 | strlen(scan))) { |
---|
358 | libtrace->format=tmp; |
---|
359 | break; |
---|
360 | } |
---|
361 | } |
---|
362 | if (libtrace->format == 0) { |
---|
363 | trace_set_err(libtrace,TRACE_ERR_BAD_FORMAT, |
---|
364 | "Unknown format (%s)",scan); |
---|
365 | } |
---|
366 | |
---|
367 | libtrace->format_data = NULL; |
---|
368 | free(scan); |
---|
369 | return libtrace; |
---|
370 | |
---|
371 | } |
---|
372 | |
---|
373 | /* Creates an output trace from a URI. |
---|
374 | * |
---|
375 | * @param uri the uri string describing the output format and destination |
---|
376 | * @returns opaque pointer to a libtrace_output_t |
---|
377 | * |
---|
378 | * If an error occured when attempting to open the output trace, NULL is |
---|
379 | * returned and trace_errno is set. |
---|
380 | */ |
---|
381 | |
---|
382 | DLLEXPORT libtrace_out_t *trace_create_output(const char *uri) { |
---|
383 | libtrace_out_t *libtrace = |
---|
384 | (libtrace_out_t*)malloc(sizeof(libtrace_out_t)); |
---|
385 | |
---|
386 | char *scan = 0; |
---|
387 | const char *uridata = 0; |
---|
388 | struct libtrace_format_t *tmp; |
---|
389 | |
---|
390 | trace_init(); |
---|
391 | |
---|
392 | libtrace->err.err_num = TRACE_ERR_NOERROR; |
---|
393 | strcpy(libtrace->err.problem,"Error message set\n"); |
---|
394 | libtrace->format = NULL; |
---|
395 | libtrace->uridata = NULL; |
---|
396 | |
---|
397 | /* Parse the URI to determine what capture format we want to write */ |
---|
398 | |
---|
399 | if ((uridata = trace_parse_uri(uri, &scan)) == 0) { |
---|
400 | trace_set_err_out(libtrace,TRACE_ERR_BAD_FORMAT, |
---|
401 | "Bad uri format (%s)",uri); |
---|
402 | return libtrace; |
---|
403 | } |
---|
404 | |
---|
405 | /* Attempt to find the format in the list of supported formats */ |
---|
406 | for(tmp=formats_list;tmp;tmp=tmp->next) { |
---|
407 | if (strlen(scan) == strlen(tmp->name) && |
---|
408 | !strncasecmp(scan, |
---|
409 | tmp->name, |
---|
410 | strlen(scan))) { |
---|
411 | libtrace->format=tmp; |
---|
412 | break; |
---|
413 | } |
---|
414 | } |
---|
415 | free(scan); |
---|
416 | |
---|
417 | if (libtrace->format == NULL) { |
---|
418 | trace_set_err_out(libtrace,TRACE_ERR_BAD_FORMAT, |
---|
419 | "Unknown output format (%s)",scan); |
---|
420 | return libtrace; |
---|
421 | } |
---|
422 | libtrace->uridata = strdup(uridata); |
---|
423 | |
---|
424 | /* libtrace->format now contains the type of uri |
---|
425 | * libtrace->uridata contains the appropriate data for this |
---|
426 | */ |
---|
427 | |
---|
428 | if (libtrace->format->init_output) { |
---|
429 | /* 0 on success, -1 on failure */ |
---|
430 | switch(libtrace->format->init_output(libtrace)) { |
---|
431 | case -1: /* failure */ |
---|
432 | return libtrace; |
---|
433 | case 0: /* success */ |
---|
434 | break; |
---|
435 | default: |
---|
436 | assert(!"Internal error: init_output() should return -1 for failure, or 0 for success"); |
---|
437 | } |
---|
438 | } else { |
---|
439 | trace_set_err_out(libtrace,TRACE_ERR_UNSUPPORTED, |
---|
440 | "Format does not support writing (%s)",scan); |
---|
441 | return libtrace; |
---|
442 | } |
---|
443 | |
---|
444 | |
---|
445 | libtrace->started=false; |
---|
446 | return libtrace; |
---|
447 | } |
---|
448 | |
---|
449 | /* Start an input trace |
---|
450 | * @param libtrace the input trace to start |
---|
451 | * @returns 0 on success |
---|
452 | * |
---|
453 | * This does the work associated with actually starting up |
---|
454 | * the trace. it may fail. |
---|
455 | */ |
---|
456 | DLLEXPORT int trace_start(libtrace_t *libtrace) |
---|
457 | { |
---|
458 | assert(libtrace); |
---|
459 | if (trace_is_err(libtrace)) |
---|
460 | return -1; |
---|
461 | if (libtrace->format->start_input) { |
---|
462 | int ret=libtrace->format->start_input(libtrace); |
---|
463 | if (ret < 0) { |
---|
464 | return ret; |
---|
465 | } |
---|
466 | } |
---|
467 | |
---|
468 | libtrace->started=true; |
---|
469 | return 0; |
---|
470 | } |
---|
471 | |
---|
472 | /* Start an output trace */ |
---|
473 | DLLEXPORT int trace_start_output(libtrace_out_t *libtrace) |
---|
474 | { |
---|
475 | assert(libtrace); |
---|
476 | if (libtrace->format->start_output) { |
---|
477 | int ret=libtrace->format->start_output(libtrace); |
---|
478 | if (ret < 0) { |
---|
479 | return ret; |
---|
480 | } |
---|
481 | } |
---|
482 | |
---|
483 | libtrace->started=true; |
---|
484 | return 0; |
---|
485 | } |
---|
486 | |
---|
487 | DLLEXPORT int trace_pause(libtrace_t *libtrace) |
---|
488 | { |
---|
489 | assert(libtrace); |
---|
490 | if (!libtrace->started) { |
---|
491 | trace_set_err(libtrace,TRACE_ERR_BAD_STATE, "You must call trace_start() before calling trace_pause()"); |
---|
492 | return -1; |
---|
493 | } |
---|
494 | if (libtrace->format->pause_input) |
---|
495 | libtrace->format->pause_input(libtrace); |
---|
496 | libtrace->started=false; |
---|
497 | return 0; |
---|
498 | } |
---|
499 | |
---|
500 | DLLEXPORT int trace_config(libtrace_t *libtrace, |
---|
501 | trace_option_t option, |
---|
502 | void *value) |
---|
503 | { |
---|
504 | int ret; |
---|
505 | |
---|
506 | if (trace_is_err(libtrace)) { |
---|
507 | return -1; |
---|
508 | } |
---|
509 | |
---|
510 | /* If the capture format supports configuration, try using their |
---|
511 | * native configuration first */ |
---|
512 | if (libtrace->format->config_input) { |
---|
513 | ret=libtrace->format->config_input(libtrace,option,value); |
---|
514 | if (ret==0) |
---|
515 | return 0; |
---|
516 | } |
---|
517 | |
---|
518 | /* If we get here, either the native configuration failed or the |
---|
519 | * format did not support configuration. However, libtrace can |
---|
520 | * deal with some options itself, so give that a go */ |
---|
521 | switch(option) { |
---|
522 | case TRACE_OPTION_SNAPLEN: |
---|
523 | /* Clear the error if there was one */ |
---|
524 | if (trace_is_err(libtrace)) { |
---|
525 | trace_get_err(libtrace); |
---|
526 | } |
---|
527 | if (*(int*)value<0 |
---|
528 | || *(int*)value>LIBTRACE_PACKET_BUFSIZE) { |
---|
529 | trace_set_err(libtrace,TRACE_ERR_BAD_STATE, |
---|
530 | "Invalid snap length"); |
---|
531 | } |
---|
532 | libtrace->snaplen=*(int*)value; |
---|
533 | return 0; |
---|
534 | case TRACE_OPTION_FILTER: |
---|
535 | /* Clear the error if there was one */ |
---|
536 | if (trace_is_err(libtrace)) { |
---|
537 | trace_get_err(libtrace); |
---|
538 | } |
---|
539 | libtrace->filter=(libtrace_filter_t *)value; |
---|
540 | return 0; |
---|
541 | case TRACE_OPTION_PROMISC: |
---|
542 | if (!trace_is_err(libtrace)) { |
---|
543 | trace_set_err(libtrace,TRACE_ERR_OPTION_UNAVAIL, |
---|
544 | "Promisc mode is not supported by this format module"); |
---|
545 | } |
---|
546 | return -1; |
---|
547 | case TRACE_OPTION_META_FREQ: |
---|
548 | if (!trace_is_err(libtrace)) { |
---|
549 | trace_set_err(libtrace, |
---|
550 | TRACE_ERR_OPTION_UNAVAIL, |
---|
551 | "This format does not support meta-data gathering"); |
---|
552 | } |
---|
553 | return -1; |
---|
554 | case TRACE_OPTION_EVENT_REALTIME: |
---|
555 | if (!trace_is_err(libtrace)) { |
---|
556 | trace_set_err(libtrace, |
---|
557 | TRACE_ERR_OPTION_UNAVAIL, |
---|
558 | "This format does not support realtime events"); |
---|
559 | } |
---|
560 | return -1; |
---|
561 | |
---|
562 | } |
---|
563 | if (!trace_is_err(libtrace)) { |
---|
564 | trace_set_err(libtrace,TRACE_ERR_UNKNOWN_OPTION, |
---|
565 | "Unknown option %i", option); |
---|
566 | } |
---|
567 | return -1; |
---|
568 | } |
---|
569 | |
---|
570 | DLLEXPORT int trace_config_output(libtrace_out_t *libtrace, |
---|
571 | trace_option_output_t option, |
---|
572 | void *value) { |
---|
573 | |
---|
574 | /* Unlike the input options, libtrace does not natively support any of |
---|
575 | * the output options - the format module must be able to deal with |
---|
576 | * them. */ |
---|
577 | if (libtrace->format->config_output) { |
---|
578 | return libtrace->format->config_output(libtrace, option, value); |
---|
579 | } |
---|
580 | return -1; |
---|
581 | } |
---|
582 | |
---|
583 | /* Close an input trace file, freeing up any resources it may have been using |
---|
584 | * |
---|
585 | */ |
---|
586 | DLLEXPORT void trace_destroy(libtrace_t *libtrace) { |
---|
587 | assert(libtrace); |
---|
588 | if (libtrace->format) { |
---|
589 | if (libtrace->started && libtrace->format->pause_input) |
---|
590 | libtrace->format->pause_input(libtrace); |
---|
591 | if (libtrace->format->fin_input) |
---|
592 | libtrace->format->fin_input(libtrace); |
---|
593 | } |
---|
594 | /* Need to free things! */ |
---|
595 | if (libtrace->uridata) |
---|
596 | free(libtrace->uridata); |
---|
597 | if (libtrace->event.packet) { |
---|
598 | /* Don't use trace_destroy_packet here - there is almost |
---|
599 | * certainly going to be another libtrace_packet_t that is |
---|
600 | * pointing to the buffer for this packet, so we don't want |
---|
601 | * to free it. Rather, it will get freed when the user calls |
---|
602 | * trace_destroy_packet on the libtrace_packet_t that they |
---|
603 | * own. |
---|
604 | * |
---|
605 | * All we need to do then is free our packet structure itself. |
---|
606 | */ |
---|
607 | free(libtrace->event.packet); |
---|
608 | } |
---|
609 | free(libtrace); |
---|
610 | } |
---|
611 | |
---|
612 | |
---|
613 | DLLEXPORT void trace_destroy_dead(libtrace_t *libtrace) { |
---|
614 | assert(libtrace); |
---|
615 | |
---|
616 | /* Don't call pause_input or fin_input, because we should never have |
---|
617 | * used this trace to do any reading anyway. Do make sure we free |
---|
618 | * any format_data that has been created, though. */ |
---|
619 | if (libtrace->format_data) |
---|
620 | free(libtrace->format_data); |
---|
621 | free(libtrace); |
---|
622 | } |
---|
623 | /* Close an output trace file, freeing up any resources it may have been using |
---|
624 | * |
---|
625 | * @param libtrace the output trace file to be destroyed |
---|
626 | */ |
---|
627 | DLLEXPORT void trace_destroy_output(libtrace_out_t *libtrace) |
---|
628 | { |
---|
629 | assert(libtrace); |
---|
630 | if (libtrace->format && libtrace->format->fin_output) |
---|
631 | libtrace->format->fin_output(libtrace); |
---|
632 | if (libtrace->uridata) |
---|
633 | free(libtrace->uridata); |
---|
634 | free(libtrace); |
---|
635 | } |
---|
636 | |
---|
637 | DLLEXPORT libtrace_packet_t *trace_create_packet(void) |
---|
638 | { |
---|
639 | libtrace_packet_t *packet = |
---|
640 | (libtrace_packet_t*)calloc((size_t)1,sizeof(libtrace_packet_t)); |
---|
641 | |
---|
642 | packet->buf_control=TRACE_CTRL_PACKET; |
---|
643 | trace_clear_cache(packet); |
---|
644 | return packet; |
---|
645 | } |
---|
646 | |
---|
647 | DLLEXPORT libtrace_packet_t *trace_copy_packet(const libtrace_packet_t *packet) { |
---|
648 | libtrace_packet_t *dest = |
---|
649 | (libtrace_packet_t *)malloc(sizeof(libtrace_packet_t)); |
---|
650 | if (!dest) { |
---|
651 | printf("Out of memory constructing packet\n"); |
---|
652 | abort(); |
---|
653 | } |
---|
654 | dest->trace=packet->trace; |
---|
655 | dest->buffer=malloc(65536); |
---|
656 | if (!dest->buffer) { |
---|
657 | printf("Out of memory allocating buffer memory\n"); |
---|
658 | abort(); |
---|
659 | } |
---|
660 | dest->header=dest->buffer; |
---|
661 | dest->payload=(void*) |
---|
662 | ((char*)dest->buffer+trace_get_framing_length(packet)); |
---|
663 | dest->type=packet->type; |
---|
664 | dest->buf_control=TRACE_CTRL_PACKET; |
---|
665 | /* Reset the cache - better to recalculate than try to convert |
---|
666 | * the values over to the new packet */ |
---|
667 | trace_clear_cache(dest); |
---|
668 | /* Ooooh nasty memcpys! This is why we want to avoid copying packets |
---|
669 | * as much as possible */ |
---|
670 | memcpy(dest->header,packet->header,trace_get_framing_length(packet)); |
---|
671 | memcpy(dest->payload,packet->payload,trace_get_capture_length(packet)); |
---|
672 | |
---|
673 | return dest; |
---|
674 | } |
---|
675 | |
---|
676 | /** Destroy a packet object |
---|
677 | */ |
---|
678 | DLLEXPORT void trace_destroy_packet(libtrace_packet_t *packet) { |
---|
679 | if (packet->buf_control == TRACE_CTRL_PACKET && packet->buffer) { |
---|
680 | free(packet->buffer); |
---|
681 | } |
---|
682 | packet->buf_control=(buf_control_t)'\0'; |
---|
683 | /* A "bad" value to force an assert |
---|
684 | * if this packet is ever reused |
---|
685 | */ |
---|
686 | free(packet); |
---|
687 | } |
---|
688 | |
---|
689 | /* Read one packet from the trace into buffer. Note that this function will |
---|
690 | * block until a packet is read (or EOF is reached). |
---|
691 | * |
---|
692 | * @param libtrace the libtrace opaque pointer |
---|
693 | * @param packet the packet opaque pointer |
---|
694 | * @returns 0 on EOF, negative value on error |
---|
695 | * |
---|
696 | */ |
---|
697 | DLLEXPORT int trace_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { |
---|
698 | |
---|
699 | assert(libtrace && "You called trace_read_packet() with a NULL libtrace parameter!\n"); |
---|
700 | if (trace_is_err(libtrace)) |
---|
701 | return -1; |
---|
702 | if (!libtrace->started) { |
---|
703 | trace_set_err(libtrace,TRACE_ERR_BAD_STATE,"You must call libtrace_start() before trace_read_packet()\n"); |
---|
704 | return -1; |
---|
705 | } |
---|
706 | if (!(packet->buf_control==TRACE_CTRL_PACKET || packet->buf_control==TRACE_CTRL_EXTERNAL)) { |
---|
707 | trace_set_err(libtrace,TRACE_ERR_BAD_STATE,"Packet passed to trace_read_packet() is invalid\n"); |
---|
708 | return -1; |
---|
709 | } |
---|
710 | assert(packet); |
---|
711 | |
---|
712 | /* Store the trace we are reading from into the packet opaque |
---|
713 | * structure */ |
---|
714 | packet->trace = libtrace; |
---|
715 | |
---|
716 | /* Finalise the packet, freeing any resources the format module |
---|
717 | * may have allocated it |
---|
718 | */ |
---|
719 | if (libtrace->format->fin_packet) { |
---|
720 | libtrace->format->fin_packet(packet); |
---|
721 | } |
---|
722 | |
---|
723 | |
---|
724 | if (libtrace->format->read_packet) { |
---|
725 | do { |
---|
726 | size_t ret; |
---|
727 | int filtret; |
---|
728 | /* Clear the packet cache */ |
---|
729 | trace_clear_cache(packet); |
---|
730 | ret=libtrace->format->read_packet(libtrace,packet); |
---|
731 | if (ret==(size_t)-1 || ret==0) { |
---|
732 | return ret; |
---|
733 | } |
---|
734 | if (libtrace->filter) { |
---|
735 | /* If the filter doesn't match, read another |
---|
736 | * packet |
---|
737 | */ |
---|
738 | filtret = trace_apply_filter(libtrace->filter, packet); |
---|
739 | if (filtret == -1) { |
---|
740 | /* Error compiling filter, probably */ |
---|
741 | return ~0U; |
---|
742 | } |
---|
743 | |
---|
744 | if (filtret == 0) { |
---|
745 | ++libtrace->filtered_packets; |
---|
746 | continue; |
---|
747 | } |
---|
748 | } |
---|
749 | if (libtrace->snaplen>0) { |
---|
750 | /* Snap the packet */ |
---|
751 | trace_set_capture_length(packet, |
---|
752 | libtrace->snaplen); |
---|
753 | } |
---|
754 | ++libtrace->accepted_packets; |
---|
755 | return ret; |
---|
756 | } while(1); |
---|
757 | } |
---|
758 | trace_set_err(libtrace,TRACE_ERR_UNSUPPORTED,"This format does not support reading packets\n"); |
---|
759 | return ~0U; |
---|
760 | } |
---|
761 | |
---|
762 | /* Converts the provided buffer into a libtrace packet of the given type. |
---|
763 | * |
---|
764 | * Unlike trace_construct_packet, the buffer is expected to begin with the |
---|
765 | * appropriate capture format header for the format type that the packet is |
---|
766 | * being converted to. This also allows for a packet to be converted into |
---|
767 | * just about capture format that is supported by libtrace, provided the |
---|
768 | * format header is present in the buffer. |
---|
769 | * |
---|
770 | * This function is primarily used to convert packets received via the RT |
---|
771 | * protocol back into their original capture format. The RT header encapsulates |
---|
772 | * the original capture format header, so after removing it the packet must |
---|
773 | * have it's header and payload pointers updated and the packet format and type |
---|
774 | * changed, amongst other things. |
---|
775 | * |
---|
776 | * Intended only for internal use at this point - this function is not |
---|
777 | * available through the external libtrace API. |
---|
778 | */ |
---|
779 | int trace_prepare_packet(libtrace_t *trace, libtrace_packet_t *packet, |
---|
780 | void *buffer, libtrace_rt_types_t rt_type, uint32_t flags) { |
---|
781 | |
---|
782 | assert(packet); |
---|
783 | assert(trace); |
---|
784 | |
---|
785 | /* XXX Proper error handling?? */ |
---|
786 | if (buffer == NULL) |
---|
787 | return -1; |
---|
788 | |
---|
789 | if (!(packet->buf_control==TRACE_CTRL_PACKET || packet->buf_control==TRACE_CTRL_EXTERNAL)) { |
---|
790 | trace_set_err(trace,TRACE_ERR_BAD_STATE,"Packet passed to trace_read_packet() is invalid\n"); |
---|
791 | return -1; |
---|
792 | } |
---|
793 | |
---|
794 | packet->trace = trace; |
---|
795 | |
---|
796 | /* Clear packet cache */ |
---|
797 | trace_clear_cache(packet); |
---|
798 | |
---|
799 | if (trace->format->prepare_packet) { |
---|
800 | return trace->format->prepare_packet(trace, packet, |
---|
801 | buffer, rt_type, flags); |
---|
802 | } |
---|
803 | trace_set_err(trace, TRACE_ERR_UNSUPPORTED, |
---|
804 | "This format does not support preparing packets\n"); |
---|
805 | return -1; |
---|
806 | |
---|
807 | } |
---|
808 | |
---|
809 | /* Writes a packet to the specified output trace |
---|
810 | * |
---|
811 | * @param libtrace describes the output format, destination, etc. |
---|
812 | * @param packet the packet to be written out |
---|
813 | * @returns the number of bytes written, -1 if write failed |
---|
814 | */ |
---|
815 | DLLEXPORT int trace_write_packet(libtrace_out_t *libtrace, libtrace_packet_t *packet) { |
---|
816 | assert(libtrace); |
---|
817 | assert(packet); |
---|
818 | /* Verify the packet is valid */ |
---|
819 | if (!libtrace->started) { |
---|
820 | trace_set_err_out(libtrace,TRACE_ERR_BAD_STATE, |
---|
821 | "Trace is not started before trace_write_packet"); |
---|
822 | return -1; |
---|
823 | } |
---|
824 | |
---|
825 | if (libtrace->format->write_packet) { |
---|
826 | return libtrace->format->write_packet(libtrace, packet); |
---|
827 | } |
---|
828 | trace_set_err_out(libtrace,TRACE_ERR_UNSUPPORTED, |
---|
829 | "This format does not support writing packets"); |
---|
830 | return -1; |
---|
831 | } |
---|
832 | |
---|
833 | /* Get a pointer to the first byte of the packet payload */ |
---|
834 | DLLEXPORT void *trace_get_packet_buffer(const libtrace_packet_t *packet, |
---|
835 | libtrace_linktype_t *linktype, uint32_t *remaining) { |
---|
836 | int cap_len; |
---|
837 | int wire_len; |
---|
838 | |
---|
839 | assert(packet != NULL); |
---|
840 | if (linktype) *linktype = trace_get_link_type(packet); |
---|
841 | if (remaining) { |
---|
842 | /* I think we should choose the minimum of the capture and |
---|
843 | * wire lengths to be the "remaining" value. If the packet has |
---|
844 | * been padded to increase the capture length, we don't want |
---|
845 | * to allow subsequent protocol decoders to consider the |
---|
846 | * padding as part of the packet. |
---|
847 | * |
---|
848 | * For example, in Auck 4 there is a trace where the IP header |
---|
849 | * length is incorrect (24 bytes) followed by a 20 byte TCP |
---|
850 | * header. Total IP length is 40 bytes. As a result, the |
---|
851 | * legacyatm padding gets treated as the "missing" bytes of |
---|
852 | * the TCP header, which isn't the greatest. We're probably |
---|
853 | * better off returning an incomplete TCP header in that case. |
---|
854 | */ |
---|
855 | |
---|
856 | cap_len = trace_get_capture_length(packet); |
---|
857 | wire_len = trace_get_wire_length(packet); |
---|
858 | |
---|
859 | assert(cap_len >= 0); |
---|
860 | |
---|
861 | /* There is the odd corrupt packet, e.g. in IPLS II, that have |
---|
862 | * massively negative wire lens. We could assert fail here on |
---|
863 | * them, but we could at least try the capture length instead. |
---|
864 | * |
---|
865 | * You may still run into problems if you try to write that |
---|
866 | * packet, but at least reading should work OK. |
---|
867 | */ |
---|
868 | if (wire_len < 0) |
---|
869 | *remaining = cap_len; |
---|
870 | else if (wire_len < cap_len) |
---|
871 | *remaining = wire_len; |
---|
872 | else |
---|
873 | *remaining = cap_len; |
---|
874 | /* *remaining = trace_get_capture_length(packet); */ |
---|
875 | } |
---|
876 | return (void *) packet->payload; |
---|
877 | } |
---|
878 | |
---|
879 | |
---|
880 | /* Get a pointer to the first byte of the packet payload |
---|
881 | * |
---|
882 | * DEPRECATED - use trace_get_packet_buffer() instead */ |
---|
883 | DLLEXPORT void *trace_get_link(const libtrace_packet_t *packet) { |
---|
884 | return (void *)packet->payload; |
---|
885 | } |
---|
886 | |
---|
887 | /* Get the current time in DAG time format |
---|
888 | * @param packet a pointer to a libtrace_packet structure |
---|
889 | * @returns a 64 bit timestamp in DAG ERF format (upper 32 bits are the seconds |
---|
890 | * past 1970-01-01, the lower 32bits are partial seconds) |
---|
891 | */ |
---|
892 | DLLEXPORT uint64_t trace_get_erf_timestamp(const libtrace_packet_t *packet) { |
---|
893 | if (packet->trace->format->get_erf_timestamp) { |
---|
894 | /* timestamp -> timestamp */ |
---|
895 | return packet->trace->format->get_erf_timestamp(packet); |
---|
896 | } else if (packet->trace->format->get_timespec) { |
---|
897 | /* timespec -> timestamp */ |
---|
898 | struct timespec ts; |
---|
899 | ts = packet->trace->format->get_timespec(packet); |
---|
900 | return ((((uint64_t)ts.tv_sec) << 32) + |
---|
901 | (((uint64_t)ts.tv_nsec << 32)/1000000000)); |
---|
902 | } else if (packet->trace->format->get_timeval) { |
---|
903 | /* timeval -> timestamp */ |
---|
904 | struct timeval tv; |
---|
905 | tv = packet->trace->format->get_timeval(packet); |
---|
906 | return ((((uint64_t)tv.tv_sec) << 32) + |
---|
907 | (((uint64_t)tv.tv_usec << 32)/1000000)); |
---|
908 | } else if (packet->trace->format->get_seconds) { |
---|
909 | /* seconds -> timestamp */ |
---|
910 | double seconds = packet->trace->format->get_seconds(packet); |
---|
911 | return (((uint64_t)seconds)<<32) |
---|
912 | + (uint64_t)((seconds-(uint64_t)seconds)*UINT_MAX); |
---|
913 | } |
---|
914 | else { |
---|
915 | return (uint64_t)0; |
---|
916 | } |
---|
917 | |
---|
918 | } |
---|
919 | |
---|
920 | /* Get the current time in struct timeval |
---|
921 | * @param packet a pointer to a libtrace_packet structure |
---|
922 | * |
---|
923 | * @returns time that this packet was seen in a struct timeval |
---|
924 | * @author Daniel Lawson |
---|
925 | * @author Perry Lorier |
---|
926 | */ |
---|
927 | DLLEXPORT struct timeval trace_get_timeval(const libtrace_packet_t *packet) { |
---|
928 | struct timeval tv; |
---|
929 | uint64_t ts = 0; |
---|
930 | if (packet->trace->format->get_timeval) { |
---|
931 | /* timeval -> timeval */ |
---|
932 | tv = packet->trace->format->get_timeval(packet); |
---|
933 | } else if (packet->trace->format->get_erf_timestamp) { |
---|
934 | /* timestamp -> timeval */ |
---|
935 | ts = packet->trace->format->get_erf_timestamp(packet); |
---|
936 | tv.tv_sec = ts >> 32; |
---|
937 | tv.tv_usec = ((ts&0xFFFFFFFF)*1000000)>>32; |
---|
938 | if (tv.tv_usec >= 1000000) { |
---|
939 | tv.tv_usec -= 1000000; |
---|
940 | tv.tv_sec += 1; |
---|
941 | } |
---|
942 | } else if (packet->trace->format->get_timespec) { |
---|
943 | struct timespec ts = packet->trace->format->get_timespec(packet); |
---|
944 | tv.tv_sec = ts.tv_sec; |
---|
945 | tv.tv_usec = ts.tv_nsec/1000; |
---|
946 | } else if (packet->trace->format->get_seconds) { |
---|
947 | /* seconds -> timeval */ |
---|
948 | double seconds = packet->trace->format->get_seconds(packet); |
---|
949 | tv.tv_sec = (uint32_t)seconds; |
---|
950 | tv.tv_usec = (uint32_t)(((seconds - tv.tv_sec) * 1000000)/UINT_MAX); |
---|
951 | } |
---|
952 | else { |
---|
953 | tv.tv_sec=-1; |
---|
954 | tv.tv_usec=-1; |
---|
955 | } |
---|
956 | |
---|
957 | return tv; |
---|
958 | } |
---|
959 | |
---|
960 | DLLEXPORT struct timespec trace_get_timespec(const libtrace_packet_t *packet) { |
---|
961 | struct timespec ts; |
---|
962 | |
---|
963 | if (packet->trace->format->get_timespec) { |
---|
964 | return packet->trace->format->get_timespec(packet); |
---|
965 | } else if (packet->trace->format->get_erf_timestamp) { |
---|
966 | /* timestamp -> timeval */ |
---|
967 | uint64_t erfts = packet->trace->format->get_erf_timestamp(packet); |
---|
968 | ts.tv_sec = erfts >> 32; |
---|
969 | ts.tv_nsec = ((erfts&0xFFFFFFFF)*1000000000)>>32; |
---|
970 | if (ts.tv_nsec >= 1000000000) { |
---|
971 | ts.tv_nsec -= 1000000000; |
---|
972 | ts.tv_sec += 1; |
---|
973 | } |
---|
974 | return ts; |
---|
975 | } else if (packet->trace->format->get_timeval) { |
---|
976 | /* timeval -> timespec */ |
---|
977 | struct timeval tv = packet->trace->format->get_timeval(packet); |
---|
978 | ts.tv_sec = tv.tv_sec; |
---|
979 | ts.tv_nsec = tv.tv_usec*1000; |
---|
980 | return ts; |
---|
981 | } else if (packet->trace->format->get_seconds) { |
---|
982 | /* seconds -> timespec */ |
---|
983 | double seconds = packet->trace->format->get_seconds(packet); |
---|
984 | ts.tv_sec = (uint32_t)seconds; |
---|
985 | ts.tv_nsec = (long)(((seconds - ts.tv_sec) * 1000000000)/UINT_MAX); |
---|
986 | return ts; |
---|
987 | } |
---|
988 | else { |
---|
989 | ts.tv_sec=-1; |
---|
990 | ts.tv_nsec=-1; |
---|
991 | return ts; |
---|
992 | } |
---|
993 | } |
---|
994 | |
---|
995 | |
---|
996 | /* Get the current time in floating point seconds |
---|
997 | * @param packet a pointer to a libtrace_packet structure |
---|
998 | * @returns time that this packet was seen in 64bit floating point seconds |
---|
999 | */ |
---|
1000 | DLLEXPORT double trace_get_seconds(const libtrace_packet_t *packet) { |
---|
1001 | double seconds = 0.0; |
---|
1002 | |
---|
1003 | if (packet->trace->format->get_seconds) { |
---|
1004 | /* seconds->seconds */ |
---|
1005 | seconds = packet->trace->format->get_seconds(packet); |
---|
1006 | } else if (packet->trace->format->get_erf_timestamp) { |
---|
1007 | /* timestamp -> seconds */ |
---|
1008 | uint64_t ts = 0; |
---|
1009 | ts = packet->trace->format->get_erf_timestamp(packet); |
---|
1010 | seconds = (ts>>32) + ((ts & UINT_MAX)*1.0 / UINT_MAX); |
---|
1011 | } else if (packet->trace->format->get_timespec) { |
---|
1012 | /* timespec -> seconds */ |
---|
1013 | struct timespec ts; |
---|
1014 | ts = packet->trace->format->get_timespec(packet); |
---|
1015 | seconds = ts.tv_sec + ((ts.tv_nsec * 1.0) / 1000000000); |
---|
1016 | } else if (packet->trace->format->get_timeval) { |
---|
1017 | /* timeval -> seconds */ |
---|
1018 | struct timeval tv; |
---|
1019 | tv = packet->trace->format->get_timeval(packet); |
---|
1020 | seconds = tv.tv_sec + ((tv.tv_usec * 1.0) / 1000000); |
---|
1021 | } |
---|
1022 | |
---|
1023 | return seconds; |
---|
1024 | } |
---|
1025 | |
---|
1026 | DLLEXPORT size_t trace_get_capture_length(const libtrace_packet_t *packet) |
---|
1027 | { |
---|
1028 | /* Cache the capture length */ |
---|
1029 | if (packet->capture_length == -1) { |
---|
1030 | if (!packet->trace->format->get_capture_length) |
---|
1031 | return ~0U; |
---|
1032 | /* Cast away constness because this is "just" a cache */ |
---|
1033 | ((libtrace_packet_t*)packet)->capture_length = |
---|
1034 | packet->trace->format->get_capture_length(packet); |
---|
1035 | } |
---|
1036 | |
---|
1037 | assert(packet->capture_length < LIBTRACE_PACKET_BUFSIZE); |
---|
1038 | |
---|
1039 | return packet->capture_length; |
---|
1040 | } |
---|
1041 | |
---|
1042 | /* Get the size of the packet as it was seen on the wire. |
---|
1043 | * @param packet a pointer to a libtrace_packet structure |
---|
1044 | * |
---|
1045 | * @returns the size of the packet as it was on the wire. |
---|
1046 | * @note Due to the trace being a header capture, or anonymisation this may |
---|
1047 | * not be the same as the Capture Len. |
---|
1048 | */ |
---|
1049 | DLLEXPORT size_t trace_get_wire_length(const libtrace_packet_t *packet){ |
---|
1050 | |
---|
1051 | if (packet->wire_length == -1) { |
---|
1052 | if (!packet->trace->format->get_wire_length) |
---|
1053 | return ~0U; |
---|
1054 | ((libtrace_packet_t *)packet)->wire_length = |
---|
1055 | packet->trace->format->get_wire_length(packet); |
---|
1056 | } |
---|
1057 | |
---|
1058 | assert(packet->wire_length < LIBTRACE_PACKET_BUFSIZE); |
---|
1059 | return packet->wire_length; |
---|
1060 | |
---|
1061 | } |
---|
1062 | |
---|
1063 | /* Get the length of the capture framing headers. |
---|
1064 | * @param packet the packet opaque pointer |
---|
1065 | * @returns the size of the packet as it was on the wire. |
---|
1066 | * @note this length corresponds to the difference between the size of a |
---|
1067 | * captured packet in memory, and the captured length of the packet |
---|
1068 | */ |
---|
1069 | DLLEXPORT SIMPLE_FUNCTION |
---|
1070 | size_t trace_get_framing_length(const libtrace_packet_t *packet) { |
---|
1071 | if (packet->trace->format->get_framing_length) { |
---|
1072 | return packet->trace->format->get_framing_length(packet); |
---|
1073 | } |
---|
1074 | return ~0U; |
---|
1075 | } |
---|
1076 | |
---|
1077 | |
---|
1078 | /* Get the type of the link layer |
---|
1079 | * @param packet a pointer to a libtrace_packet structure |
---|
1080 | * @returns libtrace_linktype_t |
---|
1081 | */ |
---|
1082 | DLLEXPORT libtrace_linktype_t trace_get_link_type(const libtrace_packet_t *packet ) { |
---|
1083 | |
---|
1084 | if (packet->link_type == 0) { |
---|
1085 | if (!packet->trace->format->get_link_type) |
---|
1086 | return TRACE_TYPE_UNKNOWN; |
---|
1087 | ((libtrace_packet_t *)packet)->link_type = |
---|
1088 | packet->trace->format->get_link_type(packet); |
---|
1089 | } |
---|
1090 | |
---|
1091 | return packet->link_type; |
---|
1092 | } |
---|
1093 | |
---|
1094 | /* process a libtrace event |
---|
1095 | * @param trace the libtrace opaque pointer |
---|
1096 | * @param packet the libtrace_packet opaque pointer |
---|
1097 | * @returns |
---|
1098 | * TRACE_EVENT_IOWAIT Waiting on I/O on fd |
---|
1099 | * TRACE_EVENT_SLEEP Next event in seconds |
---|
1100 | * TRACE_EVENT_PACKET Packet arrived in buffer with size size |
---|
1101 | * TRACE_EVENT_TERMINATE Trace terminated (perhaps with an error condition) |
---|
1102 | * FIXME currently keeps a copy of the packet inside the trace pointer, |
---|
1103 | * which in turn is stored inside the new packet object... |
---|
1104 | */ |
---|
1105 | DLLEXPORT libtrace_eventobj_t trace_event(libtrace_t *trace, |
---|
1106 | libtrace_packet_t *packet) { |
---|
1107 | libtrace_eventobj_t event = {TRACE_EVENT_IOWAIT,0,0.0,0}; |
---|
1108 | |
---|
1109 | if (!trace) { |
---|
1110 | fprintf(stderr,"You called trace_event() with a NULL trace object!\n"); |
---|
1111 | } |
---|
1112 | assert(trace); |
---|
1113 | assert(packet); |
---|
1114 | |
---|
1115 | /* Clear the packet cache */ |
---|
1116 | trace_clear_cache(packet); |
---|
1117 | |
---|
1118 | /* Store the trace we are reading from into the packet opaque |
---|
1119 | * structure */ |
---|
1120 | packet->trace = trace; |
---|
1121 | |
---|
1122 | if (packet->trace->format->trace_event) { |
---|
1123 | /* Note: incrementing accepted, filtered etc. packet |
---|
1124 | * counters is handled by the format-specific |
---|
1125 | * function so don't increment them here. |
---|
1126 | */ |
---|
1127 | event=packet->trace->format->trace_event(trace,packet); |
---|
1128 | } |
---|
1129 | return event; |
---|
1130 | |
---|
1131 | } |
---|
1132 | |
---|
1133 | /** Setup a BPF filter based on pre-compiled byte-code. |
---|
1134 | * @param bf_insns A pointer to the start of the byte-code |
---|
1135 | * @param bf_len The number of BPF instructions |
---|
1136 | * @returns an opaque pointer to a libtrace_filter_t object |
---|
1137 | * @note The supplied byte-code is not checked for correctness. |
---|
1138 | * @author Scott Raynel |
---|
1139 | */ |
---|
1140 | DLLEXPORT libtrace_filter_t * |
---|
1141 | trace_create_filter_from_bytecode(void *bf_insns, unsigned int bf_len) |
---|
1142 | { |
---|
1143 | #ifndef HAVE_BPF_FILTER |
---|
1144 | fprintf(stderr, "This version of libtrace does not have BPF support\n"); |
---|
1145 | return NULL; |
---|
1146 | #else |
---|
1147 | struct libtrace_filter_t *filter = (struct libtrace_filter_t *) |
---|
1148 | malloc(sizeof(struct libtrace_filter_t)); |
---|
1149 | filter->filter.bf_insns = (struct bpf_insn *) |
---|
1150 | malloc(sizeof(struct bpf_insn) * bf_len); |
---|
1151 | |
---|
1152 | memcpy(filter->filter.bf_insns, bf_insns, |
---|
1153 | bf_len * sizeof(struct bpf_insn)); |
---|
1154 | |
---|
1155 | filter->filter.bf_len = bf_len; |
---|
1156 | filter->filterstring = NULL; |
---|
1157 | filter->jitfilter = NULL; |
---|
1158 | /* "flag" indicates that the filter member is valid */ |
---|
1159 | filter->flag = 1; |
---|
1160 | |
---|
1161 | return filter; |
---|
1162 | #endif |
---|
1163 | } |
---|
1164 | |
---|
1165 | /* Create a BPF filter |
---|
1166 | * @param filterstring a char * containing the bpf filter string |
---|
1167 | * @returns opaque pointer pointer to a libtrace_filter_t object |
---|
1168 | */ |
---|
1169 | DLLEXPORT libtrace_filter_t *trace_create_filter(const char *filterstring) { |
---|
1170 | #ifdef HAVE_BPF_FILTER |
---|
1171 | libtrace_filter_t *filter = (libtrace_filter_t*) |
---|
1172 | malloc(sizeof(libtrace_filter_t)); |
---|
1173 | filter->filterstring = strdup(filterstring); |
---|
1174 | filter->jitfilter = NULL; |
---|
1175 | filter->flag = 0; |
---|
1176 | return filter; |
---|
1177 | #else |
---|
1178 | fprintf(stderr,"This version of libtrace does not have bpf filter support\n"); |
---|
1179 | return NULL; |
---|
1180 | #endif |
---|
1181 | } |
---|
1182 | |
---|
1183 | DLLEXPORT void trace_destroy_filter(libtrace_filter_t *filter) |
---|
1184 | { |
---|
1185 | #ifdef HAVE_BPF_FILTER |
---|
1186 | free(filter->filterstring); |
---|
1187 | if (filter->flag) |
---|
1188 | pcap_freecode(&filter->filter); |
---|
1189 | #ifdef HAVE_LLVM |
---|
1190 | if (filter->jitfilter) |
---|
1191 | destroy_program(filter->jitfilter); |
---|
1192 | #endif |
---|
1193 | free(filter); |
---|
1194 | #else |
---|
1195 | |
---|
1196 | #endif |
---|
1197 | } |
---|
1198 | |
---|
1199 | /* Compile a bpf filter, now we know the link type for the trace that we're |
---|
1200 | * applying it to. |
---|
1201 | * |
---|
1202 | * @internal |
---|
1203 | * |
---|
1204 | * @returns -1 on error, 0 on success |
---|
1205 | */ |
---|
1206 | static int trace_bpf_compile(libtrace_filter_t *filter, |
---|
1207 | const libtrace_packet_t *packet, |
---|
1208 | void *linkptr, |
---|
1209 | libtrace_linktype_t linktype ) { |
---|
1210 | #ifdef HAVE_BPF_FILTER |
---|
1211 | assert(filter); |
---|
1212 | |
---|
1213 | /* If this isn't a real packet, then fail */ |
---|
1214 | if (!linkptr) { |
---|
1215 | trace_set_err(packet->trace, |
---|
1216 | TRACE_ERR_BAD_FILTER,"Packet has no payload"); |
---|
1217 | return -1; |
---|
1218 | } |
---|
1219 | |
---|
1220 | if (filter->filterstring && ! filter->flag) { |
---|
1221 | pcap_t *pcap = NULL; |
---|
1222 | if (linktype==(libtrace_linktype_t)-1) { |
---|
1223 | trace_set_err(packet->trace, |
---|
1224 | TRACE_ERR_BAD_FILTER, |
---|
1225 | "Packet has an unknown linktype"); |
---|
1226 | return -1; |
---|
1227 | } |
---|
1228 | if (libtrace_to_pcap_dlt(linktype) == TRACE_DLT_ERROR) { |
---|
1229 | trace_set_err(packet->trace,TRACE_ERR_BAD_FILTER, |
---|
1230 | "Unknown pcap equivalent linktype"); |
---|
1231 | return -1; |
---|
1232 | } |
---|
1233 | pcap=(pcap_t *)pcap_open_dead( |
---|
1234 | (int)libtrace_to_pcap_dlt(linktype), |
---|
1235 | 1500U); |
---|
1236 | /* build filter */ |
---|
1237 | assert(pcap); |
---|
1238 | if (pcap_compile( pcap, &filter->filter, filter->filterstring, |
---|
1239 | 1, 0)) { |
---|
1240 | trace_set_err(packet->trace,TRACE_ERR_BAD_FILTER, |
---|
1241 | "Unable to compile the filter \"%s\": %s", |
---|
1242 | filter->filterstring, |
---|
1243 | pcap_geterr(pcap)); |
---|
1244 | pcap_close(pcap); |
---|
1245 | return -1; |
---|
1246 | } |
---|
1247 | pcap_close(pcap); |
---|
1248 | filter->flag=1; |
---|
1249 | } |
---|
1250 | return 0; |
---|
1251 | #else |
---|
1252 | assert(!"Internal bug: This should never be called when BPF not enabled"); |
---|
1253 | trace_set_err(packet->trace,TRACE_ERR_OPTION_UNAVAIL, |
---|
1254 | "Feature unavailable"); |
---|
1255 | return -1; |
---|
1256 | #endif |
---|
1257 | } |
---|
1258 | |
---|
1259 | DLLEXPORT int trace_apply_filter(libtrace_filter_t *filter, |
---|
1260 | const libtrace_packet_t *packet) { |
---|
1261 | #ifdef HAVE_BPF_FILTER |
---|
1262 | void *linkptr = 0; |
---|
1263 | uint32_t clen = 0; |
---|
1264 | bool free_packet_needed = false; |
---|
1265 | int ret; |
---|
1266 | libtrace_linktype_t linktype; |
---|
1267 | libtrace_packet_t *packet_copy = (libtrace_packet_t*)packet; |
---|
1268 | |
---|
1269 | assert(filter); |
---|
1270 | assert(packet); |
---|
1271 | |
---|
1272 | /* Match all non-data packets as we probably want them to pass |
---|
1273 | * through to the caller */ |
---|
1274 | linktype = trace_get_link_type(packet); |
---|
1275 | |
---|
1276 | if (linktype == TRACE_TYPE_NONDATA) |
---|
1277 | return 1; |
---|
1278 | |
---|
1279 | if (libtrace_to_pcap_dlt(linktype)==TRACE_DLT_ERROR) { |
---|
1280 | |
---|
1281 | /* If we cannot get a suitable DLT for the packet, it may |
---|
1282 | * be because the packet is encapsulated in a link type that |
---|
1283 | * does not correspond to a DLT. Therefore, we should try |
---|
1284 | * popping off headers until we either can find a suitable |
---|
1285 | * link type or we can't do any more sensible decapsulation. */ |
---|
1286 | |
---|
1287 | /* Copy the packet, as we don't want to trash the one we |
---|
1288 | * were passed in */ |
---|
1289 | packet_copy=trace_copy_packet(packet); |
---|
1290 | free_packet_needed=true; |
---|
1291 | |
---|
1292 | while (libtrace_to_pcap_dlt(linktype) == TRACE_DLT_ERROR) { |
---|
1293 | if (!demote_packet(packet_copy)) { |
---|
1294 | trace_set_err(packet->trace, |
---|
1295 | TRACE_ERR_NO_CONVERSION, |
---|
1296 | "pcap does not support this format"); |
---|
1297 | if (free_packet_needed) { |
---|
1298 | trace_destroy_packet(packet_copy); |
---|
1299 | } |
---|
1300 | return -1; |
---|
1301 | } |
---|
1302 | linktype = trace_get_link_type(packet_copy); |
---|
1303 | } |
---|
1304 | |
---|
1305 | } |
---|
1306 | |
---|
1307 | linkptr = trace_get_packet_buffer(packet_copy,NULL,&clen); |
---|
1308 | if (!linkptr) { |
---|
1309 | if (free_packet_needed) { |
---|
1310 | trace_destroy_packet(packet_copy); |
---|
1311 | } |
---|
1312 | return 0; |
---|
1313 | } |
---|
1314 | |
---|
1315 | /* We need to compile the filter now, because before we didn't know |
---|
1316 | * what the link type was |
---|
1317 | */ |
---|
1318 | if (trace_bpf_compile(filter,packet_copy,linkptr,linktype)==-1) { |
---|
1319 | if (free_packet_needed) { |
---|
1320 | trace_destroy_packet(packet_copy); |
---|
1321 | } |
---|
1322 | return -1; |
---|
1323 | } |
---|
1324 | |
---|
1325 | /* If we're jitting, we may need to JIT the BPF code now too */ |
---|
1326 | #if HAVE_LLVM |
---|
1327 | if (!filter->jitfilter) { |
---|
1328 | filter->jitfilter = compile_program(filter->filter.bf_insns, filter->filter.bf_len); |
---|
1329 | } |
---|
1330 | #endif |
---|
1331 | |
---|
1332 | assert(filter->flag); |
---|
1333 | /* Now execute the filter */ |
---|
1334 | #if HAVE_LLVM |
---|
1335 | ret=filter->jitfilter->bpf_run((unsigned char *)linkptr, clen); |
---|
1336 | #else |
---|
1337 | ret=bpf_filter(filter->filter.bf_insns,(u_char*)linkptr,(unsigned int)clen,(unsigned int)clen); |
---|
1338 | #endif |
---|
1339 | |
---|
1340 | /* If we copied the packet earlier, make sure that we free it */ |
---|
1341 | if (free_packet_needed) { |
---|
1342 | trace_destroy_packet(packet_copy); |
---|
1343 | } |
---|
1344 | return ret; |
---|
1345 | #else |
---|
1346 | fprintf(stderr,"This version of libtrace does not have bpf filter support\n"); |
---|
1347 | return 0; |
---|
1348 | #endif |
---|
1349 | } |
---|
1350 | |
---|
1351 | /* Set the direction flag, if it has one |
---|
1352 | * @param packet the packet opaque pointer |
---|
1353 | * @param direction the new direction (0,1,2,3) |
---|
1354 | * @returns a signed value containing the direction flag, or -1 if this is not supported |
---|
1355 | */ |
---|
1356 | DLLEXPORT libtrace_direction_t trace_set_direction(libtrace_packet_t *packet, |
---|
1357 | libtrace_direction_t direction) |
---|
1358 | { |
---|
1359 | assert(packet); |
---|
1360 | if (packet->trace->format->set_direction) { |
---|
1361 | return packet->trace->format->set_direction(packet,direction); |
---|
1362 | } |
---|
1363 | return (libtrace_direction_t)~0U; |
---|
1364 | } |
---|
1365 | |
---|
1366 | /* Get the direction flag, if it has one |
---|
1367 | * @param packet a pointer to a libtrace_packet structure |
---|
1368 | * @returns a signed value containing the direction flag, or -1 if this is not supported |
---|
1369 | * The direction is defined as 0 for packets originating locally (ie, outbound) |
---|
1370 | * and 1 for packets originating remotely (ie, inbound). |
---|
1371 | * Other values are possible, which might be overloaded to mean special things |
---|
1372 | * for a special trace. |
---|
1373 | */ |
---|
1374 | DLLEXPORT libtrace_direction_t trace_get_direction(const libtrace_packet_t *packet) |
---|
1375 | { |
---|
1376 | assert(packet); |
---|
1377 | if (packet->trace->format->get_direction) { |
---|
1378 | return packet->trace->format->get_direction(packet); |
---|
1379 | } |
---|
1380 | return (libtrace_direction_t)~0U; |
---|
1381 | } |
---|
1382 | |
---|
1383 | #define ROOT_SERVER(x) ((x) < 512) |
---|
1384 | #define ROOT_CLIENT(x) ((512 <= (x)) && ((x) < 1024)) |
---|
1385 | #define NONROOT_SERVER(x) ((x) >= 5000) |
---|
1386 | #define NONROOT_CLIENT(x) ((1024 <= (x)) && ((x) < 5000)) |
---|
1387 | #define DYNAMIC(x) ((49152 < (x)) && ((x) < 65535)) |
---|
1388 | #define SERVER(x) ROOT_SERVER(x) || NONROOT_SERVER(x) |
---|
1389 | #define CLIENT(x) ROOT_CLIENT(x) || NONROOT_CLIENT(x) |
---|
1390 | |
---|
1391 | /* Attempt to deduce the 'server' port |
---|
1392 | * @param protocol the IP protocol (eg, 6 or 17 for TCP or UDP) |
---|
1393 | * @param source the TCP or UDP source port |
---|
1394 | * @param dest the TCP or UDP destination port |
---|
1395 | * @returns a hint as to which port is the server port |
---|
1396 | */ |
---|
1397 | DLLEXPORT int8_t trace_get_server_port(UNUSED uint8_t protocol, |
---|
1398 | uint16_t source, uint16_t dest) |
---|
1399 | { |
---|
1400 | /* |
---|
1401 | * * If the ports are equal, return DEST |
---|
1402 | * * Check for well-known ports in the given protocol |
---|
1403 | * * Root server ports: 0 - 511 |
---|
1404 | * * Root client ports: 512 - 1023 |
---|
1405 | * * non-root client ports: 1024 - 4999 |
---|
1406 | * * non-root server ports: 5000+ |
---|
1407 | * * Check for static ranges: 1024 - 49151 |
---|
1408 | * * Check for dynamic ranges: 49152 - 65535 |
---|
1409 | * * flip a coin. |
---|
1410 | */ |
---|
1411 | |
---|
1412 | /* equal */ |
---|
1413 | if (source == dest) |
---|
1414 | return USE_DEST; |
---|
1415 | |
---|
1416 | /* root server port, 0 - 511 */ |
---|
1417 | if (ROOT_SERVER(source) && ROOT_SERVER(dest)) { |
---|
1418 | if (source < dest) |
---|
1419 | return USE_SOURCE; |
---|
1420 | return USE_DEST; |
---|
1421 | } |
---|
1422 | |
---|
1423 | if (ROOT_SERVER(source) && !ROOT_SERVER(dest)) |
---|
1424 | return USE_SOURCE; |
---|
1425 | if (!ROOT_SERVER(source) && ROOT_SERVER(dest)) |
---|
1426 | return USE_DEST; |
---|
1427 | |
---|
1428 | /* non-root server */ |
---|
1429 | if (NONROOT_SERVER(source) && NONROOT_SERVER(dest)) { |
---|
1430 | if (source < dest) |
---|
1431 | return USE_SOURCE; |
---|
1432 | return USE_DEST; |
---|
1433 | } |
---|
1434 | if (NONROOT_SERVER(source) && !NONROOT_SERVER(dest)) |
---|
1435 | return USE_SOURCE; |
---|
1436 | if (!NONROOT_SERVER(source) && NONROOT_SERVER(dest)) |
---|
1437 | return USE_DEST; |
---|
1438 | |
---|
1439 | /* root client */ |
---|
1440 | if (ROOT_CLIENT(source) && ROOT_CLIENT(dest)) { |
---|
1441 | if (source < dest) |
---|
1442 | return USE_SOURCE; |
---|
1443 | return USE_DEST; |
---|
1444 | } |
---|
1445 | if (ROOT_CLIENT(source) && !ROOT_CLIENT(dest)) { |
---|
1446 | /* prefer root-client over nonroot-client */ |
---|
1447 | if (NONROOT_CLIENT(dest)) |
---|
1448 | return USE_SOURCE; |
---|
1449 | return USE_DEST; |
---|
1450 | } |
---|
1451 | if (!ROOT_CLIENT(source) && ROOT_CLIENT(dest)) { |
---|
1452 | /* prefer root-client over nonroot-client */ |
---|
1453 | if (NONROOT_CLIENT(source)) |
---|
1454 | return USE_DEST; |
---|
1455 | return USE_SOURCE; |
---|
1456 | } |
---|
1457 | |
---|
1458 | /* nonroot client */ |
---|
1459 | if (NONROOT_CLIENT(source) && NONROOT_CLIENT(dest)) { |
---|
1460 | if (source < dest) |
---|
1461 | return USE_SOURCE; |
---|
1462 | return USE_DEST; |
---|
1463 | } |
---|
1464 | if (NONROOT_CLIENT(source) && !NONROOT_CLIENT(dest)) |
---|
1465 | return USE_DEST; |
---|
1466 | if (!NONROOT_CLIENT(source) && NONROOT_CLIENT(dest)) |
---|
1467 | return USE_SOURCE; |
---|
1468 | |
---|
1469 | /* dynamic range */ |
---|
1470 | if (DYNAMIC(source) && DYNAMIC(dest)) { |
---|
1471 | if (source < dest) |
---|
1472 | return USE_SOURCE; |
---|
1473 | return USE_DEST; |
---|
1474 | } |
---|
1475 | if (DYNAMIC(source) && !DYNAMIC(dest)) |
---|
1476 | return USE_DEST; |
---|
1477 | if (!DYNAMIC(source) && DYNAMIC(dest)) |
---|
1478 | return USE_SOURCE; |
---|
1479 | /* |
---|
1480 | if (SERVER(source) && CLIENT(dest)) |
---|
1481 | return USE_SOURCE; |
---|
1482 | |
---|
1483 | if (SERVER(dest) && CLIENT(source)) |
---|
1484 | return USE_DEST; |
---|
1485 | if (ROOT_SERVER(source) && !ROOT_SERVER(dest)) |
---|
1486 | return USE_SOURCE; |
---|
1487 | if (ROOT_SERVER(dest) && !ROOT_SERVER(source)) |
---|
1488 | return USE_DEST; |
---|
1489 | */ |
---|
1490 | /* failing that test... */ |
---|
1491 | if (source < dest) { |
---|
1492 | return USE_SOURCE; |
---|
1493 | } |
---|
1494 | return USE_DEST; |
---|
1495 | |
---|
1496 | } |
---|
1497 | |
---|
1498 | /* Truncate the packet at the suggested length |
---|
1499 | * @param packet the packet opaque pointer |
---|
1500 | * @param size the new length of the packet |
---|
1501 | * @returns the new size of the packet |
---|
1502 | * @note size and the return size refer to the network-level payload of the |
---|
1503 | * packet, and do not include any capture headers. For example, to truncate a |
---|
1504 | * packet after the IP header, set size to sizeof(ethernet_header) + |
---|
1505 | * sizeof(ip_header) |
---|
1506 | * @note If the original network-level payload is smaller than size, then the |
---|
1507 | * original size is returned and the packet is left unchanged. |
---|
1508 | */ |
---|
1509 | DLLEXPORT size_t trace_set_capture_length(libtrace_packet_t *packet, size_t size) { |
---|
1510 | assert(packet); |
---|
1511 | |
---|
1512 | if (packet->trace->format->set_capture_length) { |
---|
1513 | packet->capture_length = packet->trace->format->set_capture_length(packet,size); |
---|
1514 | return packet->capture_length; |
---|
1515 | } |
---|
1516 | |
---|
1517 | return ~0U; |
---|
1518 | } |
---|
1519 | |
---|
1520 | /* Splits a URI into two components - the format component which is seen before |
---|
1521 | * the ':', and the uridata which follows the ':'. |
---|
1522 | * |
---|
1523 | * Returns a pointer to the URI data, but updates the format parameter to |
---|
1524 | * point to a copy of the format component. |
---|
1525 | */ |
---|
1526 | |
---|
1527 | DLLEXPORT const char * trace_parse_uri(const char *uri, char **format) { |
---|
1528 | const char *uridata = 0; |
---|
1529 | |
---|
1530 | if((uridata = strchr(uri,':')) == NULL) { |
---|
1531 | /* Badly formed URI - needs a : */ |
---|
1532 | return 0; |
---|
1533 | } |
---|
1534 | |
---|
1535 | if ((unsigned)(uridata - uri) > URI_PROTO_LINE) { |
---|
1536 | /* Badly formed URI - uri type is too long */ |
---|
1537 | return 0; |
---|
1538 | } |
---|
1539 | |
---|
1540 | /* NOTE: this is allocated memory - it should be freed by the caller |
---|
1541 | * once they are done with it */ |
---|
1542 | *format=xstrndup(uri, (size_t)(uridata - uri)); |
---|
1543 | |
---|
1544 | /* Push uridata past the delimiter */ |
---|
1545 | uridata++; |
---|
1546 | |
---|
1547 | return uridata; |
---|
1548 | } |
---|
1549 | |
---|
1550 | enum base_format_t trace_get_format(libtrace_packet_t *packet) |
---|
1551 | { |
---|
1552 | assert(packet); |
---|
1553 | |
---|
1554 | return packet->trace->format->type; |
---|
1555 | } |
---|
1556 | |
---|
1557 | DLLEXPORT libtrace_err_t trace_get_err(libtrace_t *trace) |
---|
1558 | { |
---|
1559 | libtrace_err_t err = trace->err; |
---|
1560 | trace->err.err_num = 0; /* "OK" */ |
---|
1561 | trace->err.problem[0]='\0'; |
---|
1562 | return err; |
---|
1563 | } |
---|
1564 | |
---|
1565 | DLLEXPORT bool trace_is_err(libtrace_t *trace) |
---|
1566 | { |
---|
1567 | return trace->err.err_num != 0; |
---|
1568 | } |
---|
1569 | |
---|
1570 | /* Prints the input error status to standard error and clears the error state */ |
---|
1571 | DLLEXPORT void trace_perror(libtrace_t *trace,const char *msg,...) |
---|
1572 | { |
---|
1573 | char buf[256]; |
---|
1574 | va_list va; |
---|
1575 | va_start(va,msg); |
---|
1576 | vsnprintf(buf,sizeof(buf),msg,va); |
---|
1577 | va_end(va); |
---|
1578 | if(trace->err.err_num) { |
---|
1579 | if (trace->uridata) { |
---|
1580 | fprintf(stderr,"%s(%s): %s\n", |
---|
1581 | buf,trace->uridata,trace->err.problem); |
---|
1582 | } else { |
---|
1583 | fprintf(stderr,"%s: %s\n", buf, trace->err.problem); |
---|
1584 | } |
---|
1585 | } else { |
---|
1586 | if (trace->uridata) { |
---|
1587 | fprintf(stderr,"%s(%s): No error\n",buf,trace->uridata); |
---|
1588 | } else { |
---|
1589 | fprintf(stderr,"%s: No error\n", buf); |
---|
1590 | } |
---|
1591 | } |
---|
1592 | trace->err.err_num = 0; /* "OK" */ |
---|
1593 | trace->err.problem[0]='\0'; |
---|
1594 | } |
---|
1595 | |
---|
1596 | DLLEXPORT libtrace_err_t trace_get_err_output(libtrace_out_t *trace) |
---|
1597 | { |
---|
1598 | libtrace_err_t err = trace->err; |
---|
1599 | trace->err.err_num = TRACE_ERR_NOERROR; /* "OK" */ |
---|
1600 | trace->err.problem[0]='\0'; |
---|
1601 | return err; |
---|
1602 | } |
---|
1603 | |
---|
1604 | DLLEXPORT bool trace_is_err_output(libtrace_out_t *trace) |
---|
1605 | { |
---|
1606 | return trace->err.err_num != 0; |
---|
1607 | } |
---|
1608 | |
---|
1609 | /* Prints the output error status to standard error and clears the error state |
---|
1610 | */ |
---|
1611 | DLLEXPORT void trace_perror_output(libtrace_out_t *trace,const char *msg,...) |
---|
1612 | { |
---|
1613 | char buf[256]; |
---|
1614 | va_list va; |
---|
1615 | va_start(va,msg); |
---|
1616 | vsnprintf(buf,sizeof(buf),msg,va); |
---|
1617 | va_end(va); |
---|
1618 | if(trace->err.err_num) { |
---|
1619 | fprintf(stderr,"%s(%s): %s\n", |
---|
1620 | buf, |
---|
1621 | trace->uridata?trace->uridata:"no uri", |
---|
1622 | trace->err.problem); |
---|
1623 | } else { |
---|
1624 | fprintf(stderr,"%s(%s): No error\n",buf,trace->uridata); |
---|
1625 | } |
---|
1626 | trace->err.err_num = TRACE_ERR_NOERROR; /* "OK" */ |
---|
1627 | trace->err.problem[0]='\0'; |
---|
1628 | } |
---|
1629 | |
---|
1630 | DLLEXPORT int trace_seek_erf_timestamp(libtrace_t *trace, uint64_t ts) |
---|
1631 | { |
---|
1632 | if (trace->format->seek_erf) { |
---|
1633 | return trace->format->seek_erf(trace,ts); |
---|
1634 | } |
---|
1635 | else { |
---|
1636 | if (trace->format->seek_timeval) { |
---|
1637 | struct timeval tv; |
---|
1638 | #if __BYTE_ORDER == __BIG_ENDIAN |
---|
1639 | tv.tv_sec = ts & 0xFFFFFFFF; |
---|
1640 | tv.tv_usec = ((ts >> 32) * 1000000) & 0xFFFFFFFF; |
---|
1641 | #elif __BYTE_ORDER == __LITTLE_ENDIAN |
---|
1642 | tv.tv_sec = ts >> 32; |
---|
1643 | tv.tv_usec = ((ts&0xFFFFFFFF)*1000000)>>32; |
---|
1644 | #else |
---|
1645 | #error "What on earth are you running this on?" |
---|
1646 | #endif |
---|
1647 | if (tv.tv_usec >= 1000000) { |
---|
1648 | tv.tv_usec -= 1000000; |
---|
1649 | tv.tv_sec += 1; |
---|
1650 | } |
---|
1651 | return trace->format->seek_timeval(trace,tv); |
---|
1652 | } |
---|
1653 | if (trace->format->seek_seconds) { |
---|
1654 | double seconds = |
---|
1655 | (ts>>32) + ((ts & UINT_MAX)*1.0 / UINT_MAX); |
---|
1656 | return trace->format->seek_seconds(trace,seconds); |
---|
1657 | } |
---|
1658 | trace_set_err(trace, |
---|
1659 | TRACE_ERR_OPTION_UNAVAIL, |
---|
1660 | "Feature unimplemented"); |
---|
1661 | return -1; |
---|
1662 | } |
---|
1663 | } |
---|
1664 | |
---|
1665 | DLLEXPORT int trace_seek_seconds(libtrace_t *trace, double seconds) |
---|
1666 | { |
---|
1667 | if (trace->format->seek_seconds) { |
---|
1668 | return trace->format->seek_seconds(trace,seconds); |
---|
1669 | } |
---|
1670 | else { |
---|
1671 | if (trace->format->seek_timeval) { |
---|
1672 | struct timeval tv; |
---|
1673 | tv.tv_sec = (uint32_t)seconds; |
---|
1674 | tv.tv_usec = (uint32_t)(((seconds - tv.tv_sec) * 1000000)/UINT_MAX); |
---|
1675 | return trace->format->seek_timeval(trace,tv); |
---|
1676 | } |
---|
1677 | if (trace->format->seek_erf) { |
---|
1678 | uint64_t timestamp = |
---|
1679 | ((uint64_t)((uint32_t)seconds) << 32) + \ |
---|
1680 | (uint64_t)(( seconds - (uint32_t)seconds ) * UINT_MAX); |
---|
1681 | return trace->format->seek_erf(trace,timestamp); |
---|
1682 | } |
---|
1683 | trace_set_err(trace, |
---|
1684 | TRACE_ERR_OPTION_UNAVAIL, |
---|
1685 | "Feature unimplemented"); |
---|
1686 | return -1; |
---|
1687 | } |
---|
1688 | } |
---|
1689 | |
---|
1690 | DLLEXPORT int trace_seek_timeval(libtrace_t *trace, struct timeval tv) |
---|
1691 | { |
---|
1692 | if (trace->format->seek_timeval) { |
---|
1693 | return trace->format->seek_timeval(trace,tv); |
---|
1694 | } |
---|
1695 | else { |
---|
1696 | if (trace->format->seek_erf) { |
---|
1697 | uint64_t timestamp = ((((uint64_t)tv.tv_sec) << 32) + \ |
---|
1698 | (((uint64_t)tv.tv_usec * UINT_MAX)/1000000)); |
---|
1699 | return trace->format->seek_erf(trace,timestamp); |
---|
1700 | } |
---|
1701 | if (trace->format->seek_seconds) { |
---|
1702 | double seconds = tv.tv_sec + ((tv.tv_usec * 1.0)/1000000); |
---|
1703 | return trace->format->seek_seconds(trace,seconds); |
---|
1704 | } |
---|
1705 | trace_set_err(trace, |
---|
1706 | TRACE_ERR_OPTION_UNAVAIL, |
---|
1707 | "Feature unimplemented"); |
---|
1708 | return -1; |
---|
1709 | } |
---|
1710 | } |
---|
1711 | |
---|
1712 | /* Converts a binary ethernet MAC address into a printable string */ |
---|
1713 | DLLEXPORT char *trace_ether_ntoa(const uint8_t *addr, char *buf) |
---|
1714 | { |
---|
1715 | static char staticbuf[18]={0,}; |
---|
1716 | if (!buf) |
---|
1717 | buf=staticbuf; |
---|
1718 | snprintf(buf,(size_t)18,"%02x:%02x:%02x:%02x:%02x:%02x", |
---|
1719 | addr[0],addr[1],addr[2], |
---|
1720 | addr[3],addr[4],addr[5]); |
---|
1721 | return buf; |
---|
1722 | } |
---|
1723 | |
---|
1724 | /* Converts a printable ethernet MAC address into a binary format */ |
---|
1725 | DLLEXPORT uint8_t *trace_ether_aton(const char *buf, uint8_t *addr) |
---|
1726 | { |
---|
1727 | uint8_t *buf2 = addr; |
---|
1728 | unsigned int tmp[6]; |
---|
1729 | static uint8_t staticaddr[6]; |
---|
1730 | if (!buf2) |
---|
1731 | buf2=staticaddr; |
---|
1732 | sscanf(buf,"%x:%x:%x:%x:%x:%x", |
---|
1733 | &tmp[0],&tmp[1],&tmp[2], |
---|
1734 | &tmp[3],&tmp[4],&tmp[5]); |
---|
1735 | buf2[0]=tmp[0]; buf2[1]=tmp[1]; buf2[2]=tmp[2]; |
---|
1736 | buf2[3]=tmp[3]; buf2[4]=tmp[4]; buf2[5]=tmp[5]; |
---|
1737 | return buf2; |
---|
1738 | } |
---|
1739 | |
---|
1740 | |
---|
1741 | /* Creates a libtrace packet from scratch using the contents of the provided |
---|
1742 | * buffer as the packet payload. |
---|
1743 | * |
---|
1744 | * Unlike trace_prepare_packet(), the buffer should not contain any capture |
---|
1745 | * format headers; instead this function will add the PCAP header to the |
---|
1746 | * packet record. This also means only PCAP packets can be constructed using |
---|
1747 | * this function. |
---|
1748 | * |
---|
1749 | */ |
---|
1750 | DLLEXPORT |
---|
1751 | void trace_construct_packet(libtrace_packet_t *packet, |
---|
1752 | libtrace_linktype_t linktype, |
---|
1753 | const void *data, |
---|
1754 | uint16_t len) |
---|
1755 | { |
---|
1756 | size_t size; |
---|
1757 | static libtrace_t *deadtrace=NULL; |
---|
1758 | libtrace_pcapfile_pkt_hdr_t hdr; |
---|
1759 | #ifdef WIN32 |
---|
1760 | struct _timeb tstruct; |
---|
1761 | #else |
---|
1762 | struct timeval tv; |
---|
1763 | #endif |
---|
1764 | |
---|
1765 | /* We need a trace to attach the constructed packet to (and it needs |
---|
1766 | * to be PCAP) */ |
---|
1767 | if (NULL == deadtrace) |
---|
1768 | deadtrace=trace_create_dead("pcapfile"); |
---|
1769 | |
---|
1770 | /* Fill in the new PCAP header */ |
---|
1771 | #ifdef WIN32 |
---|
1772 | _ftime(&tstruct); |
---|
1773 | hdr.ts_sec=tstruct.time; |
---|
1774 | hdr.ts_usec=tstruct.millitm * 1000; |
---|
1775 | #else |
---|
1776 | gettimeofday(&tv,NULL); |
---|
1777 | hdr.ts_sec=tv.tv_sec; |
---|
1778 | hdr.ts_usec=tv.tv_usec; |
---|
1779 | #endif |
---|
1780 | |
---|
1781 | hdr.caplen=len; |
---|
1782 | hdr.wirelen=len; |
---|
1783 | |
---|
1784 | /* Now fill in the libtrace packet itself */ |
---|
1785 | packet->trace=deadtrace; |
---|
1786 | size=len+sizeof(hdr); |
---|
1787 | if (packet->buf_control==TRACE_CTRL_PACKET) { |
---|
1788 | packet->buffer=realloc(packet->buffer,size); |
---|
1789 | } |
---|
1790 | else { |
---|
1791 | packet->buffer=malloc(size); |
---|
1792 | } |
---|
1793 | packet->buf_control=TRACE_CTRL_PACKET; |
---|
1794 | packet->header=packet->buffer; |
---|
1795 | packet->payload=(void*)((char*)packet->buffer+sizeof(hdr)); |
---|
1796 | |
---|
1797 | /* Ugh, memcpy - sadly necessary */ |
---|
1798 | memcpy(packet->header,&hdr,sizeof(hdr)); |
---|
1799 | memcpy(packet->payload,data,(size_t)len); |
---|
1800 | packet->type=pcap_linktype_to_rt(libtrace_to_pcap_linktype(linktype)); |
---|
1801 | |
---|
1802 | trace_clear_cache(packet); |
---|
1803 | } |
---|
1804 | |
---|
1805 | |
---|
1806 | uint64_t trace_get_received_packets(libtrace_t *trace) |
---|
1807 | { |
---|
1808 | assert(trace); |
---|
1809 | if (trace->format->get_received_packets) { |
---|
1810 | return trace->format->get_received_packets(trace); |
---|
1811 | } |
---|
1812 | return (uint64_t)-1; |
---|
1813 | } |
---|
1814 | |
---|
1815 | uint64_t trace_get_filtered_packets(libtrace_t *trace) |
---|
1816 | { |
---|
1817 | assert(trace); |
---|
1818 | if (trace->format->get_filtered_packets) { |
---|
1819 | return trace->format->get_filtered_packets(trace)+ |
---|
1820 | trace->filtered_packets; |
---|
1821 | } |
---|
1822 | return trace->filtered_packets; |
---|
1823 | } |
---|
1824 | |
---|
1825 | uint64_t trace_get_dropped_packets(libtrace_t *trace) |
---|
1826 | { |
---|
1827 | assert(trace); |
---|
1828 | if (trace->format->get_dropped_packets) { |
---|
1829 | return trace->format->get_dropped_packets(trace); |
---|
1830 | } |
---|
1831 | return (uint64_t)-1; |
---|
1832 | } |
---|
1833 | |
---|
1834 | uint64_t trace_get_accepted_packets(libtrace_t *trace) |
---|
1835 | { |
---|
1836 | assert(trace); |
---|
1837 | return trace->accepted_packets; |
---|
1838 | } |
---|
1839 | |
---|
1840 | void trace_clear_cache(libtrace_packet_t *packet) { |
---|
1841 | |
---|
1842 | packet->l2_header = NULL; |
---|
1843 | packet->l3_header = NULL; |
---|
1844 | packet->l4_header = NULL; |
---|
1845 | packet->link_type = 0; |
---|
1846 | packet->l3_ethertype = 0; |
---|
1847 | packet->transport_proto = 0; |
---|
1848 | packet->capture_length = -1; |
---|
1849 | packet->wire_length = -1; |
---|
1850 | packet->payload_length = -1; |
---|
1851 | packet->l2_remaining = 0; |
---|
1852 | packet->l3_remaining = 0; |
---|
1853 | packet->l4_remaining = 0; |
---|
1854 | |
---|
1855 | } |
---|
1856 | |
---|
1857 | void trace_interrupt(void) { |
---|
1858 | libtrace_halt = 1; |
---|
1859 | } |
---|
1860 | |
---|
1861 | void register_format(struct libtrace_format_t *f) { |
---|
1862 | assert(f->next==NULL); /* Can't register a format twice */ |
---|
1863 | f->next=formats_list; |
---|
1864 | formats_list=f; |
---|
1865 | |
---|
1866 | /* Now, verify that the format has at least the minimum functionality. |
---|
1867 | * |
---|
1868 | * This #if can be changed to a 1 to output warnings about inconsistent |
---|
1869 | * functions being provided by format modules. This generally is very |
---|
1870 | * noisy, as almost all modules don't implement one or more functions |
---|
1871 | * for various reasons. This is very useful when checking a new |
---|
1872 | * format module is sane. |
---|
1873 | */ |
---|
1874 | #if 0 |
---|
1875 | if (f->init_input) { |
---|
1876 | #define REQUIRE(x) \ |
---|
1877 | if (!f->x) \ |
---|
1878 | fprintf(stderr,"%s: Input format should provide " #x "\n",f->name) |
---|
1879 | REQUIRE(read_packet); |
---|
1880 | REQUIRE(start_input); |
---|
1881 | REQUIRE(fin_input); |
---|
1882 | REQUIRE(get_link_type); |
---|
1883 | REQUIRE(get_capture_length); |
---|
1884 | REQUIRE(get_wire_length); |
---|
1885 | REQUIRE(get_framing_length); |
---|
1886 | REQUIRE(trace_event); |
---|
1887 | if (!f->get_erf_timestamp |
---|
1888 | && !f->get_seconds |
---|
1889 | && !f->get_timeval) { |
---|
1890 | fprintf(stderr,"%s: A trace format capable of input, should provide at least one of\n" |
---|
1891 | "get_erf_timestamp, get_seconds or trace_timeval\n",f->name); |
---|
1892 | } |
---|
1893 | if (f->trace_event!=trace_event_trace) { |
---|
1894 | /* Theres nothing that a trace file could optimise with |
---|
1895 | * config_input |
---|
1896 | */ |
---|
1897 | REQUIRE(pause_input); |
---|
1898 | REQUIRE(config_input); |
---|
1899 | REQUIRE(get_fd); |
---|
1900 | } |
---|
1901 | else { |
---|
1902 | if (f->get_fd) { |
---|
1903 | fprintf(stderr,"%s: Unnecessary get_fd\n", |
---|
1904 | f->name); |
---|
1905 | } |
---|
1906 | } |
---|
1907 | #undef REQUIRE |
---|
1908 | } |
---|
1909 | else { |
---|
1910 | #define REQUIRE(x) \ |
---|
1911 | if (f->x) \ |
---|
1912 | fprintf(stderr,"%s: Non Input format shouldn't need " #x "\n",f->name) |
---|
1913 | REQUIRE(read_packet); |
---|
1914 | REQUIRE(start_input); |
---|
1915 | REQUIRE(pause_input); |
---|
1916 | REQUIRE(fin_input); |
---|
1917 | REQUIRE(get_link_type); |
---|
1918 | REQUIRE(get_capture_length); |
---|
1919 | REQUIRE(get_wire_length); |
---|
1920 | REQUIRE(get_framing_length); |
---|
1921 | REQUIRE(trace_event); |
---|
1922 | REQUIRE(get_seconds); |
---|
1923 | REQUIRE(get_timeval); |
---|
1924 | REQUIRE(get_erf_timestamp); |
---|
1925 | #undef REQUIRE |
---|
1926 | } |
---|
1927 | if (f->init_output) { |
---|
1928 | #define REQUIRE(x) \ |
---|
1929 | if (!f->x) \ |
---|
1930 | fprintf(stderr,"%s: Output format should provide " #x "\n",f->name) |
---|
1931 | REQUIRE(write_packet); |
---|
1932 | REQUIRE(start_output); |
---|
1933 | REQUIRE(config_output); |
---|
1934 | REQUIRE(fin_output); |
---|
1935 | #undef REQUIRE |
---|
1936 | } |
---|
1937 | else { |
---|
1938 | #define REQUIRE(x) \ |
---|
1939 | if (f->x) \ |
---|
1940 | fprintf(stderr,"%s: Non Output format shouldn't need " #x "\n",f->name) |
---|
1941 | REQUIRE(write_packet); |
---|
1942 | REQUIRE(start_output); |
---|
1943 | REQUIRE(config_output); |
---|
1944 | REQUIRE(fin_output); |
---|
1945 | #undef REQUIRE |
---|
1946 | } |
---|
1947 | #endif |
---|
1948 | } |
---|
1949 | |
---|