xref: /aosp_15_r20/external/libpcap/sf-pcapng.c (revision 8b26181f966a6af5cf6981a6f474313de533bb28)
1*8b26181fSAndroid Build Coastguard Worker /*
2*8b26181fSAndroid Build Coastguard Worker  * Copyright (c) 1993, 1994, 1995, 1996, 1997
3*8b26181fSAndroid Build Coastguard Worker  *	The Regents of the University of California.  All rights reserved.
4*8b26181fSAndroid Build Coastguard Worker  *
5*8b26181fSAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
6*8b26181fSAndroid Build Coastguard Worker  * modification, are permitted provided that: (1) source code distributions
7*8b26181fSAndroid Build Coastguard Worker  * retain the above copyright notice and this paragraph in its entirety, (2)
8*8b26181fSAndroid Build Coastguard Worker  * distributions including binary code include the above copyright notice and
9*8b26181fSAndroid Build Coastguard Worker  * this paragraph in its entirety in the documentation or other materials
10*8b26181fSAndroid Build Coastguard Worker  * provided with the distribution, and (3) all advertising materials mentioning
11*8b26181fSAndroid Build Coastguard Worker  * features or use of this software display the following acknowledgement:
12*8b26181fSAndroid Build Coastguard Worker  * ``This product includes software developed by the University of California,
13*8b26181fSAndroid Build Coastguard Worker  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14*8b26181fSAndroid Build Coastguard Worker  * the University nor the names of its contributors may be used to endorse
15*8b26181fSAndroid Build Coastguard Worker  * or promote products derived from this software without specific prior
16*8b26181fSAndroid Build Coastguard Worker  * written permission.
17*8b26181fSAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18*8b26181fSAndroid Build Coastguard Worker  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19*8b26181fSAndroid Build Coastguard Worker  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20*8b26181fSAndroid Build Coastguard Worker  *
21*8b26181fSAndroid Build Coastguard Worker  * sf-pcapng.c - pcapng-file-format-specific code from savefile.c
22*8b26181fSAndroid Build Coastguard Worker  */
23*8b26181fSAndroid Build Coastguard Worker 
24*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_CONFIG_H
25*8b26181fSAndroid Build Coastguard Worker #include <config.h>
26*8b26181fSAndroid Build Coastguard Worker #endif
27*8b26181fSAndroid Build Coastguard Worker 
28*8b26181fSAndroid Build Coastguard Worker #include <pcap/pcap-inttypes.h>
29*8b26181fSAndroid Build Coastguard Worker 
30*8b26181fSAndroid Build Coastguard Worker #include <errno.h>
31*8b26181fSAndroid Build Coastguard Worker #include <memory.h>
32*8b26181fSAndroid Build Coastguard Worker #include <stdio.h>
33*8b26181fSAndroid Build Coastguard Worker #include <stdlib.h>
34*8b26181fSAndroid Build Coastguard Worker #include <string.h>
35*8b26181fSAndroid Build Coastguard Worker 
36*8b26181fSAndroid Build Coastguard Worker #include "pcap-int.h"
37*8b26181fSAndroid Build Coastguard Worker #include "pcap-util.h"
38*8b26181fSAndroid Build Coastguard Worker 
39*8b26181fSAndroid Build Coastguard Worker #include "pcap-common.h"
40*8b26181fSAndroid Build Coastguard Worker 
41*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OS_PROTO_H
42*8b26181fSAndroid Build Coastguard Worker #include "os-proto.h"
43*8b26181fSAndroid Build Coastguard Worker #endif
44*8b26181fSAndroid Build Coastguard Worker 
45*8b26181fSAndroid Build Coastguard Worker #include "sf-pcapng.h"
46*8b26181fSAndroid Build Coastguard Worker 
47*8b26181fSAndroid Build Coastguard Worker /*
48*8b26181fSAndroid Build Coastguard Worker  * Block types.
49*8b26181fSAndroid Build Coastguard Worker  */
50*8b26181fSAndroid Build Coastguard Worker 
51*8b26181fSAndroid Build Coastguard Worker /*
52*8b26181fSAndroid Build Coastguard Worker  * Common part at the beginning of all blocks.
53*8b26181fSAndroid Build Coastguard Worker  */
54*8b26181fSAndroid Build Coastguard Worker struct block_header {
55*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32	block_type;
56*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32	total_length;
57*8b26181fSAndroid Build Coastguard Worker };
58*8b26181fSAndroid Build Coastguard Worker 
59*8b26181fSAndroid Build Coastguard Worker /*
60*8b26181fSAndroid Build Coastguard Worker  * Common trailer at the end of all blocks.
61*8b26181fSAndroid Build Coastguard Worker  */
62*8b26181fSAndroid Build Coastguard Worker struct block_trailer {
63*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32	total_length;
64*8b26181fSAndroid Build Coastguard Worker };
65*8b26181fSAndroid Build Coastguard Worker 
66*8b26181fSAndroid Build Coastguard Worker /*
67*8b26181fSAndroid Build Coastguard Worker  * Common options.
68*8b26181fSAndroid Build Coastguard Worker  */
69*8b26181fSAndroid Build Coastguard Worker #define OPT_ENDOFOPT	0	/* end of options */
70*8b26181fSAndroid Build Coastguard Worker #define OPT_COMMENT	1	/* comment string */
71*8b26181fSAndroid Build Coastguard Worker 
72*8b26181fSAndroid Build Coastguard Worker /*
73*8b26181fSAndroid Build Coastguard Worker  * Option header.
74*8b26181fSAndroid Build Coastguard Worker  */
75*8b26181fSAndroid Build Coastguard Worker struct option_header {
76*8b26181fSAndroid Build Coastguard Worker 	u_short		option_code;
77*8b26181fSAndroid Build Coastguard Worker 	u_short		option_length;
78*8b26181fSAndroid Build Coastguard Worker };
79*8b26181fSAndroid Build Coastguard Worker 
80*8b26181fSAndroid Build Coastguard Worker /*
81*8b26181fSAndroid Build Coastguard Worker  * Structures for the part of each block type following the common
82*8b26181fSAndroid Build Coastguard Worker  * part.
83*8b26181fSAndroid Build Coastguard Worker  */
84*8b26181fSAndroid Build Coastguard Worker 
85*8b26181fSAndroid Build Coastguard Worker /*
86*8b26181fSAndroid Build Coastguard Worker  * Section Header Block.
87*8b26181fSAndroid Build Coastguard Worker  */
88*8b26181fSAndroid Build Coastguard Worker #define BT_SHB			0x0A0D0D0A
89*8b26181fSAndroid Build Coastguard Worker #define BT_SHB_INSANE_MAX       1024U*1024U*1U  /* 1MB should be enough */
90*8b26181fSAndroid Build Coastguard Worker struct section_header_block {
91*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32	byte_order_magic;
92*8b26181fSAndroid Build Coastguard Worker 	u_short		major_version;
93*8b26181fSAndroid Build Coastguard Worker 	u_short		minor_version;
94*8b26181fSAndroid Build Coastguard Worker 	uint64_t	section_length;
95*8b26181fSAndroid Build Coastguard Worker 	/* followed by options and trailer */
96*8b26181fSAndroid Build Coastguard Worker };
97*8b26181fSAndroid Build Coastguard Worker 
98*8b26181fSAndroid Build Coastguard Worker /*
99*8b26181fSAndroid Build Coastguard Worker  * Byte-order magic value.
100*8b26181fSAndroid Build Coastguard Worker  */
101*8b26181fSAndroid Build Coastguard Worker #define BYTE_ORDER_MAGIC	0x1A2B3C4D
102*8b26181fSAndroid Build Coastguard Worker 
103*8b26181fSAndroid Build Coastguard Worker /*
104*8b26181fSAndroid Build Coastguard Worker  * Current version number.  If major_version isn't PCAP_NG_VERSION_MAJOR,
105*8b26181fSAndroid Build Coastguard Worker  * or if minor_version isn't PCAP_NG_VERSION_MINOR or 2, that means that
106*8b26181fSAndroid Build Coastguard Worker  * this code can't read the file.
107*8b26181fSAndroid Build Coastguard Worker  */
108*8b26181fSAndroid Build Coastguard Worker #define PCAP_NG_VERSION_MAJOR	1
109*8b26181fSAndroid Build Coastguard Worker #define PCAP_NG_VERSION_MINOR	0
110*8b26181fSAndroid Build Coastguard Worker 
111*8b26181fSAndroid Build Coastguard Worker /*
112*8b26181fSAndroid Build Coastguard Worker  * Interface Description Block.
113*8b26181fSAndroid Build Coastguard Worker  */
114*8b26181fSAndroid Build Coastguard Worker #define BT_IDB			0x00000001
115*8b26181fSAndroid Build Coastguard Worker 
116*8b26181fSAndroid Build Coastguard Worker struct interface_description_block {
117*8b26181fSAndroid Build Coastguard Worker 	u_short		linktype;
118*8b26181fSAndroid Build Coastguard Worker 	u_short		reserved;
119*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32	snaplen;
120*8b26181fSAndroid Build Coastguard Worker 	/* followed by options and trailer */
121*8b26181fSAndroid Build Coastguard Worker };
122*8b26181fSAndroid Build Coastguard Worker 
123*8b26181fSAndroid Build Coastguard Worker /*
124*8b26181fSAndroid Build Coastguard Worker  * Options in the IDB.
125*8b26181fSAndroid Build Coastguard Worker  */
126*8b26181fSAndroid Build Coastguard Worker #define IF_NAME		2	/* interface name string */
127*8b26181fSAndroid Build Coastguard Worker #define IF_DESCRIPTION	3	/* interface description string */
128*8b26181fSAndroid Build Coastguard Worker #define IF_IPV4ADDR	4	/* interface's IPv4 address and netmask */
129*8b26181fSAndroid Build Coastguard Worker #define IF_IPV6ADDR	5	/* interface's IPv6 address and prefix length */
130*8b26181fSAndroid Build Coastguard Worker #define IF_MACADDR	6	/* interface's MAC address */
131*8b26181fSAndroid Build Coastguard Worker #define IF_EUIADDR	7	/* interface's EUI address */
132*8b26181fSAndroid Build Coastguard Worker #define IF_SPEED	8	/* interface's speed, in bits/s */
133*8b26181fSAndroid Build Coastguard Worker #define IF_TSRESOL	9	/* interface's time stamp resolution */
134*8b26181fSAndroid Build Coastguard Worker #define IF_TZONE	10	/* interface's time zone */
135*8b26181fSAndroid Build Coastguard Worker #define IF_FILTER	11	/* filter used when capturing on interface */
136*8b26181fSAndroid Build Coastguard Worker #define IF_OS		12	/* string OS on which capture on this interface was done */
137*8b26181fSAndroid Build Coastguard Worker #define IF_FCSLEN	13	/* FCS length for this interface */
138*8b26181fSAndroid Build Coastguard Worker #define IF_TSOFFSET	14	/* time stamp offset for this interface */
139*8b26181fSAndroid Build Coastguard Worker 
140*8b26181fSAndroid Build Coastguard Worker /*
141*8b26181fSAndroid Build Coastguard Worker  * Enhanced Packet Block.
142*8b26181fSAndroid Build Coastguard Worker  */
143*8b26181fSAndroid Build Coastguard Worker #define BT_EPB			0x00000006
144*8b26181fSAndroid Build Coastguard Worker 
145*8b26181fSAndroid Build Coastguard Worker struct enhanced_packet_block {
146*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32	interface_id;
147*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32	timestamp_high;
148*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32	timestamp_low;
149*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32	caplen;
150*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32	len;
151*8b26181fSAndroid Build Coastguard Worker 	/* followed by packet data, options, and trailer */
152*8b26181fSAndroid Build Coastguard Worker };
153*8b26181fSAndroid Build Coastguard Worker 
154*8b26181fSAndroid Build Coastguard Worker /*
155*8b26181fSAndroid Build Coastguard Worker  * Simple Packet Block.
156*8b26181fSAndroid Build Coastguard Worker  */
157*8b26181fSAndroid Build Coastguard Worker #define BT_SPB			0x00000003
158*8b26181fSAndroid Build Coastguard Worker 
159*8b26181fSAndroid Build Coastguard Worker struct simple_packet_block {
160*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32	len;
161*8b26181fSAndroid Build Coastguard Worker 	/* followed by packet data and trailer */
162*8b26181fSAndroid Build Coastguard Worker };
163*8b26181fSAndroid Build Coastguard Worker 
164*8b26181fSAndroid Build Coastguard Worker /*
165*8b26181fSAndroid Build Coastguard Worker  * Packet Block.
166*8b26181fSAndroid Build Coastguard Worker  */
167*8b26181fSAndroid Build Coastguard Worker #define BT_PB			0x00000002
168*8b26181fSAndroid Build Coastguard Worker 
169*8b26181fSAndroid Build Coastguard Worker struct packet_block {
170*8b26181fSAndroid Build Coastguard Worker 	u_short		interface_id;
171*8b26181fSAndroid Build Coastguard Worker 	u_short		drops_count;
172*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32	timestamp_high;
173*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32	timestamp_low;
174*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32	caplen;
175*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32	len;
176*8b26181fSAndroid Build Coastguard Worker 	/* followed by packet data, options, and trailer */
177*8b26181fSAndroid Build Coastguard Worker };
178*8b26181fSAndroid Build Coastguard Worker 
179*8b26181fSAndroid Build Coastguard Worker /*
180*8b26181fSAndroid Build Coastguard Worker  * Block cursor - used when processing the contents of a block.
181*8b26181fSAndroid Build Coastguard Worker  * Contains a pointer into the data being processed and a count
182*8b26181fSAndroid Build Coastguard Worker  * of bytes remaining in the block.
183*8b26181fSAndroid Build Coastguard Worker  */
184*8b26181fSAndroid Build Coastguard Worker struct block_cursor {
185*8b26181fSAndroid Build Coastguard Worker 	u_char		*data;
186*8b26181fSAndroid Build Coastguard Worker 	size_t		data_remaining;
187*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32	block_type;
188*8b26181fSAndroid Build Coastguard Worker };
189*8b26181fSAndroid Build Coastguard Worker 
190*8b26181fSAndroid Build Coastguard Worker typedef enum {
191*8b26181fSAndroid Build Coastguard Worker 	PASS_THROUGH,
192*8b26181fSAndroid Build Coastguard Worker 	SCALE_UP_DEC,
193*8b26181fSAndroid Build Coastguard Worker 	SCALE_DOWN_DEC,
194*8b26181fSAndroid Build Coastguard Worker 	SCALE_UP_BIN,
195*8b26181fSAndroid Build Coastguard Worker 	SCALE_DOWN_BIN
196*8b26181fSAndroid Build Coastguard Worker } tstamp_scale_type_t;
197*8b26181fSAndroid Build Coastguard Worker 
198*8b26181fSAndroid Build Coastguard Worker /*
199*8b26181fSAndroid Build Coastguard Worker  * Per-interface information.
200*8b26181fSAndroid Build Coastguard Worker  */
201*8b26181fSAndroid Build Coastguard Worker struct pcap_ng_if {
202*8b26181fSAndroid Build Coastguard Worker 	uint32_t snaplen;		/* snapshot length */
203*8b26181fSAndroid Build Coastguard Worker 	uint64_t tsresol;		/* time stamp resolution */
204*8b26181fSAndroid Build Coastguard Worker 	tstamp_scale_type_t scale_type;	/* how to scale */
205*8b26181fSAndroid Build Coastguard Worker 	uint64_t scale_factor;		/* time stamp scale factor for power-of-10 tsresol */
206*8b26181fSAndroid Build Coastguard Worker 	uint64_t tsoffset;		/* time stamp offset */
207*8b26181fSAndroid Build Coastguard Worker };
208*8b26181fSAndroid Build Coastguard Worker 
209*8b26181fSAndroid Build Coastguard Worker /*
210*8b26181fSAndroid Build Coastguard Worker  * Per-pcap_t private data.
211*8b26181fSAndroid Build Coastguard Worker  *
212*8b26181fSAndroid Build Coastguard Worker  * max_blocksize is the maximum size of a block that we'll accept.  We
213*8b26181fSAndroid Build Coastguard Worker  * reject blocks bigger than this, so we don't consume too much memory
214*8b26181fSAndroid Build Coastguard Worker  * with a truly huge block.  It can change as we see IDBs with different
215*8b26181fSAndroid Build Coastguard Worker  * link-layer header types.  (Currently, we don't support IDBs with
216*8b26181fSAndroid Build Coastguard Worker  * different link-layer header types, but we will support it in the
217*8b26181fSAndroid Build Coastguard Worker  * future, when we offer file-reading APIs that support it.)
218*8b26181fSAndroid Build Coastguard Worker  *
219*8b26181fSAndroid Build Coastguard Worker  * XXX - that's an issue on ILP32 platforms, where the maximum block
220*8b26181fSAndroid Build Coastguard Worker  * size of 2^31-1 would eat all but one byte of the entire address space.
221*8b26181fSAndroid Build Coastguard Worker  * It's less of an issue on ILP64/LLP64 platforms, but the actual size
222*8b26181fSAndroid Build Coastguard Worker  * of the address space may be limited by 1) the number of *significant*
223*8b26181fSAndroid Build Coastguard Worker  * address bits (currently, x86-64 only supports 48 bits of address), 2)
224*8b26181fSAndroid Build Coastguard Worker  * any limitations imposed by the operating system; 3) any limitations
225*8b26181fSAndroid Build Coastguard Worker  * imposed by the amount of available backing store for anonymous pages,
226*8b26181fSAndroid Build Coastguard Worker  * so we impose a limit regardless of the size of a pointer.
227*8b26181fSAndroid Build Coastguard Worker  */
228*8b26181fSAndroid Build Coastguard Worker struct pcap_ng_sf {
229*8b26181fSAndroid Build Coastguard Worker 	uint64_t user_tsresol;		/* time stamp resolution requested by the user */
230*8b26181fSAndroid Build Coastguard Worker 	u_int max_blocksize;		/* don't grow buffer size past this */
231*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32 ifcount;		/* number of interfaces seen in this capture */
232*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32 ifaces_size;	/* size of array below */
233*8b26181fSAndroid Build Coastguard Worker 	struct pcap_ng_if *ifaces;	/* array of interface information */
234*8b26181fSAndroid Build Coastguard Worker };
235*8b26181fSAndroid Build Coastguard Worker 
236*8b26181fSAndroid Build Coastguard Worker /*
237*8b26181fSAndroid Build Coastguard Worker  * The maximum block size we start with; we use an arbitrary value of
238*8b26181fSAndroid Build Coastguard Worker  * 16 MiB.
239*8b26181fSAndroid Build Coastguard Worker  */
240*8b26181fSAndroid Build Coastguard Worker #define INITIAL_MAX_BLOCKSIZE	(16*1024*1024)
241*8b26181fSAndroid Build Coastguard Worker 
242*8b26181fSAndroid Build Coastguard Worker /*
243*8b26181fSAndroid Build Coastguard Worker  * Maximum block size for a given maximum snapshot length; we define it
244*8b26181fSAndroid Build Coastguard Worker  * as the size of an EPB with a max_snaplen-sized packet and 128KB of
245*8b26181fSAndroid Build Coastguard Worker  * options.
246*8b26181fSAndroid Build Coastguard Worker  */
247*8b26181fSAndroid Build Coastguard Worker #define MAX_BLOCKSIZE_FOR_SNAPLEN(max_snaplen) \
248*8b26181fSAndroid Build Coastguard Worker 	(sizeof (struct block_header) + \
249*8b26181fSAndroid Build Coastguard Worker 	 sizeof (struct enhanced_packet_block) + \
250*8b26181fSAndroid Build Coastguard Worker 	 (max_snaplen) + 131072 + \
251*8b26181fSAndroid Build Coastguard Worker 	 sizeof (struct block_trailer))
252*8b26181fSAndroid Build Coastguard Worker 
253*8b26181fSAndroid Build Coastguard Worker static void pcap_ng_cleanup(pcap_t *p);
254*8b26181fSAndroid Build Coastguard Worker static int pcap_ng_next_packet(pcap_t *p, struct pcap_pkthdr *hdr,
255*8b26181fSAndroid Build Coastguard Worker     u_char **data);
256*8b26181fSAndroid Build Coastguard Worker 
257*8b26181fSAndroid Build Coastguard Worker static int
read_bytes(FILE * fp,void * buf,size_t bytes_to_read,int fail_on_eof,char * errbuf)258*8b26181fSAndroid Build Coastguard Worker read_bytes(FILE *fp, void *buf, size_t bytes_to_read, int fail_on_eof,
259*8b26181fSAndroid Build Coastguard Worker     char *errbuf)
260*8b26181fSAndroid Build Coastguard Worker {
261*8b26181fSAndroid Build Coastguard Worker 	size_t amt_read;
262*8b26181fSAndroid Build Coastguard Worker 
263*8b26181fSAndroid Build Coastguard Worker 	amt_read = fread(buf, 1, bytes_to_read, fp);
264*8b26181fSAndroid Build Coastguard Worker 	if (amt_read != bytes_to_read) {
265*8b26181fSAndroid Build Coastguard Worker 		if (ferror(fp)) {
266*8b26181fSAndroid Build Coastguard Worker 			pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
267*8b26181fSAndroid Build Coastguard Worker 			    errno, "error reading dump file");
268*8b26181fSAndroid Build Coastguard Worker 		} else {
269*8b26181fSAndroid Build Coastguard Worker 			if (amt_read == 0 && !fail_on_eof)
270*8b26181fSAndroid Build Coastguard Worker 				return (0);	/* EOF */
271*8b26181fSAndroid Build Coastguard Worker 			snprintf(errbuf, PCAP_ERRBUF_SIZE,
272*8b26181fSAndroid Build Coastguard Worker 			    "truncated pcapng dump file; tried to read %zu bytes, only got %zu",
273*8b26181fSAndroid Build Coastguard Worker 			    bytes_to_read, amt_read);
274*8b26181fSAndroid Build Coastguard Worker 		}
275*8b26181fSAndroid Build Coastguard Worker 		return (-1);
276*8b26181fSAndroid Build Coastguard Worker 	}
277*8b26181fSAndroid Build Coastguard Worker 	return (1);
278*8b26181fSAndroid Build Coastguard Worker }
279*8b26181fSAndroid Build Coastguard Worker 
280*8b26181fSAndroid Build Coastguard Worker static int
read_block(FILE * fp,pcap_t * p,struct block_cursor * cursor,char * errbuf)281*8b26181fSAndroid Build Coastguard Worker read_block(FILE *fp, pcap_t *p, struct block_cursor *cursor, char *errbuf)
282*8b26181fSAndroid Build Coastguard Worker {
283*8b26181fSAndroid Build Coastguard Worker 	struct pcap_ng_sf *ps;
284*8b26181fSAndroid Build Coastguard Worker 	int status;
285*8b26181fSAndroid Build Coastguard Worker 	struct block_header bhdr;
286*8b26181fSAndroid Build Coastguard Worker 	struct block_trailer *btrlr;
287*8b26181fSAndroid Build Coastguard Worker 	u_char *bdata;
288*8b26181fSAndroid Build Coastguard Worker 	size_t data_remaining;
289*8b26181fSAndroid Build Coastguard Worker 
290*8b26181fSAndroid Build Coastguard Worker 	ps = p->priv;
291*8b26181fSAndroid Build Coastguard Worker 
292*8b26181fSAndroid Build Coastguard Worker 	status = read_bytes(fp, &bhdr, sizeof(bhdr), 0, errbuf);
293*8b26181fSAndroid Build Coastguard Worker 	if (status <= 0)
294*8b26181fSAndroid Build Coastguard Worker 		return (status);	/* error or EOF */
295*8b26181fSAndroid Build Coastguard Worker 
296*8b26181fSAndroid Build Coastguard Worker 	if (p->swapped) {
297*8b26181fSAndroid Build Coastguard Worker 		bhdr.block_type = SWAPLONG(bhdr.block_type);
298*8b26181fSAndroid Build Coastguard Worker 		bhdr.total_length = SWAPLONG(bhdr.total_length);
299*8b26181fSAndroid Build Coastguard Worker 	}
300*8b26181fSAndroid Build Coastguard Worker 
301*8b26181fSAndroid Build Coastguard Worker 	/*
302*8b26181fSAndroid Build Coastguard Worker 	 * Is this block "too small" - i.e., is it shorter than a block
303*8b26181fSAndroid Build Coastguard Worker 	 * header plus a block trailer?
304*8b26181fSAndroid Build Coastguard Worker 	 */
305*8b26181fSAndroid Build Coastguard Worker 	if (bhdr.total_length < sizeof(struct block_header) +
306*8b26181fSAndroid Build Coastguard Worker 	    sizeof(struct block_trailer)) {
307*8b26181fSAndroid Build Coastguard Worker 		snprintf(errbuf, PCAP_ERRBUF_SIZE,
308*8b26181fSAndroid Build Coastguard Worker 		    "block in pcapng dump file has a length of %u < %zu",
309*8b26181fSAndroid Build Coastguard Worker 		    bhdr.total_length,
310*8b26181fSAndroid Build Coastguard Worker 		    sizeof(struct block_header) + sizeof(struct block_trailer));
311*8b26181fSAndroid Build Coastguard Worker 		return (-1);
312*8b26181fSAndroid Build Coastguard Worker 	}
313*8b26181fSAndroid Build Coastguard Worker 
314*8b26181fSAndroid Build Coastguard Worker 	/*
315*8b26181fSAndroid Build Coastguard Worker 	 * Is the block total length a multiple of 4?
316*8b26181fSAndroid Build Coastguard Worker 	 */
317*8b26181fSAndroid Build Coastguard Worker 	if ((bhdr.total_length % 4) != 0) {
318*8b26181fSAndroid Build Coastguard Worker 		/*
319*8b26181fSAndroid Build Coastguard Worker 		 * No.  Report that as an error.
320*8b26181fSAndroid Build Coastguard Worker 		 */
321*8b26181fSAndroid Build Coastguard Worker 		snprintf(errbuf, PCAP_ERRBUF_SIZE,
322*8b26181fSAndroid Build Coastguard Worker 		    "block in pcapng dump file has a length of %u that is not a multiple of 4",
323*8b26181fSAndroid Build Coastguard Worker 		    bhdr.total_length);
324*8b26181fSAndroid Build Coastguard Worker 		return (-1);
325*8b26181fSAndroid Build Coastguard Worker 	}
326*8b26181fSAndroid Build Coastguard Worker 
327*8b26181fSAndroid Build Coastguard Worker 	/*
328*8b26181fSAndroid Build Coastguard Worker 	 * Is the buffer big enough?
329*8b26181fSAndroid Build Coastguard Worker 	 */
330*8b26181fSAndroid Build Coastguard Worker 	if (p->bufsize < bhdr.total_length) {
331*8b26181fSAndroid Build Coastguard Worker 		/*
332*8b26181fSAndroid Build Coastguard Worker 		 * No - make it big enough, unless it's too big, in
333*8b26181fSAndroid Build Coastguard Worker 		 * which case we fail.
334*8b26181fSAndroid Build Coastguard Worker 		 */
335*8b26181fSAndroid Build Coastguard Worker 		void *bigger_buffer;
336*8b26181fSAndroid Build Coastguard Worker 
337*8b26181fSAndroid Build Coastguard Worker 		if (bhdr.total_length > ps->max_blocksize) {
338*8b26181fSAndroid Build Coastguard Worker 			snprintf(errbuf, PCAP_ERRBUF_SIZE, "pcapng block size %u > maximum %u", bhdr.total_length,
339*8b26181fSAndroid Build Coastguard Worker 			    ps->max_blocksize);
340*8b26181fSAndroid Build Coastguard Worker 			return (-1);
341*8b26181fSAndroid Build Coastguard Worker 		}
342*8b26181fSAndroid Build Coastguard Worker 		bigger_buffer = realloc(p->buffer, bhdr.total_length);
343*8b26181fSAndroid Build Coastguard Worker 		if (bigger_buffer == NULL) {
344*8b26181fSAndroid Build Coastguard Worker 			snprintf(errbuf, PCAP_ERRBUF_SIZE, "out of memory");
345*8b26181fSAndroid Build Coastguard Worker 			return (-1);
346*8b26181fSAndroid Build Coastguard Worker 		}
347*8b26181fSAndroid Build Coastguard Worker 		p->buffer = bigger_buffer;
348*8b26181fSAndroid Build Coastguard Worker 	}
349*8b26181fSAndroid Build Coastguard Worker 
350*8b26181fSAndroid Build Coastguard Worker 	/*
351*8b26181fSAndroid Build Coastguard Worker 	 * Copy the stuff we've read to the buffer, and read the rest
352*8b26181fSAndroid Build Coastguard Worker 	 * of the block.
353*8b26181fSAndroid Build Coastguard Worker 	 */
354*8b26181fSAndroid Build Coastguard Worker 	memcpy(p->buffer, &bhdr, sizeof(bhdr));
355*8b26181fSAndroid Build Coastguard Worker 	bdata = (u_char *)p->buffer + sizeof(bhdr);
356*8b26181fSAndroid Build Coastguard Worker 	data_remaining = bhdr.total_length - sizeof(bhdr);
357*8b26181fSAndroid Build Coastguard Worker 	if (read_bytes(fp, bdata, data_remaining, 1, errbuf) == -1)
358*8b26181fSAndroid Build Coastguard Worker 		return (-1);
359*8b26181fSAndroid Build Coastguard Worker 
360*8b26181fSAndroid Build Coastguard Worker 	/*
361*8b26181fSAndroid Build Coastguard Worker 	 * Get the block size from the trailer.
362*8b26181fSAndroid Build Coastguard Worker 	 */
363*8b26181fSAndroid Build Coastguard Worker 	btrlr = (struct block_trailer *)(bdata + data_remaining - sizeof (struct block_trailer));
364*8b26181fSAndroid Build Coastguard Worker 	if (p->swapped)
365*8b26181fSAndroid Build Coastguard Worker 		btrlr->total_length = SWAPLONG(btrlr->total_length);
366*8b26181fSAndroid Build Coastguard Worker 
367*8b26181fSAndroid Build Coastguard Worker 	/*
368*8b26181fSAndroid Build Coastguard Worker 	 * Is the total length from the trailer the same as the total
369*8b26181fSAndroid Build Coastguard Worker 	 * length from the header?
370*8b26181fSAndroid Build Coastguard Worker 	 */
371*8b26181fSAndroid Build Coastguard Worker 	if (bhdr.total_length != btrlr->total_length) {
372*8b26181fSAndroid Build Coastguard Worker 		/*
373*8b26181fSAndroid Build Coastguard Worker 		 * No.
374*8b26181fSAndroid Build Coastguard Worker 		 */
375*8b26181fSAndroid Build Coastguard Worker 		snprintf(errbuf, PCAP_ERRBUF_SIZE,
376*8b26181fSAndroid Build Coastguard Worker 		    "block total length in header and trailer don't match");
377*8b26181fSAndroid Build Coastguard Worker 		return (-1);
378*8b26181fSAndroid Build Coastguard Worker 	}
379*8b26181fSAndroid Build Coastguard Worker 
380*8b26181fSAndroid Build Coastguard Worker 	/*
381*8b26181fSAndroid Build Coastguard Worker 	 * Initialize the cursor.
382*8b26181fSAndroid Build Coastguard Worker 	 */
383*8b26181fSAndroid Build Coastguard Worker 	cursor->data = bdata;
384*8b26181fSAndroid Build Coastguard Worker 	cursor->data_remaining = data_remaining - sizeof(struct block_trailer);
385*8b26181fSAndroid Build Coastguard Worker 	cursor->block_type = bhdr.block_type;
386*8b26181fSAndroid Build Coastguard Worker 	return (1);
387*8b26181fSAndroid Build Coastguard Worker }
388*8b26181fSAndroid Build Coastguard Worker 
389*8b26181fSAndroid Build Coastguard Worker static void *
get_from_block_data(struct block_cursor * cursor,size_t chunk_size,char * errbuf)390*8b26181fSAndroid Build Coastguard Worker get_from_block_data(struct block_cursor *cursor, size_t chunk_size,
391*8b26181fSAndroid Build Coastguard Worker     char *errbuf)
392*8b26181fSAndroid Build Coastguard Worker {
393*8b26181fSAndroid Build Coastguard Worker 	void *data;
394*8b26181fSAndroid Build Coastguard Worker 
395*8b26181fSAndroid Build Coastguard Worker 	/*
396*8b26181fSAndroid Build Coastguard Worker 	 * Make sure we have the specified amount of data remaining in
397*8b26181fSAndroid Build Coastguard Worker 	 * the block data.
398*8b26181fSAndroid Build Coastguard Worker 	 */
399*8b26181fSAndroid Build Coastguard Worker 	if (cursor->data_remaining < chunk_size) {
400*8b26181fSAndroid Build Coastguard Worker 		snprintf(errbuf, PCAP_ERRBUF_SIZE,
401*8b26181fSAndroid Build Coastguard Worker 		    "block of type %u in pcapng dump file is too short",
402*8b26181fSAndroid Build Coastguard Worker 		    cursor->block_type);
403*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
404*8b26181fSAndroid Build Coastguard Worker 	}
405*8b26181fSAndroid Build Coastguard Worker 
406*8b26181fSAndroid Build Coastguard Worker 	/*
407*8b26181fSAndroid Build Coastguard Worker 	 * Return the current pointer, and skip past the chunk.
408*8b26181fSAndroid Build Coastguard Worker 	 */
409*8b26181fSAndroid Build Coastguard Worker 	data = cursor->data;
410*8b26181fSAndroid Build Coastguard Worker 	cursor->data += chunk_size;
411*8b26181fSAndroid Build Coastguard Worker 	cursor->data_remaining -= chunk_size;
412*8b26181fSAndroid Build Coastguard Worker 	return (data);
413*8b26181fSAndroid Build Coastguard Worker }
414*8b26181fSAndroid Build Coastguard Worker 
415*8b26181fSAndroid Build Coastguard Worker static struct option_header *
get_opthdr_from_block_data(pcap_t * p,struct block_cursor * cursor,char * errbuf)416*8b26181fSAndroid Build Coastguard Worker get_opthdr_from_block_data(pcap_t *p, struct block_cursor *cursor, char *errbuf)
417*8b26181fSAndroid Build Coastguard Worker {
418*8b26181fSAndroid Build Coastguard Worker 	struct option_header *opthdr;
419*8b26181fSAndroid Build Coastguard Worker 
420*8b26181fSAndroid Build Coastguard Worker 	opthdr = get_from_block_data(cursor, sizeof(*opthdr), errbuf);
421*8b26181fSAndroid Build Coastguard Worker 	if (opthdr == NULL) {
422*8b26181fSAndroid Build Coastguard Worker 		/*
423*8b26181fSAndroid Build Coastguard Worker 		 * Option header is cut short.
424*8b26181fSAndroid Build Coastguard Worker 		 */
425*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
426*8b26181fSAndroid Build Coastguard Worker 	}
427*8b26181fSAndroid Build Coastguard Worker 
428*8b26181fSAndroid Build Coastguard Worker 	/*
429*8b26181fSAndroid Build Coastguard Worker 	 * Byte-swap it if necessary.
430*8b26181fSAndroid Build Coastguard Worker 	 */
431*8b26181fSAndroid Build Coastguard Worker 	if (p->swapped) {
432*8b26181fSAndroid Build Coastguard Worker 		opthdr->option_code = SWAPSHORT(opthdr->option_code);
433*8b26181fSAndroid Build Coastguard Worker 		opthdr->option_length = SWAPSHORT(opthdr->option_length);
434*8b26181fSAndroid Build Coastguard Worker 	}
435*8b26181fSAndroid Build Coastguard Worker 
436*8b26181fSAndroid Build Coastguard Worker 	return (opthdr);
437*8b26181fSAndroid Build Coastguard Worker }
438*8b26181fSAndroid Build Coastguard Worker 
439*8b26181fSAndroid Build Coastguard Worker static void *
get_optvalue_from_block_data(struct block_cursor * cursor,struct option_header * opthdr,char * errbuf)440*8b26181fSAndroid Build Coastguard Worker get_optvalue_from_block_data(struct block_cursor *cursor,
441*8b26181fSAndroid Build Coastguard Worker     struct option_header *opthdr, char *errbuf)
442*8b26181fSAndroid Build Coastguard Worker {
443*8b26181fSAndroid Build Coastguard Worker 	size_t padded_option_len;
444*8b26181fSAndroid Build Coastguard Worker 	void *optvalue;
445*8b26181fSAndroid Build Coastguard Worker 
446*8b26181fSAndroid Build Coastguard Worker 	/* Pad option length to 4-byte boundary */
447*8b26181fSAndroid Build Coastguard Worker 	padded_option_len = opthdr->option_length;
448*8b26181fSAndroid Build Coastguard Worker 	padded_option_len = ((padded_option_len + 3)/4)*4;
449*8b26181fSAndroid Build Coastguard Worker 
450*8b26181fSAndroid Build Coastguard Worker 	optvalue = get_from_block_data(cursor, padded_option_len, errbuf);
451*8b26181fSAndroid Build Coastguard Worker 	if (optvalue == NULL) {
452*8b26181fSAndroid Build Coastguard Worker 		/*
453*8b26181fSAndroid Build Coastguard Worker 		 * Option value is cut short.
454*8b26181fSAndroid Build Coastguard Worker 		 */
455*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
456*8b26181fSAndroid Build Coastguard Worker 	}
457*8b26181fSAndroid Build Coastguard Worker 
458*8b26181fSAndroid Build Coastguard Worker 	return (optvalue);
459*8b26181fSAndroid Build Coastguard Worker }
460*8b26181fSAndroid Build Coastguard Worker 
461*8b26181fSAndroid Build Coastguard Worker static int
process_idb_options(pcap_t * p,struct block_cursor * cursor,uint64_t * tsresol,uint64_t * tsoffset,int * is_binary,char * errbuf)462*8b26181fSAndroid Build Coastguard Worker process_idb_options(pcap_t *p, struct block_cursor *cursor, uint64_t *tsresol,
463*8b26181fSAndroid Build Coastguard Worker     uint64_t *tsoffset, int *is_binary, char *errbuf)
464*8b26181fSAndroid Build Coastguard Worker {
465*8b26181fSAndroid Build Coastguard Worker 	struct option_header *opthdr;
466*8b26181fSAndroid Build Coastguard Worker 	void *optvalue;
467*8b26181fSAndroid Build Coastguard Worker 	int saw_tsresol, saw_tsoffset;
468*8b26181fSAndroid Build Coastguard Worker 	uint8_t tsresol_opt;
469*8b26181fSAndroid Build Coastguard Worker 	u_int i;
470*8b26181fSAndroid Build Coastguard Worker 
471*8b26181fSAndroid Build Coastguard Worker 	saw_tsresol = 0;
472*8b26181fSAndroid Build Coastguard Worker 	saw_tsoffset = 0;
473*8b26181fSAndroid Build Coastguard Worker 	while (cursor->data_remaining != 0) {
474*8b26181fSAndroid Build Coastguard Worker 		/*
475*8b26181fSAndroid Build Coastguard Worker 		 * Get the option header.
476*8b26181fSAndroid Build Coastguard Worker 		 */
477*8b26181fSAndroid Build Coastguard Worker 		opthdr = get_opthdr_from_block_data(p, cursor, errbuf);
478*8b26181fSAndroid Build Coastguard Worker 		if (opthdr == NULL) {
479*8b26181fSAndroid Build Coastguard Worker 			/*
480*8b26181fSAndroid Build Coastguard Worker 			 * Option header is cut short.
481*8b26181fSAndroid Build Coastguard Worker 			 */
482*8b26181fSAndroid Build Coastguard Worker 			return (-1);
483*8b26181fSAndroid Build Coastguard Worker 		}
484*8b26181fSAndroid Build Coastguard Worker 
485*8b26181fSAndroid Build Coastguard Worker 		/*
486*8b26181fSAndroid Build Coastguard Worker 		 * Get option value.
487*8b26181fSAndroid Build Coastguard Worker 		 */
488*8b26181fSAndroid Build Coastguard Worker 		optvalue = get_optvalue_from_block_data(cursor, opthdr,
489*8b26181fSAndroid Build Coastguard Worker 		    errbuf);
490*8b26181fSAndroid Build Coastguard Worker 		if (optvalue == NULL) {
491*8b26181fSAndroid Build Coastguard Worker 			/*
492*8b26181fSAndroid Build Coastguard Worker 			 * Option value is cut short.
493*8b26181fSAndroid Build Coastguard Worker 			 */
494*8b26181fSAndroid Build Coastguard Worker 			return (-1);
495*8b26181fSAndroid Build Coastguard Worker 		}
496*8b26181fSAndroid Build Coastguard Worker 
497*8b26181fSAndroid Build Coastguard Worker 		switch (opthdr->option_code) {
498*8b26181fSAndroid Build Coastguard Worker 
499*8b26181fSAndroid Build Coastguard Worker 		case OPT_ENDOFOPT:
500*8b26181fSAndroid Build Coastguard Worker 			if (opthdr->option_length != 0) {
501*8b26181fSAndroid Build Coastguard Worker 				snprintf(errbuf, PCAP_ERRBUF_SIZE,
502*8b26181fSAndroid Build Coastguard Worker 				    "Interface Description Block has opt_endofopt option with length %u != 0",
503*8b26181fSAndroid Build Coastguard Worker 				    opthdr->option_length);
504*8b26181fSAndroid Build Coastguard Worker 				return (-1);
505*8b26181fSAndroid Build Coastguard Worker 			}
506*8b26181fSAndroid Build Coastguard Worker 			goto done;
507*8b26181fSAndroid Build Coastguard Worker 
508*8b26181fSAndroid Build Coastguard Worker 		case IF_TSRESOL:
509*8b26181fSAndroid Build Coastguard Worker 			if (opthdr->option_length != 1) {
510*8b26181fSAndroid Build Coastguard Worker 				snprintf(errbuf, PCAP_ERRBUF_SIZE,
511*8b26181fSAndroid Build Coastguard Worker 				    "Interface Description Block has if_tsresol option with length %u != 1",
512*8b26181fSAndroid Build Coastguard Worker 				    opthdr->option_length);
513*8b26181fSAndroid Build Coastguard Worker 				return (-1);
514*8b26181fSAndroid Build Coastguard Worker 			}
515*8b26181fSAndroid Build Coastguard Worker 			if (saw_tsresol) {
516*8b26181fSAndroid Build Coastguard Worker 				snprintf(errbuf, PCAP_ERRBUF_SIZE,
517*8b26181fSAndroid Build Coastguard Worker 				    "Interface Description Block has more than one if_tsresol option");
518*8b26181fSAndroid Build Coastguard Worker 				return (-1);
519*8b26181fSAndroid Build Coastguard Worker 			}
520*8b26181fSAndroid Build Coastguard Worker 			saw_tsresol = 1;
521*8b26181fSAndroid Build Coastguard Worker 			memcpy(&tsresol_opt, optvalue, sizeof(tsresol_opt));
522*8b26181fSAndroid Build Coastguard Worker 			if (tsresol_opt & 0x80) {
523*8b26181fSAndroid Build Coastguard Worker 				/*
524*8b26181fSAndroid Build Coastguard Worker 				 * Resolution is negative power of 2.
525*8b26181fSAndroid Build Coastguard Worker 				 */
526*8b26181fSAndroid Build Coastguard Worker 				uint8_t tsresol_shift = (tsresol_opt & 0x7F);
527*8b26181fSAndroid Build Coastguard Worker 
528*8b26181fSAndroid Build Coastguard Worker 				if (tsresol_shift > 63) {
529*8b26181fSAndroid Build Coastguard Worker 					/*
530*8b26181fSAndroid Build Coastguard Worker 					 * Resolution is too high; 2^-{res}
531*8b26181fSAndroid Build Coastguard Worker 					 * won't fit in a 64-bit value.
532*8b26181fSAndroid Build Coastguard Worker 					 */
533*8b26181fSAndroid Build Coastguard Worker 					snprintf(errbuf, PCAP_ERRBUF_SIZE,
534*8b26181fSAndroid Build Coastguard Worker 					    "Interface Description Block if_tsresol option resolution 2^-%u is too high",
535*8b26181fSAndroid Build Coastguard Worker 					    tsresol_shift);
536*8b26181fSAndroid Build Coastguard Worker 					return (-1);
537*8b26181fSAndroid Build Coastguard Worker 				}
538*8b26181fSAndroid Build Coastguard Worker 				*is_binary = 1;
539*8b26181fSAndroid Build Coastguard Worker 				*tsresol = ((uint64_t)1) << tsresol_shift;
540*8b26181fSAndroid Build Coastguard Worker 			} else {
541*8b26181fSAndroid Build Coastguard Worker 				/*
542*8b26181fSAndroid Build Coastguard Worker 				 * Resolution is negative power of 10.
543*8b26181fSAndroid Build Coastguard Worker 				 */
544*8b26181fSAndroid Build Coastguard Worker 				if (tsresol_opt > 19) {
545*8b26181fSAndroid Build Coastguard Worker 					/*
546*8b26181fSAndroid Build Coastguard Worker 					 * Resolution is too high; 2^-{res}
547*8b26181fSAndroid Build Coastguard Worker 					 * won't fit in a 64-bit value (the
548*8b26181fSAndroid Build Coastguard Worker 					 * largest power of 10 that fits
549*8b26181fSAndroid Build Coastguard Worker 					 * in a 64-bit value is 10^19, as
550*8b26181fSAndroid Build Coastguard Worker 					 * the largest 64-bit unsigned
551*8b26181fSAndroid Build Coastguard Worker 					 * value is ~1.8*10^19).
552*8b26181fSAndroid Build Coastguard Worker 					 */
553*8b26181fSAndroid Build Coastguard Worker 					snprintf(errbuf, PCAP_ERRBUF_SIZE,
554*8b26181fSAndroid Build Coastguard Worker 					    "Interface Description Block if_tsresol option resolution 10^-%u is too high",
555*8b26181fSAndroid Build Coastguard Worker 					    tsresol_opt);
556*8b26181fSAndroid Build Coastguard Worker 					return (-1);
557*8b26181fSAndroid Build Coastguard Worker 				}
558*8b26181fSAndroid Build Coastguard Worker 				*is_binary = 0;
559*8b26181fSAndroid Build Coastguard Worker 				*tsresol = 1;
560*8b26181fSAndroid Build Coastguard Worker 				for (i = 0; i < tsresol_opt; i++)
561*8b26181fSAndroid Build Coastguard Worker 					*tsresol *= 10;
562*8b26181fSAndroid Build Coastguard Worker 			}
563*8b26181fSAndroid Build Coastguard Worker 			break;
564*8b26181fSAndroid Build Coastguard Worker 
565*8b26181fSAndroid Build Coastguard Worker 		case IF_TSOFFSET:
566*8b26181fSAndroid Build Coastguard Worker 			if (opthdr->option_length != 8) {
567*8b26181fSAndroid Build Coastguard Worker 				snprintf(errbuf, PCAP_ERRBUF_SIZE,
568*8b26181fSAndroid Build Coastguard Worker 				    "Interface Description Block has if_tsoffset option with length %u != 8",
569*8b26181fSAndroid Build Coastguard Worker 				    opthdr->option_length);
570*8b26181fSAndroid Build Coastguard Worker 				return (-1);
571*8b26181fSAndroid Build Coastguard Worker 			}
572*8b26181fSAndroid Build Coastguard Worker 			if (saw_tsoffset) {
573*8b26181fSAndroid Build Coastguard Worker 				snprintf(errbuf, PCAP_ERRBUF_SIZE,
574*8b26181fSAndroid Build Coastguard Worker 				    "Interface Description Block has more than one if_tsoffset option");
575*8b26181fSAndroid Build Coastguard Worker 				return (-1);
576*8b26181fSAndroid Build Coastguard Worker 			}
577*8b26181fSAndroid Build Coastguard Worker 			saw_tsoffset = 1;
578*8b26181fSAndroid Build Coastguard Worker 			memcpy(tsoffset, optvalue, sizeof(*tsoffset));
579*8b26181fSAndroid Build Coastguard Worker 			if (p->swapped)
580*8b26181fSAndroid Build Coastguard Worker 				*tsoffset = SWAPLL(*tsoffset);
581*8b26181fSAndroid Build Coastguard Worker 			break;
582*8b26181fSAndroid Build Coastguard Worker 
583*8b26181fSAndroid Build Coastguard Worker 		default:
584*8b26181fSAndroid Build Coastguard Worker 			break;
585*8b26181fSAndroid Build Coastguard Worker 		}
586*8b26181fSAndroid Build Coastguard Worker 	}
587*8b26181fSAndroid Build Coastguard Worker 
588*8b26181fSAndroid Build Coastguard Worker done:
589*8b26181fSAndroid Build Coastguard Worker 	return (0);
590*8b26181fSAndroid Build Coastguard Worker }
591*8b26181fSAndroid Build Coastguard Worker 
592*8b26181fSAndroid Build Coastguard Worker static int
add_interface(pcap_t * p,struct interface_description_block * idbp,struct block_cursor * cursor,char * errbuf)593*8b26181fSAndroid Build Coastguard Worker add_interface(pcap_t *p, struct interface_description_block *idbp,
594*8b26181fSAndroid Build Coastguard Worker     struct block_cursor *cursor, char *errbuf)
595*8b26181fSAndroid Build Coastguard Worker {
596*8b26181fSAndroid Build Coastguard Worker 	struct pcap_ng_sf *ps;
597*8b26181fSAndroid Build Coastguard Worker 	uint64_t tsresol;
598*8b26181fSAndroid Build Coastguard Worker 	uint64_t tsoffset;
599*8b26181fSAndroid Build Coastguard Worker 	int is_binary;
600*8b26181fSAndroid Build Coastguard Worker 
601*8b26181fSAndroid Build Coastguard Worker 	ps = p->priv;
602*8b26181fSAndroid Build Coastguard Worker 
603*8b26181fSAndroid Build Coastguard Worker 	/*
604*8b26181fSAndroid Build Coastguard Worker 	 * Count this interface.
605*8b26181fSAndroid Build Coastguard Worker 	 */
606*8b26181fSAndroid Build Coastguard Worker 	ps->ifcount++;
607*8b26181fSAndroid Build Coastguard Worker 
608*8b26181fSAndroid Build Coastguard Worker 	/*
609*8b26181fSAndroid Build Coastguard Worker 	 * Grow the array of per-interface information as necessary.
610*8b26181fSAndroid Build Coastguard Worker 	 */
611*8b26181fSAndroid Build Coastguard Worker 	if (ps->ifcount > ps->ifaces_size) {
612*8b26181fSAndroid Build Coastguard Worker 		/*
613*8b26181fSAndroid Build Coastguard Worker 		 * We need to grow the array.
614*8b26181fSAndroid Build Coastguard Worker 		 */
615*8b26181fSAndroid Build Coastguard Worker 		bpf_u_int32 new_ifaces_size;
616*8b26181fSAndroid Build Coastguard Worker 		struct pcap_ng_if *new_ifaces;
617*8b26181fSAndroid Build Coastguard Worker 
618*8b26181fSAndroid Build Coastguard Worker 		if (ps->ifaces_size == 0) {
619*8b26181fSAndroid Build Coastguard Worker 			/*
620*8b26181fSAndroid Build Coastguard Worker 			 * It's currently empty.
621*8b26181fSAndroid Build Coastguard Worker 			 *
622*8b26181fSAndroid Build Coastguard Worker 			 * (The Clang static analyzer doesn't do enough,
623*8b26181fSAndroid Build Coastguard Worker 			 * err, umm, dataflow *analysis* to realize that
624*8b26181fSAndroid Build Coastguard Worker 			 * ps->ifaces_size == 0 if ps->ifaces == NULL,
625*8b26181fSAndroid Build Coastguard Worker 			 * and so complains about a possible zero argument
626*8b26181fSAndroid Build Coastguard Worker 			 * to realloc(), so we check for the former
627*8b26181fSAndroid Build Coastguard Worker 			 * condition to shut it up.
628*8b26181fSAndroid Build Coastguard Worker 			 *
629*8b26181fSAndroid Build Coastguard Worker 			 * However, it doesn't complain that one of the
630*8b26181fSAndroid Build Coastguard Worker 			 * multiplications below could overflow, which is
631*8b26181fSAndroid Build Coastguard Worker 			 * a real, albeit extremely unlikely, problem (you'd
632*8b26181fSAndroid Build Coastguard Worker 			 * need a pcapng file with tens of millions of
633*8b26181fSAndroid Build Coastguard Worker 			 * interfaces).)
634*8b26181fSAndroid Build Coastguard Worker 			 */
635*8b26181fSAndroid Build Coastguard Worker 			new_ifaces_size = 1;
636*8b26181fSAndroid Build Coastguard Worker 			new_ifaces = malloc(sizeof (struct pcap_ng_if));
637*8b26181fSAndroid Build Coastguard Worker 		} else {
638*8b26181fSAndroid Build Coastguard Worker 			/*
639*8b26181fSAndroid Build Coastguard Worker 			 * It's not currently empty; double its size.
640*8b26181fSAndroid Build Coastguard Worker 			 * (Perhaps overkill once we have a lot of interfaces.)
641*8b26181fSAndroid Build Coastguard Worker 			 *
642*8b26181fSAndroid Build Coastguard Worker 			 * Check for overflow if we double it.
643*8b26181fSAndroid Build Coastguard Worker 			 */
644*8b26181fSAndroid Build Coastguard Worker 			if (ps->ifaces_size * 2 < ps->ifaces_size) {
645*8b26181fSAndroid Build Coastguard Worker 				/*
646*8b26181fSAndroid Build Coastguard Worker 				 * The maximum number of interfaces before
647*8b26181fSAndroid Build Coastguard Worker 				 * ps->ifaces_size overflows is the largest
648*8b26181fSAndroid Build Coastguard Worker 				 * possible 32-bit power of 2, as we do
649*8b26181fSAndroid Build Coastguard Worker 				 * size doubling.
650*8b26181fSAndroid Build Coastguard Worker 				 */
651*8b26181fSAndroid Build Coastguard Worker 				snprintf(errbuf, PCAP_ERRBUF_SIZE,
652*8b26181fSAndroid Build Coastguard Worker 				    "more than %u interfaces in the file",
653*8b26181fSAndroid Build Coastguard Worker 				    0x80000000U);
654*8b26181fSAndroid Build Coastguard Worker 				return (0);
655*8b26181fSAndroid Build Coastguard Worker 			}
656*8b26181fSAndroid Build Coastguard Worker 
657*8b26181fSAndroid Build Coastguard Worker 			/*
658*8b26181fSAndroid Build Coastguard Worker 			 * ps->ifaces_size * 2 doesn't overflow, so it's
659*8b26181fSAndroid Build Coastguard Worker 			 * safe to multiply.
660*8b26181fSAndroid Build Coastguard Worker 			 */
661*8b26181fSAndroid Build Coastguard Worker 			new_ifaces_size = ps->ifaces_size * 2;
662*8b26181fSAndroid Build Coastguard Worker 
663*8b26181fSAndroid Build Coastguard Worker 			/*
664*8b26181fSAndroid Build Coastguard Worker 			 * Now make sure that's not so big that it overflows
665*8b26181fSAndroid Build Coastguard Worker 			 * if we multiply by sizeof (struct pcap_ng_if).
666*8b26181fSAndroid Build Coastguard Worker 			 *
667*8b26181fSAndroid Build Coastguard Worker 			 * That can happen on 32-bit platforms, with a 32-bit
668*8b26181fSAndroid Build Coastguard Worker 			 * size_t; it shouldn't happen on 64-bit platforms,
669*8b26181fSAndroid Build Coastguard Worker 			 * with a 64-bit size_t, as new_ifaces_size is
670*8b26181fSAndroid Build Coastguard Worker 			 * 32 bits.
671*8b26181fSAndroid Build Coastguard Worker 			 */
672*8b26181fSAndroid Build Coastguard Worker 			if (new_ifaces_size * sizeof (struct pcap_ng_if) < new_ifaces_size) {
673*8b26181fSAndroid Build Coastguard Worker 				/*
674*8b26181fSAndroid Build Coastguard Worker 				 * As this fails only with 32-bit size_t,
675*8b26181fSAndroid Build Coastguard Worker 				 * the multiplication was 32x32->32, and
676*8b26181fSAndroid Build Coastguard Worker 				 * the largest 32-bit value that can safely
677*8b26181fSAndroid Build Coastguard Worker 				 * be multiplied by sizeof (struct pcap_ng_if)
678*8b26181fSAndroid Build Coastguard Worker 				 * without overflow is the largest 32-bit
679*8b26181fSAndroid Build Coastguard Worker 				 * (unsigned) value divided by
680*8b26181fSAndroid Build Coastguard Worker 				 * sizeof (struct pcap_ng_if).
681*8b26181fSAndroid Build Coastguard Worker 				 */
682*8b26181fSAndroid Build Coastguard Worker 				snprintf(errbuf, PCAP_ERRBUF_SIZE,
683*8b26181fSAndroid Build Coastguard Worker 				    "more than %u interfaces in the file",
684*8b26181fSAndroid Build Coastguard Worker 				    0xFFFFFFFFU / ((u_int)sizeof (struct pcap_ng_if)));
685*8b26181fSAndroid Build Coastguard Worker 				return (0);
686*8b26181fSAndroid Build Coastguard Worker 			}
687*8b26181fSAndroid Build Coastguard Worker 			new_ifaces = realloc(ps->ifaces, new_ifaces_size * sizeof (struct pcap_ng_if));
688*8b26181fSAndroid Build Coastguard Worker 		}
689*8b26181fSAndroid Build Coastguard Worker 		if (new_ifaces == NULL) {
690*8b26181fSAndroid Build Coastguard Worker 			/*
691*8b26181fSAndroid Build Coastguard Worker 			 * We ran out of memory.
692*8b26181fSAndroid Build Coastguard Worker 			 * Give up.
693*8b26181fSAndroid Build Coastguard Worker 			 */
694*8b26181fSAndroid Build Coastguard Worker 			snprintf(errbuf, PCAP_ERRBUF_SIZE,
695*8b26181fSAndroid Build Coastguard Worker 			    "out of memory for per-interface information (%u interfaces)",
696*8b26181fSAndroid Build Coastguard Worker 			    ps->ifcount);
697*8b26181fSAndroid Build Coastguard Worker 			return (0);
698*8b26181fSAndroid Build Coastguard Worker 		}
699*8b26181fSAndroid Build Coastguard Worker 		ps->ifaces_size = new_ifaces_size;
700*8b26181fSAndroid Build Coastguard Worker 		ps->ifaces = new_ifaces;
701*8b26181fSAndroid Build Coastguard Worker 	}
702*8b26181fSAndroid Build Coastguard Worker 
703*8b26181fSAndroid Build Coastguard Worker 	ps->ifaces[ps->ifcount - 1].snaplen = idbp->snaplen;
704*8b26181fSAndroid Build Coastguard Worker 
705*8b26181fSAndroid Build Coastguard Worker 	/*
706*8b26181fSAndroid Build Coastguard Worker 	 * Set the default time stamp resolution and offset.
707*8b26181fSAndroid Build Coastguard Worker 	 */
708*8b26181fSAndroid Build Coastguard Worker 	tsresol = 1000000;	/* microsecond resolution */
709*8b26181fSAndroid Build Coastguard Worker 	is_binary = 0;		/* which is a power of 10 */
710*8b26181fSAndroid Build Coastguard Worker 	tsoffset = 0;		/* absolute timestamps */
711*8b26181fSAndroid Build Coastguard Worker 
712*8b26181fSAndroid Build Coastguard Worker 	/*
713*8b26181fSAndroid Build Coastguard Worker 	 * Now look for various time stamp options, so we know
714*8b26181fSAndroid Build Coastguard Worker 	 * how to interpret the time stamps for this interface.
715*8b26181fSAndroid Build Coastguard Worker 	 */
716*8b26181fSAndroid Build Coastguard Worker 	if (process_idb_options(p, cursor, &tsresol, &tsoffset, &is_binary,
717*8b26181fSAndroid Build Coastguard Worker 	    errbuf) == -1)
718*8b26181fSAndroid Build Coastguard Worker 		return (0);
719*8b26181fSAndroid Build Coastguard Worker 
720*8b26181fSAndroid Build Coastguard Worker 	ps->ifaces[ps->ifcount - 1].tsresol = tsresol;
721*8b26181fSAndroid Build Coastguard Worker 	ps->ifaces[ps->ifcount - 1].tsoffset = tsoffset;
722*8b26181fSAndroid Build Coastguard Worker 
723*8b26181fSAndroid Build Coastguard Worker 	/*
724*8b26181fSAndroid Build Coastguard Worker 	 * Determine whether we're scaling up or down or not
725*8b26181fSAndroid Build Coastguard Worker 	 * at all for this interface.
726*8b26181fSAndroid Build Coastguard Worker 	 */
727*8b26181fSAndroid Build Coastguard Worker 	if (tsresol == ps->user_tsresol) {
728*8b26181fSAndroid Build Coastguard Worker 		/*
729*8b26181fSAndroid Build Coastguard Worker 		 * The resolution is the resolution the user wants,
730*8b26181fSAndroid Build Coastguard Worker 		 * so we don't have to do scaling.
731*8b26181fSAndroid Build Coastguard Worker 		 */
732*8b26181fSAndroid Build Coastguard Worker 		ps->ifaces[ps->ifcount - 1].scale_type = PASS_THROUGH;
733*8b26181fSAndroid Build Coastguard Worker 	} else if (tsresol > ps->user_tsresol) {
734*8b26181fSAndroid Build Coastguard Worker 		/*
735*8b26181fSAndroid Build Coastguard Worker 		 * The resolution is greater than what the user wants,
736*8b26181fSAndroid Build Coastguard Worker 		 * so we have to scale the timestamps down.
737*8b26181fSAndroid Build Coastguard Worker 		 */
738*8b26181fSAndroid Build Coastguard Worker 		if (is_binary)
739*8b26181fSAndroid Build Coastguard Worker 			ps->ifaces[ps->ifcount - 1].scale_type = SCALE_DOWN_BIN;
740*8b26181fSAndroid Build Coastguard Worker 		else {
741*8b26181fSAndroid Build Coastguard Worker 			/*
742*8b26181fSAndroid Build Coastguard Worker 			 * Calculate the scale factor.
743*8b26181fSAndroid Build Coastguard Worker 			 */
744*8b26181fSAndroid Build Coastguard Worker 			ps->ifaces[ps->ifcount - 1].scale_factor = tsresol/ps->user_tsresol;
745*8b26181fSAndroid Build Coastguard Worker 			ps->ifaces[ps->ifcount - 1].scale_type = SCALE_DOWN_DEC;
746*8b26181fSAndroid Build Coastguard Worker 		}
747*8b26181fSAndroid Build Coastguard Worker 	} else {
748*8b26181fSAndroid Build Coastguard Worker 		/*
749*8b26181fSAndroid Build Coastguard Worker 		 * The resolution is less than what the user wants,
750*8b26181fSAndroid Build Coastguard Worker 		 * so we have to scale the timestamps up.
751*8b26181fSAndroid Build Coastguard Worker 		 */
752*8b26181fSAndroid Build Coastguard Worker 		if (is_binary)
753*8b26181fSAndroid Build Coastguard Worker 			ps->ifaces[ps->ifcount - 1].scale_type = SCALE_UP_BIN;
754*8b26181fSAndroid Build Coastguard Worker 		else {
755*8b26181fSAndroid Build Coastguard Worker 			/*
756*8b26181fSAndroid Build Coastguard Worker 			 * Calculate the scale factor.
757*8b26181fSAndroid Build Coastguard Worker 			 */
758*8b26181fSAndroid Build Coastguard Worker 			ps->ifaces[ps->ifcount - 1].scale_factor = ps->user_tsresol/tsresol;
759*8b26181fSAndroid Build Coastguard Worker 			ps->ifaces[ps->ifcount - 1].scale_type = SCALE_UP_DEC;
760*8b26181fSAndroid Build Coastguard Worker 		}
761*8b26181fSAndroid Build Coastguard Worker 	}
762*8b26181fSAndroid Build Coastguard Worker 	return (1);
763*8b26181fSAndroid Build Coastguard Worker }
764*8b26181fSAndroid Build Coastguard Worker 
765*8b26181fSAndroid Build Coastguard Worker /*
766*8b26181fSAndroid Build Coastguard Worker  * Check whether this is a pcapng savefile and, if it is, extract the
767*8b26181fSAndroid Build Coastguard Worker  * relevant information from the header.
768*8b26181fSAndroid Build Coastguard Worker  */
769*8b26181fSAndroid Build Coastguard Worker pcap_t *
pcap_ng_check_header(const uint8_t * magic,FILE * fp,u_int precision,char * errbuf,int * err)770*8b26181fSAndroid Build Coastguard Worker pcap_ng_check_header(const uint8_t *magic, FILE *fp, u_int precision,
771*8b26181fSAndroid Build Coastguard Worker     char *errbuf, int *err)
772*8b26181fSAndroid Build Coastguard Worker {
773*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32 magic_int;
774*8b26181fSAndroid Build Coastguard Worker 	size_t amt_read;
775*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32 total_length;
776*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32 byte_order_magic;
777*8b26181fSAndroid Build Coastguard Worker 	struct block_header *bhdrp;
778*8b26181fSAndroid Build Coastguard Worker 	struct section_header_block *shbp;
779*8b26181fSAndroid Build Coastguard Worker 	pcap_t *p;
780*8b26181fSAndroid Build Coastguard Worker 	int swapped = 0;
781*8b26181fSAndroid Build Coastguard Worker 	struct pcap_ng_sf *ps;
782*8b26181fSAndroid Build Coastguard Worker 	int status;
783*8b26181fSAndroid Build Coastguard Worker 	struct block_cursor cursor;
784*8b26181fSAndroid Build Coastguard Worker 	struct interface_description_block *idbp;
785*8b26181fSAndroid Build Coastguard Worker 
786*8b26181fSAndroid Build Coastguard Worker 	/*
787*8b26181fSAndroid Build Coastguard Worker 	 * Assume no read errors.
788*8b26181fSAndroid Build Coastguard Worker 	 */
789*8b26181fSAndroid Build Coastguard Worker 	*err = 0;
790*8b26181fSAndroid Build Coastguard Worker 
791*8b26181fSAndroid Build Coastguard Worker 	/*
792*8b26181fSAndroid Build Coastguard Worker 	 * Check whether the first 4 bytes of the file are the block
793*8b26181fSAndroid Build Coastguard Worker 	 * type for a pcapng savefile.
794*8b26181fSAndroid Build Coastguard Worker 	 */
795*8b26181fSAndroid Build Coastguard Worker 	memcpy(&magic_int, magic, sizeof(magic_int));
796*8b26181fSAndroid Build Coastguard Worker 	if (magic_int != BT_SHB) {
797*8b26181fSAndroid Build Coastguard Worker 		/*
798*8b26181fSAndroid Build Coastguard Worker 		 * XXX - check whether this looks like what the block
799*8b26181fSAndroid Build Coastguard Worker 		 * type would be after being munged by mapping between
800*8b26181fSAndroid Build Coastguard Worker 		 * UN*X and DOS/Windows text file format and, if it
801*8b26181fSAndroid Build Coastguard Worker 		 * does, look for the byte-order magic number in
802*8b26181fSAndroid Build Coastguard Worker 		 * the appropriate place and, if we find it, report
803*8b26181fSAndroid Build Coastguard Worker 		 * this as possibly being a pcapng file transferred
804*8b26181fSAndroid Build Coastguard Worker 		 * between UN*X and Windows in text file format?
805*8b26181fSAndroid Build Coastguard Worker 		 */
806*8b26181fSAndroid Build Coastguard Worker 		return (NULL);	/* nope */
807*8b26181fSAndroid Build Coastguard Worker 	}
808*8b26181fSAndroid Build Coastguard Worker 
809*8b26181fSAndroid Build Coastguard Worker 	/*
810*8b26181fSAndroid Build Coastguard Worker 	 * OK, they are.  However, that's just \n\r\r\n, so it could,
811*8b26181fSAndroid Build Coastguard Worker 	 * conceivably, be an ordinary text file.
812*8b26181fSAndroid Build Coastguard Worker 	 *
813*8b26181fSAndroid Build Coastguard Worker 	 * It could not, however, conceivably be any other type of
814*8b26181fSAndroid Build Coastguard Worker 	 * capture file, so we can read the rest of the putative
815*8b26181fSAndroid Build Coastguard Worker 	 * Section Header Block; put the block type in the common
816*8b26181fSAndroid Build Coastguard Worker 	 * header, read the rest of the common header and the
817*8b26181fSAndroid Build Coastguard Worker 	 * fixed-length portion of the SHB, and look for the byte-order
818*8b26181fSAndroid Build Coastguard Worker 	 * magic value.
819*8b26181fSAndroid Build Coastguard Worker 	 */
820*8b26181fSAndroid Build Coastguard Worker 	amt_read = fread(&total_length, 1, sizeof(total_length), fp);
821*8b26181fSAndroid Build Coastguard Worker 	if (amt_read < sizeof(total_length)) {
822*8b26181fSAndroid Build Coastguard Worker 		if (ferror(fp)) {
823*8b26181fSAndroid Build Coastguard Worker 			pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
824*8b26181fSAndroid Build Coastguard Worker 			    errno, "error reading dump file");
825*8b26181fSAndroid Build Coastguard Worker 			*err = 1;
826*8b26181fSAndroid Build Coastguard Worker 			return (NULL);	/* fail */
827*8b26181fSAndroid Build Coastguard Worker 		}
828*8b26181fSAndroid Build Coastguard Worker 
829*8b26181fSAndroid Build Coastguard Worker 		/*
830*8b26181fSAndroid Build Coastguard Worker 		 * Possibly a weird short text file, so just say
831*8b26181fSAndroid Build Coastguard Worker 		 * "not pcapng".
832*8b26181fSAndroid Build Coastguard Worker 		 */
833*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
834*8b26181fSAndroid Build Coastguard Worker 	}
835*8b26181fSAndroid Build Coastguard Worker 	amt_read = fread(&byte_order_magic, 1, sizeof(byte_order_magic), fp);
836*8b26181fSAndroid Build Coastguard Worker 	if (amt_read < sizeof(byte_order_magic)) {
837*8b26181fSAndroid Build Coastguard Worker 		if (ferror(fp)) {
838*8b26181fSAndroid Build Coastguard Worker 			pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
839*8b26181fSAndroid Build Coastguard Worker 			    errno, "error reading dump file");
840*8b26181fSAndroid Build Coastguard Worker 			*err = 1;
841*8b26181fSAndroid Build Coastguard Worker 			return (NULL);	/* fail */
842*8b26181fSAndroid Build Coastguard Worker 		}
843*8b26181fSAndroid Build Coastguard Worker 
844*8b26181fSAndroid Build Coastguard Worker 		/*
845*8b26181fSAndroid Build Coastguard Worker 		 * Possibly a weird short text file, so just say
846*8b26181fSAndroid Build Coastguard Worker 		 * "not pcapng".
847*8b26181fSAndroid Build Coastguard Worker 		 */
848*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
849*8b26181fSAndroid Build Coastguard Worker 	}
850*8b26181fSAndroid Build Coastguard Worker 	if (byte_order_magic != BYTE_ORDER_MAGIC) {
851*8b26181fSAndroid Build Coastguard Worker 		byte_order_magic = SWAPLONG(byte_order_magic);
852*8b26181fSAndroid Build Coastguard Worker 		if (byte_order_magic != BYTE_ORDER_MAGIC) {
853*8b26181fSAndroid Build Coastguard Worker 			/*
854*8b26181fSAndroid Build Coastguard Worker 			 * Not a pcapng file.
855*8b26181fSAndroid Build Coastguard Worker 			 */
856*8b26181fSAndroid Build Coastguard Worker 			return (NULL);
857*8b26181fSAndroid Build Coastguard Worker 		}
858*8b26181fSAndroid Build Coastguard Worker 		swapped = 1;
859*8b26181fSAndroid Build Coastguard Worker 		total_length = SWAPLONG(total_length);
860*8b26181fSAndroid Build Coastguard Worker 	}
861*8b26181fSAndroid Build Coastguard Worker 
862*8b26181fSAndroid Build Coastguard Worker 	/*
863*8b26181fSAndroid Build Coastguard Worker 	 * Check the sanity of the total length.
864*8b26181fSAndroid Build Coastguard Worker 	 */
865*8b26181fSAndroid Build Coastguard Worker 	if (total_length < sizeof(*bhdrp) + sizeof(*shbp) + sizeof(struct block_trailer) ||
866*8b26181fSAndroid Build Coastguard Worker             (total_length > BT_SHB_INSANE_MAX)) {
867*8b26181fSAndroid Build Coastguard Worker 		snprintf(errbuf, PCAP_ERRBUF_SIZE,
868*8b26181fSAndroid Build Coastguard Worker 		    "Section Header Block in pcapng dump file has invalid length %zu < _%u_ < %u (BT_SHB_INSANE_MAX)",
869*8b26181fSAndroid Build Coastguard Worker 		    sizeof(*bhdrp) + sizeof(*shbp) + sizeof(struct block_trailer),
870*8b26181fSAndroid Build Coastguard Worker 		    total_length,
871*8b26181fSAndroid Build Coastguard Worker 		    BT_SHB_INSANE_MAX);
872*8b26181fSAndroid Build Coastguard Worker 
873*8b26181fSAndroid Build Coastguard Worker 		*err = 1;
874*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
875*8b26181fSAndroid Build Coastguard Worker 	}
876*8b26181fSAndroid Build Coastguard Worker 
877*8b26181fSAndroid Build Coastguard Worker 	/*
878*8b26181fSAndroid Build Coastguard Worker 	 * OK, this is a good pcapng file.
879*8b26181fSAndroid Build Coastguard Worker 	 * Allocate a pcap_t for it.
880*8b26181fSAndroid Build Coastguard Worker 	 */
881*8b26181fSAndroid Build Coastguard Worker 	p = PCAP_OPEN_OFFLINE_COMMON(errbuf, struct pcap_ng_sf);
882*8b26181fSAndroid Build Coastguard Worker 	if (p == NULL) {
883*8b26181fSAndroid Build Coastguard Worker 		/* Allocation failed. */
884*8b26181fSAndroid Build Coastguard Worker 		*err = 1;
885*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
886*8b26181fSAndroid Build Coastguard Worker 	}
887*8b26181fSAndroid Build Coastguard Worker 	p->swapped = swapped;
888*8b26181fSAndroid Build Coastguard Worker 	ps = p->priv;
889*8b26181fSAndroid Build Coastguard Worker 
890*8b26181fSAndroid Build Coastguard Worker 	/*
891*8b26181fSAndroid Build Coastguard Worker 	 * What precision does the user want?
892*8b26181fSAndroid Build Coastguard Worker 	 */
893*8b26181fSAndroid Build Coastguard Worker 	switch (precision) {
894*8b26181fSAndroid Build Coastguard Worker 
895*8b26181fSAndroid Build Coastguard Worker 	case PCAP_TSTAMP_PRECISION_MICRO:
896*8b26181fSAndroid Build Coastguard Worker 		ps->user_tsresol = 1000000;
897*8b26181fSAndroid Build Coastguard Worker 		break;
898*8b26181fSAndroid Build Coastguard Worker 
899*8b26181fSAndroid Build Coastguard Worker 	case PCAP_TSTAMP_PRECISION_NANO:
900*8b26181fSAndroid Build Coastguard Worker 		ps->user_tsresol = 1000000000;
901*8b26181fSAndroid Build Coastguard Worker 		break;
902*8b26181fSAndroid Build Coastguard Worker 
903*8b26181fSAndroid Build Coastguard Worker 	default:
904*8b26181fSAndroid Build Coastguard Worker 		snprintf(errbuf, PCAP_ERRBUF_SIZE,
905*8b26181fSAndroid Build Coastguard Worker 		    "unknown time stamp resolution %u", precision);
906*8b26181fSAndroid Build Coastguard Worker 		free(p);
907*8b26181fSAndroid Build Coastguard Worker 		*err = 1;
908*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
909*8b26181fSAndroid Build Coastguard Worker 	}
910*8b26181fSAndroid Build Coastguard Worker 
911*8b26181fSAndroid Build Coastguard Worker 	p->opt.tstamp_precision = precision;
912*8b26181fSAndroid Build Coastguard Worker 
913*8b26181fSAndroid Build Coastguard Worker 	/*
914*8b26181fSAndroid Build Coastguard Worker 	 * Allocate a buffer into which to read blocks.  We default to
915*8b26181fSAndroid Build Coastguard Worker 	 * the maximum of:
916*8b26181fSAndroid Build Coastguard Worker 	 *
917*8b26181fSAndroid Build Coastguard Worker 	 *	the total length of the SHB for which we read the header;
918*8b26181fSAndroid Build Coastguard Worker 	 *
919*8b26181fSAndroid Build Coastguard Worker 	 *	2K, which should be more than large enough for an Enhanced
920*8b26181fSAndroid Build Coastguard Worker 	 *	Packet Block containing a full-size Ethernet frame, and
921*8b26181fSAndroid Build Coastguard Worker 	 *	leaving room for some options.
922*8b26181fSAndroid Build Coastguard Worker 	 *
923*8b26181fSAndroid Build Coastguard Worker 	 * If we find a bigger block, we reallocate the buffer, up to
924*8b26181fSAndroid Build Coastguard Worker 	 * the maximum size.  We start out with a maximum size of
925*8b26181fSAndroid Build Coastguard Worker 	 * INITIAL_MAX_BLOCKSIZE; if we see any link-layer header types
926*8b26181fSAndroid Build Coastguard Worker 	 * with a maximum snapshot that results in a larger maximum
927*8b26181fSAndroid Build Coastguard Worker 	 * block length, we boost the maximum.
928*8b26181fSAndroid Build Coastguard Worker 	 */
929*8b26181fSAndroid Build Coastguard Worker 	p->bufsize = 2048;
930*8b26181fSAndroid Build Coastguard Worker 	if (p->bufsize < total_length)
931*8b26181fSAndroid Build Coastguard Worker 		p->bufsize = total_length;
932*8b26181fSAndroid Build Coastguard Worker 	p->buffer = malloc(p->bufsize);
933*8b26181fSAndroid Build Coastguard Worker 	if (p->buffer == NULL) {
934*8b26181fSAndroid Build Coastguard Worker 		snprintf(errbuf, PCAP_ERRBUF_SIZE, "out of memory");
935*8b26181fSAndroid Build Coastguard Worker 		free(p);
936*8b26181fSAndroid Build Coastguard Worker 		*err = 1;
937*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
938*8b26181fSAndroid Build Coastguard Worker 	}
939*8b26181fSAndroid Build Coastguard Worker 	ps->max_blocksize = INITIAL_MAX_BLOCKSIZE;
940*8b26181fSAndroid Build Coastguard Worker 
941*8b26181fSAndroid Build Coastguard Worker 	/*
942*8b26181fSAndroid Build Coastguard Worker 	 * Copy the stuff we've read to the buffer, and read the rest
943*8b26181fSAndroid Build Coastguard Worker 	 * of the SHB.
944*8b26181fSAndroid Build Coastguard Worker 	 */
945*8b26181fSAndroid Build Coastguard Worker 	bhdrp = (struct block_header *)p->buffer;
946*8b26181fSAndroid Build Coastguard Worker 	shbp = (struct section_header_block *)((u_char *)p->buffer + sizeof(struct block_header));
947*8b26181fSAndroid Build Coastguard Worker 	bhdrp->block_type = magic_int;
948*8b26181fSAndroid Build Coastguard Worker 	bhdrp->total_length = total_length;
949*8b26181fSAndroid Build Coastguard Worker 	shbp->byte_order_magic = byte_order_magic;
950*8b26181fSAndroid Build Coastguard Worker 	if (read_bytes(fp,
951*8b26181fSAndroid Build Coastguard Worker 	    (u_char *)p->buffer + (sizeof(magic_int) + sizeof(total_length) + sizeof(byte_order_magic)),
952*8b26181fSAndroid Build Coastguard Worker 	    total_length - (sizeof(magic_int) + sizeof(total_length) + sizeof(byte_order_magic)),
953*8b26181fSAndroid Build Coastguard Worker 	    1, errbuf) == -1)
954*8b26181fSAndroid Build Coastguard Worker 		goto fail;
955*8b26181fSAndroid Build Coastguard Worker 
956*8b26181fSAndroid Build Coastguard Worker 	if (p->swapped) {
957*8b26181fSAndroid Build Coastguard Worker 		/*
958*8b26181fSAndroid Build Coastguard Worker 		 * Byte-swap the fields we've read.
959*8b26181fSAndroid Build Coastguard Worker 		 */
960*8b26181fSAndroid Build Coastguard Worker 		shbp->major_version = SWAPSHORT(shbp->major_version);
961*8b26181fSAndroid Build Coastguard Worker 		shbp->minor_version = SWAPSHORT(shbp->minor_version);
962*8b26181fSAndroid Build Coastguard Worker 
963*8b26181fSAndroid Build Coastguard Worker 		/*
964*8b26181fSAndroid Build Coastguard Worker 		 * XXX - we don't care about the section length.
965*8b26181fSAndroid Build Coastguard Worker 		 */
966*8b26181fSAndroid Build Coastguard Worker 	}
967*8b26181fSAndroid Build Coastguard Worker 	/* Currently only SHB versions 1.0 and 1.2 are supported;
968*8b26181fSAndroid Build Coastguard Worker 	   version 1.2 is treated as being the same as version 1.0.
969*8b26181fSAndroid Build Coastguard Worker 	   See the current version of the pcapng specification.
970*8b26181fSAndroid Build Coastguard Worker 
971*8b26181fSAndroid Build Coastguard Worker 	   Version 1.2 is written by some programs that write additional
972*8b26181fSAndroid Build Coastguard Worker 	   block types (which can be read by any code that handles them,
973*8b26181fSAndroid Build Coastguard Worker 	   regardless of whether the minor version if 0 or 2, so that's
974*8b26181fSAndroid Build Coastguard Worker 	   not a reason to change the minor version number).
975*8b26181fSAndroid Build Coastguard Worker 
976*8b26181fSAndroid Build Coastguard Worker 	   XXX - the pcapng specification says that readers should
977*8b26181fSAndroid Build Coastguard Worker 	   just ignore sections with an unsupported version number;
978*8b26181fSAndroid Build Coastguard Worker 	   presumably they can also report an error if they skip
979*8b26181fSAndroid Build Coastguard Worker 	   all the way to the end of the file without finding
980*8b26181fSAndroid Build Coastguard Worker 	   any versions that they support. */
981*8b26181fSAndroid Build Coastguard Worker 	if (! (shbp->major_version == PCAP_NG_VERSION_MAJOR &&
982*8b26181fSAndroid Build Coastguard Worker 	       (shbp->minor_version == PCAP_NG_VERSION_MINOR ||
983*8b26181fSAndroid Build Coastguard Worker 	        shbp->minor_version == 2))) {
984*8b26181fSAndroid Build Coastguard Worker 		snprintf(errbuf, PCAP_ERRBUF_SIZE,
985*8b26181fSAndroid Build Coastguard Worker 		    "unsupported pcapng savefile version %u.%u",
986*8b26181fSAndroid Build Coastguard Worker 		    shbp->major_version, shbp->minor_version);
987*8b26181fSAndroid Build Coastguard Worker 		goto fail;
988*8b26181fSAndroid Build Coastguard Worker 	}
989*8b26181fSAndroid Build Coastguard Worker 	p->version_major = shbp->major_version;
990*8b26181fSAndroid Build Coastguard Worker 	p->version_minor = shbp->minor_version;
991*8b26181fSAndroid Build Coastguard Worker 
992*8b26181fSAndroid Build Coastguard Worker 	/*
993*8b26181fSAndroid Build Coastguard Worker 	 * Save the time stamp resolution the user requested.
994*8b26181fSAndroid Build Coastguard Worker 	 */
995*8b26181fSAndroid Build Coastguard Worker 	p->opt.tstamp_precision = precision;
996*8b26181fSAndroid Build Coastguard Worker 
997*8b26181fSAndroid Build Coastguard Worker 	/*
998*8b26181fSAndroid Build Coastguard Worker 	 * Now start looking for an Interface Description Block.
999*8b26181fSAndroid Build Coastguard Worker 	 */
1000*8b26181fSAndroid Build Coastguard Worker 	for (;;) {
1001*8b26181fSAndroid Build Coastguard Worker 		/*
1002*8b26181fSAndroid Build Coastguard Worker 		 * Read the next block.
1003*8b26181fSAndroid Build Coastguard Worker 		 */
1004*8b26181fSAndroid Build Coastguard Worker 		status = read_block(fp, p, &cursor, errbuf);
1005*8b26181fSAndroid Build Coastguard Worker 		if (status == 0) {
1006*8b26181fSAndroid Build Coastguard Worker 			/* EOF - no IDB in this file */
1007*8b26181fSAndroid Build Coastguard Worker 			snprintf(errbuf, PCAP_ERRBUF_SIZE,
1008*8b26181fSAndroid Build Coastguard Worker 			    "the capture file has no Interface Description Blocks");
1009*8b26181fSAndroid Build Coastguard Worker 			goto fail;
1010*8b26181fSAndroid Build Coastguard Worker 		}
1011*8b26181fSAndroid Build Coastguard Worker 		if (status == -1)
1012*8b26181fSAndroid Build Coastguard Worker 			goto fail;	/* error */
1013*8b26181fSAndroid Build Coastguard Worker 		switch (cursor.block_type) {
1014*8b26181fSAndroid Build Coastguard Worker 
1015*8b26181fSAndroid Build Coastguard Worker 		case BT_IDB:
1016*8b26181fSAndroid Build Coastguard Worker 			/*
1017*8b26181fSAndroid Build Coastguard Worker 			 * Get a pointer to the fixed-length portion of the
1018*8b26181fSAndroid Build Coastguard Worker 			 * IDB.
1019*8b26181fSAndroid Build Coastguard Worker 			 */
1020*8b26181fSAndroid Build Coastguard Worker 			idbp = get_from_block_data(&cursor, sizeof(*idbp),
1021*8b26181fSAndroid Build Coastguard Worker 			    errbuf);
1022*8b26181fSAndroid Build Coastguard Worker 			if (idbp == NULL)
1023*8b26181fSAndroid Build Coastguard Worker 				goto fail;	/* error */
1024*8b26181fSAndroid Build Coastguard Worker 
1025*8b26181fSAndroid Build Coastguard Worker 			/*
1026*8b26181fSAndroid Build Coastguard Worker 			 * Byte-swap it if necessary.
1027*8b26181fSAndroid Build Coastguard Worker 			 */
1028*8b26181fSAndroid Build Coastguard Worker 			if (p->swapped) {
1029*8b26181fSAndroid Build Coastguard Worker 				idbp->linktype = SWAPSHORT(idbp->linktype);
1030*8b26181fSAndroid Build Coastguard Worker 				idbp->snaplen = SWAPLONG(idbp->snaplen);
1031*8b26181fSAndroid Build Coastguard Worker 			}
1032*8b26181fSAndroid Build Coastguard Worker 
1033*8b26181fSAndroid Build Coastguard Worker 			/*
1034*8b26181fSAndroid Build Coastguard Worker 			 * Try to add this interface.
1035*8b26181fSAndroid Build Coastguard Worker 			 */
1036*8b26181fSAndroid Build Coastguard Worker 			if (!add_interface(p, idbp, &cursor, errbuf))
1037*8b26181fSAndroid Build Coastguard Worker 				goto fail;
1038*8b26181fSAndroid Build Coastguard Worker 
1039*8b26181fSAndroid Build Coastguard Worker 			goto done;
1040*8b26181fSAndroid Build Coastguard Worker 
1041*8b26181fSAndroid Build Coastguard Worker 		case BT_EPB:
1042*8b26181fSAndroid Build Coastguard Worker 		case BT_SPB:
1043*8b26181fSAndroid Build Coastguard Worker 		case BT_PB:
1044*8b26181fSAndroid Build Coastguard Worker 			/*
1045*8b26181fSAndroid Build Coastguard Worker 			 * Saw a packet before we saw any IDBs.  That's
1046*8b26181fSAndroid Build Coastguard Worker 			 * not valid, as we don't know what link-layer
1047*8b26181fSAndroid Build Coastguard Worker 			 * encapsulation the packet has.
1048*8b26181fSAndroid Build Coastguard Worker 			 */
1049*8b26181fSAndroid Build Coastguard Worker 			snprintf(errbuf, PCAP_ERRBUF_SIZE,
1050*8b26181fSAndroid Build Coastguard Worker 			    "the capture file has a packet block before any Interface Description Blocks");
1051*8b26181fSAndroid Build Coastguard Worker 			goto fail;
1052*8b26181fSAndroid Build Coastguard Worker 
1053*8b26181fSAndroid Build Coastguard Worker 		default:
1054*8b26181fSAndroid Build Coastguard Worker 			/*
1055*8b26181fSAndroid Build Coastguard Worker 			 * Just ignore it.
1056*8b26181fSAndroid Build Coastguard Worker 			 */
1057*8b26181fSAndroid Build Coastguard Worker 			break;
1058*8b26181fSAndroid Build Coastguard Worker 		}
1059*8b26181fSAndroid Build Coastguard Worker 	}
1060*8b26181fSAndroid Build Coastguard Worker 
1061*8b26181fSAndroid Build Coastguard Worker done:
1062*8b26181fSAndroid Build Coastguard Worker 	p->linktype = linktype_to_dlt(idbp->linktype);
1063*8b26181fSAndroid Build Coastguard Worker 	p->snapshot = pcap_adjust_snapshot(p->linktype, idbp->snaplen);
1064*8b26181fSAndroid Build Coastguard Worker 	p->linktype_ext = 0;
1065*8b26181fSAndroid Build Coastguard Worker 
1066*8b26181fSAndroid Build Coastguard Worker 	/*
1067*8b26181fSAndroid Build Coastguard Worker 	 * If the maximum block size for a packet with the maximum
1068*8b26181fSAndroid Build Coastguard Worker 	 * snapshot length for this DLT_ is bigger than the current
1069*8b26181fSAndroid Build Coastguard Worker 	 * maximum block size, increase the maximum.
1070*8b26181fSAndroid Build Coastguard Worker 	 */
1071*8b26181fSAndroid Build Coastguard Worker 	if (MAX_BLOCKSIZE_FOR_SNAPLEN(max_snaplen_for_dlt(p->linktype)) > ps->max_blocksize)
1072*8b26181fSAndroid Build Coastguard Worker 		ps->max_blocksize = MAX_BLOCKSIZE_FOR_SNAPLEN(max_snaplen_for_dlt(p->linktype));
1073*8b26181fSAndroid Build Coastguard Worker 
1074*8b26181fSAndroid Build Coastguard Worker 	p->next_packet_op = pcap_ng_next_packet;
1075*8b26181fSAndroid Build Coastguard Worker 	p->cleanup_op = pcap_ng_cleanup;
1076*8b26181fSAndroid Build Coastguard Worker 
1077*8b26181fSAndroid Build Coastguard Worker 	return (p);
1078*8b26181fSAndroid Build Coastguard Worker 
1079*8b26181fSAndroid Build Coastguard Worker fail:
1080*8b26181fSAndroid Build Coastguard Worker 	free(ps->ifaces);
1081*8b26181fSAndroid Build Coastguard Worker 	free(p->buffer);
1082*8b26181fSAndroid Build Coastguard Worker 	free(p);
1083*8b26181fSAndroid Build Coastguard Worker 	*err = 1;
1084*8b26181fSAndroid Build Coastguard Worker 	return (NULL);
1085*8b26181fSAndroid Build Coastguard Worker }
1086*8b26181fSAndroid Build Coastguard Worker 
1087*8b26181fSAndroid Build Coastguard Worker static void
pcap_ng_cleanup(pcap_t * p)1088*8b26181fSAndroid Build Coastguard Worker pcap_ng_cleanup(pcap_t *p)
1089*8b26181fSAndroid Build Coastguard Worker {
1090*8b26181fSAndroid Build Coastguard Worker 	struct pcap_ng_sf *ps = p->priv;
1091*8b26181fSAndroid Build Coastguard Worker 
1092*8b26181fSAndroid Build Coastguard Worker 	free(ps->ifaces);
1093*8b26181fSAndroid Build Coastguard Worker 	sf_cleanup(p);
1094*8b26181fSAndroid Build Coastguard Worker }
1095*8b26181fSAndroid Build Coastguard Worker 
1096*8b26181fSAndroid Build Coastguard Worker /*
1097*8b26181fSAndroid Build Coastguard Worker  * Read and return the next packet from the savefile.  Return the header
1098*8b26181fSAndroid Build Coastguard Worker  * in hdr and a pointer to the contents in data.  Return 1 on success, 0
1099*8b26181fSAndroid Build Coastguard Worker  * if there were no more packets, and -1 on an error.
1100*8b26181fSAndroid Build Coastguard Worker  */
1101*8b26181fSAndroid Build Coastguard Worker static int
pcap_ng_next_packet(pcap_t * p,struct pcap_pkthdr * hdr,u_char ** data)1102*8b26181fSAndroid Build Coastguard Worker pcap_ng_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char **data)
1103*8b26181fSAndroid Build Coastguard Worker {
1104*8b26181fSAndroid Build Coastguard Worker 	struct pcap_ng_sf *ps = p->priv;
1105*8b26181fSAndroid Build Coastguard Worker 	struct block_cursor cursor;
1106*8b26181fSAndroid Build Coastguard Worker 	int status;
1107*8b26181fSAndroid Build Coastguard Worker 	struct enhanced_packet_block *epbp;
1108*8b26181fSAndroid Build Coastguard Worker 	struct simple_packet_block *spbp;
1109*8b26181fSAndroid Build Coastguard Worker 	struct packet_block *pbp;
1110*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32 interface_id = 0xFFFFFFFF;
1111*8b26181fSAndroid Build Coastguard Worker 	struct interface_description_block *idbp;
1112*8b26181fSAndroid Build Coastguard Worker 	struct section_header_block *shbp;
1113*8b26181fSAndroid Build Coastguard Worker 	FILE *fp = p->rfile;
1114*8b26181fSAndroid Build Coastguard Worker 	uint64_t t, sec, frac;
1115*8b26181fSAndroid Build Coastguard Worker 
1116*8b26181fSAndroid Build Coastguard Worker 	/*
1117*8b26181fSAndroid Build Coastguard Worker 	 * Look for an Enhanced Packet Block, a Simple Packet Block,
1118*8b26181fSAndroid Build Coastguard Worker 	 * or a Packet Block.
1119*8b26181fSAndroid Build Coastguard Worker 	 */
1120*8b26181fSAndroid Build Coastguard Worker 	for (;;) {
1121*8b26181fSAndroid Build Coastguard Worker 		/*
1122*8b26181fSAndroid Build Coastguard Worker 		 * Read the block type and length; those are common
1123*8b26181fSAndroid Build Coastguard Worker 		 * to all blocks.
1124*8b26181fSAndroid Build Coastguard Worker 		 */
1125*8b26181fSAndroid Build Coastguard Worker 		status = read_block(fp, p, &cursor, p->errbuf);
1126*8b26181fSAndroid Build Coastguard Worker 		if (status == 0)
1127*8b26181fSAndroid Build Coastguard Worker 			return (0);	/* EOF */
1128*8b26181fSAndroid Build Coastguard Worker 		if (status == -1)
1129*8b26181fSAndroid Build Coastguard Worker 			return (-1);	/* error */
1130*8b26181fSAndroid Build Coastguard Worker 		switch (cursor.block_type) {
1131*8b26181fSAndroid Build Coastguard Worker 
1132*8b26181fSAndroid Build Coastguard Worker 		case BT_EPB:
1133*8b26181fSAndroid Build Coastguard Worker 			/*
1134*8b26181fSAndroid Build Coastguard Worker 			 * Get a pointer to the fixed-length portion of the
1135*8b26181fSAndroid Build Coastguard Worker 			 * EPB.
1136*8b26181fSAndroid Build Coastguard Worker 			 */
1137*8b26181fSAndroid Build Coastguard Worker 			epbp = get_from_block_data(&cursor, sizeof(*epbp),
1138*8b26181fSAndroid Build Coastguard Worker 			    p->errbuf);
1139*8b26181fSAndroid Build Coastguard Worker 			if (epbp == NULL)
1140*8b26181fSAndroid Build Coastguard Worker 				return (-1);	/* error */
1141*8b26181fSAndroid Build Coastguard Worker 
1142*8b26181fSAndroid Build Coastguard Worker 			/*
1143*8b26181fSAndroid Build Coastguard Worker 			 * Byte-swap it if necessary.
1144*8b26181fSAndroid Build Coastguard Worker 			 */
1145*8b26181fSAndroid Build Coastguard Worker 			if (p->swapped) {
1146*8b26181fSAndroid Build Coastguard Worker 				/* these were written in opposite byte order */
1147*8b26181fSAndroid Build Coastguard Worker 				interface_id = SWAPLONG(epbp->interface_id);
1148*8b26181fSAndroid Build Coastguard Worker 				hdr->caplen = SWAPLONG(epbp->caplen);
1149*8b26181fSAndroid Build Coastguard Worker 				hdr->len = SWAPLONG(epbp->len);
1150*8b26181fSAndroid Build Coastguard Worker 				t = ((uint64_t)SWAPLONG(epbp->timestamp_high)) << 32 |
1151*8b26181fSAndroid Build Coastguard Worker 				    SWAPLONG(epbp->timestamp_low);
1152*8b26181fSAndroid Build Coastguard Worker 			} else {
1153*8b26181fSAndroid Build Coastguard Worker 				interface_id = epbp->interface_id;
1154*8b26181fSAndroid Build Coastguard Worker 				hdr->caplen = epbp->caplen;
1155*8b26181fSAndroid Build Coastguard Worker 				hdr->len = epbp->len;
1156*8b26181fSAndroid Build Coastguard Worker 				t = ((uint64_t)epbp->timestamp_high) << 32 |
1157*8b26181fSAndroid Build Coastguard Worker 				    epbp->timestamp_low;
1158*8b26181fSAndroid Build Coastguard Worker 			}
1159*8b26181fSAndroid Build Coastguard Worker 			goto found;
1160*8b26181fSAndroid Build Coastguard Worker 
1161*8b26181fSAndroid Build Coastguard Worker 		case BT_SPB:
1162*8b26181fSAndroid Build Coastguard Worker 			/*
1163*8b26181fSAndroid Build Coastguard Worker 			 * Get a pointer to the fixed-length portion of the
1164*8b26181fSAndroid Build Coastguard Worker 			 * SPB.
1165*8b26181fSAndroid Build Coastguard Worker 			 */
1166*8b26181fSAndroid Build Coastguard Worker 			spbp = get_from_block_data(&cursor, sizeof(*spbp),
1167*8b26181fSAndroid Build Coastguard Worker 			    p->errbuf);
1168*8b26181fSAndroid Build Coastguard Worker 			if (spbp == NULL)
1169*8b26181fSAndroid Build Coastguard Worker 				return (-1);	/* error */
1170*8b26181fSAndroid Build Coastguard Worker 
1171*8b26181fSAndroid Build Coastguard Worker 			/*
1172*8b26181fSAndroid Build Coastguard Worker 			 * SPB packets are assumed to have arrived on
1173*8b26181fSAndroid Build Coastguard Worker 			 * the first interface.
1174*8b26181fSAndroid Build Coastguard Worker 			 */
1175*8b26181fSAndroid Build Coastguard Worker 			interface_id = 0;
1176*8b26181fSAndroid Build Coastguard Worker 
1177*8b26181fSAndroid Build Coastguard Worker 			/*
1178*8b26181fSAndroid Build Coastguard Worker 			 * Byte-swap it if necessary.
1179*8b26181fSAndroid Build Coastguard Worker 			 */
1180*8b26181fSAndroid Build Coastguard Worker 			if (p->swapped) {
1181*8b26181fSAndroid Build Coastguard Worker 				/* these were written in opposite byte order */
1182*8b26181fSAndroid Build Coastguard Worker 				hdr->len = SWAPLONG(spbp->len);
1183*8b26181fSAndroid Build Coastguard Worker 			} else
1184*8b26181fSAndroid Build Coastguard Worker 				hdr->len = spbp->len;
1185*8b26181fSAndroid Build Coastguard Worker 
1186*8b26181fSAndroid Build Coastguard Worker 			/*
1187*8b26181fSAndroid Build Coastguard Worker 			 * The SPB doesn't give the captured length;
1188*8b26181fSAndroid Build Coastguard Worker 			 * it's the minimum of the snapshot length
1189*8b26181fSAndroid Build Coastguard Worker 			 * and the packet length.
1190*8b26181fSAndroid Build Coastguard Worker 			 */
1191*8b26181fSAndroid Build Coastguard Worker 			hdr->caplen = hdr->len;
1192*8b26181fSAndroid Build Coastguard Worker 			if (hdr->caplen > (bpf_u_int32)p->snapshot)
1193*8b26181fSAndroid Build Coastguard Worker 				hdr->caplen = p->snapshot;
1194*8b26181fSAndroid Build Coastguard Worker 			t = 0;	/* no time stamps */
1195*8b26181fSAndroid Build Coastguard Worker 			goto found;
1196*8b26181fSAndroid Build Coastguard Worker 
1197*8b26181fSAndroid Build Coastguard Worker 		case BT_PB:
1198*8b26181fSAndroid Build Coastguard Worker 			/*
1199*8b26181fSAndroid Build Coastguard Worker 			 * Get a pointer to the fixed-length portion of the
1200*8b26181fSAndroid Build Coastguard Worker 			 * PB.
1201*8b26181fSAndroid Build Coastguard Worker 			 */
1202*8b26181fSAndroid Build Coastguard Worker 			pbp = get_from_block_data(&cursor, sizeof(*pbp),
1203*8b26181fSAndroid Build Coastguard Worker 			    p->errbuf);
1204*8b26181fSAndroid Build Coastguard Worker 			if (pbp == NULL)
1205*8b26181fSAndroid Build Coastguard Worker 				return (-1);	/* error */
1206*8b26181fSAndroid Build Coastguard Worker 
1207*8b26181fSAndroid Build Coastguard Worker 			/*
1208*8b26181fSAndroid Build Coastguard Worker 			 * Byte-swap it if necessary.
1209*8b26181fSAndroid Build Coastguard Worker 			 */
1210*8b26181fSAndroid Build Coastguard Worker 			if (p->swapped) {
1211*8b26181fSAndroid Build Coastguard Worker 				/* these were written in opposite byte order */
1212*8b26181fSAndroid Build Coastguard Worker 				interface_id = SWAPSHORT(pbp->interface_id);
1213*8b26181fSAndroid Build Coastguard Worker 				hdr->caplen = SWAPLONG(pbp->caplen);
1214*8b26181fSAndroid Build Coastguard Worker 				hdr->len = SWAPLONG(pbp->len);
1215*8b26181fSAndroid Build Coastguard Worker 				t = ((uint64_t)SWAPLONG(pbp->timestamp_high)) << 32 |
1216*8b26181fSAndroid Build Coastguard Worker 				    SWAPLONG(pbp->timestamp_low);
1217*8b26181fSAndroid Build Coastguard Worker 			} else {
1218*8b26181fSAndroid Build Coastguard Worker 				interface_id = pbp->interface_id;
1219*8b26181fSAndroid Build Coastguard Worker 				hdr->caplen = pbp->caplen;
1220*8b26181fSAndroid Build Coastguard Worker 				hdr->len = pbp->len;
1221*8b26181fSAndroid Build Coastguard Worker 				t = ((uint64_t)pbp->timestamp_high) << 32 |
1222*8b26181fSAndroid Build Coastguard Worker 				    pbp->timestamp_low;
1223*8b26181fSAndroid Build Coastguard Worker 			}
1224*8b26181fSAndroid Build Coastguard Worker 			goto found;
1225*8b26181fSAndroid Build Coastguard Worker 
1226*8b26181fSAndroid Build Coastguard Worker 		case BT_IDB:
1227*8b26181fSAndroid Build Coastguard Worker 			/*
1228*8b26181fSAndroid Build Coastguard Worker 			 * Interface Description Block.  Get a pointer
1229*8b26181fSAndroid Build Coastguard Worker 			 * to its fixed-length portion.
1230*8b26181fSAndroid Build Coastguard Worker 			 */
1231*8b26181fSAndroid Build Coastguard Worker 			idbp = get_from_block_data(&cursor, sizeof(*idbp),
1232*8b26181fSAndroid Build Coastguard Worker 			    p->errbuf);
1233*8b26181fSAndroid Build Coastguard Worker 			if (idbp == NULL)
1234*8b26181fSAndroid Build Coastguard Worker 				return (-1);	/* error */
1235*8b26181fSAndroid Build Coastguard Worker 
1236*8b26181fSAndroid Build Coastguard Worker 			/*
1237*8b26181fSAndroid Build Coastguard Worker 			 * Byte-swap it if necessary.
1238*8b26181fSAndroid Build Coastguard Worker 			 */
1239*8b26181fSAndroid Build Coastguard Worker 			if (p->swapped) {
1240*8b26181fSAndroid Build Coastguard Worker 				idbp->linktype = SWAPSHORT(idbp->linktype);
1241*8b26181fSAndroid Build Coastguard Worker 				idbp->snaplen = SWAPLONG(idbp->snaplen);
1242*8b26181fSAndroid Build Coastguard Worker 			}
1243*8b26181fSAndroid Build Coastguard Worker 
1244*8b26181fSAndroid Build Coastguard Worker 			/*
1245*8b26181fSAndroid Build Coastguard Worker 			 * If the link-layer type or snapshot length
1246*8b26181fSAndroid Build Coastguard Worker 			 * differ from the ones for the first IDB we
1247*8b26181fSAndroid Build Coastguard Worker 			 * saw, quit.
1248*8b26181fSAndroid Build Coastguard Worker 			 *
1249*8b26181fSAndroid Build Coastguard Worker 			 * XXX - just discard packets from those
1250*8b26181fSAndroid Build Coastguard Worker 			 * interfaces?
1251*8b26181fSAndroid Build Coastguard Worker 			 */
1252*8b26181fSAndroid Build Coastguard Worker 			if (p->linktype != idbp->linktype) {
1253*8b26181fSAndroid Build Coastguard Worker 				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1254*8b26181fSAndroid Build Coastguard Worker 				    "an interface has a type %u different from the type of the first interface",
1255*8b26181fSAndroid Build Coastguard Worker 				    idbp->linktype);
1256*8b26181fSAndroid Build Coastguard Worker 				return (-1);
1257*8b26181fSAndroid Build Coastguard Worker 			}
1258*8b26181fSAndroid Build Coastguard Worker 
1259*8b26181fSAndroid Build Coastguard Worker 			/*
1260*8b26181fSAndroid Build Coastguard Worker 			 * Check against the *adjusted* value of this IDB's
1261*8b26181fSAndroid Build Coastguard Worker 			 * snapshot length.
1262*8b26181fSAndroid Build Coastguard Worker 			 */
1263*8b26181fSAndroid Build Coastguard Worker 			if ((bpf_u_int32)p->snapshot !=
1264*8b26181fSAndroid Build Coastguard Worker 			    pcap_adjust_snapshot(p->linktype, idbp->snaplen)) {
1265*8b26181fSAndroid Build Coastguard Worker 				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1266*8b26181fSAndroid Build Coastguard Worker 				    "an interface has a snapshot length %u different from the snapshot length of the first interface",
1267*8b26181fSAndroid Build Coastguard Worker 				    idbp->snaplen);
1268*8b26181fSAndroid Build Coastguard Worker 				return (-1);
1269*8b26181fSAndroid Build Coastguard Worker 			}
1270*8b26181fSAndroid Build Coastguard Worker 
1271*8b26181fSAndroid Build Coastguard Worker 			/*
1272*8b26181fSAndroid Build Coastguard Worker 			 * Try to add this interface.
1273*8b26181fSAndroid Build Coastguard Worker 			 */
1274*8b26181fSAndroid Build Coastguard Worker 			if (!add_interface(p, idbp, &cursor, p->errbuf))
1275*8b26181fSAndroid Build Coastguard Worker 				return (-1);
1276*8b26181fSAndroid Build Coastguard Worker 			break;
1277*8b26181fSAndroid Build Coastguard Worker 
1278*8b26181fSAndroid Build Coastguard Worker 		case BT_SHB:
1279*8b26181fSAndroid Build Coastguard Worker 			/*
1280*8b26181fSAndroid Build Coastguard Worker 			 * Section Header Block.  Get a pointer
1281*8b26181fSAndroid Build Coastguard Worker 			 * to its fixed-length portion.
1282*8b26181fSAndroid Build Coastguard Worker 			 */
1283*8b26181fSAndroid Build Coastguard Worker 			shbp = get_from_block_data(&cursor, sizeof(*shbp),
1284*8b26181fSAndroid Build Coastguard Worker 			    p->errbuf);
1285*8b26181fSAndroid Build Coastguard Worker 			if (shbp == NULL)
1286*8b26181fSAndroid Build Coastguard Worker 				return (-1);	/* error */
1287*8b26181fSAndroid Build Coastguard Worker 
1288*8b26181fSAndroid Build Coastguard Worker 			/*
1289*8b26181fSAndroid Build Coastguard Worker 			 * Assume the byte order of this section is
1290*8b26181fSAndroid Build Coastguard Worker 			 * the same as that of the previous section.
1291*8b26181fSAndroid Build Coastguard Worker 			 * We'll check for that later.
1292*8b26181fSAndroid Build Coastguard Worker 			 */
1293*8b26181fSAndroid Build Coastguard Worker 			if (p->swapped) {
1294*8b26181fSAndroid Build Coastguard Worker 				shbp->byte_order_magic =
1295*8b26181fSAndroid Build Coastguard Worker 				    SWAPLONG(shbp->byte_order_magic);
1296*8b26181fSAndroid Build Coastguard Worker 				shbp->major_version =
1297*8b26181fSAndroid Build Coastguard Worker 				    SWAPSHORT(shbp->major_version);
1298*8b26181fSAndroid Build Coastguard Worker 			}
1299*8b26181fSAndroid Build Coastguard Worker 
1300*8b26181fSAndroid Build Coastguard Worker 			/*
1301*8b26181fSAndroid Build Coastguard Worker 			 * Make sure the byte order doesn't change;
1302*8b26181fSAndroid Build Coastguard Worker 			 * pcap_is_swapped() shouldn't change its
1303*8b26181fSAndroid Build Coastguard Worker 			 * return value in the middle of reading a capture.
1304*8b26181fSAndroid Build Coastguard Worker 			 */
1305*8b26181fSAndroid Build Coastguard Worker 			switch (shbp->byte_order_magic) {
1306*8b26181fSAndroid Build Coastguard Worker 
1307*8b26181fSAndroid Build Coastguard Worker 			case BYTE_ORDER_MAGIC:
1308*8b26181fSAndroid Build Coastguard Worker 				/*
1309*8b26181fSAndroid Build Coastguard Worker 				 * OK.
1310*8b26181fSAndroid Build Coastguard Worker 				 */
1311*8b26181fSAndroid Build Coastguard Worker 				break;
1312*8b26181fSAndroid Build Coastguard Worker 
1313*8b26181fSAndroid Build Coastguard Worker 			case SWAPLONG(BYTE_ORDER_MAGIC):
1314*8b26181fSAndroid Build Coastguard Worker 				/*
1315*8b26181fSAndroid Build Coastguard Worker 				 * Byte order changes.
1316*8b26181fSAndroid Build Coastguard Worker 				 */
1317*8b26181fSAndroid Build Coastguard Worker 				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1318*8b26181fSAndroid Build Coastguard Worker 				    "the file has sections with different byte orders");
1319*8b26181fSAndroid Build Coastguard Worker 				return (-1);
1320*8b26181fSAndroid Build Coastguard Worker 
1321*8b26181fSAndroid Build Coastguard Worker 			default:
1322*8b26181fSAndroid Build Coastguard Worker 				/*
1323*8b26181fSAndroid Build Coastguard Worker 				 * Not a valid SHB.
1324*8b26181fSAndroid Build Coastguard Worker 				 */
1325*8b26181fSAndroid Build Coastguard Worker 				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1326*8b26181fSAndroid Build Coastguard Worker 				    "the file has a section with a bad byte order magic field");
1327*8b26181fSAndroid Build Coastguard Worker 				return (-1);
1328*8b26181fSAndroid Build Coastguard Worker 			}
1329*8b26181fSAndroid Build Coastguard Worker 
1330*8b26181fSAndroid Build Coastguard Worker 			/*
1331*8b26181fSAndroid Build Coastguard Worker 			 * Make sure the major version is the version
1332*8b26181fSAndroid Build Coastguard Worker 			 * we handle.
1333*8b26181fSAndroid Build Coastguard Worker 			 */
1334*8b26181fSAndroid Build Coastguard Worker 			if (shbp->major_version != PCAP_NG_VERSION_MAJOR) {
1335*8b26181fSAndroid Build Coastguard Worker 				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1336*8b26181fSAndroid Build Coastguard Worker 				    "unknown pcapng savefile major version number %u",
1337*8b26181fSAndroid Build Coastguard Worker 				    shbp->major_version);
1338*8b26181fSAndroid Build Coastguard Worker 				return (-1);
1339*8b26181fSAndroid Build Coastguard Worker 			}
1340*8b26181fSAndroid Build Coastguard Worker 
1341*8b26181fSAndroid Build Coastguard Worker 			/*
1342*8b26181fSAndroid Build Coastguard Worker 			 * Reset the interface count; this section should
1343*8b26181fSAndroid Build Coastguard Worker 			 * have its own set of IDBs.  If any of them
1344*8b26181fSAndroid Build Coastguard Worker 			 * don't have the same interface type, snapshot
1345*8b26181fSAndroid Build Coastguard Worker 			 * length, or resolution as the first interface
1346*8b26181fSAndroid Build Coastguard Worker 			 * we saw, we'll fail.  (And if we don't see
1347*8b26181fSAndroid Build Coastguard Worker 			 * any IDBs, we'll fail when we see a packet
1348*8b26181fSAndroid Build Coastguard Worker 			 * block.)
1349*8b26181fSAndroid Build Coastguard Worker 			 */
1350*8b26181fSAndroid Build Coastguard Worker 			ps->ifcount = 0;
1351*8b26181fSAndroid Build Coastguard Worker 			break;
1352*8b26181fSAndroid Build Coastguard Worker 
1353*8b26181fSAndroid Build Coastguard Worker 		default:
1354*8b26181fSAndroid Build Coastguard Worker 			/*
1355*8b26181fSAndroid Build Coastguard Worker 			 * Not a packet block, IDB, or SHB; ignore it.
1356*8b26181fSAndroid Build Coastguard Worker 			 */
1357*8b26181fSAndroid Build Coastguard Worker 			break;
1358*8b26181fSAndroid Build Coastguard Worker 		}
1359*8b26181fSAndroid Build Coastguard Worker 	}
1360*8b26181fSAndroid Build Coastguard Worker 
1361*8b26181fSAndroid Build Coastguard Worker found:
1362*8b26181fSAndroid Build Coastguard Worker 	/*
1363*8b26181fSAndroid Build Coastguard Worker 	 * Is the interface ID an interface we know?
1364*8b26181fSAndroid Build Coastguard Worker 	 */
1365*8b26181fSAndroid Build Coastguard Worker 	if (interface_id >= ps->ifcount) {
1366*8b26181fSAndroid Build Coastguard Worker 		/*
1367*8b26181fSAndroid Build Coastguard Worker 		 * Yes.  Fail.
1368*8b26181fSAndroid Build Coastguard Worker 		 */
1369*8b26181fSAndroid Build Coastguard Worker 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1370*8b26181fSAndroid Build Coastguard Worker 		    "a packet arrived on interface %u, but there's no Interface Description Block for that interface",
1371*8b26181fSAndroid Build Coastguard Worker 		    interface_id);
1372*8b26181fSAndroid Build Coastguard Worker 		return (-1);
1373*8b26181fSAndroid Build Coastguard Worker 	}
1374*8b26181fSAndroid Build Coastguard Worker 
1375*8b26181fSAndroid Build Coastguard Worker 	if (hdr->caplen > (bpf_u_int32)p->snapshot) {
1376*8b26181fSAndroid Build Coastguard Worker 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1377*8b26181fSAndroid Build Coastguard Worker 		    "invalid packet capture length %u, bigger than "
1378*8b26181fSAndroid Build Coastguard Worker 		    "snaplen of %d", hdr->caplen, p->snapshot);
1379*8b26181fSAndroid Build Coastguard Worker 		return (-1);
1380*8b26181fSAndroid Build Coastguard Worker 	}
1381*8b26181fSAndroid Build Coastguard Worker 
1382*8b26181fSAndroid Build Coastguard Worker 	/*
1383*8b26181fSAndroid Build Coastguard Worker 	 * Convert the time stamp to seconds and fractions of a second,
1384*8b26181fSAndroid Build Coastguard Worker 	 * with the fractions being in units of the file-supplied resolution.
1385*8b26181fSAndroid Build Coastguard Worker 	 */
1386*8b26181fSAndroid Build Coastguard Worker 	sec = t / ps->ifaces[interface_id].tsresol + ps->ifaces[interface_id].tsoffset;
1387*8b26181fSAndroid Build Coastguard Worker 	frac = t % ps->ifaces[interface_id].tsresol;
1388*8b26181fSAndroid Build Coastguard Worker 
1389*8b26181fSAndroid Build Coastguard Worker 	/*
1390*8b26181fSAndroid Build Coastguard Worker 	 * Convert the fractions from units of the file-supplied resolution
1391*8b26181fSAndroid Build Coastguard Worker 	 * to units of the user-requested resolution.
1392*8b26181fSAndroid Build Coastguard Worker 	 */
1393*8b26181fSAndroid Build Coastguard Worker 	switch (ps->ifaces[interface_id].scale_type) {
1394*8b26181fSAndroid Build Coastguard Worker 
1395*8b26181fSAndroid Build Coastguard Worker 	case PASS_THROUGH:
1396*8b26181fSAndroid Build Coastguard Worker 		/*
1397*8b26181fSAndroid Build Coastguard Worker 		 * The interface resolution is what the user wants,
1398*8b26181fSAndroid Build Coastguard Worker 		 * so we're done.
1399*8b26181fSAndroid Build Coastguard Worker 		 */
1400*8b26181fSAndroid Build Coastguard Worker 		break;
1401*8b26181fSAndroid Build Coastguard Worker 
1402*8b26181fSAndroid Build Coastguard Worker 	case SCALE_UP_DEC:
1403*8b26181fSAndroid Build Coastguard Worker 		/*
1404*8b26181fSAndroid Build Coastguard Worker 		 * The interface resolution is less than what the user
1405*8b26181fSAndroid Build Coastguard Worker 		 * wants; scale the fractional part up to the units of
1406*8b26181fSAndroid Build Coastguard Worker 		 * the resolution the user requested by multiplying by
1407*8b26181fSAndroid Build Coastguard Worker 		 * the quotient of the user-requested resolution and the
1408*8b26181fSAndroid Build Coastguard Worker 		 * file-supplied resolution.
1409*8b26181fSAndroid Build Coastguard Worker 		 *
1410*8b26181fSAndroid Build Coastguard Worker 		 * Those resolutions are both powers of 10, and the user-
1411*8b26181fSAndroid Build Coastguard Worker 		 * requested resolution is greater than the file-supplied
1412*8b26181fSAndroid Build Coastguard Worker 		 * resolution, so the quotient in question is an integer.
1413*8b26181fSAndroid Build Coastguard Worker 		 * We've calculated that quotient already, so we just
1414*8b26181fSAndroid Build Coastguard Worker 		 * multiply by it.
1415*8b26181fSAndroid Build Coastguard Worker 		 */
1416*8b26181fSAndroid Build Coastguard Worker 		frac *= ps->ifaces[interface_id].scale_factor;
1417*8b26181fSAndroid Build Coastguard Worker 		break;
1418*8b26181fSAndroid Build Coastguard Worker 
1419*8b26181fSAndroid Build Coastguard Worker 	case SCALE_UP_BIN:
1420*8b26181fSAndroid Build Coastguard Worker 		/*
1421*8b26181fSAndroid Build Coastguard Worker 		 * The interface resolution is less than what the user
1422*8b26181fSAndroid Build Coastguard Worker 		 * wants; scale the fractional part up to the units of
1423*8b26181fSAndroid Build Coastguard Worker 		 * the resolution the user requested by multiplying by
1424*8b26181fSAndroid Build Coastguard Worker 		 * the quotient of the user-requested resolution and the
1425*8b26181fSAndroid Build Coastguard Worker 		 * file-supplied resolution.
1426*8b26181fSAndroid Build Coastguard Worker 		 *
1427*8b26181fSAndroid Build Coastguard Worker 		 * The file-supplied resolution is a power of 2, so the
1428*8b26181fSAndroid Build Coastguard Worker 		 * quotient is not an integer, so, in order to do this
1429*8b26181fSAndroid Build Coastguard Worker 		 * entirely with integer arithmetic, we multiply by the
1430*8b26181fSAndroid Build Coastguard Worker 		 * user-requested resolution and divide by the file-
1431*8b26181fSAndroid Build Coastguard Worker 		 * supplied resolution.
1432*8b26181fSAndroid Build Coastguard Worker 		 *
1433*8b26181fSAndroid Build Coastguard Worker 		 * XXX - Is there something clever we could do here,
1434*8b26181fSAndroid Build Coastguard Worker 		 * given that we know that the file-supplied resolution
1435*8b26181fSAndroid Build Coastguard Worker 		 * is a power of 2?  Doing a multiplication followed by
1436*8b26181fSAndroid Build Coastguard Worker 		 * a division runs the risk of overflowing, and involves
1437*8b26181fSAndroid Build Coastguard Worker 		 * two non-simple arithmetic operations.
1438*8b26181fSAndroid Build Coastguard Worker 		 */
1439*8b26181fSAndroid Build Coastguard Worker 		frac *= ps->user_tsresol;
1440*8b26181fSAndroid Build Coastguard Worker 		frac /= ps->ifaces[interface_id].tsresol;
1441*8b26181fSAndroid Build Coastguard Worker 		break;
1442*8b26181fSAndroid Build Coastguard Worker 
1443*8b26181fSAndroid Build Coastguard Worker 	case SCALE_DOWN_DEC:
1444*8b26181fSAndroid Build Coastguard Worker 		/*
1445*8b26181fSAndroid Build Coastguard Worker 		 * The interface resolution is greater than what the user
1446*8b26181fSAndroid Build Coastguard Worker 		 * wants; scale the fractional part up to the units of
1447*8b26181fSAndroid Build Coastguard Worker 		 * the resolution the user requested by multiplying by
1448*8b26181fSAndroid Build Coastguard Worker 		 * the quotient of the user-requested resolution and the
1449*8b26181fSAndroid Build Coastguard Worker 		 * file-supplied resolution.
1450*8b26181fSAndroid Build Coastguard Worker 		 *
1451*8b26181fSAndroid Build Coastguard Worker 		 * Those resolutions are both powers of 10, and the user-
1452*8b26181fSAndroid Build Coastguard Worker 		 * requested resolution is less than the file-supplied
1453*8b26181fSAndroid Build Coastguard Worker 		 * resolution, so the quotient in question isn't an
1454*8b26181fSAndroid Build Coastguard Worker 		 * integer, but its reciprocal is, and we can just divide
1455*8b26181fSAndroid Build Coastguard Worker 		 * by the reciprocal of the quotient.  We've calculated
1456*8b26181fSAndroid Build Coastguard Worker 		 * the reciprocal of that quotient already, so we must
1457*8b26181fSAndroid Build Coastguard Worker 		 * divide by it.
1458*8b26181fSAndroid Build Coastguard Worker 		 */
1459*8b26181fSAndroid Build Coastguard Worker 		frac /= ps->ifaces[interface_id].scale_factor;
1460*8b26181fSAndroid Build Coastguard Worker 		break;
1461*8b26181fSAndroid Build Coastguard Worker 
1462*8b26181fSAndroid Build Coastguard Worker 
1463*8b26181fSAndroid Build Coastguard Worker 	case SCALE_DOWN_BIN:
1464*8b26181fSAndroid Build Coastguard Worker 		/*
1465*8b26181fSAndroid Build Coastguard Worker 		 * The interface resolution is greater than what the user
1466*8b26181fSAndroid Build Coastguard Worker 		 * wants; convert the fractional part to units of the
1467*8b26181fSAndroid Build Coastguard Worker 		 * resolution the user requested by multiplying by the
1468*8b26181fSAndroid Build Coastguard Worker 		 * quotient of the user-requested resolution and the
1469*8b26181fSAndroid Build Coastguard Worker 		 * file-supplied resolution.  We do that by multiplying
1470*8b26181fSAndroid Build Coastguard Worker 		 * by the user-requested resolution and dividing by the
1471*8b26181fSAndroid Build Coastguard Worker 		 * file-supplied resolution, as the quotient might not
1472*8b26181fSAndroid Build Coastguard Worker 		 * fit in an integer.
1473*8b26181fSAndroid Build Coastguard Worker 		 *
1474*8b26181fSAndroid Build Coastguard Worker 		 * The file-supplied resolution is a power of 2, so the
1475*8b26181fSAndroid Build Coastguard Worker 		 * quotient is not an integer, and neither is its
1476*8b26181fSAndroid Build Coastguard Worker 		 * reciprocal, so, in order to do this entirely with
1477*8b26181fSAndroid Build Coastguard Worker 		 * integer arithmetic, we multiply by the user-requested
1478*8b26181fSAndroid Build Coastguard Worker 		 * resolution and divide by the file-supplied resolution.
1479*8b26181fSAndroid Build Coastguard Worker 		 *
1480*8b26181fSAndroid Build Coastguard Worker 		 * XXX - Is there something clever we could do here,
1481*8b26181fSAndroid Build Coastguard Worker 		 * given that we know that the file-supplied resolution
1482*8b26181fSAndroid Build Coastguard Worker 		 * is a power of 2?  Doing a multiplication followed by
1483*8b26181fSAndroid Build Coastguard Worker 		 * a division runs the risk of overflowing, and involves
1484*8b26181fSAndroid Build Coastguard Worker 		 * two non-simple arithmetic operations.
1485*8b26181fSAndroid Build Coastguard Worker 		 */
1486*8b26181fSAndroid Build Coastguard Worker 		frac *= ps->user_tsresol;
1487*8b26181fSAndroid Build Coastguard Worker 		frac /= ps->ifaces[interface_id].tsresol;
1488*8b26181fSAndroid Build Coastguard Worker 		break;
1489*8b26181fSAndroid Build Coastguard Worker 	}
1490*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
1491*8b26181fSAndroid Build Coastguard Worker 	/*
1492*8b26181fSAndroid Build Coastguard Worker 	 * tv_sec and tv_used in the Windows struct timeval are both
1493*8b26181fSAndroid Build Coastguard Worker 	 * longs.
1494*8b26181fSAndroid Build Coastguard Worker 	 */
1495*8b26181fSAndroid Build Coastguard Worker 	hdr->ts.tv_sec = (long)sec;
1496*8b26181fSAndroid Build Coastguard Worker 	hdr->ts.tv_usec = (long)frac;
1497*8b26181fSAndroid Build Coastguard Worker #else
1498*8b26181fSAndroid Build Coastguard Worker 	/*
1499*8b26181fSAndroid Build Coastguard Worker 	 * tv_sec in the UN*X struct timeval is a time_t; tv_usec is
1500*8b26181fSAndroid Build Coastguard Worker 	 * suseconds_t in UN*Xes that work the way the current Single
1501*8b26181fSAndroid Build Coastguard Worker 	 * UNIX Standard specify - but not all older UN*Xes necessarily
1502*8b26181fSAndroid Build Coastguard Worker 	 * support that type, so just cast to int.
1503*8b26181fSAndroid Build Coastguard Worker 	 */
1504*8b26181fSAndroid Build Coastguard Worker 	hdr->ts.tv_sec = (time_t)sec;
1505*8b26181fSAndroid Build Coastguard Worker 	hdr->ts.tv_usec = (int)frac;
1506*8b26181fSAndroid Build Coastguard Worker #endif
1507*8b26181fSAndroid Build Coastguard Worker 
1508*8b26181fSAndroid Build Coastguard Worker 	/*
1509*8b26181fSAndroid Build Coastguard Worker 	 * Get a pointer to the packet data.
1510*8b26181fSAndroid Build Coastguard Worker 	 */
1511*8b26181fSAndroid Build Coastguard Worker 	*data = get_from_block_data(&cursor, hdr->caplen, p->errbuf);
1512*8b26181fSAndroid Build Coastguard Worker 	if (*data == NULL)
1513*8b26181fSAndroid Build Coastguard Worker 		return (-1);
1514*8b26181fSAndroid Build Coastguard Worker 
1515*8b26181fSAndroid Build Coastguard Worker 	pcap_post_process(p->linktype, p->swapped, hdr, *data);
1516*8b26181fSAndroid Build Coastguard Worker 
1517*8b26181fSAndroid Build Coastguard Worker 	return (1);
1518*8b26181fSAndroid Build Coastguard Worker }
1519