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.
33 #include <pcap-types.h>
44 #include <limits.h> /* for INT_MAX */
48 #ifdef HAVE_OS_PROTO_H
53 #include "sf-pcapng.h"
54 #include "pcap-common.h"
59 * This isn't exported on Windows, because it would only work if both
60 * WinPcap/Npcap and the code using it were to use the Universal CRT; otherwise,
61 * a FILE structure in WinPcap/Npcap and a FILE structure in the code using it
62 * could be different if they're using different versions of the C runtime.
64 * Instead, pcap/pcap.h defines it as a macro that wraps the hopen version,
65 * with the wrapper calling _fileno() and _get_osfhandle() themselves,
66 * so that it convert the appropriate CRT version's FILE structure to
67 * a HANDLE (which is OS-defined, not CRT-defined, and is part of the Win32
70 static pcap_t
*pcap_fopen_offline_with_tstamp_precision(FILE *, u_int
, char *);
74 * Setting O_BINARY on Windows is a bit tricky.
77 #define SET_BINMODE(f) _setmode(_fileno(f), _O_BINARY)
81 sf_getnonblock(pcap_t
*p _U_
)
84 * This is a savefile, not a live capture file, so never say
85 * it's in non-blocking mode.
91 sf_setnonblock(pcap_t
*p
, int nonblock _U_
)
94 * This is a savefile, not a live capture file, so reject
95 * requests to put it in non-blocking mode. (If it's a
96 * pipe, it could be put in non-blocking mode, but that
97 * would significantly complicate the code to read packets,
98 * as it would have to handle reading partial packets and
99 * keeping the state of the read.)
101 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
102 "Savefiles cannot be put into non-blocking mode");
107 sf_cant_set_rfmon(pcap_t
*p _U_
)
110 * This is a savefile, not a device on which you can capture,
111 * so never say it supports being put into monitor mode.
117 sf_stats(pcap_t
*p
, struct pcap_stat
*ps _U_
)
119 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
120 "Statistics aren't available from savefiles");
125 static struct pcap_stat
*
126 sf_stats_ex(pcap_t
*p
, int *size _U_
)
128 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
129 "Statistics aren't available from savefiles");
134 sf_setbuff(pcap_t
*p
, int dim _U_
)
136 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
137 "The kernel buffer size cannot be set while reading from a file");
142 sf_setmode(pcap_t
*p
, int mode _U_
)
144 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
145 "impossible to set mode while reading from a file");
150 sf_setmintocopy(pcap_t
*p
, int size _U_
)
152 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
153 "The mintocopy parameter cannot be set while reading from a file");
158 sf_getevent(pcap_t
*pcap
)
160 (void)snprintf(pcap
->errbuf
, sizeof(pcap
->errbuf
),
161 "The read event cannot be retrieved while reading from a file");
162 return (INVALID_HANDLE_VALUE
);
166 sf_oid_get_request(pcap_t
*p
, bpf_u_int32 oid _U_
, void *data _U_
,
169 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
170 "An OID get request cannot be performed on a file");
175 sf_oid_set_request(pcap_t
*p
, bpf_u_int32 oid _U_
, const void *data _U_
,
178 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
179 "An OID set request cannot be performed on a file");
184 sf_sendqueue_transmit(pcap_t
*p
, pcap_send_queue
*queue _U_
, int sync _U_
)
186 pcapint_strlcpy(p
->errbuf
, "Sending packets isn't supported on savefiles",
192 sf_setuserbuffer(pcap_t
*p
, int size _U_
)
194 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
195 "The user buffer cannot be set when reading from a file");
200 sf_live_dump(pcap_t
*p
, char *filename _U_
, int maxsize _U_
, int maxpacks _U_
)
202 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
203 "Live packet dumping cannot be performed when reading from a file");
208 sf_live_dump_ended(pcap_t
*p
, int sync _U_
)
210 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
211 "Live packet dumping cannot be performed on a pcap_open_dead pcap_t");
217 sf_inject(pcap_t
*p
, const void *buf _U_
, int size _U_
)
219 pcapint_strlcpy(p
->errbuf
, "Sending packets isn't supported on savefiles",
225 * Set direction flag: Which packets do we accept on a forwarding
226 * single device? IN, OUT or both?
229 sf_setdirection(pcap_t
*p
, pcap_direction_t d _U_
)
231 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
232 "Setting direction is not supported on savefiles");
237 pcapint_sf_cleanup(pcap_t
*p
)
239 if (p
->rfile
!= stdin
)
240 (void)fclose(p
->rfile
);
241 if (p
->buffer
!= NULL
)
243 pcap_freecode(&p
->fcode
);
248 * Wrapper for fopen() and _wfopen().
250 * If we're in UTF-8 mode, map the pathname from UTF-8 to UTF-16LE and
253 * If we're not, just use fopen(); that'll treat it as being in the
257 pcapint_charset_fopen(const char *path
, const char *mode
)
260 #define MAX_MODE_LEN 16
261 wchar_t utf16_mode
[MAX_MODE_LEN
+1];
267 if (pcapint_utf_8_mode
) {
269 * Map from UTF-8 to UTF-16LE.
270 * Fail if there are invalid characters in the input
271 * string, rather than converting them to REPLACEMENT
272 * CHARACTER; the latter is appropriate for strings
273 * to be displayed to the user, but for file names
274 * you just want the attempt to open the file to fail.
276 utf16_path
= cp_to_utf_16le(CP_UTF8
, path
,
277 MB_ERR_INVALID_CHARS
);
278 if (utf16_path
== NULL
) {
280 * Error. Assume errno has been set.
282 * XXX - what about Windows errors?
288 * Now convert the mode to UTF-16LE as well.
289 * We assume the mode is ASCII, and that
290 * it's short, so that's easy.
292 for (i
= 0; (c
= *mode
) != '\0'; i
++, mode
++) {
294 /* Not an ASCII character; fail with EINVAL. */
299 if (i
>= MAX_MODE_LEN
) {
300 /* The mode string is longer than we allow. */
307 utf16_mode
[i
] = '\0';
310 * OK, we have UTF-16LE strings; hand them to
313 fp
= _wfopen(utf16_path
, utf16_mode
);
316 * Make sure freeing the UTF-16LE string doesn't
317 * overwrite the error code we got from _wfopen().
326 * This takes strings in the local code page as an
329 return (fopen(path
, mode
));
335 pcap_open_offline_with_tstamp_precision(const char *fname
, u_int precision
,
342 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
343 "A null pointer was supplied as the file name");
346 if (fname
[0] == '-' && fname
[1] == '\0')
350 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
351 "The standard input is not open");
356 * We're reading from the standard input, so put it in binary
357 * mode, as savefiles are binary files.
364 * Use pcapint_charset_fopen(); on Windows, it tests whether we're
365 * in "local code page" or "UTF-8" mode, and treats the
366 * pathname appropriately, and on other platforms, it just
369 * "b" is supported as of C90, so *all* UN*Xes should
370 * support it, even though it does nothing.
372 fp
= pcapint_charset_fopen(fname
, "rb");
374 pcapint_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
379 p
= pcap_fopen_offline_with_tstamp_precision(fp
, precision
, errbuf
);
388 pcap_open_offline(const char *fname
, char *errbuf
)
390 return (pcap_open_offline_with_tstamp_precision(fname
,
391 PCAP_TSTAMP_PRECISION_MICRO
, errbuf
));
395 pcap_t
* pcap_hopen_offline_with_tstamp_precision(intptr_t osfd
, u_int precision
,
401 fd
= _open_osfhandle(osfd
, _O_RDONLY
);
404 pcapint_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
405 errno
, "_open_osfhandle");
409 file
= _fdopen(fd
, "rb");
412 pcapint_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
418 return pcap_fopen_offline_with_tstamp_precision(file
, precision
,
422 pcap_t
* pcap_hopen_offline(intptr_t osfd
, char *errbuf
)
424 return pcap_hopen_offline_with_tstamp_precision(osfd
,
425 PCAP_TSTAMP_PRECISION_MICRO
, errbuf
);
430 * Given a link-layer header type and snapshot length, return a
431 * snapshot length to use when reading the file; it's guaranteed
432 * to be > 0 and <= INT_MAX.
434 * XXX - the only reason why we limit it to <= INT_MAX is so that
435 * it fits in p->snapshot, and the only reason that p->snapshot is
436 * signed is that pcap_snapshot() returns an int, not an unsigned int.
439 pcapint_adjust_snapshot(bpf_u_int32 linktype
, bpf_u_int32 snaplen
)
441 if (snaplen
== 0 || snaplen
> INT_MAX
) {
443 * Bogus snapshot length; use the maximum for this
444 * link-layer type as a fallback.
446 * XXX - we don't clamp snapshot lengths that are
447 * <= INT_MAX but > max_snaplen_for_dlt(linktype),
448 * so a capture file could cause us to allocate
449 * a Really Big Buffer.
451 snaplen
= max_snaplen_for_dlt(linktype
);
456 static pcap_t
*(*check_headers
[])(const uint8_t *, FILE *, u_int
, char *, int *) = {
461 #define N_FILE_TYPES (sizeof check_headers / sizeof check_headers[0])
467 pcap_fopen_offline_with_tstamp_precision(FILE *fp
, u_int precision
,
477 * Fail if we were passed a NULL fp.
479 * That shouldn't happen if we're opening with a path name, but
480 * it could happen if buggy code is opening with a FILE * and
481 * didn't bother to make sure the FILE * isn't null.
484 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
485 "Null FILE * pointer provided to savefile open routine");
490 * Read the first 4 bytes of the file; the network analyzer dump
491 * file formats we support (pcap and pcapng), and several other
492 * formats we might support in the future (such as snoop, DOS and
493 * Windows Sniffer, and Microsoft Network Monitor) all have magic
494 * numbers that are unique in their first 4 bytes.
496 amt_read
= fread(&magic
, 1, sizeof(magic
), fp
);
497 if (amt_read
!= sizeof(magic
)) {
499 pcapint_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
500 errno
, "error reading dump file");
502 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
503 "truncated dump file; tried to read %zu file header bytes, only got %zu",
504 sizeof(magic
), amt_read
);
510 * Try all file types.
512 for (i
= 0; i
< N_FILE_TYPES
; i
++) {
513 p
= (*check_headers
[i
])(magic
, fp
, precision
, errbuf
, &err
);
515 /* Yup, that's it. */
520 * Error trying to read the header.
527 * Well, who knows what this mess is....
529 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "unknown file format");
535 /* Padding only needed for live capture fcode */
540 * You can do "select()" and "poll()" on plain files on most
541 * platforms, and should be able to do so on pipes.
543 * You can't do "select()" on anything other than sockets in
544 * Windows, so, on Win32 systems, we don't have "selectable_fd".
546 p
->selectable_fd
= fileno(fp
);
549 p
->can_set_rfmon_op
= sf_cant_set_rfmon
;
550 p
->read_op
= pcapint_offline_read
;
551 p
->inject_op
= sf_inject
;
552 p
->setfilter_op
= pcapint_install_bpf_program
;
553 p
->setdirection_op
= sf_setdirection
;
554 p
->set_datalink_op
= NULL
; /* we don't support munging link-layer headers */
555 p
->getnonblock_op
= sf_getnonblock
;
556 p
->setnonblock_op
= sf_setnonblock
;
557 p
->stats_op
= sf_stats
;
559 p
->stats_ex_op
= sf_stats_ex
;
560 p
->setbuff_op
= sf_setbuff
;
561 p
->setmode_op
= sf_setmode
;
562 p
->setmintocopy_op
= sf_setmintocopy
;
563 p
->getevent_op
= sf_getevent
;
564 p
->oid_get_request_op
= sf_oid_get_request
;
565 p
->oid_set_request_op
= sf_oid_set_request
;
566 p
->sendqueue_transmit_op
= sf_sendqueue_transmit
;
567 p
->setuserbuffer_op
= sf_setuserbuffer
;
568 p
->live_dump_op
= sf_live_dump
;
569 p
->live_dump_ended_op
= sf_live_dump_ended
;
573 * For offline captures, the standard one-shot callback can
574 * be used for pcap_next()/pcap_next_ex().
576 p
->oneshot_callback
= pcapint_oneshot
;
579 * Default breakloop operation.
581 p
->breakloop_op
= pcapint_breakloop_common
;
584 * Savefiles never require special BPF code generation.
586 p
->bpf_codegen_flags
= 0;
594 * This isn't needed on Windows; we #define pcap_fopen_offline() as
595 * a wrapper around pcap_hopen_offline(), and we don't call it from
596 * inside this file, so it's unused.
600 pcap_fopen_offline(FILE *fp
, char *errbuf
)
602 return (pcap_fopen_offline_with_tstamp_precision(fp
,
603 PCAP_TSTAMP_PRECISION_MICRO
, errbuf
));
608 * Read packets from a capture file, and call the callback for each
610 * If cnt > 0, return after 'cnt' packets, otherwise continue until eof.
613 pcapint_offline_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
615 struct bpf_insn
*fcode
;
620 * This can conceivably process more than INT_MAX packets,
621 * which would overflow the packet count, causing it either
622 * to look like a negative number, and thus cause us to
623 * return a value that looks like an error, or overflow
624 * back into positive territory, and thus cause us to
625 * return a too-low count.
627 * Therefore, if the packet count is unlimited, we clip
628 * it at INT_MAX; this routine is not expected to
629 * process packets indefinitely, so that's not an issue.
631 if (PACKET_COUNT_IS_UNLIMITED(cnt
))
635 struct pcap_pkthdr h
;
639 * Has "pcap_breakloop()" been called?
640 * If so, return immediately - if we haven't read any
641 * packets, clear the flag and return -2 to indicate
642 * that we were told to break out of the loop, otherwise
643 * leave the flag set, so that the *next* call will break
644 * out of the loop without having read any packets, and
645 * return the number of packets we've processed so far.
655 status
= p
->next_packet_op(p
, &h
, &data
);
658 * Error. Pass it back to the caller.
664 * EOF. Nothing more to process;
670 * OK, we've read a packet; run it through the filter
671 * and, if it passes, process it.
673 if ((fcode
= p
->fcode
.bf_insns
) == NULL
||
674 pcapint_filter(fcode
, data
, h
.len
, h
.caplen
)) {
675 (*callback
)(user
, &h
, data
);
676 n
++; /* count the packet */
681 /*XXX this breaks semantics tcpslice expects */