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