]> The Tcpdump Group git mirrors - libpcap/blob - tests/capturetest.c
Add a "getopt.h" header and include it instead of <unistd.h> on Windows.
[libpcap] / tests / capturetest.c
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 <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <limits.h>
33 #ifdef _WIN32
34 #include "getopt.h"
35 #else
36 #include <unistd.h>
37 #endif
38 #include <errno.h>
39 #include <sys/types.h>
40
41 #include <pcap.h>
42
43 static char *program_name;
44
45 #if defined( _MSC_VER )
46 # define PCAP_NORETURN __declspec(noreturn)
47 #elif defined( __GNUC__ )
48 # define PCAP_NORETURN __attribute__((noreturn))
49 #else
50 # define PCAP_NORETURN
51 #endif
52
53 /* Forwards */
54 static void countme(u_char *, const struct pcap_pkthdr *, const u_char *);
55 static void PCAP_NORETURN usage(void);
56 static void error(const char *, ...);
57 static void warning(const char *, ...);
58 static char *copy_argv(char **);
59
60 static pcap_t *pd;
61
62 int
63 main(int argc, char **argv)
64 {
65 register int op;
66 register char *cp, *cmdbuf, *device;
67 long longarg;
68 char *p;
69 int timeout = 1000;
70 int immediate = 0;
71 int nonblock = 0;
72 bpf_u_int32 localnet, netmask;
73 struct bpf_program fcode;
74 char ebuf[PCAP_ERRBUF_SIZE];
75 int status;
76 int packet_count;
77
78 device = NULL;
79 if ((cp = strrchr(argv[0], '/')) != NULL)
80 program_name = cp + 1;
81 else
82 program_name = argv[0];
83
84 opterr = 0;
85 while ((op = getopt(argc, argv, "i:mnt:")) != -1) {
86 switch (op) {
87
88 case 'i':
89 device = optarg;
90 break;
91
92 case 'm':
93 immediate = 1;
94 break;
95
96 case 'n':
97 nonblock = 1;
98 break;
99
100 case 't':
101 longarg = strtol(optarg, &p, 10);
102 if (p == optarg || *p != '\0') {
103 error("Timeout value \"%s\" is not a number",
104 optarg);
105 /* NOTREACHED */
106 }
107 if (longarg < 0) {
108 error("Timeout value %ld is negative", longarg);
109 /* NOTREACHED */
110 }
111 if (longarg > INT_MAX) {
112 error("Timeout value %ld is too large (> %d)",
113 longarg, INT_MAX);
114 /* NOTREACHED */
115 }
116 timeout = (int)longarg;
117 break;
118
119 default:
120 usage();
121 /* NOTREACHED */
122 }
123 }
124
125 if (device == NULL) {
126 device = pcap_lookupdev(ebuf);
127 if (device == NULL)
128 error("%s", ebuf);
129 }
130 *ebuf = '\0';
131 pd = pcap_create(device, ebuf);
132 if (pd == NULL)
133 error("%s", ebuf);
134 status = pcap_set_snaplen(pd, 65535);
135 if (status != 0)
136 error("%s: pcap_set_snaplen failed: %s",
137 device, pcap_statustostr(status));
138 if (immediate) {
139 status = pcap_set_immediate_mode(pd, 1);
140 if (status != 0)
141 error("%s: pcap_set_immediate_mode failed: %s",
142 device, pcap_statustostr(status));
143 }
144 status = pcap_set_timeout(pd, timeout);
145 if (status != 0)
146 error("%s: pcap_set_timeout failed: %s",
147 device, pcap_statustostr(status));
148 status = pcap_activate(pd);
149 if (status < 0) {
150 /*
151 * pcap_activate() failed.
152 */
153 error("%s: %s\n(%s)", device,
154 pcap_statustostr(status), pcap_geterr(pd));
155 } else if (status > 0) {
156 /*
157 * pcap_activate() succeeded, but it's warning us
158 * of a problem it had.
159 */
160 warning("%s: %s\n(%s)", device,
161 pcap_statustostr(status), pcap_geterr(pd));
162 }
163 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
164 localnet = 0;
165 netmask = 0;
166 warning("%s", ebuf);
167 }
168 cmdbuf = copy_argv(&argv[optind]);
169
170 if (pcap_compile(pd, &fcode, cmdbuf, 1, netmask) < 0)
171 error("%s", pcap_geterr(pd));
172
173 if (pcap_setfilter(pd, &fcode) < 0)
174 error("%s", pcap_geterr(pd));
175 if (pcap_setnonblock(pd, nonblock, ebuf) == -1)
176 error("pcap_setnonblock failed: %s", ebuf);
177 printf("Listening on %s\n", device);
178 for (;;) {
179 packet_count = 0;
180 status = pcap_dispatch(pd, -1, countme,
181 (u_char *)&packet_count);
182 if (status < 0)
183 break;
184 if (status != 0) {
185 printf("%d packets seen, %d packets counted after pcap_dispatch returns\n",
186 status, packet_count);
187 }
188 }
189 if (status == -2) {
190 /*
191 * We got interrupted, so perhaps we didn't
192 * manage to finish a line we were printing.
193 * Print an extra newline, just in case.
194 */
195 putchar('\n');
196 }
197 (void)fflush(stdout);
198 if (status == -1) {
199 /*
200 * Error. Report it.
201 */
202 (void)fprintf(stderr, "%s: pcap_loop: %s\n",
203 program_name, pcap_geterr(pd));
204 }
205 pcap_close(pd);
206 exit(status == -1 ? 1 : 0);
207 }
208
209 static void
210 countme(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
211 {
212 int *counterp = (int *)user;
213
214 (*counterp)++;
215 }
216
217 static void
218 usage(void)
219 {
220 (void)fprintf(stderr, "Usage: %s [ -mn ] [ -i interface ] [ -t timeout] [expression]\n",
221 program_name);
222 exit(1);
223 }
224
225 /* VARARGS */
226 static void
227 error(const char *fmt, ...)
228 {
229 va_list ap;
230
231 (void)fprintf(stderr, "%s: ", program_name);
232 va_start(ap, fmt);
233 (void)vfprintf(stderr, fmt, ap);
234 va_end(ap);
235 if (*fmt) {
236 fmt += strlen(fmt);
237 if (fmt[-1] != '\n')
238 (void)fputc('\n', stderr);
239 }
240 exit(1);
241 /* NOTREACHED */
242 }
243
244 /* VARARGS */
245 static void
246 warning(const char *fmt, ...)
247 {
248 va_list ap;
249
250 (void)fprintf(stderr, "%s: WARNING: ", program_name);
251 va_start(ap, fmt);
252 (void)vfprintf(stderr, fmt, ap);
253 va_end(ap);
254 if (*fmt) {
255 fmt += strlen(fmt);
256 if (fmt[-1] != '\n')
257 (void)fputc('\n', stderr);
258 }
259 }
260
261 /*
262 * Copy arg vector into a new buffer, concatenating arguments with spaces.
263 */
264 static char *
265 copy_argv(register char **argv)
266 {
267 register char **p;
268 register u_int len = 0;
269 char *buf;
270 char *src, *dst;
271
272 p = argv;
273 if (*p == 0)
274 return 0;
275
276 while (*p)
277 len += strlen(*p++) + 1;
278
279 buf = (char *)malloc(len);
280 if (buf == NULL)
281 error("copy_argv: malloc");
282
283 p = argv;
284 dst = buf;
285 while ((src = *p++) != NULL) {
286 while ((*dst++ = *src++) != '\0')
287 ;
288 dst[-1] = ' ';
289 }
290 dst[-1] = '\0';
291
292 return buf;
293 }