| 1 | /* |
|---|
| 2 | * This file is part of libtrace |
|---|
| 3 | * |
|---|
| 4 | * Copyright (c) 2007,2008,2009,2010 The University of Waikato, Hamilton, |
|---|
| 5 | * New Zealand. |
|---|
| 6 | * |
|---|
| 7 | * Authors: Daniel Lawson |
|---|
| 8 | * Perry Lorier |
|---|
| 9 | * Shane Alcock |
|---|
| 10 | * |
|---|
| 11 | * All rights reserved. |
|---|
| 12 | * |
|---|
| 13 | * This code has been developed by the University of Waikato WAND |
|---|
| 14 | * research group. For further information please see http://www.wand.net.nz/ |
|---|
| 15 | * |
|---|
| 16 | * libtrace is free software; you can redistribute it and/or modify |
|---|
| 17 | * it under the terms of the GNU General Public License as published by |
|---|
| 18 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 19 | * (at your option) any later version. |
|---|
| 20 | * |
|---|
| 21 | * libtrace is distributed in the hope that it will be useful, |
|---|
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 24 | * GNU General Public License for more details. |
|---|
| 25 | * |
|---|
| 26 | * You should have received a copy of the GNU General Public License |
|---|
| 27 | * along with libtrace; if not, write to the Free Software |
|---|
| 28 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 29 | * |
|---|
| 30 | * $Id$ |
|---|
| 31 | * |
|---|
| 32 | */ |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | #define _GNU_SOURCE |
|---|
| 36 | #include "common.h" |
|---|
| 37 | #include "config.h" |
|---|
| 38 | #include <assert.h> |
|---|
| 39 | #include <errno.h> |
|---|
| 40 | #include <fcntl.h> |
|---|
| 41 | #include <stdio.h> |
|---|
| 42 | #include <stdlib.h> |
|---|
| 43 | #include <string.h> |
|---|
| 44 | #include <sys/stat.h> |
|---|
| 45 | #include <sys/types.h> |
|---|
| 46 | #ifndef WIN32 |
|---|
| 47 | #include <sys/socket.h> |
|---|
| 48 | #endif |
|---|
| 49 | #include <stdarg.h> |
|---|
| 50 | |
|---|
| 51 | #ifdef HAVE_LIMITS_H |
|---|
| 52 | # include <limits.h> |
|---|
| 53 | #endif |
|---|
| 54 | |
|---|
| 55 | #ifdef HAVE_SYS_LIMITS_H |
|---|
| 56 | # include <sys/limits.h> |
|---|
| 57 | #endif |
|---|
| 58 | |
|---|
| 59 | #ifdef HAVE_NET_IF_ARP_H |
|---|
| 60 | # include <net/if_arp.h> |
|---|
| 61 | #endif |
|---|
| 62 | |
|---|
| 63 | #ifdef HAVE_NET_IF_H |
|---|
| 64 | # include <net/if.h> |
|---|
| 65 | #endif |
|---|
| 66 | |
|---|
| 67 | #ifdef HAVE_NETINET_IN_H |
|---|
| 68 | # include <netinet/in.h> |
|---|
| 69 | #endif |
|---|
| 70 | |
|---|
| 71 | #ifdef HAVE_NET_ETHERNET_H |
|---|
| 72 | # include <net/ethernet.h> |
|---|
| 73 | #endif |
|---|
| 74 | |
|---|
| 75 | #ifdef HAVE_NETINET_IF_ETHER_H |
|---|
| 76 | # include <netinet/if_ether.h> |
|---|
| 77 | #endif |
|---|
| 78 | |
|---|
| 79 | #include <time.h> |
|---|
| 80 | #ifdef WIN32 |
|---|
| 81 | #include <sys/timeb.h> |
|---|
| 82 | #endif |
|---|
| 83 | |
|---|
| 84 | #include "libtrace.h" |
|---|
| 85 | #include "libtrace_int.h" |
|---|
| 86 | |
|---|
| 87 | #ifdef HAVE_PCAP_BPF_H |
|---|
| 88 | # include <pcap-bpf.h> |
|---|
| 89 | #else |
|---|
| 90 | # ifdef HAVE_NET_BPF_H |
|---|
| 91 | # include <net/bpf.h> |
|---|
| 92 | # endif |
|---|
| 93 | #endif |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | #include "libtrace_int.h" |
|---|
| 97 | #include "format_helper.h" |
|---|
| 98 | #include "rt_protocol.h" |
|---|
| 99 | |
|---|
| 100 | #define MAXOPTS 1024 |
|---|
| 101 | |
|---|
| 102 | /* This file contains much of the implementation of the libtrace API itself. */ |
|---|
| 103 | |
|---|
| 104 | static struct libtrace_format_t *formats_list = NULL; |
|---|
| 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 | if (ret==NULL) { |
|---|
| 119 | fprintf(stderr,"Out of memory"); |
|---|
| 120 | exit(EXIT_FAILURE); |
|---|
| 121 | } |
|---|
| 122 | xstrncpy(ret,src,n); |
|---|
| 123 | return ret; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | /* call all the constructors if they haven't yet all been called */ |
|---|
| 128 | static void trace_init(void) |
|---|
| 129 | { |
|---|
| 130 | if (!formats_list) { |
|---|
| 131 | duck_constructor(); |
|---|
| 132 | erf_constructor(); |
|---|
| 133 | tsh_constructor(); |
|---|
| 134 | legacy_constructor(); |
|---|
| 135 | atmhdr_constructor(); |
|---|
| 136 | #ifdef HAVE_NETPACKET_PACKET_H |
|---|
| 137 | linuxnative_constructor(); |
|---|
| 138 | #endif |
|---|
| 139 | #ifdef HAVE_LIBPCAP |
|---|
| 140 | pcap_constructor(); |
|---|
| 141 | #endif |
|---|
| 142 | #if HAVE_DECL_BIOCSETIF |
|---|
| 143 | bpf_constructor(); |
|---|
| 144 | #endif |
|---|
| 145 | pcapfile_constructor(); |
|---|
| 146 | rt_constructor(); |
|---|
| 147 | #ifdef HAVE_DAG |
|---|
| 148 | dag_constructor(); |
|---|
| 149 | #endif |
|---|
| 150 | } |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | /* Prints help information for libtrace |
|---|
| 154 | * |
|---|
| 155 | * Function prints out some basic help information regarding libtrace, |
|---|
| 156 | * and then prints out the help() function registered with each input module |
|---|
| 157 | */ |
|---|
| 158 | DLLEXPORT void trace_help(void) { |
|---|
| 159 | struct libtrace_format_t *tmp; |
|---|
| 160 | trace_init(); |
|---|
| 161 | printf("libtrace %s\n\n",PACKAGE_VERSION); |
|---|
| 162 | printf("Following this are a list of the format modules supported in this build of libtrace\n\n"); |
|---|
| 163 | for(tmp=formats_list;tmp;tmp=tmp->next) { |
|---|
| 164 | if (tmp->help) |
|---|
| 165 | tmp->help(); |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | #define URI_PROTO_LINE 16U |
|---|
| 170 | |
|---|
| 171 | /* Try to guess which format module is appropriate for a given trace file or |
|---|
| 172 | * device */ |
|---|
| 173 | static void guess_format(libtrace_t *libtrace, const char *filename) |
|---|
| 174 | { |
|---|
| 175 | struct libtrace_format_t *tmp; |
|---|
| 176 | |
|---|
| 177 | /* Try and guess based on filename */ |
|---|
| 178 | for(tmp = formats_list; tmp; tmp=tmp->next) { |
|---|
| 179 | if (tmp->probe_filename && tmp->probe_filename(filename)) { |
|---|
| 180 | libtrace->format = tmp; |
|---|
| 181 | libtrace->uridata = strdup(filename); |
|---|
| 182 | return; |
|---|
| 183 | } |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | libtrace->io = wandio_create(filename); |
|---|
| 187 | if (!libtrace->io) |
|---|
| 188 | return; |
|---|
| 189 | |
|---|
| 190 | /* Try and guess based on file magic */ |
|---|
| 191 | for(tmp = formats_list; tmp; tmp=tmp->next) { |
|---|
| 192 | if (tmp->probe_magic && tmp->probe_magic(libtrace->io)) { |
|---|
| 193 | libtrace->format = tmp; |
|---|
| 194 | libtrace->uridata = strdup(filename); |
|---|
| 195 | return; |
|---|
| 196 | } |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | /* Oh well */ |
|---|
| 200 | return; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | /* Creates an input trace from a URI |
|---|
| 204 | * |
|---|
| 205 | * @params char * containing a valid libtrace URI |
|---|
| 206 | * @returns opaque pointer to a libtrace_t |
|---|
| 207 | * |
|---|
| 208 | * Some valid URI's are: |
|---|
| 209 | * erf:/path/to/erf/file |
|---|
| 210 | * erf:/path/to/erf/file.gz |
|---|
| 211 | * erf:- (stdin) |
|---|
| 212 | * dag:/dev/dagcard |
|---|
| 213 | * pcapint:pcapinterface (eg: pcapint:eth0) |
|---|
| 214 | * pcapfile:/path/to/pcap/file |
|---|
| 215 | * pcapfile:- |
|---|
| 216 | * int:interface (eg: int:eth0) only on Linux |
|---|
| 217 | * rt:hostname |
|---|
| 218 | * rt:hostname:port |
|---|
| 219 | * |
|---|
| 220 | * If an error occured when attempting to open a trace, NULL is returned |
|---|
| 221 | * and an error is output to stdout. |
|---|
| 222 | */ |
|---|
| 223 | DLLEXPORT libtrace_t *trace_create(const char *uri) { |
|---|
| 224 | libtrace_t *libtrace = |
|---|
| 225 | (libtrace_t *)malloc(sizeof(libtrace_t)); |
|---|
| 226 | char *scan = 0; |
|---|
| 227 | const char *uridata = 0; |
|---|
| 228 | |
|---|
| 229 | trace_init(); |
|---|
| 230 | |
|---|
| 231 | assert(uri && "Passing NULL to trace_create makes me a very sad program"); |
|---|
| 232 | |
|---|
| 233 | if (!libtrace) { |
|---|
| 234 | /* Out of memory */ |
|---|
| 235 | return NULL; |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | libtrace->err.err_num = TRACE_ERR_NOERROR; |
|---|
| 239 | libtrace->format=NULL; |
|---|
| 240 | |
|---|
| 241 | libtrace->event.tdelta = 0.0; |
|---|
| 242 | libtrace->event.packet = NULL; |
|---|
| 243 | libtrace->event.psize = 0; |
|---|
| 244 | libtrace->event.trace_last_ts = 0.0; |
|---|
| 245 | libtrace->filter = NULL; |
|---|
| 246 | libtrace->snaplen = 0; |
|---|
| 247 | libtrace->started=false; |
|---|
| 248 | libtrace->uridata = NULL; |
|---|
| 249 | libtrace->io = NULL; |
|---|
| 250 | libtrace->filtered_packets = 0; |
|---|
| 251 | |
|---|
| 252 | /* Parse the URI to determine what sort of trace we are dealing with */ |
|---|
| 253 | if ((uridata = trace_parse_uri(uri, &scan)) == 0) { |
|---|
| 254 | /* Could not parse the URI nicely */ |
|---|
| 255 | guess_format(libtrace,uri); |
|---|
| 256 | if (!libtrace->format) { |
|---|
| 257 | trace_set_err(libtrace,TRACE_ERR_BAD_FORMAT,"Unable to guess format (%s)",uri); |
|---|
| 258 | return libtrace; |
|---|
| 259 | } |
|---|
| 260 | } |
|---|
| 261 | else { |
|---|
| 262 | struct libtrace_format_t *tmp; |
|---|
| 263 | |
|---|
| 264 | /* Find a format that matches the first part of the URI */ |
|---|
| 265 | for (tmp=formats_list;tmp;tmp=tmp->next) { |
|---|
| 266 | if (strlen(scan) == strlen(tmp->name) && |
|---|
| 267 | strncasecmp(scan, tmp->name, strlen(scan)) == 0 |
|---|
| 268 | ) { |
|---|
| 269 | libtrace->format=tmp; |
|---|
| 270 | break; |
|---|
| 271 | } |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | if (libtrace->format == 0) { |
|---|
| 275 | trace_set_err(libtrace, TRACE_ERR_BAD_FORMAT, |
|---|
| 276 | "Unknown format (%s)",scan); |
|---|
| 277 | return libtrace; |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | libtrace->uridata = strdup(uridata); |
|---|
| 281 | } |
|---|
| 282 | /* libtrace->format now contains the type of uri |
|---|
| 283 | * libtrace->uridata contains the appropriate data for this |
|---|
| 284 | */ |
|---|
| 285 | |
|---|
| 286 | /* Call the init_input function for the matching capture format */ |
|---|
| 287 | if (libtrace->format->init_input) { |
|---|
| 288 | int err=libtrace->format->init_input(libtrace); |
|---|
| 289 | assert (err==-1 || err==0); |
|---|
| 290 | if (err==-1) { |
|---|
| 291 | /* init_input should call trace_set_err to set |
|---|
| 292 | * the error message |
|---|
| 293 | */ |
|---|
| 294 | return libtrace; |
|---|
| 295 | } |
|---|
| 296 | } else { |
|---|
| 297 | trace_set_err(libtrace,TRACE_ERR_UNSUPPORTED, |
|---|
| 298 | "Format does not support input (%s)",scan); |
|---|
| 299 | return libtrace; |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | free(scan); |
|---|
| 304 | libtrace->err.err_num=TRACE_ERR_NOERROR; |
|---|
| 305 | libtrace->err.problem[0]='\0'; |
|---|
| 306 | return libtrace; |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | /* Creates a "dummy" trace file that has only the format type set. |
|---|
| 310 | * |
|---|
| 311 | * @returns opaque pointer to a (sparsely initialised) libtrace_t |
|---|
| 312 | * |
|---|
| 313 | * IMPORTANT: Do not attempt to call trace_read_packet or other such functions |
|---|
| 314 | * with the dummy trace. Its intended purpose is to act as a packet->trace for |
|---|
| 315 | * libtrace_packet_t's that are not associated with a libtrace_t structure. |
|---|
| 316 | */ |
|---|
| 317 | DLLEXPORT libtrace_t * trace_create_dead (const char *uri) { |
|---|
| 318 | libtrace_t *libtrace = (libtrace_t *) malloc(sizeof(libtrace_t)); |
|---|
| 319 | char *scan = (char *)calloc(sizeof(char),URI_PROTO_LINE); |
|---|
| 320 | char *uridata; |
|---|
| 321 | struct libtrace_format_t *tmp; |
|---|
| 322 | |
|---|
| 323 | trace_init(); |
|---|
| 324 | |
|---|
| 325 | libtrace->err.err_num = TRACE_ERR_NOERROR; |
|---|
| 326 | |
|---|
| 327 | if((uridata = strchr(uri,':')) == NULL) { |
|---|
| 328 | xstrncpy(scan, uri, strlen(uri)); |
|---|
| 329 | } else { |
|---|
| 330 | xstrncpy(scan,uri, (size_t)(uridata - uri)); |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | libtrace->err.err_num = TRACE_ERR_NOERROR; |
|---|
| 334 | libtrace->format=NULL; |
|---|
| 335 | |
|---|
| 336 | libtrace->event.tdelta = 0.0; |
|---|
| 337 | libtrace->event.packet = NULL; |
|---|
| 338 | libtrace->event.psize = 0; |
|---|
| 339 | libtrace->event.trace_last_ts = 0.0; |
|---|
| 340 | libtrace->filter = NULL; |
|---|
| 341 | libtrace->snaplen = 0; |
|---|
| 342 | libtrace->started=false; |
|---|
| 343 | libtrace->uridata = NULL; |
|---|
| 344 | libtrace->io = NULL; |
|---|
| 345 | libtrace->filtered_packets = 0; |
|---|
| 346 | |
|---|
| 347 | for(tmp=formats_list;tmp;tmp=tmp->next) { |
|---|
| 348 | if (strlen(scan) == strlen(tmp->name) && |
|---|
| 349 | !strncasecmp(scan, |
|---|
| 350 | tmp->name, |
|---|
| 351 | strlen(scan))) { |
|---|
| 352 | libtrace->format=tmp; |
|---|
| 353 | break; |
|---|
| 354 | } |
|---|
| 355 | } |
|---|
| 356 | if (libtrace->format == 0) { |
|---|
| 357 | trace_set_err(libtrace,TRACE_ERR_BAD_FORMAT, |
|---|
| 358 | "Unknown format (%s)",scan); |
|---|
| 359 | } |
|---|
| 360 | |
|---|
| 361 | libtrace->format_data = NULL; |
|---|
| 362 | free(scan); |
|---|
| 363 | return libtrace; |
|---|
| 364 | |
|---|
| 365 | } |
|---|
| 366 | |
|---|
| 367 | /* Creates an output trace from a URI. |
|---|
| 368 | * |
|---|
| 369 | * @param uri the uri string describing the output format and destination |
|---|
| 370 | * @returns opaque pointer to a libtrace_output_t |
|---|
| 371 | * |
|---|
| 372 | * If an error occured when attempting to open the output trace, NULL is |
|---|
| 373 | * returned and trace_errno is set. |
|---|
| 374 | */ |
|---|
| 375 | |
|---|
| 376 | DLLEXPORT libtrace_out_t *trace_create_output(const char *uri) { |
|---|
| 377 | libtrace_out_t *libtrace = |
|---|
| 378 | (libtrace_out_t*)malloc(sizeof(libtrace_out_t)); |
|---|
| 379 | |
|---|
| 380 | char *scan = 0; |
|---|
| 381 | const char *uridata = 0; |
|---|
| 382 | struct libtrace_format_t *tmp; |
|---|
| 383 | |
|---|
| 384 | trace_init(); |
|---|
| 385 | |
|---|
| 386 | libtrace->err.err_num = TRACE_ERR_NOERROR; |
|---|
| 387 | strcpy(libtrace->err.problem,"Error message set\n"); |
|---|
| 388 | libtrace->format = NULL; |
|---|
| 389 | libtrace->uridata = NULL; |
|---|
| 390 | |
|---|
| 391 | /* Parse the URI to determine what capture format we want to write */ |
|---|
| 392 | |
|---|
| 393 | if ((uridata = trace_parse_uri(uri, &scan)) == 0) { |
|---|
| 394 | trace_set_err_out(libtrace,TRACE_ERR_BAD_FORMAT, |
|---|
| 395 | "Bad uri format (%s)",uri); |
|---|
| 396 | return libtrace; |
|---|
| 397 | } |
|---|
| 398 | |
|---|
| 399 | /* Attempt to find the format in the list of supported formats */ |
|---|
| 400 | for(tmp=formats_list;tmp;tmp=tmp->next) { |
|---|
| 401 | if (strlen(scan) == strlen(tmp->name) && |
|---|
| 402 | !strncasecmp(scan, |
|---|
| 403 | tmp->name, |
|---|
| 404 | strlen(scan))) { |
|---|
| 405 | libtrace->format=tmp; |
|---|
| 406 | break; |
|---|
| 407 | } |
|---|
| 408 | } |
|---|
| 409 | free(scan); |
|---|
| 410 | |
|---|
| 411 | if (libtrace->format == NULL) { |
|---|
| 412 | trace_set_err_out(libtrace,TRACE_ERR_BAD_FORMAT, |
|---|
| 413 | "Unknown output format (%s)",scan); |
|---|
| 414 | return libtrace; |
|---|
| 415 | } |
|---|
| 416 | libtrace->uridata = strdup(uridata); |
|---|
| 417 | |
|---|
| 418 | /* libtrace->format now contains the type of uri |
|---|
| 419 | * libtrace->uridata contains the appropriate data for this |
|---|
| 420 | */ |
|---|
| 421 | |
|---|
| 422 | if (libtrace->format->init_output) { |
|---|
| 423 | /* 0 on success, -1 on failure */ |
|---|
| 424 | switch(libtrace->format->init_output(libtrace)) { |
|---|
| 425 | case -1: /* failure */ |
|---|
| 426 | return libtrace; |
|---|
| 427 | case 0: /* success */ |
|---|
| 428 | break; |
|---|
| 429 | default: |
|---|
| 430 | assert(!"Internal error: init_output() should return -1 for failure, or 0 for success"); |
|---|
| 431 | } |
|---|
| 432 | } else { |
|---|
| 433 | trace_set_err_out(libtrace,TRACE_ERR_UNSUPPORTED, |
|---|
| 434 | "Format does not support writing (%s)",scan); |
|---|
| 435 | return libtrace; |
|---|
| 436 | } |
|---|
| 437 | |
|---|
| 438 | |
|---|
| 439 | libtrace->started=false; |
|---|
| 440 | return libtrace; |
|---|
| 441 | } |
|---|
| 442 | |
|---|
| 443 | /* Start an input trace |
|---|
| 444 | * @param libtrace the input trace to start |
|---|
| 445 | * @returns 0 on success |
|---|
| 446 | * |
|---|
| 447 | * This does the work associated with actually starting up |
|---|
| 448 | * the trace. it may fail. |
|---|
| 449 | */ |
|---|
| 450 | DLLEXPORT int trace_start(libtrace_t *libtrace) |
|---|
| 451 | { |
|---|
| 452 | assert(libtrace); |
|---|
| 453 | if (trace_is_err(libtrace)) |
|---|
| 454 | return -1; |
|---|
| 455 | if (libtrace->format->start_input) { |
|---|
| 456 | int ret=libtrace->format->start_input(libtrace); |
|---|
| 457 | if (ret < 0) { |
|---|
| 458 | return ret; |
|---|
| 459 | } |
|---|
| 460 | } |
|---|
| 461 | |
|---|
| 462 | libtrace->started=true; |
|---|
| 463 | return 0; |
|---|
| 464 | } |
|---|
| 465 | |
|---|
| 466 | /* Start an output trace */ |
|---|
| 467 | DLLEXPORT int trace_start_output(libtrace_out_t *libtrace) |
|---|
| 468 | { |
|---|
| 469 | assert(libtrace); |
|---|
| 470 | if (libtrace->format->start_output) { |
|---|
| 471 | int ret=libtrace->format->start_output(libtrace); |
|---|
| 472 | if (ret < 0) { |
|---|
| 473 | return ret; |
|---|
| 474 | } |
|---|
| 475 | } |
|---|
| 476 | |
|---|
| 477 | libtrace->started=true; |
|---|
| 478 | return 0; |
|---|
| 479 | } |
|---|
| 480 | |
|---|
| 481 | DLLEXPORT int trace_pause(libtrace_t *libtrace) |
|---|
| 482 | { |
|---|
| 483 | assert(libtrace); |
|---|
| 484 | if (!libtrace->started) { |
|---|
| 485 | trace_set_err(libtrace,TRACE_ERR_BAD_STATE, "You must call trace_start() before calling trace_pause()"); |
|---|
| 486 | return -1; |
|---|
| 487 | } |
|---|
| 488 | if (libtrace->format->pause_input) |
|---|
| 489 | libtrace->format->pause_input(libtrace); |
|---|
| 490 | libtrace->started=false; |
|---|
| 491 | return 0; |
|---|
| 492 | } |
|---|
| 493 | |
|---|
| 494 | DLLEXPORT int trace_config(libtrace_t *libtrace, |
|---|
| 495 | trace_option_t option, |
|---|
| 496 | void *value) |
|---|
| 497 | { |
|---|
| 498 | int ret; |
|---|
| 499 | libtrace_err_t err; |
|---|
| 500 | |
|---|
| 501 | if (trace_is_err(libtrace)) { |
|---|
| 502 | return -1; |
|---|
| 503 | } |
|---|
| 504 | |
|---|
| 505 | /* If the capture format supports configuration, try using their |
|---|
| 506 | * native configuration first */ |
|---|
| 507 | if (libtrace->format->config_input) { |
|---|
| 508 | ret=libtrace->format->config_input(libtrace,option,value); |
|---|
| 509 | if (ret==0) |
|---|
| 510 | return 0; |
|---|
| 511 | } |
|---|
| 512 | |
|---|
| 513 | /* If we get here, either the native configuration failed or the |
|---|
| 514 | * format did not support configuration. However, libtrace can |
|---|
| 515 | * deal with some options itself, so give that a go */ |
|---|
| 516 | switch(option) { |
|---|
| 517 | case TRACE_OPTION_SNAPLEN: |
|---|
| 518 | /* Clear the error if there was one */ |
|---|
| 519 | if (trace_is_err(libtrace)) { |
|---|
| 520 | err = trace_get_err(libtrace); |
|---|
| 521 | } |
|---|
| 522 | if (*(int*)value<0 |
|---|
| 523 | || *(int*)value>LIBTRACE_PACKET_BUFSIZE) { |
|---|
| 524 | trace_set_err(libtrace,TRACE_ERR_BAD_STATE, |
|---|
| 525 | "Invalid snap length"); |
|---|
| 526 | } |
|---|
| 527 | libtrace->snaplen=*(int*)value; |
|---|
| 528 | return 0; |
|---|
| 529 | case TRACE_OPTION_FILTER: |
|---|
| 530 | /* Clear the error if there was one */ |
|---|
| 531 | if (trace_is_err(libtrace)) { |
|---|
| 532 | err = trace_get_err(libtrace); |
|---|
| 533 | } |
|---|
| 534 | libtrace->filter=(libtrace_filter_t *)value; |
|---|
| 535 | return 0; |
|---|
| 536 | case TRACE_OPTION_PROMISC: |
|---|
| 537 | if (!trace_is_err(libtrace)) { |
|---|
| 538 | trace_set_err(libtrace,TRACE_ERR_OPTION_UNAVAIL, |
|---|
| 539 | "Promisc mode is not supported by this format module"); |
|---|
| 540 | } |
|---|
| 541 | return -1; |
|---|
| 542 | case TRACE_OPTION_META_FREQ: |
|---|
| 543 | if (!trace_is_err(libtrace)) { |
|---|
| 544 | trace_set_err(libtrace, |
|---|
| 545 | TRACE_ERR_OPTION_UNAVAIL, |
|---|
| 546 | "This format does not support meta-data gathering"); |
|---|
| 547 | } |
|---|
| 548 | return -1; |
|---|
| 549 | case TRACE_OPTION_EVENT_REALTIME: |
|---|
| 550 | if (!trace_is_err(libtrace)) { |
|---|
| 551 | trace_set_err(libtrace, |
|---|
| 552 | TRACE_ERR_OPTION_UNAVAIL, |
|---|
| 553 | "This format does not support realtime events"); |
|---|
| 554 | } |
|---|
| 555 | return -1; |
|---|
| 556 | |
|---|
| 557 | } |
|---|
| 558 | if (!trace_is_err(libtrace)) { |
|---|
| 559 | trace_set_err(libtrace,TRACE_ERR_UNKNOWN_OPTION, |
|---|
| 560 | "Unknown option %i", option); |
|---|
| 561 | } |
|---|
| 562 | return -1; |
|---|
| 563 | } |
|---|
| 564 | |
|---|
| 565 | DLLEXPORT int trace_config_output(libtrace_out_t *libtrace, |
|---|
| 566 | trace_option_output_t option, |
|---|
| 567 | void *value) { |
|---|
| 568 | |
|---|
| 569 | /* Unlike the input options, libtrace does not natively support any of |
|---|
| 570 | * the output options - the format module must be able to deal with |
|---|
| 571 | * them. */ |
|---|
| 572 | if (libtrace->format->config_output) { |
|---|
| 573 | return libtrace->format->config_output(libtrace, option, value); |
|---|
| 574 | } |
|---|
| 575 | return -1; |
|---|
| 576 | } |
|---|
| 577 | |
|---|
| 578 | /* Close an input trace file, freeing up any resources it may have been using |
|---|
| 579 | * |
|---|
| 580 | */ |
|---|
| 581 | DLLEXPORT void trace_destroy(libtrace_t *libtrace) { |
|---|
| 582 | assert(libtrace); |
|---|
| 583 | if (libtrace->format) { |
|---|
| 584 | if (libtrace->started && libtrace->format->pause_input) |
|---|
| 585 | libtrace->format->pause_input(libtrace); |
|---|
| 586 | libtrace->format->fin_input(libtrace); |
|---|
| 587 | } |
|---|
| 588 | /* Need to free things! */ |
|---|
| 589 | if (libtrace->uridata) |
|---|
| 590 | free(libtrace->uridata); |
|---|
| 591 | free(libtrace); |
|---|
| 592 | } |
|---|
| 593 | |
|---|
| 594 | |
|---|
| 595 | DLLEXPORT void trace_destroy_dead(libtrace_t *libtrace) { |
|---|
| 596 | assert(libtrace); |
|---|
| 597 | |
|---|
| 598 | /* Don't call pause_input or fin_input, because we should never have |
|---|
| 599 | * used this trace to do any reading anyway. Do make sure we free |
|---|
| 600 | * any format_data that has been created, though. */ |
|---|
| 601 | if (libtrace->format_data) |
|---|
| 602 | free(libtrace->format_data); |
|---|
| 603 | free(libtrace); |
|---|
| 604 | } |
|---|
| 605 | /* Close an output trace file, freeing up any resources it may have been using |
|---|
| 606 | * |
|---|
| 607 | * @param libtrace the output trace file to be destroyed |
|---|
| 608 | */ |
|---|
| 609 | DLLEXPORT void trace_destroy_output(libtrace_out_t *libtrace) |
|---|
| 610 | { |
|---|
| 611 | assert(libtrace); |
|---|
| 612 | if (libtrace->format && libtrace->format->fin_output) |
|---|
| 613 | libtrace->format->fin_output(libtrace); |
|---|
| 614 | if (libtrace->uridata) |
|---|
| 615 | free(libtrace->uridata); |
|---|
| 616 | free(libtrace); |
|---|
| 617 | } |
|---|
| 618 | |
|---|
| 619 | DLLEXPORT libtrace_packet_t *trace_create_packet(void) |
|---|
| 620 | { |
|---|
| 621 | libtrace_packet_t *packet = |
|---|
| 622 | (libtrace_packet_t*)calloc((size_t)1,sizeof(libtrace_packet_t)); |
|---|
| 623 | |
|---|
| 624 | packet->buf_control=TRACE_CTRL_PACKET; |
|---|
| 625 | packet->capture_length=-1; |
|---|
| 626 | return packet; |
|---|
| 627 | } |
|---|
| 628 | |
|---|
| 629 | DLLEXPORT libtrace_packet_t *trace_copy_packet(const libtrace_packet_t *packet) { |
|---|
| 630 | libtrace_packet_t *dest = |
|---|
| 631 | (libtrace_packet_t *)malloc(sizeof(libtrace_packet_t)); |
|---|
| 632 | if (!dest) { |
|---|
| 633 | printf("Out of memory constructing packet\n"); |
|---|
| 634 | abort(); |
|---|
| 635 | } |
|---|
| 636 | dest->trace=packet->trace; |
|---|
| 637 | dest->buffer=malloc(65536); |
|---|
| 638 | if (!dest->buffer) { |
|---|
| 639 | printf("Out of memory allocating buffer memory\n"); |
|---|
| 640 | abort(); |
|---|
| 641 | } |
|---|
| 642 | dest->header=dest->buffer; |
|---|
| 643 | dest->payload=(void*) |
|---|
| 644 | ((char*)dest->buffer+trace_get_framing_length(packet)); |
|---|
| 645 | dest->type=packet->type; |
|---|
| 646 | dest->buf_control=TRACE_CTRL_PACKET; |
|---|
| 647 | /* Reset the cache - better to recalculate than try to convert |
|---|
| 648 | * the values over to the new packet */ |
|---|
| 649 | dest->capture_length = -1; |
|---|
| 650 | dest->l3_header = NULL; |
|---|
| 651 | dest->l3_ethertype = 0; |
|---|
| 652 | |
|---|
| 653 | /* Ooooh nasty memcpys! This is why we want to avoid copying packets |
|---|
| 654 | * as much as possible */ |
|---|
| 655 | memcpy(dest->header,packet->header,trace_get_framing_length(packet)); |
|---|
| 656 | memcpy(dest->payload,packet->payload,trace_get_capture_length(packet)); |
|---|
| 657 | |
|---|
| 658 | return dest; |
|---|
| 659 | } |
|---|
| 660 | |
|---|
| 661 | /** Destroy a packet object |
|---|
| 662 | */ |
|---|
| 663 | DLLEXPORT void trace_destroy_packet(libtrace_packet_t *packet) { |
|---|
| 664 | if (packet->buf_control == TRACE_CTRL_PACKET) { |
|---|
| 665 | free(packet->buffer); |
|---|
| 666 | } |
|---|
| 667 | packet->buf_control=(buf_control_t)'\0'; |
|---|
| 668 | /* A "bad" value to force an assert |
|---|
| 669 | * if this packet is ever reused |
|---|
| 670 | */ |
|---|
| 671 | free(packet); |
|---|
| 672 | } |
|---|
| 673 | |
|---|
| 674 | /* Read one packet from the trace into buffer. Note that this function will |
|---|
| 675 | * block until a packet is read (or EOF is reached). |
|---|
| 676 | * |
|---|
| 677 | * @param libtrace the libtrace opaque pointer |
|---|
| 678 | * @param packet the packet opaque pointer |
|---|
| 679 | * @returns 0 on EOF, negative value on error |
|---|
| 680 | * |
|---|
| 681 | */ |
|---|
| 682 | DLLEXPORT int trace_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { |
|---|
| 683 | |
|---|
| 684 | assert(libtrace && "You called trace_read_packet() with a NULL libtrace parameter!\n"); |
|---|
| 685 | if (trace_is_err(libtrace)) |
|---|
| 686 | return -1; |
|---|
| 687 | if (!libtrace->started) { |
|---|
| 688 | trace_set_err(libtrace,TRACE_ERR_BAD_STATE,"You must call libtrace_start() before trace_read_packet()\n"); |
|---|
| 689 | return -1; |
|---|
| 690 | } |
|---|
| 691 | if (!(packet->buf_control==TRACE_CTRL_PACKET || packet->buf_control==TRACE_CTRL_EXTERNAL)) { |
|---|
| 692 | trace_set_err(libtrace,TRACE_ERR_BAD_STATE,"Packet passed to trace_read_packet() is invalid\n"); |
|---|
| 693 | return -1; |
|---|
| 694 | } |
|---|
| 695 | assert(packet); |
|---|
| 696 | |
|---|
| 697 | /* Store the trace we are reading from into the packet opaque |
|---|
| 698 | * structure */ |
|---|
| 699 | packet->trace = libtrace; |
|---|
| 700 | |
|---|
| 701 | /* Finalise the packet, freeing any resources the format module |
|---|
| 702 | * may have allocated it |
|---|
| 703 | */ |
|---|
| 704 | if (libtrace->format->fin_packet) { |
|---|
| 705 | libtrace->format->fin_packet(packet); |
|---|
| 706 | } |
|---|
| 707 | |
|---|
| 708 | |
|---|
| 709 | if (libtrace->format->read_packet) { |
|---|
| 710 | do { |
|---|
| 711 | size_t ret; |
|---|
| 712 | /* Clear the packet cache */ |
|---|
| 713 | packet->capture_length = -1; |
|---|
| 714 | packet->l3_header = NULL; |
|---|
| 715 | packet->l3_ethertype = 0; |
|---|
| 716 | |
|---|
| 717 | ret=libtrace->format->read_packet(libtrace,packet); |
|---|
| 718 | if (ret==(size_t)-1 || ret==0) { |
|---|
| 719 | return ret; |
|---|
| 720 | } |
|---|
| 721 | if (libtrace->filter) { |
|---|
| 722 | /* If the filter doesn't match, read another |
|---|
| 723 | * packet |
|---|
| 724 | */ |
|---|
| 725 | if (!trace_apply_filter(libtrace->filter,packet)){ |
|---|
| 726 | ++libtrace->filtered_packets; |
|---|
| 727 | continue; |
|---|
| 728 | } |
|---|
| 729 | } |
|---|
| 730 | if (libtrace->snaplen>0) { |
|---|
| 731 | /* Snap the packet */ |
|---|
| 732 | trace_set_capture_length(packet, |
|---|
| 733 | libtrace->snaplen); |
|---|
| 734 | } |
|---|
| 735 | ++libtrace->accepted_packets; |
|---|
| 736 | return ret; |
|---|
| 737 | } while(1); |
|---|
| 738 | } |
|---|
| 739 | trace_set_err(libtrace,TRACE_ERR_UNSUPPORTED,"This format does not support reading packets\n"); |
|---|
| 740 | return ~0U; |
|---|
| 741 | } |
|---|
| 742 | |
|---|
| 743 | /* Converts the provided buffer into a libtrace packet of the given type. |
|---|
| 744 | * |
|---|
| 745 | * Unlike trace_construct_packet, the buffer is expected to begin with the |
|---|
| 746 | * appropriate capture format header for the format type that the packet is |
|---|
| 747 | * being converted to. This also allows for a packet to be converted into |
|---|
| 748 | * just about capture format that is supported by libtrace, provided the |
|---|
| 749 | * format header is present in the buffer. |
|---|
| 750 | * |
|---|
| 751 | * This function is primarily used to convert packets received via the RT |
|---|
| 752 | * protocol back into their original capture format. The RT header encapsulates |
|---|
| 753 | * the original capture format header, so after removing it the packet must |
|---|
| 754 | * have it's header and payload pointers updated and the packet format and type |
|---|
| 755 | * changed, amongst other things. |
|---|
| 756 | * |
|---|
| 757 | * Intended only for internal use at this point - this function is not |
|---|
| 758 | * available through the external libtrace API. |
|---|
| 759 | */ |
|---|
| 760 | int trace_prepare_packet(libtrace_t *trace, libtrace_packet_t *packet, |
|---|
| 761 | void *buffer, libtrace_rt_types_t rt_type, uint32_t flags) { |
|---|
| 762 | |
|---|
| 763 | assert(packet); |
|---|
| 764 | assert(trace); |
|---|
| 765 | |
|---|
| 766 | /* XXX Proper error handling?? */ |
|---|
| 767 | if (buffer == NULL) |
|---|
| 768 | return -1; |
|---|
| 769 | |
|---|
| 770 | if (!(packet->buf_control==TRACE_CTRL_PACKET || packet->buf_control==TRACE_CTRL_EXTERNAL)) { |
|---|
| 771 | trace_set_err(trace,TRACE_ERR_BAD_STATE,"Packet passed to trace_read_packet() is invalid\n"); |
|---|
| 772 | return -1; |
|---|
| 773 | } |
|---|
| 774 | |
|---|
| 775 | packet->trace = trace; |
|---|
| 776 | |
|---|
| 777 | /* Clear packet cache */ |
|---|
| 778 | packet->capture_length = -1; |
|---|
| 779 | packet->l3_header = NULL; |
|---|
| 780 | packet->l3_ethertype = 0; |
|---|
| 781 | |
|---|
| 782 | if (trace->format->prepare_packet) { |
|---|
| 783 | return trace->format->prepare_packet(trace, packet, |
|---|
| 784 | buffer, rt_type, flags); |
|---|
| 785 | } |
|---|
| 786 | trace_set_err(trace, TRACE_ERR_UNSUPPORTED, |
|---|
| 787 | "This format does not support preparing packets\n"); |
|---|
| 788 | return -1; |
|---|
| 789 | |
|---|
| 790 | } |
|---|
| 791 | |
|---|
| 792 | /* Writes a packet to the specified output trace |
|---|
| 793 | * |
|---|
| 794 | * @param libtrace describes the output format, destination, etc. |
|---|
| 795 | * @param packet the packet to be written out |
|---|
| 796 | * @returns the number of bytes written, -1 if write failed |
|---|
| 797 | */ |
|---|
| 798 | DLLEXPORT int trace_write_packet(libtrace_out_t *libtrace, libtrace_packet_t *packet) { |
|---|
| 799 | assert(libtrace); |
|---|
| 800 | assert(packet); |
|---|
| 801 | /* Verify the packet is valid */ |
|---|
| 802 | if (!libtrace->started) { |
|---|
| 803 | trace_set_err_out(libtrace,TRACE_ERR_BAD_STATE, |
|---|
| 804 | "Trace is not started before trace_write_packet"); |
|---|
| 805 | return -1; |
|---|
| 806 | } |
|---|
| 807 | |
|---|
| 808 | if (libtrace->format->write_packet) { |
|---|
| 809 | return libtrace->format->write_packet(libtrace, packet); |
|---|
| 810 | } |
|---|
| 811 | trace_set_err_out(libtrace,TRACE_ERR_UNSUPPORTED, |
|---|
| 812 | "This format does not support writing packets"); |
|---|
| 813 | return -1; |
|---|
| 814 | } |
|---|
| 815 | |
|---|
| 816 | /* Get a pointer to the first byte of the packet payload */ |
|---|
| 817 | DLLEXPORT void *trace_get_packet_buffer(const libtrace_packet_t *packet, |
|---|
| 818 | libtrace_linktype_t *linktype, uint32_t *remaining) { |
|---|
| 819 | int cap_len; |
|---|
| 820 | int wire_len; |
|---|
| 821 | |
|---|
| 822 | assert(packet != NULL); |
|---|
| 823 | if (linktype) *linktype = trace_get_link_type(packet); |
|---|
| 824 | if (remaining) { |
|---|
| 825 | /* I think we should choose the minimum of the capture and |
|---|
| 826 | * wire lengths to be the "remaining" value. If the packet has |
|---|
| 827 | * been padded to increase the capture length, we don't want |
|---|
| 828 | * to allow subsequent protocol decoders to consider the |
|---|
| 829 | * padding as part of the packet. |
|---|
| 830 | * |
|---|
| 831 | * For example, in Auck 4 there is a trace where the IP header |
|---|
| 832 | * length is incorrect (24 bytes) followed by a 20 byte TCP |
|---|
| 833 | * header. Total IP length is 40 bytes. As a result, the |
|---|
| 834 | * legacyatm padding gets treated as the "missing" bytes of |
|---|
| 835 | * the TCP header, which isn't the greatest. We're probably |
|---|
| 836 | * better off returning an incomplete TCP header in that case. |
|---|
| 837 | */ |
|---|
| 838 | |
|---|
| 839 | cap_len = trace_get_capture_length(packet); |
|---|
| 840 | wire_len = trace_get_wire_length(packet); |
|---|
| 841 | |
|---|
| 842 | assert(cap_len >= 0 && wire_len >= 0); |
|---|
| 843 | if (wire_len < cap_len) |
|---|
| 844 | *remaining = wire_len; |
|---|
| 845 | else |
|---|
| 846 | *remaining = cap_len; |
|---|
| 847 | /* *remaining = trace_get_capture_length(packet); */ |
|---|
| 848 | } |
|---|
| 849 | return (void *) packet->payload; |
|---|
| 850 | } |
|---|
| 851 | |
|---|
| 852 | |
|---|
| 853 | /* Get a pointer to the first byte of the packet payload |
|---|
| 854 | * |
|---|
| 855 | * DEPRECATED - use trace_get_packet_buffer() instead */ |
|---|
| 856 | DLLEXPORT void *trace_get_link(const libtrace_packet_t *packet) { |
|---|
| 857 | return (void *)packet->payload; |
|---|
| 858 | } |
|---|
| 859 | |
|---|
| 860 | /* Get the current time in DAG time format |
|---|
| 861 | * @param packet a pointer to a libtrace_packet structure |
|---|
| 862 | * @returns a 64 bit timestamp in DAG ERF format (upper 32 bits are the seconds |
|---|
| 863 | * past 1970-01-01, the lower 32bits are partial seconds) |
|---|
| 864 | */ |
|---|
| 865 | DLLEXPORT uint64_t trace_get_erf_timestamp(const libtrace_packet_t *packet) { |
|---|
| 866 | if (packet->trace->format->get_erf_timestamp) { |
|---|
| 867 | /* timestamp -> timestamp */ |
|---|
| 868 | return packet->trace->format->get_erf_timestamp(packet); |
|---|
| 869 | } else if (packet->trace->format->get_timespec) { |
|---|
| 870 | /* timespec -> timestamp */ |
|---|
| 871 | struct timespec ts; |
|---|
| 872 | ts = packet->trace->format->get_timespec(packet); |
|---|
| 873 | return ((((uint64_t)ts.tv_sec) << 32) + |
|---|
| 874 | (((uint64_t)ts.tv_nsec << 32)/1000000000)); |
|---|
| 875 | } else if (packet->trace->format->get_timeval) { |
|---|
| 876 | /* timeval -> timestamp */ |
|---|
| 877 | struct timeval tv; |
|---|
| 878 | tv = packet->trace->format->get_timeval(packet); |
|---|
| 879 | return ((((uint64_t)tv.tv_sec) << 32) + |
|---|
| 880 | (((uint64_t)tv.tv_usec << 32)/1000000)); |
|---|
| 881 | } else if (packet->trace->format->get_seconds) { |
|---|
| 882 | /* seconds -> timestamp */ |
|---|
| 883 | double seconds = packet->trace->format->get_seconds(packet); |
|---|
| 884 | return (((uint64_t)seconds)<<32) |
|---|
| 885 | + (uint64_t)((seconds-(uint64_t)seconds)*UINT_MAX); |
|---|
| 886 | } |
|---|
| 887 | else { |
|---|
| 888 | return (uint64_t)0; |
|---|
| 889 | } |
|---|
| 890 | |
|---|
| 891 | } |
|---|
| 892 | |
|---|
| 893 | /* Get the current time in struct timeval |
|---|
| 894 | * @param packet a pointer to a libtrace_packet structure |
|---|
| 895 | * |
|---|
| 896 | * @returns time that this packet was seen in a struct timeval |
|---|
| 897 | * @author Daniel Lawson |
|---|
| 898 | * @author Perry Lorier |
|---|
| 899 | */ |
|---|
| 900 | DLLEXPORT struct timeval trace_get_timeval(const libtrace_packet_t *packet) { |
|---|
| 901 | struct timeval tv; |
|---|
| 902 | uint64_t ts = 0; |
|---|
| 903 | if (packet->trace->format->get_timeval) { |
|---|
| 904 | /* timeval -> timeval */ |
|---|
| 905 | tv = packet->trace->format->get_timeval(packet); |
|---|
| 906 | } else if (packet->trace->format->get_erf_timestamp) { |
|---|
| 907 | /* timestamp -> timeval */ |
|---|
| 908 | ts = packet->trace->format->get_erf_timestamp(packet); |
|---|
| 909 | #if __BYTE_ORDER == __BIG_ENDIAN |
|---|
| 910 | tv.tv_sec = ts & 0xFFFFFFFF; |
|---|
| 911 | #elif __BYTE_ORDER == __LITTLE_ENDIAN |
|---|
| 912 | tv.tv_sec = ts >> 32; |
|---|
| 913 | #else |
|---|
| 914 | #error "What on earth are you running this on?" |
|---|
| 915 | #endif |
|---|
| 916 | tv.tv_usec = ((ts&0xFFFFFFFF)*1000000)>>32; |
|---|
| 917 | if (tv.tv_usec >= 1000000) { |
|---|
| 918 | tv.tv_usec -= 1000000; |
|---|
| 919 | tv.tv_sec += 1; |
|---|
| 920 | } |
|---|
| 921 | } else if (packet->trace->format->get_timespec) { |
|---|
| 922 | struct timespec ts = packet->trace->format->get_timespec(packet); |
|---|
| 923 | tv.tv_sec = ts.tv_sec; |
|---|
| 924 | tv.tv_usec = ts.tv_nsec/1000; |
|---|
| 925 | } else if (packet->trace->format->get_seconds) { |
|---|
| 926 | /* seconds -> timeval */ |
|---|
| 927 | double seconds = packet->trace->format->get_seconds(packet); |
|---|
| 928 | tv.tv_sec = (uint32_t)seconds; |
|---|
| 929 | tv.tv_usec = (uint32_t)(((seconds - tv.tv_sec) * 1000000)/UINT_MAX); |
|---|
| 930 | } |
|---|
| 931 | else { |
|---|
| 932 | tv.tv_sec=-1; |
|---|
| 933 | tv.tv_usec=-1; |
|---|
| 934 | } |
|---|
| 935 | |
|---|
| 936 | return tv; |
|---|
| 937 | } |
|---|
| 938 | |
|---|
| 939 | DLLEXPORT struct timespec trace_get_timespec(const libtrace_packet_t *packet) { |
|---|
| 940 | struct timespec ts; |
|---|
| 941 | |
|---|
| 942 | if (packet->trace->format->get_timespec) { |
|---|
| 943 | return packet->trace->format->get_timespec(packet); |
|---|
| 944 | } else if (packet->trace->format->get_erf_timestamp) { |
|---|
| 945 | /* timestamp -> timeval */ |
|---|
| 946 | uint64_t erfts = packet->trace->format->get_erf_timestamp(packet); |
|---|
| 947 | #if __BYTE_ORDER == __BIG_ENDIAN |
|---|
| 948 | ts.tv_sec = erfts & 0xFFFFFFFF; |
|---|
| 949 | #elif __BYTE_ORDER == __LITTLE_ENDIAN |
|---|
| 950 | ts.tv_sec = erfts >> 32; |
|---|
| 951 | #else |
|---|
| 952 | #error "What on earth are you running this on?" |
|---|
| 953 | #endif |
|---|
| 954 | ts.tv_nsec = ((erfts&0xFFFFFFFF)*1000000000)>>32; |
|---|
| 955 | if (ts.tv_nsec >= 1000000000) { |
|---|
| 956 | ts.tv_nsec -= 1000000000; |
|---|
| 957 | ts.tv_sec += 1; |
|---|
| 958 | } |
|---|
| 959 | return ts; |
|---|
| 960 | } else if (packet->trace->format->get_timeval) { |
|---|
| 961 | /* timeval -> timespec */ |
|---|
| 962 | struct timeval tv = packet->trace->format->get_timeval(packet); |
|---|
| 963 | ts.tv_sec = tv.tv_sec; |
|---|
| 964 | ts.tv_nsec = tv.tv_usec*1000; |
|---|
| 965 | return ts; |
|---|
| 966 | } else if (packet->trace->format->get_seconds) { |
|---|
| 967 | /* seconds -> timespec */ |
|---|
| 968 | double seconds = packet->trace->format->get_seconds(packet); |
|---|
| 969 | ts.tv_sec = (uint32_t)seconds; |
|---|
| 970 | ts.tv_nsec = (long)(((seconds - ts.tv_sec) * 1000000000)/UINT_MAX); |
|---|
| 971 | return ts; |
|---|
| 972 | } |
|---|
| 973 | else { |
|---|
| 974 | ts.tv_sec=-1; |
|---|
| 975 | ts.tv_nsec=-1; |
|---|
| 976 | return ts; |
|---|
| 977 | } |
|---|
| 978 | } |
|---|
| 979 | |
|---|
| 980 | |
|---|
| 981 | /* Get the current time in floating point seconds |
|---|
| 982 | * @param packet a pointer to a libtrace_packet structure |
|---|
| 983 | * @returns time that this packet was seen in 64bit floating point seconds |
|---|
| 984 | */ |
|---|
| 985 | DLLEXPORT double trace_get_seconds(const libtrace_packet_t *packet) { |
|---|
| 986 | double seconds = 0.0; |
|---|
| 987 | |
|---|
| 988 | if (packet->trace->format->get_seconds) { |
|---|
| 989 | /* seconds->seconds */ |
|---|
| 990 | seconds = packet->trace->format->get_seconds(packet); |
|---|
| 991 | } else if (packet->trace->format->get_erf_timestamp) { |
|---|
| 992 | /* timestamp -> seconds */ |
|---|
| 993 | uint64_t ts = 0; |
|---|
| 994 | ts = packet->trace->format->get_erf_timestamp(packet); |
|---|
| 995 | seconds = (ts>>32) + ((ts & UINT_MAX)*1.0 / UINT_MAX); |
|---|
| 996 | } else if (packet->trace->format->get_timespec) { |
|---|
| 997 | /* timespec -> seconds */ |
|---|
| 998 | struct timespec ts; |
|---|
| 999 | ts = packet->trace->format->get_timespec(packet); |
|---|
| 1000 | seconds = ts.tv_sec + ((ts.tv_nsec * 1.0) / 1000000000); |
|---|
| 1001 | } else if (packet->trace->format->get_timeval) { |
|---|
| 1002 | /* timeval -> seconds */ |
|---|
| 1003 | struct timeval tv; |
|---|
| 1004 | tv = packet->trace->format->get_timeval(packet); |
|---|
| 1005 | seconds = tv.tv_sec + ((tv.tv_usec * 1.0) / 1000000); |
|---|
| 1006 | } |
|---|
| 1007 | |
|---|
| 1008 | return seconds; |
|---|
| 1009 | } |
|---|
| 1010 | |
|---|
| 1011 | DLLEXPORT size_t trace_get_capture_length(const libtrace_packet_t *packet) |
|---|
| 1012 | { |
|---|
| 1013 | /* Cache the capture length */ |
|---|
| 1014 | if (packet->capture_length == -1) { |
|---|
| 1015 | if (!packet->trace->format->get_capture_length) |
|---|
| 1016 | return ~0U; |
|---|
| 1017 | /* Cast away constness because this is "just" a cache */ |
|---|
| 1018 | ((libtrace_packet_t*)packet)->capture_length = |
|---|
| 1019 | packet->trace->format->get_capture_length(packet); |
|---|
| 1020 | } |
|---|
| 1021 | |
|---|
| 1022 | assert(packet->capture_length < LIBTRACE_PACKET_BUFSIZE); |
|---|
| 1023 | |
|---|
| 1024 | return packet->capture_length; |
|---|
| 1025 | } |
|---|
| 1026 | |
|---|
| 1027 | /* Get the size of the packet as it was seen on the wire. |
|---|
| 1028 | * @param packet a pointer to a libtrace_packet structure |
|---|
| 1029 | * |
|---|
| 1030 | * @returns the size of the packet as it was on the wire. |
|---|
| 1031 | * @note Due to the trace being a header capture, or anonymisation this may |
|---|
| 1032 | * not be the same as the Capture Len. |
|---|
| 1033 | */ |
|---|
| 1034 | DLLEXPORT size_t trace_get_wire_length(const libtrace_packet_t *packet){ |
|---|
| 1035 | if (packet->trace->format->get_wire_length) { |
|---|
| 1036 | return packet->trace->format->get_wire_length(packet); |
|---|
| 1037 | } |
|---|
| 1038 | return ~0U; |
|---|
| 1039 | |
|---|
| 1040 | } |
|---|
| 1041 | |
|---|
| 1042 | /* Get the length of the capture framing headers. |
|---|
| 1043 | * @param packet the packet opaque pointer |
|---|
| 1044 | * @returns the size of the packet as it was on the wire. |
|---|
| 1045 | * @note this length corresponds to the difference between the size of a |
|---|
| 1046 | * captured packet in memory, and the captured length of the packet |
|---|
| 1047 | */ |
|---|
| 1048 | DLLEXPORT SIMPLE_FUNCTION |
|---|
| 1049 | size_t trace_get_framing_length(const libtrace_packet_t *packet) { |
|---|
| 1050 | if (packet->trace->format->get_framing_length) { |
|---|
| 1051 | return packet->trace->format->get_framing_length(packet); |
|---|
| 1052 | } |
|---|
| 1053 | return ~0U; |
|---|
| 1054 | } |
|---|
| 1055 | |
|---|
| 1056 | |
|---|
| 1057 | /* Get the type of the link layer |
|---|
| 1058 | * @param packet a pointer to a libtrace_packet structure |
|---|
| 1059 | * @returns libtrace_linktype_t |
|---|
| 1060 | */ |
|---|
| 1061 | DLLEXPORT libtrace_linktype_t trace_get_link_type(const libtrace_packet_t *packet ) { |
|---|
| 1062 | if (packet->trace->format->get_link_type) { |
|---|
| 1063 | return packet->trace->format->get_link_type(packet); |
|---|
| 1064 | } |
|---|
| 1065 | return (libtrace_linktype_t)-1; |
|---|
| 1066 | } |
|---|
| 1067 | |
|---|
| 1068 | /* process a libtrace event |
|---|
| 1069 | * @param trace the libtrace opaque pointer |
|---|
| 1070 | * @param packet the libtrace_packet opaque pointer |
|---|
| 1071 | * @returns |
|---|
| 1072 | * TRACE_EVENT_IOWAIT Waiting on I/O on fd |
|---|
| 1073 | * TRACE_EVENT_SLEEP Next event in seconds |
|---|
| 1074 | * TRACE_EVENT_PACKET Packet arrived in buffer with size size |
|---|
| 1075 | * TRACE_EVENT_TERMINATE Trace terminated (perhaps with an error condition) |
|---|
| 1076 | * FIXME currently keeps a copy of the packet inside the trace pointer, |
|---|
| 1077 | * which in turn is stored inside the new packet object... |
|---|
| 1078 | */ |
|---|
| 1079 | DLLEXPORT libtrace_eventobj_t trace_event(libtrace_t *trace, |
|---|
| 1080 | libtrace_packet_t *packet) { |
|---|
| 1081 | libtrace_eventobj_t event = {TRACE_EVENT_IOWAIT,0,0.0,0}; |
|---|
| 1082 | |
|---|
| 1083 | if (!trace) { |
|---|
| 1084 | fprintf(stderr,"You called trace_event() with a NULL trace object!\n"); |
|---|
| 1085 | } |
|---|
| 1086 | assert(trace); |
|---|
| 1087 | assert(packet); |
|---|
| 1088 | |
|---|
| 1089 | /* Clear the packet cache */ |
|---|
| 1090 | packet->capture_length = -1; |
|---|
| 1091 | packet->l3_header = NULL; |
|---|
| 1092 | packet->l3_ethertype = 0; |
|---|
| 1093 | |
|---|
| 1094 | /* Store the trace we are reading from into the packet opaque |
|---|
| 1095 | * structure */ |
|---|
| 1096 | packet->trace = trace; |
|---|
| 1097 | |
|---|
| 1098 | if (packet->trace->format->trace_event) { |
|---|
| 1099 | event=packet->trace->format->trace_event(trace,packet); |
|---|
| 1100 | if (event.type == TRACE_EVENT_PACKET) { |
|---|
| 1101 | ++trace->accepted_packets; |
|---|
| 1102 | } |
|---|
| 1103 | } |
|---|
| 1104 | return event; |
|---|
| 1105 | |
|---|
| 1106 | } |
|---|
| 1107 | |
|---|
| 1108 | /** Setup a BPF filter based on pre-compiled byte-code. |
|---|
| 1109 | * @param bf_insns A pointer to the start of the byte-code |
|---|
| 1110 | * @param bf_len The number of BPF instructions |
|---|
| 1111 | * @returns an opaque pointer to a libtrace_filter_t object |
|---|
| 1112 | * @note The supplied byte-code is not checked for correctness. |
|---|
| 1113 | * @author Scott Raynel |
|---|
| 1114 | */ |
|---|
| 1115 | DLLEXPORT libtrace_filter_t * |
|---|
| 1116 | trace_create_filter_from_bytecode(void *bf_insns, unsigned int bf_len) |
|---|
| 1117 | { |
|---|
| 1118 | #ifndef HAVE_BPF_FILTER |
|---|
| 1119 | fprintf(stderr, "This version of libtrace does not have BPF support\n"); |
|---|
| 1120 | return NULL; |
|---|
| 1121 | #else |
|---|
| 1122 | struct libtrace_filter_t *filter = (struct libtrace_filter_t *) |
|---|
| 1123 | malloc(sizeof(struct libtrace_filter_t)); |
|---|
| 1124 | filter->filter.bf_insns = (struct bpf_insn *) |
|---|
| 1125 | malloc(sizeof(struct bpf_insn) * bf_len); |
|---|
| 1126 | |
|---|
| 1127 | memcpy(filter->filter.bf_insns, bf_insns, |
|---|
| 1128 | bf_len * sizeof(struct bpf_insn)); |
|---|
| 1129 | |
|---|
| 1130 | filter->filter.bf_len = bf_len; |
|---|
| 1131 | filter->filterstring = NULL; |
|---|
| 1132 | filter->jitfilter = NULL; |
|---|
| 1133 | /* "flag" indicates that the filter member is valid */ |
|---|
| 1134 | filter->flag = 1; |
|---|
| 1135 | |
|---|
| 1136 | return filter; |
|---|
| 1137 | #endif |
|---|
| 1138 | } |
|---|
| 1139 | |
|---|
| 1140 | /* Create a BPF filter |
|---|
| 1141 | * @param filterstring a char * containing the bpf filter string |
|---|
| 1142 | * @returns opaque pointer pointer to a libtrace_filter_t object |
|---|
| 1143 | */ |
|---|
| 1144 | DLLEXPORT libtrace_filter_t *trace_create_filter(const char *filterstring) { |
|---|
| 1145 | #ifdef HAVE_BPF_FILTER |
|---|
| 1146 | libtrace_filter_t *filter = (libtrace_filter_t*) |
|---|
| 1147 | malloc(sizeof(libtrace_filter_t)); |
|---|
| 1148 | filter->filterstring = strdup(filterstring); |
|---|
| 1149 | filter->jitfilter = NULL; |
|---|
| 1150 | filter->flag = 0; |
|---|
| 1151 | return filter; |
|---|
| 1152 | #else |
|---|
| 1153 | fprintf(stderr,"This version of libtrace does not have bpf filter support\n"); |
|---|
| 1154 | return NULL; |
|---|
| 1155 | #endif |
|---|
| 1156 | } |
|---|
| 1157 | |
|---|
| 1158 | DLLEXPORT void trace_destroy_filter(libtrace_filter_t *filter) |
|---|
| 1159 | { |
|---|
| 1160 | #ifdef HAVE_BPF_FILTER |
|---|
| 1161 | free(filter->filterstring); |
|---|
| 1162 | if (filter->flag) |
|---|
| 1163 | pcap_freecode(&filter->filter); |
|---|
| 1164 | #ifdef HAVE_LLVM |
|---|
| 1165 | if (filter->jitfilter) |
|---|
| 1166 | destroy_program(filter->jitfilter); |
|---|
| 1167 | #endif |
|---|
| 1168 | free(filter); |
|---|
| 1169 | #else |
|---|
| 1170 | |
|---|
| 1171 | #endif |
|---|
| 1172 | } |
|---|
| 1173 | |
|---|
| 1174 | /* Compile a bpf filter, now we know the link type for the trace that we're |
|---|
| 1175 | * applying it to. |
|---|
| 1176 | * |
|---|
| 1177 | * @internal |
|---|
| 1178 | * |
|---|
| 1179 | * @returns -1 on error, 0 on success |
|---|
| 1180 | */ |
|---|
| 1181 | static int trace_bpf_compile(libtrace_filter_t *filter, |
|---|
| 1182 | const libtrace_packet_t *packet ) { |
|---|
| 1183 | #ifdef HAVE_BPF_FILTER |
|---|
| 1184 | void *linkptr = 0; |
|---|
| 1185 | libtrace_linktype_t linktype; |
|---|
| 1186 | assert(filter); |
|---|
| 1187 | |
|---|
| 1188 | /* If this isn't a real packet, then fail */ |
|---|
| 1189 | linkptr = trace_get_packet_buffer(packet,&linktype,NULL); |
|---|
| 1190 | if (!linkptr) { |
|---|
| 1191 | trace_set_err(packet->trace, |
|---|
| 1192 | TRACE_ERR_BAD_PACKET,"Packet has no payload"); |
|---|
| 1193 | return -1; |
|---|
| 1194 | } |
|---|
| 1195 | |
|---|
| 1196 | if (filter->filterstring && ! filter->flag) { |
|---|
| 1197 | pcap_t *pcap = NULL; |
|---|
| 1198 | if (linktype==(libtrace_linktype_t)-1) { |
|---|
| 1199 | trace_set_err(packet->trace, |
|---|
| 1200 | TRACE_ERR_BAD_PACKET, |
|---|
| 1201 | "Packet has an unknown linktype"); |
|---|
| 1202 | return -1; |
|---|
| 1203 | } |
|---|
| 1204 | if (libtrace_to_pcap_dlt(linktype) == ~1U) { |
|---|
| 1205 | trace_set_err(packet->trace,TRACE_ERR_BAD_PACKET, |
|---|
| 1206 | "Unknown pcap equivalent linktype"); |
|---|
| 1207 | return -1; |
|---|
| 1208 | } |
|---|
| 1209 | pcap=(pcap_t *)pcap_open_dead( |
|---|
| 1210 | (int)libtrace_to_pcap_dlt(linktype), |
|---|
| 1211 | 1500U); |
|---|
| 1212 | /* build filter */ |
|---|
| 1213 | assert(pcap); |
|---|
| 1214 | if (pcap_compile( pcap, &filter->filter, filter->filterstring, |
|---|
| 1215 | 1, 0)) { |
|---|
| 1216 | trace_set_err(packet->trace,TRACE_ERR_BAD_PACKET, |
|---|
| 1217 | "Unable to compile the filter \"%s\": %s", |
|---|
| 1218 | filter->filterstring, |
|---|
| 1219 | pcap_geterr(pcap)); |
|---|
| 1220 | pcap_close(pcap); |
|---|
| 1221 | return -1; |
|---|
| 1222 | } |
|---|
| 1223 | pcap_close(pcap); |
|---|
| 1224 | filter->flag=1; |
|---|
| 1225 | } |
|---|
| 1226 | return 0; |
|---|
| 1227 | #else |
|---|
| 1228 | assert(!"Internal bug: This should never be called when BPF not enabled"); |
|---|
| 1229 | trace_set_err(packet->trace,TRACE_ERR_OPTION_UNAVAIL, |
|---|
| 1230 | "Feature unavailable"); |
|---|
| 1231 | return -1; |
|---|
| 1232 | #endif |
|---|
| 1233 | } |
|---|
| 1234 | |
|---|
| 1235 | DLLEXPORT int trace_apply_filter(libtrace_filter_t *filter, |
|---|
| 1236 | const libtrace_packet_t *packet) { |
|---|
| 1237 | #ifdef HAVE_BPF_FILTER |
|---|
| 1238 | void *linkptr = 0; |
|---|
| 1239 | uint32_t clen = 0; |
|---|
| 1240 | bool free_packet_needed = false; |
|---|
| 1241 | int ret; |
|---|
| 1242 | libtrace_packet_t *packet_copy = (libtrace_packet_t*)packet; |
|---|
| 1243 | |
|---|
| 1244 | assert(filter); |
|---|
| 1245 | assert(packet); |
|---|
| 1246 | |
|---|
| 1247 | if (libtrace_to_pcap_dlt(trace_get_link_type(packet))==~0U) { |
|---|
| 1248 | |
|---|
| 1249 | /* If we cannot get a suitable DLT for the packet, it may |
|---|
| 1250 | * be because the packet is encapsulated in a link type that |
|---|
| 1251 | * does not correspond to a DLT. Therefore, we should try |
|---|
| 1252 | * popping off headers until we either can find a suitable |
|---|
| 1253 | * link type or we can't do any more sensible decapsulation. */ |
|---|
| 1254 | |
|---|
| 1255 | /* Copy the packet, as we don't want to trash the one we |
|---|
| 1256 | * were passed in */ |
|---|
| 1257 | packet_copy=trace_copy_packet(packet); |
|---|
| 1258 | free_packet_needed=true; |
|---|
| 1259 | |
|---|
| 1260 | while (libtrace_to_pcap_dlt(trace_get_link_type(packet_copy))== |
|---|
| 1261 | ~0U) { |
|---|
| 1262 | if (!demote_packet(packet_copy)) { |
|---|
| 1263 | trace_set_err(packet->trace, |
|---|
| 1264 | TRACE_ERR_NO_CONVERSION, |
|---|
| 1265 | "pcap does not support this format"); |
|---|
| 1266 | if (free_packet_needed) { |
|---|
| 1267 | trace_destroy_packet(packet_copy); |
|---|
| 1268 | } |
|---|
| 1269 | return -1; |
|---|
| 1270 | } |
|---|
| 1271 | } |
|---|
| 1272 | } |
|---|
| 1273 | |
|---|
| 1274 | linkptr = trace_get_packet_buffer(packet_copy,NULL,&clen); |
|---|
| 1275 | if (!linkptr) { |
|---|
| 1276 | if (free_packet_needed) { |
|---|
| 1277 | trace_destroy_packet(packet_copy); |
|---|
| 1278 | } |
|---|
| 1279 | return 0; |
|---|
| 1280 | } |
|---|
| 1281 | |
|---|
| 1282 | /* We need to compile the filter now, because before we didn't know |
|---|
| 1283 | * what the link type was |
|---|
| 1284 | */ |
|---|
| 1285 | if (trace_bpf_compile(filter,packet_copy)==-1) { |
|---|
| 1286 | if (free_packet_needed) { |
|---|
| 1287 | trace_destroy_packet(packet_copy); |
|---|
| 1288 | } |
|---|
| 1289 | return -1; |
|---|
| 1290 | } |
|---|
| 1291 | |
|---|
| 1292 | /* If we're jitting, we may need to JIT the BPF code now too */ |
|---|
| 1293 | #if HAVE_LLVM |
|---|
| 1294 | if (!filter->jitfilter) { |
|---|
| 1295 | filter->jitfilter = compile_program(filter->filter.bf_insns, filter->filter.bf_len); |
|---|
| 1296 | } |
|---|
| 1297 | #endif |
|---|
| 1298 | |
|---|
| 1299 | assert(filter->flag); |
|---|
| 1300 | /* Now execute the filter */ |
|---|
| 1301 | #if HAVE_LLVM |
|---|
| 1302 | ret=filter->jitfilter->bpf_run((unsigned char *)linkptr, clen); |
|---|
| 1303 | #else |
|---|
| 1304 | ret=bpf_filter(filter->filter.bf_insns,(u_char*)linkptr,(unsigned int)clen,(unsigned int)clen); |
|---|
| 1305 | #endif |
|---|
| 1306 | |
|---|
| 1307 | /* If we copied the packet earlier, make sure that we free it */ |
|---|
| 1308 | if (free_packet_needed) { |
|---|
| 1309 | trace_destroy_packet(packet_copy); |
|---|
| 1310 | } |
|---|
| 1311 | return ret; |
|---|
| 1312 | #else |
|---|
| 1313 | fprintf(stderr,"This version of libtrace does not have bpf filter support\n"); |
|---|
| 1314 | return 0; |
|---|
| 1315 | #endif |
|---|
| 1316 | } |
|---|
| 1317 | |
|---|
| 1318 | /* Set the direction flag, if it has one |
|---|
| 1319 | * @param packet the packet opaque pointer |
|---|
| 1320 | * @param direction the new direction (0,1,2,3) |
|---|
| 1321 | * @returns a signed value containing the direction flag, or -1 if this is not supported |
|---|
| 1322 | */ |
|---|
| 1323 | DLLEXPORT libtrace_direction_t trace_set_direction(libtrace_packet_t *packet, |
|---|
| 1324 | libtrace_direction_t direction) |
|---|
| 1325 | { |
|---|
| 1326 | assert(packet); |
|---|
| 1327 | if (packet->trace->format->set_direction) { |
|---|
| 1328 | return packet->trace->format->set_direction(packet,direction); |
|---|
| 1329 | } |
|---|
| 1330 | return (libtrace_direction_t)~0U; |
|---|
| 1331 | } |
|---|
| 1332 | |
|---|
| 1333 | /* Get the direction flag, if it has one |
|---|
| 1334 | * @param packet a pointer to a libtrace_packet structure |
|---|
| 1335 | * @returns a signed value containing the direction flag, or -1 if this is not supported |
|---|
| 1336 | * The direction is defined as 0 for packets originating locally (ie, outbound) |
|---|
| 1337 | * and 1 for packets originating remotely (ie, inbound). |
|---|
| 1338 | * Other values are possible, which might be overloaded to mean special things |
|---|
| 1339 | * for a special trace. |
|---|
| 1340 | */ |
|---|
| 1341 | DLLEXPORT libtrace_direction_t trace_get_direction(const libtrace_packet_t *packet) |
|---|
| 1342 | { |
|---|
| 1343 | assert(packet); |
|---|
| 1344 | if (packet->trace->format->get_direction) { |
|---|
| 1345 | return packet->trace->format->get_direction(packet); |
|---|
| 1346 | } |
|---|
| 1347 | return (libtrace_direction_t)~0U; |
|---|
| 1348 | } |
|---|
| 1349 | |
|---|
| 1350 | #define ROOT_SERVER(x) ((x) < 512) |
|---|
| 1351 | #define ROOT_CLIENT(x) ((512 <= (x)) && ((x) < 1024)) |
|---|
| 1352 | #define NONROOT_SERVER(x) ((x) >= 5000) |
|---|
| 1353 | #define NONROOT_CLIENT(x) ((1024 <= (x)) && ((x) < 5000)) |
|---|
| 1354 | #define DYNAMIC(x) ((49152 < (x)) && ((x) < 65535)) |
|---|
| 1355 | #define SERVER(x) ROOT_SERVER(x) || NONROOT_SERVER(x) |
|---|
| 1356 | #define CLIENT(x) ROOT_CLIENT(x) || NONROOT_CLIENT(x) |
|---|
| 1357 | |
|---|
| 1358 | /* Attempt to deduce the 'server' port |
|---|
| 1359 | * @param protocol the IP protocol (eg, 6 or 17 for TCP or UDP) |
|---|
| 1360 | * @param source the TCP or UDP source port |
|---|
| 1361 | * @param dest the TCP or UDP destination port |
|---|
| 1362 | * @returns a hint as to which port is the server port |
|---|
| 1363 | */ |
|---|
| 1364 | DLLEXPORT int8_t trace_get_server_port(UNUSED uint8_t protocol, |
|---|
| 1365 | uint16_t source, uint16_t dest) |
|---|
| 1366 | { |
|---|
| 1367 | /* |
|---|
| 1368 | * * If the ports are equal, return DEST |
|---|
| 1369 | * * Check for well-known ports in the given protocol |
|---|
| 1370 | * * Root server ports: 0 - 511 |
|---|
| 1371 | * * Root client ports: 512 - 1023 |
|---|
| 1372 | * * non-root client ports: 1024 - 4999 |
|---|
| 1373 | * * non-root server ports: 5000+ |
|---|
| 1374 | * * Check for static ranges: 1024 - 49151 |
|---|
| 1375 | * * Check for dynamic ranges: 49152 - 65535 |
|---|
| 1376 | * * flip a coin. |
|---|
| 1377 | */ |
|---|
| 1378 | |
|---|
| 1379 | /* equal */ |
|---|
| 1380 | if (source == dest) |
|---|
| 1381 | return USE_DEST; |
|---|
| 1382 | |
|---|
| 1383 | /* root server port, 0 - 511 */ |
|---|
| 1384 | if (ROOT_SERVER(source) && ROOT_SERVER(dest)) { |
|---|
| 1385 | if (source < dest) |
|---|
| 1386 | return USE_SOURCE; |
|---|
| 1387 | return USE_DEST; |
|---|
| 1388 | } |
|---|
| 1389 | |
|---|
| 1390 | if (ROOT_SERVER(source) && !ROOT_SERVER(dest)) |
|---|
| 1391 | return USE_SOURCE; |
|---|
| 1392 | if (!ROOT_SERVER(source) && ROOT_SERVER(dest)) |
|---|
| 1393 | return USE_DEST; |
|---|
| 1394 | |
|---|
| 1395 | /* non-root server */ |
|---|
| 1396 | if (NONROOT_SERVER(source) && NONROOT_SERVER(dest)) { |
|---|
| 1397 | if (source < dest) |
|---|
| 1398 | return USE_SOURCE; |
|---|
| 1399 | return USE_DEST; |
|---|
| 1400 | } |
|---|
| 1401 | if (NONROOT_SERVER(source) && !NONROOT_SERVER(dest)) |
|---|
| 1402 | return USE_SOURCE; |
|---|
| 1403 | if (!NONROOT_SERVER(source) && NONROOT_SERVER(dest)) |
|---|
| 1404 | return USE_DEST; |
|---|
| 1405 | |
|---|
| 1406 | /* root client */ |
|---|
| 1407 | if (ROOT_CLIENT(source) && ROOT_CLIENT(dest)) { |
|---|
| 1408 | if (source < dest) |
|---|
| 1409 | return USE_SOURCE; |
|---|
| 1410 | return USE_DEST; |
|---|
| 1411 | } |
|---|
| 1412 | if (ROOT_CLIENT(source) && !ROOT_CLIENT(dest)) { |
|---|
| 1413 | /* prefer root-client over nonroot-client */ |
|---|
| 1414 | if (NONROOT_CLIENT(dest)) |
|---|
| 1415 | return USE_SOURCE; |
|---|
| 1416 | return USE_DEST; |
|---|
| 1417 | } |
|---|
| 1418 | if (!ROOT_CLIENT(source) && ROOT_CLIENT(dest)) { |
|---|
| 1419 | /* prefer root-client over nonroot-client */ |
|---|
| 1420 | if (NONROOT_CLIENT(source)) |
|---|
| 1421 | return USE_DEST; |
|---|
| 1422 | return USE_SOURCE; |
|---|
| 1423 | } |
|---|
| 1424 | |
|---|
| 1425 | /* nonroot client */ |
|---|
| 1426 | if (NONROOT_CLIENT(source) && NONROOT_CLIENT(dest)) { |
|---|
| 1427 | if (source < dest) |
|---|
| 1428 | return USE_SOURCE; |
|---|
| 1429 | return USE_DEST; |
|---|
| 1430 | } |
|---|
| 1431 | if (NONROOT_CLIENT(source) && !NONROOT_CLIENT(dest)) |
|---|
| 1432 | return USE_DEST; |
|---|
| 1433 | if (!NONROOT_CLIENT(source) && NONROOT_CLIENT(dest)) |
|---|
| 1434 | return USE_SOURCE; |
|---|
| 1435 | |
|---|
| 1436 | /* dynamic range */ |
|---|
| 1437 | if (DYNAMIC(source) && DYNAMIC(dest)) { |
|---|
| 1438 | if (source < dest) |
|---|
| 1439 | return USE_SOURCE; |
|---|
| 1440 | return USE_DEST; |
|---|
| 1441 | } |
|---|
| 1442 | if (DYNAMIC(source) && !DYNAMIC(dest)) |
|---|
| 1443 | return USE_DEST; |
|---|
| 1444 | if (!DYNAMIC(source) && DYNAMIC(dest)) |
|---|
| 1445 | return USE_SOURCE; |
|---|
| 1446 | /* |
|---|
| 1447 | if (SERVER(source) && CLIENT(dest)) |
|---|
| 1448 | return USE_SOURCE; |
|---|
| 1449 | |
|---|
| 1450 | if (SERVER(dest) && CLIENT(source)) |
|---|
| 1451 | return USE_DEST; |
|---|
| 1452 | if (ROOT_SERVER(source) && !ROOT_SERVER(dest)) |
|---|
| 1453 | return USE_SOURCE; |
|---|
| 1454 | if (ROOT_SERVER(dest) && !ROOT_SERVER(source)) |
|---|
| 1455 | return USE_DEST; |
|---|
| 1456 | */ |
|---|
| 1457 | /* failing that test... */ |
|---|
| 1458 | if (source < dest) { |
|---|
| 1459 | return USE_SOURCE; |
|---|
| 1460 | } |
|---|
| 1461 | return USE_DEST; |
|---|
| 1462 | |
|---|
| 1463 | } |
|---|
| 1464 | |
|---|
| 1465 | /* Truncate the packet at the suggested length |
|---|
| 1466 | * @param packet the packet opaque pointer |
|---|
| 1467 | * @param size the new length of the packet |
|---|
| 1468 | * @returns the new size of the packet |
|---|
| 1469 | * @note size and the return size refer to the network-level payload of the |
|---|
| 1470 | * packet, and do not include any capture headers. For example, to truncate a |
|---|
| 1471 | * packet after the IP header, set size to sizeof(ethernet_header) + |
|---|
| 1472 | * sizeof(ip_header) |
|---|
| 1473 | * @note If the original network-level payload is smaller than size, then the |
|---|
| 1474 | * original size is returned and the packet is left unchanged. |
|---|
| 1475 | */ |
|---|
| 1476 | DLLEXPORT size_t trace_set_capture_length(libtrace_packet_t *packet, size_t size) { |
|---|
| 1477 | assert(packet); |
|---|
| 1478 | |
|---|
| 1479 | if (packet->trace->format->set_capture_length) { |
|---|
| 1480 | return packet->trace->format->set_capture_length(packet,size); |
|---|
| 1481 | } |
|---|
| 1482 | |
|---|
| 1483 | return ~0U; |
|---|
| 1484 | } |
|---|
| 1485 | |
|---|
| 1486 | /* Splits a URI into two components - the format component which is seen before |
|---|
| 1487 | * the ':', and the uridata which follows the ':'. |
|---|
| 1488 | * |
|---|
| 1489 | * Returns a pointer to the URI data, but updates the format parameter to |
|---|
| 1490 | * point to a copy of the format component. |
|---|
| 1491 | */ |
|---|
| 1492 | |
|---|
| 1493 | DLLEXPORT const char * trace_parse_uri(const char *uri, char **format) { |
|---|
| 1494 | const char *uridata = 0; |
|---|
| 1495 | |
|---|
| 1496 | if((uridata = strchr(uri,':')) == NULL) { |
|---|
| 1497 | /* Badly formed URI - needs a : */ |
|---|
| 1498 | return 0; |
|---|
| 1499 | } |
|---|
| 1500 | |
|---|
| 1501 | if ((unsigned)(uridata - uri) > URI_PROTO_LINE) { |
|---|
| 1502 | /* Badly formed URI - uri type is too long */ |
|---|
| 1503 | return 0; |
|---|
| 1504 | } |
|---|
| 1505 | |
|---|
| 1506 | /* NOTE: this is allocated memory - it should be freed by the caller |
|---|
| 1507 | * once they are done with it */ |
|---|
| 1508 | *format=xstrndup(uri, (size_t)(uridata - uri)); |
|---|
| 1509 | |
|---|
| 1510 | /* Push uridata past the delimiter */ |
|---|
| 1511 | uridata++; |
|---|
| 1512 | |
|---|
| 1513 | return uridata; |
|---|
| 1514 | } |
|---|
| 1515 | |
|---|
| 1516 | enum base_format_t trace_get_format(libtrace_packet_t *packet) |
|---|
| 1517 | { |
|---|
| 1518 | assert(packet); |
|---|
| 1519 | |
|---|
| 1520 | return packet->trace->format->type; |
|---|
| 1521 | } |
|---|
| 1522 | |
|---|
| 1523 | DLLEXPORT libtrace_err_t trace_get_err(libtrace_t *trace) |
|---|
| 1524 | { |
|---|
| 1525 | libtrace_err_t err = trace->err; |
|---|
| 1526 | trace->err.err_num = 0; /* "OK" */ |
|---|
| 1527 | trace->err.problem[0]='\0'; |
|---|
| 1528 | return err; |
|---|
| 1529 | } |
|---|
| 1530 | |
|---|
| 1531 | DLLEXPORT bool trace_is_err(libtrace_t *trace) |
|---|
| 1532 | { |
|---|
| 1533 | return trace->err.err_num != 0; |
|---|
| 1534 | } |
|---|
| 1535 | |
|---|
| 1536 | /* Prints the input error status to standard error and clears the error state */ |
|---|
| 1537 | DLLEXPORT void trace_perror(libtrace_t *trace,const char *msg,...) |
|---|
| 1538 | { |
|---|
| 1539 | char buf[256]; |
|---|
| 1540 | va_list va; |
|---|
| 1541 | va_start(va,msg); |
|---|
| 1542 | vsnprintf(buf,sizeof(buf),msg,va); |
|---|
| 1543 | va_end(va); |
|---|
| 1544 | if(trace->err.err_num) { |
|---|
| 1545 | if (trace->uridata) { |
|---|
| 1546 | fprintf(stderr,"%s(%s): %s\n", |
|---|
| 1547 | buf,trace->uridata,trace->err.problem); |
|---|
| 1548 | } else { |
|---|
| 1549 | fprintf(stderr,"%s: %s\n", buf, trace->err.problem); |
|---|
| 1550 | } |
|---|
| 1551 | } else { |
|---|
| 1552 | if (trace->uridata) { |
|---|
| 1553 | fprintf(stderr,"%s(%s): No error\n",buf,trace->uridata); |
|---|
| 1554 | } else { |
|---|
| 1555 | fprintf(stderr,"%s: No error\n", buf); |
|---|
| 1556 | } |
|---|
| 1557 | } |
|---|
| 1558 | trace->err.err_num = 0; /* "OK" */ |
|---|
| 1559 | trace->err.problem[0]='\0'; |
|---|
| 1560 | } |
|---|
| 1561 | |
|---|
| 1562 | DLLEXPORT libtrace_err_t trace_get_err_output(libtrace_out_t *trace) |
|---|
| 1563 | { |
|---|
| 1564 | libtrace_err_t err = trace->err; |
|---|
| 1565 | trace->err.err_num = TRACE_ERR_NOERROR; /* "OK" */ |
|---|
| 1566 | trace->err.problem[0]='\0'; |
|---|
| 1567 | return err; |
|---|
| 1568 | } |
|---|
| 1569 | |
|---|
| 1570 | DLLEXPORT bool trace_is_err_output(libtrace_out_t *trace) |
|---|
| 1571 | { |
|---|
| 1572 | return trace->err.err_num != 0; |
|---|
| 1573 | } |
|---|
| 1574 | |
|---|
| 1575 | /* Prints the output error status to standard error and clears the error state |
|---|
| 1576 | */ |
|---|
| 1577 | DLLEXPORT void trace_perror_output(libtrace_out_t *trace,const char *msg,...) |
|---|
| 1578 | { |
|---|
| 1579 | char buf[256]; |
|---|
| 1580 | va_list va; |
|---|
| 1581 | va_start(va,msg); |
|---|
| 1582 | vsnprintf(buf,sizeof(buf),msg,va); |
|---|
| 1583 | va_end(va); |
|---|
| 1584 | if(trace->err.err_num) { |
|---|
| 1585 | fprintf(stderr,"%s(%s): %s\n", |
|---|
| 1586 | buf, |
|---|
| 1587 | trace->uridata?trace->uridata:"no uri", |
|---|
| 1588 | trace->err.problem); |
|---|
| 1589 | } else { |
|---|
| 1590 | fprintf(stderr,"%s(%s): No error\n",buf,trace->uridata); |
|---|
| 1591 | } |
|---|
| 1592 | trace->err.err_num = TRACE_ERR_NOERROR; /* "OK" */ |
|---|
| 1593 | trace->err.problem[0]='\0'; |
|---|
| 1594 | } |
|---|
| 1595 | |
|---|
| 1596 | DLLEXPORT int trace_seek_erf_timestamp(libtrace_t *trace, uint64_t ts) |
|---|
| 1597 | { |
|---|
| 1598 | if (trace->format->seek_erf) { |
|---|
| 1599 | return trace->format->seek_erf(trace,ts); |
|---|
| 1600 | } |
|---|
| 1601 | else { |
|---|
| 1602 | if (trace->format->seek_timeval) { |
|---|
| 1603 | struct timeval tv; |
|---|
| 1604 | #if __BYTE_ORDER == __BIG_ENDIAN |
|---|
| 1605 | tv.tv_sec = ts & 0xFFFFFFFF; |
|---|
| 1606 | #elif __BYTE_ORDER == __LITTLE_ENDIAN |
|---|
| 1607 | tv.tv_sec = ts >> 32; |
|---|
| 1608 | #else |
|---|
| 1609 | #error "What on earth are you running this on?" |
|---|
| 1610 | #endif |
|---|
| 1611 | tv.tv_usec = ((ts&0xFFFFFFFF)*1000000)>>32; |
|---|
| 1612 | if (tv.tv_usec >= 1000000) { |
|---|
| 1613 | tv.tv_usec -= 1000000; |
|---|
| 1614 | tv.tv_sec += 1; |
|---|
| 1615 | } |
|---|
| 1616 | return trace->format->seek_timeval(trace,tv); |
|---|
| 1617 | } |
|---|
| 1618 | if (trace->format->seek_seconds) { |
|---|
| 1619 | double seconds = |
|---|
| 1620 | (ts>>32) + ((ts & UINT_MAX)*1.0 / UINT_MAX); |
|---|
| 1621 | return trace->format->seek_seconds(trace,seconds); |
|---|
| 1622 | } |
|---|
| 1623 | trace_set_err(trace, |
|---|
| 1624 | TRACE_ERR_OPTION_UNAVAIL, |
|---|
| 1625 | "Feature unimplemented"); |
|---|
| 1626 | return -1; |
|---|
| 1627 | } |
|---|
| 1628 | } |
|---|
| 1629 | |
|---|
| 1630 | DLLEXPORT int trace_seek_seconds(libtrace_t *trace, double seconds) |
|---|
| 1631 | { |
|---|
| 1632 | if (trace->format->seek_seconds) { |
|---|
| 1633 | return trace->format->seek_seconds(trace,seconds); |
|---|
| 1634 | } |
|---|
| 1635 | else { |
|---|
| 1636 | if (trace->format->seek_timeval) { |
|---|
| 1637 | struct timeval tv; |
|---|
| 1638 | tv.tv_sec = (uint32_t)seconds; |
|---|
| 1639 | tv.tv_usec = (uint32_t)(((seconds - tv.tv_sec) * 1000000)/UINT_MAX); |
|---|
| 1640 | return trace->format->seek_timeval(trace,tv); |
|---|
| 1641 | } |
|---|
| 1642 | if (trace->format->seek_erf) { |
|---|
| 1643 | uint64_t timestamp = |
|---|
| 1644 | ((uint64_t)((uint32_t)seconds) << 32) + \ |
|---|
| 1645 | (uint64_t)(( seconds - (uint32_t)seconds ) * UINT_MAX); |
|---|
| 1646 | return trace->format->seek_erf(trace,timestamp); |
|---|
| 1647 | } |
|---|
| 1648 | trace_set_err(trace, |
|---|
| 1649 | TRACE_ERR_OPTION_UNAVAIL, |
|---|
| 1650 | "Feature unimplemented"); |
|---|
| 1651 | return -1; |
|---|
| 1652 | } |
|---|
| 1653 | } |
|---|
| 1654 | |
|---|
| 1655 | DLLEXPORT int trace_seek_timeval(libtrace_t *trace, struct timeval tv) |
|---|
| 1656 | { |
|---|
| 1657 | if (trace->format->seek_timeval) { |
|---|
| 1658 | return trace->format->seek_timeval(trace,tv); |
|---|
| 1659 | } |
|---|
| 1660 | else { |
|---|
| 1661 | if (trace->format->seek_erf) { |
|---|
| 1662 | uint64_t timestamp = ((((uint64_t)tv.tv_sec) << 32) + \ |
|---|
| 1663 | (((uint64_t)tv.tv_usec * UINT_MAX)/1000000)); |
|---|
| 1664 | return trace->format->seek_erf(trace,timestamp); |
|---|
| 1665 | } |
|---|
| 1666 | if (trace->format->seek_seconds) { |
|---|
| 1667 | double seconds = tv.tv_sec + ((tv.tv_usec * 1.0)/1000000); |
|---|
| 1668 | return trace->format->seek_seconds(trace,seconds); |
|---|
| 1669 | } |
|---|
| 1670 | trace_set_err(trace, |
|---|
| 1671 | TRACE_ERR_OPTION_UNAVAIL, |
|---|
| 1672 | "Feature unimplemented"); |
|---|
| 1673 | return -1; |
|---|
| 1674 | } |
|---|
| 1675 | } |
|---|
| 1676 | |
|---|
| 1677 | /* Converts a binary ethernet MAC address into a printable string */ |
|---|
| 1678 | DLLEXPORT char *trace_ether_ntoa(const uint8_t *addr, char *buf) |
|---|
| 1679 | { |
|---|
| 1680 | static char staticbuf[18]={0,}; |
|---|
| 1681 | if (!buf) |
|---|
| 1682 | buf=staticbuf; |
|---|
| 1683 | snprintf(buf,(size_t)18,"%02x:%02x:%02x:%02x:%02x:%02x", |
|---|
| 1684 | addr[0],addr[1],addr[2], |
|---|
| 1685 | addr[3],addr[4],addr[5]); |
|---|
| 1686 | return buf; |
|---|
| 1687 | } |
|---|
| 1688 | |
|---|
| 1689 | /* Converts a printable ethernet MAC address into a binary format */ |
|---|
| 1690 | DLLEXPORT uint8_t *trace_ether_aton(const char *buf, uint8_t *addr) |
|---|
| 1691 | { |
|---|
| 1692 | uint8_t *buf2 = addr; |
|---|
| 1693 | unsigned int tmp[6]; |
|---|
| 1694 | static uint8_t staticaddr[6]; |
|---|
| 1695 | if (!buf2) |
|---|
| 1696 | buf2=staticaddr; |
|---|
| 1697 | sscanf(buf,"%x:%x:%x:%x:%x:%x", |
|---|
| 1698 | &tmp[0],&tmp[1],&tmp[2], |
|---|
| 1699 | &tmp[3],&tmp[4],&tmp[5]); |
|---|
| 1700 | buf2[0]=tmp[0]; buf2[1]=tmp[1]; buf2[2]=tmp[2]; |
|---|
| 1701 | buf2[3]=tmp[3]; buf2[4]=tmp[4]; buf2[5]=tmp[5]; |
|---|
| 1702 | return buf2; |
|---|
| 1703 | } |
|---|
| 1704 | |
|---|
| 1705 | |
|---|
| 1706 | /* Creates a libtrace packet from scratch using the contents of the provided |
|---|
| 1707 | * buffer as the packet payload. |
|---|
| 1708 | * |
|---|
| 1709 | * Unlike trace_prepare_packet(), the buffer should not contain any capture |
|---|
| 1710 | * format headers; instead this function will add the PCAP header to the |
|---|
| 1711 | * packet record. This also means only PCAP packets can be constructed using |
|---|
| 1712 | * this function. |
|---|
| 1713 | * |
|---|
| 1714 | */ |
|---|
| 1715 | DLLEXPORT |
|---|
| 1716 | void trace_construct_packet(libtrace_packet_t *packet, |
|---|
| 1717 | libtrace_linktype_t linktype, |
|---|
| 1718 | const void *data, |
|---|
| 1719 | uint16_t len) |
|---|
| 1720 | { |
|---|
| 1721 | size_t size; |
|---|
| 1722 | static libtrace_t *deadtrace=NULL; |
|---|
| 1723 | libtrace_pcapfile_pkt_hdr_t hdr; |
|---|
| 1724 | #ifdef WIN32 |
|---|
| 1725 | struct _timeb tstruct; |
|---|
| 1726 | #else |
|---|
| 1727 | struct timeval tv; |
|---|
| 1728 | #endif |
|---|
| 1729 | |
|---|
| 1730 | /* We need a trace to attach the constructed packet to (and it needs |
|---|
| 1731 | * to be PCAP) */ |
|---|
| 1732 | if (NULL == deadtrace) |
|---|
| 1733 | deadtrace=trace_create_dead("pcapfile"); |
|---|
| 1734 | |
|---|
| 1735 | /* Fill in the new PCAP header */ |
|---|
| 1736 | #ifdef WIN32 |
|---|
| 1737 | _ftime(&tstruct); |
|---|
| 1738 | hdr.ts_sec=tstruct.time; |
|---|
| 1739 | hdr.ts_usec=tstruct.millitm * 1000; |
|---|
| 1740 | #else |
|---|
| 1741 | gettimeofday(&tv,NULL); |
|---|
| 1742 | hdr.ts_sec=tv.tv_sec; |
|---|
| 1743 | hdr.ts_usec=tv.tv_usec; |
|---|
| 1744 | #endif |
|---|
| 1745 | |
|---|
| 1746 | hdr.caplen=len; |
|---|
| 1747 | hdr.wirelen=len; |
|---|
| 1748 | |
|---|
| 1749 | /* Now fill in the libtrace packet itself */ |
|---|
| 1750 | packet->trace=deadtrace; |
|---|
| 1751 | size=len+sizeof(hdr); |
|---|
| 1752 | if (packet->buf_control==TRACE_CTRL_PACKET) { |
|---|
| 1753 | packet->buffer=realloc(packet->buffer,size); |
|---|
| 1754 | } |
|---|
| 1755 | else { |
|---|
| 1756 | packet->buffer=malloc(size); |
|---|
| 1757 | } |
|---|
| 1758 | packet->buf_control=TRACE_CTRL_PACKET; |
|---|
| 1759 | packet->header=packet->buffer; |
|---|
| 1760 | packet->payload=(void*)((char*)packet->buffer+sizeof(hdr)); |
|---|
| 1761 | |
|---|
| 1762 | /* Ugh, memcpy - sadly necessary */ |
|---|
| 1763 | memcpy(packet->header,&hdr,sizeof(hdr)); |
|---|
| 1764 | memcpy(packet->payload,data,(size_t)len); |
|---|
| 1765 | packet->type=pcap_linktype_to_rt(libtrace_to_pcap_linktype(linktype)); |
|---|
| 1766 | } |
|---|
| 1767 | |
|---|
| 1768 | |
|---|
| 1769 | uint64_t trace_get_received_packets(libtrace_t *trace) |
|---|
| 1770 | { |
|---|
| 1771 | assert(trace); |
|---|
| 1772 | if (trace->format->get_received_packets) { |
|---|
| 1773 | return trace->format->get_received_packets(trace); |
|---|
| 1774 | } |
|---|
| 1775 | return (uint64_t)-1; |
|---|
| 1776 | } |
|---|
| 1777 | |
|---|
| 1778 | uint64_t trace_get_filtered_packets(libtrace_t *trace) |
|---|
| 1779 | { |
|---|
| 1780 | assert(trace); |
|---|
| 1781 | if (trace->format->get_filtered_packets) { |
|---|
| 1782 | return trace->format->get_filtered_packets(trace)+ |
|---|
| 1783 | trace->filtered_packets; |
|---|
| 1784 | } |
|---|
| 1785 | if (trace->format->get_received_packets |
|---|
| 1786 | && trace->format->get_dropped_packets) { |
|---|
| 1787 | return |
|---|
| 1788 | ((trace_get_received_packets(trace) |
|---|
| 1789 | -trace_get_accepted_packets(trace)) |
|---|
| 1790 | -trace_get_dropped_packets(trace)) |
|---|
| 1791 | +trace->filtered_packets; |
|---|
| 1792 | } |
|---|
| 1793 | return trace->filtered_packets; |
|---|
| 1794 | } |
|---|
| 1795 | |
|---|
| 1796 | uint64_t trace_get_dropped_packets(libtrace_t *trace) |
|---|
| 1797 | { |
|---|
| 1798 | assert(trace); |
|---|
| 1799 | if (trace->format->get_dropped_packets) { |
|---|
| 1800 | return trace->format->get_dropped_packets(trace); |
|---|
| 1801 | } |
|---|
| 1802 | return (uint64_t)-1; |
|---|
| 1803 | } |
|---|
| 1804 | |
|---|
| 1805 | uint64_t trace_get_accepted_packets(libtrace_t *trace) |
|---|
| 1806 | { |
|---|
| 1807 | assert(trace); |
|---|
| 1808 | return trace->accepted_packets; |
|---|
| 1809 | } |
|---|
| 1810 | |
|---|
| 1811 | void register_format(struct libtrace_format_t *f) { |
|---|
| 1812 | assert(f->next==NULL); /* Can't register a format twice */ |
|---|
| 1813 | f->next=formats_list; |
|---|
| 1814 | formats_list=f; |
|---|
| 1815 | |
|---|
| 1816 | /* Now, verify that the format has at least the minimum functionality. |
|---|
| 1817 | * |
|---|
| 1818 | * This #if can be changed to a 1 to output warnings about inconsistent |
|---|
| 1819 | * functions being provided by format modules. This generally is very |
|---|
| 1820 | * noisy, as almost all modules don't implement one or more functions |
|---|
| 1821 | * for various reasons. This is very useful when checking a new |
|---|
| 1822 | * format module is sane. |
|---|
| 1823 | */ |
|---|
| 1824 | #if 0 |
|---|
| 1825 | if (f->init_input) { |
|---|
| 1826 | #define REQUIRE(x) \ |
|---|
| 1827 | if (!f->x) \ |
|---|
| 1828 | fprintf(stderr,"%s: Input format should provide " #x "\n",f->name) |
|---|
| 1829 | REQUIRE(read_packet); |
|---|
| 1830 | REQUIRE(start_input); |
|---|
| 1831 | REQUIRE(fin_input); |
|---|
| 1832 | REQUIRE(get_link_type); |
|---|
| 1833 | REQUIRE(get_capture_length); |
|---|
| 1834 | REQUIRE(get_wire_length); |
|---|
| 1835 | REQUIRE(get_framing_length); |
|---|
| 1836 | REQUIRE(trace_event); |
|---|
| 1837 | if (!f->get_erf_timestamp |
|---|
| 1838 | && !f->get_seconds |
|---|
| 1839 | && !f->get_timeval) { |
|---|
| 1840 | fprintf(stderr,"%s: A trace format capable of input, should provide at least one of\n" |
|---|
| 1841 | "get_erf_timestamp, get_seconds or trace_timeval\n",f->name); |
|---|
| 1842 | } |
|---|
| 1843 | if (f->trace_event!=trace_event_trace) { |
|---|
| 1844 | /* Theres nothing that a trace file could optimise with |
|---|
| 1845 | * config_input |
|---|
| 1846 | */ |
|---|
| 1847 | REQUIRE(pause_input); |
|---|
| 1848 | REQUIRE(config_input); |
|---|
| 1849 | REQUIRE(get_fd); |
|---|
| 1850 | } |
|---|
| 1851 | else { |
|---|
| 1852 | if (f->get_fd) { |
|---|
| 1853 | fprintf(stderr,"%s: Unnecessary get_fd\n", |
|---|
| 1854 | f->name); |
|---|
| 1855 | } |
|---|
| 1856 | } |
|---|
| 1857 | #undef REQUIRE |
|---|
| 1858 | } |
|---|
| 1859 | else { |
|---|
| 1860 | #define REQUIRE(x) \ |
|---|
| 1861 | if (f->x) \ |
|---|
| 1862 | fprintf(stderr,"%s: Non Input format shouldn't need " #x "\n",f->name) |
|---|
| 1863 | REQUIRE(read_packet); |
|---|
| 1864 | REQUIRE(start_input); |
|---|
| 1865 | REQUIRE(pause_input); |
|---|
| 1866 | REQUIRE(fin_input); |
|---|
| 1867 | REQUIRE(get_link_type); |
|---|
| 1868 | REQUIRE(get_capture_length); |
|---|
| 1869 | REQUIRE(get_wire_length); |
|---|
| 1870 | REQUIRE(get_framing_length); |
|---|
| 1871 | REQUIRE(trace_event); |
|---|
| 1872 | REQUIRE(get_seconds); |
|---|
| 1873 | REQUIRE(get_timeval); |
|---|
| 1874 | REQUIRE(get_erf_timestamp); |
|---|
| 1875 | #undef REQUIRE |
|---|
| 1876 | } |
|---|
| 1877 | if (f->init_output) { |
|---|
| 1878 | #define REQUIRE(x) \ |
|---|
| 1879 | if (!f->x) \ |
|---|
| 1880 | fprintf(stderr,"%s: Output format should provide " #x "\n",f->name) |
|---|
| 1881 | REQUIRE(write_packet); |
|---|
| 1882 | REQUIRE(start_output); |
|---|
| 1883 | REQUIRE(config_output); |
|---|
| 1884 | REQUIRE(fin_output); |
|---|
| 1885 | #undef REQUIRE |
|---|
| 1886 | } |
|---|
| 1887 | else { |
|---|
| 1888 | #define REQUIRE(x) \ |
|---|
| 1889 | if (f->x) \ |
|---|
| 1890 | fprintf(stderr,"%s: Non Output format shouldn't need " #x "\n",f->name) |
|---|
| 1891 | REQUIRE(write_packet); |
|---|
| 1892 | REQUIRE(start_output); |
|---|
| 1893 | REQUIRE(config_output); |
|---|
| 1894 | REQUIRE(fin_output); |
|---|
| 1895 | #undef REQUIRE |
|---|
| 1896 | } |
|---|
| 1897 | #endif |
|---|
| 1898 | } |
|---|
| 1899 | |
|---|