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