1 | /* |
---|
2 | * This file is part of libtrace |
---|
3 | * |
---|
4 | * Copyright (c) 2004 The University of Waikato, Hamilton, New Zealand. |
---|
5 | * Authors: Daniel Lawson |
---|
6 | * Perry Lorier |
---|
7 | * |
---|
8 | * All rights reserved. |
---|
9 | * |
---|
10 | * This code has been developed by the University of Waikato WAND |
---|
11 | * research group. For further information please see http://www.wand.net.nz/ |
---|
12 | * |
---|
13 | * libtrace is free software; you can redistribute it and/or modify |
---|
14 | * it under the terms of the GNU General Public License as published by |
---|
15 | * the Free Software Foundation; either version 2 of the License, or |
---|
16 | * (at your option) any later version. |
---|
17 | * |
---|
18 | * libtrace is distributed in the hope that it will be useful, |
---|
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
21 | * GNU General Public License for more details. |
---|
22 | * |
---|
23 | * You should have received a copy of the GNU General Public License |
---|
24 | * along with libtrace; if not, write to the Free Software |
---|
25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
26 | * |
---|
27 | * $Id$ |
---|
28 | * |
---|
29 | */ |
---|
30 | |
---|
31 | |
---|
32 | /* @file |
---|
33 | * |
---|
34 | * @brief Trace file processing library |
---|
35 | * |
---|
36 | * @author Daniel Lawson |
---|
37 | * @author Perry Lorier |
---|
38 | * |
---|
39 | * @internal |
---|
40 | */ |
---|
41 | #define _GNU_SOURCE |
---|
42 | #include "common.h" |
---|
43 | #include "config.h" |
---|
44 | #include <assert.h> |
---|
45 | #include <errno.h> |
---|
46 | #include <fcntl.h> |
---|
47 | #include <stdio.h> |
---|
48 | #include <stdlib.h> |
---|
49 | #include <string.h> |
---|
50 | #include <sys/stat.h> |
---|
51 | #include <sys/types.h> |
---|
52 | #include <stdarg.h> |
---|
53 | |
---|
54 | #ifdef HAVE_LIMITS_H |
---|
55 | # include <limits.h> |
---|
56 | #endif |
---|
57 | |
---|
58 | #ifdef HAVE_SYS_LIMITS_H |
---|
59 | # include <sys/limits.h> |
---|
60 | #endif |
---|
61 | |
---|
62 | #ifdef HAVE_NET_IF_ARP_H |
---|
63 | # include <net/if_arp.h> |
---|
64 | #endif |
---|
65 | |
---|
66 | #ifdef HAVE_NET_IF_H |
---|
67 | # include <net/if.h> |
---|
68 | #endif |
---|
69 | |
---|
70 | #ifdef HAVE_NETINET_IN_H |
---|
71 | # include <netinet/in.h> |
---|
72 | #endif |
---|
73 | |
---|
74 | #ifdef HAVE_NET_ETHERNET_H |
---|
75 | # include <net/ethernet.h> |
---|
76 | #endif |
---|
77 | |
---|
78 | #ifdef HAVE_NETINET_IF_ETHER_H |
---|
79 | # include <netinet/if_ether.h> |
---|
80 | #endif |
---|
81 | |
---|
82 | #include <time.h> |
---|
83 | |
---|
84 | #include "libtrace.h" |
---|
85 | #include "fifo.h" |
---|
86 | #include "libtrace_int.h" |
---|
87 | #include "parse_cmd.h" |
---|
88 | |
---|
89 | #if HAVE_PCAP_BPF_H |
---|
90 | # include <pcap-bpf.h> |
---|
91 | #else |
---|
92 | # ifdef HAVE_NET_BPF_H |
---|
93 | # include <net/bpf.h> |
---|
94 | # endif |
---|
95 | #endif |
---|
96 | |
---|
97 | #include "libtrace_int.h" |
---|
98 | #include "format_helper.h" |
---|
99 | #include "rt_protocol.h" |
---|
100 | |
---|
101 | #define MAXOPTS 1024 |
---|
102 | |
---|
103 | |
---|
104 | static struct libtrace_format_t *formats_list = 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 void xstrncpy(char *dest, const char *src, size_t n) |
---|
110 | { |
---|
111 | strncpy(dest,src,n); |
---|
112 | dest[n]='\0'; |
---|
113 | } |
---|
114 | |
---|
115 | static char *xstrndup(const char *src,size_t n) |
---|
116 | { |
---|
117 | char *ret=(char*)malloc(n+1); |
---|
118 | xstrncpy(ret,src,n); |
---|
119 | return ret; |
---|
120 | } |
---|
121 | |
---|
122 | void register_format(struct libtrace_format_t *f) { |
---|
123 | assert(f->next==NULL); |
---|
124 | f->next=formats_list; |
---|
125 | formats_list=f; |
---|
126 | /* Now, verify things */ |
---|
127 | #if 0 |
---|
128 | if (format_list[nformats]->init_input) { |
---|
129 | #define REQUIRE(x) \ |
---|
130 | if (!format_list[nformats]->x) \ |
---|
131 | fprintf(stderr,"%s: Input format should provide " #x "\n",format_list[nformats]->name) |
---|
132 | REQUIRE(read_packet); |
---|
133 | REQUIRE(start_input); |
---|
134 | REQUIRE(config_input); |
---|
135 | REQUIRE(pause_input); |
---|
136 | REQUIRE(fin_input); |
---|
137 | REQUIRE(get_link_type); |
---|
138 | REQUIRE(get_capture_length); |
---|
139 | REQUIRE(get_wire_length); |
---|
140 | REQUIRE(get_framing_length); |
---|
141 | REQUIRE(trace_event); |
---|
142 | if (!format_list[nformats]->get_erf_timestamp |
---|
143 | && !format_list[nformats]->get_seconds |
---|
144 | && !format_list[nformats]->get_timeval) { |
---|
145 | fprintf(stderr,"%s: A trace format capable of input, should provide at least one of\n" |
---|
146 | "get_erf_timestamp, get_seconds or trace_timeval\n",format_list[nformats]->name); |
---|
147 | } |
---|
148 | if (format_list[nformats]->trace_event==trace_event_device) { |
---|
149 | REQUIRE(get_fd); |
---|
150 | } |
---|
151 | else { |
---|
152 | if (format_list[nformats]->get_fd) { |
---|
153 | fprintf(stderr,"%s: Unnecessary get_fd\n", |
---|
154 | format_list[nformats]->name); |
---|
155 | } |
---|
156 | } |
---|
157 | #undef REQUIRE |
---|
158 | } |
---|
159 | else { |
---|
160 | #define REQUIRE(x) \ |
---|
161 | if (format_list[nformats]->x) \ |
---|
162 | fprintf(stderr,"%s: Non Input format shouldn't need " #x "\n",format_list[nformats]->name) |
---|
163 | REQUIRE(read_packet); |
---|
164 | REQUIRE(start_input); |
---|
165 | REQUIRE(pause_input); |
---|
166 | REQUIRE(fin_input); |
---|
167 | REQUIRE(get_link_type); |
---|
168 | REQUIRE(get_capture_length); |
---|
169 | REQUIRE(get_wire_length); |
---|
170 | REQUIRE(get_framing_length); |
---|
171 | REQUIRE(trace_event); |
---|
172 | REQUIRE(get_seconds); |
---|
173 | REQUIRE(get_timeval); |
---|
174 | REQUIRE(get_erf_timestamp); |
---|
175 | #undef REQUIRE |
---|
176 | } |
---|
177 | if (format_list[nformats]->init_output) { |
---|
178 | #define REQUIRE(x) \ |
---|
179 | if (!format_list[nformats]->x) \ |
---|
180 | fprintf(stderr,"%s: Output format should provide " #x "\n",format_list[nformats]->name) |
---|
181 | REQUIRE(write_packet); |
---|
182 | REQUIRE(start_output); |
---|
183 | REQUIRE(config_output); |
---|
184 | REQUIRE(fin_output); |
---|
185 | #undef REQUIRE |
---|
186 | } |
---|
187 | else { |
---|
188 | #define REQUIRE(x) \ |
---|
189 | if (format_list[nformats]->x) \ |
---|
190 | fprintf(stderr,"%s: Non Output format shouldn't need " #x "\n",format_list[nformats]->name) |
---|
191 | REQUIRE(write_packet); |
---|
192 | REQUIRE(start_output); |
---|
193 | REQUIRE(config_output); |
---|
194 | REQUIRE(fin_output); |
---|
195 | #undef REQUIRE |
---|
196 | } |
---|
197 | #endif |
---|
198 | } |
---|
199 | |
---|
200 | /* Prints help information for libtrace |
---|
201 | * |
---|
202 | * Function prints out some basic help information regarding libtrace, |
---|
203 | * and then prints out the help() function registered with each input module |
---|
204 | */ |
---|
205 | DLLEXPORT void trace_help() { |
---|
206 | struct libtrace_format_t *tmp; |
---|
207 | printf("libtrace %s\n",PACKAGE_VERSION); |
---|
208 | for(tmp=formats_list;tmp;tmp=tmp->next) { |
---|
209 | if (tmp->help) |
---|
210 | tmp->help(); |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | #define RP_BUFSIZE 65536 |
---|
215 | #define URI_PROTO_LINE 16 |
---|
216 | |
---|
217 | /* Gets the name of the output format for a given output trace. |
---|
218 | * |
---|
219 | * @params libtrace the output trace to get the name of the format for |
---|
220 | * @returns callee-owned null-terminated char* containing the output format |
---|
221 | * |
---|
222 | */ |
---|
223 | DLLEXPORT SIMPLE_FUNCTION |
---|
224 | char *trace_get_output_format(const struct libtrace_out_t *libtrace) { |
---|
225 | char * format = libtrace->format->name; |
---|
226 | |
---|
227 | return format; |
---|
228 | } |
---|
229 | |
---|
230 | /* Create a trace file from a URI |
---|
231 | * |
---|
232 | * @params char * containing a valid libtrace URI |
---|
233 | * @returns opaque pointer to a libtrace_t |
---|
234 | * |
---|
235 | * Valid URI's are: |
---|
236 | * erf:/path/to/erf/file |
---|
237 | * erf:/path/to/erf/file.gz |
---|
238 | * erf:/path/to/rtclient/socket |
---|
239 | * erf:- (stdin) |
---|
240 | * dag:/dev/dagcard |
---|
241 | * pcapint:pcapinterface (eg: pcapint:eth0) |
---|
242 | * pcap:/path/to/pcap/file |
---|
243 | * pcap:- |
---|
244 | * rtclient:hostname |
---|
245 | * rtclient:hostname:port |
---|
246 | * wag:- |
---|
247 | * wag:/path/to/wag/file |
---|
248 | * wag:/path/to/wag/file.gz |
---|
249 | * wag:/path/to/wag/socket |
---|
250 | * |
---|
251 | * If an error occured when attempting to open a trace, NULL is returned |
---|
252 | * and an error is output to stdout. |
---|
253 | */ |
---|
254 | DLLEXPORT struct libtrace_t *trace_create(const char *uri) { |
---|
255 | struct libtrace_t *libtrace = |
---|
256 | (struct libtrace_t *)malloc(sizeof(struct libtrace_t)); |
---|
257 | char *scan = 0; |
---|
258 | const char *uridata = 0; |
---|
259 | struct libtrace_format_t *tmp; |
---|
260 | |
---|
261 | libtrace->err.err_num = TRACE_ERR_NOERROR; |
---|
262 | libtrace->format=NULL; |
---|
263 | |
---|
264 | /* parse the URI to determine what sort of event we are dealing with */ |
---|
265 | if ((uridata = trace_parse_uri(uri, &scan)) == 0) { |
---|
266 | trace_set_err(libtrace,TRACE_ERR_BAD_FORMAT,"Bad uri format (%s)",uri); |
---|
267 | return libtrace; |
---|
268 | } |
---|
269 | |
---|
270 | libtrace->event.tdelta = 0.0; |
---|
271 | libtrace->filter = NULL; |
---|
272 | libtrace->snaplen = 0; |
---|
273 | libtrace->started=false; |
---|
274 | |
---|
275 | for (tmp=formats_list;tmp;tmp=tmp->next) { |
---|
276 | if (strlen(scan) == strlen(tmp->name) && |
---|
277 | strncasecmp(scan, tmp->name, strlen(scan)) == 0 |
---|
278 | ) { |
---|
279 | libtrace->format=tmp; |
---|
280 | break; |
---|
281 | } |
---|
282 | } |
---|
283 | if (libtrace->format == 0) { |
---|
284 | trace_set_err(libtrace, TRACE_ERR_BAD_FORMAT, |
---|
285 | "Unknown format (%s)",scan); |
---|
286 | return libtrace; |
---|
287 | } |
---|
288 | |
---|
289 | libtrace->uridata = strdup(uridata); |
---|
290 | /* libtrace->format now contains the type of uri |
---|
291 | * libtrace->uridata contains the appropriate data for this |
---|
292 | */ |
---|
293 | |
---|
294 | if (libtrace->format->init_input) { |
---|
295 | int err=libtrace->format->init_input(libtrace); |
---|
296 | assert (err==-1 || err==0); |
---|
297 | if (err==-1) { |
---|
298 | /* init_input should call trace_set_err to set |
---|
299 | * the error message |
---|
300 | */ |
---|
301 | return libtrace; |
---|
302 | } |
---|
303 | } else { |
---|
304 | trace_set_err(libtrace,TRACE_ERR_NO_INIT, |
---|
305 | "Format does not support input (%s)",scan); |
---|
306 | return libtrace; |
---|
307 | } |
---|
308 | |
---|
309 | |
---|
310 | libtrace->fifo = create_tracefifo(1048576); |
---|
311 | if (!libtrace->fifo) { |
---|
312 | trace_set_err(libtrace,ENOMEM,"Could not allocate memory for fifo"); |
---|
313 | free(scan); |
---|
314 | return libtrace; |
---|
315 | } |
---|
316 | assert(libtrace->fifo); |
---|
317 | free(scan); |
---|
318 | trace_set_err(libtrace,0,""); |
---|
319 | return libtrace; |
---|
320 | } |
---|
321 | |
---|
322 | /* Creates a "dummy" trace file that has only the format type set. |
---|
323 | * |
---|
324 | * @returns opaque pointer to a (sparsely initialised) libtrace_t |
---|
325 | * |
---|
326 | * IMPORTANT: Do not attempt to call trace_read_packet or other such functions |
---|
327 | * with the dummy trace. Its intended purpose is to act as a packet->trace for |
---|
328 | * libtrace_packet_t's that are not associated with a libtrace_t structure. |
---|
329 | */ |
---|
330 | DLLEXPORT struct libtrace_t * trace_create_dead (const char *uri) { |
---|
331 | struct libtrace_t *libtrace = (struct libtrace_t *) |
---|
332 | malloc(sizeof(struct libtrace_t)); |
---|
333 | char *scan = (char *)calloc(sizeof(char),URI_PROTO_LINE); |
---|
334 | char *uridata; |
---|
335 | struct libtrace_format_t *tmp; |
---|
336 | |
---|
337 | libtrace->err.err_num = TRACE_ERR_NOERROR; |
---|
338 | |
---|
339 | if((uridata = strchr(uri,':')) == NULL) { |
---|
340 | xstrncpy(scan, uri, strlen(uri)); |
---|
341 | } else { |
---|
342 | xstrncpy(scan,uri, (uridata - uri)); |
---|
343 | } |
---|
344 | |
---|
345 | libtrace->format = 0; |
---|
346 | |
---|
347 | for(tmp=formats_list;tmp;tmp=tmp->next) { |
---|
348 | if (strlen(scan) == strlen(tmp->name) && |
---|
349 | !strncasecmp(scan, |
---|
350 | tmp->name, |
---|
351 | strlen(scan))) { |
---|
352 | libtrace->format=tmp; |
---|
353 | break; |
---|
354 | } |
---|
355 | } |
---|
356 | if (libtrace->format == 0) { |
---|
357 | trace_set_err(libtrace,TRACE_ERR_BAD_FORMAT, |
---|
358 | "Unknown format (%s)",scan); |
---|
359 | return 0; |
---|
360 | } |
---|
361 | |
---|
362 | libtrace->format_data = NULL; |
---|
363 | free(scan); |
---|
364 | return libtrace; |
---|
365 | |
---|
366 | } |
---|
367 | |
---|
368 | /* Creates a trace output file from a URI. |
---|
369 | * |
---|
370 | * @param uri the uri string describing the output format and destination |
---|
371 | * @returns opaque pointer to a libtrace_output_t |
---|
372 | * |
---|
373 | * If an error occured when attempting to open the output trace, NULL is |
---|
374 | * returned and trace_errno is set. |
---|
375 | */ |
---|
376 | |
---|
377 | DLLEXPORT libtrace_out_t *trace_create_output(const char *uri) { |
---|
378 | libtrace_out_t *libtrace = |
---|
379 | (libtrace_out_t*)malloc(sizeof(struct libtrace_out_t)); |
---|
380 | |
---|
381 | char *scan = 0; |
---|
382 | const char *uridata = 0; |
---|
383 | struct libtrace_format_t *tmp; |
---|
384 | |
---|
385 | libtrace->err.err_num = TRACE_ERR_NOERROR; |
---|
386 | strcat(libtrace->err.problem,"Error message set\n"); |
---|
387 | |
---|
388 | /* parse the URI to determine what sort of event we are dealing with */ |
---|
389 | |
---|
390 | if ((uridata = trace_parse_uri(uri, &scan)) == 0) { |
---|
391 | trace_set_err_out(libtrace,TRACE_ERR_BAD_FORMAT, |
---|
392 | "Bad uri format (%s)",uri); |
---|
393 | return libtrace; |
---|
394 | } |
---|
395 | |
---|
396 | libtrace->format = NULL; |
---|
397 | for(tmp=formats_list;tmp;tmp=tmp->next) { |
---|
398 | if (strlen(scan) == strlen(tmp->name) && |
---|
399 | !strncasecmp(scan, |
---|
400 | tmp->name, |
---|
401 | strlen(scan))) { |
---|
402 | libtrace->format=tmp; |
---|
403 | break; |
---|
404 | } |
---|
405 | } |
---|
406 | if (libtrace->format == NULL) { |
---|
407 | trace_set_err_out(libtrace,TRACE_ERR_BAD_FORMAT, |
---|
408 | "Unknown output format (%s)",scan); |
---|
409 | return libtrace; |
---|
410 | } |
---|
411 | libtrace->uridata = strdup(uridata); |
---|
412 | |
---|
413 | |
---|
414 | /* libtrace->format now contains the type of uri |
---|
415 | * libtrace->uridata contains the appropriate data for this |
---|
416 | */ |
---|
417 | |
---|
418 | if (libtrace->format->init_output) { |
---|
419 | /* 0 on success, -1 on failure */ |
---|
420 | switch(libtrace->format->init_output(libtrace)) { |
---|
421 | case -1: /* failure */ |
---|
422 | free(libtrace); |
---|
423 | return libtrace; |
---|
424 | case 0: /* success */ |
---|
425 | break; |
---|
426 | default: |
---|
427 | assert(!"init_output() should return -1 for failure, or 0 for success"); |
---|
428 | } |
---|
429 | } else { |
---|
430 | trace_set_err_out(libtrace,TRACE_ERR_NO_INIT_OUT, |
---|
431 | "Format does not support writing (%s)",scan); |
---|
432 | return libtrace; |
---|
433 | } |
---|
434 | |
---|
435 | |
---|
436 | free(scan); |
---|
437 | libtrace->started=false; |
---|
438 | return libtrace; |
---|
439 | } |
---|
440 | |
---|
441 | /* Start a trace |
---|
442 | * @param libtrace the input trace to start |
---|
443 | * @returns 0 on success |
---|
444 | * |
---|
445 | * This does the work associated with actually starting up |
---|
446 | * the trace. it may fail. |
---|
447 | */ |
---|
448 | DLLEXPORT int trace_start(struct libtrace_t *libtrace) |
---|
449 | { |
---|
450 | assert(libtrace); |
---|
451 | if (libtrace->format->start_input) { |
---|
452 | int ret=libtrace->format->start_input(libtrace); |
---|
453 | if (ret < 0) { |
---|
454 | return ret; |
---|
455 | } |
---|
456 | } |
---|
457 | |
---|
458 | libtrace->started=true; |
---|
459 | return 0; |
---|
460 | } |
---|
461 | |
---|
462 | DLLEXPORT int trace_start_output(libtrace_out_t *libtrace) |
---|
463 | { |
---|
464 | assert(libtrace); |
---|
465 | if (libtrace->format->start_output) { |
---|
466 | int ret=libtrace->format->start_output(libtrace); |
---|
467 | if (ret < 0) { |
---|
468 | return ret; |
---|
469 | } |
---|
470 | } |
---|
471 | |
---|
472 | libtrace->started=true; |
---|
473 | return 0; |
---|
474 | } |
---|
475 | |
---|
476 | DLLEXPORT int trace_pause(libtrace_t *libtrace) |
---|
477 | { |
---|
478 | assert(libtrace); |
---|
479 | assert(libtrace->started && "BUG: Called trace_pause without calling trace_start first"); |
---|
480 | if (libtrace->format->pause_input) |
---|
481 | libtrace->format->pause_input(libtrace); |
---|
482 | libtrace->started=false; |
---|
483 | return 0; |
---|
484 | } |
---|
485 | |
---|
486 | DLLEXPORT int trace_config(libtrace_t *libtrace, |
---|
487 | trace_option_t option, |
---|
488 | void *value) |
---|
489 | { |
---|
490 | int ret; |
---|
491 | if (libtrace->format->config_input) { |
---|
492 | ret=libtrace->format->config_input(libtrace,option,value); |
---|
493 | if (ret==0) |
---|
494 | return 0; |
---|
495 | } |
---|
496 | switch(option) { |
---|
497 | case TRACE_OPTION_SNAPLEN: |
---|
498 | libtrace->snaplen=*(int*)value; |
---|
499 | return 0; |
---|
500 | case TRACE_OPTION_FILTER: |
---|
501 | libtrace->filter=(struct libtrace_filter_t *)value; |
---|
502 | return 0; |
---|
503 | case TRACE_OPTION_PROMISC: |
---|
504 | trace_set_err(libtrace,TRACE_ERR_OPTION_UNAVAIL, |
---|
505 | "Promisc mode is not supported by this format module"); |
---|
506 | return -1; |
---|
507 | } |
---|
508 | trace_set_err(libtrace,TRACE_ERR_UNKNOWN_OPTION, |
---|
509 | "Unknown option %i", option); |
---|
510 | return -1; |
---|
511 | } |
---|
512 | |
---|
513 | /* Parses an output options string and calls the appropriate function to deal with output options. |
---|
514 | * |
---|
515 | * @param libtrace the output trace object to apply the options to |
---|
516 | * @param options the options string |
---|
517 | * @returns -1 if option configuration failed, 0 otherwise |
---|
518 | * |
---|
519 | * @author Shane Alcock |
---|
520 | */ |
---|
521 | DLLEXPORT int trace_config_output(struct libtrace_out_t *libtrace, |
---|
522 | trace_option_output_t option, |
---|
523 | void *value) { |
---|
524 | if (libtrace->format->config_output) { |
---|
525 | return libtrace->format->config_output(libtrace, option, value); |
---|
526 | } |
---|
527 | return -1; |
---|
528 | } |
---|
529 | |
---|
530 | /* Close a trace file, freeing up any resources it may have been using |
---|
531 | * |
---|
532 | */ |
---|
533 | DLLEXPORT void trace_destroy(struct libtrace_t *libtrace) { |
---|
534 | assert(libtrace); |
---|
535 | if (libtrace->started && libtrace->format->pause_input) |
---|
536 | libtrace->format->pause_input(libtrace); |
---|
537 | libtrace->format->fin_input(libtrace); |
---|
538 | /* need to free things! */ |
---|
539 | free(libtrace->uridata); |
---|
540 | destroy_tracefifo(libtrace->fifo); |
---|
541 | free(libtrace); |
---|
542 | } |
---|
543 | |
---|
544 | |
---|
545 | DLLEXPORT void trace_destroy_dead(struct libtrace_t *libtrace) { |
---|
546 | assert(libtrace); |
---|
547 | free(libtrace); |
---|
548 | } |
---|
549 | /* Close an output trace file, freeing up any resources it may have been using |
---|
550 | * |
---|
551 | * @param libtrace the output trace file to be destroyed |
---|
552 | * |
---|
553 | * @author Shane Alcock |
---|
554 | * */ |
---|
555 | DLLEXPORT void trace_destroy_output(struct libtrace_out_t *libtrace) { |
---|
556 | assert(libtrace); |
---|
557 | libtrace->format->fin_output(libtrace); |
---|
558 | free(libtrace->uridata); |
---|
559 | free(libtrace); |
---|
560 | } |
---|
561 | |
---|
562 | DLLEXPORT libtrace_packet_t *trace_create_packet() { |
---|
563 | libtrace_packet_t *packet = |
---|
564 | (libtrace_packet_t*)calloc(1,sizeof(libtrace_packet_t)); |
---|
565 | packet->buf_control=TRACE_CTRL_PACKET; |
---|
566 | return packet; |
---|
567 | } |
---|
568 | |
---|
569 | DLLEXPORT libtrace_packet_t *trace_copy_packet(const libtrace_packet_t *packet) { |
---|
570 | libtrace_packet_t *dest = |
---|
571 | (libtrace_packet_t *)malloc(sizeof(libtrace_packet_t)); |
---|
572 | dest->trace=packet->trace; |
---|
573 | dest->buffer=malloc( |
---|
574 | trace_get_framing_length(packet) |
---|
575 | +trace_get_capture_length(packet)); |
---|
576 | dest->header=dest->buffer; |
---|
577 | dest->payload=(void*) |
---|
578 | ((char*)dest->buffer+trace_get_framing_length(packet)); |
---|
579 | dest->size=packet->size; |
---|
580 | dest->type=packet->type; |
---|
581 | dest->buf_control=TRACE_CTRL_PACKET; |
---|
582 | memcpy(dest->header,packet->header,trace_get_framing_length(packet)); |
---|
583 | memcpy(dest->payload,packet->payload,trace_get_capture_length(packet)); |
---|
584 | |
---|
585 | return dest; |
---|
586 | } |
---|
587 | |
---|
588 | /** Destroy a packet object |
---|
589 | * |
---|
590 | * sideeffect: sets packet to NULL |
---|
591 | */ |
---|
592 | DLLEXPORT void trace_destroy_packet(struct libtrace_packet_t **packet) { |
---|
593 | if ((*packet)->buf_control == TRACE_CTRL_PACKET) { |
---|
594 | free((*packet)->buffer); |
---|
595 | } |
---|
596 | free((*packet)); |
---|
597 | *packet = NULL; |
---|
598 | } |
---|
599 | |
---|
600 | /* Read one packet from the trace into buffer |
---|
601 | * |
---|
602 | * @param libtrace the libtrace opaque pointer |
---|
603 | * @param packet the packet opaque pointer |
---|
604 | * @returns 0 on EOF, negative value on error |
---|
605 | * |
---|
606 | */ |
---|
607 | DLLEXPORT int trace_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { |
---|
608 | |
---|
609 | assert(libtrace && "You called trace_read_packet() with a NULL libtrace parameter!\n"); |
---|
610 | assert(libtrace->started && "BUG: You must call libtrace_start() before trace_read_packet()\n"); |
---|
611 | assert(packet); |
---|
612 | assert((packet->buf_control==TRACE_CTRL_PACKET || packet->buf_control==TRACE_CTRL_EXTERNAL)&& |
---|
613 | "BUG: You must allocate a packet using packet_create()"); |
---|
614 | |
---|
615 | /* Store the trace we are reading from into the packet opaque |
---|
616 | * structure */ |
---|
617 | packet->trace = libtrace; |
---|
618 | |
---|
619 | if (libtrace->format->read_packet) { |
---|
620 | do { |
---|
621 | packet->size=libtrace->format->read_packet(libtrace,packet); |
---|
622 | if (packet->size==(size_t)-1 || packet->size==0) { |
---|
623 | return packet->size; |
---|
624 | } |
---|
625 | if (libtrace->filter) { |
---|
626 | /* If the filter doesn't match, read another |
---|
627 | * packet |
---|
628 | */ |
---|
629 | if (!trace_bpf_filter(libtrace->filter,packet)){ |
---|
630 | continue; |
---|
631 | } |
---|
632 | } |
---|
633 | if (libtrace->snaplen>0) { |
---|
634 | /* Snap the packet */ |
---|
635 | trace_set_capture_length(packet, |
---|
636 | libtrace->snaplen); |
---|
637 | } |
---|
638 | return packet->size; |
---|
639 | } while(1); |
---|
640 | } |
---|
641 | trace_set_err(libtrace,TRACE_ERR_BAD_FORMAT,"This format does not support reading packets\n"); |
---|
642 | packet->size=-1; |
---|
643 | return -1; |
---|
644 | } |
---|
645 | |
---|
646 | /* Writes a packet to the specified output |
---|
647 | * |
---|
648 | * @param libtrace describes the output format, destination, etc. |
---|
649 | * @param packet the packet to be written out |
---|
650 | * @returns the number of bytes written, -1 if write failed |
---|
651 | * |
---|
652 | * @author Shane Alcock |
---|
653 | * */ |
---|
654 | DLLEXPORT int trace_write_packet(struct libtrace_out_t *libtrace, const struct libtrace_packet_t *packet) { |
---|
655 | assert(libtrace); |
---|
656 | assert(packet); |
---|
657 | /* Verify the packet is valid */ |
---|
658 | assert(packet->size<65536); |
---|
659 | assert(packet->size>0); |
---|
660 | assert(libtrace->started); |
---|
661 | |
---|
662 | if (libtrace->format->write_packet) { |
---|
663 | return libtrace->format->write_packet(libtrace, packet); |
---|
664 | } |
---|
665 | return -1; |
---|
666 | } |
---|
667 | |
---|
668 | DLLEXPORT void *trace_get_link(const struct libtrace_packet_t *packet) { |
---|
669 | return (void *)packet->payload; |
---|
670 | } |
---|
671 | |
---|
672 | /* |
---|
673 | typedef struct legacy_framing { |
---|
674 | uint64_t ts; |
---|
675 | uint32_t crc; |
---|
676 | uint32_t header; |
---|
677 | uint32_t data[12]; // pad to 64 bytes |
---|
678 | } legacy_framing_t; |
---|
679 | */ |
---|
680 | |
---|
681 | |
---|
682 | /* Get the current time in DAG time format |
---|
683 | * @param packet a pointer to a libtrace_packet structure |
---|
684 | * @returns a 64 bit timestamp in DAG ERF format (upper 32 bits are the seconds |
---|
685 | * past 1970-01-01, the lower 32bits are partial seconds) |
---|
686 | * @author Daniel Lawson |
---|
687 | */ |
---|
688 | DLLEXPORT uint64_t trace_get_erf_timestamp(const libtrace_packet_t *packet) { |
---|
689 | uint64_t timestamp = 0; |
---|
690 | double seconds = 0.0; |
---|
691 | struct timeval ts; |
---|
692 | |
---|
693 | assert(packet->size>0 && packet->size<65536); |
---|
694 | |
---|
695 | if (packet->trace->format->get_erf_timestamp) { |
---|
696 | /* timestamp -> timestamp */ |
---|
697 | timestamp = packet->trace->format->get_erf_timestamp(packet); |
---|
698 | } else if (packet->trace->format->get_timeval) { |
---|
699 | /* timeval -> timestamp */ |
---|
700 | ts = packet->trace->format->get_timeval(packet); |
---|
701 | timestamp = ((((uint64_t)ts.tv_sec) << 32) + \ |
---|
702 | (((uint64_t)ts.tv_usec * UINT_MAX)/1000000)); |
---|
703 | } else if (packet->trace->format->get_seconds) { |
---|
704 | /* seconds -> timestamp */ |
---|
705 | seconds = packet->trace->format->get_seconds(packet); |
---|
706 | timestamp = ((uint64_t)((uint32_t)seconds) << 32) + \ |
---|
707 | (uint64_t)(( seconds - (uint32_t)seconds ) * UINT_MAX); |
---|
708 | } |
---|
709 | return timestamp; |
---|
710 | } |
---|
711 | |
---|
712 | /* Get the current time in struct timeval |
---|
713 | * @param packet a pointer to a libtrace_packet structure |
---|
714 | * |
---|
715 | * @returns time that this packet was seen in a struct timeval |
---|
716 | * @author Daniel Lawson |
---|
717 | * @author Perry Lorier |
---|
718 | */ |
---|
719 | DLLEXPORT struct timeval trace_get_timeval(const libtrace_packet_t *packet) { |
---|
720 | struct timeval tv; |
---|
721 | uint64_t ts = 0; |
---|
722 | double seconds = 0.0; |
---|
723 | assert(packet->size>0 && packet->size<65536); |
---|
724 | if (packet->trace->format->get_timeval) { |
---|
725 | /* timeval -> timeval */ |
---|
726 | tv = packet->trace->format->get_timeval(packet); |
---|
727 | } else if (packet->trace->format->get_erf_timestamp) { |
---|
728 | /* timestamp -> timeval */ |
---|
729 | ts = packet->trace->format->get_erf_timestamp(packet); |
---|
730 | #if __BYTE_ORDER == __BIG_ENDIAN |
---|
731 | tv.tv_sec = ts & 0xFFFFFFFF; |
---|
732 | #elif __BYTE_ORDER == __LITTLE_ENDIAN |
---|
733 | tv.tv_sec = ts >> 32; |
---|
734 | #else |
---|
735 | #error "What on earth are you running this on?" |
---|
736 | #endif |
---|
737 | tv.tv_usec = ((ts&0xFFFFFFFF)*1000000)>>32; |
---|
738 | if (tv.tv_usec >= 1000000) { |
---|
739 | tv.tv_usec -= 1000000; |
---|
740 | tv.tv_sec += 1; |
---|
741 | } |
---|
742 | } else if (packet->trace->format->get_seconds) { |
---|
743 | /* seconds -> timeval */ |
---|
744 | seconds = packet->trace->format->get_seconds(packet); |
---|
745 | tv.tv_sec = (uint32_t)seconds; |
---|
746 | tv.tv_usec = (uint32_t)(((seconds - tv.tv_sec) * 1000000)/UINT_MAX); |
---|
747 | } |
---|
748 | |
---|
749 | return tv; |
---|
750 | } |
---|
751 | |
---|
752 | /* Get the current time in floating point seconds |
---|
753 | * @param packet a pointer to a libtrace_packet structure |
---|
754 | * @returns time that this packet was seen in 64bit floating point seconds |
---|
755 | * @author Perry Lorier |
---|
756 | */ |
---|
757 | DLLEXPORT double trace_get_seconds(const struct libtrace_packet_t *packet) { |
---|
758 | double seconds = 0.0; |
---|
759 | uint64_t ts = 0; |
---|
760 | struct timeval tv; |
---|
761 | |
---|
762 | assert(packet->size>0 && packet->size<65536); |
---|
763 | |
---|
764 | if (packet->trace->format->get_seconds) { |
---|
765 | /* seconds->seconds */ |
---|
766 | seconds = packet->trace->format->get_seconds(packet); |
---|
767 | } else if (packet->trace->format->get_erf_timestamp) { |
---|
768 | /* timestamp -> seconds */ |
---|
769 | ts = packet->trace->format->get_erf_timestamp(packet); |
---|
770 | seconds = (ts>>32) + ((ts & UINT_MAX)*1.0 / UINT_MAX); |
---|
771 | } else if (packet->trace->format->get_timeval) { |
---|
772 | /* timeval -> seconds */ |
---|
773 | tv = packet->trace->format->get_timeval(packet); |
---|
774 | seconds = tv.tv_sec + ((tv.tv_usec * 1.0) / 1000000); |
---|
775 | } |
---|
776 | |
---|
777 | return seconds; |
---|
778 | } |
---|
779 | |
---|
780 | DLLEXPORT size_t trace_get_capture_length(const libtrace_packet_t *packet) { |
---|
781 | |
---|
782 | assert(packet->size<65536); |
---|
783 | |
---|
784 | if (packet->trace->format->get_capture_length) { |
---|
785 | return packet->trace->format->get_capture_length(packet); |
---|
786 | } |
---|
787 | return -1; |
---|
788 | } |
---|
789 | |
---|
790 | /* Get the size of the packet as it was seen on the wire. |
---|
791 | * @param packet a pointer to a libtrace_packet structure |
---|
792 | * |
---|
793 | * @returns the size of the packet as it was on the wire. |
---|
794 | * @author Perry Lorier |
---|
795 | * @author Daniel Lawson |
---|
796 | * @note Due to the trace being a header capture, or anonymisation this may |
---|
797 | * not be the same as the Capture Len. |
---|
798 | */ |
---|
799 | DLLEXPORT size_t trace_get_wire_length(const libtrace_packet_t *packet){ |
---|
800 | assert(packet->size>0 && packet->size<65536); |
---|
801 | |
---|
802 | if (packet->trace->format->get_wire_length) { |
---|
803 | return packet->trace->format->get_wire_length(packet); |
---|
804 | } |
---|
805 | return -1; |
---|
806 | |
---|
807 | } |
---|
808 | |
---|
809 | /* Get the length of the capture framing headers. |
---|
810 | * @param packet the packet opaque pointer |
---|
811 | * @returns the size of the packet as it was on the wire. |
---|
812 | * @author Perry Lorier |
---|
813 | * @author Daniel Lawson |
---|
814 | * @note this length corresponds to the difference between the size of a |
---|
815 | * captured packet in memory, and the captured length of the packet |
---|
816 | */ |
---|
817 | DLLEXPORT SIMPLE_FUNCTION |
---|
818 | size_t trace_get_framing_length(const libtrace_packet_t *packet) { |
---|
819 | if (packet->trace->format->get_framing_length) { |
---|
820 | return packet->trace->format->get_framing_length(packet); |
---|
821 | } |
---|
822 | return -1; |
---|
823 | } |
---|
824 | |
---|
825 | |
---|
826 | /* Get the type of the link layer |
---|
827 | * @param packet a pointer to a libtrace_packet structure |
---|
828 | * @returns libtrace_linktype_t |
---|
829 | * @author Perry Lorier |
---|
830 | * @author Daniel Lawson |
---|
831 | */ |
---|
832 | DLLEXPORT libtrace_linktype_t trace_get_link_type(const libtrace_packet_t *packet ) { |
---|
833 | if (packet->trace->format->get_link_type) { |
---|
834 | return packet->trace->format->get_link_type(packet); |
---|
835 | } |
---|
836 | return (libtrace_linktype_t)-1; |
---|
837 | } |
---|
838 | |
---|
839 | /* process a libtrace event |
---|
840 | * @param trace the libtrace opaque pointer |
---|
841 | * @param packet the libtrace_packet opaque pointer |
---|
842 | * @returns |
---|
843 | * TRACE_EVENT_IOWAIT Waiting on I/O on fd |
---|
844 | * TRACE_EVENT_SLEEP Next event in seconds |
---|
845 | * TRACE_EVENT_PACKET Packet arrived in buffer with size size |
---|
846 | * TRACE_EVENT_TERMINATE Trace terminated (perhaps with an error condition) |
---|
847 | * FIXME currently keeps a copy of the packet inside the trace pointer, |
---|
848 | * which in turn is stored inside the new packet object... |
---|
849 | * @author Perry Lorier |
---|
850 | */ |
---|
851 | DLLEXPORT struct libtrace_eventobj_t trace_event(struct libtrace_t *trace, |
---|
852 | struct libtrace_packet_t *packet) { |
---|
853 | struct libtrace_eventobj_t event = {TRACE_EVENT_IOWAIT,0,0.0,0}; |
---|
854 | |
---|
855 | if (!trace) { |
---|
856 | fprintf(stderr,"You called trace_event() with a NULL trace object!\n"); |
---|
857 | } |
---|
858 | assert(trace); |
---|
859 | assert(packet); |
---|
860 | |
---|
861 | /* Store the trace we are reading from into the packet opaque |
---|
862 | * structure */ |
---|
863 | packet->trace = trace; |
---|
864 | |
---|
865 | if (packet->trace->format->trace_event) { |
---|
866 | return packet->trace->format->trace_event(trace,packet); |
---|
867 | } else { |
---|
868 | return event; |
---|
869 | } |
---|
870 | |
---|
871 | } |
---|
872 | |
---|
873 | /* setup a BPF filter |
---|
874 | * @param filterstring a char * containing the bpf filter string |
---|
875 | * @returns opaque pointer pointer to a libtrace_filter_t object |
---|
876 | * @author Daniel Lawson |
---|
877 | */ |
---|
878 | DLLEXPORT struct libtrace_filter_t *trace_bpf_setfilter(const char *filterstring) { |
---|
879 | #if HAVE_BPF |
---|
880 | struct libtrace_filter_t *filter = (struct libtrace_filter_t*) |
---|
881 | malloc(sizeof(struct libtrace_filter_t)); |
---|
882 | filter->filterstring = strdup(filterstring); |
---|
883 | filter->flag = 0; |
---|
884 | return filter; |
---|
885 | #else |
---|
886 | fprintf(stderr,"This version of libtrace does not have bpf filter support\n"); |
---|
887 | return 0; |
---|
888 | #endif |
---|
889 | } |
---|
890 | |
---|
891 | /* compile a bpf filter, now we know what trace it's on |
---|
892 | * @internal |
---|
893 | * |
---|
894 | * @returns -1 on error, 0 on success |
---|
895 | */ |
---|
896 | int trace_bpf_compile(libtrace_filter_t *filter, |
---|
897 | const libtrace_packet_t *packet ) { |
---|
898 | #if HAVE_BPF |
---|
899 | void *linkptr = 0; |
---|
900 | assert(filter); |
---|
901 | |
---|
902 | /* If this isn't a real packet, then fail */ |
---|
903 | linkptr = trace_get_link(packet); |
---|
904 | if (!linkptr) { |
---|
905 | trace_set_err(packet->trace, |
---|
906 | TRACE_ERR_BAD_PACKET,"Packet has no payload"); |
---|
907 | return -1; |
---|
908 | } |
---|
909 | |
---|
910 | if (filter->filterstring && ! filter->flag) { |
---|
911 | pcap_t *pcap; |
---|
912 | libtrace_linktype_t linktype=trace_get_link_type(packet); |
---|
913 | if (linktype==(libtrace_linktype_t)-1) { |
---|
914 | trace_set_err(packet->trace,TRACE_ERR_BAD_PACKET, |
---|
915 | "Packet has an unknown linktype"); |
---|
916 | return -1; |
---|
917 | } |
---|
918 | if (libtrace_to_pcap_dlt(linktype) == -1) { |
---|
919 | trace_set_err(packet->trace,TRACE_ERR_BAD_PACKET, |
---|
920 | "Unknown pcap equivilent linktype"); |
---|
921 | return -1; |
---|
922 | } |
---|
923 | pcap=(pcap_t *)pcap_open_dead( |
---|
924 | libtrace_to_pcap_dlt(linktype), |
---|
925 | 1500); |
---|
926 | /* build filter */ |
---|
927 | if (pcap_compile( pcap, &filter->filter, filter->filterstring, |
---|
928 | 1, 0)) { |
---|
929 | pcap_close(pcap); |
---|
930 | trace_set_err(packet->trace,TRACE_ERR_BAD_PACKET, |
---|
931 | "Packet has no payload"); |
---|
932 | return -1; |
---|
933 | } |
---|
934 | pcap_close(pcap); |
---|
935 | filter->flag=1; |
---|
936 | } |
---|
937 | return 0; |
---|
938 | #else |
---|
939 | assert(!"This should never be called when BPF not enabled"); |
---|
940 | trace_set_err(packet->trace,TRACE_ERR_OPTION_UNAVAIL, |
---|
941 | "Feature unavailable"); |
---|
942 | return -1; |
---|
943 | #endif |
---|
944 | } |
---|
945 | |
---|
946 | DLLEXPORT int trace_bpf_filter(struct libtrace_filter_t *filter, |
---|
947 | const struct libtrace_packet_t *packet) { |
---|
948 | #if HAVE_BPF |
---|
949 | void *linkptr = 0; |
---|
950 | int clen = 0; |
---|
951 | assert(filter); |
---|
952 | assert(packet); |
---|
953 | linkptr = trace_get_link(packet); |
---|
954 | if (!linkptr) { |
---|
955 | return 0; |
---|
956 | } |
---|
957 | |
---|
958 | /* We need to compile it now, because before we didn't know what the |
---|
959 | * link type was |
---|
960 | */ |
---|
961 | if (trace_bpf_compile(filter,packet)==-1) |
---|
962 | return -1; |
---|
963 | |
---|
964 | clen = trace_get_capture_length(packet); |
---|
965 | |
---|
966 | assert(filter->flag); |
---|
967 | return bpf_filter(filter->filter.bf_insns, linkptr, clen, clen); |
---|
968 | #else |
---|
969 | fprintf(stderr,"This version of libtrace does not have bpf filter support\n"); |
---|
970 | return 0; |
---|
971 | #endif |
---|
972 | } |
---|
973 | |
---|
974 | /* Set the direction flag, if it has one |
---|
975 | * @param packet the packet opaque pointer |
---|
976 | * @param direction the new direction (0,1,2,3) |
---|
977 | * @returns a signed value containing the direction flag, or -1 if this is not supported |
---|
978 | * @author Daniel Lawson |
---|
979 | */ |
---|
980 | DLLEXPORT int8_t trace_set_direction(struct libtrace_packet_t *packet, int8_t direction) { |
---|
981 | assert(packet); |
---|
982 | assert(packet->size>0 && packet->size<65536); |
---|
983 | if (packet->trace->format->set_direction) { |
---|
984 | return packet->trace->format->set_direction(packet,direction); |
---|
985 | } |
---|
986 | return -1; |
---|
987 | } |
---|
988 | |
---|
989 | /* Get the direction flag, if it has one |
---|
990 | * @param packet a pointer to a libtrace_packet structure |
---|
991 | * @returns a signed value containing the direction flag, or -1 if this is not supported |
---|
992 | * The direction is defined as 0 for packets originating locally (ie, outbound) |
---|
993 | * and 1 for packets originating remotely (ie, inbound). |
---|
994 | * Other values are possible, which might be overloaded to mean special things |
---|
995 | * for a special trace. |
---|
996 | * @author Daniel Lawson |
---|
997 | */ |
---|
998 | DLLEXPORT int8_t trace_get_direction(const struct libtrace_packet_t *packet) { |
---|
999 | assert(packet); |
---|
1000 | assert(packet->size>0 && packet->size<65536); |
---|
1001 | if (packet->trace->format->get_direction) { |
---|
1002 | return packet->trace->format->get_direction(packet); |
---|
1003 | } |
---|
1004 | return -1; |
---|
1005 | } |
---|
1006 | |
---|
1007 | #define ROOT_SERVER(x) ((x) < 512) |
---|
1008 | #define ROOT_CLIENT(x) ((512 <= (x)) && ((x) < 1024)) |
---|
1009 | #define NONROOT_SERVER(x) ((x) >= 5000) |
---|
1010 | #define NONROOT_CLIENT(x) ((1024 <= (x)) && ((x) < 5000)) |
---|
1011 | #define DYNAMIC(x) ((49152 < (x)) && ((x) < 65535)) |
---|
1012 | #define SERVER(x) ROOT_SERVER(x) || NONROOT_SERVER(x) |
---|
1013 | #define CLIENT(x) ROOT_CLIENT(x) || NONROOT_CLIENT(x) |
---|
1014 | |
---|
1015 | /* Attempt to deduce the 'server' port |
---|
1016 | * @param protocol the IP protocol (eg, 6 or 17 for TCP or UDP) |
---|
1017 | * @param source the TCP or UDP source port |
---|
1018 | * @param dest the TCP or UDP destination port |
---|
1019 | * @returns a hint as to which port is the server port |
---|
1020 | * @author Daniel Lawson |
---|
1021 | */ |
---|
1022 | DLLEXPORT int8_t trace_get_server_port(uint8_t protocol UNUSED, uint16_t source, uint16_t dest) { |
---|
1023 | /* |
---|
1024 | * * If the ports are equal, return DEST |
---|
1025 | * * Check for well-known ports in the given protocol |
---|
1026 | * * Root server ports: 0 - 511 |
---|
1027 | * * Root client ports: 512 - 1023 |
---|
1028 | * * non-root client ports: 1024 - 4999 |
---|
1029 | * * non-root server ports: 5000+ |
---|
1030 | * * Check for static ranges: 1024 - 49151 |
---|
1031 | * * Check for dynamic ranges: 49152 - 65535 |
---|
1032 | * * flip a coin. |
---|
1033 | */ |
---|
1034 | |
---|
1035 | /* equal */ |
---|
1036 | if (source == dest) |
---|
1037 | return USE_DEST; |
---|
1038 | |
---|
1039 | /* root server port, 0 - 511 */ |
---|
1040 | if (ROOT_SERVER(source) && ROOT_SERVER(dest)) { |
---|
1041 | if (source < dest) |
---|
1042 | return USE_SOURCE; |
---|
1043 | return USE_DEST; |
---|
1044 | } |
---|
1045 | |
---|
1046 | if (ROOT_SERVER(source) && !ROOT_SERVER(dest)) |
---|
1047 | return USE_SOURCE; |
---|
1048 | if (!ROOT_SERVER(source) && ROOT_SERVER(dest)) |
---|
1049 | return USE_DEST; |
---|
1050 | |
---|
1051 | /* non-root server */ |
---|
1052 | if (NONROOT_SERVER(source) && NONROOT_SERVER(dest)) { |
---|
1053 | if (source < dest) |
---|
1054 | return USE_SOURCE; |
---|
1055 | return USE_DEST; |
---|
1056 | } |
---|
1057 | if (NONROOT_SERVER(source) && !NONROOT_SERVER(dest)) |
---|
1058 | return USE_SOURCE; |
---|
1059 | if (!NONROOT_SERVER(source) && NONROOT_SERVER(dest)) |
---|
1060 | return USE_DEST; |
---|
1061 | |
---|
1062 | /* root client */ |
---|
1063 | if (ROOT_CLIENT(source) && ROOT_CLIENT(dest)) { |
---|
1064 | if (source < dest) |
---|
1065 | return USE_SOURCE; |
---|
1066 | return USE_DEST; |
---|
1067 | } |
---|
1068 | if (ROOT_CLIENT(source) && !ROOT_CLIENT(dest)) { |
---|
1069 | /* prefer root-client over nonroot-client */ |
---|
1070 | if (NONROOT_CLIENT(dest)) |
---|
1071 | return USE_SOURCE; |
---|
1072 | return USE_DEST; |
---|
1073 | } |
---|
1074 | if (!ROOT_CLIENT(source) && ROOT_CLIENT(dest)) { |
---|
1075 | /* prefer root-client over nonroot-client */ |
---|
1076 | if (NONROOT_CLIENT(source)) |
---|
1077 | return USE_DEST; |
---|
1078 | return USE_SOURCE; |
---|
1079 | } |
---|
1080 | |
---|
1081 | /* nonroot client */ |
---|
1082 | if (NONROOT_CLIENT(source) && NONROOT_CLIENT(dest)) { |
---|
1083 | if (source < dest) |
---|
1084 | return USE_SOURCE; |
---|
1085 | return USE_DEST; |
---|
1086 | } |
---|
1087 | if (NONROOT_CLIENT(source) && !NONROOT_CLIENT(dest)) |
---|
1088 | return USE_DEST; |
---|
1089 | if (!NONROOT_CLIENT(source) && NONROOT_CLIENT(dest)) |
---|
1090 | return USE_SOURCE; |
---|
1091 | |
---|
1092 | /* dynamic range */ |
---|
1093 | if (DYNAMIC(source) && DYNAMIC(dest)) |
---|
1094 | if (source < dest) |
---|
1095 | return USE_SOURCE; |
---|
1096 | return USE_DEST; |
---|
1097 | if (DYNAMIC(source) && !DYNAMIC(dest)) |
---|
1098 | return USE_DEST; |
---|
1099 | if (!DYNAMIC(source) && DYNAMIC(dest)) |
---|
1100 | return USE_SOURCE; |
---|
1101 | /* |
---|
1102 | if (SERVER(source) && CLIENT(dest)) |
---|
1103 | return USE_SOURCE; |
---|
1104 | |
---|
1105 | if (SERVER(dest) && CLIENT(source)) |
---|
1106 | return USE_DEST; |
---|
1107 | if (ROOT_SERVER(source) && !ROOT_SERVER(dest)) |
---|
1108 | return USE_SOURCE; |
---|
1109 | if (ROOT_SERVER(dest) && !ROOT_SERVER(source)) |
---|
1110 | return USE_DEST; |
---|
1111 | */ |
---|
1112 | /* failing that test... */ |
---|
1113 | if (source < dest) { |
---|
1114 | return USE_SOURCE; |
---|
1115 | } |
---|
1116 | return USE_DEST; |
---|
1117 | |
---|
1118 | } |
---|
1119 | |
---|
1120 | /* Truncate the packet at the suggested length |
---|
1121 | * @param packet the packet opaque pointer |
---|
1122 | * @param size the new length of the packet |
---|
1123 | * @returns the new size of the packet |
---|
1124 | * @note size and the return size refer to the network-level payload of the |
---|
1125 | * packet, and do not include any capture headers. For example, to truncate a |
---|
1126 | * packet after the IP header, set size to sizeof(ethernet_header) + |
---|
1127 | * sizeof(ip_header) |
---|
1128 | * @note If the original network-level payload is smaller than size, then the |
---|
1129 | * original size is returned and the packet is left unchanged. |
---|
1130 | * @author Daniel Lawson |
---|
1131 | */ |
---|
1132 | DLLEXPORT size_t trace_set_capture_length(struct libtrace_packet_t *packet, size_t size) { |
---|
1133 | assert(packet); |
---|
1134 | assert(packet->size>0 && packet->size<65536); |
---|
1135 | |
---|
1136 | if (packet->trace->format->set_capture_length) { |
---|
1137 | int caplen=packet->trace->format->set_capture_length(packet,size); |
---|
1138 | if (caplen!=-1) { |
---|
1139 | packet->size=trace_get_framing_length(packet)+caplen; |
---|
1140 | } |
---|
1141 | return caplen; |
---|
1142 | } |
---|
1143 | |
---|
1144 | return -1; |
---|
1145 | } |
---|
1146 | |
---|
1147 | DLLEXPORT const char * trace_parse_uri(const char *uri, char **format) { |
---|
1148 | const char *uridata = 0; |
---|
1149 | |
---|
1150 | if((uridata = strchr(uri,':')) == NULL) { |
---|
1151 | /* badly formed URI - needs a : */ |
---|
1152 | return 0; |
---|
1153 | } |
---|
1154 | |
---|
1155 | if ((uridata - uri) > URI_PROTO_LINE) { |
---|
1156 | /* badly formed URI - uri type is too long */ |
---|
1157 | return 0; |
---|
1158 | } |
---|
1159 | |
---|
1160 | *format=xstrndup(uri, (uridata - uri)); |
---|
1161 | |
---|
1162 | /* push uridata past the delimiter */ |
---|
1163 | uridata++; |
---|
1164 | |
---|
1165 | return uridata; |
---|
1166 | } |
---|
1167 | |
---|
1168 | enum base_format_t trace_get_format(struct libtrace_packet_t *packet) |
---|
1169 | { |
---|
1170 | assert(packet); |
---|
1171 | |
---|
1172 | return packet->trace->format->type; |
---|
1173 | } |
---|
1174 | |
---|
1175 | DLLEXPORT libtrace_err_t trace_get_err(libtrace_t *trace) |
---|
1176 | { |
---|
1177 | libtrace_err_t err = trace->err; |
---|
1178 | trace->err.err_num = 0; /* "OK" */ |
---|
1179 | trace->err.problem[0]='\0'; |
---|
1180 | return err; |
---|
1181 | } |
---|
1182 | |
---|
1183 | DLLEXPORT bool trace_is_err(libtrace_t *trace) |
---|
1184 | { |
---|
1185 | return trace->err.err_num != 0; |
---|
1186 | } |
---|
1187 | |
---|
1188 | DLLEXPORT void trace_perror(libtrace_t *trace,const char *msg,...) |
---|
1189 | { |
---|
1190 | char buf[256]; |
---|
1191 | va_list va; |
---|
1192 | va_start(va,msg); |
---|
1193 | vsnprintf(buf,sizeof(buf),msg,va); |
---|
1194 | va_end(va); |
---|
1195 | if(trace->err.err_num) { |
---|
1196 | fprintf(stderr,"%s(%s): %s\n", |
---|
1197 | buf,trace->uridata,trace->err.problem); |
---|
1198 | } else { |
---|
1199 | fprintf(stderr,"%s(%s): No error\n", |
---|
1200 | buf,trace->uridata); |
---|
1201 | } |
---|
1202 | } |
---|
1203 | |
---|
1204 | DLLEXPORT libtrace_err_t trace_get_err_output(libtrace_out_t *trace) |
---|
1205 | { |
---|
1206 | libtrace_err_t err = trace->err; |
---|
1207 | trace->err.err_num = 0; /* "OK" */ |
---|
1208 | trace->err.problem[0]='\0'; |
---|
1209 | return err; |
---|
1210 | } |
---|
1211 | |
---|
1212 | DLLEXPORT bool trace_is_err_output(libtrace_out_t *trace) |
---|
1213 | { |
---|
1214 | return trace->err.err_num != 0; |
---|
1215 | } |
---|
1216 | |
---|
1217 | DLLEXPORT void trace_perror_output(libtrace_out_t *trace,const char *msg,...) |
---|
1218 | { |
---|
1219 | char buf[256]; |
---|
1220 | va_list va; |
---|
1221 | va_start(va,msg); |
---|
1222 | vsnprintf(buf,sizeof(buf),msg,va); |
---|
1223 | va_end(va); |
---|
1224 | if(trace->err.err_num) { |
---|
1225 | fprintf(stderr,"%s(%s): %s\n", |
---|
1226 | buf,trace->uridata,trace->err.problem); |
---|
1227 | } else { |
---|
1228 | fprintf(stderr,"%s(%s): No error\n",buf,trace->uridata); |
---|
1229 | } |
---|
1230 | } |
---|
1231 | |
---|
1232 | DLLEXPORT int trace_seek_erf_timestamp(libtrace_t *trace, uint64_t ts) |
---|
1233 | { |
---|
1234 | if (trace->format->seek_erf) { |
---|
1235 | return trace->format->seek_erf(trace,ts); |
---|
1236 | } |
---|
1237 | else { |
---|
1238 | if (trace->format->seek_timeval) { |
---|
1239 | struct timeval tv; |
---|
1240 | #if __BYTE_ORDER == __BIG_ENDIAN |
---|
1241 | tv.tv_sec = ts & 0xFFFFFFFF; |
---|
1242 | #elif __BYTE_ORDER == __LITTLE_ENDIAN |
---|
1243 | tv.tv_sec = ts >> 32; |
---|
1244 | #else |
---|
1245 | #error "What on earth are you running this on?" |
---|
1246 | #endif |
---|
1247 | tv.tv_usec = ((ts&0xFFFFFFFF)*1000000)>>32; |
---|
1248 | if (tv.tv_usec >= 1000000) { |
---|
1249 | tv.tv_usec -= 1000000; |
---|
1250 | tv.tv_sec += 1; |
---|
1251 | } |
---|
1252 | return trace->format->seek_timeval(trace,tv); |
---|
1253 | } |
---|
1254 | if (trace->format->seek_seconds) { |
---|
1255 | double seconds = |
---|
1256 | (ts>>32) + ((ts & UINT_MAX)*1.0 / UINT_MAX); |
---|
1257 | return trace->format->seek_seconds(trace,seconds); |
---|
1258 | } |
---|
1259 | trace_set_err(trace, |
---|
1260 | TRACE_ERR_OPTION_UNAVAIL, |
---|
1261 | "Feature unimplemented"); |
---|
1262 | return -1; |
---|
1263 | } |
---|
1264 | } |
---|
1265 | |
---|
1266 | DLLEXPORT int trace_seek_seconds(libtrace_t *trace, double seconds) |
---|
1267 | { |
---|
1268 | if (trace->format->seek_seconds) { |
---|
1269 | return trace->format->seek_seconds(trace,seconds); |
---|
1270 | } |
---|
1271 | else { |
---|
1272 | if (trace->format->seek_timeval) { |
---|
1273 | struct timeval tv; |
---|
1274 | tv.tv_sec = (uint32_t)seconds; |
---|
1275 | tv.tv_usec = (uint32_t)(((seconds - tv.tv_sec) * 1000000)/UINT_MAX); |
---|
1276 | return trace->format->seek_timeval(trace,tv); |
---|
1277 | } |
---|
1278 | if (trace->format->seek_erf) { |
---|
1279 | uint64_t timestamp = |
---|
1280 | ((uint64_t)((uint32_t)seconds) << 32) + \ |
---|
1281 | (uint64_t)(( seconds - (uint32_t)seconds ) * UINT_MAX); |
---|
1282 | return trace->format->seek_erf(trace,timestamp); |
---|
1283 | } |
---|
1284 | trace_set_err(trace, |
---|
1285 | TRACE_ERR_OPTION_UNAVAIL, |
---|
1286 | "Feature unimplemented"); |
---|
1287 | return -1; |
---|
1288 | } |
---|
1289 | } |
---|
1290 | |
---|
1291 | DLLEXPORT int trace_seek_timeval(libtrace_t *trace, struct timeval tv) |
---|
1292 | { |
---|
1293 | if (trace->format->seek_timeval) { |
---|
1294 | return trace->format->seek_timeval(trace,tv); |
---|
1295 | } |
---|
1296 | else { |
---|
1297 | if (trace->format->seek_erf) { |
---|
1298 | uint64_t timestamp = ((((uint64_t)tv.tv_sec) << 32) + \ |
---|
1299 | (((uint64_t)tv.tv_usec * UINT_MAX)/1000000)); |
---|
1300 | return trace->format->seek_erf(trace,timestamp); |
---|
1301 | } |
---|
1302 | if (trace->format->seek_seconds) { |
---|
1303 | double seconds = tv.tv_sec + ((tv.tv_usec * 1.0)/1000000); |
---|
1304 | return trace->format->seek_seconds(trace,seconds); |
---|
1305 | } |
---|
1306 | trace_set_err(trace, |
---|
1307 | TRACE_ERR_OPTION_UNAVAIL, |
---|
1308 | "Feature unimplemented"); |
---|
1309 | return -1; |
---|
1310 | } |
---|
1311 | } |
---|
1312 | |
---|
1313 | DLLEXPORT char *trace_ether_ntoa(const uint8_t *addr, char *buf) |
---|
1314 | { |
---|
1315 | char *buf2 = buf; |
---|
1316 | static char staticbuf[18]={0,}; |
---|
1317 | if (!buf2) |
---|
1318 | buf2=staticbuf; |
---|
1319 | snprintf(buf2,18,"%02x:%02x:%02x:%02x:%02x:%02x", |
---|
1320 | addr[0],addr[1],addr[2], |
---|
1321 | addr[3],addr[4],addr[5]); |
---|
1322 | return buf2; |
---|
1323 | } |
---|
1324 | |
---|
1325 | DLLEXPORT uint8_t *trace_ether_aton(const char *buf, uint8_t *addr) |
---|
1326 | { |
---|
1327 | uint8_t *buf2 = addr; |
---|
1328 | unsigned int tmp[6]; |
---|
1329 | static uint8_t staticaddr[6]; |
---|
1330 | if (!buf2) |
---|
1331 | buf2=staticaddr; |
---|
1332 | sscanf(buf,"%x:%x:%x:%x:%x:%x", |
---|
1333 | &tmp[0],&tmp[1],&tmp[2], |
---|
1334 | &tmp[3],&tmp[4],&tmp[5]); |
---|
1335 | buf2[0]=tmp[0]; buf2[1]=tmp[1]; buf2[2]=tmp[2]; |
---|
1336 | buf2[3]=tmp[3]; buf2[4]=tmp[4]; buf2[5]=tmp[5]; |
---|
1337 | return buf2; |
---|
1338 | } |
---|
1339 | |
---|