xref: /minix/external/bsd/libpcap/dist/tests/opentest.c (revision bb9622b5)
1 /*	$NetBSD: opentest.c,v 1.2 2014/11/19 19:33:31 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that: (1) source code distributions
9  * retain the above copyright notice and this paragraph in its entirety, (2)
10  * distributions including binary code include the above copyright notice and
11  * this paragraph in its entirety in the documentation or other materials
12  * provided with the distribution, and (3) all advertising materials mentioning
13  * features or use of this software display the following acknowledgement:
14  * ``This product includes software developed by the University of California,
15  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16  * the University nor the names of its contributors may be used to endorse
17  * or promote products derived from this software without specific prior
18  * written permission.
19  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22  */
23 
24 #include <sys/cdefs.h>
25 __RCSID("$NetBSD: opentest.c,v 1.2 2014/11/19 19:33:31 christos Exp $");
26 
27 #ifndef lint
28 static const char copyright[] =
29     "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
30 The Regents of the University of California.  All rights reserved.\n";
31 #endif
32 
33 #include <pcap.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stdarg.h>
38 #include <unistd.h>
39 #include <errno.h>
40 
41 #define MAXIMUM_SNAPLEN		65535
42 
43 static char *program_name;
44 
45 /* Forwards */
46 static void usage(void) __attribute__((noreturn));
47 static void error(const char *, ...);
48 static void warning(const char *, ...);
49 
50 extern int optind;
51 extern int opterr;
52 extern char *optarg;
53 
54 int
55 main(int argc, char **argv)
56 {
57 	register int op;
58 	register char *cp, *device;
59 	int dorfmon, dopromisc, snaplen, useactivate, bufsize;
60 	char ebuf[PCAP_ERRBUF_SIZE];
61 	pcap_t *pd;
62 	int status = 0;
63 
64 	device = NULL;
65 	dorfmon = 0;
66 	dopromisc = 0;
67 	snaplen = MAXIMUM_SNAPLEN;
68 	bufsize = 0;
69 	useactivate = 0;
70 	if ((cp = strrchr(argv[0], '/')) != NULL)
71 		program_name = cp + 1;
72 	else
73 		program_name = argv[0];
74 
75 	opterr = 0;
76 	while ((op = getopt(argc, argv, "i:Ips:aB:")) != -1) {
77 		switch (op) {
78 
79 		case 'i':
80 			device = optarg;
81 			break;
82 
83 		case 'I':
84 			dorfmon = 1;
85 			useactivate = 1;	/* required for rfmon */
86 			break;
87 
88 		case 'p':
89 			dopromisc = 1;
90 			break;
91 
92 		case 's': {
93 			char *end;
94 
95 			snaplen = strtol(optarg, &end, 0);
96 			if (optarg == end || *end != '\0'
97 			    || snaplen < 0 || snaplen > MAXIMUM_SNAPLEN)
98 				error("invalid snaplen %s", optarg);
99 			else if (snaplen == 0)
100 				snaplen = MAXIMUM_SNAPLEN;
101 			break;
102 		}
103 
104 		case 'B':
105 			bufsize = atoi(optarg)*1024;
106 			if (bufsize <= 0)
107 				error("invalid packet buffer size %s", optarg);
108 			useactivate = 1;	/* required for bufsize */
109 			break;
110 
111 		case 'a':
112 			useactivate = 1;
113 			break;
114 
115 		default:
116 			usage();
117 			/* NOTREACHED */
118 		}
119 	}
120 
121 	if (useactivate) {
122 		pd = pcap_create(device, ebuf);
123 		if (pd == NULL)
124 			error("%s", ebuf);
125 		status = pcap_set_snaplen(pd, snaplen);
126 		if (status != 0)
127 			error("%s: pcap_set_snaplen failed: %s",
128 			    device, pcap_statustostr(status));
129 		if (dopromisc) {
130 			status = pcap_set_promisc(pd, 1);
131 			if (status != 0)
132 				error("%s: pcap_set_promisc failed: %s",
133 				    device, pcap_statustostr(status));
134 		}
135 		if (dorfmon) {
136 			status = pcap_set_rfmon(pd, 1);
137 			if (status != 0)
138 				error("%s: pcap_set_rfmon failed: %s",
139 				    device, pcap_statustostr(status));
140 		}
141 		status = pcap_set_timeout(pd, 1000);
142 		if (status != 0)
143 			error("%s: pcap_set_timeout failed: %s",
144 			    device, pcap_statustostr(status));
145 		if (bufsize != 0) {
146 			status = pcap_set_buffer_size(pd, bufsize);
147 			if (status != 0)
148 				error("%s: pcap_set_buffer_size failed: %s",
149 				    device, pcap_statustostr(status));
150 		}
151 		status = pcap_activate(pd);
152 		if (status < 0) {
153 			/*
154 			 * pcap_activate() failed.
155 			 */
156 			error("%s: %s\n(%s)", device,
157 			    pcap_statustostr(status), pcap_geterr(pd));
158 		} else if (status > 0) {
159 			/*
160 			 * pcap_activate() succeeded, but it's warning us
161 			 * of a problem it had.
162 			 */
163 			warning("%s: %s\n(%s)", device,
164 			    pcap_statustostr(status), pcap_geterr(pd));
165 		}
166 	} else {
167 		*ebuf = '\0';
168 		pd = pcap_open_live(device, 65535, 0, 1000, ebuf);
169 		if (pd == NULL)
170 			error("%s", ebuf);
171 		else if (*ebuf)
172 			warning("%s", ebuf);
173 	}
174 	pcap_close(pd);
175 	exit(status < 0 ? 1 : 0);
176 }
177 
178 static void
179 usage(void)
180 {
181 	(void)fprintf(stderr,
182 	    "Usage: %s [ -Ipa ] [ -i interface ] [ -s snaplen ] [ -B bufsize ]\n",
183 	    program_name);
184 	exit(1);
185 }
186 
187 /* VARARGS */
188 static void
189 error(const char *fmt, ...)
190 {
191 	va_list ap;
192 
193 	(void)fprintf(stderr, "%s: ", program_name);
194 	va_start(ap, fmt);
195 	(void)vfprintf(stderr, fmt, ap);
196 	va_end(ap);
197 	if (*fmt) {
198 		fmt += strlen(fmt);
199 		if (fmt[-1] != '\n')
200 			(void)fputc('\n', stderr);
201 	}
202 	exit(1);
203 	/* NOTREACHED */
204 }
205 
206 /* VARARGS */
207 static void
208 warning(const char *fmt, ...)
209 {
210 	va_list ap;
211 
212 	(void)fprintf(stderr, "%s: WARNING: ", program_name);
213 	va_start(ap, fmt);
214 	(void)vfprintf(stderr, fmt, ap);
215 	va_end(ap);
216 	if (*fmt) {
217 		fmt += strlen(fmt);
218 		if (fmt[-1] != '\n')
219 			(void)fputc('\n', stderr);
220 	}
221 }
222