1 | /* |
---|
2 | * |
---|
3 | * Copyright (c) 2007-2016 The University of Waikato, Hamilton, New Zealand. |
---|
4 | * All rights reserved. |
---|
5 | * |
---|
6 | * This file is part of libtrace. |
---|
7 | * |
---|
8 | * This code has been developed by the University of Waikato WAND |
---|
9 | * research group. For further information please see http://www.wand.net.nz/ |
---|
10 | * |
---|
11 | * libtrace is free software; you can redistribute it and/or modify |
---|
12 | * it under the terms of the GNU Lesser General Public License as published by |
---|
13 | * the Free Software Foundation; either version 3 of the License, or |
---|
14 | * (at your option) any later version. |
---|
15 | * |
---|
16 | * libtrace is distributed in the hope that it will be useful, |
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 | * GNU Lesser General Public License for more details. |
---|
20 | * |
---|
21 | * You should have received a copy of the GNU Lesser General Public License |
---|
22 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | * |
---|
24 | * |
---|
25 | */ |
---|
26 | |
---|
27 | #define _GNU_SOURCE |
---|
28 | #include "common.h" |
---|
29 | #include "config.h" |
---|
30 | #include <assert.h> |
---|
31 | #include <errno.h> |
---|
32 | #include <fcntl.h> |
---|
33 | #include <stdio.h> |
---|
34 | #include <stdlib.h> |
---|
35 | #include <string.h> |
---|
36 | #include <sys/stat.h> |
---|
37 | #include <sys/types.h> |
---|
38 | #ifndef WIN32 |
---|
39 | #include <sys/socket.h> |
---|
40 | #endif |
---|
41 | #include <stdarg.h> |
---|
42 | #include <sys/param.h> |
---|
43 | |
---|
44 | #ifdef HAVE_LIMITS_H |
---|
45 | # include <limits.h> |
---|
46 | #endif |
---|
47 | |
---|
48 | #ifdef HAVE_SYS_LIMITS_H |
---|
49 | # include <sys/limits.h> |
---|
50 | #endif |
---|
51 | |
---|
52 | #ifdef HAVE_NET_IF_ARP_H |
---|
53 | # include <net/if_arp.h> |
---|
54 | #endif |
---|
55 | |
---|
56 | #ifdef HAVE_NET_IF_H |
---|
57 | # include <net/if.h> |
---|
58 | #endif |
---|
59 | |
---|
60 | #ifdef HAVE_NETINET_IN_H |
---|
61 | # include <netinet/in.h> |
---|
62 | #endif |
---|
63 | |
---|
64 | #ifdef HAVE_NET_ETHERNET_H |
---|
65 | # include <net/ethernet.h> |
---|
66 | #endif |
---|
67 | |
---|
68 | #ifdef HAVE_NETINET_IF_ETHER_H |
---|
69 | # include <netinet/if_ether.h> |
---|
70 | #endif |
---|
71 | |
---|
72 | #include <time.h> |
---|
73 | #ifdef WIN32 |
---|
74 | #include <sys/timeb.h> |
---|
75 | #endif |
---|
76 | |
---|
77 | #include "libtrace.h" |
---|
78 | #include "libtrace_int.h" |
---|
79 | |
---|
80 | #ifdef HAVE_PCAP_BPF_H |
---|
81 | # include <pcap-bpf.h> |
---|
82 | #else |
---|
83 | # ifdef HAVE_NET_BPF_H |
---|
84 | # include <net/bpf.h> |
---|
85 | # endif |
---|
86 | #endif |
---|
87 | |
---|
88 | |
---|
89 | #include "libtrace_int.h" |
---|
90 | #include "format_helper.h" |
---|
91 | #include "rt_protocol.h" |
---|
92 | |
---|
93 | #include <pthread.h> |
---|
94 | #include <signal.h> |
---|
95 | |
---|
96 | #define MAXOPTS 1024 |
---|
97 | |
---|
98 | /* This file contains much of the implementation of the libtrace API itself. */ |
---|
99 | |
---|
100 | static struct libtrace_format_t *formats_list = NULL; |
---|
101 | |
---|
102 | volatile int libtrace_halt = 0; |
---|
103 | /* Set once pstart is called used for backwards compatibility reasons */ |
---|
104 | int libtrace_parallel = 0; |
---|
105 | |
---|
106 | static const libtrace_packet_cache_t clearcache = { |
---|
107 | -1, -1, -1, -1, NULL, 0, 0, NULL, 0, 0, NULL, 0, 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 inline void xstrncpy(char *dest, const char *src, size_t n, |
---|
113 | size_t destlen) |
---|
114 | { |
---|
115 | size_t slen = destlen - 1; |
---|
116 | if (n < slen) { |
---|
117 | slen = n; |
---|
118 | } |
---|
119 | strncpy(dest,src,slen); |
---|
120 | dest[slen]='\0'; |
---|
121 | } |
---|
122 | |
---|
123 | static char *xstrndup(const char *src,size_t n) |
---|
124 | { |
---|
125 | char *ret=(char*)malloc(n+1); |
---|
126 | if (ret==NULL) { |
---|
127 | fprintf(stderr,"Out of memory\n"); |
---|
128 | exit(EXIT_FAILURE); |
---|
129 | } |
---|
130 | xstrncpy(ret,src,n,n+1); |
---|
131 | return ret; |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | /* call all the constructors if they haven't yet all been called */ |
---|
136 | __attribute__((constructor)) |
---|
137 | static void trace_init(void) |
---|
138 | { |
---|
139 | struct timeval tv; |
---|
140 | gettimeofday(&tv, NULL); |
---|
141 | srand(tv.tv_sec); |
---|
142 | |
---|
143 | if (!formats_list) { |
---|
144 | duck_constructor(); |
---|
145 | erf_constructor(); |
---|
146 | tsh_constructor(); |
---|
147 | legacy_constructor(); |
---|
148 | atmhdr_constructor(); |
---|
149 | linuxring_constructor(); |
---|
150 | linuxnative_constructor(); |
---|
151 | #ifdef HAVE_LIBPCAP |
---|
152 | pcap_constructor(); |
---|
153 | #endif |
---|
154 | bpf_constructor(); |
---|
155 | pcapfile_constructor(); |
---|
156 | pcapng_constructor(); |
---|
157 | rt_constructor(); |
---|
158 | ndag_constructor(); |
---|
159 | #ifdef HAVE_WANDDER |
---|
160 | etsilive_constructor(); |
---|
161 | #endif |
---|
162 | #ifdef HAVE_DAG |
---|
163 | dag_constructor(); |
---|
164 | #endif |
---|
165 | #ifdef HAVE_DPDK |
---|
166 | dpdk_constructor(); |
---|
167 | dpdkndag_constructor(); |
---|
168 | #endif |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | /* Prints help information for libtrace |
---|
173 | * |
---|
174 | * Function prints out some basic help information regarding libtrace, |
---|
175 | * and then prints out the help() function registered with each input module |
---|
176 | */ |
---|
177 | DLLEXPORT void trace_help(void) { |
---|
178 | struct libtrace_format_t *tmp; |
---|
179 | trace_init(); |
---|
180 | printf("libtrace %s\n\n",PACKAGE_VERSION); |
---|
181 | printf("Following this are a list of the format modules supported in this build of libtrace\n\n"); |
---|
182 | for(tmp=formats_list;tmp;tmp=tmp->next) { |
---|
183 | if (tmp->help) |
---|
184 | tmp->help(); |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | #define URI_PROTO_LINE 16U |
---|
189 | |
---|
190 | /* Try to guess which format module is appropriate for a given trace file or |
---|
191 | * device */ |
---|
192 | static void guess_format(libtrace_t *libtrace, const char *filename) |
---|
193 | { |
---|
194 | struct libtrace_format_t *tmp; |
---|
195 | |
---|
196 | /* Try and guess based on filename */ |
---|
197 | for(tmp = formats_list; tmp; tmp=tmp->next) { |
---|
198 | if (tmp->probe_filename && tmp->probe_filename(filename)) { |
---|
199 | libtrace->format = tmp; |
---|
200 | libtrace->uridata = strdup(filename); |
---|
201 | return; |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | libtrace->io = wandio_create(filename); |
---|
206 | if (!libtrace->io) { |
---|
207 | trace_set_err(libtrace,TRACE_ERR_URI_NOT_FOUND,"Unable to find URI (%s)", filename); |
---|
208 | return; |
---|
209 | } |
---|
210 | |
---|
211 | /* Try and guess based on file magic */ |
---|
212 | for(tmp = formats_list; tmp; tmp=tmp->next) { |
---|
213 | if (tmp->probe_magic && tmp->probe_magic(libtrace->io)) { |
---|
214 | libtrace->format = tmp; |
---|
215 | libtrace->uridata = strdup(filename); |
---|
216 | return; |
---|
217 | } |
---|
218 | } |
---|
219 | |
---|
220 | /* No formats matched -- make sure we clean up the IO object we |
---|
221 | * used to probe the file magic */ |
---|
222 | wandio_destroy(libtrace->io); |
---|
223 | trace_set_err(libtrace,TRACE_ERR_BAD_FORMAT,"Unable to guess format (%s)", filename); |
---|
224 | return; |
---|
225 | } |
---|
226 | |
---|
227 | /* Creates an input trace from a URI |
---|
228 | * |
---|
229 | * @params char * containing a valid libtrace URI |
---|
230 | * @returns opaque pointer to a libtrace_t |
---|
231 | * |
---|
232 | * Some valid URI's are: |
---|
233 | * erf:/path/to/erf/file |
---|
234 | * erf:/path/to/erf/file.gz |
---|
235 | * erf:- (stdin) |
---|
236 | * dag:/dev/dagcard |
---|
237 | * pcapint:pcapinterface (eg: pcapint:eth0) |
---|
238 | * pcapfile:/path/to/pcap/file |
---|
239 | * pcapfile:- |
---|
240 | * int:interface (eg: int:eth0) only on Linux |
---|
241 | * rt:hostname |
---|
242 | * rt:hostname:port |
---|
243 | * |
---|
244 | * If an error occured when attempting to open a trace, NULL is returned |
---|
245 | * and an error is output to stdout. |
---|
246 | */ |
---|
247 | DLLEXPORT libtrace_t *trace_create(const char *uri) { |
---|
248 | libtrace_t *libtrace = |
---|
249 | (libtrace_t *)malloc(sizeof(libtrace_t)); |
---|
250 | char *scan = 0; |
---|
251 | const char *uridata = 0; |
---|
252 | |
---|
253 | trace_init(); |
---|
254 | |
---|
255 | if (!libtrace) { |
---|
256 | fprintf(stderr, "Unable to allocate memory in trace_create()\n"); |
---|
257 | return NULL; |
---|
258 | } |
---|
259 | |
---|
260 | if(!uri) { |
---|
261 | trace_set_err(libtrace, TRACE_ERR_URI_NULL, "NULL uri passed to trace_create()"); |
---|
262 | return libtrace; |
---|
263 | } |
---|
264 | |
---|
265 | libtrace->err.err_num = TRACE_ERR_NOERROR; |
---|
266 | libtrace->format=NULL; |
---|
267 | |
---|
268 | libtrace->event.packet = NULL; |
---|
269 | libtrace->event.psize = 0; |
---|
270 | libtrace->event.first_ts = 0.0; |
---|
271 | libtrace->event.first_now = 0.0; |
---|
272 | libtrace->event.waiting = false; |
---|
273 | libtrace->filter = NULL; |
---|
274 | libtrace->snaplen = 0; |
---|
275 | libtrace->replayspeedup = 1; |
---|
276 | libtrace->started=false; |
---|
277 | libtrace->startcount=0; |
---|
278 | libtrace->uridata = NULL; |
---|
279 | libtrace->io = NULL; |
---|
280 | libtrace->filtered_packets = 0; |
---|
281 | libtrace->accepted_packets = 0; |
---|
282 | libtrace->last_packet = NULL; |
---|
283 | |
---|
284 | /* Parallel inits */ |
---|
285 | ASSERT_RET(pthread_mutex_init(&libtrace->libtrace_lock, NULL), == 0); |
---|
286 | ASSERT_RET(pthread_mutex_init(&libtrace->read_packet_lock, NULL), == 0); |
---|
287 | ASSERT_RET(pthread_cond_init(&libtrace->perpkt_cond, NULL), == 0); |
---|
288 | libtrace->state = STATE_NEW; |
---|
289 | libtrace->perpkt_queue_full = false; |
---|
290 | libtrace->global_blob = NULL; |
---|
291 | libtrace->hasher = NULL; |
---|
292 | libtrace->hasher_data = NULL; |
---|
293 | libtrace->hasher_owner = HASH_OWNED_EXTERNAL; |
---|
294 | libtrace_zero_ocache(&libtrace->packet_freelist); |
---|
295 | libtrace_zero_thread(&libtrace->hasher_thread); |
---|
296 | libtrace_zero_thread(&libtrace->reporter_thread); |
---|
297 | libtrace_zero_thread(&libtrace->keepalive_thread); |
---|
298 | libtrace->reporter_thread.type = THREAD_EMPTY; |
---|
299 | libtrace->perpkt_thread_count = 0; |
---|
300 | libtrace->perpkt_threads = NULL; |
---|
301 | libtrace->tracetime = 0; |
---|
302 | libtrace->first_packets.first = 0; |
---|
303 | libtrace->first_packets.count = 0; |
---|
304 | libtrace->first_packets.packets = NULL; |
---|
305 | libtrace->stats = NULL; |
---|
306 | libtrace->pread = NULL; |
---|
307 | libtrace->sequence_number = 0; |
---|
308 | ZERO_USER_CONFIG(libtrace->config); |
---|
309 | memset(&libtrace->combiner, 0, sizeof(libtrace->combiner)); |
---|
310 | libtrace->perpkt_cbs = NULL; |
---|
311 | libtrace->reporter_cbs = NULL; |
---|
312 | |
---|
313 | /* Parse the URI to determine what sort of trace we are dealing with */ |
---|
314 | if ((uridata = trace_parse_uri(uri, &scan)) == 0) { |
---|
315 | /* Could not parse the URI nicely */ |
---|
316 | guess_format(libtrace,uri); |
---|
317 | if (trace_is_err(libtrace)) { |
---|
318 | return libtrace; |
---|
319 | } |
---|
320 | } |
---|
321 | else { |
---|
322 | struct libtrace_format_t *tmp; |
---|
323 | |
---|
324 | /* Find a format that matches the first part of the URI */ |
---|
325 | for (tmp=formats_list;tmp;tmp=tmp->next) { |
---|
326 | if (strlen(scan) == strlen(tmp->name) && |
---|
327 | strncasecmp(scan, tmp->name, strlen(scan)) == 0 |
---|
328 | ) { |
---|
329 | libtrace->format=tmp; |
---|
330 | break; |
---|
331 | } |
---|
332 | } |
---|
333 | |
---|
334 | if (libtrace->format == 0) { |
---|
335 | trace_set_err(libtrace, TRACE_ERR_BAD_FORMAT, |
---|
336 | "Unknown format (%s)",scan); |
---|
337 | return libtrace; |
---|
338 | } |
---|
339 | |
---|
340 | libtrace->uridata = strdup(uridata); |
---|
341 | } |
---|
342 | /* libtrace->format now contains the type of uri |
---|
343 | * libtrace->uridata contains the appropriate data for this |
---|
344 | */ |
---|
345 | |
---|
346 | /* Call the init_input function for the matching capture format */ |
---|
347 | if (libtrace->format->init_input) { |
---|
348 | int err=libtrace->format->init_input(libtrace); |
---|
349 | if (err==-1) { |
---|
350 | /* init_input should call trace_set_err to set the |
---|
351 | * error message |
---|
352 | */ |
---|
353 | return libtrace; |
---|
354 | } |
---|
355 | } else { |
---|
356 | trace_set_err(libtrace,TRACE_ERR_UNSUPPORTED, |
---|
357 | "Format does not support input (%s)",scan); |
---|
358 | return libtrace; |
---|
359 | } |
---|
360 | |
---|
361 | if (scan) |
---|
362 | free(scan); |
---|
363 | libtrace->err.err_num=TRACE_ERR_NOERROR; |
---|
364 | libtrace->err.problem[0]='\0'; |
---|
365 | return libtrace; |
---|
366 | } |
---|
367 | |
---|
368 | /* Creates a "dummy" trace file that has only the format type set. |
---|
369 | * |
---|
370 | * @returns opaque pointer to a (sparsely initialised) libtrace_t |
---|
371 | * |
---|
372 | * IMPORTANT: Do not attempt to call trace_read_packet or other such functions |
---|
373 | * with the dummy trace. Its intended purpose is to act as a packet->trace for |
---|
374 | * libtrace_packet_t's that are not associated with a libtrace_t structure. |
---|
375 | */ |
---|
376 | DLLEXPORT libtrace_t * trace_create_dead (const char *uri) { |
---|
377 | libtrace_t *libtrace = (libtrace_t *) malloc(sizeof(libtrace_t)); |
---|
378 | char *scan = (char *)calloc(sizeof(char),URI_PROTO_LINE); |
---|
379 | char *uridata; |
---|
380 | struct libtrace_format_t *tmp; |
---|
381 | |
---|
382 | trace_init(); |
---|
383 | |
---|
384 | libtrace->err.err_num = TRACE_ERR_NOERROR; |
---|
385 | |
---|
386 | if((uridata = strchr(uri,':')) == NULL) { |
---|
387 | xstrncpy(scan, uri, strlen(uri), URI_PROTO_LINE); |
---|
388 | } else { |
---|
389 | xstrncpy(scan,uri, (size_t)(uridata - uri), URI_PROTO_LINE); |
---|
390 | } |
---|
391 | |
---|
392 | libtrace->err.err_num = TRACE_ERR_NOERROR; |
---|
393 | libtrace->format=NULL; |
---|
394 | |
---|
395 | libtrace->event.packet = NULL; |
---|
396 | libtrace->event.psize = 0; |
---|
397 | libtrace->event.first_ts = 0; |
---|
398 | libtrace->event.first_now = 0; |
---|
399 | libtrace->filter = NULL; |
---|
400 | libtrace->snaplen = 0; |
---|
401 | libtrace->started=false; |
---|
402 | libtrace->startcount = 0; |
---|
403 | libtrace->uridata = NULL; |
---|
404 | libtrace->io = NULL; |
---|
405 | libtrace->filtered_packets = 0; |
---|
406 | libtrace->accepted_packets = 0; |
---|
407 | libtrace->last_packet = NULL; |
---|
408 | |
---|
409 | /* Parallel inits */ |
---|
410 | ASSERT_RET(pthread_mutex_init(&libtrace->libtrace_lock, NULL), == 0); |
---|
411 | ASSERT_RET(pthread_mutex_init(&libtrace->read_packet_lock, NULL), == 0); |
---|
412 | ASSERT_RET(pthread_cond_init(&libtrace->perpkt_cond, NULL), == 0); |
---|
413 | libtrace->state = STATE_NEW; // TODO MAYBE DEAD |
---|
414 | libtrace->perpkt_queue_full = false; |
---|
415 | libtrace->global_blob = NULL; |
---|
416 | libtrace->hasher = NULL; |
---|
417 | libtrace_zero_ocache(&libtrace->packet_freelist); |
---|
418 | libtrace_zero_thread(&libtrace->hasher_thread); |
---|
419 | libtrace_zero_thread(&libtrace->reporter_thread); |
---|
420 | libtrace_zero_thread(&libtrace->keepalive_thread); |
---|
421 | libtrace->reporter_thread.type = THREAD_EMPTY; |
---|
422 | libtrace->perpkt_thread_count = 0; |
---|
423 | libtrace->perpkt_threads = NULL; |
---|
424 | libtrace->tracetime = 0; |
---|
425 | libtrace->stats = NULL; |
---|
426 | libtrace->pread = NULL; |
---|
427 | libtrace->sequence_number = 0; |
---|
428 | ZERO_USER_CONFIG(libtrace->config); |
---|
429 | memset(&libtrace->combiner, 0, sizeof(libtrace->combiner)); |
---|
430 | libtrace->perpkt_cbs = NULL; |
---|
431 | libtrace->reporter_cbs = NULL; |
---|
432 | for(tmp=formats_list;tmp;tmp=tmp->next) { |
---|
433 | if (strlen(scan) == strlen(tmp->name) && |
---|
434 | !strncasecmp(scan, |
---|
435 | tmp->name, |
---|
436 | strlen(scan))) { |
---|
437 | libtrace->format=tmp; |
---|
438 | break; |
---|
439 | } |
---|
440 | } |
---|
441 | if (libtrace->format == 0) { |
---|
442 | trace_set_err(libtrace,TRACE_ERR_BAD_FORMAT, |
---|
443 | "Unknown format (%s)",scan); |
---|
444 | } |
---|
445 | |
---|
446 | libtrace->format_data = NULL; |
---|
447 | free(scan); |
---|
448 | return libtrace; |
---|
449 | |
---|
450 | } |
---|
451 | |
---|
452 | /* Creates an output trace from a URI. |
---|
453 | * |
---|
454 | * @param uri the uri string describing the output format and destination |
---|
455 | * @returns opaque pointer to a libtrace_output_t |
---|
456 | * |
---|
457 | * If an error occured when attempting to open the output trace, NULL is |
---|
458 | * returned and trace_errno is set. |
---|
459 | */ |
---|
460 | |
---|
461 | DLLEXPORT libtrace_out_t *trace_create_output(const char *uri) { |
---|
462 | libtrace_out_t *libtrace = |
---|
463 | (libtrace_out_t*)malloc(sizeof(libtrace_out_t)); |
---|
464 | |
---|
465 | char *scan = 0; |
---|
466 | const char *uridata = 0; |
---|
467 | struct libtrace_format_t *tmp; |
---|
468 | |
---|
469 | trace_init(); |
---|
470 | |
---|
471 | libtrace->err.err_num = TRACE_ERR_NOERROR; |
---|
472 | strcpy(libtrace->err.problem,"Error message set\n"); |
---|
473 | libtrace->format = NULL; |
---|
474 | libtrace->uridata = NULL; |
---|
475 | |
---|
476 | /* Parse the URI to determine what capture format we want to write */ |
---|
477 | |
---|
478 | if ((uridata = trace_parse_uri(uri, &scan)) == 0) { |
---|
479 | trace_set_err_out(libtrace,TRACE_ERR_BAD_FORMAT, |
---|
480 | "Bad uri format (%s)",uri); |
---|
481 | return libtrace; |
---|
482 | } |
---|
483 | |
---|
484 | /* Attempt to find the format in the list of supported formats */ |
---|
485 | for(tmp=formats_list;tmp;tmp=tmp->next) { |
---|
486 | if (strlen(scan) == strlen(tmp->name) && |
---|
487 | !strncasecmp(scan, |
---|
488 | tmp->name, |
---|
489 | strlen(scan))) { |
---|
490 | libtrace->format=tmp; |
---|
491 | break; |
---|
492 | } |
---|
493 | } |
---|
494 | free(scan); |
---|
495 | |
---|
496 | if (libtrace->format == NULL) { |
---|
497 | trace_set_err_out(libtrace,TRACE_ERR_BAD_FORMAT, |
---|
498 | "Unknown output format (%s)",scan); |
---|
499 | return libtrace; |
---|
500 | } |
---|
501 | libtrace->uridata = strdup(uridata); |
---|
502 | |
---|
503 | /* libtrace->format now contains the type of uri |
---|
504 | * libtrace->uridata contains the appropriate data for this |
---|
505 | */ |
---|
506 | |
---|
507 | if (libtrace->format->init_output) { |
---|
508 | int err = libtrace->format->init_output(libtrace); |
---|
509 | if (err == -1) { |
---|
510 | /* init_output should call trace_set_err to set the |
---|
511 | * error message |
---|
512 | */ |
---|
513 | return libtrace; |
---|
514 | } |
---|
515 | } else { |
---|
516 | trace_set_err_out(libtrace,TRACE_ERR_UNSUPPORTED, |
---|
517 | "Format does not support writing (%s)",scan); |
---|
518 | return libtrace; |
---|
519 | } |
---|
520 | |
---|
521 | |
---|
522 | libtrace->started=false; |
---|
523 | return libtrace; |
---|
524 | } |
---|
525 | |
---|
526 | /* Start an input trace |
---|
527 | * @param libtrace the input trace to start |
---|
528 | * @returns 0 on success |
---|
529 | * |
---|
530 | * This does the work associated with actually starting up |
---|
531 | * the trace. it may fail. |
---|
532 | */ |
---|
533 | DLLEXPORT int trace_start(libtrace_t *libtrace) |
---|
534 | { |
---|
535 | if(!libtrace) { |
---|
536 | fprintf(stderr, "NULL trace passed to trace_start()\n"); |
---|
537 | return TRACE_ERR_NULL_TRACE; |
---|
538 | } |
---|
539 | |
---|
540 | if (trace_is_err(libtrace)) |
---|
541 | return -1; |
---|
542 | if (libtrace->format->start_input) { |
---|
543 | int ret=libtrace->format->start_input(libtrace); |
---|
544 | if (ret < 0) { |
---|
545 | return ret; |
---|
546 | } |
---|
547 | } |
---|
548 | libtrace->startcount ++; |
---|
549 | libtrace->started=true; |
---|
550 | return 0; |
---|
551 | } |
---|
552 | |
---|
553 | /* Start an output trace */ |
---|
554 | DLLEXPORT int trace_start_output(libtrace_out_t *libtrace) |
---|
555 | { |
---|
556 | if(!libtrace) { |
---|
557 | fprintf(stderr, "NULL trace passed to trace_start_output()\n"); |
---|
558 | return TRACE_ERR_NULL_TRACE; |
---|
559 | } |
---|
560 | if (libtrace->format->start_output) { |
---|
561 | int ret=libtrace->format->start_output(libtrace); |
---|
562 | if (ret < 0) { |
---|
563 | return ret; |
---|
564 | } |
---|
565 | } |
---|
566 | |
---|
567 | libtrace->started=true; |
---|
568 | return 0; |
---|
569 | } |
---|
570 | |
---|
571 | DLLEXPORT int trace_pause(libtrace_t *libtrace) |
---|
572 | { |
---|
573 | if(!libtrace) { |
---|
574 | fprintf(stderr, "NULL trace passed to trace_pause()\n"); |
---|
575 | return TRACE_ERR_NULL_TRACE; |
---|
576 | } |
---|
577 | if (!libtrace->started) { |
---|
578 | trace_set_err(libtrace,TRACE_ERR_BAD_STATE, "You must call trace_start() before calling trace_pause()"); |
---|
579 | return -1; |
---|
580 | } |
---|
581 | |
---|
582 | /* Finish the last packet we read - for backwards compatibility */ |
---|
583 | if (!libtrace_parallel && libtrace->last_packet) |
---|
584 | trace_fin_packet(libtrace->last_packet); |
---|
585 | if(libtrace->last_packet != NULL) { |
---|
586 | trace_set_err(libtrace, TRACE_ERR_PAUSE_FIN, "Unable to remove all data stored against trace in trace_pause()"); |
---|
587 | return -1; |
---|
588 | } |
---|
589 | |
---|
590 | if (libtrace->format->pause_input) |
---|
591 | libtrace->format->pause_input(libtrace); |
---|
592 | |
---|
593 | libtrace->started=false; |
---|
594 | return 0; |
---|
595 | } |
---|
596 | |
---|
597 | DLLEXPORT int trace_config(libtrace_t *libtrace, |
---|
598 | trace_option_t option, |
---|
599 | void *value) |
---|
600 | { |
---|
601 | int ret; |
---|
602 | |
---|
603 | if (trace_is_err(libtrace)) { |
---|
604 | return -1; |
---|
605 | } |
---|
606 | |
---|
607 | if (option == TRACE_OPTION_HASHER) |
---|
608 | return trace_set_hasher(libtrace, |
---|
609 | (enum hasher_types) *((int *) value), |
---|
610 | NULL, NULL); |
---|
611 | |
---|
612 | /* If the capture format supports configuration, try using their |
---|
613 | * native configuration first */ |
---|
614 | if (libtrace->format->config_input) { |
---|
615 | ret=libtrace->format->config_input(libtrace,option,value); |
---|
616 | if (ret==0) |
---|
617 | return 0; |
---|
618 | } |
---|
619 | |
---|
620 | /* If we get here, either the native configuration failed or the |
---|
621 | * format did not support configuration. However, libtrace can |
---|
622 | * deal with some options itself, so give that a go */ |
---|
623 | switch(option) { |
---|
624 | case TRACE_OPTION_REPLAY_SPEEDUP: |
---|
625 | /* Clear the error if there was one */ |
---|
626 | if (trace_is_err(libtrace)) { |
---|
627 | trace_get_err(libtrace); |
---|
628 | } |
---|
629 | if (*(int*)value<1 |
---|
630 | || *(int*)value>LIBTRACE_MAX_REPLAY_SPEEDUP) { |
---|
631 | trace_set_err(libtrace,TRACE_ERR_BAD_STATE, |
---|
632 | "Invalid replay speed"); |
---|
633 | } |
---|
634 | libtrace->replayspeedup=*(int*)value; |
---|
635 | return 0; |
---|
636 | |
---|
637 | case TRACE_OPTION_SNAPLEN: |
---|
638 | /* Clear the error if there was one */ |
---|
639 | if (trace_is_err(libtrace)) { |
---|
640 | trace_get_err(libtrace); |
---|
641 | } |
---|
642 | if (*(int*)value<0 |
---|
643 | || *(int*)value>LIBTRACE_PACKET_BUFSIZE) { |
---|
644 | trace_set_err(libtrace,TRACE_ERR_BAD_STATE, |
---|
645 | "Invalid snap length"); |
---|
646 | } |
---|
647 | libtrace->snaplen=*(int*)value; |
---|
648 | return 0; |
---|
649 | case TRACE_OPTION_FILTER: |
---|
650 | /* Clear the error if there was one */ |
---|
651 | if (trace_is_err(libtrace)) { |
---|
652 | trace_get_err(libtrace); |
---|
653 | } |
---|
654 | libtrace->filter=(libtrace_filter_t *)value; |
---|
655 | return 0; |
---|
656 | case TRACE_OPTION_PROMISC: |
---|
657 | if (!trace_is_err(libtrace)) { |
---|
658 | trace_set_err(libtrace,TRACE_ERR_OPTION_UNAVAIL, |
---|
659 | "Promisc mode is not supported by this format module"); |
---|
660 | } |
---|
661 | return -1; |
---|
662 | case TRACE_OPTION_META_FREQ: |
---|
663 | if (!trace_is_err(libtrace)) { |
---|
664 | trace_set_err(libtrace, |
---|
665 | TRACE_ERR_OPTION_UNAVAIL, |
---|
666 | "This format does not support meta-data gathering"); |
---|
667 | } |
---|
668 | return -1; |
---|
669 | case TRACE_OPTION_EVENT_REALTIME: |
---|
670 | if (!trace_is_err(libtrace)) { |
---|
671 | trace_set_err(libtrace, |
---|
672 | TRACE_ERR_OPTION_UNAVAIL, |
---|
673 | "This format does not support realtime events"); |
---|
674 | } |
---|
675 | return -1; |
---|
676 | case TRACE_OPTION_HASHER: |
---|
677 | /* Dealt with earlier */ |
---|
678 | return -1; |
---|
679 | case TRACE_OPTION_CONSTANT_ERF_FRAMING: |
---|
680 | if (!trace_is_err(libtrace)) { |
---|
681 | trace_set_err(libtrace, |
---|
682 | TRACE_ERR_OPTION_UNAVAIL, |
---|
683 | "This format does not feature an ERF header or does not support bypassing the framing length calculation"); |
---|
684 | } |
---|
685 | return -1; |
---|
686 | |
---|
687 | case TRACE_OPTION_DISCARD_META: |
---|
688 | if (!trace_is_err(libtrace)) { |
---|
689 | trace_set_err(libtrace, TRACE_ERR_OPTION_UNAVAIL, |
---|
690 | "Libtrace does not support meta packets for this format"); |
---|
691 | } |
---|
692 | return -1; |
---|
693 | } |
---|
694 | if (!trace_is_err(libtrace)) { |
---|
695 | trace_set_err(libtrace,TRACE_ERR_UNKNOWN_OPTION, |
---|
696 | "Unknown option %i", option); |
---|
697 | } |
---|
698 | return -1; |
---|
699 | } |
---|
700 | |
---|
701 | DLLEXPORT int trace_set_snaplen(libtrace_t *trace, int snaplen) { |
---|
702 | return trace_config(trace, TRACE_OPTION_SNAPLEN, &snaplen); |
---|
703 | } |
---|
704 | |
---|
705 | DLLEXPORT int trace_set_promisc(libtrace_t *trace, bool promisc) { |
---|
706 | int tmp = promisc; |
---|
707 | return trace_config(trace, TRACE_OPTION_PROMISC, &tmp); |
---|
708 | } |
---|
709 | |
---|
710 | DLLEXPORT int trace_set_filter(libtrace_t *trace, libtrace_filter_t *filter) { |
---|
711 | return trace_config(trace, TRACE_OPTION_FILTER, filter); |
---|
712 | } |
---|
713 | |
---|
714 | DLLEXPORT int trace_set_meta_freq(libtrace_t *trace, int freq) { |
---|
715 | return trace_config(trace, TRACE_OPTION_META_FREQ, &freq); |
---|
716 | } |
---|
717 | |
---|
718 | DLLEXPORT int trace_set_event_realtime(libtrace_t *trace, bool realtime) { |
---|
719 | int tmp = realtime; |
---|
720 | return trace_config(trace, TRACE_OPTION_EVENT_REALTIME, &tmp); |
---|
721 | } |
---|
722 | |
---|
723 | DLLEXPORT int trace_config_output(libtrace_out_t *libtrace, |
---|
724 | trace_option_output_t option, |
---|
725 | void *value) { |
---|
726 | |
---|
727 | /* Unlike the input options, libtrace does not natively support any of |
---|
728 | * the output options - the format module must be able to deal with |
---|
729 | * them. */ |
---|
730 | if (libtrace->format->config_output) { |
---|
731 | return libtrace->format->config_output(libtrace, option, value); |
---|
732 | } |
---|
733 | return -1; |
---|
734 | } |
---|
735 | |
---|
736 | /* Close an input trace file, freeing up any resources it may have been using |
---|
737 | * |
---|
738 | */ |
---|
739 | DLLEXPORT void trace_destroy(libtrace_t *libtrace) { |
---|
740 | int i; |
---|
741 | |
---|
742 | if(!libtrace) { |
---|
743 | fprintf(stderr, "NULL trace passed to trace_destroy()\n"); |
---|
744 | return; |
---|
745 | } |
---|
746 | |
---|
747 | ASSERT_RET(pthread_mutex_destroy(&libtrace->libtrace_lock), == 0); |
---|
748 | ASSERT_RET(pthread_mutex_destroy(&libtrace->read_packet_lock), == 0); |
---|
749 | ASSERT_RET(pthread_cond_destroy(&libtrace->perpkt_cond), == 0); |
---|
750 | |
---|
751 | /* destroy any packets that are still around */ |
---|
752 | if (libtrace->state != STATE_NEW && libtrace->first_packets.packets) { |
---|
753 | for (i = 0; i < libtrace->perpkt_thread_count; ++i) { |
---|
754 | if(libtrace->first_packets.packets[i].packet) { |
---|
755 | trace_destroy_packet(libtrace->first_packets.packets[i].packet); |
---|
756 | } |
---|
757 | } |
---|
758 | free(libtrace->first_packets.packets); |
---|
759 | ASSERT_RET(pthread_spin_destroy(&libtrace->first_packets.lock), == 0); |
---|
760 | } |
---|
761 | |
---|
762 | /* Finish any the last packet we read - for backwards compatibility */ |
---|
763 | if (!libtrace_parallel && libtrace->last_packet) { |
---|
764 | trace_fin_packet(libtrace->last_packet); |
---|
765 | } |
---|
766 | if (libtrace->last_packet != NULL) { |
---|
767 | trace_set_err(libtrace, TRACE_ERR_PAUSE_FIN, "Unable to remove all data stored against trace in trace_destroy()"); |
---|
768 | return; |
---|
769 | } |
---|
770 | |
---|
771 | if (libtrace->format) { |
---|
772 | if (libtrace->started && libtrace->format->pause_input) |
---|
773 | libtrace->format->pause_input(libtrace); |
---|
774 | if (libtrace->format->fin_input) |
---|
775 | libtrace->format->fin_input(libtrace); |
---|
776 | } |
---|
777 | /* Need to free things! */ |
---|
778 | if (libtrace->uridata) |
---|
779 | free(libtrace->uridata); |
---|
780 | |
---|
781 | if (libtrace->stats) |
---|
782 | free(libtrace->stats); |
---|
783 | |
---|
784 | /* Empty any packet memory */ |
---|
785 | if (libtrace->state != STATE_NEW) { |
---|
786 | // This has all of our packets |
---|
787 | libtrace_ocache_destroy(&libtrace->packet_freelist); |
---|
788 | for (i = 0; i < libtrace->perpkt_thread_count; ++i) { |
---|
789 | libtrace_message_queue_destroy(&libtrace->perpkt_threads[i].messages); |
---|
790 | } |
---|
791 | if (libtrace->hasher_thread.type == THREAD_HASHER) |
---|
792 | libtrace_message_queue_destroy(&libtrace->hasher_thread.messages); |
---|
793 | if (libtrace->keepalive_thread.type == THREAD_KEEPALIVE) |
---|
794 | libtrace_message_queue_destroy(&libtrace->keepalive_thread.messages); |
---|
795 | if (libtrace->reporter_thread.type == THREAD_REPORTER) |
---|
796 | libtrace_message_queue_destroy(&libtrace->reporter_thread.messages); |
---|
797 | |
---|
798 | |
---|
799 | if (libtrace->combiner.destroy && libtrace->reporter_cbs) |
---|
800 | libtrace->combiner.destroy(libtrace, &libtrace->combiner); |
---|
801 | free(libtrace->perpkt_threads); |
---|
802 | libtrace->perpkt_threads = NULL; |
---|
803 | libtrace->perpkt_thread_count = 0; |
---|
804 | |
---|
805 | } |
---|
806 | |
---|
807 | if (libtrace->hasher_owner == HASH_OWNED_LIBTRACE) { |
---|
808 | if (libtrace->hasher_data) { |
---|
809 | free(libtrace->hasher_data); |
---|
810 | } |
---|
811 | } |
---|
812 | |
---|
813 | |
---|
814 | if (libtrace->perpkt_cbs) |
---|
815 | trace_destroy_callback_set(libtrace->perpkt_cbs); |
---|
816 | if (libtrace->reporter_cbs) |
---|
817 | trace_destroy_callback_set(libtrace->reporter_cbs); |
---|
818 | |
---|
819 | if (libtrace->event.packet) { |
---|
820 | /* Don't use trace_destroy_packet here - there is almost |
---|
821 | * certainly going to be another libtrace_packet_t that is |
---|
822 | * pointing to the buffer for this packet, so we don't want |
---|
823 | * to free it. Rather, it will get freed when the user calls |
---|
824 | * trace_destroy_packet on the libtrace_packet_t that they |
---|
825 | * own. |
---|
826 | * |
---|
827 | * All we need to do then is free our packet structure itself. |
---|
828 | */ |
---|
829 | free(libtrace->event.packet); |
---|
830 | } |
---|
831 | |
---|
832 | free(libtrace); |
---|
833 | } |
---|
834 | |
---|
835 | |
---|
836 | DLLEXPORT void trace_destroy_dead(libtrace_t *libtrace) { |
---|
837 | if(!libtrace) { |
---|
838 | fprintf(stderr, "NULL trace passed to trace_destroy_dead()\n"); |
---|
839 | return; |
---|
840 | } |
---|
841 | |
---|
842 | ASSERT_RET(pthread_mutex_destroy(&libtrace->libtrace_lock), == 0); |
---|
843 | ASSERT_RET(pthread_mutex_destroy(&libtrace->read_packet_lock), == 0); |
---|
844 | ASSERT_RET(pthread_cond_destroy(&libtrace->perpkt_cond), == 0); |
---|
845 | |
---|
846 | /* Don't call pause_input or fin_input, because we should never have |
---|
847 | * used this trace to do any reading anyway. Do make sure we free |
---|
848 | * any format_data that has been created, though. */ |
---|
849 | if (libtrace->format_data) |
---|
850 | free(libtrace->format_data); |
---|
851 | free(libtrace); |
---|
852 | } |
---|
853 | /* Close an output trace file, freeing up any resources it may have been using |
---|
854 | * |
---|
855 | * @param libtrace the output trace file to be destroyed |
---|
856 | */ |
---|
857 | DLLEXPORT void trace_destroy_output(libtrace_out_t *libtrace) { |
---|
858 | if(!libtrace) { |
---|
859 | fprintf(stderr, "NULL trace passed to trace_destroy_output()\n"); |
---|
860 | return; |
---|
861 | } |
---|
862 | if (libtrace->format && libtrace->format->fin_output) |
---|
863 | libtrace->format->fin_output(libtrace); |
---|
864 | if (libtrace->uridata) |
---|
865 | free(libtrace->uridata); |
---|
866 | free(libtrace); |
---|
867 | } |
---|
868 | |
---|
869 | DLLEXPORT int trace_flush_output(libtrace_out_t *libtrace) { |
---|
870 | if (!libtrace) { |
---|
871 | fprintf(stderr, "NULL trace passed to trace_flush_output()\n"); |
---|
872 | return TRACE_ERR_NULL_TRACE; |
---|
873 | } |
---|
874 | if (libtrace->format && libtrace->format->flush_output) { |
---|
875 | return libtrace->format->flush_output(libtrace); |
---|
876 | } |
---|
877 | |
---|
878 | return 0; |
---|
879 | } |
---|
880 | |
---|
881 | DLLEXPORT libtrace_packet_t *trace_create_packet(void) |
---|
882 | { |
---|
883 | libtrace_packet_t *packet = |
---|
884 | (libtrace_packet_t*)calloc((size_t)1,sizeof(libtrace_packet_t)); |
---|
885 | |
---|
886 | if (packet == NULL) |
---|
887 | return NULL; |
---|
888 | |
---|
889 | packet->buf_control=TRACE_CTRL_PACKET; |
---|
890 | packet->which_trace_start = 0; |
---|
891 | pthread_mutex_init(&(packet->ref_lock), NULL); |
---|
892 | trace_clear_cache(packet); |
---|
893 | return packet; |
---|
894 | } |
---|
895 | |
---|
896 | DLLEXPORT libtrace_packet_t *trace_copy_packet(const libtrace_packet_t *packet) { |
---|
897 | libtrace_packet_t *dest; |
---|
898 | |
---|
899 | if (packet->which_trace_start != packet->trace->startcount) { |
---|
900 | return NULL; |
---|
901 | } |
---|
902 | |
---|
903 | dest = (libtrace_packet_t *)calloc((size_t)1, sizeof(libtrace_packet_t)); |
---|
904 | if (!dest) { |
---|
905 | printf("Out of memory constructing packet\n"); |
---|
906 | abort(); |
---|
907 | } |
---|
908 | dest->trace=packet->trace; |
---|
909 | dest->buffer=malloc(65536); |
---|
910 | if (!dest->buffer) { |
---|
911 | printf("Out of memory allocating buffer memory\n"); |
---|
912 | abort(); |
---|
913 | } |
---|
914 | dest->header=dest->buffer; |
---|
915 | dest->payload=(void*) |
---|
916 | ((char*)dest->buffer+trace_get_framing_length(packet)); |
---|
917 | dest->type=packet->type; |
---|
918 | dest->buf_control=TRACE_CTRL_PACKET; |
---|
919 | dest->order = packet->order; |
---|
920 | dest->hash = packet->hash; |
---|
921 | dest->error = packet->error; |
---|
922 | dest->which_trace_start = packet->which_trace_start; |
---|
923 | /* Reset the cache - better to recalculate than try to convert |
---|
924 | * the values over to the new packet */ |
---|
925 | trace_clear_cache(dest); |
---|
926 | /* Ooooh nasty memcpys! This is why we want to avoid copying packets |
---|
927 | * as much as possible */ |
---|
928 | memcpy(dest->header,packet->header,trace_get_framing_length(packet)); |
---|
929 | memcpy(dest->payload,packet->payload,trace_get_capture_length(packet)); |
---|
930 | |
---|
931 | return dest; |
---|
932 | } |
---|
933 | |
---|
934 | /** Destroy a packet object |
---|
935 | */ |
---|
936 | DLLEXPORT void trace_destroy_packet(libtrace_packet_t *packet) { |
---|
937 | /* Free any resources possibly associated with the packet */ |
---|
938 | if (libtrace_parallel && packet->trace && packet->trace->format->fin_packet) { |
---|
939 | packet->trace->format->fin_packet(packet); |
---|
940 | } |
---|
941 | if (!libtrace_parallel && packet->trace && |
---|
942 | packet->trace->last_packet == packet) { |
---|
943 | packet->trace->last_packet = NULL; |
---|
944 | } |
---|
945 | |
---|
946 | if (packet->buf_control == TRACE_CTRL_PACKET && packet->buffer) { |
---|
947 | free(packet->buffer); |
---|
948 | } |
---|
949 | pthread_mutex_destroy(&(packet->ref_lock)); |
---|
950 | packet->buf_control=(buf_control_t)'\0'; |
---|
951 | /* A "bad" value to force an assert |
---|
952 | * if this packet is ever reused |
---|
953 | */ |
---|
954 | free(packet); |
---|
955 | } |
---|
956 | |
---|
957 | /** |
---|
958 | * Removes any possible data stored againt the trace and releases any data. |
---|
959 | * This will not destroy a reusable good malloc'd buffer (TRACE_CTRL_PACKET) |
---|
960 | * use trace_destroy_packet() for those diabolical purposes. |
---|
961 | */ |
---|
962 | void trace_fin_packet(libtrace_packet_t *packet) { |
---|
963 | if (packet) |
---|
964 | { |
---|
965 | if (packet->trace && packet->trace->format->fin_packet) { |
---|
966 | packet->trace->format->fin_packet(packet); |
---|
967 | } |
---|
968 | |
---|
969 | if (packet->srcbucket && packet->internalid != 0) { |
---|
970 | libtrace_bucket_t *b = (libtrace_bucket_t *)packet->srcbucket; |
---|
971 | libtrace_release_bucket_id(b, packet->internalid); |
---|
972 | } |
---|
973 | |
---|
974 | if (packet->trace) { |
---|
975 | if (!libtrace_parallel && packet->trace->last_packet == packet) |
---|
976 | packet->trace->last_packet = NULL; |
---|
977 | } |
---|
978 | |
---|
979 | // No matter what we remove the header and link pointers |
---|
980 | packet->trace = NULL; |
---|
981 | packet->header = NULL; |
---|
982 | packet->payload = NULL; |
---|
983 | |
---|
984 | if (packet->buf_control != TRACE_CTRL_PACKET) |
---|
985 | { |
---|
986 | packet->buffer = NULL; |
---|
987 | } |
---|
988 | |
---|
989 | trace_clear_cache(packet); |
---|
990 | packet->hash = 0; |
---|
991 | packet->order = 0; |
---|
992 | packet->srcbucket = NULL; |
---|
993 | } |
---|
994 | } |
---|
995 | |
---|
996 | /* Read one packet from the trace into buffer. Note that this function will |
---|
997 | * block until a packet is read (or EOF is reached). |
---|
998 | * |
---|
999 | * @param libtrace the libtrace opaque pointer |
---|
1000 | * @param packet the packet opaque pointer |
---|
1001 | * @returns 0 on EOF, negative value on error |
---|
1002 | * |
---|
1003 | */ |
---|
1004 | DLLEXPORT int trace_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { |
---|
1005 | |
---|
1006 | if (!libtrace) { |
---|
1007 | fprintf(stderr, "NULL trace passed to trace_read_packet()\n"); |
---|
1008 | return TRACE_ERR_NULL_TRACE; |
---|
1009 | } |
---|
1010 | |
---|
1011 | if (trace_is_err(libtrace)) |
---|
1012 | return -1; |
---|
1013 | |
---|
1014 | if (!libtrace->started) { |
---|
1015 | trace_set_err(libtrace,TRACE_ERR_BAD_STATE,"You must call libtrace_start() before trace_read_packet()"); |
---|
1016 | return -1; |
---|
1017 | } |
---|
1018 | |
---|
1019 | if (!packet) { |
---|
1020 | trace_set_err(libtrace, TRACE_ERR_NULL_PACKET, "NULL packet passed into trace_read_packet()"); |
---|
1021 | return -1; |
---|
1022 | } |
---|
1023 | |
---|
1024 | if (!(packet->buf_control==TRACE_CTRL_PACKET |
---|
1025 | || packet->buf_control==TRACE_CTRL_EXTERNAL)) { |
---|
1026 | trace_set_err(libtrace,TRACE_ERR_BAD_STATE,"Packet passed to trace_read_packet() is invalid"); |
---|
1027 | return -1; |
---|
1028 | } |
---|
1029 | |
---|
1030 | if (libtrace->format->read_packet) { |
---|
1031 | /* Finalise the packet, freeing any resources the format module |
---|
1032 | * may have allocated it and zeroing all data associated with it. |
---|
1033 | */ |
---|
1034 | if (packet->trace == libtrace) { |
---|
1035 | trace_fin_packet(packet); |
---|
1036 | } |
---|
1037 | do { |
---|
1038 | size_t ret; |
---|
1039 | int filtret; |
---|
1040 | if ((ret=is_halted(libtrace)) != (size_t)-1) |
---|
1041 | return ret; |
---|
1042 | /* Store the trace we are reading from into the packet opaque |
---|
1043 | * structure */ |
---|
1044 | packet->trace = libtrace; |
---|
1045 | packet->which_trace_start = libtrace->startcount; |
---|
1046 | ret=libtrace->format->read_packet(libtrace,packet); |
---|
1047 | if (ret==(size_t)READ_MESSAGE || |
---|
1048 | ret==(size_t)-1 || ret==0) { |
---|
1049 | packet->trace = NULL; |
---|
1050 | return ret; |
---|
1051 | } |
---|
1052 | if (libtrace->filter) { |
---|
1053 | /* If the filter doesn't match, read another |
---|
1054 | * packet |
---|
1055 | */ |
---|
1056 | filtret = trace_apply_filter(libtrace->filter, packet); |
---|
1057 | if (filtret == -1) { |
---|
1058 | /* Error compiling filter, probably */ |
---|
1059 | return ~0U; |
---|
1060 | } |
---|
1061 | |
---|
1062 | if (filtret == 0) { |
---|
1063 | ++libtrace->filtered_packets; |
---|
1064 | trace_fin_packet(packet); |
---|
1065 | continue; |
---|
1066 | } |
---|
1067 | } |
---|
1068 | if (libtrace->snaplen>0) { |
---|
1069 | /* Snap the packet */ |
---|
1070 | trace_set_capture_length(packet, |
---|
1071 | libtrace->snaplen); |
---|
1072 | } |
---|
1073 | if (!IS_LIBTRACE_META_PACKET(packet)) { |
---|
1074 | ++libtrace->accepted_packets; |
---|
1075 | } |
---|
1076 | if (packet->order == 0) { |
---|
1077 | trace_packet_set_order(packet, libtrace->sequence_number); |
---|
1078 | } |
---|
1079 | ++libtrace->sequence_number; |
---|
1080 | if (!libtrace_parallel && packet->trace == libtrace) |
---|
1081 | libtrace->last_packet = packet; |
---|
1082 | |
---|
1083 | return ret; |
---|
1084 | } while(1); |
---|
1085 | } |
---|
1086 | trace_set_err(libtrace,TRACE_ERR_UNSUPPORTED,"This format does not support reading packets\n"); |
---|
1087 | return ~0U; |
---|
1088 | } |
---|
1089 | |
---|
1090 | /* Converts the provided buffer into a libtrace packet of the given type. |
---|
1091 | * |
---|
1092 | * Unlike trace_construct_packet, the buffer is expected to begin with the |
---|
1093 | * appropriate capture format header for the format type that the packet is |
---|
1094 | * being converted to. This also allows for a packet to be converted into |
---|
1095 | * just about capture format that is supported by libtrace, provided the |
---|
1096 | * format header is present in the buffer. |
---|
1097 | * |
---|
1098 | * This function is primarily used to convert packets received via the RT |
---|
1099 | * protocol back into their original capture format. The RT header encapsulates |
---|
1100 | * the original capture format header, so after removing it the packet must |
---|
1101 | * have it's header and payload pointers updated and the packet format and type |
---|
1102 | * changed, amongst other things. |
---|
1103 | * |
---|
1104 | * Intended only for internal use at this point - this function is not |
---|
1105 | * available through the external libtrace API. |
---|
1106 | */ |
---|
1107 | int trace_prepare_packet(libtrace_t *trace, libtrace_packet_t *packet, |
---|
1108 | void *buffer, libtrace_rt_types_t rt_type, uint32_t flags) { |
---|
1109 | |
---|
1110 | if (!trace) { |
---|
1111 | fprintf(stderr, "NULL trace passed into trace_prepare_packet()\n"); |
---|
1112 | return TRACE_ERR_NULL_TRACE; |
---|
1113 | } |
---|
1114 | |
---|
1115 | if (!packet) { |
---|
1116 | trace_set_err(trace, TRACE_ERR_NULL_TRACE, "NULL packet passed into trace_prepare_packet()"); |
---|
1117 | return -1; |
---|
1118 | } |
---|
1119 | |
---|
1120 | if (!buffer) { |
---|
1121 | trace_set_err(trace, TRACE_ERR_NULL_BUFFER, "NULL buffer passed into trace_prepare_packet()"); |
---|
1122 | return -1; |
---|
1123 | } |
---|
1124 | |
---|
1125 | if (!(packet->buf_control==TRACE_CTRL_PACKET || packet->buf_control==TRACE_CTRL_EXTERNAL)) { |
---|
1126 | trace_set_err(trace,TRACE_ERR_BAD_STATE,"Packet passed to trace_read_packet() is invalid"); |
---|
1127 | return -1; |
---|
1128 | } |
---|
1129 | |
---|
1130 | packet->trace = trace; |
---|
1131 | if (!libtrace_parallel) |
---|
1132 | trace->last_packet = packet; |
---|
1133 | /* Clear packet cache */ |
---|
1134 | trace_clear_cache(packet); |
---|
1135 | |
---|
1136 | if (trace->format->prepare_packet) { |
---|
1137 | return trace->format->prepare_packet(trace, packet, |
---|
1138 | buffer, rt_type, flags); |
---|
1139 | } |
---|
1140 | trace_set_err(trace, TRACE_ERR_UNSUPPORTED, |
---|
1141 | "This format does not support preparing packets"); |
---|
1142 | return -1; |
---|
1143 | |
---|
1144 | } |
---|
1145 | |
---|
1146 | /* Writes a packet to the specified output trace |
---|
1147 | * |
---|
1148 | * @param libtrace describes the output format, destination, etc. |
---|
1149 | * @param packet the packet to be written out |
---|
1150 | * @returns the number of bytes written, -1 if write failed |
---|
1151 | */ |
---|
1152 | DLLEXPORT int trace_write_packet(libtrace_out_t *libtrace, libtrace_packet_t *packet) { |
---|
1153 | |
---|
1154 | if (!libtrace) { |
---|
1155 | fprintf(stderr, "NULL trace passed into trace_write_packet()\n"); |
---|
1156 | return TRACE_ERR_NULL_TRACE; |
---|
1157 | } |
---|
1158 | if (!packet) { |
---|
1159 | trace_set_err_out(libtrace, TRACE_ERR_NULL_PACKET, "NULL trace passed into trace_write_packet()"); |
---|
1160 | return -1; |
---|
1161 | } |
---|
1162 | /* Verify the packet is valid */ |
---|
1163 | if (!libtrace->started) { |
---|
1164 | trace_set_err_out(libtrace,TRACE_ERR_BAD_STATE, |
---|
1165 | "You must call trace_start_output() before calling trace_write_packet()"); |
---|
1166 | return -1; |
---|
1167 | } |
---|
1168 | |
---|
1169 | /* Don't try to convert meta-packets across formats */ |
---|
1170 | if (strcmp(libtrace->format->name, packet->trace->format->name) != 0 && |
---|
1171 | IS_LIBTRACE_META_PACKET(packet)) { |
---|
1172 | return 0; |
---|
1173 | } |
---|
1174 | |
---|
1175 | if (libtrace->format->write_packet) { |
---|
1176 | return libtrace->format->write_packet(libtrace, packet); |
---|
1177 | } |
---|
1178 | trace_set_err_out(libtrace,TRACE_ERR_UNSUPPORTED, |
---|
1179 | "This format does not support writing packets"); |
---|
1180 | return -1; |
---|
1181 | } |
---|
1182 | |
---|
1183 | /* Get a pointer to the first byte of the packet payload */ |
---|
1184 | DLLEXPORT void *trace_get_packet_buffer(const libtrace_packet_t *packet, |
---|
1185 | libtrace_linktype_t *linktype, uint32_t *remaining) { |
---|
1186 | int cap_len; |
---|
1187 | int wire_len; |
---|
1188 | libtrace_linktype_t ltype; |
---|
1189 | |
---|
1190 | if (!packet) { |
---|
1191 | fprintf(stderr, "NULL packet passed into trace_get_packet_buffer()\n"); |
---|
1192 | return NULL; |
---|
1193 | } |
---|
1194 | ltype = trace_get_link_type(packet); |
---|
1195 | |
---|
1196 | if (linktype) { |
---|
1197 | *linktype = ltype; |
---|
1198 | } |
---|
1199 | |
---|
1200 | if (ltype == TRACE_TYPE_CONTENT_INVALID) { |
---|
1201 | if (remaining) { |
---|
1202 | *remaining = 0; |
---|
1203 | } |
---|
1204 | return NULL; |
---|
1205 | } |
---|
1206 | |
---|
1207 | if (remaining) { |
---|
1208 | /* I think we should choose the minimum of the capture and |
---|
1209 | * wire lengths to be the "remaining" value. If the packet has |
---|
1210 | * been padded to increase the capture length, we don't want |
---|
1211 | * to allow subsequent protocol decoders to consider the |
---|
1212 | * padding as part of the packet. |
---|
1213 | * |
---|
1214 | * For example, in Auck 4 there is a trace where the IP header |
---|
1215 | * length is incorrect (24 bytes) followed by a 20 byte TCP |
---|
1216 | * header. Total IP length is 40 bytes. As a result, the |
---|
1217 | * legacyatm padding gets treated as the "missing" bytes of |
---|
1218 | * the TCP header, which isn't the greatest. We're probably |
---|
1219 | * better off returning an incomplete TCP header in that case. |
---|
1220 | */ |
---|
1221 | |
---|
1222 | cap_len = trace_get_capture_length(packet); |
---|
1223 | wire_len = trace_get_wire_length(packet); |
---|
1224 | |
---|
1225 | if (!(cap_len >= 0)) { |
---|
1226 | fprintf(stderr, "Was expecting capture length of atleast 0 in trace_get_packet_buffer()\n"); |
---|
1227 | return NULL; |
---|
1228 | } |
---|
1229 | |
---|
1230 | /* There is the odd corrupt packet, e.g. in IPLS II, that have |
---|
1231 | * massively negative wire lens. We could assert fail here on |
---|
1232 | * them, but we could at least try the capture length instead. |
---|
1233 | * |
---|
1234 | * You may still run into problems if you try to write that |
---|
1235 | * packet, but at least reading should work OK. |
---|
1236 | */ |
---|
1237 | if (wire_len < 0) |
---|
1238 | *remaining = cap_len; |
---|
1239 | else if (wire_len < cap_len) |
---|
1240 | *remaining = wire_len; |
---|
1241 | else |
---|
1242 | *remaining = cap_len; |
---|
1243 | /* *remaining = trace_get_capture_length(packet); */ |
---|
1244 | } |
---|
1245 | return (void *) packet->payload; |
---|
1246 | } |
---|
1247 | |
---|
1248 | |
---|
1249 | /* Get a pointer to the first byte of the packet payload |
---|
1250 | * |
---|
1251 | * DEPRECATED - use trace_get_packet_buffer() instead */ |
---|
1252 | DLLEXPORT void *trace_get_link(const libtrace_packet_t *packet) { |
---|
1253 | return (void *)packet->payload; |
---|
1254 | } |
---|
1255 | |
---|
1256 | /* Get the current time in DAG time format |
---|
1257 | * @param packet a pointer to a libtrace_packet structure |
---|
1258 | * @returns a 64 bit timestamp in DAG ERF format (upper 32 bits are the seconds |
---|
1259 | * past 1970-01-01, the lower 32bits are partial seconds) |
---|
1260 | */ |
---|
1261 | DLLEXPORT uint64_t trace_get_erf_timestamp(const libtrace_packet_t *packet) { |
---|
1262 | if (packet->which_trace_start != packet->trace->startcount) { |
---|
1263 | return (uint64_t)0; |
---|
1264 | } |
---|
1265 | |
---|
1266 | if (packet->trace->format->get_erf_timestamp) { |
---|
1267 | /* timestamp -> timestamp */ |
---|
1268 | return packet->trace->format->get_erf_timestamp(packet); |
---|
1269 | } else if (packet->trace->format->get_timespec) { |
---|
1270 | /* timespec -> timestamp */ |
---|
1271 | struct timespec ts; |
---|
1272 | ts = packet->trace->format->get_timespec(packet); |
---|
1273 | return ((((uint64_t)ts.tv_sec) << 32) + |
---|
1274 | (((uint64_t)ts.tv_nsec << 32)/1000000000)); |
---|
1275 | } else if (packet->trace->format->get_timeval) { |
---|
1276 | /* timeval -> timestamp */ |
---|
1277 | struct timeval tv; |
---|
1278 | tv = packet->trace->format->get_timeval(packet); |
---|
1279 | return ((((uint64_t)tv.tv_sec) << 32) + |
---|
1280 | (((uint64_t)tv.tv_usec << 32)/1000000)); |
---|
1281 | } else if (packet->trace->format->get_seconds) { |
---|
1282 | /* seconds -> timestamp */ |
---|
1283 | double seconds = packet->trace->format->get_seconds(packet); |
---|
1284 | return (((uint64_t)seconds)<<32) |
---|
1285 | + (uint64_t)((seconds-(uint64_t)seconds)*UINT_MAX); |
---|
1286 | } |
---|
1287 | else { |
---|
1288 | return (uint64_t)0; |
---|
1289 | } |
---|
1290 | } |
---|
1291 | |
---|
1292 | /* Get the current time in struct timeval |
---|
1293 | * @param packet a pointer to a libtrace_packet structure |
---|
1294 | * |
---|
1295 | * @returns time that this packet was seen in a struct timeval |
---|
1296 | * @author Daniel Lawson |
---|
1297 | * @author Perry Lorier |
---|
1298 | */ |
---|
1299 | DLLEXPORT struct timeval trace_get_timeval(const libtrace_packet_t *packet) { |
---|
1300 | struct timeval tv; |
---|
1301 | uint64_t ts = 0; |
---|
1302 | |
---|
1303 | if (packet->which_trace_start != packet->trace->startcount) { |
---|
1304 | tv.tv_sec=-1; |
---|
1305 | tv.tv_usec=-1; |
---|
1306 | } else if (packet->trace->format->get_timeval) { |
---|
1307 | /* timeval -> timeval */ |
---|
1308 | tv = packet->trace->format->get_timeval(packet); |
---|
1309 | } else if (packet->trace->format->get_erf_timestamp) { |
---|
1310 | /* timestamp -> timeval */ |
---|
1311 | ts = packet->trace->format->get_erf_timestamp(packet); |
---|
1312 | tv.tv_sec = ts >> 32; |
---|
1313 | tv.tv_usec = ((ts&0xFFFFFFFF)*1000000)>>32; |
---|
1314 | if (tv.tv_usec >= 1000000) { |
---|
1315 | tv.tv_usec -= 1000000; |
---|
1316 | tv.tv_sec += 1; |
---|
1317 | } |
---|
1318 | } else if (packet->trace->format->get_timespec) { |
---|
1319 | struct timespec ts = packet->trace->format->get_timespec(packet); |
---|
1320 | tv.tv_sec = ts.tv_sec; |
---|
1321 | tv.tv_usec = ts.tv_nsec/1000; |
---|
1322 | } else if (packet->trace->format->get_seconds) { |
---|
1323 | /* seconds -> timeval */ |
---|
1324 | double seconds = packet->trace->format->get_seconds(packet); |
---|
1325 | tv.tv_sec = (uint32_t)seconds; |
---|
1326 | tv.tv_usec = (uint32_t)(((seconds - tv.tv_sec) * 1000000)/UINT_MAX); |
---|
1327 | } |
---|
1328 | else { |
---|
1329 | tv.tv_sec=-1; |
---|
1330 | tv.tv_usec=-1; |
---|
1331 | } |
---|
1332 | |
---|
1333 | return tv; |
---|
1334 | } |
---|
1335 | |
---|
1336 | DLLEXPORT struct timespec trace_get_timespec(const libtrace_packet_t *packet) { |
---|
1337 | struct timespec ts; |
---|
1338 | |
---|
1339 | if (packet->which_trace_start != packet->trace->startcount) { |
---|
1340 | ts.tv_sec=-1; |
---|
1341 | ts.tv_nsec=-1; |
---|
1342 | } else if (packet->trace->format->get_timespec) { |
---|
1343 | return packet->trace->format->get_timespec(packet); |
---|
1344 | } else if (packet->trace->format->get_erf_timestamp) { |
---|
1345 | /* timestamp -> timeval */ |
---|
1346 | uint64_t erfts = packet->trace->format->get_erf_timestamp(packet); |
---|
1347 | ts.tv_sec = erfts >> 32; |
---|
1348 | ts.tv_nsec = ((erfts&0xFFFFFFFF)*1000000000)>>32; |
---|
1349 | if (ts.tv_nsec >= 1000000000) { |
---|
1350 | ts.tv_nsec -= 1000000000; |
---|
1351 | ts.tv_sec += 1; |
---|
1352 | } |
---|
1353 | } else if (packet->trace->format->get_timeval) { |
---|
1354 | /* timeval -> timespec */ |
---|
1355 | struct timeval tv = packet->trace->format->get_timeval(packet); |
---|
1356 | ts.tv_sec = tv.tv_sec; |
---|
1357 | ts.tv_nsec = tv.tv_usec*1000; |
---|
1358 | } else if (packet->trace->format->get_seconds) { |
---|
1359 | /* seconds -> timespec */ |
---|
1360 | double seconds = packet->trace->format->get_seconds(packet); |
---|
1361 | ts.tv_sec = (uint32_t)seconds; |
---|
1362 | ts.tv_nsec = (long)(((seconds - ts.tv_sec) * 1000000000)/UINT_MAX); |
---|
1363 | } |
---|
1364 | else { |
---|
1365 | ts.tv_sec=-1; |
---|
1366 | ts.tv_nsec=-1; |
---|
1367 | } |
---|
1368 | return ts; |
---|
1369 | } |
---|
1370 | |
---|
1371 | |
---|
1372 | /* Get the current time in floating point seconds |
---|
1373 | * @param packet a pointer to a libtrace_packet structure |
---|
1374 | * @returns time that this packet was seen in 64bit floating point seconds |
---|
1375 | */ |
---|
1376 | DLLEXPORT double trace_get_seconds(const libtrace_packet_t *packet) { |
---|
1377 | double seconds = 0.0; |
---|
1378 | |
---|
1379 | if (packet->which_trace_start != packet->trace->startcount) { |
---|
1380 | return 0.0; |
---|
1381 | } |
---|
1382 | |
---|
1383 | if (packet->trace->format->get_seconds) { |
---|
1384 | /* seconds->seconds */ |
---|
1385 | seconds = packet->trace->format->get_seconds(packet); |
---|
1386 | } else if (packet->trace->format->get_erf_timestamp) { |
---|
1387 | /* timestamp -> seconds */ |
---|
1388 | uint64_t ts = 0; |
---|
1389 | ts = packet->trace->format->get_erf_timestamp(packet); |
---|
1390 | seconds = (ts>>32) + ((ts & UINT_MAX)*1.0 / UINT_MAX); |
---|
1391 | } else if (packet->trace->format->get_timespec) { |
---|
1392 | /* timespec -> seconds */ |
---|
1393 | struct timespec ts; |
---|
1394 | ts = packet->trace->format->get_timespec(packet); |
---|
1395 | seconds = ts.tv_sec + ((ts.tv_nsec * 1.0) / 1000000000); |
---|
1396 | } else if (packet->trace->format->get_timeval) { |
---|
1397 | /* timeval -> seconds */ |
---|
1398 | struct timeval tv; |
---|
1399 | tv = packet->trace->format->get_timeval(packet); |
---|
1400 | seconds = tv.tv_sec + ((tv.tv_usec * 1.0) / 1000000); |
---|
1401 | } |
---|
1402 | |
---|
1403 | return seconds; |
---|
1404 | } |
---|
1405 | |
---|
1406 | DLLEXPORT size_t trace_get_capture_length(const libtrace_packet_t *packet) |
---|
1407 | { |
---|
1408 | /* Cache the capture length */ |
---|
1409 | if (packet->which_trace_start != packet->trace->startcount) { |
---|
1410 | return ~0U; |
---|
1411 | } |
---|
1412 | if (packet->cached.capture_length == -1) { |
---|
1413 | if (!packet->trace->format->get_capture_length) |
---|
1414 | return ~0U; |
---|
1415 | /* Cast away constness because this is "just" a cache */ |
---|
1416 | ((libtrace_packet_t*)packet)->cached.capture_length = |
---|
1417 | packet->trace->format->get_capture_length(packet); |
---|
1418 | } |
---|
1419 | |
---|
1420 | if (!(packet->cached.capture_length < LIBTRACE_PACKET_BUFSIZE)) { |
---|
1421 | fprintf(stderr, "Capture length is greater than the buffer size in trace_get_capture_length()\n"); |
---|
1422 | return 0; |
---|
1423 | /* should we be returning ~OU here? */ |
---|
1424 | } |
---|
1425 | |
---|
1426 | return packet->cached.capture_length; |
---|
1427 | } |
---|
1428 | |
---|
1429 | /* Get the size of the packet as it was seen on the wire. |
---|
1430 | * @param packet a pointer to a libtrace_packet structure |
---|
1431 | * |
---|
1432 | * @returns the size of the packet as it was on the wire. |
---|
1433 | * @note Due to the trace being a header capture, or anonymisation this may |
---|
1434 | * not be the same as the Capture Len. |
---|
1435 | */ |
---|
1436 | DLLEXPORT size_t trace_get_wire_length(const libtrace_packet_t *packet){ |
---|
1437 | |
---|
1438 | size_t wiresub = 0; |
---|
1439 | |
---|
1440 | if (packet->which_trace_start != packet->trace->startcount) { |
---|
1441 | return ~0U; |
---|
1442 | } |
---|
1443 | |
---|
1444 | if (packet->cached.wire_length == -1) { |
---|
1445 | if (!packet->trace->format->get_wire_length) |
---|
1446 | return ~0U; |
---|
1447 | ((libtrace_packet_t *)packet)->cached.wire_length = |
---|
1448 | packet->trace->format->get_wire_length(packet); |
---|
1449 | } |
---|
1450 | |
---|
1451 | if (packet->type >= TRACE_RT_DATA_DLT && packet->type <= |
---|
1452 | TRACE_RT_DATA_DLT_END) { |
---|
1453 | |
---|
1454 | /* pcap wire lengths in libtrace include an extra four bytes |
---|
1455 | * for the FCS (to be consistent with other formats that do |
---|
1456 | * capture the FCS), but these bytes don't actually exist on |
---|
1457 | * the wire. Therefore, we shouldn't get upset if our "wire" |
---|
1458 | * length exceeds the max buffer size by four bytes or less. |
---|
1459 | */ |
---|
1460 | if (packet->cached.wire_length >= 4) { |
---|
1461 | wiresub = 4; |
---|
1462 | } else { |
---|
1463 | wiresub = packet->cached.wire_length; |
---|
1464 | } |
---|
1465 | } else { |
---|
1466 | wiresub = 0; |
---|
1467 | } |
---|
1468 | |
---|
1469 | if (!(packet->cached.wire_length - wiresub < LIBTRACE_PACKET_BUFSIZE)) { |
---|
1470 | fprintf(stderr, "Wire length %zu exceeds expected maximum packet size of %d -- packet is likely corrupt.\n", |
---|
1471 | packet->cached.wire_length - wiresub, |
---|
1472 | LIBTRACE_PACKET_BUFSIZE); |
---|
1473 | |
---|
1474 | /* should we be returning ~OU here? */ |
---|
1475 | } |
---|
1476 | return packet->cached.wire_length; |
---|
1477 | |
---|
1478 | } |
---|
1479 | |
---|
1480 | /* Get the length of the capture framing headers. |
---|
1481 | * @param packet the packet opaque pointer |
---|
1482 | * @returns the size of the packet as it was on the wire. |
---|
1483 | * @note this length corresponds to the difference between the size of a |
---|
1484 | * captured packet in memory, and the captured length of the packet |
---|
1485 | */ |
---|
1486 | DLLEXPORT SIMPLE_FUNCTION |
---|
1487 | size_t trace_get_framing_length(const libtrace_packet_t *packet) { |
---|
1488 | if (packet->which_trace_start != packet->trace->startcount) { |
---|
1489 | return ~0U; |
---|
1490 | } |
---|
1491 | |
---|
1492 | if (packet->cached.framing_length >= 0) { |
---|
1493 | return packet->cached.framing_length; |
---|
1494 | } |
---|
1495 | |
---|
1496 | if (packet->trace->format->get_framing_length) { |
---|
1497 | ((libtrace_packet_t *)packet)->cached.framing_length = |
---|
1498 | packet->trace->format->get_framing_length(packet); |
---|
1499 | return packet->cached.framing_length; |
---|
1500 | } |
---|
1501 | return ~0U; |
---|
1502 | } |
---|
1503 | |
---|
1504 | |
---|
1505 | /* Get the type of the link layer |
---|
1506 | * @param packet a pointer to a libtrace_packet structure |
---|
1507 | * @returns libtrace_linktype_t |
---|
1508 | */ |
---|
1509 | DLLEXPORT libtrace_linktype_t trace_get_link_type(const libtrace_packet_t *packet ) { |
---|
1510 | |
---|
1511 | if (packet->which_trace_start != packet->trace->startcount) { |
---|
1512 | return TRACE_TYPE_CONTENT_INVALID; |
---|
1513 | } |
---|
1514 | |
---|
1515 | if (packet->cached.link_type == 0) { |
---|
1516 | if (!packet->trace->format->get_link_type) |
---|
1517 | return TRACE_TYPE_UNKNOWN; |
---|
1518 | ((libtrace_packet_t *)packet)->cached.link_type = |
---|
1519 | packet->trace->format->get_link_type(packet); |
---|
1520 | } |
---|
1521 | |
---|
1522 | return packet->cached.link_type; |
---|
1523 | } |
---|
1524 | |
---|
1525 | /* process a libtrace event |
---|
1526 | * @param trace the libtrace opaque pointer |
---|
1527 | * @param packet the libtrace_packet opaque pointer |
---|
1528 | * @returns |
---|
1529 | * TRACE_EVENT_IOWAIT Waiting on I/O on fd |
---|
1530 | * TRACE_EVENT_SLEEP Next event in seconds |
---|
1531 | * TRACE_EVENT_PACKET Packet arrived in buffer with size size |
---|
1532 | * TRACE_EVENT_TERMINATE Trace terminated (perhaps with an error condition) |
---|
1533 | * FIXME currently keeps a copy of the packet inside the trace pointer, |
---|
1534 | * which in turn is stored inside the new packet object... |
---|
1535 | */ |
---|
1536 | DLLEXPORT libtrace_eventobj_t trace_event(libtrace_t *trace, |
---|
1537 | libtrace_packet_t *packet) { |
---|
1538 | libtrace_eventobj_t event = {TRACE_EVENT_IOWAIT,0,0.0,0}; |
---|
1539 | |
---|
1540 | if (!trace) { |
---|
1541 | fprintf(stderr, "NULL trace passed into trace_event()"); |
---|
1542 | /* Return default event on error? */ |
---|
1543 | return event; |
---|
1544 | } |
---|
1545 | if (!packet) { |
---|
1546 | trace_set_err(trace, TRACE_ERR_NULL_PACKET, "NULL packet passed into trace_event()"); |
---|
1547 | /* Return default event on error? */ |
---|
1548 | return event; |
---|
1549 | } |
---|
1550 | |
---|
1551 | /* Free the last packet */ |
---|
1552 | trace_fin_packet(packet); |
---|
1553 | /* Store the trace we are reading from into the packet opaque |
---|
1554 | * structure */ |
---|
1555 | packet->trace = trace; |
---|
1556 | |
---|
1557 | if (packet->trace->format->trace_event) { |
---|
1558 | /* Note: incrementing accepted, filtered etc. packet |
---|
1559 | * counters is handled by the format-specific |
---|
1560 | * function so don't increment them here. |
---|
1561 | */ |
---|
1562 | event=packet->trace->format->trace_event(trace,packet); |
---|
1563 | } |
---|
1564 | return event; |
---|
1565 | |
---|
1566 | } |
---|
1567 | |
---|
1568 | /** Setup a BPF filter based on pre-compiled byte-code. |
---|
1569 | * @param bf_insns A pointer to the start of the byte-code |
---|
1570 | * @param bf_len The number of BPF instructions |
---|
1571 | * @returns an opaque pointer to a libtrace_filter_t object |
---|
1572 | * @note The supplied byte-code is not checked for correctness. |
---|
1573 | * @author Scott Raynel |
---|
1574 | */ |
---|
1575 | DLLEXPORT libtrace_filter_t * |
---|
1576 | trace_create_filter_from_bytecode(void *bf_insns, unsigned int bf_len) |
---|
1577 | { |
---|
1578 | #ifndef HAVE_BPF |
---|
1579 | fprintf(stderr, "This version of libtrace does not have BPF support\n"); |
---|
1580 | return NULL; |
---|
1581 | #else |
---|
1582 | struct libtrace_filter_t *filter = (struct libtrace_filter_t *) |
---|
1583 | calloc(1, sizeof(struct libtrace_filter_t)); |
---|
1584 | filter->filter.bf_insns = (struct bpf_insn *) |
---|
1585 | malloc(sizeof(struct bpf_insn) * bf_len); |
---|
1586 | |
---|
1587 | memcpy(filter->filter.bf_insns, bf_insns, |
---|
1588 | bf_len * sizeof(struct bpf_insn)); |
---|
1589 | |
---|
1590 | filter->filter.bf_len = bf_len; |
---|
1591 | filter->filterstring = NULL; |
---|
1592 | filter->jitfilter = NULL; |
---|
1593 | /* "flag" indicates that the filter member is valid */ |
---|
1594 | filter->flag = 1; |
---|
1595 | |
---|
1596 | return filter; |
---|
1597 | #endif |
---|
1598 | } |
---|
1599 | |
---|
1600 | /* Create a BPF filter |
---|
1601 | * @param filterstring a char * containing the bpf filter string |
---|
1602 | * @returns opaque pointer pointer to a libtrace_filter_t object |
---|
1603 | */ |
---|
1604 | DLLEXPORT libtrace_filter_t *trace_create_filter(const char *filterstring) { |
---|
1605 | #ifdef HAVE_BPF |
---|
1606 | libtrace_filter_t *filter = (libtrace_filter_t*) |
---|
1607 | calloc(1, sizeof(libtrace_filter_t)); |
---|
1608 | filter->filterstring = strdup(filterstring); |
---|
1609 | filter->jitfilter = NULL; |
---|
1610 | filter->flag = 0; |
---|
1611 | return filter; |
---|
1612 | #else |
---|
1613 | fprintf(stderr,"This version of libtrace does not have bpf filter support\n"); |
---|
1614 | return NULL; |
---|
1615 | #endif |
---|
1616 | } |
---|
1617 | |
---|
1618 | DLLEXPORT void trace_destroy_filter(libtrace_filter_t *filter) |
---|
1619 | { |
---|
1620 | #ifdef HAVE_BPF |
---|
1621 | free(filter->filterstring); |
---|
1622 | if (filter->flag) |
---|
1623 | pcap_freecode(&filter->filter); |
---|
1624 | #ifdef HAVE_LLVM |
---|
1625 | if (filter->jitfilter) |
---|
1626 | destroy_program(filter->jitfilter); |
---|
1627 | #endif |
---|
1628 | free(filter); |
---|
1629 | #else |
---|
1630 | |
---|
1631 | #endif |
---|
1632 | } |
---|
1633 | |
---|
1634 | /* Compile a bpf filter, now we know the link type for the trace that we're |
---|
1635 | * applying it to. |
---|
1636 | * |
---|
1637 | * @internal |
---|
1638 | * |
---|
1639 | * @returns -1 on error, 0 on success |
---|
1640 | */ |
---|
1641 | static int trace_bpf_compile(libtrace_filter_t *filter, |
---|
1642 | const libtrace_packet_t *packet, |
---|
1643 | void *linkptr, |
---|
1644 | libtrace_linktype_t linktype ) { |
---|
1645 | #ifdef HAVE_BPF |
---|
1646 | /* It just so happens that the underlying libs used by pthread arn't |
---|
1647 | * thread safe, namely lex/flex thingys, so single threaded compile |
---|
1648 | * multi threaded running should be safe. |
---|
1649 | */ |
---|
1650 | static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
---|
1651 | |
---|
1652 | if (!packet) { |
---|
1653 | fprintf(stderr, "NULL packet passed into trace_bpf_compile()"); |
---|
1654 | return TRACE_ERR_NULL_PACKET; |
---|
1655 | } |
---|
1656 | |
---|
1657 | if (!filter) { |
---|
1658 | trace_set_err(packet->trace, |
---|
1659 | TRACE_ERR_NULL_FILTER, "Filter is NULL trace_bpf_compile()"); |
---|
1660 | return -1; |
---|
1661 | } |
---|
1662 | |
---|
1663 | /* If this isn't a real packet, then fail */ |
---|
1664 | if (!linkptr) { |
---|
1665 | trace_set_err(packet->trace, |
---|
1666 | TRACE_ERR_BAD_FILTER,"Packet has no payload"); |
---|
1667 | return -1; |
---|
1668 | } |
---|
1669 | |
---|
1670 | if (filter->filterstring && ! filter->flag) { |
---|
1671 | pcap_t *pcap = NULL; |
---|
1672 | if (linktype==(libtrace_linktype_t)-1) { |
---|
1673 | trace_set_err(packet->trace, |
---|
1674 | TRACE_ERR_BAD_FILTER, |
---|
1675 | "Packet has an unknown linktype"); |
---|
1676 | return -1; |
---|
1677 | } |
---|
1678 | if (libtrace_to_pcap_dlt(linktype) == TRACE_DLT_ERROR) { |
---|
1679 | trace_set_err(packet->trace,TRACE_ERR_BAD_FILTER, |
---|
1680 | "Unknown pcap equivalent linktype"); |
---|
1681 | return -1; |
---|
1682 | } |
---|
1683 | pthread_mutex_lock(&mutex); |
---|
1684 | /* Make sure not one bet us to this */ |
---|
1685 | if (filter->flag) { |
---|
1686 | pthread_mutex_unlock(&mutex); |
---|
1687 | return -1; |
---|
1688 | } |
---|
1689 | pcap=(pcap_t *)pcap_open_dead( |
---|
1690 | (int)libtrace_to_pcap_dlt(linktype), |
---|
1691 | 1500U); |
---|
1692 | /* build filter */ |
---|
1693 | if (!pcap) { |
---|
1694 | trace_set_err(packet->trace, TRACE_ERR_BAD_FILTER, |
---|
1695 | "Unable to open pcap_t for compiling filters trace_bpf_compile()"); |
---|
1696 | return -1; |
---|
1697 | } |
---|
1698 | if (pcap_compile( pcap, &filter->filter, filter->filterstring, |
---|
1699 | 1, 0)) { |
---|
1700 | trace_set_err(packet->trace,TRACE_ERR_BAD_FILTER, |
---|
1701 | "Unable to compile the filter \"%s\": %s", |
---|
1702 | filter->filterstring, |
---|
1703 | pcap_geterr(pcap)); |
---|
1704 | pcap_close(pcap); |
---|
1705 | pthread_mutex_unlock(&mutex); |
---|
1706 | return -1; |
---|
1707 | } |
---|
1708 | pcap_close(pcap); |
---|
1709 | filter->flag=1; |
---|
1710 | pthread_mutex_unlock(&mutex); |
---|
1711 | } |
---|
1712 | return 0; |
---|
1713 | #else |
---|
1714 | trace_set_err(packet->trace,TRACE_ERR_OPTION_UNAVAIL, |
---|
1715 | "Feature unavailable"); |
---|
1716 | return -1; |
---|
1717 | #endif |
---|
1718 | } |
---|
1719 | |
---|
1720 | DLLEXPORT int trace_apply_filter(libtrace_filter_t *filter, |
---|
1721 | const libtrace_packet_t *packet) { |
---|
1722 | #ifdef HAVE_BPF |
---|
1723 | void *linkptr = 0; |
---|
1724 | uint32_t clen = 0; |
---|
1725 | bool free_packet_needed = false; |
---|
1726 | int ret; |
---|
1727 | libtrace_linktype_t linktype; |
---|
1728 | libtrace_packet_t *packet_copy = (libtrace_packet_t*)packet; |
---|
1729 | #ifdef HAVE_LLVM |
---|
1730 | static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
---|
1731 | #endif |
---|
1732 | |
---|
1733 | if (!packet) { |
---|
1734 | fprintf(stderr, "NULL packet passed into trace_apply_filter()\n"); |
---|
1735 | return TRACE_ERR_NULL_PACKET; |
---|
1736 | } |
---|
1737 | if (!filter) { |
---|
1738 | trace_set_err(packet->trace, TRACE_ERR_NULL_FILTER, |
---|
1739 | "NULL filter passed into trace_apply_filter()"); |
---|
1740 | return -1; |
---|
1741 | } |
---|
1742 | |
---|
1743 | /* Match all non-data packets as we probably want them to pass |
---|
1744 | * through to the caller */ |
---|
1745 | linktype = trace_get_link_type(packet); |
---|
1746 | |
---|
1747 | if (linktype == TRACE_TYPE_NONDATA || linktype == TRACE_TYPE_ERF_META |
---|
1748 | || linktype == TRACE_TYPE_PCAPNG_META) |
---|
1749 | return 1; |
---|
1750 | |
---|
1751 | if (libtrace_to_pcap_dlt(linktype)==TRACE_DLT_ERROR) { |
---|
1752 | |
---|
1753 | /* If we cannot get a suitable DLT for the packet, it may |
---|
1754 | * be because the packet is encapsulated in a link type that |
---|
1755 | * does not correspond to a DLT. Therefore, we should try |
---|
1756 | * popping off headers until we either can find a suitable |
---|
1757 | * link type or we can't do any more sensible decapsulation. */ |
---|
1758 | |
---|
1759 | /* Copy the packet, as we don't want to trash the one we |
---|
1760 | * were passed in */ |
---|
1761 | packet_copy=trace_copy_packet(packet); |
---|
1762 | free_packet_needed=true; |
---|
1763 | |
---|
1764 | while (libtrace_to_pcap_dlt(linktype) == TRACE_DLT_ERROR) { |
---|
1765 | if (!demote_packet(packet_copy)) { |
---|
1766 | trace_set_err(packet->trace, |
---|
1767 | TRACE_ERR_NO_CONVERSION, |
---|
1768 | "pcap does not support this linktype so cannot apply BPF filters"); |
---|
1769 | if (free_packet_needed) { |
---|
1770 | trace_destroy_packet(packet_copy); |
---|
1771 | } |
---|
1772 | return -1; |
---|
1773 | } |
---|
1774 | linktype = trace_get_link_type(packet_copy); |
---|
1775 | } |
---|
1776 | |
---|
1777 | } |
---|
1778 | |
---|
1779 | linkptr = trace_get_packet_buffer(packet_copy,NULL,&clen); |
---|
1780 | if (!linkptr) { |
---|
1781 | if (free_packet_needed) { |
---|
1782 | trace_destroy_packet(packet_copy); |
---|
1783 | } |
---|
1784 | return 0; |
---|
1785 | } |
---|
1786 | |
---|
1787 | /* We need to compile the filter now, because before we didn't know |
---|
1788 | * what the link type was |
---|
1789 | */ |
---|
1790 | // Note internal mutex locking used here |
---|
1791 | if (trace_bpf_compile(filter,packet_copy,linkptr,linktype)==-1) { |
---|
1792 | if (free_packet_needed) { |
---|
1793 | trace_destroy_packet(packet_copy); |
---|
1794 | } |
---|
1795 | return -1; |
---|
1796 | } |
---|
1797 | |
---|
1798 | /* If we're jitting, we may need to JIT the BPF code now too */ |
---|
1799 | #if HAVE_LLVM |
---|
1800 | if (!filter->jitfilter) { |
---|
1801 | ASSERT_RET(pthread_mutex_lock(&mutex), == 0); |
---|
1802 | /* Again double check here like the bpf filter */ |
---|
1803 | if(!filter->jitfilter) |
---|
1804 | /* Looking at compile_program source this appears to be thread safe |
---|
1805 | * however if this gets called twice we will leak this memory :( |
---|
1806 | * as such lock here anyways */ |
---|
1807 | filter->jitfilter = compile_program(filter->filter.bf_insns, filter->filter.bf_len); |
---|
1808 | ASSERT_RET(pthread_mutex_unlock(&mutex), == 0); |
---|
1809 | } |
---|
1810 | #endif |
---|
1811 | |
---|
1812 | if (!filter->flag) { |
---|
1813 | trace_set_err(packet->trace, TRACE_ERR_BAD_FILTER, |
---|
1814 | "Bad filter passed into trace_apply_filter()"); |
---|
1815 | return -1; |
---|
1816 | } |
---|
1817 | /* Now execute the filter */ |
---|
1818 | #if HAVE_LLVM |
---|
1819 | ret=filter->jitfilter->bpf_run((unsigned char *)linkptr, clen); |
---|
1820 | #else |
---|
1821 | ret=bpf_filter(filter->filter.bf_insns,(u_char*)linkptr,(unsigned int)clen,(unsigned int)clen); |
---|
1822 | #endif |
---|
1823 | |
---|
1824 | /* If we copied the packet earlier, make sure that we free it */ |
---|
1825 | if (free_packet_needed) { |
---|
1826 | trace_destroy_packet(packet_copy); |
---|
1827 | } |
---|
1828 | return ret; |
---|
1829 | #else |
---|
1830 | fprintf(stderr,"This version of libtrace does not have bpf filter support\n"); |
---|
1831 | return 0; |
---|
1832 | #endif |
---|
1833 | } |
---|
1834 | |
---|
1835 | /* Set the direction flag, if it has one |
---|
1836 | * @param packet the packet opaque pointer |
---|
1837 | * @param direction the new direction (0,1,2,3) |
---|
1838 | * @returns a signed value containing the direction flag, or -1 if this is not supported |
---|
1839 | */ |
---|
1840 | DLLEXPORT libtrace_direction_t trace_set_direction(libtrace_packet_t *packet, |
---|
1841 | libtrace_direction_t direction) |
---|
1842 | { |
---|
1843 | if (!packet) { |
---|
1844 | fprintf(stderr, "NULL packet passed into trace_set_direction()\n"); |
---|
1845 | return (libtrace_direction_t)~0U; |
---|
1846 | } |
---|
1847 | if (packet->trace->format->set_direction) { |
---|
1848 | return packet->trace->format->set_direction(packet,direction); |
---|
1849 | } |
---|
1850 | return (libtrace_direction_t)~0U; |
---|
1851 | } |
---|
1852 | |
---|
1853 | /* Get the direction flag, if it has one |
---|
1854 | * @param packet a pointer to a libtrace_packet structure |
---|
1855 | * @returns a signed value containing the direction flag, or -1 if this is not supported |
---|
1856 | * The direction is defined as 0 for packets originating locally (ie, outbound) |
---|
1857 | * and 1 for packets originating remotely (ie, inbound). |
---|
1858 | * Other values are possible, which might be overloaded to mean special things |
---|
1859 | * for a special trace. |
---|
1860 | */ |
---|
1861 | DLLEXPORT libtrace_direction_t trace_get_direction(const libtrace_packet_t *packet) |
---|
1862 | { |
---|
1863 | if (!packet) { |
---|
1864 | fprintf(stderr, "NULL packet passed into trace_set_direction()\n"); |
---|
1865 | return (libtrace_direction_t)~0U; |
---|
1866 | } |
---|
1867 | if (packet->which_trace_start != packet->trace->startcount) { |
---|
1868 | return (libtrace_direction_t)~0U; |
---|
1869 | } |
---|
1870 | if (packet->trace->format->get_direction) { |
---|
1871 | return packet->trace->format->get_direction(packet); |
---|
1872 | } |
---|
1873 | return (libtrace_direction_t)~0U; |
---|
1874 | } |
---|
1875 | |
---|
1876 | #define ROOT_SERVER(x) ((x) < 512) |
---|
1877 | #define ROOT_CLIENT(x) ((512 <= (x)) && ((x) < 1024)) |
---|
1878 | #define NONROOT_SERVER(x) ((x) >= 5000) |
---|
1879 | #define NONROOT_CLIENT(x) ((1024 <= (x)) && ((x) < 5000)) |
---|
1880 | #define DYNAMIC(x) ((49152 < (x)) && ((x) < 65535)) |
---|
1881 | #define SERVER(x) ROOT_SERVER(x) || NONROOT_SERVER(x) |
---|
1882 | #define CLIENT(x) ROOT_CLIENT(x) || NONROOT_CLIENT(x) |
---|
1883 | |
---|
1884 | /* Attempt to deduce the 'server' port |
---|
1885 | * @param protocol the IP protocol (eg, 6 or 17 for TCP or UDP) |
---|
1886 | * @param source the TCP or UDP source port |
---|
1887 | * @param dest the TCP or UDP destination port |
---|
1888 | * @returns a hint as to which port is the server port |
---|
1889 | */ |
---|
1890 | DLLEXPORT int8_t trace_get_server_port(UNUSED uint8_t protocol, |
---|
1891 | uint16_t source, uint16_t dest) |
---|
1892 | { |
---|
1893 | /* |
---|
1894 | * * If the ports are equal, return DEST |
---|
1895 | * * Check for well-known ports in the given protocol |
---|
1896 | * * Root server ports: 0 - 511 |
---|
1897 | * * Root client ports: 512 - 1023 |
---|
1898 | * * non-root client ports: 1024 - 4999 |
---|
1899 | * * non-root server ports: 5000+ |
---|
1900 | * * Check for static ranges: 1024 - 49151 |
---|
1901 | * * Check for dynamic ranges: 49152 - 65535 |
---|
1902 | * * flip a coin. |
---|
1903 | */ |
---|
1904 | |
---|
1905 | /* equal */ |
---|
1906 | if (source == dest) |
---|
1907 | return USE_DEST; |
---|
1908 | |
---|
1909 | /* root server port, 0 - 511 */ |
---|
1910 | if (ROOT_SERVER(source) && ROOT_SERVER(dest)) { |
---|
1911 | if (source < dest) |
---|
1912 | return USE_SOURCE; |
---|
1913 | return USE_DEST; |
---|
1914 | } |
---|
1915 | |
---|
1916 | if (ROOT_SERVER(source) && !ROOT_SERVER(dest)) |
---|
1917 | return USE_SOURCE; |
---|
1918 | if (!ROOT_SERVER(source) && ROOT_SERVER(dest)) |
---|
1919 | return USE_DEST; |
---|
1920 | |
---|
1921 | /* non-root server */ |
---|
1922 | if (NONROOT_SERVER(source) && NONROOT_SERVER(dest)) { |
---|
1923 | if (source < dest) |
---|
1924 | return USE_SOURCE; |
---|
1925 | return USE_DEST; |
---|
1926 | } |
---|
1927 | if (NONROOT_SERVER(source) && !NONROOT_SERVER(dest)) |
---|
1928 | return USE_SOURCE; |
---|
1929 | if (!NONROOT_SERVER(source) && NONROOT_SERVER(dest)) |
---|
1930 | return USE_DEST; |
---|
1931 | |
---|
1932 | /* root client */ |
---|
1933 | if (ROOT_CLIENT(source) && ROOT_CLIENT(dest)) { |
---|
1934 | if (source < dest) |
---|
1935 | return USE_SOURCE; |
---|
1936 | return USE_DEST; |
---|
1937 | } |
---|
1938 | if (ROOT_CLIENT(source) && !ROOT_CLIENT(dest)) { |
---|
1939 | /* prefer root-client over nonroot-client */ |
---|
1940 | if (NONROOT_CLIENT(dest)) |
---|
1941 | return USE_SOURCE; |
---|
1942 | return USE_DEST; |
---|
1943 | } |
---|
1944 | if (!ROOT_CLIENT(source) && ROOT_CLIENT(dest)) { |
---|
1945 | /* prefer root-client over nonroot-client */ |
---|
1946 | if (NONROOT_CLIENT(source)) |
---|
1947 | return USE_DEST; |
---|
1948 | return USE_SOURCE; |
---|
1949 | } |
---|
1950 | |
---|
1951 | /* nonroot client */ |
---|
1952 | if (NONROOT_CLIENT(source) && NONROOT_CLIENT(dest)) { |
---|
1953 | if (source < dest) |
---|
1954 | return USE_SOURCE; |
---|
1955 | return USE_DEST; |
---|
1956 | } |
---|
1957 | if (NONROOT_CLIENT(source) && !NONROOT_CLIENT(dest)) |
---|
1958 | return USE_DEST; |
---|
1959 | if (!NONROOT_CLIENT(source) && NONROOT_CLIENT(dest)) |
---|
1960 | return USE_SOURCE; |
---|
1961 | |
---|
1962 | /* dynamic range */ |
---|
1963 | if (DYNAMIC(source) && DYNAMIC(dest)) { |
---|
1964 | if (source < dest) |
---|
1965 | return USE_SOURCE; |
---|
1966 | return USE_DEST; |
---|
1967 | } |
---|
1968 | if (DYNAMIC(source) && !DYNAMIC(dest)) |
---|
1969 | return USE_DEST; |
---|
1970 | if (!DYNAMIC(source) && DYNAMIC(dest)) |
---|
1971 | return USE_SOURCE; |
---|
1972 | /* |
---|
1973 | if (SERVER(source) && CLIENT(dest)) |
---|
1974 | return USE_SOURCE; |
---|
1975 | |
---|
1976 | if (SERVER(dest) && CLIENT(source)) |
---|
1977 | return USE_DEST; |
---|
1978 | if (ROOT_SERVER(source) && !ROOT_SERVER(dest)) |
---|
1979 | return USE_SOURCE; |
---|
1980 | if (ROOT_SERVER(dest) && !ROOT_SERVER(source)) |
---|
1981 | return USE_DEST; |
---|
1982 | */ |
---|
1983 | /* failing that test... */ |
---|
1984 | if (source < dest) { |
---|
1985 | return USE_SOURCE; |
---|
1986 | } |
---|
1987 | return USE_DEST; |
---|
1988 | |
---|
1989 | } |
---|
1990 | |
---|
1991 | /* Truncate the packet at the suggested length |
---|
1992 | * @param packet the packet opaque pointer |
---|
1993 | * @param size the new length of the packet |
---|
1994 | * @returns the new size of the packet |
---|
1995 | * @note size and the return size refer to the network-level payload of the |
---|
1996 | * packet, and do not include any capture headers. For example, to truncate a |
---|
1997 | * packet after the IP header, set size to sizeof(ethernet_header) + |
---|
1998 | * sizeof(ip_header) |
---|
1999 | * @note If the original network-level payload is smaller than size, then the |
---|
2000 | * original size is returned and the packet is left unchanged. |
---|
2001 | */ |
---|
2002 | DLLEXPORT size_t trace_set_capture_length(libtrace_packet_t *packet, size_t size) { |
---|
2003 | if (!packet) { |
---|
2004 | fprintf(stderr, "NULL packet passed into trace_set_capture_length()\n"); |
---|
2005 | return ~0U; |
---|
2006 | } |
---|
2007 | |
---|
2008 | if (packet->trace->format->set_capture_length) { |
---|
2009 | packet->cached.capture_length = packet->trace->format->set_capture_length(packet,size); |
---|
2010 | return packet->cached.capture_length; |
---|
2011 | } |
---|
2012 | |
---|
2013 | return ~0U; |
---|
2014 | } |
---|
2015 | |
---|
2016 | /* Splits a URI into two components - the format component which is seen before |
---|
2017 | * the ':', and the uridata which follows the ':'. |
---|
2018 | * |
---|
2019 | * Returns a pointer to the URI data, but updates the format parameter to |
---|
2020 | * point to a copy of the format component. |
---|
2021 | */ |
---|
2022 | |
---|
2023 | DLLEXPORT const char * trace_parse_uri(const char *uri, char **format) { |
---|
2024 | const char *uridata = 0; |
---|
2025 | |
---|
2026 | if((uridata = strchr(uri,':')) == NULL) { |
---|
2027 | /* Badly formed URI - needs a : */ |
---|
2028 | return 0; |
---|
2029 | } |
---|
2030 | |
---|
2031 | if ((unsigned)(uridata - uri) > URI_PROTO_LINE) { |
---|
2032 | /* Badly formed URI - uri type is too long */ |
---|
2033 | return 0; |
---|
2034 | } |
---|
2035 | |
---|
2036 | /* NOTE: this is allocated memory - it should be freed by the caller |
---|
2037 | * once they are done with it */ |
---|
2038 | *format=xstrndup(uri, (size_t)(uridata - uri)); |
---|
2039 | |
---|
2040 | /* Push uridata past the delimiter */ |
---|
2041 | uridata++; |
---|
2042 | |
---|
2043 | return uridata; |
---|
2044 | } |
---|
2045 | |
---|
2046 | enum base_format_t trace_get_format(libtrace_packet_t *packet) { |
---|
2047 | if (!packet) { |
---|
2048 | fprintf(stderr, "NULL packet passed into trace_get_format()\n"); |
---|
2049 | return TRACE_FORMAT_UNKNOWN; |
---|
2050 | } |
---|
2051 | |
---|
2052 | return packet->trace->format->type; |
---|
2053 | } |
---|
2054 | |
---|
2055 | DLLEXPORT libtrace_err_t trace_get_err(libtrace_t *trace) |
---|
2056 | { |
---|
2057 | libtrace_err_t err = trace->err; |
---|
2058 | trace->err.err_num = 0; /* "OK" */ |
---|
2059 | trace->err.problem[0]='\0'; |
---|
2060 | return err; |
---|
2061 | } |
---|
2062 | |
---|
2063 | DLLEXPORT bool trace_is_err(libtrace_t *trace) |
---|
2064 | { |
---|
2065 | return trace->err.err_num != 0; |
---|
2066 | } |
---|
2067 | |
---|
2068 | /* Prints the input error status to standard error and clears the error state */ |
---|
2069 | DLLEXPORT void trace_perror(libtrace_t *trace,const char *msg,...) |
---|
2070 | { |
---|
2071 | char buf[256]; |
---|
2072 | va_list va; |
---|
2073 | va_start(va,msg); |
---|
2074 | vsnprintf(buf,sizeof(buf),msg,va); |
---|
2075 | va_end(va); |
---|
2076 | if(trace->err.err_num) { |
---|
2077 | if (trace->uridata) { |
---|
2078 | fprintf(stderr,"%s(%s): %s\n", |
---|
2079 | buf,trace->uridata,trace->err.problem); |
---|
2080 | } else { |
---|
2081 | fprintf(stderr,"%s: %s\n", buf, trace->err.problem); |
---|
2082 | } |
---|
2083 | } else { |
---|
2084 | if (trace->uridata) { |
---|
2085 | fprintf(stderr,"%s(%s): No error\n",buf,trace->uridata); |
---|
2086 | } else { |
---|
2087 | fprintf(stderr,"%s: No error\n", buf); |
---|
2088 | } |
---|
2089 | } |
---|
2090 | trace->err.err_num = 0; /* "OK" */ |
---|
2091 | trace->err.problem[0]='\0'; |
---|
2092 | } |
---|
2093 | |
---|
2094 | DLLEXPORT libtrace_err_t trace_get_err_output(libtrace_out_t *trace) |
---|
2095 | { |
---|
2096 | libtrace_err_t err = trace->err; |
---|
2097 | trace->err.err_num = TRACE_ERR_NOERROR; /* "OK" */ |
---|
2098 | trace->err.problem[0]='\0'; |
---|
2099 | return err; |
---|
2100 | } |
---|
2101 | |
---|
2102 | DLLEXPORT bool trace_is_err_output(libtrace_out_t *trace) |
---|
2103 | { |
---|
2104 | return trace->err.err_num != 0; |
---|
2105 | } |
---|
2106 | |
---|
2107 | /* Prints the output error status to standard error and clears the error state |
---|
2108 | */ |
---|
2109 | DLLEXPORT void trace_perror_output(libtrace_out_t *trace,const char *msg,...) |
---|
2110 | { |
---|
2111 | char buf[256]; |
---|
2112 | va_list va; |
---|
2113 | va_start(va,msg); |
---|
2114 | vsnprintf(buf,sizeof(buf),msg,va); |
---|
2115 | va_end(va); |
---|
2116 | if(trace->err.err_num) { |
---|
2117 | fprintf(stderr,"%s(%s): %s\n", |
---|
2118 | buf, |
---|
2119 | trace->uridata?trace->uridata:"no uri", |
---|
2120 | trace->err.problem); |
---|
2121 | } else { |
---|
2122 | fprintf(stderr,"%s(%s): No error\n",buf,trace->uridata); |
---|
2123 | } |
---|
2124 | trace->err.err_num = TRACE_ERR_NOERROR; /* "OK" */ |
---|
2125 | trace->err.problem[0]='\0'; |
---|
2126 | } |
---|
2127 | |
---|
2128 | DLLEXPORT int trace_seek_erf_timestamp(libtrace_t *trace, uint64_t ts) |
---|
2129 | { |
---|
2130 | if (trace->format->seek_erf) { |
---|
2131 | return trace->format->seek_erf(trace,ts); |
---|
2132 | } |
---|
2133 | else { |
---|
2134 | if (trace->format->seek_timeval) { |
---|
2135 | struct timeval tv; |
---|
2136 | #if __BYTE_ORDER == __BIG_ENDIAN |
---|
2137 | tv.tv_sec = ts & 0xFFFFFFFF; |
---|
2138 | tv.tv_usec = ((ts >> 32) * 1000000) & 0xFFFFFFFF; |
---|
2139 | #elif __BYTE_ORDER == __LITTLE_ENDIAN |
---|
2140 | tv.tv_sec = ts >> 32; |
---|
2141 | tv.tv_usec = ((ts&0xFFFFFFFF)*1000000)>>32; |
---|
2142 | #else |
---|
2143 | #error "What on earth are you running this on?" |
---|
2144 | #endif |
---|
2145 | if (tv.tv_usec >= 1000000) { |
---|
2146 | tv.tv_usec -= 1000000; |
---|
2147 | tv.tv_sec += 1; |
---|
2148 | } |
---|
2149 | return trace->format->seek_timeval(trace,tv); |
---|
2150 | } |
---|
2151 | if (trace->format->seek_seconds) { |
---|
2152 | double seconds = |
---|
2153 | (ts>>32) + ((ts & UINT_MAX)*1.0 / UINT_MAX); |
---|
2154 | return trace->format->seek_seconds(trace,seconds); |
---|
2155 | } |
---|
2156 | trace_set_err(trace, |
---|
2157 | TRACE_ERR_OPTION_UNAVAIL, |
---|
2158 | "Feature unimplemented"); |
---|
2159 | return -1; |
---|
2160 | } |
---|
2161 | } |
---|
2162 | |
---|
2163 | DLLEXPORT int trace_seek_seconds(libtrace_t *trace, double seconds) |
---|
2164 | { |
---|
2165 | if (trace->format->seek_seconds) { |
---|
2166 | return trace->format->seek_seconds(trace,seconds); |
---|
2167 | } |
---|
2168 | else { |
---|
2169 | if (trace->format->seek_timeval) { |
---|
2170 | struct timeval tv; |
---|
2171 | tv.tv_sec = (uint32_t)seconds; |
---|
2172 | tv.tv_usec = (uint32_t)(((seconds - tv.tv_sec) * 1000000)/UINT_MAX); |
---|
2173 | return trace->format->seek_timeval(trace,tv); |
---|
2174 | } |
---|
2175 | if (trace->format->seek_erf) { |
---|
2176 | uint64_t timestamp = |
---|
2177 | ((uint64_t)((uint32_t)seconds) << 32) + \ |
---|
2178 | (uint64_t)(( seconds - (uint32_t)seconds ) * UINT_MAX); |
---|
2179 | return trace->format->seek_erf(trace,timestamp); |
---|
2180 | } |
---|
2181 | trace_set_err(trace, |
---|
2182 | TRACE_ERR_OPTION_UNAVAIL, |
---|
2183 | "Feature unimplemented"); |
---|
2184 | return -1; |
---|
2185 | } |
---|
2186 | } |
---|
2187 | |
---|
2188 | DLLEXPORT int trace_seek_timeval(libtrace_t *trace, struct timeval tv) |
---|
2189 | { |
---|
2190 | if (trace->format->seek_timeval) { |
---|
2191 | return trace->format->seek_timeval(trace,tv); |
---|
2192 | } |
---|
2193 | else { |
---|
2194 | if (trace->format->seek_erf) { |
---|
2195 | uint64_t timestamp = ((((uint64_t)tv.tv_sec) << 32) + \ |
---|
2196 | (((uint64_t)tv.tv_usec * UINT_MAX)/1000000)); |
---|
2197 | return trace->format->seek_erf(trace,timestamp); |
---|
2198 | } |
---|
2199 | if (trace->format->seek_seconds) { |
---|
2200 | double seconds = tv.tv_sec + ((tv.tv_usec * 1.0)/1000000); |
---|
2201 | return trace->format->seek_seconds(trace,seconds); |
---|
2202 | } |
---|
2203 | trace_set_err(trace, |
---|
2204 | TRACE_ERR_OPTION_UNAVAIL, |
---|
2205 | "Feature unimplemented"); |
---|
2206 | return -1; |
---|
2207 | } |
---|
2208 | } |
---|
2209 | |
---|
2210 | /* Converts a binary ethernet MAC address into a printable string */ |
---|
2211 | DLLEXPORT char *trace_ether_ntoa(const uint8_t *addr, char *buf) |
---|
2212 | { |
---|
2213 | static char staticbuf[18]={0,}; |
---|
2214 | if (!buf) |
---|
2215 | buf=staticbuf; |
---|
2216 | snprintf(buf,(size_t)18,"%02x:%02x:%02x:%02x:%02x:%02x", |
---|
2217 | addr[0],addr[1],addr[2], |
---|
2218 | addr[3],addr[4],addr[5]); |
---|
2219 | return buf; |
---|
2220 | } |
---|
2221 | |
---|
2222 | /* Converts a printable ethernet MAC address into a binary format */ |
---|
2223 | DLLEXPORT uint8_t *trace_ether_aton(const char *buf, uint8_t *addr) |
---|
2224 | { |
---|
2225 | uint8_t *buf2 = addr; |
---|
2226 | unsigned int tmp[6]; |
---|
2227 | static uint8_t staticaddr[6]; |
---|
2228 | if (!buf2) |
---|
2229 | buf2=staticaddr; |
---|
2230 | sscanf(buf,"%x:%x:%x:%x:%x:%x", |
---|
2231 | &tmp[0],&tmp[1],&tmp[2], |
---|
2232 | &tmp[3],&tmp[4],&tmp[5]); |
---|
2233 | buf2[0]=tmp[0]; buf2[1]=tmp[1]; buf2[2]=tmp[2]; |
---|
2234 | buf2[3]=tmp[3]; buf2[4]=tmp[4]; buf2[5]=tmp[5]; |
---|
2235 | return buf2; |
---|
2236 | } |
---|
2237 | |
---|
2238 | |
---|
2239 | /* Creates a libtrace packet from scratch using the contents of the provided |
---|
2240 | * buffer as the packet payload. |
---|
2241 | * |
---|
2242 | * Unlike trace_prepare_packet(), the buffer should not contain any capture |
---|
2243 | * format headers; instead this function will add the PCAP header to the |
---|
2244 | * packet record. This also means only PCAP packets can be constructed using |
---|
2245 | * this function. |
---|
2246 | * |
---|
2247 | */ |
---|
2248 | DLLEXPORT |
---|
2249 | void trace_construct_packet(libtrace_packet_t *packet, |
---|
2250 | libtrace_linktype_t linktype, |
---|
2251 | const void *data, |
---|
2252 | uint16_t len) { |
---|
2253 | |
---|
2254 | if (!packet) { |
---|
2255 | fprintf(stderr, "NULL packet passed into trace_contruct_packet()\n"); |
---|
2256 | return; |
---|
2257 | } |
---|
2258 | /* Check a valid linktype was supplied */ |
---|
2259 | if (linktype == TRACE_TYPE_UNKNOWN || linktype == TRACE_TYPE_CONTENT_INVALID) { |
---|
2260 | fprintf(stderr, "Unknown or invalid linktype passed into trace_construct_packet()\n"); |
---|
2261 | return; |
---|
2262 | } |
---|
2263 | |
---|
2264 | size_t size; |
---|
2265 | static libtrace_t *deadtrace=NULL; |
---|
2266 | libtrace_pcapfile_pkt_hdr_t hdr; |
---|
2267 | #ifdef WIN32 |
---|
2268 | struct _timeb tstruct; |
---|
2269 | #else |
---|
2270 | struct timeval tv; |
---|
2271 | #endif |
---|
2272 | |
---|
2273 | /* We need a trace to attach the constructed packet to (and it needs |
---|
2274 | * to be PCAP) */ |
---|
2275 | if (NULL == deadtrace) |
---|
2276 | deadtrace=trace_create_dead("pcapfile"); |
---|
2277 | |
---|
2278 | /* Fill in the new PCAP header */ |
---|
2279 | #ifdef WIN32 |
---|
2280 | _ftime(&tstruct); |
---|
2281 | hdr.ts_sec=tstruct.time; |
---|
2282 | hdr.ts_usec=tstruct.millitm * 1000; |
---|
2283 | #else |
---|
2284 | gettimeofday(&tv,NULL); |
---|
2285 | hdr.ts_sec=tv.tv_sec; |
---|
2286 | hdr.ts_usec=tv.tv_usec; |
---|
2287 | #endif |
---|
2288 | |
---|
2289 | hdr.caplen=len; |
---|
2290 | hdr.wirelen=len; |
---|
2291 | |
---|
2292 | /* Now fill in the libtrace packet itself */ |
---|
2293 | if (!deadtrace) { |
---|
2294 | fprintf(stderr, "Unable to create dummy trace for use within trace_construct_packet()\n"); |
---|
2295 | return; |
---|
2296 | } |
---|
2297 | packet->trace=deadtrace; |
---|
2298 | size=len+sizeof(hdr); |
---|
2299 | if (size < LIBTRACE_PACKET_BUFSIZE) |
---|
2300 | size = LIBTRACE_PACKET_BUFSIZE; |
---|
2301 | if (packet->buf_control==TRACE_CTRL_PACKET) { |
---|
2302 | packet->buffer = realloc(packet->buffer, size); |
---|
2303 | } |
---|
2304 | else { |
---|
2305 | packet->buffer = malloc(size); |
---|
2306 | } |
---|
2307 | packet->buf_control=TRACE_CTRL_PACKET; |
---|
2308 | packet->header=packet->buffer; |
---|
2309 | packet->payload=(void*)((char*)packet->buffer+sizeof(hdr)); |
---|
2310 | |
---|
2311 | /* Ugh, memmove - sadly necessary, also beware that we might be |
---|
2312 | * moving data around within this packet, so ordering is important. |
---|
2313 | */ |
---|
2314 | if (data != NULL) { |
---|
2315 | memmove(packet->payload, data, (size_t)len); |
---|
2316 | } else { |
---|
2317 | packet->payload = NULL; |
---|
2318 | } |
---|
2319 | memmove(packet->header, &hdr, sizeof(hdr)); |
---|
2320 | packet->type=pcap_linktype_to_rt(libtrace_to_pcap_linktype(linktype)); |
---|
2321 | |
---|
2322 | trace_clear_cache(packet); |
---|
2323 | } |
---|
2324 | |
---|
2325 | |
---|
2326 | uint64_t trace_get_received_packets(libtrace_t *trace) |
---|
2327 | { |
---|
2328 | uint64_t ret; |
---|
2329 | |
---|
2330 | if (!trace) { |
---|
2331 | fprintf(stderr, "NULL trace passed to trace_get_received_packets()\n"); |
---|
2332 | /* When the number of received packets is not known we return UINT64_MAX */ |
---|
2333 | return UINT64_MAX; |
---|
2334 | } |
---|
2335 | |
---|
2336 | if (trace->format->get_received_packets) { |
---|
2337 | if ((ret = trace->format->get_received_packets(trace)) != UINT64_MAX) |
---|
2338 | return ret; |
---|
2339 | } else if (trace->format->get_statistics) { |
---|
2340 | struct libtrace_stat_t stat; |
---|
2341 | stat.magic = LIBTRACE_STAT_MAGIC; |
---|
2342 | trace_get_statistics(trace, &stat); |
---|
2343 | if (stat.received_valid) |
---|
2344 | return stat.received; |
---|
2345 | } |
---|
2346 | |
---|
2347 | // Read the cached value taken before the trace was paused/closed |
---|
2348 | if(trace->stats && trace->stats->received_valid) |
---|
2349 | return trace->stats->received; |
---|
2350 | else |
---|
2351 | return UINT64_MAX; |
---|
2352 | } |
---|
2353 | |
---|
2354 | uint64_t trace_get_filtered_packets(libtrace_t *trace) |
---|
2355 | { |
---|
2356 | if (!trace) { |
---|
2357 | fprintf(stderr, "NULL trace passed to trace_get_filtered_packets()\n"); |
---|
2358 | return UINT64_MAX; |
---|
2359 | } |
---|
2360 | int i = 0; |
---|
2361 | uint64_t lib_filtered = trace->filtered_packets; |
---|
2362 | for (i = 0; i < trace->perpkt_thread_count; i++) { |
---|
2363 | lib_filtered += trace->perpkt_threads[i].filtered_packets; |
---|
2364 | } |
---|
2365 | if (trace->format->get_filtered_packets) { |
---|
2366 | uint64_t trace_filtered = trace->format->get_filtered_packets(trace); |
---|
2367 | if (trace_filtered == UINT64_MAX) |
---|
2368 | return UINT64_MAX; |
---|
2369 | else |
---|
2370 | return trace_filtered + lib_filtered; |
---|
2371 | } else if (trace->format->get_statistics) { |
---|
2372 | struct libtrace_stat_t stat; |
---|
2373 | stat.magic = LIBTRACE_STAT_MAGIC; |
---|
2374 | trace_get_statistics(trace, &stat); |
---|
2375 | if (stat.filtered_valid) |
---|
2376 | return lib_filtered + stat.filtered; |
---|
2377 | else |
---|
2378 | return UINT64_MAX; |
---|
2379 | } |
---|
2380 | |
---|
2381 | // Read the cached value taken before the trace was paused/closed |
---|
2382 | if(trace->stats && trace->stats->filtered_valid) |
---|
2383 | return trace->stats->filtered + lib_filtered; |
---|
2384 | else |
---|
2385 | return lib_filtered; |
---|
2386 | } |
---|
2387 | |
---|
2388 | uint64_t trace_get_dropped_packets(libtrace_t *trace) |
---|
2389 | { |
---|
2390 | if (!trace) { |
---|
2391 | fprintf(stderr, "NULL trace passed into trace_get_dropped_packets()\n"); |
---|
2392 | return UINT64_MAX; |
---|
2393 | } |
---|
2394 | uint64_t ret; |
---|
2395 | |
---|
2396 | if (trace->format->get_dropped_packets) { |
---|
2397 | if ((ret = trace->format->get_dropped_packets(trace)) != UINT64_MAX) |
---|
2398 | return ret; |
---|
2399 | } else if (trace->format->get_statistics) { |
---|
2400 | struct libtrace_stat_t stat; |
---|
2401 | stat.magic = LIBTRACE_STAT_MAGIC; |
---|
2402 | trace_get_statistics(trace, &stat); |
---|
2403 | if (stat.dropped_valid) |
---|
2404 | return stat.dropped; |
---|
2405 | } |
---|
2406 | |
---|
2407 | // Read the cached value taken before the trace was paused/closed |
---|
2408 | if(trace->stats && trace->stats->dropped_valid) |
---|
2409 | return trace->stats->dropped; |
---|
2410 | else |
---|
2411 | return UINT64_MAX; |
---|
2412 | } |
---|
2413 | |
---|
2414 | uint64_t trace_get_accepted_packets(libtrace_t *trace) |
---|
2415 | { |
---|
2416 | if (!trace) { |
---|
2417 | fprintf(stderr, "NULL trace passed into trace_get_accepted_packets()\n"); |
---|
2418 | return UINT64_MAX; |
---|
2419 | } |
---|
2420 | int i = 0; |
---|
2421 | uint64_t ret = 0; |
---|
2422 | /* We always add to a thread's accepted count before dispatching the |
---|
2423 | * packet to the user. However if the underlying trace is single |
---|
2424 | * threaded it will also be increasing the global count. So if we |
---|
2425 | * find perpkt ignore the global count. |
---|
2426 | */ |
---|
2427 | for (i = 0; i < trace->perpkt_thread_count; i++) { |
---|
2428 | ret += trace->perpkt_threads[i].accepted_packets; |
---|
2429 | } |
---|
2430 | return ret ? ret : trace->accepted_packets; |
---|
2431 | } |
---|
2432 | |
---|
2433 | libtrace_stat_t *trace_get_statistics(libtrace_t *trace, libtrace_stat_t *stat) |
---|
2434 | { |
---|
2435 | uint64_t ret = 0; |
---|
2436 | int i; |
---|
2437 | if (!trace) { |
---|
2438 | fprintf(stderr, "NULL trace passed into trace_get_statistics()\n"); |
---|
2439 | return NULL; |
---|
2440 | } |
---|
2441 | if (stat == NULL) { |
---|
2442 | if (trace->stats == NULL) |
---|
2443 | trace->stats = trace_create_statistics(); |
---|
2444 | stat = trace->stats; |
---|
2445 | } |
---|
2446 | if (stat->magic != LIBTRACE_STAT_MAGIC) { |
---|
2447 | trace_set_err(trace, TRACE_ERR_STAT, "Use trace_create_statistics() to allocate " |
---|
2448 | "statistics prior to calling trace_get_statistics()"); |
---|
2449 | return NULL; |
---|
2450 | } |
---|
2451 | |
---|
2452 | /* If the trace has paused or finished get the cached results */ |
---|
2453 | if (trace->state == STATE_PAUSED || |
---|
2454 | trace->state == STATE_FINISHED || |
---|
2455 | trace->state == STATE_FINISHING || |
---|
2456 | trace->state == STATE_JOINED) { |
---|
2457 | if (trace->stats && trace->stats != stat) |
---|
2458 | *stat = *trace->stats; |
---|
2459 | return stat; |
---|
2460 | } |
---|
2461 | |
---|
2462 | stat->reserved1 = 0; |
---|
2463 | stat->reserved2 = 0; |
---|
2464 | #define X(x) stat->x ##_valid = 0; |
---|
2465 | LIBTRACE_STAT_FIELDS; |
---|
2466 | #undef X |
---|
2467 | /* Both accepted and filtered are stored against in the library */ |
---|
2468 | |
---|
2469 | /* We always add to a thread's accepted count before dispatching the |
---|
2470 | * packet to the user. However if the underlying trace is single |
---|
2471 | * threaded it will also be increasing the global count. So if we |
---|
2472 | * find perpkt ignore the global count. |
---|
2473 | */ |
---|
2474 | for (i = 0; i < trace->perpkt_thread_count; i++) { |
---|
2475 | ret += trace->perpkt_threads[i].accepted_packets; |
---|
2476 | } |
---|
2477 | |
---|
2478 | stat->accepted_valid = 1; |
---|
2479 | stat->accepted = ret ? ret : trace->accepted_packets; |
---|
2480 | |
---|
2481 | stat->filtered_valid = 1; |
---|
2482 | stat->filtered = trace->filtered_packets; |
---|
2483 | for (i = 0; i < trace->perpkt_thread_count; i++) { |
---|
2484 | stat->filtered += trace->perpkt_threads[i].filtered_packets; |
---|
2485 | } |
---|
2486 | |
---|
2487 | if (trace->format->get_statistics) { |
---|
2488 | trace->format->get_statistics(trace, stat); |
---|
2489 | } |
---|
2490 | return stat; |
---|
2491 | } |
---|
2492 | |
---|
2493 | void trace_get_thread_statistics(libtrace_t *trace, libtrace_thread_t *t, |
---|
2494 | libtrace_stat_t *stat) |
---|
2495 | { |
---|
2496 | if (!trace) { |
---|
2497 | fprintf(stderr, "NULL trace passed into trace_get_thread_statistics()\n"); |
---|
2498 | return; |
---|
2499 | } |
---|
2500 | if (!stat) { |
---|
2501 | trace_set_err(trace, TRACE_ERR_STAT, "NULL statistics structure passed into " |
---|
2502 | "trace_get_thread_statistics()"); |
---|
2503 | return; |
---|
2504 | } |
---|
2505 | if (stat->magic != LIBTRACE_STAT_MAGIC) { |
---|
2506 | trace_set_err(trace, TRACE_ERR_STAT, "Use trace_create_statistics() to " |
---|
2507 | "allocate statistics prior to calling trace_get_thread_statistics()"); |
---|
2508 | return; |
---|
2509 | } |
---|
2510 | stat->reserved1 = 0; |
---|
2511 | stat->reserved2 = 0; |
---|
2512 | #define X(x) stat->x ##_valid= 0; |
---|
2513 | LIBTRACE_STAT_FIELDS; |
---|
2514 | #undef X |
---|
2515 | stat->accepted_valid = 1; |
---|
2516 | stat->accepted = t->accepted_packets; |
---|
2517 | stat->filtered_valid = 1; |
---|
2518 | stat->filtered = t->filtered_packets; |
---|
2519 | if (!trace_has_dedicated_hasher(trace) && trace->format->get_thread_statistics) { |
---|
2520 | trace->format->get_thread_statistics(trace, t, stat); |
---|
2521 | } |
---|
2522 | } |
---|
2523 | |
---|
2524 | libtrace_stat_t *trace_create_statistics(void) { |
---|
2525 | libtrace_stat_t *ret; |
---|
2526 | ret = malloc(sizeof(libtrace_stat_t)); |
---|
2527 | if (ret) { |
---|
2528 | memset(ret, 0, sizeof(libtrace_stat_t)); |
---|
2529 | ret->magic = LIBTRACE_STAT_MAGIC; |
---|
2530 | } |
---|
2531 | return ret; |
---|
2532 | } |
---|
2533 | |
---|
2534 | void trace_clear_statistics(libtrace_stat_t *s) { |
---|
2535 | memset(s, 0, sizeof(libtrace_stat_t)); |
---|
2536 | s->magic = LIBTRACE_STAT_MAGIC; |
---|
2537 | } |
---|
2538 | |
---|
2539 | void trace_subtract_statistics(const libtrace_stat_t *a, const libtrace_stat_t *b, |
---|
2540 | libtrace_stat_t *c) { |
---|
2541 | |
---|
2542 | if (a->magic != LIBTRACE_STAT_MAGIC |
---|
2543 | || b->magic != LIBTRACE_STAT_MAGIC |
---|
2544 | || c->magic != LIBTRACE_STAT_MAGIC) { |
---|
2545 | fprintf(stderr, "Use trace_create_statistics() to allocate statistics prior to " |
---|
2546 | "calling trace_subtract_statistics()\n"); |
---|
2547 | return; |
---|
2548 | } |
---|
2549 | |
---|
2550 | #define X(x) \ |
---|
2551 | if (a->x ##_valid && b->x ##_valid) { \ |
---|
2552 | c->x ##_valid = 1; \ |
---|
2553 | c->x = a->x - b->x; \ |
---|
2554 | } else {\ |
---|
2555 | c->x ##_valid = 0;\ |
---|
2556 | } |
---|
2557 | LIBTRACE_STAT_FIELDS |
---|
2558 | #undef X |
---|
2559 | } |
---|
2560 | |
---|
2561 | void trace_add_statistics(const libtrace_stat_t *a, const libtrace_stat_t *b, |
---|
2562 | libtrace_stat_t *c) { |
---|
2563 | if (a->magic != LIBTRACE_STAT_MAGIC |
---|
2564 | || b->magic != LIBTRACE_STAT_MAGIC |
---|
2565 | || c->magic != LIBTRACE_STAT_MAGIC) { |
---|
2566 | fprintf(stderr, "Use trace_create_statistics() to allocate statistics prior to " |
---|
2567 | "calling trace_add_statistics()\n"); |
---|
2568 | return; |
---|
2569 | } |
---|
2570 | |
---|
2571 | #define X(x) \ |
---|
2572 | if (a->x ##_valid&& b->x ##_valid) { \ |
---|
2573 | c->x ##_valid = 1; \ |
---|
2574 | c->x = a->x + b->x; \ |
---|
2575 | } else {\ |
---|
2576 | c->x ##_valid = 0;\ |
---|
2577 | } |
---|
2578 | LIBTRACE_STAT_FIELDS |
---|
2579 | #undef X |
---|
2580 | } |
---|
2581 | |
---|
2582 | int trace_print_statistics(const libtrace_stat_t *s, FILE *f, const char *format) { |
---|
2583 | if (s->magic != LIBTRACE_STAT_MAGIC) { |
---|
2584 | fprintf(stderr, "Use trace_create_statistics() to allocate statistics prior to " |
---|
2585 | "calling trace_print_statistics\n"); |
---|
2586 | return TRACE_ERR_STAT; |
---|
2587 | } |
---|
2588 | if (format == NULL) |
---|
2589 | format = "%s: %"PRIu64"\n"; |
---|
2590 | #define xstr(s) str(s) |
---|
2591 | #define str(s) #s |
---|
2592 | #define X(x) \ |
---|
2593 | if (s->x ##_valid) { \ |
---|
2594 | if (fprintf(f, format, xstr(x), s->x) < 0) \ |
---|
2595 | return -1; \ |
---|
2596 | } |
---|
2597 | LIBTRACE_STAT_FIELDS |
---|
2598 | #undef X |
---|
2599 | return 0; |
---|
2600 | } |
---|
2601 | |
---|
2602 | |
---|
2603 | inline void trace_clear_cache(libtrace_packet_t *packet) { |
---|
2604 | |
---|
2605 | packet->cached = clearcache; |
---|
2606 | } |
---|
2607 | |
---|
2608 | void trace_interrupt(void) { |
---|
2609 | libtrace_halt = 1; |
---|
2610 | } |
---|
2611 | |
---|
2612 | void register_format(struct libtrace_format_t *f) { |
---|
2613 | if (f->next != NULL) { |
---|
2614 | fprintf(stderr, "You cannot register a format twice in register_format()"); |
---|
2615 | return; |
---|
2616 | } |
---|
2617 | f->next=formats_list; |
---|
2618 | formats_list=f; |
---|
2619 | |
---|
2620 | /* Now, verify that the format has at least the minimum functionality. |
---|
2621 | * |
---|
2622 | * This #if can be changed to a 1 to output warnings about inconsistent |
---|
2623 | * functions being provided by format modules. This generally is very |
---|
2624 | * noisy, as almost all modules don't implement one or more functions |
---|
2625 | * for various reasons. This is very useful when checking a new |
---|
2626 | * format module is sane. |
---|
2627 | */ |
---|
2628 | #if 0 |
---|
2629 | if (f->init_input) { |
---|
2630 | #define REQUIRE(x) \ |
---|
2631 | if (!f->x) \ |
---|
2632 | fprintf(stderr,"%s: Input format should provide " #x "\n",f->name) |
---|
2633 | REQUIRE(read_packet); |
---|
2634 | REQUIRE(start_input); |
---|
2635 | REQUIRE(fin_input); |
---|
2636 | REQUIRE(get_link_type); |
---|
2637 | REQUIRE(get_capture_length); |
---|
2638 | REQUIRE(get_wire_length); |
---|
2639 | REQUIRE(get_framing_length); |
---|
2640 | REQUIRE(trace_event); |
---|
2641 | if (!f->get_erf_timestamp |
---|
2642 | && !f->get_seconds |
---|
2643 | && !f->get_timeval) { |
---|
2644 | fprintf(stderr,"%s: A trace format capable of input, should provide at least one of\n" |
---|
2645 | "get_erf_timestamp, get_seconds or trace_timeval\n",f->name); |
---|
2646 | } |
---|
2647 | if (f->trace_event!=trace_event_trace) { |
---|
2648 | /* Theres nothing that a trace file could optimise with |
---|
2649 | * config_input |
---|
2650 | */ |
---|
2651 | REQUIRE(pause_input); |
---|
2652 | REQUIRE(config_input); |
---|
2653 | REQUIRE(get_fd); |
---|
2654 | } |
---|
2655 | else { |
---|
2656 | if (f->get_fd) { |
---|
2657 | fprintf(stderr,"%s: Unnecessary get_fd\n", |
---|
2658 | f->name); |
---|
2659 | } |
---|
2660 | } |
---|
2661 | #undef REQUIRE |
---|
2662 | } |
---|
2663 | else { |
---|
2664 | #define REQUIRE(x) \ |
---|
2665 | if (f->x) \ |
---|
2666 | fprintf(stderr,"%s: Non Input format shouldn't need " #x "\n",f->name) |
---|
2667 | REQUIRE(read_packet); |
---|
2668 | REQUIRE(start_input); |
---|
2669 | REQUIRE(pause_input); |
---|
2670 | REQUIRE(fin_input); |
---|
2671 | REQUIRE(get_link_type); |
---|
2672 | REQUIRE(get_capture_length); |
---|
2673 | REQUIRE(get_wire_length); |
---|
2674 | REQUIRE(get_framing_length); |
---|
2675 | REQUIRE(trace_event); |
---|
2676 | REQUIRE(get_seconds); |
---|
2677 | REQUIRE(get_timeval); |
---|
2678 | REQUIRE(get_erf_timestamp); |
---|
2679 | #undef REQUIRE |
---|
2680 | } |
---|
2681 | if (f->init_output) { |
---|
2682 | #define REQUIRE(x) \ |
---|
2683 | if (!f->x) \ |
---|
2684 | fprintf(stderr,"%s: Output format should provide " #x "\n",f->name) |
---|
2685 | REQUIRE(write_packet); |
---|
2686 | REQUIRE(start_output); |
---|
2687 | REQUIRE(config_output); |
---|
2688 | REQUIRE(fin_output); |
---|
2689 | #undef REQUIRE |
---|
2690 | } |
---|
2691 | else { |
---|
2692 | #define REQUIRE(x) \ |
---|
2693 | if (f->x) \ |
---|
2694 | fprintf(stderr,"%s: Non Output format shouldn't need " #x "\n",f->name) |
---|
2695 | REQUIRE(write_packet); |
---|
2696 | REQUIRE(start_output); |
---|
2697 | REQUIRE(config_output); |
---|
2698 | REQUIRE(fin_output); |
---|
2699 | #undef REQUIRE |
---|
2700 | } |
---|
2701 | #endif |
---|
2702 | } |
---|
2703 | |
---|