2 * Copyright (c) 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
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
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.
21 * savefile.c - supports offline use of tcpdump
22 * Extraction/creation by Jeffrey Mogul, DECWRL
23 * Modified by Steve McCanne, LBL.
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.
35 #include <pcap-types.h>
46 #include <limits.h> /* for INT_MAX */
50 #ifdef HAVE_OS_PROTO_H
55 #include "sf-pcapng.h"
56 #include "pcap-common.h"
60 * These aren't exported on Windows, because they would only work if both
61 * WinPcap/Npcap and the code using it were to use the Universal CRT; otherwise,
62 * a FILE structure in WinPcap/Npcap and a FILE structure in the code using it
63 * could be different if they're using different versions of the C runtime.
65 * Instead, pcap/pcap.h defines them as macros that wrap the hopen versions,
66 * with the wrappers calling _fileno() and _get_osfhandle() themselves,
67 * so that they convert the appropriate CRT version's FILE structure to
68 * a HANDLE (which is OS-defined, not CRT-defined, and is part of the Win32
71 static pcap_t *pcap_fopen_offline_with_tstamp_precision(FILE *, u_int, char *);
72 static pcap_t *pcap_fopen_offline(FILE *, char *);
76 * Setting O_BINARY on DOS/Windows is a bit tricky
79 #define SET_BINMODE(f) _setmode(_fileno(f), _O_BINARY)
81 #if defined(__HIGHC__)
82 #define SET_BINMODE(f) setmode(f, O_BINARY)
84 #define SET_BINMODE(f) setmode(fileno(f), O_BINARY)
89 sf_getnonblock(pcap_t *p _U_)
92 * This is a savefile, not a live capture file, so never say
93 * it's in non-blocking mode.
99 sf_setnonblock(pcap_t *p, int nonblock _U_)
102 * This is a savefile, not a live capture file, so reject
103 * requests to put it in non-blocking mode. (If it's a
104 * pipe, it could be put in non-blocking mode, but that
105 * would significantly complicate the code to read packets,
106 * as it would have to handle reading partial packets and
107 * keeping the state of the read.)
109 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
110 "Savefiles cannot be put into non-blocking mode");
115 sf_stats(pcap_t *p, struct pcap_stat *ps _U_)
117 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
118 "Statistics aren't available from savefiles");
123 static struct pcap_stat *
124 sf_stats_ex(pcap_t *p, int *size)
126 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
127 "Statistics aren't available from savefiles");
132 sf_setbuff(pcap_t *p, int dim)
134 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
135 "The kernel buffer size cannot be set while reading from a file");
140 sf_setmode(pcap_t *p, int mode)
142 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
143 "impossible to set mode while reading from a file");
148 sf_setmintocopy(pcap_t *p, int size)
150 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
151 "The mintocopy parameter cannot be set while reading from a file");
156 sf_getevent(pcap_t *pcap)
158 (void)pcap_snprintf(pcap->errbuf, sizeof(pcap->errbuf),
159 "The read event cannot be retrieved while reading from a file");
160 return (INVALID_HANDLE_VALUE);
164 sf_oid_get_request(pcap_t *p, bpf_u_int32 oid _U_, void *data _U_,
167 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
168 "An OID get request cannot be performed on a file");
173 sf_oid_set_request(pcap_t *p, bpf_u_int32 oid _U_, const void *data _U_,
176 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
177 "An OID set request cannot be performed on a file");
182 sf_sendqueue_transmit(pcap_t *p, pcap_send_queue *queue, int sync)
184 pcap_strlcpy(p->errbuf, "Sending packets isn't supported on savefiles",
190 sf_setuserbuffer(pcap_t *p, int size)
192 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
193 "The user buffer cannot be set when reading from a file");
198 sf_live_dump(pcap_t *p, char *filename, int maxsize, int maxpacks)
200 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
201 "Live packet dumping cannot be performed when reading from a file");
206 sf_live_dump_ended(pcap_t *p, int sync)
208 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
209 "Live packet dumping cannot be performed on a pcap_open_dead pcap_t");
213 static PAirpcapHandle
214 sf_get_airpcap_handle(pcap_t *pcap)
221 sf_inject(pcap_t *p, const void *buf _U_, size_t size _U_)
223 pcap_strlcpy(p->errbuf, "Sending packets isn't supported on savefiles",
229 * Set direction flag: Which packets do we accept on a forwarding
230 * single device? IN, OUT or both?
233 sf_setdirection(pcap_t *p, pcap_direction_t d _U_)
235 pcap_snprintf(p->errbuf, sizeof(p->errbuf),
236 "Setting direction is not supported on savefiles");
241 sf_cleanup(pcap_t *p)
243 if (p->rfile != stdin)
244 (void)fclose(p->rfile);
245 if (p->buffer != NULL)
247 pcap_freecode(&p->fcode);
251 pcap_open_offline_with_tstamp_precision(const char *fname, u_int precision,
258 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
259 "A null pointer was supplied as the file name");
262 if (fname[0] == '-' && fname[1] == '\0')
265 #if defined(_WIN32) || defined(MSDOS)
267 * We're reading from the standard input, so put it in binary
268 * mode, as savefiles are binary files.
275 * "b" is supported as of C90, so *all* UN*Xes should
276 * support it, even though it does nothing. It's
277 * required on Windows, as the file is a binary file
278 * and must be read in binary mode.
280 fp = fopen(fname, "rb");
282 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
287 p = pcap_fopen_offline_with_tstamp_precision(fp, precision, errbuf);
296 pcap_open_offline(const char *fname, char *errbuf)
298 return (pcap_open_offline_with_tstamp_precision(fname,
299 PCAP_TSTAMP_PRECISION_MICRO, errbuf));
303 pcap_t* pcap_hopen_offline_with_tstamp_precision(intptr_t osfd, u_int precision,
309 fd = _open_osfhandle(osfd, _O_RDONLY);
312 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
313 errno, "_open_osfhandle");
317 file = _fdopen(fd, "rb");
320 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
326 return pcap_fopen_offline_with_tstamp_precision(file, precision,
330 pcap_t* pcap_hopen_offline(intptr_t osfd, char *errbuf)
332 return pcap_hopen_offline_with_tstamp_precision(osfd,
333 PCAP_TSTAMP_PRECISION_MICRO, errbuf);
338 * Given a link-layer header type and snapshot length, return a
339 * snapshot length to use when reading the file; it's guaranteed
340 * to be > 0 and <= INT_MAX.
342 * XXX - the only reason why we limit it to <= INT_MAX is so that
343 * it fits in p->snapshot, and the only reason that p->snapshot is
344 * signed is that pcap_snapshot() returns an int, not an unsigned int.
347 pcap_adjust_snapshot(bpf_u_int32 linktype, bpf_u_int32 snaplen)
349 if (snaplen == 0 || snaplen > INT_MAX) {
351 * Bogus snapshot length; use the maximum for this
352 * link-layer type as a fallback.
354 * XXX - we don't clamp snapshot lengths that are
355 * <= INT_MAX but > max_snaplen_for_dlt(linktype),
356 * so a capture file could cause us to allocate
357 * a Really Big Buffer.
359 snaplen = max_snaplen_for_dlt(linktype);
364 static pcap_t *(*check_headers[])(const uint8_t *, FILE *, u_int, char *, int *) = {
369 #define N_FILE_TYPES (sizeof check_headers / sizeof check_headers[0])
375 pcap_fopen_offline_with_tstamp_precision(FILE *fp, u_int precision,
385 * Read the first 4 bytes of the file; the network analyzer dump
386 * file formats we support (pcap and pcapng), and several other
387 * formats we might support in the future (such as snoop, DOS and
388 * Windows Sniffer, and Microsoft Network Monitor) all have magic
389 * numbers that are unique in their first 4 bytes.
391 amt_read = fread(&magic, 1, sizeof(magic), fp);
392 if (amt_read != sizeof(magic)) {
394 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
395 errno, "error reading dump file");
397 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
398 "truncated dump file; tried to read %" PRIsize " file header bytes, only got %" PRIsize,
399 sizeof(magic), amt_read);
405 * Try all file types.
407 for (i = 0; i < N_FILE_TYPES; i++) {
408 p = (*check_headers[i])(magic, fp, precision, errbuf, &err);
410 /* Yup, that's it. */
415 * Error trying to read the header.
422 * Well, who knows what this mess is....
424 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "unknown file format");
430 /* Padding only needed for live capture fcode */
433 #if !defined(_WIN32) && !defined(MSDOS)
435 * You can do "select()" and "poll()" on plain files on most
436 * platforms, and should be able to do so on pipes.
438 * You can't do "select()" on anything other than sockets in
439 * Windows, so, on Win32 systems, we don't have "selectable_fd".
441 p->selectable_fd = fileno(fp);
444 p->read_op = pcap_offline_read;
445 p->inject_op = sf_inject;
446 p->setfilter_op = install_bpf_program;
447 p->setdirection_op = sf_setdirection;
448 p->set_datalink_op = NULL; /* we don't support munging link-layer headers */
449 p->getnonblock_op = sf_getnonblock;
450 p->setnonblock_op = sf_setnonblock;
451 p->stats_op = sf_stats;
453 p->stats_ex_op = sf_stats_ex;
454 p->setbuff_op = sf_setbuff;
455 p->setmode_op = sf_setmode;
456 p->setmintocopy_op = sf_setmintocopy;
457 p->getevent_op = sf_getevent;
458 p->oid_get_request_op = sf_oid_get_request;
459 p->oid_set_request_op = sf_oid_set_request;
460 p->sendqueue_transmit_op = sf_sendqueue_transmit;
461 p->setuserbuffer_op = sf_setuserbuffer;
462 p->live_dump_op = sf_live_dump;
463 p->live_dump_ended_op = sf_live_dump_ended;
464 p->get_airpcap_handle_op = sf_get_airpcap_handle;
468 * For offline captures, the standard one-shot callback can
469 * be used for pcap_next()/pcap_next_ex().
471 p->oneshot_callback = pcap_oneshot;
474 * Savefiles never require special BPF code generation.
476 p->bpf_codegen_flags = 0;
487 pcap_fopen_offline(FILE *fp, char *errbuf)
489 return (pcap_fopen_offline_with_tstamp_precision(fp,
490 PCAP_TSTAMP_PRECISION_MICRO, errbuf));
494 * Read packets from a capture file, and call the callback for each
496 * If cnt > 0, return after 'cnt' packets, otherwise continue until eof.
499 pcap_offline_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
501 struct bpf_insn *fcode;
506 while (status == 0) {
507 struct pcap_pkthdr h;
510 * Has "pcap_breakloop()" been called?
511 * If so, return immediately - if we haven't read any
512 * packets, clear the flag and return -2 to indicate
513 * that we were told to break out of the loop, otherwise
514 * leave the flag set, so that the *next* call will break
515 * out of the loop without having read any packets, and
516 * return the number of packets we've processed so far.
526 status = p->next_packet_op(p, &h, &data);
533 if ((fcode = p->fcode.bf_insns) == NULL ||
534 bpf_filter(fcode, data, h.len, h.caplen)) {
535 (*callback)(user, &h, data);
536 if (++n >= cnt && cnt > 0)
540 /*XXX this breaks semantics tcpslice expects */