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