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