xref: /freebsd-src/contrib/libpcap/testprogs/capturetest.c (revision 6f9cba8f8b5efd16249633e52483ea351876b67b)
157e22627SCy Schubert /*
257e22627SCy Schubert  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
357e22627SCy Schubert  *	The Regents of the University of California.  All rights reserved.
457e22627SCy Schubert  *
557e22627SCy Schubert  * Redistribution and use in source and binary forms, with or without
657e22627SCy Schubert  * modification, are permitted provided that: (1) source code distributions
757e22627SCy Schubert  * retain the above copyright notice and this paragraph in its entirety, (2)
857e22627SCy Schubert  * distributions including binary code include the above copyright notice and
957e22627SCy Schubert  * this paragraph in its entirety in the documentation or other materials
1057e22627SCy Schubert  * provided with the distribution, and (3) all advertising materials mentioning
1157e22627SCy Schubert  * features or use of this software display the following acknowledgement:
1257e22627SCy Schubert  * ``This product includes software developed by the University of California,
1357e22627SCy Schubert  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1457e22627SCy Schubert  * the University nor the names of its contributors may be used to endorse
1557e22627SCy Schubert  * or promote products derived from this software without specific prior
1657e22627SCy Schubert  * written permission.
1757e22627SCy Schubert  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1857e22627SCy Schubert  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1957e22627SCy Schubert  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2057e22627SCy Schubert  */
2157e22627SCy Schubert 
2257e22627SCy Schubert #include "varattrs.h"
2357e22627SCy Schubert 
2457e22627SCy Schubert #ifndef lint
2557e22627SCy Schubert static const char copyright[] _U_ =
2657e22627SCy Schubert     "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
2757e22627SCy Schubert The Regents of the University of California.  All rights reserved.\n";
2857e22627SCy Schubert #endif
2957e22627SCy Schubert 
3057e22627SCy Schubert #include <stdio.h>
3157e22627SCy Schubert #include <stdlib.h>
3257e22627SCy Schubert #include <string.h>
3357e22627SCy Schubert #include <stdarg.h>
3457e22627SCy Schubert #include <limits.h>
3557e22627SCy Schubert #ifdef _WIN32
3657e22627SCy Schubert   #include "getopt.h"
3757e22627SCy Schubert #else
3857e22627SCy Schubert   #include <unistd.h>
3957e22627SCy Schubert #endif
4057e22627SCy Schubert #include <errno.h>
41*6f9cba8fSJoseph Mingrone #ifndef _WIN32
42*6f9cba8fSJoseph Mingrone   #include <signal.h>
43*6f9cba8fSJoseph Mingrone #endif
4457e22627SCy Schubert #include <sys/types.h>
4557e22627SCy Schubert 
4657e22627SCy Schubert #include <pcap.h>
4757e22627SCy Schubert 
4857e22627SCy Schubert #include "pcap/funcattrs.h"
4957e22627SCy Schubert 
5057e22627SCy Schubert #ifdef _WIN32
5157e22627SCy Schubert   #include "portability.h"
5257e22627SCy Schubert #endif
5357e22627SCy Schubert 
5457e22627SCy Schubert static char *program_name;
5557e22627SCy Schubert 
5657e22627SCy Schubert /* Forwards */
5757e22627SCy Schubert static void countme(u_char *, const struct pcap_pkthdr *, const u_char *);
5857e22627SCy Schubert static void PCAP_NORETURN usage(void);
5957e22627SCy Schubert static void PCAP_NORETURN error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
6057e22627SCy Schubert static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
6157e22627SCy Schubert static char *copy_argv(char **);
6257e22627SCy Schubert 
6357e22627SCy Schubert static pcap_t *pd;
64*6f9cba8fSJoseph Mingrone #ifndef _WIN32
65*6f9cba8fSJoseph Mingrone static int breaksigint = 0;
66*6f9cba8fSJoseph Mingrone #endif
67*6f9cba8fSJoseph Mingrone 
68*6f9cba8fSJoseph Mingrone #ifndef _WIN32
69*6f9cba8fSJoseph Mingrone static void
sigint_handler(int signum _U_)70*6f9cba8fSJoseph Mingrone sigint_handler(int signum _U_)
71*6f9cba8fSJoseph Mingrone {
72*6f9cba8fSJoseph Mingrone 	if (breaksigint)
73*6f9cba8fSJoseph Mingrone 		pcap_breakloop(pd);
74*6f9cba8fSJoseph Mingrone }
75*6f9cba8fSJoseph Mingrone #endif
76*6f9cba8fSJoseph Mingrone 
77*6f9cba8fSJoseph Mingrone #ifdef _WIN32
78*6f9cba8fSJoseph Mingrone /*
79*6f9cba8fSJoseph Mingrone  * We don't have UN*X-style signals, so we don't have anything to test.
80*6f9cba8fSJoseph Mingrone  */
81*6f9cba8fSJoseph Mingrone #define B_OPTION	""
82*6f9cba8fSJoseph Mingrone #define R_OPTION	""
83*6f9cba8fSJoseph Mingrone #define S_OPTION	""
84*6f9cba8fSJoseph Mingrone #else
85*6f9cba8fSJoseph Mingrone /*
86*6f9cba8fSJoseph Mingrone  * We do have UN*X-style signals (we assume that "not Windows" means "UN*X").
87*6f9cba8fSJoseph Mingrone  */
88*6f9cba8fSJoseph Mingrone #define B_OPTION	"b"
89*6f9cba8fSJoseph Mingrone #define R_OPTION	"r"
90*6f9cba8fSJoseph Mingrone #define S_OPTION	"s"
91*6f9cba8fSJoseph Mingrone #endif
92*6f9cba8fSJoseph Mingrone 
93*6f9cba8fSJoseph Mingrone #define COMMAND_OPTIONS	B_OPTION "i:mn" R_OPTION S_OPTION "t:"
94*6f9cba8fSJoseph Mingrone #define USAGE_OPTIONS	"-" B_OPTION "mn" R_OPTION S_OPTION
9557e22627SCy Schubert 
9657e22627SCy Schubert int
main(int argc,char ** argv)9757e22627SCy Schubert main(int argc, char **argv)
9857e22627SCy Schubert {
9957e22627SCy Schubert 	register int op;
10057e22627SCy Schubert 	register char *cp, *cmdbuf, *device;
10157e22627SCy Schubert 	long longarg;
10257e22627SCy Schubert 	char *p;
10357e22627SCy Schubert 	int timeout = 1000;
10457e22627SCy Schubert 	int immediate = 0;
10557e22627SCy Schubert 	int nonblock = 0;
106*6f9cba8fSJoseph Mingrone #ifndef _WIN32
107*6f9cba8fSJoseph Mingrone 	int sigrestart = 0;
108*6f9cba8fSJoseph Mingrone 	int catchsigint = 0;
109*6f9cba8fSJoseph Mingrone #endif
11057e22627SCy Schubert 	pcap_if_t *devlist;
11157e22627SCy Schubert 	bpf_u_int32 localnet, netmask;
11257e22627SCy Schubert 	struct bpf_program fcode;
11357e22627SCy Schubert 	char ebuf[PCAP_ERRBUF_SIZE];
11457e22627SCy Schubert 	int status;
11557e22627SCy Schubert 	int packet_count;
11657e22627SCy Schubert 
11757e22627SCy Schubert 	device = NULL;
11857e22627SCy Schubert 	if ((cp = strrchr(argv[0], '/')) != NULL)
11957e22627SCy Schubert 		program_name = cp + 1;
12057e22627SCy Schubert 	else
12157e22627SCy Schubert 		program_name = argv[0];
12257e22627SCy Schubert 
12357e22627SCy Schubert 	opterr = 0;
124*6f9cba8fSJoseph Mingrone 	while ((op = getopt(argc, argv, COMMAND_OPTIONS)) != -1) {
12557e22627SCy Schubert 		switch (op) {
12657e22627SCy Schubert 
127*6f9cba8fSJoseph Mingrone #ifndef _WIN32
128*6f9cba8fSJoseph Mingrone 		case 'b':
129*6f9cba8fSJoseph Mingrone 			breaksigint = 1;
130*6f9cba8fSJoseph Mingrone 			break;
131*6f9cba8fSJoseph Mingrone #endif
132*6f9cba8fSJoseph Mingrone 
13357e22627SCy Schubert 		case 'i':
13457e22627SCy Schubert 			device = optarg;
13557e22627SCy Schubert 			break;
13657e22627SCy Schubert 
13757e22627SCy Schubert 		case 'm':
13857e22627SCy Schubert 			immediate = 1;
13957e22627SCy Schubert 			break;
14057e22627SCy Schubert 
14157e22627SCy Schubert 		case 'n':
14257e22627SCy Schubert 			nonblock = 1;
14357e22627SCy Schubert 			break;
14457e22627SCy Schubert 
145*6f9cba8fSJoseph Mingrone #ifndef _WIN32
146*6f9cba8fSJoseph Mingrone 		case 'r':
147*6f9cba8fSJoseph Mingrone 			sigrestart = 1;
148*6f9cba8fSJoseph Mingrone 			break;
149*6f9cba8fSJoseph Mingrone 
150*6f9cba8fSJoseph Mingrone 		case 's':
151*6f9cba8fSJoseph Mingrone 			catchsigint = 1;
152*6f9cba8fSJoseph Mingrone 			break;
153*6f9cba8fSJoseph Mingrone #endif
154*6f9cba8fSJoseph Mingrone 
15557e22627SCy Schubert 		case 't':
15657e22627SCy Schubert 			longarg = strtol(optarg, &p, 10);
15757e22627SCy Schubert 			if (p == optarg || *p != '\0') {
15857e22627SCy Schubert 				error("Timeout value \"%s\" is not a number",
15957e22627SCy Schubert 				    optarg);
16057e22627SCy Schubert 				/* NOTREACHED */
16157e22627SCy Schubert 			}
16257e22627SCy Schubert 			if (longarg < 0) {
16357e22627SCy Schubert 				error("Timeout value %ld is negative", longarg);
16457e22627SCy Schubert 				/* NOTREACHED */
16557e22627SCy Schubert 			}
16657e22627SCy Schubert 			if (longarg > INT_MAX) {
16757e22627SCy Schubert 				error("Timeout value %ld is too large (> %d)",
16857e22627SCy Schubert 				    longarg, INT_MAX);
16957e22627SCy Schubert 				/* NOTREACHED */
17057e22627SCy Schubert 			}
17157e22627SCy Schubert 			timeout = (int)longarg;
17257e22627SCy Schubert 			break;
17357e22627SCy Schubert 
17457e22627SCy Schubert 		default:
17557e22627SCy Schubert 			usage();
17657e22627SCy Schubert 			/* NOTREACHED */
17757e22627SCy Schubert 		}
17857e22627SCy Schubert 	}
17957e22627SCy Schubert 
18057e22627SCy Schubert 	if (device == NULL) {
18157e22627SCy Schubert 		if (pcap_findalldevs(&devlist, ebuf) == -1)
18257e22627SCy Schubert 			error("%s", ebuf);
18357e22627SCy Schubert 		if (devlist == NULL)
18457e22627SCy Schubert 			error("no interfaces available for capture");
18557e22627SCy Schubert 		device = strdup(devlist->name);
18657e22627SCy Schubert 		pcap_freealldevs(devlist);
18757e22627SCy Schubert 	}
18857e22627SCy Schubert 	*ebuf = '\0';
189*6f9cba8fSJoseph Mingrone 
190*6f9cba8fSJoseph Mingrone #ifndef _WIN32
191*6f9cba8fSJoseph Mingrone 	/*
192*6f9cba8fSJoseph Mingrone 	 * If we were told to catch SIGINT, do so.
193*6f9cba8fSJoseph Mingrone 	 */
194*6f9cba8fSJoseph Mingrone 	if (catchsigint) {
195*6f9cba8fSJoseph Mingrone 		struct sigaction action;
196*6f9cba8fSJoseph Mingrone 
197*6f9cba8fSJoseph Mingrone 		action.sa_handler = sigint_handler;
198*6f9cba8fSJoseph Mingrone 		sigemptyset(&action.sa_mask);
199*6f9cba8fSJoseph Mingrone 
200*6f9cba8fSJoseph Mingrone 		/*
201*6f9cba8fSJoseph Mingrone 		 * Should SIGINT interrupt, or restart, system calls?
202*6f9cba8fSJoseph Mingrone 		 */
203*6f9cba8fSJoseph Mingrone 		action.sa_flags = sigrestart ? SA_RESTART : 0;
204*6f9cba8fSJoseph Mingrone 
205*6f9cba8fSJoseph Mingrone 		if (sigaction(SIGINT, &action, NULL) == -1)
206*6f9cba8fSJoseph Mingrone 			error("Can't catch SIGINT: %s\n",
207*6f9cba8fSJoseph Mingrone 			    strerror(errno));
208*6f9cba8fSJoseph Mingrone 	}
209*6f9cba8fSJoseph Mingrone #endif
210*6f9cba8fSJoseph Mingrone 
21157e22627SCy Schubert 	pd = pcap_create(device, ebuf);
21257e22627SCy Schubert 	if (pd == NULL)
21357e22627SCy Schubert 		error("%s", ebuf);
21457e22627SCy Schubert 	status = pcap_set_snaplen(pd, 65535);
21557e22627SCy Schubert 	if (status != 0)
21657e22627SCy Schubert 		error("%s: pcap_set_snaplen failed: %s",
21757e22627SCy Schubert 			    device, pcap_statustostr(status));
21857e22627SCy Schubert 	if (immediate) {
21957e22627SCy Schubert 		status = pcap_set_immediate_mode(pd, 1);
22057e22627SCy Schubert 		if (status != 0)
22157e22627SCy Schubert 			error("%s: pcap_set_immediate_mode failed: %s",
22257e22627SCy Schubert 			    device, pcap_statustostr(status));
22357e22627SCy Schubert 	}
22457e22627SCy Schubert 	status = pcap_set_timeout(pd, timeout);
22557e22627SCy Schubert 	if (status != 0)
22657e22627SCy Schubert 		error("%s: pcap_set_timeout failed: %s",
22757e22627SCy Schubert 		    device, pcap_statustostr(status));
22857e22627SCy Schubert 	status = pcap_activate(pd);
22957e22627SCy Schubert 	if (status < 0) {
23057e22627SCy Schubert 		/*
23157e22627SCy Schubert 		 * pcap_activate() failed.
23257e22627SCy Schubert 		 */
23357e22627SCy Schubert 		error("%s: %s\n(%s)", device,
23457e22627SCy Schubert 		    pcap_statustostr(status), pcap_geterr(pd));
23557e22627SCy Schubert 	} else if (status > 0) {
23657e22627SCy Schubert 		/*
23757e22627SCy Schubert 		 * pcap_activate() succeeded, but it's warning us
23857e22627SCy Schubert 		 * of a problem it had.
23957e22627SCy Schubert 		 */
24057e22627SCy Schubert 		warning("%s: %s\n(%s)", device,
24157e22627SCy Schubert 		    pcap_statustostr(status), pcap_geterr(pd));
24257e22627SCy Schubert 	}
24357e22627SCy Schubert 	if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
24457e22627SCy Schubert 		localnet = 0;
24557e22627SCy Schubert 		netmask = 0;
24657e22627SCy Schubert 		warning("%s", ebuf);
24757e22627SCy Schubert 	}
24857e22627SCy Schubert 	cmdbuf = copy_argv(&argv[optind]);
24957e22627SCy Schubert 
25057e22627SCy Schubert 	if (pcap_compile(pd, &fcode, cmdbuf, 1, netmask) < 0)
25157e22627SCy Schubert 		error("%s", pcap_geterr(pd));
25257e22627SCy Schubert 
25357e22627SCy Schubert 	if (pcap_setfilter(pd, &fcode) < 0)
25457e22627SCy Schubert 		error("%s", pcap_geterr(pd));
25557e22627SCy Schubert 	if (pcap_setnonblock(pd, nonblock, ebuf) == -1)
25657e22627SCy Schubert 		error("pcap_setnonblock failed: %s", ebuf);
25757e22627SCy Schubert 	printf("Listening on %s\n", device);
25857e22627SCy Schubert 	for (;;) {
25957e22627SCy Schubert 		packet_count = 0;
26057e22627SCy Schubert 		status = pcap_dispatch(pd, -1, countme,
26157e22627SCy Schubert 		    (u_char *)&packet_count);
26257e22627SCy Schubert 		if (status < 0)
26357e22627SCy Schubert 			break;
26457e22627SCy Schubert 		if (status != 0) {
26557e22627SCy Schubert 			printf("%d packets seen, %d packets counted after pcap_dispatch returns\n",
26657e22627SCy Schubert 			    status, packet_count);
267*6f9cba8fSJoseph Mingrone 			struct pcap_stat ps;
268*6f9cba8fSJoseph Mingrone 			pcap_stats(pd, &ps);
269*6f9cba8fSJoseph Mingrone 			printf("%d ps_recv, %d ps_drop, %d ps_ifdrop\n",
270*6f9cba8fSJoseph Mingrone 			    ps.ps_recv, ps.ps_drop, ps.ps_ifdrop);
27157e22627SCy Schubert 		}
27257e22627SCy Schubert 	}
27357e22627SCy Schubert 	if (status == -2) {
27457e22627SCy Schubert 		/*
27557e22627SCy Schubert 		 * We got interrupted, so perhaps we didn't
27657e22627SCy Schubert 		 * manage to finish a line we were printing.
27757e22627SCy Schubert 		 * Print an extra newline, just in case.
27857e22627SCy Schubert 		 */
27957e22627SCy Schubert 		putchar('\n');
280*6f9cba8fSJoseph Mingrone 		printf("Broken out of loop from SIGINT handler\n");
28157e22627SCy Schubert 	}
28257e22627SCy Schubert 	(void)fflush(stdout);
28357e22627SCy Schubert 	if (status == -1) {
28457e22627SCy Schubert 		/*
28557e22627SCy Schubert 		 * Error.  Report it.
28657e22627SCy Schubert 		 */
287*6f9cba8fSJoseph Mingrone 		(void)fprintf(stderr, "%s: pcap_dispatch: %s\n",
28857e22627SCy Schubert 		    program_name, pcap_geterr(pd));
28957e22627SCy Schubert 	}
29057e22627SCy Schubert 	pcap_close(pd);
29157e22627SCy Schubert 	pcap_freecode(&fcode);
29257e22627SCy Schubert 	free(cmdbuf);
29357e22627SCy Schubert 	exit(status == -1 ? 1 : 0);
29457e22627SCy Schubert }
29557e22627SCy Schubert 
29657e22627SCy Schubert static void
countme(u_char * user,const struct pcap_pkthdr * h _U_,const u_char * sp _U_)29757e22627SCy Schubert countme(u_char *user, const struct pcap_pkthdr *h _U_, const u_char *sp _U_)
29857e22627SCy Schubert {
29957e22627SCy Schubert 	int *counterp = (int *)user;
30057e22627SCy Schubert 
30157e22627SCy Schubert 	(*counterp)++;
30257e22627SCy Schubert }
30357e22627SCy Schubert 
30457e22627SCy Schubert static void
usage(void)30557e22627SCy Schubert usage(void)
30657e22627SCy Schubert {
307*6f9cba8fSJoseph Mingrone 	(void)fprintf(stderr, "Usage: %s [ " USAGE_OPTIONS " ] [ -i interface ] [ -t timeout] [expression]\n",
30857e22627SCy Schubert 	    program_name);
30957e22627SCy Schubert 	exit(1);
31057e22627SCy Schubert }
31157e22627SCy Schubert 
31257e22627SCy Schubert /* VARARGS */
31357e22627SCy Schubert static void
error(const char * fmt,...)31457e22627SCy Schubert error(const char *fmt, ...)
31557e22627SCy Schubert {
31657e22627SCy Schubert 	va_list ap;
31757e22627SCy Schubert 
31857e22627SCy Schubert 	(void)fprintf(stderr, "%s: ", program_name);
31957e22627SCy Schubert 	va_start(ap, fmt);
32057e22627SCy Schubert 	(void)vfprintf(stderr, fmt, ap);
32157e22627SCy Schubert 	va_end(ap);
32257e22627SCy Schubert 	if (*fmt) {
32357e22627SCy Schubert 		fmt += strlen(fmt);
32457e22627SCy Schubert 		if (fmt[-1] != '\n')
32557e22627SCy Schubert 			(void)fputc('\n', stderr);
32657e22627SCy Schubert 	}
32757e22627SCy Schubert 	exit(1);
32857e22627SCy Schubert 	/* NOTREACHED */
32957e22627SCy Schubert }
33057e22627SCy Schubert 
33157e22627SCy Schubert /* VARARGS */
33257e22627SCy Schubert static void
warning(const char * fmt,...)33357e22627SCy Schubert warning(const char *fmt, ...)
33457e22627SCy Schubert {
33557e22627SCy Schubert 	va_list ap;
33657e22627SCy Schubert 
33757e22627SCy Schubert 	(void)fprintf(stderr, "%s: WARNING: ", program_name);
33857e22627SCy Schubert 	va_start(ap, fmt);
33957e22627SCy Schubert 	(void)vfprintf(stderr, fmt, ap);
34057e22627SCy Schubert 	va_end(ap);
34157e22627SCy Schubert 	if (*fmt) {
34257e22627SCy Schubert 		fmt += strlen(fmt);
34357e22627SCy Schubert 		if (fmt[-1] != '\n')
34457e22627SCy Schubert 			(void)fputc('\n', stderr);
34557e22627SCy Schubert 	}
34657e22627SCy Schubert }
34757e22627SCy Schubert 
34857e22627SCy Schubert /*
34957e22627SCy Schubert  * Copy arg vector into a new buffer, concatenating arguments with spaces.
35057e22627SCy Schubert  */
35157e22627SCy Schubert static char *
copy_argv(register char ** argv)35257e22627SCy Schubert copy_argv(register char **argv)
35357e22627SCy Schubert {
35457e22627SCy Schubert 	register char **p;
355*6f9cba8fSJoseph Mingrone 	register size_t len = 0;
35657e22627SCy Schubert 	char *buf;
35757e22627SCy Schubert 	char *src, *dst;
35857e22627SCy Schubert 
35957e22627SCy Schubert 	p = argv;
36057e22627SCy Schubert 	if (*p == 0)
36157e22627SCy Schubert 		return 0;
36257e22627SCy Schubert 
36357e22627SCy Schubert 	while (*p)
36457e22627SCy Schubert 		len += strlen(*p++) + 1;
36557e22627SCy Schubert 
36657e22627SCy Schubert 	buf = (char *)malloc(len);
36757e22627SCy Schubert 	if (buf == NULL)
36857e22627SCy Schubert 		error("copy_argv: malloc");
36957e22627SCy Schubert 
37057e22627SCy Schubert 	p = argv;
37157e22627SCy Schubert 	dst = buf;
37257e22627SCy Schubert 	while ((src = *p++) != NULL) {
37357e22627SCy Schubert 		while ((*dst++ = *src++) != '\0')
37457e22627SCy Schubert 			;
37557e22627SCy Schubert 		dst[-1] = ' ';
37657e22627SCy Schubert 	}
37757e22627SCy Schubert 	dst[-1] = '\0';
37857e22627SCy Schubert 
37957e22627SCy Schubert 	return buf;
38057e22627SCy Schubert }
381