• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that: (1) source code distributions
7  * retain the above copyright notice and this paragraph in its entirety, (2)
8  * distributions including binary code include the above copyright notice and
9  * this paragraph in its entirety in the documentation or other materials
10  * provided with the distribution, and (3) all advertising materials mentioning
11  * features or use of this software display the following acknowledgement:
12  * ``This product includes software developed by the University of California,
13  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14  * the University nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior
16  * written permission.
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20  */
21 
22 #ifndef lint
23 static const char copyright[] _U_ =
24     "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
25 The Regents of the University of California.  All rights reserved.\n";
26 #endif
27 
28 #include <pcap.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdarg.h>
33 #ifdef _WIN32
34 #include "getopt.h"
35 #else
36 #include <unistd.h>
37 #endif
38 #include <errno.h>
39 
40 #define MAXIMUM_SNAPLEN		65535
41 
42 static char *program_name;
43 
44 /*
45  * This was introduced by Clang:
46  *
47  *     http://clang.llvm.org/docs/LanguageExtensions.html#has-attribute
48  *
49  * in some version (which version?); it has been picked up by GCC 5.0.
50  */
51 #ifndef __has_attribute
52   /*
53    * It's a macro, so you can check whether it's defined to check
54    * whether it's supported.
55    *
56    * If it's not, define it to always return 0, so that we move on to
57    * the fallback checks.
58    */
59   #define __has_attribute(x) 0
60 #endif
61 
62 #if __has_attribute(noreturn) \
63     || (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 205)) \
64     || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590)) \
65     || (defined(__xlC__) && __xlC__ >= 0x0A01) \
66     || (defined(__HP_aCC) && __HP_aCC >= 61000)
67   /*
68    * Compiler with support for it, or GCC 2.5 and later, or Solaris Studio 12
69    * (Sun C 5.9) and later, or IBM XL C 10.1 and later (do any earlier
70    * versions of XL C support this?), or HP aCC A.06.10 and later.
71    */
72   #define PCAP_NORETURN __attribute((noreturn))
73 #elif defined( _MSC_VER )
74   #define PCAP_NORETURN __declspec(noreturn)
75 #else
76   #define PCAP_NORETURN
77 #endif
78 
79 #if __has_attribute(__format__) \
80     || (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 203)) \
81     || (defined(__xlC__) && __xlC__ >= 0x0A01) \
82     || (defined(__HP_aCC) && __HP_aCC >= 61000)
83   /*
84    * Compiler with support for it, or GCC 2.3 and later, or IBM XL C 10.1
85    * and later (do any earlier versions of XL C support this?),
86    * or HP aCC A.06.10 and later.
87    */
88   #define PCAP_PRINTFLIKE(x,y) __attribute__((__format__(__printf__,x,y)))
89 #else
90   #define PCAP_PRINTFLIKE(x,y)
91 #endif
92 
93 /* Forwards */
94 static void PCAP_NORETURN usage(void);
95 static void PCAP_NORETURN error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
96 static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
97 
98 int
main(int argc,char ** argv)99 main(int argc, char **argv)
100 {
101 	register int op;
102 	register char *cp, *device;
103 	int dorfmon, dopromisc, snaplen, useactivate, bufsize;
104 	char ebuf[PCAP_ERRBUF_SIZE];
105 	pcap_t *pd;
106 	int status = 0;
107 
108 	device = NULL;
109 	dorfmon = 0;
110 	dopromisc = 0;
111 	snaplen = MAXIMUM_SNAPLEN;
112 	bufsize = 0;
113 	useactivate = 0;
114 	if ((cp = strrchr(argv[0], '/')) != NULL)
115 		program_name = cp + 1;
116 	else
117 		program_name = argv[0];
118 
119 	opterr = 0;
120 	while ((op = getopt(argc, argv, "i:Ips:aB:")) != -1) {
121 		switch (op) {
122 
123 		case 'i':
124 			device = optarg;
125 			break;
126 
127 		case 'I':
128 			dorfmon = 1;
129 			useactivate = 1;	/* required for rfmon */
130 			break;
131 
132 		case 'p':
133 			dopromisc = 1;
134 			break;
135 
136 		case 's': {
137 			char *end;
138 
139 			snaplen = strtol(optarg, &end, 0);
140 			if (optarg == end || *end != '\0'
141 			    || snaplen < 0 || snaplen > MAXIMUM_SNAPLEN)
142 				error("invalid snaplen %s", optarg);
143 			else if (snaplen == 0)
144 				snaplen = MAXIMUM_SNAPLEN;
145 			break;
146 		}
147 
148 		case 'B':
149 			bufsize = atoi(optarg)*1024;
150 			if (bufsize <= 0)
151 				error("invalid packet buffer size %s", optarg);
152 			useactivate = 1;	/* required for bufsize */
153 			break;
154 
155 		case 'a':
156 			useactivate = 1;
157 			break;
158 
159 		default:
160 			usage();
161 			/* NOTREACHED */
162 		}
163 	}
164 
165 	if (device == NULL) {
166 		device = pcap_lookupdev(ebuf);
167 		if (device == NULL)
168 			error("pcap_lookupdev failed: %s", ebuf);
169 	}
170 	if (useactivate) {
171 		pd = pcap_create(device, ebuf);
172 		if (pd == NULL)
173 			error("%s: pcap_create failed: %s", device, ebuf);
174 		status = pcap_set_snaplen(pd, snaplen);
175 		if (status != 0)
176 			error("%s: pcap_set_snaplen failed: %s",
177 			    device, pcap_statustostr(status));
178 		if (dopromisc) {
179 			status = pcap_set_promisc(pd, 1);
180 			if (status != 0)
181 				error("%s: pcap_set_promisc failed: %s",
182 				    device, pcap_statustostr(status));
183 		}
184 		if (dorfmon) {
185 			status = pcap_set_rfmon(pd, 1);
186 			if (status != 0)
187 				error("%s: pcap_set_rfmon failed: %s",
188 				    device, pcap_statustostr(status));
189 		}
190 		status = pcap_set_timeout(pd, 1000);
191 		if (status != 0)
192 			error("%s: pcap_set_timeout failed: %s",
193 			    device, pcap_statustostr(status));
194 		if (bufsize != 0) {
195 			status = pcap_set_buffer_size(pd, bufsize);
196 			if (status != 0)
197 				error("%s: pcap_set_buffer_size failed: %s",
198 				    device, pcap_statustostr(status));
199 		}
200 		status = pcap_activate(pd);
201 		if (status < 0) {
202 			/*
203 			 * pcap_activate() failed.
204 			 */
205 			error("%s: %s\n(%s)", device,
206 			    pcap_statustostr(status), pcap_geterr(pd));
207 		} else if (status > 0) {
208 			/*
209 			 * pcap_activate() succeeded, but it's warning us
210 			 * of a problem it had.
211 			 */
212 			warning("%s: %s\n(%s)", device,
213 			    pcap_statustostr(status), pcap_geterr(pd));
214 		} else
215 			printf("%s opened successfully\n", device);
216 	} else {
217 		*ebuf = '\0';
218 		pd = pcap_open_live(device, 65535, 0, 1000, ebuf);
219 		if (pd == NULL)
220 			error("%s", ebuf);
221 		else if (*ebuf)
222 			warning("%s", ebuf);
223 		else
224 			printf("%s opened successfully\n", device);
225 	}
226 	pcap_close(pd);
227 	exit(status < 0 ? 1 : 0);
228 }
229 
230 static void
usage(void)231 usage(void)
232 {
233 	(void)fprintf(stderr,
234 	    "Usage: %s [ -Ipa ] [ -i interface ] [ -s snaplen ] [ -B bufsize ]\n",
235 	    program_name);
236 	exit(1);
237 }
238 
239 /* VARARGS */
240 static void
error(const char * fmt,...)241 error(const char *fmt, ...)
242 {
243 	va_list ap;
244 
245 	(void)fprintf(stderr, "%s: ", program_name);
246 	va_start(ap, fmt);
247 	(void)vfprintf(stderr, fmt, ap);
248 	va_end(ap);
249 	if (*fmt) {
250 		fmt += strlen(fmt);
251 		if (fmt[-1] != '\n')
252 			(void)fputc('\n', stderr);
253 	}
254 	exit(1);
255 	/* NOTREACHED */
256 }
257 
258 /* VARARGS */
259 static void
warning(const char * fmt,...)260 warning(const char *fmt, ...)
261 {
262 	va_list ap;
263 
264 	(void)fprintf(stderr, "%s: WARNING: ", program_name);
265 	va_start(ap, fmt);
266 	(void)vfprintf(stderr, fmt, ap);
267 	va_end(ap);
268 	if (*fmt) {
269 		fmt += strlen(fmt);
270 		if (fmt[-1] != '\n')
271 			(void)fputc('\n', stderr);
272 	}
273 }
274