savefile.c revision 75107
117683Spst/*
239291Sfenner * Copyright (c) 1993, 1994, 1995, 1996, 1997
317683Spst *	The Regents of the University of California.  All rights reserved.
417683Spst *
517683Spst * Redistribution and use in source and binary forms, with or without
617683Spst * modification, are permitted provided that: (1) source code distributions
717683Spst * retain the above copyright notice and this paragraph in its entirety, (2)
817683Spst * distributions including binary code include the above copyright notice and
917683Spst * this paragraph in its entirety in the documentation or other materials
1017683Spst * provided with the distribution, and (3) all advertising materials mentioning
1117683Spst * features or use of this software display the following acknowledgement:
1217683Spst * ``This product includes software developed by the University of California,
1317683Spst * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1417683Spst * the University nor the names of its contributors may be used to endorse
1517683Spst * or promote products derived from this software without specific prior
1617683Spst * written permission.
1717683Spst * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1817683Spst * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1917683Spst * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2026175Sfenner *
2117683Spst * savefile.c - supports offline use of tcpdump
2217683Spst *	Extraction/creation by Jeffrey Mogul, DECWRL
2317683Spst *	Modified by Steve McCanne, LBL.
2417683Spst *
2517683Spst * Used to save the received packet headers, after filtering, to
2617683Spst * a file, and then read them later.
2717683Spst * The first record in the file contains saved values for the machine
2817683Spst * dependent values so we can print the dump file on any architecture.
2917683Spst */
3017683Spst
3126175Sfenner#ifndef lint
3226175Sfennerstatic const char rcsid[] =
3375107Sfenner    "@(#) $Header: /tcpdump/master/libpcap/savefile.c,v 1.49 2000/12/21 10:29:23 guy Exp $ (LBL)";
3426175Sfenner#endif
3526175Sfenner
3675107Sfenner#ifdef HAVE_CONFIG_H
3775107Sfenner#include "config.h"
3875107Sfenner#endif
3975107Sfenner
4017683Spst#include <sys/types.h>
4117683Spst#include <sys/time.h>
4217683Spst
4317683Spst#include <errno.h>
4417683Spst#include <memory.h>
4517683Spst#include <stdio.h>
4617683Spst#include <stdlib.h>
4775107Sfenner#include <string.h>
4817683Spst#include <unistd.h>
4917683Spst
5017683Spst#include "pcap-int.h"
5117683Spst
5217683Spst#ifdef HAVE_OS_PROTO_H
5317683Spst#include "os-proto.h"
5417683Spst#endif
5517683Spst
5617683Spst#define TCPDUMP_MAGIC 0xa1b2c3d4
5775107Sfenner#define PATCHED_TCPDUMP_MAGIC 0xa1b2cd34
5817683Spst
5917683Spst/*
6017683Spst * We use the "receiver-makes-right" approach to byte order,
6117683Spst * because time is at a premium when we are writing the file.
6217683Spst * In other words, the pcap_file_header and pcap_pkthdr,
6317683Spst * records are written in host byte order.
6417683Spst * Note that the packets are always written in network byte order.
6517683Spst *
6617683Spst * ntoh[ls] aren't sufficient because we might need to swap on a big-endian
6717683Spst * machine (if the file was written in little-end order).
6817683Spst */
6917683Spst#define	SWAPLONG(y) \
7017683Spst((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
7117683Spst#define	SWAPSHORT(y) \
7226175Sfenner	( (((y)&0xff)<<8) | ((u_short)((y)&0xff00)>>8) )
7317683Spst
7417683Spst#define SFERR_TRUNC		1
7517683Spst#define SFERR_BADVERSION	2
7617683Spst#define SFERR_BADF		3
7717683Spst#define SFERR_EOF		4 /* not really an error, just a status */
7817683Spst
7975107Sfenner/*
8075107Sfenner * We don't write DLT_* values to the capture file header, because
8175107Sfenner * they're not the same on all platforms.
8275107Sfenner *
8375107Sfenner * Unfortunately, the various flavors of BSD have not always used the same
8475107Sfenner * numerical values for the same data types, and various patches to
8575107Sfenner * libpcap for non-BSD OSes have added their own DLT_* codes for link
8675107Sfenner * layer encapsulation types seen on those OSes, and those codes have had,
8775107Sfenner * in some cases, values that were also used, on other platforms, for other
8875107Sfenner * link layer encapsulation types.
8975107Sfenner *
9075107Sfenner * This means that capture files of a type whose numerical DLT_* code
9175107Sfenner * means different things on different BSDs, or with different versions
9275107Sfenner * of libpcap, can't always be read on systems other than those like
9375107Sfenner * the one running on the machine on which the capture was made.
9475107Sfenner *
9575107Sfenner * Instead, we define here a set of LINKTYPE_* codes, and map DLT_* codes
9675107Sfenner * to LINKTYPE_* codes when writing a savefile header, and map LINKTYPE_*
9775107Sfenner * codes to DLT_* codes when reading a savefile header.
9875107Sfenner *
9975107Sfenner * For those DLT_* codes that have, as far as we know, the same values on
10075107Sfenner * all platforms (DLT_NULL through DLT_FDDI), we define LINKTYPE_xxx as
10175107Sfenner * DLT_xxx; that way, captures of those types can still be read by
10275107Sfenner * versions of libpcap that map LINKTYPE_* values to DLT_* values, and
10375107Sfenner * captures of those types written by versions of libpcap that map DLT_
10475107Sfenner * values to LINKTYPE_ values can still be read by older versions
10575107Sfenner * of libpcap.
10675107Sfenner *
10775107Sfenner * The other LINKTYPE_* codes are given values starting at 100, in the
10875107Sfenner * hopes that no DLT_* code will be given one of those values.
10975107Sfenner *
11075107Sfenner * In order to ensure that a given LINKTYPE_* code's value will refer to
11175107Sfenner * the same encapsulation type on all platforms, you should not allocate
11275107Sfenner * a new LINKTYPE_* value without consulting "tcpdump-workers@tcpdump.org".
11375107Sfenner * The tcpdump developers will allocate a value for you, and will not
11475107Sfenner * subsequently allocate it to anybody else; that value will be added to
11575107Sfenner * the "pcap.h" in the tcpdump.org CVS repository, so that a future
11675107Sfenner * libpcap release will include it.
11775107Sfenner *
11875107Sfenner * You should, if possible, also contribute patches to libpcap and tcpdump
11975107Sfenner * to handle the new encapsulation type, so that they can also be checked
12075107Sfenner * into the tcpdump.org CVS repository and so that they will appear in
12175107Sfenner * future libpcap and tcpdump releases.
12275107Sfenner */
12375107Sfenner#define LINKTYPE_NULL		DLT_NULL
12475107Sfenner#define LINKTYPE_ETHERNET	DLT_EN10MB	/* also for 100Mb and up */
12575107Sfenner#define LINKTYPE_EXP_ETHERNET	DLT_EN3MB	/* 3Mb experimental Ethernet */
12675107Sfenner#define LINKTYPE_AX25		DLT_AX25
12775107Sfenner#define LINKTYPE_PRONET		DLT_PRONET
12875107Sfenner#define LINKTYPE_CHAOS		DLT_CHAOS
12975107Sfenner#define LINKTYPE_TOKEN_RING	DLT_IEEE802	/* DLT_IEEE802 is used for Token Ring */
13075107Sfenner#define LINKTYPE_ARCNET		DLT_ARCNET
13175107Sfenner#define LINKTYPE_SLIP		DLT_SLIP
13275107Sfenner#define LINKTYPE_PPP		DLT_PPP
13375107Sfenner#define LINKTYPE_FDDI		DLT_FDDI
13475107Sfenner
13575107Sfenner/*
13675107Sfenner * LINKTYPE_PPP is for use when there might, or might not, be an RFC 1662
13775107Sfenner * PPP in HDLC-like framing header (with 0xff 0x03 before the PPP protocol
13875107Sfenner * field) at the beginning of the packet.
13975107Sfenner *
14075107Sfenner * This is for use when there is always such a header; the address field
14175107Sfenner * might be 0xff, for regular PPP, or it might be an address field for Cisco
14275107Sfenner * point-to-point with HDLC framing as per section 4.3.1 of RFC 1547 ("Cisco
14375107Sfenner * HDLC").  This is, for example, what you get with NetBSD's DLT_PPP_SERIAL.
14475107Sfenner *
14575107Sfenner * We give it the same value as NetBSD's DLT_PPP_SERIAL, in the hopes that
14675107Sfenner * nobody else will choose a DLT_ value of 50, and so that DLT_PPP_SERIAL
14775107Sfenner * captures will be written out with a link type that NetBSD's tcpdump
14875107Sfenner * can read.
14975107Sfenner */
15075107Sfenner#define LINKTYPE_PPP_HDLC	50		/* PPP in HDLC-like framing */
15175107Sfenner
15275107Sfenner#define LINKTYPE_ATM_RFC1483	100		/* LLC/SNAP-encapsulated ATM */
15375107Sfenner#define LINKTYPE_RAW		101		/* raw IP */
15475107Sfenner#define LINKTYPE_SLIP_BSDOS	102		/* BSD/OS SLIP BPF header */
15575107Sfenner#define LINKTYPE_PPP_BSDOS	103		/* BSD/OS PPP BPF header */
15675107Sfenner#define LINKTYPE_C_HDLC		104		/* Cisco HDLC */
15775107Sfenner#define LINKTYPE_ATM_CLIP	106		/* Linux Classical IP over ATM */
15875107Sfenner
15975107Sfenner/*
16075107Sfenner * Reserved for future use.
16175107Sfenner */
16275107Sfenner#define LINKTYPE_IEEE802_11	105		/* IEEE 802.11 (wireless) */
16375107Sfenner#define LINKTYPE_FR		107		/* BSD/OS Frame Relay */
16475107Sfenner#define LINKTYPE_LOOP		108		/* OpenBSD loopback */
16575107Sfenner#define LINKTYPE_ENC		109		/* OpenBSD IPSEC enc */
16675107Sfenner#define LINKTYPE_LANE8023	110		/* ATM LANE + 802.3 */
16775107Sfenner#define LINKTYPE_HIPPI		111		/* NetBSD HIPPI */
16875107Sfenner#define LINKTYPE_HDLC		112		/* NetBSD HDLC framing */
16975107Sfenner
17075107Sfenner#define LINKTYPE_LINUX_SLL	113		/* Linux cooked socket capture */
17175107Sfenner
17275107Sfennerstatic struct linktype_map {
17375107Sfenner	int	dlt;
17475107Sfenner	int	linktype;
17575107Sfenner} map[] = {
17675107Sfenner	/*
17775107Sfenner	 * These DLT_* codes have LINKTYPE_* codes with values identical
17875107Sfenner	 * to the values of the corresponding DLT_* code.
17975107Sfenner	 */
18075107Sfenner	{ DLT_NULL,		LINKTYPE_NULL },
18175107Sfenner	{ DLT_EN10MB,		LINKTYPE_ETHERNET },
18275107Sfenner	{ DLT_EN3MB,		LINKTYPE_EXP_ETHERNET },
18375107Sfenner	{ DLT_AX25,		LINKTYPE_AX25 },
18475107Sfenner	{ DLT_PRONET,		LINKTYPE_PRONET },
18575107Sfenner	{ DLT_CHAOS,		LINKTYPE_CHAOS },
18675107Sfenner	{ DLT_IEEE802,		LINKTYPE_TOKEN_RING },
18775107Sfenner	{ DLT_ARCNET,		LINKTYPE_ARCNET },
18875107Sfenner	{ DLT_SLIP,		LINKTYPE_SLIP },
18975107Sfenner	{ DLT_PPP,		LINKTYPE_PPP },
19075107Sfenner	{ DLT_FDDI,	 	LINKTYPE_FDDI },
19175107Sfenner
19275107Sfenner	/*
19375107Sfenner	 * These DLT_* codes have different values on different
19475107Sfenner	 * platforms; we map them to LINKTYPE_* codes that
19575107Sfenner	 * have values that should never be equal to any DLT_*
19675107Sfenner	 * code.
19775107Sfenner	 */
19875107Sfenner	{ DLT_ATM_RFC1483, 	LINKTYPE_ATM_RFC1483 },
19975107Sfenner	{ DLT_RAW,		LINKTYPE_RAW },
20075107Sfenner	{ DLT_SLIP_BSDOS,	LINKTYPE_SLIP_BSDOS },
20175107Sfenner	{ DLT_PPP_BSDOS,	LINKTYPE_PPP_BSDOS },
20275107Sfenner
20375107Sfenner	/* BSD/OS Cisco HDLC */
20475107Sfenner	{ DLT_C_HDLC,		LINKTYPE_C_HDLC },
20575107Sfenner
20675107Sfenner	/*
20775107Sfenner	 * These DLT_* codes are not on all platforms, but, so far,
20875107Sfenner	 * there don't appear to be any platforms that define
20975107Sfenner	 * other codes with those values; we map them to
21075107Sfenner	 * different LINKTYPE_* values anyway, just in case.
21175107Sfenner	 */
21275107Sfenner
21375107Sfenner	/* Linux ATM Classical IP */
21475107Sfenner	{ DLT_ATM_CLIP,		LINKTYPE_ATM_CLIP },
21575107Sfenner
21675107Sfenner	/* NetBSD sync/async serial PPP (or Cisco HDLC) */
21775107Sfenner	{ DLT_PPP_SERIAL,	LINKTYPE_PPP_HDLC },
21875107Sfenner
21975107Sfenner	/* IEEE 802.11 wireless */
22075107Sfenner	{ DLT_IEEE802_11,	LINKTYPE_IEEE802_11 },
22175107Sfenner
22275107Sfenner	/* OpenBSD loopback */
22375107Sfenner	{ DLT_LOOP,		LINKTYPE_LOOP },
22475107Sfenner
22575107Sfenner	/* Linux cooked socket capture */
22675107Sfenner	{ DLT_LINUX_SLL,	LINKTYPE_LINUX_SLL },
22775107Sfenner
22875107Sfenner	/*
22975107Sfenner	 * Any platform that defines additional DLT_* codes should:
23075107Sfenner	 *
23175107Sfenner	 *	request a LINKTYPE_* code and value from tcpdump.org,
23275107Sfenner	 *	as per the above;
23375107Sfenner	 *
23475107Sfenner	 *	add, in their version of libpcap, an entry to map
23575107Sfenner	 *	those DLT_* codes to the corresponding LINKTYPE_*
23675107Sfenner	 *	code;
23775107Sfenner	 *
23875107Sfenner	 *	redefine, in their "net/bpf.h", any DLT_* values
23975107Sfenner	 *	that collide with the values used by their additional
24075107Sfenner	 *	DLT_* codes, to remove those collisions (but without
24175107Sfenner	 *	making them collide with any of the LINKTYPE_*
24275107Sfenner	 *	values equal to 50 or above; they should also avoid
24375107Sfenner	 *	defining DLT_* values that collide with those
24475107Sfenner	 *	LINKTYPE_* values, either).
24575107Sfenner	 */
24675107Sfenner	{ -1,			-1 }
24775107Sfenner};
24875107Sfenner
24917683Spststatic int
25075107Sfennerdlt_to_linktype(int dlt)
25175107Sfenner{
25275107Sfenner	int i;
25375107Sfenner
25475107Sfenner	for (i = 0; map[i].dlt != -1; i++) {
25575107Sfenner		if (map[i].dlt == dlt)
25675107Sfenner			return (map[i].linktype);
25775107Sfenner	}
25875107Sfenner
25975107Sfenner	/*
26075107Sfenner	 * If we don't have a mapping for this DLT_ code, return an
26175107Sfenner	 * error; that means that the table above needs to have an
26275107Sfenner	 * entry added.
26375107Sfenner	 */
26475107Sfenner	return (-1);
26575107Sfenner}
26675107Sfenner
26775107Sfennerstatic int
26875107Sfennerlinktype_to_dlt(int linktype)
26975107Sfenner{
27075107Sfenner	int i;
27175107Sfenner
27275107Sfenner	for (i = 0; map[i].linktype != -1; i++) {
27375107Sfenner		if (map[i].linktype == linktype)
27475107Sfenner			return (map[i].dlt);
27575107Sfenner	}
27675107Sfenner
27775107Sfenner	/*
27875107Sfenner	 * If we don't have an entry for this link type, return
27975107Sfenner	 * the link type value; it may be a DLT_ value from an
28075107Sfenner	 * older version of libpcap.
28175107Sfenner	 */
28275107Sfenner	return linktype;
28375107Sfenner}
28475107Sfenner
28575107Sfennerstatic int
28617683Spstsf_write_header(FILE *fp, int linktype, int thiszone, int snaplen)
28717683Spst{
28817683Spst	struct pcap_file_header hdr;
28917683Spst
29017683Spst	hdr.magic = TCPDUMP_MAGIC;
29117683Spst	hdr.version_major = PCAP_VERSION_MAJOR;
29217683Spst	hdr.version_minor = PCAP_VERSION_MINOR;
29317683Spst
29417683Spst	hdr.thiszone = thiszone;
29517683Spst	hdr.snaplen = snaplen;
29617683Spst	hdr.sigfigs = 0;
29717683Spst	hdr.linktype = linktype;
29817683Spst
29917683Spst	if (fwrite((char *)&hdr, sizeof(hdr), 1, fp) != 1)
30017683Spst		return (-1);
30117683Spst
30217683Spst	return (0);
30317683Spst}
30417683Spst
30517683Spststatic void
30617683Spstswap_hdr(struct pcap_file_header *hp)
30717683Spst{
30817683Spst	hp->version_major = SWAPSHORT(hp->version_major);
30917683Spst	hp->version_minor = SWAPSHORT(hp->version_minor);
31017683Spst	hp->thiszone = SWAPLONG(hp->thiszone);
31117683Spst	hp->sigfigs = SWAPLONG(hp->sigfigs);
31217683Spst	hp->snaplen = SWAPLONG(hp->snaplen);
31317683Spst	hp->linktype = SWAPLONG(hp->linktype);
31417683Spst}
31517683Spst
31617683Spstpcap_t *
31739291Sfennerpcap_open_offline(const char *fname, char *errbuf)
31817683Spst{
31917683Spst	register pcap_t *p;
32017683Spst	register FILE *fp;
32117683Spst	struct pcap_file_header hdr;
32275107Sfenner	bpf_u_int32 magic;
32317683Spst	int linklen;
32417683Spst
32517683Spst	p = (pcap_t *)malloc(sizeof(*p));
32617683Spst	if (p == NULL) {
32775107Sfenner		strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
32817683Spst		return (NULL);
32917683Spst	}
33017683Spst
33117683Spst	memset((char *)p, 0, sizeof(*p));
33217683Spst	/*
33317683Spst	 * Set this field so we don't close stdin in pcap_close!
33417683Spst	 */
33517683Spst	p->fd = -1;
33617683Spst
33717683Spst	if (fname[0] == '-' && fname[1] == '\0')
33817683Spst		fp = stdin;
33917683Spst	else {
34017683Spst		fp = fopen(fname, "r");
34117683Spst		if (fp == NULL) {
34275107Sfenner			snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", fname,
34375107Sfenner			    pcap_strerror(errno));
34417683Spst			goto bad;
34517683Spst		}
34617683Spst	}
34717683Spst	if (fread((char *)&hdr, sizeof(hdr), 1, fp) != 1) {
34875107Sfenner		snprintf(errbuf, PCAP_ERRBUF_SIZE, "fread: %s",
34975107Sfenner		    pcap_strerror(errno));
35017683Spst		goto bad;
35117683Spst	}
35275107Sfenner	magic = hdr.magic;
35375107Sfenner	if (magic != TCPDUMP_MAGIC && magic != PATCHED_TCPDUMP_MAGIC) {
35475107Sfenner		magic = SWAPLONG(magic);
35575107Sfenner		if (magic != TCPDUMP_MAGIC && magic != PATCHED_TCPDUMP_MAGIC) {
35675107Sfenner			snprintf(errbuf, PCAP_ERRBUF_SIZE,
35775107Sfenner			    "bad dump file format");
35817683Spst			goto bad;
35917683Spst		}
36017683Spst		p->sf.swapped = 1;
36117683Spst		swap_hdr(&hdr);
36217683Spst	}
36375107Sfenner	if (magic == PATCHED_TCPDUMP_MAGIC) {
36475107Sfenner		/*
36575107Sfenner		 * XXX - the patch that's in some versions of libpcap
36675107Sfenner		 * changes the packet header but not the magic number;
36775107Sfenner		 * we'd have to use some hacks^H^H^H^H^Hheuristics to
36875107Sfenner		 * detect that.
36975107Sfenner		 */
37075107Sfenner		p->sf.hdrsize = sizeof(struct pcap_sf_patched_pkthdr);
37175107Sfenner	} else
37275107Sfenner		p->sf.hdrsize = sizeof(struct pcap_sf_pkthdr);
37317683Spst	if (hdr.version_major < PCAP_VERSION_MAJOR) {
37475107Sfenner		snprintf(errbuf, PCAP_ERRBUF_SIZE, "archaic file format");
37517683Spst		goto bad;
37617683Spst	}
37717683Spst	p->tzoff = hdr.thiszone;
37817683Spst	p->snapshot = hdr.snaplen;
37975107Sfenner	p->linktype = linktype_to_dlt(hdr.linktype);
38017683Spst	p->sf.rfile = fp;
38117683Spst	p->bufsize = hdr.snaplen;
38217683Spst
38317683Spst	/* Align link header as required for proper data alignment */
38417683Spst	/* XXX should handle all types */
38517683Spst	switch (p->linktype) {
38617683Spst
38717683Spst	case DLT_EN10MB:
38817683Spst		linklen = 14;
38917683Spst		break;
39017683Spst
39117683Spst	case DLT_FDDI:
39217683Spst		linklen = 13 + 8;	/* fddi_header + llc */
39317683Spst		break;
39417683Spst
39517683Spst	case DLT_NULL:
39617683Spst	default:
39717683Spst		linklen = 0;
39817683Spst		break;
39917683Spst	}
40017683Spst
40175107Sfenner	if (p->bufsize < 0)
40275107Sfenner		p->bufsize = BPF_MAXBUFSIZE;
40317683Spst	p->sf.base = (u_char *)malloc(p->bufsize + BPF_ALIGNMENT);
40475107Sfenner	if (p->sf.base == NULL) {
40575107Sfenner		strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
40675107Sfenner		goto bad;
40775107Sfenner	}
40817683Spst	p->buffer = p->sf.base + BPF_ALIGNMENT - (linklen % BPF_ALIGNMENT);
40917683Spst	p->sf.version_major = hdr.version_major;
41017683Spst	p->sf.version_minor = hdr.version_minor;
41117683Spst#ifdef PCAP_FDDIPAD
41217683Spst	/* XXX padding only needed for kernel fcode */
41317683Spst	pcap_fddipad = 0;
41417683Spst#endif
41517683Spst
41617683Spst	return (p);
41717683Spst bad:
41817683Spst	free(p);
41917683Spst	return (NULL);
42017683Spst}
42117683Spst
42217683Spst/*
42317683Spst * Read sf_readfile and return the next packet.  Return the header in hdr
42417683Spst * and the contents in buf.  Return 0 on success, SFERR_EOF if there were
42517683Spst * no more packets, and SFERR_TRUNC if a partial packet was encountered.
42617683Spst */
42717683Spststatic int
42817683Spstsf_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char *buf, int buflen)
42917683Spst{
43075107Sfenner	struct pcap_sf_patched_pkthdr sf_hdr;
43117683Spst	FILE *fp = p->sf.rfile;
43217683Spst
43375107Sfenner	/*
43475107Sfenner	 * Read the packet header; the structure we use as a buffer
43575107Sfenner	 * is the longer structure for files generated by the patched
43675107Sfenner	 * libpcap, but if the file has the magic number for an
43775107Sfenner	 * unpatched libpcap we only read as many bytes as the regular
43875107Sfenner	 * header has.
43975107Sfenner	 */
44075107Sfenner	if (fread(&sf_hdr, p->sf.hdrsize, 1, fp) != 1) {
44117683Spst		/* probably an EOF, though could be a truncated packet */
44217683Spst		return (1);
44317683Spst	}
44417683Spst
44517683Spst	if (p->sf.swapped) {
44617683Spst		/* these were written in opposite byte order */
44756889Sfenner		hdr->caplen = SWAPLONG(sf_hdr.caplen);
44856889Sfenner		hdr->len = SWAPLONG(sf_hdr.len);
44956889Sfenner		hdr->ts.tv_sec = SWAPLONG(sf_hdr.ts.tv_sec);
45056889Sfenner		hdr->ts.tv_usec = SWAPLONG(sf_hdr.ts.tv_usec);
45156889Sfenner	} else {
45256889Sfenner		hdr->caplen = sf_hdr.caplen;
45356889Sfenner		hdr->len = sf_hdr.len;
45456889Sfenner		hdr->ts.tv_sec = sf_hdr.ts.tv_sec;
45556889Sfenner		hdr->ts.tv_usec = sf_hdr.ts.tv_usec;
45617683Spst	}
45717683Spst	/*
45817683Spst	 * We interchanged the caplen and len fields at version 2.3,
45917683Spst	 * in order to match the bpf header layout.  But unfortunately
46017683Spst	 * some files were written with version 2.3 in their headers
46117683Spst	 * but without the interchanged fields.
46217683Spst	 */
46317683Spst	if (p->sf.version_minor < 3 ||
46417683Spst	    (p->sf.version_minor == 3 && hdr->caplen > hdr->len)) {
46517683Spst		int t = hdr->caplen;
46617683Spst		hdr->caplen = hdr->len;
46717683Spst		hdr->len = t;
46817683Spst	}
46917683Spst
47017683Spst	if (hdr->caplen > buflen) {
47117683Spst		/*
47217683Spst		 * This can happen due to Solaris 2.3 systems tripping
47317683Spst		 * over the BUFMOD problem and not setting the snapshot
47417683Spst		 * correctly in the savefile header.  If the caplen isn't
47517683Spst		 * grossly wrong, try to salvage.
47617683Spst		 */
47717683Spst		static u_char *tp = NULL;
47817683Spst		static int tsize = 0;
47917683Spst
48026175Sfenner		if (hdr->caplen > 65535) {
48175107Sfenner			snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
48275107Sfenner			    "bogus savefile header");
48326175Sfenner			return (-1);
48426175Sfenner		}
48575107Sfenner
48617683Spst		if (tsize < hdr->caplen) {
48717683Spst			tsize = ((hdr->caplen + 1023) / 1024) * 1024;
48817683Spst			if (tp != NULL)
48917683Spst				free((u_char *)tp);
49017683Spst			tp = (u_char *)malloc(tsize);
49117683Spst			if (tp == NULL) {
49226175Sfenner				tsize = 0;
49375107Sfenner				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
49475107Sfenner				    "BUFMOD hack malloc");
49517683Spst				return (-1);
49617683Spst			}
49717683Spst		}
49817683Spst		if (fread((char *)tp, hdr->caplen, 1, fp) != 1) {
49975107Sfenner			snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
50075107Sfenner			    "truncated dump file");
50117683Spst			return (-1);
50217683Spst		}
50326175Sfenner		/*
50426175Sfenner		 * We can only keep up to buflen bytes.  Since caplen > buflen
50526175Sfenner		 * is exactly how we got here, we know we can only keep the
50626175Sfenner		 * first buflen bytes and must drop the remainder.  Adjust
50726175Sfenner		 * caplen accordingly, so we don't get confused later as
50826175Sfenner		 * to how many bytes we have to play with.
50926175Sfenner		 */
51026175Sfenner		hdr->caplen = buflen;
51117683Spst		memcpy((char *)buf, (char *)tp, buflen);
51217683Spst
51317683Spst	} else {
51417683Spst		/* read the packet itself */
51517683Spst
51617683Spst		if (fread((char *)buf, hdr->caplen, 1, fp) != 1) {
51775107Sfenner			snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
51875107Sfenner			    "truncated dump file");
51917683Spst			return (-1);
52017683Spst		}
52117683Spst	}
52217683Spst	return (0);
52317683Spst}
52417683Spst
52517683Spst/*
52617683Spst * Print out packets stored in the file initialized by sf_read_init().
52717683Spst * If cnt > 0, return after 'cnt' packets, otherwise continue until eof.
52817683Spst */
52917683Spstint
53017683Spstpcap_offline_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
53117683Spst{
53217683Spst	struct bpf_insn *fcode = p->fcode.bf_insns;
53317683Spst	int status = 0;
53417683Spst	int n = 0;
53517683Spst
53617683Spst	while (status == 0) {
53717683Spst		struct pcap_pkthdr h;
53817683Spst
53917683Spst		status = sf_next_packet(p, &h, p->buffer, p->bufsize);
54017683Spst		if (status) {
54117683Spst			if (status == 1)
54217683Spst				return (0);
54317683Spst			return (status);
54417683Spst		}
54517683Spst
54617683Spst		if (fcode == NULL ||
54717683Spst		    bpf_filter(fcode, p->buffer, h.len, h.caplen)) {
54817683Spst			(*callback)(user, &h, p->buffer);
54917683Spst			if (++n >= cnt && cnt > 0)
55017683Spst				break;
55117683Spst		}
55217683Spst	}
55317683Spst	/*XXX this breaks semantics tcpslice expects */
55417683Spst	return (n);
55517683Spst}
55617683Spst
55717683Spst/*
55817683Spst * Output a packet to the initialized dump file.
55917683Spst */
56017683Spstvoid
56117683Spstpcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
56217683Spst{
56317683Spst	register FILE *f;
56456889Sfenner	struct pcap_sf_pkthdr sf_hdr;
56517683Spst
56617683Spst	f = (FILE *)user;
56756889Sfenner	sf_hdr.ts.tv_sec  = h->ts.tv_sec;
56856889Sfenner	sf_hdr.ts.tv_usec = h->ts.tv_usec;
56956889Sfenner	sf_hdr.caplen     = h->caplen;
57056889Sfenner	sf_hdr.len        = h->len;
57117683Spst	/* XXX we should check the return status */
57256889Sfenner	(void)fwrite(&sf_hdr, sizeof(sf_hdr), 1, f);
57317683Spst	(void)fwrite((char *)sp, h->caplen, 1, f);
57417683Spst}
57517683Spst
57617683Spst/*
57717683Spst * Initialize so that sf_write() will output to the file named 'fname'.
57817683Spst */
57917683Spstpcap_dumper_t *
58039291Sfennerpcap_dump_open(pcap_t *p, const char *fname)
58117683Spst{
58217683Spst	FILE *f;
58375107Sfenner	int linktype;
58475107Sfenner
58575107Sfenner	linktype = dlt_to_linktype(p->linktype);
58675107Sfenner	if (linktype == -1) {
58775107Sfenner		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
58875107Sfenner		    "%s: link-layer type %d isn't supported in savefiles",
58975107Sfenner		    fname, linktype);
59075107Sfenner		return (NULL);
59175107Sfenner	}
59275107Sfenner
59317683Spst	if (fname[0] == '-' && fname[1] == '\0')
59417683Spst		f = stdout;
59517683Spst	else {
59617683Spst		f = fopen(fname, "w");
59717683Spst		if (f == NULL) {
59875107Sfenner			snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s",
59917683Spst			    fname, pcap_strerror(errno));
60017683Spst			return (NULL);
60117683Spst		}
60217683Spst	}
60375107Sfenner	(void)sf_write_header(f, linktype, p->tzoff, p->snapshot);
60417683Spst	return ((pcap_dumper_t *)f);
60517683Spst}
60617683Spst
60717683Spstvoid
60817683Spstpcap_dump_close(pcap_dumper_t *p)
60917683Spst{
61017683Spst
61117683Spst#ifdef notyet
61217683Spst	if (ferror((FILE *)p))
61317683Spst		return-an-error;
61417683Spst	/* XXX should check return from fclose() too */
61517683Spst#endif
61617683Spst	(void)fclose((FILE *)p);
61717683Spst}
618