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