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.110 2004-09-28 01:09:52 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 * We don't write DLT_* values to the capture file header, because
93 * they're not the same on all platforms.
95 * Unfortunately, the various flavors of BSD have not always used the same
96 * numerical values for the same data types, and various patches to
97 * libpcap for non-BSD OSes have added their own DLT_* codes for link
98 * layer encapsulation types seen on those OSes, and those codes have had,
99 * in some cases, values that were also used, on other platforms, for other
100 * link layer encapsulation types.
102 * This means that capture files of a type whose numerical DLT_* code
103 * means different things on different BSDs, or with different versions
104 * of libpcap, can't always be read on systems other than those like
105 * the one running on the machine on which the capture was made.
107 * Instead, we define here a set of LINKTYPE_* codes, and map DLT_* codes
108 * to LINKTYPE_* codes when writing a savefile header, and map LINKTYPE_*
109 * codes to DLT_* codes when reading a savefile header.
111 * For those DLT_* codes that have, as far as we know, the same values on
112 * all platforms (DLT_NULL through DLT_FDDI), we define LINKTYPE_xxx as
113 * DLT_xxx; that way, captures of those types can still be read by
114 * versions of libpcap that map LINKTYPE_* values to DLT_* values, and
115 * captures of those types written by versions of libpcap that map DLT_
116 * values to LINKTYPE_ values can still be read by older versions
119 * The other LINKTYPE_* codes are given values starting at 100, in the
120 * hopes that no DLT_* code will be given one of those values.
122 * In order to ensure that a given LINKTYPE_* code's value will refer to
123 * the same encapsulation type on all platforms, you should not allocate
124 * a new LINKTYPE_* value without consulting "tcpdump-workers@tcpdump.org".
125 * The tcpdump developers will allocate a value for you, and will not
126 * subsequently allocate it to anybody else; that value will be added to
127 * the "pcap.h" in the tcpdump.org CVS repository, so that a future
128 * libpcap release will include it.
130 * You should, if possible, also contribute patches to libpcap and tcpdump
131 * to handle the new encapsulation type, so that they can also be checked
132 * into the tcpdump.org CVS repository and so that they will appear in
133 * future libpcap and tcpdump releases.
135 * Do *NOT* assume that any values after the largest value in this file
136 * are available; you might not have the most up-to-date version of this
137 * file, and new values after that one might have been assigned. Also,
138 * do *NOT* use any values below 100 - those might already have been
139 * taken by one (or more!) organizations.
141 #define LINKTYPE_NULL DLT_NULL
142 #define LINKTYPE_ETHERNET DLT_EN10MB /* also for 100Mb and up */
143 #define LINKTYPE_EXP_ETHERNET DLT_EN3MB /* 3Mb experimental Ethernet */
144 #define LINKTYPE_AX25 DLT_AX25
145 #define LINKTYPE_PRONET DLT_PRONET
146 #define LINKTYPE_CHAOS DLT_CHAOS
147 #define LINKTYPE_TOKEN_RING DLT_IEEE802 /* DLT_IEEE802 is used for Token Ring */
148 #define LINKTYPE_ARCNET DLT_ARCNET /* BSD-style headers */
149 #define LINKTYPE_SLIP DLT_SLIP
150 #define LINKTYPE_PPP DLT_PPP
151 #define LINKTYPE_FDDI DLT_FDDI
154 * LINKTYPE_PPP is for use when there might, or might not, be an RFC 1662
155 * PPP in HDLC-like framing header (with 0xff 0x03 before the PPP protocol
156 * field) at the beginning of the packet.
158 * This is for use when there is always such a header; the address field
159 * might be 0xff, for regular PPP, or it might be an address field for Cisco
160 * point-to-point with HDLC framing as per section 4.3.1 of RFC 1547 ("Cisco
161 * HDLC"). This is, for example, what you get with NetBSD's DLT_PPP_SERIAL.
163 * We give it the same value as NetBSD's DLT_PPP_SERIAL, in the hopes that
164 * nobody else will choose a DLT_ value of 50, and so that DLT_PPP_SERIAL
165 * captures will be written out with a link type that NetBSD's tcpdump
168 #define LINKTYPE_PPP_HDLC 50 /* PPP in HDLC-like framing */
170 #define LINKTYPE_PPP_ETHER 51 /* NetBSD PPP-over-Ethernet */
172 #define LINKTYPE_SYMANTEC_FIREWALL 99 /* Symantec Enterprise Firewall */
174 #define LINKTYPE_ATM_RFC1483 100 /* LLC/SNAP-encapsulated ATM */
175 #define LINKTYPE_RAW 101 /* raw IP */
176 #define LINKTYPE_SLIP_BSDOS 102 /* BSD/OS SLIP BPF header */
177 #define LINKTYPE_PPP_BSDOS 103 /* BSD/OS PPP BPF header */
178 #define LINKTYPE_C_HDLC 104 /* Cisco HDLC */
179 #define LINKTYPE_IEEE802_11 105 /* IEEE 802.11 (wireless) */
180 #define LINKTYPE_ATM_CLIP 106 /* Linux Classical IP over ATM */
181 #define LINKTYPE_FRELAY 107 /* Frame Relay */
182 #define LINKTYPE_LOOP 108 /* OpenBSD loopback */
183 #define LINKTYPE_ENC 109 /* OpenBSD IPSEC enc */
186 * These three types are reserved for future use.
188 #define LINKTYPE_LANE8023 110 /* ATM LANE + 802.3 */
189 #define LINKTYPE_HIPPI 111 /* NetBSD HIPPI */
190 #define LINKTYPE_HDLC 112 /* NetBSD HDLC framing */
192 #define LINKTYPE_LINUX_SLL 113 /* Linux cooked socket capture */
193 #define LINKTYPE_LTALK 114 /* Apple LocalTalk hardware */
194 #define LINKTYPE_ECONET 115 /* Acorn Econet */
197 * Reserved for use with OpenBSD ipfilter.
199 #define LINKTYPE_IPFILTER 116
201 #define LINKTYPE_PFLOG 117 /* OpenBSD DLT_PFLOG */
202 #define LINKTYPE_CISCO_IOS 118 /* For Cisco-internal use */
203 #define LINKTYPE_PRISM_HEADER 119 /* 802.11+Prism II monitor mode */
204 #define LINKTYPE_AIRONET_HEADER 120 /* FreeBSD Aironet driver stuff */
207 * Reserved for Siemens HiPath HDLC.
209 #define LINKTYPE_HHDLC 121
211 #define LINKTYPE_IP_OVER_FC 122 /* RFC 2625 IP-over-Fibre Channel */
212 #define LINKTYPE_SUNATM 123 /* Solaris+SunATM */
215 * Reserved as per request from Kent Dahlgren <kent@praesum.com>
218 #define LINKTYPE_RIO 124 /* RapidIO */
219 #define LINKTYPE_PCI_EXP 125 /* PCI Express */
220 #define LINKTYPE_AURORA 126 /* Xilinx Aurora link layer */
222 #define LINKTYPE_IEEE802_11_RADIO 127 /* 802.11 plus BSD radio header */
225 * Reserved for the TZSP encapsulation, as per request from
226 * Chris Waters <chris.waters@networkchemistry.com>
227 * TZSP is a generic encapsulation for any other link type,
228 * which includes a means to include meta-information
229 * with the packet, e.g. signal strength and channel
230 * for 802.11 packets.
232 #define LINKTYPE_TZSP 128 /* Tazmen Sniffer Protocol */
234 #define LINKTYPE_ARCNET_LINUX 129 /* Linux-style headers */
237 * Juniper-private data link types, as per request from
238 * Hannes Gredler <hannes@juniper.net>. The corresponding
239 * DLT_s are used for passing on chassis-internal
240 * metainformation such as QOS profiles, etc..
242 #define LINKTYPE_JUNIPER_MLPPP 130
243 #define LINKTYPE_JUNIPER_MLFR 131
244 #define LINKTYPE_JUNIPER_ES 132
245 #define LINKTYPE_JUNIPER_GGSN 133
246 #define LINKTYPE_JUNIPER_MFR 134
247 #define LINKTYPE_JUNIPER_ATM2 135
248 #define LINKTYPE_JUNIPER_SERVICES 136
249 #define LINKTYPE_JUNIPER_ATM1 137
251 #define LINKTYPE_APPLE_IP_OVER_IEEE1394 138 /* Apple IP-over-IEEE 1394 cooked header */
253 #define LINKTYPE_RAWSS7 139 /* see rawss7.h for */
254 #define LINKTYPE_RAWSS7_MTP2 140 /* information on these */
255 #define LINKTYPE_RAWSS7_MTP3 141 /* definitions */
256 #define LINKTYPE_RAWSS7_SCCP 142
258 #define LINKTYPE_DOCSIS 143 /* DOCSIS MAC frames */
260 #define LINKTYPE_LINUX_IRDA 144 /* Linux-IrDA */
263 * Reserved for IBM SP switch and IBM Next Federation switch.
265 #define LINKTYPE_IBM_SP 145
266 #define LINKTYPE_IBM_SN 146
269 * Reserved for private use. If you have some link-layer header type
270 * that you want to use within your organization, with the capture files
271 * using that link-layer header type not ever be sent outside your
272 * organization, you can use these values.
274 * No libpcap release will use these for any purpose, nor will any
275 * tcpdump release use them, either.
277 * Do *NOT* use these in capture files that you expect anybody not using
278 * your private versions of capture-file-reading tools to read; in
279 * particular, do *NOT* use them in products, otherwise you may find that
280 * people won't be able to use tcpdump, or snort, or Ethereal, or... to
281 * read capture files from your firewall/intrusion detection/traffic
282 * monitoring/etc. appliance, or whatever product uses that LINKTYPE_ value,
283 * and you may also find that the developers of those applications will
284 * not accept patches to let them read those files.
286 * Also, do not use them if somebody might send you a capture using them
287 * for *their* private type and tools using them for *your* private type
288 * would have to read them.
290 * Instead, in those cases, ask "tcpdump-workers@tcpdump.org" for a new DLT_
291 * and LINKTYPE_ value, as per the comment in pcap-bpf.h, and use the type
294 #define LINKTYPE_USER0 147
295 #define LINKTYPE_USER1 148
296 #define LINKTYPE_USER2 149
297 #define LINKTYPE_USER3 150
298 #define LINKTYPE_USER4 151
299 #define LINKTYPE_USER5 152
300 #define LINKTYPE_USER6 153
301 #define LINKTYPE_USER7 154
302 #define LINKTYPE_USER8 155
303 #define LINKTYPE_USER9 156
304 #define LINKTYPE_USER10 157
305 #define LINKTYPE_USER11 158
306 #define LINKTYPE_USER12 159
307 #define LINKTYPE_USER13 160
308 #define LINKTYPE_USER14 161
309 #define LINKTYPE_USER15 162
312 * For future use with 802.11 captures - defined by AbsoluteValue
313 * Systems to store a number of bits of link-layer information
314 * including radio information:
316 * http://www.shaftnet.org/~pizza/software/capturefrm.txt
318 * but could and arguably should also be used by non-AVS Linux
319 * 802.11 drivers; that may happen in the future.
321 #define LINKTYPE_IEEE802_11_RADIO_AVS 163 /* 802.11 plus AVS radio header */
324 * Juniper-private data link type, as per request from
325 * Hannes Gredler <hannes@juniper.net>. The corresponding
326 * DLT_s are used for passing on chassis-internal
327 * metainformation such as QOS profiles, etc..
329 #define LINKTYPE_JUNIPER_MONITOR 164
332 * Reserved for BACnet MS/TP.
334 #define LINKTYPE_BACNET_MS_TP 165
337 * another PPP variant as per request from Karsten Keil <kkeil@suse.de>
338 * the first byte (0xff) of the PPP header (0xff03) is tweaked to accomodate
339 * the direction 0x00 = IN, 0x01 = OUT
341 #define LINKTYPE_PPP_WITHDIRECTION 166
344 static struct linktype_map
{
349 * These DLT_* codes have LINKTYPE_* codes with values identical
350 * to the values of the corresponding DLT_* code.
352 { DLT_NULL
, LINKTYPE_NULL
},
353 { DLT_EN10MB
, LINKTYPE_ETHERNET
},
354 { DLT_EN3MB
, LINKTYPE_EXP_ETHERNET
},
355 { DLT_AX25
, LINKTYPE_AX25
},
356 { DLT_PRONET
, LINKTYPE_PRONET
},
357 { DLT_CHAOS
, LINKTYPE_CHAOS
},
358 { DLT_IEEE802
, LINKTYPE_TOKEN_RING
},
359 { DLT_ARCNET
, LINKTYPE_ARCNET
},
360 { DLT_SLIP
, LINKTYPE_SLIP
},
361 { DLT_PPP
, LINKTYPE_PPP
},
362 { DLT_FDDI
, LINKTYPE_FDDI
},
365 * These DLT_* codes have different values on different
366 * platforms; we map them to LINKTYPE_* codes that
367 * have values that should never be equal to any DLT_*
371 /* BSD/OS Frame Relay */
372 { DLT_FR
, LINKTYPE_FRELAY
},
375 { DLT_SYMANTEC_FIREWALL
, LINKTYPE_SYMANTEC_FIREWALL
},
376 { DLT_ATM_RFC1483
, LINKTYPE_ATM_RFC1483
},
377 { DLT_RAW
, LINKTYPE_RAW
},
378 { DLT_SLIP_BSDOS
, LINKTYPE_SLIP_BSDOS
},
379 { DLT_PPP_BSDOS
, LINKTYPE_PPP_BSDOS
},
381 /* BSD/OS Cisco HDLC */
382 { DLT_C_HDLC
, LINKTYPE_C_HDLC
},
385 * These DLT_* codes are not on all platforms, but, so far,
386 * there don't appear to be any platforms that define
387 * other codes with those values; we map them to
388 * different LINKTYPE_* values anyway, just in case.
391 /* Linux ATM Classical IP */
392 { DLT_ATM_CLIP
, LINKTYPE_ATM_CLIP
},
394 /* NetBSD sync/async serial PPP (or Cisco HDLC) */
395 { DLT_PPP_SERIAL
, LINKTYPE_PPP_HDLC
},
397 /* NetBSD PPP over Ethernet */
398 { DLT_PPP_ETHER
, LINKTYPE_PPP_ETHER
},
400 /* IEEE 802.11 wireless */
401 { DLT_IEEE802_11
, LINKTYPE_IEEE802_11
},
404 { DLT_FRELAY
, LINKTYPE_FRELAY
},
406 /* OpenBSD loopback */
407 { DLT_LOOP
, LINKTYPE_LOOP
},
409 /* Linux cooked socket capture */
410 { DLT_LINUX_SLL
, LINKTYPE_LINUX_SLL
},
412 /* Apple LocalTalk hardware */
413 { DLT_LTALK
, LINKTYPE_LTALK
},
416 { DLT_ECONET
, LINKTYPE_ECONET
},
418 /* OpenBSD DLT_PFLOG */
419 { DLT_PFLOG
, LINKTYPE_PFLOG
},
421 /* For Cisco-internal use */
422 { DLT_CISCO_IOS
, LINKTYPE_CISCO_IOS
},
424 /* Prism II monitor-mode header plus 802.11 header */
425 { DLT_PRISM_HEADER
, LINKTYPE_PRISM_HEADER
},
427 /* FreeBSD Aironet driver stuff */
428 { DLT_AIRONET_HEADER
, LINKTYPE_AIRONET_HEADER
},
430 /* Siemens HiPath HDLC */
431 { DLT_HHDLC
, LINKTYPE_HHDLC
},
433 /* RFC 2625 IP-over-Fibre Channel */
434 { DLT_IP_OVER_FC
, LINKTYPE_IP_OVER_FC
},
437 { DLT_SUNATM
, LINKTYPE_SUNATM
},
440 { DLT_RIO
, LINKTYPE_RIO
},
443 { DLT_PCI_EXP
, LINKTYPE_PCI_EXP
},
445 /* Xilinx Aurora link layer */
446 { DLT_AURORA
, LINKTYPE_AURORA
},
448 /* 802.11 plus BSD radio header */
449 { DLT_IEEE802_11_RADIO
, LINKTYPE_IEEE802_11_RADIO
},
451 /* Tazmen Sniffer Protocol */
452 { DLT_TZSP
, LINKTYPE_TZSP
},
454 /* Arcnet with Linux-style link-layer headers */
455 { DLT_ARCNET_LINUX
, LINKTYPE_ARCNET_LINUX
},
457 /* Juniper-internal chassis encapsulation */
458 { DLT_JUNIPER_MLPPP
, LINKTYPE_JUNIPER_MLPPP
},
459 { DLT_JUNIPER_MLFR
, LINKTYPE_JUNIPER_MLFR
},
460 { DLT_JUNIPER_ES
, LINKTYPE_JUNIPER_ES
},
461 { DLT_JUNIPER_GGSN
, LINKTYPE_JUNIPER_GGSN
},
462 { DLT_JUNIPER_MFR
, LINKTYPE_JUNIPER_MFR
},
463 { DLT_JUNIPER_ATM2
, LINKTYPE_JUNIPER_ATM2
},
464 { DLT_JUNIPER_SERVICES
, LINKTYPE_JUNIPER_SERVICES
},
465 { DLT_JUNIPER_ATM1
, LINKTYPE_JUNIPER_ATM1
},
467 /* Apple IP-over-IEEE 1394 cooked header */
468 { DLT_APPLE_IP_OVER_IEEE1394
, LINKTYPE_APPLE_IP_OVER_IEEE1394
},
470 /* DOCSIS MAC frames */
471 { DLT_DOCSIS
, LINKTYPE_DOCSIS
},
473 /* IrDA IrLAP packets + Linux-cooked header */
474 { DLT_LINUX_IRDA
, LINKTYPE_LINUX_IRDA
},
476 /* IBM SP and Next Federation switches */
477 { DLT_IBM_SP
, LINKTYPE_IBM_SP
},
478 { DLT_IBM_SN
, LINKTYPE_IBM_SN
},
480 /* 802.11 plus AVS radio header */
481 { DLT_IEEE802_11_RADIO_AVS
, LINKTYPE_IEEE802_11_RADIO_AVS
},
484 * Any platform that defines additional DLT_* codes should:
486 * request a LINKTYPE_* code and value from tcpdump.org,
489 * add, in their version of libpcap, an entry to map
490 * those DLT_* codes to the corresponding LINKTYPE_*
493 * redefine, in their "net/bpf.h", any DLT_* values
494 * that collide with the values used by their additional
495 * DLT_* codes, to remove those collisions (but without
496 * making them collide with any of the LINKTYPE_*
497 * values equal to 50 or above; they should also avoid
498 * defining DLT_* values that collide with those
499 * LINKTYPE_* values, either).
502 /* Juniper-internal chassis encapsulation */
503 { DLT_JUNIPER_MONITOR
, LINKTYPE_JUNIPER_MONITOR
},
506 { DLT_BACNET_MS_TP
, LINKTYPE_BACNET_MS_TP
},
508 /* PPP with direction flag in the PPP header */
509 { DLT_PPP_WITHDIRECTION
,LINKTYPE_PPP_WITHDIRECTION
},
515 dlt_to_linktype(int dlt
)
519 for (i
= 0; map
[i
].dlt
!= -1; i
++) {
520 if (map
[i
].dlt
== dlt
)
521 return (map
[i
].linktype
);
525 * If we don't have a mapping for this DLT_ code, return an
526 * error; that means that the table above needs to have an
533 linktype_to_dlt(int linktype
)
537 for (i
= 0; map
[i
].linktype
!= -1; i
++) {
538 if (map
[i
].linktype
== linktype
)
543 * If we don't have an entry for this link type, return
544 * the link type value; it may be a DLT_ value from an
545 * older version of libpcap.
551 sf_write_header(FILE *fp
, int linktype
, int thiszone
, int snaplen
)
553 struct pcap_file_header hdr
;
555 hdr
.magic
= TCPDUMP_MAGIC
;
556 hdr
.version_major
= PCAP_VERSION_MAJOR
;
557 hdr
.version_minor
= PCAP_VERSION_MINOR
;
559 hdr
.thiszone
= thiszone
;
560 hdr
.snaplen
= snaplen
;
562 hdr
.linktype
= linktype
;
564 if (fwrite((char *)&hdr
, sizeof(hdr
), 1, fp
) != 1)
571 swap_hdr(struct pcap_file_header
*hp
)
573 hp
->version_major
= SWAPSHORT(hp
->version_major
);
574 hp
->version_minor
= SWAPSHORT(hp
->version_minor
);
575 hp
->thiszone
= SWAPLONG(hp
->thiszone
);
576 hp
->sigfigs
= SWAPLONG(hp
->sigfigs
);
577 hp
->snaplen
= SWAPLONG(hp
->snaplen
);
578 hp
->linktype
= SWAPLONG(hp
->linktype
);
582 sf_getnonblock(pcap_t
*p
, char *errbuf
)
585 * This is a savefile, not a live capture file, so never say
586 * it's in non-blocking mode.
592 sf_setnonblock(pcap_t
*p
, int nonblock
, char *errbuf
)
595 * This is a savefile, not a live capture file, so ignore
596 * requests to put it in non-blocking mode.
602 sf_stats(pcap_t
*p
, struct pcap_stat
*ps
)
604 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
605 "Statistics aren't available from savefiles");
610 sf_inject(pcap_t
*p
, const void *buf _U_
, size_t size _U_
)
612 strlcpy(p
->errbuf
, "Sending packets isn't supported on savefiles",
620 if (p
->sf
.rfile
!= stdin
)
621 (void)fclose(p
->sf
.rfile
);
622 if (p
->sf
.base
!= NULL
)
627 pcap_open_offline(const char *fname
, char *errbuf
)
631 struct pcap_file_header hdr
;
635 p
= (pcap_t
*)malloc(sizeof(*p
));
637 strlcpy(errbuf
, "out of swap", PCAP_ERRBUF_SIZE
);
641 memset((char *)p
, 0, sizeof(*p
));
643 if (fname
[0] == '-' && fname
[1] == '\0')
647 fp
= fopen(fname
, "r");
649 fp
= fopen(fname
, "rb");
652 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "%s: %s", fname
,
653 pcap_strerror(errno
));
657 if (fread((char *)&hdr
, sizeof(hdr
), 1, fp
) != 1) {
658 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "fread: %s",
659 pcap_strerror(errno
));
663 if (magic
!= TCPDUMP_MAGIC
&& magic
!= KUZNETZOV_TCPDUMP_MAGIC
) {
664 magic
= SWAPLONG(magic
);
665 if (magic
!= TCPDUMP_MAGIC
&& magic
!= KUZNETZOV_TCPDUMP_MAGIC
) {
666 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
667 "bad dump file format");
673 if (magic
== KUZNETZOV_TCPDUMP_MAGIC
) {
675 * XXX - the patch that's in some versions of libpcap
676 * changes the packet header but not the magic number,
677 * and some other versions with this magic number have
678 * some extra debugging information in the packet header;
679 * we'd have to use some hacks^H^H^H^H^Hheuristics to
680 * detect those variants.
682 * Ethereal does that, but it does so by trying to read
683 * the first two packets of the file with each of the
684 * record header formats. That currently means it seeks
685 * backwards and retries the reads, which doesn't work
686 * on pipes. We want to be able to read from a pipe, so
687 * that strategy won't work; we'd have to buffer some
688 * data ourselves and read from that buffer in order to
691 p
->sf
.hdrsize
= sizeof(struct pcap_sf_patched_pkthdr
);
693 p
->sf
.hdrsize
= sizeof(struct pcap_sf_pkthdr
);
694 if (hdr
.version_major
< PCAP_VERSION_MAJOR
) {
695 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "archaic file format");
698 p
->tzoff
= hdr
.thiszone
;
699 p
->snapshot
= hdr
.snaplen
;
700 p
->linktype
= linktype_to_dlt(hdr
.linktype
);
703 p
->bufsize
= hdr
.snaplen
;
705 /* Allocate the space for pcap_pkthdr as well. It will be used by pcap_read_ex */
706 p
->bufsize
= hdr
.snaplen
+sizeof(struct pcap_pkthdr
);
709 /* Align link header as required for proper data alignment */
710 /* XXX should handle all types */
711 switch (p
->linktype
) {
718 linklen
= 13 + 8; /* fddi_header + llc */
728 p
->bufsize
= BPF_MAXBUFSIZE
;
729 p
->sf
.base
= (u_char
*)malloc(p
->bufsize
+ BPF_ALIGNMENT
);
730 if (p
->sf
.base
== NULL
) {
731 strlcpy(errbuf
, "out of swap", PCAP_ERRBUF_SIZE
);
734 p
->buffer
= p
->sf
.base
+ BPF_ALIGNMENT
- (linklen
% BPF_ALIGNMENT
);
735 p
->sf
.version_major
= hdr
.version_major
;
736 p
->sf
.version_minor
= hdr
.version_minor
;
738 /* XXX padding only needed for kernel fcode */
743 * We interchanged the caplen and len fields at version 2.3,
744 * in order to match the bpf header layout. But unfortunately
745 * some files were written with version 2.3 in their headers
746 * but without the interchanged fields.
748 * In addition, DG/UX tcpdump writes out files with a version
749 * number of 543.0, and with the caplen and len fields in the
752 switch (hdr
.version_major
) {
755 if (hdr
.version_minor
< 3)
756 p
->sf
.lengths_swapped
= SWAPPED
;
757 else if (hdr
.version_minor
== 3)
758 p
->sf
.lengths_swapped
= MAYBE_SWAPPED
;
760 p
->sf
.lengths_swapped
= NOT_SWAPPED
;
764 p
->sf
.lengths_swapped
= SWAPPED
;
768 p
->sf
.lengths_swapped
= NOT_SWAPPED
;
774 * You can do "select()" and "poll()" on plain files on most
775 * platforms, and should be able to do so on pipes.
777 * You can't do "select()" on anything other than sockets in
778 * Windows, so, on Win32 systems, we don't have "selectable_fd".
780 p
->selectable_fd
= fileno(fp
);
783 p
->read_op
= pcap_offline_read
;
784 p
->inject_op
= sf_inject
;
785 p
->setfilter_op
= install_bpf_program
;
786 p
->set_datalink_op
= NULL
; /* we don't support munging link-layer headers */
787 p
->getnonblock_op
= sf_getnonblock
;
788 p
->setnonblock_op
= sf_setnonblock
;
789 p
->stats_op
= sf_stats
;
790 p
->close_op
= sf_close
;
794 if (fp
!= NULL
&& fp
!= stdin
)
801 * Read sf_readfile and return the next packet. Return the header in hdr
802 * and the contents in buf. Return 0 on success, SFERR_EOF if there were
803 * no more packets, and SFERR_TRUNC if a partial packet was encountered.
806 sf_next_packet(pcap_t
*p
, struct pcap_pkthdr
*hdr
, u_char
*buf
, u_int buflen
)
808 struct pcap_sf_patched_pkthdr sf_hdr
;
809 FILE *fp
= p
->sf
.rfile
;
814 * Read the packet header; the structure we use as a buffer
815 * is the longer structure for files generated by the patched
816 * libpcap, but if the file has the magic number for an
817 * unpatched libpcap we only read as many bytes as the regular
820 amt_read
= fread(&sf_hdr
, 1, p
->sf
.hdrsize
, fp
);
821 if (amt_read
!= p
->sf
.hdrsize
) {
823 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
824 "error reading dump file: %s",
825 pcap_strerror(errno
));
829 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
830 "truncated dump file; tried to read %d header bytes, only got %lu",
831 p
->sf
.hdrsize
, (unsigned long)amt_read
);
840 /* these were written in opposite byte order */
841 hdr
->caplen
= SWAPLONG(sf_hdr
.caplen
);
842 hdr
->len
= SWAPLONG(sf_hdr
.len
);
843 hdr
->ts
.tv_sec
= SWAPLONG(sf_hdr
.ts
.tv_sec
);
844 hdr
->ts
.tv_usec
= SWAPLONG(sf_hdr
.ts
.tv_usec
);
846 hdr
->caplen
= sf_hdr
.caplen
;
847 hdr
->len
= sf_hdr
.len
;
848 hdr
->ts
.tv_sec
= sf_hdr
.ts
.tv_sec
;
849 hdr
->ts
.tv_usec
= sf_hdr
.ts
.tv_usec
;
851 /* Swap the caplen and len fields, if necessary. */
852 switch (p
->sf
.lengths_swapped
) {
858 if (hdr
->caplen
<= hdr
->len
) {
860 * The captured length is <= the actual length,
861 * so presumably they weren't swapped.
869 hdr
->caplen
= hdr
->len
;
874 if (hdr
->caplen
> buflen
) {
876 * This can happen due to Solaris 2.3 systems tripping
877 * over the BUFMOD problem and not setting the snapshot
878 * correctly in the savefile header. If the caplen isn't
879 * grossly wrong, try to salvage.
881 static u_char
*tp
= NULL
;
882 static size_t tsize
= 0;
884 if (hdr
->caplen
> 65535) {
885 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
886 "bogus savefile header");
890 if (tsize
< hdr
->caplen
) {
891 tsize
= ((hdr
->caplen
+ 1023) / 1024) * 1024;
894 tp
= (u_char
*)malloc(tsize
);
897 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
898 "BUFMOD hack malloc");
902 amt_read
= fread((char *)tp
, 1, hdr
->caplen
, fp
);
903 if (amt_read
!= hdr
->caplen
) {
905 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
906 "error reading dump file: %s",
907 pcap_strerror(errno
));
909 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
910 "truncated dump file; tried to read %u captured bytes, only got %lu",
911 hdr
->caplen
, (unsigned long)amt_read
);
916 * We can only keep up to buflen bytes. Since caplen > buflen
917 * is exactly how we got here, we know we can only keep the
918 * first buflen bytes and must drop the remainder. Adjust
919 * caplen accordingly, so we don't get confused later as
920 * to how many bytes we have to play with.
922 hdr
->caplen
= buflen
;
923 memcpy((char *)buf
, (char *)tp
, buflen
);
926 /* read the packet itself */
927 amt_read
= fread((char *)buf
, 1, hdr
->caplen
, fp
);
928 if (amt_read
!= hdr
->caplen
) {
930 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
931 "error reading dump file: %s",
932 pcap_strerror(errno
));
934 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
935 "truncated dump file; tried to read %u captured bytes, only got %lu",
936 hdr
->caplen
, (unsigned long)amt_read
);
945 * Print out packets stored in the file initialized by sf_read_init().
946 * If cnt > 0, return after 'cnt' packets, otherwise continue until eof.
949 pcap_offline_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
951 struct bpf_insn
*fcode
= p
->fcode
.bf_insns
;
955 while (status
== 0) {
956 struct pcap_pkthdr h
;
959 * Has "pcap_breakloop()" been called?
960 * If so, return immediately - if we haven't read any
961 * packets, clear the flag and return -2 to indicate
962 * that we were told to break out of the loop, otherwise
963 * leave the flag set, so that the *next* call will break
964 * out of the loop without having read any packets, and
965 * return the number of packets we've processed so far.
975 status
= sf_next_packet(p
, &h
, p
->buffer
, p
->bufsize
);
983 bpf_filter(fcode
, p
->buffer
, h
.len
, h
.caplen
)) {
984 (*callback
)(user
, &h
, p
->buffer
);
985 if (++n
>= cnt
&& cnt
> 0)
989 /*XXX this breaks semantics tcpslice expects */
994 * Output a packet to the initialized dump file.
997 pcap_dump(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
1000 struct pcap_sf_pkthdr sf_hdr
;
1003 sf_hdr
.ts
.tv_sec
= h
->ts
.tv_sec
;
1004 sf_hdr
.ts
.tv_usec
= h
->ts
.tv_usec
;
1005 sf_hdr
.caplen
= h
->caplen
;
1006 sf_hdr
.len
= h
->len
;
1007 /* XXX we should check the return status */
1008 (void)fwrite(&sf_hdr
, sizeof(sf_hdr
), 1, f
);
1009 (void)fwrite((char *)sp
, h
->caplen
, 1, f
);
1013 * Initialize so that sf_write() will output to the file named 'fname'.
1016 pcap_dump_open(pcap_t
*p
, const char *fname
)
1021 linktype
= dlt_to_linktype(p
->linktype
);
1022 if (linktype
== -1) {
1023 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1024 "%s: link-layer type %d isn't supported in savefiles",
1029 if (fname
[0] == '-' && fname
[1] == '\0') {
1032 _setmode(_fileno(f
), _O_BINARY
);
1036 f
= fopen(fname
, "w");
1038 f
= fopen(fname
, "wb");
1041 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "%s: %s",
1042 fname
, pcap_strerror(errno
));
1046 setbuf(f
, NULL
); /* XXX - why? */
1049 (void)sf_write_header(f
, linktype
, p
->tzoff
, p
->snapshot
);
1050 return ((pcap_dumper_t
*)f
);
1054 pcap_dump_file(pcap_dumper_t
*p
)
1060 pcap_dump_flush(pcap_dumper_t
*p
)
1063 if (fflush((FILE *)p
) == EOF
)
1070 pcap_dump_close(pcap_dumper_t
*p
)
1074 if (ferror((FILE *)p
))
1076 /* XXX should check return from fclose() too */
1078 (void)fclose((FILE *)p
);