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.
36 #include <pcap-stdinc.h>
43 #ifdef HAVE_SYS_BITYPES_H
44 #include <sys/bitypes.h>
46 #include <sys/types.h>
58 #ifdef HAVE_OS_PROTO_H
63 #include "sf-pcap-ng.h"
66 * Setting O_BINARY on DOS/Windows is a bit tricky
69 #define SET_BINMODE(f) _setmode(_fileno(f), _O_BINARY)
71 #if defined(__HIGHC__)
72 #define SET_BINMODE(f) setmode(f, O_BINARY)
74 #define SET_BINMODE(f) setmode(fileno(f), O_BINARY)
79 sf_getnonblock(pcap_t
*p
, char *errbuf
)
82 * This is a savefile, not a live capture file, so never say
83 * it's in non-blocking mode.
89 sf_setnonblock(pcap_t
*p
, int nonblock
, char *errbuf
)
92 * This is a savefile, not a live capture file, so reject
93 * requests to put it in non-blocking mode. (If it's a
94 * pipe, it could be put in non-blocking mode, but that
95 * would significantly complicate the code to read packets,
96 * as it would have to handle reading partial packets and
97 * keeping the state of the read.)
99 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
100 "Savefiles cannot be put into non-blocking mode");
105 sf_stats(pcap_t
*p
, struct pcap_stat
*ps
)
107 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
108 "Statistics aren't available from savefiles");
114 sf_setbuff(pcap_t
*p
, int dim
)
116 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
117 "The kernel buffer size cannot be set while reading from a file");
122 sf_setmode(pcap_t
*p
, int mode
)
124 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
125 "impossible to set mode while reading from a file");
130 sf_setmintocopy(pcap_t
*p
, int size
)
132 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
133 "The mintocopy parameter cannot be set while reading from a file");
139 sf_inject(pcap_t
*p
, const void *buf _U_
, size_t size _U_
)
141 strlcpy(p
->errbuf
, "Sending packets isn't supported on savefiles",
147 * Set direction flag: Which packets do we accept on a forwarding
148 * single device? IN, OUT or both?
151 sf_setdirection(pcap_t
*p
, pcap_direction_t d
)
153 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
154 "Setting direction is not supported on savefiles");
159 sf_cleanup(pcap_t
*p
)
161 if (p
->rfile
!= stdin
)
162 (void)fclose(p
->rfile
);
163 if (p
->buffer
!= NULL
)
165 pcap_freecode(&p
->fcode
);
169 pcap_open_offline_with_tstamp_precision(const char *fname
, u_int precision
,
175 if (fname
[0] == '-' && fname
[1] == '\0')
178 #if defined(WIN32) || defined(MSDOS)
180 * We're reading from the standard input, so put it in binary
181 * mode, as savefiles are binary files.
187 #if !defined(WIN32) && !defined(MSDOS)
188 fp
= fopen(fname
, "r");
190 fp
= fopen(fname
, "rb");
193 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "%s: %s", fname
,
194 pcap_strerror(errno
));
198 p
= pcap_fopen_offline_with_tstamp_precision(fp
, precision
, errbuf
);
207 pcap_open_offline(const char *fname
, char *errbuf
)
209 return (pcap_open_offline_with_tstamp_precision(fname
,
210 PCAP_TSTAMP_PRECISION_MICRO
, errbuf
));
214 pcap_t
* pcap_hopen_offline_with_tstamp_precision(intptr_t osfd
, u_int precision
,
220 fd
= _open_osfhandle(osfd
, _O_RDONLY
);
223 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, pcap_strerror(errno
));
227 file
= _fdopen(fd
, "rb");
230 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, pcap_strerror(errno
));
234 return pcap_fopen_offline_with_tstamp_precision(file
, precision
,
238 pcap_t
* pcap_hopen_offline(intptr_t osfd
, char *errbuf
)
240 return pcap_hopen_offline_with_tstamp_precision(osfd
,
241 PCAP_TSTAMP_PRECISION_MICRO
, errbuf
);
245 static pcap_t
*(*check_headers
[])(bpf_u_int32
, FILE *, u_int
, char *, int *) = {
250 #define N_FILE_TYPES (sizeof check_headers / sizeof check_headers[0])
256 pcap_fopen_offline_with_tstamp_precision(FILE *fp
, u_int precision
,
266 * Read the first 4 bytes of the file; the network analyzer dump
267 * file formats we support (pcap and pcap-ng), and several other
268 * formats we might support in the future (such as snoop, DOS and
269 * Windows Sniffer, and Microsoft Network Monitor) all have magic
270 * numbers that are unique in their first 4 bytes.
272 amt_read
= fread((char *)&magic
, 1, sizeof(magic
), fp
);
273 if (amt_read
!= sizeof(magic
)) {
275 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
276 "error reading dump file: %s",
277 pcap_strerror(errno
));
279 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
280 "truncated dump file; tried to read %lu file header bytes, only got %lu",
281 (unsigned long)sizeof(magic
),
282 (unsigned long)amt_read
);
288 * Try all file types.
290 for (i
= 0; i
< N_FILE_TYPES
; i
++) {
291 p
= (*check_headers
[i
])(magic
, fp
, precision
, errbuf
, &err
);
293 /* Yup, that's it. */
298 * Error trying to read the header.
305 * Well, who knows what this mess is....
307 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "unknown file format");
313 /* Padding only needed for live capture fcode */
316 #if !defined(WIN32) && !defined(MSDOS)
318 * You can do "select()" and "poll()" on plain files on most
319 * platforms, and should be able to do so on pipes.
321 * You can't do "select()" on anything other than sockets in
322 * Windows, so, on Win32 systems, we don't have "selectable_fd".
324 p
->selectable_fd
= fileno(fp
);
327 p
->read_op
= pcap_offline_read
;
328 p
->inject_op
= sf_inject
;
329 p
->setfilter_op
= install_bpf_program
;
330 p
->setdirection_op
= sf_setdirection
;
331 p
->set_datalink_op
= NULL
; /* we don't support munging link-layer headers */
332 p
->getnonblock_op
= sf_getnonblock
;
333 p
->setnonblock_op
= sf_setnonblock
;
334 p
->stats_op
= sf_stats
;
336 p
->setbuff_op
= sf_setbuff
;
337 p
->setmode_op
= sf_setmode
;
338 p
->setmintocopy_op
= sf_setmintocopy
;
342 * For offline captures, the standard one-shot callback can
343 * be used for pcap_next()/pcap_next_ex().
345 p
->oneshot_callback
= pcap_oneshot
;
348 * Savefiles never require special BPF code generation.
350 p
->bpf_codegen_flags
= 0;
361 pcap_fopen_offline(FILE *fp
, char *errbuf
)
363 return (pcap_fopen_offline_with_tstamp_precision(fp
,
364 PCAP_TSTAMP_PRECISION_MICRO
, errbuf
));
368 * Read packets from a capture file, and call the callback for each
370 * If cnt > 0, return after 'cnt' packets, otherwise continue until eof.
373 pcap_offline_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
375 struct bpf_insn
*fcode
;
380 while (status
== 0) {
381 struct pcap_pkthdr h
;
384 * Has "pcap_breakloop()" been called?
385 * If so, return immediately - if we haven't read any
386 * packets, clear the flag and return -2 to indicate
387 * that we were told to break out of the loop, otherwise
388 * leave the flag set, so that the *next* call will break
389 * out of the loop without having read any packets, and
390 * return the number of packets we've processed so far.
400 status
= p
->next_packet_op(p
, &h
, &data
);
407 if ((fcode
= p
->fcode
.bf_insns
) == NULL
||
408 bpf_filter(fcode
, data
, h
.len
, h
.caplen
)) {
409 (*callback
)(user
, &h
, data
);
410 if (++n
>= cnt
&& cnt
> 0)
414 /*XXX this breaks semantics tcpslice expects */