2 * Copyright (c) 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
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
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.
21 * savefile.c - supports offline use of tcpdump
22 * Extraction/creation by Jeffrey Mogul, DECWRL
23 * Modified by Steve McCanne, LBL.
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.
32 static const char rcsid[] _U_ =
33 "@(#) $Header: /tcpdump/master/libpcap/savefile.c,v 1.92.2.11 2004/03/11 23:46:14 guy Exp $ (LBL)";
48 #ifdef HAVE_OS_PROTO_H
52 #define TCPDUMP_MAGIC 0xa1b2c3d4
53 #define PATCHED_TCPDUMP_MAGIC 0xa1b2cd34
56 * We use the "receiver-makes-right" approach to byte order,
57 * because time is at a premium when we are writing the file.
58 * In other words, the pcap_file_header and pcap_pkthdr,
59 * records are written in host byte order.
60 * Note that the bytes of packet data are written out in the order in
61 * which they were received, so multi-byte fields in packets are not
62 * written in host byte order, they're written in whatever order the
63 * sending machine put them in.
65 * ntoh[ls] aren't sufficient because we might need to swap on a big-endian
66 * machine (if the file was written in little-end order).
69 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
70 #define SWAPSHORT(y) \
71 ( (((y)&0xff)<<8) | ((u_short)((y)&0xff00)>>8) )
74 #define SFERR_BADVERSION 2
76 #define SFERR_EOF 4 /* not really an error, just a status */
79 * We don't write DLT_* values to the capture file header, because
80 * they're not the same on all platforms.
82 * Unfortunately, the various flavors of BSD have not always used the same
83 * numerical values for the same data types, and various patches to
84 * libpcap for non-BSD OSes have added their own DLT_* codes for link
85 * layer encapsulation types seen on those OSes, and those codes have had,
86 * in some cases, values that were also used, on other platforms, for other
87 * link layer encapsulation types.
89 * This means that capture files of a type whose numerical DLT_* code
90 * means different things on different BSDs, or with different versions
91 * of libpcap, can't always be read on systems other than those like
92 * the one running on the machine on which the capture was made.
94 * Instead, we define here a set of LINKTYPE_* codes, and map DLT_* codes
95 * to LINKTYPE_* codes when writing a savefile header, and map LINKTYPE_*
96 * codes to DLT_* codes when reading a savefile header.
98 * For those DLT_* codes that have, as far as we know, the same values on
99 * all platforms (DLT_NULL through DLT_FDDI), we define LINKTYPE_xxx as
100 * DLT_xxx; that way, captures of those types can still be read by
101 * versions of libpcap that map LINKTYPE_* values to DLT_* values, and
102 * captures of those types written by versions of libpcap that map DLT_
103 * values to LINKTYPE_ values can still be read by older versions
106 * The other LINKTYPE_* codes are given values starting at 100, in the
107 * hopes that no DLT_* code will be given one of those values.
109 * In order to ensure that a given LINKTYPE_* code's value will refer to
110 * the same encapsulation type on all platforms, you should not allocate
111 * a new LINKTYPE_* value without consulting "tcpdump-workers@tcpdump.org".
112 * The tcpdump developers will allocate a value for you, and will not
113 * subsequently allocate it to anybody else; that value will be added to
114 * the "pcap.h" in the tcpdump.org CVS repository, so that a future
115 * libpcap release will include it.
117 * You should, if possible, also contribute patches to libpcap and tcpdump
118 * to handle the new encapsulation type, so that they can also be checked
119 * into the tcpdump.org CVS repository and so that they will appear in
120 * future libpcap and tcpdump releases.
122 * Do *NOT* assume that any values after the largest value in this file
123 * are available; you might not have the most up-to-date version of this
124 * file, and new values after that one might have been assigned. Also,
125 * do *NOT* use any values below 100 - those might already have been
126 * taken by one (or more!) organizations.
128 #define LINKTYPE_NULL DLT_NULL
129 #define LINKTYPE_ETHERNET DLT_EN10MB /* also for 100Mb and up */
130 #define LINKTYPE_EXP_ETHERNET DLT_EN3MB /* 3Mb experimental Ethernet */
131 #define LINKTYPE_AX25 DLT_AX25
132 #define LINKTYPE_PRONET DLT_PRONET
133 #define LINKTYPE_CHAOS DLT_CHAOS
134 #define LINKTYPE_TOKEN_RING DLT_IEEE802 /* DLT_IEEE802 is used for Token Ring */
135 #define LINKTYPE_ARCNET DLT_ARCNET /* BSD-style headers */
136 #define LINKTYPE_SLIP DLT_SLIP
137 #define LINKTYPE_PPP DLT_PPP
138 #define LINKTYPE_FDDI DLT_FDDI
141 * LINKTYPE_PPP is for use when there might, or might not, be an RFC 1662
142 * PPP in HDLC-like framing header (with 0xff 0x03 before the PPP protocol
143 * field) at the beginning of the packet.
145 * This is for use when there is always such a header; the address field
146 * might be 0xff, for regular PPP, or it might be an address field for Cisco
147 * point-to-point with HDLC framing as per section 4.3.1 of RFC 1547 ("Cisco
148 * HDLC"). This is, for example, what you get with NetBSD's DLT_PPP_SERIAL.
150 * We give it the same value as NetBSD's DLT_PPP_SERIAL, in the hopes that
151 * nobody else will choose a DLT_ value of 50, and so that DLT_PPP_SERIAL
152 * captures will be written out with a link type that NetBSD's tcpdump
155 #define LINKTYPE_PPP_HDLC 50 /* PPP in HDLC-like framing */
157 #define LINKTYPE_PPP_ETHER 51 /* NetBSD PPP-over-Ethernet */
160 * This isn't supported in libpcap 0.8[.x], but is supported in the
161 * current CVS version; we include it here to note that it's not available
162 * for anybody else to use.
164 #define LINKTYPE_SYMANTEC_FIREWALL 99 /* Symantec Enterprise Firewall */
166 #define LINKTYPE_ATM_RFC1483 100 /* LLC/SNAP-encapsulated ATM */
167 #define LINKTYPE_RAW 101 /* raw IP */
168 #define LINKTYPE_SLIP_BSDOS 102 /* BSD/OS SLIP BPF header */
169 #define LINKTYPE_PPP_BSDOS 103 /* BSD/OS PPP BPF header */
170 #define LINKTYPE_C_HDLC 104 /* Cisco HDLC */
171 #define LINKTYPE_IEEE802_11 105 /* IEEE 802.11 (wireless) */
172 #define LINKTYPE_ATM_CLIP 106 /* Linux Classical IP over ATM */
173 #define LINKTYPE_FRELAY 107 /* Frame Relay */
174 #define LINKTYPE_LOOP 108 /* OpenBSD loopback */
175 #define LINKTYPE_ENC 109 /* OpenBSD IPSEC enc */
178 * These three types are reserved for future use.
180 #define LINKTYPE_LANE8023 110 /* ATM LANE + 802.3 */
181 #define LINKTYPE_HIPPI 111 /* NetBSD HIPPI */
182 #define LINKTYPE_HDLC 112 /* NetBSD HDLC framing */
184 #define LINKTYPE_LINUX_SLL 113 /* Linux cooked socket capture */
185 #define LINKTYPE_LTALK 114 /* Apple LocalTalk hardware */
186 #define LINKTYPE_ECONET 115 /* Acorn Econet */
189 * Reserved for use with OpenBSD ipfilter.
191 #define LINKTYPE_IPFILTER 116
193 #define LINKTYPE_PFLOG 117 /* OpenBSD DLT_PFLOG */
194 #define LINKTYPE_CISCO_IOS 118 /* For Cisco-internal use */
195 #define LINKTYPE_PRISM_HEADER 119 /* 802.11+Prism II monitor mode */
196 #define LINKTYPE_AIRONET_HEADER 120 /* FreeBSD Aironet driver stuff */
199 * Reserved for Siemens HiPath HDLC.
201 #define LINKTYPE_HHDLC 121
203 #define LINKTYPE_IP_OVER_FC 122 /* RFC 2625 IP-over-Fibre Channel */
204 #define LINKTYPE_SUNATM 123 /* Solaris+SunATM */
207 * Reserved as per request from Kent Dahlgren <kent@praesum.com>
210 #define LINKTYPE_RIO 124 /* RapidIO */
211 #define LINKTYPE_PCI_EXP 125 /* PCI Express */
212 #define LINKTYPE_AURORA 126 /* Xilinx Aurora link layer */
214 #define LINKTYPE_IEEE802_11_RADIO 127 /* 802.11 plus BSD radio header */
217 * Reserved for the TZSP encapsulation, as per request from
218 * Chris Waters <chris.waters@networkchemistry.com>
219 * TZSP is a generic encapsulation for any other link type,
220 * which includes a means to include meta-information
221 * with the packet, e.g. signal strength and channel
222 * for 802.11 packets.
224 #define LINKTYPE_TZSP 128 /* Tazmen Sniffer Protocol */
226 #define LINKTYPE_ARCNET_LINUX 129 /* Linux-style headers */
229 * Juniper-private data link types, as per request from
230 * Hannes Gredler <hannes@juniper.net>. The corresponding
231 * DLT_s are used for passing on chassis-internal
232 * metainformation such as QOS profiles, etc..
234 #define LINKTYPE_JUNIPER_MLPPP 130
235 #define LINKTYPE_JUNIPER_MLFR 131
236 #define LINKTYPE_JUNIPER_ES 132
237 #define LINKTYPE_JUNIPER_GGSN 133
238 #define LINKTYPE_JUNIPER_MFR 134
239 #define LINKTYPE_JUNIPER_ATM2 135
240 #define LINKTYPE_JUNIPER_SERVICES 136
241 #define LINKTYPE_JUNIPER_ATM1 137
243 #define LINKTYPE_APPLE_IP_OVER_IEEE1394 138 /* Apple IP-over-IEEE 1394 cooked header */
245 #define LINKTYPE_RAWSS7 139 /* see rawss7.h for */
246 #define LINKTYPE_RAWSS7_MTP2 140 /* information on these */
247 #define LINKTYPE_RAWSS7_MTP3 141 /* definitions */
248 #define LINKTYPE_RAWSS7_SCCP 142
251 * This isn't supported in libpcap 0.8[.x], but is supported in the
252 * current CVS version; we include it here to note that it's not available
253 * for anybody else to use.
255 #define LINKTYPE_DOCSIS 143 /* DOCSIS MAC frames */
257 #define LINKTYPE_LINUX_IRDA 144 /* Linux-IrDA */
260 * Reserved for IBM SP switch and IBM Next Federation switch.
262 #define LINKTYPE_IBM_SP 145
263 #define LINKTYPE_IBM_SN 146
266 * Reserved for private use. If you have some link-layer header type
267 * that you want to use within your organization, with the capture files
268 * using that link-layer header type not ever be sent outside your
269 * organization, you can use these values.
271 * No libpcap release will use these for any purpose, nor will any
272 * tcpdump release use them, either.
274 * Do *NOT* use these in capture files that you expect anybody not using
275 * your private versions of capture-file-reading tools to read; in
276 * particular, do *NOT* use them in products, otherwise you may find that
277 * people won't be able to use tcpdump, or snort, or Ethereal, or... to
278 * read capture files from your firewall/intrusion detection/traffic
279 * monitoring/etc. appliance, or whatever product uses that LINKTYPE_ value,
280 * and you may also find that the developers of those applications will
281 * not accept patches to let them read those files.
283 * Also, do not use them if somebody might send you a capture using them
284 * for *their* private type and tools using them for *your* private type
285 * would have to read them.
287 * Instead, in those cases, ask "tcpdump-workers@tcpdump.org" for a new DLT_
288 * and LINKTYPE_ value, as per the comment in pcap-bpf.h, and use the type
291 #define LINKTYPE_USER0 147
292 #define LINKTYPE_USER1 148
293 #define LINKTYPE_USER2 149
294 #define LINKTYPE_USER3 150
295 #define LINKTYPE_USER4 151
296 #define LINKTYPE_USER5 152
297 #define LINKTYPE_USER6 153
298 #define LINKTYPE_USER7 154
299 #define LINKTYPE_USER8 155
300 #define LINKTYPE_USER9 156
301 #define LINKTYPE_USER10 157
302 #define LINKTYPE_USER11 158
303 #define LINKTYPE_USER12 159
304 #define LINKTYPE_USER13 160
305 #define LINKTYPE_USER14 161
306 #define LINKTYPE_USER15 162
309 * For future use with 802.11 captures - defined by AbsoluteValue
310 * Systems to store a number of bits of link-layer information
311 * including radio information:
313 * http://www.shaftnet.org/~pizza/software/capturefrm.txt
315 * but could and arguably should also be used by non-AVS Linux
316 * 802.11 drivers; that may happen in the future.
318 #define LINKTYPE_IEEE802_11_RADIO_AVS 163 /* 802.11 plus AVS radio header */
321 * Juniper-private data link type, as per request from
322 * Hannes Gredler <hannes@juniper.net>. The corresponding
323 * DLT_s are used for passing on chassis-internal
324 * metainformation such as QOS profiles, etc..
326 #define LINKTYPE_JUNIPER_MONITOR 164
328 static struct linktype_map {
333 * These DLT_* codes have LINKTYPE_* codes with values identical
334 * to the values of the corresponding DLT_* code.
336 { DLT_NULL, LINKTYPE_NULL },
337 { DLT_EN10MB, LINKTYPE_ETHERNET },
338 { DLT_EN3MB, LINKTYPE_EXP_ETHERNET },
339 { DLT_AX25, LINKTYPE_AX25 },
340 { DLT_PRONET, LINKTYPE_PRONET },
341 { DLT_CHAOS, LINKTYPE_CHAOS },
342 { DLT_IEEE802, LINKTYPE_TOKEN_RING },
343 { DLT_ARCNET, LINKTYPE_ARCNET },
344 { DLT_SLIP, LINKTYPE_SLIP },
345 { DLT_PPP, LINKTYPE_PPP },
346 { DLT_FDDI, LINKTYPE_FDDI },
349 * These DLT_* codes have different values on different
350 * platforms; we map them to LINKTYPE_* codes that
351 * have values that should never be equal to any DLT_*
355 /* BSD/OS Frame Relay */
356 { DLT_FR, LINKTYPE_FRELAY },
359 { DLT_SYMANTEC_FIREWALL, LINKTYPE_SYMANTEC_FIREWALL },
360 { DLT_ATM_RFC1483, LINKTYPE_ATM_RFC1483 },
361 { DLT_RAW, LINKTYPE_RAW },
362 { DLT_SLIP_BSDOS, LINKTYPE_SLIP_BSDOS },
363 { DLT_PPP_BSDOS, LINKTYPE_PPP_BSDOS },
365 /* BSD/OS Cisco HDLC */
366 { DLT_C_HDLC, LINKTYPE_C_HDLC },
369 * These DLT_* codes are not on all platforms, but, so far,
370 * there don't appear to be any platforms that define
371 * other codes with those values; we map them to
372 * different LINKTYPE_* values anyway, just in case.
375 /* Linux ATM Classical IP */
376 { DLT_ATM_CLIP, LINKTYPE_ATM_CLIP },
378 /* NetBSD sync/async serial PPP (or Cisco HDLC) */
379 { DLT_PPP_SERIAL, LINKTYPE_PPP_HDLC },
381 /* NetBSD PPP over Ethernet */
382 { DLT_PPP_ETHER, LINKTYPE_PPP_ETHER },
384 /* IEEE 802.11 wireless */
385 { DLT_IEEE802_11, LINKTYPE_IEEE802_11 },
388 { DLT_FRELAY, LINKTYPE_FRELAY },
390 /* OpenBSD loopback */
391 { DLT_LOOP, LINKTYPE_LOOP },
393 /* Linux cooked socket capture */
394 { DLT_LINUX_SLL, LINKTYPE_LINUX_SLL },
396 /* Apple LocalTalk hardware */
397 { DLT_LTALK, LINKTYPE_LTALK },
400 { DLT_ECONET, LINKTYPE_ECONET },
402 /* OpenBSD DLT_PFLOG */
403 { DLT_PFLOG, LINKTYPE_PFLOG },
405 /* For Cisco-internal use */
406 { DLT_CISCO_IOS, LINKTYPE_CISCO_IOS },
408 /* Prism II monitor-mode header plus 802.11 header */
409 { DLT_PRISM_HEADER, LINKTYPE_PRISM_HEADER },
411 /* FreeBSD Aironet driver stuff */
412 { DLT_AIRONET_HEADER, LINKTYPE_AIRONET_HEADER },
414 /* Siemens HiPath HDLC */
415 { DLT_HHDLC, LINKTYPE_HHDLC },
417 /* RFC 2625 IP-over-Fibre Channel */
418 { DLT_IP_OVER_FC, LINKTYPE_IP_OVER_FC },
421 { DLT_SUNATM, LINKTYPE_SUNATM },
424 { DLT_RIO, LINKTYPE_RIO },
427 { DLT_PCI_EXP, LINKTYPE_PCI_EXP },
429 /* Xilinx Aurora link layer */
430 { DLT_AURORA, LINKTYPE_AURORA },
432 /* 802.11 plus BSD radio header */
433 { DLT_IEEE802_11_RADIO, LINKTYPE_IEEE802_11_RADIO },
435 /* Tazmen Sniffer Protocol */
436 { DLT_TZSP, LINKTYPE_TZSP },
438 /* Arcnet with Linux-style link-layer headers */
439 { DLT_ARCNET_LINUX, LINKTYPE_ARCNET_LINUX },
441 /* Juniper-internal chassis encapsulation */
442 { DLT_JUNIPER_MLPPP, LINKTYPE_JUNIPER_MLPPP },
443 { DLT_JUNIPER_MLFR, LINKTYPE_JUNIPER_MLFR },
444 { DLT_JUNIPER_ES, LINKTYPE_JUNIPER_ES },
445 { DLT_JUNIPER_GGSN, LINKTYPE_JUNIPER_GGSN },
446 { DLT_JUNIPER_MFR, LINKTYPE_JUNIPER_MFR },
447 { DLT_JUNIPER_ATM2, LINKTYPE_JUNIPER_ATM2 },
448 { DLT_JUNIPER_SERVICES, LINKTYPE_JUNIPER_SERVICES },
449 { DLT_JUNIPER_ATM1, LINKTYPE_JUNIPER_ATM1 },
451 /* Apple IP-over-IEEE 1394 cooked header */
452 { DLT_APPLE_IP_OVER_IEEE1394, LINKTYPE_APPLE_IP_OVER_IEEE1394 },
454 /* DOCSIS MAC frames */
455 { DLT_DOCSIS, LINKTYPE_DOCSIS },
457 /* IrDA IrLAP packets + Linux-cooked header */
458 { DLT_LINUX_IRDA, LINKTYPE_LINUX_IRDA },
460 /* IBM SP and Next Federation switches */
461 { DLT_IBM_SP, LINKTYPE_IBM_SP },
462 { DLT_IBM_SN, LINKTYPE_IBM_SN },
464 /* 802.11 plus AVS radio header */
465 { DLT_IEEE802_11_RADIO_AVS, LINKTYPE_IEEE802_11_RADIO_AVS },
468 * Any platform that defines additional DLT_* codes should:
470 * request a LINKTYPE_* code and value from tcpdump.org,
473 * add, in their version of libpcap, an entry to map
474 * those DLT_* codes to the corresponding LINKTYPE_*
477 * redefine, in their "net/bpf.h", any DLT_* values
478 * that collide with the values used by their additional
479 * DLT_* codes, to remove those collisions (but without
480 * making them collide with any of the LINKTYPE_*
481 * values equal to 50 or above; they should also avoid
482 * defining DLT_* values that collide with those
483 * LINKTYPE_* values, either).
486 /* Juniper-internal chassis encapsulation */
487 { DLT_JUNIPER_MONITOR, LINKTYPE_JUNIPER_MONITOR },
493 dlt_to_linktype(int dlt)
497 for (i = 0; map[i].dlt != -1; i++) {
498 if (map[i].dlt == dlt)
499 return (map[i].linktype);
503 * If we don't have a mapping for this DLT_ code, return an
504 * error; that means that the table above needs to have an
511 linktype_to_dlt(int linktype)
515 for (i = 0; map[i].linktype != -1; i++) {
516 if (map[i].linktype == linktype)
521 * If we don't have an entry for this link type, return
522 * the link type value; it may be a DLT_ value from an
523 * older version of libpcap.
529 sf_write_header(FILE *fp, int linktype, int thiszone, int snaplen)
531 struct pcap_file_header hdr;
533 hdr.magic = TCPDUMP_MAGIC;
534 hdr.version_major = PCAP_VERSION_MAJOR;
535 hdr.version_minor = PCAP_VERSION_MINOR;
537 hdr.thiszone = thiszone;
538 hdr.snaplen = snaplen;
540 hdr.linktype = linktype;
542 if (fwrite((char *)&hdr, sizeof(hdr), 1, fp) != 1)
549 swap_hdr(struct pcap_file_header *hp)
551 hp->version_major = SWAPSHORT(hp->version_major);
552 hp->version_minor = SWAPSHORT(hp->version_minor);
553 hp->thiszone = SWAPLONG(hp->thiszone);
554 hp->sigfigs = SWAPLONG(hp->sigfigs);
555 hp->snaplen = SWAPLONG(hp->snaplen);
556 hp->linktype = SWAPLONG(hp->linktype);
560 sf_getnonblock(pcap_t *p, char *errbuf)
563 * This is a savefile, not a live capture file, so never say
564 * it's in non-blocking mode.
570 sf_setnonblock(pcap_t *p, int nonblock, char *errbuf)
573 * This is a savefile, not a live capture file, so ignore
574 * requests to put it in non-blocking mode.
580 sf_stats(pcap_t *p, struct pcap_stat *ps)
582 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
583 "Statistics aren't available from savefiles");
590 if (p->sf.rfile != stdin)
591 (void)fclose(p->sf.rfile);
592 if (p->sf.base != NULL)
597 pcap_open_offline(const char *fname, char *errbuf)
601 struct pcap_file_header hdr;
605 p = (pcap_t *)malloc(sizeof(*p));
607 strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
611 memset((char *)p, 0, sizeof(*p));
613 if (fname[0] == '-' && fname[1] == '\0')
617 fp = fopen(fname, "r");
619 fp = fopen(fname, "rb");
622 snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", fname,
623 pcap_strerror(errno));
627 if (fread((char *)&hdr, sizeof(hdr), 1, fp) != 1) {
628 snprintf(errbuf, PCAP_ERRBUF_SIZE, "fread: %s",
629 pcap_strerror(errno));
633 if (magic != TCPDUMP_MAGIC && magic != PATCHED_TCPDUMP_MAGIC) {
634 magic = SWAPLONG(magic);
635 if (magic != TCPDUMP_MAGIC && magic != PATCHED_TCPDUMP_MAGIC) {
636 snprintf(errbuf, PCAP_ERRBUF_SIZE,
637 "bad dump file format");
643 if (magic == PATCHED_TCPDUMP_MAGIC) {
645 * XXX - the patch that's in some versions of libpcap
646 * changes the packet header but not the magic number;
647 * we'd have to use some hacks^H^H^H^H^Hheuristics to
650 p->sf.hdrsize = sizeof(struct pcap_sf_patched_pkthdr);
652 p->sf.hdrsize = sizeof(struct pcap_sf_pkthdr);
653 if (hdr.version_major < PCAP_VERSION_MAJOR) {
654 snprintf(errbuf, PCAP_ERRBUF_SIZE, "archaic file format");
657 p->tzoff = hdr.thiszone;
658 p->snapshot = hdr.snaplen;
659 p->linktype = linktype_to_dlt(hdr.linktype);
662 p->bufsize = hdr.snaplen;
664 /* Allocate the space for pcap_pkthdr as well. It will be used by pcap_read_ex */
665 p->bufsize = hdr.snaplen+sizeof(struct pcap_pkthdr);
668 /* Align link header as required for proper data alignment */
669 /* XXX should handle all types */
670 switch (p->linktype) {
677 linklen = 13 + 8; /* fddi_header + llc */
687 p->bufsize = BPF_MAXBUFSIZE;
688 p->sf.base = (u_char *)malloc(p->bufsize + BPF_ALIGNMENT);
689 if (p->sf.base == NULL) {
690 strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
693 p->buffer = p->sf.base + BPF_ALIGNMENT - (linklen % BPF_ALIGNMENT);
694 p->sf.version_major = hdr.version_major;
695 p->sf.version_minor = hdr.version_minor;
697 /* XXX padding only needed for kernel fcode */
702 * We interchanged the caplen and len fields at version 2.3,
703 * in order to match the bpf header layout. But unfortunately
704 * some files were written with version 2.3 in their headers
705 * but without the interchanged fields.
707 * In addition, DG/UX tcpdump writes out files with a version
708 * number of 543.0, and with the caplen and len fields in the
711 switch (hdr.version_major) {
714 if (hdr.version_minor < 3)
715 p->sf.lengths_swapped = SWAPPED;
716 else if (hdr.version_minor == 3)
717 p->sf.lengths_swapped = MAYBE_SWAPPED;
719 p->sf.lengths_swapped = NOT_SWAPPED;
723 p->sf.lengths_swapped = SWAPPED;
727 p->sf.lengths_swapped = NOT_SWAPPED;
733 * You can do "select()" and "poll()" on plain files on most
734 * platforms, and should be able to do so on pipes.
736 * You can't do "select()" on anything other than sockets in
737 * Windows, so, on Win32 systems, we don't have "selectable_fd".
739 p->selectable_fd = fileno(fp);
742 p->read_op = pcap_offline_read;
743 p->setfilter_op = install_bpf_program;
744 p->set_datalink_op = NULL; /* we don't support munging link-layer headers */
745 p->getnonblock_op = sf_getnonblock;
746 p->setnonblock_op = sf_setnonblock;
747 p->stats_op = sf_stats;
748 p->close_op = sf_close;
759 * Read sf_readfile and return the next packet. Return the header in hdr
760 * and the contents in buf. Return 0 on success, SFERR_EOF if there were
761 * no more packets, and SFERR_TRUNC if a partial packet was encountered.
764 sf_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char *buf, u_int buflen)
766 struct pcap_sf_patched_pkthdr sf_hdr;
767 FILE *fp = p->sf.rfile;
772 * Read the packet header; the structure we use as a buffer
773 * is the longer structure for files generated by the patched
774 * libpcap, but if the file has the magic number for an
775 * unpatched libpcap we only read as many bytes as the regular
778 amt_read = fread(&sf_hdr, 1, p->sf.hdrsize, fp);
779 if (amt_read != p->sf.hdrsize) {
781 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
782 "error reading dump file: %s",
783 pcap_strerror(errno));
787 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
788 "truncated dump file; tried to read %d header bytes, only got %lu",
789 p->sf.hdrsize, (unsigned long)amt_read);
798 /* these were written in opposite byte order */
799 hdr->caplen = SWAPLONG(sf_hdr.caplen);
800 hdr->len = SWAPLONG(sf_hdr.len);
801 hdr->ts.tv_sec = SWAPLONG(sf_hdr.ts.tv_sec);
802 hdr->ts.tv_usec = SWAPLONG(sf_hdr.ts.tv_usec);
804 hdr->caplen = sf_hdr.caplen;
805 hdr->len = sf_hdr.len;
806 hdr->ts.tv_sec = sf_hdr.ts.tv_sec;
807 hdr->ts.tv_usec = sf_hdr.ts.tv_usec;
809 /* Swap the caplen and len fields, if necessary. */
810 switch (p->sf.lengths_swapped) {
816 if (hdr->caplen <= hdr->len) {
818 * The captured length is <= the actual length,
819 * so presumably they weren't swapped.
827 hdr->caplen = hdr->len;
832 if (hdr->caplen > buflen) {
834 * This can happen due to Solaris 2.3 systems tripping
835 * over the BUFMOD problem and not setting the snapshot
836 * correctly in the savefile header. If the caplen isn't
837 * grossly wrong, try to salvage.
839 static u_char *tp = NULL;
840 static size_t tsize = 0;
842 if (hdr->caplen > 65535) {
843 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
844 "bogus savefile header");
848 if (tsize < hdr->caplen) {
849 tsize = ((hdr->caplen + 1023) / 1024) * 1024;
852 tp = (u_char *)malloc(tsize);
855 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
856 "BUFMOD hack malloc");
860 amt_read = fread((char *)tp, 1, hdr->caplen, fp);
861 if (amt_read != hdr->caplen) {
863 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
864 "error reading dump file: %s",
865 pcap_strerror(errno));
867 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
868 "truncated dump file; tried to read %u captured bytes, only got %lu",
869 hdr->caplen, (unsigned long)amt_read);
874 * We can only keep up to buflen bytes. Since caplen > buflen
875 * is exactly how we got here, we know we can only keep the
876 * first buflen bytes and must drop the remainder. Adjust
877 * caplen accordingly, so we don't get confused later as
878 * to how many bytes we have to play with.
880 hdr->caplen = buflen;
881 memcpy((char *)buf, (char *)tp, buflen);
884 /* read the packet itself */
885 amt_read = fread((char *)buf, 1, hdr->caplen, fp);
886 if (amt_read != hdr->caplen) {
888 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
889 "error reading dump file: %s",
890 pcap_strerror(errno));
892 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
893 "truncated dump file; tried to read %u captured bytes, only got %lu",
894 hdr->caplen, (unsigned long)amt_read);
903 * Print out packets stored in the file initialized by sf_read_init().
904 * If cnt > 0, return after 'cnt' packets, otherwise continue until eof.
907 pcap_offline_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
909 struct bpf_insn *fcode = p->fcode.bf_insns;
913 while (status == 0) {
914 struct pcap_pkthdr h;
917 * Has "pcap_breakloop()" been called?
918 * If so, return immediately - if we haven't read any
919 * packets, clear the flag and return -2 to indicate
920 * that we were told to break out of the loop, otherwise
921 * leave the flag set, so that the *next* call will break
922 * out of the loop without having read any packets, and
923 * return the number of packets we've processed so far.
933 status = sf_next_packet(p, &h, p->buffer, p->bufsize);
941 bpf_filter(fcode, p->buffer, h.len, h.caplen)) {
942 (*callback)(user, &h, p->buffer);
943 if (++n >= cnt && cnt > 0)
947 /*XXX this breaks semantics tcpslice expects */
952 * Output a packet to the initialized dump file.
955 pcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
958 struct pcap_sf_pkthdr sf_hdr;
961 sf_hdr.ts.tv_sec = h->ts.tv_sec;
962 sf_hdr.ts.tv_usec = h->ts.tv_usec;
963 sf_hdr.caplen = h->caplen;
965 /* XXX we should check the return status */
966 (void)fwrite(&sf_hdr, sizeof(sf_hdr), 1, f);
967 (void)fwrite((char *)sp, h->caplen, 1, f);
971 * Initialize so that sf_write() will output to the file named 'fname'.
974 pcap_dump_open(pcap_t *p, const char *fname)
979 linktype = dlt_to_linktype(p->linktype);
980 if (linktype == -1) {
981 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
982 "%s: link-layer type %d isn't supported in savefiles",
987 if (fname[0] == '-' && fname[1] == '\0') {
990 _setmode(_fileno(f), _O_BINARY);
994 f = fopen(fname, "w");
996 f = fopen(fname, "wb");
997 setbuf(f, NULL); /* XXX - why? */
1000 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s",
1001 fname, pcap_strerror(errno));
1005 (void)sf_write_header(f, linktype, p->tzoff, p->snapshot);
1006 return ((pcap_dumper_t *)f);
1010 pcap_dump_file(pcap_dumper_t *p)
1016 pcap_dump_flush(pcap_dumper_t *p)
1019 if (fflush((FILE *)p) == EOF)
1026 pcap_dump_close(pcap_dumper_t *p)
1030 if (ferror((FILE *)p))
1032 /* XXX should check return from fclose() too */
1034 (void)fclose((FILE *)p);