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.119 2004-12-18 08:52:11 guy Exp $ (LBL)";
48 #ifdef HAVE_OS_PROTO_H
53 * Standard libpcap format.
55 #define TCPDUMP_MAGIC 0xa1b2c3d4
58 * Alexey Kuznetzov's modified libpcap format.
60 #define KUZNETZOV_TCPDUMP_MAGIC 0xa1b2cd34
63 * Reserved for Francisco Mesquita <francisco.mesquita@radiomovel.pt>
64 * for another modified format.
66 #define FMESQUITA_TCPDUMP_MAGIC 0xa1b234cd
69 * We use the "receiver-makes-right" approach to byte order,
70 * because time is at a premium when we are writing the file.
71 * In other words, the pcap_file_header and pcap_pkthdr,
72 * records are written in host byte order.
73 * Note that the bytes of packet data are written out in the order in
74 * which they were received, so multi-byte fields in packets are not
75 * written in host byte order, they're written in whatever order the
76 * sending machine put them in.
78 * ntoh[ls] aren't sufficient because we might need to swap on a big-endian
79 * machine (if the file was written in little-end order).
82 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
83 #define SWAPSHORT(y) \
84 ( (((y)&0xff)<<8) | ((u_short)((y)&0xff00)>>8) )
87 #define SFERR_BADVERSION 2
89 #define SFERR_EOF 4 /* not really an error, just a status */
92 * Setting O_BINARY on DOS/Windows is a bit tricky
95 #define SET_BINMODE(f) _setmode(fileno(f), O_BINARY)
97 #if defined(__HIGHC__)
98 #define SET_BINMODE(f) setmode(f, O_BINARY)
100 #define SET_BINMODE(f) setmode(fileno(f), O_BINARY)
105 * We don't write DLT_* values to the capture file header, because
106 * they're not the same on all platforms.
108 * Unfortunately, the various flavors of BSD have not always used the same
109 * numerical values for the same data types, and various patches to
110 * libpcap for non-BSD OSes have added their own DLT_* codes for link
111 * layer encapsulation types seen on those OSes, and those codes have had,
112 * in some cases, values that were also used, on other platforms, for other
113 * link layer encapsulation types.
115 * This means that capture files of a type whose numerical DLT_* code
116 * means different things on different BSDs, or with different versions
117 * of libpcap, can't always be read on systems other than those like
118 * the one running on the machine on which the capture was made.
120 * Instead, we define here a set of LINKTYPE_* codes, and map DLT_* codes
121 * to LINKTYPE_* codes when writing a savefile header, and map LINKTYPE_*
122 * codes to DLT_* codes when reading a savefile header.
124 * For those DLT_* codes that have, as far as we know, the same values on
125 * all platforms (DLT_NULL through DLT_FDDI), we define LINKTYPE_xxx as
126 * DLT_xxx; that way, captures of those types can still be read by
127 * versions of libpcap that map LINKTYPE_* values to DLT_* values, and
128 * captures of those types written by versions of libpcap that map DLT_
129 * values to LINKTYPE_ values can still be read by older versions
132 * The other LINKTYPE_* codes are given values starting at 100, in the
133 * hopes that no DLT_* code will be given one of those values.
135 * In order to ensure that a given LINKTYPE_* code's value will refer to
136 * the same encapsulation type on all platforms, you should not allocate
137 * a new LINKTYPE_* value without consulting "tcpdump-workers@tcpdump.org".
138 * The tcpdump developers will allocate a value for you, and will not
139 * subsequently allocate it to anybody else; that value will be added to
140 * the "pcap.h" in the tcpdump.org CVS repository, so that a future
141 * libpcap release will include it.
143 * You should, if possible, also contribute patches to libpcap and tcpdump
144 * to handle the new encapsulation type, so that they can also be checked
145 * into the tcpdump.org CVS repository and so that they will appear in
146 * future libpcap and tcpdump releases.
148 * Do *NOT* assume that any values after the largest value in this file
149 * are available; you might not have the most up-to-date version of this
150 * file, and new values after that one might have been assigned. Also,
151 * do *NOT* use any values below 100 - those might already have been
152 * taken by one (or more!) organizations.
154 #define LINKTYPE_NULL DLT_NULL
155 #define LINKTYPE_ETHERNET DLT_EN10MB /* also for 100Mb and up */
156 #define LINKTYPE_EXP_ETHERNET DLT_EN3MB /* 3Mb experimental Ethernet */
157 #define LINKTYPE_AX25 DLT_AX25
158 #define LINKTYPE_PRONET DLT_PRONET
159 #define LINKTYPE_CHAOS DLT_CHAOS
160 #define LINKTYPE_TOKEN_RING DLT_IEEE802 /* DLT_IEEE802 is used for Token Ring */
161 #define LINKTYPE_ARCNET DLT_ARCNET /* BSD-style headers */
162 #define LINKTYPE_SLIP DLT_SLIP
163 #define LINKTYPE_PPP DLT_PPP
164 #define LINKTYPE_FDDI DLT_FDDI
167 * LINKTYPE_PPP is for use when there might, or might not, be an RFC 1662
168 * PPP in HDLC-like framing header (with 0xff 0x03 before the PPP protocol
169 * field) at the beginning of the packet.
171 * This is for use when there is always such a header; the address field
172 * might be 0xff, for regular PPP, or it might be an address field for Cisco
173 * point-to-point with HDLC framing as per section 4.3.1 of RFC 1547 ("Cisco
174 * HDLC"). This is, for example, what you get with NetBSD's DLT_PPP_SERIAL.
176 * We give it the same value as NetBSD's DLT_PPP_SERIAL, in the hopes that
177 * nobody else will choose a DLT_ value of 50, and so that DLT_PPP_SERIAL
178 * captures will be written out with a link type that NetBSD's tcpdump
181 #define LINKTYPE_PPP_HDLC 50 /* PPP in HDLC-like framing */
183 #define LINKTYPE_PPP_ETHER 51 /* NetBSD PPP-over-Ethernet */
185 #define LINKTYPE_SYMANTEC_FIREWALL 99 /* Symantec Enterprise Firewall */
187 #define LINKTYPE_ATM_RFC1483 100 /* LLC/SNAP-encapsulated ATM */
188 #define LINKTYPE_RAW 101 /* raw IP */
189 #define LINKTYPE_SLIP_BSDOS 102 /* BSD/OS SLIP BPF header */
190 #define LINKTYPE_PPP_BSDOS 103 /* BSD/OS PPP BPF header */
191 #define LINKTYPE_C_HDLC 104 /* Cisco HDLC */
192 #define LINKTYPE_IEEE802_11 105 /* IEEE 802.11 (wireless) */
193 #define LINKTYPE_ATM_CLIP 106 /* Linux Classical IP over ATM */
194 #define LINKTYPE_FRELAY 107 /* Frame Relay */
195 #define LINKTYPE_LOOP 108 /* OpenBSD loopback */
196 #define LINKTYPE_ENC 109 /* OpenBSD IPSEC enc */
199 * These three types are reserved for future use.
201 #define LINKTYPE_LANE8023 110 /* ATM LANE + 802.3 */
202 #define LINKTYPE_HIPPI 111 /* NetBSD HIPPI */
203 #define LINKTYPE_HDLC 112 /* NetBSD HDLC framing */
205 #define LINKTYPE_LINUX_SLL 113 /* Linux cooked socket capture */
206 #define LINKTYPE_LTALK 114 /* Apple LocalTalk hardware */
207 #define LINKTYPE_ECONET 115 /* Acorn Econet */
210 * Reserved for use with OpenBSD ipfilter.
212 #define LINKTYPE_IPFILTER 116
214 #define LINKTYPE_PFLOG 117 /* OpenBSD DLT_PFLOG */
215 #define LINKTYPE_CISCO_IOS 118 /* For Cisco-internal use */
216 #define LINKTYPE_PRISM_HEADER 119 /* 802.11+Prism II monitor mode */
217 #define LINKTYPE_AIRONET_HEADER 120 /* FreeBSD Aironet driver stuff */
220 * Reserved for Siemens HiPath HDLC.
222 #define LINKTYPE_HHDLC 121
224 #define LINKTYPE_IP_OVER_FC 122 /* RFC 2625 IP-over-Fibre Channel */
225 #define LINKTYPE_SUNATM 123 /* Solaris+SunATM */
228 * Reserved as per request from Kent Dahlgren <kent@praesum.com>
231 #define LINKTYPE_RIO 124 /* RapidIO */
232 #define LINKTYPE_PCI_EXP 125 /* PCI Express */
233 #define LINKTYPE_AURORA 126 /* Xilinx Aurora link layer */
235 #define LINKTYPE_IEEE802_11_RADIO 127 /* 802.11 plus BSD radio header */
238 * Reserved for the TZSP encapsulation, as per request from
239 * Chris Waters <chris.waters@networkchemistry.com>
240 * TZSP is a generic encapsulation for any other link type,
241 * which includes a means to include meta-information
242 * with the packet, e.g. signal strength and channel
243 * for 802.11 packets.
245 #define LINKTYPE_TZSP 128 /* Tazmen Sniffer Protocol */
247 #define LINKTYPE_ARCNET_LINUX 129 /* Linux-style headers */
250 * Juniper-private data link types, as per request from
251 * Hannes Gredler <hannes@juniper.net>. The corresponding
252 * DLT_s are used for passing on chassis-internal
253 * metainformation such as QOS profiles, etc..
255 #define LINKTYPE_JUNIPER_MLPPP 130
256 #define LINKTYPE_JUNIPER_MLFR 131
257 #define LINKTYPE_JUNIPER_ES 132
258 #define LINKTYPE_JUNIPER_GGSN 133
259 #define LINKTYPE_JUNIPER_MFR 134
260 #define LINKTYPE_JUNIPER_ATM2 135
261 #define LINKTYPE_JUNIPER_SERVICES 136
262 #define LINKTYPE_JUNIPER_ATM1 137
264 #define LINKTYPE_APPLE_IP_OVER_IEEE1394 138 /* Apple IP-over-IEEE 1394 cooked header */
266 #define LINKTYPE_RAWSS7 139 /* see rawss7.h for */
267 #define LINKTYPE_RAWSS7_MTP2 140 /* information on these */
268 #define LINKTYPE_RAWSS7_MTP3 141 /* definitions */
269 #define LINKTYPE_RAWSS7_SCCP 142
271 #define LINKTYPE_DOCSIS 143 /* DOCSIS MAC frames */
273 #define LINKTYPE_LINUX_IRDA 144 /* Linux-IrDA */
276 * Reserved for IBM SP switch and IBM Next Federation switch.
278 #define LINKTYPE_IBM_SP 145
279 #define LINKTYPE_IBM_SN 146
282 * Reserved for private use. If you have some link-layer header type
283 * that you want to use within your organization, with the capture files
284 * using that link-layer header type not ever be sent outside your
285 * organization, you can use these values.
287 * No libpcap release will use these for any purpose, nor will any
288 * tcpdump release use them, either.
290 * Do *NOT* use these in capture files that you expect anybody not using
291 * your private versions of capture-file-reading tools to read; in
292 * particular, do *NOT* use them in products, otherwise you may find that
293 * people won't be able to use tcpdump, or snort, or Ethereal, or... to
294 * read capture files from your firewall/intrusion detection/traffic
295 * monitoring/etc. appliance, or whatever product uses that LINKTYPE_ value,
296 * and you may also find that the developers of those applications will
297 * not accept patches to let them read those files.
299 * Also, do not use them if somebody might send you a capture using them
300 * for *their* private type and tools using them for *your* private type
301 * would have to read them.
303 * Instead, in those cases, ask "tcpdump-workers@tcpdump.org" for a new DLT_
304 * and LINKTYPE_ value, as per the comment in pcap-bpf.h, and use the type
307 #define LINKTYPE_USER0 147
308 #define LINKTYPE_USER1 148
309 #define LINKTYPE_USER2 149
310 #define LINKTYPE_USER3 150
311 #define LINKTYPE_USER4 151
312 #define LINKTYPE_USER5 152
313 #define LINKTYPE_USER6 153
314 #define LINKTYPE_USER7 154
315 #define LINKTYPE_USER8 155
316 #define LINKTYPE_USER9 156
317 #define LINKTYPE_USER10 157
318 #define LINKTYPE_USER11 158
319 #define LINKTYPE_USER12 159
320 #define LINKTYPE_USER13 160
321 #define LINKTYPE_USER14 161
322 #define LINKTYPE_USER15 162
325 * For future use with 802.11 captures - defined by AbsoluteValue
326 * Systems to store a number of bits of link-layer information
327 * including radio information:
329 * http://www.shaftnet.org/~pizza/software/capturefrm.txt
331 * but could and arguably should also be used by non-AVS Linux
332 * 802.11 drivers; that may happen in the future.
334 #define LINKTYPE_IEEE802_11_RADIO_AVS 163 /* 802.11 plus AVS radio header */
337 * Juniper-private data link type, as per request from
338 * Hannes Gredler <hannes@juniper.net>. The corresponding
339 * DLT_s are used for passing on chassis-internal
340 * metainformation such as QOS profiles, etc..
342 #define LINKTYPE_JUNIPER_MONITOR 164
345 * Reserved for BACnet MS/TP.
347 #define LINKTYPE_BACNET_MS_TP 165
350 * another PPP variant as per request from Karsten Keil <kkeil@suse.de>
351 * the first byte (0xff) of the PPP header (0xff03) is tweaked to accomodate
352 * the direction 0x00 = IN, 0x01 = OUT
354 #define LINKTYPE_PPP_WITHDIRECTION 166
357 * Juniper-private data link type, as per request from
358 * Hannes Gredler <hannes@juniper.net>. The DLT_s are used
359 * for passing on chassis-internal metainformation such as
360 * QOS profiles, cookies, etc..
362 #define LINKTYPE_JUNIPER_PPPOE 167
363 #define LINKTYPE_JUNIPER_PPPOE_ATM 168
365 #define LINKTYPE_GPRS_LLC 169 /* GPRS LLC */
366 #define LINKTYPE_GPF_T 170 /* GPF-T (ITU-T G.7041/Y.1303) */
367 #define LINKTYPE_GPF_F 171 /* GPF-T (ITU-T G.7041/Y.1303) */
369 static struct linktype_map
{
374 * These DLT_* codes have LINKTYPE_* codes with values identical
375 * to the values of the corresponding DLT_* code.
377 { DLT_NULL
, LINKTYPE_NULL
},
378 { DLT_EN10MB
, LINKTYPE_ETHERNET
},
379 { DLT_EN3MB
, LINKTYPE_EXP_ETHERNET
},
380 { DLT_AX25
, LINKTYPE_AX25
},
381 { DLT_PRONET
, LINKTYPE_PRONET
},
382 { DLT_CHAOS
, LINKTYPE_CHAOS
},
383 { DLT_IEEE802
, LINKTYPE_TOKEN_RING
},
384 { DLT_ARCNET
, LINKTYPE_ARCNET
},
385 { DLT_SLIP
, LINKTYPE_SLIP
},
386 { DLT_PPP
, LINKTYPE_PPP
},
387 { DLT_FDDI
, LINKTYPE_FDDI
},
390 * These DLT_* codes have different values on different
391 * platforms; we map them to LINKTYPE_* codes that
392 * have values that should never be equal to any DLT_*
396 /* BSD/OS Frame Relay */
397 { DLT_FR
, LINKTYPE_FRELAY
},
400 { DLT_SYMANTEC_FIREWALL
, LINKTYPE_SYMANTEC_FIREWALL
},
401 { DLT_ATM_RFC1483
, LINKTYPE_ATM_RFC1483
},
402 { DLT_RAW
, LINKTYPE_RAW
},
403 { DLT_SLIP_BSDOS
, LINKTYPE_SLIP_BSDOS
},
404 { DLT_PPP_BSDOS
, LINKTYPE_PPP_BSDOS
},
406 /* BSD/OS Cisco HDLC */
407 { DLT_C_HDLC
, LINKTYPE_C_HDLC
},
410 * These DLT_* codes are not on all platforms, but, so far,
411 * there don't appear to be any platforms that define
412 * other codes with those values; we map them to
413 * different LINKTYPE_* values anyway, just in case.
416 /* Linux ATM Classical IP */
417 { DLT_ATM_CLIP
, LINKTYPE_ATM_CLIP
},
419 /* NetBSD sync/async serial PPP (or Cisco HDLC) */
420 { DLT_PPP_SERIAL
, LINKTYPE_PPP_HDLC
},
422 /* NetBSD PPP over Ethernet */
423 { DLT_PPP_ETHER
, LINKTYPE_PPP_ETHER
},
425 /* IEEE 802.11 wireless */
426 { DLT_IEEE802_11
, LINKTYPE_IEEE802_11
},
429 { DLT_FRELAY
, LINKTYPE_FRELAY
},
431 /* OpenBSD loopback */
432 { DLT_LOOP
, LINKTYPE_LOOP
},
434 /* Linux cooked socket capture */
435 { DLT_LINUX_SLL
, LINKTYPE_LINUX_SLL
},
437 /* Apple LocalTalk hardware */
438 { DLT_LTALK
, LINKTYPE_LTALK
},
441 { DLT_ECONET
, LINKTYPE_ECONET
},
443 /* OpenBSD DLT_PFLOG */
444 { DLT_PFLOG
, LINKTYPE_PFLOG
},
446 /* For Cisco-internal use */
447 { DLT_CISCO_IOS
, LINKTYPE_CISCO_IOS
},
449 /* Prism II monitor-mode header plus 802.11 header */
450 { DLT_PRISM_HEADER
, LINKTYPE_PRISM_HEADER
},
452 /* FreeBSD Aironet driver stuff */
453 { DLT_AIRONET_HEADER
, LINKTYPE_AIRONET_HEADER
},
455 /* Siemens HiPath HDLC */
456 { DLT_HHDLC
, LINKTYPE_HHDLC
},
458 /* RFC 2625 IP-over-Fibre Channel */
459 { DLT_IP_OVER_FC
, LINKTYPE_IP_OVER_FC
},
462 { DLT_SUNATM
, LINKTYPE_SUNATM
},
465 { DLT_RIO
, LINKTYPE_RIO
},
468 { DLT_PCI_EXP
, LINKTYPE_PCI_EXP
},
470 /* Xilinx Aurora link layer */
471 { DLT_AURORA
, LINKTYPE_AURORA
},
473 /* 802.11 plus BSD radio header */
474 { DLT_IEEE802_11_RADIO
, LINKTYPE_IEEE802_11_RADIO
},
476 /* Tazmen Sniffer Protocol */
477 { DLT_TZSP
, LINKTYPE_TZSP
},
479 /* Arcnet with Linux-style link-layer headers */
480 { DLT_ARCNET_LINUX
, LINKTYPE_ARCNET_LINUX
},
482 /* Juniper-internal chassis encapsulation */
483 { DLT_JUNIPER_MLPPP
, LINKTYPE_JUNIPER_MLPPP
},
484 { DLT_JUNIPER_MLFR
, LINKTYPE_JUNIPER_MLFR
},
485 { DLT_JUNIPER_ES
, LINKTYPE_JUNIPER_ES
},
486 { DLT_JUNIPER_GGSN
, LINKTYPE_JUNIPER_GGSN
},
487 { DLT_JUNIPER_MFR
, LINKTYPE_JUNIPER_MFR
},
488 { DLT_JUNIPER_ATM2
, LINKTYPE_JUNIPER_ATM2
},
489 { DLT_JUNIPER_SERVICES
, LINKTYPE_JUNIPER_SERVICES
},
490 { DLT_JUNIPER_ATM1
, LINKTYPE_JUNIPER_ATM1
},
492 /* Apple IP-over-IEEE 1394 cooked header */
493 { DLT_APPLE_IP_OVER_IEEE1394
, LINKTYPE_APPLE_IP_OVER_IEEE1394
},
495 /* DOCSIS MAC frames */
496 { DLT_DOCSIS
, LINKTYPE_DOCSIS
},
498 /* IrDA IrLAP packets + Linux-cooked header */
499 { DLT_LINUX_IRDA
, LINKTYPE_LINUX_IRDA
},
501 /* IBM SP and Next Federation switches */
502 { DLT_IBM_SP
, LINKTYPE_IBM_SP
},
503 { DLT_IBM_SN
, LINKTYPE_IBM_SN
},
505 /* 802.11 plus AVS radio header */
506 { DLT_IEEE802_11_RADIO_AVS
, LINKTYPE_IEEE802_11_RADIO_AVS
},
509 * Any platform that defines additional DLT_* codes should:
511 * request a LINKTYPE_* code and value from tcpdump.org,
514 * add, in their version of libpcap, an entry to map
515 * those DLT_* codes to the corresponding LINKTYPE_*
518 * redefine, in their "net/bpf.h", any DLT_* values
519 * that collide with the values used by their additional
520 * DLT_* codes, to remove those collisions (but without
521 * making them collide with any of the LINKTYPE_*
522 * values equal to 50 or above; they should also avoid
523 * defining DLT_* values that collide with those
524 * LINKTYPE_* values, either).
527 /* Juniper-internal chassis encapsulation */
528 { DLT_JUNIPER_MONITOR
, LINKTYPE_JUNIPER_MONITOR
},
531 { DLT_BACNET_MS_TP
, LINKTYPE_BACNET_MS_TP
},
533 /* PPP with direction flag in the PPP header */
534 { DLT_PPP_WITHDIRECTION
,LINKTYPE_PPP_WITHDIRECTION
},
536 /* Juniper-internal chassis encapsulation */
537 { DLT_JUNIPER_PPPOE
, LINKTYPE_JUNIPER_PPPOE
},
538 { DLT_JUNIPER_PPPOE_ATM
,LINKTYPE_JUNIPER_PPPOE_ATM
},
541 { DLT_GPRS_LLC
, LINKTYPE_GPRS_LLC
},
543 /* Transparent Generic Framing Procedure (ITU-T G.7041/Y.1303) */
544 { DLT_GPF_T
, LINKTYPE_GPF_T
},
546 /* Framed Generic Framing Procedure (ITU-T G.7041/Y.1303) */
547 { DLT_GPF_F
, LINKTYPE_GPF_F
},
553 dlt_to_linktype(int dlt
)
557 for (i
= 0; map
[i
].dlt
!= -1; i
++) {
558 if (map
[i
].dlt
== dlt
)
559 return (map
[i
].linktype
);
563 * If we don't have a mapping for this DLT_ code, return an
564 * error; that means that the table above needs to have an
571 linktype_to_dlt(int linktype
)
575 for (i
= 0; map
[i
].linktype
!= -1; i
++) {
576 if (map
[i
].linktype
== linktype
)
581 * If we don't have an entry for this link type, return
582 * the link type value; it may be a DLT_ value from an
583 * older version of libpcap.
589 sf_write_header(FILE *fp
, int linktype
, int thiszone
, int snaplen
)
591 struct pcap_file_header hdr
;
593 hdr
.magic
= TCPDUMP_MAGIC
;
594 hdr
.version_major
= PCAP_VERSION_MAJOR
;
595 hdr
.version_minor
= PCAP_VERSION_MINOR
;
597 hdr
.thiszone
= thiszone
;
598 hdr
.snaplen
= snaplen
;
600 hdr
.linktype
= linktype
;
602 if (fwrite((char *)&hdr
, sizeof(hdr
), 1, fp
) != 1)
609 swap_hdr(struct pcap_file_header
*hp
)
611 hp
->version_major
= SWAPSHORT(hp
->version_major
);
612 hp
->version_minor
= SWAPSHORT(hp
->version_minor
);
613 hp
->thiszone
= SWAPLONG(hp
->thiszone
);
614 hp
->sigfigs
= SWAPLONG(hp
->sigfigs
);
615 hp
->snaplen
= SWAPLONG(hp
->snaplen
);
616 hp
->linktype
= SWAPLONG(hp
->linktype
);
620 sf_getnonblock(pcap_t
*p
, char *errbuf
)
623 * This is a savefile, not a live capture file, so never say
624 * it's in non-blocking mode.
630 sf_setnonblock(pcap_t
*p
, int nonblock
, char *errbuf
)
633 * This is a savefile, not a live capture file, so ignore
634 * requests to put it in non-blocking mode.
640 sf_stats(pcap_t
*p
, struct pcap_stat
*ps
)
642 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
643 "Statistics aren't available from savefiles");
648 sf_inject(pcap_t
*p
, const void *buf _U_
, size_t size _U_
)
650 strlcpy(p
->errbuf
, "Sending packets isn't supported on savefiles",
658 if (p
->sf
.rfile
!= stdin
)
659 (void)fclose(p
->sf
.rfile
);
660 if (p
->sf
.base
!= NULL
)
665 pcap_open_offline(const char *fname
, char *errbuf
)
670 if (fname
[0] == '-' && fname
[1] == '\0')
673 #if !defined(WIN32) && !defined(MSDOS)
674 fp
= fopen(fname
, "r");
676 fp
= fopen(fname
, "rb");
679 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "%s: %s", fname
,
680 pcap_strerror(errno
));
684 p
= pcap_fopen_offline(fp
, errbuf
);
693 pcap_fopen_offline(FILE *fp
, char *errbuf
)
696 struct pcap_file_header hdr
;
701 p
= (pcap_t
*)malloc(sizeof(*p
));
703 strlcpy(errbuf
, "out of swap", PCAP_ERRBUF_SIZE
);
707 memset((char *)p
, 0, sizeof(*p
));
709 amt_read
= fread((char *)&hdr
, 1, sizeof(hdr
), fp
);
710 if (amt_read
!= sizeof(hdr
)) {
712 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
713 "error reading dump file: %s",
714 pcap_strerror(errno
));
716 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
717 "truncated dump file; tried to read %lu file header bytes, only got %lu",
718 sizeof(hdr
), (unsigned long)amt_read
);
723 if (magic
!= TCPDUMP_MAGIC
&& magic
!= KUZNETZOV_TCPDUMP_MAGIC
) {
724 magic
= SWAPLONG(magic
);
725 if (magic
!= TCPDUMP_MAGIC
&& magic
!= KUZNETZOV_TCPDUMP_MAGIC
) {
726 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
727 "bad dump file format");
733 if (magic
== KUZNETZOV_TCPDUMP_MAGIC
) {
735 * XXX - the patch that's in some versions of libpcap
736 * changes the packet header but not the magic number,
737 * and some other versions with this magic number have
738 * some extra debugging information in the packet header;
739 * we'd have to use some hacks^H^H^H^H^Hheuristics to
740 * detect those variants.
742 * Ethereal does that, but it does so by trying to read
743 * the first two packets of the file with each of the
744 * record header formats. That currently means it seeks
745 * backwards and retries the reads, which doesn't work
746 * on pipes. We want to be able to read from a pipe, so
747 * that strategy won't work; we'd have to buffer some
748 * data ourselves and read from that buffer in order to
751 p
->sf
.hdrsize
= sizeof(struct pcap_sf_patched_pkthdr
);
753 p
->sf
.hdrsize
= sizeof(struct pcap_sf_pkthdr
);
754 if (hdr
.version_major
< PCAP_VERSION_MAJOR
) {
755 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "archaic file format");
758 p
->tzoff
= hdr
.thiszone
;
759 p
->snapshot
= hdr
.snaplen
;
760 p
->linktype
= linktype_to_dlt(hdr
.linktype
);
763 p
->bufsize
= hdr
.snaplen
;
765 /* Allocate the space for pcap_pkthdr as well. It will be used by pcap_read_ex */
766 p
->bufsize
= hdr
.snaplen
+sizeof(struct pcap_pkthdr
);
769 /* Align link header as required for proper data alignment */
770 /* XXX should handle all types */
771 switch (p
->linktype
) {
778 linklen
= 13 + 8; /* fddi_header + llc */
788 p
->bufsize
= BPF_MAXBUFSIZE
;
789 p
->sf
.base
= (u_char
*)malloc(p
->bufsize
+ BPF_ALIGNMENT
);
790 if (p
->sf
.base
== NULL
) {
791 strlcpy(errbuf
, "out of swap", PCAP_ERRBUF_SIZE
);
794 p
->buffer
= p
->sf
.base
+ BPF_ALIGNMENT
- (linklen
% BPF_ALIGNMENT
);
795 p
->sf
.version_major
= hdr
.version_major
;
796 p
->sf
.version_minor
= hdr
.version_minor
;
798 /* Padding only needed for live capture fcode */
803 * We interchanged the caplen and len fields at version 2.3,
804 * in order to match the bpf header layout. But unfortunately
805 * some files were written with version 2.3 in their headers
806 * but without the interchanged fields.
808 * In addition, DG/UX tcpdump writes out files with a version
809 * number of 543.0, and with the caplen and len fields in the
812 switch (hdr
.version_major
) {
815 if (hdr
.version_minor
< 3)
816 p
->sf
.lengths_swapped
= SWAPPED
;
817 else if (hdr
.version_minor
== 3)
818 p
->sf
.lengths_swapped
= MAYBE_SWAPPED
;
820 p
->sf
.lengths_swapped
= NOT_SWAPPED
;
824 p
->sf
.lengths_swapped
= SWAPPED
;
828 p
->sf
.lengths_swapped
= NOT_SWAPPED
;
832 #if !defined(WIN32) && !defined(MSDOS)
834 * You can do "select()" and "poll()" on plain files on most
835 * platforms, and should be able to do so on pipes.
837 * You can't do "select()" on anything other than sockets in
838 * Windows, so, on Win32 systems, we don't have "selectable_fd".
840 p
->selectable_fd
= fileno(fp
);
843 p
->read_op
= pcap_offline_read
;
844 p
->inject_op
= sf_inject
;
845 p
->setfilter_op
= install_bpf_program
;
846 p
->set_datalink_op
= NULL
; /* we don't support munging link-layer headers */
847 p
->getnonblock_op
= sf_getnonblock
;
848 p
->setnonblock_op
= sf_setnonblock
;
849 p
->stats_op
= sf_stats
;
850 p
->close_op
= sf_close
;
852 #if defined(WIN32) || defined(MSDOS)
854 * If we're reading from the standard input, put it in binary
855 * mode, as savefiles are binary files.
868 * Read sf_readfile and return the next packet. Return the header in hdr
869 * and the contents in buf. Return 0 on success, SFERR_EOF if there were
870 * no more packets, and SFERR_TRUNC if a partial packet was encountered.
873 sf_next_packet(pcap_t
*p
, struct pcap_pkthdr
*hdr
, u_char
*buf
, u_int buflen
)
875 struct pcap_sf_patched_pkthdr sf_hdr
;
876 FILE *fp
= p
->sf
.rfile
;
881 * Read the packet header; the structure we use as a buffer
882 * is the longer structure for files generated by the patched
883 * libpcap, but if the file has the magic number for an
884 * unpatched libpcap we only read as many bytes as the regular
887 amt_read
= fread(&sf_hdr
, 1, p
->sf
.hdrsize
, fp
);
888 if (amt_read
!= p
->sf
.hdrsize
) {
890 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
891 "error reading dump file: %s",
892 pcap_strerror(errno
));
896 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
897 "truncated dump file; tried to read %d header bytes, only got %lu",
898 p
->sf
.hdrsize
, (unsigned long)amt_read
);
907 /* these were written in opposite byte order */
908 hdr
->caplen
= SWAPLONG(sf_hdr
.caplen
);
909 hdr
->len
= SWAPLONG(sf_hdr
.len
);
910 hdr
->ts
.tv_sec
= SWAPLONG(sf_hdr
.ts
.tv_sec
);
911 hdr
->ts
.tv_usec
= SWAPLONG(sf_hdr
.ts
.tv_usec
);
913 hdr
->caplen
= sf_hdr
.caplen
;
914 hdr
->len
= sf_hdr
.len
;
915 hdr
->ts
.tv_sec
= sf_hdr
.ts
.tv_sec
;
916 hdr
->ts
.tv_usec
= sf_hdr
.ts
.tv_usec
;
918 /* Swap the caplen and len fields, if necessary. */
919 switch (p
->sf
.lengths_swapped
) {
925 if (hdr
->caplen
<= hdr
->len
) {
927 * The captured length is <= the actual length,
928 * so presumably they weren't swapped.
936 hdr
->caplen
= hdr
->len
;
941 if (hdr
->caplen
> buflen
) {
943 * This can happen due to Solaris 2.3 systems tripping
944 * over the BUFMOD problem and not setting the snapshot
945 * correctly in the savefile header. If the caplen isn't
946 * grossly wrong, try to salvage.
948 static u_char
*tp
= NULL
;
949 static size_t tsize
= 0;
951 if (hdr
->caplen
> 65535) {
952 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
953 "bogus savefile header");
957 if (tsize
< hdr
->caplen
) {
958 tsize
= ((hdr
->caplen
+ 1023) / 1024) * 1024;
961 tp
= (u_char
*)malloc(tsize
);
964 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
965 "BUFMOD hack malloc");
969 amt_read
= fread((char *)tp
, 1, hdr
->caplen
, fp
);
970 if (amt_read
!= hdr
->caplen
) {
972 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
973 "error reading dump file: %s",
974 pcap_strerror(errno
));
976 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
977 "truncated dump file; tried to read %u captured bytes, only got %lu",
978 hdr
->caplen
, (unsigned long)amt_read
);
983 * We can only keep up to buflen bytes. Since caplen > buflen
984 * is exactly how we got here, we know we can only keep the
985 * first buflen bytes and must drop the remainder. Adjust
986 * caplen accordingly, so we don't get confused later as
987 * to how many bytes we have to play with.
989 hdr
->caplen
= buflen
;
990 memcpy((char *)buf
, (char *)tp
, buflen
);
993 /* read the packet itself */
994 amt_read
= fread((char *)buf
, 1, hdr
->caplen
, fp
);
995 if (amt_read
!= hdr
->caplen
) {
997 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
998 "error reading dump file: %s",
999 pcap_strerror(errno
));
1001 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1002 "truncated dump file; tried to read %u captured bytes, only got %lu",
1003 hdr
->caplen
, (unsigned long)amt_read
);
1012 * Print out packets stored in the file initialized by sf_read_init().
1013 * If cnt > 0, return after 'cnt' packets, otherwise continue until eof.
1016 pcap_offline_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
1018 struct bpf_insn
*fcode
;
1022 while (status
== 0) {
1023 struct pcap_pkthdr h
;
1026 * Has "pcap_breakloop()" been called?
1027 * If so, return immediately - if we haven't read any
1028 * packets, clear the flag and return -2 to indicate
1029 * that we were told to break out of the loop, otherwise
1030 * leave the flag set, so that the *next* call will break
1031 * out of the loop without having read any packets, and
1032 * return the number of packets we've processed so far.
1034 if (p
->break_loop
) {
1042 status
= sf_next_packet(p
, &h
, p
->buffer
, p
->bufsize
);
1049 if ((fcode
= p
->fcode
.bf_insns
) == NULL
||
1050 bpf_filter(fcode
, p
->buffer
, h
.len
, h
.caplen
)) {
1051 (*callback
)(user
, &h
, p
->buffer
);
1052 if (++n
>= cnt
&& cnt
> 0)
1056 /*XXX this breaks semantics tcpslice expects */
1061 * Output a packet to the initialized dump file.
1064 pcap_dump(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1067 struct pcap_sf_pkthdr sf_hdr
;
1070 sf_hdr
.ts
.tv_sec
= h
->ts
.tv_sec
;
1071 sf_hdr
.ts
.tv_usec
= h
->ts
.tv_usec
;
1072 sf_hdr
.caplen
= h
->caplen
;
1073 sf_hdr
.len
= h
->len
;
1074 /* XXX we should check the return status */
1075 (void)fwrite(&sf_hdr
, sizeof(sf_hdr
), 1, f
);
1076 (void)fwrite((char *)sp
, h
->caplen
, 1, f
);
1079 static pcap_dumper_t
*
1080 pcap_setup_dump(pcap_t
*p
, int linktype
, FILE *f
, const char *fname
)
1083 #if defined(WIN32) || defined(MSDOS)
1085 * If we're writing to the standard output, put it in binary
1086 * mode, as savefiles are binary files.
1088 * Otherwise, we turn off buffering.
1089 * XXX - why? And why not on the standard output?
1096 if (sf_write_header(f
, linktype
, p
->tzoff
, p
->snapshot
) == -1) {
1097 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Can't write to %s: %s",
1098 fname
, pcap_strerror(errno
));
1103 return ((pcap_dumper_t
*)f
);
1107 * Initialize so that sf_write() will output to the file named 'fname'.
1110 pcap_dump_open(pcap_t
*p
, const char *fname
)
1115 linktype
= dlt_to_linktype(p
->linktype
);
1116 if (linktype
== -1) {
1117 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1118 "%s: link-layer type %d isn't supported in savefiles",
1123 if (fname
[0] == '-' && fname
[1] == '\0') {
1125 fname
= "standard output";
1127 #if !defined(WIN32) && !defined(MSDOS)
1128 f
= fopen(fname
, "w");
1130 f
= fopen(fname
, "wb");
1133 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "%s: %s",
1134 fname
, pcap_strerror(errno
));
1138 return (pcap_setup_dump(p
, linktype
, f
, fname
));
1142 * Initialize so that sf_write() will output to the given stream.
1145 pcap_dump_fopen(pcap_t
*p
, FILE *f
)
1149 linktype
= dlt_to_linktype(p
->linktype
);
1150 if (linktype
== -1) {
1151 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1152 "stream: link-layer type %d isn't supported in savefiles",
1157 return (pcap_setup_dump(p
, linktype
, f
, "stream"));
1161 pcap_dump_file(pcap_dumper_t
*p
)
1167 pcap_dump_flush(pcap_dumper_t
*p
)
1170 if (fflush((FILE *)p
) == EOF
)
1177 pcap_dump_close(pcap_dumper_t
*p
)
1181 if (ferror((FILE *)p
))
1183 /* XXX should check return from fclose() too */
1185 (void)fclose((FILE *)p
);