1a0ee43a1SRui Paulo /*
2a0ee43a1SRui Paulo * Copyright (c) 1993, 1994, 1995, 1996, 1997
3a0ee43a1SRui Paulo * The Regents of the University of California. All rights reserved.
4a0ee43a1SRui Paulo *
5a0ee43a1SRui Paulo * Redistribution and use in source and binary forms, with or without
6a0ee43a1SRui Paulo * modification, are permitted provided that: (1) source code distributions
7a0ee43a1SRui Paulo * retain the above copyright notice and this paragraph in its entirety, (2)
8a0ee43a1SRui Paulo * distributions including binary code include the above copyright notice and
9a0ee43a1SRui Paulo * this paragraph in its entirety in the documentation or other materials
10a0ee43a1SRui Paulo * provided with the distribution, and (3) all advertising materials mentioning
11a0ee43a1SRui Paulo * features or use of this software display the following acknowledgement:
12a0ee43a1SRui Paulo * ``This product includes software developed by the University of California,
13a0ee43a1SRui Paulo * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14a0ee43a1SRui Paulo * the University nor the names of its contributors may be used to endorse
15a0ee43a1SRui Paulo * or promote products derived from this software without specific prior
16a0ee43a1SRui Paulo * written permission.
17a0ee43a1SRui Paulo * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18a0ee43a1SRui Paulo * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19a0ee43a1SRui Paulo * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20a0ee43a1SRui Paulo *
21a0ee43a1SRui Paulo * sf-pcap.c - libpcap-file-format-specific code from savefile.c
22a0ee43a1SRui Paulo * Extraction/creation by Jeffrey Mogul, DECWRL
23a0ee43a1SRui Paulo * Modified by Steve McCanne, LBL.
24a0ee43a1SRui Paulo *
25a0ee43a1SRui Paulo * Used to save the received packet headers, after filtering, to
26a0ee43a1SRui Paulo * a file, and then read them later.
27a0ee43a1SRui Paulo * The first record in the file contains saved values for the machine
28a0ee43a1SRui Paulo * dependent values so we can print the dump file on any architecture.
29a0ee43a1SRui Paulo */
30a0ee43a1SRui Paulo
31b00ab754SHans Petter Selasky #include <config.h>
32a0ee43a1SRui Paulo
33b00ab754SHans Petter Selasky #include <pcap-types.h>
34ada6f083SXin LI #ifdef _WIN32
35b00ab754SHans Petter Selasky #include <io.h>
36b00ab754SHans Petter Selasky #include <fcntl.h>
37ada6f083SXin LI #endif /* _WIN32 */
38a0ee43a1SRui Paulo
39a0ee43a1SRui Paulo #include <errno.h>
40a0ee43a1SRui Paulo #include <memory.h>
41a0ee43a1SRui Paulo #include <stdio.h>
42a0ee43a1SRui Paulo #include <stdlib.h>
43a0ee43a1SRui Paulo #include <string.h>
4457e22627SCy Schubert #include <limits.h> /* for INT_MAX */
45a0ee43a1SRui Paulo
46a0ee43a1SRui Paulo #include "pcap-int.h"
476f9cba8fSJoseph Mingrone #include "pcap-util.h"
48a0ee43a1SRui Paulo
49a0ee43a1SRui Paulo #include "pcap-common.h"
50a0ee43a1SRui Paulo
51a0ee43a1SRui Paulo #ifdef HAVE_OS_PROTO_H
52a0ee43a1SRui Paulo #include "os-proto.h"
53a0ee43a1SRui Paulo #endif
54a0ee43a1SRui Paulo
55a0ee43a1SRui Paulo #include "sf-pcap.h"
56a0ee43a1SRui Paulo
57a0ee43a1SRui Paulo /*
58a0ee43a1SRui Paulo * Setting O_BINARY on DOS/Windows is a bit tricky
59a0ee43a1SRui Paulo */
60ada6f083SXin LI #if defined(_WIN32)
61a0ee43a1SRui Paulo #define SET_BINMODE(f) _setmode(_fileno(f), _O_BINARY)
62a0ee43a1SRui Paulo #elif defined(MSDOS)
63a0ee43a1SRui Paulo #if defined(__HIGHC__)
64a0ee43a1SRui Paulo #define SET_BINMODE(f) setmode(f, O_BINARY)
65a0ee43a1SRui Paulo #else
66a0ee43a1SRui Paulo #define SET_BINMODE(f) setmode(fileno(f), O_BINARY)
67a0ee43a1SRui Paulo #endif
68a0ee43a1SRui Paulo #endif
69a0ee43a1SRui Paulo
70a0ee43a1SRui Paulo /*
71a0ee43a1SRui Paulo * Standard libpcap format.
726f9cba8fSJoseph Mingrone *
736f9cba8fSJoseph Mingrone * The same value is used in the rpcap protocol as an indication of
746f9cba8fSJoseph Mingrone * the server byte order, to let the client know whether it needs to
756f9cba8fSJoseph Mingrone * byte-swap some host-byte-order metadata.
76a0ee43a1SRui Paulo */
77a0ee43a1SRui Paulo #define TCPDUMP_MAGIC 0xa1b2c3d4
78a0ee43a1SRui Paulo
79a0ee43a1SRui Paulo /*
80a0ee43a1SRui Paulo * Alexey Kuznetzov's modified libpcap format.
81a0ee43a1SRui Paulo */
82a0ee43a1SRui Paulo #define KUZNETZOV_TCPDUMP_MAGIC 0xa1b2cd34
83a0ee43a1SRui Paulo
84a0ee43a1SRui Paulo /*
85a0ee43a1SRui Paulo * Reserved for Francisco Mesquita <francisco.mesquita@radiomovel.pt>
86a0ee43a1SRui Paulo * for another modified format.
87a0ee43a1SRui Paulo */
88a0ee43a1SRui Paulo #define FMESQUITA_TCPDUMP_MAGIC 0xa1b234cd
89a0ee43a1SRui Paulo
90a0ee43a1SRui Paulo /*
91*afdbf109SJoseph Mingrone * Navtel Communications' format, with nanosecond timestamps,
92a0ee43a1SRui Paulo * as per a request from Dumas Hwang <dumas.hwang@navtelcom.com>.
93a0ee43a1SRui Paulo */
94a0ee43a1SRui Paulo #define NAVTEL_TCPDUMP_MAGIC 0xa12b3c4d
95a0ee43a1SRui Paulo
96a0ee43a1SRui Paulo /*
97a0ee43a1SRui Paulo * Normal libpcap format, except for seconds/nanoseconds timestamps,
98a0ee43a1SRui Paulo * as per a request by Ulf Lamping <ulf.lamping@web.de>
99a0ee43a1SRui Paulo */
100a0ee43a1SRui Paulo #define NSEC_TCPDUMP_MAGIC 0xa1b23c4d
101a0ee43a1SRui Paulo
102a0ee43a1SRui Paulo /*
103*afdbf109SJoseph Mingrone * This is a timeval as stored in a savefile.
104*afdbf109SJoseph Mingrone * It has to use the same types everywhere, independent of the actual
105*afdbf109SJoseph Mingrone * `struct timeval'; `struct timeval' has 32-bit tv_sec values on some
106*afdbf109SJoseph Mingrone * platforms and 64-bit tv_sec values on other platforms, and writing
107*afdbf109SJoseph Mingrone * out native `struct timeval' values would mean files could only be
108*afdbf109SJoseph Mingrone * read on systems with the same tv_sec size as the system on which
109*afdbf109SJoseph Mingrone * the file was written.
110a0ee43a1SRui Paulo *
111*afdbf109SJoseph Mingrone * THe fields are unsigned, as that's what the pcap draft specification
112*afdbf109SJoseph Mingrone * says they are. (That gives pcap a 68-year Y2.038K reprieve, although
113*afdbf109SJoseph Mingrone * in 2106 it runs out for good. pcapng doesn't have that problem,
114*afdbf109SJoseph Mingrone * unless you pick a *really* high time stamp precision.)
115a0ee43a1SRui Paulo */
116*afdbf109SJoseph Mingrone
117*afdbf109SJoseph Mingrone struct pcap_timeval {
118*afdbf109SJoseph Mingrone bpf_u_int32 tv_sec; /* seconds */
119*afdbf109SJoseph Mingrone bpf_u_int32 tv_usec; /* microseconds */
120*afdbf109SJoseph Mingrone };
121*afdbf109SJoseph Mingrone
122*afdbf109SJoseph Mingrone /*
123*afdbf109SJoseph Mingrone * This is a `pcap_pkthdr' as actually stored in a savefile.
124*afdbf109SJoseph Mingrone *
125*afdbf109SJoseph Mingrone * Do not change the format of this structure, in any way (this includes
126*afdbf109SJoseph Mingrone * changes that only affect the length of fields in this structure),
127*afdbf109SJoseph Mingrone * and do not make the time stamp anything other than seconds and
128*afdbf109SJoseph Mingrone * microseconds (e.g., seconds and nanoseconds). Instead:
129*afdbf109SJoseph Mingrone *
130*afdbf109SJoseph Mingrone * introduce a new structure for the new format;
131*afdbf109SJoseph Mingrone *
132*afdbf109SJoseph Mingrone * send mail to "tcpdump-workers@lists.tcpdump.org", requesting
133*afdbf109SJoseph Mingrone * a new magic number for your new capture file format, and, when
134*afdbf109SJoseph Mingrone * you get the new magic number, put it in "savefile.c";
135*afdbf109SJoseph Mingrone *
136*afdbf109SJoseph Mingrone * use that magic number for save files with the changed record
137*afdbf109SJoseph Mingrone * header;
138*afdbf109SJoseph Mingrone *
139*afdbf109SJoseph Mingrone * make the code in "savefile.c" capable of reading files with
140*afdbf109SJoseph Mingrone * the old record header as well as files with the new record header
141*afdbf109SJoseph Mingrone * (using the magic number to determine the header format).
142*afdbf109SJoseph Mingrone *
143*afdbf109SJoseph Mingrone * Then supply the changes by forking the branch at
144*afdbf109SJoseph Mingrone *
145*afdbf109SJoseph Mingrone * https://github.com/the-tcpdump-group/libpcap/tree/master
146*afdbf109SJoseph Mingrone *
147*afdbf109SJoseph Mingrone * and issuing a pull request, so that future versions of libpcap and
148*afdbf109SJoseph Mingrone * programs that use it (such as tcpdump) will be able to read your new
149*afdbf109SJoseph Mingrone * capture file format.
150*afdbf109SJoseph Mingrone */
151*afdbf109SJoseph Mingrone
152*afdbf109SJoseph Mingrone struct pcap_sf_pkthdr {
153*afdbf109SJoseph Mingrone struct pcap_timeval ts; /* time stamp */
154*afdbf109SJoseph Mingrone bpf_u_int32 caplen; /* length of portion present */
155*afdbf109SJoseph Mingrone bpf_u_int32 len; /* length of this packet (off wire) */
156*afdbf109SJoseph Mingrone };
157*afdbf109SJoseph Mingrone
158*afdbf109SJoseph Mingrone /*
159*afdbf109SJoseph Mingrone * How a `pcap_pkthdr' is actually stored in savefiles written
160*afdbf109SJoseph Mingrone * by some patched versions of libpcap (e.g. the ones in Red
161*afdbf109SJoseph Mingrone * Hat Linux 6.1 and 6.2).
162*afdbf109SJoseph Mingrone *
163*afdbf109SJoseph Mingrone * Do not change the format of this structure, in any way (this includes
164*afdbf109SJoseph Mingrone * changes that only affect the length of fields in this structure).
165*afdbf109SJoseph Mingrone * Instead, introduce a new structure, as per the above.
166*afdbf109SJoseph Mingrone */
167*afdbf109SJoseph Mingrone
168*afdbf109SJoseph Mingrone struct pcap_sf_patched_pkthdr {
169*afdbf109SJoseph Mingrone struct pcap_timeval ts; /* time stamp */
170*afdbf109SJoseph Mingrone bpf_u_int32 caplen; /* length of portion present */
171*afdbf109SJoseph Mingrone bpf_u_int32 len; /* length of this packet (off wire) */
172*afdbf109SJoseph Mingrone int index;
173*afdbf109SJoseph Mingrone unsigned short protocol;
174*afdbf109SJoseph Mingrone unsigned char pkt_type;
175*afdbf109SJoseph Mingrone };
176a0ee43a1SRui Paulo
177a0ee43a1SRui Paulo static int pcap_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char **datap);
178a0ee43a1SRui Paulo
17957e22627SCy Schubert #ifdef _WIN32
18057e22627SCy Schubert /*
18157e22627SCy Schubert * This isn't exported on Windows, because it would only work if both
18257e22627SCy Schubert * libpcap and the code using it were using the same C runtime; otherwise they
18357e22627SCy Schubert * would be using different definitions of a FILE structure.
18457e22627SCy Schubert *
18557e22627SCy Schubert * Instead we define this as a macro in pcap/pcap.h that wraps the hopen
18657e22627SCy Schubert * version that we do export, passing it a raw OS HANDLE, as defined by the
18757e22627SCy Schubert * Win32 / Win64 ABI, obtained from the _fileno() and _get_osfhandle()
18857e22627SCy Schubert * functions of the appropriate CRT.
18957e22627SCy Schubert */
19057e22627SCy Schubert static pcap_dumper_t *pcap_dump_fopen(pcap_t *p, FILE *f);
19157e22627SCy Schubert #endif /* _WIN32 */
19257e22627SCy Schubert
193a0ee43a1SRui Paulo /*
194681ed54cSXin LI * Private data for reading pcap savefiles.
195681ed54cSXin LI */
196681ed54cSXin LI typedef enum {
197681ed54cSXin LI NOT_SWAPPED,
198681ed54cSXin LI SWAPPED,
199681ed54cSXin LI MAYBE_SWAPPED
200681ed54cSXin LI } swapped_type_t;
201681ed54cSXin LI
202681ed54cSXin LI typedef enum {
203681ed54cSXin LI PASS_THROUGH,
204681ed54cSXin LI SCALE_UP,
205681ed54cSXin LI SCALE_DOWN
206681ed54cSXin LI } tstamp_scale_type_t;
207681ed54cSXin LI
208681ed54cSXin LI struct pcap_sf {
209681ed54cSXin LI size_t hdrsize;
210681ed54cSXin LI swapped_type_t lengths_swapped;
211681ed54cSXin LI tstamp_scale_type_t scale_type;
212681ed54cSXin LI };
213681ed54cSXin LI
214681ed54cSXin LI /*
215a0ee43a1SRui Paulo * Check whether this is a pcap savefile and, if it is, extract the
216a0ee43a1SRui Paulo * relevant information from the header.
217a0ee43a1SRui Paulo */
218681ed54cSXin LI pcap_t *
pcap_check_header(const uint8_t * magic,FILE * fp,u_int precision,char * errbuf,int * err)21957e22627SCy Schubert pcap_check_header(const uint8_t *magic, FILE *fp, u_int precision, char *errbuf,
220681ed54cSXin LI int *err)
221a0ee43a1SRui Paulo {
22257e22627SCy Schubert bpf_u_int32 magic_int;
223a0ee43a1SRui Paulo struct pcap_file_header hdr;
224a0ee43a1SRui Paulo size_t amt_read;
225681ed54cSXin LI pcap_t *p;
226681ed54cSXin LI int swapped = 0;
227681ed54cSXin LI struct pcap_sf *ps;
228681ed54cSXin LI
229681ed54cSXin LI /*
230681ed54cSXin LI * Assume no read errors.
231681ed54cSXin LI */
232681ed54cSXin LI *err = 0;
233a0ee43a1SRui Paulo
234a0ee43a1SRui Paulo /*
235a0ee43a1SRui Paulo * Check whether the first 4 bytes of the file are the magic
236a0ee43a1SRui Paulo * number for a pcap savefile, or for a byte-swapped pcap
237a0ee43a1SRui Paulo * savefile.
238a0ee43a1SRui Paulo */
23957e22627SCy Schubert memcpy(&magic_int, magic, sizeof(magic_int));
24057e22627SCy Schubert if (magic_int != TCPDUMP_MAGIC &&
24157e22627SCy Schubert magic_int != KUZNETZOV_TCPDUMP_MAGIC &&
24257e22627SCy Schubert magic_int != NSEC_TCPDUMP_MAGIC) {
24357e22627SCy Schubert magic_int = SWAPLONG(magic_int);
24457e22627SCy Schubert if (magic_int != TCPDUMP_MAGIC &&
24557e22627SCy Schubert magic_int != KUZNETZOV_TCPDUMP_MAGIC &&
24657e22627SCy Schubert magic_int != NSEC_TCPDUMP_MAGIC)
247681ed54cSXin LI return (NULL); /* nope */
248681ed54cSXin LI swapped = 1;
249a0ee43a1SRui Paulo }
250a0ee43a1SRui Paulo
251a0ee43a1SRui Paulo /*
252a0ee43a1SRui Paulo * They are. Put the magic number in the header, and read
253a0ee43a1SRui Paulo * the rest of the header.
254a0ee43a1SRui Paulo */
25557e22627SCy Schubert hdr.magic = magic_int;
256a0ee43a1SRui Paulo amt_read = fread(((char *)&hdr) + sizeof hdr.magic, 1,
257a0ee43a1SRui Paulo sizeof(hdr) - sizeof(hdr.magic), fp);
258a0ee43a1SRui Paulo if (amt_read != sizeof(hdr) - sizeof(hdr.magic)) {
259a0ee43a1SRui Paulo if (ferror(fp)) {
260*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
261b00ab754SHans Petter Selasky errno, "error reading dump file");
262a0ee43a1SRui Paulo } else {
2636f9cba8fSJoseph Mingrone snprintf(errbuf, PCAP_ERRBUF_SIZE,
2646f9cba8fSJoseph Mingrone "truncated dump file; tried to read %zu file header bytes, only got %zu",
26557e22627SCy Schubert sizeof(hdr), amt_read);
266a0ee43a1SRui Paulo }
267681ed54cSXin LI *err = 1;
268681ed54cSXin LI return (NULL);
269a0ee43a1SRui Paulo }
270a0ee43a1SRui Paulo
271a0ee43a1SRui Paulo /*
272a0ee43a1SRui Paulo * If it's a byte-swapped capture file, byte-swap the header.
273a0ee43a1SRui Paulo */
274681ed54cSXin LI if (swapped) {
275a0ee43a1SRui Paulo hdr.version_major = SWAPSHORT(hdr.version_major);
276a0ee43a1SRui Paulo hdr.version_minor = SWAPSHORT(hdr.version_minor);
277a0ee43a1SRui Paulo hdr.thiszone = SWAPLONG(hdr.thiszone);
278a0ee43a1SRui Paulo hdr.sigfigs = SWAPLONG(hdr.sigfigs);
279a0ee43a1SRui Paulo hdr.snaplen = SWAPLONG(hdr.snaplen);
280a0ee43a1SRui Paulo hdr.linktype = SWAPLONG(hdr.linktype);
281a0ee43a1SRui Paulo }
282a0ee43a1SRui Paulo
283a0ee43a1SRui Paulo if (hdr.version_major < PCAP_VERSION_MAJOR) {
2846f9cba8fSJoseph Mingrone snprintf(errbuf, PCAP_ERRBUF_SIZE,
285a0ee43a1SRui Paulo "archaic pcap savefile format");
286681ed54cSXin LI *err = 1;
287681ed54cSXin LI return (NULL);
288a0ee43a1SRui Paulo }
289681ed54cSXin LI
290681ed54cSXin LI /*
291ada6f083SXin LI * currently only versions 2.[0-4] are supported with
292ada6f083SXin LI * the exception of 543.0 for DG/UX tcpdump.
293ada6f083SXin LI */
294ada6f083SXin LI if (! ((hdr.version_major == PCAP_VERSION_MAJOR &&
295ada6f083SXin LI hdr.version_minor <= PCAP_VERSION_MINOR) ||
296ada6f083SXin LI (hdr.version_major == 543 &&
297ada6f083SXin LI hdr.version_minor == 0))) {
2986f9cba8fSJoseph Mingrone snprintf(errbuf, PCAP_ERRBUF_SIZE,
299ada6f083SXin LI "unsupported pcap savefile version %u.%u",
300ada6f083SXin LI hdr.version_major, hdr.version_minor);
301ada6f083SXin LI *err = 1;
302ada6f083SXin LI return NULL;
303ada6f083SXin LI }
304ada6f083SXin LI
305ada6f083SXin LI /*
306*afdbf109SJoseph Mingrone * Check the main reserved field.
307*afdbf109SJoseph Mingrone */
308*afdbf109SJoseph Mingrone if (LT_RESERVED1(hdr.linktype) != 0) {
309*afdbf109SJoseph Mingrone snprintf(errbuf, PCAP_ERRBUF_SIZE,
310*afdbf109SJoseph Mingrone "savefile linktype reserved field not zero (0x%08x)",
311*afdbf109SJoseph Mingrone LT_RESERVED1(hdr.linktype));
312*afdbf109SJoseph Mingrone *err = 1;
313*afdbf109SJoseph Mingrone return NULL;
314*afdbf109SJoseph Mingrone }
315*afdbf109SJoseph Mingrone
316*afdbf109SJoseph Mingrone /*
317681ed54cSXin LI * OK, this is a good pcap file.
318681ed54cSXin LI * Allocate a pcap_t for it.
319681ed54cSXin LI */
3206f9cba8fSJoseph Mingrone p = PCAP_OPEN_OFFLINE_COMMON(errbuf, struct pcap_sf);
321681ed54cSXin LI if (p == NULL) {
322681ed54cSXin LI /* Allocation failed. */
323681ed54cSXin LI *err = 1;
324681ed54cSXin LI return (NULL);
325681ed54cSXin LI }
326681ed54cSXin LI p->swapped = swapped;
327681ed54cSXin LI p->version_major = hdr.version_major;
328681ed54cSXin LI p->version_minor = hdr.version_minor;
329a0ee43a1SRui Paulo p->linktype = linktype_to_dlt(LT_LINKTYPE(hdr.linktype));
330a0ee43a1SRui Paulo p->linktype_ext = LT_LINKTYPE_EXT(hdr.linktype);
331*afdbf109SJoseph Mingrone p->snapshot = pcapint_adjust_snapshot(p->linktype, hdr.snaplen);
332a0ee43a1SRui Paulo
333681ed54cSXin LI p->next_packet_op = pcap_next_packet;
334681ed54cSXin LI
335681ed54cSXin LI ps = p->priv;
336681ed54cSXin LI
337681ed54cSXin LI p->opt.tstamp_precision = precision;
338681ed54cSXin LI
339681ed54cSXin LI /*
340681ed54cSXin LI * Will we need to scale the timestamps to match what the
341681ed54cSXin LI * user wants?
342681ed54cSXin LI */
343681ed54cSXin LI switch (precision) {
344681ed54cSXin LI
345681ed54cSXin LI case PCAP_TSTAMP_PRECISION_MICRO:
34657e22627SCy Schubert if (magic_int == NSEC_TCPDUMP_MAGIC) {
347681ed54cSXin LI /*
348681ed54cSXin LI * The file has nanoseconds, the user
349681ed54cSXin LI * wants microseconds; scale the
350681ed54cSXin LI * precision down.
351681ed54cSXin LI */
352681ed54cSXin LI ps->scale_type = SCALE_DOWN;
353681ed54cSXin LI } else {
354681ed54cSXin LI /*
355681ed54cSXin LI * The file has microseconds, the
356681ed54cSXin LI * user wants microseconds; nothing to do.
357681ed54cSXin LI */
358681ed54cSXin LI ps->scale_type = PASS_THROUGH;
359681ed54cSXin LI }
360681ed54cSXin LI break;
361681ed54cSXin LI
362681ed54cSXin LI case PCAP_TSTAMP_PRECISION_NANO:
36357e22627SCy Schubert if (magic_int == NSEC_TCPDUMP_MAGIC) {
364681ed54cSXin LI /*
365681ed54cSXin LI * The file has nanoseconds, the
366681ed54cSXin LI * user wants nanoseconds; nothing to do.
367681ed54cSXin LI */
368681ed54cSXin LI ps->scale_type = PASS_THROUGH;
369681ed54cSXin LI } else {
370681ed54cSXin LI /*
3716f9cba8fSJoseph Mingrone * The file has microseconds, the user
372681ed54cSXin LI * wants nanoseconds; scale the
373681ed54cSXin LI * precision up.
374681ed54cSXin LI */
375681ed54cSXin LI ps->scale_type = SCALE_UP;
376681ed54cSXin LI }
377681ed54cSXin LI break;
378681ed54cSXin LI
379681ed54cSXin LI default:
3806f9cba8fSJoseph Mingrone snprintf(errbuf, PCAP_ERRBUF_SIZE,
381681ed54cSXin LI "unknown time stamp resolution %u", precision);
382681ed54cSXin LI free(p);
383681ed54cSXin LI *err = 1;
384681ed54cSXin LI return (NULL);
385681ed54cSXin LI }
386a0ee43a1SRui Paulo
387a0ee43a1SRui Paulo /*
388a0ee43a1SRui Paulo * We interchanged the caplen and len fields at version 2.3,
389a0ee43a1SRui Paulo * in order to match the bpf header layout. But unfortunately
390a0ee43a1SRui Paulo * some files were written with version 2.3 in their headers
391a0ee43a1SRui Paulo * but without the interchanged fields.
392a0ee43a1SRui Paulo *
393a0ee43a1SRui Paulo * In addition, DG/UX tcpdump writes out files with a version
394a0ee43a1SRui Paulo * number of 543.0, and with the caplen and len fields in the
395a0ee43a1SRui Paulo * pre-2.3 order.
396a0ee43a1SRui Paulo */
397a0ee43a1SRui Paulo switch (hdr.version_major) {
398a0ee43a1SRui Paulo
399a0ee43a1SRui Paulo case 2:
400a0ee43a1SRui Paulo if (hdr.version_minor < 3)
401681ed54cSXin LI ps->lengths_swapped = SWAPPED;
402a0ee43a1SRui Paulo else if (hdr.version_minor == 3)
403681ed54cSXin LI ps->lengths_swapped = MAYBE_SWAPPED;
404a0ee43a1SRui Paulo else
405681ed54cSXin LI ps->lengths_swapped = NOT_SWAPPED;
406a0ee43a1SRui Paulo break;
407a0ee43a1SRui Paulo
408a0ee43a1SRui Paulo case 543:
409681ed54cSXin LI ps->lengths_swapped = SWAPPED;
410a0ee43a1SRui Paulo break;
411a0ee43a1SRui Paulo
412a0ee43a1SRui Paulo default:
413681ed54cSXin LI ps->lengths_swapped = NOT_SWAPPED;
414a0ee43a1SRui Paulo break;
415a0ee43a1SRui Paulo }
416a0ee43a1SRui Paulo
41757e22627SCy Schubert if (magic_int == KUZNETZOV_TCPDUMP_MAGIC) {
418a0ee43a1SRui Paulo /*
419a0ee43a1SRui Paulo * XXX - the patch that's in some versions of libpcap
420a0ee43a1SRui Paulo * changes the packet header but not the magic number,
421a0ee43a1SRui Paulo * and some other versions with this magic number have
422a0ee43a1SRui Paulo * some extra debugging information in the packet header;
423a0ee43a1SRui Paulo * we'd have to use some hacks^H^H^H^H^Hheuristics to
424a0ee43a1SRui Paulo * detect those variants.
425a0ee43a1SRui Paulo *
426a0ee43a1SRui Paulo * Ethereal does that, but it does so by trying to read
427a0ee43a1SRui Paulo * the first two packets of the file with each of the
428a0ee43a1SRui Paulo * record header formats. That currently means it seeks
429a0ee43a1SRui Paulo * backwards and retries the reads, which doesn't work
430a0ee43a1SRui Paulo * on pipes. We want to be able to read from a pipe, so
431a0ee43a1SRui Paulo * that strategy won't work; we'd have to buffer some
432a0ee43a1SRui Paulo * data ourselves and read from that buffer in order to
433a0ee43a1SRui Paulo * make that work.
434a0ee43a1SRui Paulo */
435681ed54cSXin LI ps->hdrsize = sizeof(struct pcap_sf_patched_pkthdr);
436a0ee43a1SRui Paulo
437a0ee43a1SRui Paulo if (p->linktype == DLT_EN10MB) {
438a0ee43a1SRui Paulo /*
439a0ee43a1SRui Paulo * This capture might have been done in raw mode
440a0ee43a1SRui Paulo * or cooked mode.
441a0ee43a1SRui Paulo *
442a0ee43a1SRui Paulo * If it was done in cooked mode, p->snapshot was
443a0ee43a1SRui Paulo * passed to recvfrom() as the buffer size, meaning
444a0ee43a1SRui Paulo * that the most packet data that would be copied
445a0ee43a1SRui Paulo * would be p->snapshot. However, a faked Ethernet
446a0ee43a1SRui Paulo * header would then have been added to it, so the
447a0ee43a1SRui Paulo * most data that would be in a packet in the file
448a0ee43a1SRui Paulo * would be p->snapshot + 14.
449a0ee43a1SRui Paulo *
450a0ee43a1SRui Paulo * We can't easily tell whether the capture was done
451a0ee43a1SRui Paulo * in raw mode or cooked mode, so we'll assume it was
452a0ee43a1SRui Paulo * cooked mode, and add 14 to the snapshot length.
453a0ee43a1SRui Paulo * That means that, for a raw capture, the snapshot
454a0ee43a1SRui Paulo * length will be misleading if you use it to figure
455a0ee43a1SRui Paulo * out why a capture doesn't have all the packet data,
456a0ee43a1SRui Paulo * but there's not much we can do to avoid that.
45757e22627SCy Schubert *
45857e22627SCy Schubert * But don't grow the snapshot length past the
45957e22627SCy Schubert * maximum value of an int.
460a0ee43a1SRui Paulo */
46157e22627SCy Schubert if (p->snapshot <= INT_MAX - 14)
462a0ee43a1SRui Paulo p->snapshot += 14;
46357e22627SCy Schubert else
46457e22627SCy Schubert p->snapshot = INT_MAX;
465a0ee43a1SRui Paulo }
466a0ee43a1SRui Paulo } else
467681ed54cSXin LI ps->hdrsize = sizeof(struct pcap_sf_pkthdr);
468a0ee43a1SRui Paulo
469a0ee43a1SRui Paulo /*
470a0ee43a1SRui Paulo * Allocate a buffer for the packet data.
471b00ab754SHans Petter Selasky * Choose the minimum of the file's snapshot length and 2K bytes;
472b00ab754SHans Petter Selasky * that should be enough for most network packets - we'll grow it
473b00ab754SHans Petter Selasky * if necessary. That way, we don't allocate a huge chunk of
474b00ab754SHans Petter Selasky * memory just because there's a huge snapshot length, as the
475b00ab754SHans Petter Selasky * snapshot length might be larger than the size of the largest
476b00ab754SHans Petter Selasky * packet.
477a0ee43a1SRui Paulo */
478a0ee43a1SRui Paulo p->bufsize = p->snapshot;
479b00ab754SHans Petter Selasky if (p->bufsize > 2048)
480b00ab754SHans Petter Selasky p->bufsize = 2048;
481a0ee43a1SRui Paulo p->buffer = malloc(p->bufsize);
482a0ee43a1SRui Paulo if (p->buffer == NULL) {
4836f9cba8fSJoseph Mingrone snprintf(errbuf, PCAP_ERRBUF_SIZE, "out of memory");
484681ed54cSXin LI free(p);
485681ed54cSXin LI *err = 1;
486681ed54cSXin LI return (NULL);
487a0ee43a1SRui Paulo }
488a0ee43a1SRui Paulo
489*afdbf109SJoseph Mingrone p->cleanup_op = pcapint_sf_cleanup;
490681ed54cSXin LI
491681ed54cSXin LI return (p);
492a0ee43a1SRui Paulo }
493a0ee43a1SRui Paulo
494a0ee43a1SRui Paulo /*
495b00ab754SHans Petter Selasky * Grow the packet buffer to the specified size.
496b00ab754SHans Petter Selasky */
497b00ab754SHans Petter Selasky static int
grow_buffer(pcap_t * p,u_int bufsize)498b00ab754SHans Petter Selasky grow_buffer(pcap_t *p, u_int bufsize)
499b00ab754SHans Petter Selasky {
500b00ab754SHans Petter Selasky void *bigger_buffer;
501b00ab754SHans Petter Selasky
502b00ab754SHans Petter Selasky bigger_buffer = realloc(p->buffer, bufsize);
503b00ab754SHans Petter Selasky if (bigger_buffer == NULL) {
5046f9cba8fSJoseph Mingrone snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "out of memory");
505b00ab754SHans Petter Selasky return (0);
506b00ab754SHans Petter Selasky }
507b00ab754SHans Petter Selasky p->buffer = bigger_buffer;
508b00ab754SHans Petter Selasky p->bufsize = bufsize;
509b00ab754SHans Petter Selasky return (1);
510b00ab754SHans Petter Selasky }
511b00ab754SHans Petter Selasky
512b00ab754SHans Petter Selasky /*
513a0ee43a1SRui Paulo * Read and return the next packet from the savefile. Return the header
5146f9cba8fSJoseph Mingrone * in hdr and a pointer to the contents in data. Return 1 on success, 0
515a0ee43a1SRui Paulo * if there were no more packets, and -1 on an error.
516a0ee43a1SRui Paulo */
517a0ee43a1SRui Paulo static int
pcap_next_packet(pcap_t * p,struct pcap_pkthdr * hdr,u_char ** data)518a0ee43a1SRui Paulo pcap_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char **data)
519a0ee43a1SRui Paulo {
520681ed54cSXin LI struct pcap_sf *ps = p->priv;
521a0ee43a1SRui Paulo struct pcap_sf_patched_pkthdr sf_hdr;
522681ed54cSXin LI FILE *fp = p->rfile;
523a0ee43a1SRui Paulo size_t amt_read;
524a0ee43a1SRui Paulo bpf_u_int32 t;
525a0ee43a1SRui Paulo
526a0ee43a1SRui Paulo /*
527a0ee43a1SRui Paulo * Read the packet header; the structure we use as a buffer
528a0ee43a1SRui Paulo * is the longer structure for files generated by the patched
529a0ee43a1SRui Paulo * libpcap, but if the file has the magic number for an
530a0ee43a1SRui Paulo * unpatched libpcap we only read as many bytes as the regular
531a0ee43a1SRui Paulo * header has.
532a0ee43a1SRui Paulo */
533681ed54cSXin LI amt_read = fread(&sf_hdr, 1, ps->hdrsize, fp);
534681ed54cSXin LI if (amt_read != ps->hdrsize) {
535a0ee43a1SRui Paulo if (ferror(fp)) {
536*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
537b00ab754SHans Petter Selasky errno, "error reading dump file");
538a0ee43a1SRui Paulo return (-1);
539a0ee43a1SRui Paulo } else {
540a0ee43a1SRui Paulo if (amt_read != 0) {
5416f9cba8fSJoseph Mingrone snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
5426f9cba8fSJoseph Mingrone "truncated dump file; tried to read %zu header bytes, only got %zu",
54357e22627SCy Schubert ps->hdrsize, amt_read);
544a0ee43a1SRui Paulo return (-1);
545a0ee43a1SRui Paulo }
546a0ee43a1SRui Paulo /* EOF */
5476f9cba8fSJoseph Mingrone return (0);
548a0ee43a1SRui Paulo }
549a0ee43a1SRui Paulo }
550a0ee43a1SRui Paulo
551681ed54cSXin LI if (p->swapped) {
552a0ee43a1SRui Paulo /* these were written in opposite byte order */
553a0ee43a1SRui Paulo hdr->caplen = SWAPLONG(sf_hdr.caplen);
554a0ee43a1SRui Paulo hdr->len = SWAPLONG(sf_hdr.len);
555a0ee43a1SRui Paulo hdr->ts.tv_sec = SWAPLONG(sf_hdr.ts.tv_sec);
556a0ee43a1SRui Paulo hdr->ts.tv_usec = SWAPLONG(sf_hdr.ts.tv_usec);
557a0ee43a1SRui Paulo } else {
558a0ee43a1SRui Paulo hdr->caplen = sf_hdr.caplen;
559a0ee43a1SRui Paulo hdr->len = sf_hdr.len;
560a0ee43a1SRui Paulo hdr->ts.tv_sec = sf_hdr.ts.tv_sec;
561a0ee43a1SRui Paulo hdr->ts.tv_usec = sf_hdr.ts.tv_usec;
562a0ee43a1SRui Paulo }
563681ed54cSXin LI
564681ed54cSXin LI switch (ps->scale_type) {
565681ed54cSXin LI
566681ed54cSXin LI case PASS_THROUGH:
567681ed54cSXin LI /*
568681ed54cSXin LI * Just pass the time stamp through.
569681ed54cSXin LI */
570681ed54cSXin LI break;
571681ed54cSXin LI
572681ed54cSXin LI case SCALE_UP:
573681ed54cSXin LI /*
574681ed54cSXin LI * File has microseconds, user wants nanoseconds; convert
575681ed54cSXin LI * it.
576681ed54cSXin LI */
577681ed54cSXin LI hdr->ts.tv_usec = hdr->ts.tv_usec * 1000;
578681ed54cSXin LI break;
579681ed54cSXin LI
580681ed54cSXin LI case SCALE_DOWN:
581681ed54cSXin LI /*
582681ed54cSXin LI * File has nanoseconds, user wants microseconds; convert
583681ed54cSXin LI * it.
584681ed54cSXin LI */
585681ed54cSXin LI hdr->ts.tv_usec = hdr->ts.tv_usec / 1000;
586681ed54cSXin LI break;
587681ed54cSXin LI }
588681ed54cSXin LI
589a0ee43a1SRui Paulo /* Swap the caplen and len fields, if necessary. */
590681ed54cSXin LI switch (ps->lengths_swapped) {
591a0ee43a1SRui Paulo
592a0ee43a1SRui Paulo case NOT_SWAPPED:
593a0ee43a1SRui Paulo break;
594a0ee43a1SRui Paulo
595a0ee43a1SRui Paulo case MAYBE_SWAPPED:
596a0ee43a1SRui Paulo if (hdr->caplen <= hdr->len) {
597a0ee43a1SRui Paulo /*
598a0ee43a1SRui Paulo * The captured length is <= the actual length,
599a0ee43a1SRui Paulo * so presumably they weren't swapped.
600a0ee43a1SRui Paulo */
601a0ee43a1SRui Paulo break;
602a0ee43a1SRui Paulo }
603a0ee43a1SRui Paulo /* FALLTHROUGH */
604a0ee43a1SRui Paulo
605a0ee43a1SRui Paulo case SWAPPED:
606a0ee43a1SRui Paulo t = hdr->caplen;
607a0ee43a1SRui Paulo hdr->caplen = hdr->len;
608a0ee43a1SRui Paulo hdr->len = t;
609a0ee43a1SRui Paulo break;
610a0ee43a1SRui Paulo }
611a0ee43a1SRui Paulo
612a0ee43a1SRui Paulo /*
613b00ab754SHans Petter Selasky * Is the packet bigger than we consider sane?
614b00ab754SHans Petter Selasky */
615b00ab754SHans Petter Selasky if (hdr->caplen > max_snaplen_for_dlt(p->linktype)) {
616b00ab754SHans Petter Selasky /*
617b00ab754SHans Petter Selasky * Yes. This may be a damaged or fuzzed file.
618b00ab754SHans Petter Selasky *
619b00ab754SHans Petter Selasky * Is it bigger than the snapshot length?
620b00ab754SHans Petter Selasky * (We don't treat that as an error if it's not
621b00ab754SHans Petter Selasky * bigger than the maximum we consider sane; see
622b00ab754SHans Petter Selasky * below.)
623b00ab754SHans Petter Selasky */
624b00ab754SHans Petter Selasky if (hdr->caplen > (bpf_u_int32)p->snapshot) {
6256f9cba8fSJoseph Mingrone snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
626b00ab754SHans Petter Selasky "invalid packet capture length %u, bigger than "
627b00ab754SHans Petter Selasky "snaplen of %d", hdr->caplen, p->snapshot);
628b00ab754SHans Petter Selasky } else {
6296f9cba8fSJoseph Mingrone snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
630b00ab754SHans Petter Selasky "invalid packet capture length %u, bigger than "
631b00ab754SHans Petter Selasky "maximum of %u", hdr->caplen,
632b00ab754SHans Petter Selasky max_snaplen_for_dlt(p->linktype));
633b00ab754SHans Petter Selasky }
634b00ab754SHans Petter Selasky return (-1);
635b00ab754SHans Petter Selasky }
636b00ab754SHans Petter Selasky
637b00ab754SHans Petter Selasky if (hdr->caplen > (bpf_u_int32)p->snapshot) {
638b00ab754SHans Petter Selasky /*
639b00ab754SHans Petter Selasky * The packet is bigger than the snapshot length
640b00ab754SHans Petter Selasky * for this file.
641b00ab754SHans Petter Selasky *
642a0ee43a1SRui Paulo * This can happen due to Solaris 2.3 systems tripping
643a0ee43a1SRui Paulo * over the BUFMOD problem and not setting the snapshot
644b00ab754SHans Petter Selasky * length correctly in the savefile header.
645b00ab754SHans Petter Selasky *
646b00ab754SHans Petter Selasky * libpcap 0.4 and later on Solaris 2.3 should set the
647b00ab754SHans Petter Selasky * snapshot length correctly in the pcap file header,
648b00ab754SHans Petter Selasky * even though they don't set a snapshot length in bufmod
649b00ab754SHans Petter Selasky * (the buggy bufmod chops off the *beginning* of the
650b00ab754SHans Petter Selasky * packet if a snapshot length is specified); they should
651b00ab754SHans Petter Selasky * also reduce the captured length, as supplied to the
652b00ab754SHans Petter Selasky * per-packet callback, to the snapshot length if it's
653b00ab754SHans Petter Selasky * greater than the snapshot length, so the code using
654b00ab754SHans Petter Selasky * libpcap should see the packet cut off at the snapshot
655b00ab754SHans Petter Selasky * length, even though the full packet is copied up to
656b00ab754SHans Petter Selasky * userland.
657b00ab754SHans Petter Selasky *
658b00ab754SHans Petter Selasky * However, perhaps some versions of libpcap failed to
6596f9cba8fSJoseph Mingrone * set the snapshot length correctly in the file header
660b00ab754SHans Petter Selasky * or the per-packet header, or perhaps this is a
661*afdbf109SJoseph Mingrone * corrupted savefile or a savefile built/modified by a
662b00ab754SHans Petter Selasky * fuzz tester, so we check anyway. We grow the buffer
663b00ab754SHans Petter Selasky * to be big enough for the snapshot length, read up
664b00ab754SHans Petter Selasky * to the snapshot length, discard the rest of the
665b00ab754SHans Petter Selasky * packet, and report the snapshot length as the captured
666b00ab754SHans Petter Selasky * length; we don't want to hand our caller a packet
667b00ab754SHans Petter Selasky * bigger than the snapshot length, because they might
668b00ab754SHans Petter Selasky * be assuming they'll never be handed such a packet,
669b00ab754SHans Petter Selasky * and might copy the packet into a snapshot-length-
670b00ab754SHans Petter Selasky * sized buffer, assuming it'll fit.
671a0ee43a1SRui Paulo */
672ada6f083SXin LI size_t bytes_to_discard;
673ada6f083SXin LI size_t bytes_to_read, bytes_read;
674ada6f083SXin LI char discard_buf[4096];
675a0ee43a1SRui Paulo
676b00ab754SHans Petter Selasky if (hdr->caplen > p->bufsize) {
677b00ab754SHans Petter Selasky /*
678b00ab754SHans Petter Selasky * Grow the buffer to the snapshot length.
679b00ab754SHans Petter Selasky */
680b00ab754SHans Petter Selasky if (!grow_buffer(p, p->snapshot))
681a0ee43a1SRui Paulo return (-1);
682a0ee43a1SRui Paulo }
683a0ee43a1SRui Paulo
684ada6f083SXin LI /*
685b00ab754SHans Petter Selasky * Read the first p->snapshot bytes into the buffer.
686ada6f083SXin LI */
687b00ab754SHans Petter Selasky amt_read = fread(p->buffer, 1, p->snapshot, fp);
688b00ab754SHans Petter Selasky if (amt_read != (bpf_u_int32)p->snapshot) {
689a0ee43a1SRui Paulo if (ferror(fp)) {
690*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf,
691b00ab754SHans Petter Selasky PCAP_ERRBUF_SIZE, errno,
692b00ab754SHans Petter Selasky "error reading dump file");
693a0ee43a1SRui Paulo } else {
694ada6f083SXin LI /*
695ada6f083SXin LI * Yes, this uses hdr->caplen; technically,
696ada6f083SXin LI * it's true, because we would try to read
697ada6f083SXin LI * and discard the rest of those bytes, and
698ada6f083SXin LI * that would fail because we got EOF before
699ada6f083SXin LI * the read finished.
700ada6f083SXin LI */
7016f9cba8fSJoseph Mingrone snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
7026f9cba8fSJoseph Mingrone "truncated dump file; tried to read %d captured bytes, only got %zu",
70357e22627SCy Schubert p->snapshot, amt_read);
704a0ee43a1SRui Paulo }
705a0ee43a1SRui Paulo return (-1);
706a0ee43a1SRui Paulo }
707ada6f083SXin LI
708a0ee43a1SRui Paulo /*
709ada6f083SXin LI * Now read and discard what's left.
710ada6f083SXin LI */
711b00ab754SHans Petter Selasky bytes_to_discard = hdr->caplen - p->snapshot;
712ada6f083SXin LI bytes_read = amt_read;
713ada6f083SXin LI while (bytes_to_discard != 0) {
714ada6f083SXin LI bytes_to_read = bytes_to_discard;
715ada6f083SXin LI if (bytes_to_read > sizeof (discard_buf))
716ada6f083SXin LI bytes_to_read = sizeof (discard_buf);
717ada6f083SXin LI amt_read = fread(discard_buf, 1, bytes_to_read, fp);
718ada6f083SXin LI bytes_read += amt_read;
719ada6f083SXin LI if (amt_read != bytes_to_read) {
720ada6f083SXin LI if (ferror(fp)) {
721*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf,
722b00ab754SHans Petter Selasky PCAP_ERRBUF_SIZE, errno,
723b00ab754SHans Petter Selasky "error reading dump file");
724ada6f083SXin LI } else {
7256f9cba8fSJoseph Mingrone snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
7266f9cba8fSJoseph Mingrone "truncated dump file; tried to read %u captured bytes, only got %zu",
72757e22627SCy Schubert hdr->caplen, bytes_read);
728ada6f083SXin LI }
729ada6f083SXin LI return (-1);
730ada6f083SXin LI }
731ada6f083SXin LI bytes_to_discard -= amt_read;
732ada6f083SXin LI }
733ada6f083SXin LI
734ada6f083SXin LI /*
735ada6f083SXin LI * Adjust caplen accordingly, so we don't get confused later
736ada6f083SXin LI * as to how many bytes we have to play with.
737a0ee43a1SRui Paulo */
738b00ab754SHans Petter Selasky hdr->caplen = p->snapshot;
739a0ee43a1SRui Paulo } else {
74057e22627SCy Schubert /*
74157e22627SCy Schubert * The packet is within the snapshot length for this file.
74257e22627SCy Schubert */
743b00ab754SHans Petter Selasky if (hdr->caplen > p->bufsize) {
744b00ab754SHans Petter Selasky /*
745b00ab754SHans Petter Selasky * Grow the buffer to the next power of 2, or
746b00ab754SHans Petter Selasky * the snaplen, whichever is lower.
747b00ab754SHans Petter Selasky */
748b00ab754SHans Petter Selasky u_int new_bufsize;
749b00ab754SHans Petter Selasky
750b00ab754SHans Petter Selasky new_bufsize = hdr->caplen;
751b00ab754SHans Petter Selasky /*
7526f9cba8fSJoseph Mingrone * https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
753b00ab754SHans Petter Selasky */
754b00ab754SHans Petter Selasky new_bufsize--;
755b00ab754SHans Petter Selasky new_bufsize |= new_bufsize >> 1;
756b00ab754SHans Petter Selasky new_bufsize |= new_bufsize >> 2;
757b00ab754SHans Petter Selasky new_bufsize |= new_bufsize >> 4;
758b00ab754SHans Petter Selasky new_bufsize |= new_bufsize >> 8;
759b00ab754SHans Petter Selasky new_bufsize |= new_bufsize >> 16;
760b00ab754SHans Petter Selasky new_bufsize++;
761b00ab754SHans Petter Selasky
762b00ab754SHans Petter Selasky if (new_bufsize > (u_int)p->snapshot)
763b00ab754SHans Petter Selasky new_bufsize = p->snapshot;
764b00ab754SHans Petter Selasky
765b00ab754SHans Petter Selasky if (!grow_buffer(p, new_bufsize))
766b00ab754SHans Petter Selasky return (-1);
767b00ab754SHans Petter Selasky }
768b00ab754SHans Petter Selasky
769a0ee43a1SRui Paulo /* read the packet itself */
770a0ee43a1SRui Paulo amt_read = fread(p->buffer, 1, hdr->caplen, fp);
771a0ee43a1SRui Paulo if (amt_read != hdr->caplen) {
772a0ee43a1SRui Paulo if (ferror(fp)) {
773*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf,
774b00ab754SHans Petter Selasky PCAP_ERRBUF_SIZE, errno,
775b00ab754SHans Petter Selasky "error reading dump file");
776a0ee43a1SRui Paulo } else {
7776f9cba8fSJoseph Mingrone snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
7786f9cba8fSJoseph Mingrone "truncated dump file; tried to read %u captured bytes, only got %zu",
77957e22627SCy Schubert hdr->caplen, amt_read);
780a0ee43a1SRui Paulo }
781a0ee43a1SRui Paulo return (-1);
782a0ee43a1SRui Paulo }
783a0ee43a1SRui Paulo }
784a0ee43a1SRui Paulo *data = p->buffer;
785a0ee43a1SRui Paulo
786*afdbf109SJoseph Mingrone pcapint_post_process(p->linktype, p->swapped, hdr, *data);
787a0ee43a1SRui Paulo
7886f9cba8fSJoseph Mingrone return (1);
789a0ee43a1SRui Paulo }
790a0ee43a1SRui Paulo
791a0ee43a1SRui Paulo static int
sf_write_header(pcap_t * p,FILE * fp,int linktype,int snaplen)7926f9cba8fSJoseph Mingrone sf_write_header(pcap_t *p, FILE *fp, int linktype, int snaplen)
793a0ee43a1SRui Paulo {
794a0ee43a1SRui Paulo struct pcap_file_header hdr;
795a0ee43a1SRui Paulo
796681ed54cSXin LI hdr.magic = p->opt.tstamp_precision == PCAP_TSTAMP_PRECISION_NANO ? NSEC_TCPDUMP_MAGIC : TCPDUMP_MAGIC;
797a0ee43a1SRui Paulo hdr.version_major = PCAP_VERSION_MAJOR;
798a0ee43a1SRui Paulo hdr.version_minor = PCAP_VERSION_MINOR;
799a0ee43a1SRui Paulo
8006f9cba8fSJoseph Mingrone /*
8016f9cba8fSJoseph Mingrone * https://www.tcpdump.org/manpages/pcap-savefile.5.txt states:
802*afdbf109SJoseph Mingrone * thiszone (Reserved1): 4-byte not used - SHOULD be filled with 0
803*afdbf109SJoseph Mingrone * sigfigs (Reserved2): 4-byte not used - SHOULD be filled with 0
8046f9cba8fSJoseph Mingrone */
8056f9cba8fSJoseph Mingrone hdr.thiszone = 0;
806a0ee43a1SRui Paulo hdr.sigfigs = 0;
8076f9cba8fSJoseph Mingrone hdr.snaplen = snaplen;
808a0ee43a1SRui Paulo hdr.linktype = linktype;
809a0ee43a1SRui Paulo
810a0ee43a1SRui Paulo if (fwrite((char *)&hdr, sizeof(hdr), 1, fp) != 1)
811a0ee43a1SRui Paulo return (-1);
812a0ee43a1SRui Paulo
813a0ee43a1SRui Paulo return (0);
814a0ee43a1SRui Paulo }
815a0ee43a1SRui Paulo
816a0ee43a1SRui Paulo /*
817a0ee43a1SRui Paulo * Output a packet to the initialized dump file.
818a0ee43a1SRui Paulo */
819a0ee43a1SRui Paulo void
pcap_dump(u_char * user,const struct pcap_pkthdr * h,const u_char * sp)820a0ee43a1SRui Paulo pcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
821a0ee43a1SRui Paulo {
822a0ee43a1SRui Paulo register FILE *f;
823a0ee43a1SRui Paulo struct pcap_sf_pkthdr sf_hdr;
824a0ee43a1SRui Paulo
825a0ee43a1SRui Paulo f = (FILE *)user;
8266f9cba8fSJoseph Mingrone /*
8276f9cba8fSJoseph Mingrone * If the output file handle is in an error state, don't write
8286f9cba8fSJoseph Mingrone * anything.
8296f9cba8fSJoseph Mingrone *
8306f9cba8fSJoseph Mingrone * While in principle a file handle can return from an error state
8316f9cba8fSJoseph Mingrone * to a normal state (for example if a disk that is full has space
8326f9cba8fSJoseph Mingrone * freed), we have possibly left a broken file already, and won't
8336f9cba8fSJoseph Mingrone * be able to clean it up. The safest option is to do nothing.
8346f9cba8fSJoseph Mingrone *
8356f9cba8fSJoseph Mingrone * Note that if we could guarantee that fwrite() was atomic we
8366f9cba8fSJoseph Mingrone * might be able to insure that we don't produce a corrupted file,
8376f9cba8fSJoseph Mingrone * but the standard defines fwrite() as a series of fputc() calls,
8386f9cba8fSJoseph Mingrone * so we really have no insurance that things are not fubared.
8396f9cba8fSJoseph Mingrone *
8406f9cba8fSJoseph Mingrone * http://pubs.opengroup.org/onlinepubs/009695399/functions/fwrite.html
8416f9cba8fSJoseph Mingrone */
8426f9cba8fSJoseph Mingrone if (ferror(f))
8436f9cba8fSJoseph Mingrone return;
8446f9cba8fSJoseph Mingrone /*
8456f9cba8fSJoseph Mingrone * Better not try writing pcap files after
846*afdbf109SJoseph Mingrone * 2106-02-07 06:28:15 UTC; switch to pcapng.
847*afdbf109SJoseph Mingrone * (And better not try writing pcap files with time stamps
848*afdbf109SJoseph Mingrone * that predate 1970-01-01 00:00:00 UTC; that's not supported.
849*afdbf109SJoseph Mingrone * You could try using pcapng with the if_tsoffset field in
850*afdbf109SJoseph Mingrone * the IDB for the interface(s) with packets with those time
851*afdbf109SJoseph Mingrone * stamps, but you may also have to get a link-layer type for
852*afdbf109SJoseph Mingrone * IBM Bisync or whatever link layer even older forms
853*afdbf109SJoseph Mingrone * of computer communication used.)
8546f9cba8fSJoseph Mingrone */
855*afdbf109SJoseph Mingrone sf_hdr.ts.tv_sec = (bpf_u_int32)h->ts.tv_sec;
856*afdbf109SJoseph Mingrone sf_hdr.ts.tv_usec = (bpf_u_int32)h->ts.tv_usec;
857a0ee43a1SRui Paulo sf_hdr.caplen = h->caplen;
858a0ee43a1SRui Paulo sf_hdr.len = h->len;
8596f9cba8fSJoseph Mingrone /*
8606f9cba8fSJoseph Mingrone * We only write the packet if we can write the header properly.
8616f9cba8fSJoseph Mingrone *
8626f9cba8fSJoseph Mingrone * This doesn't prevent us from having corrupted output, and if we
8636f9cba8fSJoseph Mingrone * for some reason don't get a complete write we don't have any
8646f9cba8fSJoseph Mingrone * way to set ferror() to prevent future writes from being
8656f9cba8fSJoseph Mingrone * attempted, but it is better than nothing.
8666f9cba8fSJoseph Mingrone */
8676f9cba8fSJoseph Mingrone if (fwrite(&sf_hdr, sizeof(sf_hdr), 1, f) == 1) {
868a0ee43a1SRui Paulo (void)fwrite(sp, h->caplen, 1, f);
869a0ee43a1SRui Paulo }
8706f9cba8fSJoseph Mingrone }
871a0ee43a1SRui Paulo
872a0ee43a1SRui Paulo static pcap_dumper_t *
pcap_setup_dump(pcap_t * p,int linktype,FILE * f,const char * fname)873a0ee43a1SRui Paulo pcap_setup_dump(pcap_t *p, int linktype, FILE *f, const char *fname)
874a0ee43a1SRui Paulo {
875a0ee43a1SRui Paulo
876ada6f083SXin LI #if defined(_WIN32) || defined(MSDOS)
877a0ee43a1SRui Paulo /*
878a0ee43a1SRui Paulo * If we're writing to the standard output, put it in binary
879a0ee43a1SRui Paulo * mode, as savefiles are binary files.
880a0ee43a1SRui Paulo *
881a0ee43a1SRui Paulo * Otherwise, we turn off buffering.
882a0ee43a1SRui Paulo * XXX - why? And why not on the standard output?
883a0ee43a1SRui Paulo */
884a0ee43a1SRui Paulo if (f == stdout)
885a0ee43a1SRui Paulo SET_BINMODE(f);
886a0ee43a1SRui Paulo else
887b00ab754SHans Petter Selasky setvbuf(f, NULL, _IONBF, 0);
888a0ee43a1SRui Paulo #endif
8896f9cba8fSJoseph Mingrone if (sf_write_header(p, f, linktype, p->snapshot) == -1) {
890*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
891b00ab754SHans Petter Selasky errno, "Can't write to %s", fname);
892a0ee43a1SRui Paulo if (f != stdout)
893a0ee43a1SRui Paulo (void)fclose(f);
894a0ee43a1SRui Paulo return (NULL);
895a0ee43a1SRui Paulo }
896a0ee43a1SRui Paulo return ((pcap_dumper_t *)f);
897a0ee43a1SRui Paulo }
898a0ee43a1SRui Paulo
899a0ee43a1SRui Paulo /*
900a0ee43a1SRui Paulo * Initialize so that sf_write() will output to the file named 'fname'.
901a0ee43a1SRui Paulo */
902a0ee43a1SRui Paulo pcap_dumper_t *
pcap_dump_open(pcap_t * p,const char * fname)903a0ee43a1SRui Paulo pcap_dump_open(pcap_t *p, const char *fname)
904a0ee43a1SRui Paulo {
905a0ee43a1SRui Paulo FILE *f;
906a0ee43a1SRui Paulo int linktype;
907a0ee43a1SRui Paulo
908a0ee43a1SRui Paulo /*
909a0ee43a1SRui Paulo * If this pcap_t hasn't been activated, it doesn't have a
910a0ee43a1SRui Paulo * link-layer type, so we can't use it.
911a0ee43a1SRui Paulo */
912a0ee43a1SRui Paulo if (!p->activated) {
9136f9cba8fSJoseph Mingrone snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
914a0ee43a1SRui Paulo "%s: not-yet-activated pcap_t passed to pcap_dump_open",
915a0ee43a1SRui Paulo fname);
916a0ee43a1SRui Paulo return (NULL);
917a0ee43a1SRui Paulo }
918a0ee43a1SRui Paulo linktype = dlt_to_linktype(p->linktype);
919a0ee43a1SRui Paulo if (linktype == -1) {
9206f9cba8fSJoseph Mingrone snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
921a0ee43a1SRui Paulo "%s: link-layer type %d isn't supported in savefiles",
922a0ee43a1SRui Paulo fname, p->linktype);
923a0ee43a1SRui Paulo return (NULL);
924a0ee43a1SRui Paulo }
925a0ee43a1SRui Paulo linktype |= p->linktype_ext;
926a0ee43a1SRui Paulo
927ada6f083SXin LI if (fname == NULL) {
9286f9cba8fSJoseph Mingrone snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
929ada6f083SXin LI "A null pointer was supplied as the file name");
930ada6f083SXin LI return NULL;
931ada6f083SXin LI }
932a0ee43a1SRui Paulo if (fname[0] == '-' && fname[1] == '\0') {
933a0ee43a1SRui Paulo f = stdout;
934a0ee43a1SRui Paulo fname = "standard output";
935a0ee43a1SRui Paulo } else {
936b00ab754SHans Petter Selasky /*
937b00ab754SHans Petter Selasky * "b" is supported as of C90, so *all* UN*Xes should
938b00ab754SHans Petter Selasky * support it, even though it does nothing. It's
939b00ab754SHans Petter Selasky * required on Windows, as the file is a binary file
940b00ab754SHans Petter Selasky * and must be written in binary mode.
941b00ab754SHans Petter Selasky */
942*afdbf109SJoseph Mingrone f = pcapint_charset_fopen(fname, "wb");
943a0ee43a1SRui Paulo if (f == NULL) {
944*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
945b00ab754SHans Petter Selasky errno, "%s", fname);
946a0ee43a1SRui Paulo return (NULL);
947a0ee43a1SRui Paulo }
948a0ee43a1SRui Paulo }
949a0ee43a1SRui Paulo return (pcap_setup_dump(p, linktype, f, fname));
950a0ee43a1SRui Paulo }
951a0ee43a1SRui Paulo
95257e22627SCy Schubert #ifdef _WIN32
95357e22627SCy Schubert /*
95457e22627SCy Schubert * Initialize so that sf_write() will output to a stream wrapping the given raw
95557e22627SCy Schubert * OS file HANDLE.
95657e22627SCy Schubert */
95757e22627SCy Schubert pcap_dumper_t *
pcap_dump_hopen(pcap_t * p,intptr_t osfd)95857e22627SCy Schubert pcap_dump_hopen(pcap_t *p, intptr_t osfd)
95957e22627SCy Schubert {
96057e22627SCy Schubert int fd;
96157e22627SCy Schubert FILE *file;
96257e22627SCy Schubert
96357e22627SCy Schubert fd = _open_osfhandle(osfd, _O_APPEND);
96457e22627SCy Schubert if (fd < 0) {
965*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
96657e22627SCy Schubert errno, "_open_osfhandle");
96757e22627SCy Schubert return NULL;
96857e22627SCy Schubert }
96957e22627SCy Schubert
97057e22627SCy Schubert file = _fdopen(fd, "wb");
97157e22627SCy Schubert if (file == NULL) {
972*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
97357e22627SCy Schubert errno, "_fdopen");
97457e22627SCy Schubert _close(fd);
97557e22627SCy Schubert return NULL;
97657e22627SCy Schubert }
97757e22627SCy Schubert
97857e22627SCy Schubert return pcap_dump_fopen(p, file);
97957e22627SCy Schubert }
98057e22627SCy Schubert #endif /* _WIN32 */
98157e22627SCy Schubert
982a0ee43a1SRui Paulo /*
983a0ee43a1SRui Paulo * Initialize so that sf_write() will output to the given stream.
984a0ee43a1SRui Paulo */
98557e22627SCy Schubert #ifdef _WIN32
98657e22627SCy Schubert static
98757e22627SCy Schubert #endif /* _WIN32 */
988a0ee43a1SRui Paulo pcap_dumper_t *
pcap_dump_fopen(pcap_t * p,FILE * f)989a0ee43a1SRui Paulo pcap_dump_fopen(pcap_t *p, FILE *f)
990a0ee43a1SRui Paulo {
991a0ee43a1SRui Paulo int linktype;
992a0ee43a1SRui Paulo
993a0ee43a1SRui Paulo linktype = dlt_to_linktype(p->linktype);
994a0ee43a1SRui Paulo if (linktype == -1) {
9956f9cba8fSJoseph Mingrone snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
996a0ee43a1SRui Paulo "stream: link-layer type %d isn't supported in savefiles",
997a0ee43a1SRui Paulo p->linktype);
998a0ee43a1SRui Paulo return (NULL);
999a0ee43a1SRui Paulo }
1000a0ee43a1SRui Paulo linktype |= p->linktype_ext;
1001a0ee43a1SRui Paulo
1002a0ee43a1SRui Paulo return (pcap_setup_dump(p, linktype, f, "stream"));
1003a0ee43a1SRui Paulo }
1004a0ee43a1SRui Paulo
1005ada6f083SXin LI pcap_dumper_t *
pcap_dump_open_append(pcap_t * p,const char * fname)1006ada6f083SXin LI pcap_dump_open_append(pcap_t *p, const char *fname)
1007ada6f083SXin LI {
1008ada6f083SXin LI FILE *f;
1009ada6f083SXin LI int linktype;
1010ada6f083SXin LI size_t amt_read;
1011ada6f083SXin LI struct pcap_file_header ph;
1012ada6f083SXin LI
1013ada6f083SXin LI linktype = dlt_to_linktype(p->linktype);
1014ada6f083SXin LI if (linktype == -1) {
10156f9cba8fSJoseph Mingrone snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1016ada6f083SXin LI "%s: link-layer type %d isn't supported in savefiles",
1017ada6f083SXin LI fname, linktype);
1018ada6f083SXin LI return (NULL);
1019ada6f083SXin LI }
1020ada6f083SXin LI
1021ada6f083SXin LI if (fname == NULL) {
10226f9cba8fSJoseph Mingrone snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1023ada6f083SXin LI "A null pointer was supplied as the file name");
1024ada6f083SXin LI return NULL;
1025ada6f083SXin LI }
1026ada6f083SXin LI if (fname[0] == '-' && fname[1] == '\0')
1027ada6f083SXin LI return (pcap_setup_dump(p, linktype, stdout, "standard output"));
1028ada6f083SXin LI
1029b00ab754SHans Petter Selasky /*
103057e22627SCy Schubert * "a" will cause the file *not* to be truncated if it exists
103157e22627SCy Schubert * but will cause it to be created if it doesn't. It will
103257e22627SCy Schubert * also cause all writes to be done at the end of the file,
103357e22627SCy Schubert * but will allow reads to be done anywhere in the file. This
103457e22627SCy Schubert * is what we need, because we need to read from the beginning
103557e22627SCy Schubert * of the file to see if it already has a header and packets
103657e22627SCy Schubert * or if it doesn't.
103757e22627SCy Schubert *
1038b00ab754SHans Petter Selasky * "b" is supported as of C90, so *all* UN*Xes should support it,
1039b00ab754SHans Petter Selasky * even though it does nothing. It's required on Windows, as the
1040b00ab754SHans Petter Selasky * file is a binary file and must be read in binary mode.
1041b00ab754SHans Petter Selasky */
1042*afdbf109SJoseph Mingrone f = pcapint_charset_fopen(fname, "ab+");
1043ada6f083SXin LI if (f == NULL) {
1044*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
1045b00ab754SHans Petter Selasky errno, "%s", fname);
1046ada6f083SXin LI return (NULL);
1047ada6f083SXin LI }
1048ada6f083SXin LI
1049ada6f083SXin LI /*
1050ada6f083SXin LI * Try to read a pcap header.
105157e22627SCy Schubert *
105257e22627SCy Schubert * We do not assume that the file will be positioned at the
105357e22627SCy Schubert * beginning immediately after we've opened it - we seek to
105457e22627SCy Schubert * the beginning. ISO C says it's implementation-defined
105557e22627SCy Schubert * whether the file position indicator is at the beginning
105657e22627SCy Schubert * or the end of the file after an append-mode open, and
105757e22627SCy Schubert * it wasn't obvious from the Single UNIX Specification
105857e22627SCy Schubert * or the Microsoft documentation how that works on SUS-
105957e22627SCy Schubert * compliant systems or on Windows.
1060ada6f083SXin LI */
106157e22627SCy Schubert if (fseek(f, 0, SEEK_SET) == -1) {
1062*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
106357e22627SCy Schubert errno, "Can't seek to the beginning of %s", fname);
106457e22627SCy Schubert (void)fclose(f);
106557e22627SCy Schubert return (NULL);
106657e22627SCy Schubert }
1067ada6f083SXin LI amt_read = fread(&ph, 1, sizeof (ph), f);
1068ada6f083SXin LI if (amt_read != sizeof (ph)) {
1069ada6f083SXin LI if (ferror(f)) {
1070*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
1071b00ab754SHans Petter Selasky errno, "%s", fname);
107257e22627SCy Schubert (void)fclose(f);
1073ada6f083SXin LI return (NULL);
1074ada6f083SXin LI } else if (feof(f) && amt_read > 0) {
10756f9cba8fSJoseph Mingrone snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1076ada6f083SXin LI "%s: truncated pcap file header", fname);
107757e22627SCy Schubert (void)fclose(f);
1078ada6f083SXin LI return (NULL);
1079ada6f083SXin LI }
1080ada6f083SXin LI }
1081ada6f083SXin LI
1082ada6f083SXin LI #if defined(_WIN32) || defined(MSDOS)
1083ada6f083SXin LI /*
1084ada6f083SXin LI * We turn off buffering.
1085ada6f083SXin LI * XXX - why? And why not on the standard output?
1086ada6f083SXin LI */
1087b00ab754SHans Petter Selasky setvbuf(f, NULL, _IONBF, 0);
1088ada6f083SXin LI #endif
1089ada6f083SXin LI
1090ada6f083SXin LI /*
1091ada6f083SXin LI * If a header is already present and:
1092ada6f083SXin LI *
1093ada6f083SXin LI * it's not for a pcap file of the appropriate resolution
1094ada6f083SXin LI * and the right byte order for this machine;
1095ada6f083SXin LI *
1096ada6f083SXin LI * the link-layer header types don't match;
1097ada6f083SXin LI *
1098ada6f083SXin LI * the snapshot lengths don't match;
1099ada6f083SXin LI *
1100ada6f083SXin LI * return an error.
1101ada6f083SXin LI */
1102ada6f083SXin LI if (amt_read > 0) {
1103ada6f083SXin LI /*
1104ada6f083SXin LI * A header is already present.
1105ada6f083SXin LI * Do the checks.
1106ada6f083SXin LI */
1107ada6f083SXin LI switch (ph.magic) {
1108ada6f083SXin LI
1109ada6f083SXin LI case TCPDUMP_MAGIC:
1110ada6f083SXin LI if (p->opt.tstamp_precision != PCAP_TSTAMP_PRECISION_MICRO) {
11116f9cba8fSJoseph Mingrone snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1112ada6f083SXin LI "%s: different time stamp precision, cannot append to file", fname);
111357e22627SCy Schubert (void)fclose(f);
1114ada6f083SXin LI return (NULL);
1115ada6f083SXin LI }
1116ada6f083SXin LI break;
1117ada6f083SXin LI
1118ada6f083SXin LI case NSEC_TCPDUMP_MAGIC:
1119ada6f083SXin LI if (p->opt.tstamp_precision != PCAP_TSTAMP_PRECISION_NANO) {
11206f9cba8fSJoseph Mingrone snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1121ada6f083SXin LI "%s: different time stamp precision, cannot append to file", fname);
112257e22627SCy Schubert (void)fclose(f);
1123ada6f083SXin LI return (NULL);
1124ada6f083SXin LI }
1125ada6f083SXin LI break;
1126ada6f083SXin LI
1127ada6f083SXin LI case SWAPLONG(TCPDUMP_MAGIC):
1128ada6f083SXin LI case SWAPLONG(NSEC_TCPDUMP_MAGIC):
11296f9cba8fSJoseph Mingrone snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1130ada6f083SXin LI "%s: different byte order, cannot append to file", fname);
113157e22627SCy Schubert (void)fclose(f);
1132ada6f083SXin LI return (NULL);
1133ada6f083SXin LI
1134ada6f083SXin LI case KUZNETZOV_TCPDUMP_MAGIC:
1135ada6f083SXin LI case SWAPLONG(KUZNETZOV_TCPDUMP_MAGIC):
1136ada6f083SXin LI case NAVTEL_TCPDUMP_MAGIC:
1137ada6f083SXin LI case SWAPLONG(NAVTEL_TCPDUMP_MAGIC):
11386f9cba8fSJoseph Mingrone snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1139ada6f083SXin LI "%s: not a pcap file to which we can append", fname);
114057e22627SCy Schubert (void)fclose(f);
1141ada6f083SXin LI return (NULL);
1142ada6f083SXin LI
1143ada6f083SXin LI default:
11446f9cba8fSJoseph Mingrone snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1145ada6f083SXin LI "%s: not a pcap file", fname);
114657e22627SCy Schubert (void)fclose(f);
1147ada6f083SXin LI return (NULL);
1148ada6f083SXin LI }
1149ada6f083SXin LI
1150ada6f083SXin LI /*
1151ada6f083SXin LI * Good version?
1152ada6f083SXin LI */
1153ada6f083SXin LI if (ph.version_major != PCAP_VERSION_MAJOR ||
1154ada6f083SXin LI ph.version_minor != PCAP_VERSION_MINOR) {
11556f9cba8fSJoseph Mingrone snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1156ada6f083SXin LI "%s: version is %u.%u, cannot append to file", fname,
1157ada6f083SXin LI ph.version_major, ph.version_minor);
115857e22627SCy Schubert (void)fclose(f);
1159ada6f083SXin LI return (NULL);
1160ada6f083SXin LI }
1161ada6f083SXin LI if ((bpf_u_int32)linktype != ph.linktype) {
11626f9cba8fSJoseph Mingrone snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1163ada6f083SXin LI "%s: different linktype, cannot append to file", fname);
116457e22627SCy Schubert (void)fclose(f);
1165ada6f083SXin LI return (NULL);
1166ada6f083SXin LI }
1167ada6f083SXin LI if ((bpf_u_int32)p->snapshot != ph.snaplen) {
11686f9cba8fSJoseph Mingrone snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1169ada6f083SXin LI "%s: different snaplen, cannot append to file", fname);
117057e22627SCy Schubert (void)fclose(f);
1171ada6f083SXin LI return (NULL);
1172ada6f083SXin LI }
1173ada6f083SXin LI } else {
1174ada6f083SXin LI /*
1175ada6f083SXin LI * A header isn't present; attempt to write it.
1176ada6f083SXin LI */
11776f9cba8fSJoseph Mingrone if (sf_write_header(p, f, linktype, p->snapshot) == -1) {
1178*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
1179b00ab754SHans Petter Selasky errno, "Can't write to %s", fname);
1180ada6f083SXin LI (void)fclose(f);
1181ada6f083SXin LI return (NULL);
1182ada6f083SXin LI }
1183ada6f083SXin LI }
1184ada6f083SXin LI
1185ada6f083SXin LI /*
1186ada6f083SXin LI * Start writing at the end of the file.
118757e22627SCy Schubert *
118857e22627SCy Schubert * XXX - this shouldn't be necessary, given that we're opening
118957e22627SCy Schubert * the file in append mode, and ISO C specifies that all writes
119057e22627SCy Schubert * are done at the end of the file in that mode.
1191ada6f083SXin LI */
1192ada6f083SXin LI if (fseek(f, 0, SEEK_END) == -1) {
1193*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
119457e22627SCy Schubert errno, "Can't seek to the end of %s", fname);
1195ada6f083SXin LI (void)fclose(f);
1196ada6f083SXin LI return (NULL);
1197ada6f083SXin LI }
1198ada6f083SXin LI return ((pcap_dumper_t *)f);
1199ada6f083SXin LI }
1200ada6f083SXin LI
1201a0ee43a1SRui Paulo FILE *
pcap_dump_file(pcap_dumper_t * p)1202a0ee43a1SRui Paulo pcap_dump_file(pcap_dumper_t *p)
1203a0ee43a1SRui Paulo {
1204a0ee43a1SRui Paulo return ((FILE *)p);
1205a0ee43a1SRui Paulo }
1206a0ee43a1SRui Paulo
1207a0ee43a1SRui Paulo long
pcap_dump_ftell(pcap_dumper_t * p)1208a0ee43a1SRui Paulo pcap_dump_ftell(pcap_dumper_t *p)
1209a0ee43a1SRui Paulo {
1210a0ee43a1SRui Paulo return (ftell((FILE *)p));
1211a0ee43a1SRui Paulo }
1212a0ee43a1SRui Paulo
1213b00ab754SHans Petter Selasky #if defined(HAVE_FSEEKO)
1214b00ab754SHans Petter Selasky /*
1215b00ab754SHans Petter Selasky * We have fseeko(), so we have ftello().
1216b00ab754SHans Petter Selasky * If we have large file support (files larger than 2^31-1 bytes),
1217b00ab754SHans Petter Selasky * ftello() will give us a current file position with more than 32
1218b00ab754SHans Petter Selasky * bits.
1219b00ab754SHans Petter Selasky */
1220b00ab754SHans Petter Selasky int64_t
pcap_dump_ftell64(pcap_dumper_t * p)1221b00ab754SHans Petter Selasky pcap_dump_ftell64(pcap_dumper_t *p)
1222b00ab754SHans Petter Selasky {
1223b00ab754SHans Petter Selasky return (ftello((FILE *)p));
1224b00ab754SHans Petter Selasky }
1225b00ab754SHans Petter Selasky #elif defined(_MSC_VER)
1226b00ab754SHans Petter Selasky /*
1227b00ab754SHans Petter Selasky * We have Visual Studio; we support only 2005 and later, so we have
1228b00ab754SHans Petter Selasky * _ftelli64().
1229b00ab754SHans Petter Selasky */
1230b00ab754SHans Petter Selasky int64_t
pcap_dump_ftell64(pcap_dumper_t * p)1231b00ab754SHans Petter Selasky pcap_dump_ftell64(pcap_dumper_t *p)
1232b00ab754SHans Petter Selasky {
1233b00ab754SHans Petter Selasky return (_ftelli64((FILE *)p));
1234b00ab754SHans Petter Selasky }
1235b00ab754SHans Petter Selasky #else
1236b00ab754SHans Petter Selasky /*
1237b00ab754SHans Petter Selasky * We don't have ftello() or _ftelli64(), so fall back on ftell().
1238b00ab754SHans Petter Selasky * Either long is 64 bits, in which case ftell() should suffice,
1239b00ab754SHans Petter Selasky * or this is probably an older 32-bit UN*X without large file
1240b00ab754SHans Petter Selasky * support, which means you'll probably get errors trying to
1241b00ab754SHans Petter Selasky * write files > 2^31-1, so it won't matter anyway.
1242b00ab754SHans Petter Selasky *
1243b00ab754SHans Petter Selasky * XXX - what about MinGW?
1244b00ab754SHans Petter Selasky */
1245b00ab754SHans Petter Selasky int64_t
pcap_dump_ftell64(pcap_dumper_t * p)1246b00ab754SHans Petter Selasky pcap_dump_ftell64(pcap_dumper_t *p)
1247b00ab754SHans Petter Selasky {
1248b00ab754SHans Petter Selasky return (ftell((FILE *)p));
1249b00ab754SHans Petter Selasky }
1250b00ab754SHans Petter Selasky #endif
1251b00ab754SHans Petter Selasky
1252a0ee43a1SRui Paulo int
pcap_dump_flush(pcap_dumper_t * p)1253a0ee43a1SRui Paulo pcap_dump_flush(pcap_dumper_t *p)
1254a0ee43a1SRui Paulo {
1255a0ee43a1SRui Paulo
1256a0ee43a1SRui Paulo if (fflush((FILE *)p) == EOF)
1257a0ee43a1SRui Paulo return (-1);
1258a0ee43a1SRui Paulo else
1259a0ee43a1SRui Paulo return (0);
1260a0ee43a1SRui Paulo }
1261a0ee43a1SRui Paulo
1262a0ee43a1SRui Paulo void
pcap_dump_close(pcap_dumper_t * p)1263a0ee43a1SRui Paulo pcap_dump_close(pcap_dumper_t *p)
1264a0ee43a1SRui Paulo {
1265a0ee43a1SRui Paulo
1266a0ee43a1SRui Paulo #ifdef notyet
1267a0ee43a1SRui Paulo if (ferror((FILE *)p))
1268a0ee43a1SRui Paulo return-an-error;
1269a0ee43a1SRui Paulo /* XXX should check return from fclose() too */
1270a0ee43a1SRui Paulo #endif
1271a0ee43a1SRui Paulo (void)fclose((FILE *)p);
1272a0ee43a1SRui Paulo }
1273