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.
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)";
41 #include <pcap-stdinc.h>
48 #ifdef HAVE_SYS_BITYPES_H
49 #include <sys/bitypes.h>
51 #include <sys/types.h>
63 #ifdef HAVE_OS_PROTO_H
68 #include "sf-pcap-ng.h"
71 * Setting O_BINARY on DOS/Windows is a bit tricky
74 #define SET_BINMODE(f) _setmode(_fileno(f), _O_BINARY)
76 #if defined(__HIGHC__)
77 #define SET_BINMODE(f) setmode(f, O_BINARY)
79 #define SET_BINMODE(f) setmode(fileno(f), O_BINARY)
84 sf_getnonblock(pcap_t
*p
, char *errbuf
)
87 * This is a savefile, not a live capture file, so never say
88 * it's in non-blocking mode.
94 sf_setnonblock(pcap_t
*p
, int nonblock
, char *errbuf
)
97 * This is a savefile, not a live capture file, so reject
98 * requests to put it in non-blocking mode. (If it's a
99 * pipe, it could be put in non-blocking mode, but that
100 * would significantly complicate the code to read packets,
101 * as it would have to handle reading partial packets and
102 * keeping the state of the read.)
104 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
105 "Savefiles cannot be put into non-blocking mode");
110 sf_stats(pcap_t
*p
, struct pcap_stat
*ps
)
112 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
113 "Statistics aren't available from savefiles");
119 sf_setbuff(pcap_t
*p
, int dim
)
121 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
122 "The kernel buffer size cannot be set while reading from a file");
127 sf_setmode(pcap_t
*p
, int mode
)
129 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
130 "impossible to set mode while reading from a file");
135 sf_setmintocopy(pcap_t
*p
, int size
)
137 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
138 "The mintocopy parameter cannot be set while reading from a file");
144 sf_inject(pcap_t
*p
, const void *buf _U_
, size_t size _U_
)
146 strlcpy(p
->errbuf
, "Sending packets isn't supported on savefiles",
152 * Set direction flag: Which packets do we accept on a forwarding
153 * single device? IN, OUT or both?
156 sf_setdirection(pcap_t
*p
, pcap_direction_t d
)
158 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
159 "Setting direction is not supported on savefiles");
164 sf_cleanup(pcap_t
*p
)
166 if (p
->sf
.rfile
!= stdin
)
167 (void)fclose(p
->sf
.rfile
);
168 if (p
->buffer
!= NULL
)
170 pcap_freecode(&p
->fcode
);
174 pcap_open_offline(const char *fname
, char *errbuf
)
179 if (fname
[0] == '-' && fname
[1] == '\0')
182 #if defined(WIN32) || defined(MSDOS)
184 * We're reading from the standard input, so put it in binary
185 * mode, as savefiles are binary files.
191 #if !defined(WIN32) && !defined(MSDOS)
192 fp
= fopen(fname
, "r");
194 fp
= fopen(fname
, "rb");
197 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "%s: %s", fname
,
198 pcap_strerror(errno
));
202 p
= pcap_fopen_offline(fp
, errbuf
);
211 pcap_t
* pcap_hopen_offline(intptr_t osfd
, char *errbuf
)
216 fd
= _open_osfhandle(osfd
, _O_RDONLY
);
219 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, pcap_strerror(errno
));
223 file
= _fdopen(fd
, "rb");
226 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, pcap_strerror(errno
));
230 return pcap_fopen_offline(file
, errbuf
);
234 static int (*check_headers
[])(pcap_t
*, bpf_u_int32
, FILE *, char *) = {
239 #define N_FILE_TYPES (sizeof check_headers / sizeof check_headers[0])
245 pcap_fopen_offline(FILE *fp
, char *errbuf
)
252 p
= pcap_create_common("(savefile)", errbuf
);
257 * Read the first 4 bytes of the file; the network analyzer dump
258 * file formats we support (pcap and pcap-ng), and several other
259 * formats we might support in the future (such as snoop, DOS and
260 * Windows Sniffer, and Microsoft Network Monitor) all have magic
261 * numbers that are unique in their first 4 bytes.
263 amt_read
= fread((char *)&magic
, 1, sizeof(magic
), fp
);
264 if (amt_read
!= sizeof(magic
)) {
266 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
267 "error reading dump file: %s",
268 pcap_strerror(errno
));
270 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
271 "truncated dump file; tried to read %lu file header bytes, only got %lu",
272 (unsigned long)sizeof(magic
),
273 (unsigned long)amt_read
);
279 * Try all file types.
281 for (i
= 0; i
< N_FILE_TYPES
; i
++) {
282 switch ((*check_headers
[i
])(p
, magic
, fp
, errbuf
)) {
286 * Error trying to read the header.
299 * Well, who knows what this mess is....
301 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "unknown file format");
308 /* Padding only needed for live capture fcode */
312 #if !defined(WIN32) && !defined(MSDOS)
314 * You can do "select()" and "poll()" on plain files on most
315 * platforms, and should be able to do so on pipes.
317 * You can't do "select()" on anything other than sockets in
318 * Windows, so, on Win32 systems, we don't have "selectable_fd".
320 p
->selectable_fd
= fileno(fp
);
323 p
->read_op
= pcap_offline_read
;
324 p
->inject_op
= sf_inject
;
325 p
->setfilter_op
= install_bpf_program
;
326 p
->setdirection_op
= sf_setdirection
;
327 p
->set_datalink_op
= NULL
; /* we don't support munging link-layer headers */
328 p
->getnonblock_op
= sf_getnonblock
;
329 p
->setnonblock_op
= sf_setnonblock
;
330 p
->stats_op
= sf_stats
;
332 p
->setbuff_op
= sf_setbuff
;
333 p
->setmode_op
= sf_setmode
;
334 p
->setmintocopy_op
= sf_setmintocopy
;
336 p
->cleanup_op
= sf_cleanup
;
346 * Read packets from a capture file, and call the callback for each
348 * If cnt > 0, return after 'cnt' packets, otherwise continue until eof.
351 pcap_offline_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
353 struct bpf_insn
*fcode
;
359 static int samp_npkt
; // parameter needed for sampling, whtn '1 out of N' method has been requested
360 static struct timeval samp_time
; // parameter needed for sampling, whtn '1 every N ms' method has been requested
361 #endif /* HAVE_REMOTE */
363 while (status
== 0) {
364 struct pcap_pkthdr h
;
367 * Has "pcap_breakloop()" been called?
368 * If so, return immediately - if we haven't read any
369 * packets, clear the flag and return -2 to indicate
370 * that we were told to break out of the loop, otherwise
371 * leave the flag set, so that the *next* call will break
372 * out of the loop without having read any packets, and
373 * return the number of packets we've processed so far.
383 status
= p
->sf
.next_packet_op(p
, &h
, &data
);
390 if ((fcode
= p
->fcode
.bf_insns
) == NULL
||
391 bpf_filter(fcode
, data
, h
.len
, h
.caplen
)) {
392 (*callback
)(user
, &h
, data
);
393 if (++n
>= cnt
&& cnt
> 0)
397 /*XXX this breaks semantics tcpslice expects */