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