xref: /aosp_15_r20/external/libpcap/testprogs/threadsignaltest.c (revision 8b26181f966a6af5cf6981a6f474313de533bb28)
1*8b26181fSAndroid Build Coastguard Worker /*
2*8b26181fSAndroid Build Coastguard Worker  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
3*8b26181fSAndroid Build Coastguard Worker  *	The Regents of the University of California.  All rights reserved.
4*8b26181fSAndroid Build Coastguard Worker  *
5*8b26181fSAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
6*8b26181fSAndroid Build Coastguard Worker  * modification, are permitted provided that: (1) source code distributions
7*8b26181fSAndroid Build Coastguard Worker  * retain the above copyright notice and this paragraph in its entirety, (2)
8*8b26181fSAndroid Build Coastguard Worker  * distributions including binary code include the above copyright notice and
9*8b26181fSAndroid Build Coastguard Worker  * this paragraph in its entirety in the documentation or other materials
10*8b26181fSAndroid Build Coastguard Worker  * provided with the distribution, and (3) all advertising materials mentioning
11*8b26181fSAndroid Build Coastguard Worker  * features or use of this software display the following acknowledgement:
12*8b26181fSAndroid Build Coastguard Worker  * ``This product includes software developed by the University of California,
13*8b26181fSAndroid Build Coastguard Worker  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14*8b26181fSAndroid Build Coastguard Worker  * the University nor the names of its contributors may be used to endorse
15*8b26181fSAndroid Build Coastguard Worker  * or promote products derived from this software without specific prior
16*8b26181fSAndroid Build Coastguard Worker  * written permission.
17*8b26181fSAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18*8b26181fSAndroid Build Coastguard Worker  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19*8b26181fSAndroid Build Coastguard Worker  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20*8b26181fSAndroid Build Coastguard Worker  */
21*8b26181fSAndroid Build Coastguard Worker 
22*8b26181fSAndroid Build Coastguard Worker #include "varattrs.h"
23*8b26181fSAndroid Build Coastguard Worker 
24*8b26181fSAndroid Build Coastguard Worker #ifndef lint
25*8b26181fSAndroid Build Coastguard Worker static const char copyright[] _U_ =
26*8b26181fSAndroid Build Coastguard Worker     "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
27*8b26181fSAndroid Build Coastguard Worker The Regents of the University of California.  All rights reserved.\n";
28*8b26181fSAndroid Build Coastguard Worker #endif
29*8b26181fSAndroid Build Coastguard Worker 
30*8b26181fSAndroid Build Coastguard Worker #include <stdio.h>
31*8b26181fSAndroid Build Coastguard Worker #include <stdlib.h>
32*8b26181fSAndroid Build Coastguard Worker #include <string.h>
33*8b26181fSAndroid Build Coastguard Worker #include <stdarg.h>
34*8b26181fSAndroid Build Coastguard Worker #include <limits.h>
35*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
36*8b26181fSAndroid Build Coastguard Worker   #include <winsock2.h>
37*8b26181fSAndroid Build Coastguard Worker   #include <windows.h>
38*8b26181fSAndroid Build Coastguard Worker 
39*8b26181fSAndroid Build Coastguard Worker   #define THREAD_HANDLE			HANDLE
40*8b26181fSAndroid Build Coastguard Worker   #define THREAD_FUNC_ARG_TYPE		LPVOID
41*8b26181fSAndroid Build Coastguard Worker   #define THREAD_FUNC_RETURN_TYPE	DWORD __stdcall
42*8b26181fSAndroid Build Coastguard Worker 
43*8b26181fSAndroid Build Coastguard Worker   #include "getopt.h"
44*8b26181fSAndroid Build Coastguard Worker #else
45*8b26181fSAndroid Build Coastguard Worker   #include <pthread.h>
46*8b26181fSAndroid Build Coastguard Worker   #include <signal.h>
47*8b26181fSAndroid Build Coastguard Worker   #include <unistd.h>
48*8b26181fSAndroid Build Coastguard Worker 
49*8b26181fSAndroid Build Coastguard Worker   #define THREAD_HANDLE			pthread_t
50*8b26181fSAndroid Build Coastguard Worker   #define THREAD_FUNC_ARG_TYPE		void *
51*8b26181fSAndroid Build Coastguard Worker   #define THREAD_FUNC_RETURN_TYPE	void *
52*8b26181fSAndroid Build Coastguard Worker #endif
53*8b26181fSAndroid Build Coastguard Worker #include <errno.h>
54*8b26181fSAndroid Build Coastguard Worker #include <sys/types.h>
55*8b26181fSAndroid Build Coastguard Worker 
56*8b26181fSAndroid Build Coastguard Worker #include <pcap.h>
57*8b26181fSAndroid Build Coastguard Worker 
58*8b26181fSAndroid Build Coastguard Worker #include "pcap/funcattrs.h"
59*8b26181fSAndroid Build Coastguard Worker 
60*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
61*8b26181fSAndroid Build Coastguard Worker   #include "portability.h"
62*8b26181fSAndroid Build Coastguard Worker #endif
63*8b26181fSAndroid Build Coastguard Worker 
64*8b26181fSAndroid Build Coastguard Worker static char *program_name;
65*8b26181fSAndroid Build Coastguard Worker 
66*8b26181fSAndroid Build Coastguard Worker /* Forwards */
67*8b26181fSAndroid Build Coastguard Worker static void countme(u_char *, const struct pcap_pkthdr *, const u_char *);
68*8b26181fSAndroid Build Coastguard Worker static void PCAP_NORETURN usage(void);
69*8b26181fSAndroid Build Coastguard Worker static void PCAP_NORETURN error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
70*8b26181fSAndroid Build Coastguard Worker static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
71*8b26181fSAndroid Build Coastguard Worker static char *copy_argv(char **);
72*8b26181fSAndroid Build Coastguard Worker 
73*8b26181fSAndroid Build Coastguard Worker static pcap_t *pd;
74*8b26181fSAndroid Build Coastguard Worker 
75*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
76*8b26181fSAndroid Build Coastguard Worker /*
77*8b26181fSAndroid Build Coastguard Worker  * Generate a string for a Win32-specific error (i.e. an error generated when
78*8b26181fSAndroid Build Coastguard Worker  * calling a Win32 API).
79*8b26181fSAndroid Build Coastguard Worker  * For errors occurred during standard C calls, we still use pcap_strerror()
80*8b26181fSAndroid Build Coastguard Worker  */
81*8b26181fSAndroid Build Coastguard Worker #define ERRBUF_SIZE	1024
82*8b26181fSAndroid Build Coastguard Worker static const char *
win32_strerror(DWORD error)83*8b26181fSAndroid Build Coastguard Worker win32_strerror(DWORD error)
84*8b26181fSAndroid Build Coastguard Worker {
85*8b26181fSAndroid Build Coastguard Worker   static char errbuf[ERRBUF_SIZE+1];
86*8b26181fSAndroid Build Coastguard Worker   size_t errlen;
87*8b26181fSAndroid Build Coastguard Worker 
88*8b26181fSAndroid Build Coastguard Worker   FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf,
89*8b26181fSAndroid Build Coastguard Worker                 ERRBUF_SIZE, NULL);
90*8b26181fSAndroid Build Coastguard Worker 
91*8b26181fSAndroid Build Coastguard Worker   /*
92*8b26181fSAndroid Build Coastguard Worker    * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
93*8b26181fSAndroid Build Coastguard Worker    * message.  Get rid of it.
94*8b26181fSAndroid Build Coastguard Worker    */
95*8b26181fSAndroid Build Coastguard Worker   errlen = strlen(errbuf);
96*8b26181fSAndroid Build Coastguard Worker   if (errlen >= 2) {
97*8b26181fSAndroid Build Coastguard Worker     errbuf[errlen - 1] = '\0';
98*8b26181fSAndroid Build Coastguard Worker     errbuf[errlen - 2] = '\0';
99*8b26181fSAndroid Build Coastguard Worker     errlen -= 2;
100*8b26181fSAndroid Build Coastguard Worker   }
101*8b26181fSAndroid Build Coastguard Worker   return errbuf;
102*8b26181fSAndroid Build Coastguard Worker }
103*8b26181fSAndroid Build Coastguard Worker #else
104*8b26181fSAndroid Build Coastguard Worker static void
catch_sigusr1(int sig _U_)105*8b26181fSAndroid Build Coastguard Worker catch_sigusr1(int sig _U_)
106*8b26181fSAndroid Build Coastguard Worker {
107*8b26181fSAndroid Build Coastguard Worker 	printf("Got SIGUSR1\n");
108*8b26181fSAndroid Build Coastguard Worker }
109*8b26181fSAndroid Build Coastguard Worker #endif
110*8b26181fSAndroid Build Coastguard Worker 
111*8b26181fSAndroid Build Coastguard Worker static void
sleep_secs(int secs)112*8b26181fSAndroid Build Coastguard Worker sleep_secs(int secs)
113*8b26181fSAndroid Build Coastguard Worker {
114*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
115*8b26181fSAndroid Build Coastguard Worker 	Sleep(secs*1000);
116*8b26181fSAndroid Build Coastguard Worker #else
117*8b26181fSAndroid Build Coastguard Worker 	unsigned secs_remaining;
118*8b26181fSAndroid Build Coastguard Worker 
119*8b26181fSAndroid Build Coastguard Worker 	if (secs <= 0)
120*8b26181fSAndroid Build Coastguard Worker 		return;
121*8b26181fSAndroid Build Coastguard Worker 	secs_remaining = secs;
122*8b26181fSAndroid Build Coastguard Worker 	while (secs_remaining != 0)
123*8b26181fSAndroid Build Coastguard Worker 		secs_remaining = sleep(secs_remaining);
124*8b26181fSAndroid Build Coastguard Worker #endif
125*8b26181fSAndroid Build Coastguard Worker }
126*8b26181fSAndroid Build Coastguard Worker 
127*8b26181fSAndroid Build Coastguard Worker static THREAD_FUNC_RETURN_TYPE
capture_thread_func(THREAD_FUNC_ARG_TYPE arg)128*8b26181fSAndroid Build Coastguard Worker capture_thread_func(THREAD_FUNC_ARG_TYPE arg)
129*8b26181fSAndroid Build Coastguard Worker {
130*8b26181fSAndroid Build Coastguard Worker 	char *device = arg;
131*8b26181fSAndroid Build Coastguard Worker 	int packet_count;
132*8b26181fSAndroid Build Coastguard Worker 	int status;
133*8b26181fSAndroid Build Coastguard Worker #ifndef _WIN32
134*8b26181fSAndroid Build Coastguard Worker 	struct sigaction action;
135*8b26181fSAndroid Build Coastguard Worker 	sigset_t mask;
136*8b26181fSAndroid Build Coastguard Worker #endif
137*8b26181fSAndroid Build Coastguard Worker 
138*8b26181fSAndroid Build Coastguard Worker #ifndef _WIN32
139*8b26181fSAndroid Build Coastguard Worker 	sigemptyset(&mask);
140*8b26181fSAndroid Build Coastguard Worker 	action.sa_handler = catch_sigusr1;
141*8b26181fSAndroid Build Coastguard Worker 	action.sa_mask = mask;
142*8b26181fSAndroid Build Coastguard Worker 	action.sa_flags = 0;
143*8b26181fSAndroid Build Coastguard Worker 	if (sigaction(SIGUSR1, &action, NULL) == -1)
144*8b26181fSAndroid Build Coastguard Worker 		error("Can't catch SIGUSR1: %s", strerror(errno));
145*8b26181fSAndroid Build Coastguard Worker #endif
146*8b26181fSAndroid Build Coastguard Worker 
147*8b26181fSAndroid Build Coastguard Worker 	printf("Listening on %s\n", device);
148*8b26181fSAndroid Build Coastguard Worker 	for (;;) {
149*8b26181fSAndroid Build Coastguard Worker 		packet_count = 0;
150*8b26181fSAndroid Build Coastguard Worker 		status = pcap_dispatch(pd, -1, countme,
151*8b26181fSAndroid Build Coastguard Worker 		    (u_char *)&packet_count);
152*8b26181fSAndroid Build Coastguard Worker 		if (status < 0)
153*8b26181fSAndroid Build Coastguard Worker 			break;
154*8b26181fSAndroid Build Coastguard Worker 		if (status != 0) {
155*8b26181fSAndroid Build Coastguard Worker 			printf("%d packets seen, %d packets counted after pcap_dispatch returns\n",
156*8b26181fSAndroid Build Coastguard Worker 			    status, packet_count);
157*8b26181fSAndroid Build Coastguard Worker 		} else
158*8b26181fSAndroid Build Coastguard Worker 			printf("No packets seen by pcap_dispatch\n");
159*8b26181fSAndroid Build Coastguard Worker 	}
160*8b26181fSAndroid Build Coastguard Worker 	if (status == PCAP_ERROR_BREAK) {
161*8b26181fSAndroid Build Coastguard Worker 		/*
162*8b26181fSAndroid Build Coastguard Worker 		 * We got interrupted, so perhaps we didn't
163*8b26181fSAndroid Build Coastguard Worker 		 * manage to finish a line we were printing.
164*8b26181fSAndroid Build Coastguard Worker 		 * Print an extra newline, just in case.
165*8b26181fSAndroid Build Coastguard Worker 		 */
166*8b26181fSAndroid Build Coastguard Worker 		putchar('\n');
167*8b26181fSAndroid Build Coastguard Worker 		printf("Loop got broken\n");
168*8b26181fSAndroid Build Coastguard Worker 	}
169*8b26181fSAndroid Build Coastguard Worker 	(void)fflush(stdout);
170*8b26181fSAndroid Build Coastguard Worker 	if (status == PCAP_ERROR) {
171*8b26181fSAndroid Build Coastguard Worker 		/*
172*8b26181fSAndroid Build Coastguard Worker 		 * Error.  Report it.
173*8b26181fSAndroid Build Coastguard Worker 		 */
174*8b26181fSAndroid Build Coastguard Worker 		(void)fprintf(stderr, "%s: pcap_dispatch: %s\n",
175*8b26181fSAndroid Build Coastguard Worker 		    program_name, pcap_geterr(pd));
176*8b26181fSAndroid Build Coastguard Worker 	}
177*8b26181fSAndroid Build Coastguard Worker 	return 0;
178*8b26181fSAndroid Build Coastguard Worker }
179*8b26181fSAndroid Build Coastguard Worker 
180*8b26181fSAndroid Build Coastguard Worker int
main(int argc,char ** argv)181*8b26181fSAndroid Build Coastguard Worker main(int argc, char **argv)
182*8b26181fSAndroid Build Coastguard Worker {
183*8b26181fSAndroid Build Coastguard Worker 	register int op;
184*8b26181fSAndroid Build Coastguard Worker 	register char *cp, *cmdbuf, *device;
185*8b26181fSAndroid Build Coastguard Worker 	int do_wakeup = 1;
186*8b26181fSAndroid Build Coastguard Worker 	pcap_if_t *devlist;
187*8b26181fSAndroid Build Coastguard Worker 	bpf_u_int32 localnet, netmask;
188*8b26181fSAndroid Build Coastguard Worker 	struct bpf_program fcode;
189*8b26181fSAndroid Build Coastguard Worker 	char ebuf[PCAP_ERRBUF_SIZE];
190*8b26181fSAndroid Build Coastguard Worker 	int status;
191*8b26181fSAndroid Build Coastguard Worker 	THREAD_HANDLE capture_thread;
192*8b26181fSAndroid Build Coastguard Worker #ifndef _WIN32
193*8b26181fSAndroid Build Coastguard Worker 	void *retval;
194*8b26181fSAndroid Build Coastguard Worker #endif
195*8b26181fSAndroid Build Coastguard Worker 
196*8b26181fSAndroid Build Coastguard Worker 	device = NULL;
197*8b26181fSAndroid Build Coastguard Worker 	if ((cp = strrchr(argv[0], '/')) != NULL)
198*8b26181fSAndroid Build Coastguard Worker 		program_name = cp + 1;
199*8b26181fSAndroid Build Coastguard Worker 	else
200*8b26181fSAndroid Build Coastguard Worker 		program_name = argv[0];
201*8b26181fSAndroid Build Coastguard Worker 
202*8b26181fSAndroid Build Coastguard Worker 	opterr = 0;
203*8b26181fSAndroid Build Coastguard Worker 	while ((op = getopt(argc, argv, "i:n")) != -1) {
204*8b26181fSAndroid Build Coastguard Worker 		switch (op) {
205*8b26181fSAndroid Build Coastguard Worker 
206*8b26181fSAndroid Build Coastguard Worker 		case 'i':
207*8b26181fSAndroid Build Coastguard Worker 			device = optarg;
208*8b26181fSAndroid Build Coastguard Worker 			break;
209*8b26181fSAndroid Build Coastguard Worker 
210*8b26181fSAndroid Build Coastguard Worker 		case 'n':
211*8b26181fSAndroid Build Coastguard Worker 			do_wakeup = 0;
212*8b26181fSAndroid Build Coastguard Worker 			break;
213*8b26181fSAndroid Build Coastguard Worker 
214*8b26181fSAndroid Build Coastguard Worker 		default:
215*8b26181fSAndroid Build Coastguard Worker 			usage();
216*8b26181fSAndroid Build Coastguard Worker 			/* NOTREACHED */
217*8b26181fSAndroid Build Coastguard Worker 		}
218*8b26181fSAndroid Build Coastguard Worker 	}
219*8b26181fSAndroid Build Coastguard Worker 
220*8b26181fSAndroid Build Coastguard Worker 	if (device == NULL) {
221*8b26181fSAndroid Build Coastguard Worker 		if (pcap_findalldevs(&devlist, ebuf) == -1)
222*8b26181fSAndroid Build Coastguard Worker 			error("%s", ebuf);
223*8b26181fSAndroid Build Coastguard Worker 		if (devlist == NULL)
224*8b26181fSAndroid Build Coastguard Worker 			error("no interfaces available for capture");
225*8b26181fSAndroid Build Coastguard Worker 		device = strdup(devlist->name);
226*8b26181fSAndroid Build Coastguard Worker 		pcap_freealldevs(devlist);
227*8b26181fSAndroid Build Coastguard Worker 	}
228*8b26181fSAndroid Build Coastguard Worker 	*ebuf = '\0';
229*8b26181fSAndroid Build Coastguard Worker 	pd = pcap_create(device, ebuf);
230*8b26181fSAndroid Build Coastguard Worker 	if (pd == NULL)
231*8b26181fSAndroid Build Coastguard Worker 		error("%s", ebuf);
232*8b26181fSAndroid Build Coastguard Worker 	status = pcap_set_snaplen(pd, 65535);
233*8b26181fSAndroid Build Coastguard Worker 	if (status != 0)
234*8b26181fSAndroid Build Coastguard Worker 		error("%s: pcap_set_snaplen failed: %s",
235*8b26181fSAndroid Build Coastguard Worker 			    device, pcap_statustostr(status));
236*8b26181fSAndroid Build Coastguard Worker 	status = pcap_set_timeout(pd, 5*60*1000);
237*8b26181fSAndroid Build Coastguard Worker 	if (status != 0)
238*8b26181fSAndroid Build Coastguard Worker 		error("%s: pcap_set_timeout failed: %s",
239*8b26181fSAndroid Build Coastguard Worker 		    device, pcap_statustostr(status));
240*8b26181fSAndroid Build Coastguard Worker 	status = pcap_activate(pd);
241*8b26181fSAndroid Build Coastguard Worker 	if (status < 0) {
242*8b26181fSAndroid Build Coastguard Worker 		/*
243*8b26181fSAndroid Build Coastguard Worker 		 * pcap_activate() failed.
244*8b26181fSAndroid Build Coastguard Worker 		 */
245*8b26181fSAndroid Build Coastguard Worker 		error("%s: %s\n(%s)", device,
246*8b26181fSAndroid Build Coastguard Worker 		    pcap_statustostr(status), pcap_geterr(pd));
247*8b26181fSAndroid Build Coastguard Worker 	} else if (status > 0) {
248*8b26181fSAndroid Build Coastguard Worker 		/*
249*8b26181fSAndroid Build Coastguard Worker 		 * pcap_activate() succeeded, but it's warning us
250*8b26181fSAndroid Build Coastguard Worker 		 * of a problem it had.
251*8b26181fSAndroid Build Coastguard Worker 		 */
252*8b26181fSAndroid Build Coastguard Worker 		warning("%s: %s\n(%s)", device,
253*8b26181fSAndroid Build Coastguard Worker 		    pcap_statustostr(status), pcap_geterr(pd));
254*8b26181fSAndroid Build Coastguard Worker 	}
255*8b26181fSAndroid Build Coastguard Worker 	if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
256*8b26181fSAndroid Build Coastguard Worker 		localnet = 0;
257*8b26181fSAndroid Build Coastguard Worker 		netmask = 0;
258*8b26181fSAndroid Build Coastguard Worker 		warning("%s", ebuf);
259*8b26181fSAndroid Build Coastguard Worker 	}
260*8b26181fSAndroid Build Coastguard Worker 	cmdbuf = copy_argv(&argv[optind]);
261*8b26181fSAndroid Build Coastguard Worker 
262*8b26181fSAndroid Build Coastguard Worker 	if (pcap_compile(pd, &fcode, cmdbuf, 1, netmask) < 0)
263*8b26181fSAndroid Build Coastguard Worker 		error("%s", pcap_geterr(pd));
264*8b26181fSAndroid Build Coastguard Worker 
265*8b26181fSAndroid Build Coastguard Worker 	if (pcap_setfilter(pd, &fcode) < 0)
266*8b26181fSAndroid Build Coastguard Worker 		error("%s", pcap_geterr(pd));
267*8b26181fSAndroid Build Coastguard Worker 
268*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
269*8b26181fSAndroid Build Coastguard Worker 	capture_thread = CreateThread(NULL, 0, capture_thread_func, device,
270*8b26181fSAndroid Build Coastguard Worker 	    0, NULL);
271*8b26181fSAndroid Build Coastguard Worker 	if (capture_thread == NULL)
272*8b26181fSAndroid Build Coastguard Worker 		error("Can't create capture thread: %s",
273*8b26181fSAndroid Build Coastguard Worker 		    win32_strerror(GetLastError()));
274*8b26181fSAndroid Build Coastguard Worker #else
275*8b26181fSAndroid Build Coastguard Worker 	status = pthread_create(&capture_thread, NULL, capture_thread_func,
276*8b26181fSAndroid Build Coastguard Worker 	    device);
277*8b26181fSAndroid Build Coastguard Worker 	if (status != 0)
278*8b26181fSAndroid Build Coastguard Worker 		error("Can't create capture thread: %s", strerror(status));
279*8b26181fSAndroid Build Coastguard Worker #endif
280*8b26181fSAndroid Build Coastguard Worker 	sleep_secs(60);
281*8b26181fSAndroid Build Coastguard Worker 	printf("Doing pcap_breakloop()\n");
282*8b26181fSAndroid Build Coastguard Worker 	pcap_breakloop(pd);
283*8b26181fSAndroid Build Coastguard Worker 	if (do_wakeup) {
284*8b26181fSAndroid Build Coastguard Worker 		/*
285*8b26181fSAndroid Build Coastguard Worker 		 * Force a wakeup in the capture thread.
286*8b26181fSAndroid Build Coastguard Worker 		 *
287*8b26181fSAndroid Build Coastguard Worker 		 * On some platforms, with some devices,, pcap_breakloop()
288*8b26181fSAndroid Build Coastguard Worker 		 * can't do that itself.  On Windows, poke the device's
289*8b26181fSAndroid Build Coastguard Worker 		 * event handle; on UN*X, send a SIGUSR1 to the thread.
290*8b26181fSAndroid Build Coastguard Worker 		 */
291*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
292*8b26181fSAndroid Build Coastguard Worker 		printf("Setting event\n");
293*8b26181fSAndroid Build Coastguard Worker 		if (!SetEvent(pcap_getevent(pd)))
294*8b26181fSAndroid Build Coastguard Worker 			error("Can't set event for pcap_t: %s",
295*8b26181fSAndroid Build Coastguard Worker 			    win32_strerror(GetLastError()));
296*8b26181fSAndroid Build Coastguard Worker #else
297*8b26181fSAndroid Build Coastguard Worker 		printf("Sending SIGUSR1\n");
298*8b26181fSAndroid Build Coastguard Worker 		status = pthread_kill(capture_thread, SIGUSR1);
299*8b26181fSAndroid Build Coastguard Worker 		if (status != 0)
300*8b26181fSAndroid Build Coastguard Worker 			warning("Can't interrupt capture thread: %s",
301*8b26181fSAndroid Build Coastguard Worker 			strerror(status));
302*8b26181fSAndroid Build Coastguard Worker #endif
303*8b26181fSAndroid Build Coastguard Worker 	}
304*8b26181fSAndroid Build Coastguard Worker 
305*8b26181fSAndroid Build Coastguard Worker 	/*
306*8b26181fSAndroid Build Coastguard Worker 	 * Now wait for the capture thread to terminate.
307*8b26181fSAndroid Build Coastguard Worker 	 */
308*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
309*8b26181fSAndroid Build Coastguard Worker 	if (WaitForSingleObject(capture_thread, INFINITE) == WAIT_FAILED)
310*8b26181fSAndroid Build Coastguard Worker 		error("Wait for thread termination failed: %s",
311*8b26181fSAndroid Build Coastguard Worker 		    win32_strerror(GetLastError()));
312*8b26181fSAndroid Build Coastguard Worker 	CloseHandle(capture_thread);
313*8b26181fSAndroid Build Coastguard Worker #else
314*8b26181fSAndroid Build Coastguard Worker 	status = pthread_join(capture_thread, &retval);
315*8b26181fSAndroid Build Coastguard Worker 	if (status != 0)
316*8b26181fSAndroid Build Coastguard Worker 		error("Wait for thread termination failed: %s",
317*8b26181fSAndroid Build Coastguard Worker 		    strerror(status));
318*8b26181fSAndroid Build Coastguard Worker #endif
319*8b26181fSAndroid Build Coastguard Worker 
320*8b26181fSAndroid Build Coastguard Worker 	pcap_close(pd);
321*8b26181fSAndroid Build Coastguard Worker 	pcap_freecode(&fcode);
322*8b26181fSAndroid Build Coastguard Worker 	exit(status == -1 ? 1 : 0);
323*8b26181fSAndroid Build Coastguard Worker }
324*8b26181fSAndroid Build Coastguard Worker 
325*8b26181fSAndroid Build Coastguard Worker static void
countme(u_char * user,const struct pcap_pkthdr * h _U_,const u_char * sp _U_)326*8b26181fSAndroid Build Coastguard Worker countme(u_char *user, const struct pcap_pkthdr *h _U_, const u_char *sp _U_)
327*8b26181fSAndroid Build Coastguard Worker {
328*8b26181fSAndroid Build Coastguard Worker 	int *counterp = (int *)user;
329*8b26181fSAndroid Build Coastguard Worker 
330*8b26181fSAndroid Build Coastguard Worker 	(*counterp)++;
331*8b26181fSAndroid Build Coastguard Worker }
332*8b26181fSAndroid Build Coastguard Worker 
333*8b26181fSAndroid Build Coastguard Worker static void
usage(void)334*8b26181fSAndroid Build Coastguard Worker usage(void)
335*8b26181fSAndroid Build Coastguard Worker {
336*8b26181fSAndroid Build Coastguard Worker 	(void)fprintf(stderr, "Usage: %s [ -n ] [ -i interface ] [ expression ]\n",
337*8b26181fSAndroid Build Coastguard Worker 	    program_name);
338*8b26181fSAndroid Build Coastguard Worker 	exit(1);
339*8b26181fSAndroid Build Coastguard Worker }
340*8b26181fSAndroid Build Coastguard Worker 
341*8b26181fSAndroid Build Coastguard Worker /* VARARGS */
342*8b26181fSAndroid Build Coastguard Worker static void
error(const char * fmt,...)343*8b26181fSAndroid Build Coastguard Worker error(const char *fmt, ...)
344*8b26181fSAndroid Build Coastguard Worker {
345*8b26181fSAndroid Build Coastguard Worker 	va_list ap;
346*8b26181fSAndroid Build Coastguard Worker 
347*8b26181fSAndroid Build Coastguard Worker 	(void)fprintf(stderr, "%s: ", program_name);
348*8b26181fSAndroid Build Coastguard Worker 	va_start(ap, fmt);
349*8b26181fSAndroid Build Coastguard Worker 	(void)vfprintf(stderr, fmt, ap);
350*8b26181fSAndroid Build Coastguard Worker 	va_end(ap);
351*8b26181fSAndroid Build Coastguard Worker 	if (*fmt) {
352*8b26181fSAndroid Build Coastguard Worker 		fmt += strlen(fmt);
353*8b26181fSAndroid Build Coastguard Worker 		if (fmt[-1] != '\n')
354*8b26181fSAndroid Build Coastguard Worker 			(void)fputc('\n', stderr);
355*8b26181fSAndroid Build Coastguard Worker 	}
356*8b26181fSAndroid Build Coastguard Worker 	exit(1);
357*8b26181fSAndroid Build Coastguard Worker 	/* NOTREACHED */
358*8b26181fSAndroid Build Coastguard Worker }
359*8b26181fSAndroid Build Coastguard Worker 
360*8b26181fSAndroid Build Coastguard Worker /* VARARGS */
361*8b26181fSAndroid Build Coastguard Worker static void
warning(const char * fmt,...)362*8b26181fSAndroid Build Coastguard Worker warning(const char *fmt, ...)
363*8b26181fSAndroid Build Coastguard Worker {
364*8b26181fSAndroid Build Coastguard Worker 	va_list ap;
365*8b26181fSAndroid Build Coastguard Worker 
366*8b26181fSAndroid Build Coastguard Worker 	(void)fprintf(stderr, "%s: WARNING: ", program_name);
367*8b26181fSAndroid Build Coastguard Worker 	va_start(ap, fmt);
368*8b26181fSAndroid Build Coastguard Worker 	(void)vfprintf(stderr, fmt, ap);
369*8b26181fSAndroid Build Coastguard Worker 	va_end(ap);
370*8b26181fSAndroid Build Coastguard Worker 	if (*fmt) {
371*8b26181fSAndroid Build Coastguard Worker 		fmt += strlen(fmt);
372*8b26181fSAndroid Build Coastguard Worker 		if (fmt[-1] != '\n')
373*8b26181fSAndroid Build Coastguard Worker 			(void)fputc('\n', stderr);
374*8b26181fSAndroid Build Coastguard Worker 	}
375*8b26181fSAndroid Build Coastguard Worker }
376*8b26181fSAndroid Build Coastguard Worker 
377*8b26181fSAndroid Build Coastguard Worker /*
378*8b26181fSAndroid Build Coastguard Worker  * Copy arg vector into a new buffer, concatenating arguments with spaces.
379*8b26181fSAndroid Build Coastguard Worker  */
380*8b26181fSAndroid Build Coastguard Worker static char *
copy_argv(register char ** argv)381*8b26181fSAndroid Build Coastguard Worker copy_argv(register char **argv)
382*8b26181fSAndroid Build Coastguard Worker {
383*8b26181fSAndroid Build Coastguard Worker 	register char **p;
384*8b26181fSAndroid Build Coastguard Worker 	register size_t len = 0;
385*8b26181fSAndroid Build Coastguard Worker 	char *buf;
386*8b26181fSAndroid Build Coastguard Worker 	char *src, *dst;
387*8b26181fSAndroid Build Coastguard Worker 
388*8b26181fSAndroid Build Coastguard Worker 	p = argv;
389*8b26181fSAndroid Build Coastguard Worker 	if (*p == 0)
390*8b26181fSAndroid Build Coastguard Worker 		return 0;
391*8b26181fSAndroid Build Coastguard Worker 
392*8b26181fSAndroid Build Coastguard Worker 	while (*p)
393*8b26181fSAndroid Build Coastguard Worker 		len += strlen(*p++) + 1;
394*8b26181fSAndroid Build Coastguard Worker 
395*8b26181fSAndroid Build Coastguard Worker 	buf = (char *)malloc(len);
396*8b26181fSAndroid Build Coastguard Worker 	if (buf == NULL)
397*8b26181fSAndroid Build Coastguard Worker 		error("copy_argv: malloc");
398*8b26181fSAndroid Build Coastguard Worker 
399*8b26181fSAndroid Build Coastguard Worker 	p = argv;
400*8b26181fSAndroid Build Coastguard Worker 	dst = buf;
401*8b26181fSAndroid Build Coastguard Worker 	while ((src = *p++) != NULL) {
402*8b26181fSAndroid Build Coastguard Worker 		while ((*dst++ = *src++) != '\0')
403*8b26181fSAndroid Build Coastguard Worker 			;
404*8b26181fSAndroid Build Coastguard Worker 		dst[-1] = ' ';
405*8b26181fSAndroid Build Coastguard Worker 	}
406*8b26181fSAndroid Build Coastguard Worker 	dst[-1] = '\0';
407*8b26181fSAndroid Build Coastguard Worker 
408*8b26181fSAndroid Build Coastguard Worker 	return buf;
409*8b26181fSAndroid Build Coastguard Worker }
410