xref: /freebsd/contrib/libpcap/testprogs/opentest.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 <pcap.h>
3157e22627SCy Schubert #include <stdio.h>
3257e22627SCy Schubert #include <stdlib.h>
3357e22627SCy Schubert #include <string.h>
3457e22627SCy Schubert #include <stdarg.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>
4157e22627SCy Schubert 
4257e22627SCy Schubert #include "pcap/funcattrs.h"
4357e22627SCy Schubert 
4457e22627SCy Schubert #ifdef _WIN32
4557e22627SCy Schubert   #include "portability.h"
4657e22627SCy Schubert #endif
4757e22627SCy Schubert 
48*6f9cba8fSJoseph Mingrone #define MAXIMUM_SNAPLEN		262144
4957e22627SCy Schubert 
5057e22627SCy Schubert static char *program_name;
5157e22627SCy Schubert 
5257e22627SCy Schubert /* Forwards */
5357e22627SCy Schubert static void PCAP_NORETURN usage(void);
5457e22627SCy Schubert static void PCAP_NORETURN error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
5557e22627SCy Schubert static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
5657e22627SCy Schubert 
5757e22627SCy Schubert int
main(int argc,char ** argv)5857e22627SCy Schubert main(int argc, char **argv)
5957e22627SCy Schubert {
6057e22627SCy Schubert 	register int op;
6157e22627SCy Schubert 	register char *cp, *device;
6257e22627SCy Schubert 	int dorfmon, dopromisc, snaplen, useactivate, bufsize;
6357e22627SCy Schubert 	char ebuf[PCAP_ERRBUF_SIZE];
6457e22627SCy Schubert 	pcap_if_t *devlist;
6557e22627SCy Schubert 	pcap_t *pd;
6657e22627SCy Schubert 	int status = 0;
6757e22627SCy Schubert 
6857e22627SCy Schubert 	device = NULL;
6957e22627SCy Schubert 	dorfmon = 0;
7057e22627SCy Schubert 	dopromisc = 0;
7157e22627SCy Schubert 	snaplen = MAXIMUM_SNAPLEN;
7257e22627SCy Schubert 	bufsize = 0;
7357e22627SCy Schubert 	useactivate = 0;
7457e22627SCy Schubert 	if ((cp = strrchr(argv[0], '/')) != NULL)
7557e22627SCy Schubert 		program_name = cp + 1;
7657e22627SCy Schubert 	else
7757e22627SCy Schubert 		program_name = argv[0];
7857e22627SCy Schubert 
7957e22627SCy Schubert 	opterr = 0;
8057e22627SCy Schubert 	while ((op = getopt(argc, argv, "i:Ips:aB:")) != -1) {
8157e22627SCy Schubert 		switch (op) {
8257e22627SCy Schubert 
8357e22627SCy Schubert 		case 'i':
84*6f9cba8fSJoseph Mingrone 			device = strdup(optarg);
8557e22627SCy Schubert 			break;
8657e22627SCy Schubert 
8757e22627SCy Schubert 		case 'I':
8857e22627SCy Schubert 			dorfmon = 1;
8957e22627SCy Schubert 			useactivate = 1;	/* required for rfmon */
9057e22627SCy Schubert 			break;
9157e22627SCy Schubert 
9257e22627SCy Schubert 		case 'p':
9357e22627SCy Schubert 			dopromisc = 1;
9457e22627SCy Schubert 			break;
9557e22627SCy Schubert 
9657e22627SCy Schubert 		case 's': {
9757e22627SCy Schubert 			char *end;
98*6f9cba8fSJoseph Mingrone 			long long_snaplen;
9957e22627SCy Schubert 
100*6f9cba8fSJoseph Mingrone 			long_snaplen = strtol(optarg, &end, 0);
10157e22627SCy Schubert 			if (optarg == end || *end != '\0'
102*6f9cba8fSJoseph Mingrone 			    || long_snaplen < 0
103*6f9cba8fSJoseph Mingrone 			    || long_snaplen > MAXIMUM_SNAPLEN)
10457e22627SCy Schubert 				error("invalid snaplen %s", optarg);
105*6f9cba8fSJoseph Mingrone 			else {
106*6f9cba8fSJoseph Mingrone 				if (snaplen == 0)
10757e22627SCy Schubert 					snaplen = MAXIMUM_SNAPLEN;
108*6f9cba8fSJoseph Mingrone 				else
109*6f9cba8fSJoseph Mingrone 					snaplen = (int)long_snaplen;
110*6f9cba8fSJoseph Mingrone 			}
11157e22627SCy Schubert 			break;
11257e22627SCy Schubert 		}
11357e22627SCy Schubert 
11457e22627SCy Schubert 		case 'B':
11557e22627SCy Schubert 			bufsize = atoi(optarg)*1024;
11657e22627SCy Schubert 			if (bufsize <= 0)
11757e22627SCy Schubert 				error("invalid packet buffer size %s", optarg);
11857e22627SCy Schubert 			useactivate = 1;	/* required for bufsize */
11957e22627SCy Schubert 			break;
12057e22627SCy Schubert 
12157e22627SCy Schubert 		case 'a':
12257e22627SCy Schubert 			useactivate = 1;
12357e22627SCy Schubert 			break;
12457e22627SCy Schubert 
12557e22627SCy Schubert 		default:
12657e22627SCy Schubert 			usage();
12757e22627SCy Schubert 			/* NOTREACHED */
12857e22627SCy Schubert 		}
12957e22627SCy Schubert 	}
13057e22627SCy Schubert 
13157e22627SCy Schubert 	if (device == NULL) {
13257e22627SCy Schubert 		if (pcap_findalldevs(&devlist, ebuf) == -1)
13357e22627SCy Schubert 			error("%s", ebuf);
13457e22627SCy Schubert 		if (devlist == NULL)
13557e22627SCy Schubert 			error("no interfaces available for capture");
13657e22627SCy Schubert 		device = strdup(devlist->name);
13757e22627SCy Schubert 		pcap_freealldevs(devlist);
13857e22627SCy Schubert 	}
13957e22627SCy Schubert 	if (useactivate) {
14057e22627SCy Schubert 		pd = pcap_create(device, ebuf);
14157e22627SCy Schubert 		if (pd == NULL)
14257e22627SCy Schubert 			error("%s: pcap_create failed: %s", device, ebuf);
14357e22627SCy Schubert 		status = pcap_set_snaplen(pd, snaplen);
14457e22627SCy Schubert 		if (status != 0)
14557e22627SCy Schubert 			error("%s: pcap_set_snaplen failed: %s",
14657e22627SCy Schubert 			    device, pcap_statustostr(status));
14757e22627SCy Schubert 		if (dopromisc) {
14857e22627SCy Schubert 			status = pcap_set_promisc(pd, 1);
14957e22627SCy Schubert 			if (status != 0)
15057e22627SCy Schubert 				error("%s: pcap_set_promisc failed: %s",
15157e22627SCy Schubert 				    device, pcap_statustostr(status));
15257e22627SCy Schubert 		}
15357e22627SCy Schubert 		if (dorfmon) {
15457e22627SCy Schubert 			status = pcap_set_rfmon(pd, 1);
15557e22627SCy Schubert 			if (status != 0)
15657e22627SCy Schubert 				error("%s: pcap_set_rfmon failed: %s",
15757e22627SCy Schubert 				    device, pcap_statustostr(status));
15857e22627SCy Schubert 		}
15957e22627SCy Schubert 		status = pcap_set_timeout(pd, 1000);
16057e22627SCy Schubert 		if (status != 0)
16157e22627SCy Schubert 			error("%s: pcap_set_timeout failed: %s",
16257e22627SCy Schubert 			    device, pcap_statustostr(status));
16357e22627SCy Schubert 		if (bufsize != 0) {
16457e22627SCy Schubert 			status = pcap_set_buffer_size(pd, bufsize);
16557e22627SCy Schubert 			if (status != 0)
16657e22627SCy Schubert 				error("%s: pcap_set_buffer_size failed: %s",
16757e22627SCy Schubert 				    device, pcap_statustostr(status));
16857e22627SCy Schubert 		}
16957e22627SCy Schubert 		status = pcap_activate(pd);
17057e22627SCy Schubert 		if (status < 0) {
17157e22627SCy Schubert 			/*
17257e22627SCy Schubert 			 * pcap_activate() failed.
17357e22627SCy Schubert 			 */
17457e22627SCy Schubert 			error("%s: %s\n(%s)", device,
17557e22627SCy Schubert 			    pcap_statustostr(status), pcap_geterr(pd));
17657e22627SCy Schubert 		} else if (status > 0) {
17757e22627SCy Schubert 			/*
17857e22627SCy Schubert 			 * pcap_activate() succeeded, but it's warning us
17957e22627SCy Schubert 			 * of a problem it had.
18057e22627SCy Schubert 			 */
18157e22627SCy Schubert 			warning("%s: %s\n(%s)", device,
18257e22627SCy Schubert 			    pcap_statustostr(status), pcap_geterr(pd));
18357e22627SCy Schubert 		} else
18457e22627SCy Schubert 			printf("%s opened successfully\n", device);
18557e22627SCy Schubert 	} else {
18657e22627SCy Schubert 		*ebuf = '\0';
18757e22627SCy Schubert 		pd = pcap_open_live(device, 65535, 0, 1000, ebuf);
18857e22627SCy Schubert 		if (pd == NULL)
18957e22627SCy Schubert 			error("%s", ebuf);
19057e22627SCy Schubert 		else if (*ebuf)
19157e22627SCy Schubert 			warning("%s", ebuf);
19257e22627SCy Schubert 		else
19357e22627SCy Schubert 			printf("%s opened successfully\n", device);
19457e22627SCy Schubert 	}
195*6f9cba8fSJoseph Mingrone 	free(device);
19657e22627SCy Schubert 	pcap_close(pd);
19757e22627SCy Schubert 	exit(status < 0 ? 1 : 0);
19857e22627SCy Schubert }
19957e22627SCy Schubert 
20057e22627SCy Schubert static void
usage(void)20157e22627SCy Schubert usage(void)
20257e22627SCy Schubert {
20357e22627SCy Schubert 	(void)fprintf(stderr,
20457e22627SCy Schubert 	    "Usage: %s [ -Ipa ] [ -i interface ] [ -s snaplen ] [ -B bufsize ]\n",
20557e22627SCy Schubert 	    program_name);
20657e22627SCy Schubert 	exit(1);
20757e22627SCy Schubert }
20857e22627SCy Schubert 
20957e22627SCy Schubert /* VARARGS */
21057e22627SCy Schubert static void
error(const char * fmt,...)21157e22627SCy Schubert error(const char *fmt, ...)
21257e22627SCy Schubert {
21357e22627SCy Schubert 	va_list ap;
21457e22627SCy Schubert 
21557e22627SCy Schubert 	(void)fprintf(stderr, "%s: ", program_name);
21657e22627SCy Schubert 	va_start(ap, fmt);
21757e22627SCy Schubert 	(void)vfprintf(stderr, fmt, ap);
21857e22627SCy Schubert 	va_end(ap);
21957e22627SCy Schubert 	if (*fmt) {
22057e22627SCy Schubert 		fmt += strlen(fmt);
22157e22627SCy Schubert 		if (fmt[-1] != '\n')
22257e22627SCy Schubert 			(void)fputc('\n', stderr);
22357e22627SCy Schubert 	}
22457e22627SCy Schubert 	exit(1);
22557e22627SCy Schubert 	/* NOTREACHED */
22657e22627SCy Schubert }
22757e22627SCy Schubert 
22857e22627SCy Schubert /* VARARGS */
22957e22627SCy Schubert static void
warning(const char * fmt,...)23057e22627SCy Schubert warning(const char *fmt, ...)
23157e22627SCy Schubert {
23257e22627SCy Schubert 	va_list ap;
23357e22627SCy Schubert 
23457e22627SCy Schubert 	(void)fprintf(stderr, "%s: WARNING: ", program_name);
23557e22627SCy Schubert 	va_start(ap, fmt);
23657e22627SCy Schubert 	(void)vfprintf(stderr, fmt, ap);
23757e22627SCy Schubert 	va_end(ap);
23857e22627SCy Schubert 	if (*fmt) {
23957e22627SCy Schubert 		fmt += strlen(fmt);
24057e22627SCy Schubert 		if (fmt[-1] != '\n')
24157e22627SCy Schubert 			(void)fputc('\n', stderr);
24257e22627SCy Schubert 	}
24357e22627SCy Schubert }
244