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