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 * sf-pcap.c - libpcap-file-format-specific code from savefile.c
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 #include "pcap-common.h"
52 #ifdef HAVE_OS_PROTO_H
59 * Setting O_BINARY on DOS/Windows is a bit tricky
62 #define SET_BINMODE(f) _setmode(_fileno(f), _O_BINARY)
64 #if defined(__HIGHC__)
65 #define SET_BINMODE(f) setmode(f, O_BINARY)
67 #define SET_BINMODE(f) setmode(fileno(f), O_BINARY)
72 * Standard libpcap format.
74 #define TCPDUMP_MAGIC 0xa1b2c3d4
77 * Alexey Kuznetzov's modified libpcap format.
79 #define KUZNETZOV_TCPDUMP_MAGIC 0xa1b2cd34
82 * Reserved for Francisco Mesquita <francisco.mesquita@radiomovel.pt>
83 * for another modified format.
85 #define FMESQUITA_TCPDUMP_MAGIC 0xa1b234cd
88 * Navtel Communcations' format, with nanosecond timestamps,
89 * as per a request from Dumas Hwang <dumas.hwang@navtelcom.com>.
91 #define NAVTEL_TCPDUMP_MAGIC 0xa12b3c4d
94 * Normal libpcap format, except for seconds/nanoseconds timestamps,
95 * as per a request by Ulf Lamping <ulf.lamping@web.de>
97 #define NSEC_TCPDUMP_MAGIC 0xa1b23c4d
100 * Mechanism for storing information about a capture in the upper
101 * 6 bits of a linktype value in a capture file.
103 * LT_LINKTYPE_EXT(x) extracts the additional information.
105 * The rest of the bits are for a value describing the link-layer
106 * value. LT_LINKTYPE(x) extracts that value.
108 #define LT_LINKTYPE(x) ((x) & 0x03FFFFFF)
109 #define LT_LINKTYPE_EXT(x) ((x) & 0xFC000000)
111 static int pcap_next_packet(pcap_t
*p
, struct pcap_pkthdr
*hdr
, u_char
**datap
);
115 * This isn't exported on Windows, because it would only work if both
116 * libpcap and the code using it were using the same C runtime; otherwise they
117 * would be using different definitions of a FILE structure.
119 * Instead we define this as a macro in pcap/pcap.h that wraps the hopen
120 * version that we do export, passing it a raw OS HANDLE, as defined by the
121 * Win32 / Win64 ABI, obtained from the _fileno() and _get_osfhandle()
122 * functions of the appropriate CRT.
124 static pcap_dumper_t
*pcap_dump_fopen(pcap_t
*p
, FILE *f
);
128 * Private data for reading pcap savefiles.
140 } tstamp_scale_type_t
;
144 swapped_type_t lengths_swapped
;
145 tstamp_scale_type_t scale_type
;
149 * Check whether this is a pcap savefile and, if it is, extract the
150 * relevant information from the header.
153 pcap_check_header(const uint8_t *magic
, FILE *fp
, u_int precision
, char *errbuf
,
156 bpf_u_int32 magic_int
;
157 struct pcap_file_header hdr
;
164 * Assume no read errors.
169 * Check whether the first 4 bytes of the file are the magic
170 * number for a pcap savefile, or for a byte-swapped pcap
173 memcpy(&magic_int
, magic
, sizeof(magic_int
));
174 if (magic_int
!= TCPDUMP_MAGIC
&&
175 magic_int
!= KUZNETZOV_TCPDUMP_MAGIC
&&
176 magic_int
!= NSEC_TCPDUMP_MAGIC
) {
177 magic_int
= SWAPLONG(magic_int
);
178 if (magic_int
!= TCPDUMP_MAGIC
&&
179 magic_int
!= KUZNETZOV_TCPDUMP_MAGIC
&&
180 magic_int
!= NSEC_TCPDUMP_MAGIC
)
181 return (NULL
); /* nope */
186 * They are. Put the magic number in the header, and read
187 * the rest of the header.
189 hdr
.magic
= magic_int
;
190 amt_read
= fread(((char *)&hdr
) + sizeof hdr
.magic
, 1,
191 sizeof(hdr
) - sizeof(hdr
.magic
), fp
);
192 if (amt_read
!= sizeof(hdr
) - sizeof(hdr
.magic
)) {
194 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
195 errno
, "error reading dump file");
197 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
198 "truncated dump file; tried to read %" PRIsize
" file header bytes, only got %" PRIsize
,
199 sizeof(hdr
), amt_read
);
206 * If it's a byte-swapped capture file, byte-swap the header.
209 hdr
.version_major
= SWAPSHORT(hdr
.version_major
);
210 hdr
.version_minor
= SWAPSHORT(hdr
.version_minor
);
211 hdr
.thiszone
= SWAPLONG(hdr
.thiszone
);
212 hdr
.sigfigs
= SWAPLONG(hdr
.sigfigs
);
213 hdr
.snaplen
= SWAPLONG(hdr
.snaplen
);
214 hdr
.linktype
= SWAPLONG(hdr
.linktype
);
217 if (hdr
.version_major
< PCAP_VERSION_MAJOR
) {
218 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
219 "archaic pcap savefile format");
225 * currently only versions 2.[0-4] are supported with
226 * the exception of 543.0 for DG/UX tcpdump.
228 if (! ((hdr
.version_major
== PCAP_VERSION_MAJOR
&&
229 hdr
.version_minor
<= PCAP_VERSION_MINOR
) ||
230 (hdr
.version_major
== 543 &&
231 hdr
.version_minor
== 0))) {
232 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
233 "unsupported pcap savefile version %u.%u",
234 hdr
.version_major
, hdr
.version_minor
);
240 * OK, this is a good pcap file.
241 * Allocate a pcap_t for it.
243 p
= pcap_open_offline_common(errbuf
, sizeof (struct pcap_sf
));
245 /* Allocation failed. */
249 p
->swapped
= swapped
;
250 p
->version_major
= hdr
.version_major
;
251 p
->version_minor
= hdr
.version_minor
;
252 p
->snapshot
= hdr
.snaplen
;
253 if (p
->snapshot
<= 0) {
255 * Bogus snapshot length; use the maximum for this
256 * link-layer type as a fallback.
258 * XXX - the only reason why snapshot is signed is
259 * that pcap_snapshot() returns an int, not an
262 p
->snapshot
= max_snaplen_for_dlt(hdr
.linktype
);
264 p
->linktype
= linktype_to_dlt(LT_LINKTYPE(hdr
.linktype
));
265 p
->linktype_ext
= LT_LINKTYPE_EXT(hdr
.linktype
);
267 p
->next_packet_op
= pcap_next_packet
;
271 p
->opt
.tstamp_precision
= precision
;
274 * Will we need to scale the timestamps to match what the
279 case PCAP_TSTAMP_PRECISION_MICRO
:
280 if (magic_int
== NSEC_TCPDUMP_MAGIC
) {
282 * The file has nanoseconds, the user
283 * wants microseconds; scale the
286 ps
->scale_type
= SCALE_DOWN
;
289 * The file has microseconds, the
290 * user wants microseconds; nothing to do.
292 ps
->scale_type
= PASS_THROUGH
;
296 case PCAP_TSTAMP_PRECISION_NANO
:
297 if (magic_int
== NSEC_TCPDUMP_MAGIC
) {
299 * The file has nanoseconds, the
300 * user wants nanoseconds; nothing to do.
302 ps
->scale_type
= PASS_THROUGH
;
305 * The file has microoseconds, the user
306 * wants nanoseconds; scale the
309 ps
->scale_type
= SCALE_UP
;
314 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
315 "unknown time stamp resolution %u", precision
);
322 * We interchanged the caplen and len fields at version 2.3,
323 * in order to match the bpf header layout. But unfortunately
324 * some files were written with version 2.3 in their headers
325 * but without the interchanged fields.
327 * In addition, DG/UX tcpdump writes out files with a version
328 * number of 543.0, and with the caplen and len fields in the
331 switch (hdr
.version_major
) {
334 if (hdr
.version_minor
< 3)
335 ps
->lengths_swapped
= SWAPPED
;
336 else if (hdr
.version_minor
== 3)
337 ps
->lengths_swapped
= MAYBE_SWAPPED
;
339 ps
->lengths_swapped
= NOT_SWAPPED
;
343 ps
->lengths_swapped
= SWAPPED
;
347 ps
->lengths_swapped
= NOT_SWAPPED
;
351 if (magic_int
== KUZNETZOV_TCPDUMP_MAGIC
) {
353 * XXX - the patch that's in some versions of libpcap
354 * changes the packet header but not the magic number,
355 * and some other versions with this magic number have
356 * some extra debugging information in the packet header;
357 * we'd have to use some hacks^H^H^H^H^Hheuristics to
358 * detect those variants.
360 * Ethereal does that, but it does so by trying to read
361 * the first two packets of the file with each of the
362 * record header formats. That currently means it seeks
363 * backwards and retries the reads, which doesn't work
364 * on pipes. We want to be able to read from a pipe, so
365 * that strategy won't work; we'd have to buffer some
366 * data ourselves and read from that buffer in order to
369 ps
->hdrsize
= sizeof(struct pcap_sf_patched_pkthdr
);
371 if (p
->linktype
== DLT_EN10MB
) {
373 * This capture might have been done in raw mode
376 * If it was done in cooked mode, p->snapshot was
377 * passed to recvfrom() as the buffer size, meaning
378 * that the most packet data that would be copied
379 * would be p->snapshot. However, a faked Ethernet
380 * header would then have been added to it, so the
381 * most data that would be in a packet in the file
382 * would be p->snapshot + 14.
384 * We can't easily tell whether the capture was done
385 * in raw mode or cooked mode, so we'll assume it was
386 * cooked mode, and add 14 to the snapshot length.
387 * That means that, for a raw capture, the snapshot
388 * length will be misleading if you use it to figure
389 * out why a capture doesn't have all the packet data,
390 * but there's not much we can do to avoid that.
392 * But don't grow the snapshot length past the
393 * maximum value of an int.
395 if (p
->snapshot
<= INT_MAX
- 14)
398 p
->snapshot
= INT_MAX
;
401 ps
->hdrsize
= sizeof(struct pcap_sf_pkthdr
);
404 * Allocate a buffer for the packet data.
405 * Choose the minimum of the file's snapshot length and 2K bytes;
406 * that should be enough for most network packets - we'll grow it
407 * if necessary. That way, we don't allocate a huge chunk of
408 * memory just because there's a huge snapshot length, as the
409 * snapshot length might be larger than the size of the largest
412 p
->bufsize
= p
->snapshot
;
413 if (p
->bufsize
> 2048)
415 p
->buffer
= malloc(p
->bufsize
);
416 if (p
->buffer
== NULL
) {
417 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "out of memory");
423 p
->cleanup_op
= sf_cleanup
;
429 * Grow the packet buffer to the specified size.
432 grow_buffer(pcap_t
*p
, u_int bufsize
)
436 bigger_buffer
= realloc(p
->buffer
, bufsize
);
437 if (bigger_buffer
== NULL
) {
438 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "out of memory");
441 p
->buffer
= bigger_buffer
;
442 p
->bufsize
= bufsize
;
447 * Read and return the next packet from the savefile. Return the header
448 * in hdr and a pointer to the contents in data. Return 0 on success, 1
449 * if there were no more packets, and -1 on an error.
452 pcap_next_packet(pcap_t
*p
, struct pcap_pkthdr
*hdr
, u_char
**data
)
454 struct pcap_sf
*ps
= p
->priv
;
455 struct pcap_sf_patched_pkthdr sf_hdr
;
461 * Read the packet header; the structure we use as a buffer
462 * is the longer structure for files generated by the patched
463 * libpcap, but if the file has the magic number for an
464 * unpatched libpcap we only read as many bytes as the regular
467 amt_read
= fread(&sf_hdr
, 1, ps
->hdrsize
, fp
);
468 if (amt_read
!= ps
->hdrsize
) {
470 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
471 errno
, "error reading dump file");
475 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
476 "truncated dump file; tried to read %" PRIsize
" header bytes, only got %" PRIsize
,
477 ps
->hdrsize
, amt_read
);
486 /* these were written in opposite byte order */
487 hdr
->caplen
= SWAPLONG(sf_hdr
.caplen
);
488 hdr
->len
= SWAPLONG(sf_hdr
.len
);
489 hdr
->ts
.tv_sec
= SWAPLONG(sf_hdr
.ts
.tv_sec
);
490 hdr
->ts
.tv_usec
= SWAPLONG(sf_hdr
.ts
.tv_usec
);
492 hdr
->caplen
= sf_hdr
.caplen
;
493 hdr
->len
= sf_hdr
.len
;
494 hdr
->ts
.tv_sec
= sf_hdr
.ts
.tv_sec
;
495 hdr
->ts
.tv_usec
= sf_hdr
.ts
.tv_usec
;
498 switch (ps
->scale_type
) {
502 * Just pass the time stamp through.
508 * File has microseconds, user wants nanoseconds; convert
511 hdr
->ts
.tv_usec
= hdr
->ts
.tv_usec
* 1000;
516 * File has nanoseconds, user wants microseconds; convert
519 hdr
->ts
.tv_usec
= hdr
->ts
.tv_usec
/ 1000;
523 /* Swap the caplen and len fields, if necessary. */
524 switch (ps
->lengths_swapped
) {
530 if (hdr
->caplen
<= hdr
->len
) {
532 * The captured length is <= the actual length,
533 * so presumably they weren't swapped.
541 hdr
->caplen
= hdr
->len
;
547 * Is the packet bigger than we consider sane?
549 if (hdr
->caplen
> max_snaplen_for_dlt(p
->linktype
)) {
551 * Yes. This may be a damaged or fuzzed file.
553 * Is it bigger than the snapshot length?
554 * (We don't treat that as an error if it's not
555 * bigger than the maximum we consider sane; see
558 if (hdr
->caplen
> (bpf_u_int32
)p
->snapshot
) {
559 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
560 "invalid packet capture length %u, bigger than "
561 "snaplen of %d", hdr
->caplen
, p
->snapshot
);
563 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
564 "invalid packet capture length %u, bigger than "
565 "maximum of %u", hdr
->caplen
,
566 max_snaplen_for_dlt(p
->linktype
));
571 if (hdr
->caplen
> (bpf_u_int32
)p
->snapshot
) {
573 * The packet is bigger than the snapshot length
576 * This can happen due to Solaris 2.3 systems tripping
577 * over the BUFMOD problem and not setting the snapshot
578 * length correctly in the savefile header.
580 * libpcap 0.4 and later on Solaris 2.3 should set the
581 * snapshot length correctly in the pcap file header,
582 * even though they don't set a snapshot length in bufmod
583 * (the buggy bufmod chops off the *beginning* of the
584 * packet if a snapshot length is specified); they should
585 * also reduce the captured length, as supplied to the
586 * per-packet callback, to the snapshot length if it's
587 * greater than the snapshot length, so the code using
588 * libpcap should see the packet cut off at the snapshot
589 * length, even though the full packet is copied up to
592 * However, perhaps some versions of libpcap failed to
593 * set the snapshot length currectly in the file header
594 * or the per-packet header, or perhaps this is a
595 * corrupted safefile or a savefile built/modified by a
596 * fuzz tester, so we check anyway. We grow the buffer
597 * to be big enough for the snapshot length, read up
598 * to the snapshot length, discard the rest of the
599 * packet, and report the snapshot length as the captured
600 * length; we don't want to hand our caller a packet
601 * bigger than the snapshot length, because they might
602 * be assuming they'll never be handed such a packet,
603 * and might copy the packet into a snapshot-length-
604 * sized buffer, assuming it'll fit.
606 size_t bytes_to_discard
;
607 size_t bytes_to_read
, bytes_read
;
608 char discard_buf
[4096];
610 if (hdr
->caplen
> p
->bufsize
) {
612 * Grow the buffer to the snapshot length.
614 if (!grow_buffer(p
, p
->snapshot
))
619 * Read the first p->snapshot bytes into the buffer.
621 amt_read
= fread(p
->buffer
, 1, p
->snapshot
, fp
);
622 if (amt_read
!= (bpf_u_int32
)p
->snapshot
) {
624 pcap_fmt_errmsg_for_errno(p
->errbuf
,
625 PCAP_ERRBUF_SIZE
, errno
,
626 "error reading dump file");
629 * Yes, this uses hdr->caplen; technically,
630 * it's true, because we would try to read
631 * and discard the rest of those bytes, and
632 * that would fail because we got EOF before
635 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
636 "truncated dump file; tried to read %u captured bytes, only got %" PRIsize
,
637 p
->snapshot
, amt_read
);
643 * Now read and discard what's left.
645 bytes_to_discard
= hdr
->caplen
- p
->snapshot
;
646 bytes_read
= amt_read
;
647 while (bytes_to_discard
!= 0) {
648 bytes_to_read
= bytes_to_discard
;
649 if (bytes_to_read
> sizeof (discard_buf
))
650 bytes_to_read
= sizeof (discard_buf
);
651 amt_read
= fread(discard_buf
, 1, bytes_to_read
, fp
);
652 bytes_read
+= amt_read
;
653 if (amt_read
!= bytes_to_read
) {
655 pcap_fmt_errmsg_for_errno(p
->errbuf
,
656 PCAP_ERRBUF_SIZE
, errno
,
657 "error reading dump file");
659 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
660 "truncated dump file; tried to read %u captured bytes, only got %" PRIsize
,
661 hdr
->caplen
, bytes_read
);
665 bytes_to_discard
-= amt_read
;
669 * Adjust caplen accordingly, so we don't get confused later
670 * as to how many bytes we have to play with.
672 hdr
->caplen
= p
->snapshot
;
675 * The packet is within the snapshot length for this file.
677 if (hdr
->caplen
> p
->bufsize
) {
679 * Grow the buffer to the next power of 2, or
680 * the snaplen, whichever is lower.
684 new_bufsize
= hdr
->caplen
;
686 * http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
689 new_bufsize
|= new_bufsize
>> 1;
690 new_bufsize
|= new_bufsize
>> 2;
691 new_bufsize
|= new_bufsize
>> 4;
692 new_bufsize
|= new_bufsize
>> 8;
693 new_bufsize
|= new_bufsize
>> 16;
696 if (new_bufsize
> (u_int
)p
->snapshot
)
697 new_bufsize
= p
->snapshot
;
699 if (!grow_buffer(p
, new_bufsize
))
703 /* read the packet itself */
704 amt_read
= fread(p
->buffer
, 1, hdr
->caplen
, fp
);
705 if (amt_read
!= hdr
->caplen
) {
707 pcap_fmt_errmsg_for_errno(p
->errbuf
,
708 PCAP_ERRBUF_SIZE
, errno
,
709 "error reading dump file");
711 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
712 "truncated dump file; tried to read %u captured bytes, only got %" PRIsize
,
713 hdr
->caplen
, amt_read
);
721 swap_pseudo_headers(p
->linktype
, hdr
, *data
);
727 sf_write_header(pcap_t
*p
, FILE *fp
, int linktype
, int snaplen
)
729 struct pcap_file_header hdr
;
731 hdr
.magic
= p
->opt
.tstamp_precision
== PCAP_TSTAMP_PRECISION_NANO
? NSEC_TCPDUMP_MAGIC
: TCPDUMP_MAGIC
;
732 hdr
.version_major
= PCAP_VERSION_MAJOR
;
733 hdr
.version_minor
= PCAP_VERSION_MINOR
;
736 * https://www.tcpdump.org/manpages/pcap-savefile.5.txt states:
737 * thiszone: 4-byte time zone offset; this is always 0.
738 * sigfigs: 4-byte number giving the accuracy of time stamps
739 * in the file; this is always 0.
743 hdr
.snaplen
= snaplen
;
744 hdr
.linktype
= linktype
;
746 if (fwrite((char *)&hdr
, sizeof(hdr
), 1, fp
) != 1)
753 * Output a packet to the initialized dump file.
756 pcap_dump(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*sp
)
759 struct pcap_sf_pkthdr sf_hdr
;
763 * Better not try writing pcap files after
764 * 2038-01-19 03:14:07 UTC; switch to pcapng.
766 sf_hdr
.ts
.tv_sec
= (bpf_int32
)h
->ts
.tv_sec
;
767 sf_hdr
.ts
.tv_usec
= (bpf_int32
)h
->ts
.tv_usec
;
768 sf_hdr
.caplen
= h
->caplen
;
770 /* XXX we should check the return status */
771 (void)fwrite(&sf_hdr
, sizeof(sf_hdr
), 1, f
);
772 (void)fwrite(sp
, h
->caplen
, 1, f
);
775 static pcap_dumper_t
*
776 pcap_setup_dump(pcap_t
*p
, int linktype
, FILE *f
, const char *fname
)
779 #if defined(_WIN32) || defined(MSDOS)
781 * If we're writing to the standard output, put it in binary
782 * mode, as savefiles are binary files.
784 * Otherwise, we turn off buffering.
785 * XXX - why? And why not on the standard output?
790 setvbuf(f
, NULL
, _IONBF
, 0);
792 if (sf_write_header(p
, f
, linktype
, p
->snapshot
) == -1) {
793 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
794 errno
, "Can't write to %s", fname
);
799 return ((pcap_dumper_t
*)f
);
803 * Initialize so that sf_write() will output to the file named 'fname'.
806 pcap_dump_open(pcap_t
*p
, const char *fname
)
812 * If this pcap_t hasn't been activated, it doesn't have a
813 * link-layer type, so we can't use it.
816 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
817 "%s: not-yet-activated pcap_t passed to pcap_dump_open",
821 linktype
= dlt_to_linktype(p
->linktype
);
822 if (linktype
== -1) {
823 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
824 "%s: link-layer type %d isn't supported in savefiles",
828 linktype
|= p
->linktype_ext
;
831 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
832 "A null pointer was supplied as the file name");
835 if (fname
[0] == '-' && fname
[1] == '\0') {
837 fname
= "standard output";
840 * "b" is supported as of C90, so *all* UN*Xes should
841 * support it, even though it does nothing. It's
842 * required on Windows, as the file is a binary file
843 * and must be written in binary mode.
845 f
= fopen(fname
, "wb");
847 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
852 return (pcap_setup_dump(p
, linktype
, f
, fname
));
857 * Initialize so that sf_write() will output to a stream wrapping the given raw
861 pcap_dump_hopen(pcap_t
*p
, intptr_t osfd
)
866 fd
= _open_osfhandle(osfd
, _O_APPEND
);
868 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
869 errno
, "_open_osfhandle");
873 file
= _fdopen(fd
, "wb");
875 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
881 return pcap_dump_fopen(p
, file
);
886 * Initialize so that sf_write() will output to the given stream.
892 pcap_dump_fopen(pcap_t
*p
, FILE *f
)
896 linktype
= dlt_to_linktype(p
->linktype
);
897 if (linktype
== -1) {
898 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
899 "stream: link-layer type %d isn't supported in savefiles",
903 linktype
|= p
->linktype_ext
;
905 return (pcap_setup_dump(p
, linktype
, f
, "stream"));
909 pcap_dump_open_append(pcap_t
*p
, const char *fname
)
914 struct pcap_file_header ph
;
916 linktype
= dlt_to_linktype(p
->linktype
);
917 if (linktype
== -1) {
918 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
919 "%s: link-layer type %d isn't supported in savefiles",
925 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
926 "A null pointer was supplied as the file name");
929 if (fname
[0] == '-' && fname
[1] == '\0')
930 return (pcap_setup_dump(p
, linktype
, stdout
, "standard output"));
933 * "a" will cause the file *not* to be truncated if it exists
934 * but will cause it to be created if it doesn't. It will
935 * also cause all writes to be done at the end of the file,
936 * but will allow reads to be done anywhere in the file. This
937 * is what we need, because we need to read from the beginning
938 * of the file to see if it already has a header and packets
941 * "b" is supported as of C90, so *all* UN*Xes should support it,
942 * even though it does nothing. It's required on Windows, as the
943 * file is a binary file and must be read in binary mode.
945 f
= fopen(fname
, "ab+");
947 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
953 * Try to read a pcap header.
955 * We do not assume that the file will be positioned at the
956 * beginning immediately after we've opened it - we seek to
957 * the beginning. ISO C says it's implementation-defined
958 * whether the file position indicator is at the beginning
959 * or the end of the file after an append-mode open, and
960 * it wasn't obvious from the Single UNIX Specification
961 * or the Microsoft documentation how that works on SUS-
962 * compliant systems or on Windows.
964 if (fseek(f
, 0, SEEK_SET
) == -1) {
965 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
966 errno
, "Can't seek to the beginning of %s", fname
);
970 amt_read
= fread(&ph
, 1, sizeof (ph
), f
);
971 if (amt_read
!= sizeof (ph
)) {
973 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
977 } else if (feof(f
) && amt_read
> 0) {
978 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
979 "%s: truncated pcap file header", fname
);
985 #if defined(_WIN32) || defined(MSDOS)
987 * We turn off buffering.
988 * XXX - why? And why not on the standard output?
990 setvbuf(f
, NULL
, _IONBF
, 0);
994 * If a header is already present and:
996 * it's not for a pcap file of the appropriate resolution
997 * and the right byte order for this machine;
999 * the link-layer header types don't match;
1001 * the snapshot lengths don't match;
1007 * A header is already present.
1013 if (p
->opt
.tstamp_precision
!= PCAP_TSTAMP_PRECISION_MICRO
) {
1014 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1015 "%s: different time stamp precision, cannot append to file", fname
);
1021 case NSEC_TCPDUMP_MAGIC
:
1022 if (p
->opt
.tstamp_precision
!= PCAP_TSTAMP_PRECISION_NANO
) {
1023 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1024 "%s: different time stamp precision, cannot append to file", fname
);
1030 case SWAPLONG(TCPDUMP_MAGIC
):
1031 case SWAPLONG(NSEC_TCPDUMP_MAGIC
):
1032 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1033 "%s: different byte order, cannot append to file", fname
);
1037 case KUZNETZOV_TCPDUMP_MAGIC
:
1038 case SWAPLONG(KUZNETZOV_TCPDUMP_MAGIC
):
1039 case NAVTEL_TCPDUMP_MAGIC
:
1040 case SWAPLONG(NAVTEL_TCPDUMP_MAGIC
):
1041 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1042 "%s: not a pcap file to which we can append", fname
);
1047 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1048 "%s: not a pcap file", fname
);
1056 if (ph
.version_major
!= PCAP_VERSION_MAJOR
||
1057 ph
.version_minor
!= PCAP_VERSION_MINOR
) {
1058 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1059 "%s: version is %u.%u, cannot append to file", fname
,
1060 ph
.version_major
, ph
.version_minor
);
1064 if ((bpf_u_int32
)linktype
!= ph
.linktype
) {
1065 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1066 "%s: different linktype, cannot append to file", fname
);
1070 if ((bpf_u_int32
)p
->snapshot
!= ph
.snaplen
) {
1071 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1072 "%s: different snaplen, cannot append to file", fname
);
1078 * A header isn't present; attempt to write it.
1080 if (sf_write_header(p
, f
, linktype
, p
->snapshot
) == -1) {
1081 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1082 errno
, "Can't write to %s", fname
);
1089 * Start writing at the end of the file.
1091 * XXX - this shouldn't be necessary, given that we're opening
1092 * the file in append mode, and ISO C specifies that all writes
1093 * are done at the end of the file in that mode.
1095 if (fseek(f
, 0, SEEK_END
) == -1) {
1096 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1097 errno
, "Can't seek to the end of %s", fname
);
1101 return ((pcap_dumper_t
*)f
);
1105 pcap_dump_file(pcap_dumper_t
*p
)
1111 pcap_dump_ftell(pcap_dumper_t
*p
)
1113 return (ftell((FILE *)p
));
1116 #if defined(HAVE_FSEEKO)
1118 * We have fseeko(), so we have ftello().
1119 * If we have large file support (files larger than 2^31-1 bytes),
1120 * ftello() will give us a current file position with more than 32
1124 pcap_dump_ftell64(pcap_dumper_t
*p
)
1126 return (ftello((FILE *)p
));
1128 #elif defined(_MSC_VER)
1130 * We have Visual Studio; we support only 2005 and later, so we have
1134 pcap_dump_ftell64(pcap_dumper_t
*p
)
1136 return (_ftelli64((FILE *)p
));
1140 * We don't have ftello() or _ftelli64(), so fall back on ftell().
1141 * Either long is 64 bits, in which case ftell() should suffice,
1142 * or this is probably an older 32-bit UN*X without large file
1143 * support, which means you'll probably get errors trying to
1144 * write files > 2^31-1, so it won't matter anyway.
1146 * XXX - what about MinGW?
1149 pcap_dump_ftell64(pcap_dumper_t
*p
)
1151 return (ftell((FILE *)p
));
1156 pcap_dump_flush(pcap_dumper_t
*p
)
1159 if (fflush((FILE *)p
) == EOF
)
1166 pcap_dump_close(pcap_dumper_t
*p
)
1170 if (ferror((FILE *)p
))
1172 /* XXX should check return from fclose() too */
1174 (void)fclose((FILE *)p
);