1*d5dfe396Schristos /* $NetBSD: savefile.c,v 1.6 2019/10/01 16:02:12 christos Exp $ */
2e3899b6dSchristos
354a6ec8aSchristos /*
454a6ec8aSchristos * Copyright (c) 1993, 1994, 1995, 1996, 1997
554a6ec8aSchristos * The Regents of the University of California. All rights reserved.
654a6ec8aSchristos *
754a6ec8aSchristos * Redistribution and use in source and binary forms, with or without
854a6ec8aSchristos * modification, are permitted provided that: (1) source code distributions
954a6ec8aSchristos * retain the above copyright notice and this paragraph in its entirety, (2)
1054a6ec8aSchristos * distributions including binary code include the above copyright notice and
1154a6ec8aSchristos * this paragraph in its entirety in the documentation or other materials
1254a6ec8aSchristos * provided with the distribution, and (3) all advertising materials mentioning
1354a6ec8aSchristos * features or use of this software display the following acknowledgement:
1454a6ec8aSchristos * ``This product includes software developed by the University of California,
1554a6ec8aSchristos * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1654a6ec8aSchristos * the University nor the names of its contributors may be used to endorse
1754a6ec8aSchristos * or promote products derived from this software without specific prior
1854a6ec8aSchristos * written permission.
1954a6ec8aSchristos * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
2054a6ec8aSchristos * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
2154a6ec8aSchristos * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2254a6ec8aSchristos *
2354a6ec8aSchristos * savefile.c - supports offline use of tcpdump
2454a6ec8aSchristos * Extraction/creation by Jeffrey Mogul, DECWRL
2554a6ec8aSchristos * Modified by Steve McCanne, LBL.
2654a6ec8aSchristos *
2754a6ec8aSchristos * Used to save the received packet headers, after filtering, to
2854a6ec8aSchristos * a file, and then read them later.
2954a6ec8aSchristos * The first record in the file contains saved values for the machine
3054a6ec8aSchristos * dependent values so we can print the dump file on any architecture.
3154a6ec8aSchristos */
3254a6ec8aSchristos
33e3899b6dSchristos #include <sys/cdefs.h>
34*d5dfe396Schristos __RCSID("$NetBSD: savefile.c,v 1.6 2019/10/01 16:02:12 christos Exp $");
35e3899b6dSchristos
3654a6ec8aSchristos #ifdef HAVE_CONFIG_H
37e1375535Schristos #include <config.h>
3854a6ec8aSchristos #endif
3954a6ec8aSchristos
40e1375535Schristos #include <pcap-types.h>
4103e25b48Schristos #ifdef _WIN32
42e1375535Schristos #include <io.h>
43e1375535Schristos #include <fcntl.h>
4403e25b48Schristos #endif /* _WIN32 */
4554a6ec8aSchristos
4654a6ec8aSchristos #include <errno.h>
4754a6ec8aSchristos #include <memory.h>
4854a6ec8aSchristos #include <stdio.h>
4954a6ec8aSchristos #include <stdlib.h>
5054a6ec8aSchristos #include <string.h>
51*d5dfe396Schristos #include <limits.h> /* for INT_MAX */
5254a6ec8aSchristos
5354a6ec8aSchristos #include "pcap-int.h"
5454a6ec8aSchristos
5554a6ec8aSchristos #ifdef HAVE_OS_PROTO_H
5654a6ec8aSchristos #include "os-proto.h"
5754a6ec8aSchristos #endif
5854a6ec8aSchristos
5954a6ec8aSchristos #include "sf-pcap.h"
60e1375535Schristos #include "sf-pcapng.h"
61*d5dfe396Schristos #include "pcap-common.h"
6254a6ec8aSchristos
6303e25b48Schristos #ifdef _WIN32
6403e25b48Schristos /*
6503e25b48Schristos * These aren't exported on Windows, because they would only work if both
66*d5dfe396Schristos * WinPcap/Npcap and the code using it were to use the Universal CRT; otherwise,
67*d5dfe396Schristos * a FILE structure in WinPcap/Npcap and a FILE structure in the code using it
6803e25b48Schristos * could be different if they're using different versions of the C runtime.
6903e25b48Schristos *
7003e25b48Schristos * Instead, pcap/pcap.h defines them as macros that wrap the hopen versions,
7103e25b48Schristos * with the wrappers calling _fileno() and _get_osfhandle() themselves,
7203e25b48Schristos * so that they convert the appropriate CRT version's FILE structure to
7303e25b48Schristos * a HANDLE (which is OS-defined, not CRT-defined, and is part of the Win32
7403e25b48Schristos * and Win64 ABIs).
7503e25b48Schristos */
7603e25b48Schristos static pcap_t *pcap_fopen_offline_with_tstamp_precision(FILE *, u_int, char *);
7703e25b48Schristos static pcap_t *pcap_fopen_offline(FILE *, char *);
7803e25b48Schristos #endif
7903e25b48Schristos
8054a6ec8aSchristos /*
8154a6ec8aSchristos * Setting O_BINARY on DOS/Windows is a bit tricky
8254a6ec8aSchristos */
8303e25b48Schristos #if defined(_WIN32)
8454a6ec8aSchristos #define SET_BINMODE(f) _setmode(_fileno(f), _O_BINARY)
8554a6ec8aSchristos #elif defined(MSDOS)
8654a6ec8aSchristos #if defined(__HIGHC__)
8754a6ec8aSchristos #define SET_BINMODE(f) setmode(f, O_BINARY)
8854a6ec8aSchristos #else
8954a6ec8aSchristos #define SET_BINMODE(f) setmode(fileno(f), O_BINARY)
9054a6ec8aSchristos #endif
9154a6ec8aSchristos #endif
9254a6ec8aSchristos
9354a6ec8aSchristos static int
sf_getnonblock(pcap_t * p _U_)94e1375535Schristos sf_getnonblock(pcap_t *p _U_)
9554a6ec8aSchristos {
9654a6ec8aSchristos /*
9754a6ec8aSchristos * This is a savefile, not a live capture file, so never say
9854a6ec8aSchristos * it's in non-blocking mode.
9954a6ec8aSchristos */
10054a6ec8aSchristos return (0);
10154a6ec8aSchristos }
10254a6ec8aSchristos
10354a6ec8aSchristos static int
sf_setnonblock(pcap_t * p,int nonblock _U_)104e1375535Schristos sf_setnonblock(pcap_t *p, int nonblock _U_)
10554a6ec8aSchristos {
10654a6ec8aSchristos /*
107774c01f3Schristos * This is a savefile, not a live capture file, so reject
108774c01f3Schristos * requests to put it in non-blocking mode. (If it's a
109774c01f3Schristos * pipe, it could be put in non-blocking mode, but that
110774c01f3Schristos * would significantly complicate the code to read packets,
111774c01f3Schristos * as it would have to handle reading partial packets and
112774c01f3Schristos * keeping the state of the read.)
11354a6ec8aSchristos */
11403e25b48Schristos pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
115774c01f3Schristos "Savefiles cannot be put into non-blocking mode");
116774c01f3Schristos return (-1);
11754a6ec8aSchristos }
11854a6ec8aSchristos
11954a6ec8aSchristos static int
sf_stats(pcap_t * p,struct pcap_stat * ps _U_)120e1375535Schristos sf_stats(pcap_t *p, struct pcap_stat *ps _U_)
12154a6ec8aSchristos {
12203e25b48Schristos pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
12354a6ec8aSchristos "Statistics aren't available from savefiles");
12454a6ec8aSchristos return (-1);
12554a6ec8aSchristos }
12654a6ec8aSchristos
12703e25b48Schristos #ifdef _WIN32
12803e25b48Schristos static struct pcap_stat *
sf_stats_ex(pcap_t * p,int * size)12903e25b48Schristos sf_stats_ex(pcap_t *p, int *size)
13003e25b48Schristos {
13103e25b48Schristos pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
13203e25b48Schristos "Statistics aren't available from savefiles");
13303e25b48Schristos return (NULL);
13403e25b48Schristos }
13503e25b48Schristos
13654a6ec8aSchristos static int
sf_setbuff(pcap_t * p,int dim)13754a6ec8aSchristos sf_setbuff(pcap_t *p, int dim)
13854a6ec8aSchristos {
13903e25b48Schristos pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
14054a6ec8aSchristos "The kernel buffer size cannot be set while reading from a file");
14154a6ec8aSchristos return (-1);
14254a6ec8aSchristos }
14354a6ec8aSchristos
14454a6ec8aSchristos static int
sf_setmode(pcap_t * p,int mode)14554a6ec8aSchristos sf_setmode(pcap_t *p, int mode)
14654a6ec8aSchristos {
14703e25b48Schristos pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
14854a6ec8aSchristos "impossible to set mode while reading from a file");
14954a6ec8aSchristos return (-1);
15054a6ec8aSchristos }
15154a6ec8aSchristos
15254a6ec8aSchristos static int
sf_setmintocopy(pcap_t * p,int size)15354a6ec8aSchristos sf_setmintocopy(pcap_t *p, int size)
15454a6ec8aSchristos {
15503e25b48Schristos pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
15654a6ec8aSchristos "The mintocopy parameter cannot be set while reading from a file");
15754a6ec8aSchristos return (-1);
15854a6ec8aSchristos }
15903e25b48Schristos
16003e25b48Schristos static HANDLE
sf_getevent(pcap_t * pcap)16103e25b48Schristos sf_getevent(pcap_t *pcap)
16203e25b48Schristos {
16303e25b48Schristos (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
16403e25b48Schristos "The read event cannot be retrieved while reading from a file");
16503e25b48Schristos return (INVALID_HANDLE_VALUE);
16603e25b48Schristos }
16703e25b48Schristos
16803e25b48Schristos static int
sf_oid_get_request(pcap_t * p,bpf_u_int32 oid _U_,void * data _U_,size_t * lenp _U_)16903e25b48Schristos sf_oid_get_request(pcap_t *p, bpf_u_int32 oid _U_, void *data _U_,
17003e25b48Schristos size_t *lenp _U_)
17103e25b48Schristos {
17203e25b48Schristos pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
17303e25b48Schristos "An OID get request cannot be performed on a file");
17403e25b48Schristos return (PCAP_ERROR);
17503e25b48Schristos }
17603e25b48Schristos
17703e25b48Schristos static int
sf_oid_set_request(pcap_t * p,bpf_u_int32 oid _U_,const void * data _U_,size_t * lenp _U_)17803e25b48Schristos sf_oid_set_request(pcap_t *p, bpf_u_int32 oid _U_, const void *data _U_,
17903e25b48Schristos size_t *lenp _U_)
18003e25b48Schristos {
18103e25b48Schristos pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
18203e25b48Schristos "An OID set request cannot be performed on a file");
18303e25b48Schristos return (PCAP_ERROR);
18403e25b48Schristos }
18503e25b48Schristos
18603e25b48Schristos static u_int
sf_sendqueue_transmit(pcap_t * p,pcap_send_queue * queue,int sync)18703e25b48Schristos sf_sendqueue_transmit(pcap_t *p, pcap_send_queue *queue, int sync)
18803e25b48Schristos {
189*d5dfe396Schristos pcap_strlcpy(p->errbuf, "Sending packets isn't supported on savefiles",
19003e25b48Schristos PCAP_ERRBUF_SIZE);
19103e25b48Schristos return (0);
19203e25b48Schristos }
19303e25b48Schristos
19403e25b48Schristos static int
sf_setuserbuffer(pcap_t * p,int size)19503e25b48Schristos sf_setuserbuffer(pcap_t *p, int size)
19603e25b48Schristos {
19703e25b48Schristos pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
19803e25b48Schristos "The user buffer cannot be set when reading from a file");
19903e25b48Schristos return (-1);
20003e25b48Schristos }
20103e25b48Schristos
20203e25b48Schristos static int
sf_live_dump(pcap_t * p,char * filename,int maxsize,int maxpacks)20303e25b48Schristos sf_live_dump(pcap_t *p, char *filename, int maxsize, int maxpacks)
20403e25b48Schristos {
20503e25b48Schristos pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
20603e25b48Schristos "Live packet dumping cannot be performed when reading from a file");
20703e25b48Schristos return (-1);
20803e25b48Schristos }
20903e25b48Schristos
21003e25b48Schristos static int
sf_live_dump_ended(pcap_t * p,int sync)21103e25b48Schristos sf_live_dump_ended(pcap_t *p, int sync)
21203e25b48Schristos {
21303e25b48Schristos pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
21403e25b48Schristos "Live packet dumping cannot be performed on a pcap_open_dead pcap_t");
21503e25b48Schristos return (-1);
21603e25b48Schristos }
21703e25b48Schristos
21803e25b48Schristos static PAirpcapHandle
sf_get_airpcap_handle(pcap_t * pcap)21903e25b48Schristos sf_get_airpcap_handle(pcap_t *pcap)
22003e25b48Schristos {
22103e25b48Schristos return (NULL);
22203e25b48Schristos }
22354a6ec8aSchristos #endif
22454a6ec8aSchristos
22554a6ec8aSchristos static int
sf_inject(pcap_t * p,const void * buf _U_,size_t size _U_)22654a6ec8aSchristos sf_inject(pcap_t *p, const void *buf _U_, size_t size _U_)
22754a6ec8aSchristos {
228*d5dfe396Schristos pcap_strlcpy(p->errbuf, "Sending packets isn't supported on savefiles",
22954a6ec8aSchristos PCAP_ERRBUF_SIZE);
23054a6ec8aSchristos return (-1);
23154a6ec8aSchristos }
23254a6ec8aSchristos
23354a6ec8aSchristos /*
23454a6ec8aSchristos * Set direction flag: Which packets do we accept on a forwarding
23554a6ec8aSchristos * single device? IN, OUT or both?
23654a6ec8aSchristos */
23754a6ec8aSchristos static int
sf_setdirection(pcap_t * p,pcap_direction_t d _U_)238e1375535Schristos sf_setdirection(pcap_t *p, pcap_direction_t d _U_)
23954a6ec8aSchristos {
24003e25b48Schristos pcap_snprintf(p->errbuf, sizeof(p->errbuf),
24154a6ec8aSchristos "Setting direction is not supported on savefiles");
24254a6ec8aSchristos return (-1);
24354a6ec8aSchristos }
24454a6ec8aSchristos
245c15d5effSchristos void
sf_cleanup(pcap_t * p)24654a6ec8aSchristos sf_cleanup(pcap_t *p)
24754a6ec8aSchristos {
248c15d5effSchristos if (p->rfile != stdin)
249c15d5effSchristos (void)fclose(p->rfile);
25054a6ec8aSchristos if (p->buffer != NULL)
25154a6ec8aSchristos free(p->buffer);
252774c01f3Schristos pcap_freecode(&p->fcode);
25354a6ec8aSchristos }
25454a6ec8aSchristos
25554a6ec8aSchristos pcap_t *
pcap_open_offline_with_tstamp_precision(const char * fname,u_int precision,char * errbuf)256c15d5effSchristos pcap_open_offline_with_tstamp_precision(const char *fname, u_int precision,
257c15d5effSchristos char *errbuf)
25854a6ec8aSchristos {
25954a6ec8aSchristos FILE *fp;
26054a6ec8aSchristos pcap_t *p;
26154a6ec8aSchristos
26203e25b48Schristos if (fname == NULL) {
26303e25b48Schristos pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
26403e25b48Schristos "A null pointer was supplied as the file name");
26503e25b48Schristos return (NULL);
26603e25b48Schristos }
26754a6ec8aSchristos if (fname[0] == '-' && fname[1] == '\0')
26854a6ec8aSchristos {
26954a6ec8aSchristos fp = stdin;
27003e25b48Schristos #if defined(_WIN32) || defined(MSDOS)
27154a6ec8aSchristos /*
27254a6ec8aSchristos * We're reading from the standard input, so put it in binary
27354a6ec8aSchristos * mode, as savefiles are binary files.
27454a6ec8aSchristos */
27554a6ec8aSchristos SET_BINMODE(fp);
27654a6ec8aSchristos #endif
27754a6ec8aSchristos }
27854a6ec8aSchristos else {
279e1375535Schristos /*
280e1375535Schristos * "b" is supported as of C90, so *all* UN*Xes should
281e1375535Schristos * support it, even though it does nothing. It's
282e1375535Schristos * required on Windows, as the file is a binary file
283e1375535Schristos * and must be read in binary mode.
284e1375535Schristos */
28554a6ec8aSchristos fp = fopen(fname, "rb");
28654a6ec8aSchristos if (fp == NULL) {
287e1375535Schristos pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
288e1375535Schristos errno, "%s", fname);
28954a6ec8aSchristos return (NULL);
29054a6ec8aSchristos }
29154a6ec8aSchristos }
292c15d5effSchristos p = pcap_fopen_offline_with_tstamp_precision(fp, precision, errbuf);
29354a6ec8aSchristos if (p == NULL) {
29454a6ec8aSchristos if (fp != stdin)
29554a6ec8aSchristos fclose(fp);
29654a6ec8aSchristos }
29754a6ec8aSchristos return (p);
29854a6ec8aSchristos }
29954a6ec8aSchristos
300c15d5effSchristos pcap_t *
pcap_open_offline(const char * fname,char * errbuf)301c15d5effSchristos pcap_open_offline(const char *fname, char *errbuf)
302c15d5effSchristos {
303c15d5effSchristos return (pcap_open_offline_with_tstamp_precision(fname,
304c15d5effSchristos PCAP_TSTAMP_PRECISION_MICRO, errbuf));
305c15d5effSchristos }
306c15d5effSchristos
30703e25b48Schristos #ifdef _WIN32
pcap_hopen_offline_with_tstamp_precision(intptr_t osfd,u_int precision,char * errbuf)308c15d5effSchristos pcap_t* pcap_hopen_offline_with_tstamp_precision(intptr_t osfd, u_int precision,
309c15d5effSchristos char *errbuf)
31054a6ec8aSchristos {
31154a6ec8aSchristos int fd;
31254a6ec8aSchristos FILE *file;
31354a6ec8aSchristos
31454a6ec8aSchristos fd = _open_osfhandle(osfd, _O_RDONLY);
31554a6ec8aSchristos if ( fd < 0 )
31654a6ec8aSchristos {
317e1375535Schristos pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
318e1375535Schristos errno, "_open_osfhandle");
31954a6ec8aSchristos return NULL;
32054a6ec8aSchristos }
32154a6ec8aSchristos
32254a6ec8aSchristos file = _fdopen(fd, "rb");
32354a6ec8aSchristos if ( file == NULL )
32454a6ec8aSchristos {
325e1375535Schristos pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
326e1375535Schristos errno, "_fdopen");
327*d5dfe396Schristos _close(fd);
32854a6ec8aSchristos return NULL;
32954a6ec8aSchristos }
33054a6ec8aSchristos
331c15d5effSchristos return pcap_fopen_offline_with_tstamp_precision(file, precision,
332c15d5effSchristos errbuf);
333c15d5effSchristos }
334c15d5effSchristos
pcap_hopen_offline(intptr_t osfd,char * errbuf)335c15d5effSchristos pcap_t* pcap_hopen_offline(intptr_t osfd, char *errbuf)
336c15d5effSchristos {
337c15d5effSchristos return pcap_hopen_offline_with_tstamp_precision(osfd,
338c15d5effSchristos PCAP_TSTAMP_PRECISION_MICRO, errbuf);
33954a6ec8aSchristos }
34054a6ec8aSchristos #endif
34154a6ec8aSchristos
342*d5dfe396Schristos /*
343*d5dfe396Schristos * Given a link-layer header type and snapshot length, return a
344*d5dfe396Schristos * snapshot length to use when reading the file; it's guaranteed
345*d5dfe396Schristos * to be > 0 and <= INT_MAX.
346*d5dfe396Schristos *
347*d5dfe396Schristos * XXX - the only reason why we limit it to <= INT_MAX is so that
348*d5dfe396Schristos * it fits in p->snapshot, and the only reason that p->snapshot is
349*d5dfe396Schristos * signed is that pcap_snapshot() returns an int, not an unsigned int.
350*d5dfe396Schristos */
351*d5dfe396Schristos bpf_u_int32
pcap_adjust_snapshot(bpf_u_int32 linktype,bpf_u_int32 snaplen)352*d5dfe396Schristos pcap_adjust_snapshot(bpf_u_int32 linktype, bpf_u_int32 snaplen)
353*d5dfe396Schristos {
354*d5dfe396Schristos if (snaplen == 0 || snaplen > INT_MAX) {
355*d5dfe396Schristos /*
356*d5dfe396Schristos * Bogus snapshot length; use the maximum for this
357*d5dfe396Schristos * link-layer type as a fallback.
358*d5dfe396Schristos *
359*d5dfe396Schristos * XXX - we don't clamp snapshot lengths that are
360*d5dfe396Schristos * <= INT_MAX but > max_snaplen_for_dlt(linktype),
361*d5dfe396Schristos * so a capture file could cause us to allocate
362*d5dfe396Schristos * a Really Big Buffer.
363*d5dfe396Schristos */
364*d5dfe396Schristos snaplen = max_snaplen_for_dlt(linktype);
365*d5dfe396Schristos }
366*d5dfe396Schristos return snaplen;
367*d5dfe396Schristos }
368*d5dfe396Schristos
369*d5dfe396Schristos static pcap_t *(*check_headers[])(const uint8_t *, FILE *, u_int, char *, int *) = {
37054a6ec8aSchristos pcap_check_header,
37154a6ec8aSchristos pcap_ng_check_header
37254a6ec8aSchristos };
37354a6ec8aSchristos
37454a6ec8aSchristos #define N_FILE_TYPES (sizeof check_headers / sizeof check_headers[0])
37554a6ec8aSchristos
37603e25b48Schristos #ifdef _WIN32
37754a6ec8aSchristos static
37854a6ec8aSchristos #endif
37954a6ec8aSchristos pcap_t *
pcap_fopen_offline_with_tstamp_precision(FILE * fp,u_int precision,char * errbuf)380c15d5effSchristos pcap_fopen_offline_with_tstamp_precision(FILE *fp, u_int precision,
381c15d5effSchristos char *errbuf)
38254a6ec8aSchristos {
38354a6ec8aSchristos register pcap_t *p;
384*d5dfe396Schristos uint8_t magic[4];
38554a6ec8aSchristos size_t amt_read;
38654a6ec8aSchristos u_int i;
387c15d5effSchristos int err;
38854a6ec8aSchristos
38954a6ec8aSchristos /*
39054a6ec8aSchristos * Read the first 4 bytes of the file; the network analyzer dump
391e1375535Schristos * file formats we support (pcap and pcapng), and several other
39254a6ec8aSchristos * formats we might support in the future (such as snoop, DOS and
39354a6ec8aSchristos * Windows Sniffer, and Microsoft Network Monitor) all have magic
39454a6ec8aSchristos * numbers that are unique in their first 4 bytes.
39554a6ec8aSchristos */
396*d5dfe396Schristos amt_read = fread(&magic, 1, sizeof(magic), fp);
39754a6ec8aSchristos if (amt_read != sizeof(magic)) {
39854a6ec8aSchristos if (ferror(fp)) {
399e1375535Schristos pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
400e1375535Schristos errno, "error reading dump file");
40154a6ec8aSchristos } else {
40203e25b48Schristos pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
403*d5dfe396Schristos "truncated dump file; tried to read %" PRIsize " file header bytes, only got %" PRIsize,
404*d5dfe396Schristos sizeof(magic), amt_read);
40554a6ec8aSchristos }
406c15d5effSchristos return (NULL);
40754a6ec8aSchristos }
40854a6ec8aSchristos
40954a6ec8aSchristos /*
41054a6ec8aSchristos * Try all file types.
41154a6ec8aSchristos */
41254a6ec8aSchristos for (i = 0; i < N_FILE_TYPES; i++) {
413c15d5effSchristos p = (*check_headers[i])(magic, fp, precision, errbuf, &err);
414c15d5effSchristos if (p != NULL) {
415c15d5effSchristos /* Yup, that's it. */
416c15d5effSchristos goto found;
417c15d5effSchristos }
418c15d5effSchristos if (err) {
41954a6ec8aSchristos /*
42054a6ec8aSchristos * Error trying to read the header.
42154a6ec8aSchristos */
422c15d5effSchristos return (NULL);
42354a6ec8aSchristos }
42454a6ec8aSchristos }
42554a6ec8aSchristos
42654a6ec8aSchristos /*
42754a6ec8aSchristos * Well, who knows what this mess is....
42854a6ec8aSchristos */
42903e25b48Schristos pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "unknown file format");
430c15d5effSchristos return (NULL);
43154a6ec8aSchristos
43254a6ec8aSchristos found:
433c15d5effSchristos p->rfile = fp;
43454a6ec8aSchristos
43554a6ec8aSchristos /* Padding only needed for live capture fcode */
43654a6ec8aSchristos p->fddipad = 0;
43754a6ec8aSchristos
43803e25b48Schristos #if !defined(_WIN32) && !defined(MSDOS)
43954a6ec8aSchristos /*
44054a6ec8aSchristos * You can do "select()" and "poll()" on plain files on most
44154a6ec8aSchristos * platforms, and should be able to do so on pipes.
44254a6ec8aSchristos *
44354a6ec8aSchristos * You can't do "select()" on anything other than sockets in
44454a6ec8aSchristos * Windows, so, on Win32 systems, we don't have "selectable_fd".
44554a6ec8aSchristos */
44654a6ec8aSchristos p->selectable_fd = fileno(fp);
44754a6ec8aSchristos #endif
44854a6ec8aSchristos
44954a6ec8aSchristos p->read_op = pcap_offline_read;
45054a6ec8aSchristos p->inject_op = sf_inject;
45154a6ec8aSchristos p->setfilter_op = install_bpf_program;
45254a6ec8aSchristos p->setdirection_op = sf_setdirection;
45354a6ec8aSchristos p->set_datalink_op = NULL; /* we don't support munging link-layer headers */
45454a6ec8aSchristos p->getnonblock_op = sf_getnonblock;
45554a6ec8aSchristos p->setnonblock_op = sf_setnonblock;
45654a6ec8aSchristos p->stats_op = sf_stats;
45703e25b48Schristos #ifdef _WIN32
45803e25b48Schristos p->stats_ex_op = sf_stats_ex;
45954a6ec8aSchristos p->setbuff_op = sf_setbuff;
46054a6ec8aSchristos p->setmode_op = sf_setmode;
46154a6ec8aSchristos p->setmintocopy_op = sf_setmintocopy;
46203e25b48Schristos p->getevent_op = sf_getevent;
46303e25b48Schristos p->oid_get_request_op = sf_oid_get_request;
46403e25b48Schristos p->oid_set_request_op = sf_oid_set_request;
46503e25b48Schristos p->sendqueue_transmit_op = sf_sendqueue_transmit;
46603e25b48Schristos p->setuserbuffer_op = sf_setuserbuffer;
46703e25b48Schristos p->live_dump_op = sf_live_dump;
46803e25b48Schristos p->live_dump_ended_op = sf_live_dump_ended;
46903e25b48Schristos p->get_airpcap_handle_op = sf_get_airpcap_handle;
47054a6ec8aSchristos #endif
471c15d5effSchristos
472c15d5effSchristos /*
473c15d5effSchristos * For offline captures, the standard one-shot callback can
474c15d5effSchristos * be used for pcap_next()/pcap_next_ex().
475c15d5effSchristos */
476c15d5effSchristos p->oneshot_callback = pcap_oneshot;
477c15d5effSchristos
4786c7e2519Schristos /*
4796c7e2519Schristos * Savefiles never require special BPF code generation.
4806c7e2519Schristos */
4816c7e2519Schristos p->bpf_codegen_flags = 0;
4826c7e2519Schristos
48354a6ec8aSchristos p->activated = 1;
48454a6ec8aSchristos
48554a6ec8aSchristos return (p);
486c15d5effSchristos }
487c15d5effSchristos
48803e25b48Schristos #ifdef _WIN32
489c15d5effSchristos static
490c15d5effSchristos #endif
491c15d5effSchristos pcap_t *
pcap_fopen_offline(FILE * fp,char * errbuf)492c15d5effSchristos pcap_fopen_offline(FILE *fp, char *errbuf)
493c15d5effSchristos {
494c15d5effSchristos return (pcap_fopen_offline_with_tstamp_precision(fp,
495c15d5effSchristos PCAP_TSTAMP_PRECISION_MICRO, errbuf));
49654a6ec8aSchristos }
49754a6ec8aSchristos
49854a6ec8aSchristos /*
49954a6ec8aSchristos * Read packets from a capture file, and call the callback for each
50054a6ec8aSchristos * packet.
50154a6ec8aSchristos * If cnt > 0, return after 'cnt' packets, otherwise continue until eof.
50254a6ec8aSchristos */
50354a6ec8aSchristos int
pcap_offline_read(pcap_t * p,int cnt,pcap_handler callback,u_char * user)50454a6ec8aSchristos pcap_offline_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
50554a6ec8aSchristos {
50654a6ec8aSchristos struct bpf_insn *fcode;
50754a6ec8aSchristos int status = 0;
50854a6ec8aSchristos int n = 0;
50954a6ec8aSchristos u_char *data;
51054a6ec8aSchristos
51154a6ec8aSchristos while (status == 0) {
51254a6ec8aSchristos struct pcap_pkthdr h;
51354a6ec8aSchristos
51454a6ec8aSchristos /*
51554a6ec8aSchristos * Has "pcap_breakloop()" been called?
51654a6ec8aSchristos * If so, return immediately - if we haven't read any
51754a6ec8aSchristos * packets, clear the flag and return -2 to indicate
51854a6ec8aSchristos * that we were told to break out of the loop, otherwise
51954a6ec8aSchristos * leave the flag set, so that the *next* call will break
52054a6ec8aSchristos * out of the loop without having read any packets, and
52154a6ec8aSchristos * return the number of packets we've processed so far.
52254a6ec8aSchristos */
52354a6ec8aSchristos if (p->break_loop) {
52454a6ec8aSchristos if (n == 0) {
52554a6ec8aSchristos p->break_loop = 0;
52654a6ec8aSchristos return (-2);
52754a6ec8aSchristos } else
52854a6ec8aSchristos return (n);
52954a6ec8aSchristos }
53054a6ec8aSchristos
531c15d5effSchristos status = p->next_packet_op(p, &h, &data);
53254a6ec8aSchristos if (status) {
53354a6ec8aSchristos if (status == 1)
53454a6ec8aSchristos return (0);
53554a6ec8aSchristos return (status);
53654a6ec8aSchristos }
53754a6ec8aSchristos
53854a6ec8aSchristos if ((fcode = p->fcode.bf_insns) == NULL ||
539774c01f3Schristos bpf_filter(fcode, data, h.len, h.caplen)) {
54054a6ec8aSchristos (*callback)(user, &h, data);
54154a6ec8aSchristos if (++n >= cnt && cnt > 0)
54254a6ec8aSchristos break;
54354a6ec8aSchristos }
54454a6ec8aSchristos }
54554a6ec8aSchristos /*XXX this breaks semantics tcpslice expects */
54654a6ec8aSchristos return (n);
54754a6ec8aSchristos }
548