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-ng.c - pcap-ng-file-format-specific code from savefile.c
28 #include <pcap/pcap-inttypes.h>
38 #include "pcap-common.h"
40 #ifdef HAVE_OS_PROTO_H
44 #include "sf-pcap-ng.h"
51 * Common part at the beginning of all blocks.
54 bpf_u_int32 block_type
;
55 bpf_u_int32 total_length
;
59 * Common trailer at the end of all blocks.
61 struct block_trailer
{
62 bpf_u_int32 total_length
;
68 #define OPT_ENDOFOPT 0 /* end of options */
69 #define OPT_COMMENT 1 /* comment string */
74 struct option_header
{
76 u_short option_length
;
80 * Structures for the part of each block type following the common
85 * Section Header Block.
87 #define BT_SHB 0x0A0D0D0A
89 struct section_header_block
{
90 bpf_u_int32 byte_order_magic
;
91 u_short major_version
;
92 u_short minor_version
;
93 uint64_t section_length
;
94 /* followed by options and trailer */
98 * Byte-order magic value.
100 #define BYTE_ORDER_MAGIC 0x1A2B3C4D
103 * Current version number. If major_version isn't PCAP_NG_VERSION_MAJOR,
104 * that means that this code can't read the file.
106 #define PCAP_NG_VERSION_MAJOR 1
107 #define PCAP_NG_VERSION_MINOR 0
110 * Interface Description Block.
112 #define BT_IDB 0x00000001
114 struct interface_description_block
{
118 /* followed by options and trailer */
122 * Options in the IDB.
124 #define IF_NAME 2 /* interface name string */
125 #define IF_DESCRIPTION 3 /* interface description string */
126 #define IF_IPV4ADDR 4 /* interface's IPv4 address and netmask */
127 #define IF_IPV6ADDR 5 /* interface's IPv6 address and prefix length */
128 #define IF_MACADDR 6 /* interface's MAC address */
129 #define IF_EUIADDR 7 /* interface's EUI address */
130 #define IF_SPEED 8 /* interface's speed, in bits/s */
131 #define IF_TSRESOL 9 /* interface's time stamp resolution */
132 #define IF_TZONE 10 /* interface's time zone */
133 #define IF_FILTER 11 /* filter used when capturing on interface */
134 #define IF_OS 12 /* string OS on which capture on this interface was done */
135 #define IF_FCSLEN 13 /* FCS length for this interface */
136 #define IF_TSOFFSET 14 /* time stamp offset for this interface */
139 * Enhanced Packet Block.
141 #define BT_EPB 0x00000006
143 struct enhanced_packet_block
{
144 bpf_u_int32 interface_id
;
145 bpf_u_int32 timestamp_high
;
146 bpf_u_int32 timestamp_low
;
149 /* followed by packet data, options, and trailer */
153 * Simple Packet Block.
155 #define BT_SPB 0x00000003
157 struct simple_packet_block
{
159 /* followed by packet data and trailer */
165 #define BT_PB 0x00000002
167 struct packet_block
{
168 u_short interface_id
;
170 bpf_u_int32 timestamp_high
;
171 bpf_u_int32 timestamp_low
;
174 /* followed by packet data, options, and trailer */
178 * Block cursor - used when processing the contents of a block.
179 * Contains a pointer into the data being processed and a count
180 * of bytes remaining in the block.
182 struct block_cursor
{
184 size_t data_remaining
;
185 bpf_u_int32 block_type
;
194 } tstamp_scale_type_t
;
197 * Per-interface information.
200 u_int tsresol
; /* time stamp resolution */
201 tstamp_scale_type_t scale_type
; /* how to scale */
202 u_int scale_factor
; /* time stamp scale factor for power-of-10 tsresol */
203 uint64_t tsoffset
; /* time stamp offset */
207 * Per-pcap_t private data.
209 * max_blocksize is the maximum size of a block that we'll accept. We
210 * reject blocks bigger than this, so we don't consume too much memory
211 * with a truly huge block. It can change as we see IDBs with different
212 * link-layer header types. (Currently, we don't support IDBs with
213 * different link-layer header types, but we will support it in the
214 * future, when we offer file-reading APIs that support it.)
216 * XXX - that's an issue on ILP32 platforms, where the maximum block
217 * size of 2^31-1 would eat all but one byte of the entire address space.
218 * It's less of an issue on ILP64/LLP64 platforms, but the actual size
219 * of the address space may be limited by 1) the number of *significant*
220 * address bits (currently, x86-64 only supports 48 bits of address), 2)
221 * any limitations imposed by the operating system; 3) any limitations
222 * imposed by the amount of available backing store for anonymous pages,
223 * so we impose a limit regardless of the size of a pointer.
226 u_int user_tsresol
; /* time stamp resolution requested by the user */
227 u_int max_blocksize
; /* don't grow buffer size past this */
228 bpf_u_int32 ifcount
; /* number of interfaces seen in this capture */
229 bpf_u_int32 ifaces_size
; /* size of array below */
230 struct pcap_ng_if
*ifaces
; /* array of interface information */
234 * Maximum block size for a given maximum snapshot length; we calculate
237 * We define it as the size of an EPB with a max_snaplen-sized
238 * packet and 128KB of options.
240 #define MAX_BLOCKSIZE(max_snaplen) (sizeof (struct block_header) + \
241 sizeof (struct enhanced_packet_block) + \
242 (max_snaplen) + 131072 + \
243 sizeof (struct block_trailer))
245 static void pcap_ng_cleanup(pcap_t
*p
);
246 static int pcap_ng_next_packet(pcap_t
*p
, struct pcap_pkthdr
*hdr
,
250 read_bytes(FILE *fp
, void *buf
, size_t bytes_to_read
, int fail_on_eof
,
255 amt_read
= fread(buf
, 1, bytes_to_read
, fp
);
256 if (amt_read
!= bytes_to_read
) {
258 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
259 "error reading dump file: %s",
260 pcap_strerror(errno
));
262 if (amt_read
== 0 && !fail_on_eof
)
263 return (0); /* EOF */
264 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
265 "truncated dump file; tried to read %lu bytes, only got %lu",
266 (unsigned long)bytes_to_read
,
267 (unsigned long)amt_read
);
275 read_block(FILE *fp
, pcap_t
*p
, struct block_cursor
*cursor
, char *errbuf
)
277 struct pcap_ng_sf
*ps
;
279 struct block_header bhdr
;
281 size_t data_remaining
;
285 status
= read_bytes(fp
, &bhdr
, sizeof(bhdr
), 0, errbuf
);
287 return (status
); /* error or EOF */
290 bhdr
.block_type
= SWAPLONG(bhdr
.block_type
);
291 bhdr
.total_length
= SWAPLONG(bhdr
.total_length
);
295 * Is this block "too big"?
297 * We choose 16MB as "too big", for now, so that we handle
298 * "reasonably" large buffers but don't chew up all the
299 * memory if we read a malformed file.
301 if (bhdr
.total_length
> 16*1024*1024) {
302 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
303 "pcap-ng block size %u > maximum %u",
304 bhdr
.total_length
, 16*1024*1024);
309 * Is this block "too small" - i.e., is it shorter than a block
310 * header plus a block trailer?
312 if (bhdr
.total_length
< sizeof(struct block_header
) +
313 sizeof(struct block_trailer
)) {
314 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
315 "block in pcap-ng dump file has a length of %u < %lu",
317 (unsigned long)(sizeof(struct block_header
) + sizeof(struct block_trailer
)));
322 * Is the buffer big enough?
324 if (p
->bufsize
< bhdr
.total_length
) {
326 * No - make it big enough, unless it's too big.
330 if (bhdr
.total_length
> ps
->max_blocksize
) {
331 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "block is larger than maximum block size %u",
335 bigger_buffer
= realloc(p
->buffer
, bhdr
.total_length
);
336 if (bigger_buffer
== NULL
) {
337 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "out of memory");
340 p
->buffer
= bigger_buffer
;
344 * Copy the stuff we've read to the buffer, and read the rest
347 memcpy(p
->buffer
, &bhdr
, sizeof(bhdr
));
348 bdata
= (u_char
*)p
->buffer
+ sizeof(bhdr
);
349 data_remaining
= bhdr
.total_length
- sizeof(bhdr
);
350 if (read_bytes(fp
, bdata
, data_remaining
, 1, errbuf
) == -1)
354 * Initialize the cursor.
356 cursor
->data
= bdata
;
357 cursor
->data_remaining
= data_remaining
- sizeof(struct block_trailer
);
358 cursor
->block_type
= bhdr
.block_type
;
363 get_from_block_data(struct block_cursor
*cursor
, size_t chunk_size
,
369 * Make sure we have the specified amount of data remaining in
372 if (cursor
->data_remaining
< chunk_size
) {
373 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
374 "block of type %u in pcap-ng dump file is too short",
380 * Return the current pointer, and skip past the chunk.
383 cursor
->data
+= chunk_size
;
384 cursor
->data_remaining
-= chunk_size
;
388 static struct option_header
*
389 get_opthdr_from_block_data(pcap_t
*p
, struct block_cursor
*cursor
, char *errbuf
)
391 struct option_header
*opthdr
;
393 opthdr
= get_from_block_data(cursor
, sizeof(*opthdr
), errbuf
);
394 if (opthdr
== NULL
) {
396 * Option header is cut short.
402 * Byte-swap it if necessary.
405 opthdr
->option_code
= SWAPSHORT(opthdr
->option_code
);
406 opthdr
->option_length
= SWAPSHORT(opthdr
->option_length
);
413 get_optvalue_from_block_data(struct block_cursor
*cursor
,
414 struct option_header
*opthdr
, char *errbuf
)
416 size_t padded_option_len
;
419 /* Pad option length to 4-byte boundary */
420 padded_option_len
= opthdr
->option_length
;
421 padded_option_len
= ((padded_option_len
+ 3)/4)*4;
423 optvalue
= get_from_block_data(cursor
, padded_option_len
, errbuf
);
424 if (optvalue
== NULL
) {
426 * Option value is cut short.
435 process_idb_options(pcap_t
*p
, struct block_cursor
*cursor
, u_int
*tsresol
,
436 uint64_t *tsoffset
, int *is_binary
, char *errbuf
)
438 struct option_header
*opthdr
;
440 int saw_tsresol
, saw_tsoffset
;
446 while (cursor
->data_remaining
!= 0) {
448 * Get the option header.
450 opthdr
= get_opthdr_from_block_data(p
, cursor
, errbuf
);
451 if (opthdr
== NULL
) {
453 * Option header is cut short.
461 optvalue
= get_optvalue_from_block_data(cursor
, opthdr
,
463 if (optvalue
== NULL
) {
465 * Option value is cut short.
470 switch (opthdr
->option_code
) {
473 if (opthdr
->option_length
!= 0) {
474 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
475 "Interface Description Block has opt_endofopt option with length %u != 0",
476 opthdr
->option_length
);
482 if (opthdr
->option_length
!= 1) {
483 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
484 "Interface Description Block has if_tsresol option with length %u != 1",
485 opthdr
->option_length
);
489 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
490 "Interface Description Block has more than one if_tsresol option");
494 memcpy(&tsresol_opt
, optvalue
, sizeof(tsresol_opt
));
495 if (tsresol_opt
& 0x80) {
497 * Resolution is negative power of 2.
500 *tsresol
= 1 << (tsresol_opt
& 0x7F);
503 * Resolution is negative power of 10.
507 for (i
= 0; i
< tsresol_opt
; i
++)
512 * Resolution is too high.
514 if (tsresol_opt
& 0x80) {
515 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
516 "Interface Description Block if_tsresol option resolution 2^-%u is too high",
519 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
520 "Interface Description Block if_tsresol option resolution 10^-%u is too high",
528 if (opthdr
->option_length
!= 8) {
529 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
530 "Interface Description Block has if_tsoffset option with length %u != 8",
531 opthdr
->option_length
);
535 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
536 "Interface Description Block has more than one if_tsoffset option");
540 memcpy(tsoffset
, optvalue
, sizeof(*tsoffset
));
542 *tsoffset
= SWAPLL(*tsoffset
);
555 add_interface(pcap_t
*p
, struct block_cursor
*cursor
, char *errbuf
)
557 struct pcap_ng_sf
*ps
;
565 * Count this interface.
570 * Grow the array of per-interface information as necessary.
572 if (ps
->ifcount
> ps
->ifaces_size
) {
574 * We need to grow the array.
576 bpf_u_int32 new_ifaces_size
;
577 struct pcap_ng_if
*new_ifaces
;
579 if (ps
->ifaces_size
== 0) {
581 * It's currently empty.
583 * (The Clang static analyzer doesn't do enough,
584 * err, umm, dataflow *analysis* to realize that
585 * ps->ifaces_size == 0 if ps->ifaces == NULL,
586 * and so complains about a possible zero argument
587 * to realloc(), so we check for the former
588 * condition to shut it up.
590 * However, it doesn't complain that one of the
591 * multiplications below could overflow, which is
592 * a real, albeit extremely unlikely, problem (you'd
593 * need a pcap-ng file with tens of millions of
597 new_ifaces
= malloc(sizeof (struct pcap_ng_if
));
600 * It's not currently empty; double its size.
601 * (Perhaps overkill once we have a lot of interfaces.)
603 * Check for overflow if we double it.
605 if (ps
->ifaces_size
* 2 < ps
->ifaces_size
) {
607 * The maximum number of interfaces before
608 * ps->ifaces_size overflows is the largest
609 * possible 32-bit power of 2, as we do
612 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
613 "more than %u interfaces in the file",
619 * ps->ifaces_size * 2 doesn't overflow, so it's
622 new_ifaces_size
= ps
->ifaces_size
* 2;
625 * Now make sure that's not so big that it overflows
626 * if we multiply by sizeof (struct pcap_ng_if).
628 * That can happen on 32-bit platforms, with a 32-bit
629 * size_t; it shouldn't happen on 64-bit platforms,
630 * with a 64-bit size_t, as new_ifaces_size is
633 if (new_ifaces_size
* sizeof (struct pcap_ng_if
) < new_ifaces_size
) {
635 * As this fails only with 32-bit size_t,
636 * the multiplication was 32x32->32, and
637 * the largest 32-bit value that can safely
638 * be multiplied by sizeof (struct pcap_ng_if)
639 * without overflow is the largest 32-bit
640 * (unsigned) value divided by
641 * sizeof (struct pcap_ng_if).
643 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
644 "more than %u interfaces in the file",
645 0xFFFFFFFFU
/ ((u_int
)sizeof (struct pcap_ng_if
)));
648 new_ifaces
= realloc(ps
->ifaces
, new_ifaces_size
* sizeof (struct pcap_ng_if
));
650 if (new_ifaces
== NULL
) {
652 * We ran out of memory.
655 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
656 "out of memory for per-interface information (%u interfaces)",
660 ps
->ifaces_size
= new_ifaces_size
;
661 ps
->ifaces
= new_ifaces
;
665 * Set the default time stamp resolution and offset.
667 tsresol
= 1000000; /* microsecond resolution */
668 is_binary
= 0; /* which is a power of 10 */
669 tsoffset
= 0; /* absolute timestamps */
672 * Now look for various time stamp options, so we know
673 * how to interpret the time stamps for this interface.
675 if (process_idb_options(p
, cursor
, &tsresol
, &tsoffset
, &is_binary
,
679 ps
->ifaces
[ps
->ifcount
- 1].tsresol
= tsresol
;
680 ps
->ifaces
[ps
->ifcount
- 1].tsoffset
= tsoffset
;
683 * Determine whether we're scaling up or down or not
684 * at all for this interface.
686 if (tsresol
== ps
->user_tsresol
) {
688 * The resolution is the resolution the user wants,
689 * so we don't have to do scaling.
691 ps
->ifaces
[ps
->ifcount
- 1].scale_type
= PASS_THROUGH
;
692 } else if (tsresol
> ps
->user_tsresol
) {
694 * The resolution is greater than what the user wants,
695 * so we have to scale the timestamps down.
698 ps
->ifaces
[ps
->ifcount
- 1].scale_type
= SCALE_DOWN_BIN
;
701 * Calculate the scale factor.
703 ps
->ifaces
[ps
->ifcount
- 1].scale_factor
= tsresol
/ps
->user_tsresol
;
704 ps
->ifaces
[ps
->ifcount
- 1].scale_type
= SCALE_DOWN_DEC
;
708 * The resolution is less than what the user wants,
709 * so we have to scale the timestamps up.
712 ps
->ifaces
[ps
->ifcount
- 1].scale_type
= SCALE_UP_BIN
;
715 * Calculate the scale factor.
717 ps
->ifaces
[ps
->ifcount
- 1].scale_factor
= ps
->user_tsresol
/tsresol
;
718 ps
->ifaces
[ps
->ifcount
- 1].scale_type
= SCALE_UP_DEC
;
725 * Check whether this is a pcap-ng savefile and, if it is, extract the
726 * relevant information from the header.
729 pcap_ng_check_header(bpf_u_int32 magic
, FILE *fp
, u_int precision
, char *errbuf
,
733 bpf_u_int32 total_length
;
734 bpf_u_int32 byte_order_magic
;
735 struct block_header
*bhdrp
;
736 struct section_header_block
*shbp
;
739 struct pcap_ng_sf
*ps
;
741 struct block_cursor cursor
;
742 struct interface_description_block
*idbp
;
745 * Assume no read errors.
750 * Check whether the first 4 bytes of the file are the block
751 * type for a pcap-ng savefile.
753 if (magic
!= BT_SHB
) {
755 * XXX - check whether this looks like what the block
756 * type would be after being munged by mapping between
757 * UN*X and DOS/Windows text file format and, if it
758 * does, look for the byte-order magic number in
759 * the appropriate place and, if we find it, report
760 * this as possibly being a pcap-ng file transferred
761 * between UN*X and Windows in text file format?
763 return (NULL
); /* nope */
767 * OK, they are. However, that's just \n\r\r\n, so it could,
768 * conceivably, be an ordinary text file.
770 * It could not, however, conceivably be any other type of
771 * capture file, so we can read the rest of the putative
772 * Section Header Block; put the block type in the common
773 * header, read the rest of the common header and the
774 * fixed-length portion of the SHB, and look for the byte-order
777 amt_read
= fread(&total_length
, 1, sizeof(total_length
), fp
);
778 if (amt_read
< sizeof(total_length
)) {
780 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
781 "error reading dump file: %s",
782 pcap_strerror(errno
));
784 return (NULL
); /* fail */
788 * Possibly a weird short text file, so just say
793 amt_read
= fread(&byte_order_magic
, 1, sizeof(byte_order_magic
), fp
);
794 if (amt_read
< sizeof(byte_order_magic
)) {
796 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
797 "error reading dump file: %s",
798 pcap_strerror(errno
));
800 return (NULL
); /* fail */
804 * Possibly a weird short text file, so just say
809 if (byte_order_magic
!= BYTE_ORDER_MAGIC
) {
810 byte_order_magic
= SWAPLONG(byte_order_magic
);
811 if (byte_order_magic
!= BYTE_ORDER_MAGIC
) {
813 * Not a pcap-ng file.
818 total_length
= SWAPLONG(total_length
);
822 * Check the sanity of the total length.
824 if (total_length
< sizeof(*bhdrp
) + sizeof(*shbp
) + sizeof(struct block_trailer
)) {
825 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
826 "Section Header Block in pcap-ng dump file has a length of %u < %lu",
828 (unsigned long)(sizeof(*bhdrp
) + sizeof(*shbp
) + sizeof(struct block_trailer
)));
834 * OK, this is a good pcap-ng file.
835 * Allocate a pcap_t for it.
837 p
= pcap_open_offline_common(errbuf
, sizeof (struct pcap_ng_sf
));
839 /* Allocation failed. */
843 p
->swapped
= swapped
;
847 * What precision does the user want?
851 case PCAP_TSTAMP_PRECISION_MICRO
:
852 ps
->user_tsresol
= 1000000;
855 case PCAP_TSTAMP_PRECISION_NANO
:
856 ps
->user_tsresol
= 1000000000;
860 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
861 "unknown time stamp resolution %u", precision
);
867 p
->opt
.tstamp_precision
= precision
;
870 * Allocate a buffer into which to read blocks. We default to
873 * the total length of the SHB for which we read the header;
875 * 2K, which should be more than large enough for an Enhanced
876 * Packet Block containing a full-size Ethernet frame, and
877 * leaving room for some options.
879 * If we find a bigger block, we reallocate the buffer, up to
880 * the maximum size. We start out with a maximum size based
881 * on a maximum snapshot length of MAXIMUM_SNAPLEN; if we see
882 * any link-layer header types with a larger maximum snapshot
883 * length, we boost the maximum.
886 if (p
->bufsize
< total_length
)
887 p
->bufsize
= total_length
;
888 p
->buffer
= malloc(p
->bufsize
);
889 if (p
->buffer
== NULL
) {
890 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "out of memory");
895 ps
->max_blocksize
= MAX_BLOCKSIZE(MAXIMUM_SNAPLEN
);
898 * Copy the stuff we've read to the buffer, and read the rest
901 bhdrp
= (struct block_header
*)p
->buffer
;
902 shbp
= (struct section_header_block
*)((u_char
*)p
->buffer
+ sizeof(struct block_header
));
903 bhdrp
->block_type
= magic
;
904 bhdrp
->total_length
= total_length
;
905 shbp
->byte_order_magic
= byte_order_magic
;
907 (u_char
*)p
->buffer
+ (sizeof(magic
) + sizeof(total_length
) + sizeof(byte_order_magic
)),
908 total_length
- (sizeof(magic
) + sizeof(total_length
) + sizeof(byte_order_magic
)),
914 * Byte-swap the fields we've read.
916 shbp
->major_version
= SWAPSHORT(shbp
->major_version
);
917 shbp
->minor_version
= SWAPSHORT(shbp
->minor_version
);
920 * XXX - we don't care about the section length.
923 /* currently only SHB version 1.0 is supported */
924 if (! (shbp
->major_version
== PCAP_NG_VERSION_MAJOR
&&
925 shbp
->minor_version
== PCAP_NG_VERSION_MINOR
)) {
926 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
927 "unsupported pcap-ng savefile version %u.%u",
928 shbp
->major_version
, shbp
->minor_version
);
931 p
->version_major
= shbp
->major_version
;
932 p
->version_minor
= shbp
->minor_version
;
935 * Save the time stamp resolution the user requested.
937 p
->opt
.tstamp_precision
= precision
;
940 * Now start looking for an Interface Description Block.
944 * Read the next block.
946 status
= read_block(fp
, p
, &cursor
, errbuf
);
948 /* EOF - no IDB in this file */
949 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
950 "the capture file has no Interface Description Blocks");
954 goto fail
; /* error */
955 switch (cursor
.block_type
) {
959 * Get a pointer to the fixed-length portion of the
962 idbp
= get_from_block_data(&cursor
, sizeof(*idbp
),
965 goto fail
; /* error */
968 * Byte-swap it if necessary.
971 idbp
->linktype
= SWAPSHORT(idbp
->linktype
);
972 idbp
->snaplen
= SWAPLONG(idbp
->snaplen
);
976 * Try to add this interface.
978 if (!add_interface(p
, &cursor
, errbuf
))
987 * Saw a packet before we saw any IDBs. That's
988 * not valid, as we don't know what link-layer
989 * encapsulation the packet has.
991 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
992 "the capture file has a packet block before any Interface Description Blocks");
1004 p
->tzoff
= 0; /* XXX - not used in pcap */
1005 p
->snapshot
= idbp
->snaplen
;
1006 if (p
->snapshot
<= 0) {
1008 * Bogus snapshot length; use the maximum for this
1009 * link-layer type as a fallback.
1011 * XXX - the only reason why snapshot is signed is
1012 * that pcap_snapshot() returns an int, not an
1015 p
->snapshot
= max_snaplen_for_dlt(idbp
->linktype
);
1017 p
->linktype
= linktype_to_dlt(idbp
->linktype
);
1018 p
->linktype_ext
= 0;
1021 * If the maximum block size for a packet with the maximum
1022 * snapshot length for this DLT_ is bigger than the current
1023 * maximum block size, increase the maximum.
1025 if (MAX_BLOCKSIZE(max_snaplen_for_dlt(p
->linktype
)) > ps
->max_blocksize
)
1026 ps
->max_blocksize
= MAX_BLOCKSIZE(max_snaplen_for_dlt(p
->linktype
));
1028 p
->next_packet_op
= pcap_ng_next_packet
;
1029 p
->cleanup_op
= pcap_ng_cleanup
;
1042 pcap_ng_cleanup(pcap_t
*p
)
1044 struct pcap_ng_sf
*ps
= p
->priv
;
1051 * Read and return the next packet from the savefile. Return the header
1052 * in hdr and a pointer to the contents in data. Return 0 on success, 1
1053 * if there were no more packets, and -1 on an error.
1056 pcap_ng_next_packet(pcap_t
*p
, struct pcap_pkthdr
*hdr
, u_char
**data
)
1058 struct pcap_ng_sf
*ps
= p
->priv
;
1059 struct block_cursor cursor
;
1061 struct enhanced_packet_block
*epbp
;
1062 struct simple_packet_block
*spbp
;
1063 struct packet_block
*pbp
;
1064 bpf_u_int32 interface_id
= 0xFFFFFFFF;
1065 struct interface_description_block
*idbp
;
1066 struct section_header_block
*shbp
;
1067 FILE *fp
= p
->rfile
;
1068 uint64_t t
, sec
, frac
;
1071 * Look for an Enhanced Packet Block, a Simple Packet Block,
1072 * or a Packet Block.
1076 * Read the block type and length; those are common
1079 status
= read_block(fp
, p
, &cursor
, p
->errbuf
);
1081 return (1); /* EOF */
1083 return (-1); /* error */
1084 switch (cursor
.block_type
) {
1088 * Get a pointer to the fixed-length portion of the
1091 epbp
= get_from_block_data(&cursor
, sizeof(*epbp
),
1094 return (-1); /* error */
1097 * Byte-swap it if necessary.
1100 /* these were written in opposite byte order */
1101 interface_id
= SWAPLONG(epbp
->interface_id
);
1102 hdr
->caplen
= SWAPLONG(epbp
->caplen
);
1103 hdr
->len
= SWAPLONG(epbp
->len
);
1104 t
= ((uint64_t)SWAPLONG(epbp
->timestamp_high
)) << 32 |
1105 SWAPLONG(epbp
->timestamp_low
);
1107 interface_id
= epbp
->interface_id
;
1108 hdr
->caplen
= epbp
->caplen
;
1109 hdr
->len
= epbp
->len
;
1110 t
= ((uint64_t)epbp
->timestamp_high
) << 32 |
1111 epbp
->timestamp_low
;
1117 * Get a pointer to the fixed-length portion of the
1120 spbp
= get_from_block_data(&cursor
, sizeof(*spbp
),
1123 return (-1); /* error */
1126 * SPB packets are assumed to have arrived on
1127 * the first interface.
1132 * Byte-swap it if necessary.
1135 /* these were written in opposite byte order */
1136 hdr
->len
= SWAPLONG(spbp
->len
);
1138 hdr
->len
= spbp
->len
;
1141 * The SPB doesn't give the captured length;
1142 * it's the minimum of the snapshot length
1143 * and the packet length.
1145 hdr
->caplen
= hdr
->len
;
1146 if (hdr
->caplen
> (bpf_u_int32
)p
->snapshot
)
1147 hdr
->caplen
= p
->snapshot
;
1148 t
= 0; /* no time stamps */
1153 * Get a pointer to the fixed-length portion of the
1156 pbp
= get_from_block_data(&cursor
, sizeof(*pbp
),
1159 return (-1); /* error */
1162 * Byte-swap it if necessary.
1165 /* these were written in opposite byte order */
1166 interface_id
= SWAPSHORT(pbp
->interface_id
);
1167 hdr
->caplen
= SWAPLONG(pbp
->caplen
);
1168 hdr
->len
= SWAPLONG(pbp
->len
);
1169 t
= ((uint64_t)SWAPLONG(pbp
->timestamp_high
)) << 32 |
1170 SWAPLONG(pbp
->timestamp_low
);
1172 interface_id
= pbp
->interface_id
;
1173 hdr
->caplen
= pbp
->caplen
;
1174 hdr
->len
= pbp
->len
;
1175 t
= ((uint64_t)pbp
->timestamp_high
) << 32 |
1182 * Interface Description Block. Get a pointer
1183 * to its fixed-length portion.
1185 idbp
= get_from_block_data(&cursor
, sizeof(*idbp
),
1188 return (-1); /* error */
1191 * Byte-swap it if necessary.
1194 idbp
->linktype
= SWAPSHORT(idbp
->linktype
);
1195 idbp
->snaplen
= SWAPLONG(idbp
->snaplen
);
1199 * If the link-layer type or snapshot length
1200 * differ from the ones for the first IDB we
1203 * XXX - just discard packets from those
1206 if (p
->linktype
!= idbp
->linktype
) {
1207 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1208 "an interface has a type %u different from the type of the first interface",
1212 if ((bpf_u_int32
)p
->snapshot
!= idbp
->snaplen
) {
1213 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1214 "an interface has a snapshot length %u different from the type of the first interface",
1220 * Try to add this interface.
1222 if (!add_interface(p
, &cursor
, p
->errbuf
))
1228 * Section Header Block. Get a pointer
1229 * to its fixed-length portion.
1231 shbp
= get_from_block_data(&cursor
, sizeof(*shbp
),
1234 return (-1); /* error */
1237 * Assume the byte order of this section is
1238 * the same as that of the previous section.
1239 * We'll check for that later.
1242 shbp
->byte_order_magic
=
1243 SWAPLONG(shbp
->byte_order_magic
);
1244 shbp
->major_version
=
1245 SWAPSHORT(shbp
->major_version
);
1249 * Make sure the byte order doesn't change;
1250 * pcap_is_swapped() shouldn't change its
1251 * return value in the middle of reading a capture.
1253 switch (shbp
->byte_order_magic
) {
1255 case BYTE_ORDER_MAGIC
:
1261 case SWAPLONG(BYTE_ORDER_MAGIC
):
1263 * Byte order changes.
1265 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1266 "the file has sections with different byte orders");
1273 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1274 "the file has a section with a bad byte order magic field");
1279 * Make sure the major version is the version
1282 if (shbp
->major_version
!= PCAP_NG_VERSION_MAJOR
) {
1283 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1284 "unknown pcap-ng savefile major version number %u",
1285 shbp
->major_version
);
1290 * Reset the interface count; this section should
1291 * have its own set of IDBs. If any of them
1292 * don't have the same interface type, snapshot
1293 * length, or resolution as the first interface
1294 * we saw, we'll fail. (And if we don't see
1295 * any IDBs, we'll fail when we see a packet
1303 * Not a packet block, IDB, or SHB; ignore it.
1311 * Is the interface ID an interface we know?
1313 if (interface_id
>= ps
->ifcount
) {
1317 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1318 "a packet arrived on interface %u, but there's no Interface Description Block for that interface",
1323 if (hdr
->caplen
> (bpf_u_int32
)p
->snapshot
) {
1324 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1325 "invalid packet capture length %u, bigger than "
1326 "snaplen of %d", hdr
->caplen
, p
->snapshot
);
1331 * Convert the time stamp to seconds and fractions of a second,
1332 * with the fractions being in units of the file-supplied resolution.
1334 sec
= t
/ ps
->ifaces
[interface_id
].tsresol
+ ps
->ifaces
[interface_id
].tsoffset
;
1335 frac
= t
% ps
->ifaces
[interface_id
].tsresol
;
1338 * Convert the fractions from units of the file-supplied resolution
1339 * to units of the user-requested resolution.
1341 switch (ps
->ifaces
[interface_id
].scale_type
) {
1345 * The interface resolution is what the user wants,
1352 * The interface resolution is less than what the user
1353 * wants; scale the fractional part up to the units of
1354 * the resolution the user requested by multiplying by
1355 * the quotient of the user-requested resolution and the
1356 * file-supplied resolution.
1358 * Those resolutions are both powers of 10, and the user-
1359 * requested resolution is greater than the file-supplied
1360 * resolution, so the quotient in question is an integer.
1361 * We've calculated that quotient already, so we just
1364 frac
*= ps
->ifaces
[interface_id
].scale_factor
;
1369 * The interface resolution is less than what the user
1370 * wants; scale the fractional part up to the units of
1371 * the resolution the user requested by multiplying by
1372 * the quotient of the user-requested resolution and the
1373 * file-supplied resolution.
1375 * The file-supplied resolution is a power of 2, so the
1376 * quotient is not an integer, so, in order to do this
1377 * entirely with integer arithmetic, we multiply by the
1378 * user-requested resolution and divide by the file-
1379 * supplied resolution.
1381 * XXX - Is there something clever we could do here,
1382 * given that we know that the file-supplied resolution
1383 * is a power of 2? Doing a multiplication followed by
1384 * a division runs the risk of overflowing, and involves
1385 * two non-simple arithmetic operations.
1387 frac
*= ps
->user_tsresol
;
1388 frac
/= ps
->ifaces
[interface_id
].tsresol
;
1391 case SCALE_DOWN_DEC
:
1393 * The interface resolution is greater than what the user
1394 * wants; scale the fractional part up to the units of
1395 * the resolution the user requested by multiplying by
1396 * the quotient of the user-requested resolution and the
1397 * file-supplied resolution.
1399 * Those resolutions are both powers of 10, and the user-
1400 * requested resolution is less than the file-supplied
1401 * resolution, so the quotient in question isn't an
1402 * integer, but its reciprocal is, and we can just divide
1403 * by the reciprocal of the quotient. We've calculated
1404 * the reciprocal of that quotient already, so we must
1407 frac
/= ps
->ifaces
[interface_id
].scale_factor
;
1411 case SCALE_DOWN_BIN
:
1413 * The interface resolution is greater than what the user
1414 * wants; convert the fractional part to units of the
1415 * resolution the user requested by multiplying by the
1416 * quotient of the user-requested resolution and the
1417 * file-supplied resolution. We do that by multiplying
1418 * by the user-requested resolution and dividing by the
1419 * file-supplied resolution, as the quotient might not
1420 * fit in an integer.
1422 * The file-supplied resolution is a power of 2, so the
1423 * quotient is not an integer, and neither is its
1424 * reciprocal, so, in order to do this entirely with
1425 * integer arithmetic, we multiply by the user-requested
1426 * resolution and divide by the file-supplied resolution.
1428 * XXX - Is there something clever we could do here,
1429 * given that we know that the file-supplied resolution
1430 * is a power of 2? Doing a multiplication followed by
1431 * a division runs the risk of overflowing, and involves
1432 * two non-simple arithmetic operations.
1434 frac
*= ps
->user_tsresol
;
1435 frac
/= ps
->ifaces
[interface_id
].tsresol
;
1440 * tv_sec and tv_used in the Windows struct timeval are both
1443 hdr
->ts
.tv_sec
= (long)sec
;
1444 hdr
->ts
.tv_usec
= (long)frac
;
1447 * tv_sec in the UN*X struct timeval is a time_t; tv_usec is
1448 * suseconds_t in UN*Xes that work the way the current Single
1449 * UNIX Standard specify - but not all older UN*Xes necessarily
1450 * support that type, so just cast to int.
1452 hdr
->ts
.tv_sec
= (time_t)sec
;
1453 hdr
->ts
.tv_usec
= (int)frac
;
1457 * Get a pointer to the packet data.
1459 *data
= get_from_block_data(&cursor
, hdr
->caplen
, p
->errbuf
);
1464 swap_pseudo_headers(p
->linktype
, hdr
, *data
);