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