| 1 | /* |
|---|
| 2 | * This file is part of libtrace |
|---|
| 3 | * |
|---|
| 4 | * Copyright (c) 2007,2008,2009,2010 The University of Waikato, Hamilton, |
|---|
| 5 | * New Zealand. |
|---|
| 6 | * |
|---|
| 7 | * Authors: Daniel Lawson |
|---|
| 8 | * Perry Lorier |
|---|
| 9 | * Shane Alcock |
|---|
| 10 | * |
|---|
| 11 | * All rights reserved. |
|---|
| 12 | * |
|---|
| 13 | * This code has been developed by the University of Waikato WAND |
|---|
| 14 | * research group. For further information please see http://www.wand.net.nz/ |
|---|
| 15 | * |
|---|
| 16 | * libtrace is free software; you can redistribute it and/or modify |
|---|
| 17 | * it under the terms of the GNU General Public License as published by |
|---|
| 18 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 19 | * (at your option) any later version. |
|---|
| 20 | * |
|---|
| 21 | * libtrace is distributed in the hope that it will be useful, |
|---|
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 24 | * GNU General Public License for more details. |
|---|
| 25 | * |
|---|
| 26 | * You should have received a copy of the GNU General Public License |
|---|
| 27 | * along with libtrace; if not, write to the Free Software |
|---|
| 28 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 29 | * |
|---|
| 30 | * $Id$ |
|---|
| 31 | * |
|---|
| 32 | */ |
|---|
| 33 | |
|---|
| 34 | #include "common.h" |
|---|
| 35 | #include "config.h" |
|---|
| 36 | #include "libtrace.h" |
|---|
| 37 | #include "libtrace_int.h" |
|---|
| 38 | #include "format_helper.h" |
|---|
| 39 | |
|---|
| 40 | #include <sys/stat.h> |
|---|
| 41 | #include <assert.h> |
|---|
| 42 | #include <stdio.h> |
|---|
| 43 | #include <stdlib.h> |
|---|
| 44 | #include <string.h> |
|---|
| 45 | #include <errno.h> |
|---|
| 46 | #include <fcntl.h> |
|---|
| 47 | #include <stdbool.h> |
|---|
| 48 | |
|---|
| 49 | /* This format module implements our own, more efficient, version of the PCAP |
|---|
| 50 | * file format. This should always be used in preference to the "pcap" format |
|---|
| 51 | * provided in format_pcap.c. |
|---|
| 52 | * |
|---|
| 53 | * This is a trace file format and does not implement any live interface |
|---|
| 54 | * capture. This is covered by "pcapint" in format_pcap.c. |
|---|
| 55 | * |
|---|
| 56 | * This format supports both reading and writing, regardless of the version |
|---|
| 57 | * of your PCAP library. |
|---|
| 58 | */ |
|---|
| 59 | |
|---|
| 60 | #define DATA(x) ((struct pcapfile_format_data_t*)((x)->format_data)) |
|---|
| 61 | #define DATAOUT(x) ((struct pcapfile_format_data_out_t*)((x)->format_data)) |
|---|
| 62 | #define IN_OPTIONS DATA(libtrace)->options |
|---|
| 63 | |
|---|
| 64 | typedef struct pcapfile_header_t { |
|---|
| 65 | uint32_t magic_number; /* magic number */ |
|---|
| 66 | uint16_t version_major; /* major version number */ |
|---|
| 67 | uint16_t version_minor; /* minor version number */ |
|---|
| 68 | int32_t thiszone; /* GMT to local correction */ |
|---|
| 69 | uint32_t sigfigs; /* timestamp accuracy */ |
|---|
| 70 | uint32_t snaplen; /* aka "wirelen" */ |
|---|
| 71 | uint32_t network; /* data link type */ |
|---|
| 72 | } pcapfile_header_t; |
|---|
| 73 | |
|---|
| 74 | struct pcapfile_format_data_t { |
|---|
| 75 | struct { |
|---|
| 76 | /* Indicates whether the event API should replicate the pauses |
|---|
| 77 | * between packets */ |
|---|
| 78 | int real_time; |
|---|
| 79 | } options; |
|---|
| 80 | |
|---|
| 81 | /* The PCAP meta-header that should be written at the start of each |
|---|
| 82 | * trace */ |
|---|
| 83 | pcapfile_header_t header; |
|---|
| 84 | /* Indicates whether the input trace is started */ |
|---|
| 85 | bool started; |
|---|
| 86 | }; |
|---|
| 87 | |
|---|
| 88 | struct pcapfile_format_data_out_t { |
|---|
| 89 | iow_t *file; |
|---|
| 90 | int compress_type; |
|---|
| 91 | int level; |
|---|
| 92 | int flag; |
|---|
| 93 | |
|---|
| 94 | }; |
|---|
| 95 | |
|---|
| 96 | static int pcapfile_probe_magic(io_t *io) |
|---|
| 97 | { |
|---|
| 98 | pcapfile_header_t header; |
|---|
| 99 | int len; |
|---|
| 100 | len = wandio_peek(io, &header, sizeof(header)); |
|---|
| 101 | /* Is this long enough? */ |
|---|
| 102 | if (len < (int)sizeof(header)) { |
|---|
| 103 | return 0; |
|---|
| 104 | } |
|---|
| 105 | /* Pcap magic? */ |
|---|
| 106 | if (header.magic_number == 0xa1b2c3d4 || header.magic_number == 0xd4c3b2a1) { |
|---|
| 107 | return 1; |
|---|
| 108 | } |
|---|
| 109 | /* Nope, not pcap */ |
|---|
| 110 | return 0; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | static int pcapfile_init_input(libtrace_t *libtrace) { |
|---|
| 114 | libtrace->format_data = malloc(sizeof(struct pcapfile_format_data_t)); |
|---|
| 115 | |
|---|
| 116 | if (libtrace->format_data == NULL) { |
|---|
| 117 | trace_set_err(libtrace,ENOMEM,"Out of memory"); |
|---|
| 118 | return -1; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | IN_OPTIONS.real_time = 0; |
|---|
| 122 | DATA(libtrace)->started = false; |
|---|
| 123 | return 0; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | static int pcapfile_init_output(libtrace_out_t *libtrace) { |
|---|
| 127 | libtrace->format_data = |
|---|
| 128 | malloc(sizeof(struct pcapfile_format_data_out_t)); |
|---|
| 129 | |
|---|
| 130 | DATAOUT(libtrace)->file=NULL; |
|---|
| 131 | DATAOUT(libtrace)->compress_type=TRACE_OPTION_COMPRESSTYPE_NONE; |
|---|
| 132 | DATAOUT(libtrace)->level=0; |
|---|
| 133 | DATAOUT(libtrace)->flag=O_CREAT|O_WRONLY; |
|---|
| 134 | |
|---|
| 135 | return 0; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | static uint16_t swaps(libtrace_t *libtrace, uint16_t num) |
|---|
| 140 | { |
|---|
| 141 | /* To deal with open_dead traces that might try and use this |
|---|
| 142 | * if we don't have any per trace data, assume host byte order |
|---|
| 143 | */ |
|---|
| 144 | if (!DATA(libtrace)) |
|---|
| 145 | return num; |
|---|
| 146 | |
|---|
| 147 | /* We can use the PCAP magic number to determine the byte order */ |
|---|
| 148 | if (DATA(libtrace)->header.magic_number == 0xd4c3b2a1) |
|---|
| 149 | return byteswap16(num); |
|---|
| 150 | |
|---|
| 151 | return num; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | static uint32_t swapl(libtrace_t *libtrace, uint32_t num) |
|---|
| 155 | { |
|---|
| 156 | /* To deal with open_dead traces that might try and use this |
|---|
| 157 | * if we don't have any per trace data, assume host byte order |
|---|
| 158 | */ |
|---|
| 159 | if (!DATA(libtrace)) |
|---|
| 160 | return num; |
|---|
| 161 | |
|---|
| 162 | /* We can use the PCAP magic number to determine the byte order */ |
|---|
| 163 | if (DATA(libtrace)->header.magic_number == 0xd4c3b2a1) |
|---|
| 164 | return byteswap32(num); |
|---|
| 165 | |
|---|
| 166 | return num; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | static int pcapfile_start_input(libtrace_t *libtrace) |
|---|
| 171 | { |
|---|
| 172 | int err; |
|---|
| 173 | |
|---|
| 174 | if (!libtrace->io) { |
|---|
| 175 | libtrace->io=trace_open_file(libtrace); |
|---|
| 176 | DATA(libtrace)->started=false; |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | if (!DATA(libtrace)->started) { |
|---|
| 180 | |
|---|
| 181 | if (!libtrace->io) |
|---|
| 182 | return -1; |
|---|
| 183 | |
|---|
| 184 | err=wandio_read(libtrace->io, |
|---|
| 185 | &DATA(libtrace)->header, |
|---|
| 186 | sizeof(DATA(libtrace)->header)); |
|---|
| 187 | |
|---|
| 188 | DATA(libtrace)->started = true; |
|---|
| 189 | |
|---|
| 190 | if (err<1) |
|---|
| 191 | return -1; |
|---|
| 192 | |
|---|
| 193 | if (swapl(libtrace,DATA(libtrace)->header.magic_number) != |
|---|
| 194 | 0xa1b2c3d4) { |
|---|
| 195 | trace_set_err(libtrace,TRACE_ERR_INIT_FAILED, |
|---|
| 196 | "Not a pcap tracefile (magic=%08x)\n",swapl(libtrace,DATA(libtrace)->header.magic_number)); |
|---|
| 197 | return -1; /* Not a pcap file */ |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | if (swaps(libtrace,DATA(libtrace)->header.version_major)!=2 |
|---|
| 201 | && swaps(libtrace,DATA(libtrace)->header.version_minor)!=4) { |
|---|
| 202 | trace_set_err(libtrace,TRACE_ERR_INIT_FAILED, |
|---|
| 203 | "Unknown pcap tracefile version %d.%d\n", |
|---|
| 204 | swaps(libtrace, |
|---|
| 205 | DATA(libtrace)->header.version_major), |
|---|
| 206 | swaps(libtrace, |
|---|
| 207 | DATA(libtrace)->header.version_minor)); |
|---|
| 208 | return -1; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | return 0; |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | static int pcapfile_start_output(libtrace_out_t *libtrace UNUSED) |
|---|
| 217 | { |
|---|
| 218 | /* We can't open the output file until we've seen the first packet. |
|---|
| 219 | * This is because we need to know the DLT to set the "network" |
|---|
| 220 | * value in the meta-header */ |
|---|
| 221 | |
|---|
| 222 | return 0; |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | static int pcapfile_config_input(libtrace_t *libtrace, |
|---|
| 226 | trace_option_t option, |
|---|
| 227 | void *data) |
|---|
| 228 | { |
|---|
| 229 | switch(option) { |
|---|
| 230 | case TRACE_OPTION_EVENT_REALTIME: |
|---|
| 231 | IN_OPTIONS.real_time = *(int *)data; |
|---|
| 232 | return 0; |
|---|
| 233 | case TRACE_OPTION_META_FREQ: |
|---|
| 234 | case TRACE_OPTION_SNAPLEN: |
|---|
| 235 | case TRACE_OPTION_PROMISC: |
|---|
| 236 | case TRACE_OPTION_FILTER: |
|---|
| 237 | /* All these are either unsupported or handled |
|---|
| 238 | * by trace_config */ |
|---|
| 239 | break; |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | trace_set_err(libtrace,TRACE_ERR_UNKNOWN_OPTION, |
|---|
| 243 | "Unknown option %i", option); |
|---|
| 244 | return -1; |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | static int pcapfile_fin_input(libtrace_t *libtrace) |
|---|
| 248 | { |
|---|
| 249 | if (libtrace->io) |
|---|
| 250 | wandio_destroy(libtrace->io); |
|---|
| 251 | free(libtrace->format_data); |
|---|
| 252 | return 0; /* success */ |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | static int pcapfile_fin_output(libtrace_out_t *libtrace) |
|---|
| 256 | { |
|---|
| 257 | if (DATAOUT(libtrace)->file) |
|---|
| 258 | wandio_wdestroy(DATAOUT(libtrace)->file); |
|---|
| 259 | free(libtrace->format_data); |
|---|
| 260 | libtrace->format_data=NULL; |
|---|
| 261 | return 0; /* success */ |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | static int pcapfile_config_output(libtrace_out_t *libtrace, |
|---|
| 265 | trace_option_output_t option, |
|---|
| 266 | void *value) |
|---|
| 267 | { |
|---|
| 268 | switch (option) { |
|---|
| 269 | case TRACE_OPTION_OUTPUT_COMPRESS: |
|---|
| 270 | DATAOUT(libtrace)->level = *(int*)value; |
|---|
| 271 | return 0; |
|---|
| 272 | case TRACE_OPTION_OUTPUT_COMPRESSTYPE: |
|---|
| 273 | DATAOUT(libtrace)->compress_type = *(int*)value; |
|---|
| 274 | return 0; |
|---|
| 275 | case TRACE_OPTION_OUTPUT_FILEFLAGS: |
|---|
| 276 | DATAOUT(libtrace)->flag = *(int*)value; |
|---|
| 277 | return 0; |
|---|
| 278 | default: |
|---|
| 279 | /* Unknown option */ |
|---|
| 280 | trace_set_err_out(libtrace,TRACE_ERR_UNKNOWN_OPTION, |
|---|
| 281 | "Unknown option"); |
|---|
| 282 | return -1; |
|---|
| 283 | } |
|---|
| 284 | return -1; |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | static int pcapfile_prepare_packet(libtrace_t *libtrace, |
|---|
| 288 | libtrace_packet_t *packet, void *buffer, |
|---|
| 289 | libtrace_rt_types_t rt_type, uint32_t flags) { |
|---|
| 290 | |
|---|
| 291 | if (packet->buffer != buffer && |
|---|
| 292 | packet->buf_control == TRACE_CTRL_PACKET) { |
|---|
| 293 | free(packet->buffer); |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | if ((flags & TRACE_PREP_OWN_BUFFER) == TRACE_PREP_OWN_BUFFER) { |
|---|
| 297 | packet->buf_control = TRACE_CTRL_PACKET; |
|---|
| 298 | } else |
|---|
| 299 | packet->buf_control = TRACE_CTRL_EXTERNAL; |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | packet->buffer = buffer; |
|---|
| 303 | packet->header = buffer; |
|---|
| 304 | packet->payload = (char*)packet->buffer |
|---|
| 305 | + sizeof(libtrace_pcapfile_pkt_hdr_t); |
|---|
| 306 | packet->type = rt_type; |
|---|
| 307 | |
|---|
| 308 | if (libtrace->format_data == NULL) { |
|---|
| 309 | if (pcapfile_init_input(libtrace)) |
|---|
| 310 | return -1; |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | return 0; |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | static int pcapfile_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) |
|---|
| 317 | { |
|---|
| 318 | int err; |
|---|
| 319 | uint32_t flags = 0; |
|---|
| 320 | size_t bytes_to_read = 0; |
|---|
| 321 | |
|---|
| 322 | assert(libtrace->format_data); |
|---|
| 323 | |
|---|
| 324 | packet->type = pcap_linktype_to_rt(swapl(libtrace, |
|---|
| 325 | DATA(libtrace)->header.network)); |
|---|
| 326 | |
|---|
| 327 | if (!packet->buffer || packet->buf_control == TRACE_CTRL_EXTERNAL) { |
|---|
| 328 | packet->buffer = malloc((size_t)LIBTRACE_PACKET_BUFSIZE); |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | flags |= TRACE_PREP_OWN_BUFFER; |
|---|
| 332 | |
|---|
| 333 | err=wandio_read(libtrace->io, |
|---|
| 334 | packet->buffer, |
|---|
| 335 | sizeof(libtrace_pcapfile_pkt_hdr_t)); |
|---|
| 336 | |
|---|
| 337 | if (err<0) { |
|---|
| 338 | trace_set_err(libtrace,errno,"reading packet"); |
|---|
| 339 | return -1; |
|---|
| 340 | } |
|---|
| 341 | if (err==0) { |
|---|
| 342 | /* EOF */ |
|---|
| 343 | return 0; |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | bytes_to_read = swapl(libtrace,((libtrace_pcapfile_pkt_hdr_t*)packet->buffer)->caplen); |
|---|
| 347 | |
|---|
| 348 | assert(bytes_to_read < LIBTRACE_PACKET_BUFSIZE); |
|---|
| 349 | |
|---|
| 350 | /* If there is no payload to read, do not ask wandio_read to try and |
|---|
| 351 | * read zero bytes - we'll just get back a zero that we will |
|---|
| 352 | * misinterpret as EOF! */ |
|---|
| 353 | if (bytes_to_read == 0) { |
|---|
| 354 | packet->header = packet->buffer; |
|---|
| 355 | return sizeof(libtrace_pcapfile_pkt_hdr_t); |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | err=wandio_read(libtrace->io, |
|---|
| 359 | (char*)packet->buffer+sizeof(libtrace_pcapfile_pkt_hdr_t), |
|---|
| 360 | (size_t)swapl(libtrace,((libtrace_pcapfile_pkt_hdr_t*)packet->buffer)->caplen) |
|---|
| 361 | ); |
|---|
| 362 | |
|---|
| 363 | |
|---|
| 364 | if (err<0) { |
|---|
| 365 | trace_set_err(libtrace,errno,"reading packet"); |
|---|
| 366 | return -1; |
|---|
| 367 | } |
|---|
| 368 | if (err==0) { |
|---|
| 369 | return 0; |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | if (pcapfile_prepare_packet(libtrace, packet, packet->buffer, |
|---|
| 373 | packet->type, flags)) { |
|---|
| 374 | return -1; |
|---|
| 375 | } |
|---|
| 376 | |
|---|
| 377 | return sizeof(libtrace_pcapfile_pkt_hdr_t) + bytes_to_read; |
|---|
| 378 | } |
|---|
| 379 | |
|---|
| 380 | static int pcapfile_write_packet(libtrace_out_t *out, |
|---|
| 381 | libtrace_packet_t *packet) |
|---|
| 382 | { |
|---|
| 383 | struct libtrace_pcapfile_pkt_hdr_t hdr; |
|---|
| 384 | struct timeval tv = trace_get_timeval(packet); |
|---|
| 385 | int numbytes; |
|---|
| 386 | int ret; |
|---|
| 387 | void *ptr; |
|---|
| 388 | uint32_t remaining; |
|---|
| 389 | libtrace_linktype_t linktype; |
|---|
| 390 | |
|---|
| 391 | ptr = trace_get_packet_buffer(packet,&linktype,&remaining); |
|---|
| 392 | |
|---|
| 393 | /* Silently discard RT metadata packets and packets with an |
|---|
| 394 | * unknown linktype. */ |
|---|
| 395 | if (linktype == TRACE_TYPE_METADATA || linktype == ~0U) { |
|---|
| 396 | return 0; |
|---|
| 397 | } |
|---|
| 398 | |
|---|
| 399 | /* If this packet cannot be converted to a pcap linktype then |
|---|
| 400 | * pop off the top header until it can be converted |
|---|
| 401 | */ |
|---|
| 402 | while (libtrace_to_pcap_linktype(linktype)==~0U) { |
|---|
| 403 | if (!demote_packet(packet)) { |
|---|
| 404 | trace_set_err_out(out, |
|---|
| 405 | TRACE_ERR_NO_CONVERSION, |
|---|
| 406 | "pcap does not support this format"); |
|---|
| 407 | return -1; |
|---|
| 408 | } |
|---|
| 409 | |
|---|
| 410 | ptr = trace_get_packet_buffer(packet,&linktype,&remaining); |
|---|
| 411 | } |
|---|
| 412 | |
|---|
| 413 | |
|---|
| 414 | /* Now we know the link type write out a header if we've not done |
|---|
| 415 | * so already |
|---|
| 416 | */ |
|---|
| 417 | if (!DATAOUT(out)->file) { |
|---|
| 418 | struct pcapfile_header_t pcaphdr; |
|---|
| 419 | |
|---|
| 420 | DATAOUT(out)->file=trace_open_file_out(out, |
|---|
| 421 | DATAOUT(out)->compress_type, |
|---|
| 422 | DATAOUT(out)->level, |
|---|
| 423 | DATAOUT(out)->flag); |
|---|
| 424 | if (!DATAOUT(out)->file) |
|---|
| 425 | return -1; |
|---|
| 426 | |
|---|
| 427 | pcaphdr.magic_number = 0xa1b2c3d4; |
|---|
| 428 | pcaphdr.version_major = 2; |
|---|
| 429 | pcaphdr.version_minor = 4; |
|---|
| 430 | pcaphdr.thiszone = 0; |
|---|
| 431 | pcaphdr.sigfigs = 0; |
|---|
| 432 | pcaphdr.snaplen = 65536; |
|---|
| 433 | pcaphdr.network = |
|---|
| 434 | libtrace_to_pcap_linktype(linktype); |
|---|
| 435 | |
|---|
| 436 | wandio_wwrite(DATAOUT(out)->file, |
|---|
| 437 | &pcaphdr, sizeof(pcaphdr)); |
|---|
| 438 | } |
|---|
| 439 | |
|---|
| 440 | hdr.ts_sec = tv.tv_sec; |
|---|
| 441 | hdr.ts_usec = tv.tv_usec; |
|---|
| 442 | hdr.caplen = trace_get_capture_length(packet); |
|---|
| 443 | assert(hdr.caplen < LIBTRACE_PACKET_BUFSIZE); |
|---|
| 444 | /* PCAP doesn't include the FCS in its wire length value, but we do */ |
|---|
| 445 | if (linktype==TRACE_TYPE_ETH) { |
|---|
| 446 | if (trace_get_wire_length(packet) >= 4) { |
|---|
| 447 | hdr.wirelen = trace_get_wire_length(packet)-4; |
|---|
| 448 | |
|---|
| 449 | } |
|---|
| 450 | else { |
|---|
| 451 | hdr.wirelen = 0; |
|---|
| 452 | } |
|---|
| 453 | } |
|---|
| 454 | else |
|---|
| 455 | hdr.wirelen = trace_get_wire_length(packet); |
|---|
| 456 | |
|---|
| 457 | assert(hdr.wirelen < LIBTRACE_PACKET_BUFSIZE); |
|---|
| 458 | |
|---|
| 459 | /* Ensure we have a valid capture length, especially if we're going |
|---|
| 460 | * to "remove" the FCS from the wire length */ |
|---|
| 461 | if (hdr.caplen > hdr.wirelen) |
|---|
| 462 | hdr.caplen = hdr.wirelen; |
|---|
| 463 | |
|---|
| 464 | /* Write the packet header */ |
|---|
| 465 | numbytes=wandio_wwrite(DATAOUT(out)->file, |
|---|
| 466 | &hdr, sizeof(hdr)); |
|---|
| 467 | |
|---|
| 468 | if (numbytes!=sizeof(hdr)) |
|---|
| 469 | return -1; |
|---|
| 470 | |
|---|
| 471 | /* Write the rest of the packet now */ |
|---|
| 472 | ret=wandio_wwrite(DATAOUT(out)->file, |
|---|
| 473 | ptr, |
|---|
| 474 | hdr.caplen); |
|---|
| 475 | |
|---|
| 476 | if (ret!=(int)hdr.caplen) |
|---|
| 477 | return -1; |
|---|
| 478 | |
|---|
| 479 | return numbytes+ret; |
|---|
| 480 | } |
|---|
| 481 | |
|---|
| 482 | static libtrace_linktype_t pcapfile_get_link_type( |
|---|
| 483 | const libtrace_packet_t *packet) |
|---|
| 484 | { |
|---|
| 485 | return pcap_linktype_to_libtrace(rt_to_pcap_linktype(packet->type)); |
|---|
| 486 | } |
|---|
| 487 | |
|---|
| 488 | static libtrace_direction_t pcapfile_get_direction(const libtrace_packet_t *packet) |
|---|
| 489 | { |
|---|
| 490 | libtrace_direction_t direction = -1; |
|---|
| 491 | switch(pcapfile_get_link_type(packet)) { |
|---|
| 492 | /* We can only get the direction for PCAP packets that have |
|---|
| 493 | * been encapsulated in Linux SLL or PFLOG */ |
|---|
| 494 | case TRACE_TYPE_LINUX_SLL: |
|---|
| 495 | { |
|---|
| 496 | libtrace_sll_header_t *sll; |
|---|
| 497 | libtrace_linktype_t linktype; |
|---|
| 498 | |
|---|
| 499 | sll = (libtrace_sll_header_t*)trace_get_packet_buffer( |
|---|
| 500 | packet, |
|---|
| 501 | &linktype, |
|---|
| 502 | NULL); |
|---|
| 503 | if (!sll) { |
|---|
| 504 | trace_set_err(packet->trace, |
|---|
| 505 | TRACE_ERR_BAD_PACKET, |
|---|
| 506 | "Bad or missing packet"); |
|---|
| 507 | return -1; |
|---|
| 508 | } |
|---|
| 509 | /* 0 == LINUX_SLL_HOST */ |
|---|
| 510 | /* the Waikato Capture point defines "packets |
|---|
| 511 | * originating locally" (ie, outbound), with a |
|---|
| 512 | * direction of 0, and "packets destined locally" |
|---|
| 513 | * (ie, inbound), with a direction of 1. |
|---|
| 514 | * This is kind-of-opposite to LINUX_SLL. |
|---|
| 515 | * We return consistent values here, however |
|---|
| 516 | * |
|---|
| 517 | * Note that in recent versions of pcap, you can |
|---|
| 518 | * use "inbound" and "outbound" on ppp in linux |
|---|
| 519 | */ |
|---|
| 520 | if (ntohs(sll->pkttype == 0)) { |
|---|
| 521 | direction = TRACE_DIR_INCOMING; |
|---|
| 522 | } else { |
|---|
| 523 | direction = TRACE_DIR_OUTGOING; |
|---|
| 524 | } |
|---|
| 525 | break; |
|---|
| 526 | |
|---|
| 527 | } |
|---|
| 528 | case TRACE_TYPE_PFLOG: |
|---|
| 529 | { |
|---|
| 530 | libtrace_pflog_header_t *pflog; |
|---|
| 531 | libtrace_linktype_t linktype; |
|---|
| 532 | |
|---|
| 533 | pflog=(libtrace_pflog_header_t*)trace_get_packet_buffer( |
|---|
| 534 | packet,&linktype,NULL); |
|---|
| 535 | if (!pflog) { |
|---|
| 536 | trace_set_err(packet->trace, |
|---|
| 537 | TRACE_ERR_BAD_PACKET, |
|---|
| 538 | "Bad or missing packet"); |
|---|
| 539 | return -1; |
|---|
| 540 | } |
|---|
| 541 | /* enum { PF_IN=0, PF_OUT=1 }; */ |
|---|
| 542 | if (ntohs(pflog->dir==0)) { |
|---|
| 543 | |
|---|
| 544 | direction = TRACE_DIR_INCOMING; |
|---|
| 545 | } |
|---|
| 546 | else { |
|---|
| 547 | direction = TRACE_DIR_OUTGOING; |
|---|
| 548 | } |
|---|
| 549 | break; |
|---|
| 550 | } |
|---|
| 551 | default: |
|---|
| 552 | break; |
|---|
| 553 | } |
|---|
| 554 | return direction; |
|---|
| 555 | } |
|---|
| 556 | |
|---|
| 557 | |
|---|
| 558 | static struct timeval pcapfile_get_timeval( |
|---|
| 559 | const libtrace_packet_t *packet) |
|---|
| 560 | { |
|---|
| 561 | libtrace_pcapfile_pkt_hdr_t *hdr; |
|---|
| 562 | struct timeval ts; |
|---|
| 563 | |
|---|
| 564 | assert(packet->header); |
|---|
| 565 | |
|---|
| 566 | hdr = (libtrace_pcapfile_pkt_hdr_t*)packet->header; |
|---|
| 567 | ts.tv_sec = swapl(packet->trace,hdr->ts_sec); |
|---|
| 568 | ts.tv_usec = swapl(packet->trace,hdr->ts_usec); |
|---|
| 569 | return ts; |
|---|
| 570 | } |
|---|
| 571 | |
|---|
| 572 | |
|---|
| 573 | static int pcapfile_get_capture_length(const libtrace_packet_t *packet) { |
|---|
| 574 | libtrace_pcapfile_pkt_hdr_t *pcapptr; |
|---|
| 575 | |
|---|
| 576 | assert(packet->header); |
|---|
| 577 | pcapptr = (libtrace_pcapfile_pkt_hdr_t *)packet->header; |
|---|
| 578 | |
|---|
| 579 | return swapl(packet->trace,pcapptr->caplen); |
|---|
| 580 | } |
|---|
| 581 | |
|---|
| 582 | static int pcapfile_get_wire_length(const libtrace_packet_t *packet) { |
|---|
| 583 | libtrace_pcapfile_pkt_hdr_t *pcapptr; |
|---|
| 584 | |
|---|
| 585 | assert(packet->header); |
|---|
| 586 | |
|---|
| 587 | pcapptr = (libtrace_pcapfile_pkt_hdr_t *)packet->header; |
|---|
| 588 | if (packet->type==pcap_linktype_to_rt(TRACE_DLT_EN10MB)) |
|---|
| 589 | /* Include the missing FCS */ |
|---|
| 590 | return swapl(packet->trace,pcapptr->wirelen)+4; |
|---|
| 591 | else if (packet->type==pcap_linktype_to_rt(TRACE_DLT_IEEE802_11_RADIO)){ |
|---|
| 592 | /* If the packet is Radiotap and the flags field indicates |
|---|
| 593 | * that the FCS is not included in the 802.11 frame, then |
|---|
| 594 | * we need to add 4 to the wire-length to account for it. |
|---|
| 595 | */ |
|---|
| 596 | uint8_t flags; |
|---|
| 597 | void *link; |
|---|
| 598 | libtrace_linktype_t linktype; |
|---|
| 599 | link = trace_get_packet_buffer(packet, &linktype, NULL); |
|---|
| 600 | trace_get_wireless_flags(link, linktype, &flags); |
|---|
| 601 | if ((flags & TRACE_RADIOTAP_F_FCS) == 0) |
|---|
| 602 | return swapl(packet->trace,pcapptr->wirelen)+4; |
|---|
| 603 | } |
|---|
| 604 | return swapl(packet->trace,pcapptr->wirelen); |
|---|
| 605 | } |
|---|
| 606 | |
|---|
| 607 | static int pcapfile_get_framing_length(const libtrace_packet_t *packet UNUSED) { |
|---|
| 608 | return sizeof(libtrace_pcapfile_pkt_hdr_t); |
|---|
| 609 | } |
|---|
| 610 | |
|---|
| 611 | static size_t pcapfile_set_capture_length(libtrace_packet_t *packet,size_t size) { |
|---|
| 612 | libtrace_pcapfile_pkt_hdr_t *pcapptr = 0; |
|---|
| 613 | assert(packet); |
|---|
| 614 | assert(packet->header); |
|---|
| 615 | if (size > trace_get_capture_length(packet)) { |
|---|
| 616 | /* Can't make a packet larger */ |
|---|
| 617 | return trace_get_capture_length(packet); |
|---|
| 618 | } |
|---|
| 619 | /* Reset the cached capture length */ |
|---|
| 620 | packet->capture_length = -1; |
|---|
| 621 | pcapptr = (libtrace_pcapfile_pkt_hdr_t *)packet->header; |
|---|
| 622 | pcapptr->caplen = swapl(packet->trace,(uint32_t)size); |
|---|
| 623 | return trace_get_capture_length(packet); |
|---|
| 624 | } |
|---|
| 625 | |
|---|
| 626 | static struct libtrace_eventobj_t pcapfile_event(libtrace_t *libtrace, libtrace_packet_t *packet) { |
|---|
| 627 | |
|---|
| 628 | libtrace_eventobj_t event = {0,0,0.0,0}; |
|---|
| 629 | |
|---|
| 630 | /* If we are being told to replay packets as fast as possible, then |
|---|
| 631 | * we just need to read and return the next packet in the trace */ |
|---|
| 632 | |
|---|
| 633 | if (IN_OPTIONS.real_time) { |
|---|
| 634 | event.size = pcapfile_read_packet(libtrace, packet); |
|---|
| 635 | if (event.size < 1) |
|---|
| 636 | event.type = TRACE_EVENT_TERMINATE; |
|---|
| 637 | else |
|---|
| 638 | event.type = TRACE_EVENT_PACKET; |
|---|
| 639 | return event; |
|---|
| 640 | } else { |
|---|
| 641 | return trace_event_trace(libtrace, packet); |
|---|
| 642 | } |
|---|
| 643 | } |
|---|
| 644 | |
|---|
| 645 | static void pcapfile_help(void) { |
|---|
| 646 | printf("pcapfile format module: $Revision$\n"); |
|---|
| 647 | printf("Supported input URIs:\n"); |
|---|
| 648 | printf("\tpcapfile:/path/to/file\n"); |
|---|
| 649 | printf("\tpcapfile:/path/to/file.gz\n"); |
|---|
| 650 | printf("\n"); |
|---|
| 651 | printf("\te.g.: pcapfile:/tmp/trace.pcap\n"); |
|---|
| 652 | printf("\n"); |
|---|
| 653 | } |
|---|
| 654 | |
|---|
| 655 | static struct libtrace_format_t pcapfile = { |
|---|
| 656 | "pcapfile", |
|---|
| 657 | "$Id$", |
|---|
| 658 | TRACE_FORMAT_PCAPFILE, |
|---|
| 659 | NULL, /* probe filename */ |
|---|
| 660 | pcapfile_probe_magic, /* probe magic */ |
|---|
| 661 | pcapfile_init_input, /* init_input */ |
|---|
| 662 | pcapfile_config_input, /* config_input */ |
|---|
| 663 | pcapfile_start_input, /* start_input */ |
|---|
| 664 | NULL, /* pause_input */ |
|---|
| 665 | pcapfile_init_output, /* init_output */ |
|---|
| 666 | pcapfile_config_output, /* config_output */ |
|---|
| 667 | pcapfile_start_output, /* start_output */ |
|---|
| 668 | pcapfile_fin_input, /* fin_input */ |
|---|
| 669 | pcapfile_fin_output, /* fin_output */ |
|---|
| 670 | pcapfile_read_packet, /* read_packet */ |
|---|
| 671 | pcapfile_prepare_packet, /* prepare_packet */ |
|---|
| 672 | NULL, /* fin_packet */ |
|---|
| 673 | pcapfile_write_packet, /* write_packet */ |
|---|
| 674 | pcapfile_get_link_type, /* get_link_type */ |
|---|
| 675 | pcapfile_get_direction, /* get_direction */ |
|---|
| 676 | NULL, /* set_direction */ |
|---|
| 677 | NULL, /* get_erf_timestamp */ |
|---|
| 678 | pcapfile_get_timeval, /* get_timeval */ |
|---|
| 679 | NULL, /* get_timespec */ |
|---|
| 680 | NULL, /* get_seconds */ |
|---|
| 681 | NULL, /* seek_erf */ |
|---|
| 682 | NULL, /* seek_timeval */ |
|---|
| 683 | NULL, /* seek_seconds */ |
|---|
| 684 | pcapfile_get_capture_length, /* get_capture_length */ |
|---|
| 685 | pcapfile_get_wire_length, /* get_wire_length */ |
|---|
| 686 | pcapfile_get_framing_length, /* get_framing_length */ |
|---|
| 687 | pcapfile_set_capture_length, /* set_capture_length */ |
|---|
| 688 | NULL, /* get_received_packets */ |
|---|
| 689 | NULL, /* get_filtered_packets */ |
|---|
| 690 | NULL, /* get_dropped_packets */ |
|---|
| 691 | NULL, /* get_captured_packets */ |
|---|
| 692 | NULL, /* get_fd */ |
|---|
| 693 | pcapfile_event, /* trace_event */ |
|---|
| 694 | pcapfile_help, /* help */ |
|---|
| 695 | NULL /* next pointer */ |
|---|
| 696 | }; |
|---|
| 697 | |
|---|
| 698 | |
|---|
| 699 | void pcapfile_constructor(void) { |
|---|
| 700 | register_format(&pcapfile); |
|---|
| 701 | } |
|---|
| 702 | |
|---|
| 703 | |
|---|