xref: /netbsd/external/bsd/libpcap/dist/savefile.c (revision 6550d01e)
1 /*
2  * Copyright (c) 1993, 1994, 1995, 1996, 1997
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that: (1) source code distributions
7  * retain the above copyright notice and this paragraph in its entirety, (2)
8  * distributions including binary code include the above copyright notice and
9  * this paragraph in its entirety in the documentation or other materials
10  * provided with the distribution, and (3) all advertising materials mentioning
11  * features or use of this software display the following acknowledgement:
12  * ``This product includes software developed by the University of California,
13  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14  * the University nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior
16  * written permission.
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20  *
21  * savefile.c - supports offline use of tcpdump
22  *	Extraction/creation by Jeffrey Mogul, DECWRL
23  *	Modified by Steve McCanne, LBL.
24  *
25  * Used to save the received packet headers, after filtering, to
26  * a file, and then read them later.
27  * The first record in the file contains saved values for the machine
28  * dependent values so we can print the dump file on any architecture.
29  */
30 
31 #ifndef lint
32 static const char rcsid[] _U_ =
33     "@(#) Header: /tcpdump/master/libpcap/savefile.c,v 1.183 2008-12-23 20:13:29 guy Exp (LBL)";
34 #endif
35 
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39 
40 #ifdef WIN32
41 #include <pcap-stdinc.h>
42 #else /* WIN32 */
43 #if HAVE_INTTYPES_H
44 #include <inttypes.h>
45 #elif HAVE_STDINT_H
46 #include <stdint.h>
47 #endif
48 #ifdef HAVE_SYS_BITYPES_H
49 #include <sys/bitypes.h>
50 #endif
51 #include <sys/types.h>
52 #endif /* WIN32 */
53 
54 #include <errno.h>
55 #include <memory.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 
60 #include "pcap-int.h"
61 #include "pcap/usb.h"
62 
63 #ifdef HAVE_OS_PROTO_H
64 #include "os-proto.h"
65 #endif
66 
67 #include "sf-pcap.h"
68 #include "sf-pcap-ng.h"
69 
70 /*
71  * Setting O_BINARY on DOS/Windows is a bit tricky
72  */
73 #if defined(WIN32)
74   #define SET_BINMODE(f)  _setmode(_fileno(f), _O_BINARY)
75 #elif defined(MSDOS)
76   #if defined(__HIGHC__)
77   #define SET_BINMODE(f)  setmode(f, O_BINARY)
78   #else
79   #define SET_BINMODE(f)  setmode(fileno(f), O_BINARY)
80   #endif
81 #endif
82 
83 static int
84 sf_getnonblock(pcap_t *p, char *errbuf)
85 {
86 	/*
87 	 * This is a savefile, not a live capture file, so never say
88 	 * it's in non-blocking mode.
89 	 */
90 	return (0);
91 }
92 
93 static int
94 sf_setnonblock(pcap_t *p, int nonblock, char *errbuf)
95 {
96 	/*
97 	 * This is a savefile, not a live capture file, so ignore
98 	 * requests to put it in non-blocking mode.
99 	 */
100 	return (0);
101 }
102 
103 static int
104 sf_stats(pcap_t *p, struct pcap_stat *ps)
105 {
106 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
107 	    "Statistics aren't available from savefiles");
108 	return (-1);
109 }
110 
111 #ifdef WIN32
112 static int
113 sf_setbuff(pcap_t *p, int dim)
114 {
115 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
116 	    "The kernel buffer size cannot be set while reading from a file");
117 	return (-1);
118 }
119 
120 static int
121 sf_setmode(pcap_t *p, int mode)
122 {
123 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
124 	    "impossible to set mode while reading from a file");
125 	return (-1);
126 }
127 
128 static int
129 sf_setmintocopy(pcap_t *p, int size)
130 {
131 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
132 	    "The mintocopy parameter cannot be set while reading from a file");
133 	return (-1);
134 }
135 #endif
136 
137 static int
138 sf_inject(pcap_t *p, const void *buf _U_, size_t size _U_)
139 {
140 	strlcpy(p->errbuf, "Sending packets isn't supported on savefiles",
141 	    PCAP_ERRBUF_SIZE);
142 	return (-1);
143 }
144 
145 /*
146  * Set direction flag: Which packets do we accept on a forwarding
147  * single device? IN, OUT or both?
148  */
149 static int
150 sf_setdirection(pcap_t *p, pcap_direction_t d)
151 {
152 	snprintf(p->errbuf, sizeof(p->errbuf),
153 	    "Setting direction is not supported on savefiles");
154 	return (-1);
155 }
156 
157 static void
158 sf_cleanup(pcap_t *p)
159 {
160 	if (p->sf.rfile != stdin)
161 		(void)fclose(p->sf.rfile);
162 	if (p->buffer != NULL)
163 		free(p->buffer);
164 }
165 
166 pcap_t *
167 pcap_open_offline(const char *fname, char *errbuf)
168 {
169 	FILE *fp;
170 	pcap_t *p;
171 
172 	if (fname[0] == '-' && fname[1] == '\0')
173 	{
174 		fp = stdin;
175 #if defined(WIN32) || defined(MSDOS)
176 		/*
177 		 * We're reading from the standard input, so put it in binary
178 		 * mode, as savefiles are binary files.
179 		 */
180 		SET_BINMODE(fp);
181 #endif
182 	}
183 	else {
184 #if !defined(WIN32) && !defined(MSDOS)
185 		fp = fopen(fname, "r");
186 #else
187 		fp = fopen(fname, "rb");
188 #endif
189 		if (fp == NULL) {
190 			snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", fname,
191 			    pcap_strerror(errno));
192 			return (NULL);
193 		}
194 	}
195 	p = pcap_fopen_offline(fp, errbuf);
196 	if (p == NULL) {
197 		if (fp != stdin)
198 			fclose(fp);
199 	}
200 	return (p);
201 }
202 
203 #ifdef WIN32
204 pcap_t* pcap_hopen_offline(intptr_t osfd, char *errbuf)
205 {
206 	int fd;
207 	FILE *file;
208 
209 	fd = _open_osfhandle(osfd, _O_RDONLY);
210 	if ( fd < 0 )
211 	{
212 		snprintf(errbuf, PCAP_ERRBUF_SIZE, pcap_strerror(errno));
213 		return NULL;
214 	}
215 
216 	file = _fdopen(fd, "rb");
217 	if ( file == NULL )
218 	{
219 		snprintf(errbuf, PCAP_ERRBUF_SIZE, pcap_strerror(errno));
220 		return NULL;
221 	}
222 
223 	return pcap_fopen_offline(file, errbuf);
224 }
225 #endif
226 
227 static int (*check_headers[])(pcap_t *, bpf_u_int32, FILE *, char *) = {
228 	pcap_check_header,
229 	pcap_ng_check_header
230 };
231 
232 #define	N_FILE_TYPES	(sizeof check_headers / sizeof check_headers[0])
233 
234 #ifdef WIN32
235 static
236 #endif
237 pcap_t *
238 pcap_fopen_offline(FILE *fp, char *errbuf)
239 {
240 	register pcap_t *p;
241 	bpf_u_int32 magic;
242 	size_t amt_read;
243 	u_int i;
244 
245 	p = pcap_create_common("(savefile)", errbuf);
246 	if (p == NULL)
247 		return (NULL);
248 
249 	/*
250 	 * Read the first 4 bytes of the file; the network analyzer dump
251 	 * file formats we support (pcap and pcap-ng), and several other
252 	 * formats we might support in the future (such as snoop, DOS and
253 	 * Windows Sniffer, and Microsoft Network Monitor) all have magic
254 	 * numbers that are unique in their first 4 bytes.
255 	 */
256 	amt_read = fread((char *)&magic, 1, sizeof(magic), fp);
257 	if (amt_read != sizeof(magic)) {
258 		if (ferror(fp)) {
259 			snprintf(errbuf, PCAP_ERRBUF_SIZE,
260 			    "error reading dump file: %s",
261 			    pcap_strerror(errno));
262 		} else {
263 			snprintf(errbuf, PCAP_ERRBUF_SIZE,
264 			    "truncated dump file; tried to read %lu file header bytes, only got %lu",
265 			    (unsigned long)sizeof(magic),
266 			    (unsigned long)amt_read);
267 		}
268 		goto bad;
269 	}
270 
271 	/*
272 	 * Try all file types.
273 	 */
274 	for (i = 0; i < N_FILE_TYPES; i++) {
275 		switch ((*check_headers[i])(p, magic, fp, errbuf)) {
276 
277 		case -1:
278 			/*
279 			 * Error trying to read the header.
280 			 */
281 			goto bad;
282 
283 		case 1:
284 			/*
285 			 * Yup, that's it.
286 			 */
287 			goto found;
288 		}
289 	}
290 
291 	/*
292 	 * Well, who knows what this mess is....
293 	 */
294 	snprintf(errbuf, PCAP_ERRBUF_SIZE, "unknown file format");
295 	goto bad;
296 
297 found:
298 	p->sf.rfile = fp;
299 
300 #ifdef PCAP_FDDIPAD
301 	/* Padding only needed for live capture fcode */
302 	p->fddipad = 0;
303 #endif
304 
305 #if !defined(WIN32) && !defined(MSDOS)
306 	/*
307 	 * You can do "select()" and "poll()" on plain files on most
308 	 * platforms, and should be able to do so on pipes.
309 	 *
310 	 * You can't do "select()" on anything other than sockets in
311 	 * Windows, so, on Win32 systems, we don't have "selectable_fd".
312 	 */
313 	p->selectable_fd = fileno(fp);
314 #endif
315 
316 	p->read_op = pcap_offline_read;
317 	p->inject_op = sf_inject;
318 	p->setfilter_op = install_bpf_program;
319 	p->setdirection_op = sf_setdirection;
320 	p->set_datalink_op = NULL;	/* we don't support munging link-layer headers */
321 	p->getnonblock_op = sf_getnonblock;
322 	p->setnonblock_op = sf_setnonblock;
323 	p->stats_op = sf_stats;
324 #ifdef WIN32
325 	p->setbuff_op = sf_setbuff;
326 	p->setmode_op = sf_setmode;
327 	p->setmintocopy_op = sf_setmintocopy;
328 #endif
329 	p->cleanup_op = sf_cleanup;
330 	p->activated = 1;
331 
332 	return (p);
333  bad:
334 	free(p);
335 	return (NULL);
336 }
337 
338 /*
339  * Read packets from a capture file, and call the callback for each
340  * packet.
341  * If cnt > 0, return after 'cnt' packets, otherwise continue until eof.
342  */
343 int
344 pcap_offline_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
345 {
346 	struct bpf_insn *fcode;
347 	int status = 0;
348 	int n = 0;
349 	u_char *data;
350 
351 	while (status == 0) {
352 		struct pcap_pkthdr h;
353 
354 		/*
355 		 * Has "pcap_breakloop()" been called?
356 		 * If so, return immediately - if we haven't read any
357 		 * packets, clear the flag and return -2 to indicate
358 		 * that we were told to break out of the loop, otherwise
359 		 * leave the flag set, so that the *next* call will break
360 		 * out of the loop without having read any packets, and
361 		 * return the number of packets we've processed so far.
362 		 */
363 		if (p->break_loop) {
364 			if (n == 0) {
365 				p->break_loop = 0;
366 				return (-2);
367 			} else
368 				return (n);
369 		}
370 
371 		status = p->sf.next_packet_op(p, &h, &data);
372 		if (status) {
373 			if (status == 1)
374 				return (0);
375 			return (status);
376 		}
377 
378 		if ((fcode = p->fcode.bf_insns) == NULL ||
379 		    bpf_filter(fcode, p->buffer, h.len, h.caplen)) {
380 			(*callback)(user, &h, data);
381 			if (++n >= cnt && cnt > 0)
382 				break;
383 		}
384 	}
385 	/*XXX this breaks semantics tcpslice expects */
386 	return (n);
387 }
388