1 /* 2 * Copyright (c) 1993, 1994, 1995, 1996, 1997 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that: (1) source code distributions 7 * retain the above copyright notice and this paragraph in its entirety, (2) 8 * distributions including binary code include the above copyright notice and 9 * this paragraph in its entirety in the documentation or other materials 10 * provided with the distribution, and (3) all advertising materials mentioning 11 * features or use of this software display the following acknowledgement: 12 * ``This product includes software developed by the University of California, 13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 14 * the University nor the names of its contributors may be used to endorse 15 * or promote products derived from this software without specific prior 16 * written permission. 17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 20 * 21 * savefile.c - supports offline use of tcpdump 22 * Extraction/creation by Jeffrey Mogul, DECWRL 23 * Modified by Steve McCanne, LBL. 24 * 25 * Used to save the received packet headers, after filtering, to 26 * a file, and then read them later. 27 * The first record in the file contains saved values for the machine 28 * dependent values so we can print the dump file on any architecture. 29 */ 30 31 #ifndef lint 32 static const char rcsid[] _U_ = 33 "@(#) $Header: /tcpdump/master/libpcap/savefile.c,v 1.126.2.13 2005/08/29 21:05:45 guy Exp $ (LBL)"; 34 #endif 35 36 #ifdef HAVE_CONFIG_H 37 #include "config.h" 38 #endif 39 40 #include <errno.h> 41 #include <memory.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 46 #include "pcap-int.h" 47 48 #ifdef HAVE_OS_PROTO_H 49 #include "os-proto.h" 50 #endif 51 52 /* 53 * Standard libpcap format. 54 */ 55 #define TCPDUMP_MAGIC 0xa1b2c3d4 56 57 /* 58 * Alexey Kuznetzov's modified libpcap format. 59 */ 60 #define KUZNETZOV_TCPDUMP_MAGIC 0xa1b2cd34 61 62 /* 63 * Reserved for Francisco Mesquita <francisco.mesquita@radiomovel.pt> 64 * for another modified format. 65 */ 66 #define FMESQUITA_TCPDUMP_MAGIC 0xa1b234cd 67 68 /* 69 * Navtel Communcations' format, with nanosecond timestamps, 70 * as per a request from Dumas Hwang <dumas.hwang@navtelcom.com>. 71 */ 72 #define NAVTEL_TCPDUMP_MAGIC 0xa12b3c4d 73 74 /* 75 * Normal libpcap format, except for seconds/nanoseconds timestamps, 76 * as per a request by Ulf Lamping <ulf.lamping@web.de> 77 */ 78 #define NSEC_TCPDUMP_MAGIC 0xa1b23c4d 79 80 /* 81 * We use the "receiver-makes-right" approach to byte order, 82 * because time is at a premium when we are writing the file. 83 * In other words, the pcap_file_header and pcap_pkthdr, 84 * records are written in host byte order. 85 * Note that the bytes of packet data are written out in the order in 86 * which they were received, so multi-byte fields in packets are not 87 * written in host byte order, they're written in whatever order the 88 * sending machine put them in. 89 * 90 * ntoh[ls] aren't sufficient because we might need to swap on a big-endian 91 * machine (if the file was written in little-end order). 92 */ 93 #define SWAPLONG(y) \ 94 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff)) 95 #define SWAPSHORT(y) \ 96 ( (((y)&0xff)<<8) | ((u_short)((y)&0xff00)>>8) ) 97 98 #define SFERR_TRUNC 1 99 #define SFERR_BADVERSION 2 100 #define SFERR_BADF 3 101 #define SFERR_EOF 4 /* not really an error, just a status */ 102 103 /* 104 * Setting O_BINARY on DOS/Windows is a bit tricky 105 */ 106 #if defined(WIN32) 107 #define SET_BINMODE(f) _setmode(_fileno(f), _O_BINARY) 108 #elif defined(MSDOS) 109 #if defined(__HIGHC__) 110 #define SET_BINMODE(f) setmode(f, O_BINARY) 111 #else 112 #define SET_BINMODE(f) setmode(fileno(f), O_BINARY) 113 #endif 114 #endif 115 116 /* 117 * We don't write DLT_* values to the capture file header, because 118 * they're not the same on all platforms. 119 * 120 * Unfortunately, the various flavors of BSD have not always used the same 121 * numerical values for the same data types, and various patches to 122 * libpcap for non-BSD OSes have added their own DLT_* codes for link 123 * layer encapsulation types seen on those OSes, and those codes have had, 124 * in some cases, values that were also used, on other platforms, for other 125 * link layer encapsulation types. 126 * 127 * This means that capture files of a type whose numerical DLT_* code 128 * means different things on different BSDs, or with different versions 129 * of libpcap, can't always be read on systems other than those like 130 * the one running on the machine on which the capture was made. 131 * 132 * Instead, we define here a set of LINKTYPE_* codes, and map DLT_* codes 133 * to LINKTYPE_* codes when writing a savefile header, and map LINKTYPE_* 134 * codes to DLT_* codes when reading a savefile header. 135 * 136 * For those DLT_* codes that have, as far as we know, the same values on 137 * all platforms (DLT_NULL through DLT_FDDI), we define LINKTYPE_xxx as 138 * DLT_xxx; that way, captures of those types can still be read by 139 * versions of libpcap that map LINKTYPE_* values to DLT_* values, and 140 * captures of those types written by versions of libpcap that map DLT_ 141 * values to LINKTYPE_ values can still be read by older versions 142 * of libpcap. 143 * 144 * The other LINKTYPE_* codes are given values starting at 100, in the 145 * hopes that no DLT_* code will be given one of those values. 146 * 147 * In order to ensure that a given LINKTYPE_* code's value will refer to 148 * the same encapsulation type on all platforms, you should not allocate 149 * a new LINKTYPE_* value without consulting "tcpdump-workers@tcpdump.org". 150 * The tcpdump developers will allocate a value for you, and will not 151 * subsequently allocate it to anybody else; that value will be added to 152 * the "pcap.h" in the tcpdump.org CVS repository, so that a future 153 * libpcap release will include it. 154 * 155 * You should, if possible, also contribute patches to libpcap and tcpdump 156 * to handle the new encapsulation type, so that they can also be checked 157 * into the tcpdump.org CVS repository and so that they will appear in 158 * future libpcap and tcpdump releases. 159 * 160 * Do *NOT* assume that any values after the largest value in this file 161 * are available; you might not have the most up-to-date version of this 162 * file, and new values after that one might have been assigned. Also, 163 * do *NOT* use any values below 100 - those might already have been 164 * taken by one (or more!) organizations. 165 */ 166 #define LINKTYPE_NULL DLT_NULL 167 #define LINKTYPE_ETHERNET DLT_EN10MB /* also for 100Mb and up */ 168 #define LINKTYPE_EXP_ETHERNET DLT_EN3MB /* 3Mb experimental Ethernet */ 169 #define LINKTYPE_AX25 DLT_AX25 170 #define LINKTYPE_PRONET DLT_PRONET 171 #define LINKTYPE_CHAOS DLT_CHAOS 172 #define LINKTYPE_TOKEN_RING DLT_IEEE802 /* DLT_IEEE802 is used for Token Ring */ 173 #define LINKTYPE_ARCNET DLT_ARCNET /* BSD-style headers */ 174 #define LINKTYPE_SLIP DLT_SLIP 175 #define LINKTYPE_PPP DLT_PPP 176 #define LINKTYPE_FDDI DLT_FDDI 177 178 /* 179 * LINKTYPE_PPP is for use when there might, or might not, be an RFC 1662 180 * PPP in HDLC-like framing header (with 0xff 0x03 before the PPP protocol 181 * field) at the beginning of the packet. 182 * 183 * This is for use when there is always such a header; the address field 184 * might be 0xff, for regular PPP, or it might be an address field for Cisco 185 * point-to-point with HDLC framing as per section 4.3.1 of RFC 1547 ("Cisco 186 * HDLC"). This is, for example, what you get with NetBSD's DLT_PPP_SERIAL. 187 * 188 * We give it the same value as NetBSD's DLT_PPP_SERIAL, in the hopes that 189 * nobody else will choose a DLT_ value of 50, and so that DLT_PPP_SERIAL 190 * captures will be written out with a link type that NetBSD's tcpdump 191 * can read. 192 */ 193 #define LINKTYPE_PPP_HDLC 50 /* PPP in HDLC-like framing */ 194 195 #define LINKTYPE_PPP_ETHER 51 /* NetBSD PPP-over-Ethernet */ 196 197 #define LINKTYPE_SYMANTEC_FIREWALL 99 /* Symantec Enterprise Firewall */ 198 199 #define LINKTYPE_ATM_RFC1483 100 /* LLC/SNAP-encapsulated ATM */ 200 #define LINKTYPE_RAW 101 /* raw IP */ 201 #define LINKTYPE_SLIP_BSDOS 102 /* BSD/OS SLIP BPF header */ 202 #define LINKTYPE_PPP_BSDOS 103 /* BSD/OS PPP BPF header */ 203 #define LINKTYPE_C_HDLC 104 /* Cisco HDLC */ 204 #define LINKTYPE_IEEE802_11 105 /* IEEE 802.11 (wireless) */ 205 #define LINKTYPE_ATM_CLIP 106 /* Linux Classical IP over ATM */ 206 #define LINKTYPE_FRELAY 107 /* Frame Relay */ 207 #define LINKTYPE_LOOP 108 /* OpenBSD loopback */ 208 #define LINKTYPE_ENC 109 /* OpenBSD IPSEC enc */ 209 210 /* 211 * These three types are reserved for future use. 212 */ 213 #define LINKTYPE_LANE8023 110 /* ATM LANE + 802.3 */ 214 #define LINKTYPE_HIPPI 111 /* NetBSD HIPPI */ 215 #define LINKTYPE_HDLC 112 /* NetBSD HDLC framing */ 216 217 #define LINKTYPE_LINUX_SLL 113 /* Linux cooked socket capture */ 218 #define LINKTYPE_LTALK 114 /* Apple LocalTalk hardware */ 219 #define LINKTYPE_ECONET 115 /* Acorn Econet */ 220 221 /* 222 * Reserved for use with OpenBSD ipfilter. 223 */ 224 #define LINKTYPE_IPFILTER 116 225 226 #define LINKTYPE_PFLOG 117 /* OpenBSD DLT_PFLOG */ 227 #define LINKTYPE_CISCO_IOS 118 /* For Cisco-internal use */ 228 #define LINKTYPE_PRISM_HEADER 119 /* 802.11+Prism II monitor mode */ 229 #define LINKTYPE_AIRONET_HEADER 120 /* FreeBSD Aironet driver stuff */ 230 231 /* 232 * Reserved for Siemens HiPath HDLC. 233 */ 234 #define LINKTYPE_HHDLC 121 235 236 #define LINKTYPE_IP_OVER_FC 122 /* RFC 2625 IP-over-Fibre Channel */ 237 #define LINKTYPE_SUNATM 123 /* Solaris+SunATM */ 238 239 /* 240 * Reserved as per request from Kent Dahlgren <kent@praesum.com> 241 * for private use. 242 */ 243 #define LINKTYPE_RIO 124 /* RapidIO */ 244 #define LINKTYPE_PCI_EXP 125 /* PCI Express */ 245 #define LINKTYPE_AURORA 126 /* Xilinx Aurora link layer */ 246 247 #define LINKTYPE_IEEE802_11_RADIO 127 /* 802.11 plus BSD radio header */ 248 249 /* 250 * Reserved for the TZSP encapsulation, as per request from 251 * Chris Waters <chris.waters@networkchemistry.com> 252 * TZSP is a generic encapsulation for any other link type, 253 * which includes a means to include meta-information 254 * with the packet, e.g. signal strength and channel 255 * for 802.11 packets. 256 */ 257 #define LINKTYPE_TZSP 128 /* Tazmen Sniffer Protocol */ 258 259 #define LINKTYPE_ARCNET_LINUX 129 /* Linux-style headers */ 260 261 /* 262 * Juniper-private data link types, as per request from 263 * Hannes Gredler <hannes@juniper.net>. The corresponding 264 * DLT_s are used for passing on chassis-internal 265 * metainformation such as QOS profiles, etc.. 266 */ 267 #define LINKTYPE_JUNIPER_MLPPP 130 268 #define LINKTYPE_JUNIPER_MLFR 131 269 #define LINKTYPE_JUNIPER_ES 132 270 #define LINKTYPE_JUNIPER_GGSN 133 271 #define LINKTYPE_JUNIPER_MFR 134 272 #define LINKTYPE_JUNIPER_ATM2 135 273 #define LINKTYPE_JUNIPER_SERVICES 136 274 #define LINKTYPE_JUNIPER_ATM1 137 275 276 #define LINKTYPE_APPLE_IP_OVER_IEEE1394 138 /* Apple IP-over-IEEE 1394 cooked header */ 277 278 #define LINKTYPE_MTP2_WITH_PHDR 139 279 #define LINKTYPE_MTP2 140 280 #define LINKTYPE_MTP3 141 281 #define LINKTYPE_SCCP 142 282 283 #define LINKTYPE_DOCSIS 143 /* DOCSIS MAC frames */ 284 285 #define LINKTYPE_LINUX_IRDA 144 /* Linux-IrDA */ 286 287 /* 288 * Reserved for IBM SP switch and IBM Next Federation switch. 289 */ 290 #define LINKTYPE_IBM_SP 145 291 #define LINKTYPE_IBM_SN 146 292 293 /* 294 * Reserved for private use. If you have some link-layer header type 295 * that you want to use within your organization, with the capture files 296 * using that link-layer header type not ever be sent outside your 297 * organization, you can use these values. 298 * 299 * No libpcap release will use these for any purpose, nor will any 300 * tcpdump release use them, either. 301 * 302 * Do *NOT* use these in capture files that you expect anybody not using 303 * your private versions of capture-file-reading tools to read; in 304 * particular, do *NOT* use them in products, otherwise you may find that 305 * people won't be able to use tcpdump, or snort, or Ethereal, or... to 306 * read capture files from your firewall/intrusion detection/traffic 307 * monitoring/etc. appliance, or whatever product uses that LINKTYPE_ value, 308 * and you may also find that the developers of those applications will 309 * not accept patches to let them read those files. 310 * 311 * Also, do not use them if somebody might send you a capture using them 312 * for *their* private type and tools using them for *your* private type 313 * would have to read them. 314 * 315 * Instead, in those cases, ask "tcpdump-workers@tcpdump.org" for a new DLT_ 316 * and LINKTYPE_ value, as per the comment in pcap-bpf.h, and use the type 317 * you're given. 318 */ 319 #define LINKTYPE_USER0 147 320 #define LINKTYPE_USER1 148 321 #define LINKTYPE_USER2 149 322 #define LINKTYPE_USER3 150 323 #define LINKTYPE_USER4 151 324 #define LINKTYPE_USER5 152 325 #define LINKTYPE_USER6 153 326 #define LINKTYPE_USER7 154 327 #define LINKTYPE_USER8 155 328 #define LINKTYPE_USER9 156 329 #define LINKTYPE_USER10 157 330 #define LINKTYPE_USER11 158 331 #define LINKTYPE_USER12 159 332 #define LINKTYPE_USER13 160 333 #define LINKTYPE_USER14 161 334 #define LINKTYPE_USER15 162 335 336 /* 337 * For future use with 802.11 captures - defined by AbsoluteValue 338 * Systems to store a number of bits of link-layer information 339 * including radio information: 340 * 341 * http://www.shaftnet.org/~pizza/software/capturefrm.txt 342 * 343 * but could and arguably should also be used by non-AVS Linux 344 * 802.11 drivers; that may happen in the future. 345 */ 346 #define LINKTYPE_IEEE802_11_RADIO_AVS 163 /* 802.11 plus AVS radio header */ 347 348 /* 349 * Juniper-private data link type, as per request from 350 * Hannes Gredler <hannes@juniper.net>. The corresponding 351 * DLT_s are used for passing on chassis-internal 352 * metainformation such as QOS profiles, etc.. 353 */ 354 #define LINKTYPE_JUNIPER_MONITOR 164 355 356 /* 357 * Reserved for BACnet MS/TP. 358 */ 359 #define LINKTYPE_BACNET_MS_TP 165 360 361 /* 362 * Another PPP variant as per request from Karsten Keil <kkeil@suse.de>. 363 * 364 * This is used in some OSes to allow a kernel socket filter to distinguish 365 * between incoming and outgoing packets, on a socket intended to 366 * supply pppd with outgoing packets so it can do dial-on-demand and 367 * hangup-on-lack-of-demand; incoming packets are filtered out so they 368 * don't cause pppd to hold the connection up (you don't want random 369 * input packets such as port scans, packets from old lost connections, 370 * etc. to force the connection to stay up). 371 * 372 * The first byte of the PPP header (0xff03) is modified to accomodate 373 * the direction - 0x00 = IN, 0x01 = OUT. 374 */ 375 #define LINKTYPE_PPP_PPPD 166 376 377 /* 378 * Juniper-private data link type, as per request from 379 * Hannes Gredler <hannes@juniper.net>. The DLT_s are used 380 * for passing on chassis-internal metainformation such as 381 * QOS profiles, cookies, etc.. 382 */ 383 #define LINKTYPE_JUNIPER_PPPOE 167 384 #define LINKTYPE_JUNIPER_PPPOE_ATM 168 385 386 #define LINKTYPE_GPRS_LLC 169 /* GPRS LLC */ 387 #define LINKTYPE_GPF_T 170 /* GPF-T (ITU-T G.7041/Y.1303) */ 388 #define LINKTYPE_GPF_F 171 /* GPF-T (ITU-T G.7041/Y.1303) */ 389 390 /* 391 * Requested by Oolan Zimmer <oz@gcom.com> for use in Gcom's T1/E1 line 392 * monitoring equipment. 393 */ 394 #define LINKTYPE_GCOM_T1E1 172 395 #define LINKTYPE_GCOM_SERIAL 173 396 397 /* 398 * Juniper-private data link type, as per request from 399 * Hannes Gredler <hannes@juniper.net>. The DLT_ is used 400 * for internal communication to Physical Interface Cards (PIC) 401 */ 402 #define LINKTYPE_JUNIPER_PIC_PEER 174 403 404 /* 405 * Link types requested by Gregor Maier <gregor@endace.com> of Endace 406 * Measurement Systems. They add an ERF header (see 407 * http://www.endace.com/support/EndaceRecordFormat.pdf) in front of 408 * the link-layer header. 409 */ 410 #define LINKTYPE_ERF_ETH 175 /* Ethernet */ 411 #define LINKTYPE_ERF_POS 176 /* Packet-over-SONET */ 412 413 /* 414 * Requested by Daniele Orlandi <daniele@orlandi.com> for raw LAPD 415 * for vISDN (http://www.orlandi.com/visdn/). Its link-layer header 416 * includes additional information before the LAPD header, so it's 417 * not necessarily a generic LAPD header. 418 */ 419 #define LINKTYPE_LINUX_LAPD 177 420 421 /* 422 * Juniper-private data link type, as per request from 423 * Hannes Gredler <hannes@juniper.net>. 424 * The Link Types are used for prepending meta-information 425 * like interface index, interface name 426 * before standard Ethernet, PPP, Frelay & C-HDLC Frames 427 */ 428 #define LINKTYPE_JUNIPER_ETHER 178 429 #define LINKTYPE_JUNIPER_PPP 179 430 #define LINKTYPE_JUNIPER_FRELAY 180 431 #define LINKTYPE_JUNIPER_CHDLC 181 432 433 static struct linktype_map { 434 int dlt; 435 int linktype; 436 } map[] = { 437 /* 438 * These DLT_* codes have LINKTYPE_* codes with values identical 439 * to the values of the corresponding DLT_* code. 440 */ 441 { DLT_NULL, LINKTYPE_NULL }, 442 { DLT_EN10MB, LINKTYPE_ETHERNET }, 443 { DLT_EN3MB, LINKTYPE_EXP_ETHERNET }, 444 { DLT_AX25, LINKTYPE_AX25 }, 445 { DLT_PRONET, LINKTYPE_PRONET }, 446 { DLT_CHAOS, LINKTYPE_CHAOS }, 447 { DLT_IEEE802, LINKTYPE_TOKEN_RING }, 448 { DLT_ARCNET, LINKTYPE_ARCNET }, 449 { DLT_SLIP, LINKTYPE_SLIP }, 450 { DLT_PPP, LINKTYPE_PPP }, 451 { DLT_FDDI, LINKTYPE_FDDI }, 452 453 /* 454 * These DLT_* codes have different values on different 455 * platforms; we map them to LINKTYPE_* codes that 456 * have values that should never be equal to any DLT_* 457 * code. 458 */ 459 #ifdef DLT_FR 460 /* BSD/OS Frame Relay */ 461 { DLT_FR, LINKTYPE_FRELAY }, 462 #endif 463 464 { DLT_SYMANTEC_FIREWALL, LINKTYPE_SYMANTEC_FIREWALL }, 465 { DLT_ATM_RFC1483, LINKTYPE_ATM_RFC1483 }, 466 { DLT_RAW, LINKTYPE_RAW }, 467 { DLT_SLIP_BSDOS, LINKTYPE_SLIP_BSDOS }, 468 { DLT_PPP_BSDOS, LINKTYPE_PPP_BSDOS }, 469 470 /* BSD/OS Cisco HDLC */ 471 { DLT_C_HDLC, LINKTYPE_C_HDLC }, 472 473 /* 474 * These DLT_* codes are not on all platforms, but, so far, 475 * there don't appear to be any platforms that define 476 * other codes with those values; we map them to 477 * different LINKTYPE_* values anyway, just in case. 478 */ 479 480 /* Linux ATM Classical IP */ 481 { DLT_ATM_CLIP, LINKTYPE_ATM_CLIP }, 482 483 /* NetBSD sync/async serial PPP (or Cisco HDLC) */ 484 { DLT_PPP_SERIAL, LINKTYPE_PPP_HDLC }, 485 486 /* NetBSD PPP over Ethernet */ 487 { DLT_PPP_ETHER, LINKTYPE_PPP_ETHER }, 488 489 /* IEEE 802.11 wireless */ 490 { DLT_IEEE802_11, LINKTYPE_IEEE802_11 }, 491 492 /* Frame Relay */ 493 { DLT_FRELAY, LINKTYPE_FRELAY }, 494 495 /* OpenBSD loopback */ 496 { DLT_LOOP, LINKTYPE_LOOP }, 497 498 /* Linux cooked socket capture */ 499 { DLT_LINUX_SLL, LINKTYPE_LINUX_SLL }, 500 501 /* Apple LocalTalk hardware */ 502 { DLT_LTALK, LINKTYPE_LTALK }, 503 504 /* Acorn Econet */ 505 { DLT_ECONET, LINKTYPE_ECONET }, 506 507 /* OpenBSD DLT_PFLOG */ 508 { DLT_PFLOG, LINKTYPE_PFLOG }, 509 510 /* For Cisco-internal use */ 511 { DLT_CISCO_IOS, LINKTYPE_CISCO_IOS }, 512 513 /* Prism II monitor-mode header plus 802.11 header */ 514 { DLT_PRISM_HEADER, LINKTYPE_PRISM_HEADER }, 515 516 /* FreeBSD Aironet driver stuff */ 517 { DLT_AIRONET_HEADER, LINKTYPE_AIRONET_HEADER }, 518 519 /* Siemens HiPath HDLC */ 520 { DLT_HHDLC, LINKTYPE_HHDLC }, 521 522 /* RFC 2625 IP-over-Fibre Channel */ 523 { DLT_IP_OVER_FC, LINKTYPE_IP_OVER_FC }, 524 525 /* Solaris+SunATM */ 526 { DLT_SUNATM, LINKTYPE_SUNATM }, 527 528 /* RapidIO */ 529 { DLT_RIO, LINKTYPE_RIO }, 530 531 /* PCI Express */ 532 { DLT_PCI_EXP, LINKTYPE_PCI_EXP }, 533 534 /* Xilinx Aurora link layer */ 535 { DLT_AURORA, LINKTYPE_AURORA }, 536 537 /* 802.11 plus BSD radio header */ 538 { DLT_IEEE802_11_RADIO, LINKTYPE_IEEE802_11_RADIO }, 539 540 /* Tazmen Sniffer Protocol */ 541 { DLT_TZSP, LINKTYPE_TZSP }, 542 543 /* Arcnet with Linux-style link-layer headers */ 544 { DLT_ARCNET_LINUX, LINKTYPE_ARCNET_LINUX }, 545 546 /* Juniper-internal chassis encapsulation */ 547 { DLT_JUNIPER_MLPPP, LINKTYPE_JUNIPER_MLPPP }, 548 { DLT_JUNIPER_MLFR, LINKTYPE_JUNIPER_MLFR }, 549 { DLT_JUNIPER_ES, LINKTYPE_JUNIPER_ES }, 550 { DLT_JUNIPER_GGSN, LINKTYPE_JUNIPER_GGSN }, 551 { DLT_JUNIPER_MFR, LINKTYPE_JUNIPER_MFR }, 552 { DLT_JUNIPER_ATM2, LINKTYPE_JUNIPER_ATM2 }, 553 { DLT_JUNIPER_SERVICES, LINKTYPE_JUNIPER_SERVICES }, 554 { DLT_JUNIPER_ATM1, LINKTYPE_JUNIPER_ATM1 }, 555 556 /* Apple IP-over-IEEE 1394 cooked header */ 557 { DLT_APPLE_IP_OVER_IEEE1394, LINKTYPE_APPLE_IP_OVER_IEEE1394 }, 558 559 /* SS7 */ 560 { DLT_MTP2_WITH_PHDR, LINKTYPE_MTP2_WITH_PHDR }, 561 { DLT_MTP2, LINKTYPE_MTP2 }, 562 { DLT_MTP3, LINKTYPE_MTP3 }, 563 { DLT_SCCP, LINKTYPE_SCCP }, 564 565 /* DOCSIS MAC frames */ 566 { DLT_DOCSIS, LINKTYPE_DOCSIS }, 567 568 /* IrDA IrLAP packets + Linux-cooked header */ 569 { DLT_LINUX_IRDA, LINKTYPE_LINUX_IRDA }, 570 571 /* IBM SP and Next Federation switches */ 572 { DLT_IBM_SP, LINKTYPE_IBM_SP }, 573 { DLT_IBM_SN, LINKTYPE_IBM_SN }, 574 575 /* 802.11 plus AVS radio header */ 576 { DLT_IEEE802_11_RADIO_AVS, LINKTYPE_IEEE802_11_RADIO_AVS }, 577 578 /* 579 * Any platform that defines additional DLT_* codes should: 580 * 581 * request a LINKTYPE_* code and value from tcpdump.org, 582 * as per the above; 583 * 584 * add, in their version of libpcap, an entry to map 585 * those DLT_* codes to the corresponding LINKTYPE_* 586 * code; 587 * 588 * redefine, in their "net/bpf.h", any DLT_* values 589 * that collide with the values used by their additional 590 * DLT_* codes, to remove those collisions (but without 591 * making them collide with any of the LINKTYPE_* 592 * values equal to 50 or above; they should also avoid 593 * defining DLT_* values that collide with those 594 * LINKTYPE_* values, either). 595 */ 596 597 /* Juniper-internal chassis encapsulation */ 598 { DLT_JUNIPER_MONITOR, LINKTYPE_JUNIPER_MONITOR }, 599 600 /* BACnet MS/TP */ 601 { DLT_BACNET_MS_TP, LINKTYPE_BACNET_MS_TP }, 602 603 /* PPP for pppd, with direction flag in the PPP header */ 604 { DLT_PPP_PPPD, LINKTYPE_PPP_PPPD}, 605 606 /* Juniper-internal chassis encapsulation */ 607 { DLT_JUNIPER_PPPOE, LINKTYPE_JUNIPER_PPPOE }, 608 { DLT_JUNIPER_PPPOE_ATM,LINKTYPE_JUNIPER_PPPOE_ATM }, 609 610 /* GPRS LLC */ 611 { DLT_GPRS_LLC, LINKTYPE_GPRS_LLC }, 612 613 /* Transparent Generic Framing Procedure (ITU-T G.7041/Y.1303) */ 614 { DLT_GPF_T, LINKTYPE_GPF_T }, 615 616 /* Framed Generic Framing Procedure (ITU-T G.7041/Y.1303) */ 617 { DLT_GPF_F, LINKTYPE_GPF_F }, 618 619 { DLT_GCOM_T1E1, LINKTYPE_GCOM_T1E1 }, 620 { DLT_GCOM_SERIAL, LINKTYPE_GCOM_SERIAL }, 621 622 /* Juniper-internal chassis encapsulation */ 623 { DLT_JUNIPER_PIC_PEER, LINKTYPE_JUNIPER_PIC_PEER }, 624 625 /* Endace types */ 626 { DLT_ERF_ETH, LINKTYPE_ERF_ETH }, 627 { DLT_ERF_POS, LINKTYPE_ERF_POS }, 628 629 /* viSDN LAPD */ 630 { DLT_LINUX_LAPD, LINKTYPE_LINUX_LAPD }, 631 632 /* Juniper meta-information before Ether, PPP, Frame Relay, C-HDLC Frames */ 633 { DLT_JUNIPER_ETHER, LINKTYPE_JUNIPER_ETHER }, 634 { DLT_JUNIPER_PPP, LINKTYPE_JUNIPER_PPP }, 635 { DLT_JUNIPER_FRELAY, LINKTYPE_JUNIPER_FRELAY }, 636 { DLT_JUNIPER_CHDLC, LINKTYPE_JUNIPER_CHDLC }, 637 638 639 { -1, -1 } 640 }; 641 642 static int 643 dlt_to_linktype(int dlt) 644 { 645 int i; 646 647 for (i = 0; map[i].dlt != -1; i++) { 648 if (map[i].dlt == dlt) 649 return (map[i].linktype); 650 } 651 652 /* 653 * If we don't have a mapping for this DLT_ code, return an 654 * error; that means that the table above needs to have an 655 * entry added. 656 */ 657 return (-1); 658 } 659 660 static int 661 linktype_to_dlt(int linktype) 662 { 663 int i; 664 665 for (i = 0; map[i].linktype != -1; i++) { 666 if (map[i].linktype == linktype) 667 return (map[i].dlt); 668 } 669 670 /* 671 * If we don't have an entry for this link type, return 672 * the link type value; it may be a DLT_ value from an 673 * older version of libpcap. 674 */ 675 return linktype; 676 } 677 678 static int 679 sf_write_header(FILE *fp, int linktype, int thiszone, int snaplen) 680 { 681 struct pcap_file_header hdr; 682 683 hdr.magic = TCPDUMP_MAGIC; 684 hdr.version_major = PCAP_VERSION_MAJOR; 685 hdr.version_minor = PCAP_VERSION_MINOR; 686 687 hdr.thiszone = thiszone; 688 hdr.snaplen = snaplen; 689 hdr.sigfigs = 0; 690 hdr.linktype = linktype; 691 692 if (fwrite((char *)&hdr, sizeof(hdr), 1, fp) != 1) 693 return (-1); 694 695 return (0); 696 } 697 698 static void 699 swap_hdr(struct pcap_file_header *hp) 700 { 701 hp->version_major = SWAPSHORT(hp->version_major); 702 hp->version_minor = SWAPSHORT(hp->version_minor); 703 hp->thiszone = SWAPLONG(hp->thiszone); 704 hp->sigfigs = SWAPLONG(hp->sigfigs); 705 hp->snaplen = SWAPLONG(hp->snaplen); 706 hp->linktype = SWAPLONG(hp->linktype); 707 } 708 709 static int 710 sf_getnonblock(pcap_t *p, char *errbuf) 711 { 712 /* 713 * This is a savefile, not a live capture file, so never say 714 * it's in non-blocking mode. 715 */ 716 return (0); 717 } 718 719 static int 720 sf_setnonblock(pcap_t *p, int nonblock, char *errbuf) 721 { 722 /* 723 * This is a savefile, not a live capture file, so ignore 724 * requests to put it in non-blocking mode. 725 */ 726 return (0); 727 } 728 729 static int 730 sf_stats(pcap_t *p, struct pcap_stat *ps) 731 { 732 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 733 "Statistics aren't available from savefiles"); 734 return (-1); 735 } 736 737 static int 738 sf_inject(pcap_t *p, const void *buf _U_, size_t size _U_) 739 { 740 strlcpy(p->errbuf, "Sending packets isn't supported on savefiles", 741 PCAP_ERRBUF_SIZE); 742 return (-1); 743 } 744 745 /* 746 * Set direction flag: Which packets do we accept on a forwarding 747 * single device? IN, OUT or both? 748 */ 749 static int 750 sf_setdirection(pcap_t *p, pcap_direction_t d) 751 { 752 snprintf(p->errbuf, sizeof(p->errbuf), 753 "Setting direction is not supported on savefiles"); 754 return (-1); 755 } 756 757 static void 758 sf_close(pcap_t *p) 759 { 760 if (p->sf.rfile != stdin) 761 (void)fclose(p->sf.rfile); 762 if (p->sf.base != NULL) 763 free(p->sf.base); 764 } 765 766 pcap_t * 767 pcap_open_offline(const char *fname, char *errbuf) 768 { 769 FILE *fp; 770 pcap_t *p; 771 772 if (fname[0] == '-' && fname[1] == '\0') 773 { 774 fp = stdin; 775 #if defined(WIN32) || defined(MSDOS) 776 /* 777 * We're reading from the standard input, so put it in binary 778 * mode, as savefiles are binary files. 779 */ 780 SET_BINMODE(fp); 781 #endif 782 } 783 else { 784 #if !defined(WIN32) && !defined(MSDOS) 785 fp = fopen(fname, "r"); 786 #else 787 fp = fopen(fname, "rb"); 788 #endif 789 if (fp == NULL) { 790 snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", fname, 791 pcap_strerror(errno)); 792 return (NULL); 793 } 794 } 795 p = pcap_fopen_offline(fp, errbuf); 796 if (p == NULL) { 797 if (fp != stdin) 798 fclose(fp); 799 } 800 return (p); 801 } 802 803 pcap_t * 804 pcap_fopen_offline(FILE *fp, char *errbuf) 805 { 806 register pcap_t *p; 807 struct pcap_file_header hdr; 808 size_t amt_read; 809 bpf_u_int32 magic; 810 int linklen; 811 812 p = (pcap_t *)malloc(sizeof(*p)); 813 if (p == NULL) { 814 strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE); 815 return (NULL); 816 } 817 818 memset((char *)p, 0, sizeof(*p)); 819 820 amt_read = fread((char *)&hdr, 1, sizeof(hdr), fp); 821 if (amt_read != sizeof(hdr)) { 822 if (ferror(fp)) { 823 snprintf(errbuf, PCAP_ERRBUF_SIZE, 824 "error reading dump file: %s", 825 pcap_strerror(errno)); 826 } else { 827 snprintf(errbuf, PCAP_ERRBUF_SIZE, 828 "truncated dump file; tried to read %lu file header bytes, only got %lu", 829 (unsigned long)sizeof(hdr), 830 (unsigned long)amt_read); 831 } 832 goto bad; 833 } 834 magic = hdr.magic; 835 if (magic != TCPDUMP_MAGIC && magic != KUZNETZOV_TCPDUMP_MAGIC) { 836 magic = SWAPLONG(magic); 837 if (magic != TCPDUMP_MAGIC && magic != KUZNETZOV_TCPDUMP_MAGIC) { 838 snprintf(errbuf, PCAP_ERRBUF_SIZE, 839 "bad dump file format"); 840 goto bad; 841 } 842 p->sf.swapped = 1; 843 swap_hdr(&hdr); 844 } 845 if (magic == KUZNETZOV_TCPDUMP_MAGIC) { 846 /* 847 * XXX - the patch that's in some versions of libpcap 848 * changes the packet header but not the magic number, 849 * and some other versions with this magic number have 850 * some extra debugging information in the packet header; 851 * we'd have to use some hacks^H^H^H^H^Hheuristics to 852 * detect those variants. 853 * 854 * Ethereal does that, but it does so by trying to read 855 * the first two packets of the file with each of the 856 * record header formats. That currently means it seeks 857 * backwards and retries the reads, which doesn't work 858 * on pipes. We want to be able to read from a pipe, so 859 * that strategy won't work; we'd have to buffer some 860 * data ourselves and read from that buffer in order to 861 * make that work. 862 */ 863 p->sf.hdrsize = sizeof(struct pcap_sf_patched_pkthdr); 864 } else 865 p->sf.hdrsize = sizeof(struct pcap_sf_pkthdr); 866 if (hdr.version_major < PCAP_VERSION_MAJOR) { 867 snprintf(errbuf, PCAP_ERRBUF_SIZE, "archaic file format"); 868 goto bad; 869 } 870 p->tzoff = hdr.thiszone; 871 p->snapshot = hdr.snaplen; 872 p->linktype = linktype_to_dlt(hdr.linktype); 873 p->sf.rfile = fp; 874 #ifndef WIN32 875 p->bufsize = hdr.snaplen; 876 #else 877 /* Allocate the space for pcap_pkthdr as well. It will be used by pcap_read_ex */ 878 p->bufsize = hdr.snaplen+sizeof(struct pcap_pkthdr); 879 #endif 880 881 /* Align link header as required for proper data alignment */ 882 /* XXX should handle all types */ 883 switch (p->linktype) { 884 885 case DLT_EN10MB: 886 linklen = 14; 887 break; 888 889 case DLT_FDDI: 890 linklen = 13 + 8; /* fddi_header + llc */ 891 break; 892 893 case DLT_NULL: 894 default: 895 linklen = 0; 896 break; 897 } 898 899 if (p->bufsize < 0) 900 p->bufsize = BPF_MAXBUFSIZE; 901 p->sf.base = (u_char *)malloc(p->bufsize + BPF_ALIGNMENT); 902 if (p->sf.base == NULL) { 903 strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE); 904 goto bad; 905 } 906 p->buffer = p->sf.base + BPF_ALIGNMENT - (linklen % BPF_ALIGNMENT); 907 p->sf.version_major = hdr.version_major; 908 p->sf.version_minor = hdr.version_minor; 909 #ifdef PCAP_FDDIPAD 910 /* Padding only needed for live capture fcode */ 911 p->fddipad = 0; 912 #endif 913 914 /* 915 * We interchanged the caplen and len fields at version 2.3, 916 * in order to match the bpf header layout. But unfortunately 917 * some files were written with version 2.3 in their headers 918 * but without the interchanged fields. 919 * 920 * In addition, DG/UX tcpdump writes out files with a version 921 * number of 543.0, and with the caplen and len fields in the 922 * pre-2.3 order. 923 */ 924 switch (hdr.version_major) { 925 926 case 2: 927 if (hdr.version_minor < 3) 928 p->sf.lengths_swapped = SWAPPED; 929 else if (hdr.version_minor == 3) 930 p->sf.lengths_swapped = MAYBE_SWAPPED; 931 else 932 p->sf.lengths_swapped = NOT_SWAPPED; 933 break; 934 935 case 543: 936 p->sf.lengths_swapped = SWAPPED; 937 break; 938 939 default: 940 p->sf.lengths_swapped = NOT_SWAPPED; 941 break; 942 } 943 944 #if !defined(WIN32) && !defined(MSDOS) 945 /* 946 * You can do "select()" and "poll()" on plain files on most 947 * platforms, and should be able to do so on pipes. 948 * 949 * You can't do "select()" on anything other than sockets in 950 * Windows, so, on Win32 systems, we don't have "selectable_fd". 951 */ 952 p->selectable_fd = fileno(fp); 953 #endif 954 955 p->read_op = pcap_offline_read; 956 p->inject_op = sf_inject; 957 p->setfilter_op = install_bpf_program; 958 p->setdirection_op = sf_setdirection; 959 p->set_datalink_op = NULL; /* we don't support munging link-layer headers */ 960 p->getnonblock_op = sf_getnonblock; 961 p->setnonblock_op = sf_setnonblock; 962 p->stats_op = sf_stats; 963 p->close_op = sf_close; 964 965 return (p); 966 bad: 967 free(p); 968 return (NULL); 969 } 970 971 /* 972 * Read sf_readfile and return the next packet. Return the header in hdr 973 * and the contents in buf. Return 0 on success, SFERR_EOF if there were 974 * no more packets, and SFERR_TRUNC if a partial packet was encountered. 975 */ 976 static int 977 sf_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char *buf, u_int buflen) 978 { 979 struct pcap_sf_patched_pkthdr sf_hdr; 980 FILE *fp = p->sf.rfile; 981 size_t amt_read; 982 bpf_u_int32 t; 983 984 /* 985 * Read the packet header; the structure we use as a buffer 986 * is the longer structure for files generated by the patched 987 * libpcap, but if the file has the magic number for an 988 * unpatched libpcap we only read as many bytes as the regular 989 * header has. 990 */ 991 amt_read = fread(&sf_hdr, 1, p->sf.hdrsize, fp); 992 if (amt_read != p->sf.hdrsize) { 993 if (ferror(fp)) { 994 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 995 "error reading dump file: %s", 996 pcap_strerror(errno)); 997 return (-1); 998 } else { 999 if (amt_read != 0) { 1000 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1001 "truncated dump file; tried to read %d header bytes, only got %lu", 1002 p->sf.hdrsize, (unsigned long)amt_read); 1003 return (-1); 1004 } 1005 /* EOF */ 1006 return (1); 1007 } 1008 } 1009 1010 if (p->sf.swapped) { 1011 /* these were written in opposite byte order */ 1012 hdr->caplen = SWAPLONG(sf_hdr.caplen); 1013 hdr->len = SWAPLONG(sf_hdr.len); 1014 hdr->ts.tv_sec = SWAPLONG(sf_hdr.ts.tv_sec); 1015 hdr->ts.tv_usec = SWAPLONG(sf_hdr.ts.tv_usec); 1016 } else { 1017 hdr->caplen = sf_hdr.caplen; 1018 hdr->len = sf_hdr.len; 1019 hdr->ts.tv_sec = sf_hdr.ts.tv_sec; 1020 hdr->ts.tv_usec = sf_hdr.ts.tv_usec; 1021 } 1022 /* Swap the caplen and len fields, if necessary. */ 1023 switch (p->sf.lengths_swapped) { 1024 1025 case NOT_SWAPPED: 1026 break; 1027 1028 case MAYBE_SWAPPED: 1029 if (hdr->caplen <= hdr->len) { 1030 /* 1031 * The captured length is <= the actual length, 1032 * so presumably they weren't swapped. 1033 */ 1034 break; 1035 } 1036 /* FALLTHROUGH */ 1037 1038 case SWAPPED: 1039 t = hdr->caplen; 1040 hdr->caplen = hdr->len; 1041 hdr->len = t; 1042 break; 1043 } 1044 1045 if (hdr->caplen > buflen) { 1046 /* 1047 * This can happen due to Solaris 2.3 systems tripping 1048 * over the BUFMOD problem and not setting the snapshot 1049 * correctly in the savefile header. If the caplen isn't 1050 * grossly wrong, try to salvage. 1051 */ 1052 static u_char *tp = NULL; 1053 static size_t tsize = 0; 1054 1055 if (hdr->caplen > 65535) { 1056 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1057 "bogus savefile header"); 1058 return (-1); 1059 } 1060 1061 if (tsize < hdr->caplen) { 1062 tsize = ((hdr->caplen + 1023) / 1024) * 1024; 1063 if (tp != NULL) 1064 free((u_char *)tp); 1065 tp = (u_char *)malloc(tsize); 1066 if (tp == NULL) { 1067 tsize = 0; 1068 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1069 "BUFMOD hack malloc"); 1070 return (-1); 1071 } 1072 } 1073 amt_read = fread((char *)tp, 1, hdr->caplen, fp); 1074 if (amt_read != hdr->caplen) { 1075 if (ferror(fp)) { 1076 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1077 "error reading dump file: %s", 1078 pcap_strerror(errno)); 1079 } else { 1080 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1081 "truncated dump file; tried to read %u captured bytes, only got %lu", 1082 hdr->caplen, (unsigned long)amt_read); 1083 } 1084 return (-1); 1085 } 1086 /* 1087 * We can only keep up to buflen bytes. Since caplen > buflen 1088 * is exactly how we got here, we know we can only keep the 1089 * first buflen bytes and must drop the remainder. Adjust 1090 * caplen accordingly, so we don't get confused later as 1091 * to how many bytes we have to play with. 1092 */ 1093 hdr->caplen = buflen; 1094 memcpy((char *)buf, (char *)tp, buflen); 1095 1096 } else { 1097 /* read the packet itself */ 1098 amt_read = fread((char *)buf, 1, hdr->caplen, fp); 1099 if (amt_read != hdr->caplen) { 1100 if (ferror(fp)) { 1101 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1102 "error reading dump file: %s", 1103 pcap_strerror(errno)); 1104 } else { 1105 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1106 "truncated dump file; tried to read %u captured bytes, only got %lu", 1107 hdr->caplen, (unsigned long)amt_read); 1108 } 1109 return (-1); 1110 } 1111 } 1112 return (0); 1113 } 1114 1115 /* 1116 * Print out packets stored in the file initialized by sf_read_init(). 1117 * If cnt > 0, return after 'cnt' packets, otherwise continue until eof. 1118 */ 1119 int 1120 pcap_offline_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 1121 { 1122 struct bpf_insn *fcode; 1123 int status = 0; 1124 int n = 0; 1125 1126 while (status == 0) { 1127 struct pcap_pkthdr h; 1128 1129 /* 1130 * Has "pcap_breakloop()" been called? 1131 * If so, return immediately - if we haven't read any 1132 * packets, clear the flag and return -2 to indicate 1133 * that we were told to break out of the loop, otherwise 1134 * leave the flag set, so that the *next* call will break 1135 * out of the loop without having read any packets, and 1136 * return the number of packets we've processed so far. 1137 */ 1138 if (p->break_loop) { 1139 if (n == 0) { 1140 p->break_loop = 0; 1141 return (-2); 1142 } else 1143 return (n); 1144 } 1145 1146 status = sf_next_packet(p, &h, p->buffer, p->bufsize); 1147 if (status) { 1148 if (status == 1) 1149 return (0); 1150 return (status); 1151 } 1152 1153 if ((fcode = p->fcode.bf_insns) == NULL || 1154 bpf_filter(fcode, p->buffer, h.len, h.caplen)) { 1155 (*callback)(user, &h, p->buffer); 1156 if (++n >= cnt && cnt > 0) 1157 break; 1158 } 1159 } 1160 /*XXX this breaks semantics tcpslice expects */ 1161 return (n); 1162 } 1163 1164 /* 1165 * Output a packet to the initialized dump file. 1166 */ 1167 void 1168 pcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp) 1169 { 1170 register FILE *f; 1171 struct pcap_sf_pkthdr sf_hdr; 1172 1173 f = (FILE *)user; 1174 sf_hdr.ts.tv_sec = h->ts.tv_sec; 1175 sf_hdr.ts.tv_usec = h->ts.tv_usec; 1176 sf_hdr.caplen = h->caplen; 1177 sf_hdr.len = h->len; 1178 /* XXX we should check the return status */ 1179 (void)fwrite(&sf_hdr, sizeof(sf_hdr), 1, f); 1180 (void)fwrite((char *)sp, h->caplen, 1, f); 1181 } 1182 1183 static pcap_dumper_t * 1184 pcap_setup_dump(pcap_t *p, int linktype, FILE *f, const char *fname) 1185 { 1186 1187 #if defined(WIN32) || defined(MSDOS) 1188 /* 1189 * If we're writing to the standard output, put it in binary 1190 * mode, as savefiles are binary files. 1191 * 1192 * Otherwise, we turn off buffering. 1193 * XXX - why? And why not on the standard output? 1194 */ 1195 if (f == stdout) 1196 SET_BINMODE(f); 1197 else 1198 setbuf(f, NULL); 1199 #endif 1200 if (sf_write_header(f, linktype, p->tzoff, p->snapshot) == -1) { 1201 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "Can't write to %s: %s", 1202 fname, pcap_strerror(errno)); 1203 if (f != stdout) 1204 (void)fclose(f); 1205 return (NULL); 1206 } 1207 return ((pcap_dumper_t *)f); 1208 } 1209 1210 /* 1211 * Initialize so that sf_write() will output to the file named 'fname'. 1212 */ 1213 pcap_dumper_t * 1214 pcap_dump_open(pcap_t *p, const char *fname) 1215 { 1216 FILE *f; 1217 int linktype; 1218 1219 linktype = dlt_to_linktype(p->linktype); 1220 if (linktype == -1) { 1221 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1222 "%s: link-layer type %d isn't supported in savefiles", 1223 fname, linktype); 1224 return (NULL); 1225 } 1226 1227 if (fname[0] == '-' && fname[1] == '\0') { 1228 f = stdout; 1229 fname = "standard output"; 1230 } else { 1231 #if !defined(WIN32) && !defined(MSDOS) 1232 f = fopen(fname, "w"); 1233 #else 1234 f = fopen(fname, "wb"); 1235 #endif 1236 if (f == NULL) { 1237 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s", 1238 fname, pcap_strerror(errno)); 1239 return (NULL); 1240 } 1241 } 1242 return (pcap_setup_dump(p, linktype, f, fname)); 1243 } 1244 1245 /* 1246 * Initialize so that sf_write() will output to the given stream. 1247 */ 1248 pcap_dumper_t * 1249 pcap_dump_fopen(pcap_t *p, FILE *f) 1250 { 1251 int linktype; 1252 1253 linktype = dlt_to_linktype(p->linktype); 1254 if (linktype == -1) { 1255 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1256 "stream: link-layer type %d isn't supported in savefiles", 1257 linktype); 1258 return (NULL); 1259 } 1260 1261 return (pcap_setup_dump(p, linktype, f, "stream")); 1262 } 1263 1264 FILE * 1265 pcap_dump_file(pcap_dumper_t *p) 1266 { 1267 return ((FILE *)p); 1268 } 1269 1270 long 1271 pcap_dump_ftell(pcap_dumper_t *p) 1272 { 1273 return (ftell((FILE *)p)); 1274 } 1275 1276 int 1277 pcap_dump_flush(pcap_dumper_t *p) 1278 { 1279 1280 if (fflush((FILE *)p) == EOF) 1281 return (-1); 1282 else 1283 return (0); 1284 } 1285 1286 void 1287 pcap_dump_close(pcap_dumper_t *p) 1288 { 1289 1290 #ifdef notyet 1291 if (ferror((FILE *)p)) 1292 return-an-error; 1293 /* XXX should check return from fclose() too */ 1294 #endif 1295 (void)fclose((FILE *)p); 1296 } 1297