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