xref: /openbsd-src/lib/libpcap/savefile.c (revision ac17704babad29bba70f30455d39f3f4e1d0112d)
1*ac17704bSsashan /*	$OpenBSD: savefile.c,v 1.18 2023/08/10 15:47:05 sashan Exp $	*/
2df930be7Sderaadt 
3df930be7Sderaadt /*
401efc7efSderaadt  * Copyright (c) 1993, 1994, 1995, 1996, 1997
5df930be7Sderaadt  *	The Regents of the University of California.  All rights reserved.
6df930be7Sderaadt  *
7df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
8df930be7Sderaadt  * modification, are permitted provided that: (1) source code distributions
9df930be7Sderaadt  * retain the above copyright notice and this paragraph in its entirety, (2)
10df930be7Sderaadt  * distributions including binary code include the above copyright notice and
11df930be7Sderaadt  * this paragraph in its entirety in the documentation or other materials
12df930be7Sderaadt  * provided with the distribution, and (3) all advertising materials mentioning
13df930be7Sderaadt  * features or use of this software display the following acknowledgement:
14df930be7Sderaadt  * ``This product includes software developed by the University of California,
15df930be7Sderaadt  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16df930be7Sderaadt  * the University nor the names of its contributors may be used to endorse
17df930be7Sderaadt  * or promote products derived from this software without specific prior
18df930be7Sderaadt  * written permission.
19df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20df930be7Sderaadt  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21df930be7Sderaadt  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2201efc7efSderaadt  *
23df930be7Sderaadt  * savefile.c - supports offline use of tcpdump
24df930be7Sderaadt  *	Extraction/creation by Jeffrey Mogul, DECWRL
25df930be7Sderaadt  *	Modified by Steve McCanne, LBL.
26df930be7Sderaadt  *
27df930be7Sderaadt  * Used to save the received packet headers, after filtering, to
28df930be7Sderaadt  * a file, and then read them later.
29df930be7Sderaadt  * The first record in the file contains saved values for the machine
30df930be7Sderaadt  * dependent values so we can print the dump file on any architecture.
31df930be7Sderaadt  */
32df930be7Sderaadt 
33df930be7Sderaadt #include <sys/types.h>
34df930be7Sderaadt #include <sys/time.h>
35df930be7Sderaadt 
36df930be7Sderaadt #include <errno.h>
37df930be7Sderaadt #include <stdio.h>
38df930be7Sderaadt #include <stdlib.h>
393a9b5ec4Smmcc #include <string.h>
40df930be7Sderaadt #include <unistd.h>
41df930be7Sderaadt 
429b113833Smickey #ifdef HAVE_OS_PROTO_H
439b113833Smickey #include "os-proto.h"
449b113833Smickey #endif
459b113833Smickey 
46df930be7Sderaadt #include "pcap-int.h"
47df930be7Sderaadt 
48df930be7Sderaadt #define TCPDUMP_MAGIC 0xa1b2c3d4
49df930be7Sderaadt 
50df930be7Sderaadt /*
51df930be7Sderaadt  * We use the "receiver-makes-right" approach to byte order,
52df930be7Sderaadt  * because time is at a premium when we are writing the file.
53df930be7Sderaadt  * In other words, the pcap_file_header and pcap_pkthdr,
54df930be7Sderaadt  * records are written in host byte order.
55df930be7Sderaadt  * Note that the packets are always written in network byte order.
56df930be7Sderaadt  *
57df930be7Sderaadt  * ntoh[ls] aren't sufficient because we might need to swap on a big-endian
58df930be7Sderaadt  * machine (if the file was written in little-end order).
59df930be7Sderaadt  */
60df930be7Sderaadt #define	SWAPLONG(y) \
61df930be7Sderaadt ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
62df930be7Sderaadt #define	SWAPSHORT(y) \
6301efc7efSderaadt 	( (((y)&0xff)<<8) | ((u_short)((y)&0xff00)>>8) )
64df930be7Sderaadt 
65df930be7Sderaadt #define SFERR_TRUNC		1
66df930be7Sderaadt #define SFERR_BADVERSION	2
67df930be7Sderaadt #define SFERR_BADF		3
68df930be7Sderaadt #define SFERR_EOF		4 /* not really an error, just a status */
69df930be7Sderaadt 
70df930be7Sderaadt static int
sf_write_header(FILE * fp,int linktype,int thiszone,int snaplen)71df930be7Sderaadt sf_write_header(FILE *fp, int linktype, int thiszone, int snaplen)
72df930be7Sderaadt {
73df930be7Sderaadt 	struct pcap_file_header hdr;
74df930be7Sderaadt 
75df930be7Sderaadt 	hdr.magic = TCPDUMP_MAGIC;
76df930be7Sderaadt 	hdr.version_major = PCAP_VERSION_MAJOR;
77df930be7Sderaadt 	hdr.version_minor = PCAP_VERSION_MINOR;
78df930be7Sderaadt 
79df930be7Sderaadt 	hdr.thiszone = thiszone;
80df930be7Sderaadt 	hdr.snaplen = snaplen;
81df930be7Sderaadt 	hdr.sigfigs = 0;
82df930be7Sderaadt 	hdr.linktype = linktype;
83df930be7Sderaadt 
84df930be7Sderaadt 	if (fwrite((char *)&hdr, sizeof(hdr), 1, fp) != 1)
85df930be7Sderaadt 		return (-1);
86df930be7Sderaadt 
87df930be7Sderaadt 	return (0);
88df930be7Sderaadt }
89df930be7Sderaadt 
90df930be7Sderaadt static void
swap_hdr(struct pcap_file_header * hp)91df930be7Sderaadt swap_hdr(struct pcap_file_header *hp)
92df930be7Sderaadt {
93df930be7Sderaadt 	hp->version_major = SWAPSHORT(hp->version_major);
94df930be7Sderaadt 	hp->version_minor = SWAPSHORT(hp->version_minor);
95df930be7Sderaadt 	hp->thiszone = SWAPLONG(hp->thiszone);
96df930be7Sderaadt 	hp->sigfigs = SWAPLONG(hp->sigfigs);
97df930be7Sderaadt 	hp->snaplen = SWAPLONG(hp->snaplen);
98df930be7Sderaadt 	hp->linktype = SWAPLONG(hp->linktype);
99df930be7Sderaadt }
100df930be7Sderaadt 
101df930be7Sderaadt pcap_t *
pcap_open_offline(const char * fname,char * errbuf)10201efc7efSderaadt pcap_open_offline(const char *fname, char *errbuf)
103df930be7Sderaadt {
104c1bf1209Sdjm 	pcap_t *p;
105c1bf1209Sdjm 	FILE *fp;
106c1bf1209Sdjm 
107c1bf1209Sdjm 	if (fname[0] == '-' && fname[1] == '\0')
108c1bf1209Sdjm 		fp = stdin;
109c1bf1209Sdjm 	else {
110c1bf1209Sdjm 		fp = fopen(fname, "r");
111c1bf1209Sdjm 		if (fp == NULL) {
112c1bf1209Sdjm 			snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", fname,
113c1bf1209Sdjm 			    pcap_strerror(errno));
114c1bf1209Sdjm 			return (NULL);
115c1bf1209Sdjm 		}
116c1bf1209Sdjm 	}
117c1bf1209Sdjm 	p = pcap_fopen_offline(fp, errbuf);
118c1bf1209Sdjm 	if (p == NULL) {
119c1bf1209Sdjm 		if (fp != stdin)
120c1bf1209Sdjm 			fclose(fp);
121c1bf1209Sdjm 	}
122c1bf1209Sdjm 	return (p);
123c1bf1209Sdjm }
124c1bf1209Sdjm 
125c1bf1209Sdjm pcap_t *
pcap_fopen_offline(FILE * fp,char * errbuf)126c1bf1209Sdjm pcap_fopen_offline(FILE *fp, char *errbuf)
127c1bf1209Sdjm {
128c1bf1209Sdjm 	pcap_t *p;
129df930be7Sderaadt 	struct pcap_file_header hdr;
130df930be7Sderaadt 	int linklen;
131df930be7Sderaadt 
13266cf3f76Slteo 	p = calloc(1, sizeof(*p));
133df930be7Sderaadt 	if (p == NULL) {
13401efc7efSderaadt 		strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
135df930be7Sderaadt 		return (NULL);
136df930be7Sderaadt 	}
137df930be7Sderaadt 
138df930be7Sderaadt 	/*
139c1bf1209Sdjm 	 * Set this field so we don't double-close in pcap_close!
140df930be7Sderaadt 	 */
141df930be7Sderaadt 	p->fd = -1;
142df930be7Sderaadt 
143df930be7Sderaadt 	if (fread((char *)&hdr, sizeof(hdr), 1, fp) != 1) {
14413c7aa11Sderaadt 		snprintf(errbuf, PCAP_ERRBUF_SIZE, "fread: %s",
14513c7aa11Sderaadt 		    pcap_strerror(errno));
146df930be7Sderaadt 		goto bad;
147df930be7Sderaadt 	}
148df930be7Sderaadt 	if (hdr.magic != TCPDUMP_MAGIC) {
149df930be7Sderaadt 		if (SWAPLONG(hdr.magic) != TCPDUMP_MAGIC) {
15013c7aa11Sderaadt 			snprintf(errbuf, PCAP_ERRBUF_SIZE,
15113c7aa11Sderaadt 			    "bad dump file format");
152df930be7Sderaadt 			goto bad;
153df930be7Sderaadt 		}
154df930be7Sderaadt 		p->sf.swapped = 1;
155df930be7Sderaadt 		swap_hdr(&hdr);
156df930be7Sderaadt 	}
157df930be7Sderaadt 	if (hdr.version_major < PCAP_VERSION_MAJOR) {
15813c7aa11Sderaadt 		snprintf(errbuf, PCAP_ERRBUF_SIZE, "archaic file format");
159df930be7Sderaadt 		goto bad;
160df930be7Sderaadt 	}
161df930be7Sderaadt 	p->tzoff = hdr.thiszone;
162df930be7Sderaadt 	p->snapshot = hdr.snaplen;
163*ac17704bSsashan 	/*
164*ac17704bSsashan 	 * Handle some LINKTYPE_ values in pcap headers that aren't
165*ac17704bSsashan 	 * the same as the corresponding OpenBSD DLT_ values.
166*ac17704bSsashan 	 *
167*ac17704bSsashan 	 * Those LINKTYPE_ values were assigned for DLT_s whose
168*ac17704bSsashan 	 * numerical values differ between platforms, so that
169*ac17704bSsashan 	 * the link-layer type value in pcap file headers can
170*ac17704bSsashan 	 * be platform-independent.  This means that code reading
171*ac17704bSsashan 	 * a pcap file doesn't have to know on which platform a
172*ac17704bSsashan 	 * file was written in order to read it correctly.
173*ac17704bSsashan 	 *
174*ac17704bSsashan 	 * See
175*ac17704bSsashan 	 *
176*ac17704bSsashan 	 *     https://www.tcpdump.org/linktypes.html
177*ac17704bSsashan 	 *
178*ac17704bSsashan 	 * for the current list of LINKTYPE_ values and the corresponding
179*ac17704bSsashan 	 * DLT_ values.
180*ac17704bSsashan 	 */
181*ac17704bSsashan 	switch (hdr.linktype) {
182*ac17704bSsashan 
183*ac17704bSsashan 	case 100:
184*ac17704bSsashan 		/* LINKTYPE_ATM_RFC1483 */
185*ac17704bSsashan 		p->linktype = DLT_ATM_RFC1483;
186*ac17704bSsashan 		break;
187*ac17704bSsashan 
188*ac17704bSsashan 	case 101:
189*ac17704bSsashan 		/* LINKTYPE_RAW */
190*ac17704bSsashan 		p->linktype = DLT_RAW;
191*ac17704bSsashan 		break;
192*ac17704bSsashan 
193*ac17704bSsashan 	case 102:
194*ac17704bSsashan 		/* LINKTYPE_SLIP_BSDOS */
195*ac17704bSsashan 		p->linktype = DLT_SLIP_BSDOS;
196*ac17704bSsashan 		break;
197*ac17704bSsashan 
198*ac17704bSsashan 	case 103:
199*ac17704bSsashan 		/* LINKTYPE_PPP_BSDOS */
200*ac17704bSsashan 		p->linktype = DLT_PPP_BSDOS;
201*ac17704bSsashan 		break;
202*ac17704bSsashan 
203*ac17704bSsashan 	case 108:
204*ac17704bSsashan 		/* LINKTYPE_LOOP */
205*ac17704bSsashan 		p->linktype = DLT_LOOP;
206*ac17704bSsashan 		break;
207*ac17704bSsashan 
208*ac17704bSsashan 	case 109:
209*ac17704bSsashan 		/* LINKTYPE_ENC */
210*ac17704bSsashan 		p->linktype = DLT_ENC;
211*ac17704bSsashan 		break;
212*ac17704bSsashan 
213*ac17704bSsashan 	case 256:
214*ac17704bSsashan 		/* LINKTYPE_PFSYNC */
215*ac17704bSsashan 		p->linktype = DLT_PFSYNC;
216*ac17704bSsashan 		break;
217*ac17704bSsashan 
218*ac17704bSsashan 	default:
219df930be7Sderaadt 		p->linktype = hdr.linktype;
220*ac17704bSsashan 		break;
221*ac17704bSsashan 	}
222df930be7Sderaadt 	p->sf.rfile = fp;
223df930be7Sderaadt 	p->bufsize = hdr.snaplen;
2249b113833Smickey 
225df930be7Sderaadt 	/* Align link header as required for proper data alignment */
2269b113833Smickey 	/* XXX should handle all types */
2279b113833Smickey 	switch (p->linktype) {
2289b113833Smickey 
2299b113833Smickey 	case DLT_EN10MB:
2309b113833Smickey 		linklen = 14;
2319b113833Smickey 		break;
2329b113833Smickey 
2339b113833Smickey 	case DLT_FDDI:
2349b113833Smickey 		linklen = 13 + 8;	/* fddi_header + llc */
2359b113833Smickey 		break;
2369b113833Smickey 
2379b113833Smickey 	case DLT_NULL:
2389b113833Smickey 	default:
2399b113833Smickey 		linklen = 0;
2409b113833Smickey 		break;
2419b113833Smickey 	}
2429b113833Smickey 
243a6c638c4Sderaadt 	if (p->bufsize < 0)
244a6c638c4Sderaadt 		p->bufsize = BPF_MAXBUFSIZE;
2451b8dcd42Slteo 	p->sf.base = malloc(p->bufsize + BPF_ALIGNMENT);
246a6c638c4Sderaadt 	if (p->sf.base == NULL) {
24701efc7efSderaadt 		strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
248a6c638c4Sderaadt 		goto bad;
249a6c638c4Sderaadt 	}
250df930be7Sderaadt 	p->buffer = p->sf.base + BPF_ALIGNMENT - (linklen % BPF_ALIGNMENT);
251df930be7Sderaadt 	p->sf.version_major = hdr.version_major;
252df930be7Sderaadt 	p->sf.version_minor = hdr.version_minor;
25301efc7efSderaadt #ifdef PCAP_FDDIPAD
25401efc7efSderaadt 	/* XXX padding only needed for kernel fcode */
25501efc7efSderaadt 	pcap_fddipad = 0;
25601efc7efSderaadt #endif
257df930be7Sderaadt 
258df930be7Sderaadt 	return (p);
259df930be7Sderaadt  bad:
260df930be7Sderaadt 	free(p);
261df930be7Sderaadt 	return (NULL);
262df930be7Sderaadt }
263df930be7Sderaadt 
264df930be7Sderaadt /*
265df930be7Sderaadt  * Read sf_readfile and return the next packet.  Return the header in hdr
266df930be7Sderaadt  * and the contents in buf.  Return 0 on success, SFERR_EOF if there were
267df930be7Sderaadt  * no more packets, and SFERR_TRUNC if a partial packet was encountered.
268df930be7Sderaadt  */
269df930be7Sderaadt static int
sf_next_packet(pcap_t * p,struct pcap_pkthdr * hdr,u_char * buf,int buflen)270df930be7Sderaadt sf_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char *buf, int buflen)
271df930be7Sderaadt {
272df930be7Sderaadt 	FILE *fp = p->sf.rfile;
273df930be7Sderaadt 
274df930be7Sderaadt 	/* read the stamp */
275df930be7Sderaadt 	if (fread((char *)hdr, sizeof(struct pcap_pkthdr), 1, fp) != 1) {
276df930be7Sderaadt 		/* probably an EOF, though could be a truncated packet */
277df930be7Sderaadt 		return (1);
278df930be7Sderaadt 	}
279df930be7Sderaadt 
280df930be7Sderaadt 	if (p->sf.swapped) {
281df930be7Sderaadt 		/* these were written in opposite byte order */
282df930be7Sderaadt 		hdr->caplen = SWAPLONG(hdr->caplen);
283df930be7Sderaadt 		hdr->len = SWAPLONG(hdr->len);
284df930be7Sderaadt 		hdr->ts.tv_sec = SWAPLONG(hdr->ts.tv_sec);
285df930be7Sderaadt 		hdr->ts.tv_usec = SWAPLONG(hdr->ts.tv_usec);
286df930be7Sderaadt 	}
287df930be7Sderaadt 	/*
288df930be7Sderaadt 	 * We interchanged the caplen and len fields at version 2.3,
289df930be7Sderaadt 	 * in order to match the bpf header layout.  But unfortunately
290df930be7Sderaadt 	 * some files were written with version 2.3 in their headers
291df930be7Sderaadt 	 * but without the interchanged fields.
292df930be7Sderaadt 	 */
293df930be7Sderaadt 	if (p->sf.version_minor < 3 ||
294df930be7Sderaadt 	    (p->sf.version_minor == 3 && hdr->caplen > hdr->len)) {
295df930be7Sderaadt 		int t = hdr->caplen;
296df930be7Sderaadt 		hdr->caplen = hdr->len;
297df930be7Sderaadt 		hdr->len = t;
298df930be7Sderaadt 	}
299df930be7Sderaadt 
300df930be7Sderaadt 	if (hdr->caplen > buflen) {
3019b113833Smickey 		/*
3029b113833Smickey 		 * This can happen due to Solaris 2.3 systems tripping
3039b113833Smickey 		 * over the BUFMOD problem and not setting the snapshot
3049b113833Smickey 		 * correctly in the savefile header.  If the caplen isn't
3059b113833Smickey 		 * grossly wrong, try to salvage.
3069b113833Smickey 		 */
3079b113833Smickey 		static u_char *tp = NULL;
3089b113833Smickey 		static int tsize = 0;
3099b113833Smickey 
31001efc7efSderaadt 		if (hdr->caplen > 65535) {
31101efc7efSderaadt 			snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
31201efc7efSderaadt 			    "bogus savefile header");
31301efc7efSderaadt 			return (-1);
31401efc7efSderaadt 		}
31501efc7efSderaadt 
3169b113833Smickey 		if (tsize < hdr->caplen) {
3179b113833Smickey 			tsize = ((hdr->caplen + 1023) / 1024) * 1024;
3181b8dcd42Slteo 			free(tp);
3191b8dcd42Slteo 			tp = malloc(tsize);
3209b113833Smickey 			if (tp == NULL) {
32101efc7efSderaadt 				tsize = 0;
32213c7aa11Sderaadt 				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
32313c7aa11Sderaadt 				    "BUFMOD hack malloc");
324df930be7Sderaadt 				return (-1);
325df930be7Sderaadt 			}
3269b113833Smickey 		}
3279b113833Smickey 		if (fread((char *)tp, hdr->caplen, 1, fp) != 1) {
32813c7aa11Sderaadt 			snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
32913c7aa11Sderaadt 			    "truncated dump file");
3309b113833Smickey 			return (-1);
3319b113833Smickey 		}
33201efc7efSderaadt 		/*
33301efc7efSderaadt 		 * We can only keep up to buflen bytes.  Since caplen > buflen
33401efc7efSderaadt 		 * is exactly how we got here, we know we can only keep the
33501efc7efSderaadt 		 * first buflen bytes and must drop the remainder.  Adjust
33601efc7efSderaadt 		 * caplen accordingly, so we don't get confused later as
33701efc7efSderaadt 		 * to how many bytes we have to play with.
33801efc7efSderaadt 		 */
33901efc7efSderaadt 		hdr->caplen = buflen;
3409b113833Smickey 		memcpy((char *)buf, (char *)tp, buflen);
341df930be7Sderaadt 
3429b113833Smickey 	} else {
343df930be7Sderaadt 		/* read the packet itself */
344df930be7Sderaadt 
345df930be7Sderaadt 		if (fread((char *)buf, hdr->caplen, 1, fp) != 1) {
34613c7aa11Sderaadt 			snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
34713c7aa11Sderaadt 			    "truncated dump file");
348df930be7Sderaadt 			return (-1);
349df930be7Sderaadt 		}
3509b113833Smickey 	}
351df930be7Sderaadt 	return (0);
352df930be7Sderaadt }
353df930be7Sderaadt 
354df930be7Sderaadt /*
355df930be7Sderaadt  * Print out packets stored in the file initialized by sf_read_init().
356df930be7Sderaadt  * If cnt > 0, return after 'cnt' packets, otherwise continue until eof.
357df930be7Sderaadt  */
358df930be7Sderaadt int
pcap_offline_read(pcap_t * p,int cnt,pcap_handler callback,u_char * user)359df930be7Sderaadt pcap_offline_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
360df930be7Sderaadt {
361df930be7Sderaadt 	struct bpf_insn *fcode = p->fcode.bf_insns;
362df930be7Sderaadt 	int status = 0;
363df930be7Sderaadt 	int n = 0;
364df930be7Sderaadt 
365df930be7Sderaadt 	while (status == 0) {
366df930be7Sderaadt 		struct pcap_pkthdr h;
367df930be7Sderaadt 
368c5e4be46Sdlg 		/*
369c5e4be46Sdlg 		 * Has "pcap_breakloop()" been called?
370c5e4be46Sdlg 		 * If so, return immediately - if we haven't read any
371c5e4be46Sdlg 		 * packets, clear the flag and return -2 to indicate
372c5e4be46Sdlg 		 * that we were told to break out of the loop, otherwise
373c5e4be46Sdlg 		 * leave the flag set, so that the *next* call will break
374c5e4be46Sdlg 		 * out of the loop without having read any packets, and
375c5e4be46Sdlg 		 * return the number of packets we've processed so far.
376c5e4be46Sdlg 		 */
377c5e4be46Sdlg 		if (p->break_loop) {
378c5e4be46Sdlg 			if (n == 0) {
379c5e4be46Sdlg 				p->break_loop = 0;
380c5e4be46Sdlg 				return (PCAP_ERROR_BREAK);
381c5e4be46Sdlg 			} else
382c5e4be46Sdlg 				return (n);
383c5e4be46Sdlg 		}
384c5e4be46Sdlg 
385df930be7Sderaadt 		status = sf_next_packet(p, &h, p->buffer, p->bufsize);
3869b113833Smickey 		if (status) {
3879b113833Smickey 			if (status == 1)
3889b113833Smickey 				return (0);
3899b113833Smickey 			return (status);
3909b113833Smickey 		}
391df930be7Sderaadt 
392df930be7Sderaadt 		if (fcode == NULL ||
393df930be7Sderaadt 		    bpf_filter(fcode, p->buffer, h.len, h.caplen)) {
394df930be7Sderaadt 			(*callback)(user, &h, p->buffer);
395df930be7Sderaadt 			if (++n >= cnt && cnt > 0)
396df930be7Sderaadt 				break;
397df930be7Sderaadt 		}
398df930be7Sderaadt 	}
399df930be7Sderaadt 	/*XXX this breaks semantics tcpslice expects */
400df930be7Sderaadt 	return (n);
401df930be7Sderaadt }
402df930be7Sderaadt 
403df930be7Sderaadt /*
404df930be7Sderaadt  * Output a packet to the initialized dump file.
405df930be7Sderaadt  */
406df930be7Sderaadt void
pcap_dump(u_char * user,const struct pcap_pkthdr * h,const u_char * sp)407df930be7Sderaadt pcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
408df930be7Sderaadt {
409d0438536Smmcc 	FILE *f;
4109b113833Smickey 
4119b113833Smickey 	f = (FILE *)user;
4129b113833Smickey 	/* XXX we should check the return status */
413df930be7Sderaadt 	(void)fwrite((char *)h, sizeof(*h), 1, f);
414df930be7Sderaadt 	(void)fwrite((char *)sp, h->caplen, 1, f);
415df930be7Sderaadt }
416df930be7Sderaadt 
417c1bf1209Sdjm static pcap_dumper_t *
pcap_setup_dump(pcap_t * p,FILE * f,const char * fname)418c1bf1209Sdjm pcap_setup_dump(pcap_t *p, FILE *f, const char *fname)
419c1bf1209Sdjm {
420c1bf1209Sdjm 	if (sf_write_header(f, p->linktype, p->tzoff, p->snapshot) == -1) {
421c1bf1209Sdjm 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "Can't write to %s: %s",
422c1bf1209Sdjm 		    fname, pcap_strerror(errno));
423c1bf1209Sdjm 		if (f != stdout)
424c1bf1209Sdjm 			(void)fclose(f);
425c1bf1209Sdjm 		return (NULL);
426c1bf1209Sdjm 	}
427c1bf1209Sdjm 	return ((pcap_dumper_t *)f);
428c1bf1209Sdjm }
429c1bf1209Sdjm 
430df930be7Sderaadt /*
431df930be7Sderaadt  * Initialize so that sf_write() will output to the file named 'fname'.
432df930be7Sderaadt  */
433df930be7Sderaadt pcap_dumper_t *
pcap_dump_open(pcap_t * p,const char * fname)43401efc7efSderaadt pcap_dump_open(pcap_t *p, const char *fname)
435df930be7Sderaadt {
436df930be7Sderaadt 	FILE *f;
437df930be7Sderaadt 	if (fname[0] == '-' && fname[1] == '\0')
438df930be7Sderaadt 		f = stdout;
439df930be7Sderaadt 	else {
440df930be7Sderaadt 		f = fopen(fname, "w");
441df930be7Sderaadt 		if (f == NULL) {
44213c7aa11Sderaadt 			snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s",
443df930be7Sderaadt 			    fname, pcap_strerror(errno));
444df930be7Sderaadt 			return (NULL);
445df930be7Sderaadt 		}
446df930be7Sderaadt 	}
447c1bf1209Sdjm 	return (pcap_setup_dump(p, f, fname));
448c1bf1209Sdjm }
449c1bf1209Sdjm 
450c1bf1209Sdjm /*
451c1bf1209Sdjm  * Initialize so that sf_write() will output to the given stream.
452c1bf1209Sdjm  */
453c1bf1209Sdjm pcap_dumper_t *
pcap_dump_fopen(pcap_t * p,FILE * f)454c1bf1209Sdjm pcap_dump_fopen(pcap_t *p, FILE *f)
455c1bf1209Sdjm {
456c1bf1209Sdjm 	return (pcap_setup_dump(p, f, "stream"));
457c1bf1209Sdjm }
458c1bf1209Sdjm 
459c1bf1209Sdjm FILE *
pcap_dump_file(pcap_dumper_t * p)460c1bf1209Sdjm pcap_dump_file(pcap_dumper_t *p)
461c1bf1209Sdjm {
462c1bf1209Sdjm 	return ((FILE *)p);
463c1bf1209Sdjm }
464c1bf1209Sdjm 
465c1bf1209Sdjm long
pcap_dump_ftell(pcap_dumper_t * p)466c1bf1209Sdjm pcap_dump_ftell(pcap_dumper_t *p)
467c1bf1209Sdjm {
468c1bf1209Sdjm 	return (ftell((FILE *)p));
469c1bf1209Sdjm }
470c1bf1209Sdjm 
47111297935Slteo int
pcap_dump_flush(pcap_dumper_t * p)472c1bf1209Sdjm pcap_dump_flush(pcap_dumper_t *p)
473c1bf1209Sdjm {
474c1bf1209Sdjm 
475c1bf1209Sdjm 	if (fflush((FILE *)p) == EOF)
476c1bf1209Sdjm 		return (-1);
477c1bf1209Sdjm 	else
478c1bf1209Sdjm 		return (0);
479df930be7Sderaadt }
480df930be7Sderaadt 
481df930be7Sderaadt void
pcap_dump_close(pcap_dumper_t * p)482df930be7Sderaadt pcap_dump_close(pcap_dumper_t *p)
483df930be7Sderaadt {
4849b113833Smickey 
4859b113833Smickey #ifdef notyet
4869b113833Smickey 	if (ferror((FILE *)p))
4879b113833Smickey 		return-an-error;
4889b113833Smickey 	/* XXX should check return from fclose() too */
4899b113833Smickey #endif
4909b113833Smickey 	(void)fclose((FILE *)p);
491df930be7Sderaadt }
492