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