xref: /aosp_15_r20/external/libpcap/pcap.c (revision 8b26181f966a6af5cf6981a6f474313de533bb28)
1*8b26181fSAndroid Build Coastguard Worker /*
2*8b26181fSAndroid Build Coastguard Worker  * Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998
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 the following conditions
7*8b26181fSAndroid Build Coastguard Worker  * are met:
8*8b26181fSAndroid Build Coastguard Worker  * 1. Redistributions of source code must retain the above copyright
9*8b26181fSAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer.
10*8b26181fSAndroid Build Coastguard Worker  * 2. Redistributions in binary form must reproduce the above copyright
11*8b26181fSAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer in the
12*8b26181fSAndroid Build Coastguard Worker  *    documentation and/or other materials provided with the distribution.
13*8b26181fSAndroid Build Coastguard Worker  * 3. All advertising materials mentioning features or use of this software
14*8b26181fSAndroid Build Coastguard Worker  *    must display the following acknowledgement:
15*8b26181fSAndroid Build Coastguard Worker  *	This product includes software developed by the Computer Systems
16*8b26181fSAndroid Build Coastguard Worker  *	Engineering Group at Lawrence Berkeley Laboratory.
17*8b26181fSAndroid Build Coastguard Worker  * 4. Neither the name of the University nor of the Laboratory may be used
18*8b26181fSAndroid Build Coastguard Worker  *    to endorse or promote products derived from this software without
19*8b26181fSAndroid Build Coastguard Worker  *    specific prior written permission.
20*8b26181fSAndroid Build Coastguard Worker  *
21*8b26181fSAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22*8b26181fSAndroid Build Coastguard Worker  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23*8b26181fSAndroid Build Coastguard Worker  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24*8b26181fSAndroid Build Coastguard Worker  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25*8b26181fSAndroid Build Coastguard Worker  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*8b26181fSAndroid Build Coastguard Worker  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27*8b26181fSAndroid Build Coastguard Worker  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28*8b26181fSAndroid Build Coastguard Worker  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29*8b26181fSAndroid Build Coastguard Worker  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30*8b26181fSAndroid Build Coastguard Worker  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*8b26181fSAndroid Build Coastguard Worker  * SUCH DAMAGE.
32*8b26181fSAndroid Build Coastguard Worker  */
33*8b26181fSAndroid Build Coastguard Worker 
34*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_CONFIG_H
35*8b26181fSAndroid Build Coastguard Worker #include <config.h>
36*8b26181fSAndroid Build Coastguard Worker #endif
37*8b26181fSAndroid Build Coastguard Worker 
38*8b26181fSAndroid Build Coastguard Worker #include <pcap-types.h>
39*8b26181fSAndroid Build Coastguard Worker #ifndef _WIN32
40*8b26181fSAndroid Build Coastguard Worker #include <sys/param.h>
41*8b26181fSAndroid Build Coastguard Worker #ifndef MSDOS
42*8b26181fSAndroid Build Coastguard Worker #include <sys/file.h>
43*8b26181fSAndroid Build Coastguard Worker #endif
44*8b26181fSAndroid Build Coastguard Worker #include <sys/ioctl.h>
45*8b26181fSAndroid Build Coastguard Worker #include <sys/socket.h>
46*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_SYS_SOCKIO_H
47*8b26181fSAndroid Build Coastguard Worker #include <sys/sockio.h>
48*8b26181fSAndroid Build Coastguard Worker #endif
49*8b26181fSAndroid Build Coastguard Worker 
50*8b26181fSAndroid Build Coastguard Worker struct mbuf;		/* Squelch compiler warnings on some platforms for */
51*8b26181fSAndroid Build Coastguard Worker struct rtentry;		/* declarations in <net/if.h> */
52*8b26181fSAndroid Build Coastguard Worker #include <net/if.h>
53*8b26181fSAndroid Build Coastguard Worker #include <netinet/in.h>
54*8b26181fSAndroid Build Coastguard Worker #endif /* _WIN32 */
55*8b26181fSAndroid Build Coastguard Worker 
56*8b26181fSAndroid Build Coastguard Worker #include <stdio.h>
57*8b26181fSAndroid Build Coastguard Worker #include <stdlib.h>
58*8b26181fSAndroid Build Coastguard Worker #include <string.h>
59*8b26181fSAndroid Build Coastguard Worker #if !defined(_MSC_VER) && !defined(__BORLANDC__) && !defined(__MINGW32__)
60*8b26181fSAndroid Build Coastguard Worker #include <unistd.h>
61*8b26181fSAndroid Build Coastguard Worker #endif
62*8b26181fSAndroid Build Coastguard Worker #include <fcntl.h>
63*8b26181fSAndroid Build Coastguard Worker #include <errno.h>
64*8b26181fSAndroid Build Coastguard Worker #include <limits.h>
65*8b26181fSAndroid Build Coastguard Worker 
66*8b26181fSAndroid Build Coastguard Worker #include "diag-control.h"
67*8b26181fSAndroid Build Coastguard Worker 
68*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OS_PROTO_H
69*8b26181fSAndroid Build Coastguard Worker #include "os-proto.h"
70*8b26181fSAndroid Build Coastguard Worker #endif
71*8b26181fSAndroid Build Coastguard Worker 
72*8b26181fSAndroid Build Coastguard Worker #ifdef MSDOS
73*8b26181fSAndroid Build Coastguard Worker #include "pcap-dos.h"
74*8b26181fSAndroid Build Coastguard Worker #endif
75*8b26181fSAndroid Build Coastguard Worker 
76*8b26181fSAndroid Build Coastguard Worker #include "pcap-int.h"
77*8b26181fSAndroid Build Coastguard Worker 
78*8b26181fSAndroid Build Coastguard Worker #include "optimize.h"
79*8b26181fSAndroid Build Coastguard Worker 
80*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_DAG_API
81*8b26181fSAndroid Build Coastguard Worker #include "pcap-dag.h"
82*8b26181fSAndroid Build Coastguard Worker #endif /* HAVE_DAG_API */
83*8b26181fSAndroid Build Coastguard Worker 
84*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_SEPTEL_API
85*8b26181fSAndroid Build Coastguard Worker #include "pcap-septel.h"
86*8b26181fSAndroid Build Coastguard Worker #endif /* HAVE_SEPTEL_API */
87*8b26181fSAndroid Build Coastguard Worker 
88*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_SNF_API
89*8b26181fSAndroid Build Coastguard Worker #include "pcap-snf.h"
90*8b26181fSAndroid Build Coastguard Worker #endif /* HAVE_SNF_API */
91*8b26181fSAndroid Build Coastguard Worker 
92*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_TC_API
93*8b26181fSAndroid Build Coastguard Worker #include "pcap-tc.h"
94*8b26181fSAndroid Build Coastguard Worker #endif /* HAVE_TC_API */
95*8b26181fSAndroid Build Coastguard Worker 
96*8b26181fSAndroid Build Coastguard Worker #ifdef PCAP_SUPPORT_LINUX_USBMON
97*8b26181fSAndroid Build Coastguard Worker #include "pcap-usb-linux.h"
98*8b26181fSAndroid Build Coastguard Worker #endif
99*8b26181fSAndroid Build Coastguard Worker 
100*8b26181fSAndroid Build Coastguard Worker #ifdef PCAP_SUPPORT_BT
101*8b26181fSAndroid Build Coastguard Worker #include "pcap-bt-linux.h"
102*8b26181fSAndroid Build Coastguard Worker #endif
103*8b26181fSAndroid Build Coastguard Worker 
104*8b26181fSAndroid Build Coastguard Worker #ifdef PCAP_SUPPORT_BT_MONITOR
105*8b26181fSAndroid Build Coastguard Worker #include "pcap-bt-monitor-linux.h"
106*8b26181fSAndroid Build Coastguard Worker #endif
107*8b26181fSAndroid Build Coastguard Worker 
108*8b26181fSAndroid Build Coastguard Worker #ifdef PCAP_SUPPORT_NETFILTER
109*8b26181fSAndroid Build Coastguard Worker #include "pcap-netfilter-linux.h"
110*8b26181fSAndroid Build Coastguard Worker #endif
111*8b26181fSAndroid Build Coastguard Worker 
112*8b26181fSAndroid Build Coastguard Worker #ifdef PCAP_SUPPORT_NETMAP
113*8b26181fSAndroid Build Coastguard Worker #include "pcap-netmap.h"
114*8b26181fSAndroid Build Coastguard Worker #endif
115*8b26181fSAndroid Build Coastguard Worker 
116*8b26181fSAndroid Build Coastguard Worker #ifdef PCAP_SUPPORT_DBUS
117*8b26181fSAndroid Build Coastguard Worker #include "pcap-dbus.h"
118*8b26181fSAndroid Build Coastguard Worker #endif
119*8b26181fSAndroid Build Coastguard Worker 
120*8b26181fSAndroid Build Coastguard Worker #ifdef PCAP_SUPPORT_RDMASNIFF
121*8b26181fSAndroid Build Coastguard Worker #include "pcap-rdmasniff.h"
122*8b26181fSAndroid Build Coastguard Worker #endif
123*8b26181fSAndroid Build Coastguard Worker 
124*8b26181fSAndroid Build Coastguard Worker #ifdef PCAP_SUPPORT_DPDK
125*8b26181fSAndroid Build Coastguard Worker #include "pcap-dpdk.h"
126*8b26181fSAndroid Build Coastguard Worker #endif
127*8b26181fSAndroid Build Coastguard Worker 
128*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_AIRPCAP_API
129*8b26181fSAndroid Build Coastguard Worker #include "pcap-airpcap.h"
130*8b26181fSAndroid Build Coastguard Worker #endif
131*8b26181fSAndroid Build Coastguard Worker 
132*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
133*8b26181fSAndroid Build Coastguard Worker /*
134*8b26181fSAndroid Build Coastguard Worker  * To quote the WSAStartup() documentation:
135*8b26181fSAndroid Build Coastguard Worker  *
136*8b26181fSAndroid Build Coastguard Worker  *   The WSAStartup function typically leads to protocol-specific helper
137*8b26181fSAndroid Build Coastguard Worker  *   DLLs being loaded. As a result, the WSAStartup function should not
138*8b26181fSAndroid Build Coastguard Worker  *   be called from the DllMain function in a application DLL. This can
139*8b26181fSAndroid Build Coastguard Worker  *   potentially cause deadlocks.
140*8b26181fSAndroid Build Coastguard Worker  *
141*8b26181fSAndroid Build Coastguard Worker  * and the WSACleanup() documentation:
142*8b26181fSAndroid Build Coastguard Worker  *
143*8b26181fSAndroid Build Coastguard Worker  *   The WSACleanup function typically leads to protocol-specific helper
144*8b26181fSAndroid Build Coastguard Worker  *   DLLs being unloaded. As a result, the WSACleanup function should not
145*8b26181fSAndroid Build Coastguard Worker  *   be called from the DllMain function in a application DLL. This can
146*8b26181fSAndroid Build Coastguard Worker  *   potentially cause deadlocks.
147*8b26181fSAndroid Build Coastguard Worker  *
148*8b26181fSAndroid Build Coastguard Worker  * So we don't initialize Winsock in a DllMain() routine.
149*8b26181fSAndroid Build Coastguard Worker  *
150*8b26181fSAndroid Build Coastguard Worker  * pcap_init() should be called to initialize pcap on both UN*X and
151*8b26181fSAndroid Build Coastguard Worker  * Windows; it will initialize Winsock on Windows.  (It will also be
152*8b26181fSAndroid Build Coastguard Worker  * initialized as needed if pcap_init() hasn't been called.)
153*8b26181fSAndroid Build Coastguard Worker  */
154*8b26181fSAndroid Build Coastguard Worker 
155*8b26181fSAndroid Build Coastguard Worker /*
156*8b26181fSAndroid Build Coastguard Worker  * Start Winsock.
157*8b26181fSAndroid Build Coastguard Worker  * Internal routine.
158*8b26181fSAndroid Build Coastguard Worker  */
159*8b26181fSAndroid Build Coastguard Worker static int
internal_wsockinit(char * errbuf)160*8b26181fSAndroid Build Coastguard Worker internal_wsockinit(char *errbuf)
161*8b26181fSAndroid Build Coastguard Worker {
162*8b26181fSAndroid Build Coastguard Worker 	WORD wVersionRequested;
163*8b26181fSAndroid Build Coastguard Worker 	WSADATA wsaData;
164*8b26181fSAndroid Build Coastguard Worker 	static int err = -1;
165*8b26181fSAndroid Build Coastguard Worker 	static int done = 0;
166*8b26181fSAndroid Build Coastguard Worker 	int status;
167*8b26181fSAndroid Build Coastguard Worker 
168*8b26181fSAndroid Build Coastguard Worker 	if (done)
169*8b26181fSAndroid Build Coastguard Worker 		return (err);
170*8b26181fSAndroid Build Coastguard Worker 
171*8b26181fSAndroid Build Coastguard Worker 	/*
172*8b26181fSAndroid Build Coastguard Worker 	 * Versions of Windows that don't support Winsock 2.2 are
173*8b26181fSAndroid Build Coastguard Worker 	 * too old for us.
174*8b26181fSAndroid Build Coastguard Worker 	 */
175*8b26181fSAndroid Build Coastguard Worker 	wVersionRequested = MAKEWORD(2, 2);
176*8b26181fSAndroid Build Coastguard Worker 	status = WSAStartup(wVersionRequested, &wsaData);
177*8b26181fSAndroid Build Coastguard Worker 	done = 1;
178*8b26181fSAndroid Build Coastguard Worker 	if (status != 0) {
179*8b26181fSAndroid Build Coastguard Worker 		if (errbuf != NULL) {
180*8b26181fSAndroid Build Coastguard Worker 			pcap_fmt_errmsg_for_win32_err(errbuf, PCAP_ERRBUF_SIZE,
181*8b26181fSAndroid Build Coastguard Worker 			    status, "WSAStartup() failed");
182*8b26181fSAndroid Build Coastguard Worker 		}
183*8b26181fSAndroid Build Coastguard Worker 		return (err);
184*8b26181fSAndroid Build Coastguard Worker 	}
185*8b26181fSAndroid Build Coastguard Worker 	atexit ((void(*)(void))WSACleanup);
186*8b26181fSAndroid Build Coastguard Worker 	err = 0;
187*8b26181fSAndroid Build Coastguard Worker 	return (err);
188*8b26181fSAndroid Build Coastguard Worker }
189*8b26181fSAndroid Build Coastguard Worker 
190*8b26181fSAndroid Build Coastguard Worker /*
191*8b26181fSAndroid Build Coastguard Worker  * Exported in case some applications using WinPcap/Npcap called it,
192*8b26181fSAndroid Build Coastguard Worker  * even though it wasn't exported.
193*8b26181fSAndroid Build Coastguard Worker  */
194*8b26181fSAndroid Build Coastguard Worker int
wsockinit(void)195*8b26181fSAndroid Build Coastguard Worker wsockinit(void)
196*8b26181fSAndroid Build Coastguard Worker {
197*8b26181fSAndroid Build Coastguard Worker 	return (internal_wsockinit(NULL));
198*8b26181fSAndroid Build Coastguard Worker }
199*8b26181fSAndroid Build Coastguard Worker 
200*8b26181fSAndroid Build Coastguard Worker /*
201*8b26181fSAndroid Build Coastguard Worker  * This is the exported function; new programs should call this.
202*8b26181fSAndroid Build Coastguard Worker  * *Newer* programs should call pcap_init().
203*8b26181fSAndroid Build Coastguard Worker  */
204*8b26181fSAndroid Build Coastguard Worker int
pcap_wsockinit(void)205*8b26181fSAndroid Build Coastguard Worker pcap_wsockinit(void)
206*8b26181fSAndroid Build Coastguard Worker {
207*8b26181fSAndroid Build Coastguard Worker 	return (internal_wsockinit(NULL));
208*8b26181fSAndroid Build Coastguard Worker }
209*8b26181fSAndroid Build Coastguard Worker #endif /* _WIN32 */
210*8b26181fSAndroid Build Coastguard Worker 
211*8b26181fSAndroid Build Coastguard Worker /*
212*8b26181fSAndroid Build Coastguard Worker  * Do whatever initialization is needed for libpcap.
213*8b26181fSAndroid Build Coastguard Worker  *
214*8b26181fSAndroid Build Coastguard Worker  * The argument specifies whether we use the local code page or UTF-8
215*8b26181fSAndroid Build Coastguard Worker  * for strings; on UN*X, we just assume UTF-8 in places where the encoding
216*8b26181fSAndroid Build Coastguard Worker  * would matter, whereas, on Windows, we use the local code page for
217*8b26181fSAndroid Build Coastguard Worker  * PCAP_CHAR_ENC_LOCAL and UTF-8 for PCAP_CHAR_ENC_UTF_8.
218*8b26181fSAndroid Build Coastguard Worker  *
219*8b26181fSAndroid Build Coastguard Worker  * On Windows, we also disable the hack in pcap_create() to deal with
220*8b26181fSAndroid Build Coastguard Worker  * being handed UTF-16 strings, because if the user calls this they're
221*8b26181fSAndroid Build Coastguard Worker  * explicitly declaring that they will either be passing local code
222*8b26181fSAndroid Build Coastguard Worker  * page strings or UTF-8 strings, so we don't need to allow UTF-16LE
223*8b26181fSAndroid Build Coastguard Worker  * strings to be passed.  For good measure, on Windows *and* UN*X,
224*8b26181fSAndroid Build Coastguard Worker  * we disable pcap_lookupdev(), to prevent anybody from even
225*8b26181fSAndroid Build Coastguard Worker  * *trying* to pass the result of pcap_lookupdev() - which might be
226*8b26181fSAndroid Build Coastguard Worker  * UTF-16LE on Windows, for ugly compatibility reasons - to pcap_create()
227*8b26181fSAndroid Build Coastguard Worker  * or pcap_open_live() or pcap_open().
228*8b26181fSAndroid Build Coastguard Worker  *
229*8b26181fSAndroid Build Coastguard Worker  * Returns 0 on success, -1 on error.
230*8b26181fSAndroid Build Coastguard Worker  */
231*8b26181fSAndroid Build Coastguard Worker int pcap_new_api;		/* pcap_lookupdev() always fails */
232*8b26181fSAndroid Build Coastguard Worker int pcap_utf_8_mode;		/* Strings should be in UTF-8. */
233*8b26181fSAndroid Build Coastguard Worker 
234*8b26181fSAndroid Build Coastguard Worker int
pcap_init(unsigned int opts,char * errbuf)235*8b26181fSAndroid Build Coastguard Worker pcap_init(unsigned int opts, char *errbuf)
236*8b26181fSAndroid Build Coastguard Worker {
237*8b26181fSAndroid Build Coastguard Worker 	static int initialized;
238*8b26181fSAndroid Build Coastguard Worker 
239*8b26181fSAndroid Build Coastguard Worker 	/*
240*8b26181fSAndroid Build Coastguard Worker 	 * Don't allow multiple calls that set different modes; that
241*8b26181fSAndroid Build Coastguard Worker 	 * may mean a library is initializing pcap in one mode and
242*8b26181fSAndroid Build Coastguard Worker 	 * a program using that library, or another library used by
243*8b26181fSAndroid Build Coastguard Worker 	 * that program, is initializing it in another mode.
244*8b26181fSAndroid Build Coastguard Worker 	 */
245*8b26181fSAndroid Build Coastguard Worker 	switch (opts) {
246*8b26181fSAndroid Build Coastguard Worker 
247*8b26181fSAndroid Build Coastguard Worker 	case PCAP_CHAR_ENC_LOCAL:
248*8b26181fSAndroid Build Coastguard Worker 		/* Leave "UTF-8 mode" off. */
249*8b26181fSAndroid Build Coastguard Worker 		if (initialized) {
250*8b26181fSAndroid Build Coastguard Worker 			if (pcap_utf_8_mode) {
251*8b26181fSAndroid Build Coastguard Worker 				snprintf(errbuf, PCAP_ERRBUF_SIZE,
252*8b26181fSAndroid Build Coastguard Worker 				    "Multiple pcap_init calls with different character encodings");
253*8b26181fSAndroid Build Coastguard Worker 				return (PCAP_ERROR);
254*8b26181fSAndroid Build Coastguard Worker 			}
255*8b26181fSAndroid Build Coastguard Worker 		}
256*8b26181fSAndroid Build Coastguard Worker 		break;
257*8b26181fSAndroid Build Coastguard Worker 
258*8b26181fSAndroid Build Coastguard Worker 	case PCAP_CHAR_ENC_UTF_8:
259*8b26181fSAndroid Build Coastguard Worker 		/* Turn on "UTF-8 mode". */
260*8b26181fSAndroid Build Coastguard Worker 		if (initialized) {
261*8b26181fSAndroid Build Coastguard Worker 			if (!pcap_utf_8_mode) {
262*8b26181fSAndroid Build Coastguard Worker 				snprintf(errbuf, PCAP_ERRBUF_SIZE,
263*8b26181fSAndroid Build Coastguard Worker 				    "Multiple pcap_init calls with different character encodings");
264*8b26181fSAndroid Build Coastguard Worker 				return (PCAP_ERROR);
265*8b26181fSAndroid Build Coastguard Worker 			}
266*8b26181fSAndroid Build Coastguard Worker 		}
267*8b26181fSAndroid Build Coastguard Worker 		pcap_utf_8_mode = 1;
268*8b26181fSAndroid Build Coastguard Worker 		break;
269*8b26181fSAndroid Build Coastguard Worker 
270*8b26181fSAndroid Build Coastguard Worker 	default:
271*8b26181fSAndroid Build Coastguard Worker 		snprintf(errbuf, PCAP_ERRBUF_SIZE, "Unknown options specified");
272*8b26181fSAndroid Build Coastguard Worker 		return (PCAP_ERROR);
273*8b26181fSAndroid Build Coastguard Worker 	}
274*8b26181fSAndroid Build Coastguard Worker 
275*8b26181fSAndroid Build Coastguard Worker 	/*
276*8b26181fSAndroid Build Coastguard Worker 	 * Turn the appropriate mode on for error messages; those routines
277*8b26181fSAndroid Build Coastguard Worker 	 * are also used in rpcapd, which has no access to pcap's internal
278*8b26181fSAndroid Build Coastguard Worker 	 * UTF-8 mode flag, so we have to call a routine to set its
279*8b26181fSAndroid Build Coastguard Worker 	 * UTF-8 mode flag.
280*8b26181fSAndroid Build Coastguard Worker 	 */
281*8b26181fSAndroid Build Coastguard Worker 	pcap_fmt_set_encoding(opts);
282*8b26181fSAndroid Build Coastguard Worker 
283*8b26181fSAndroid Build Coastguard Worker 	if (initialized) {
284*8b26181fSAndroid Build Coastguard Worker 		/*
285*8b26181fSAndroid Build Coastguard Worker 		 * Nothing more to do; for example, on Windows, we've
286*8b26181fSAndroid Build Coastguard Worker 		 * already initialized Winsock.
287*8b26181fSAndroid Build Coastguard Worker 		 */
288*8b26181fSAndroid Build Coastguard Worker 		return (0);
289*8b26181fSAndroid Build Coastguard Worker 	}
290*8b26181fSAndroid Build Coastguard Worker 
291*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
292*8b26181fSAndroid Build Coastguard Worker 	/*
293*8b26181fSAndroid Build Coastguard Worker 	 * Now set up Winsock.
294*8b26181fSAndroid Build Coastguard Worker 	 */
295*8b26181fSAndroid Build Coastguard Worker 	if (internal_wsockinit(errbuf) == -1) {
296*8b26181fSAndroid Build Coastguard Worker 		/* Failed. */
297*8b26181fSAndroid Build Coastguard Worker 		return (PCAP_ERROR);
298*8b26181fSAndroid Build Coastguard Worker 	}
299*8b26181fSAndroid Build Coastguard Worker #endif
300*8b26181fSAndroid Build Coastguard Worker 
301*8b26181fSAndroid Build Coastguard Worker 	/*
302*8b26181fSAndroid Build Coastguard Worker 	 * We're done.
303*8b26181fSAndroid Build Coastguard Worker 	 */
304*8b26181fSAndroid Build Coastguard Worker 	initialized = 1;
305*8b26181fSAndroid Build Coastguard Worker 	pcap_new_api = 1;
306*8b26181fSAndroid Build Coastguard Worker 	return (0);
307*8b26181fSAndroid Build Coastguard Worker }
308*8b26181fSAndroid Build Coastguard Worker 
309*8b26181fSAndroid Build Coastguard Worker /*
310*8b26181fSAndroid Build Coastguard Worker  * String containing the library version.
311*8b26181fSAndroid Build Coastguard Worker  * Not explicitly exported via a header file - the right API to use
312*8b26181fSAndroid Build Coastguard Worker  * is pcap_lib_version() - but some programs included it, so we
313*8b26181fSAndroid Build Coastguard Worker  * provide it.
314*8b26181fSAndroid Build Coastguard Worker  *
315*8b26181fSAndroid Build Coastguard Worker  * We declare it here, right before defining it, to squelch any
316*8b26181fSAndroid Build Coastguard Worker  * warnings we might get from compilers about the lack of a
317*8b26181fSAndroid Build Coastguard Worker  * declaration.
318*8b26181fSAndroid Build Coastguard Worker  */
319*8b26181fSAndroid Build Coastguard Worker PCAP_API char pcap_version[];
320*8b26181fSAndroid Build Coastguard Worker PCAP_API_DEF char pcap_version[] = PACKAGE_VERSION;
321*8b26181fSAndroid Build Coastguard Worker 
322*8b26181fSAndroid Build Coastguard Worker static void
pcap_set_not_initialized_message(pcap_t * pcap)323*8b26181fSAndroid Build Coastguard Worker pcap_set_not_initialized_message(pcap_t *pcap)
324*8b26181fSAndroid Build Coastguard Worker {
325*8b26181fSAndroid Build Coastguard Worker 	if (pcap->activated) {
326*8b26181fSAndroid Build Coastguard Worker 		/* A module probably forgot to set the function pointer */
327*8b26181fSAndroid Build Coastguard Worker 		(void)snprintf(pcap->errbuf, sizeof(pcap->errbuf),
328*8b26181fSAndroid Build Coastguard Worker 		    "This operation isn't properly handled by that device");
329*8b26181fSAndroid Build Coastguard Worker 		return;
330*8b26181fSAndroid Build Coastguard Worker 	}
331*8b26181fSAndroid Build Coastguard Worker 	/* in case the caller doesn't check for PCAP_ERROR_NOT_ACTIVATED */
332*8b26181fSAndroid Build Coastguard Worker 	(void)snprintf(pcap->errbuf, sizeof(pcap->errbuf),
333*8b26181fSAndroid Build Coastguard Worker 	    "This handle hasn't been activated yet");
334*8b26181fSAndroid Build Coastguard Worker }
335*8b26181fSAndroid Build Coastguard Worker 
336*8b26181fSAndroid Build Coastguard Worker static int
pcap_read_not_initialized(pcap_t * pcap,int cnt _U_,pcap_handler callback _U_,u_char * user _U_)337*8b26181fSAndroid Build Coastguard Worker pcap_read_not_initialized(pcap_t *pcap, int cnt _U_, pcap_handler callback _U_,
338*8b26181fSAndroid Build Coastguard Worker     u_char *user _U_)
339*8b26181fSAndroid Build Coastguard Worker {
340*8b26181fSAndroid Build Coastguard Worker 	pcap_set_not_initialized_message(pcap);
341*8b26181fSAndroid Build Coastguard Worker 	/* this means 'not initialized' */
342*8b26181fSAndroid Build Coastguard Worker 	return (PCAP_ERROR_NOT_ACTIVATED);
343*8b26181fSAndroid Build Coastguard Worker }
344*8b26181fSAndroid Build Coastguard Worker 
345*8b26181fSAndroid Build Coastguard Worker static int
pcap_inject_not_initialized(pcap_t * pcap,const void * buf _U_,int size _U_)346*8b26181fSAndroid Build Coastguard Worker pcap_inject_not_initialized(pcap_t *pcap, const void * buf _U_, int size _U_)
347*8b26181fSAndroid Build Coastguard Worker {
348*8b26181fSAndroid Build Coastguard Worker 	pcap_set_not_initialized_message(pcap);
349*8b26181fSAndroid Build Coastguard Worker 	/* this means 'not initialized' */
350*8b26181fSAndroid Build Coastguard Worker 	return (PCAP_ERROR_NOT_ACTIVATED);
351*8b26181fSAndroid Build Coastguard Worker }
352*8b26181fSAndroid Build Coastguard Worker 
353*8b26181fSAndroid Build Coastguard Worker static int
pcap_setfilter_not_initialized(pcap_t * pcap,struct bpf_program * fp _U_)354*8b26181fSAndroid Build Coastguard Worker pcap_setfilter_not_initialized(pcap_t *pcap, struct bpf_program *fp _U_)
355*8b26181fSAndroid Build Coastguard Worker {
356*8b26181fSAndroid Build Coastguard Worker 	pcap_set_not_initialized_message(pcap);
357*8b26181fSAndroid Build Coastguard Worker 	/* this means 'not initialized' */
358*8b26181fSAndroid Build Coastguard Worker 	return (PCAP_ERROR_NOT_ACTIVATED);
359*8b26181fSAndroid Build Coastguard Worker }
360*8b26181fSAndroid Build Coastguard Worker 
361*8b26181fSAndroid Build Coastguard Worker static int
pcap_setdirection_not_initialized(pcap_t * pcap,pcap_direction_t d _U_)362*8b26181fSAndroid Build Coastguard Worker pcap_setdirection_not_initialized(pcap_t *pcap, pcap_direction_t d _U_)
363*8b26181fSAndroid Build Coastguard Worker {
364*8b26181fSAndroid Build Coastguard Worker 	pcap_set_not_initialized_message(pcap);
365*8b26181fSAndroid Build Coastguard Worker 	/* this means 'not initialized' */
366*8b26181fSAndroid Build Coastguard Worker 	return (PCAP_ERROR_NOT_ACTIVATED);
367*8b26181fSAndroid Build Coastguard Worker }
368*8b26181fSAndroid Build Coastguard Worker 
369*8b26181fSAndroid Build Coastguard Worker static int
pcap_set_datalink_not_initialized(pcap_t * pcap,int dlt _U_)370*8b26181fSAndroid Build Coastguard Worker pcap_set_datalink_not_initialized(pcap_t *pcap, int dlt _U_)
371*8b26181fSAndroid Build Coastguard Worker {
372*8b26181fSAndroid Build Coastguard Worker 	pcap_set_not_initialized_message(pcap);
373*8b26181fSAndroid Build Coastguard Worker 	/* this means 'not initialized' */
374*8b26181fSAndroid Build Coastguard Worker 	return (PCAP_ERROR_NOT_ACTIVATED);
375*8b26181fSAndroid Build Coastguard Worker }
376*8b26181fSAndroid Build Coastguard Worker 
377*8b26181fSAndroid Build Coastguard Worker static int
pcap_getnonblock_not_initialized(pcap_t * pcap)378*8b26181fSAndroid Build Coastguard Worker pcap_getnonblock_not_initialized(pcap_t *pcap)
379*8b26181fSAndroid Build Coastguard Worker {
380*8b26181fSAndroid Build Coastguard Worker 	pcap_set_not_initialized_message(pcap);
381*8b26181fSAndroid Build Coastguard Worker 	/* this means 'not initialized' */
382*8b26181fSAndroid Build Coastguard Worker 	return (PCAP_ERROR_NOT_ACTIVATED);
383*8b26181fSAndroid Build Coastguard Worker }
384*8b26181fSAndroid Build Coastguard Worker 
385*8b26181fSAndroid Build Coastguard Worker static int
pcap_stats_not_initialized(pcap_t * pcap,struct pcap_stat * ps _U_)386*8b26181fSAndroid Build Coastguard Worker pcap_stats_not_initialized(pcap_t *pcap, struct pcap_stat *ps _U_)
387*8b26181fSAndroid Build Coastguard Worker {
388*8b26181fSAndroid Build Coastguard Worker 	pcap_set_not_initialized_message(pcap);
389*8b26181fSAndroid Build Coastguard Worker 	/* this means 'not initialized' */
390*8b26181fSAndroid Build Coastguard Worker 	return (PCAP_ERROR_NOT_ACTIVATED);
391*8b26181fSAndroid Build Coastguard Worker }
392*8b26181fSAndroid Build Coastguard Worker 
393*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
394*8b26181fSAndroid Build Coastguard Worker static struct pcap_stat *
pcap_stats_ex_not_initialized(pcap_t * pcap,int * pcap_stat_size _U_)395*8b26181fSAndroid Build Coastguard Worker pcap_stats_ex_not_initialized(pcap_t *pcap, int *pcap_stat_size _U_)
396*8b26181fSAndroid Build Coastguard Worker {
397*8b26181fSAndroid Build Coastguard Worker 	pcap_set_not_initialized_message(pcap);
398*8b26181fSAndroid Build Coastguard Worker 	return (NULL);
399*8b26181fSAndroid Build Coastguard Worker }
400*8b26181fSAndroid Build Coastguard Worker 
401*8b26181fSAndroid Build Coastguard Worker static int
pcap_setbuff_not_initialized(pcap_t * pcap,int dim _U_)402*8b26181fSAndroid Build Coastguard Worker pcap_setbuff_not_initialized(pcap_t *pcap, int dim _U_)
403*8b26181fSAndroid Build Coastguard Worker {
404*8b26181fSAndroid Build Coastguard Worker 	pcap_set_not_initialized_message(pcap);
405*8b26181fSAndroid Build Coastguard Worker 	/* this means 'not initialized' */
406*8b26181fSAndroid Build Coastguard Worker 	return (PCAP_ERROR_NOT_ACTIVATED);
407*8b26181fSAndroid Build Coastguard Worker }
408*8b26181fSAndroid Build Coastguard Worker 
409*8b26181fSAndroid Build Coastguard Worker static int
pcap_setmode_not_initialized(pcap_t * pcap,int mode _U_)410*8b26181fSAndroid Build Coastguard Worker pcap_setmode_not_initialized(pcap_t *pcap, int mode _U_)
411*8b26181fSAndroid Build Coastguard Worker {
412*8b26181fSAndroid Build Coastguard Worker 	pcap_set_not_initialized_message(pcap);
413*8b26181fSAndroid Build Coastguard Worker 	/* this means 'not initialized' */
414*8b26181fSAndroid Build Coastguard Worker 	return (PCAP_ERROR_NOT_ACTIVATED);
415*8b26181fSAndroid Build Coastguard Worker }
416*8b26181fSAndroid Build Coastguard Worker 
417*8b26181fSAndroid Build Coastguard Worker static int
pcap_setmintocopy_not_initialized(pcap_t * pcap,int size _U_)418*8b26181fSAndroid Build Coastguard Worker pcap_setmintocopy_not_initialized(pcap_t *pcap, int size _U_)
419*8b26181fSAndroid Build Coastguard Worker {
420*8b26181fSAndroid Build Coastguard Worker 	pcap_set_not_initialized_message(pcap);
421*8b26181fSAndroid Build Coastguard Worker 	/* this means 'not initialized' */
422*8b26181fSAndroid Build Coastguard Worker 	return (PCAP_ERROR_NOT_ACTIVATED);
423*8b26181fSAndroid Build Coastguard Worker }
424*8b26181fSAndroid Build Coastguard Worker 
425*8b26181fSAndroid Build Coastguard Worker static HANDLE
pcap_getevent_not_initialized(pcap_t * pcap)426*8b26181fSAndroid Build Coastguard Worker pcap_getevent_not_initialized(pcap_t *pcap)
427*8b26181fSAndroid Build Coastguard Worker {
428*8b26181fSAndroid Build Coastguard Worker 	pcap_set_not_initialized_message(pcap);
429*8b26181fSAndroid Build Coastguard Worker 	return (INVALID_HANDLE_VALUE);
430*8b26181fSAndroid Build Coastguard Worker }
431*8b26181fSAndroid Build Coastguard Worker 
432*8b26181fSAndroid Build Coastguard Worker static int
pcap_oid_get_request_not_initialized(pcap_t * pcap,bpf_u_int32 oid _U_,void * data _U_,size_t * lenp _U_)433*8b26181fSAndroid Build Coastguard Worker pcap_oid_get_request_not_initialized(pcap_t *pcap, bpf_u_int32 oid _U_,
434*8b26181fSAndroid Build Coastguard Worker     void *data _U_, size_t *lenp _U_)
435*8b26181fSAndroid Build Coastguard Worker {
436*8b26181fSAndroid Build Coastguard Worker 	pcap_set_not_initialized_message(pcap);
437*8b26181fSAndroid Build Coastguard Worker 	return (PCAP_ERROR_NOT_ACTIVATED);
438*8b26181fSAndroid Build Coastguard Worker }
439*8b26181fSAndroid Build Coastguard Worker 
440*8b26181fSAndroid Build Coastguard Worker static int
pcap_oid_set_request_not_initialized(pcap_t * pcap,bpf_u_int32 oid _U_,const void * data _U_,size_t * lenp _U_)441*8b26181fSAndroid Build Coastguard Worker pcap_oid_set_request_not_initialized(pcap_t *pcap, bpf_u_int32 oid _U_,
442*8b26181fSAndroid Build Coastguard Worker     const void *data _U_, size_t *lenp _U_)
443*8b26181fSAndroid Build Coastguard Worker {
444*8b26181fSAndroid Build Coastguard Worker 	pcap_set_not_initialized_message(pcap);
445*8b26181fSAndroid Build Coastguard Worker 	return (PCAP_ERROR_NOT_ACTIVATED);
446*8b26181fSAndroid Build Coastguard Worker }
447*8b26181fSAndroid Build Coastguard Worker 
448*8b26181fSAndroid Build Coastguard Worker static u_int
pcap_sendqueue_transmit_not_initialized(pcap_t * pcap,pcap_send_queue * queue _U_,int sync _U_)449*8b26181fSAndroid Build Coastguard Worker pcap_sendqueue_transmit_not_initialized(pcap_t *pcap, pcap_send_queue* queue _U_,
450*8b26181fSAndroid Build Coastguard Worker     int sync _U_)
451*8b26181fSAndroid Build Coastguard Worker {
452*8b26181fSAndroid Build Coastguard Worker 	pcap_set_not_initialized_message(pcap);
453*8b26181fSAndroid Build Coastguard Worker 	return (0);
454*8b26181fSAndroid Build Coastguard Worker }
455*8b26181fSAndroid Build Coastguard Worker 
456*8b26181fSAndroid Build Coastguard Worker static int
pcap_setuserbuffer_not_initialized(pcap_t * pcap,int size _U_)457*8b26181fSAndroid Build Coastguard Worker pcap_setuserbuffer_not_initialized(pcap_t *pcap, int size _U_)
458*8b26181fSAndroid Build Coastguard Worker {
459*8b26181fSAndroid Build Coastguard Worker 	pcap_set_not_initialized_message(pcap);
460*8b26181fSAndroid Build Coastguard Worker 	return (PCAP_ERROR_NOT_ACTIVATED);
461*8b26181fSAndroid Build Coastguard Worker }
462*8b26181fSAndroid Build Coastguard Worker 
463*8b26181fSAndroid Build Coastguard Worker static int
pcap_live_dump_not_initialized(pcap_t * pcap,char * filename _U_,int maxsize _U_,int maxpacks _U_)464*8b26181fSAndroid Build Coastguard Worker pcap_live_dump_not_initialized(pcap_t *pcap, char *filename _U_, int maxsize _U_,
465*8b26181fSAndroid Build Coastguard Worker     int maxpacks _U_)
466*8b26181fSAndroid Build Coastguard Worker {
467*8b26181fSAndroid Build Coastguard Worker 	pcap_set_not_initialized_message(pcap);
468*8b26181fSAndroid Build Coastguard Worker 	return (PCAP_ERROR_NOT_ACTIVATED);
469*8b26181fSAndroid Build Coastguard Worker }
470*8b26181fSAndroid Build Coastguard Worker 
471*8b26181fSAndroid Build Coastguard Worker static int
pcap_live_dump_ended_not_initialized(pcap_t * pcap,int sync _U_)472*8b26181fSAndroid Build Coastguard Worker pcap_live_dump_ended_not_initialized(pcap_t *pcap, int sync _U_)
473*8b26181fSAndroid Build Coastguard Worker {
474*8b26181fSAndroid Build Coastguard Worker 	pcap_set_not_initialized_message(pcap);
475*8b26181fSAndroid Build Coastguard Worker 	return (PCAP_ERROR_NOT_ACTIVATED);
476*8b26181fSAndroid Build Coastguard Worker }
477*8b26181fSAndroid Build Coastguard Worker 
478*8b26181fSAndroid Build Coastguard Worker static PAirpcapHandle
pcap_get_airpcap_handle_not_initialized(pcap_t * pcap)479*8b26181fSAndroid Build Coastguard Worker pcap_get_airpcap_handle_not_initialized(pcap_t *pcap)
480*8b26181fSAndroid Build Coastguard Worker {
481*8b26181fSAndroid Build Coastguard Worker 	pcap_set_not_initialized_message(pcap);
482*8b26181fSAndroid Build Coastguard Worker 	return (NULL);
483*8b26181fSAndroid Build Coastguard Worker }
484*8b26181fSAndroid Build Coastguard Worker #endif
485*8b26181fSAndroid Build Coastguard Worker 
486*8b26181fSAndroid Build Coastguard Worker /*
487*8b26181fSAndroid Build Coastguard Worker  * Returns 1 if rfmon mode can be set on the pcap_t, 0 if it can't,
488*8b26181fSAndroid Build Coastguard Worker  * a PCAP_ERROR value on an error.
489*8b26181fSAndroid Build Coastguard Worker  */
490*8b26181fSAndroid Build Coastguard Worker int
pcap_can_set_rfmon(pcap_t * p)491*8b26181fSAndroid Build Coastguard Worker pcap_can_set_rfmon(pcap_t *p)
492*8b26181fSAndroid Build Coastguard Worker {
493*8b26181fSAndroid Build Coastguard Worker 	return (p->can_set_rfmon_op(p));
494*8b26181fSAndroid Build Coastguard Worker }
495*8b26181fSAndroid Build Coastguard Worker 
496*8b26181fSAndroid Build Coastguard Worker /*
497*8b26181fSAndroid Build Coastguard Worker  * For systems where rfmon mode is never supported.
498*8b26181fSAndroid Build Coastguard Worker  */
499*8b26181fSAndroid Build Coastguard Worker static int
pcap_cant_set_rfmon(pcap_t * p _U_)500*8b26181fSAndroid Build Coastguard Worker pcap_cant_set_rfmon(pcap_t *p _U_)
501*8b26181fSAndroid Build Coastguard Worker {
502*8b26181fSAndroid Build Coastguard Worker 	return (0);
503*8b26181fSAndroid Build Coastguard Worker }
504*8b26181fSAndroid Build Coastguard Worker 
505*8b26181fSAndroid Build Coastguard Worker /*
506*8b26181fSAndroid Build Coastguard Worker  * Sets *tstamp_typesp to point to an array 1 or more supported time stamp
507*8b26181fSAndroid Build Coastguard Worker  * types; the return value is the number of supported time stamp types.
508*8b26181fSAndroid Build Coastguard Worker  * The list should be freed by a call to pcap_free_tstamp_types() when
509*8b26181fSAndroid Build Coastguard Worker  * you're done with it.
510*8b26181fSAndroid Build Coastguard Worker  *
511*8b26181fSAndroid Build Coastguard Worker  * A return value of 0 means "you don't get a choice of time stamp type",
512*8b26181fSAndroid Build Coastguard Worker  * in which case *tstamp_typesp is set to null.
513*8b26181fSAndroid Build Coastguard Worker  *
514*8b26181fSAndroid Build Coastguard Worker  * PCAP_ERROR is returned on error.
515*8b26181fSAndroid Build Coastguard Worker  */
516*8b26181fSAndroid Build Coastguard Worker int
pcap_list_tstamp_types(pcap_t * p,int ** tstamp_typesp)517*8b26181fSAndroid Build Coastguard Worker pcap_list_tstamp_types(pcap_t *p, int **tstamp_typesp)
518*8b26181fSAndroid Build Coastguard Worker {
519*8b26181fSAndroid Build Coastguard Worker 	if (p->tstamp_type_count == 0) {
520*8b26181fSAndroid Build Coastguard Worker 		/*
521*8b26181fSAndroid Build Coastguard Worker 		 * We don't support multiple time stamp types.
522*8b26181fSAndroid Build Coastguard Worker 		 * That means the only type we support is PCAP_TSTAMP_HOST;
523*8b26181fSAndroid Build Coastguard Worker 		 * set up a list containing only that type.
524*8b26181fSAndroid Build Coastguard Worker 		 */
525*8b26181fSAndroid Build Coastguard Worker 		*tstamp_typesp = (int*)malloc(sizeof(**tstamp_typesp));
526*8b26181fSAndroid Build Coastguard Worker 		if (*tstamp_typesp == NULL) {
527*8b26181fSAndroid Build Coastguard Worker 			pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
528*8b26181fSAndroid Build Coastguard Worker 			    errno, "malloc");
529*8b26181fSAndroid Build Coastguard Worker 			return (PCAP_ERROR);
530*8b26181fSAndroid Build Coastguard Worker 		}
531*8b26181fSAndroid Build Coastguard Worker 		**tstamp_typesp = PCAP_TSTAMP_HOST;
532*8b26181fSAndroid Build Coastguard Worker 		return (1);
533*8b26181fSAndroid Build Coastguard Worker 	} else {
534*8b26181fSAndroid Build Coastguard Worker 		*tstamp_typesp = (int*)calloc(sizeof(**tstamp_typesp),
535*8b26181fSAndroid Build Coastguard Worker 		    p->tstamp_type_count);
536*8b26181fSAndroid Build Coastguard Worker 		if (*tstamp_typesp == NULL) {
537*8b26181fSAndroid Build Coastguard Worker 			pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
538*8b26181fSAndroid Build Coastguard Worker 			    errno, "malloc");
539*8b26181fSAndroid Build Coastguard Worker 			return (PCAP_ERROR);
540*8b26181fSAndroid Build Coastguard Worker 		}
541*8b26181fSAndroid Build Coastguard Worker 		(void)memcpy(*tstamp_typesp, p->tstamp_type_list,
542*8b26181fSAndroid Build Coastguard Worker 		    sizeof(**tstamp_typesp) * p->tstamp_type_count);
543*8b26181fSAndroid Build Coastguard Worker 		return (p->tstamp_type_count);
544*8b26181fSAndroid Build Coastguard Worker 	}
545*8b26181fSAndroid Build Coastguard Worker }
546*8b26181fSAndroid Build Coastguard Worker 
547*8b26181fSAndroid Build Coastguard Worker /*
548*8b26181fSAndroid Build Coastguard Worker  * In Windows, you might have a library built with one version of the
549*8b26181fSAndroid Build Coastguard Worker  * C runtime library and an application built with another version of
550*8b26181fSAndroid Build Coastguard Worker  * the C runtime library, which means that the library might use one
551*8b26181fSAndroid Build Coastguard Worker  * version of malloc() and free() and the application might use another
552*8b26181fSAndroid Build Coastguard Worker  * version of malloc() and free().  If so, that means something
553*8b26181fSAndroid Build Coastguard Worker  * allocated by the library cannot be freed by the application, so we
554*8b26181fSAndroid Build Coastguard Worker  * need to have a pcap_free_tstamp_types() routine to free up the list
555*8b26181fSAndroid Build Coastguard Worker  * allocated by pcap_list_tstamp_types(), even though it's just a wrapper
556*8b26181fSAndroid Build Coastguard Worker  * around free().
557*8b26181fSAndroid Build Coastguard Worker  */
558*8b26181fSAndroid Build Coastguard Worker void
pcap_free_tstamp_types(int * tstamp_type_list)559*8b26181fSAndroid Build Coastguard Worker pcap_free_tstamp_types(int *tstamp_type_list)
560*8b26181fSAndroid Build Coastguard Worker {
561*8b26181fSAndroid Build Coastguard Worker 	free(tstamp_type_list);
562*8b26181fSAndroid Build Coastguard Worker }
563*8b26181fSAndroid Build Coastguard Worker 
564*8b26181fSAndroid Build Coastguard Worker /*
565*8b26181fSAndroid Build Coastguard Worker  * Default one-shot callback; overridden for capture types where the
566*8b26181fSAndroid Build Coastguard Worker  * packet data cannot be guaranteed to be available after the callback
567*8b26181fSAndroid Build Coastguard Worker  * returns, so that a copy must be made.
568*8b26181fSAndroid Build Coastguard Worker  */
569*8b26181fSAndroid Build Coastguard Worker void
pcap_oneshot(u_char * user,const struct pcap_pkthdr * h,const u_char * pkt)570*8b26181fSAndroid Build Coastguard Worker pcap_oneshot(u_char *user, const struct pcap_pkthdr *h, const u_char *pkt)
571*8b26181fSAndroid Build Coastguard Worker {
572*8b26181fSAndroid Build Coastguard Worker 	struct oneshot_userdata *sp = (struct oneshot_userdata *)user;
573*8b26181fSAndroid Build Coastguard Worker 
574*8b26181fSAndroid Build Coastguard Worker 	*sp->hdr = *h;
575*8b26181fSAndroid Build Coastguard Worker 	*sp->pkt = pkt;
576*8b26181fSAndroid Build Coastguard Worker }
577*8b26181fSAndroid Build Coastguard Worker 
578*8b26181fSAndroid Build Coastguard Worker const u_char *
pcap_next(pcap_t * p,struct pcap_pkthdr * h)579*8b26181fSAndroid Build Coastguard Worker pcap_next(pcap_t *p, struct pcap_pkthdr *h)
580*8b26181fSAndroid Build Coastguard Worker {
581*8b26181fSAndroid Build Coastguard Worker 	struct oneshot_userdata s;
582*8b26181fSAndroid Build Coastguard Worker 	const u_char *pkt;
583*8b26181fSAndroid Build Coastguard Worker 
584*8b26181fSAndroid Build Coastguard Worker 	s.hdr = h;
585*8b26181fSAndroid Build Coastguard Worker 	s.pkt = &pkt;
586*8b26181fSAndroid Build Coastguard Worker 	s.pd = p;
587*8b26181fSAndroid Build Coastguard Worker 	if (pcap_dispatch(p, 1, p->oneshot_callback, (u_char *)&s) <= 0)
588*8b26181fSAndroid Build Coastguard Worker 		return (0);
589*8b26181fSAndroid Build Coastguard Worker 	return (pkt);
590*8b26181fSAndroid Build Coastguard Worker }
591*8b26181fSAndroid Build Coastguard Worker 
592*8b26181fSAndroid Build Coastguard Worker int
pcap_next_ex(pcap_t * p,struct pcap_pkthdr ** pkt_header,const u_char ** pkt_data)593*8b26181fSAndroid Build Coastguard Worker pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header,
594*8b26181fSAndroid Build Coastguard Worker     const u_char **pkt_data)
595*8b26181fSAndroid Build Coastguard Worker {
596*8b26181fSAndroid Build Coastguard Worker 	struct oneshot_userdata s;
597*8b26181fSAndroid Build Coastguard Worker 
598*8b26181fSAndroid Build Coastguard Worker 	s.hdr = &p->pcap_header;
599*8b26181fSAndroid Build Coastguard Worker 	s.pkt = pkt_data;
600*8b26181fSAndroid Build Coastguard Worker 	s.pd = p;
601*8b26181fSAndroid Build Coastguard Worker 
602*8b26181fSAndroid Build Coastguard Worker 	/* Saves a pointer to the packet headers */
603*8b26181fSAndroid Build Coastguard Worker 	*pkt_header= &p->pcap_header;
604*8b26181fSAndroid Build Coastguard Worker 
605*8b26181fSAndroid Build Coastguard Worker 	if (p->rfile != NULL) {
606*8b26181fSAndroid Build Coastguard Worker 		int status;
607*8b26181fSAndroid Build Coastguard Worker 
608*8b26181fSAndroid Build Coastguard Worker 		/* We are on an offline capture */
609*8b26181fSAndroid Build Coastguard Worker 		status = pcap_offline_read(p, 1, p->oneshot_callback,
610*8b26181fSAndroid Build Coastguard Worker 		    (u_char *)&s);
611*8b26181fSAndroid Build Coastguard Worker 
612*8b26181fSAndroid Build Coastguard Worker 		/*
613*8b26181fSAndroid Build Coastguard Worker 		 * Return codes for pcap_offline_read() are:
614*8b26181fSAndroid Build Coastguard Worker 		 *   -  0: EOF
615*8b26181fSAndroid Build Coastguard Worker 		 *   - -1: error
616*8b26181fSAndroid Build Coastguard Worker 		 *   - >0: OK - result is number of packets read, so
617*8b26181fSAndroid Build Coastguard Worker 		 *         it will be 1 in this case, as we've passed
618*8b26181fSAndroid Build Coastguard Worker 		 *         a maximum packet count of 1
619*8b26181fSAndroid Build Coastguard Worker 		 * The first one ('0') conflicts with the return code of
620*8b26181fSAndroid Build Coastguard Worker 		 * 0 from pcap_read() meaning "no packets arrived before
621*8b26181fSAndroid Build Coastguard Worker 		 * the timeout expired", so we map it to -2 so you can
622*8b26181fSAndroid Build Coastguard Worker 		 * distinguish between an EOF from a savefile and a
623*8b26181fSAndroid Build Coastguard Worker 		 * "no packets arrived before the timeout expired, try
624*8b26181fSAndroid Build Coastguard Worker 		 * again" from a live capture.
625*8b26181fSAndroid Build Coastguard Worker 		 */
626*8b26181fSAndroid Build Coastguard Worker 		if (status == 0)
627*8b26181fSAndroid Build Coastguard Worker 			return (-2);
628*8b26181fSAndroid Build Coastguard Worker 		else
629*8b26181fSAndroid Build Coastguard Worker 			return (status);
630*8b26181fSAndroid Build Coastguard Worker 	}
631*8b26181fSAndroid Build Coastguard Worker 
632*8b26181fSAndroid Build Coastguard Worker 	/*
633*8b26181fSAndroid Build Coastguard Worker 	 * Return codes for pcap_read() are:
634*8b26181fSAndroid Build Coastguard Worker 	 *   -  0: timeout
635*8b26181fSAndroid Build Coastguard Worker 	 *   - -1: error
636*8b26181fSAndroid Build Coastguard Worker 	 *   - -2: loop was broken out of with pcap_breakloop()
637*8b26181fSAndroid Build Coastguard Worker 	 *   - >0: OK, result is number of packets captured, so
638*8b26181fSAndroid Build Coastguard Worker 	 *         it will be 1 in this case, as we've passed
639*8b26181fSAndroid Build Coastguard Worker 	 *         a maximum packet count of 1
640*8b26181fSAndroid Build Coastguard Worker 	 * The first one ('0') conflicts with the return code of 0 from
641*8b26181fSAndroid Build Coastguard Worker 	 * pcap_offline_read() meaning "end of file".
642*8b26181fSAndroid Build Coastguard Worker 	*/
643*8b26181fSAndroid Build Coastguard Worker 	return (p->read_op(p, 1, p->oneshot_callback, (u_char *)&s));
644*8b26181fSAndroid Build Coastguard Worker }
645*8b26181fSAndroid Build Coastguard Worker 
646*8b26181fSAndroid Build Coastguard Worker /*
647*8b26181fSAndroid Build Coastguard Worker  * Implementation of a pcap_if_list_t.
648*8b26181fSAndroid Build Coastguard Worker  */
649*8b26181fSAndroid Build Coastguard Worker struct pcap_if_list {
650*8b26181fSAndroid Build Coastguard Worker 	pcap_if_t *beginning;
651*8b26181fSAndroid Build Coastguard Worker };
652*8b26181fSAndroid Build Coastguard Worker 
653*8b26181fSAndroid Build Coastguard Worker static struct capture_source_type {
654*8b26181fSAndroid Build Coastguard Worker 	int (*findalldevs_op)(pcap_if_list_t *, char *);
655*8b26181fSAndroid Build Coastguard Worker 	pcap_t *(*create_op)(const char *, char *, int *);
656*8b26181fSAndroid Build Coastguard Worker } capture_source_types[] = {
657*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_DAG_API
658*8b26181fSAndroid Build Coastguard Worker 	{ dag_findalldevs, dag_create },
659*8b26181fSAndroid Build Coastguard Worker #endif
660*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_SEPTEL_API
661*8b26181fSAndroid Build Coastguard Worker 	{ septel_findalldevs, septel_create },
662*8b26181fSAndroid Build Coastguard Worker #endif
663*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_SNF_API
664*8b26181fSAndroid Build Coastguard Worker 	{ snf_findalldevs, snf_create },
665*8b26181fSAndroid Build Coastguard Worker #endif
666*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_TC_API
667*8b26181fSAndroid Build Coastguard Worker 	{ TcFindAllDevs, TcCreate },
668*8b26181fSAndroid Build Coastguard Worker #endif
669*8b26181fSAndroid Build Coastguard Worker #ifdef PCAP_SUPPORT_BT
670*8b26181fSAndroid Build Coastguard Worker 	{ bt_findalldevs, bt_create },
671*8b26181fSAndroid Build Coastguard Worker #endif
672*8b26181fSAndroid Build Coastguard Worker #ifdef PCAP_SUPPORT_BT_MONITOR
673*8b26181fSAndroid Build Coastguard Worker 	{ bt_monitor_findalldevs, bt_monitor_create },
674*8b26181fSAndroid Build Coastguard Worker #endif
675*8b26181fSAndroid Build Coastguard Worker #ifdef PCAP_SUPPORT_LINUX_USBMON
676*8b26181fSAndroid Build Coastguard Worker 	{ usb_findalldevs, usb_create },
677*8b26181fSAndroid Build Coastguard Worker #endif
678*8b26181fSAndroid Build Coastguard Worker #ifdef PCAP_SUPPORT_NETFILTER
679*8b26181fSAndroid Build Coastguard Worker 	{ netfilter_findalldevs, netfilter_create },
680*8b26181fSAndroid Build Coastguard Worker #endif
681*8b26181fSAndroid Build Coastguard Worker #ifdef PCAP_SUPPORT_NETMAP
682*8b26181fSAndroid Build Coastguard Worker 	{ pcap_netmap_findalldevs, pcap_netmap_create },
683*8b26181fSAndroid Build Coastguard Worker #endif
684*8b26181fSAndroid Build Coastguard Worker #ifdef PCAP_SUPPORT_DBUS
685*8b26181fSAndroid Build Coastguard Worker 	{ dbus_findalldevs, dbus_create },
686*8b26181fSAndroid Build Coastguard Worker #endif
687*8b26181fSAndroid Build Coastguard Worker #ifdef PCAP_SUPPORT_RDMASNIFF
688*8b26181fSAndroid Build Coastguard Worker 	{ rdmasniff_findalldevs, rdmasniff_create },
689*8b26181fSAndroid Build Coastguard Worker #endif
690*8b26181fSAndroid Build Coastguard Worker #ifdef PCAP_SUPPORT_DPDK
691*8b26181fSAndroid Build Coastguard Worker 	{ pcap_dpdk_findalldevs, pcap_dpdk_create },
692*8b26181fSAndroid Build Coastguard Worker #endif
693*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_AIRPCAP_API
694*8b26181fSAndroid Build Coastguard Worker 	{ airpcap_findalldevs, airpcap_create },
695*8b26181fSAndroid Build Coastguard Worker #endif
696*8b26181fSAndroid Build Coastguard Worker 	{ NULL, NULL }
697*8b26181fSAndroid Build Coastguard Worker };
698*8b26181fSAndroid Build Coastguard Worker 
699*8b26181fSAndroid Build Coastguard Worker /*
700*8b26181fSAndroid Build Coastguard Worker  * Get a list of all capture sources that are up and that we can open.
701*8b26181fSAndroid Build Coastguard Worker  * Returns -1 on error, 0 otherwise.
702*8b26181fSAndroid Build Coastguard Worker  * The list, as returned through "alldevsp", may be null if no interfaces
703*8b26181fSAndroid Build Coastguard Worker  * were up and could be opened.
704*8b26181fSAndroid Build Coastguard Worker  */
705*8b26181fSAndroid Build Coastguard Worker int
pcap_findalldevs(pcap_if_t ** alldevsp,char * errbuf)706*8b26181fSAndroid Build Coastguard Worker pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)
707*8b26181fSAndroid Build Coastguard Worker {
708*8b26181fSAndroid Build Coastguard Worker 	size_t i;
709*8b26181fSAndroid Build Coastguard Worker 	pcap_if_list_t devlist;
710*8b26181fSAndroid Build Coastguard Worker 
711*8b26181fSAndroid Build Coastguard Worker 	/*
712*8b26181fSAndroid Build Coastguard Worker 	 * Find all the local network interfaces on which we
713*8b26181fSAndroid Build Coastguard Worker 	 * can capture.
714*8b26181fSAndroid Build Coastguard Worker 	 */
715*8b26181fSAndroid Build Coastguard Worker 	devlist.beginning = NULL;
716*8b26181fSAndroid Build Coastguard Worker 	if (pcap_platform_finddevs(&devlist, errbuf) == -1) {
717*8b26181fSAndroid Build Coastguard Worker 		/*
718*8b26181fSAndroid Build Coastguard Worker 		 * Failed - free all of the entries we were given
719*8b26181fSAndroid Build Coastguard Worker 		 * before we failed.
720*8b26181fSAndroid Build Coastguard Worker 		 */
721*8b26181fSAndroid Build Coastguard Worker 		if (devlist.beginning != NULL)
722*8b26181fSAndroid Build Coastguard Worker 			pcap_freealldevs(devlist.beginning);
723*8b26181fSAndroid Build Coastguard Worker 		*alldevsp = NULL;
724*8b26181fSAndroid Build Coastguard Worker 		return (-1);
725*8b26181fSAndroid Build Coastguard Worker 	}
726*8b26181fSAndroid Build Coastguard Worker 
727*8b26181fSAndroid Build Coastguard Worker 	/*
728*8b26181fSAndroid Build Coastguard Worker 	 * Ask each of the non-local-network-interface capture
729*8b26181fSAndroid Build Coastguard Worker 	 * source types what interfaces they have.
730*8b26181fSAndroid Build Coastguard Worker 	 */
731*8b26181fSAndroid Build Coastguard Worker 	for (i = 0; capture_source_types[i].findalldevs_op != NULL; i++) {
732*8b26181fSAndroid Build Coastguard Worker 		if (capture_source_types[i].findalldevs_op(&devlist, errbuf) == -1) {
733*8b26181fSAndroid Build Coastguard Worker 			/*
734*8b26181fSAndroid Build Coastguard Worker 			 * We had an error; free the list we've been
735*8b26181fSAndroid Build Coastguard Worker 			 * constructing.
736*8b26181fSAndroid Build Coastguard Worker 			 */
737*8b26181fSAndroid Build Coastguard Worker 			if (devlist.beginning != NULL)
738*8b26181fSAndroid Build Coastguard Worker 				pcap_freealldevs(devlist.beginning);
739*8b26181fSAndroid Build Coastguard Worker 			*alldevsp = NULL;
740*8b26181fSAndroid Build Coastguard Worker 			return (-1);
741*8b26181fSAndroid Build Coastguard Worker 		}
742*8b26181fSAndroid Build Coastguard Worker 	}
743*8b26181fSAndroid Build Coastguard Worker 
744*8b26181fSAndroid Build Coastguard Worker 	/*
745*8b26181fSAndroid Build Coastguard Worker 	 * Return the first entry of the list of all devices.
746*8b26181fSAndroid Build Coastguard Worker 	 */
747*8b26181fSAndroid Build Coastguard Worker 	*alldevsp = devlist.beginning;
748*8b26181fSAndroid Build Coastguard Worker 	return (0);
749*8b26181fSAndroid Build Coastguard Worker }
750*8b26181fSAndroid Build Coastguard Worker 
751*8b26181fSAndroid Build Coastguard Worker static struct sockaddr *
dup_sockaddr(struct sockaddr * sa,size_t sa_length)752*8b26181fSAndroid Build Coastguard Worker dup_sockaddr(struct sockaddr *sa, size_t sa_length)
753*8b26181fSAndroid Build Coastguard Worker {
754*8b26181fSAndroid Build Coastguard Worker 	struct sockaddr *newsa;
755*8b26181fSAndroid Build Coastguard Worker 
756*8b26181fSAndroid Build Coastguard Worker 	if ((newsa = malloc(sa_length)) == NULL)
757*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
758*8b26181fSAndroid Build Coastguard Worker 	return (memcpy(newsa, sa, sa_length));
759*8b26181fSAndroid Build Coastguard Worker }
760*8b26181fSAndroid Build Coastguard Worker 
761*8b26181fSAndroid Build Coastguard Worker /*
762*8b26181fSAndroid Build Coastguard Worker  * Construct a "figure of merit" for an interface, for use when sorting
763*8b26181fSAndroid Build Coastguard Worker  * the list of interfaces, in which interfaces that are up are superior
764*8b26181fSAndroid Build Coastguard Worker  * to interfaces that aren't up, interfaces that are up and running are
765*8b26181fSAndroid Build Coastguard Worker  * superior to interfaces that are up but not running, and non-loopback
766*8b26181fSAndroid Build Coastguard Worker  * interfaces that are up and running are superior to loopback interfaces,
767*8b26181fSAndroid Build Coastguard Worker  * and interfaces with the same flags have a figure of merit that's higher
768*8b26181fSAndroid Build Coastguard Worker  * the lower the instance number.
769*8b26181fSAndroid Build Coastguard Worker  *
770*8b26181fSAndroid Build Coastguard Worker  * The goal is to try to put the interfaces most likely to be useful for
771*8b26181fSAndroid Build Coastguard Worker  * capture at the beginning of the list.
772*8b26181fSAndroid Build Coastguard Worker  *
773*8b26181fSAndroid Build Coastguard Worker  * The figure of merit, which is lower the "better" the interface is,
774*8b26181fSAndroid Build Coastguard Worker  * has the uppermost bit set if the interface isn't running, the bit
775*8b26181fSAndroid Build Coastguard Worker  * below that set if the interface isn't up, the bit below that
776*8b26181fSAndroid Build Coastguard Worker  * set if the interface is a loopback interface, and the bit below
777*8b26181fSAndroid Build Coastguard Worker  * that set if it's the "any" interface.
778*8b26181fSAndroid Build Coastguard Worker  *
779*8b26181fSAndroid Build Coastguard Worker  * Note: we don't sort by unit number because 1) not all interfaces have
780*8b26181fSAndroid Build Coastguard Worker  * a unit number (systemd, for example, might assign interface names
781*8b26181fSAndroid Build Coastguard Worker  * based on the interface's MAC address or on the physical location of
782*8b26181fSAndroid Build Coastguard Worker  * the adapter's connector), and 2) if the name does end with a simple
783*8b26181fSAndroid Build Coastguard Worker  * unit number, it's not a global property of the interface, it's only
784*8b26181fSAndroid Build Coastguard Worker  * useful as a sort key for device names with the same prefix, so xyz0
785*8b26181fSAndroid Build Coastguard Worker  * shouldn't necessarily sort before abc2.  This means that interfaces
786*8b26181fSAndroid Build Coastguard Worker  * with the same figure of merit will be sorted by the order in which
787*8b26181fSAndroid Build Coastguard Worker  * the mechanism from which we're getting the interfaces supplies them.
788*8b26181fSAndroid Build Coastguard Worker  */
789*8b26181fSAndroid Build Coastguard Worker static u_int
get_figure_of_merit(pcap_if_t * dev)790*8b26181fSAndroid Build Coastguard Worker get_figure_of_merit(pcap_if_t *dev)
791*8b26181fSAndroid Build Coastguard Worker {
792*8b26181fSAndroid Build Coastguard Worker 	u_int n;
793*8b26181fSAndroid Build Coastguard Worker 
794*8b26181fSAndroid Build Coastguard Worker 	n = 0;
795*8b26181fSAndroid Build Coastguard Worker 	if (!(dev->flags & PCAP_IF_RUNNING))
796*8b26181fSAndroid Build Coastguard Worker 		n |= 0x80000000;
797*8b26181fSAndroid Build Coastguard Worker 	if (!(dev->flags & PCAP_IF_UP))
798*8b26181fSAndroid Build Coastguard Worker 		n |= 0x40000000;
799*8b26181fSAndroid Build Coastguard Worker 
800*8b26181fSAndroid Build Coastguard Worker 	/*
801*8b26181fSAndroid Build Coastguard Worker 	 * Give non-wireless interfaces that aren't disconnected a better
802*8b26181fSAndroid Build Coastguard Worker 	 * figure of merit than interfaces that are disconnected, as
803*8b26181fSAndroid Build Coastguard Worker 	 * "disconnected" should indicate that the interface isn't
804*8b26181fSAndroid Build Coastguard Worker 	 * plugged into a network and thus won't give you any traffic.
805*8b26181fSAndroid Build Coastguard Worker 	 *
806*8b26181fSAndroid Build Coastguard Worker 	 * For wireless interfaces, it means "associated with a network",
807*8b26181fSAndroid Build Coastguard Worker 	 * which we presume not to necessarily prevent capture, as you
808*8b26181fSAndroid Build Coastguard Worker 	 * might run the adapter in some flavor of monitor mode.
809*8b26181fSAndroid Build Coastguard Worker 	 */
810*8b26181fSAndroid Build Coastguard Worker 	if (!(dev->flags & PCAP_IF_WIRELESS) &&
811*8b26181fSAndroid Build Coastguard Worker 	    (dev->flags & PCAP_IF_CONNECTION_STATUS) == PCAP_IF_CONNECTION_STATUS_DISCONNECTED)
812*8b26181fSAndroid Build Coastguard Worker 		n |= 0x20000000;
813*8b26181fSAndroid Build Coastguard Worker 
814*8b26181fSAndroid Build Coastguard Worker 	/*
815*8b26181fSAndroid Build Coastguard Worker 	 * Sort loopback devices after non-loopback devices, *except* for
816*8b26181fSAndroid Build Coastguard Worker 	 * disconnected devices.
817*8b26181fSAndroid Build Coastguard Worker 	 */
818*8b26181fSAndroid Build Coastguard Worker 	if (dev->flags & PCAP_IF_LOOPBACK)
819*8b26181fSAndroid Build Coastguard Worker 		n |= 0x10000000;
820*8b26181fSAndroid Build Coastguard Worker 
821*8b26181fSAndroid Build Coastguard Worker 	/*
822*8b26181fSAndroid Build Coastguard Worker 	 * Sort the "any" device before loopback and disconnected devices,
823*8b26181fSAndroid Build Coastguard Worker 	 * but after all other devices.
824*8b26181fSAndroid Build Coastguard Worker 	 */
825*8b26181fSAndroid Build Coastguard Worker 	if (strcmp(dev->name, "any") == 0)
826*8b26181fSAndroid Build Coastguard Worker 		n |= 0x08000000;
827*8b26181fSAndroid Build Coastguard Worker 
828*8b26181fSAndroid Build Coastguard Worker 	return (n);
829*8b26181fSAndroid Build Coastguard Worker }
830*8b26181fSAndroid Build Coastguard Worker 
831*8b26181fSAndroid Build Coastguard Worker #ifndef _WIN32
832*8b26181fSAndroid Build Coastguard Worker /*
833*8b26181fSAndroid Build Coastguard Worker  * Try to get a description for a given device.
834*8b26181fSAndroid Build Coastguard Worker  * Returns a mallocated description if it could and NULL if it couldn't.
835*8b26181fSAndroid Build Coastguard Worker  *
836*8b26181fSAndroid Build Coastguard Worker  * XXX - on FreeBSDs that support it, should it get the sysctl named
837*8b26181fSAndroid Build Coastguard Worker  * "dev.{adapter family name}.{adapter unit}.%desc" to get a description
838*8b26181fSAndroid Build Coastguard Worker  * of the adapter?  Note that "dev.an.0.%desc" is "Aironet PC4500/PC4800"
839*8b26181fSAndroid Build Coastguard Worker  * with my Cisco 350 card, so the name isn't entirely descriptive.  The
840*8b26181fSAndroid Build Coastguard Worker  * "dev.an.0.%pnpinfo" has a better description, although one might argue
841*8b26181fSAndroid Build Coastguard Worker  * that the problem is really a driver bug - if it can find out that it's
842*8b26181fSAndroid Build Coastguard Worker  * a Cisco 340 or 350, rather than an old Aironet card, it should use
843*8b26181fSAndroid Build Coastguard Worker  * that in the description.
844*8b26181fSAndroid Build Coastguard Worker  *
845*8b26181fSAndroid Build Coastguard Worker  * Do NetBSD, DragonflyBSD, or OpenBSD support this as well?  FreeBSD
846*8b26181fSAndroid Build Coastguard Worker  * and OpenBSD let you get a description, but it's not generated by the OS,
847*8b26181fSAndroid Build Coastguard Worker  * it's set with another ioctl that ifconfig supports; we use that to get
848*8b26181fSAndroid Build Coastguard Worker  * a description in FreeBSD and OpenBSD, but if there is no such
849*8b26181fSAndroid Build Coastguard Worker  * description available, it still might be nice to get some description
850*8b26181fSAndroid Build Coastguard Worker  * string based on the device type or something such as that.
851*8b26181fSAndroid Build Coastguard Worker  *
852*8b26181fSAndroid Build Coastguard Worker  * In macOS, the System Configuration framework can apparently return
853*8b26181fSAndroid Build Coastguard Worker  * names in 10.4 and later.
854*8b26181fSAndroid Build Coastguard Worker  *
855*8b26181fSAndroid Build Coastguard Worker  * It also appears that freedesktop.org's HAL offers an "info.product"
856*8b26181fSAndroid Build Coastguard Worker  * string, but the HAL specification says it "should not be used in any
857*8b26181fSAndroid Build Coastguard Worker  * UI" and "subsystem/capability specific properties" should be used
858*8b26181fSAndroid Build Coastguard Worker  * instead and, in any case, I think HAL is being deprecated in
859*8b26181fSAndroid Build Coastguard Worker  * favor of other stuff such as DeviceKit.  DeviceKit doesn't appear
860*8b26181fSAndroid Build Coastguard Worker  * to have any obvious product information for devices, but maybe
861*8b26181fSAndroid Build Coastguard Worker  * I haven't looked hard enough.
862*8b26181fSAndroid Build Coastguard Worker  *
863*8b26181fSAndroid Build Coastguard Worker  * Using the System Configuration framework, or HAL, or DeviceKit, or
864*8b26181fSAndroid Build Coastguard Worker  * whatever, would require that libpcap applications be linked with
865*8b26181fSAndroid Build Coastguard Worker  * the frameworks/libraries in question.  That shouldn't be a problem
866*8b26181fSAndroid Build Coastguard Worker  * for programs linking with the shared version of libpcap (unless
867*8b26181fSAndroid Build Coastguard Worker  * you're running on AIX - which I think is the only UN*X that doesn't
868*8b26181fSAndroid Build Coastguard Worker  * support linking a shared library with other libraries on which it
869*8b26181fSAndroid Build Coastguard Worker  * depends, and having an executable linked only with the first shared
870*8b26181fSAndroid Build Coastguard Worker  * library automatically pick up the other libraries when started -
871*8b26181fSAndroid Build Coastguard Worker  * and using HAL or whatever).  Programs linked with the static
872*8b26181fSAndroid Build Coastguard Worker  * version of libpcap would have to use pcap-config with the --static
873*8b26181fSAndroid Build Coastguard Worker  * flag in order to get the right linker flags in order to pick up
874*8b26181fSAndroid Build Coastguard Worker  * the additional libraries/frameworks; those programs need that anyway
875*8b26181fSAndroid Build Coastguard Worker  * for libpcap 1.1 and beyond on Linux, as, by default, it requires
876*8b26181fSAndroid Build Coastguard Worker  * -lnl.
877*8b26181fSAndroid Build Coastguard Worker  *
878*8b26181fSAndroid Build Coastguard Worker  * Do any other UN*Xes, or desktop environments support getting a
879*8b26181fSAndroid Build Coastguard Worker  * description?
880*8b26181fSAndroid Build Coastguard Worker  */
881*8b26181fSAndroid Build Coastguard Worker static char *
882*8b26181fSAndroid Build Coastguard Worker #ifdef SIOCGIFDESCR
get_if_description(const char * name)883*8b26181fSAndroid Build Coastguard Worker get_if_description(const char *name)
884*8b26181fSAndroid Build Coastguard Worker {
885*8b26181fSAndroid Build Coastguard Worker 	char *description = NULL;
886*8b26181fSAndroid Build Coastguard Worker 	int s;
887*8b26181fSAndroid Build Coastguard Worker 	struct ifreq ifrdesc;
888*8b26181fSAndroid Build Coastguard Worker #ifndef IFDESCRSIZE
889*8b26181fSAndroid Build Coastguard Worker 	size_t descrlen = 64;
890*8b26181fSAndroid Build Coastguard Worker #else
891*8b26181fSAndroid Build Coastguard Worker 	size_t descrlen = IFDESCRSIZE;
892*8b26181fSAndroid Build Coastguard Worker #endif /* IFDESCRSIZE */
893*8b26181fSAndroid Build Coastguard Worker 
894*8b26181fSAndroid Build Coastguard Worker 	/*
895*8b26181fSAndroid Build Coastguard Worker 	 * Get the description for the interface.
896*8b26181fSAndroid Build Coastguard Worker 	 */
897*8b26181fSAndroid Build Coastguard Worker 	memset(&ifrdesc, 0, sizeof ifrdesc);
898*8b26181fSAndroid Build Coastguard Worker 	pcap_strlcpy(ifrdesc.ifr_name, name, sizeof ifrdesc.ifr_name);
899*8b26181fSAndroid Build Coastguard Worker 	s = socket(AF_INET, SOCK_DGRAM, 0);
900*8b26181fSAndroid Build Coastguard Worker 	if (s >= 0) {
901*8b26181fSAndroid Build Coastguard Worker #ifdef __FreeBSD__
902*8b26181fSAndroid Build Coastguard Worker 		/*
903*8b26181fSAndroid Build Coastguard Worker 		 * On FreeBSD, if the buffer isn't big enough for the
904*8b26181fSAndroid Build Coastguard Worker 		 * description, the ioctl succeeds, but the description
905*8b26181fSAndroid Build Coastguard Worker 		 * isn't copied, ifr_buffer.length is set to the description
906*8b26181fSAndroid Build Coastguard Worker 		 * length, and ifr_buffer.buffer is set to NULL.
907*8b26181fSAndroid Build Coastguard Worker 		 */
908*8b26181fSAndroid Build Coastguard Worker 		for (;;) {
909*8b26181fSAndroid Build Coastguard Worker 			free(description);
910*8b26181fSAndroid Build Coastguard Worker 			if ((description = malloc(descrlen)) != NULL) {
911*8b26181fSAndroid Build Coastguard Worker 				ifrdesc.ifr_buffer.buffer = description;
912*8b26181fSAndroid Build Coastguard Worker 				ifrdesc.ifr_buffer.length = descrlen;
913*8b26181fSAndroid Build Coastguard Worker 				if (ioctl(s, SIOCGIFDESCR, &ifrdesc) == 0) {
914*8b26181fSAndroid Build Coastguard Worker 					if (ifrdesc.ifr_buffer.buffer ==
915*8b26181fSAndroid Build Coastguard Worker 					    description)
916*8b26181fSAndroid Build Coastguard Worker 						break;
917*8b26181fSAndroid Build Coastguard Worker 					else
918*8b26181fSAndroid Build Coastguard Worker 						descrlen = ifrdesc.ifr_buffer.length;
919*8b26181fSAndroid Build Coastguard Worker 				} else {
920*8b26181fSAndroid Build Coastguard Worker 					/*
921*8b26181fSAndroid Build Coastguard Worker 					 * Failed to get interface description.
922*8b26181fSAndroid Build Coastguard Worker 					 */
923*8b26181fSAndroid Build Coastguard Worker 					free(description);
924*8b26181fSAndroid Build Coastguard Worker 					description = NULL;
925*8b26181fSAndroid Build Coastguard Worker 					break;
926*8b26181fSAndroid Build Coastguard Worker 				}
927*8b26181fSAndroid Build Coastguard Worker 			} else
928*8b26181fSAndroid Build Coastguard Worker 				break;
929*8b26181fSAndroid Build Coastguard Worker 		}
930*8b26181fSAndroid Build Coastguard Worker #else /* __FreeBSD__ */
931*8b26181fSAndroid Build Coastguard Worker 		/*
932*8b26181fSAndroid Build Coastguard Worker 		 * The only other OS that currently supports
933*8b26181fSAndroid Build Coastguard Worker 		 * SIOCGIFDESCR is OpenBSD, and it has no way
934*8b26181fSAndroid Build Coastguard Worker 		 * to get the description length - it's clamped
935*8b26181fSAndroid Build Coastguard Worker 		 * to a maximum of IFDESCRSIZE.
936*8b26181fSAndroid Build Coastguard Worker 		 */
937*8b26181fSAndroid Build Coastguard Worker 		if ((description = malloc(descrlen)) != NULL) {
938*8b26181fSAndroid Build Coastguard Worker 			ifrdesc.ifr_data = (caddr_t)description;
939*8b26181fSAndroid Build Coastguard Worker 			if (ioctl(s, SIOCGIFDESCR, &ifrdesc) != 0) {
940*8b26181fSAndroid Build Coastguard Worker 				/*
941*8b26181fSAndroid Build Coastguard Worker 				 * Failed to get interface description.
942*8b26181fSAndroid Build Coastguard Worker 				 */
943*8b26181fSAndroid Build Coastguard Worker 				free(description);
944*8b26181fSAndroid Build Coastguard Worker 				description = NULL;
945*8b26181fSAndroid Build Coastguard Worker 			}
946*8b26181fSAndroid Build Coastguard Worker 		}
947*8b26181fSAndroid Build Coastguard Worker #endif /* __FreeBSD__ */
948*8b26181fSAndroid Build Coastguard Worker 		close(s);
949*8b26181fSAndroid Build Coastguard Worker 		if (description != NULL && description[0] == '\0') {
950*8b26181fSAndroid Build Coastguard Worker 			/*
951*8b26181fSAndroid Build Coastguard Worker 			 * Description is empty, so discard it.
952*8b26181fSAndroid Build Coastguard Worker 			 */
953*8b26181fSAndroid Build Coastguard Worker 			free(description);
954*8b26181fSAndroid Build Coastguard Worker 			description = NULL;
955*8b26181fSAndroid Build Coastguard Worker 		}
956*8b26181fSAndroid Build Coastguard Worker 	}
957*8b26181fSAndroid Build Coastguard Worker 
958*8b26181fSAndroid Build Coastguard Worker #ifdef __FreeBSD__
959*8b26181fSAndroid Build Coastguard Worker 	/*
960*8b26181fSAndroid Build Coastguard Worker 	 * For FreeBSD, if we didn't get a description, and this is
961*8b26181fSAndroid Build Coastguard Worker 	 * a device with a name of the form usbusN, label it as a USB
962*8b26181fSAndroid Build Coastguard Worker 	 * bus.
963*8b26181fSAndroid Build Coastguard Worker 	 */
964*8b26181fSAndroid Build Coastguard Worker 	if (description == NULL) {
965*8b26181fSAndroid Build Coastguard Worker 		if (strncmp(name, "usbus", 5) == 0) {
966*8b26181fSAndroid Build Coastguard Worker 			/*
967*8b26181fSAndroid Build Coastguard Worker 			 * OK, it begins with "usbus".
968*8b26181fSAndroid Build Coastguard Worker 			 */
969*8b26181fSAndroid Build Coastguard Worker 			long busnum;
970*8b26181fSAndroid Build Coastguard Worker 			char *p;
971*8b26181fSAndroid Build Coastguard Worker 
972*8b26181fSAndroid Build Coastguard Worker 			errno = 0;
973*8b26181fSAndroid Build Coastguard Worker 			busnum = strtol(name + 5, &p, 10);
974*8b26181fSAndroid Build Coastguard Worker 			if (errno == 0 && p != name + 5 && *p == '\0' &&
975*8b26181fSAndroid Build Coastguard Worker 			    busnum >= 0 && busnum <= INT_MAX) {
976*8b26181fSAndroid Build Coastguard Worker 				/*
977*8b26181fSAndroid Build Coastguard Worker 				 * OK, it's a valid number that's not
978*8b26181fSAndroid Build Coastguard Worker 				 * bigger than INT_MAX.  Construct
979*8b26181fSAndroid Build Coastguard Worker 				 * a description from it.
980*8b26181fSAndroid Build Coastguard Worker 				 * (If that fails, we don't worry about
981*8b26181fSAndroid Build Coastguard Worker 				 * it, we just return NULL.)
982*8b26181fSAndroid Build Coastguard Worker 				 */
983*8b26181fSAndroid Build Coastguard Worker 				if (pcap_asprintf(&description,
984*8b26181fSAndroid Build Coastguard Worker 				    "USB bus number %ld", busnum) == -1) {
985*8b26181fSAndroid Build Coastguard Worker 					/* Failed. */
986*8b26181fSAndroid Build Coastguard Worker 					description = NULL;
987*8b26181fSAndroid Build Coastguard Worker 				}
988*8b26181fSAndroid Build Coastguard Worker 			}
989*8b26181fSAndroid Build Coastguard Worker 		}
990*8b26181fSAndroid Build Coastguard Worker 	}
991*8b26181fSAndroid Build Coastguard Worker #endif
992*8b26181fSAndroid Build Coastguard Worker 	return (description);
993*8b26181fSAndroid Build Coastguard Worker #else /* SIOCGIFDESCR */
994*8b26181fSAndroid Build Coastguard Worker get_if_description(const char *name _U_)
995*8b26181fSAndroid Build Coastguard Worker {
996*8b26181fSAndroid Build Coastguard Worker 	return (NULL);
997*8b26181fSAndroid Build Coastguard Worker #endif /* SIOCGIFDESCR */
998*8b26181fSAndroid Build Coastguard Worker }
999*8b26181fSAndroid Build Coastguard Worker 
1000*8b26181fSAndroid Build Coastguard Worker /*
1001*8b26181fSAndroid Build Coastguard Worker  * Look for a given device in the specified list of devices.
1002*8b26181fSAndroid Build Coastguard Worker  *
1003*8b26181fSAndroid Build Coastguard Worker  * If we find it, return a pointer to its entry.
1004*8b26181fSAndroid Build Coastguard Worker  *
1005*8b26181fSAndroid Build Coastguard Worker  * If we don't find it, attempt to add an entry for it, with the specified
1006*8b26181fSAndroid Build Coastguard Worker  * IFF_ flags and description, and, if that succeeds, return a pointer to
1007*8b26181fSAndroid Build Coastguard Worker  * the new entry, otherwise return NULL and set errbuf to an error message.
1008*8b26181fSAndroid Build Coastguard Worker  */
1009*8b26181fSAndroid Build Coastguard Worker pcap_if_t *
1010*8b26181fSAndroid Build Coastguard Worker find_or_add_if(pcap_if_list_t *devlistp, const char *name,
1011*8b26181fSAndroid Build Coastguard Worker     bpf_u_int32 if_flags, get_if_flags_func get_flags_func, char *errbuf)
1012*8b26181fSAndroid Build Coastguard Worker {
1013*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32 pcap_flags;
1014*8b26181fSAndroid Build Coastguard Worker 
1015*8b26181fSAndroid Build Coastguard Worker 	/*
1016*8b26181fSAndroid Build Coastguard Worker 	 * Convert IFF_ flags to pcap flags.
1017*8b26181fSAndroid Build Coastguard Worker 	 */
1018*8b26181fSAndroid Build Coastguard Worker 	pcap_flags = 0;
1019*8b26181fSAndroid Build Coastguard Worker #ifdef IFF_LOOPBACK
1020*8b26181fSAndroid Build Coastguard Worker 	if (if_flags & IFF_LOOPBACK)
1021*8b26181fSAndroid Build Coastguard Worker 		pcap_flags |= PCAP_IF_LOOPBACK;
1022*8b26181fSAndroid Build Coastguard Worker #else
1023*8b26181fSAndroid Build Coastguard Worker 	/*
1024*8b26181fSAndroid Build Coastguard Worker 	 * We don't have IFF_LOOPBACK, so look at the device name to
1025*8b26181fSAndroid Build Coastguard Worker 	 * see if it looks like a loopback device.
1026*8b26181fSAndroid Build Coastguard Worker 	 */
1027*8b26181fSAndroid Build Coastguard Worker 	if (name[0] == 'l' && name[1] == 'o' &&
1028*8b26181fSAndroid Build Coastguard Worker 	    (PCAP_ISDIGIT(name[2]) || name[2] == '\0'))
1029*8b26181fSAndroid Build Coastguard Worker 		pcap_flags |= PCAP_IF_LOOPBACK;
1030*8b26181fSAndroid Build Coastguard Worker #endif
1031*8b26181fSAndroid Build Coastguard Worker #ifdef IFF_UP
1032*8b26181fSAndroid Build Coastguard Worker 	if (if_flags & IFF_UP)
1033*8b26181fSAndroid Build Coastguard Worker 		pcap_flags |= PCAP_IF_UP;
1034*8b26181fSAndroid Build Coastguard Worker #endif
1035*8b26181fSAndroid Build Coastguard Worker #ifdef IFF_RUNNING
1036*8b26181fSAndroid Build Coastguard Worker 	if (if_flags & IFF_RUNNING)
1037*8b26181fSAndroid Build Coastguard Worker 		pcap_flags |= PCAP_IF_RUNNING;
1038*8b26181fSAndroid Build Coastguard Worker #endif
1039*8b26181fSAndroid Build Coastguard Worker 
1040*8b26181fSAndroid Build Coastguard Worker 	/*
1041*8b26181fSAndroid Build Coastguard Worker 	 * Attempt to find an entry for this device; if we don't find one,
1042*8b26181fSAndroid Build Coastguard Worker 	 * attempt to add one.
1043*8b26181fSAndroid Build Coastguard Worker 	 */
1044*8b26181fSAndroid Build Coastguard Worker 	return (find_or_add_dev(devlistp, name, pcap_flags,
1045*8b26181fSAndroid Build Coastguard Worker 	    get_flags_func, get_if_description(name), errbuf));
1046*8b26181fSAndroid Build Coastguard Worker }
1047*8b26181fSAndroid Build Coastguard Worker 
1048*8b26181fSAndroid Build Coastguard Worker /*
1049*8b26181fSAndroid Build Coastguard Worker  * Look for a given device in the specified list of devices.
1050*8b26181fSAndroid Build Coastguard Worker  *
1051*8b26181fSAndroid Build Coastguard Worker  * If we find it, then, if the specified address isn't null, add it to
1052*8b26181fSAndroid Build Coastguard Worker  * the list of addresses for the device and return 0.
1053*8b26181fSAndroid Build Coastguard Worker  *
1054*8b26181fSAndroid Build Coastguard Worker  * If we don't find it, attempt to add an entry for it, with the specified
1055*8b26181fSAndroid Build Coastguard Worker  * IFF_ flags and description, and, if that succeeds, add the specified
1056*8b26181fSAndroid Build Coastguard Worker  * address to its list of addresses if that address is non-null, and
1057*8b26181fSAndroid Build Coastguard Worker  * return 0, otherwise return -1 and set errbuf to an error message.
1058*8b26181fSAndroid Build Coastguard Worker  *
1059*8b26181fSAndroid Build Coastguard Worker  * (We can get called with a null address because we might get a list
1060*8b26181fSAndroid Build Coastguard Worker  * of interface name/address combinations from the underlying OS, with
1061*8b26181fSAndroid Build Coastguard Worker  * the address being absent in some cases, rather than a list of
1062*8b26181fSAndroid Build Coastguard Worker  * interfaces with each interface having a list of addresses, so this
1063*8b26181fSAndroid Build Coastguard Worker  * call may be the only call made to add to the list, and we want to
1064*8b26181fSAndroid Build Coastguard Worker  * add interfaces even if they have no addresses.)
1065*8b26181fSAndroid Build Coastguard Worker  */
1066*8b26181fSAndroid Build Coastguard Worker int
1067*8b26181fSAndroid Build Coastguard Worker add_addr_to_if(pcap_if_list_t *devlistp, const char *name,
1068*8b26181fSAndroid Build Coastguard Worker     bpf_u_int32 if_flags, get_if_flags_func get_flags_func,
1069*8b26181fSAndroid Build Coastguard Worker     struct sockaddr *addr, size_t addr_size,
1070*8b26181fSAndroid Build Coastguard Worker     struct sockaddr *netmask, size_t netmask_size,
1071*8b26181fSAndroid Build Coastguard Worker     struct sockaddr *broadaddr, size_t broadaddr_size,
1072*8b26181fSAndroid Build Coastguard Worker     struct sockaddr *dstaddr, size_t dstaddr_size,
1073*8b26181fSAndroid Build Coastguard Worker     char *errbuf)
1074*8b26181fSAndroid Build Coastguard Worker {
1075*8b26181fSAndroid Build Coastguard Worker 	pcap_if_t *curdev;
1076*8b26181fSAndroid Build Coastguard Worker 
1077*8b26181fSAndroid Build Coastguard Worker 	/*
1078*8b26181fSAndroid Build Coastguard Worker 	 * Check whether the device exists and, if not, add it.
1079*8b26181fSAndroid Build Coastguard Worker 	 */
1080*8b26181fSAndroid Build Coastguard Worker 	curdev = find_or_add_if(devlistp, name, if_flags, get_flags_func,
1081*8b26181fSAndroid Build Coastguard Worker 	    errbuf);
1082*8b26181fSAndroid Build Coastguard Worker 	if (curdev == NULL) {
1083*8b26181fSAndroid Build Coastguard Worker 		/*
1084*8b26181fSAndroid Build Coastguard Worker 		 * Error - give up.
1085*8b26181fSAndroid Build Coastguard Worker 		 */
1086*8b26181fSAndroid Build Coastguard Worker 		return (-1);
1087*8b26181fSAndroid Build Coastguard Worker 	}
1088*8b26181fSAndroid Build Coastguard Worker 
1089*8b26181fSAndroid Build Coastguard Worker 	if (addr == NULL) {
1090*8b26181fSAndroid Build Coastguard Worker 		/*
1091*8b26181fSAndroid Build Coastguard Worker 		 * There's no address to add; this entry just meant
1092*8b26181fSAndroid Build Coastguard Worker 		 * "here's a new interface".
1093*8b26181fSAndroid Build Coastguard Worker 		 */
1094*8b26181fSAndroid Build Coastguard Worker 		return (0);
1095*8b26181fSAndroid Build Coastguard Worker 	}
1096*8b26181fSAndroid Build Coastguard Worker 
1097*8b26181fSAndroid Build Coastguard Worker 	/*
1098*8b26181fSAndroid Build Coastguard Worker 	 * "curdev" is an entry for this interface, and we have an
1099*8b26181fSAndroid Build Coastguard Worker 	 * address for it; add an entry for that address to the
1100*8b26181fSAndroid Build Coastguard Worker 	 * interface's list of addresses.
1101*8b26181fSAndroid Build Coastguard Worker 	 */
1102*8b26181fSAndroid Build Coastguard Worker 	return (add_addr_to_dev(curdev, addr, addr_size, netmask,
1103*8b26181fSAndroid Build Coastguard Worker 	    netmask_size, broadaddr, broadaddr_size, dstaddr,
1104*8b26181fSAndroid Build Coastguard Worker 	    dstaddr_size, errbuf));
1105*8b26181fSAndroid Build Coastguard Worker }
1106*8b26181fSAndroid Build Coastguard Worker #endif /* _WIN32 */
1107*8b26181fSAndroid Build Coastguard Worker 
1108*8b26181fSAndroid Build Coastguard Worker /*
1109*8b26181fSAndroid Build Coastguard Worker  * Add an entry to the list of addresses for an interface.
1110*8b26181fSAndroid Build Coastguard Worker  * "curdev" is the entry for that interface.
1111*8b26181fSAndroid Build Coastguard Worker  */
1112*8b26181fSAndroid Build Coastguard Worker int
1113*8b26181fSAndroid Build Coastguard Worker add_addr_to_dev(pcap_if_t *curdev,
1114*8b26181fSAndroid Build Coastguard Worker     struct sockaddr *addr, size_t addr_size,
1115*8b26181fSAndroid Build Coastguard Worker     struct sockaddr *netmask, size_t netmask_size,
1116*8b26181fSAndroid Build Coastguard Worker     struct sockaddr *broadaddr, size_t broadaddr_size,
1117*8b26181fSAndroid Build Coastguard Worker     struct sockaddr *dstaddr, size_t dstaddr_size,
1118*8b26181fSAndroid Build Coastguard Worker     char *errbuf)
1119*8b26181fSAndroid Build Coastguard Worker {
1120*8b26181fSAndroid Build Coastguard Worker 	pcap_addr_t *curaddr, *prevaddr, *nextaddr;
1121*8b26181fSAndroid Build Coastguard Worker 
1122*8b26181fSAndroid Build Coastguard Worker 	/*
1123*8b26181fSAndroid Build Coastguard Worker 	 * Allocate the new entry and fill it in.
1124*8b26181fSAndroid Build Coastguard Worker 	 */
1125*8b26181fSAndroid Build Coastguard Worker 	curaddr = (pcap_addr_t *)malloc(sizeof(pcap_addr_t));
1126*8b26181fSAndroid Build Coastguard Worker 	if (curaddr == NULL) {
1127*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1128*8b26181fSAndroid Build Coastguard Worker 		    errno, "malloc");
1129*8b26181fSAndroid Build Coastguard Worker 		return (-1);
1130*8b26181fSAndroid Build Coastguard Worker 	}
1131*8b26181fSAndroid Build Coastguard Worker 
1132*8b26181fSAndroid Build Coastguard Worker 	curaddr->next = NULL;
1133*8b26181fSAndroid Build Coastguard Worker 	if (addr != NULL && addr_size != 0) {
1134*8b26181fSAndroid Build Coastguard Worker 		curaddr->addr = (struct sockaddr *)dup_sockaddr(addr, addr_size);
1135*8b26181fSAndroid Build Coastguard Worker 		if (curaddr->addr == NULL) {
1136*8b26181fSAndroid Build Coastguard Worker 			pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1137*8b26181fSAndroid Build Coastguard Worker 			    errno, "malloc");
1138*8b26181fSAndroid Build Coastguard Worker 			free(curaddr);
1139*8b26181fSAndroid Build Coastguard Worker 			return (-1);
1140*8b26181fSAndroid Build Coastguard Worker 		}
1141*8b26181fSAndroid Build Coastguard Worker 	} else
1142*8b26181fSAndroid Build Coastguard Worker 		curaddr->addr = NULL;
1143*8b26181fSAndroid Build Coastguard Worker 
1144*8b26181fSAndroid Build Coastguard Worker 	if (netmask != NULL && netmask_size != 0) {
1145*8b26181fSAndroid Build Coastguard Worker 		curaddr->netmask = (struct sockaddr *)dup_sockaddr(netmask, netmask_size);
1146*8b26181fSAndroid Build Coastguard Worker 		if (curaddr->netmask == NULL) {
1147*8b26181fSAndroid Build Coastguard Worker 			pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1148*8b26181fSAndroid Build Coastguard Worker 			    errno, "malloc");
1149*8b26181fSAndroid Build Coastguard Worker 			if (curaddr->addr != NULL)
1150*8b26181fSAndroid Build Coastguard Worker 				free(curaddr->addr);
1151*8b26181fSAndroid Build Coastguard Worker 			free(curaddr);
1152*8b26181fSAndroid Build Coastguard Worker 			return (-1);
1153*8b26181fSAndroid Build Coastguard Worker 		}
1154*8b26181fSAndroid Build Coastguard Worker 	} else
1155*8b26181fSAndroid Build Coastguard Worker 		curaddr->netmask = NULL;
1156*8b26181fSAndroid Build Coastguard Worker 
1157*8b26181fSAndroid Build Coastguard Worker 	if (broadaddr != NULL && broadaddr_size != 0) {
1158*8b26181fSAndroid Build Coastguard Worker 		curaddr->broadaddr = (struct sockaddr *)dup_sockaddr(broadaddr, broadaddr_size);
1159*8b26181fSAndroid Build Coastguard Worker 		if (curaddr->broadaddr == NULL) {
1160*8b26181fSAndroid Build Coastguard Worker 			pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1161*8b26181fSAndroid Build Coastguard Worker 			    errno, "malloc");
1162*8b26181fSAndroid Build Coastguard Worker 			if (curaddr->netmask != NULL)
1163*8b26181fSAndroid Build Coastguard Worker 				free(curaddr->netmask);
1164*8b26181fSAndroid Build Coastguard Worker 			if (curaddr->addr != NULL)
1165*8b26181fSAndroid Build Coastguard Worker 				free(curaddr->addr);
1166*8b26181fSAndroid Build Coastguard Worker 			free(curaddr);
1167*8b26181fSAndroid Build Coastguard Worker 			return (-1);
1168*8b26181fSAndroid Build Coastguard Worker 		}
1169*8b26181fSAndroid Build Coastguard Worker 	} else
1170*8b26181fSAndroid Build Coastguard Worker 		curaddr->broadaddr = NULL;
1171*8b26181fSAndroid Build Coastguard Worker 
1172*8b26181fSAndroid Build Coastguard Worker 	if (dstaddr != NULL && dstaddr_size != 0) {
1173*8b26181fSAndroid Build Coastguard Worker 		curaddr->dstaddr = (struct sockaddr *)dup_sockaddr(dstaddr, dstaddr_size);
1174*8b26181fSAndroid Build Coastguard Worker 		if (curaddr->dstaddr == NULL) {
1175*8b26181fSAndroid Build Coastguard Worker 			pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1176*8b26181fSAndroid Build Coastguard Worker 			    errno, "malloc");
1177*8b26181fSAndroid Build Coastguard Worker 			if (curaddr->broadaddr != NULL)
1178*8b26181fSAndroid Build Coastguard Worker 				free(curaddr->broadaddr);
1179*8b26181fSAndroid Build Coastguard Worker 			if (curaddr->netmask != NULL)
1180*8b26181fSAndroid Build Coastguard Worker 				free(curaddr->netmask);
1181*8b26181fSAndroid Build Coastguard Worker 			if (curaddr->addr != NULL)
1182*8b26181fSAndroid Build Coastguard Worker 				free(curaddr->addr);
1183*8b26181fSAndroid Build Coastguard Worker 			free(curaddr);
1184*8b26181fSAndroid Build Coastguard Worker 			return (-1);
1185*8b26181fSAndroid Build Coastguard Worker 		}
1186*8b26181fSAndroid Build Coastguard Worker 	} else
1187*8b26181fSAndroid Build Coastguard Worker 		curaddr->dstaddr = NULL;
1188*8b26181fSAndroid Build Coastguard Worker 
1189*8b26181fSAndroid Build Coastguard Worker 	/*
1190*8b26181fSAndroid Build Coastguard Worker 	 * Find the end of the list of addresses.
1191*8b26181fSAndroid Build Coastguard Worker 	 */
1192*8b26181fSAndroid Build Coastguard Worker 	for (prevaddr = curdev->addresses; prevaddr != NULL; prevaddr = nextaddr) {
1193*8b26181fSAndroid Build Coastguard Worker 		nextaddr = prevaddr->next;
1194*8b26181fSAndroid Build Coastguard Worker 		if (nextaddr == NULL) {
1195*8b26181fSAndroid Build Coastguard Worker 			/*
1196*8b26181fSAndroid Build Coastguard Worker 			 * This is the end of the list.
1197*8b26181fSAndroid Build Coastguard Worker 			 */
1198*8b26181fSAndroid Build Coastguard Worker 			break;
1199*8b26181fSAndroid Build Coastguard Worker 		}
1200*8b26181fSAndroid Build Coastguard Worker 	}
1201*8b26181fSAndroid Build Coastguard Worker 
1202*8b26181fSAndroid Build Coastguard Worker 	if (prevaddr == NULL) {
1203*8b26181fSAndroid Build Coastguard Worker 		/*
1204*8b26181fSAndroid Build Coastguard Worker 		 * The list was empty; this is the first member.
1205*8b26181fSAndroid Build Coastguard Worker 		 */
1206*8b26181fSAndroid Build Coastguard Worker 		curdev->addresses = curaddr;
1207*8b26181fSAndroid Build Coastguard Worker 	} else {
1208*8b26181fSAndroid Build Coastguard Worker 		/*
1209*8b26181fSAndroid Build Coastguard Worker 		 * "prevaddr" is the last member of the list; append
1210*8b26181fSAndroid Build Coastguard Worker 		 * this member to it.
1211*8b26181fSAndroid Build Coastguard Worker 		 */
1212*8b26181fSAndroid Build Coastguard Worker 		prevaddr->next = curaddr;
1213*8b26181fSAndroid Build Coastguard Worker 	}
1214*8b26181fSAndroid Build Coastguard Worker 
1215*8b26181fSAndroid Build Coastguard Worker 	return (0);
1216*8b26181fSAndroid Build Coastguard Worker }
1217*8b26181fSAndroid Build Coastguard Worker 
1218*8b26181fSAndroid Build Coastguard Worker /*
1219*8b26181fSAndroid Build Coastguard Worker  * Look for a given device in the specified list of devices.
1220*8b26181fSAndroid Build Coastguard Worker  *
1221*8b26181fSAndroid Build Coastguard Worker  * If we find it, return 0 and set *curdev_ret to point to it.
1222*8b26181fSAndroid Build Coastguard Worker  *
1223*8b26181fSAndroid Build Coastguard Worker  * If we don't find it, attempt to add an entry for it, with the specified
1224*8b26181fSAndroid Build Coastguard Worker  * flags and description, and, if that succeeds, return 0, otherwise
1225*8b26181fSAndroid Build Coastguard Worker  * return -1 and set errbuf to an error message.
1226*8b26181fSAndroid Build Coastguard Worker  */
1227*8b26181fSAndroid Build Coastguard Worker pcap_if_t *
1228*8b26181fSAndroid Build Coastguard Worker find_or_add_dev(pcap_if_list_t *devlistp, const char *name, bpf_u_int32 flags,
1229*8b26181fSAndroid Build Coastguard Worker     get_if_flags_func get_flags_func, const char *description, char *errbuf)
1230*8b26181fSAndroid Build Coastguard Worker {
1231*8b26181fSAndroid Build Coastguard Worker 	pcap_if_t *curdev;
1232*8b26181fSAndroid Build Coastguard Worker 
1233*8b26181fSAndroid Build Coastguard Worker 	/*
1234*8b26181fSAndroid Build Coastguard Worker 	 * Is there already an entry in the list for this device?
1235*8b26181fSAndroid Build Coastguard Worker 	 */
1236*8b26181fSAndroid Build Coastguard Worker 	curdev = find_dev(devlistp, name);
1237*8b26181fSAndroid Build Coastguard Worker 	if (curdev != NULL) {
1238*8b26181fSAndroid Build Coastguard Worker 		/*
1239*8b26181fSAndroid Build Coastguard Worker 		 * Yes, return it.
1240*8b26181fSAndroid Build Coastguard Worker 		 */
1241*8b26181fSAndroid Build Coastguard Worker 		return (curdev);
1242*8b26181fSAndroid Build Coastguard Worker 	}
1243*8b26181fSAndroid Build Coastguard Worker 
1244*8b26181fSAndroid Build Coastguard Worker 	/*
1245*8b26181fSAndroid Build Coastguard Worker 	 * No, we didn't find it.
1246*8b26181fSAndroid Build Coastguard Worker 	 */
1247*8b26181fSAndroid Build Coastguard Worker 
1248*8b26181fSAndroid Build Coastguard Worker 	/*
1249*8b26181fSAndroid Build Coastguard Worker 	 * Try to get additional flags for the device.
1250*8b26181fSAndroid Build Coastguard Worker 	 */
1251*8b26181fSAndroid Build Coastguard Worker 	if ((*get_flags_func)(name, &flags, errbuf) == -1) {
1252*8b26181fSAndroid Build Coastguard Worker 		/*
1253*8b26181fSAndroid Build Coastguard Worker 		 * Failed.
1254*8b26181fSAndroid Build Coastguard Worker 		 */
1255*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
1256*8b26181fSAndroid Build Coastguard Worker 	}
1257*8b26181fSAndroid Build Coastguard Worker 
1258*8b26181fSAndroid Build Coastguard Worker 	/*
1259*8b26181fSAndroid Build Coastguard Worker 	 * Now, try to add it to the list of devices.
1260*8b26181fSAndroid Build Coastguard Worker 	 */
1261*8b26181fSAndroid Build Coastguard Worker 	return (add_dev(devlistp, name, flags, description, errbuf));
1262*8b26181fSAndroid Build Coastguard Worker }
1263*8b26181fSAndroid Build Coastguard Worker 
1264*8b26181fSAndroid Build Coastguard Worker /*
1265*8b26181fSAndroid Build Coastguard Worker  * Look for a given device in the specified list of devices, and return
1266*8b26181fSAndroid Build Coastguard Worker  * the entry for it if we find it or NULL if we don't.
1267*8b26181fSAndroid Build Coastguard Worker  */
1268*8b26181fSAndroid Build Coastguard Worker pcap_if_t *
1269*8b26181fSAndroid Build Coastguard Worker find_dev(pcap_if_list_t *devlistp, const char *name)
1270*8b26181fSAndroid Build Coastguard Worker {
1271*8b26181fSAndroid Build Coastguard Worker 	pcap_if_t *curdev;
1272*8b26181fSAndroid Build Coastguard Worker 
1273*8b26181fSAndroid Build Coastguard Worker 	/*
1274*8b26181fSAndroid Build Coastguard Worker 	 * Is there an entry in the list for this device?
1275*8b26181fSAndroid Build Coastguard Worker 	 */
1276*8b26181fSAndroid Build Coastguard Worker 	for (curdev = devlistp->beginning; curdev != NULL;
1277*8b26181fSAndroid Build Coastguard Worker 	    curdev = curdev->next) {
1278*8b26181fSAndroid Build Coastguard Worker 		if (strcmp(name, curdev->name) == 0) {
1279*8b26181fSAndroid Build Coastguard Worker 			/*
1280*8b26181fSAndroid Build Coastguard Worker 			 * We found it, so, yes, there is.  No need to
1281*8b26181fSAndroid Build Coastguard Worker 			 * add it.  Provide the entry we found to our
1282*8b26181fSAndroid Build Coastguard Worker 			 * caller.
1283*8b26181fSAndroid Build Coastguard Worker 			 */
1284*8b26181fSAndroid Build Coastguard Worker 			return (curdev);
1285*8b26181fSAndroid Build Coastguard Worker 		}
1286*8b26181fSAndroid Build Coastguard Worker 	}
1287*8b26181fSAndroid Build Coastguard Worker 
1288*8b26181fSAndroid Build Coastguard Worker 	/*
1289*8b26181fSAndroid Build Coastguard Worker 	 * No.
1290*8b26181fSAndroid Build Coastguard Worker 	 */
1291*8b26181fSAndroid Build Coastguard Worker 	return (NULL);
1292*8b26181fSAndroid Build Coastguard Worker }
1293*8b26181fSAndroid Build Coastguard Worker 
1294*8b26181fSAndroid Build Coastguard Worker /*
1295*8b26181fSAndroid Build Coastguard Worker  * Attempt to add an entry for a device, with the specified flags
1296*8b26181fSAndroid Build Coastguard Worker  * and description, and, if that succeeds, return 0 and return a pointer
1297*8b26181fSAndroid Build Coastguard Worker  * to the new entry, otherwise return NULL and set errbuf to an error
1298*8b26181fSAndroid Build Coastguard Worker  * message.
1299*8b26181fSAndroid Build Coastguard Worker  *
1300*8b26181fSAndroid Build Coastguard Worker  * If we weren't given a description, try to get one.
1301*8b26181fSAndroid Build Coastguard Worker  */
1302*8b26181fSAndroid Build Coastguard Worker pcap_if_t *
1303*8b26181fSAndroid Build Coastguard Worker add_dev(pcap_if_list_t *devlistp, const char *name, bpf_u_int32 flags,
1304*8b26181fSAndroid Build Coastguard Worker     const char *description, char *errbuf)
1305*8b26181fSAndroid Build Coastguard Worker {
1306*8b26181fSAndroid Build Coastguard Worker 	pcap_if_t *curdev, *prevdev, *nextdev;
1307*8b26181fSAndroid Build Coastguard Worker 	u_int this_figure_of_merit, nextdev_figure_of_merit;
1308*8b26181fSAndroid Build Coastguard Worker 
1309*8b26181fSAndroid Build Coastguard Worker 	curdev = malloc(sizeof(pcap_if_t));
1310*8b26181fSAndroid Build Coastguard Worker 	if (curdev == NULL) {
1311*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1312*8b26181fSAndroid Build Coastguard Worker 		    errno, "malloc");
1313*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
1314*8b26181fSAndroid Build Coastguard Worker 	}
1315*8b26181fSAndroid Build Coastguard Worker 
1316*8b26181fSAndroid Build Coastguard Worker 	/*
1317*8b26181fSAndroid Build Coastguard Worker 	 * Fill in the entry.
1318*8b26181fSAndroid Build Coastguard Worker 	 */
1319*8b26181fSAndroid Build Coastguard Worker 	curdev->next = NULL;
1320*8b26181fSAndroid Build Coastguard Worker 	curdev->name = strdup(name);
1321*8b26181fSAndroid Build Coastguard Worker 	if (curdev->name == NULL) {
1322*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1323*8b26181fSAndroid Build Coastguard Worker 		    errno, "malloc");
1324*8b26181fSAndroid Build Coastguard Worker 		free(curdev);
1325*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
1326*8b26181fSAndroid Build Coastguard Worker 	}
1327*8b26181fSAndroid Build Coastguard Worker 	if (description == NULL) {
1328*8b26181fSAndroid Build Coastguard Worker 		/*
1329*8b26181fSAndroid Build Coastguard Worker 		 * We weren't handed a description for the interface.
1330*8b26181fSAndroid Build Coastguard Worker 		 */
1331*8b26181fSAndroid Build Coastguard Worker 		curdev->description = NULL;
1332*8b26181fSAndroid Build Coastguard Worker 	} else {
1333*8b26181fSAndroid Build Coastguard Worker 		/*
1334*8b26181fSAndroid Build Coastguard Worker 		 * We were handed a description; make a copy.
1335*8b26181fSAndroid Build Coastguard Worker 		 */
1336*8b26181fSAndroid Build Coastguard Worker 		curdev->description = strdup(description);
1337*8b26181fSAndroid Build Coastguard Worker 		if (curdev->description == NULL) {
1338*8b26181fSAndroid Build Coastguard Worker 			pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1339*8b26181fSAndroid Build Coastguard Worker 			    errno, "malloc");
1340*8b26181fSAndroid Build Coastguard Worker 			free(curdev->name);
1341*8b26181fSAndroid Build Coastguard Worker 			free(curdev);
1342*8b26181fSAndroid Build Coastguard Worker 			return (NULL);
1343*8b26181fSAndroid Build Coastguard Worker 		}
1344*8b26181fSAndroid Build Coastguard Worker 	}
1345*8b26181fSAndroid Build Coastguard Worker 	curdev->addresses = NULL;	/* list starts out as empty */
1346*8b26181fSAndroid Build Coastguard Worker 	curdev->flags = flags;
1347*8b26181fSAndroid Build Coastguard Worker 
1348*8b26181fSAndroid Build Coastguard Worker 	/*
1349*8b26181fSAndroid Build Coastguard Worker 	 * Add it to the list, in the appropriate location.
1350*8b26181fSAndroid Build Coastguard Worker 	 * First, get the "figure of merit" for this interface.
1351*8b26181fSAndroid Build Coastguard Worker 	 */
1352*8b26181fSAndroid Build Coastguard Worker 	this_figure_of_merit = get_figure_of_merit(curdev);
1353*8b26181fSAndroid Build Coastguard Worker 
1354*8b26181fSAndroid Build Coastguard Worker 	/*
1355*8b26181fSAndroid Build Coastguard Worker 	 * Now look for the last interface with an figure of merit
1356*8b26181fSAndroid Build Coastguard Worker 	 * less than or equal to the new interface's figure of merit.
1357*8b26181fSAndroid Build Coastguard Worker 	 *
1358*8b26181fSAndroid Build Coastguard Worker 	 * We start with "prevdev" being NULL, meaning we're before
1359*8b26181fSAndroid Build Coastguard Worker 	 * the first element in the list.
1360*8b26181fSAndroid Build Coastguard Worker 	 */
1361*8b26181fSAndroid Build Coastguard Worker 	prevdev = NULL;
1362*8b26181fSAndroid Build Coastguard Worker 	for (;;) {
1363*8b26181fSAndroid Build Coastguard Worker 		/*
1364*8b26181fSAndroid Build Coastguard Worker 		 * Get the interface after this one.
1365*8b26181fSAndroid Build Coastguard Worker 		 */
1366*8b26181fSAndroid Build Coastguard Worker 		if (prevdev == NULL) {
1367*8b26181fSAndroid Build Coastguard Worker 			/*
1368*8b26181fSAndroid Build Coastguard Worker 			 * The next element is the first element.
1369*8b26181fSAndroid Build Coastguard Worker 			 */
1370*8b26181fSAndroid Build Coastguard Worker 			nextdev = devlistp->beginning;
1371*8b26181fSAndroid Build Coastguard Worker 		} else
1372*8b26181fSAndroid Build Coastguard Worker 			nextdev = prevdev->next;
1373*8b26181fSAndroid Build Coastguard Worker 
1374*8b26181fSAndroid Build Coastguard Worker 		/*
1375*8b26181fSAndroid Build Coastguard Worker 		 * Are we at the end of the list?
1376*8b26181fSAndroid Build Coastguard Worker 		 */
1377*8b26181fSAndroid Build Coastguard Worker 		if (nextdev == NULL) {
1378*8b26181fSAndroid Build Coastguard Worker 			/*
1379*8b26181fSAndroid Build Coastguard Worker 			 * Yes - we have to put the new entry after "prevdev".
1380*8b26181fSAndroid Build Coastguard Worker 			 */
1381*8b26181fSAndroid Build Coastguard Worker 			break;
1382*8b26181fSAndroid Build Coastguard Worker 		}
1383*8b26181fSAndroid Build Coastguard Worker 
1384*8b26181fSAndroid Build Coastguard Worker 		/*
1385*8b26181fSAndroid Build Coastguard Worker 		 * Is the new interface's figure of merit less
1386*8b26181fSAndroid Build Coastguard Worker 		 * than the next interface's figure of merit,
1387*8b26181fSAndroid Build Coastguard Worker 		 * meaning that the new interface is better
1388*8b26181fSAndroid Build Coastguard Worker 		 * than the next interface?
1389*8b26181fSAndroid Build Coastguard Worker 		 */
1390*8b26181fSAndroid Build Coastguard Worker 		nextdev_figure_of_merit = get_figure_of_merit(nextdev);
1391*8b26181fSAndroid Build Coastguard Worker 		if (this_figure_of_merit < nextdev_figure_of_merit) {
1392*8b26181fSAndroid Build Coastguard Worker 			/*
1393*8b26181fSAndroid Build Coastguard Worker 			 * Yes - we should put the new entry
1394*8b26181fSAndroid Build Coastguard Worker 			 * before "nextdev", i.e. after "prevdev".
1395*8b26181fSAndroid Build Coastguard Worker 			 */
1396*8b26181fSAndroid Build Coastguard Worker 			break;
1397*8b26181fSAndroid Build Coastguard Worker 		}
1398*8b26181fSAndroid Build Coastguard Worker 
1399*8b26181fSAndroid Build Coastguard Worker 		prevdev = nextdev;
1400*8b26181fSAndroid Build Coastguard Worker 	}
1401*8b26181fSAndroid Build Coastguard Worker 
1402*8b26181fSAndroid Build Coastguard Worker 	/*
1403*8b26181fSAndroid Build Coastguard Worker 	 * Insert before "nextdev".
1404*8b26181fSAndroid Build Coastguard Worker 	 */
1405*8b26181fSAndroid Build Coastguard Worker 	curdev->next = nextdev;
1406*8b26181fSAndroid Build Coastguard Worker 
1407*8b26181fSAndroid Build Coastguard Worker 	/*
1408*8b26181fSAndroid Build Coastguard Worker 	 * Insert after "prevdev" - unless "prevdev" is null,
1409*8b26181fSAndroid Build Coastguard Worker 	 * in which case this is the first interface.
1410*8b26181fSAndroid Build Coastguard Worker 	 */
1411*8b26181fSAndroid Build Coastguard Worker 	if (prevdev == NULL) {
1412*8b26181fSAndroid Build Coastguard Worker 		/*
1413*8b26181fSAndroid Build Coastguard Worker 		 * This is the first interface.  Make it
1414*8b26181fSAndroid Build Coastguard Worker 		 * the first element in the list of devices.
1415*8b26181fSAndroid Build Coastguard Worker 		 */
1416*8b26181fSAndroid Build Coastguard Worker 		devlistp->beginning = curdev;
1417*8b26181fSAndroid Build Coastguard Worker 	} else
1418*8b26181fSAndroid Build Coastguard Worker 		prevdev->next = curdev;
1419*8b26181fSAndroid Build Coastguard Worker 	return (curdev);
1420*8b26181fSAndroid Build Coastguard Worker }
1421*8b26181fSAndroid Build Coastguard Worker 
1422*8b26181fSAndroid Build Coastguard Worker /*
1423*8b26181fSAndroid Build Coastguard Worker  * Free a list of interfaces.
1424*8b26181fSAndroid Build Coastguard Worker  */
1425*8b26181fSAndroid Build Coastguard Worker void
1426*8b26181fSAndroid Build Coastguard Worker pcap_freealldevs(pcap_if_t *alldevs)
1427*8b26181fSAndroid Build Coastguard Worker {
1428*8b26181fSAndroid Build Coastguard Worker 	pcap_if_t *curdev, *nextdev;
1429*8b26181fSAndroid Build Coastguard Worker 	pcap_addr_t *curaddr, *nextaddr;
1430*8b26181fSAndroid Build Coastguard Worker 
1431*8b26181fSAndroid Build Coastguard Worker 	for (curdev = alldevs; curdev != NULL; curdev = nextdev) {
1432*8b26181fSAndroid Build Coastguard Worker 		nextdev = curdev->next;
1433*8b26181fSAndroid Build Coastguard Worker 
1434*8b26181fSAndroid Build Coastguard Worker 		/*
1435*8b26181fSAndroid Build Coastguard Worker 		 * Free all addresses.
1436*8b26181fSAndroid Build Coastguard Worker 		 */
1437*8b26181fSAndroid Build Coastguard Worker 		for (curaddr = curdev->addresses; curaddr != NULL; curaddr = nextaddr) {
1438*8b26181fSAndroid Build Coastguard Worker 			nextaddr = curaddr->next;
1439*8b26181fSAndroid Build Coastguard Worker 			if (curaddr->addr)
1440*8b26181fSAndroid Build Coastguard Worker 				free(curaddr->addr);
1441*8b26181fSAndroid Build Coastguard Worker 			if (curaddr->netmask)
1442*8b26181fSAndroid Build Coastguard Worker 				free(curaddr->netmask);
1443*8b26181fSAndroid Build Coastguard Worker 			if (curaddr->broadaddr)
1444*8b26181fSAndroid Build Coastguard Worker 				free(curaddr->broadaddr);
1445*8b26181fSAndroid Build Coastguard Worker 			if (curaddr->dstaddr)
1446*8b26181fSAndroid Build Coastguard Worker 				free(curaddr->dstaddr);
1447*8b26181fSAndroid Build Coastguard Worker 			free(curaddr);
1448*8b26181fSAndroid Build Coastguard Worker 		}
1449*8b26181fSAndroid Build Coastguard Worker 
1450*8b26181fSAndroid Build Coastguard Worker 		/*
1451*8b26181fSAndroid Build Coastguard Worker 		 * Free the name string.
1452*8b26181fSAndroid Build Coastguard Worker 		 */
1453*8b26181fSAndroid Build Coastguard Worker 		free(curdev->name);
1454*8b26181fSAndroid Build Coastguard Worker 
1455*8b26181fSAndroid Build Coastguard Worker 		/*
1456*8b26181fSAndroid Build Coastguard Worker 		 * Free the description string, if any.
1457*8b26181fSAndroid Build Coastguard Worker 		 */
1458*8b26181fSAndroid Build Coastguard Worker 		if (curdev->description != NULL)
1459*8b26181fSAndroid Build Coastguard Worker 			free(curdev->description);
1460*8b26181fSAndroid Build Coastguard Worker 
1461*8b26181fSAndroid Build Coastguard Worker 		/*
1462*8b26181fSAndroid Build Coastguard Worker 		 * Free the interface.
1463*8b26181fSAndroid Build Coastguard Worker 		 */
1464*8b26181fSAndroid Build Coastguard Worker 		free(curdev);
1465*8b26181fSAndroid Build Coastguard Worker 	}
1466*8b26181fSAndroid Build Coastguard Worker }
1467*8b26181fSAndroid Build Coastguard Worker 
1468*8b26181fSAndroid Build Coastguard Worker /*
1469*8b26181fSAndroid Build Coastguard Worker  * pcap-npf.c has its own pcap_lookupdev(), for compatibility reasons, as
1470*8b26181fSAndroid Build Coastguard Worker  * it actually returns the names of all interfaces, with a NUL separator
1471*8b26181fSAndroid Build Coastguard Worker  * between them; some callers may depend on that.
1472*8b26181fSAndroid Build Coastguard Worker  *
1473*8b26181fSAndroid Build Coastguard Worker  * MS-DOS has its own pcap_lookupdev(), but that might be useful only
1474*8b26181fSAndroid Build Coastguard Worker  * as an optimization.
1475*8b26181fSAndroid Build Coastguard Worker  *
1476*8b26181fSAndroid Build Coastguard Worker  * In all other cases, we just use pcap_findalldevs() to get a list of
1477*8b26181fSAndroid Build Coastguard Worker  * devices, and pick from that list.
1478*8b26181fSAndroid Build Coastguard Worker  */
1479*8b26181fSAndroid Build Coastguard Worker #if !defined(HAVE_PACKET32) && !defined(MSDOS)
1480*8b26181fSAndroid Build Coastguard Worker /*
1481*8b26181fSAndroid Build Coastguard Worker  * Return the name of a network interface attached to the system, or NULL
1482*8b26181fSAndroid Build Coastguard Worker  * if none can be found.  The interface must be configured up; the
1483*8b26181fSAndroid Build Coastguard Worker  * lowest unit number is preferred; loopback is ignored.
1484*8b26181fSAndroid Build Coastguard Worker  */
1485*8b26181fSAndroid Build Coastguard Worker char *
1486*8b26181fSAndroid Build Coastguard Worker pcap_lookupdev(char *errbuf)
1487*8b26181fSAndroid Build Coastguard Worker {
1488*8b26181fSAndroid Build Coastguard Worker 	pcap_if_t *alldevs;
1489*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
1490*8b26181fSAndroid Build Coastguard Worker   /*
1491*8b26181fSAndroid Build Coastguard Worker    * Windows - use the same size as the old WinPcap 3.1 code.
1492*8b26181fSAndroid Build Coastguard Worker    * XXX - this is probably bigger than it needs to be.
1493*8b26181fSAndroid Build Coastguard Worker    */
1494*8b26181fSAndroid Build Coastguard Worker   #define IF_NAMESIZE 8192
1495*8b26181fSAndroid Build Coastguard Worker #else
1496*8b26181fSAndroid Build Coastguard Worker   /*
1497*8b26181fSAndroid Build Coastguard Worker    * UN*X - use the system's interface name size.
1498*8b26181fSAndroid Build Coastguard Worker    * XXX - that might not be large enough for capture devices
1499*8b26181fSAndroid Build Coastguard Worker    * that aren't regular network interfaces.
1500*8b26181fSAndroid Build Coastguard Worker    */
1501*8b26181fSAndroid Build Coastguard Worker   /* for old BSD systems, including bsdi3 */
1502*8b26181fSAndroid Build Coastguard Worker   #ifndef IF_NAMESIZE
1503*8b26181fSAndroid Build Coastguard Worker   #define IF_NAMESIZE IFNAMSIZ
1504*8b26181fSAndroid Build Coastguard Worker   #endif
1505*8b26181fSAndroid Build Coastguard Worker #endif
1506*8b26181fSAndroid Build Coastguard Worker 	static char device[IF_NAMESIZE + 1];
1507*8b26181fSAndroid Build Coastguard Worker 	char *ret;
1508*8b26181fSAndroid Build Coastguard Worker 
1509*8b26181fSAndroid Build Coastguard Worker 	/*
1510*8b26181fSAndroid Build Coastguard Worker 	 * We disable this in "new API" mode, because 1) in WinPcap/Npcap,
1511*8b26181fSAndroid Build Coastguard Worker 	 * it may return UTF-16 strings, for backwards-compatibility
1512*8b26181fSAndroid Build Coastguard Worker 	 * reasons, and we're also disabling the hack to make that work,
1513*8b26181fSAndroid Build Coastguard Worker 	 * for not-going-past-the-end-of-a-string reasons, and 2) we
1514*8b26181fSAndroid Build Coastguard Worker 	 * want its behavior to be consistent.
1515*8b26181fSAndroid Build Coastguard Worker 	 *
1516*8b26181fSAndroid Build Coastguard Worker 	 * In addition, it's not thread-safe, so we've marked it as
1517*8b26181fSAndroid Build Coastguard Worker 	 * deprecated.
1518*8b26181fSAndroid Build Coastguard Worker 	 */
1519*8b26181fSAndroid Build Coastguard Worker 	if (pcap_new_api) {
1520*8b26181fSAndroid Build Coastguard Worker 		snprintf(errbuf, PCAP_ERRBUF_SIZE,
1521*8b26181fSAndroid Build Coastguard Worker 		    "pcap_lookupdev() is deprecated and is not supported in programs calling pcap_init()");
1522*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
1523*8b26181fSAndroid Build Coastguard Worker 	}
1524*8b26181fSAndroid Build Coastguard Worker 
1525*8b26181fSAndroid Build Coastguard Worker 	if (pcap_findalldevs(&alldevs, errbuf) == -1)
1526*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
1527*8b26181fSAndroid Build Coastguard Worker 
1528*8b26181fSAndroid Build Coastguard Worker 	if (alldevs == NULL || (alldevs->flags & PCAP_IF_LOOPBACK)) {
1529*8b26181fSAndroid Build Coastguard Worker 		/*
1530*8b26181fSAndroid Build Coastguard Worker 		 * There are no devices on the list, or the first device
1531*8b26181fSAndroid Build Coastguard Worker 		 * on the list is a loopback device, which means there
1532*8b26181fSAndroid Build Coastguard Worker 		 * are no non-loopback devices on the list.  This means
1533*8b26181fSAndroid Build Coastguard Worker 		 * we can't return any device.
1534*8b26181fSAndroid Build Coastguard Worker 		 *
1535*8b26181fSAndroid Build Coastguard Worker 		 * XXX - why not return a loopback device?  If we can't
1536*8b26181fSAndroid Build Coastguard Worker 		 * capture on it, it won't be on the list, and if it's
1537*8b26181fSAndroid Build Coastguard Worker 		 * on the list, there aren't any non-loopback devices,
1538*8b26181fSAndroid Build Coastguard Worker 		 * so why not just supply it as the default device?
1539*8b26181fSAndroid Build Coastguard Worker 		 */
1540*8b26181fSAndroid Build Coastguard Worker 		(void)pcap_strlcpy(errbuf, "no suitable device found",
1541*8b26181fSAndroid Build Coastguard Worker 		    PCAP_ERRBUF_SIZE);
1542*8b26181fSAndroid Build Coastguard Worker 		ret = NULL;
1543*8b26181fSAndroid Build Coastguard Worker 	} else {
1544*8b26181fSAndroid Build Coastguard Worker 		/*
1545*8b26181fSAndroid Build Coastguard Worker 		 * Return the name of the first device on the list.
1546*8b26181fSAndroid Build Coastguard Worker 		 */
1547*8b26181fSAndroid Build Coastguard Worker 		(void)pcap_strlcpy(device, alldevs->name, sizeof(device));
1548*8b26181fSAndroid Build Coastguard Worker 		ret = device;
1549*8b26181fSAndroid Build Coastguard Worker 	}
1550*8b26181fSAndroid Build Coastguard Worker 
1551*8b26181fSAndroid Build Coastguard Worker 	pcap_freealldevs(alldevs);
1552*8b26181fSAndroid Build Coastguard Worker 	return (ret);
1553*8b26181fSAndroid Build Coastguard Worker }
1554*8b26181fSAndroid Build Coastguard Worker #endif /* !defined(HAVE_PACKET32) && !defined(MSDOS) */
1555*8b26181fSAndroid Build Coastguard Worker 
1556*8b26181fSAndroid Build Coastguard Worker #if !defined(_WIN32) && !defined(MSDOS)
1557*8b26181fSAndroid Build Coastguard Worker /*
1558*8b26181fSAndroid Build Coastguard Worker  * We don't just fetch the entire list of devices, search for the
1559*8b26181fSAndroid Build Coastguard Worker  * particular device, and use its first IPv4 address, as that's too
1560*8b26181fSAndroid Build Coastguard Worker  * much work to get just one device's netmask.
1561*8b26181fSAndroid Build Coastguard Worker  *
1562*8b26181fSAndroid Build Coastguard Worker  * If we had an API to get attributes for a given device, we could
1563*8b26181fSAndroid Build Coastguard Worker  * use that.
1564*8b26181fSAndroid Build Coastguard Worker  */
1565*8b26181fSAndroid Build Coastguard Worker int
1566*8b26181fSAndroid Build Coastguard Worker pcap_lookupnet(const char *device, bpf_u_int32 *netp, bpf_u_int32 *maskp,
1567*8b26181fSAndroid Build Coastguard Worker     char *errbuf)
1568*8b26181fSAndroid Build Coastguard Worker {
1569*8b26181fSAndroid Build Coastguard Worker 	register int fd;
1570*8b26181fSAndroid Build Coastguard Worker 	register struct sockaddr_in *sin4;
1571*8b26181fSAndroid Build Coastguard Worker 	struct ifreq ifr;
1572*8b26181fSAndroid Build Coastguard Worker 
1573*8b26181fSAndroid Build Coastguard Worker 	/*
1574*8b26181fSAndroid Build Coastguard Worker 	 * The pseudo-device "any" listens on all interfaces and therefore
1575*8b26181fSAndroid Build Coastguard Worker 	 * has the network address and -mask "0.0.0.0" therefore catching
1576*8b26181fSAndroid Build Coastguard Worker 	 * all traffic. Using NULL for the interface is the same as "any".
1577*8b26181fSAndroid Build Coastguard Worker 	 */
1578*8b26181fSAndroid Build Coastguard Worker 	if (!device || strcmp(device, "any") == 0
1579*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_DAG_API
1580*8b26181fSAndroid Build Coastguard Worker 	    || strstr(device, "dag") != NULL
1581*8b26181fSAndroid Build Coastguard Worker #endif
1582*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_SEPTEL_API
1583*8b26181fSAndroid Build Coastguard Worker 	    || strstr(device, "septel") != NULL
1584*8b26181fSAndroid Build Coastguard Worker #endif
1585*8b26181fSAndroid Build Coastguard Worker #ifdef PCAP_SUPPORT_BT
1586*8b26181fSAndroid Build Coastguard Worker 	    || strstr(device, "bluetooth") != NULL
1587*8b26181fSAndroid Build Coastguard Worker #endif
1588*8b26181fSAndroid Build Coastguard Worker #ifdef PCAP_SUPPORT_LINUX_USBMON
1589*8b26181fSAndroid Build Coastguard Worker 	    || strstr(device, "usbmon") != NULL
1590*8b26181fSAndroid Build Coastguard Worker #endif
1591*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_SNF_API
1592*8b26181fSAndroid Build Coastguard Worker 	    || strstr(device, "snf") != NULL
1593*8b26181fSAndroid Build Coastguard Worker #endif
1594*8b26181fSAndroid Build Coastguard Worker #ifdef PCAP_SUPPORT_NETMAP
1595*8b26181fSAndroid Build Coastguard Worker 	    || strncmp(device, "netmap:", 7) == 0
1596*8b26181fSAndroid Build Coastguard Worker 	    || strncmp(device, "vale", 4) == 0
1597*8b26181fSAndroid Build Coastguard Worker #endif
1598*8b26181fSAndroid Build Coastguard Worker #ifdef PCAP_SUPPORT_DPDK
1599*8b26181fSAndroid Build Coastguard Worker 	    || strncmp(device, "dpdk:", 5) == 0
1600*8b26181fSAndroid Build Coastguard Worker #endif
1601*8b26181fSAndroid Build Coastguard Worker 	    ) {
1602*8b26181fSAndroid Build Coastguard Worker 		*netp = *maskp = 0;
1603*8b26181fSAndroid Build Coastguard Worker 		return 0;
1604*8b26181fSAndroid Build Coastguard Worker 	}
1605*8b26181fSAndroid Build Coastguard Worker 
1606*8b26181fSAndroid Build Coastguard Worker 	fd = socket(AF_INET, SOCK_DGRAM, 0);
1607*8b26181fSAndroid Build Coastguard Worker 	if (fd < 0) {
1608*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1609*8b26181fSAndroid Build Coastguard Worker 		    errno, "socket");
1610*8b26181fSAndroid Build Coastguard Worker 		return (-1);
1611*8b26181fSAndroid Build Coastguard Worker 	}
1612*8b26181fSAndroid Build Coastguard Worker 	memset(&ifr, 0, sizeof(ifr));
1613*8b26181fSAndroid Build Coastguard Worker #ifdef linux
1614*8b26181fSAndroid Build Coastguard Worker 	/* XXX Work around Linux kernel bug */
1615*8b26181fSAndroid Build Coastguard Worker 	ifr.ifr_addr.sa_family = AF_INET;
1616*8b26181fSAndroid Build Coastguard Worker #endif
1617*8b26181fSAndroid Build Coastguard Worker 	(void)pcap_strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
1618*8b26181fSAndroid Build Coastguard Worker 	if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0) {
1619*8b26181fSAndroid Build Coastguard Worker 		if (errno == EADDRNOTAVAIL) {
1620*8b26181fSAndroid Build Coastguard Worker 			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
1621*8b26181fSAndroid Build Coastguard Worker 			    "%s: no IPv4 address assigned", device);
1622*8b26181fSAndroid Build Coastguard Worker 		} else {
1623*8b26181fSAndroid Build Coastguard Worker 			pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1624*8b26181fSAndroid Build Coastguard Worker 			    errno, "SIOCGIFADDR: %s", device);
1625*8b26181fSAndroid Build Coastguard Worker 		}
1626*8b26181fSAndroid Build Coastguard Worker 		(void)close(fd);
1627*8b26181fSAndroid Build Coastguard Worker 		return (-1);
1628*8b26181fSAndroid Build Coastguard Worker 	}
1629*8b26181fSAndroid Build Coastguard Worker 	sin4 = (struct sockaddr_in *)&ifr.ifr_addr;
1630*8b26181fSAndroid Build Coastguard Worker 	*netp = sin4->sin_addr.s_addr;
1631*8b26181fSAndroid Build Coastguard Worker 	memset(&ifr, 0, sizeof(ifr));
1632*8b26181fSAndroid Build Coastguard Worker #ifdef linux
1633*8b26181fSAndroid Build Coastguard Worker 	/* XXX Work around Linux kernel bug */
1634*8b26181fSAndroid Build Coastguard Worker 	ifr.ifr_addr.sa_family = AF_INET;
1635*8b26181fSAndroid Build Coastguard Worker #endif
1636*8b26181fSAndroid Build Coastguard Worker 	(void)pcap_strlcpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
1637*8b26181fSAndroid Build Coastguard Worker 	if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifr) < 0) {
1638*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1639*8b26181fSAndroid Build Coastguard Worker 		    errno, "SIOCGIFNETMASK: %s", device);
1640*8b26181fSAndroid Build Coastguard Worker 		(void)close(fd);
1641*8b26181fSAndroid Build Coastguard Worker 		return (-1);
1642*8b26181fSAndroid Build Coastguard Worker 	}
1643*8b26181fSAndroid Build Coastguard Worker 	(void)close(fd);
1644*8b26181fSAndroid Build Coastguard Worker 	*maskp = sin4->sin_addr.s_addr;
1645*8b26181fSAndroid Build Coastguard Worker 	if (*maskp == 0) {
1646*8b26181fSAndroid Build Coastguard Worker 		if (IN_CLASSA(*netp))
1647*8b26181fSAndroid Build Coastguard Worker 			*maskp = IN_CLASSA_NET;
1648*8b26181fSAndroid Build Coastguard Worker 		else if (IN_CLASSB(*netp))
1649*8b26181fSAndroid Build Coastguard Worker 			*maskp = IN_CLASSB_NET;
1650*8b26181fSAndroid Build Coastguard Worker 		else if (IN_CLASSC(*netp))
1651*8b26181fSAndroid Build Coastguard Worker 			*maskp = IN_CLASSC_NET;
1652*8b26181fSAndroid Build Coastguard Worker 		else {
1653*8b26181fSAndroid Build Coastguard Worker 			(void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
1654*8b26181fSAndroid Build Coastguard Worker 			    "inet class for 0x%x unknown", *netp);
1655*8b26181fSAndroid Build Coastguard Worker 			return (-1);
1656*8b26181fSAndroid Build Coastguard Worker 		}
1657*8b26181fSAndroid Build Coastguard Worker 	}
1658*8b26181fSAndroid Build Coastguard Worker 	*netp &= *maskp;
1659*8b26181fSAndroid Build Coastguard Worker 	return (0);
1660*8b26181fSAndroid Build Coastguard Worker }
1661*8b26181fSAndroid Build Coastguard Worker #endif /* !defined(_WIN32) && !defined(MSDOS) */
1662*8b26181fSAndroid Build Coastguard Worker 
1663*8b26181fSAndroid Build Coastguard Worker #ifdef ENABLE_REMOTE
1664*8b26181fSAndroid Build Coastguard Worker #include "pcap-rpcap.h"
1665*8b26181fSAndroid Build Coastguard Worker 
1666*8b26181fSAndroid Build Coastguard Worker /*
1667*8b26181fSAndroid Build Coastguard Worker  * Extract a substring from a string.
1668*8b26181fSAndroid Build Coastguard Worker  */
1669*8b26181fSAndroid Build Coastguard Worker static char *
1670*8b26181fSAndroid Build Coastguard Worker get_substring(const char *p, size_t len, char *ebuf)
1671*8b26181fSAndroid Build Coastguard Worker {
1672*8b26181fSAndroid Build Coastguard Worker 	char *token;
1673*8b26181fSAndroid Build Coastguard Worker 
1674*8b26181fSAndroid Build Coastguard Worker 	token = malloc(len + 1);
1675*8b26181fSAndroid Build Coastguard Worker 	if (token == NULL) {
1676*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1677*8b26181fSAndroid Build Coastguard Worker 		    errno, "malloc");
1678*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
1679*8b26181fSAndroid Build Coastguard Worker 	}
1680*8b26181fSAndroid Build Coastguard Worker 	memcpy(token, p, len);
1681*8b26181fSAndroid Build Coastguard Worker 	token[len] = '\0';
1682*8b26181fSAndroid Build Coastguard Worker 	return (token);
1683*8b26181fSAndroid Build Coastguard Worker }
1684*8b26181fSAndroid Build Coastguard Worker 
1685*8b26181fSAndroid Build Coastguard Worker /*
1686*8b26181fSAndroid Build Coastguard Worker  * Parse a capture source that might be a URL.
1687*8b26181fSAndroid Build Coastguard Worker  *
1688*8b26181fSAndroid Build Coastguard Worker  * If the source is not a URL, *schemep, *userinfop, *hostp, and *portp
1689*8b26181fSAndroid Build Coastguard Worker  * are set to NULL, *pathp is set to point to the source, and 0 is
1690*8b26181fSAndroid Build Coastguard Worker  * returned.
1691*8b26181fSAndroid Build Coastguard Worker  *
1692*8b26181fSAndroid Build Coastguard Worker  * If source is a URL, and the URL refers to a local device (a special
1693*8b26181fSAndroid Build Coastguard Worker  * case of rpcap:), *schemep, *userinfop, *hostp, and *portp are set
1694*8b26181fSAndroid Build Coastguard Worker  * to NULL, *pathp is set to point to the device name, and 0 is returned.
1695*8b26181fSAndroid Build Coastguard Worker  *
1696*8b26181fSAndroid Build Coastguard Worker  * If source is a URL, and it's not a special case that refers to a local
1697*8b26181fSAndroid Build Coastguard Worker  * device, and the parse succeeds:
1698*8b26181fSAndroid Build Coastguard Worker  *
1699*8b26181fSAndroid Build Coastguard Worker  *    *schemep is set to point to an allocated string containing the scheme;
1700*8b26181fSAndroid Build Coastguard Worker  *
1701*8b26181fSAndroid Build Coastguard Worker  *    if user information is present in the URL, *userinfop is set to point
1702*8b26181fSAndroid Build Coastguard Worker  *    to an allocated string containing the user information, otherwise
1703*8b26181fSAndroid Build Coastguard Worker  *    it's set to NULL;
1704*8b26181fSAndroid Build Coastguard Worker  *
1705*8b26181fSAndroid Build Coastguard Worker  *    if host information is present in the URL, *hostp is set to point
1706*8b26181fSAndroid Build Coastguard Worker  *    to an allocated string containing the host information, otherwise
1707*8b26181fSAndroid Build Coastguard Worker  *    it's set to NULL;
1708*8b26181fSAndroid Build Coastguard Worker  *
1709*8b26181fSAndroid Build Coastguard Worker  *    if a port number is present in the URL, *portp is set to point
1710*8b26181fSAndroid Build Coastguard Worker  *    to an allocated string containing the port number, otherwise
1711*8b26181fSAndroid Build Coastguard Worker  *    it's set to NULL;
1712*8b26181fSAndroid Build Coastguard Worker  *
1713*8b26181fSAndroid Build Coastguard Worker  *    *pathp is set to point to an allocated string containing the
1714*8b26181fSAndroid Build Coastguard Worker  *    path;
1715*8b26181fSAndroid Build Coastguard Worker  *
1716*8b26181fSAndroid Build Coastguard Worker  * and 0 is returned.
1717*8b26181fSAndroid Build Coastguard Worker  *
1718*8b26181fSAndroid Build Coastguard Worker  * If the parse fails, ebuf is set to an error string, and -1 is returned.
1719*8b26181fSAndroid Build Coastguard Worker  */
1720*8b26181fSAndroid Build Coastguard Worker static int
1721*8b26181fSAndroid Build Coastguard Worker pcap_parse_source(const char *source, char **schemep, char **userinfop,
1722*8b26181fSAndroid Build Coastguard Worker     char **hostp, char **portp, char **pathp, char *ebuf)
1723*8b26181fSAndroid Build Coastguard Worker {
1724*8b26181fSAndroid Build Coastguard Worker 	char *colonp;
1725*8b26181fSAndroid Build Coastguard Worker 	size_t scheme_len;
1726*8b26181fSAndroid Build Coastguard Worker 	char *scheme;
1727*8b26181fSAndroid Build Coastguard Worker 	const char *endp;
1728*8b26181fSAndroid Build Coastguard Worker 	size_t authority_len;
1729*8b26181fSAndroid Build Coastguard Worker 	char *authority;
1730*8b26181fSAndroid Build Coastguard Worker 	char *parsep, *atsignp, *bracketp;
1731*8b26181fSAndroid Build Coastguard Worker 	char *userinfo, *host, *port, *path;
1732*8b26181fSAndroid Build Coastguard Worker 
1733*8b26181fSAndroid Build Coastguard Worker 	/*
1734*8b26181fSAndroid Build Coastguard Worker 	 * Start out returning nothing.
1735*8b26181fSAndroid Build Coastguard Worker 	 */
1736*8b26181fSAndroid Build Coastguard Worker 	*schemep = NULL;
1737*8b26181fSAndroid Build Coastguard Worker 	*userinfop = NULL;
1738*8b26181fSAndroid Build Coastguard Worker 	*hostp = NULL;
1739*8b26181fSAndroid Build Coastguard Worker 	*portp = NULL;
1740*8b26181fSAndroid Build Coastguard Worker 	*pathp = NULL;
1741*8b26181fSAndroid Build Coastguard Worker 
1742*8b26181fSAndroid Build Coastguard Worker 	/*
1743*8b26181fSAndroid Build Coastguard Worker 	 * RFC 3986 says:
1744*8b26181fSAndroid Build Coastguard Worker 	 *
1745*8b26181fSAndroid Build Coastguard Worker 	 *   URI         = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
1746*8b26181fSAndroid Build Coastguard Worker 	 *
1747*8b26181fSAndroid Build Coastguard Worker 	 *   hier-part   = "//" authority path-abempty
1748*8b26181fSAndroid Build Coastguard Worker 	 *               / path-absolute
1749*8b26181fSAndroid Build Coastguard Worker 	 *               / path-rootless
1750*8b26181fSAndroid Build Coastguard Worker 	 *               / path-empty
1751*8b26181fSAndroid Build Coastguard Worker 	 *
1752*8b26181fSAndroid Build Coastguard Worker 	 *   authority   = [ userinfo "@" ] host [ ":" port ]
1753*8b26181fSAndroid Build Coastguard Worker 	 *
1754*8b26181fSAndroid Build Coastguard Worker 	 *   userinfo    = *( unreserved / pct-encoded / sub-delims / ":" )
1755*8b26181fSAndroid Build Coastguard Worker          *
1756*8b26181fSAndroid Build Coastguard Worker          * Step 1: look for the ":" at the end of the scheme.
1757*8b26181fSAndroid Build Coastguard Worker 	 * A colon in the source is *NOT* sufficient to indicate that
1758*8b26181fSAndroid Build Coastguard Worker 	 * this is a URL, as interface names on some platforms might
1759*8b26181fSAndroid Build Coastguard Worker 	 * include colons (e.g., I think some Solaris interfaces
1760*8b26181fSAndroid Build Coastguard Worker 	 * might).
1761*8b26181fSAndroid Build Coastguard Worker 	 */
1762*8b26181fSAndroid Build Coastguard Worker 	colonp = strchr(source, ':');
1763*8b26181fSAndroid Build Coastguard Worker 	if (colonp == NULL) {
1764*8b26181fSAndroid Build Coastguard Worker 		/*
1765*8b26181fSAndroid Build Coastguard Worker 		 * The source is the device to open.
1766*8b26181fSAndroid Build Coastguard Worker 		 * Return a NULL pointer for the scheme, user information,
1767*8b26181fSAndroid Build Coastguard Worker 		 * host, and port, and return the device as the path.
1768*8b26181fSAndroid Build Coastguard Worker 		 */
1769*8b26181fSAndroid Build Coastguard Worker 		*pathp = strdup(source);
1770*8b26181fSAndroid Build Coastguard Worker 		if (*pathp == NULL) {
1771*8b26181fSAndroid Build Coastguard Worker 			pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1772*8b26181fSAndroid Build Coastguard Worker 			    errno, "malloc");
1773*8b26181fSAndroid Build Coastguard Worker 			return (-1);
1774*8b26181fSAndroid Build Coastguard Worker 		}
1775*8b26181fSAndroid Build Coastguard Worker 		return (0);
1776*8b26181fSAndroid Build Coastguard Worker 	}
1777*8b26181fSAndroid Build Coastguard Worker 
1778*8b26181fSAndroid Build Coastguard Worker 	/*
1779*8b26181fSAndroid Build Coastguard Worker 	 * All schemes must have "//" after them, i.e. we only support
1780*8b26181fSAndroid Build Coastguard Worker 	 * hier-part   = "//" authority path-abempty, not
1781*8b26181fSAndroid Build Coastguard Worker 	 * hier-part   = path-absolute
1782*8b26181fSAndroid Build Coastguard Worker 	 * hier-part   = path-rootless
1783*8b26181fSAndroid Build Coastguard Worker 	 * hier-part   = path-empty
1784*8b26181fSAndroid Build Coastguard Worker 	 *
1785*8b26181fSAndroid Build Coastguard Worker 	 * We need that in order to distinguish between a local device
1786*8b26181fSAndroid Build Coastguard Worker 	 * name that happens to contain a colon and a URI.
1787*8b26181fSAndroid Build Coastguard Worker 	 */
1788*8b26181fSAndroid Build Coastguard Worker 	if (strncmp(colonp + 1, "//", 2) != 0) {
1789*8b26181fSAndroid Build Coastguard Worker 		/*
1790*8b26181fSAndroid Build Coastguard Worker 		 * The source is the device to open.
1791*8b26181fSAndroid Build Coastguard Worker 		 * Return a NULL pointer for the scheme, user information,
1792*8b26181fSAndroid Build Coastguard Worker 		 * host, and port, and return the device as the path.
1793*8b26181fSAndroid Build Coastguard Worker 		 */
1794*8b26181fSAndroid Build Coastguard Worker 		*pathp = strdup(source);
1795*8b26181fSAndroid Build Coastguard Worker 		if (*pathp == NULL) {
1796*8b26181fSAndroid Build Coastguard Worker 			pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1797*8b26181fSAndroid Build Coastguard Worker 			    errno, "malloc");
1798*8b26181fSAndroid Build Coastguard Worker 			return (-1);
1799*8b26181fSAndroid Build Coastguard Worker 		}
1800*8b26181fSAndroid Build Coastguard Worker 		return (0);
1801*8b26181fSAndroid Build Coastguard Worker 	}
1802*8b26181fSAndroid Build Coastguard Worker 
1803*8b26181fSAndroid Build Coastguard Worker 	/*
1804*8b26181fSAndroid Build Coastguard Worker 	 * XXX - check whether the purported scheme could be a scheme?
1805*8b26181fSAndroid Build Coastguard Worker 	 */
1806*8b26181fSAndroid Build Coastguard Worker 
1807*8b26181fSAndroid Build Coastguard Worker 	/*
1808*8b26181fSAndroid Build Coastguard Worker 	 * OK, this looks like a URL.
1809*8b26181fSAndroid Build Coastguard Worker 	 * Get the scheme.
1810*8b26181fSAndroid Build Coastguard Worker 	 */
1811*8b26181fSAndroid Build Coastguard Worker 	scheme_len = colonp - source;
1812*8b26181fSAndroid Build Coastguard Worker 	scheme = malloc(scheme_len + 1);
1813*8b26181fSAndroid Build Coastguard Worker 	if (scheme == NULL) {
1814*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1815*8b26181fSAndroid Build Coastguard Worker 		    errno, "malloc");
1816*8b26181fSAndroid Build Coastguard Worker 		return (-1);
1817*8b26181fSAndroid Build Coastguard Worker 	}
1818*8b26181fSAndroid Build Coastguard Worker 	memcpy(scheme, source, scheme_len);
1819*8b26181fSAndroid Build Coastguard Worker 	scheme[scheme_len] = '\0';
1820*8b26181fSAndroid Build Coastguard Worker 
1821*8b26181fSAndroid Build Coastguard Worker 	/*
1822*8b26181fSAndroid Build Coastguard Worker 	 * Treat file: specially - take everything after file:// as
1823*8b26181fSAndroid Build Coastguard Worker 	 * the pathname.
1824*8b26181fSAndroid Build Coastguard Worker 	 */
1825*8b26181fSAndroid Build Coastguard Worker 	if (pcap_strcasecmp(scheme, "file") == 0) {
1826*8b26181fSAndroid Build Coastguard Worker 		*pathp = strdup(colonp + 3);
1827*8b26181fSAndroid Build Coastguard Worker 		if (*pathp == NULL) {
1828*8b26181fSAndroid Build Coastguard Worker 			pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1829*8b26181fSAndroid Build Coastguard Worker 			    errno, "malloc");
1830*8b26181fSAndroid Build Coastguard Worker 			free(scheme);
1831*8b26181fSAndroid Build Coastguard Worker 			return (-1);
1832*8b26181fSAndroid Build Coastguard Worker 		}
1833*8b26181fSAndroid Build Coastguard Worker 		*schemep = scheme;
1834*8b26181fSAndroid Build Coastguard Worker 		return (0);
1835*8b26181fSAndroid Build Coastguard Worker 	}
1836*8b26181fSAndroid Build Coastguard Worker 
1837*8b26181fSAndroid Build Coastguard Worker 	/*
1838*8b26181fSAndroid Build Coastguard Worker 	 * The WinPcap documentation says you can specify a local
1839*8b26181fSAndroid Build Coastguard Worker 	 * interface with "rpcap://{device}"; we special-case
1840*8b26181fSAndroid Build Coastguard Worker 	 * that here.  If the scheme is "rpcap", and there are
1841*8b26181fSAndroid Build Coastguard Worker 	 * no slashes past the "//", we just return the device.
1842*8b26181fSAndroid Build Coastguard Worker 	 *
1843*8b26181fSAndroid Build Coastguard Worker 	 * XXX - %-escaping?
1844*8b26181fSAndroid Build Coastguard Worker 	 */
1845*8b26181fSAndroid Build Coastguard Worker 	if ((pcap_strcasecmp(scheme, "rpcap") == 0 ||
1846*8b26181fSAndroid Build Coastguard Worker 	    pcap_strcasecmp(scheme, "rpcaps") == 0) &&
1847*8b26181fSAndroid Build Coastguard Worker 	    strchr(colonp + 3, '/') == NULL) {
1848*8b26181fSAndroid Build Coastguard Worker 		/*
1849*8b26181fSAndroid Build Coastguard Worker 		 * Local device.
1850*8b26181fSAndroid Build Coastguard Worker 		 *
1851*8b26181fSAndroid Build Coastguard Worker 		 * Return a NULL pointer for the scheme, user information,
1852*8b26181fSAndroid Build Coastguard Worker 		 * host, and port, and return the device as the path.
1853*8b26181fSAndroid Build Coastguard Worker 		 */
1854*8b26181fSAndroid Build Coastguard Worker 		free(scheme);
1855*8b26181fSAndroid Build Coastguard Worker 		*pathp = strdup(colonp + 3);
1856*8b26181fSAndroid Build Coastguard Worker 		if (*pathp == NULL) {
1857*8b26181fSAndroid Build Coastguard Worker 			pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1858*8b26181fSAndroid Build Coastguard Worker 			    errno, "malloc");
1859*8b26181fSAndroid Build Coastguard Worker 			return (-1);
1860*8b26181fSAndroid Build Coastguard Worker 		}
1861*8b26181fSAndroid Build Coastguard Worker 		return (0);
1862*8b26181fSAndroid Build Coastguard Worker 	}
1863*8b26181fSAndroid Build Coastguard Worker 
1864*8b26181fSAndroid Build Coastguard Worker 	/*
1865*8b26181fSAndroid Build Coastguard Worker 	 * OK, now start parsing the authority.
1866*8b26181fSAndroid Build Coastguard Worker 	 * Get token, terminated with / or terminated at the end of
1867*8b26181fSAndroid Build Coastguard Worker 	 * the string.
1868*8b26181fSAndroid Build Coastguard Worker 	 */
1869*8b26181fSAndroid Build Coastguard Worker 	authority_len = strcspn(colonp + 3, "/");
1870*8b26181fSAndroid Build Coastguard Worker 	authority = get_substring(colonp + 3, authority_len, ebuf);
1871*8b26181fSAndroid Build Coastguard Worker 	if (authority == NULL) {
1872*8b26181fSAndroid Build Coastguard Worker 		/*
1873*8b26181fSAndroid Build Coastguard Worker 		 * Error.
1874*8b26181fSAndroid Build Coastguard Worker 		 */
1875*8b26181fSAndroid Build Coastguard Worker 		free(scheme);
1876*8b26181fSAndroid Build Coastguard Worker 		return (-1);
1877*8b26181fSAndroid Build Coastguard Worker 	}
1878*8b26181fSAndroid Build Coastguard Worker 	endp = colonp + 3 + authority_len;
1879*8b26181fSAndroid Build Coastguard Worker 
1880*8b26181fSAndroid Build Coastguard Worker 	/*
1881*8b26181fSAndroid Build Coastguard Worker 	 * Now carve the authority field into its components.
1882*8b26181fSAndroid Build Coastguard Worker 	 */
1883*8b26181fSAndroid Build Coastguard Worker 	parsep = authority;
1884*8b26181fSAndroid Build Coastguard Worker 
1885*8b26181fSAndroid Build Coastguard Worker 	/*
1886*8b26181fSAndroid Build Coastguard Worker 	 * Is there a userinfo field?
1887*8b26181fSAndroid Build Coastguard Worker 	 */
1888*8b26181fSAndroid Build Coastguard Worker 	atsignp = strchr(parsep, '@');
1889*8b26181fSAndroid Build Coastguard Worker 	if (atsignp != NULL) {
1890*8b26181fSAndroid Build Coastguard Worker 		/*
1891*8b26181fSAndroid Build Coastguard Worker 		 * Yes.
1892*8b26181fSAndroid Build Coastguard Worker 		 */
1893*8b26181fSAndroid Build Coastguard Worker 		size_t userinfo_len;
1894*8b26181fSAndroid Build Coastguard Worker 
1895*8b26181fSAndroid Build Coastguard Worker 		userinfo_len = atsignp - parsep;
1896*8b26181fSAndroid Build Coastguard Worker 		userinfo = get_substring(parsep, userinfo_len, ebuf);
1897*8b26181fSAndroid Build Coastguard Worker 		if (userinfo == NULL) {
1898*8b26181fSAndroid Build Coastguard Worker 			/*
1899*8b26181fSAndroid Build Coastguard Worker 			 * Error.
1900*8b26181fSAndroid Build Coastguard Worker 			 */
1901*8b26181fSAndroid Build Coastguard Worker 			free(authority);
1902*8b26181fSAndroid Build Coastguard Worker 			free(scheme);
1903*8b26181fSAndroid Build Coastguard Worker 			return (-1);
1904*8b26181fSAndroid Build Coastguard Worker 		}
1905*8b26181fSAndroid Build Coastguard Worker 		parsep = atsignp + 1;
1906*8b26181fSAndroid Build Coastguard Worker 	} else {
1907*8b26181fSAndroid Build Coastguard Worker 		/*
1908*8b26181fSAndroid Build Coastguard Worker 		 * No.
1909*8b26181fSAndroid Build Coastguard Worker 		 */
1910*8b26181fSAndroid Build Coastguard Worker 		userinfo = NULL;
1911*8b26181fSAndroid Build Coastguard Worker 	}
1912*8b26181fSAndroid Build Coastguard Worker 
1913*8b26181fSAndroid Build Coastguard Worker 	/*
1914*8b26181fSAndroid Build Coastguard Worker 	 * Is there a host field?
1915*8b26181fSAndroid Build Coastguard Worker 	 */
1916*8b26181fSAndroid Build Coastguard Worker 	if (*parsep == '\0') {
1917*8b26181fSAndroid Build Coastguard Worker 		/*
1918*8b26181fSAndroid Build Coastguard Worker 		 * No; there's no host field or port field.
1919*8b26181fSAndroid Build Coastguard Worker 		 */
1920*8b26181fSAndroid Build Coastguard Worker 		host = NULL;
1921*8b26181fSAndroid Build Coastguard Worker 		port = NULL;
1922*8b26181fSAndroid Build Coastguard Worker 	} else {
1923*8b26181fSAndroid Build Coastguard Worker 		/*
1924*8b26181fSAndroid Build Coastguard Worker 		 * Yes.
1925*8b26181fSAndroid Build Coastguard Worker 		 */
1926*8b26181fSAndroid Build Coastguard Worker 		size_t host_len;
1927*8b26181fSAndroid Build Coastguard Worker 
1928*8b26181fSAndroid Build Coastguard Worker 		/*
1929*8b26181fSAndroid Build Coastguard Worker 		 * Is it an IP-literal?
1930*8b26181fSAndroid Build Coastguard Worker 		 */
1931*8b26181fSAndroid Build Coastguard Worker 		if (*parsep == '[') {
1932*8b26181fSAndroid Build Coastguard Worker 			/*
1933*8b26181fSAndroid Build Coastguard Worker 			 * Yes.
1934*8b26181fSAndroid Build Coastguard Worker 			 * Treat verything up to the closing square
1935*8b26181fSAndroid Build Coastguard Worker 			 * bracket as the IP-Literal; we don't worry
1936*8b26181fSAndroid Build Coastguard Worker 			 * about whether it's a valid IPv6address or
1937*8b26181fSAndroid Build Coastguard Worker 			 * IPvFuture (or an IPv4address, for that
1938*8b26181fSAndroid Build Coastguard Worker 			 * matter, just in case we get handed a
1939*8b26181fSAndroid Build Coastguard Worker 			 * URL with an IPv4 IP-Literal, of the sort
1940*8b26181fSAndroid Build Coastguard Worker 			 * that pcap_createsrcstr() used to generate,
1941*8b26181fSAndroid Build Coastguard Worker 			 * and that pcap_parsesrcstr(), in the original
1942*8b26181fSAndroid Build Coastguard Worker 			 * WinPcap code, accepted).
1943*8b26181fSAndroid Build Coastguard Worker 			 */
1944*8b26181fSAndroid Build Coastguard Worker 			bracketp = strchr(parsep, ']');
1945*8b26181fSAndroid Build Coastguard Worker 			if (bracketp == NULL) {
1946*8b26181fSAndroid Build Coastguard Worker 				/*
1947*8b26181fSAndroid Build Coastguard Worker 				 * There's no closing square bracket.
1948*8b26181fSAndroid Build Coastguard Worker 				 */
1949*8b26181fSAndroid Build Coastguard Worker 				snprintf(ebuf, PCAP_ERRBUF_SIZE,
1950*8b26181fSAndroid Build Coastguard Worker 				    "IP-literal in URL doesn't end with ]");
1951*8b26181fSAndroid Build Coastguard Worker 				free(userinfo);
1952*8b26181fSAndroid Build Coastguard Worker 				free(authority);
1953*8b26181fSAndroid Build Coastguard Worker 				free(scheme);
1954*8b26181fSAndroid Build Coastguard Worker 				return (-1);
1955*8b26181fSAndroid Build Coastguard Worker 			}
1956*8b26181fSAndroid Build Coastguard Worker 			if (*(bracketp + 1) != '\0' &&
1957*8b26181fSAndroid Build Coastguard Worker 			    *(bracketp + 1) != ':') {
1958*8b26181fSAndroid Build Coastguard Worker 				/*
1959*8b26181fSAndroid Build Coastguard Worker 				 * There's extra crud after the
1960*8b26181fSAndroid Build Coastguard Worker 				 * closing square bracketn.
1961*8b26181fSAndroid Build Coastguard Worker 				 */
1962*8b26181fSAndroid Build Coastguard Worker 				snprintf(ebuf, PCAP_ERRBUF_SIZE,
1963*8b26181fSAndroid Build Coastguard Worker 				    "Extra text after IP-literal in URL");
1964*8b26181fSAndroid Build Coastguard Worker 				free(userinfo);
1965*8b26181fSAndroid Build Coastguard Worker 				free(authority);
1966*8b26181fSAndroid Build Coastguard Worker 				free(scheme);
1967*8b26181fSAndroid Build Coastguard Worker 				return (-1);
1968*8b26181fSAndroid Build Coastguard Worker 			}
1969*8b26181fSAndroid Build Coastguard Worker 			host_len = (bracketp - 1) - parsep;
1970*8b26181fSAndroid Build Coastguard Worker 			host = get_substring(parsep + 1, host_len, ebuf);
1971*8b26181fSAndroid Build Coastguard Worker 			if (host == NULL) {
1972*8b26181fSAndroid Build Coastguard Worker 				/*
1973*8b26181fSAndroid Build Coastguard Worker 				 * Error.
1974*8b26181fSAndroid Build Coastguard Worker 				 */
1975*8b26181fSAndroid Build Coastguard Worker 				free(userinfo);
1976*8b26181fSAndroid Build Coastguard Worker 				free(authority);
1977*8b26181fSAndroid Build Coastguard Worker 				free(scheme);
1978*8b26181fSAndroid Build Coastguard Worker 				return (-1);
1979*8b26181fSAndroid Build Coastguard Worker 			}
1980*8b26181fSAndroid Build Coastguard Worker 			parsep = bracketp + 1;
1981*8b26181fSAndroid Build Coastguard Worker 		} else {
1982*8b26181fSAndroid Build Coastguard Worker 			/*
1983*8b26181fSAndroid Build Coastguard Worker 			 * No.
1984*8b26181fSAndroid Build Coastguard Worker 			 * Treat everything up to a : or the end of
1985*8b26181fSAndroid Build Coastguard Worker 			 * the string as the host.
1986*8b26181fSAndroid Build Coastguard Worker 			 */
1987*8b26181fSAndroid Build Coastguard Worker 			host_len = strcspn(parsep, ":");
1988*8b26181fSAndroid Build Coastguard Worker 			host = get_substring(parsep, host_len, ebuf);
1989*8b26181fSAndroid Build Coastguard Worker 			if (host == NULL) {
1990*8b26181fSAndroid Build Coastguard Worker 				/*
1991*8b26181fSAndroid Build Coastguard Worker 				 * Error.
1992*8b26181fSAndroid Build Coastguard Worker 				 */
1993*8b26181fSAndroid Build Coastguard Worker 				free(userinfo);
1994*8b26181fSAndroid Build Coastguard Worker 				free(authority);
1995*8b26181fSAndroid Build Coastguard Worker 				free(scheme);
1996*8b26181fSAndroid Build Coastguard Worker 				return (-1);
1997*8b26181fSAndroid Build Coastguard Worker 			}
1998*8b26181fSAndroid Build Coastguard Worker 			parsep = parsep + host_len;
1999*8b26181fSAndroid Build Coastguard Worker 		}
2000*8b26181fSAndroid Build Coastguard Worker 
2001*8b26181fSAndroid Build Coastguard Worker 		/*
2002*8b26181fSAndroid Build Coastguard Worker 		 * Is there a port field?
2003*8b26181fSAndroid Build Coastguard Worker 		 */
2004*8b26181fSAndroid Build Coastguard Worker 		if (*parsep == ':') {
2005*8b26181fSAndroid Build Coastguard Worker 			/*
2006*8b26181fSAndroid Build Coastguard Worker 			 * Yes.  It's the rest of the authority field.
2007*8b26181fSAndroid Build Coastguard Worker 			 */
2008*8b26181fSAndroid Build Coastguard Worker 			size_t port_len;
2009*8b26181fSAndroid Build Coastguard Worker 
2010*8b26181fSAndroid Build Coastguard Worker 			parsep++;
2011*8b26181fSAndroid Build Coastguard Worker 			port_len = strlen(parsep);
2012*8b26181fSAndroid Build Coastguard Worker 			port = get_substring(parsep, port_len, ebuf);
2013*8b26181fSAndroid Build Coastguard Worker 			if (port == NULL) {
2014*8b26181fSAndroid Build Coastguard Worker 				/*
2015*8b26181fSAndroid Build Coastguard Worker 				 * Error.
2016*8b26181fSAndroid Build Coastguard Worker 				 */
2017*8b26181fSAndroid Build Coastguard Worker 				free(host);
2018*8b26181fSAndroid Build Coastguard Worker 				free(userinfo);
2019*8b26181fSAndroid Build Coastguard Worker 				free(authority);
2020*8b26181fSAndroid Build Coastguard Worker 				free(scheme);
2021*8b26181fSAndroid Build Coastguard Worker 				return (-1);
2022*8b26181fSAndroid Build Coastguard Worker 			}
2023*8b26181fSAndroid Build Coastguard Worker 		} else {
2024*8b26181fSAndroid Build Coastguard Worker 			/*
2025*8b26181fSAndroid Build Coastguard Worker 			 * No.
2026*8b26181fSAndroid Build Coastguard Worker 			 */
2027*8b26181fSAndroid Build Coastguard Worker 			port = NULL;
2028*8b26181fSAndroid Build Coastguard Worker 		}
2029*8b26181fSAndroid Build Coastguard Worker 	}
2030*8b26181fSAndroid Build Coastguard Worker 	free(authority);
2031*8b26181fSAndroid Build Coastguard Worker 
2032*8b26181fSAndroid Build Coastguard Worker 	/*
2033*8b26181fSAndroid Build Coastguard Worker 	 * Everything else is the path.  Strip off the leading /.
2034*8b26181fSAndroid Build Coastguard Worker 	 */
2035*8b26181fSAndroid Build Coastguard Worker 	if (*endp == '\0')
2036*8b26181fSAndroid Build Coastguard Worker 		path = strdup("");
2037*8b26181fSAndroid Build Coastguard Worker 	else
2038*8b26181fSAndroid Build Coastguard Worker 		path = strdup(endp + 1);
2039*8b26181fSAndroid Build Coastguard Worker 	if (path == NULL) {
2040*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
2041*8b26181fSAndroid Build Coastguard Worker 		    errno, "malloc");
2042*8b26181fSAndroid Build Coastguard Worker 		free(port);
2043*8b26181fSAndroid Build Coastguard Worker 		free(host);
2044*8b26181fSAndroid Build Coastguard Worker 		free(userinfo);
2045*8b26181fSAndroid Build Coastguard Worker 		free(scheme);
2046*8b26181fSAndroid Build Coastguard Worker 		return (-1);
2047*8b26181fSAndroid Build Coastguard Worker 	}
2048*8b26181fSAndroid Build Coastguard Worker 	*schemep = scheme;
2049*8b26181fSAndroid Build Coastguard Worker 	*userinfop = userinfo;
2050*8b26181fSAndroid Build Coastguard Worker 	*hostp = host;
2051*8b26181fSAndroid Build Coastguard Worker 	*portp = port;
2052*8b26181fSAndroid Build Coastguard Worker 	*pathp = path;
2053*8b26181fSAndroid Build Coastguard Worker 	return (0);
2054*8b26181fSAndroid Build Coastguard Worker }
2055*8b26181fSAndroid Build Coastguard Worker 
2056*8b26181fSAndroid Build Coastguard Worker int
2057*8b26181fSAndroid Build Coastguard Worker pcap_createsrcstr_ex(char *source, int type, const char *host, const char *port,
2058*8b26181fSAndroid Build Coastguard Worker     const char *name, unsigned char uses_ssl, char *errbuf)
2059*8b26181fSAndroid Build Coastguard Worker {
2060*8b26181fSAndroid Build Coastguard Worker 	switch (type) {
2061*8b26181fSAndroid Build Coastguard Worker 
2062*8b26181fSAndroid Build Coastguard Worker 	case PCAP_SRC_FILE:
2063*8b26181fSAndroid Build Coastguard Worker 		pcap_strlcpy(source, PCAP_SRC_FILE_STRING, PCAP_BUF_SIZE);
2064*8b26181fSAndroid Build Coastguard Worker 		if (name != NULL && *name != '\0') {
2065*8b26181fSAndroid Build Coastguard Worker 			pcap_strlcat(source, name, PCAP_BUF_SIZE);
2066*8b26181fSAndroid Build Coastguard Worker 			return (0);
2067*8b26181fSAndroid Build Coastguard Worker 		} else {
2068*8b26181fSAndroid Build Coastguard Worker 			snprintf(errbuf, PCAP_ERRBUF_SIZE,
2069*8b26181fSAndroid Build Coastguard Worker 			    "The file name cannot be NULL.");
2070*8b26181fSAndroid Build Coastguard Worker 			return (-1);
2071*8b26181fSAndroid Build Coastguard Worker 		}
2072*8b26181fSAndroid Build Coastguard Worker 
2073*8b26181fSAndroid Build Coastguard Worker 	case PCAP_SRC_IFREMOTE:
2074*8b26181fSAndroid Build Coastguard Worker 		pcap_strlcpy(source,
2075*8b26181fSAndroid Build Coastguard Worker 		    (uses_ssl ? "rpcaps://" : PCAP_SRC_IF_STRING),
2076*8b26181fSAndroid Build Coastguard Worker 		    PCAP_BUF_SIZE);
2077*8b26181fSAndroid Build Coastguard Worker 		if (host != NULL && *host != '\0') {
2078*8b26181fSAndroid Build Coastguard Worker 			if (strchr(host, ':') != NULL) {
2079*8b26181fSAndroid Build Coastguard Worker 				/*
2080*8b26181fSAndroid Build Coastguard Worker 				 * The host name contains a colon, so it's
2081*8b26181fSAndroid Build Coastguard Worker 				 * probably an IPv6 address, and needs to
2082*8b26181fSAndroid Build Coastguard Worker 				 * be included in square brackets.
2083*8b26181fSAndroid Build Coastguard Worker 				 */
2084*8b26181fSAndroid Build Coastguard Worker 				pcap_strlcat(source, "[", PCAP_BUF_SIZE);
2085*8b26181fSAndroid Build Coastguard Worker 				pcap_strlcat(source, host, PCAP_BUF_SIZE);
2086*8b26181fSAndroid Build Coastguard Worker 				pcap_strlcat(source, "]", PCAP_BUF_SIZE);
2087*8b26181fSAndroid Build Coastguard Worker 			} else
2088*8b26181fSAndroid Build Coastguard Worker 				pcap_strlcat(source, host, PCAP_BUF_SIZE);
2089*8b26181fSAndroid Build Coastguard Worker 
2090*8b26181fSAndroid Build Coastguard Worker 			if (port != NULL && *port != '\0') {
2091*8b26181fSAndroid Build Coastguard Worker 				pcap_strlcat(source, ":", PCAP_BUF_SIZE);
2092*8b26181fSAndroid Build Coastguard Worker 				pcap_strlcat(source, port, PCAP_BUF_SIZE);
2093*8b26181fSAndroid Build Coastguard Worker 			}
2094*8b26181fSAndroid Build Coastguard Worker 
2095*8b26181fSAndroid Build Coastguard Worker 			pcap_strlcat(source, "/", PCAP_BUF_SIZE);
2096*8b26181fSAndroid Build Coastguard Worker 		} else {
2097*8b26181fSAndroid Build Coastguard Worker 			snprintf(errbuf, PCAP_ERRBUF_SIZE,
2098*8b26181fSAndroid Build Coastguard Worker 			    "The host name cannot be NULL.");
2099*8b26181fSAndroid Build Coastguard Worker 			return (-1);
2100*8b26181fSAndroid Build Coastguard Worker 		}
2101*8b26181fSAndroid Build Coastguard Worker 
2102*8b26181fSAndroid Build Coastguard Worker 		if (name != NULL && *name != '\0')
2103*8b26181fSAndroid Build Coastguard Worker 			pcap_strlcat(source, name, PCAP_BUF_SIZE);
2104*8b26181fSAndroid Build Coastguard Worker 
2105*8b26181fSAndroid Build Coastguard Worker 		return (0);
2106*8b26181fSAndroid Build Coastguard Worker 
2107*8b26181fSAndroid Build Coastguard Worker 	case PCAP_SRC_IFLOCAL:
2108*8b26181fSAndroid Build Coastguard Worker 		pcap_strlcpy(source, PCAP_SRC_IF_STRING, PCAP_BUF_SIZE);
2109*8b26181fSAndroid Build Coastguard Worker 
2110*8b26181fSAndroid Build Coastguard Worker 		if (name != NULL && *name != '\0')
2111*8b26181fSAndroid Build Coastguard Worker 			pcap_strlcat(source, name, PCAP_BUF_SIZE);
2112*8b26181fSAndroid Build Coastguard Worker 
2113*8b26181fSAndroid Build Coastguard Worker 		return (0);
2114*8b26181fSAndroid Build Coastguard Worker 
2115*8b26181fSAndroid Build Coastguard Worker 	default:
2116*8b26181fSAndroid Build Coastguard Worker 		snprintf(errbuf, PCAP_ERRBUF_SIZE,
2117*8b26181fSAndroid Build Coastguard Worker 		    "The interface type is not valid.");
2118*8b26181fSAndroid Build Coastguard Worker 		return (-1);
2119*8b26181fSAndroid Build Coastguard Worker 	}
2120*8b26181fSAndroid Build Coastguard Worker }
2121*8b26181fSAndroid Build Coastguard Worker 
2122*8b26181fSAndroid Build Coastguard Worker 
2123*8b26181fSAndroid Build Coastguard Worker int
2124*8b26181fSAndroid Build Coastguard Worker pcap_createsrcstr(char *source, int type, const char *host, const char *port,
2125*8b26181fSAndroid Build Coastguard Worker     const char *name, char *errbuf)
2126*8b26181fSAndroid Build Coastguard Worker {
2127*8b26181fSAndroid Build Coastguard Worker 	return (pcap_createsrcstr_ex(source, type, host, port, name, 0, errbuf));
2128*8b26181fSAndroid Build Coastguard Worker }
2129*8b26181fSAndroid Build Coastguard Worker 
2130*8b26181fSAndroid Build Coastguard Worker int
2131*8b26181fSAndroid Build Coastguard Worker pcap_parsesrcstr_ex(const char *source, int *type, char *host, char *port,
2132*8b26181fSAndroid Build Coastguard Worker     char *name, unsigned char *uses_ssl, char *errbuf)
2133*8b26181fSAndroid Build Coastguard Worker {
2134*8b26181fSAndroid Build Coastguard Worker 	char *scheme, *tmpuserinfo, *tmphost, *tmpport, *tmppath;
2135*8b26181fSAndroid Build Coastguard Worker 
2136*8b26181fSAndroid Build Coastguard Worker 	/* Initialization stuff */
2137*8b26181fSAndroid Build Coastguard Worker 	if (host)
2138*8b26181fSAndroid Build Coastguard Worker 		*host = '\0';
2139*8b26181fSAndroid Build Coastguard Worker 	if (port)
2140*8b26181fSAndroid Build Coastguard Worker 		*port = '\0';
2141*8b26181fSAndroid Build Coastguard Worker 	if (name)
2142*8b26181fSAndroid Build Coastguard Worker 		*name = '\0';
2143*8b26181fSAndroid Build Coastguard Worker 	if (uses_ssl)
2144*8b26181fSAndroid Build Coastguard Worker 		*uses_ssl = 0;
2145*8b26181fSAndroid Build Coastguard Worker 
2146*8b26181fSAndroid Build Coastguard Worker 	/* Parse the source string */
2147*8b26181fSAndroid Build Coastguard Worker 	if (pcap_parse_source(source, &scheme, &tmpuserinfo, &tmphost,
2148*8b26181fSAndroid Build Coastguard Worker 	    &tmpport, &tmppath, errbuf) == -1) {
2149*8b26181fSAndroid Build Coastguard Worker 		/*
2150*8b26181fSAndroid Build Coastguard Worker 		 * Fail.
2151*8b26181fSAndroid Build Coastguard Worker 		 */
2152*8b26181fSAndroid Build Coastguard Worker 		return (-1);
2153*8b26181fSAndroid Build Coastguard Worker 	}
2154*8b26181fSAndroid Build Coastguard Worker 
2155*8b26181fSAndroid Build Coastguard Worker 	if (scheme == NULL) {
2156*8b26181fSAndroid Build Coastguard Worker 		/*
2157*8b26181fSAndroid Build Coastguard Worker 		 * Local device.
2158*8b26181fSAndroid Build Coastguard Worker 		 */
2159*8b26181fSAndroid Build Coastguard Worker 		if (name && tmppath)
2160*8b26181fSAndroid Build Coastguard Worker 			pcap_strlcpy(name, tmppath, PCAP_BUF_SIZE);
2161*8b26181fSAndroid Build Coastguard Worker 		if (type)
2162*8b26181fSAndroid Build Coastguard Worker 			*type = PCAP_SRC_IFLOCAL;
2163*8b26181fSAndroid Build Coastguard Worker 		free(tmppath);
2164*8b26181fSAndroid Build Coastguard Worker 		free(tmpport);
2165*8b26181fSAndroid Build Coastguard Worker 		free(tmphost);
2166*8b26181fSAndroid Build Coastguard Worker 		free(tmpuserinfo);
2167*8b26181fSAndroid Build Coastguard Worker 		return (0);
2168*8b26181fSAndroid Build Coastguard Worker 	}
2169*8b26181fSAndroid Build Coastguard Worker 
2170*8b26181fSAndroid Build Coastguard Worker 	int is_rpcap = 0;
2171*8b26181fSAndroid Build Coastguard Worker 	if (strcmp(scheme, "rpcaps") == 0) {
2172*8b26181fSAndroid Build Coastguard Worker 		is_rpcap = 1;
2173*8b26181fSAndroid Build Coastguard Worker 		if (uses_ssl) *uses_ssl = 1;
2174*8b26181fSAndroid Build Coastguard Worker 	} else if (strcmp(scheme, "rpcap") == 0) {
2175*8b26181fSAndroid Build Coastguard Worker 		is_rpcap = 1;
2176*8b26181fSAndroid Build Coastguard Worker 	}
2177*8b26181fSAndroid Build Coastguard Worker 
2178*8b26181fSAndroid Build Coastguard Worker 	if (is_rpcap) {
2179*8b26181fSAndroid Build Coastguard Worker 		/*
2180*8b26181fSAndroid Build Coastguard Worker 		 * rpcap[s]://
2181*8b26181fSAndroid Build Coastguard Worker 		 *
2182*8b26181fSAndroid Build Coastguard Worker 		 * pcap_parse_source() has already handled the case of
2183*8b26181fSAndroid Build Coastguard Worker 		 * rpcap[s]://device
2184*8b26181fSAndroid Build Coastguard Worker 		 */
2185*8b26181fSAndroid Build Coastguard Worker 		if (host && tmphost) {
2186*8b26181fSAndroid Build Coastguard Worker 			if (tmpuserinfo)
2187*8b26181fSAndroid Build Coastguard Worker 				snprintf(host, PCAP_BUF_SIZE, "%s@%s",
2188*8b26181fSAndroid Build Coastguard Worker 				    tmpuserinfo, tmphost);
2189*8b26181fSAndroid Build Coastguard Worker 			else
2190*8b26181fSAndroid Build Coastguard Worker 				pcap_strlcpy(host, tmphost, PCAP_BUF_SIZE);
2191*8b26181fSAndroid Build Coastguard Worker 		}
2192*8b26181fSAndroid Build Coastguard Worker 		if (port && tmpport)
2193*8b26181fSAndroid Build Coastguard Worker 			pcap_strlcpy(port, tmpport, PCAP_BUF_SIZE);
2194*8b26181fSAndroid Build Coastguard Worker 		if (name && tmppath)
2195*8b26181fSAndroid Build Coastguard Worker 			pcap_strlcpy(name, tmppath, PCAP_BUF_SIZE);
2196*8b26181fSAndroid Build Coastguard Worker 		if (type)
2197*8b26181fSAndroid Build Coastguard Worker 			*type = PCAP_SRC_IFREMOTE;
2198*8b26181fSAndroid Build Coastguard Worker 		free(tmppath);
2199*8b26181fSAndroid Build Coastguard Worker 		free(tmpport);
2200*8b26181fSAndroid Build Coastguard Worker 		free(tmphost);
2201*8b26181fSAndroid Build Coastguard Worker 		free(tmpuserinfo);
2202*8b26181fSAndroid Build Coastguard Worker 		free(scheme);
2203*8b26181fSAndroid Build Coastguard Worker 		return (0);
2204*8b26181fSAndroid Build Coastguard Worker 	}
2205*8b26181fSAndroid Build Coastguard Worker 
2206*8b26181fSAndroid Build Coastguard Worker 	if (strcmp(scheme, "file") == 0) {
2207*8b26181fSAndroid Build Coastguard Worker 		/*
2208*8b26181fSAndroid Build Coastguard Worker 		 * file://
2209*8b26181fSAndroid Build Coastguard Worker 		 */
2210*8b26181fSAndroid Build Coastguard Worker 		if (name && tmppath)
2211*8b26181fSAndroid Build Coastguard Worker 			pcap_strlcpy(name, tmppath, PCAP_BUF_SIZE);
2212*8b26181fSAndroid Build Coastguard Worker 		if (type)
2213*8b26181fSAndroid Build Coastguard Worker 			*type = PCAP_SRC_FILE;
2214*8b26181fSAndroid Build Coastguard Worker 		free(tmppath);
2215*8b26181fSAndroid Build Coastguard Worker 		free(tmpport);
2216*8b26181fSAndroid Build Coastguard Worker 		free(tmphost);
2217*8b26181fSAndroid Build Coastguard Worker 		free(tmpuserinfo);
2218*8b26181fSAndroid Build Coastguard Worker 		free(scheme);
2219*8b26181fSAndroid Build Coastguard Worker 		return (0);
2220*8b26181fSAndroid Build Coastguard Worker 	}
2221*8b26181fSAndroid Build Coastguard Worker 
2222*8b26181fSAndroid Build Coastguard Worker 	/*
2223*8b26181fSAndroid Build Coastguard Worker 	 * Neither rpcap: nor file:; just treat the entire string
2224*8b26181fSAndroid Build Coastguard Worker 	 * as a local device.
2225*8b26181fSAndroid Build Coastguard Worker 	 */
2226*8b26181fSAndroid Build Coastguard Worker 	if (name)
2227*8b26181fSAndroid Build Coastguard Worker 		pcap_strlcpy(name, source, PCAP_BUF_SIZE);
2228*8b26181fSAndroid Build Coastguard Worker 	if (type)
2229*8b26181fSAndroid Build Coastguard Worker 		*type = PCAP_SRC_IFLOCAL;
2230*8b26181fSAndroid Build Coastguard Worker 	free(tmppath);
2231*8b26181fSAndroid Build Coastguard Worker 	free(tmpport);
2232*8b26181fSAndroid Build Coastguard Worker 	free(tmphost);
2233*8b26181fSAndroid Build Coastguard Worker 	free(tmpuserinfo);
2234*8b26181fSAndroid Build Coastguard Worker 	free(scheme);
2235*8b26181fSAndroid Build Coastguard Worker 	return (0);
2236*8b26181fSAndroid Build Coastguard Worker }
2237*8b26181fSAndroid Build Coastguard Worker 
2238*8b26181fSAndroid Build Coastguard Worker int
2239*8b26181fSAndroid Build Coastguard Worker pcap_parsesrcstr(const char *source, int *type, char *host, char *port,
2240*8b26181fSAndroid Build Coastguard Worker     char *name, char *errbuf)
2241*8b26181fSAndroid Build Coastguard Worker {
2242*8b26181fSAndroid Build Coastguard Worker 	return (pcap_parsesrcstr_ex(source, type, host, port, name, NULL, errbuf));
2243*8b26181fSAndroid Build Coastguard Worker }
2244*8b26181fSAndroid Build Coastguard Worker #endif
2245*8b26181fSAndroid Build Coastguard Worker 
2246*8b26181fSAndroid Build Coastguard Worker pcap_t *
2247*8b26181fSAndroid Build Coastguard Worker pcap_create(const char *device, char *errbuf)
2248*8b26181fSAndroid Build Coastguard Worker {
2249*8b26181fSAndroid Build Coastguard Worker 	size_t i;
2250*8b26181fSAndroid Build Coastguard Worker 	int is_theirs;
2251*8b26181fSAndroid Build Coastguard Worker 	pcap_t *p;
2252*8b26181fSAndroid Build Coastguard Worker 	char *device_str;
2253*8b26181fSAndroid Build Coastguard Worker 
2254*8b26181fSAndroid Build Coastguard Worker 	/*
2255*8b26181fSAndroid Build Coastguard Worker 	 * A null device name is equivalent to the "any" device -
2256*8b26181fSAndroid Build Coastguard Worker 	 * which might not be supported on this platform, but
2257*8b26181fSAndroid Build Coastguard Worker 	 * this means that you'll get a "not supported" error
2258*8b26181fSAndroid Build Coastguard Worker 	 * rather than, say, a crash when we try to dereference
2259*8b26181fSAndroid Build Coastguard Worker 	 * the null pointer.
2260*8b26181fSAndroid Build Coastguard Worker 	 */
2261*8b26181fSAndroid Build Coastguard Worker 	if (device == NULL)
2262*8b26181fSAndroid Build Coastguard Worker 		device_str = strdup("any");
2263*8b26181fSAndroid Build Coastguard Worker 	else {
2264*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
2265*8b26181fSAndroid Build Coastguard Worker 		/*
2266*8b26181fSAndroid Build Coastguard Worker 		 * On Windows, for backwards compatibility reasons,
2267*8b26181fSAndroid Build Coastguard Worker 		 * pcap_lookupdev() returns a pointer to a sequence of
2268*8b26181fSAndroid Build Coastguard Worker 		 * pairs of UTF-16LE device names and local code page
2269*8b26181fSAndroid Build Coastguard Worker 		 * description strings.
2270*8b26181fSAndroid Build Coastguard Worker 		 *
2271*8b26181fSAndroid Build Coastguard Worker 		 * This means that if a program uses pcap_lookupdev()
2272*8b26181fSAndroid Build Coastguard Worker 		 * to get a default device, and hands that to an API
2273*8b26181fSAndroid Build Coastguard Worker 		 * that opens devices, we'll get handed a UTF-16LE
2274*8b26181fSAndroid Build Coastguard Worker 		 * string, not a string in the local code page.
2275*8b26181fSAndroid Build Coastguard Worker 		 *
2276*8b26181fSAndroid Build Coastguard Worker 		 * To work around that, we check whether the string
2277*8b26181fSAndroid Build Coastguard Worker 		 * looks as if it might be a UTF-16LE string and, if
2278*8b26181fSAndroid Build Coastguard Worker 		 * so, convert it back to the local code page's
2279*8b26181fSAndroid Build Coastguard Worker 		 * extended ASCII.
2280*8b26181fSAndroid Build Coastguard Worker 		 *
2281*8b26181fSAndroid Build Coastguard Worker 		 * We disable that check in "new API" mode, because:
2282*8b26181fSAndroid Build Coastguard Worker 		 *
2283*8b26181fSAndroid Build Coastguard Worker 		 *   1) You *cannot* reliably detect whether a
2284*8b26181fSAndroid Build Coastguard Worker 		 *   string is UTF-16LE or not; "a" could either
2285*8b26181fSAndroid Build Coastguard Worker 		 *   be a one-character ASCII string or the first
2286*8b26181fSAndroid Build Coastguard Worker 		 *   character of a UTF-16LE string.
2287*8b26181fSAndroid Build Coastguard Worker 		 *
2288*8b26181fSAndroid Build Coastguard Worker 		 *   2) Doing that test can run past the end of
2289*8b26181fSAndroid Build Coastguard Worker 		 *   the string, if it's a 1-character ASCII
2290*8b26181fSAndroid Build Coastguard Worker 		 *   string
2291*8b26181fSAndroid Build Coastguard Worker 		 *
2292*8b26181fSAndroid Build Coastguard Worker 		 * This particular version of this heuristic dates
2293*8b26181fSAndroid Build Coastguard Worker 		 * back to WinPcap 4.1.1; PacketOpenAdapter() does
2294*8b26181fSAndroid Build Coastguard Worker 		 * uses the same heuristic, with the exact same
2295*8b26181fSAndroid Build Coastguard Worker 		 * vulnerability.
2296*8b26181fSAndroid Build Coastguard Worker 		 *
2297*8b26181fSAndroid Build Coastguard Worker 		 * That's why we disable this in "new API" mode.
2298*8b26181fSAndroid Build Coastguard Worker 		 * We keep it around in legacy mode for backwards
2299*8b26181fSAndroid Build Coastguard Worker 		 * compatibility.
2300*8b26181fSAndroid Build Coastguard Worker 		 */
2301*8b26181fSAndroid Build Coastguard Worker 		if (!pcap_new_api && device[0] != '\0' && device[1] == '\0') {
2302*8b26181fSAndroid Build Coastguard Worker 			size_t length;
2303*8b26181fSAndroid Build Coastguard Worker 
2304*8b26181fSAndroid Build Coastguard Worker 			length = wcslen((wchar_t *)device);
2305*8b26181fSAndroid Build Coastguard Worker 			device_str = (char *)malloc(length + 1);
2306*8b26181fSAndroid Build Coastguard Worker 			if (device_str == NULL) {
2307*8b26181fSAndroid Build Coastguard Worker 				pcap_fmt_errmsg_for_errno(errbuf,
2308*8b26181fSAndroid Build Coastguard Worker 				    PCAP_ERRBUF_SIZE, errno,
2309*8b26181fSAndroid Build Coastguard Worker 				    "malloc");
2310*8b26181fSAndroid Build Coastguard Worker 				return (NULL);
2311*8b26181fSAndroid Build Coastguard Worker 			}
2312*8b26181fSAndroid Build Coastguard Worker 
2313*8b26181fSAndroid Build Coastguard Worker 			snprintf(device_str, length + 1, "%ws",
2314*8b26181fSAndroid Build Coastguard Worker 			    (const wchar_t *)device);
2315*8b26181fSAndroid Build Coastguard Worker 		} else
2316*8b26181fSAndroid Build Coastguard Worker #endif
2317*8b26181fSAndroid Build Coastguard Worker 			device_str = strdup(device);
2318*8b26181fSAndroid Build Coastguard Worker 	}
2319*8b26181fSAndroid Build Coastguard Worker 	if (device_str == NULL) {
2320*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
2321*8b26181fSAndroid Build Coastguard Worker 		    errno, "malloc");
2322*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
2323*8b26181fSAndroid Build Coastguard Worker 	}
2324*8b26181fSAndroid Build Coastguard Worker 
2325*8b26181fSAndroid Build Coastguard Worker 	/*
2326*8b26181fSAndroid Build Coastguard Worker 	 * Try each of the non-local-network-interface capture
2327*8b26181fSAndroid Build Coastguard Worker 	 * source types until we find one that works for this
2328*8b26181fSAndroid Build Coastguard Worker 	 * device or run out of types.
2329*8b26181fSAndroid Build Coastguard Worker 	 */
2330*8b26181fSAndroid Build Coastguard Worker 	for (i = 0; capture_source_types[i].create_op != NULL; i++) {
2331*8b26181fSAndroid Build Coastguard Worker 		is_theirs = 0;
2332*8b26181fSAndroid Build Coastguard Worker 		p = capture_source_types[i].create_op(device_str, errbuf,
2333*8b26181fSAndroid Build Coastguard Worker 		    &is_theirs);
2334*8b26181fSAndroid Build Coastguard Worker 		if (is_theirs) {
2335*8b26181fSAndroid Build Coastguard Worker 			/*
2336*8b26181fSAndroid Build Coastguard Worker 			 * The device name refers to a device of the
2337*8b26181fSAndroid Build Coastguard Worker 			 * type in question; either it succeeded,
2338*8b26181fSAndroid Build Coastguard Worker 			 * in which case p refers to a pcap_t to
2339*8b26181fSAndroid Build Coastguard Worker 			 * later activate for the device, or it
2340*8b26181fSAndroid Build Coastguard Worker 			 * failed, in which case p is null and we
2341*8b26181fSAndroid Build Coastguard Worker 			 * should return that to report the failure
2342*8b26181fSAndroid Build Coastguard Worker 			 * to create.
2343*8b26181fSAndroid Build Coastguard Worker 			 */
2344*8b26181fSAndroid Build Coastguard Worker 			if (p == NULL) {
2345*8b26181fSAndroid Build Coastguard Worker 				/*
2346*8b26181fSAndroid Build Coastguard Worker 				 * We assume the caller filled in errbuf.
2347*8b26181fSAndroid Build Coastguard Worker 				 */
2348*8b26181fSAndroid Build Coastguard Worker 				free(device_str);
2349*8b26181fSAndroid Build Coastguard Worker 				return (NULL);
2350*8b26181fSAndroid Build Coastguard Worker 			}
2351*8b26181fSAndroid Build Coastguard Worker 			p->opt.device = device_str;
2352*8b26181fSAndroid Build Coastguard Worker 			return (p);
2353*8b26181fSAndroid Build Coastguard Worker 		}
2354*8b26181fSAndroid Build Coastguard Worker 	}
2355*8b26181fSAndroid Build Coastguard Worker 
2356*8b26181fSAndroid Build Coastguard Worker 	/*
2357*8b26181fSAndroid Build Coastguard Worker 	 * OK, try it as a regular network interface.
2358*8b26181fSAndroid Build Coastguard Worker 	 */
2359*8b26181fSAndroid Build Coastguard Worker 	p = pcap_create_interface(device_str, errbuf);
2360*8b26181fSAndroid Build Coastguard Worker 	if (p == NULL) {
2361*8b26181fSAndroid Build Coastguard Worker 		/*
2362*8b26181fSAndroid Build Coastguard Worker 		 * We assume the caller filled in errbuf.
2363*8b26181fSAndroid Build Coastguard Worker 		 */
2364*8b26181fSAndroid Build Coastguard Worker 		free(device_str);
2365*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
2366*8b26181fSAndroid Build Coastguard Worker 	}
2367*8b26181fSAndroid Build Coastguard Worker 	p->opt.device = device_str;
2368*8b26181fSAndroid Build Coastguard Worker 	return (p);
2369*8b26181fSAndroid Build Coastguard Worker }
2370*8b26181fSAndroid Build Coastguard Worker 
2371*8b26181fSAndroid Build Coastguard Worker /*
2372*8b26181fSAndroid Build Coastguard Worker  * Set nonblocking mode on an unactivated pcap_t; this sets a flag
2373*8b26181fSAndroid Build Coastguard Worker  * checked by pcap_activate(), which sets the mode after calling
2374*8b26181fSAndroid Build Coastguard Worker  * the activate routine.
2375*8b26181fSAndroid Build Coastguard Worker  */
2376*8b26181fSAndroid Build Coastguard Worker static int
2377*8b26181fSAndroid Build Coastguard Worker pcap_setnonblock_unactivated(pcap_t *p, int nonblock)
2378*8b26181fSAndroid Build Coastguard Worker {
2379*8b26181fSAndroid Build Coastguard Worker 	p->opt.nonblock = nonblock;
2380*8b26181fSAndroid Build Coastguard Worker 	return (0);
2381*8b26181fSAndroid Build Coastguard Worker }
2382*8b26181fSAndroid Build Coastguard Worker 
2383*8b26181fSAndroid Build Coastguard Worker static void
2384*8b26181fSAndroid Build Coastguard Worker initialize_ops(pcap_t *p)
2385*8b26181fSAndroid Build Coastguard Worker {
2386*8b26181fSAndroid Build Coastguard Worker 	/*
2387*8b26181fSAndroid Build Coastguard Worker 	 * Set operation pointers for operations that only work on
2388*8b26181fSAndroid Build Coastguard Worker 	 * an activated pcap_t to point to a routine that returns
2389*8b26181fSAndroid Build Coastguard Worker 	 * a "this isn't activated" error.
2390*8b26181fSAndroid Build Coastguard Worker 	 */
2391*8b26181fSAndroid Build Coastguard Worker 	p->read_op = pcap_read_not_initialized;
2392*8b26181fSAndroid Build Coastguard Worker 	p->inject_op = pcap_inject_not_initialized;
2393*8b26181fSAndroid Build Coastguard Worker 	p->setfilter_op = pcap_setfilter_not_initialized;
2394*8b26181fSAndroid Build Coastguard Worker 	p->setdirection_op = pcap_setdirection_not_initialized;
2395*8b26181fSAndroid Build Coastguard Worker 	p->set_datalink_op = pcap_set_datalink_not_initialized;
2396*8b26181fSAndroid Build Coastguard Worker 	p->getnonblock_op = pcap_getnonblock_not_initialized;
2397*8b26181fSAndroid Build Coastguard Worker 	p->stats_op = pcap_stats_not_initialized;
2398*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
2399*8b26181fSAndroid Build Coastguard Worker 	p->stats_ex_op = pcap_stats_ex_not_initialized;
2400*8b26181fSAndroid Build Coastguard Worker 	p->setbuff_op = pcap_setbuff_not_initialized;
2401*8b26181fSAndroid Build Coastguard Worker 	p->setmode_op = pcap_setmode_not_initialized;
2402*8b26181fSAndroid Build Coastguard Worker 	p->setmintocopy_op = pcap_setmintocopy_not_initialized;
2403*8b26181fSAndroid Build Coastguard Worker 	p->getevent_op = pcap_getevent_not_initialized;
2404*8b26181fSAndroid Build Coastguard Worker 	p->oid_get_request_op = pcap_oid_get_request_not_initialized;
2405*8b26181fSAndroid Build Coastguard Worker 	p->oid_set_request_op = pcap_oid_set_request_not_initialized;
2406*8b26181fSAndroid Build Coastguard Worker 	p->sendqueue_transmit_op = pcap_sendqueue_transmit_not_initialized;
2407*8b26181fSAndroid Build Coastguard Worker 	p->setuserbuffer_op = pcap_setuserbuffer_not_initialized;
2408*8b26181fSAndroid Build Coastguard Worker 	p->live_dump_op = pcap_live_dump_not_initialized;
2409*8b26181fSAndroid Build Coastguard Worker 	p->live_dump_ended_op = pcap_live_dump_ended_not_initialized;
2410*8b26181fSAndroid Build Coastguard Worker 	p->get_airpcap_handle_op = pcap_get_airpcap_handle_not_initialized;
2411*8b26181fSAndroid Build Coastguard Worker #endif
2412*8b26181fSAndroid Build Coastguard Worker 
2413*8b26181fSAndroid Build Coastguard Worker 	/*
2414*8b26181fSAndroid Build Coastguard Worker 	 * Default cleanup operation - implementations can override
2415*8b26181fSAndroid Build Coastguard Worker 	 * this, but should call pcap_cleanup_live_common() after
2416*8b26181fSAndroid Build Coastguard Worker 	 * doing their own additional cleanup.
2417*8b26181fSAndroid Build Coastguard Worker 	 */
2418*8b26181fSAndroid Build Coastguard Worker 	p->cleanup_op = pcap_cleanup_live_common;
2419*8b26181fSAndroid Build Coastguard Worker 
2420*8b26181fSAndroid Build Coastguard Worker 	/*
2421*8b26181fSAndroid Build Coastguard Worker 	 * In most cases, the standard one-shot callback can
2422*8b26181fSAndroid Build Coastguard Worker 	 * be used for pcap_next()/pcap_next_ex().
2423*8b26181fSAndroid Build Coastguard Worker 	 */
2424*8b26181fSAndroid Build Coastguard Worker 	p->oneshot_callback = pcap_oneshot;
2425*8b26181fSAndroid Build Coastguard Worker 
2426*8b26181fSAndroid Build Coastguard Worker 	/*
2427*8b26181fSAndroid Build Coastguard Worker 	 * Default breakloop operation - implementations can override
2428*8b26181fSAndroid Build Coastguard Worker 	 * this, but should call pcap_breakloop_common() before doing
2429*8b26181fSAndroid Build Coastguard Worker 	 * their own logic.
2430*8b26181fSAndroid Build Coastguard Worker 	 */
2431*8b26181fSAndroid Build Coastguard Worker 	p->breakloop_op = pcap_breakloop_common;
2432*8b26181fSAndroid Build Coastguard Worker }
2433*8b26181fSAndroid Build Coastguard Worker 
2434*8b26181fSAndroid Build Coastguard Worker static pcap_t *
2435*8b26181fSAndroid Build Coastguard Worker pcap_alloc_pcap_t(char *ebuf, size_t total_size, size_t private_offset)
2436*8b26181fSAndroid Build Coastguard Worker {
2437*8b26181fSAndroid Build Coastguard Worker 	char *chunk;
2438*8b26181fSAndroid Build Coastguard Worker 	pcap_t *p;
2439*8b26181fSAndroid Build Coastguard Worker 
2440*8b26181fSAndroid Build Coastguard Worker 	/*
2441*8b26181fSAndroid Build Coastguard Worker 	 * total_size is the size of a structure containing a pcap_t
2442*8b26181fSAndroid Build Coastguard Worker 	 * followed by a private structure.
2443*8b26181fSAndroid Build Coastguard Worker 	 */
2444*8b26181fSAndroid Build Coastguard Worker 	chunk = calloc(total_size, 1);
2445*8b26181fSAndroid Build Coastguard Worker 	if (chunk == NULL) {
2446*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
2447*8b26181fSAndroid Build Coastguard Worker 		    errno, "malloc");
2448*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
2449*8b26181fSAndroid Build Coastguard Worker 	}
2450*8b26181fSAndroid Build Coastguard Worker 
2451*8b26181fSAndroid Build Coastguard Worker 	/*
2452*8b26181fSAndroid Build Coastguard Worker 	 * Get a pointer to the pcap_t at the beginning.
2453*8b26181fSAndroid Build Coastguard Worker 	 */
2454*8b26181fSAndroid Build Coastguard Worker 	p = (pcap_t *)chunk;
2455*8b26181fSAndroid Build Coastguard Worker 
2456*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
2457*8b26181fSAndroid Build Coastguard Worker 	p->handle = INVALID_HANDLE_VALUE;	/* not opened yet */
2458*8b26181fSAndroid Build Coastguard Worker #else /* _WIN32 */
2459*8b26181fSAndroid Build Coastguard Worker 	p->fd = -1;	/* not opened yet */
2460*8b26181fSAndroid Build Coastguard Worker #ifndef MSDOS
2461*8b26181fSAndroid Build Coastguard Worker 	p->selectable_fd = -1;
2462*8b26181fSAndroid Build Coastguard Worker 	p->required_select_timeout = NULL;
2463*8b26181fSAndroid Build Coastguard Worker #endif /* MSDOS */
2464*8b26181fSAndroid Build Coastguard Worker #endif /* _WIN32 */
2465*8b26181fSAndroid Build Coastguard Worker 
2466*8b26181fSAndroid Build Coastguard Worker 	/*
2467*8b26181fSAndroid Build Coastguard Worker 	 * private_offset is the offset, in bytes, of the private
2468*8b26181fSAndroid Build Coastguard Worker 	 * data from the beginning of the structure.
2469*8b26181fSAndroid Build Coastguard Worker 	 *
2470*8b26181fSAndroid Build Coastguard Worker 	 * Set the pointer to the private data; that's private_offset
2471*8b26181fSAndroid Build Coastguard Worker 	 * bytes past the pcap_t.
2472*8b26181fSAndroid Build Coastguard Worker 	 */
2473*8b26181fSAndroid Build Coastguard Worker 	p->priv = (void *)(chunk + private_offset);
2474*8b26181fSAndroid Build Coastguard Worker 
2475*8b26181fSAndroid Build Coastguard Worker 	return (p);
2476*8b26181fSAndroid Build Coastguard Worker }
2477*8b26181fSAndroid Build Coastguard Worker 
2478*8b26181fSAndroid Build Coastguard Worker pcap_t *
2479*8b26181fSAndroid Build Coastguard Worker pcap_create_common(char *ebuf, size_t total_size, size_t private_offset)
2480*8b26181fSAndroid Build Coastguard Worker {
2481*8b26181fSAndroid Build Coastguard Worker 	pcap_t *p;
2482*8b26181fSAndroid Build Coastguard Worker 
2483*8b26181fSAndroid Build Coastguard Worker 	p = pcap_alloc_pcap_t(ebuf, total_size, private_offset);
2484*8b26181fSAndroid Build Coastguard Worker 	if (p == NULL)
2485*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
2486*8b26181fSAndroid Build Coastguard Worker 
2487*8b26181fSAndroid Build Coastguard Worker 	/*
2488*8b26181fSAndroid Build Coastguard Worker 	 * Default to "can't set rfmon mode"; if it's supported by
2489*8b26181fSAndroid Build Coastguard Worker 	 * a platform, the create routine that called us can set
2490*8b26181fSAndroid Build Coastguard Worker 	 * the op to its routine to check whether a particular
2491*8b26181fSAndroid Build Coastguard Worker 	 * device supports it.
2492*8b26181fSAndroid Build Coastguard Worker 	 */
2493*8b26181fSAndroid Build Coastguard Worker 	p->can_set_rfmon_op = pcap_cant_set_rfmon;
2494*8b26181fSAndroid Build Coastguard Worker 
2495*8b26181fSAndroid Build Coastguard Worker 	/*
2496*8b26181fSAndroid Build Coastguard Worker 	 * If pcap_setnonblock() is called on a not-yet-activated
2497*8b26181fSAndroid Build Coastguard Worker 	 * pcap_t, default to setting a flag and turning
2498*8b26181fSAndroid Build Coastguard Worker 	 * on non-blocking mode when activated.
2499*8b26181fSAndroid Build Coastguard Worker 	 */
2500*8b26181fSAndroid Build Coastguard Worker 	p->setnonblock_op = pcap_setnonblock_unactivated;
2501*8b26181fSAndroid Build Coastguard Worker 
2502*8b26181fSAndroid Build Coastguard Worker 	initialize_ops(p);
2503*8b26181fSAndroid Build Coastguard Worker 
2504*8b26181fSAndroid Build Coastguard Worker 	/* put in some defaults*/
2505*8b26181fSAndroid Build Coastguard Worker 	p->snapshot = 0;		/* max packet size unspecified */
2506*8b26181fSAndroid Build Coastguard Worker 	p->opt.timeout = 0;		/* no timeout specified */
2507*8b26181fSAndroid Build Coastguard Worker 	p->opt.buffer_size = 0;		/* use the platform's default */
2508*8b26181fSAndroid Build Coastguard Worker 	p->opt.promisc = 0;
2509*8b26181fSAndroid Build Coastguard Worker 	p->opt.rfmon = 0;
2510*8b26181fSAndroid Build Coastguard Worker 	p->opt.immediate = 0;
2511*8b26181fSAndroid Build Coastguard Worker 	p->opt.tstamp_type = -1;	/* default to not setting time stamp type */
2512*8b26181fSAndroid Build Coastguard Worker 	p->opt.tstamp_precision = PCAP_TSTAMP_PRECISION_MICRO;
2513*8b26181fSAndroid Build Coastguard Worker 	/*
2514*8b26181fSAndroid Build Coastguard Worker 	 * Platform-dependent options.
2515*8b26181fSAndroid Build Coastguard Worker 	 */
2516*8b26181fSAndroid Build Coastguard Worker #ifdef __linux__
2517*8b26181fSAndroid Build Coastguard Worker 	p->opt.protocol = 0;
2518*8b26181fSAndroid Build Coastguard Worker #endif
2519*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
2520*8b26181fSAndroid Build Coastguard Worker 	p->opt.nocapture_local = 0;
2521*8b26181fSAndroid Build Coastguard Worker #endif
2522*8b26181fSAndroid Build Coastguard Worker 
2523*8b26181fSAndroid Build Coastguard Worker 	/*
2524*8b26181fSAndroid Build Coastguard Worker 	 * Start out with no BPF code generation flags set.
2525*8b26181fSAndroid Build Coastguard Worker 	 */
2526*8b26181fSAndroid Build Coastguard Worker 	p->bpf_codegen_flags = 0;
2527*8b26181fSAndroid Build Coastguard Worker 
2528*8b26181fSAndroid Build Coastguard Worker 	return (p);
2529*8b26181fSAndroid Build Coastguard Worker }
2530*8b26181fSAndroid Build Coastguard Worker 
2531*8b26181fSAndroid Build Coastguard Worker int
2532*8b26181fSAndroid Build Coastguard Worker pcap_check_activated(pcap_t *p)
2533*8b26181fSAndroid Build Coastguard Worker {
2534*8b26181fSAndroid Build Coastguard Worker 	if (p->activated) {
2535*8b26181fSAndroid Build Coastguard Worker 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "can't perform "
2536*8b26181fSAndroid Build Coastguard Worker 			" operation on activated capture");
2537*8b26181fSAndroid Build Coastguard Worker 		return (-1);
2538*8b26181fSAndroid Build Coastguard Worker 	}
2539*8b26181fSAndroid Build Coastguard Worker 	return (0);
2540*8b26181fSAndroid Build Coastguard Worker }
2541*8b26181fSAndroid Build Coastguard Worker 
2542*8b26181fSAndroid Build Coastguard Worker int
2543*8b26181fSAndroid Build Coastguard Worker pcap_set_snaplen(pcap_t *p, int snaplen)
2544*8b26181fSAndroid Build Coastguard Worker {
2545*8b26181fSAndroid Build Coastguard Worker 	if (pcap_check_activated(p))
2546*8b26181fSAndroid Build Coastguard Worker 		return (PCAP_ERROR_ACTIVATED);
2547*8b26181fSAndroid Build Coastguard Worker 	p->snapshot = snaplen;
2548*8b26181fSAndroid Build Coastguard Worker 	return (0);
2549*8b26181fSAndroid Build Coastguard Worker }
2550*8b26181fSAndroid Build Coastguard Worker 
2551*8b26181fSAndroid Build Coastguard Worker int
2552*8b26181fSAndroid Build Coastguard Worker pcap_set_promisc(pcap_t *p, int promisc)
2553*8b26181fSAndroid Build Coastguard Worker {
2554*8b26181fSAndroid Build Coastguard Worker 	if (pcap_check_activated(p))
2555*8b26181fSAndroid Build Coastguard Worker 		return (PCAP_ERROR_ACTIVATED);
2556*8b26181fSAndroid Build Coastguard Worker 	p->opt.promisc = promisc;
2557*8b26181fSAndroid Build Coastguard Worker 	return (0);
2558*8b26181fSAndroid Build Coastguard Worker }
2559*8b26181fSAndroid Build Coastguard Worker 
2560*8b26181fSAndroid Build Coastguard Worker int
2561*8b26181fSAndroid Build Coastguard Worker pcap_set_rfmon(pcap_t *p, int rfmon)
2562*8b26181fSAndroid Build Coastguard Worker {
2563*8b26181fSAndroid Build Coastguard Worker 	if (pcap_check_activated(p))
2564*8b26181fSAndroid Build Coastguard Worker 		return (PCAP_ERROR_ACTIVATED);
2565*8b26181fSAndroid Build Coastguard Worker 	p->opt.rfmon = rfmon;
2566*8b26181fSAndroid Build Coastguard Worker 	return (0);
2567*8b26181fSAndroid Build Coastguard Worker }
2568*8b26181fSAndroid Build Coastguard Worker 
2569*8b26181fSAndroid Build Coastguard Worker int
2570*8b26181fSAndroid Build Coastguard Worker pcap_set_timeout(pcap_t *p, int timeout_ms)
2571*8b26181fSAndroid Build Coastguard Worker {
2572*8b26181fSAndroid Build Coastguard Worker 	if (pcap_check_activated(p))
2573*8b26181fSAndroid Build Coastguard Worker 		return (PCAP_ERROR_ACTIVATED);
2574*8b26181fSAndroid Build Coastguard Worker 	p->opt.timeout = timeout_ms;
2575*8b26181fSAndroid Build Coastguard Worker 	return (0);
2576*8b26181fSAndroid Build Coastguard Worker }
2577*8b26181fSAndroid Build Coastguard Worker 
2578*8b26181fSAndroid Build Coastguard Worker int
2579*8b26181fSAndroid Build Coastguard Worker pcap_set_tstamp_type(pcap_t *p, int tstamp_type)
2580*8b26181fSAndroid Build Coastguard Worker {
2581*8b26181fSAndroid Build Coastguard Worker 	int i;
2582*8b26181fSAndroid Build Coastguard Worker 
2583*8b26181fSAndroid Build Coastguard Worker 	if (pcap_check_activated(p))
2584*8b26181fSAndroid Build Coastguard Worker 		return (PCAP_ERROR_ACTIVATED);
2585*8b26181fSAndroid Build Coastguard Worker 
2586*8b26181fSAndroid Build Coastguard Worker 	/*
2587*8b26181fSAndroid Build Coastguard Worker 	 * The argument should have been u_int, but that's too late
2588*8b26181fSAndroid Build Coastguard Worker 	 * to change now - it's an API.
2589*8b26181fSAndroid Build Coastguard Worker 	 */
2590*8b26181fSAndroid Build Coastguard Worker 	if (tstamp_type < 0)
2591*8b26181fSAndroid Build Coastguard Worker 		return (PCAP_WARNING_TSTAMP_TYPE_NOTSUP);
2592*8b26181fSAndroid Build Coastguard Worker 
2593*8b26181fSAndroid Build Coastguard Worker 	/*
2594*8b26181fSAndroid Build Coastguard Worker 	 * If p->tstamp_type_count is 0, we only support PCAP_TSTAMP_HOST;
2595*8b26181fSAndroid Build Coastguard Worker 	 * the default time stamp type is PCAP_TSTAMP_HOST.
2596*8b26181fSAndroid Build Coastguard Worker 	 */
2597*8b26181fSAndroid Build Coastguard Worker 	if (p->tstamp_type_count == 0) {
2598*8b26181fSAndroid Build Coastguard Worker 		if (tstamp_type == PCAP_TSTAMP_HOST) {
2599*8b26181fSAndroid Build Coastguard Worker 			p->opt.tstamp_type = tstamp_type;
2600*8b26181fSAndroid Build Coastguard Worker 			return (0);
2601*8b26181fSAndroid Build Coastguard Worker 		}
2602*8b26181fSAndroid Build Coastguard Worker 	} else {
2603*8b26181fSAndroid Build Coastguard Worker 		/*
2604*8b26181fSAndroid Build Coastguard Worker 		 * Check whether we claim to support this type of time stamp.
2605*8b26181fSAndroid Build Coastguard Worker 		 */
2606*8b26181fSAndroid Build Coastguard Worker 		for (i = 0; i < p->tstamp_type_count; i++) {
2607*8b26181fSAndroid Build Coastguard Worker 			if (p->tstamp_type_list[i] == (u_int)tstamp_type) {
2608*8b26181fSAndroid Build Coastguard Worker 				/*
2609*8b26181fSAndroid Build Coastguard Worker 				 * Yes.
2610*8b26181fSAndroid Build Coastguard Worker 				 */
2611*8b26181fSAndroid Build Coastguard Worker 				p->opt.tstamp_type = tstamp_type;
2612*8b26181fSAndroid Build Coastguard Worker 				return (0);
2613*8b26181fSAndroid Build Coastguard Worker 			}
2614*8b26181fSAndroid Build Coastguard Worker 		}
2615*8b26181fSAndroid Build Coastguard Worker 	}
2616*8b26181fSAndroid Build Coastguard Worker 
2617*8b26181fSAndroid Build Coastguard Worker 	/*
2618*8b26181fSAndroid Build Coastguard Worker 	 * We don't support this type of time stamp.
2619*8b26181fSAndroid Build Coastguard Worker 	 */
2620*8b26181fSAndroid Build Coastguard Worker 	return (PCAP_WARNING_TSTAMP_TYPE_NOTSUP);
2621*8b26181fSAndroid Build Coastguard Worker }
2622*8b26181fSAndroid Build Coastguard Worker 
2623*8b26181fSAndroid Build Coastguard Worker int
2624*8b26181fSAndroid Build Coastguard Worker pcap_set_immediate_mode(pcap_t *p, int immediate)
2625*8b26181fSAndroid Build Coastguard Worker {
2626*8b26181fSAndroid Build Coastguard Worker 	if (pcap_check_activated(p))
2627*8b26181fSAndroid Build Coastguard Worker 		return (PCAP_ERROR_ACTIVATED);
2628*8b26181fSAndroid Build Coastguard Worker 	p->opt.immediate = immediate;
2629*8b26181fSAndroid Build Coastguard Worker 	return (0);
2630*8b26181fSAndroid Build Coastguard Worker }
2631*8b26181fSAndroid Build Coastguard Worker 
2632*8b26181fSAndroid Build Coastguard Worker int
2633*8b26181fSAndroid Build Coastguard Worker pcap_set_buffer_size(pcap_t *p, int buffer_size)
2634*8b26181fSAndroid Build Coastguard Worker {
2635*8b26181fSAndroid Build Coastguard Worker 	if (pcap_check_activated(p))
2636*8b26181fSAndroid Build Coastguard Worker 		return (PCAP_ERROR_ACTIVATED);
2637*8b26181fSAndroid Build Coastguard Worker 	if (buffer_size <= 0) {
2638*8b26181fSAndroid Build Coastguard Worker 		/*
2639*8b26181fSAndroid Build Coastguard Worker 		 * Silently ignore invalid values.
2640*8b26181fSAndroid Build Coastguard Worker 		 */
2641*8b26181fSAndroid Build Coastguard Worker 		return (0);
2642*8b26181fSAndroid Build Coastguard Worker 	}
2643*8b26181fSAndroid Build Coastguard Worker 	p->opt.buffer_size = buffer_size;
2644*8b26181fSAndroid Build Coastguard Worker 	return (0);
2645*8b26181fSAndroid Build Coastguard Worker }
2646*8b26181fSAndroid Build Coastguard Worker 
2647*8b26181fSAndroid Build Coastguard Worker int
2648*8b26181fSAndroid Build Coastguard Worker pcap_set_tstamp_precision(pcap_t *p, int tstamp_precision)
2649*8b26181fSAndroid Build Coastguard Worker {
2650*8b26181fSAndroid Build Coastguard Worker 	int i;
2651*8b26181fSAndroid Build Coastguard Worker 
2652*8b26181fSAndroid Build Coastguard Worker 	if (pcap_check_activated(p))
2653*8b26181fSAndroid Build Coastguard Worker 		return (PCAP_ERROR_ACTIVATED);
2654*8b26181fSAndroid Build Coastguard Worker 
2655*8b26181fSAndroid Build Coastguard Worker 	/*
2656*8b26181fSAndroid Build Coastguard Worker 	 * The argument should have been u_int, but that's too late
2657*8b26181fSAndroid Build Coastguard Worker 	 * to change now - it's an API.
2658*8b26181fSAndroid Build Coastguard Worker 	 */
2659*8b26181fSAndroid Build Coastguard Worker 	if (tstamp_precision < 0)
2660*8b26181fSAndroid Build Coastguard Worker 		return (PCAP_ERROR_TSTAMP_PRECISION_NOTSUP);
2661*8b26181fSAndroid Build Coastguard Worker 
2662*8b26181fSAndroid Build Coastguard Worker 	/*
2663*8b26181fSAndroid Build Coastguard Worker 	 * If p->tstamp_precision_count is 0, we only support setting
2664*8b26181fSAndroid Build Coastguard Worker 	 * the time stamp precision to microsecond precision; every
2665*8b26181fSAndroid Build Coastguard Worker 	 * pcap module *MUST* support microsecond precision, even if
2666*8b26181fSAndroid Build Coastguard Worker 	 * it does so by converting the native precision to
2667*8b26181fSAndroid Build Coastguard Worker 	 * microseconds.
2668*8b26181fSAndroid Build Coastguard Worker 	 */
2669*8b26181fSAndroid Build Coastguard Worker 	if (p->tstamp_precision_count == 0) {
2670*8b26181fSAndroid Build Coastguard Worker 		if (tstamp_precision == PCAP_TSTAMP_PRECISION_MICRO) {
2671*8b26181fSAndroid Build Coastguard Worker 			p->opt.tstamp_precision = tstamp_precision;
2672*8b26181fSAndroid Build Coastguard Worker 			return (0);
2673*8b26181fSAndroid Build Coastguard Worker 		}
2674*8b26181fSAndroid Build Coastguard Worker 	} else {
2675*8b26181fSAndroid Build Coastguard Worker 		/*
2676*8b26181fSAndroid Build Coastguard Worker 		 * Check whether we claim to support this precision of
2677*8b26181fSAndroid Build Coastguard Worker 		 * time stamp.
2678*8b26181fSAndroid Build Coastguard Worker 		 */
2679*8b26181fSAndroid Build Coastguard Worker 		for (i = 0; i < p->tstamp_precision_count; i++) {
2680*8b26181fSAndroid Build Coastguard Worker 			if (p->tstamp_precision_list[i] == (u_int)tstamp_precision) {
2681*8b26181fSAndroid Build Coastguard Worker 				/*
2682*8b26181fSAndroid Build Coastguard Worker 				 * Yes.
2683*8b26181fSAndroid Build Coastguard Worker 				 */
2684*8b26181fSAndroid Build Coastguard Worker 				p->opt.tstamp_precision = tstamp_precision;
2685*8b26181fSAndroid Build Coastguard Worker 				return (0);
2686*8b26181fSAndroid Build Coastguard Worker 			}
2687*8b26181fSAndroid Build Coastguard Worker 		}
2688*8b26181fSAndroid Build Coastguard Worker 	}
2689*8b26181fSAndroid Build Coastguard Worker 
2690*8b26181fSAndroid Build Coastguard Worker 	/*
2691*8b26181fSAndroid Build Coastguard Worker 	 * We don't support this time stamp precision.
2692*8b26181fSAndroid Build Coastguard Worker 	 */
2693*8b26181fSAndroid Build Coastguard Worker 	return (PCAP_ERROR_TSTAMP_PRECISION_NOTSUP);
2694*8b26181fSAndroid Build Coastguard Worker }
2695*8b26181fSAndroid Build Coastguard Worker 
2696*8b26181fSAndroid Build Coastguard Worker int
2697*8b26181fSAndroid Build Coastguard Worker pcap_get_tstamp_precision(pcap_t *p)
2698*8b26181fSAndroid Build Coastguard Worker {
2699*8b26181fSAndroid Build Coastguard Worker         return (p->opt.tstamp_precision);
2700*8b26181fSAndroid Build Coastguard Worker }
2701*8b26181fSAndroid Build Coastguard Worker 
2702*8b26181fSAndroid Build Coastguard Worker int
2703*8b26181fSAndroid Build Coastguard Worker pcap_activate(pcap_t *p)
2704*8b26181fSAndroid Build Coastguard Worker {
2705*8b26181fSAndroid Build Coastguard Worker 	int status;
2706*8b26181fSAndroid Build Coastguard Worker 
2707*8b26181fSAndroid Build Coastguard Worker 	/*
2708*8b26181fSAndroid Build Coastguard Worker 	 * Catch attempts to re-activate an already-activated
2709*8b26181fSAndroid Build Coastguard Worker 	 * pcap_t; this should, for example, catch code that
2710*8b26181fSAndroid Build Coastguard Worker 	 * calls pcap_open_live() followed by pcap_activate(),
2711*8b26181fSAndroid Build Coastguard Worker 	 * as some code that showed up in a Stack Exchange
2712*8b26181fSAndroid Build Coastguard Worker 	 * question did.
2713*8b26181fSAndroid Build Coastguard Worker 	 */
2714*8b26181fSAndroid Build Coastguard Worker 	if (pcap_check_activated(p))
2715*8b26181fSAndroid Build Coastguard Worker 		return (PCAP_ERROR_ACTIVATED);
2716*8b26181fSAndroid Build Coastguard Worker 	status = p->activate_op(p);
2717*8b26181fSAndroid Build Coastguard Worker 	if (status >= 0) {
2718*8b26181fSAndroid Build Coastguard Worker 		/*
2719*8b26181fSAndroid Build Coastguard Worker 		 * If somebody requested non-blocking mode before
2720*8b26181fSAndroid Build Coastguard Worker 		 * calling pcap_activate(), turn it on now.
2721*8b26181fSAndroid Build Coastguard Worker 		 */
2722*8b26181fSAndroid Build Coastguard Worker 		if (p->opt.nonblock) {
2723*8b26181fSAndroid Build Coastguard Worker 			status = p->setnonblock_op(p, 1);
2724*8b26181fSAndroid Build Coastguard Worker 			if (status < 0) {
2725*8b26181fSAndroid Build Coastguard Worker 				/*
2726*8b26181fSAndroid Build Coastguard Worker 				 * Failed.  Undo everything done by
2727*8b26181fSAndroid Build Coastguard Worker 				 * the activate operation.
2728*8b26181fSAndroid Build Coastguard Worker 				 */
2729*8b26181fSAndroid Build Coastguard Worker 				p->cleanup_op(p);
2730*8b26181fSAndroid Build Coastguard Worker 				initialize_ops(p);
2731*8b26181fSAndroid Build Coastguard Worker 				return (status);
2732*8b26181fSAndroid Build Coastguard Worker 			}
2733*8b26181fSAndroid Build Coastguard Worker 		}
2734*8b26181fSAndroid Build Coastguard Worker 		p->activated = 1;
2735*8b26181fSAndroid Build Coastguard Worker 	} else {
2736*8b26181fSAndroid Build Coastguard Worker 		if (p->errbuf[0] == '\0') {
2737*8b26181fSAndroid Build Coastguard Worker 			/*
2738*8b26181fSAndroid Build Coastguard Worker 			 * No error message supplied by the activate routine;
2739*8b26181fSAndroid Build Coastguard Worker 			 * for the benefit of programs that don't specially
2740*8b26181fSAndroid Build Coastguard Worker 			 * handle errors other than PCAP_ERROR, return the
2741*8b26181fSAndroid Build Coastguard Worker 			 * error message corresponding to the status.
2742*8b26181fSAndroid Build Coastguard Worker 			 */
2743*8b26181fSAndroid Build Coastguard Worker 			snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s",
2744*8b26181fSAndroid Build Coastguard Worker 			    pcap_statustostr(status));
2745*8b26181fSAndroid Build Coastguard Worker 		}
2746*8b26181fSAndroid Build Coastguard Worker 
2747*8b26181fSAndroid Build Coastguard Worker 		/*
2748*8b26181fSAndroid Build Coastguard Worker 		 * Undo any operation pointer setting, etc. done by
2749*8b26181fSAndroid Build Coastguard Worker 		 * the activate operation.
2750*8b26181fSAndroid Build Coastguard Worker 		 */
2751*8b26181fSAndroid Build Coastguard Worker 		initialize_ops(p);
2752*8b26181fSAndroid Build Coastguard Worker 	}
2753*8b26181fSAndroid Build Coastguard Worker 	return (status);
2754*8b26181fSAndroid Build Coastguard Worker }
2755*8b26181fSAndroid Build Coastguard Worker 
2756*8b26181fSAndroid Build Coastguard Worker pcap_t *
2757*8b26181fSAndroid Build Coastguard Worker pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, char *errbuf)
2758*8b26181fSAndroid Build Coastguard Worker {
2759*8b26181fSAndroid Build Coastguard Worker 	pcap_t *p;
2760*8b26181fSAndroid Build Coastguard Worker 	int status;
2761*8b26181fSAndroid Build Coastguard Worker #ifdef ENABLE_REMOTE
2762*8b26181fSAndroid Build Coastguard Worker 	char host[PCAP_BUF_SIZE + 1];
2763*8b26181fSAndroid Build Coastguard Worker 	char port[PCAP_BUF_SIZE + 1];
2764*8b26181fSAndroid Build Coastguard Worker 	char name[PCAP_BUF_SIZE + 1];
2765*8b26181fSAndroid Build Coastguard Worker 	int srctype;
2766*8b26181fSAndroid Build Coastguard Worker 
2767*8b26181fSAndroid Build Coastguard Worker 	/*
2768*8b26181fSAndroid Build Coastguard Worker 	 * A null device name is equivalent to the "any" device -
2769*8b26181fSAndroid Build Coastguard Worker 	 * which might not be supported on this platform, but
2770*8b26181fSAndroid Build Coastguard Worker 	 * this means that you'll get a "not supported" error
2771*8b26181fSAndroid Build Coastguard Worker 	 * rather than, say, a crash when we try to dereference
2772*8b26181fSAndroid Build Coastguard Worker 	 * the null pointer.
2773*8b26181fSAndroid Build Coastguard Worker 	 */
2774*8b26181fSAndroid Build Coastguard Worker 	if (device == NULL)
2775*8b26181fSAndroid Build Coastguard Worker 		device = "any";
2776*8b26181fSAndroid Build Coastguard Worker 
2777*8b26181fSAndroid Build Coastguard Worker 	/*
2778*8b26181fSAndroid Build Coastguard Worker 	 * Retrofit - we have to make older applications compatible with
2779*8b26181fSAndroid Build Coastguard Worker 	 * remote capture.
2780*8b26181fSAndroid Build Coastguard Worker 	 * So we're calling pcap_open_remote() from here; this is a very
2781*8b26181fSAndroid Build Coastguard Worker 	 * dirty hack.
2782*8b26181fSAndroid Build Coastguard Worker 	 * Obviously, we cannot exploit all the new features; for instance,
2783*8b26181fSAndroid Build Coastguard Worker 	 * we cannot send authentication, we cannot use a UDP data connection,
2784*8b26181fSAndroid Build Coastguard Worker 	 * and so on.
2785*8b26181fSAndroid Build Coastguard Worker 	 */
2786*8b26181fSAndroid Build Coastguard Worker 	if (pcap_parsesrcstr(device, &srctype, host, port, name, errbuf))
2787*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
2788*8b26181fSAndroid Build Coastguard Worker 
2789*8b26181fSAndroid Build Coastguard Worker 	if (srctype == PCAP_SRC_IFREMOTE) {
2790*8b26181fSAndroid Build Coastguard Worker 		/*
2791*8b26181fSAndroid Build Coastguard Worker 		 * Although we already have host, port and iface, we prefer
2792*8b26181fSAndroid Build Coastguard Worker 		 * to pass only 'device' to pcap_open_rpcap(), so that it has
2793*8b26181fSAndroid Build Coastguard Worker 		 * to call pcap_parsesrcstr() again.
2794*8b26181fSAndroid Build Coastguard Worker 		 * This is less optimized, but much clearer.
2795*8b26181fSAndroid Build Coastguard Worker 		 */
2796*8b26181fSAndroid Build Coastguard Worker 		return (pcap_open_rpcap(device, snaplen,
2797*8b26181fSAndroid Build Coastguard Worker 		    promisc ? PCAP_OPENFLAG_PROMISCUOUS : 0, to_ms,
2798*8b26181fSAndroid Build Coastguard Worker 		    NULL, errbuf));
2799*8b26181fSAndroid Build Coastguard Worker 	}
2800*8b26181fSAndroid Build Coastguard Worker 	if (srctype == PCAP_SRC_FILE) {
2801*8b26181fSAndroid Build Coastguard Worker 		snprintf(errbuf, PCAP_ERRBUF_SIZE, "unknown URL scheme \"file\"");
2802*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
2803*8b26181fSAndroid Build Coastguard Worker 	}
2804*8b26181fSAndroid Build Coastguard Worker 	if (srctype == PCAP_SRC_IFLOCAL) {
2805*8b26181fSAndroid Build Coastguard Worker 		/*
2806*8b26181fSAndroid Build Coastguard Worker 		 * If it starts with rpcap://, that refers to a local device
2807*8b26181fSAndroid Build Coastguard Worker 		 * (no host part in the URL). Remove the rpcap://, and
2808*8b26181fSAndroid Build Coastguard Worker 		 * fall through to the regular open path.
2809*8b26181fSAndroid Build Coastguard Worker 		 */
2810*8b26181fSAndroid Build Coastguard Worker 		if (strncmp(device, PCAP_SRC_IF_STRING, strlen(PCAP_SRC_IF_STRING)) == 0) {
2811*8b26181fSAndroid Build Coastguard Worker 			size_t len = strlen(device) - strlen(PCAP_SRC_IF_STRING) + 1;
2812*8b26181fSAndroid Build Coastguard Worker 
2813*8b26181fSAndroid Build Coastguard Worker 			if (len > 0)
2814*8b26181fSAndroid Build Coastguard Worker 				device += strlen(PCAP_SRC_IF_STRING);
2815*8b26181fSAndroid Build Coastguard Worker 		}
2816*8b26181fSAndroid Build Coastguard Worker 	}
2817*8b26181fSAndroid Build Coastguard Worker #endif	/* ENABLE_REMOTE */
2818*8b26181fSAndroid Build Coastguard Worker 
2819*8b26181fSAndroid Build Coastguard Worker 	p = pcap_create(device, errbuf);
2820*8b26181fSAndroid Build Coastguard Worker 	if (p == NULL)
2821*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
2822*8b26181fSAndroid Build Coastguard Worker 	status = pcap_set_snaplen(p, snaplen);
2823*8b26181fSAndroid Build Coastguard Worker 	if (status < 0)
2824*8b26181fSAndroid Build Coastguard Worker 		goto fail;
2825*8b26181fSAndroid Build Coastguard Worker 	status = pcap_set_promisc(p, promisc);
2826*8b26181fSAndroid Build Coastguard Worker 	if (status < 0)
2827*8b26181fSAndroid Build Coastguard Worker 		goto fail;
2828*8b26181fSAndroid Build Coastguard Worker 	status = pcap_set_timeout(p, to_ms);
2829*8b26181fSAndroid Build Coastguard Worker 	if (status < 0)
2830*8b26181fSAndroid Build Coastguard Worker 		goto fail;
2831*8b26181fSAndroid Build Coastguard Worker 	/*
2832*8b26181fSAndroid Build Coastguard Worker 	 * Mark this as opened with pcap_open_live(), so that, for
2833*8b26181fSAndroid Build Coastguard Worker 	 * example, we show the full list of DLT_ values, rather
2834*8b26181fSAndroid Build Coastguard Worker 	 * than just the ones that are compatible with capturing
2835*8b26181fSAndroid Build Coastguard Worker 	 * when not in monitor mode.  That allows existing applications
2836*8b26181fSAndroid Build Coastguard Worker 	 * to work the way they used to work, but allows new applications
2837*8b26181fSAndroid Build Coastguard Worker 	 * that know about the new open API to, for example, find out the
2838*8b26181fSAndroid Build Coastguard Worker 	 * DLT_ values that they can select without changing whether
2839*8b26181fSAndroid Build Coastguard Worker 	 * the adapter is in monitor mode or not.
2840*8b26181fSAndroid Build Coastguard Worker 	 */
2841*8b26181fSAndroid Build Coastguard Worker 	p->oldstyle = 1;
2842*8b26181fSAndroid Build Coastguard Worker 	status = pcap_activate(p);
2843*8b26181fSAndroid Build Coastguard Worker 	if (status < 0)
2844*8b26181fSAndroid Build Coastguard Worker 		goto fail;
2845*8b26181fSAndroid Build Coastguard Worker 	return (p);
2846*8b26181fSAndroid Build Coastguard Worker fail:
2847*8b26181fSAndroid Build Coastguard Worker 	if (status == PCAP_ERROR) {
2848*8b26181fSAndroid Build Coastguard Worker 		/*
2849*8b26181fSAndroid Build Coastguard Worker 		 * Another buffer is a bit cumbersome, but it avoids
2850*8b26181fSAndroid Build Coastguard Worker 		 * -Wformat-truncation.
2851*8b26181fSAndroid Build Coastguard Worker 		 */
2852*8b26181fSAndroid Build Coastguard Worker 		char trimbuf[PCAP_ERRBUF_SIZE - 5]; /* 2 bytes shorter */
2853*8b26181fSAndroid Build Coastguard Worker 
2854*8b26181fSAndroid Build Coastguard Worker 		pcap_strlcpy(trimbuf, p->errbuf, sizeof(trimbuf));
2855*8b26181fSAndroid Build Coastguard Worker 		snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %.*s", device,
2856*8b26181fSAndroid Build Coastguard Worker 		    PCAP_ERRBUF_SIZE - 3, trimbuf);
2857*8b26181fSAndroid Build Coastguard Worker 	} else if (status == PCAP_ERROR_NO_SUCH_DEVICE ||
2858*8b26181fSAndroid Build Coastguard Worker 	    status == PCAP_ERROR_PERM_DENIED ||
2859*8b26181fSAndroid Build Coastguard Worker 	    status == PCAP_ERROR_PROMISC_PERM_DENIED) {
2860*8b26181fSAndroid Build Coastguard Worker 		/*
2861*8b26181fSAndroid Build Coastguard Worker 		 * Only show the additional message if it's not
2862*8b26181fSAndroid Build Coastguard Worker 		 * empty.
2863*8b26181fSAndroid Build Coastguard Worker 		 */
2864*8b26181fSAndroid Build Coastguard Worker 		if (p->errbuf[0] != '\0') {
2865*8b26181fSAndroid Build Coastguard Worker 			/*
2866*8b26181fSAndroid Build Coastguard Worker 			 * Idem.
2867*8b26181fSAndroid Build Coastguard Worker 			 */
2868*8b26181fSAndroid Build Coastguard Worker 			char trimbuf[PCAP_ERRBUF_SIZE - 8]; /* 2 bytes shorter */
2869*8b26181fSAndroid Build Coastguard Worker 
2870*8b26181fSAndroid Build Coastguard Worker 			pcap_strlcpy(trimbuf, p->errbuf, sizeof(trimbuf));
2871*8b26181fSAndroid Build Coastguard Worker 			snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s (%.*s)",
2872*8b26181fSAndroid Build Coastguard Worker 			    device, pcap_statustostr(status),
2873*8b26181fSAndroid Build Coastguard Worker 			    PCAP_ERRBUF_SIZE - 6, trimbuf);
2874*8b26181fSAndroid Build Coastguard Worker 		} else {
2875*8b26181fSAndroid Build Coastguard Worker 			snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s",
2876*8b26181fSAndroid Build Coastguard Worker 			    device, pcap_statustostr(status));
2877*8b26181fSAndroid Build Coastguard Worker 		}
2878*8b26181fSAndroid Build Coastguard Worker 	} else {
2879*8b26181fSAndroid Build Coastguard Worker 		snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", device,
2880*8b26181fSAndroid Build Coastguard Worker 		    pcap_statustostr(status));
2881*8b26181fSAndroid Build Coastguard Worker 	}
2882*8b26181fSAndroid Build Coastguard Worker 	pcap_close(p);
2883*8b26181fSAndroid Build Coastguard Worker 	return (NULL);
2884*8b26181fSAndroid Build Coastguard Worker }
2885*8b26181fSAndroid Build Coastguard Worker 
2886*8b26181fSAndroid Build Coastguard Worker pcap_t *
2887*8b26181fSAndroid Build Coastguard Worker pcap_open_offline_common(char *ebuf, size_t total_size, size_t private_offset)
2888*8b26181fSAndroid Build Coastguard Worker {
2889*8b26181fSAndroid Build Coastguard Worker 	pcap_t *p;
2890*8b26181fSAndroid Build Coastguard Worker 
2891*8b26181fSAndroid Build Coastguard Worker 	p = pcap_alloc_pcap_t(ebuf, total_size, private_offset);
2892*8b26181fSAndroid Build Coastguard Worker 	if (p == NULL)
2893*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
2894*8b26181fSAndroid Build Coastguard Worker 
2895*8b26181fSAndroid Build Coastguard Worker 	p->opt.tstamp_precision = PCAP_TSTAMP_PRECISION_MICRO;
2896*8b26181fSAndroid Build Coastguard Worker 
2897*8b26181fSAndroid Build Coastguard Worker 	return (p);
2898*8b26181fSAndroid Build Coastguard Worker }
2899*8b26181fSAndroid Build Coastguard Worker 
2900*8b26181fSAndroid Build Coastguard Worker int
2901*8b26181fSAndroid Build Coastguard Worker pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
2902*8b26181fSAndroid Build Coastguard Worker {
2903*8b26181fSAndroid Build Coastguard Worker 	return (p->read_op(p, cnt, callback, user));
2904*8b26181fSAndroid Build Coastguard Worker }
2905*8b26181fSAndroid Build Coastguard Worker 
2906*8b26181fSAndroid Build Coastguard Worker int
2907*8b26181fSAndroid Build Coastguard Worker pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
2908*8b26181fSAndroid Build Coastguard Worker {
2909*8b26181fSAndroid Build Coastguard Worker 	register int n;
2910*8b26181fSAndroid Build Coastguard Worker 
2911*8b26181fSAndroid Build Coastguard Worker 	for (;;) {
2912*8b26181fSAndroid Build Coastguard Worker 		if (p->rfile != NULL) {
2913*8b26181fSAndroid Build Coastguard Worker 			/*
2914*8b26181fSAndroid Build Coastguard Worker 			 * 0 means EOF, so don't loop if we get 0.
2915*8b26181fSAndroid Build Coastguard Worker 			 */
2916*8b26181fSAndroid Build Coastguard Worker 			n = pcap_offline_read(p, cnt, callback, user);
2917*8b26181fSAndroid Build Coastguard Worker 		} else {
2918*8b26181fSAndroid Build Coastguard Worker 			/*
2919*8b26181fSAndroid Build Coastguard Worker 			 * XXX keep reading until we get something
2920*8b26181fSAndroid Build Coastguard Worker 			 * (or an error occurs)
2921*8b26181fSAndroid Build Coastguard Worker 			 */
2922*8b26181fSAndroid Build Coastguard Worker 			do {
2923*8b26181fSAndroid Build Coastguard Worker 				n = p->read_op(p, cnt, callback, user);
2924*8b26181fSAndroid Build Coastguard Worker 			} while (n == 0);
2925*8b26181fSAndroid Build Coastguard Worker 		}
2926*8b26181fSAndroid Build Coastguard Worker 		if (n <= 0)
2927*8b26181fSAndroid Build Coastguard Worker 			return (n);
2928*8b26181fSAndroid Build Coastguard Worker 		if (!PACKET_COUNT_IS_UNLIMITED(cnt)) {
2929*8b26181fSAndroid Build Coastguard Worker 			cnt -= n;
2930*8b26181fSAndroid Build Coastguard Worker 			if (cnt <= 0)
2931*8b26181fSAndroid Build Coastguard Worker 				return (0);
2932*8b26181fSAndroid Build Coastguard Worker 		}
2933*8b26181fSAndroid Build Coastguard Worker 	}
2934*8b26181fSAndroid Build Coastguard Worker }
2935*8b26181fSAndroid Build Coastguard Worker 
2936*8b26181fSAndroid Build Coastguard Worker /*
2937*8b26181fSAndroid Build Coastguard Worker  * Force the loop in "pcap_read()" or "pcap_read_offline()" to terminate.
2938*8b26181fSAndroid Build Coastguard Worker  */
2939*8b26181fSAndroid Build Coastguard Worker void
2940*8b26181fSAndroid Build Coastguard Worker pcap_breakloop(pcap_t *p)
2941*8b26181fSAndroid Build Coastguard Worker {
2942*8b26181fSAndroid Build Coastguard Worker 	p->breakloop_op(p);
2943*8b26181fSAndroid Build Coastguard Worker }
2944*8b26181fSAndroid Build Coastguard Worker 
2945*8b26181fSAndroid Build Coastguard Worker int
2946*8b26181fSAndroid Build Coastguard Worker pcap_datalink(pcap_t *p)
2947*8b26181fSAndroid Build Coastguard Worker {
2948*8b26181fSAndroid Build Coastguard Worker 	if (!p->activated)
2949*8b26181fSAndroid Build Coastguard Worker 		return (PCAP_ERROR_NOT_ACTIVATED);
2950*8b26181fSAndroid Build Coastguard Worker 	return (p->linktype);
2951*8b26181fSAndroid Build Coastguard Worker }
2952*8b26181fSAndroid Build Coastguard Worker 
2953*8b26181fSAndroid Build Coastguard Worker int
2954*8b26181fSAndroid Build Coastguard Worker pcap_datalink_ext(pcap_t *p)
2955*8b26181fSAndroid Build Coastguard Worker {
2956*8b26181fSAndroid Build Coastguard Worker 	if (!p->activated)
2957*8b26181fSAndroid Build Coastguard Worker 		return (PCAP_ERROR_NOT_ACTIVATED);
2958*8b26181fSAndroid Build Coastguard Worker 	return (p->linktype_ext);
2959*8b26181fSAndroid Build Coastguard Worker }
2960*8b26181fSAndroid Build Coastguard Worker 
2961*8b26181fSAndroid Build Coastguard Worker int
2962*8b26181fSAndroid Build Coastguard Worker pcap_list_datalinks(pcap_t *p, int **dlt_buffer)
2963*8b26181fSAndroid Build Coastguard Worker {
2964*8b26181fSAndroid Build Coastguard Worker 	if (!p->activated)
2965*8b26181fSAndroid Build Coastguard Worker 		return (PCAP_ERROR_NOT_ACTIVATED);
2966*8b26181fSAndroid Build Coastguard Worker 	if (p->dlt_count == 0) {
2967*8b26181fSAndroid Build Coastguard Worker 		/*
2968*8b26181fSAndroid Build Coastguard Worker 		 * We couldn't fetch the list of DLTs, which means
2969*8b26181fSAndroid Build Coastguard Worker 		 * this platform doesn't support changing the
2970*8b26181fSAndroid Build Coastguard Worker 		 * DLT for an interface.  Return a list of DLTs
2971*8b26181fSAndroid Build Coastguard Worker 		 * containing only the DLT this device supports.
2972*8b26181fSAndroid Build Coastguard Worker 		 */
2973*8b26181fSAndroid Build Coastguard Worker 		*dlt_buffer = (int*)malloc(sizeof(**dlt_buffer));
2974*8b26181fSAndroid Build Coastguard Worker 		if (*dlt_buffer == NULL) {
2975*8b26181fSAndroid Build Coastguard Worker 			pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
2976*8b26181fSAndroid Build Coastguard Worker 			    errno, "malloc");
2977*8b26181fSAndroid Build Coastguard Worker 			return (PCAP_ERROR);
2978*8b26181fSAndroid Build Coastguard Worker 		}
2979*8b26181fSAndroid Build Coastguard Worker 		**dlt_buffer = p->linktype;
2980*8b26181fSAndroid Build Coastguard Worker 		return (1);
2981*8b26181fSAndroid Build Coastguard Worker 	} else {
2982*8b26181fSAndroid Build Coastguard Worker 		*dlt_buffer = (int*)calloc(sizeof(**dlt_buffer), p->dlt_count);
2983*8b26181fSAndroid Build Coastguard Worker 		if (*dlt_buffer == NULL) {
2984*8b26181fSAndroid Build Coastguard Worker 			pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
2985*8b26181fSAndroid Build Coastguard Worker 			    errno, "malloc");
2986*8b26181fSAndroid Build Coastguard Worker 			return (PCAP_ERROR);
2987*8b26181fSAndroid Build Coastguard Worker 		}
2988*8b26181fSAndroid Build Coastguard Worker 		(void)memcpy(*dlt_buffer, p->dlt_list,
2989*8b26181fSAndroid Build Coastguard Worker 		    sizeof(**dlt_buffer) * p->dlt_count);
2990*8b26181fSAndroid Build Coastguard Worker 		return (p->dlt_count);
2991*8b26181fSAndroid Build Coastguard Worker 	}
2992*8b26181fSAndroid Build Coastguard Worker }
2993*8b26181fSAndroid Build Coastguard Worker 
2994*8b26181fSAndroid Build Coastguard Worker /*
2995*8b26181fSAndroid Build Coastguard Worker  * In Windows, you might have a library built with one version of the
2996*8b26181fSAndroid Build Coastguard Worker  * C runtime library and an application built with another version of
2997*8b26181fSAndroid Build Coastguard Worker  * the C runtime library, which means that the library might use one
2998*8b26181fSAndroid Build Coastguard Worker  * version of malloc() and free() and the application might use another
2999*8b26181fSAndroid Build Coastguard Worker  * version of malloc() and free().  If so, that means something
3000*8b26181fSAndroid Build Coastguard Worker  * allocated by the library cannot be freed by the application, so we
3001*8b26181fSAndroid Build Coastguard Worker  * need to have a pcap_free_datalinks() routine to free up the list
3002*8b26181fSAndroid Build Coastguard Worker  * allocated by pcap_list_datalinks(), even though it's just a wrapper
3003*8b26181fSAndroid Build Coastguard Worker  * around free().
3004*8b26181fSAndroid Build Coastguard Worker  */
3005*8b26181fSAndroid Build Coastguard Worker void
3006*8b26181fSAndroid Build Coastguard Worker pcap_free_datalinks(int *dlt_list)
3007*8b26181fSAndroid Build Coastguard Worker {
3008*8b26181fSAndroid Build Coastguard Worker 	free(dlt_list);
3009*8b26181fSAndroid Build Coastguard Worker }
3010*8b26181fSAndroid Build Coastguard Worker 
3011*8b26181fSAndroid Build Coastguard Worker int
3012*8b26181fSAndroid Build Coastguard Worker pcap_set_datalink(pcap_t *p, int dlt)
3013*8b26181fSAndroid Build Coastguard Worker {
3014*8b26181fSAndroid Build Coastguard Worker 	int i;
3015*8b26181fSAndroid Build Coastguard Worker 	const char *dlt_name;
3016*8b26181fSAndroid Build Coastguard Worker 
3017*8b26181fSAndroid Build Coastguard Worker 	if (dlt < 0)
3018*8b26181fSAndroid Build Coastguard Worker 		goto unsupported;
3019*8b26181fSAndroid Build Coastguard Worker 
3020*8b26181fSAndroid Build Coastguard Worker 	if (p->dlt_count == 0 || p->set_datalink_op == NULL) {
3021*8b26181fSAndroid Build Coastguard Worker 		/*
3022*8b26181fSAndroid Build Coastguard Worker 		 * We couldn't fetch the list of DLTs, or we don't
3023*8b26181fSAndroid Build Coastguard Worker 		 * have a "set datalink" operation, which means
3024*8b26181fSAndroid Build Coastguard Worker 		 * this platform doesn't support changing the
3025*8b26181fSAndroid Build Coastguard Worker 		 * DLT for an interface.  Check whether the new
3026*8b26181fSAndroid Build Coastguard Worker 		 * DLT is the one this interface supports.
3027*8b26181fSAndroid Build Coastguard Worker 		 */
3028*8b26181fSAndroid Build Coastguard Worker 		if (p->linktype != dlt)
3029*8b26181fSAndroid Build Coastguard Worker 			goto unsupported;
3030*8b26181fSAndroid Build Coastguard Worker 
3031*8b26181fSAndroid Build Coastguard Worker 		/*
3032*8b26181fSAndroid Build Coastguard Worker 		 * It is, so there's nothing we need to do here.
3033*8b26181fSAndroid Build Coastguard Worker 		 */
3034*8b26181fSAndroid Build Coastguard Worker 		return (0);
3035*8b26181fSAndroid Build Coastguard Worker 	}
3036*8b26181fSAndroid Build Coastguard Worker 	for (i = 0; i < p->dlt_count; i++)
3037*8b26181fSAndroid Build Coastguard Worker 		if (p->dlt_list[i] == (u_int)dlt)
3038*8b26181fSAndroid Build Coastguard Worker 			break;
3039*8b26181fSAndroid Build Coastguard Worker 	if (i >= p->dlt_count)
3040*8b26181fSAndroid Build Coastguard Worker 		goto unsupported;
3041*8b26181fSAndroid Build Coastguard Worker 	if (p->dlt_count == 2 && p->dlt_list[0] == DLT_EN10MB &&
3042*8b26181fSAndroid Build Coastguard Worker 	    dlt == DLT_DOCSIS) {
3043*8b26181fSAndroid Build Coastguard Worker 		/*
3044*8b26181fSAndroid Build Coastguard Worker 		 * This is presumably an Ethernet device, as the first
3045*8b26181fSAndroid Build Coastguard Worker 		 * link-layer type it offers is DLT_EN10MB, and the only
3046*8b26181fSAndroid Build Coastguard Worker 		 * other type it offers is DLT_DOCSIS.  That means that
3047*8b26181fSAndroid Build Coastguard Worker 		 * we can't tell the driver to supply DOCSIS link-layer
3048*8b26181fSAndroid Build Coastguard Worker 		 * headers - we're just pretending that's what we're
3049*8b26181fSAndroid Build Coastguard Worker 		 * getting, as, presumably, we're capturing on a dedicated
3050*8b26181fSAndroid Build Coastguard Worker 		 * link to a Cisco Cable Modem Termination System, and
3051*8b26181fSAndroid Build Coastguard Worker 		 * it's putting raw DOCSIS frames on the wire inside low-level
3052*8b26181fSAndroid Build Coastguard Worker 		 * Ethernet framing.
3053*8b26181fSAndroid Build Coastguard Worker 		 */
3054*8b26181fSAndroid Build Coastguard Worker 		p->linktype = dlt;
3055*8b26181fSAndroid Build Coastguard Worker 		return (0);
3056*8b26181fSAndroid Build Coastguard Worker 	}
3057*8b26181fSAndroid Build Coastguard Worker 	if (p->set_datalink_op(p, dlt) == -1)
3058*8b26181fSAndroid Build Coastguard Worker 		return (-1);
3059*8b26181fSAndroid Build Coastguard Worker 	p->linktype = dlt;
3060*8b26181fSAndroid Build Coastguard Worker 	return (0);
3061*8b26181fSAndroid Build Coastguard Worker 
3062*8b26181fSAndroid Build Coastguard Worker unsupported:
3063*8b26181fSAndroid Build Coastguard Worker 	dlt_name = pcap_datalink_val_to_name(dlt);
3064*8b26181fSAndroid Build Coastguard Worker 	if (dlt_name != NULL) {
3065*8b26181fSAndroid Build Coastguard Worker 		(void) snprintf(p->errbuf, sizeof(p->errbuf),
3066*8b26181fSAndroid Build Coastguard Worker 		    "%s is not one of the DLTs supported by this device",
3067*8b26181fSAndroid Build Coastguard Worker 		    dlt_name);
3068*8b26181fSAndroid Build Coastguard Worker 	} else {
3069*8b26181fSAndroid Build Coastguard Worker 		(void) snprintf(p->errbuf, sizeof(p->errbuf),
3070*8b26181fSAndroid Build Coastguard Worker 		    "DLT %d is not one of the DLTs supported by this device",
3071*8b26181fSAndroid Build Coastguard Worker 		    dlt);
3072*8b26181fSAndroid Build Coastguard Worker 	}
3073*8b26181fSAndroid Build Coastguard Worker 	return (-1);
3074*8b26181fSAndroid Build Coastguard Worker }
3075*8b26181fSAndroid Build Coastguard Worker 
3076*8b26181fSAndroid Build Coastguard Worker /*
3077*8b26181fSAndroid Build Coastguard Worker  * This array is designed for mapping upper and lower case letter
3078*8b26181fSAndroid Build Coastguard Worker  * together for a case independent comparison.  The mappings are
3079*8b26181fSAndroid Build Coastguard Worker  * based upon ascii character sequences.
3080*8b26181fSAndroid Build Coastguard Worker  */
3081*8b26181fSAndroid Build Coastguard Worker static const u_char charmap[] = {
3082*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\000', (u_char)'\001', (u_char)'\002', (u_char)'\003',
3083*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\004', (u_char)'\005', (u_char)'\006', (u_char)'\007',
3084*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\010', (u_char)'\011', (u_char)'\012', (u_char)'\013',
3085*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\014', (u_char)'\015', (u_char)'\016', (u_char)'\017',
3086*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\020', (u_char)'\021', (u_char)'\022', (u_char)'\023',
3087*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\024', (u_char)'\025', (u_char)'\026', (u_char)'\027',
3088*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\030', (u_char)'\031', (u_char)'\032', (u_char)'\033',
3089*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\034', (u_char)'\035', (u_char)'\036', (u_char)'\037',
3090*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\040', (u_char)'\041', (u_char)'\042', (u_char)'\043',
3091*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\044', (u_char)'\045', (u_char)'\046', (u_char)'\047',
3092*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\050', (u_char)'\051', (u_char)'\052', (u_char)'\053',
3093*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\054', (u_char)'\055', (u_char)'\056', (u_char)'\057',
3094*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\060', (u_char)'\061', (u_char)'\062', (u_char)'\063',
3095*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\064', (u_char)'\065', (u_char)'\066', (u_char)'\067',
3096*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\070', (u_char)'\071', (u_char)'\072', (u_char)'\073',
3097*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\074', (u_char)'\075', (u_char)'\076', (u_char)'\077',
3098*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\100', (u_char)'\141', (u_char)'\142', (u_char)'\143',
3099*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
3100*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
3101*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
3102*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
3103*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
3104*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\133',
3105*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\134', (u_char)'\135', (u_char)'\136', (u_char)'\137',
3106*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\140', (u_char)'\141', (u_char)'\142', (u_char)'\143',
3107*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
3108*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
3109*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
3110*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
3111*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
3112*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\173',
3113*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\174', (u_char)'\175', (u_char)'\176', (u_char)'\177',
3114*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\200', (u_char)'\201', (u_char)'\202', (u_char)'\203',
3115*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\204', (u_char)'\205', (u_char)'\206', (u_char)'\207',
3116*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\210', (u_char)'\211', (u_char)'\212', (u_char)'\213',
3117*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\214', (u_char)'\215', (u_char)'\216', (u_char)'\217',
3118*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\220', (u_char)'\221', (u_char)'\222', (u_char)'\223',
3119*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\224', (u_char)'\225', (u_char)'\226', (u_char)'\227',
3120*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\230', (u_char)'\231', (u_char)'\232', (u_char)'\233',
3121*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\234', (u_char)'\235', (u_char)'\236', (u_char)'\237',
3122*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\240', (u_char)'\241', (u_char)'\242', (u_char)'\243',
3123*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\244', (u_char)'\245', (u_char)'\246', (u_char)'\247',
3124*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\250', (u_char)'\251', (u_char)'\252', (u_char)'\253',
3125*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\254', (u_char)'\255', (u_char)'\256', (u_char)'\257',
3126*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\260', (u_char)'\261', (u_char)'\262', (u_char)'\263',
3127*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\264', (u_char)'\265', (u_char)'\266', (u_char)'\267',
3128*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\270', (u_char)'\271', (u_char)'\272', (u_char)'\273',
3129*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\274', (u_char)'\275', (u_char)'\276', (u_char)'\277',
3130*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\300', (u_char)'\341', (u_char)'\342', (u_char)'\343',
3131*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
3132*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
3133*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
3134*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
3135*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
3136*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\333',
3137*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\334', (u_char)'\335', (u_char)'\336', (u_char)'\337',
3138*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\340', (u_char)'\341', (u_char)'\342', (u_char)'\343',
3139*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
3140*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
3141*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
3142*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
3143*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
3144*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\373',
3145*8b26181fSAndroid Build Coastguard Worker 	(u_char)'\374', (u_char)'\375', (u_char)'\376', (u_char)'\377',
3146*8b26181fSAndroid Build Coastguard Worker };
3147*8b26181fSAndroid Build Coastguard Worker 
3148*8b26181fSAndroid Build Coastguard Worker int
3149*8b26181fSAndroid Build Coastguard Worker pcap_strcasecmp(const char *s1, const char *s2)
3150*8b26181fSAndroid Build Coastguard Worker {
3151*8b26181fSAndroid Build Coastguard Worker 	register const u_char	*cm = charmap,
3152*8b26181fSAndroid Build Coastguard Worker 				*us1 = (const u_char *)s1,
3153*8b26181fSAndroid Build Coastguard Worker 				*us2 = (const u_char *)s2;
3154*8b26181fSAndroid Build Coastguard Worker 
3155*8b26181fSAndroid Build Coastguard Worker 	while (cm[*us1] == cm[*us2++])
3156*8b26181fSAndroid Build Coastguard Worker 		if (*us1++ == '\0')
3157*8b26181fSAndroid Build Coastguard Worker 			return(0);
3158*8b26181fSAndroid Build Coastguard Worker 	return (cm[*us1] - cm[*--us2]);
3159*8b26181fSAndroid Build Coastguard Worker }
3160*8b26181fSAndroid Build Coastguard Worker 
3161*8b26181fSAndroid Build Coastguard Worker struct dlt_choice {
3162*8b26181fSAndroid Build Coastguard Worker 	const char *name;
3163*8b26181fSAndroid Build Coastguard Worker 	const char *description;
3164*8b26181fSAndroid Build Coastguard Worker 	int	dlt;
3165*8b26181fSAndroid Build Coastguard Worker };
3166*8b26181fSAndroid Build Coastguard Worker 
3167*8b26181fSAndroid Build Coastguard Worker #define DLT_CHOICE(code, description) { #code, description, DLT_ ## code }
3168*8b26181fSAndroid Build Coastguard Worker #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 }
3169*8b26181fSAndroid Build Coastguard Worker 
3170*8b26181fSAndroid Build Coastguard Worker static struct dlt_choice dlt_choices[] = {
3171*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(NULL, "BSD loopback"),
3172*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(EN10MB, "Ethernet"),
3173*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(IEEE802, "Token ring"),
3174*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(ARCNET, "BSD ARCNET"),
3175*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(SLIP, "SLIP"),
3176*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(PPP, "PPP"),
3177*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(FDDI, "FDDI"),
3178*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(ATM_RFC1483, "RFC 1483 LLC-encapsulated ATM"),
3179*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(RAW, "Raw IP"),
3180*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(SLIP_BSDOS, "BSD/OS SLIP"),
3181*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(PPP_BSDOS, "BSD/OS PPP"),
3182*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(ATM_CLIP, "Linux Classical IP over ATM"),
3183*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(PPP_SERIAL, "PPP over serial"),
3184*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(PPP_ETHER, "PPPoE"),
3185*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(SYMANTEC_FIREWALL, "Symantec Firewall"),
3186*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(C_HDLC, "Cisco HDLC"),
3187*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(IEEE802_11, "802.11"),
3188*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(FRELAY, "Frame Relay"),
3189*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(LOOP, "OpenBSD loopback"),
3190*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(ENC, "OpenBSD encapsulated IP"),
3191*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(LINUX_SLL, "Linux cooked v1"),
3192*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(LTALK, "Localtalk"),
3193*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(PFLOG, "OpenBSD pflog file"),
3194*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(PFSYNC, "Packet filter state syncing"),
3195*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(PRISM_HEADER, "802.11 plus Prism header"),
3196*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"),
3197*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(SUNATM, "Sun raw ATM"),
3198*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(IEEE802_11_RADIO, "802.11 plus radiotap header"),
3199*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(ARCNET_LINUX, "Linux ARCNET"),
3200*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(JUNIPER_MLPPP, "Juniper Multi-Link PPP"),
3201*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(JUNIPER_MLFR, "Juniper Multi-Link Frame Relay"),
3202*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(JUNIPER_ES, "Juniper Encryption Services PIC"),
3203*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(JUNIPER_GGSN, "Juniper GGSN PIC"),
3204*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(JUNIPER_MFR, "Juniper FRF.16 Frame Relay"),
3205*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(JUNIPER_ATM2, "Juniper ATM2 PIC"),
3206*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(JUNIPER_SERVICES, "Juniper Advanced Services PIC"),
3207*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(JUNIPER_ATM1, "Juniper ATM1 PIC"),
3208*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(APPLE_IP_OVER_IEEE1394, "Apple IP-over-IEEE 1394"),
3209*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(MTP2_WITH_PHDR, "SS7 MTP2 with Pseudo-header"),
3210*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(MTP2, "SS7 MTP2"),
3211*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(MTP3, "SS7 MTP3"),
3212*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(SCCP, "SS7 SCCP"),
3213*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(DOCSIS, "DOCSIS"),
3214*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(LINUX_IRDA, "Linux IrDA"),
3215*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(IEEE802_11_RADIO_AVS, "802.11 plus AVS radio information header"),
3216*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(JUNIPER_MONITOR, "Juniper Passive Monitor PIC"),
3217*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(BACNET_MS_TP, "BACnet MS/TP"),
3218*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(PPP_PPPD, "PPP for pppd, with direction flag"),
3219*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(JUNIPER_PPPOE, "Juniper PPPoE"),
3220*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(JUNIPER_PPPOE_ATM, "Juniper PPPoE/ATM"),
3221*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(GPRS_LLC, "GPRS LLC"),
3222*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(GPF_T, "GPF-T"),
3223*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(GPF_F, "GPF-F"),
3224*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(JUNIPER_PIC_PEER, "Juniper PIC Peer"),
3225*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(ERF_ETH, "Ethernet with Endace ERF header"),
3226*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(ERF_POS, "Packet-over-SONET with Endace ERF header"),
3227*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(LINUX_LAPD, "Linux vISDN LAPD"),
3228*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(JUNIPER_ETHER, "Juniper Ethernet"),
3229*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(JUNIPER_PPP, "Juniper PPP"),
3230*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(JUNIPER_FRELAY, "Juniper Frame Relay"),
3231*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(JUNIPER_CHDLC, "Juniper C-HDLC"),
3232*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(MFR, "FRF.16 Frame Relay"),
3233*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(JUNIPER_VP, "Juniper Voice PIC"),
3234*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(A429, "Arinc 429"),
3235*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(A653_ICM, "Arinc 653 Interpartition Communication"),
3236*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(USB_FREEBSD, "USB with FreeBSD header"),
3237*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(BLUETOOTH_HCI_H4, "Bluetooth HCI UART transport layer"),
3238*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(IEEE802_16_MAC_CPS, "IEEE 802.16 MAC Common Part Sublayer"),
3239*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(USB_LINUX, "USB with Linux header"),
3240*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(CAN20B, "Controller Area Network (CAN) v. 2.0B"),
3241*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(IEEE802_15_4_LINUX, "IEEE 802.15.4 with Linux padding"),
3242*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(PPI, "Per-Packet Information"),
3243*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(IEEE802_16_MAC_CPS_RADIO, "IEEE 802.16 MAC Common Part Sublayer plus radiotap header"),
3244*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(JUNIPER_ISM, "Juniper Integrated Service Module"),
3245*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(IEEE802_15_4, "IEEE 802.15.4 with FCS"),
3246*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(SITA, "SITA pseudo-header"),
3247*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(ERF, "Endace ERF header"),
3248*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(RAIF1, "Ethernet with u10 Networks pseudo-header"),
3249*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(IPMB_KONTRON, "IPMB with Kontron pseudo-header"),
3250*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(JUNIPER_ST, "Juniper Secure Tunnel"),
3251*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(BLUETOOTH_HCI_H4_WITH_PHDR, "Bluetooth HCI UART transport layer plus pseudo-header"),
3252*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(AX25_KISS, "AX.25 with KISS header"),
3253*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(IPMB_LINUX, "IPMB with Linux/Pigeon Point pseudo-header"),
3254*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(IEEE802_15_4_NONASK_PHY, "IEEE 802.15.4 with non-ASK PHY data"),
3255*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(MPLS, "MPLS with label as link-layer header"),
3256*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(LINUX_EVDEV, "Linux evdev events"),
3257*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(USB_LINUX_MMAPPED, "USB with padded Linux header"),
3258*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(DECT, "DECT"),
3259*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(AOS, "AOS Space Data Link protocol"),
3260*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(WIHART, "Wireless HART"),
3261*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(FC_2, "Fibre Channel FC-2"),
3262*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(FC_2_WITH_FRAME_DELIMS, "Fibre Channel FC-2 with frame delimiters"),
3263*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(IPNET, "Solaris ipnet"),
3264*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(CAN_SOCKETCAN, "CAN-bus with SocketCAN headers"),
3265*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(IPV4, "Raw IPv4"),
3266*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(IPV6, "Raw IPv6"),
3267*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(IEEE802_15_4_NOFCS, "IEEE 802.15.4 without FCS"),
3268*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(DBUS, "D-Bus"),
3269*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(JUNIPER_VS, "Juniper Virtual Server"),
3270*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(JUNIPER_SRX_E2E, "Juniper SRX E2E"),
3271*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(JUNIPER_FIBRECHANNEL, "Juniper Fibre Channel"),
3272*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(DVB_CI, "DVB-CI"),
3273*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(MUX27010, "MUX27010"),
3274*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(STANAG_5066_D_PDU, "STANAG 5066 D_PDUs"),
3275*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(JUNIPER_ATM_CEMIC, "Juniper ATM CEMIC"),
3276*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(NFLOG, "Linux netfilter log messages"),
3277*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(NETANALYZER, "Ethernet with Hilscher netANALYZER pseudo-header"),
3278*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(NETANALYZER_TRANSPARENT, "Ethernet with Hilscher netANALYZER pseudo-header and with preamble and SFD"),
3279*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(IPOIB, "RFC 4391 IP-over-Infiniband"),
3280*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(MPEG_2_TS, "MPEG-2 transport stream"),
3281*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(NG40, "ng40 protocol tester Iub/Iur"),
3282*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(NFC_LLCP, "NFC LLCP PDUs with pseudo-header"),
3283*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(INFINIBAND, "InfiniBand"),
3284*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(SCTP, "SCTP"),
3285*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(USBPCAP, "USB with USBPcap header"),
3286*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(RTAC_SERIAL, "Schweitzer Engineering Laboratories RTAC packets"),
3287*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(BLUETOOTH_LE_LL, "Bluetooth Low Energy air interface"),
3288*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(NETLINK, "Linux netlink"),
3289*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(BLUETOOTH_LINUX_MONITOR, "Bluetooth Linux Monitor"),
3290*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(BLUETOOTH_BREDR_BB, "Bluetooth Basic Rate/Enhanced Data Rate baseband packets"),
3291*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(BLUETOOTH_LE_LL_WITH_PHDR, "Bluetooth Low Energy air interface with pseudo-header"),
3292*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(PROFIBUS_DL, "PROFIBUS data link layer"),
3293*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(PKTAP, "Apple DLT_PKTAP"),
3294*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(EPON, "Ethernet with 802.3 Clause 65 EPON preamble"),
3295*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(IPMI_HPM_2, "IPMI trace packets"),
3296*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(ZWAVE_R1_R2, "Z-Wave RF profile R1 and R2 packets"),
3297*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(ZWAVE_R3, "Z-Wave RF profile R3 packets"),
3298*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(WATTSTOPPER_DLM, "WattStopper Digital Lighting Management (DLM) and Legrand Nitoo Open protocol"),
3299*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(ISO_14443, "ISO 14443 messages"),
3300*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(RDS, "IEC 62106 Radio Data System groups"),
3301*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(USB_DARWIN, "USB with Darwin header"),
3302*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(OPENFLOW, "OpenBSD DLT_OPENFLOW"),
3303*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(SDLC, "IBM SDLC frames"),
3304*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(TI_LLN_SNIFFER, "TI LLN sniffer frames"),
3305*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(VSOCK, "Linux vsock"),
3306*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(NORDIC_BLE, "Nordic Semiconductor Bluetooth LE sniffer frames"),
3307*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(DOCSIS31_XRA31, "Excentis XRA-31 DOCSIS 3.1 RF sniffer frames"),
3308*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(ETHERNET_MPACKET, "802.3br mPackets"),
3309*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(DISPLAYPORT_AUX, "DisplayPort AUX channel monitoring data"),
3310*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(LINUX_SLL2, "Linux cooked v2"),
3311*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(OPENVIZSLA, "OpenVizsla USB"),
3312*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(EBHSCR, "Elektrobit High Speed Capture and Replay (EBHSCR)"),
3313*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(VPP_DISPATCH, "VPP graph dispatch tracer"),
3314*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(DSA_TAG_BRCM, "Broadcom tag"),
3315*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(DSA_TAG_BRCM_PREPEND, "Broadcom tag (prepended)"),
3316*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(IEEE802_15_4_TAP, "IEEE 802.15.4 with pseudo-header"),
3317*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(DSA_TAG_DSA, "Marvell DSA"),
3318*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(DSA_TAG_EDSA, "Marvell EDSA"),
3319*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(ELEE, "ELEE lawful intercept packets"),
3320*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(Z_WAVE_SERIAL, "Z-Wave serial frames between host and chip"),
3321*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(USB_2_0, "USB 2.0/1.1/1.0 as transmitted over the cable"),
3322*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE(ATSC_ALP, "ATSC Link-Layer Protocol packets"),
3323*8b26181fSAndroid Build Coastguard Worker 	DLT_CHOICE_SENTINEL
3324*8b26181fSAndroid Build Coastguard Worker };
3325*8b26181fSAndroid Build Coastguard Worker 
3326*8b26181fSAndroid Build Coastguard Worker int
3327*8b26181fSAndroid Build Coastguard Worker pcap_datalink_name_to_val(const char *name)
3328*8b26181fSAndroid Build Coastguard Worker {
3329*8b26181fSAndroid Build Coastguard Worker 	int i;
3330*8b26181fSAndroid Build Coastguard Worker 
3331*8b26181fSAndroid Build Coastguard Worker 	for (i = 0; dlt_choices[i].name != NULL; i++) {
3332*8b26181fSAndroid Build Coastguard Worker 		if (pcap_strcasecmp(dlt_choices[i].name, name) == 0)
3333*8b26181fSAndroid Build Coastguard Worker 			return (dlt_choices[i].dlt);
3334*8b26181fSAndroid Build Coastguard Worker 	}
3335*8b26181fSAndroid Build Coastguard Worker 	return (-1);
3336*8b26181fSAndroid Build Coastguard Worker }
3337*8b26181fSAndroid Build Coastguard Worker 
3338*8b26181fSAndroid Build Coastguard Worker const char *
3339*8b26181fSAndroid Build Coastguard Worker pcap_datalink_val_to_name(int dlt)
3340*8b26181fSAndroid Build Coastguard Worker {
3341*8b26181fSAndroid Build Coastguard Worker 	int i;
3342*8b26181fSAndroid Build Coastguard Worker 
3343*8b26181fSAndroid Build Coastguard Worker 	for (i = 0; dlt_choices[i].name != NULL; i++) {
3344*8b26181fSAndroid Build Coastguard Worker 		if (dlt_choices[i].dlt == dlt)
3345*8b26181fSAndroid Build Coastguard Worker 			return (dlt_choices[i].name);
3346*8b26181fSAndroid Build Coastguard Worker 	}
3347*8b26181fSAndroid Build Coastguard Worker 	return (NULL);
3348*8b26181fSAndroid Build Coastguard Worker }
3349*8b26181fSAndroid Build Coastguard Worker 
3350*8b26181fSAndroid Build Coastguard Worker const char *
3351*8b26181fSAndroid Build Coastguard Worker pcap_datalink_val_to_description(int dlt)
3352*8b26181fSAndroid Build Coastguard Worker {
3353*8b26181fSAndroid Build Coastguard Worker 	int i;
3354*8b26181fSAndroid Build Coastguard Worker 
3355*8b26181fSAndroid Build Coastguard Worker 	for (i = 0; dlt_choices[i].name != NULL; i++) {
3356*8b26181fSAndroid Build Coastguard Worker 		if (dlt_choices[i].dlt == dlt)
3357*8b26181fSAndroid Build Coastguard Worker 			return (dlt_choices[i].description);
3358*8b26181fSAndroid Build Coastguard Worker 	}
3359*8b26181fSAndroid Build Coastguard Worker 	return (NULL);
3360*8b26181fSAndroid Build Coastguard Worker }
3361*8b26181fSAndroid Build Coastguard Worker 
3362*8b26181fSAndroid Build Coastguard Worker const char *
3363*8b26181fSAndroid Build Coastguard Worker pcap_datalink_val_to_description_or_dlt(int dlt)
3364*8b26181fSAndroid Build Coastguard Worker {
3365*8b26181fSAndroid Build Coastguard Worker         static char unkbuf[40];
3366*8b26181fSAndroid Build Coastguard Worker         const char *description;
3367*8b26181fSAndroid Build Coastguard Worker 
3368*8b26181fSAndroid Build Coastguard Worker         description = pcap_datalink_val_to_description(dlt);
3369*8b26181fSAndroid Build Coastguard Worker         if (description != NULL) {
3370*8b26181fSAndroid Build Coastguard Worker                 return description;
3371*8b26181fSAndroid Build Coastguard Worker         } else {
3372*8b26181fSAndroid Build Coastguard Worker                 (void)snprintf(unkbuf, sizeof(unkbuf), "DLT %d", dlt);
3373*8b26181fSAndroid Build Coastguard Worker                 return unkbuf;
3374*8b26181fSAndroid Build Coastguard Worker         }
3375*8b26181fSAndroid Build Coastguard Worker }
3376*8b26181fSAndroid Build Coastguard Worker 
3377*8b26181fSAndroid Build Coastguard Worker struct tstamp_type_choice {
3378*8b26181fSAndroid Build Coastguard Worker 	const char *name;
3379*8b26181fSAndroid Build Coastguard Worker 	const char *description;
3380*8b26181fSAndroid Build Coastguard Worker 	int	type;
3381*8b26181fSAndroid Build Coastguard Worker };
3382*8b26181fSAndroid Build Coastguard Worker 
3383*8b26181fSAndroid Build Coastguard Worker static struct tstamp_type_choice tstamp_type_choices[] = {
3384*8b26181fSAndroid Build Coastguard Worker 	{ "host", "Host", PCAP_TSTAMP_HOST },
3385*8b26181fSAndroid Build Coastguard Worker 	{ "host_lowprec", "Host, low precision", PCAP_TSTAMP_HOST_LOWPREC },
3386*8b26181fSAndroid Build Coastguard Worker 	{ "host_hiprec", "Host, high precision", PCAP_TSTAMP_HOST_HIPREC },
3387*8b26181fSAndroid Build Coastguard Worker 	{ "adapter", "Adapter", PCAP_TSTAMP_ADAPTER },
3388*8b26181fSAndroid Build Coastguard Worker 	{ "adapter_unsynced", "Adapter, not synced with system time", PCAP_TSTAMP_ADAPTER_UNSYNCED },
3389*8b26181fSAndroid Build Coastguard Worker 	{ "host_hiprec_unsynced", "Host, high precision, not synced with system time", PCAP_TSTAMP_HOST_HIPREC_UNSYNCED },
3390*8b26181fSAndroid Build Coastguard Worker 	{ NULL, NULL, 0 }
3391*8b26181fSAndroid Build Coastguard Worker };
3392*8b26181fSAndroid Build Coastguard Worker 
3393*8b26181fSAndroid Build Coastguard Worker int
3394*8b26181fSAndroid Build Coastguard Worker pcap_tstamp_type_name_to_val(const char *name)
3395*8b26181fSAndroid Build Coastguard Worker {
3396*8b26181fSAndroid Build Coastguard Worker 	int i;
3397*8b26181fSAndroid Build Coastguard Worker 
3398*8b26181fSAndroid Build Coastguard Worker 	for (i = 0; tstamp_type_choices[i].name != NULL; i++) {
3399*8b26181fSAndroid Build Coastguard Worker 		if (pcap_strcasecmp(tstamp_type_choices[i].name, name) == 0)
3400*8b26181fSAndroid Build Coastguard Worker 			return (tstamp_type_choices[i].type);
3401*8b26181fSAndroid Build Coastguard Worker 	}
3402*8b26181fSAndroid Build Coastguard Worker 	return (PCAP_ERROR);
3403*8b26181fSAndroid Build Coastguard Worker }
3404*8b26181fSAndroid Build Coastguard Worker 
3405*8b26181fSAndroid Build Coastguard Worker const char *
3406*8b26181fSAndroid Build Coastguard Worker pcap_tstamp_type_val_to_name(int tstamp_type)
3407*8b26181fSAndroid Build Coastguard Worker {
3408*8b26181fSAndroid Build Coastguard Worker 	int i;
3409*8b26181fSAndroid Build Coastguard Worker 
3410*8b26181fSAndroid Build Coastguard Worker 	for (i = 0; tstamp_type_choices[i].name != NULL; i++) {
3411*8b26181fSAndroid Build Coastguard Worker 		if (tstamp_type_choices[i].type == tstamp_type)
3412*8b26181fSAndroid Build Coastguard Worker 			return (tstamp_type_choices[i].name);
3413*8b26181fSAndroid Build Coastguard Worker 	}
3414*8b26181fSAndroid Build Coastguard Worker 	return (NULL);
3415*8b26181fSAndroid Build Coastguard Worker }
3416*8b26181fSAndroid Build Coastguard Worker 
3417*8b26181fSAndroid Build Coastguard Worker const char *
3418*8b26181fSAndroid Build Coastguard Worker pcap_tstamp_type_val_to_description(int tstamp_type)
3419*8b26181fSAndroid Build Coastguard Worker {
3420*8b26181fSAndroid Build Coastguard Worker 	int i;
3421*8b26181fSAndroid Build Coastguard Worker 
3422*8b26181fSAndroid Build Coastguard Worker 	for (i = 0; tstamp_type_choices[i].name != NULL; i++) {
3423*8b26181fSAndroid Build Coastguard Worker 		if (tstamp_type_choices[i].type == tstamp_type)
3424*8b26181fSAndroid Build Coastguard Worker 			return (tstamp_type_choices[i].description);
3425*8b26181fSAndroid Build Coastguard Worker 	}
3426*8b26181fSAndroid Build Coastguard Worker 	return (NULL);
3427*8b26181fSAndroid Build Coastguard Worker }
3428*8b26181fSAndroid Build Coastguard Worker 
3429*8b26181fSAndroid Build Coastguard Worker int
3430*8b26181fSAndroid Build Coastguard Worker pcap_snapshot(pcap_t *p)
3431*8b26181fSAndroid Build Coastguard Worker {
3432*8b26181fSAndroid Build Coastguard Worker 	if (!p->activated)
3433*8b26181fSAndroid Build Coastguard Worker 		return (PCAP_ERROR_NOT_ACTIVATED);
3434*8b26181fSAndroid Build Coastguard Worker 	return (p->snapshot);
3435*8b26181fSAndroid Build Coastguard Worker }
3436*8b26181fSAndroid Build Coastguard Worker 
3437*8b26181fSAndroid Build Coastguard Worker int
3438*8b26181fSAndroid Build Coastguard Worker pcap_is_swapped(pcap_t *p)
3439*8b26181fSAndroid Build Coastguard Worker {
3440*8b26181fSAndroid Build Coastguard Worker 	if (!p->activated)
3441*8b26181fSAndroid Build Coastguard Worker 		return (PCAP_ERROR_NOT_ACTIVATED);
3442*8b26181fSAndroid Build Coastguard Worker 	return (p->swapped);
3443*8b26181fSAndroid Build Coastguard Worker }
3444*8b26181fSAndroid Build Coastguard Worker 
3445*8b26181fSAndroid Build Coastguard Worker int
3446*8b26181fSAndroid Build Coastguard Worker pcap_major_version(pcap_t *p)
3447*8b26181fSAndroid Build Coastguard Worker {
3448*8b26181fSAndroid Build Coastguard Worker 	if (!p->activated)
3449*8b26181fSAndroid Build Coastguard Worker 		return (PCAP_ERROR_NOT_ACTIVATED);
3450*8b26181fSAndroid Build Coastguard Worker 	return (p->version_major);
3451*8b26181fSAndroid Build Coastguard Worker }
3452*8b26181fSAndroid Build Coastguard Worker 
3453*8b26181fSAndroid Build Coastguard Worker int
3454*8b26181fSAndroid Build Coastguard Worker pcap_minor_version(pcap_t *p)
3455*8b26181fSAndroid Build Coastguard Worker {
3456*8b26181fSAndroid Build Coastguard Worker 	if (!p->activated)
3457*8b26181fSAndroid Build Coastguard Worker 		return (PCAP_ERROR_NOT_ACTIVATED);
3458*8b26181fSAndroid Build Coastguard Worker 	return (p->version_minor);
3459*8b26181fSAndroid Build Coastguard Worker }
3460*8b26181fSAndroid Build Coastguard Worker 
3461*8b26181fSAndroid Build Coastguard Worker int
3462*8b26181fSAndroid Build Coastguard Worker pcap_bufsize(pcap_t *p)
3463*8b26181fSAndroid Build Coastguard Worker {
3464*8b26181fSAndroid Build Coastguard Worker 	if (!p->activated)
3465*8b26181fSAndroid Build Coastguard Worker 		return (PCAP_ERROR_NOT_ACTIVATED);
3466*8b26181fSAndroid Build Coastguard Worker 	return (p->bufsize);
3467*8b26181fSAndroid Build Coastguard Worker }
3468*8b26181fSAndroid Build Coastguard Worker 
3469*8b26181fSAndroid Build Coastguard Worker FILE *
3470*8b26181fSAndroid Build Coastguard Worker pcap_file(pcap_t *p)
3471*8b26181fSAndroid Build Coastguard Worker {
3472*8b26181fSAndroid Build Coastguard Worker 	return (p->rfile);
3473*8b26181fSAndroid Build Coastguard Worker }
3474*8b26181fSAndroid Build Coastguard Worker 
3475*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
3476*8b26181fSAndroid Build Coastguard Worker int
3477*8b26181fSAndroid Build Coastguard Worker pcap_fileno(pcap_t *p)
3478*8b26181fSAndroid Build Coastguard Worker {
3479*8b26181fSAndroid Build Coastguard Worker 	if (p->handle != INVALID_HANDLE_VALUE) {
3480*8b26181fSAndroid Build Coastguard Worker 		/*
3481*8b26181fSAndroid Build Coastguard Worker 		 * This is a bogus and now-deprecated API; we
3482*8b26181fSAndroid Build Coastguard Worker 		 * squelch the narrowing warning for the cast
3483*8b26181fSAndroid Build Coastguard Worker 		 * from HANDLE to intptr_t.  If Windows programmmers
3484*8b26181fSAndroid Build Coastguard Worker 		 * need to get at the HANDLE for a pcap_t, *if*
3485*8b26181fSAndroid Build Coastguard Worker 		 * there is one, they should request such a
3486*8b26181fSAndroid Build Coastguard Worker 		 * routine (and be prepared for it to return
3487*8b26181fSAndroid Build Coastguard Worker 		 * INVALID_HANDLE_VALUE).
3488*8b26181fSAndroid Build Coastguard Worker 		 */
3489*8b26181fSAndroid Build Coastguard Worker DIAG_OFF_NARROWING
3490*8b26181fSAndroid Build Coastguard Worker 		return ((int)(intptr_t)p->handle);
3491*8b26181fSAndroid Build Coastguard Worker DIAG_ON_NARROWING
3492*8b26181fSAndroid Build Coastguard Worker 	} else
3493*8b26181fSAndroid Build Coastguard Worker 		return (PCAP_ERROR);
3494*8b26181fSAndroid Build Coastguard Worker }
3495*8b26181fSAndroid Build Coastguard Worker #else /* _WIN32 */
3496*8b26181fSAndroid Build Coastguard Worker int
3497*8b26181fSAndroid Build Coastguard Worker pcap_fileno(pcap_t *p)
3498*8b26181fSAndroid Build Coastguard Worker {
3499*8b26181fSAndroid Build Coastguard Worker 	return (p->fd);
3500*8b26181fSAndroid Build Coastguard Worker }
3501*8b26181fSAndroid Build Coastguard Worker #endif /* _WIN32 */
3502*8b26181fSAndroid Build Coastguard Worker 
3503*8b26181fSAndroid Build Coastguard Worker #if !defined(_WIN32) && !defined(MSDOS)
3504*8b26181fSAndroid Build Coastguard Worker int
3505*8b26181fSAndroid Build Coastguard Worker pcap_get_selectable_fd(pcap_t *p)
3506*8b26181fSAndroid Build Coastguard Worker {
3507*8b26181fSAndroid Build Coastguard Worker 	return (p->selectable_fd);
3508*8b26181fSAndroid Build Coastguard Worker }
3509*8b26181fSAndroid Build Coastguard Worker 
3510*8b26181fSAndroid Build Coastguard Worker const struct timeval *
3511*8b26181fSAndroid Build Coastguard Worker pcap_get_required_select_timeout(pcap_t *p)
3512*8b26181fSAndroid Build Coastguard Worker {
3513*8b26181fSAndroid Build Coastguard Worker 	return (p->required_select_timeout);
3514*8b26181fSAndroid Build Coastguard Worker }
3515*8b26181fSAndroid Build Coastguard Worker #endif
3516*8b26181fSAndroid Build Coastguard Worker 
3517*8b26181fSAndroid Build Coastguard Worker void
3518*8b26181fSAndroid Build Coastguard Worker pcap_perror(pcap_t *p, const char *prefix)
3519*8b26181fSAndroid Build Coastguard Worker {
3520*8b26181fSAndroid Build Coastguard Worker 	fprintf(stderr, "%s: %s\n", prefix, p->errbuf);
3521*8b26181fSAndroid Build Coastguard Worker }
3522*8b26181fSAndroid Build Coastguard Worker 
3523*8b26181fSAndroid Build Coastguard Worker char *
3524*8b26181fSAndroid Build Coastguard Worker pcap_geterr(pcap_t *p)
3525*8b26181fSAndroid Build Coastguard Worker {
3526*8b26181fSAndroid Build Coastguard Worker 	return (p->errbuf);
3527*8b26181fSAndroid Build Coastguard Worker }
3528*8b26181fSAndroid Build Coastguard Worker 
3529*8b26181fSAndroid Build Coastguard Worker int
3530*8b26181fSAndroid Build Coastguard Worker pcap_getnonblock(pcap_t *p, char *errbuf)
3531*8b26181fSAndroid Build Coastguard Worker {
3532*8b26181fSAndroid Build Coastguard Worker 	int ret;
3533*8b26181fSAndroid Build Coastguard Worker 
3534*8b26181fSAndroid Build Coastguard Worker 	ret = p->getnonblock_op(p);
3535*8b26181fSAndroid Build Coastguard Worker 	if (ret == -1) {
3536*8b26181fSAndroid Build Coastguard Worker 		/*
3537*8b26181fSAndroid Build Coastguard Worker 		 * The get nonblock operation sets p->errbuf; this
3538*8b26181fSAndroid Build Coastguard Worker 		 * function *shouldn't* have had a separate errbuf
3539*8b26181fSAndroid Build Coastguard Worker 		 * argument, as it didn't need one, but I goofed
3540*8b26181fSAndroid Build Coastguard Worker 		 * when adding it.
3541*8b26181fSAndroid Build Coastguard Worker 		 *
3542*8b26181fSAndroid Build Coastguard Worker 		 * We copy the error message to errbuf, so callers
3543*8b26181fSAndroid Build Coastguard Worker 		 * can find it in either place.
3544*8b26181fSAndroid Build Coastguard Worker 		 */
3545*8b26181fSAndroid Build Coastguard Worker 		pcap_strlcpy(errbuf, p->errbuf, PCAP_ERRBUF_SIZE);
3546*8b26181fSAndroid Build Coastguard Worker 	}
3547*8b26181fSAndroid Build Coastguard Worker 	return (ret);
3548*8b26181fSAndroid Build Coastguard Worker }
3549*8b26181fSAndroid Build Coastguard Worker 
3550*8b26181fSAndroid Build Coastguard Worker /*
3551*8b26181fSAndroid Build Coastguard Worker  * Get the current non-blocking mode setting, under the assumption that
3552*8b26181fSAndroid Build Coastguard Worker  * it's just the standard POSIX non-blocking flag.
3553*8b26181fSAndroid Build Coastguard Worker  */
3554*8b26181fSAndroid Build Coastguard Worker #if !defined(_WIN32) && !defined(MSDOS)
3555*8b26181fSAndroid Build Coastguard Worker int
3556*8b26181fSAndroid Build Coastguard Worker pcap_getnonblock_fd(pcap_t *p)
3557*8b26181fSAndroid Build Coastguard Worker {
3558*8b26181fSAndroid Build Coastguard Worker 	int fdflags;
3559*8b26181fSAndroid Build Coastguard Worker 
3560*8b26181fSAndroid Build Coastguard Worker 	fdflags = fcntl(p->fd, F_GETFL, 0);
3561*8b26181fSAndroid Build Coastguard Worker 	if (fdflags == -1) {
3562*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
3563*8b26181fSAndroid Build Coastguard Worker 		    errno, "F_GETFL");
3564*8b26181fSAndroid Build Coastguard Worker 		return (-1);
3565*8b26181fSAndroid Build Coastguard Worker 	}
3566*8b26181fSAndroid Build Coastguard Worker 	if (fdflags & O_NONBLOCK)
3567*8b26181fSAndroid Build Coastguard Worker 		return (1);
3568*8b26181fSAndroid Build Coastguard Worker 	else
3569*8b26181fSAndroid Build Coastguard Worker 		return (0);
3570*8b26181fSAndroid Build Coastguard Worker }
3571*8b26181fSAndroid Build Coastguard Worker #endif
3572*8b26181fSAndroid Build Coastguard Worker 
3573*8b26181fSAndroid Build Coastguard Worker int
3574*8b26181fSAndroid Build Coastguard Worker pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf)
3575*8b26181fSAndroid Build Coastguard Worker {
3576*8b26181fSAndroid Build Coastguard Worker 	int ret;
3577*8b26181fSAndroid Build Coastguard Worker 
3578*8b26181fSAndroid Build Coastguard Worker 	ret = p->setnonblock_op(p, nonblock);
3579*8b26181fSAndroid Build Coastguard Worker 	if (ret == -1) {
3580*8b26181fSAndroid Build Coastguard Worker 		/*
3581*8b26181fSAndroid Build Coastguard Worker 		 * The set nonblock operation sets p->errbuf; this
3582*8b26181fSAndroid Build Coastguard Worker 		 * function *shouldn't* have had a separate errbuf
3583*8b26181fSAndroid Build Coastguard Worker 		 * argument, as it didn't need one, but I goofed
3584*8b26181fSAndroid Build Coastguard Worker 		 * when adding it.
3585*8b26181fSAndroid Build Coastguard Worker 		 *
3586*8b26181fSAndroid Build Coastguard Worker 		 * We copy the error message to errbuf, so callers
3587*8b26181fSAndroid Build Coastguard Worker 		 * can find it in either place.
3588*8b26181fSAndroid Build Coastguard Worker 		 */
3589*8b26181fSAndroid Build Coastguard Worker 		pcap_strlcpy(errbuf, p->errbuf, PCAP_ERRBUF_SIZE);
3590*8b26181fSAndroid Build Coastguard Worker 	}
3591*8b26181fSAndroid Build Coastguard Worker 	return (ret);
3592*8b26181fSAndroid Build Coastguard Worker }
3593*8b26181fSAndroid Build Coastguard Worker 
3594*8b26181fSAndroid Build Coastguard Worker #if !defined(_WIN32) && !defined(MSDOS)
3595*8b26181fSAndroid Build Coastguard Worker /*
3596*8b26181fSAndroid Build Coastguard Worker  * Set non-blocking mode, under the assumption that it's just the
3597*8b26181fSAndroid Build Coastguard Worker  * standard POSIX non-blocking flag.  (This can be called by the
3598*8b26181fSAndroid Build Coastguard Worker  * per-platform non-blocking-mode routine if that routine also
3599*8b26181fSAndroid Build Coastguard Worker  * needs to do some additional work.)
3600*8b26181fSAndroid Build Coastguard Worker  */
3601*8b26181fSAndroid Build Coastguard Worker int
3602*8b26181fSAndroid Build Coastguard Worker pcap_setnonblock_fd(pcap_t *p, int nonblock)
3603*8b26181fSAndroid Build Coastguard Worker {
3604*8b26181fSAndroid Build Coastguard Worker 	int fdflags;
3605*8b26181fSAndroid Build Coastguard Worker 
3606*8b26181fSAndroid Build Coastguard Worker 	fdflags = fcntl(p->fd, F_GETFL, 0);
3607*8b26181fSAndroid Build Coastguard Worker 	if (fdflags == -1) {
3608*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
3609*8b26181fSAndroid Build Coastguard Worker 		    errno, "F_GETFL");
3610*8b26181fSAndroid Build Coastguard Worker 		return (-1);
3611*8b26181fSAndroid Build Coastguard Worker 	}
3612*8b26181fSAndroid Build Coastguard Worker 	if (nonblock)
3613*8b26181fSAndroid Build Coastguard Worker 		fdflags |= O_NONBLOCK;
3614*8b26181fSAndroid Build Coastguard Worker 	else
3615*8b26181fSAndroid Build Coastguard Worker 		fdflags &= ~O_NONBLOCK;
3616*8b26181fSAndroid Build Coastguard Worker 	if (fcntl(p->fd, F_SETFL, fdflags) == -1) {
3617*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
3618*8b26181fSAndroid Build Coastguard Worker 		    errno, "F_SETFL");
3619*8b26181fSAndroid Build Coastguard Worker 		return (-1);
3620*8b26181fSAndroid Build Coastguard Worker 	}
3621*8b26181fSAndroid Build Coastguard Worker 	return (0);
3622*8b26181fSAndroid Build Coastguard Worker }
3623*8b26181fSAndroid Build Coastguard Worker #endif
3624*8b26181fSAndroid Build Coastguard Worker 
3625*8b26181fSAndroid Build Coastguard Worker /*
3626*8b26181fSAndroid Build Coastguard Worker  * Generate error strings for PCAP_ERROR_ and PCAP_WARNING_ values.
3627*8b26181fSAndroid Build Coastguard Worker  */
3628*8b26181fSAndroid Build Coastguard Worker const char *
3629*8b26181fSAndroid Build Coastguard Worker pcap_statustostr(int errnum)
3630*8b26181fSAndroid Build Coastguard Worker {
3631*8b26181fSAndroid Build Coastguard Worker 	static char ebuf[15+10+1];
3632*8b26181fSAndroid Build Coastguard Worker 
3633*8b26181fSAndroid Build Coastguard Worker 	switch (errnum) {
3634*8b26181fSAndroid Build Coastguard Worker 
3635*8b26181fSAndroid Build Coastguard Worker 	case PCAP_WARNING:
3636*8b26181fSAndroid Build Coastguard Worker 		return("Generic warning");
3637*8b26181fSAndroid Build Coastguard Worker 
3638*8b26181fSAndroid Build Coastguard Worker 	case PCAP_WARNING_TSTAMP_TYPE_NOTSUP:
3639*8b26181fSAndroid Build Coastguard Worker 		return ("That type of time stamp is not supported by that device");
3640*8b26181fSAndroid Build Coastguard Worker 
3641*8b26181fSAndroid Build Coastguard Worker 	case PCAP_WARNING_PROMISC_NOTSUP:
3642*8b26181fSAndroid Build Coastguard Worker 		return ("That device doesn't support promiscuous mode");
3643*8b26181fSAndroid Build Coastguard Worker 
3644*8b26181fSAndroid Build Coastguard Worker 	case PCAP_ERROR:
3645*8b26181fSAndroid Build Coastguard Worker 		return("Generic error");
3646*8b26181fSAndroid Build Coastguard Worker 
3647*8b26181fSAndroid Build Coastguard Worker 	case PCAP_ERROR_BREAK:
3648*8b26181fSAndroid Build Coastguard Worker 		return("Loop terminated by pcap_breakloop");
3649*8b26181fSAndroid Build Coastguard Worker 
3650*8b26181fSAndroid Build Coastguard Worker 	case PCAP_ERROR_NOT_ACTIVATED:
3651*8b26181fSAndroid Build Coastguard Worker 		return("The pcap_t has not been activated");
3652*8b26181fSAndroid Build Coastguard Worker 
3653*8b26181fSAndroid Build Coastguard Worker 	case PCAP_ERROR_ACTIVATED:
3654*8b26181fSAndroid Build Coastguard Worker 		return ("The setting can't be changed after the pcap_t is activated");
3655*8b26181fSAndroid Build Coastguard Worker 
3656*8b26181fSAndroid Build Coastguard Worker 	case PCAP_ERROR_NO_SUCH_DEVICE:
3657*8b26181fSAndroid Build Coastguard Worker 		return ("No such device exists");
3658*8b26181fSAndroid Build Coastguard Worker 
3659*8b26181fSAndroid Build Coastguard Worker 	case PCAP_ERROR_RFMON_NOTSUP:
3660*8b26181fSAndroid Build Coastguard Worker 		return ("That device doesn't support monitor mode");
3661*8b26181fSAndroid Build Coastguard Worker 
3662*8b26181fSAndroid Build Coastguard Worker 	case PCAP_ERROR_NOT_RFMON:
3663*8b26181fSAndroid Build Coastguard Worker 		return ("That operation is supported only in monitor mode");
3664*8b26181fSAndroid Build Coastguard Worker 
3665*8b26181fSAndroid Build Coastguard Worker 	case PCAP_ERROR_PERM_DENIED:
3666*8b26181fSAndroid Build Coastguard Worker 		return ("You don't have permission to perform this capture on that device");
3667*8b26181fSAndroid Build Coastguard Worker 
3668*8b26181fSAndroid Build Coastguard Worker 	case PCAP_ERROR_IFACE_NOT_UP:
3669*8b26181fSAndroid Build Coastguard Worker 		return ("That device is not up");
3670*8b26181fSAndroid Build Coastguard Worker 
3671*8b26181fSAndroid Build Coastguard Worker 	case PCAP_ERROR_CANTSET_TSTAMP_TYPE:
3672*8b26181fSAndroid Build Coastguard Worker 		return ("That device doesn't support setting the time stamp type");
3673*8b26181fSAndroid Build Coastguard Worker 
3674*8b26181fSAndroid Build Coastguard Worker 	case PCAP_ERROR_PROMISC_PERM_DENIED:
3675*8b26181fSAndroid Build Coastguard Worker 		return ("You don't have permission to capture in promiscuous mode on that device");
3676*8b26181fSAndroid Build Coastguard Worker 
3677*8b26181fSAndroid Build Coastguard Worker 	case PCAP_ERROR_TSTAMP_PRECISION_NOTSUP:
3678*8b26181fSAndroid Build Coastguard Worker 		return ("That device doesn't support that time stamp precision");
3679*8b26181fSAndroid Build Coastguard Worker 	}
3680*8b26181fSAndroid Build Coastguard Worker 	(void)snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum);
3681*8b26181fSAndroid Build Coastguard Worker 	return(ebuf);
3682*8b26181fSAndroid Build Coastguard Worker }
3683*8b26181fSAndroid Build Coastguard Worker 
3684*8b26181fSAndroid Build Coastguard Worker /*
3685*8b26181fSAndroid Build Coastguard Worker  * Not all systems have strerror().
3686*8b26181fSAndroid Build Coastguard Worker  */
3687*8b26181fSAndroid Build Coastguard Worker const char *
3688*8b26181fSAndroid Build Coastguard Worker pcap_strerror(int errnum)
3689*8b26181fSAndroid Build Coastguard Worker {
3690*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_STRERROR
3691*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
3692*8b26181fSAndroid Build Coastguard Worker 	static char errbuf[PCAP_ERRBUF_SIZE];
3693*8b26181fSAndroid Build Coastguard Worker 	errno_t err = strerror_s(errbuf, PCAP_ERRBUF_SIZE, errnum);
3694*8b26181fSAndroid Build Coastguard Worker 
3695*8b26181fSAndroid Build Coastguard Worker 	if (err != 0) /* err = 0 if successful */
3696*8b26181fSAndroid Build Coastguard Worker 		pcap_strlcpy(errbuf, "strerror_s() error", PCAP_ERRBUF_SIZE);
3697*8b26181fSAndroid Build Coastguard Worker 	return (errbuf);
3698*8b26181fSAndroid Build Coastguard Worker #else
3699*8b26181fSAndroid Build Coastguard Worker 	return (strerror(errnum));
3700*8b26181fSAndroid Build Coastguard Worker #endif /* _WIN32 */
3701*8b26181fSAndroid Build Coastguard Worker #else
3702*8b26181fSAndroid Build Coastguard Worker 	extern int sys_nerr;
3703*8b26181fSAndroid Build Coastguard Worker 	extern const char *const sys_errlist[];
3704*8b26181fSAndroid Build Coastguard Worker 	static char errbuf[PCAP_ERRBUF_SIZE];
3705*8b26181fSAndroid Build Coastguard Worker 
3706*8b26181fSAndroid Build Coastguard Worker 	if ((unsigned int)errnum < sys_nerr)
3707*8b26181fSAndroid Build Coastguard Worker 		return ((char *)sys_errlist[errnum]);
3708*8b26181fSAndroid Build Coastguard Worker 	(void)snprintf(errbuf, sizeof errbuf, "Unknown error: %d", errnum);
3709*8b26181fSAndroid Build Coastguard Worker 	return (errbuf);
3710*8b26181fSAndroid Build Coastguard Worker #endif
3711*8b26181fSAndroid Build Coastguard Worker }
3712*8b26181fSAndroid Build Coastguard Worker 
3713*8b26181fSAndroid Build Coastguard Worker int
3714*8b26181fSAndroid Build Coastguard Worker pcap_setfilter(pcap_t *p, struct bpf_program *fp)
3715*8b26181fSAndroid Build Coastguard Worker {
3716*8b26181fSAndroid Build Coastguard Worker 	return (p->setfilter_op(p, fp));
3717*8b26181fSAndroid Build Coastguard Worker }
3718*8b26181fSAndroid Build Coastguard Worker 
3719*8b26181fSAndroid Build Coastguard Worker /*
3720*8b26181fSAndroid Build Coastguard Worker  * Set direction flag, which controls whether we accept only incoming
3721*8b26181fSAndroid Build Coastguard Worker  * packets, only outgoing packets, or both.
3722*8b26181fSAndroid Build Coastguard Worker  * Note that, depending on the platform, some or all direction arguments
3723*8b26181fSAndroid Build Coastguard Worker  * might not be supported.
3724*8b26181fSAndroid Build Coastguard Worker  */
3725*8b26181fSAndroid Build Coastguard Worker int
3726*8b26181fSAndroid Build Coastguard Worker pcap_setdirection(pcap_t *p, pcap_direction_t d)
3727*8b26181fSAndroid Build Coastguard Worker {
3728*8b26181fSAndroid Build Coastguard Worker 	if (p->setdirection_op == NULL) {
3729*8b26181fSAndroid Build Coastguard Worker 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
3730*8b26181fSAndroid Build Coastguard Worker 		    "Setting direction is not supported on this device");
3731*8b26181fSAndroid Build Coastguard Worker 		return (-1);
3732*8b26181fSAndroid Build Coastguard Worker 	} else {
3733*8b26181fSAndroid Build Coastguard Worker 		switch (d) {
3734*8b26181fSAndroid Build Coastguard Worker 
3735*8b26181fSAndroid Build Coastguard Worker 		case PCAP_D_IN:
3736*8b26181fSAndroid Build Coastguard Worker 		case PCAP_D_OUT:
3737*8b26181fSAndroid Build Coastguard Worker 		case PCAP_D_INOUT:
3738*8b26181fSAndroid Build Coastguard Worker 			/*
3739*8b26181fSAndroid Build Coastguard Worker 			 * Valid direction.
3740*8b26181fSAndroid Build Coastguard Worker 			 */
3741*8b26181fSAndroid Build Coastguard Worker 			return (p->setdirection_op(p, d));
3742*8b26181fSAndroid Build Coastguard Worker 
3743*8b26181fSAndroid Build Coastguard Worker 		default:
3744*8b26181fSAndroid Build Coastguard Worker 			/*
3745*8b26181fSAndroid Build Coastguard Worker 			 * Invalid direction.
3746*8b26181fSAndroid Build Coastguard Worker 			 */
3747*8b26181fSAndroid Build Coastguard Worker 			snprintf(p->errbuf, sizeof(p->errbuf),
3748*8b26181fSAndroid Build Coastguard Worker 			    "Invalid direction");
3749*8b26181fSAndroid Build Coastguard Worker 			return (-1);
3750*8b26181fSAndroid Build Coastguard Worker 		}
3751*8b26181fSAndroid Build Coastguard Worker 	}
3752*8b26181fSAndroid Build Coastguard Worker }
3753*8b26181fSAndroid Build Coastguard Worker 
3754*8b26181fSAndroid Build Coastguard Worker int
3755*8b26181fSAndroid Build Coastguard Worker pcap_stats(pcap_t *p, struct pcap_stat *ps)
3756*8b26181fSAndroid Build Coastguard Worker {
3757*8b26181fSAndroid Build Coastguard Worker 	return (p->stats_op(p, ps));
3758*8b26181fSAndroid Build Coastguard Worker }
3759*8b26181fSAndroid Build Coastguard Worker 
3760*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
3761*8b26181fSAndroid Build Coastguard Worker struct pcap_stat *
3762*8b26181fSAndroid Build Coastguard Worker pcap_stats_ex(pcap_t *p, int *pcap_stat_size)
3763*8b26181fSAndroid Build Coastguard Worker {
3764*8b26181fSAndroid Build Coastguard Worker 	return (p->stats_ex_op(p, pcap_stat_size));
3765*8b26181fSAndroid Build Coastguard Worker }
3766*8b26181fSAndroid Build Coastguard Worker 
3767*8b26181fSAndroid Build Coastguard Worker int
3768*8b26181fSAndroid Build Coastguard Worker pcap_setbuff(pcap_t *p, int dim)
3769*8b26181fSAndroid Build Coastguard Worker {
3770*8b26181fSAndroid Build Coastguard Worker 	return (p->setbuff_op(p, dim));
3771*8b26181fSAndroid Build Coastguard Worker }
3772*8b26181fSAndroid Build Coastguard Worker 
3773*8b26181fSAndroid Build Coastguard Worker int
3774*8b26181fSAndroid Build Coastguard Worker pcap_setmode(pcap_t *p, int mode)
3775*8b26181fSAndroid Build Coastguard Worker {
3776*8b26181fSAndroid Build Coastguard Worker 	return (p->setmode_op(p, mode));
3777*8b26181fSAndroid Build Coastguard Worker }
3778*8b26181fSAndroid Build Coastguard Worker 
3779*8b26181fSAndroid Build Coastguard Worker int
3780*8b26181fSAndroid Build Coastguard Worker pcap_setmintocopy(pcap_t *p, int size)
3781*8b26181fSAndroid Build Coastguard Worker {
3782*8b26181fSAndroid Build Coastguard Worker 	return (p->setmintocopy_op(p, size));
3783*8b26181fSAndroid Build Coastguard Worker }
3784*8b26181fSAndroid Build Coastguard Worker 
3785*8b26181fSAndroid Build Coastguard Worker HANDLE
3786*8b26181fSAndroid Build Coastguard Worker pcap_getevent(pcap_t *p)
3787*8b26181fSAndroid Build Coastguard Worker {
3788*8b26181fSAndroid Build Coastguard Worker 	return (p->getevent_op(p));
3789*8b26181fSAndroid Build Coastguard Worker }
3790*8b26181fSAndroid Build Coastguard Worker 
3791*8b26181fSAndroid Build Coastguard Worker int
3792*8b26181fSAndroid Build Coastguard Worker pcap_oid_get_request(pcap_t *p, bpf_u_int32 oid, void *data, size_t *lenp)
3793*8b26181fSAndroid Build Coastguard Worker {
3794*8b26181fSAndroid Build Coastguard Worker 	return (p->oid_get_request_op(p, oid, data, lenp));
3795*8b26181fSAndroid Build Coastguard Worker }
3796*8b26181fSAndroid Build Coastguard Worker 
3797*8b26181fSAndroid Build Coastguard Worker int
3798*8b26181fSAndroid Build Coastguard Worker pcap_oid_set_request(pcap_t *p, bpf_u_int32 oid, const void *data, size_t *lenp)
3799*8b26181fSAndroid Build Coastguard Worker {
3800*8b26181fSAndroid Build Coastguard Worker 	return (p->oid_set_request_op(p, oid, data, lenp));
3801*8b26181fSAndroid Build Coastguard Worker }
3802*8b26181fSAndroid Build Coastguard Worker 
3803*8b26181fSAndroid Build Coastguard Worker pcap_send_queue *
3804*8b26181fSAndroid Build Coastguard Worker pcap_sendqueue_alloc(u_int memsize)
3805*8b26181fSAndroid Build Coastguard Worker {
3806*8b26181fSAndroid Build Coastguard Worker 	pcap_send_queue *tqueue;
3807*8b26181fSAndroid Build Coastguard Worker 
3808*8b26181fSAndroid Build Coastguard Worker 	/* Allocate the queue */
3809*8b26181fSAndroid Build Coastguard Worker 	tqueue = (pcap_send_queue *)malloc(sizeof(pcap_send_queue));
3810*8b26181fSAndroid Build Coastguard Worker 	if (tqueue == NULL){
3811*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
3812*8b26181fSAndroid Build Coastguard Worker 	}
3813*8b26181fSAndroid Build Coastguard Worker 
3814*8b26181fSAndroid Build Coastguard Worker 	/* Allocate the buffer */
3815*8b26181fSAndroid Build Coastguard Worker 	tqueue->buffer = (char *)malloc(memsize);
3816*8b26181fSAndroid Build Coastguard Worker 	if (tqueue->buffer == NULL) {
3817*8b26181fSAndroid Build Coastguard Worker 		free(tqueue);
3818*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
3819*8b26181fSAndroid Build Coastguard Worker 	}
3820*8b26181fSAndroid Build Coastguard Worker 
3821*8b26181fSAndroid Build Coastguard Worker 	tqueue->maxlen = memsize;
3822*8b26181fSAndroid Build Coastguard Worker 	tqueue->len = 0;
3823*8b26181fSAndroid Build Coastguard Worker 
3824*8b26181fSAndroid Build Coastguard Worker 	return (tqueue);
3825*8b26181fSAndroid Build Coastguard Worker }
3826*8b26181fSAndroid Build Coastguard Worker 
3827*8b26181fSAndroid Build Coastguard Worker void
3828*8b26181fSAndroid Build Coastguard Worker pcap_sendqueue_destroy(pcap_send_queue *queue)
3829*8b26181fSAndroid Build Coastguard Worker {
3830*8b26181fSAndroid Build Coastguard Worker 	free(queue->buffer);
3831*8b26181fSAndroid Build Coastguard Worker 	free(queue);
3832*8b26181fSAndroid Build Coastguard Worker }
3833*8b26181fSAndroid Build Coastguard Worker 
3834*8b26181fSAndroid Build Coastguard Worker int
3835*8b26181fSAndroid Build Coastguard Worker pcap_sendqueue_queue(pcap_send_queue *queue, const struct pcap_pkthdr *pkt_header, const u_char *pkt_data)
3836*8b26181fSAndroid Build Coastguard Worker {
3837*8b26181fSAndroid Build Coastguard Worker 	if (queue->len + sizeof(struct pcap_pkthdr) + pkt_header->caplen > queue->maxlen){
3838*8b26181fSAndroid Build Coastguard Worker 		return (-1);
3839*8b26181fSAndroid Build Coastguard Worker 	}
3840*8b26181fSAndroid Build Coastguard Worker 
3841*8b26181fSAndroid Build Coastguard Worker 	/* Copy the pcap_pkthdr header*/
3842*8b26181fSAndroid Build Coastguard Worker 	memcpy(queue->buffer + queue->len, pkt_header, sizeof(struct pcap_pkthdr));
3843*8b26181fSAndroid Build Coastguard Worker 	queue->len += sizeof(struct pcap_pkthdr);
3844*8b26181fSAndroid Build Coastguard Worker 
3845*8b26181fSAndroid Build Coastguard Worker 	/* copy the packet */
3846*8b26181fSAndroid Build Coastguard Worker 	memcpy(queue->buffer + queue->len, pkt_data, pkt_header->caplen);
3847*8b26181fSAndroid Build Coastguard Worker 	queue->len += pkt_header->caplen;
3848*8b26181fSAndroid Build Coastguard Worker 
3849*8b26181fSAndroid Build Coastguard Worker 	return (0);
3850*8b26181fSAndroid Build Coastguard Worker }
3851*8b26181fSAndroid Build Coastguard Worker 
3852*8b26181fSAndroid Build Coastguard Worker u_int
3853*8b26181fSAndroid Build Coastguard Worker pcap_sendqueue_transmit(pcap_t *p, pcap_send_queue *queue, int sync)
3854*8b26181fSAndroid Build Coastguard Worker {
3855*8b26181fSAndroid Build Coastguard Worker 	return (p->sendqueue_transmit_op(p, queue, sync));
3856*8b26181fSAndroid Build Coastguard Worker }
3857*8b26181fSAndroid Build Coastguard Worker 
3858*8b26181fSAndroid Build Coastguard Worker int
3859*8b26181fSAndroid Build Coastguard Worker pcap_setuserbuffer(pcap_t *p, int size)
3860*8b26181fSAndroid Build Coastguard Worker {
3861*8b26181fSAndroid Build Coastguard Worker 	return (p->setuserbuffer_op(p, size));
3862*8b26181fSAndroid Build Coastguard Worker }
3863*8b26181fSAndroid Build Coastguard Worker 
3864*8b26181fSAndroid Build Coastguard Worker int
3865*8b26181fSAndroid Build Coastguard Worker pcap_live_dump(pcap_t *p, char *filename, int maxsize, int maxpacks)
3866*8b26181fSAndroid Build Coastguard Worker {
3867*8b26181fSAndroid Build Coastguard Worker 	return (p->live_dump_op(p, filename, maxsize, maxpacks));
3868*8b26181fSAndroid Build Coastguard Worker }
3869*8b26181fSAndroid Build Coastguard Worker 
3870*8b26181fSAndroid Build Coastguard Worker int
3871*8b26181fSAndroid Build Coastguard Worker pcap_live_dump_ended(pcap_t *p, int sync)
3872*8b26181fSAndroid Build Coastguard Worker {
3873*8b26181fSAndroid Build Coastguard Worker 	return (p->live_dump_ended_op(p, sync));
3874*8b26181fSAndroid Build Coastguard Worker }
3875*8b26181fSAndroid Build Coastguard Worker 
3876*8b26181fSAndroid Build Coastguard Worker PAirpcapHandle
3877*8b26181fSAndroid Build Coastguard Worker pcap_get_airpcap_handle(pcap_t *p)
3878*8b26181fSAndroid Build Coastguard Worker {
3879*8b26181fSAndroid Build Coastguard Worker 	PAirpcapHandle handle;
3880*8b26181fSAndroid Build Coastguard Worker 
3881*8b26181fSAndroid Build Coastguard Worker 	handle = p->get_airpcap_handle_op(p);
3882*8b26181fSAndroid Build Coastguard Worker 	if (handle == NULL) {
3883*8b26181fSAndroid Build Coastguard Worker 		(void)snprintf(p->errbuf, sizeof(p->errbuf),
3884*8b26181fSAndroid Build Coastguard Worker 		    "This isn't an AirPcap device");
3885*8b26181fSAndroid Build Coastguard Worker 	}
3886*8b26181fSAndroid Build Coastguard Worker 	return (handle);
3887*8b26181fSAndroid Build Coastguard Worker }
3888*8b26181fSAndroid Build Coastguard Worker #endif
3889*8b26181fSAndroid Build Coastguard Worker 
3890*8b26181fSAndroid Build Coastguard Worker /*
3891*8b26181fSAndroid Build Coastguard Worker  * On some platforms, we need to clean up promiscuous or monitor mode
3892*8b26181fSAndroid Build Coastguard Worker  * when we close a device - and we want that to happen even if the
3893*8b26181fSAndroid Build Coastguard Worker  * application just exits without explicitl closing devices.
3894*8b26181fSAndroid Build Coastguard Worker  * On those platforms, we need to register a "close all the pcaps"
3895*8b26181fSAndroid Build Coastguard Worker  * routine to be called when we exit, and need to maintain a list of
3896*8b26181fSAndroid Build Coastguard Worker  * pcaps that need to be closed to clean up modes.
3897*8b26181fSAndroid Build Coastguard Worker  *
3898*8b26181fSAndroid Build Coastguard Worker  * XXX - not thread-safe.
3899*8b26181fSAndroid Build Coastguard Worker  */
3900*8b26181fSAndroid Build Coastguard Worker 
3901*8b26181fSAndroid Build Coastguard Worker /*
3902*8b26181fSAndroid Build Coastguard Worker  * List of pcaps on which we've done something that needs to be
3903*8b26181fSAndroid Build Coastguard Worker  * cleaned up.
3904*8b26181fSAndroid Build Coastguard Worker  * If there are any such pcaps, we arrange to call "pcap_close_all()"
3905*8b26181fSAndroid Build Coastguard Worker  * when we exit, and have it close all of them.
3906*8b26181fSAndroid Build Coastguard Worker  */
3907*8b26181fSAndroid Build Coastguard Worker static struct pcap *pcaps_to_close;
3908*8b26181fSAndroid Build Coastguard Worker 
3909*8b26181fSAndroid Build Coastguard Worker /*
3910*8b26181fSAndroid Build Coastguard Worker  * TRUE if we've already called "atexit()" to cause "pcap_close_all()" to
3911*8b26181fSAndroid Build Coastguard Worker  * be called on exit.
3912*8b26181fSAndroid Build Coastguard Worker  */
3913*8b26181fSAndroid Build Coastguard Worker static int did_atexit;
3914*8b26181fSAndroid Build Coastguard Worker 
3915*8b26181fSAndroid Build Coastguard Worker static void
3916*8b26181fSAndroid Build Coastguard Worker pcap_close_all(void)
3917*8b26181fSAndroid Build Coastguard Worker {
3918*8b26181fSAndroid Build Coastguard Worker 	struct pcap *handle;
3919*8b26181fSAndroid Build Coastguard Worker 
3920*8b26181fSAndroid Build Coastguard Worker 	while ((handle = pcaps_to_close) != NULL) {
3921*8b26181fSAndroid Build Coastguard Worker 		pcap_close(handle);
3922*8b26181fSAndroid Build Coastguard Worker 
3923*8b26181fSAndroid Build Coastguard Worker 		/*
3924*8b26181fSAndroid Build Coastguard Worker 		 * If a pcap module adds a pcap_t to the "close all"
3925*8b26181fSAndroid Build Coastguard Worker 		 * list by calling pcap_add_to_pcaps_to_close(), it
3926*8b26181fSAndroid Build Coastguard Worker 		 * must have a cleanup routine that removes it from the
3927*8b26181fSAndroid Build Coastguard Worker 		 * list, by calling pcap_remove_from_pcaps_to_close(),
3928*8b26181fSAndroid Build Coastguard Worker 		 * and must make that cleanup routine the cleanup_op
3929*8b26181fSAndroid Build Coastguard Worker 		 * for the pcap_t.
3930*8b26181fSAndroid Build Coastguard Worker 		 *
3931*8b26181fSAndroid Build Coastguard Worker 		 * That means that, after pcap_close() - which calls
3932*8b26181fSAndroid Build Coastguard Worker 		 * the cleanup_op for the pcap_t - the pcap_t must
3933*8b26181fSAndroid Build Coastguard Worker 		 * have been removed from the list, so pcaps_to_close
3934*8b26181fSAndroid Build Coastguard Worker 		 * must not be equal to handle.
3935*8b26181fSAndroid Build Coastguard Worker 		 *
3936*8b26181fSAndroid Build Coastguard Worker 		 * We check for that, and abort if handle is still
3937*8b26181fSAndroid Build Coastguard Worker 		 * at the head of the list, to prevent infinite loops.
3938*8b26181fSAndroid Build Coastguard Worker 		 */
3939*8b26181fSAndroid Build Coastguard Worker 		if (pcaps_to_close == handle)
3940*8b26181fSAndroid Build Coastguard Worker 			abort();
3941*8b26181fSAndroid Build Coastguard Worker 	}
3942*8b26181fSAndroid Build Coastguard Worker }
3943*8b26181fSAndroid Build Coastguard Worker 
3944*8b26181fSAndroid Build Coastguard Worker int
3945*8b26181fSAndroid Build Coastguard Worker pcap_do_addexit(pcap_t *p)
3946*8b26181fSAndroid Build Coastguard Worker {
3947*8b26181fSAndroid Build Coastguard Worker 	/*
3948*8b26181fSAndroid Build Coastguard Worker 	 * If we haven't already done so, arrange to have
3949*8b26181fSAndroid Build Coastguard Worker 	 * "pcap_close_all()" called when we exit.
3950*8b26181fSAndroid Build Coastguard Worker 	 */
3951*8b26181fSAndroid Build Coastguard Worker 	if (!did_atexit) {
3952*8b26181fSAndroid Build Coastguard Worker 		if (atexit(pcap_close_all) != 0) {
3953*8b26181fSAndroid Build Coastguard Worker 			/*
3954*8b26181fSAndroid Build Coastguard Worker 			 * "atexit()" failed; let our caller know.
3955*8b26181fSAndroid Build Coastguard Worker 			 */
3956*8b26181fSAndroid Build Coastguard Worker 			pcap_strlcpy(p->errbuf, "atexit failed", PCAP_ERRBUF_SIZE);
3957*8b26181fSAndroid Build Coastguard Worker 			return (0);
3958*8b26181fSAndroid Build Coastguard Worker 		}
3959*8b26181fSAndroid Build Coastguard Worker 		did_atexit = 1;
3960*8b26181fSAndroid Build Coastguard Worker 	}
3961*8b26181fSAndroid Build Coastguard Worker 	return (1);
3962*8b26181fSAndroid Build Coastguard Worker }
3963*8b26181fSAndroid Build Coastguard Worker 
3964*8b26181fSAndroid Build Coastguard Worker void
3965*8b26181fSAndroid Build Coastguard Worker pcap_add_to_pcaps_to_close(pcap_t *p)
3966*8b26181fSAndroid Build Coastguard Worker {
3967*8b26181fSAndroid Build Coastguard Worker 	p->next = pcaps_to_close;
3968*8b26181fSAndroid Build Coastguard Worker 	pcaps_to_close = p;
3969*8b26181fSAndroid Build Coastguard Worker }
3970*8b26181fSAndroid Build Coastguard Worker 
3971*8b26181fSAndroid Build Coastguard Worker void
3972*8b26181fSAndroid Build Coastguard Worker pcap_remove_from_pcaps_to_close(pcap_t *p)
3973*8b26181fSAndroid Build Coastguard Worker {
3974*8b26181fSAndroid Build Coastguard Worker 	pcap_t *pc, *prevpc;
3975*8b26181fSAndroid Build Coastguard Worker 
3976*8b26181fSAndroid Build Coastguard Worker 	for (pc = pcaps_to_close, prevpc = NULL; pc != NULL;
3977*8b26181fSAndroid Build Coastguard Worker 	    prevpc = pc, pc = pc->next) {
3978*8b26181fSAndroid Build Coastguard Worker 		if (pc == p) {
3979*8b26181fSAndroid Build Coastguard Worker 			/*
3980*8b26181fSAndroid Build Coastguard Worker 			 * Found it.  Remove it from the list.
3981*8b26181fSAndroid Build Coastguard Worker 			 */
3982*8b26181fSAndroid Build Coastguard Worker 			if (prevpc == NULL) {
3983*8b26181fSAndroid Build Coastguard Worker 				/*
3984*8b26181fSAndroid Build Coastguard Worker 				 * It was at the head of the list.
3985*8b26181fSAndroid Build Coastguard Worker 				 */
3986*8b26181fSAndroid Build Coastguard Worker 				pcaps_to_close = pc->next;
3987*8b26181fSAndroid Build Coastguard Worker 			} else {
3988*8b26181fSAndroid Build Coastguard Worker 				/*
3989*8b26181fSAndroid Build Coastguard Worker 				 * It was in the middle of the list.
3990*8b26181fSAndroid Build Coastguard Worker 				 */
3991*8b26181fSAndroid Build Coastguard Worker 				prevpc->next = pc->next;
3992*8b26181fSAndroid Build Coastguard Worker 			}
3993*8b26181fSAndroid Build Coastguard Worker 			break;
3994*8b26181fSAndroid Build Coastguard Worker 		}
3995*8b26181fSAndroid Build Coastguard Worker 	}
3996*8b26181fSAndroid Build Coastguard Worker }
3997*8b26181fSAndroid Build Coastguard Worker 
3998*8b26181fSAndroid Build Coastguard Worker void
3999*8b26181fSAndroid Build Coastguard Worker pcap_breakloop_common(pcap_t *p)
4000*8b26181fSAndroid Build Coastguard Worker {
4001*8b26181fSAndroid Build Coastguard Worker 	p->break_loop = 1;
4002*8b26181fSAndroid Build Coastguard Worker }
4003*8b26181fSAndroid Build Coastguard Worker 
4004*8b26181fSAndroid Build Coastguard Worker 
4005*8b26181fSAndroid Build Coastguard Worker void
4006*8b26181fSAndroid Build Coastguard Worker pcap_cleanup_live_common(pcap_t *p)
4007*8b26181fSAndroid Build Coastguard Worker {
4008*8b26181fSAndroid Build Coastguard Worker 	if (p->opt.device != NULL) {
4009*8b26181fSAndroid Build Coastguard Worker 		free(p->opt.device);
4010*8b26181fSAndroid Build Coastguard Worker 		p->opt.device = NULL;
4011*8b26181fSAndroid Build Coastguard Worker 	}
4012*8b26181fSAndroid Build Coastguard Worker 	if (p->buffer != NULL) {
4013*8b26181fSAndroid Build Coastguard Worker 		free(p->buffer);
4014*8b26181fSAndroid Build Coastguard Worker 		p->buffer = NULL;
4015*8b26181fSAndroid Build Coastguard Worker 	}
4016*8b26181fSAndroid Build Coastguard Worker 	if (p->dlt_list != NULL) {
4017*8b26181fSAndroid Build Coastguard Worker 		free(p->dlt_list);
4018*8b26181fSAndroid Build Coastguard Worker 		p->dlt_list = NULL;
4019*8b26181fSAndroid Build Coastguard Worker 		p->dlt_count = 0;
4020*8b26181fSAndroid Build Coastguard Worker 	}
4021*8b26181fSAndroid Build Coastguard Worker 	if (p->tstamp_type_list != NULL) {
4022*8b26181fSAndroid Build Coastguard Worker 		free(p->tstamp_type_list);
4023*8b26181fSAndroid Build Coastguard Worker 		p->tstamp_type_list = NULL;
4024*8b26181fSAndroid Build Coastguard Worker 		p->tstamp_type_count = 0;
4025*8b26181fSAndroid Build Coastguard Worker 	}
4026*8b26181fSAndroid Build Coastguard Worker 	if (p->tstamp_precision_list != NULL) {
4027*8b26181fSAndroid Build Coastguard Worker 		free(p->tstamp_precision_list);
4028*8b26181fSAndroid Build Coastguard Worker 		p->tstamp_precision_list = NULL;
4029*8b26181fSAndroid Build Coastguard Worker 		p->tstamp_precision_count = 0;
4030*8b26181fSAndroid Build Coastguard Worker 	}
4031*8b26181fSAndroid Build Coastguard Worker 	pcap_freecode(&p->fcode);
4032*8b26181fSAndroid Build Coastguard Worker #if !defined(_WIN32) && !defined(MSDOS)
4033*8b26181fSAndroid Build Coastguard Worker 	if (p->fd >= 0) {
4034*8b26181fSAndroid Build Coastguard Worker 		close(p->fd);
4035*8b26181fSAndroid Build Coastguard Worker 		p->fd = -1;
4036*8b26181fSAndroid Build Coastguard Worker 	}
4037*8b26181fSAndroid Build Coastguard Worker 	p->selectable_fd = -1;
4038*8b26181fSAndroid Build Coastguard Worker #endif
4039*8b26181fSAndroid Build Coastguard Worker }
4040*8b26181fSAndroid Build Coastguard Worker 
4041*8b26181fSAndroid Build Coastguard Worker /*
4042*8b26181fSAndroid Build Coastguard Worker  * API compatible with WinPcap's "send a packet" routine - returns -1
4043*8b26181fSAndroid Build Coastguard Worker  * on error, 0 otherwise.
4044*8b26181fSAndroid Build Coastguard Worker  *
4045*8b26181fSAndroid Build Coastguard Worker  * XXX - what if we get a short write?
4046*8b26181fSAndroid Build Coastguard Worker  */
4047*8b26181fSAndroid Build Coastguard Worker int
4048*8b26181fSAndroid Build Coastguard Worker pcap_sendpacket(pcap_t *p, const u_char *buf, int size)
4049*8b26181fSAndroid Build Coastguard Worker {
4050*8b26181fSAndroid Build Coastguard Worker 	if (size <= 0) {
4051*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
4052*8b26181fSAndroid Build Coastguard Worker 		    errno, "The number of bytes to be sent must be positive");
4053*8b26181fSAndroid Build Coastguard Worker 		return (PCAP_ERROR);
4054*8b26181fSAndroid Build Coastguard Worker 	}
4055*8b26181fSAndroid Build Coastguard Worker 
4056*8b26181fSAndroid Build Coastguard Worker 	if (p->inject_op(p, buf, size) == -1)
4057*8b26181fSAndroid Build Coastguard Worker 		return (-1);
4058*8b26181fSAndroid Build Coastguard Worker 	return (0);
4059*8b26181fSAndroid Build Coastguard Worker }
4060*8b26181fSAndroid Build Coastguard Worker 
4061*8b26181fSAndroid Build Coastguard Worker /*
4062*8b26181fSAndroid Build Coastguard Worker  * API compatible with OpenBSD's "send a packet" routine - returns -1 on
4063*8b26181fSAndroid Build Coastguard Worker  * error, number of bytes written otherwise.
4064*8b26181fSAndroid Build Coastguard Worker  */
4065*8b26181fSAndroid Build Coastguard Worker int
4066*8b26181fSAndroid Build Coastguard Worker pcap_inject(pcap_t *p, const void *buf, size_t size)
4067*8b26181fSAndroid Build Coastguard Worker {
4068*8b26181fSAndroid Build Coastguard Worker 	/*
4069*8b26181fSAndroid Build Coastguard Worker 	 * We return the number of bytes written, so the number of
4070*8b26181fSAndroid Build Coastguard Worker 	 * bytes to write must fit in an int.
4071*8b26181fSAndroid Build Coastguard Worker 	 */
4072*8b26181fSAndroid Build Coastguard Worker 	if (size > INT_MAX) {
4073*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
4074*8b26181fSAndroid Build Coastguard Worker 		    errno, "More than %d bytes cannot be injected", INT_MAX);
4075*8b26181fSAndroid Build Coastguard Worker 		return (PCAP_ERROR);
4076*8b26181fSAndroid Build Coastguard Worker 	}
4077*8b26181fSAndroid Build Coastguard Worker 
4078*8b26181fSAndroid Build Coastguard Worker 	if (size == 0) {
4079*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
4080*8b26181fSAndroid Build Coastguard Worker 		    errno, "The number of bytes to be injected must not be zero");
4081*8b26181fSAndroid Build Coastguard Worker 		return (PCAP_ERROR);
4082*8b26181fSAndroid Build Coastguard Worker 	}
4083*8b26181fSAndroid Build Coastguard Worker 
4084*8b26181fSAndroid Build Coastguard Worker 	return (p->inject_op(p, buf, (int)size));
4085*8b26181fSAndroid Build Coastguard Worker }
4086*8b26181fSAndroid Build Coastguard Worker 
4087*8b26181fSAndroid Build Coastguard Worker void
4088*8b26181fSAndroid Build Coastguard Worker pcap_close(pcap_t *p)
4089*8b26181fSAndroid Build Coastguard Worker {
4090*8b26181fSAndroid Build Coastguard Worker 	p->cleanup_op(p);
4091*8b26181fSAndroid Build Coastguard Worker 	free(p);
4092*8b26181fSAndroid Build Coastguard Worker }
4093*8b26181fSAndroid Build Coastguard Worker 
4094*8b26181fSAndroid Build Coastguard Worker /*
4095*8b26181fSAndroid Build Coastguard Worker  * Helpers for safely loading code at run time.
4096*8b26181fSAndroid Build Coastguard Worker  * Currently Windows-only.
4097*8b26181fSAndroid Build Coastguard Worker  */
4098*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
4099*8b26181fSAndroid Build Coastguard Worker //
4100*8b26181fSAndroid Build Coastguard Worker // This wrapper around loadlibrary appends the system folder (usually
4101*8b26181fSAndroid Build Coastguard Worker // C:\Windows\System32) to the relative path of the DLL, so that the DLL
4102*8b26181fSAndroid Build Coastguard Worker // is always loaded from an absolute path (it's no longer possible to
4103*8b26181fSAndroid Build Coastguard Worker // load modules from the application folder).
4104*8b26181fSAndroid Build Coastguard Worker // This solves the DLL Hijacking issue discovered in August 2010:
4105*8b26181fSAndroid Build Coastguard Worker //
4106*8b26181fSAndroid Build Coastguard Worker // https://blog.rapid7.com/2010/08/23/exploiting-dll-hijacking-flaws/
4107*8b26181fSAndroid Build Coastguard Worker // https://blog.rapid7.com/2010/08/23/application-dll-load-hijacking/
4108*8b26181fSAndroid Build Coastguard Worker // (the purported Rapid7 blog post link in the first of those two links
4109*8b26181fSAndroid Build Coastguard Worker // is broken; the second of those links works.)
4110*8b26181fSAndroid Build Coastguard Worker //
4111*8b26181fSAndroid Build Coastguard Worker // If any links there are broken from all the content shuffling Rapid&
4112*8b26181fSAndroid Build Coastguard Worker // did, see archived versions of the posts at their original homes, at
4113*8b26181fSAndroid Build Coastguard Worker //
4114*8b26181fSAndroid Build Coastguard Worker // https://web.archive.org/web/20110122175058/http://blog.metasploit.com/2010/08/exploiting-dll-hijacking-flaws.html
4115*8b26181fSAndroid Build Coastguard Worker // https://web.archive.org/web/20100828112111/http://blog.rapid7.com/?p=5325
4116*8b26181fSAndroid Build Coastguard Worker //
4117*8b26181fSAndroid Build Coastguard Worker pcap_code_handle_t
4118*8b26181fSAndroid Build Coastguard Worker pcap_load_code(const char *name)
4119*8b26181fSAndroid Build Coastguard Worker {
4120*8b26181fSAndroid Build Coastguard Worker 	/*
4121*8b26181fSAndroid Build Coastguard Worker 	 * XXX - should this work in UTF-16LE rather than in the local
4122*8b26181fSAndroid Build Coastguard Worker 	 * ANSI code page?
4123*8b26181fSAndroid Build Coastguard Worker 	 */
4124*8b26181fSAndroid Build Coastguard Worker 	CHAR path[MAX_PATH];
4125*8b26181fSAndroid Build Coastguard Worker 	CHAR fullFileName[MAX_PATH];
4126*8b26181fSAndroid Build Coastguard Worker 	UINT res;
4127*8b26181fSAndroid Build Coastguard Worker 	HMODULE hModule = NULL;
4128*8b26181fSAndroid Build Coastguard Worker 
4129*8b26181fSAndroid Build Coastguard Worker 	do
4130*8b26181fSAndroid Build Coastguard Worker 	{
4131*8b26181fSAndroid Build Coastguard Worker 		res = GetSystemDirectoryA(path, MAX_PATH);
4132*8b26181fSAndroid Build Coastguard Worker 
4133*8b26181fSAndroid Build Coastguard Worker 		if (res == 0) {
4134*8b26181fSAndroid Build Coastguard Worker 			//
4135*8b26181fSAndroid Build Coastguard Worker 			// some bad failure occurred;
4136*8b26181fSAndroid Build Coastguard Worker 			//
4137*8b26181fSAndroid Build Coastguard Worker 			break;
4138*8b26181fSAndroid Build Coastguard Worker 		}
4139*8b26181fSAndroid Build Coastguard Worker 
4140*8b26181fSAndroid Build Coastguard Worker 		if (res > MAX_PATH) {
4141*8b26181fSAndroid Build Coastguard Worker 			//
4142*8b26181fSAndroid Build Coastguard Worker 			// the buffer was not big enough
4143*8b26181fSAndroid Build Coastguard Worker 			//
4144*8b26181fSAndroid Build Coastguard Worker 			SetLastError(ERROR_INSUFFICIENT_BUFFER);
4145*8b26181fSAndroid Build Coastguard Worker 			break;
4146*8b26181fSAndroid Build Coastguard Worker 		}
4147*8b26181fSAndroid Build Coastguard Worker 
4148*8b26181fSAndroid Build Coastguard Worker 		if (res + 1 + strlen(name) + 1 < MAX_PATH) {
4149*8b26181fSAndroid Build Coastguard Worker 			memcpy(fullFileName, path, res * sizeof(TCHAR));
4150*8b26181fSAndroid Build Coastguard Worker 			fullFileName[res] = '\\';
4151*8b26181fSAndroid Build Coastguard Worker 			memcpy(&fullFileName[res + 1], name, (strlen(name) + 1) * sizeof(TCHAR));
4152*8b26181fSAndroid Build Coastguard Worker 
4153*8b26181fSAndroid Build Coastguard Worker 			hModule = LoadLibraryA(fullFileName);
4154*8b26181fSAndroid Build Coastguard Worker 		} else
4155*8b26181fSAndroid Build Coastguard Worker 			SetLastError(ERROR_INSUFFICIENT_BUFFER);
4156*8b26181fSAndroid Build Coastguard Worker 
4157*8b26181fSAndroid Build Coastguard Worker 	} while(FALSE);
4158*8b26181fSAndroid Build Coastguard Worker 
4159*8b26181fSAndroid Build Coastguard Worker 	return hModule;
4160*8b26181fSAndroid Build Coastguard Worker }
4161*8b26181fSAndroid Build Coastguard Worker 
4162*8b26181fSAndroid Build Coastguard Worker pcap_funcptr_t
4163*8b26181fSAndroid Build Coastguard Worker pcap_find_function(pcap_code_handle_t code, const char *func)
4164*8b26181fSAndroid Build Coastguard Worker {
4165*8b26181fSAndroid Build Coastguard Worker 	return (GetProcAddress(code, func));
4166*8b26181fSAndroid Build Coastguard Worker }
4167*8b26181fSAndroid Build Coastguard Worker #endif
4168*8b26181fSAndroid Build Coastguard Worker 
4169*8b26181fSAndroid Build Coastguard Worker /*
4170*8b26181fSAndroid Build Coastguard Worker  * Given a BPF program, a pcap_pkthdr structure for a packet, and the raw
4171*8b26181fSAndroid Build Coastguard Worker  * data for the packet, check whether the packet passes the filter.
4172*8b26181fSAndroid Build Coastguard Worker  * Returns the return value of the filter program, which will be zero if
4173*8b26181fSAndroid Build Coastguard Worker  * the packet doesn't pass and non-zero if the packet does pass.
4174*8b26181fSAndroid Build Coastguard Worker  */
4175*8b26181fSAndroid Build Coastguard Worker int
4176*8b26181fSAndroid Build Coastguard Worker pcap_offline_filter(const struct bpf_program *fp, const struct pcap_pkthdr *h,
4177*8b26181fSAndroid Build Coastguard Worker     const u_char *pkt)
4178*8b26181fSAndroid Build Coastguard Worker {
4179*8b26181fSAndroid Build Coastguard Worker 	const struct bpf_insn *fcode = fp->bf_insns;
4180*8b26181fSAndroid Build Coastguard Worker 
4181*8b26181fSAndroid Build Coastguard Worker 	if (fcode != NULL)
4182*8b26181fSAndroid Build Coastguard Worker 		return (pcap_filter(fcode, pkt, h->len, h->caplen));
4183*8b26181fSAndroid Build Coastguard Worker 	else
4184*8b26181fSAndroid Build Coastguard Worker 		return (0);
4185*8b26181fSAndroid Build Coastguard Worker }
4186*8b26181fSAndroid Build Coastguard Worker 
4187*8b26181fSAndroid Build Coastguard Worker static int
4188*8b26181fSAndroid Build Coastguard Worker pcap_can_set_rfmon_dead(pcap_t *p)
4189*8b26181fSAndroid Build Coastguard Worker {
4190*8b26181fSAndroid Build Coastguard Worker 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
4191*8b26181fSAndroid Build Coastguard Worker 	    "Rfmon mode doesn't apply on a pcap_open_dead pcap_t");
4192*8b26181fSAndroid Build Coastguard Worker 	return (PCAP_ERROR);
4193*8b26181fSAndroid Build Coastguard Worker }
4194*8b26181fSAndroid Build Coastguard Worker 
4195*8b26181fSAndroid Build Coastguard Worker static int
4196*8b26181fSAndroid Build Coastguard Worker pcap_read_dead(pcap_t *p, int cnt _U_, pcap_handler callback _U_,
4197*8b26181fSAndroid Build Coastguard Worker     u_char *user _U_)
4198*8b26181fSAndroid Build Coastguard Worker {
4199*8b26181fSAndroid Build Coastguard Worker 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
4200*8b26181fSAndroid Build Coastguard Worker 	    "Packets aren't available from a pcap_open_dead pcap_t");
4201*8b26181fSAndroid Build Coastguard Worker 	return (-1);
4202*8b26181fSAndroid Build Coastguard Worker }
4203*8b26181fSAndroid Build Coastguard Worker 
4204*8b26181fSAndroid Build Coastguard Worker static void
4205*8b26181fSAndroid Build Coastguard Worker pcap_breakloop_dead(pcap_t *p _U_)
4206*8b26181fSAndroid Build Coastguard Worker {
4207*8b26181fSAndroid Build Coastguard Worker 	/*
4208*8b26181fSAndroid Build Coastguard Worker 	 * A "dead" pcap_t is just a placeholder to use in order to
4209*8b26181fSAndroid Build Coastguard Worker 	 * compile a filter to BPF code or to open a savefile for
4210*8b26181fSAndroid Build Coastguard Worker 	 * writing.  It doesn't support any operations, including
4211*8b26181fSAndroid Build Coastguard Worker 	 * capturing or reading packets, so there will never be a
4212*8b26181fSAndroid Build Coastguard Worker 	 * get-packets loop in progress to break out *of*.
4213*8b26181fSAndroid Build Coastguard Worker 	 *
4214*8b26181fSAndroid Build Coastguard Worker 	 * As such, this routine doesn't need to do anything.
4215*8b26181fSAndroid Build Coastguard Worker 	 */
4216*8b26181fSAndroid Build Coastguard Worker }
4217*8b26181fSAndroid Build Coastguard Worker 
4218*8b26181fSAndroid Build Coastguard Worker static int
4219*8b26181fSAndroid Build Coastguard Worker pcap_inject_dead(pcap_t *p, const void *buf _U_, int size _U_)
4220*8b26181fSAndroid Build Coastguard Worker {
4221*8b26181fSAndroid Build Coastguard Worker 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
4222*8b26181fSAndroid Build Coastguard Worker 	    "Packets can't be sent on a pcap_open_dead pcap_t");
4223*8b26181fSAndroid Build Coastguard Worker 	return (-1);
4224*8b26181fSAndroid Build Coastguard Worker }
4225*8b26181fSAndroid Build Coastguard Worker 
4226*8b26181fSAndroid Build Coastguard Worker static int
4227*8b26181fSAndroid Build Coastguard Worker pcap_setfilter_dead(pcap_t *p, struct bpf_program *fp _U_)
4228*8b26181fSAndroid Build Coastguard Worker {
4229*8b26181fSAndroid Build Coastguard Worker 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
4230*8b26181fSAndroid Build Coastguard Worker 	    "A filter cannot be set on a pcap_open_dead pcap_t");
4231*8b26181fSAndroid Build Coastguard Worker 	return (-1);
4232*8b26181fSAndroid Build Coastguard Worker }
4233*8b26181fSAndroid Build Coastguard Worker 
4234*8b26181fSAndroid Build Coastguard Worker static int
4235*8b26181fSAndroid Build Coastguard Worker pcap_setdirection_dead(pcap_t *p, pcap_direction_t d _U_)
4236*8b26181fSAndroid Build Coastguard Worker {
4237*8b26181fSAndroid Build Coastguard Worker 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
4238*8b26181fSAndroid Build Coastguard Worker 	    "The packet direction cannot be set on a pcap_open_dead pcap_t");
4239*8b26181fSAndroid Build Coastguard Worker 	return (-1);
4240*8b26181fSAndroid Build Coastguard Worker }
4241*8b26181fSAndroid Build Coastguard Worker 
4242*8b26181fSAndroid Build Coastguard Worker static int
4243*8b26181fSAndroid Build Coastguard Worker pcap_set_datalink_dead(pcap_t *p, int dlt _U_)
4244*8b26181fSAndroid Build Coastguard Worker {
4245*8b26181fSAndroid Build Coastguard Worker 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
4246*8b26181fSAndroid Build Coastguard Worker 	    "The link-layer header type cannot be set on a pcap_open_dead pcap_t");
4247*8b26181fSAndroid Build Coastguard Worker 	return (-1);
4248*8b26181fSAndroid Build Coastguard Worker }
4249*8b26181fSAndroid Build Coastguard Worker 
4250*8b26181fSAndroid Build Coastguard Worker static int
4251*8b26181fSAndroid Build Coastguard Worker pcap_getnonblock_dead(pcap_t *p)
4252*8b26181fSAndroid Build Coastguard Worker {
4253*8b26181fSAndroid Build Coastguard Worker 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
4254*8b26181fSAndroid Build Coastguard Worker 	    "A pcap_open_dead pcap_t does not have a non-blocking mode setting");
4255*8b26181fSAndroid Build Coastguard Worker 	return (-1);
4256*8b26181fSAndroid Build Coastguard Worker }
4257*8b26181fSAndroid Build Coastguard Worker 
4258*8b26181fSAndroid Build Coastguard Worker static int
4259*8b26181fSAndroid Build Coastguard Worker pcap_setnonblock_dead(pcap_t *p, int nonblock _U_)
4260*8b26181fSAndroid Build Coastguard Worker {
4261*8b26181fSAndroid Build Coastguard Worker 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
4262*8b26181fSAndroid Build Coastguard Worker 	    "A pcap_open_dead pcap_t does not have a non-blocking mode setting");
4263*8b26181fSAndroid Build Coastguard Worker 	return (-1);
4264*8b26181fSAndroid Build Coastguard Worker }
4265*8b26181fSAndroid Build Coastguard Worker 
4266*8b26181fSAndroid Build Coastguard Worker static int
4267*8b26181fSAndroid Build Coastguard Worker pcap_stats_dead(pcap_t *p, struct pcap_stat *ps _U_)
4268*8b26181fSAndroid Build Coastguard Worker {
4269*8b26181fSAndroid Build Coastguard Worker 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
4270*8b26181fSAndroid Build Coastguard Worker 	    "Statistics aren't available from a pcap_open_dead pcap_t");
4271*8b26181fSAndroid Build Coastguard Worker 	return (-1);
4272*8b26181fSAndroid Build Coastguard Worker }
4273*8b26181fSAndroid Build Coastguard Worker 
4274*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
4275*8b26181fSAndroid Build Coastguard Worker static struct pcap_stat *
4276*8b26181fSAndroid Build Coastguard Worker pcap_stats_ex_dead(pcap_t *p, int *pcap_stat_size _U_)
4277*8b26181fSAndroid Build Coastguard Worker {
4278*8b26181fSAndroid Build Coastguard Worker 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
4279*8b26181fSAndroid Build Coastguard Worker 	    "Statistics aren't available from a pcap_open_dead pcap_t");
4280*8b26181fSAndroid Build Coastguard Worker 	return (NULL);
4281*8b26181fSAndroid Build Coastguard Worker }
4282*8b26181fSAndroid Build Coastguard Worker 
4283*8b26181fSAndroid Build Coastguard Worker static int
4284*8b26181fSAndroid Build Coastguard Worker pcap_setbuff_dead(pcap_t *p, int dim _U_)
4285*8b26181fSAndroid Build Coastguard Worker {
4286*8b26181fSAndroid Build Coastguard Worker 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
4287*8b26181fSAndroid Build Coastguard Worker 	    "The kernel buffer size cannot be set on a pcap_open_dead pcap_t");
4288*8b26181fSAndroid Build Coastguard Worker 	return (-1);
4289*8b26181fSAndroid Build Coastguard Worker }
4290*8b26181fSAndroid Build Coastguard Worker 
4291*8b26181fSAndroid Build Coastguard Worker static int
4292*8b26181fSAndroid Build Coastguard Worker pcap_setmode_dead(pcap_t *p, int mode _U_)
4293*8b26181fSAndroid Build Coastguard Worker {
4294*8b26181fSAndroid Build Coastguard Worker 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
4295*8b26181fSAndroid Build Coastguard Worker 	    "impossible to set mode on a pcap_open_dead pcap_t");
4296*8b26181fSAndroid Build Coastguard Worker 	return (-1);
4297*8b26181fSAndroid Build Coastguard Worker }
4298*8b26181fSAndroid Build Coastguard Worker 
4299*8b26181fSAndroid Build Coastguard Worker static int
4300*8b26181fSAndroid Build Coastguard Worker pcap_setmintocopy_dead(pcap_t *p, int size _U_)
4301*8b26181fSAndroid Build Coastguard Worker {
4302*8b26181fSAndroid Build Coastguard Worker 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
4303*8b26181fSAndroid Build Coastguard Worker 	    "The mintocopy parameter cannot be set on a pcap_open_dead pcap_t");
4304*8b26181fSAndroid Build Coastguard Worker 	return (-1);
4305*8b26181fSAndroid Build Coastguard Worker }
4306*8b26181fSAndroid Build Coastguard Worker 
4307*8b26181fSAndroid Build Coastguard Worker static HANDLE
4308*8b26181fSAndroid Build Coastguard Worker pcap_getevent_dead(pcap_t *p)
4309*8b26181fSAndroid Build Coastguard Worker {
4310*8b26181fSAndroid Build Coastguard Worker 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
4311*8b26181fSAndroid Build Coastguard Worker 	    "A pcap_open_dead pcap_t has no event handle");
4312*8b26181fSAndroid Build Coastguard Worker 	return (INVALID_HANDLE_VALUE);
4313*8b26181fSAndroid Build Coastguard Worker }
4314*8b26181fSAndroid Build Coastguard Worker 
4315*8b26181fSAndroid Build Coastguard Worker static int
4316*8b26181fSAndroid Build Coastguard Worker pcap_oid_get_request_dead(pcap_t *p, bpf_u_int32 oid _U_, void *data _U_,
4317*8b26181fSAndroid Build Coastguard Worker     size_t *lenp _U_)
4318*8b26181fSAndroid Build Coastguard Worker {
4319*8b26181fSAndroid Build Coastguard Worker 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
4320*8b26181fSAndroid Build Coastguard Worker 	    "An OID get request cannot be performed on a pcap_open_dead pcap_t");
4321*8b26181fSAndroid Build Coastguard Worker 	return (PCAP_ERROR);
4322*8b26181fSAndroid Build Coastguard Worker }
4323*8b26181fSAndroid Build Coastguard Worker 
4324*8b26181fSAndroid Build Coastguard Worker static int
4325*8b26181fSAndroid Build Coastguard Worker pcap_oid_set_request_dead(pcap_t *p, bpf_u_int32 oid _U_, const void *data _U_,
4326*8b26181fSAndroid Build Coastguard Worker     size_t *lenp _U_)
4327*8b26181fSAndroid Build Coastguard Worker {
4328*8b26181fSAndroid Build Coastguard Worker 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
4329*8b26181fSAndroid Build Coastguard Worker 	    "An OID set request cannot be performed on a pcap_open_dead pcap_t");
4330*8b26181fSAndroid Build Coastguard Worker 	return (PCAP_ERROR);
4331*8b26181fSAndroid Build Coastguard Worker }
4332*8b26181fSAndroid Build Coastguard Worker 
4333*8b26181fSAndroid Build Coastguard Worker static u_int
4334*8b26181fSAndroid Build Coastguard Worker pcap_sendqueue_transmit_dead(pcap_t *p, pcap_send_queue *queue _U_,
4335*8b26181fSAndroid Build Coastguard Worker     int sync _U_)
4336*8b26181fSAndroid Build Coastguard Worker {
4337*8b26181fSAndroid Build Coastguard Worker 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
4338*8b26181fSAndroid Build Coastguard Worker 	    "Packets cannot be transmitted on a pcap_open_dead pcap_t");
4339*8b26181fSAndroid Build Coastguard Worker 	return (0);
4340*8b26181fSAndroid Build Coastguard Worker }
4341*8b26181fSAndroid Build Coastguard Worker 
4342*8b26181fSAndroid Build Coastguard Worker static int
4343*8b26181fSAndroid Build Coastguard Worker pcap_setuserbuffer_dead(pcap_t *p, int size _U_)
4344*8b26181fSAndroid Build Coastguard Worker {
4345*8b26181fSAndroid Build Coastguard Worker 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
4346*8b26181fSAndroid Build Coastguard Worker 	    "The user buffer cannot be set on a pcap_open_dead pcap_t");
4347*8b26181fSAndroid Build Coastguard Worker 	return (-1);
4348*8b26181fSAndroid Build Coastguard Worker }
4349*8b26181fSAndroid Build Coastguard Worker 
4350*8b26181fSAndroid Build Coastguard Worker static int
4351*8b26181fSAndroid Build Coastguard Worker pcap_live_dump_dead(pcap_t *p, char *filename _U_, int maxsize _U_,
4352*8b26181fSAndroid Build Coastguard Worker     int maxpacks _U_)
4353*8b26181fSAndroid Build Coastguard Worker {
4354*8b26181fSAndroid Build Coastguard Worker 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
4355*8b26181fSAndroid Build Coastguard Worker 	    "Live packet dumping cannot be performed on a pcap_open_dead pcap_t");
4356*8b26181fSAndroid Build Coastguard Worker 	return (-1);
4357*8b26181fSAndroid Build Coastguard Worker }
4358*8b26181fSAndroid Build Coastguard Worker 
4359*8b26181fSAndroid Build Coastguard Worker static int
4360*8b26181fSAndroid Build Coastguard Worker pcap_live_dump_ended_dead(pcap_t *p, int sync _U_)
4361*8b26181fSAndroid Build Coastguard Worker {
4362*8b26181fSAndroid Build Coastguard Worker 	snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
4363*8b26181fSAndroid Build Coastguard Worker 	    "Live packet dumping cannot be performed on a pcap_open_dead pcap_t");
4364*8b26181fSAndroid Build Coastguard Worker 	return (-1);
4365*8b26181fSAndroid Build Coastguard Worker }
4366*8b26181fSAndroid Build Coastguard Worker 
4367*8b26181fSAndroid Build Coastguard Worker static PAirpcapHandle
4368*8b26181fSAndroid Build Coastguard Worker pcap_get_airpcap_handle_dead(pcap_t *p _U_)
4369*8b26181fSAndroid Build Coastguard Worker {
4370*8b26181fSAndroid Build Coastguard Worker 	return (NULL);
4371*8b26181fSAndroid Build Coastguard Worker }
4372*8b26181fSAndroid Build Coastguard Worker #endif /* _WIN32 */
4373*8b26181fSAndroid Build Coastguard Worker 
4374*8b26181fSAndroid Build Coastguard Worker static void
4375*8b26181fSAndroid Build Coastguard Worker pcap_cleanup_dead(pcap_t *p _U_)
4376*8b26181fSAndroid Build Coastguard Worker {
4377*8b26181fSAndroid Build Coastguard Worker 	/* Nothing to do. */
4378*8b26181fSAndroid Build Coastguard Worker }
4379*8b26181fSAndroid Build Coastguard Worker 
4380*8b26181fSAndroid Build Coastguard Worker pcap_t *
4381*8b26181fSAndroid Build Coastguard Worker pcap_open_dead_with_tstamp_precision(int linktype, int snaplen, u_int precision)
4382*8b26181fSAndroid Build Coastguard Worker {
4383*8b26181fSAndroid Build Coastguard Worker 	pcap_t *p;
4384*8b26181fSAndroid Build Coastguard Worker 
4385*8b26181fSAndroid Build Coastguard Worker 	switch (precision) {
4386*8b26181fSAndroid Build Coastguard Worker 
4387*8b26181fSAndroid Build Coastguard Worker 	case PCAP_TSTAMP_PRECISION_MICRO:
4388*8b26181fSAndroid Build Coastguard Worker 	case PCAP_TSTAMP_PRECISION_NANO:
4389*8b26181fSAndroid Build Coastguard Worker 		break;
4390*8b26181fSAndroid Build Coastguard Worker 
4391*8b26181fSAndroid Build Coastguard Worker 	default:
4392*8b26181fSAndroid Build Coastguard Worker 		/*
4393*8b26181fSAndroid Build Coastguard Worker 		 * This doesn't really matter, but we don't have any way
4394*8b26181fSAndroid Build Coastguard Worker 		 * to report particular errors, so the only failure we
4395*8b26181fSAndroid Build Coastguard Worker 		 * should have is a memory allocation failure.  Just
4396*8b26181fSAndroid Build Coastguard Worker 		 * pick microsecond precision.
4397*8b26181fSAndroid Build Coastguard Worker 		 */
4398*8b26181fSAndroid Build Coastguard Worker 		precision = PCAP_TSTAMP_PRECISION_MICRO;
4399*8b26181fSAndroid Build Coastguard Worker 		break;
4400*8b26181fSAndroid Build Coastguard Worker 	}
4401*8b26181fSAndroid Build Coastguard Worker 	p = malloc(sizeof(*p));
4402*8b26181fSAndroid Build Coastguard Worker 	if (p == NULL)
4403*8b26181fSAndroid Build Coastguard Worker 		return NULL;
4404*8b26181fSAndroid Build Coastguard Worker 	memset (p, 0, sizeof(*p));
4405*8b26181fSAndroid Build Coastguard Worker 	p->snapshot = snaplen;
4406*8b26181fSAndroid Build Coastguard Worker 	p->linktype = linktype;
4407*8b26181fSAndroid Build Coastguard Worker 	p->opt.tstamp_precision = precision;
4408*8b26181fSAndroid Build Coastguard Worker 	p->can_set_rfmon_op = pcap_can_set_rfmon_dead;
4409*8b26181fSAndroid Build Coastguard Worker 	p->read_op = pcap_read_dead;
4410*8b26181fSAndroid Build Coastguard Worker 	p->inject_op = pcap_inject_dead;
4411*8b26181fSAndroid Build Coastguard Worker 	p->setfilter_op = pcap_setfilter_dead;
4412*8b26181fSAndroid Build Coastguard Worker 	p->setdirection_op = pcap_setdirection_dead;
4413*8b26181fSAndroid Build Coastguard Worker 	p->set_datalink_op = pcap_set_datalink_dead;
4414*8b26181fSAndroid Build Coastguard Worker 	p->getnonblock_op = pcap_getnonblock_dead;
4415*8b26181fSAndroid Build Coastguard Worker 	p->setnonblock_op = pcap_setnonblock_dead;
4416*8b26181fSAndroid Build Coastguard Worker 	p->stats_op = pcap_stats_dead;
4417*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
4418*8b26181fSAndroid Build Coastguard Worker 	p->stats_ex_op = pcap_stats_ex_dead;
4419*8b26181fSAndroid Build Coastguard Worker 	p->setbuff_op = pcap_setbuff_dead;
4420*8b26181fSAndroid Build Coastguard Worker 	p->setmode_op = pcap_setmode_dead;
4421*8b26181fSAndroid Build Coastguard Worker 	p->setmintocopy_op = pcap_setmintocopy_dead;
4422*8b26181fSAndroid Build Coastguard Worker 	p->getevent_op = pcap_getevent_dead;
4423*8b26181fSAndroid Build Coastguard Worker 	p->oid_get_request_op = pcap_oid_get_request_dead;
4424*8b26181fSAndroid Build Coastguard Worker 	p->oid_set_request_op = pcap_oid_set_request_dead;
4425*8b26181fSAndroid Build Coastguard Worker 	p->sendqueue_transmit_op = pcap_sendqueue_transmit_dead;
4426*8b26181fSAndroid Build Coastguard Worker 	p->setuserbuffer_op = pcap_setuserbuffer_dead;
4427*8b26181fSAndroid Build Coastguard Worker 	p->live_dump_op = pcap_live_dump_dead;
4428*8b26181fSAndroid Build Coastguard Worker 	p->live_dump_ended_op = pcap_live_dump_ended_dead;
4429*8b26181fSAndroid Build Coastguard Worker 	p->get_airpcap_handle_op = pcap_get_airpcap_handle_dead;
4430*8b26181fSAndroid Build Coastguard Worker #endif
4431*8b26181fSAndroid Build Coastguard Worker 	p->breakloop_op = pcap_breakloop_dead;
4432*8b26181fSAndroid Build Coastguard Worker 	p->cleanup_op = pcap_cleanup_dead;
4433*8b26181fSAndroid Build Coastguard Worker 
4434*8b26181fSAndroid Build Coastguard Worker 	/*
4435*8b26181fSAndroid Build Coastguard Worker 	 * A "dead" pcap_t never requires special BPF code generation.
4436*8b26181fSAndroid Build Coastguard Worker 	 */
4437*8b26181fSAndroid Build Coastguard Worker 	p->bpf_codegen_flags = 0;
4438*8b26181fSAndroid Build Coastguard Worker 
4439*8b26181fSAndroid Build Coastguard Worker 	p->activated = 1;
4440*8b26181fSAndroid Build Coastguard Worker 	return (p);
4441*8b26181fSAndroid Build Coastguard Worker }
4442*8b26181fSAndroid Build Coastguard Worker 
4443*8b26181fSAndroid Build Coastguard Worker pcap_t *
4444*8b26181fSAndroid Build Coastguard Worker pcap_open_dead(int linktype, int snaplen)
4445*8b26181fSAndroid Build Coastguard Worker {
4446*8b26181fSAndroid Build Coastguard Worker 	return (pcap_open_dead_with_tstamp_precision(linktype, snaplen,
4447*8b26181fSAndroid Build Coastguard Worker 	    PCAP_TSTAMP_PRECISION_MICRO));
4448*8b26181fSAndroid Build Coastguard Worker }
4449*8b26181fSAndroid Build Coastguard Worker 
4450*8b26181fSAndroid Build Coastguard Worker #ifdef YYDEBUG
4451*8b26181fSAndroid Build Coastguard Worker /*
4452*8b26181fSAndroid Build Coastguard Worker  * Set the internal "debug printout" flag for the filter expression parser.
4453*8b26181fSAndroid Build Coastguard Worker  * The code to print that stuff is present only if YYDEBUG is defined, so
4454*8b26181fSAndroid Build Coastguard Worker  * the flag, and the routine to set it, are defined only if YYDEBUG is
4455*8b26181fSAndroid Build Coastguard Worker  * defined.
4456*8b26181fSAndroid Build Coastguard Worker  *
4457*8b26181fSAndroid Build Coastguard Worker  * This is intended for libpcap developers, not for general use.
4458*8b26181fSAndroid Build Coastguard Worker  * If you want to set these in a program, you'll have to declare this
4459*8b26181fSAndroid Build Coastguard Worker  * routine yourself, with the appropriate DLL import attribute on Windows;
4460*8b26181fSAndroid Build Coastguard Worker  * it's not declared in any header file, and won't be declared in any
4461*8b26181fSAndroid Build Coastguard Worker  * header file provided by libpcap.
4462*8b26181fSAndroid Build Coastguard Worker  */
4463*8b26181fSAndroid Build Coastguard Worker PCAP_API void pcap_set_parser_debug(int value);
4464*8b26181fSAndroid Build Coastguard Worker 
4465*8b26181fSAndroid Build Coastguard Worker PCAP_API_DEF void
4466*8b26181fSAndroid Build Coastguard Worker pcap_set_parser_debug(int value)
4467*8b26181fSAndroid Build Coastguard Worker {
4468*8b26181fSAndroid Build Coastguard Worker 	pcap_debug = value;
4469*8b26181fSAndroid Build Coastguard Worker }
4470*8b26181fSAndroid Build Coastguard Worker #endif
4471