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