xref: /aosp_15_r20/external/libpcap/sf-pcap.c (revision 8b26181f966a6af5cf6981a6f474313de533bb28)
1*8b26181fSAndroid Build Coastguard Worker /*
2*8b26181fSAndroid Build Coastguard Worker  * Copyright (c) 1993, 1994, 1995, 1996, 1997
3*8b26181fSAndroid Build Coastguard Worker  *	The Regents of the University of California.  All rights reserved.
4*8b26181fSAndroid Build Coastguard Worker  *
5*8b26181fSAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
6*8b26181fSAndroid Build Coastguard Worker  * modification, are permitted provided that: (1) source code distributions
7*8b26181fSAndroid Build Coastguard Worker  * retain the above copyright notice and this paragraph in its entirety, (2)
8*8b26181fSAndroid Build Coastguard Worker  * distributions including binary code include the above copyright notice and
9*8b26181fSAndroid Build Coastguard Worker  * this paragraph in its entirety in the documentation or other materials
10*8b26181fSAndroid Build Coastguard Worker  * provided with the distribution, and (3) all advertising materials mentioning
11*8b26181fSAndroid Build Coastguard Worker  * features or use of this software display the following acknowledgement:
12*8b26181fSAndroid Build Coastguard Worker  * ``This product includes software developed by the University of California,
13*8b26181fSAndroid Build Coastguard Worker  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14*8b26181fSAndroid Build Coastguard Worker  * the University nor the names of its contributors may be used to endorse
15*8b26181fSAndroid Build Coastguard Worker  * or promote products derived from this software without specific prior
16*8b26181fSAndroid Build Coastguard Worker  * written permission.
17*8b26181fSAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18*8b26181fSAndroid Build Coastguard Worker  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19*8b26181fSAndroid Build Coastguard Worker  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20*8b26181fSAndroid Build Coastguard Worker  *
21*8b26181fSAndroid Build Coastguard Worker  * sf-pcap.c - libpcap-file-format-specific code from savefile.c
22*8b26181fSAndroid Build Coastguard Worker  *	Extraction/creation by Jeffrey Mogul, DECWRL
23*8b26181fSAndroid Build Coastguard Worker  *	Modified by Steve McCanne, LBL.
24*8b26181fSAndroid Build Coastguard Worker  *
25*8b26181fSAndroid Build Coastguard Worker  * Used to save the received packet headers, after filtering, to
26*8b26181fSAndroid Build Coastguard Worker  * a file, and then read them later.
27*8b26181fSAndroid Build Coastguard Worker  * The first record in the file contains saved values for the machine
28*8b26181fSAndroid Build Coastguard Worker  * dependent values so we can print the dump file on any architecture.
29*8b26181fSAndroid Build Coastguard Worker  */
30*8b26181fSAndroid Build Coastguard Worker 
31*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_CONFIG_H
32*8b26181fSAndroid Build Coastguard Worker #include <config.h>
33*8b26181fSAndroid Build Coastguard Worker #endif
34*8b26181fSAndroid Build Coastguard Worker 
35*8b26181fSAndroid Build Coastguard Worker #include <pcap-types.h>
36*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
37*8b26181fSAndroid Build Coastguard Worker #include <io.h>
38*8b26181fSAndroid Build Coastguard Worker #include <fcntl.h>
39*8b26181fSAndroid Build Coastguard Worker #endif /* _WIN32 */
40*8b26181fSAndroid Build Coastguard Worker 
41*8b26181fSAndroid Build Coastguard Worker #include <errno.h>
42*8b26181fSAndroid Build Coastguard Worker #include <memory.h>
43*8b26181fSAndroid Build Coastguard Worker #include <stdio.h>
44*8b26181fSAndroid Build Coastguard Worker #include <stdlib.h>
45*8b26181fSAndroid Build Coastguard Worker #include <string.h>
46*8b26181fSAndroid Build Coastguard Worker #include <limits.h> /* for INT_MAX */
47*8b26181fSAndroid Build Coastguard Worker 
48*8b26181fSAndroid Build Coastguard Worker #include "pcap-int.h"
49*8b26181fSAndroid Build Coastguard Worker #include "pcap-util.h"
50*8b26181fSAndroid Build Coastguard Worker 
51*8b26181fSAndroid Build Coastguard Worker #include "pcap-common.h"
52*8b26181fSAndroid Build Coastguard Worker 
53*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OS_PROTO_H
54*8b26181fSAndroid Build Coastguard Worker #include "os-proto.h"
55*8b26181fSAndroid Build Coastguard Worker #endif
56*8b26181fSAndroid Build Coastguard Worker 
57*8b26181fSAndroid Build Coastguard Worker #include "sf-pcap.h"
58*8b26181fSAndroid Build Coastguard Worker 
59*8b26181fSAndroid Build Coastguard Worker /*
60*8b26181fSAndroid Build Coastguard Worker  * Setting O_BINARY on DOS/Windows is a bit tricky
61*8b26181fSAndroid Build Coastguard Worker  */
62*8b26181fSAndroid Build Coastguard Worker #if defined(_WIN32)
63*8b26181fSAndroid Build Coastguard Worker   #define SET_BINMODE(f)  _setmode(_fileno(f), _O_BINARY)
64*8b26181fSAndroid Build Coastguard Worker #elif defined(MSDOS)
65*8b26181fSAndroid Build Coastguard Worker   #if defined(__HIGHC__)
66*8b26181fSAndroid Build Coastguard Worker   #define SET_BINMODE(f)  setmode(f, O_BINARY)
67*8b26181fSAndroid Build Coastguard Worker   #else
68*8b26181fSAndroid Build Coastguard Worker   #define SET_BINMODE(f)  setmode(fileno(f), O_BINARY)
69*8b26181fSAndroid Build Coastguard Worker   #endif
70*8b26181fSAndroid Build Coastguard Worker #endif
71*8b26181fSAndroid Build Coastguard Worker 
72*8b26181fSAndroid Build Coastguard Worker /*
73*8b26181fSAndroid Build Coastguard Worker  * Standard libpcap format.
74*8b26181fSAndroid Build Coastguard Worker  *
75*8b26181fSAndroid Build Coastguard Worker  * The same value is used in the rpcap protocol as an indication of
76*8b26181fSAndroid Build Coastguard Worker  * the server byte order, to let the client know whether it needs to
77*8b26181fSAndroid Build Coastguard Worker  * byte-swap some host-byte-order metadata.
78*8b26181fSAndroid Build Coastguard Worker  */
79*8b26181fSAndroid Build Coastguard Worker #define TCPDUMP_MAGIC		0xa1b2c3d4
80*8b26181fSAndroid Build Coastguard Worker 
81*8b26181fSAndroid Build Coastguard Worker /*
82*8b26181fSAndroid Build Coastguard Worker  * Alexey Kuznetzov's modified libpcap format.
83*8b26181fSAndroid Build Coastguard Worker  */
84*8b26181fSAndroid Build Coastguard Worker #define KUZNETZOV_TCPDUMP_MAGIC	0xa1b2cd34
85*8b26181fSAndroid Build Coastguard Worker 
86*8b26181fSAndroid Build Coastguard Worker /*
87*8b26181fSAndroid Build Coastguard Worker  * Reserved for Francisco Mesquita <[email protected]>
88*8b26181fSAndroid Build Coastguard Worker  * for another modified format.
89*8b26181fSAndroid Build Coastguard Worker  */
90*8b26181fSAndroid Build Coastguard Worker #define FMESQUITA_TCPDUMP_MAGIC	0xa1b234cd
91*8b26181fSAndroid Build Coastguard Worker 
92*8b26181fSAndroid Build Coastguard Worker /*
93*8b26181fSAndroid Build Coastguard Worker  * Navtel Communcations' format, with nanosecond timestamps,
94*8b26181fSAndroid Build Coastguard Worker  * as per a request from Dumas Hwang <[email protected]>.
95*8b26181fSAndroid Build Coastguard Worker  */
96*8b26181fSAndroid Build Coastguard Worker #define NAVTEL_TCPDUMP_MAGIC	0xa12b3c4d
97*8b26181fSAndroid Build Coastguard Worker 
98*8b26181fSAndroid Build Coastguard Worker /*
99*8b26181fSAndroid Build Coastguard Worker  * Normal libpcap format, except for seconds/nanoseconds timestamps,
100*8b26181fSAndroid Build Coastguard Worker  * as per a request by Ulf Lamping <[email protected]>
101*8b26181fSAndroid Build Coastguard Worker  */
102*8b26181fSAndroid Build Coastguard Worker #define NSEC_TCPDUMP_MAGIC	0xa1b23c4d
103*8b26181fSAndroid Build Coastguard Worker 
104*8b26181fSAndroid Build Coastguard Worker /*
105*8b26181fSAndroid Build Coastguard Worker  * Mechanism for storing information about a capture in the upper
106*8b26181fSAndroid Build Coastguard Worker  * 6 bits of a linktype value in a capture file.
107*8b26181fSAndroid Build Coastguard Worker  *
108*8b26181fSAndroid Build Coastguard Worker  * LT_LINKTYPE_EXT(x) extracts the additional information.
109*8b26181fSAndroid Build Coastguard Worker  *
110*8b26181fSAndroid Build Coastguard Worker  * The rest of the bits are for a value describing the link-layer
111*8b26181fSAndroid Build Coastguard Worker  * value.  LT_LINKTYPE(x) extracts that value.
112*8b26181fSAndroid Build Coastguard Worker  */
113*8b26181fSAndroid Build Coastguard Worker #define LT_LINKTYPE(x)		((x) & 0x03FFFFFF)
114*8b26181fSAndroid Build Coastguard Worker #define LT_LINKTYPE_EXT(x)	((x) & 0xFC000000)
115*8b26181fSAndroid Build Coastguard Worker 
116*8b26181fSAndroid Build Coastguard Worker static int pcap_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char **datap);
117*8b26181fSAndroid Build Coastguard Worker 
118*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
119*8b26181fSAndroid Build Coastguard Worker /*
120*8b26181fSAndroid Build Coastguard Worker  * This isn't exported on Windows, because it would only work if both
121*8b26181fSAndroid Build Coastguard Worker  * libpcap and the code using it were using the same C runtime; otherwise they
122*8b26181fSAndroid Build Coastguard Worker  * would be using different definitions of a FILE structure.
123*8b26181fSAndroid Build Coastguard Worker  *
124*8b26181fSAndroid Build Coastguard Worker  * Instead we define this as a macro in pcap/pcap.h that wraps the hopen
125*8b26181fSAndroid Build Coastguard Worker  * version that we do export, passing it a raw OS HANDLE, as defined by the
126*8b26181fSAndroid Build Coastguard Worker  * Win32 / Win64 ABI, obtained from the _fileno() and _get_osfhandle()
127*8b26181fSAndroid Build Coastguard Worker  * functions of the appropriate CRT.
128*8b26181fSAndroid Build Coastguard Worker  */
129*8b26181fSAndroid Build Coastguard Worker static pcap_dumper_t *pcap_dump_fopen(pcap_t *p, FILE *f);
130*8b26181fSAndroid Build Coastguard Worker #endif /* _WIN32 */
131*8b26181fSAndroid Build Coastguard Worker 
132*8b26181fSAndroid Build Coastguard Worker /*
133*8b26181fSAndroid Build Coastguard Worker  * Private data for reading pcap savefiles.
134*8b26181fSAndroid Build Coastguard Worker  */
135*8b26181fSAndroid Build Coastguard Worker typedef enum {
136*8b26181fSAndroid Build Coastguard Worker 	NOT_SWAPPED,
137*8b26181fSAndroid Build Coastguard Worker 	SWAPPED,
138*8b26181fSAndroid Build Coastguard Worker 	MAYBE_SWAPPED
139*8b26181fSAndroid Build Coastguard Worker } swapped_type_t;
140*8b26181fSAndroid Build Coastguard Worker 
141*8b26181fSAndroid Build Coastguard Worker typedef enum {
142*8b26181fSAndroid Build Coastguard Worker 	PASS_THROUGH,
143*8b26181fSAndroid Build Coastguard Worker 	SCALE_UP,
144*8b26181fSAndroid Build Coastguard Worker 	SCALE_DOWN
145*8b26181fSAndroid Build Coastguard Worker } tstamp_scale_type_t;
146*8b26181fSAndroid Build Coastguard Worker 
147*8b26181fSAndroid Build Coastguard Worker struct pcap_sf {
148*8b26181fSAndroid Build Coastguard Worker 	size_t hdrsize;
149*8b26181fSAndroid Build Coastguard Worker 	swapped_type_t lengths_swapped;
150*8b26181fSAndroid Build Coastguard Worker 	tstamp_scale_type_t scale_type;
151*8b26181fSAndroid Build Coastguard Worker };
152*8b26181fSAndroid Build Coastguard Worker 
153*8b26181fSAndroid Build Coastguard Worker /*
154*8b26181fSAndroid Build Coastguard Worker  * Check whether this is a pcap savefile and, if it is, extract the
155*8b26181fSAndroid Build Coastguard Worker  * relevant information from the header.
156*8b26181fSAndroid Build Coastguard Worker  */
157*8b26181fSAndroid Build Coastguard Worker pcap_t *
pcap_check_header(const uint8_t * magic,FILE * fp,u_int precision,char * errbuf,int * err)158*8b26181fSAndroid Build Coastguard Worker pcap_check_header(const uint8_t *magic, FILE *fp, u_int precision, char *errbuf,
159*8b26181fSAndroid Build Coastguard Worker 		  int *err)
160*8b26181fSAndroid Build Coastguard Worker {
161*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32 magic_int;
162*8b26181fSAndroid Build Coastguard Worker 	struct pcap_file_header hdr;
163*8b26181fSAndroid Build Coastguard Worker 	size_t amt_read;
164*8b26181fSAndroid Build Coastguard Worker 	pcap_t *p;
165*8b26181fSAndroid Build Coastguard Worker 	int swapped = 0;
166*8b26181fSAndroid Build Coastguard Worker 	struct pcap_sf *ps;
167*8b26181fSAndroid Build Coastguard Worker 
168*8b26181fSAndroid Build Coastguard Worker 	/*
169*8b26181fSAndroid Build Coastguard Worker 	 * Assume no read errors.
170*8b26181fSAndroid Build Coastguard Worker 	 */
171*8b26181fSAndroid Build Coastguard Worker 	*err = 0;
172*8b26181fSAndroid Build Coastguard Worker 
173*8b26181fSAndroid Build Coastguard Worker 	/*
174*8b26181fSAndroid Build Coastguard Worker 	 * Check whether the first 4 bytes of the file are the magic
175*8b26181fSAndroid Build Coastguard Worker 	 * number for a pcap savefile, or for a byte-swapped pcap
176*8b26181fSAndroid Build Coastguard Worker 	 * savefile.
177*8b26181fSAndroid Build Coastguard Worker 	 */
178*8b26181fSAndroid Build Coastguard Worker 	memcpy(&magic_int, magic, sizeof(magic_int));
179*8b26181fSAndroid Build Coastguard Worker 	if (magic_int != TCPDUMP_MAGIC &&
180*8b26181fSAndroid Build Coastguard Worker 	    magic_int != KUZNETZOV_TCPDUMP_MAGIC &&
181*8b26181fSAndroid Build Coastguard Worker 	    magic_int != NSEC_TCPDUMP_MAGIC) {
182*8b26181fSAndroid Build Coastguard Worker 		magic_int = SWAPLONG(magic_int);
183*8b26181fSAndroid Build Coastguard Worker 		if (magic_int != TCPDUMP_MAGIC &&
184*8b26181fSAndroid Build Coastguard Worker 		    magic_int != KUZNETZOV_TCPDUMP_MAGIC &&
185*8b26181fSAndroid Build Coastguard Worker 		    magic_int != NSEC_TCPDUMP_MAGIC)
186*8b26181fSAndroid Build Coastguard Worker 			return (NULL);	/* nope */
187*8b26181fSAndroid Build Coastguard Worker 		swapped = 1;
188*8b26181fSAndroid Build Coastguard Worker 	}
189*8b26181fSAndroid Build Coastguard Worker 
190*8b26181fSAndroid Build Coastguard Worker 	/*
191*8b26181fSAndroid Build Coastguard Worker 	 * They are.  Put the magic number in the header, and read
192*8b26181fSAndroid Build Coastguard Worker 	 * the rest of the header.
193*8b26181fSAndroid Build Coastguard Worker 	 */
194*8b26181fSAndroid Build Coastguard Worker 	hdr.magic = magic_int;
195*8b26181fSAndroid Build Coastguard Worker 	amt_read = fread(((char *)&hdr) + sizeof hdr.magic, 1,
196*8b26181fSAndroid Build Coastguard Worker 	    sizeof(hdr) - sizeof(hdr.magic), fp);
197*8b26181fSAndroid Build Coastguard Worker 	if (amt_read != sizeof(hdr) - sizeof(hdr.magic)) {
198*8b26181fSAndroid Build Coastguard Worker 		if (ferror(fp)) {
199*8b26181fSAndroid Build Coastguard Worker 			pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
200*8b26181fSAndroid Build Coastguard Worker 			    errno, "error reading dump file");
201*8b26181fSAndroid Build Coastguard Worker 		} else {
202*8b26181fSAndroid Build Coastguard Worker 			snprintf(errbuf, PCAP_ERRBUF_SIZE,
203*8b26181fSAndroid Build Coastguard Worker 			    "truncated dump file; tried to read %zu file header bytes, only got %zu",
204*8b26181fSAndroid Build Coastguard Worker 			    sizeof(hdr), amt_read);
205*8b26181fSAndroid Build Coastguard Worker 		}
206*8b26181fSAndroid Build Coastguard Worker 		*err = 1;
207*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
208*8b26181fSAndroid Build Coastguard Worker 	}
209*8b26181fSAndroid Build Coastguard Worker 
210*8b26181fSAndroid Build Coastguard Worker 	/*
211*8b26181fSAndroid Build Coastguard Worker 	 * If it's a byte-swapped capture file, byte-swap the header.
212*8b26181fSAndroid Build Coastguard Worker 	 */
213*8b26181fSAndroid Build Coastguard Worker 	if (swapped) {
214*8b26181fSAndroid Build Coastguard Worker 		hdr.version_major = SWAPSHORT(hdr.version_major);
215*8b26181fSAndroid Build Coastguard Worker 		hdr.version_minor = SWAPSHORT(hdr.version_minor);
216*8b26181fSAndroid Build Coastguard Worker 		hdr.thiszone = SWAPLONG(hdr.thiszone);
217*8b26181fSAndroid Build Coastguard Worker 		hdr.sigfigs = SWAPLONG(hdr.sigfigs);
218*8b26181fSAndroid Build Coastguard Worker 		hdr.snaplen = SWAPLONG(hdr.snaplen);
219*8b26181fSAndroid Build Coastguard Worker 		hdr.linktype = SWAPLONG(hdr.linktype);
220*8b26181fSAndroid Build Coastguard Worker 	}
221*8b26181fSAndroid Build Coastguard Worker 
222*8b26181fSAndroid Build Coastguard Worker 	if (hdr.version_major < PCAP_VERSION_MAJOR) {
223*8b26181fSAndroid Build Coastguard Worker 		snprintf(errbuf, PCAP_ERRBUF_SIZE,
224*8b26181fSAndroid Build Coastguard Worker 		    "archaic pcap savefile format");
225*8b26181fSAndroid Build Coastguard Worker 		*err = 1;
226*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
227*8b26181fSAndroid Build Coastguard Worker 	}
228*8b26181fSAndroid Build Coastguard Worker 
229*8b26181fSAndroid Build Coastguard Worker 	/*
230*8b26181fSAndroid Build Coastguard Worker 	 * currently only versions 2.[0-4] are supported with
231*8b26181fSAndroid Build Coastguard Worker 	 * the exception of 543.0 for DG/UX tcpdump.
232*8b26181fSAndroid Build Coastguard Worker 	 */
233*8b26181fSAndroid Build Coastguard Worker 	if (! ((hdr.version_major == PCAP_VERSION_MAJOR &&
234*8b26181fSAndroid Build Coastguard Worker 		hdr.version_minor <= PCAP_VERSION_MINOR) ||
235*8b26181fSAndroid Build Coastguard Worker 	       (hdr.version_major == 543 &&
236*8b26181fSAndroid Build Coastguard Worker 		hdr.version_minor == 0))) {
237*8b26181fSAndroid Build Coastguard Worker 		snprintf(errbuf, PCAP_ERRBUF_SIZE,
238*8b26181fSAndroid Build Coastguard Worker 			 "unsupported pcap savefile version %u.%u",
239*8b26181fSAndroid Build Coastguard Worker 			 hdr.version_major, hdr.version_minor);
240*8b26181fSAndroid Build Coastguard Worker 		*err = 1;
241*8b26181fSAndroid Build Coastguard Worker 		return NULL;
242*8b26181fSAndroid Build Coastguard Worker 	}
243*8b26181fSAndroid Build Coastguard Worker 
244*8b26181fSAndroid Build Coastguard Worker 	/*
245*8b26181fSAndroid Build Coastguard Worker 	 * OK, this is a good pcap file.
246*8b26181fSAndroid Build Coastguard Worker 	 * Allocate a pcap_t for it.
247*8b26181fSAndroid Build Coastguard Worker 	 */
248*8b26181fSAndroid Build Coastguard Worker 	p = PCAP_OPEN_OFFLINE_COMMON(errbuf, struct pcap_sf);
249*8b26181fSAndroid Build Coastguard Worker 	if (p == NULL) {
250*8b26181fSAndroid Build Coastguard Worker 		/* Allocation failed. */
251*8b26181fSAndroid Build Coastguard Worker 		*err = 1;
252*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
253*8b26181fSAndroid Build Coastguard Worker 	}
254*8b26181fSAndroid Build Coastguard Worker 	p->swapped = swapped;
255*8b26181fSAndroid Build Coastguard Worker 	p->version_major = hdr.version_major;
256*8b26181fSAndroid Build Coastguard Worker 	p->version_minor = hdr.version_minor;
257*8b26181fSAndroid Build Coastguard Worker 	p->linktype = linktype_to_dlt(LT_LINKTYPE(hdr.linktype));
258*8b26181fSAndroid Build Coastguard Worker 	p->linktype_ext = LT_LINKTYPE_EXT(hdr.linktype);
259*8b26181fSAndroid Build Coastguard Worker 	p->snapshot = pcap_adjust_snapshot(p->linktype, hdr.snaplen);
260*8b26181fSAndroid Build Coastguard Worker 
261*8b26181fSAndroid Build Coastguard Worker 	p->next_packet_op = pcap_next_packet;
262*8b26181fSAndroid Build Coastguard Worker 
263*8b26181fSAndroid Build Coastguard Worker 	ps = p->priv;
264*8b26181fSAndroid Build Coastguard Worker 
265*8b26181fSAndroid Build Coastguard Worker 	p->opt.tstamp_precision = precision;
266*8b26181fSAndroid Build Coastguard Worker 
267*8b26181fSAndroid Build Coastguard Worker 	/*
268*8b26181fSAndroid Build Coastguard Worker 	 * Will we need to scale the timestamps to match what the
269*8b26181fSAndroid Build Coastguard Worker 	 * user wants?
270*8b26181fSAndroid Build Coastguard Worker 	 */
271*8b26181fSAndroid Build Coastguard Worker 	switch (precision) {
272*8b26181fSAndroid Build Coastguard Worker 
273*8b26181fSAndroid Build Coastguard Worker 	case PCAP_TSTAMP_PRECISION_MICRO:
274*8b26181fSAndroid Build Coastguard Worker 		if (magic_int == NSEC_TCPDUMP_MAGIC) {
275*8b26181fSAndroid Build Coastguard Worker 			/*
276*8b26181fSAndroid Build Coastguard Worker 			 * The file has nanoseconds, the user
277*8b26181fSAndroid Build Coastguard Worker 			 * wants microseconds; scale the
278*8b26181fSAndroid Build Coastguard Worker 			 * precision down.
279*8b26181fSAndroid Build Coastguard Worker 			 */
280*8b26181fSAndroid Build Coastguard Worker 			ps->scale_type = SCALE_DOWN;
281*8b26181fSAndroid Build Coastguard Worker 		} else {
282*8b26181fSAndroid Build Coastguard Worker 			/*
283*8b26181fSAndroid Build Coastguard Worker 			 * The file has microseconds, the
284*8b26181fSAndroid Build Coastguard Worker 			 * user wants microseconds; nothing to do.
285*8b26181fSAndroid Build Coastguard Worker 			 */
286*8b26181fSAndroid Build Coastguard Worker 			ps->scale_type = PASS_THROUGH;
287*8b26181fSAndroid Build Coastguard Worker 		}
288*8b26181fSAndroid Build Coastguard Worker 		break;
289*8b26181fSAndroid Build Coastguard Worker 
290*8b26181fSAndroid Build Coastguard Worker 	case PCAP_TSTAMP_PRECISION_NANO:
291*8b26181fSAndroid Build Coastguard Worker 		if (magic_int == NSEC_TCPDUMP_MAGIC) {
292*8b26181fSAndroid Build Coastguard Worker 			/*
293*8b26181fSAndroid Build Coastguard Worker 			 * The file has nanoseconds, the
294*8b26181fSAndroid Build Coastguard Worker 			 * user wants nanoseconds; nothing to do.
295*8b26181fSAndroid Build Coastguard Worker 			 */
296*8b26181fSAndroid Build Coastguard Worker 			ps->scale_type = PASS_THROUGH;
297*8b26181fSAndroid Build Coastguard Worker 		} else {
298*8b26181fSAndroid Build Coastguard Worker 			/*
299*8b26181fSAndroid Build Coastguard Worker 			 * The file has microseconds, the user
300*8b26181fSAndroid Build Coastguard Worker 			 * wants nanoseconds; scale the
301*8b26181fSAndroid Build Coastguard Worker 			 * precision up.
302*8b26181fSAndroid Build Coastguard Worker 			 */
303*8b26181fSAndroid Build Coastguard Worker 			ps->scale_type = SCALE_UP;
304*8b26181fSAndroid Build Coastguard Worker 		}
305*8b26181fSAndroid Build Coastguard Worker 		break;
306*8b26181fSAndroid Build Coastguard Worker 
307*8b26181fSAndroid Build Coastguard Worker 	default:
308*8b26181fSAndroid Build Coastguard Worker 		snprintf(errbuf, PCAP_ERRBUF_SIZE,
309*8b26181fSAndroid Build Coastguard Worker 		    "unknown time stamp resolution %u", precision);
310*8b26181fSAndroid Build Coastguard Worker 		free(p);
311*8b26181fSAndroid Build Coastguard Worker 		*err = 1;
312*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
313*8b26181fSAndroid Build Coastguard Worker 	}
314*8b26181fSAndroid Build Coastguard Worker 
315*8b26181fSAndroid Build Coastguard Worker 	/*
316*8b26181fSAndroid Build Coastguard Worker 	 * We interchanged the caplen and len fields at version 2.3,
317*8b26181fSAndroid Build Coastguard Worker 	 * in order to match the bpf header layout.  But unfortunately
318*8b26181fSAndroid Build Coastguard Worker 	 * some files were written with version 2.3 in their headers
319*8b26181fSAndroid Build Coastguard Worker 	 * but without the interchanged fields.
320*8b26181fSAndroid Build Coastguard Worker 	 *
321*8b26181fSAndroid Build Coastguard Worker 	 * In addition, DG/UX tcpdump writes out files with a version
322*8b26181fSAndroid Build Coastguard Worker 	 * number of 543.0, and with the caplen and len fields in the
323*8b26181fSAndroid Build Coastguard Worker 	 * pre-2.3 order.
324*8b26181fSAndroid Build Coastguard Worker 	 */
325*8b26181fSAndroid Build Coastguard Worker 	switch (hdr.version_major) {
326*8b26181fSAndroid Build Coastguard Worker 
327*8b26181fSAndroid Build Coastguard Worker 	case 2:
328*8b26181fSAndroid Build Coastguard Worker 		if (hdr.version_minor < 3)
329*8b26181fSAndroid Build Coastguard Worker 			ps->lengths_swapped = SWAPPED;
330*8b26181fSAndroid Build Coastguard Worker 		else if (hdr.version_minor == 3)
331*8b26181fSAndroid Build Coastguard Worker 			ps->lengths_swapped = MAYBE_SWAPPED;
332*8b26181fSAndroid Build Coastguard Worker 		else
333*8b26181fSAndroid Build Coastguard Worker 			ps->lengths_swapped = NOT_SWAPPED;
334*8b26181fSAndroid Build Coastguard Worker 		break;
335*8b26181fSAndroid Build Coastguard Worker 
336*8b26181fSAndroid Build Coastguard Worker 	case 543:
337*8b26181fSAndroid Build Coastguard Worker 		ps->lengths_swapped = SWAPPED;
338*8b26181fSAndroid Build Coastguard Worker 		break;
339*8b26181fSAndroid Build Coastguard Worker 
340*8b26181fSAndroid Build Coastguard Worker 	default:
341*8b26181fSAndroid Build Coastguard Worker 		ps->lengths_swapped = NOT_SWAPPED;
342*8b26181fSAndroid Build Coastguard Worker 		break;
343*8b26181fSAndroid Build Coastguard Worker 	}
344*8b26181fSAndroid Build Coastguard Worker 
345*8b26181fSAndroid Build Coastguard Worker 	if (magic_int == KUZNETZOV_TCPDUMP_MAGIC) {
346*8b26181fSAndroid Build Coastguard Worker 		/*
347*8b26181fSAndroid Build Coastguard Worker 		 * XXX - the patch that's in some versions of libpcap
348*8b26181fSAndroid Build Coastguard Worker 		 * changes the packet header but not the magic number,
349*8b26181fSAndroid Build Coastguard Worker 		 * and some other versions with this magic number have
350*8b26181fSAndroid Build Coastguard Worker 		 * some extra debugging information in the packet header;
351*8b26181fSAndroid Build Coastguard Worker 		 * we'd have to use some hacks^H^H^H^H^Hheuristics to
352*8b26181fSAndroid Build Coastguard Worker 		 * detect those variants.
353*8b26181fSAndroid Build Coastguard Worker 		 *
354*8b26181fSAndroid Build Coastguard Worker 		 * Ethereal does that, but it does so by trying to read
355*8b26181fSAndroid Build Coastguard Worker 		 * the first two packets of the file with each of the
356*8b26181fSAndroid Build Coastguard Worker 		 * record header formats.  That currently means it seeks
357*8b26181fSAndroid Build Coastguard Worker 		 * backwards and retries the reads, which doesn't work
358*8b26181fSAndroid Build Coastguard Worker 		 * on pipes.  We want to be able to read from a pipe, so
359*8b26181fSAndroid Build Coastguard Worker 		 * that strategy won't work; we'd have to buffer some
360*8b26181fSAndroid Build Coastguard Worker 		 * data ourselves and read from that buffer in order to
361*8b26181fSAndroid Build Coastguard Worker 		 * make that work.
362*8b26181fSAndroid Build Coastguard Worker 		 */
363*8b26181fSAndroid Build Coastguard Worker 		ps->hdrsize = sizeof(struct pcap_sf_patched_pkthdr);
364*8b26181fSAndroid Build Coastguard Worker 
365*8b26181fSAndroid Build Coastguard Worker 		if (p->linktype == DLT_EN10MB) {
366*8b26181fSAndroid Build Coastguard Worker 			/*
367*8b26181fSAndroid Build Coastguard Worker 			 * This capture might have been done in raw mode
368*8b26181fSAndroid Build Coastguard Worker 			 * or cooked mode.
369*8b26181fSAndroid Build Coastguard Worker 			 *
370*8b26181fSAndroid Build Coastguard Worker 			 * If it was done in cooked mode, p->snapshot was
371*8b26181fSAndroid Build Coastguard Worker 			 * passed to recvfrom() as the buffer size, meaning
372*8b26181fSAndroid Build Coastguard Worker 			 * that the most packet data that would be copied
373*8b26181fSAndroid Build Coastguard Worker 			 * would be p->snapshot.  However, a faked Ethernet
374*8b26181fSAndroid Build Coastguard Worker 			 * header would then have been added to it, so the
375*8b26181fSAndroid Build Coastguard Worker 			 * most data that would be in a packet in the file
376*8b26181fSAndroid Build Coastguard Worker 			 * would be p->snapshot + 14.
377*8b26181fSAndroid Build Coastguard Worker 			 *
378*8b26181fSAndroid Build Coastguard Worker 			 * We can't easily tell whether the capture was done
379*8b26181fSAndroid Build Coastguard Worker 			 * in raw mode or cooked mode, so we'll assume it was
380*8b26181fSAndroid Build Coastguard Worker 			 * cooked mode, and add 14 to the snapshot length.
381*8b26181fSAndroid Build Coastguard Worker 			 * That means that, for a raw capture, the snapshot
382*8b26181fSAndroid Build Coastguard Worker 			 * length will be misleading if you use it to figure
383*8b26181fSAndroid Build Coastguard Worker 			 * out why a capture doesn't have all the packet data,
384*8b26181fSAndroid Build Coastguard Worker 			 * but there's not much we can do to avoid that.
385*8b26181fSAndroid Build Coastguard Worker 			 *
386*8b26181fSAndroid Build Coastguard Worker 			 * But don't grow the snapshot length past the
387*8b26181fSAndroid Build Coastguard Worker 			 * maximum value of an int.
388*8b26181fSAndroid Build Coastguard Worker 			 */
389*8b26181fSAndroid Build Coastguard Worker 			if (p->snapshot <= INT_MAX - 14)
390*8b26181fSAndroid Build Coastguard Worker 				p->snapshot += 14;
391*8b26181fSAndroid Build Coastguard Worker 			else
392*8b26181fSAndroid Build Coastguard Worker 				p->snapshot = INT_MAX;
393*8b26181fSAndroid Build Coastguard Worker 		}
394*8b26181fSAndroid Build Coastguard Worker 	} else
395*8b26181fSAndroid Build Coastguard Worker 		ps->hdrsize = sizeof(struct pcap_sf_pkthdr);
396*8b26181fSAndroid Build Coastguard Worker 
397*8b26181fSAndroid Build Coastguard Worker 	/*
398*8b26181fSAndroid Build Coastguard Worker 	 * Allocate a buffer for the packet data.
399*8b26181fSAndroid Build Coastguard Worker 	 * Choose the minimum of the file's snapshot length and 2K bytes;
400*8b26181fSAndroid Build Coastguard Worker 	 * that should be enough for most network packets - we'll grow it
401*8b26181fSAndroid Build Coastguard Worker 	 * if necessary.  That way, we don't allocate a huge chunk of
402*8b26181fSAndroid Build Coastguard Worker 	 * memory just because there's a huge snapshot length, as the
403*8b26181fSAndroid Build Coastguard Worker 	 * snapshot length might be larger than the size of the largest
404*8b26181fSAndroid Build Coastguard Worker 	 * packet.
405*8b26181fSAndroid Build Coastguard Worker 	 */
406*8b26181fSAndroid Build Coastguard Worker 	p->bufsize = p->snapshot;
407*8b26181fSAndroid Build Coastguard Worker 	if (p->bufsize > 2048)
408*8b26181fSAndroid Build Coastguard Worker 		p->bufsize = 2048;
409*8b26181fSAndroid Build Coastguard Worker 	p->buffer = malloc(p->bufsize);
410*8b26181fSAndroid Build Coastguard Worker 	if (p->buffer == NULL) {
411*8b26181fSAndroid Build Coastguard Worker 		snprintf(errbuf, PCAP_ERRBUF_SIZE, "out of memory");
412*8b26181fSAndroid Build Coastguard Worker 		free(p);
413*8b26181fSAndroid Build Coastguard Worker 		*err = 1;
414*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
415*8b26181fSAndroid Build Coastguard Worker 	}
416*8b26181fSAndroid Build Coastguard Worker 
417*8b26181fSAndroid Build Coastguard Worker 	p->cleanup_op = sf_cleanup;
418*8b26181fSAndroid Build Coastguard Worker 
419*8b26181fSAndroid Build Coastguard Worker 	return (p);
420*8b26181fSAndroid Build Coastguard Worker }
421*8b26181fSAndroid Build Coastguard Worker 
422*8b26181fSAndroid Build Coastguard Worker /*
423*8b26181fSAndroid Build Coastguard Worker  * Grow the packet buffer to the specified size.
424*8b26181fSAndroid Build Coastguard Worker  */
425*8b26181fSAndroid Build Coastguard Worker static int
grow_buffer(pcap_t * p,u_int bufsize)426*8b26181fSAndroid Build Coastguard Worker grow_buffer(pcap_t *p, u_int bufsize)
427*8b26181fSAndroid Build Coastguard Worker {
428*8b26181fSAndroid Build Coastguard Worker 	void *bigger_buffer;
429*8b26181fSAndroid Build Coastguard Worker 
430*8b26181fSAndroid Build Coastguard Worker 	bigger_buffer = realloc(p->buffer, bufsize);
431*8b26181fSAndroid Build Coastguard Worker 	if (bigger_buffer == NULL) {
432*8b26181fSAndroid Build Coastguard Worker 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "out of memory");
433*8b26181fSAndroid Build Coastguard Worker 		return (0);
434*8b26181fSAndroid Build Coastguard Worker 	}
435*8b26181fSAndroid Build Coastguard Worker 	p->buffer = bigger_buffer;
436*8b26181fSAndroid Build Coastguard Worker 	p->bufsize = bufsize;
437*8b26181fSAndroid Build Coastguard Worker 	return (1);
438*8b26181fSAndroid Build Coastguard Worker }
439*8b26181fSAndroid Build Coastguard Worker 
440*8b26181fSAndroid Build Coastguard Worker /*
441*8b26181fSAndroid Build Coastguard Worker  * Read and return the next packet from the savefile.  Return the header
442*8b26181fSAndroid Build Coastguard Worker  * in hdr and a pointer to the contents in data.  Return 1 on success, 0
443*8b26181fSAndroid Build Coastguard Worker  * if there were no more packets, and -1 on an error.
444*8b26181fSAndroid Build Coastguard Worker  */
445*8b26181fSAndroid Build Coastguard Worker static int
pcap_next_packet(pcap_t * p,struct pcap_pkthdr * hdr,u_char ** data)446*8b26181fSAndroid Build Coastguard Worker pcap_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char **data)
447*8b26181fSAndroid Build Coastguard Worker {
448*8b26181fSAndroid Build Coastguard Worker 	struct pcap_sf *ps = p->priv;
449*8b26181fSAndroid Build Coastguard Worker 	struct pcap_sf_patched_pkthdr sf_hdr;
450*8b26181fSAndroid Build Coastguard Worker 	FILE *fp = p->rfile;
451*8b26181fSAndroid Build Coastguard Worker 	size_t amt_read;
452*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32 t;
453*8b26181fSAndroid Build Coastguard Worker 
454*8b26181fSAndroid Build Coastguard Worker 	/*
455*8b26181fSAndroid Build Coastguard Worker 	 * Read the packet header; the structure we use as a buffer
456*8b26181fSAndroid Build Coastguard Worker 	 * is the longer structure for files generated by the patched
457*8b26181fSAndroid Build Coastguard Worker 	 * libpcap, but if the file has the magic number for an
458*8b26181fSAndroid Build Coastguard Worker 	 * unpatched libpcap we only read as many bytes as the regular
459*8b26181fSAndroid Build Coastguard Worker 	 * header has.
460*8b26181fSAndroid Build Coastguard Worker 	 */
461*8b26181fSAndroid Build Coastguard Worker 	amt_read = fread(&sf_hdr, 1, ps->hdrsize, fp);
462*8b26181fSAndroid Build Coastguard Worker 	if (amt_read != ps->hdrsize) {
463*8b26181fSAndroid Build Coastguard Worker 		if (ferror(fp)) {
464*8b26181fSAndroid Build Coastguard Worker 			pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
465*8b26181fSAndroid Build Coastguard Worker 			    errno, "error reading dump file");
466*8b26181fSAndroid Build Coastguard Worker 			return (-1);
467*8b26181fSAndroid Build Coastguard Worker 		} else {
468*8b26181fSAndroid Build Coastguard Worker 			if (amt_read != 0) {
469*8b26181fSAndroid Build Coastguard Worker 				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
470*8b26181fSAndroid Build Coastguard Worker 				    "truncated dump file; tried to read %zu header bytes, only got %zu",
471*8b26181fSAndroid Build Coastguard Worker 				    ps->hdrsize, amt_read);
472*8b26181fSAndroid Build Coastguard Worker 				return (-1);
473*8b26181fSAndroid Build Coastguard Worker 			}
474*8b26181fSAndroid Build Coastguard Worker 			/* EOF */
475*8b26181fSAndroid Build Coastguard Worker 			return (0);
476*8b26181fSAndroid Build Coastguard Worker 		}
477*8b26181fSAndroid Build Coastguard Worker 	}
478*8b26181fSAndroid Build Coastguard Worker 
479*8b26181fSAndroid Build Coastguard Worker 	if (p->swapped) {
480*8b26181fSAndroid Build Coastguard Worker 		/* these were written in opposite byte order */
481*8b26181fSAndroid Build Coastguard Worker 		hdr->caplen = SWAPLONG(sf_hdr.caplen);
482*8b26181fSAndroid Build Coastguard Worker 		hdr->len = SWAPLONG(sf_hdr.len);
483*8b26181fSAndroid Build Coastguard Worker 		hdr->ts.tv_sec = SWAPLONG(sf_hdr.ts.tv_sec);
484*8b26181fSAndroid Build Coastguard Worker 		hdr->ts.tv_usec = SWAPLONG(sf_hdr.ts.tv_usec);
485*8b26181fSAndroid Build Coastguard Worker 	} else {
486*8b26181fSAndroid Build Coastguard Worker 		hdr->caplen = sf_hdr.caplen;
487*8b26181fSAndroid Build Coastguard Worker 		hdr->len = sf_hdr.len;
488*8b26181fSAndroid Build Coastguard Worker 		hdr->ts.tv_sec = sf_hdr.ts.tv_sec;
489*8b26181fSAndroid Build Coastguard Worker 		hdr->ts.tv_usec = sf_hdr.ts.tv_usec;
490*8b26181fSAndroid Build Coastguard Worker 	}
491*8b26181fSAndroid Build Coastguard Worker 
492*8b26181fSAndroid Build Coastguard Worker 	switch (ps->scale_type) {
493*8b26181fSAndroid Build Coastguard Worker 
494*8b26181fSAndroid Build Coastguard Worker 	case PASS_THROUGH:
495*8b26181fSAndroid Build Coastguard Worker 		/*
496*8b26181fSAndroid Build Coastguard Worker 		 * Just pass the time stamp through.
497*8b26181fSAndroid Build Coastguard Worker 		 */
498*8b26181fSAndroid Build Coastguard Worker 		break;
499*8b26181fSAndroid Build Coastguard Worker 
500*8b26181fSAndroid Build Coastguard Worker 	case SCALE_UP:
501*8b26181fSAndroid Build Coastguard Worker 		/*
502*8b26181fSAndroid Build Coastguard Worker 		 * File has microseconds, user wants nanoseconds; convert
503*8b26181fSAndroid Build Coastguard Worker 		 * it.
504*8b26181fSAndroid Build Coastguard Worker 		 */
505*8b26181fSAndroid Build Coastguard Worker 		hdr->ts.tv_usec = hdr->ts.tv_usec * 1000;
506*8b26181fSAndroid Build Coastguard Worker 		break;
507*8b26181fSAndroid Build Coastguard Worker 
508*8b26181fSAndroid Build Coastguard Worker 	case SCALE_DOWN:
509*8b26181fSAndroid Build Coastguard Worker 		/*
510*8b26181fSAndroid Build Coastguard Worker 		 * File has nanoseconds, user wants microseconds; convert
511*8b26181fSAndroid Build Coastguard Worker 		 * it.
512*8b26181fSAndroid Build Coastguard Worker 		 */
513*8b26181fSAndroid Build Coastguard Worker 		hdr->ts.tv_usec = hdr->ts.tv_usec / 1000;
514*8b26181fSAndroid Build Coastguard Worker 		break;
515*8b26181fSAndroid Build Coastguard Worker 	}
516*8b26181fSAndroid Build Coastguard Worker 
517*8b26181fSAndroid Build Coastguard Worker 	/* Swap the caplen and len fields, if necessary. */
518*8b26181fSAndroid Build Coastguard Worker 	switch (ps->lengths_swapped) {
519*8b26181fSAndroid Build Coastguard Worker 
520*8b26181fSAndroid Build Coastguard Worker 	case NOT_SWAPPED:
521*8b26181fSAndroid Build Coastguard Worker 		break;
522*8b26181fSAndroid Build Coastguard Worker 
523*8b26181fSAndroid Build Coastguard Worker 	case MAYBE_SWAPPED:
524*8b26181fSAndroid Build Coastguard Worker 		if (hdr->caplen <= hdr->len) {
525*8b26181fSAndroid Build Coastguard Worker 			/*
526*8b26181fSAndroid Build Coastguard Worker 			 * The captured length is <= the actual length,
527*8b26181fSAndroid Build Coastguard Worker 			 * so presumably they weren't swapped.
528*8b26181fSAndroid Build Coastguard Worker 			 */
529*8b26181fSAndroid Build Coastguard Worker 			break;
530*8b26181fSAndroid Build Coastguard Worker 		}
531*8b26181fSAndroid Build Coastguard Worker 		/* FALLTHROUGH */
532*8b26181fSAndroid Build Coastguard Worker 
533*8b26181fSAndroid Build Coastguard Worker 	case SWAPPED:
534*8b26181fSAndroid Build Coastguard Worker 		t = hdr->caplen;
535*8b26181fSAndroid Build Coastguard Worker 		hdr->caplen = hdr->len;
536*8b26181fSAndroid Build Coastguard Worker 		hdr->len = t;
537*8b26181fSAndroid Build Coastguard Worker 		break;
538*8b26181fSAndroid Build Coastguard Worker 	}
539*8b26181fSAndroid Build Coastguard Worker 
540*8b26181fSAndroid Build Coastguard Worker 	/*
541*8b26181fSAndroid Build Coastguard Worker 	 * Is the packet bigger than we consider sane?
542*8b26181fSAndroid Build Coastguard Worker 	 */
543*8b26181fSAndroid Build Coastguard Worker 	if (hdr->caplen > max_snaplen_for_dlt(p->linktype)) {
544*8b26181fSAndroid Build Coastguard Worker 		/*
545*8b26181fSAndroid Build Coastguard Worker 		 * Yes.  This may be a damaged or fuzzed file.
546*8b26181fSAndroid Build Coastguard Worker 		 *
547*8b26181fSAndroid Build Coastguard Worker 		 * Is it bigger than the snapshot length?
548*8b26181fSAndroid Build Coastguard Worker 		 * (We don't treat that as an error if it's not
549*8b26181fSAndroid Build Coastguard Worker 		 * bigger than the maximum we consider sane; see
550*8b26181fSAndroid Build Coastguard Worker 		 * below.)
551*8b26181fSAndroid Build Coastguard Worker 		 */
552*8b26181fSAndroid Build Coastguard Worker 		if (hdr->caplen > (bpf_u_int32)p->snapshot) {
553*8b26181fSAndroid Build Coastguard Worker 			snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
554*8b26181fSAndroid Build Coastguard Worker 			    "invalid packet capture length %u, bigger than "
555*8b26181fSAndroid Build Coastguard Worker 			    "snaplen of %d", hdr->caplen, p->snapshot);
556*8b26181fSAndroid Build Coastguard Worker 		} else {
557*8b26181fSAndroid Build Coastguard Worker 			snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
558*8b26181fSAndroid Build Coastguard Worker 			    "invalid packet capture length %u, bigger than "
559*8b26181fSAndroid Build Coastguard Worker 			    "maximum of %u", hdr->caplen,
560*8b26181fSAndroid Build Coastguard Worker 			    max_snaplen_for_dlt(p->linktype));
561*8b26181fSAndroid Build Coastguard Worker 		}
562*8b26181fSAndroid Build Coastguard Worker 		return (-1);
563*8b26181fSAndroid Build Coastguard Worker 	}
564*8b26181fSAndroid Build Coastguard Worker 
565*8b26181fSAndroid Build Coastguard Worker 	if (hdr->caplen > (bpf_u_int32)p->snapshot) {
566*8b26181fSAndroid Build Coastguard Worker 		/*
567*8b26181fSAndroid Build Coastguard Worker 		 * The packet is bigger than the snapshot length
568*8b26181fSAndroid Build Coastguard Worker 		 * for this file.
569*8b26181fSAndroid Build Coastguard Worker 		 *
570*8b26181fSAndroid Build Coastguard Worker 		 * This can happen due to Solaris 2.3 systems tripping
571*8b26181fSAndroid Build Coastguard Worker 		 * over the BUFMOD problem and not setting the snapshot
572*8b26181fSAndroid Build Coastguard Worker 		 * length correctly in the savefile header.
573*8b26181fSAndroid Build Coastguard Worker 		 *
574*8b26181fSAndroid Build Coastguard Worker 		 * libpcap 0.4 and later on Solaris 2.3 should set the
575*8b26181fSAndroid Build Coastguard Worker 		 * snapshot length correctly in the pcap file header,
576*8b26181fSAndroid Build Coastguard Worker 		 * even though they don't set a snapshot length in bufmod
577*8b26181fSAndroid Build Coastguard Worker 		 * (the buggy bufmod chops off the *beginning* of the
578*8b26181fSAndroid Build Coastguard Worker 		 * packet if a snapshot length is specified); they should
579*8b26181fSAndroid Build Coastguard Worker 		 * also reduce the captured length, as supplied to the
580*8b26181fSAndroid Build Coastguard Worker 		 * per-packet callback, to the snapshot length if it's
581*8b26181fSAndroid Build Coastguard Worker 		 * greater than the snapshot length, so the code using
582*8b26181fSAndroid Build Coastguard Worker 		 * libpcap should see the packet cut off at the snapshot
583*8b26181fSAndroid Build Coastguard Worker 		 * length, even though the full packet is copied up to
584*8b26181fSAndroid Build Coastguard Worker 		 * userland.
585*8b26181fSAndroid Build Coastguard Worker 		 *
586*8b26181fSAndroid Build Coastguard Worker 		 * However, perhaps some versions of libpcap failed to
587*8b26181fSAndroid Build Coastguard Worker 		 * set the snapshot length correctly in the file header
588*8b26181fSAndroid Build Coastguard Worker 		 * or the per-packet header, or perhaps this is a
589*8b26181fSAndroid Build Coastguard Worker 		 * corrupted safefile or a savefile built/modified by a
590*8b26181fSAndroid Build Coastguard Worker 		 * fuzz tester, so we check anyway.  We grow the buffer
591*8b26181fSAndroid Build Coastguard Worker 		 * to be big enough for the snapshot length, read up
592*8b26181fSAndroid Build Coastguard Worker 		 * to the snapshot length, discard the rest of the
593*8b26181fSAndroid Build Coastguard Worker 		 * packet, and report the snapshot length as the captured
594*8b26181fSAndroid Build Coastguard Worker 		 * length; we don't want to hand our caller a packet
595*8b26181fSAndroid Build Coastguard Worker 		 * bigger than the snapshot length, because they might
596*8b26181fSAndroid Build Coastguard Worker 		 * be assuming they'll never be handed such a packet,
597*8b26181fSAndroid Build Coastguard Worker 		 * and might copy the packet into a snapshot-length-
598*8b26181fSAndroid Build Coastguard Worker 		 * sized buffer, assuming it'll fit.
599*8b26181fSAndroid Build Coastguard Worker 		 */
600*8b26181fSAndroid Build Coastguard Worker 		size_t bytes_to_discard;
601*8b26181fSAndroid Build Coastguard Worker 		size_t bytes_to_read, bytes_read;
602*8b26181fSAndroid Build Coastguard Worker 		char discard_buf[4096];
603*8b26181fSAndroid Build Coastguard Worker 
604*8b26181fSAndroid Build Coastguard Worker 		if (hdr->caplen > p->bufsize) {
605*8b26181fSAndroid Build Coastguard Worker 			/*
606*8b26181fSAndroid Build Coastguard Worker 			 * Grow the buffer to the snapshot length.
607*8b26181fSAndroid Build Coastguard Worker 			 */
608*8b26181fSAndroid Build Coastguard Worker 			if (!grow_buffer(p, p->snapshot))
609*8b26181fSAndroid Build Coastguard Worker 				return (-1);
610*8b26181fSAndroid Build Coastguard Worker 		}
611*8b26181fSAndroid Build Coastguard Worker 
612*8b26181fSAndroid Build Coastguard Worker 		/*
613*8b26181fSAndroid Build Coastguard Worker 		 * Read the first p->snapshot bytes into the buffer.
614*8b26181fSAndroid Build Coastguard Worker 		 */
615*8b26181fSAndroid Build Coastguard Worker 		amt_read = fread(p->buffer, 1, p->snapshot, fp);
616*8b26181fSAndroid Build Coastguard Worker 		if (amt_read != (bpf_u_int32)p->snapshot) {
617*8b26181fSAndroid Build Coastguard Worker 			if (ferror(fp)) {
618*8b26181fSAndroid Build Coastguard Worker 				pcap_fmt_errmsg_for_errno(p->errbuf,
619*8b26181fSAndroid Build Coastguard Worker 				     PCAP_ERRBUF_SIZE, errno,
620*8b26181fSAndroid Build Coastguard Worker 				    "error reading dump file");
621*8b26181fSAndroid Build Coastguard Worker 			} else {
622*8b26181fSAndroid Build Coastguard Worker 				/*
623*8b26181fSAndroid Build Coastguard Worker 				 * Yes, this uses hdr->caplen; technically,
624*8b26181fSAndroid Build Coastguard Worker 				 * it's true, because we would try to read
625*8b26181fSAndroid Build Coastguard Worker 				 * and discard the rest of those bytes, and
626*8b26181fSAndroid Build Coastguard Worker 				 * that would fail because we got EOF before
627*8b26181fSAndroid Build Coastguard Worker 				 * the read finished.
628*8b26181fSAndroid Build Coastguard Worker 				 */
629*8b26181fSAndroid Build Coastguard Worker 				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
630*8b26181fSAndroid Build Coastguard Worker 				    "truncated dump file; tried to read %d captured bytes, only got %zu",
631*8b26181fSAndroid Build Coastguard Worker 				    p->snapshot, amt_read);
632*8b26181fSAndroid Build Coastguard Worker 			}
633*8b26181fSAndroid Build Coastguard Worker 			return (-1);
634*8b26181fSAndroid Build Coastguard Worker 		}
635*8b26181fSAndroid Build Coastguard Worker 
636*8b26181fSAndroid Build Coastguard Worker 		/*
637*8b26181fSAndroid Build Coastguard Worker 		 * Now read and discard what's left.
638*8b26181fSAndroid Build Coastguard Worker 		 */
639*8b26181fSAndroid Build Coastguard Worker 		bytes_to_discard = hdr->caplen - p->snapshot;
640*8b26181fSAndroid Build Coastguard Worker 		bytes_read = amt_read;
641*8b26181fSAndroid Build Coastguard Worker 		while (bytes_to_discard != 0) {
642*8b26181fSAndroid Build Coastguard Worker 			bytes_to_read = bytes_to_discard;
643*8b26181fSAndroid Build Coastguard Worker 			if (bytes_to_read > sizeof (discard_buf))
644*8b26181fSAndroid Build Coastguard Worker 				bytes_to_read = sizeof (discard_buf);
645*8b26181fSAndroid Build Coastguard Worker 			amt_read = fread(discard_buf, 1, bytes_to_read, fp);
646*8b26181fSAndroid Build Coastguard Worker 			bytes_read += amt_read;
647*8b26181fSAndroid Build Coastguard Worker 			if (amt_read != bytes_to_read) {
648*8b26181fSAndroid Build Coastguard Worker 				if (ferror(fp)) {
649*8b26181fSAndroid Build Coastguard Worker 					pcap_fmt_errmsg_for_errno(p->errbuf,
650*8b26181fSAndroid Build Coastguard Worker 					    PCAP_ERRBUF_SIZE, errno,
651*8b26181fSAndroid Build Coastguard Worker 					    "error reading dump file");
652*8b26181fSAndroid Build Coastguard Worker 				} else {
653*8b26181fSAndroid Build Coastguard Worker 					snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
654*8b26181fSAndroid Build Coastguard Worker 					    "truncated dump file; tried to read %u captured bytes, only got %zu",
655*8b26181fSAndroid Build Coastguard Worker 					    hdr->caplen, bytes_read);
656*8b26181fSAndroid Build Coastguard Worker 				}
657*8b26181fSAndroid Build Coastguard Worker 				return (-1);
658*8b26181fSAndroid Build Coastguard Worker 			}
659*8b26181fSAndroid Build Coastguard Worker 			bytes_to_discard -= amt_read;
660*8b26181fSAndroid Build Coastguard Worker 		}
661*8b26181fSAndroid Build Coastguard Worker 
662*8b26181fSAndroid Build Coastguard Worker 		/*
663*8b26181fSAndroid Build Coastguard Worker 		 * Adjust caplen accordingly, so we don't get confused later
664*8b26181fSAndroid Build Coastguard Worker 		 * as to how many bytes we have to play with.
665*8b26181fSAndroid Build Coastguard Worker 		 */
666*8b26181fSAndroid Build Coastguard Worker 		hdr->caplen = p->snapshot;
667*8b26181fSAndroid Build Coastguard Worker 	} else {
668*8b26181fSAndroid Build Coastguard Worker 		/*
669*8b26181fSAndroid Build Coastguard Worker 		 * The packet is within the snapshot length for this file.
670*8b26181fSAndroid Build Coastguard Worker 		 */
671*8b26181fSAndroid Build Coastguard Worker 		if (hdr->caplen > p->bufsize) {
672*8b26181fSAndroid Build Coastguard Worker 			/*
673*8b26181fSAndroid Build Coastguard Worker 			 * Grow the buffer to the next power of 2, or
674*8b26181fSAndroid Build Coastguard Worker 			 * the snaplen, whichever is lower.
675*8b26181fSAndroid Build Coastguard Worker 			 */
676*8b26181fSAndroid Build Coastguard Worker 			u_int new_bufsize;
677*8b26181fSAndroid Build Coastguard Worker 
678*8b26181fSAndroid Build Coastguard Worker 			new_bufsize = hdr->caplen;
679*8b26181fSAndroid Build Coastguard Worker 			/*
680*8b26181fSAndroid Build Coastguard Worker 			 * https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
681*8b26181fSAndroid Build Coastguard Worker 			 */
682*8b26181fSAndroid Build Coastguard Worker 			new_bufsize--;
683*8b26181fSAndroid Build Coastguard Worker 			new_bufsize |= new_bufsize >> 1;
684*8b26181fSAndroid Build Coastguard Worker 			new_bufsize |= new_bufsize >> 2;
685*8b26181fSAndroid Build Coastguard Worker 			new_bufsize |= new_bufsize >> 4;
686*8b26181fSAndroid Build Coastguard Worker 			new_bufsize |= new_bufsize >> 8;
687*8b26181fSAndroid Build Coastguard Worker 			new_bufsize |= new_bufsize >> 16;
688*8b26181fSAndroid Build Coastguard Worker 			new_bufsize++;
689*8b26181fSAndroid Build Coastguard Worker 
690*8b26181fSAndroid Build Coastguard Worker 			if (new_bufsize > (u_int)p->snapshot)
691*8b26181fSAndroid Build Coastguard Worker 				new_bufsize = p->snapshot;
692*8b26181fSAndroid Build Coastguard Worker 
693*8b26181fSAndroid Build Coastguard Worker 			if (!grow_buffer(p, new_bufsize))
694*8b26181fSAndroid Build Coastguard Worker 				return (-1);
695*8b26181fSAndroid Build Coastguard Worker 		}
696*8b26181fSAndroid Build Coastguard Worker 
697*8b26181fSAndroid Build Coastguard Worker 		/* read the packet itself */
698*8b26181fSAndroid Build Coastguard Worker 		amt_read = fread(p->buffer, 1, hdr->caplen, fp);
699*8b26181fSAndroid Build Coastguard Worker 		if (amt_read != hdr->caplen) {
700*8b26181fSAndroid Build Coastguard Worker 			if (ferror(fp)) {
701*8b26181fSAndroid Build Coastguard Worker 				pcap_fmt_errmsg_for_errno(p->errbuf,
702*8b26181fSAndroid Build Coastguard Worker 				    PCAP_ERRBUF_SIZE, errno,
703*8b26181fSAndroid Build Coastguard Worker 				    "error reading dump file");
704*8b26181fSAndroid Build Coastguard Worker 			} else {
705*8b26181fSAndroid Build Coastguard Worker 				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
706*8b26181fSAndroid Build Coastguard Worker 				    "truncated dump file; tried to read %u captured bytes, only got %zu",
707*8b26181fSAndroid Build Coastguard Worker 				    hdr->caplen, amt_read);
708*8b26181fSAndroid Build Coastguard Worker 			}
709*8b26181fSAndroid Build Coastguard Worker 			return (-1);
710*8b26181fSAndroid Build Coastguard Worker 		}
711*8b26181fSAndroid Build Coastguard Worker 	}
712*8b26181fSAndroid Build Coastguard Worker 	*data = p->buffer;
713*8b26181fSAndroid Build Coastguard Worker 
714*8b26181fSAndroid Build Coastguard Worker 	pcap_post_process(p->linktype, p->swapped, hdr, *data);
715*8b26181fSAndroid Build Coastguard Worker 
716*8b26181fSAndroid Build Coastguard Worker 	return (1);
717*8b26181fSAndroid Build Coastguard Worker }
718*8b26181fSAndroid Build Coastguard Worker 
719*8b26181fSAndroid Build Coastguard Worker static int
sf_write_header(pcap_t * p,FILE * fp,int linktype,int snaplen)720*8b26181fSAndroid Build Coastguard Worker sf_write_header(pcap_t *p, FILE *fp, int linktype, int snaplen)
721*8b26181fSAndroid Build Coastguard Worker {
722*8b26181fSAndroid Build Coastguard Worker 	struct pcap_file_header hdr;
723*8b26181fSAndroid Build Coastguard Worker 
724*8b26181fSAndroid Build Coastguard Worker 	hdr.magic = p->opt.tstamp_precision == PCAP_TSTAMP_PRECISION_NANO ? NSEC_TCPDUMP_MAGIC : TCPDUMP_MAGIC;
725*8b26181fSAndroid Build Coastguard Worker 	hdr.version_major = PCAP_VERSION_MAJOR;
726*8b26181fSAndroid Build Coastguard Worker 	hdr.version_minor = PCAP_VERSION_MINOR;
727*8b26181fSAndroid Build Coastguard Worker 
728*8b26181fSAndroid Build Coastguard Worker 	/*
729*8b26181fSAndroid Build Coastguard Worker 	 * https://www.tcpdump.org/manpages/pcap-savefile.5.txt states:
730*8b26181fSAndroid Build Coastguard Worker 	 * thiszone: 4-byte time zone offset; this is always 0.
731*8b26181fSAndroid Build Coastguard Worker 	 * sigfigs:  4-byte number giving the accuracy of time stamps
732*8b26181fSAndroid Build Coastguard Worker 	 *           in the file; this is always 0.
733*8b26181fSAndroid Build Coastguard Worker 	 */
734*8b26181fSAndroid Build Coastguard Worker 	hdr.thiszone = 0;
735*8b26181fSAndroid Build Coastguard Worker 	hdr.sigfigs = 0;
736*8b26181fSAndroid Build Coastguard Worker 	hdr.snaplen = snaplen;
737*8b26181fSAndroid Build Coastguard Worker 	hdr.linktype = linktype;
738*8b26181fSAndroid Build Coastguard Worker 
739*8b26181fSAndroid Build Coastguard Worker 	if (fwrite((char *)&hdr, sizeof(hdr), 1, fp) != 1)
740*8b26181fSAndroid Build Coastguard Worker 		return (-1);
741*8b26181fSAndroid Build Coastguard Worker 
742*8b26181fSAndroid Build Coastguard Worker 	return (0);
743*8b26181fSAndroid Build Coastguard Worker }
744*8b26181fSAndroid Build Coastguard Worker 
745*8b26181fSAndroid Build Coastguard Worker /*
746*8b26181fSAndroid Build Coastguard Worker  * Output a packet to the initialized dump file.
747*8b26181fSAndroid Build Coastguard Worker  */
748*8b26181fSAndroid Build Coastguard Worker void
pcap_dump(u_char * user,const struct pcap_pkthdr * h,const u_char * sp)749*8b26181fSAndroid Build Coastguard Worker pcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
750*8b26181fSAndroid Build Coastguard Worker {
751*8b26181fSAndroid Build Coastguard Worker 	register FILE *f;
752*8b26181fSAndroid Build Coastguard Worker 	struct pcap_sf_pkthdr sf_hdr;
753*8b26181fSAndroid Build Coastguard Worker 
754*8b26181fSAndroid Build Coastguard Worker 	f = (FILE *)user;
755*8b26181fSAndroid Build Coastguard Worker 	/*
756*8b26181fSAndroid Build Coastguard Worker 	 * If the output file handle is in an error state, don't write
757*8b26181fSAndroid Build Coastguard Worker 	 * anything.
758*8b26181fSAndroid Build Coastguard Worker 	 *
759*8b26181fSAndroid Build Coastguard Worker 	 * While in principle a file handle can return from an error state
760*8b26181fSAndroid Build Coastguard Worker 	 * to a normal state (for example if a disk that is full has space
761*8b26181fSAndroid Build Coastguard Worker 	 * freed), we have possibly left a broken file already, and won't
762*8b26181fSAndroid Build Coastguard Worker 	 * be able to clean it up. The safest option is to do nothing.
763*8b26181fSAndroid Build Coastguard Worker 	 *
764*8b26181fSAndroid Build Coastguard Worker 	 * Note that if we could guarantee that fwrite() was atomic we
765*8b26181fSAndroid Build Coastguard Worker 	 * might be able to insure that we don't produce a corrupted file,
766*8b26181fSAndroid Build Coastguard Worker 	 * but the standard defines fwrite() as a series of fputc() calls,
767*8b26181fSAndroid Build Coastguard Worker 	 * so we really have no insurance that things are not fubared.
768*8b26181fSAndroid Build Coastguard Worker 	 *
769*8b26181fSAndroid Build Coastguard Worker 	 * http://pubs.opengroup.org/onlinepubs/009695399/functions/fwrite.html
770*8b26181fSAndroid Build Coastguard Worker 	 */
771*8b26181fSAndroid Build Coastguard Worker 	if (ferror(f))
772*8b26181fSAndroid Build Coastguard Worker 		return;
773*8b26181fSAndroid Build Coastguard Worker 	/*
774*8b26181fSAndroid Build Coastguard Worker 	 * Better not try writing pcap files after
775*8b26181fSAndroid Build Coastguard Worker 	 * 2038-01-19 03:14:07 UTC; switch to pcapng.
776*8b26181fSAndroid Build Coastguard Worker 	 */
777*8b26181fSAndroid Build Coastguard Worker 	sf_hdr.ts.tv_sec  = (bpf_int32)h->ts.tv_sec;
778*8b26181fSAndroid Build Coastguard Worker 	sf_hdr.ts.tv_usec = (bpf_int32)h->ts.tv_usec;
779*8b26181fSAndroid Build Coastguard Worker 	sf_hdr.caplen     = h->caplen;
780*8b26181fSAndroid Build Coastguard Worker 	sf_hdr.len        = h->len;
781*8b26181fSAndroid Build Coastguard Worker 	/*
782*8b26181fSAndroid Build Coastguard Worker 	 * We only write the packet if we can write the header properly.
783*8b26181fSAndroid Build Coastguard Worker 	 *
784*8b26181fSAndroid Build Coastguard Worker 	 * This doesn't prevent us from having corrupted output, and if we
785*8b26181fSAndroid Build Coastguard Worker 	 * for some reason don't get a complete write we don't have any
786*8b26181fSAndroid Build Coastguard Worker 	 * way to set ferror() to prevent future writes from being
787*8b26181fSAndroid Build Coastguard Worker 	 * attempted, but it is better than nothing.
788*8b26181fSAndroid Build Coastguard Worker 	 */
789*8b26181fSAndroid Build Coastguard Worker 	if (fwrite(&sf_hdr, sizeof(sf_hdr), 1, f) == 1) {
790*8b26181fSAndroid Build Coastguard Worker 		(void)fwrite(sp, h->caplen, 1, f);
791*8b26181fSAndroid Build Coastguard Worker 	}
792*8b26181fSAndroid Build Coastguard Worker }
793*8b26181fSAndroid Build Coastguard Worker 
794*8b26181fSAndroid Build Coastguard Worker static pcap_dumper_t *
pcap_setup_dump(pcap_t * p,int linktype,FILE * f,const char * fname)795*8b26181fSAndroid Build Coastguard Worker pcap_setup_dump(pcap_t *p, int linktype, FILE *f, const char *fname)
796*8b26181fSAndroid Build Coastguard Worker {
797*8b26181fSAndroid Build Coastguard Worker 
798*8b26181fSAndroid Build Coastguard Worker #if defined(_WIN32) || defined(MSDOS)
799*8b26181fSAndroid Build Coastguard Worker 	/*
800*8b26181fSAndroid Build Coastguard Worker 	 * If we're writing to the standard output, put it in binary
801*8b26181fSAndroid Build Coastguard Worker 	 * mode, as savefiles are binary files.
802*8b26181fSAndroid Build Coastguard Worker 	 *
803*8b26181fSAndroid Build Coastguard Worker 	 * Otherwise, we turn off buffering.
804*8b26181fSAndroid Build Coastguard Worker 	 * XXX - why?  And why not on the standard output?
805*8b26181fSAndroid Build Coastguard Worker 	 */
806*8b26181fSAndroid Build Coastguard Worker 	if (f == stdout)
807*8b26181fSAndroid Build Coastguard Worker 		SET_BINMODE(f);
808*8b26181fSAndroid Build Coastguard Worker 	else
809*8b26181fSAndroid Build Coastguard Worker 		setvbuf(f, NULL, _IONBF, 0);
810*8b26181fSAndroid Build Coastguard Worker #endif
811*8b26181fSAndroid Build Coastguard Worker 	if (sf_write_header(p, f, linktype, p->snapshot) == -1) {
812*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
813*8b26181fSAndroid Build Coastguard Worker 		    errno, "Can't write to %s", fname);
814*8b26181fSAndroid Build Coastguard Worker 		if (f != stdout)
815*8b26181fSAndroid Build Coastguard Worker 			(void)fclose(f);
816*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
817*8b26181fSAndroid Build Coastguard Worker 	}
818*8b26181fSAndroid Build Coastguard Worker 	return ((pcap_dumper_t *)f);
819*8b26181fSAndroid Build Coastguard Worker }
820*8b26181fSAndroid Build Coastguard Worker 
821*8b26181fSAndroid Build Coastguard Worker /*
822*8b26181fSAndroid Build Coastguard Worker  * Initialize so that sf_write() will output to the file named 'fname'.
823*8b26181fSAndroid Build Coastguard Worker  */
824*8b26181fSAndroid Build Coastguard Worker pcap_dumper_t *
pcap_dump_open(pcap_t * p,const char * fname)825*8b26181fSAndroid Build Coastguard Worker pcap_dump_open(pcap_t *p, const char *fname)
826*8b26181fSAndroid Build Coastguard Worker {
827*8b26181fSAndroid Build Coastguard Worker 	FILE *f;
828*8b26181fSAndroid Build Coastguard Worker 	int linktype;
829*8b26181fSAndroid Build Coastguard Worker 
830*8b26181fSAndroid Build Coastguard Worker 	/*
831*8b26181fSAndroid Build Coastguard Worker 	 * If this pcap_t hasn't been activated, it doesn't have a
832*8b26181fSAndroid Build Coastguard Worker 	 * link-layer type, so we can't use it.
833*8b26181fSAndroid Build Coastguard Worker 	 */
834*8b26181fSAndroid Build Coastguard Worker 	if (!p->activated) {
835*8b26181fSAndroid Build Coastguard Worker 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
836*8b26181fSAndroid Build Coastguard Worker 		    "%s: not-yet-activated pcap_t passed to pcap_dump_open",
837*8b26181fSAndroid Build Coastguard Worker 		    fname);
838*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
839*8b26181fSAndroid Build Coastguard Worker 	}
840*8b26181fSAndroid Build Coastguard Worker 	linktype = dlt_to_linktype(p->linktype);
841*8b26181fSAndroid Build Coastguard Worker 	if (linktype == -1) {
842*8b26181fSAndroid Build Coastguard Worker 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
843*8b26181fSAndroid Build Coastguard Worker 		    "%s: link-layer type %d isn't supported in savefiles",
844*8b26181fSAndroid Build Coastguard Worker 		    fname, p->linktype);
845*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
846*8b26181fSAndroid Build Coastguard Worker 	}
847*8b26181fSAndroid Build Coastguard Worker 	linktype |= p->linktype_ext;
848*8b26181fSAndroid Build Coastguard Worker 
849*8b26181fSAndroid Build Coastguard Worker 	if (fname == NULL) {
850*8b26181fSAndroid Build Coastguard Worker 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
851*8b26181fSAndroid Build Coastguard Worker 		    "A null pointer was supplied as the file name");
852*8b26181fSAndroid Build Coastguard Worker 		return NULL;
853*8b26181fSAndroid Build Coastguard Worker 	}
854*8b26181fSAndroid Build Coastguard Worker 	if (fname[0] == '-' && fname[1] == '\0') {
855*8b26181fSAndroid Build Coastguard Worker 		f = stdout;
856*8b26181fSAndroid Build Coastguard Worker 		fname = "standard output";
857*8b26181fSAndroid Build Coastguard Worker 	} else {
858*8b26181fSAndroid Build Coastguard Worker 		/*
859*8b26181fSAndroid Build Coastguard Worker 		 * "b" is supported as of C90, so *all* UN*Xes should
860*8b26181fSAndroid Build Coastguard Worker 		 * support it, even though it does nothing.  It's
861*8b26181fSAndroid Build Coastguard Worker 		 * required on Windows, as the file is a binary file
862*8b26181fSAndroid Build Coastguard Worker 		 * and must be written in binary mode.
863*8b26181fSAndroid Build Coastguard Worker 		 */
864*8b26181fSAndroid Build Coastguard Worker 		f = charset_fopen(fname, "wb");
865*8b26181fSAndroid Build Coastguard Worker 		if (f == NULL) {
866*8b26181fSAndroid Build Coastguard Worker 			pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
867*8b26181fSAndroid Build Coastguard Worker 			    errno, "%s", fname);
868*8b26181fSAndroid Build Coastguard Worker 			return (NULL);
869*8b26181fSAndroid Build Coastguard Worker 		}
870*8b26181fSAndroid Build Coastguard Worker 	}
871*8b26181fSAndroid Build Coastguard Worker 	return (pcap_setup_dump(p, linktype, f, fname));
872*8b26181fSAndroid Build Coastguard Worker }
873*8b26181fSAndroid Build Coastguard Worker 
874*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
875*8b26181fSAndroid Build Coastguard Worker /*
876*8b26181fSAndroid Build Coastguard Worker  * Initialize so that sf_write() will output to a stream wrapping the given raw
877*8b26181fSAndroid Build Coastguard Worker  * OS file HANDLE.
878*8b26181fSAndroid Build Coastguard Worker  */
879*8b26181fSAndroid Build Coastguard Worker pcap_dumper_t *
pcap_dump_hopen(pcap_t * p,intptr_t osfd)880*8b26181fSAndroid Build Coastguard Worker pcap_dump_hopen(pcap_t *p, intptr_t osfd)
881*8b26181fSAndroid Build Coastguard Worker {
882*8b26181fSAndroid Build Coastguard Worker 	int fd;
883*8b26181fSAndroid Build Coastguard Worker 	FILE *file;
884*8b26181fSAndroid Build Coastguard Worker 
885*8b26181fSAndroid Build Coastguard Worker 	fd = _open_osfhandle(osfd, _O_APPEND);
886*8b26181fSAndroid Build Coastguard Worker 	if (fd < 0) {
887*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
888*8b26181fSAndroid Build Coastguard Worker 		    errno, "_open_osfhandle");
889*8b26181fSAndroid Build Coastguard Worker 		return NULL;
890*8b26181fSAndroid Build Coastguard Worker 	}
891*8b26181fSAndroid Build Coastguard Worker 
892*8b26181fSAndroid Build Coastguard Worker 	file = _fdopen(fd, "wb");
893*8b26181fSAndroid Build Coastguard Worker 	if (file == NULL) {
894*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
895*8b26181fSAndroid Build Coastguard Worker 		    errno, "_fdopen");
896*8b26181fSAndroid Build Coastguard Worker 		_close(fd);
897*8b26181fSAndroid Build Coastguard Worker 		return NULL;
898*8b26181fSAndroid Build Coastguard Worker 	}
899*8b26181fSAndroid Build Coastguard Worker 
900*8b26181fSAndroid Build Coastguard Worker 	return pcap_dump_fopen(p, file);
901*8b26181fSAndroid Build Coastguard Worker }
902*8b26181fSAndroid Build Coastguard Worker #endif /* _WIN32 */
903*8b26181fSAndroid Build Coastguard Worker 
904*8b26181fSAndroid Build Coastguard Worker /*
905*8b26181fSAndroid Build Coastguard Worker  * Initialize so that sf_write() will output to the given stream.
906*8b26181fSAndroid Build Coastguard Worker  */
907*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
908*8b26181fSAndroid Build Coastguard Worker static
909*8b26181fSAndroid Build Coastguard Worker #endif /* _WIN32 */
910*8b26181fSAndroid Build Coastguard Worker pcap_dumper_t *
pcap_dump_fopen(pcap_t * p,FILE * f)911*8b26181fSAndroid Build Coastguard Worker pcap_dump_fopen(pcap_t *p, FILE *f)
912*8b26181fSAndroid Build Coastguard Worker {
913*8b26181fSAndroid Build Coastguard Worker 	int linktype;
914*8b26181fSAndroid Build Coastguard Worker 
915*8b26181fSAndroid Build Coastguard Worker 	linktype = dlt_to_linktype(p->linktype);
916*8b26181fSAndroid Build Coastguard Worker 	if (linktype == -1) {
917*8b26181fSAndroid Build Coastguard Worker 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
918*8b26181fSAndroid Build Coastguard Worker 		    "stream: link-layer type %d isn't supported in savefiles",
919*8b26181fSAndroid Build Coastguard Worker 		    p->linktype);
920*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
921*8b26181fSAndroid Build Coastguard Worker 	}
922*8b26181fSAndroid Build Coastguard Worker 	linktype |= p->linktype_ext;
923*8b26181fSAndroid Build Coastguard Worker 
924*8b26181fSAndroid Build Coastguard Worker 	return (pcap_setup_dump(p, linktype, f, "stream"));
925*8b26181fSAndroid Build Coastguard Worker }
926*8b26181fSAndroid Build Coastguard Worker 
927*8b26181fSAndroid Build Coastguard Worker pcap_dumper_t *
pcap_dump_open_append(pcap_t * p,const char * fname)928*8b26181fSAndroid Build Coastguard Worker pcap_dump_open_append(pcap_t *p, const char *fname)
929*8b26181fSAndroid Build Coastguard Worker {
930*8b26181fSAndroid Build Coastguard Worker 	FILE *f;
931*8b26181fSAndroid Build Coastguard Worker 	int linktype;
932*8b26181fSAndroid Build Coastguard Worker 	size_t amt_read;
933*8b26181fSAndroid Build Coastguard Worker 	struct pcap_file_header ph;
934*8b26181fSAndroid Build Coastguard Worker 
935*8b26181fSAndroid Build Coastguard Worker 	linktype = dlt_to_linktype(p->linktype);
936*8b26181fSAndroid Build Coastguard Worker 	if (linktype == -1) {
937*8b26181fSAndroid Build Coastguard Worker 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
938*8b26181fSAndroid Build Coastguard Worker 		    "%s: link-layer type %d isn't supported in savefiles",
939*8b26181fSAndroid Build Coastguard Worker 		    fname, linktype);
940*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
941*8b26181fSAndroid Build Coastguard Worker 	}
942*8b26181fSAndroid Build Coastguard Worker 
943*8b26181fSAndroid Build Coastguard Worker 	if (fname == NULL) {
944*8b26181fSAndroid Build Coastguard Worker 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
945*8b26181fSAndroid Build Coastguard Worker 		    "A null pointer was supplied as the file name");
946*8b26181fSAndroid Build Coastguard Worker 		return NULL;
947*8b26181fSAndroid Build Coastguard Worker 	}
948*8b26181fSAndroid Build Coastguard Worker 	if (fname[0] == '-' && fname[1] == '\0')
949*8b26181fSAndroid Build Coastguard Worker 		return (pcap_setup_dump(p, linktype, stdout, "standard output"));
950*8b26181fSAndroid Build Coastguard Worker 
951*8b26181fSAndroid Build Coastguard Worker 	/*
952*8b26181fSAndroid Build Coastguard Worker 	 * "a" will cause the file *not* to be truncated if it exists
953*8b26181fSAndroid Build Coastguard Worker 	 * but will cause it to be created if it doesn't.  It will
954*8b26181fSAndroid Build Coastguard Worker 	 * also cause all writes to be done at the end of the file,
955*8b26181fSAndroid Build Coastguard Worker 	 * but will allow reads to be done anywhere in the file.  This
956*8b26181fSAndroid Build Coastguard Worker 	 * is what we need, because we need to read from the beginning
957*8b26181fSAndroid Build Coastguard Worker 	 * of the file to see if it already has a header and packets
958*8b26181fSAndroid Build Coastguard Worker 	 * or if it doesn't.
959*8b26181fSAndroid Build Coastguard Worker 	 *
960*8b26181fSAndroid Build Coastguard Worker 	 * "b" is supported as of C90, so *all* UN*Xes should support it,
961*8b26181fSAndroid Build Coastguard Worker 	 * even though it does nothing.  It's required on Windows, as the
962*8b26181fSAndroid Build Coastguard Worker 	 * file is a binary file and must be read in binary mode.
963*8b26181fSAndroid Build Coastguard Worker 	 */
964*8b26181fSAndroid Build Coastguard Worker 	f = charset_fopen(fname, "ab+");
965*8b26181fSAndroid Build Coastguard Worker 	if (f == NULL) {
966*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
967*8b26181fSAndroid Build Coastguard Worker 		    errno, "%s", fname);
968*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
969*8b26181fSAndroid Build Coastguard Worker 	}
970*8b26181fSAndroid Build Coastguard Worker 
971*8b26181fSAndroid Build Coastguard Worker 	/*
972*8b26181fSAndroid Build Coastguard Worker 	 * Try to read a pcap header.
973*8b26181fSAndroid Build Coastguard Worker 	 *
974*8b26181fSAndroid Build Coastguard Worker 	 * We do not assume that the file will be positioned at the
975*8b26181fSAndroid Build Coastguard Worker 	 * beginning immediately after we've opened it - we seek to
976*8b26181fSAndroid Build Coastguard Worker 	 * the beginning.  ISO C says it's implementation-defined
977*8b26181fSAndroid Build Coastguard Worker 	 * whether the file position indicator is at the beginning
978*8b26181fSAndroid Build Coastguard Worker 	 * or the end of the file after an append-mode open, and
979*8b26181fSAndroid Build Coastguard Worker 	 * it wasn't obvious from the Single UNIX Specification
980*8b26181fSAndroid Build Coastguard Worker 	 * or the Microsoft documentation how that works on SUS-
981*8b26181fSAndroid Build Coastguard Worker 	 * compliant systems or on Windows.
982*8b26181fSAndroid Build Coastguard Worker 	 */
983*8b26181fSAndroid Build Coastguard Worker 	if (fseek(f, 0, SEEK_SET) == -1) {
984*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
985*8b26181fSAndroid Build Coastguard Worker 		    errno, "Can't seek to the beginning of %s", fname);
986*8b26181fSAndroid Build Coastguard Worker 		(void)fclose(f);
987*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
988*8b26181fSAndroid Build Coastguard Worker 	}
989*8b26181fSAndroid Build Coastguard Worker 	amt_read = fread(&ph, 1, sizeof (ph), f);
990*8b26181fSAndroid Build Coastguard Worker 	if (amt_read != sizeof (ph)) {
991*8b26181fSAndroid Build Coastguard Worker 		if (ferror(f)) {
992*8b26181fSAndroid Build Coastguard Worker 			pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
993*8b26181fSAndroid Build Coastguard Worker 			    errno, "%s", fname);
994*8b26181fSAndroid Build Coastguard Worker 			(void)fclose(f);
995*8b26181fSAndroid Build Coastguard Worker 			return (NULL);
996*8b26181fSAndroid Build Coastguard Worker 		} else if (feof(f) && amt_read > 0) {
997*8b26181fSAndroid Build Coastguard Worker 			snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
998*8b26181fSAndroid Build Coastguard Worker 			    "%s: truncated pcap file header", fname);
999*8b26181fSAndroid Build Coastguard Worker 			(void)fclose(f);
1000*8b26181fSAndroid Build Coastguard Worker 			return (NULL);
1001*8b26181fSAndroid Build Coastguard Worker 		}
1002*8b26181fSAndroid Build Coastguard Worker 	}
1003*8b26181fSAndroid Build Coastguard Worker 
1004*8b26181fSAndroid Build Coastguard Worker #if defined(_WIN32) || defined(MSDOS)
1005*8b26181fSAndroid Build Coastguard Worker 	/*
1006*8b26181fSAndroid Build Coastguard Worker 	 * We turn off buffering.
1007*8b26181fSAndroid Build Coastguard Worker 	 * XXX - why?  And why not on the standard output?
1008*8b26181fSAndroid Build Coastguard Worker 	 */
1009*8b26181fSAndroid Build Coastguard Worker 	setvbuf(f, NULL, _IONBF, 0);
1010*8b26181fSAndroid Build Coastguard Worker #endif
1011*8b26181fSAndroid Build Coastguard Worker 
1012*8b26181fSAndroid Build Coastguard Worker 	/*
1013*8b26181fSAndroid Build Coastguard Worker 	 * If a header is already present and:
1014*8b26181fSAndroid Build Coastguard Worker 	 *
1015*8b26181fSAndroid Build Coastguard Worker 	 *	it's not for a pcap file of the appropriate resolution
1016*8b26181fSAndroid Build Coastguard Worker 	 *	and the right byte order for this machine;
1017*8b26181fSAndroid Build Coastguard Worker 	 *
1018*8b26181fSAndroid Build Coastguard Worker 	 *	the link-layer header types don't match;
1019*8b26181fSAndroid Build Coastguard Worker 	 *
1020*8b26181fSAndroid Build Coastguard Worker 	 *	the snapshot lengths don't match;
1021*8b26181fSAndroid Build Coastguard Worker 	 *
1022*8b26181fSAndroid Build Coastguard Worker 	 * return an error.
1023*8b26181fSAndroid Build Coastguard Worker 	 */
1024*8b26181fSAndroid Build Coastguard Worker 	if (amt_read > 0) {
1025*8b26181fSAndroid Build Coastguard Worker 		/*
1026*8b26181fSAndroid Build Coastguard Worker 		 * A header is already present.
1027*8b26181fSAndroid Build Coastguard Worker 		 * Do the checks.
1028*8b26181fSAndroid Build Coastguard Worker 		 */
1029*8b26181fSAndroid Build Coastguard Worker 		switch (ph.magic) {
1030*8b26181fSAndroid Build Coastguard Worker 
1031*8b26181fSAndroid Build Coastguard Worker 		case TCPDUMP_MAGIC:
1032*8b26181fSAndroid Build Coastguard Worker 			if (p->opt.tstamp_precision != PCAP_TSTAMP_PRECISION_MICRO) {
1033*8b26181fSAndroid Build Coastguard Worker 				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1034*8b26181fSAndroid Build Coastguard Worker 				    "%s: different time stamp precision, cannot append to file", fname);
1035*8b26181fSAndroid Build Coastguard Worker 				(void)fclose(f);
1036*8b26181fSAndroid Build Coastguard Worker 				return (NULL);
1037*8b26181fSAndroid Build Coastguard Worker 			}
1038*8b26181fSAndroid Build Coastguard Worker 			break;
1039*8b26181fSAndroid Build Coastguard Worker 
1040*8b26181fSAndroid Build Coastguard Worker 		case NSEC_TCPDUMP_MAGIC:
1041*8b26181fSAndroid Build Coastguard Worker 			if (p->opt.tstamp_precision != PCAP_TSTAMP_PRECISION_NANO) {
1042*8b26181fSAndroid Build Coastguard Worker 				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1043*8b26181fSAndroid Build Coastguard Worker 				    "%s: different time stamp precision, cannot append to file", fname);
1044*8b26181fSAndroid Build Coastguard Worker 				(void)fclose(f);
1045*8b26181fSAndroid Build Coastguard Worker 				return (NULL);
1046*8b26181fSAndroid Build Coastguard Worker 			}
1047*8b26181fSAndroid Build Coastguard Worker 			break;
1048*8b26181fSAndroid Build Coastguard Worker 
1049*8b26181fSAndroid Build Coastguard Worker 		case SWAPLONG(TCPDUMP_MAGIC):
1050*8b26181fSAndroid Build Coastguard Worker 		case SWAPLONG(NSEC_TCPDUMP_MAGIC):
1051*8b26181fSAndroid Build Coastguard Worker 			snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1052*8b26181fSAndroid Build Coastguard Worker 			    "%s: different byte order, cannot append to file", fname);
1053*8b26181fSAndroid Build Coastguard Worker 			(void)fclose(f);
1054*8b26181fSAndroid Build Coastguard Worker 			return (NULL);
1055*8b26181fSAndroid Build Coastguard Worker 
1056*8b26181fSAndroid Build Coastguard Worker 		case KUZNETZOV_TCPDUMP_MAGIC:
1057*8b26181fSAndroid Build Coastguard Worker 		case SWAPLONG(KUZNETZOV_TCPDUMP_MAGIC):
1058*8b26181fSAndroid Build Coastguard Worker 		case NAVTEL_TCPDUMP_MAGIC:
1059*8b26181fSAndroid Build Coastguard Worker 		case SWAPLONG(NAVTEL_TCPDUMP_MAGIC):
1060*8b26181fSAndroid Build Coastguard Worker 			snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1061*8b26181fSAndroid Build Coastguard Worker 			    "%s: not a pcap file to which we can append", fname);
1062*8b26181fSAndroid Build Coastguard Worker 			(void)fclose(f);
1063*8b26181fSAndroid Build Coastguard Worker 			return (NULL);
1064*8b26181fSAndroid Build Coastguard Worker 
1065*8b26181fSAndroid Build Coastguard Worker 		default:
1066*8b26181fSAndroid Build Coastguard Worker 			snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1067*8b26181fSAndroid Build Coastguard Worker 			    "%s: not a pcap file", fname);
1068*8b26181fSAndroid Build Coastguard Worker 			(void)fclose(f);
1069*8b26181fSAndroid Build Coastguard Worker 			return (NULL);
1070*8b26181fSAndroid Build Coastguard Worker 		}
1071*8b26181fSAndroid Build Coastguard Worker 
1072*8b26181fSAndroid Build Coastguard Worker 		/*
1073*8b26181fSAndroid Build Coastguard Worker 		 * Good version?
1074*8b26181fSAndroid Build Coastguard Worker 		 */
1075*8b26181fSAndroid Build Coastguard Worker 		if (ph.version_major != PCAP_VERSION_MAJOR ||
1076*8b26181fSAndroid Build Coastguard Worker 		    ph.version_minor != PCAP_VERSION_MINOR) {
1077*8b26181fSAndroid Build Coastguard Worker 			snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1078*8b26181fSAndroid Build Coastguard Worker 			    "%s: version is %u.%u, cannot append to file", fname,
1079*8b26181fSAndroid Build Coastguard Worker 			    ph.version_major, ph.version_minor);
1080*8b26181fSAndroid Build Coastguard Worker 			(void)fclose(f);
1081*8b26181fSAndroid Build Coastguard Worker 			return (NULL);
1082*8b26181fSAndroid Build Coastguard Worker 		}
1083*8b26181fSAndroid Build Coastguard Worker 		if ((bpf_u_int32)linktype != ph.linktype) {
1084*8b26181fSAndroid Build Coastguard Worker 			snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1085*8b26181fSAndroid Build Coastguard Worker 			    "%s: different linktype, cannot append to file", fname);
1086*8b26181fSAndroid Build Coastguard Worker 			(void)fclose(f);
1087*8b26181fSAndroid Build Coastguard Worker 			return (NULL);
1088*8b26181fSAndroid Build Coastguard Worker 		}
1089*8b26181fSAndroid Build Coastguard Worker 		if ((bpf_u_int32)p->snapshot != ph.snaplen) {
1090*8b26181fSAndroid Build Coastguard Worker 			snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1091*8b26181fSAndroid Build Coastguard Worker 			    "%s: different snaplen, cannot append to file", fname);
1092*8b26181fSAndroid Build Coastguard Worker 			(void)fclose(f);
1093*8b26181fSAndroid Build Coastguard Worker 			return (NULL);
1094*8b26181fSAndroid Build Coastguard Worker 		}
1095*8b26181fSAndroid Build Coastguard Worker 	} else {
1096*8b26181fSAndroid Build Coastguard Worker 		/*
1097*8b26181fSAndroid Build Coastguard Worker 		 * A header isn't present; attempt to write it.
1098*8b26181fSAndroid Build Coastguard Worker 		 */
1099*8b26181fSAndroid Build Coastguard Worker 		if (sf_write_header(p, f, linktype, p->snapshot) == -1) {
1100*8b26181fSAndroid Build Coastguard Worker 			pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
1101*8b26181fSAndroid Build Coastguard Worker 			    errno, "Can't write to %s", fname);
1102*8b26181fSAndroid Build Coastguard Worker 			(void)fclose(f);
1103*8b26181fSAndroid Build Coastguard Worker 			return (NULL);
1104*8b26181fSAndroid Build Coastguard Worker 		}
1105*8b26181fSAndroid Build Coastguard Worker 	}
1106*8b26181fSAndroid Build Coastguard Worker 
1107*8b26181fSAndroid Build Coastguard Worker 	/*
1108*8b26181fSAndroid Build Coastguard Worker 	 * Start writing at the end of the file.
1109*8b26181fSAndroid Build Coastguard Worker 	 *
1110*8b26181fSAndroid Build Coastguard Worker 	 * XXX - this shouldn't be necessary, given that we're opening
1111*8b26181fSAndroid Build Coastguard Worker 	 * the file in append mode, and ISO C specifies that all writes
1112*8b26181fSAndroid Build Coastguard Worker 	 * are done at the end of the file in that mode.
1113*8b26181fSAndroid Build Coastguard Worker 	 */
1114*8b26181fSAndroid Build Coastguard Worker 	if (fseek(f, 0, SEEK_END) == -1) {
1115*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
1116*8b26181fSAndroid Build Coastguard Worker 		    errno, "Can't seek to the end of %s", fname);
1117*8b26181fSAndroid Build Coastguard Worker 		(void)fclose(f);
1118*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
1119*8b26181fSAndroid Build Coastguard Worker 	}
1120*8b26181fSAndroid Build Coastguard Worker 	return ((pcap_dumper_t *)f);
1121*8b26181fSAndroid Build Coastguard Worker }
1122*8b26181fSAndroid Build Coastguard Worker 
1123*8b26181fSAndroid Build Coastguard Worker FILE *
pcap_dump_file(pcap_dumper_t * p)1124*8b26181fSAndroid Build Coastguard Worker pcap_dump_file(pcap_dumper_t *p)
1125*8b26181fSAndroid Build Coastguard Worker {
1126*8b26181fSAndroid Build Coastguard Worker 	return ((FILE *)p);
1127*8b26181fSAndroid Build Coastguard Worker }
1128*8b26181fSAndroid Build Coastguard Worker 
1129*8b26181fSAndroid Build Coastguard Worker long
pcap_dump_ftell(pcap_dumper_t * p)1130*8b26181fSAndroid Build Coastguard Worker pcap_dump_ftell(pcap_dumper_t *p)
1131*8b26181fSAndroid Build Coastguard Worker {
1132*8b26181fSAndroid Build Coastguard Worker 	return (ftell((FILE *)p));
1133*8b26181fSAndroid Build Coastguard Worker }
1134*8b26181fSAndroid Build Coastguard Worker 
1135*8b26181fSAndroid Build Coastguard Worker #if defined(HAVE_FSEEKO)
1136*8b26181fSAndroid Build Coastguard Worker /*
1137*8b26181fSAndroid Build Coastguard Worker  * We have fseeko(), so we have ftello().
1138*8b26181fSAndroid Build Coastguard Worker  * If we have large file support (files larger than 2^31-1 bytes),
1139*8b26181fSAndroid Build Coastguard Worker  * ftello() will give us a current file position with more than 32
1140*8b26181fSAndroid Build Coastguard Worker  * bits.
1141*8b26181fSAndroid Build Coastguard Worker  */
1142*8b26181fSAndroid Build Coastguard Worker int64_t
pcap_dump_ftell64(pcap_dumper_t * p)1143*8b26181fSAndroid Build Coastguard Worker pcap_dump_ftell64(pcap_dumper_t *p)
1144*8b26181fSAndroid Build Coastguard Worker {
1145*8b26181fSAndroid Build Coastguard Worker 	return (ftello((FILE *)p));
1146*8b26181fSAndroid Build Coastguard Worker }
1147*8b26181fSAndroid Build Coastguard Worker #elif defined(_MSC_VER)
1148*8b26181fSAndroid Build Coastguard Worker /*
1149*8b26181fSAndroid Build Coastguard Worker  * We have Visual Studio; we support only 2005 and later, so we have
1150*8b26181fSAndroid Build Coastguard Worker  * _ftelli64().
1151*8b26181fSAndroid Build Coastguard Worker  */
1152*8b26181fSAndroid Build Coastguard Worker int64_t
pcap_dump_ftell64(pcap_dumper_t * p)1153*8b26181fSAndroid Build Coastguard Worker pcap_dump_ftell64(pcap_dumper_t *p)
1154*8b26181fSAndroid Build Coastguard Worker {
1155*8b26181fSAndroid Build Coastguard Worker 	return (_ftelli64((FILE *)p));
1156*8b26181fSAndroid Build Coastguard Worker }
1157*8b26181fSAndroid Build Coastguard Worker #else
1158*8b26181fSAndroid Build Coastguard Worker /*
1159*8b26181fSAndroid Build Coastguard Worker  * We don't have ftello() or _ftelli64(), so fall back on ftell().
1160*8b26181fSAndroid Build Coastguard Worker  * Either long is 64 bits, in which case ftell() should suffice,
1161*8b26181fSAndroid Build Coastguard Worker  * or this is probably an older 32-bit UN*X without large file
1162*8b26181fSAndroid Build Coastguard Worker  * support, which means you'll probably get errors trying to
1163*8b26181fSAndroid Build Coastguard Worker  * write files > 2^31-1, so it won't matter anyway.
1164*8b26181fSAndroid Build Coastguard Worker  *
1165*8b26181fSAndroid Build Coastguard Worker  * XXX - what about MinGW?
1166*8b26181fSAndroid Build Coastguard Worker  */
1167*8b26181fSAndroid Build Coastguard Worker int64_t
pcap_dump_ftell64(pcap_dumper_t * p)1168*8b26181fSAndroid Build Coastguard Worker pcap_dump_ftell64(pcap_dumper_t *p)
1169*8b26181fSAndroid Build Coastguard Worker {
1170*8b26181fSAndroid Build Coastguard Worker 	return (ftell((FILE *)p));
1171*8b26181fSAndroid Build Coastguard Worker }
1172*8b26181fSAndroid Build Coastguard Worker #endif
1173*8b26181fSAndroid Build Coastguard Worker 
1174*8b26181fSAndroid Build Coastguard Worker int
pcap_dump_flush(pcap_dumper_t * p)1175*8b26181fSAndroid Build Coastguard Worker pcap_dump_flush(pcap_dumper_t *p)
1176*8b26181fSAndroid Build Coastguard Worker {
1177*8b26181fSAndroid Build Coastguard Worker 
1178*8b26181fSAndroid Build Coastguard Worker 	if (fflush((FILE *)p) == EOF)
1179*8b26181fSAndroid Build Coastguard Worker 		return (-1);
1180*8b26181fSAndroid Build Coastguard Worker 	else
1181*8b26181fSAndroid Build Coastguard Worker 		return (0);
1182*8b26181fSAndroid Build Coastguard Worker }
1183*8b26181fSAndroid Build Coastguard Worker 
1184*8b26181fSAndroid Build Coastguard Worker void
pcap_dump_close(pcap_dumper_t * p)1185*8b26181fSAndroid Build Coastguard Worker pcap_dump_close(pcap_dumper_t *p)
1186*8b26181fSAndroid Build Coastguard Worker {
1187*8b26181fSAndroid Build Coastguard Worker 
1188*8b26181fSAndroid Build Coastguard Worker #ifdef notyet
1189*8b26181fSAndroid Build Coastguard Worker 	if (ferror((FILE *)p))
1190*8b26181fSAndroid Build Coastguard Worker 		return-an-error;
1191*8b26181fSAndroid Build Coastguard Worker 	/* XXX should check return from fclose() too */
1192*8b26181fSAndroid Build Coastguard Worker #endif
1193*8b26181fSAndroid Build Coastguard Worker 	(void)fclose((FILE *)p);
1194*8b26181fSAndroid Build Coastguard Worker }
1195