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