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