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