]> The Tcpdump Group git mirrors - libpcap/blob - testprogs/writecaptest.c
Add support for setting the snapshot length and not setting the filter.
[libpcap] / testprogs / writecaptest.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 #include "varattrs.h"
23
24 #ifndef lint
25 static const char copyright[] _U_ =
26 "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
27 The Regents of the University of California. All rights reserved.\n";
28 #endif
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdarg.h>
34 #include <limits.h>
35 #ifdef _WIN32
36 #include "getopt.h"
37 #else
38 #include <unistd.h>
39 #endif
40 #include <errno.h>
41 #ifndef _WIN32
42 #include <signal.h>
43 #endif
44 #include <sys/types.h>
45
46 #include <pcap.h>
47
48 #include "pcap/funcattrs.h"
49
50 #ifdef _WIN32
51 #include "portability.h"
52 #endif
53
54 static char *program_name;
55
56 /* Forwards */
57 static void PCAP_NORETURN usage(void);
58 static void PCAP_NORETURN error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
59 static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
60 static char *copy_argv(char **);
61
62 static pcap_t *pd;
63
64 #ifdef _WIN32
65 static BOOL WINAPI
66 stop_capture(DWORD ctrltype _U_)
67 {
68 pcap_breakloop(pd);
69 return TRUE;
70 }
71 #else
72 static void
73 stop_capture(int signum _U_)
74 {
75 pcap_breakloop(pd);
76 }
77 #endif
78
79 #define COMMAND_OPTIONS "Li:s:w:y:"
80
81 int
82 main(int argc, char **argv)
83 {
84 int op;
85 char *cp, *cmdbuf = NULL, *device, *end, *savefile = NULL;
86 int snaplen;
87 int snaplen_set = 0;
88 pcap_if_t *devlist;
89 int show_dlt_types = 0;
90 int ndlts;
91 int *dlts;
92 bpf_u_int32 localnet, netmask;
93 struct bpf_program fcode;
94 char ebuf[PCAP_ERRBUF_SIZE];
95 #ifndef _WIN32
96 struct sigaction action;
97 #endif
98 int dlt;
99 const char *dlt_name = NULL;
100 int status;
101 pcap_dumper_t *pdd;
102
103 device = NULL;
104 if ((cp = strrchr(argv[0], '/')) != NULL)
105 program_name = cp + 1;
106 else
107 program_name = argv[0];
108
109 opterr = 0;
110 while ((op = getopt(argc, argv, COMMAND_OPTIONS)) != -1) {
111 switch (op) {
112
113 case 'L':
114 show_dlt_types = 1;
115 break;
116
117 case 'i':
118 device = optarg;
119 break;
120
121 case 's':
122 snaplen = (int)strtol(optarg, &end, 0);
123 if (optarg == end || *end != '\0' || snaplen < 0)
124 error("invalid snaplen %s (must be >= 0)",
125 optarg);
126 snaplen_set = 1;
127 break;
128
129 case 'w':
130 savefile = optarg;
131 break;
132
133 case 'y':
134 dlt_name = optarg;
135 break;
136
137 default:
138 usage();
139 /* NOTREACHED */
140 }
141 }
142
143 if (device == NULL) {
144 if (pcap_findalldevs(&devlist, ebuf) == -1)
145 error("%s", ebuf);
146 if (devlist == NULL)
147 error("no interfaces available for capture");
148 device = strdup(devlist->name);
149 pcap_freealldevs(devlist);
150 }
151 if (show_dlt_types) {
152 pd = pcap_create(device, ebuf);
153 if (pd == NULL)
154 error("%s", ebuf);
155 status = pcap_activate(pd);
156 if (status < 0) {
157 /*
158 * pcap_activate() failed.
159 */
160 error("%s: %s\n(%s)", device,
161 pcap_statustostr(status), pcap_geterr(pd));
162 }
163 ndlts = pcap_list_datalinks(pd, &dlts);
164 if (ndlts < 0) {
165 /*
166 * pcap_list_datalinks() failed.
167 */
168 error("%s: %s\n(%s)", device,
169 pcap_statustostr(status), pcap_geterr(pd));
170 }
171 for (int i = 0; i < ndlts; i++) {
172 dlt_name = pcap_datalink_val_to_name(dlts[i]);
173 if (dlt_name == NULL)
174 printf("DLT %d", dlts[i]);
175 else
176 printf("%s", dlt_name);
177 printf("\n");
178 }
179 pcap_free_datalinks(dlts);
180 pcap_close(pd);
181 return 0;
182 }
183
184 if (savefile == NULL)
185 error("no savefile specified");
186
187 *ebuf = '\0';
188
189 pd = pcap_create(device, ebuf);
190 if (pd == NULL)
191 error("%s", ebuf);
192 if (snaplen_set) {
193 status = pcap_set_snaplen(pd, snaplen);
194 if (status != 0)
195 error("%s: pcap_set_snaplen failed: %s",
196 device, pcap_statustostr(status));
197 }
198 status = pcap_set_timeout(pd, 100);
199 if (status != 0)
200 error("%s: pcap_set_timeout failed: %s",
201 device, pcap_statustostr(status));
202 status = pcap_activate(pd);
203 if (status < 0) {
204 /*
205 * pcap_activate() failed.
206 */
207 error("%s: %s\n(%s)", device,
208 pcap_statustostr(status), pcap_geterr(pd));
209 } else if (status > 0) {
210 /*
211 * pcap_activate() succeeded, but it's warning us
212 * of a problem it had.
213 */
214 warning("%s: %s\n(%s)", device,
215 pcap_statustostr(status), pcap_geterr(pd));
216 }
217 if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
218 localnet = 0;
219 netmask = 0;
220 warning("%s", ebuf);
221 }
222
223 if (dlt_name != NULL) {
224 dlt = pcap_datalink_name_to_val(dlt_name);
225 if (dlt == PCAP_ERROR)
226 error("%s isn't a valid DLT name", dlt_name);
227 if (pcap_set_datalink(pd, dlt) == PCAP_ERROR)
228 error("%s: %s", device, pcap_geterr(pd));
229 }
230
231 /*
232 * Don't set a filter unless we were given one on the
233 * command line; if capturing doesn't work, or doesn't
234 * use the snapshot length, without a filter, that's
235 * a bug.
236 */
237 if (optind < argc) {
238 cmdbuf = copy_argv(&argv[optind]);
239
240 if (pcap_compile(pd, &fcode, cmdbuf, 1, netmask) < 0)
241 error("%s", pcap_geterr(pd));
242
243 if (pcap_setfilter(pd, &fcode) < 0)
244 error("%s", pcap_geterr(pd));
245 }
246
247 pdd = pcap_dump_open(pd, savefile);
248 if (pdd == NULL)
249 error("%s", pcap_geterr(pd));
250
251 #ifdef _WIN32
252 SetConsoleCtrlHandler(stop_capture, TRUE);
253 #else
254 action.sa_handler = stop_capture;
255 sigemptyset(&action.sa_mask);
256 action.sa_flags = 0;
257 if (sigaction(SIGINT, &action, NULL) == -1)
258 error("Can't catch SIGINT: %s\n", strerror(errno));
259 #endif
260
261 printf("Listening on %s, link-type ", device);
262 dlt = pcap_datalink(pd);
263 dlt_name = pcap_datalink_val_to_name(dlt);
264 if (dlt_name == NULL)
265 printf("DLT %d", dlt);
266 else
267 printf("%s", dlt_name);
268 printf("\n");
269 for (;;) {
270 status = pcap_dispatch(pd, -1, pcap_dump, (u_char *)pdd);
271 if (status < 0)
272 break;
273 if (status != 0) {
274 printf("%d packets seen\n", status);
275 struct pcap_stat ps;
276 pcap_stats(pd, &ps);
277 printf("%d ps_recv, %d ps_drop, %d ps_ifdrop\n",
278 ps.ps_recv, ps.ps_drop, ps.ps_ifdrop);
279 }
280 }
281 if (status == -2) {
282 /*
283 * We got interrupted, so perhaps we didn't
284 * manage to finish a line we were printing.
285 * Print an extra newline, just in case.
286 */
287 putchar('\n');
288 printf("Broken out of loop from SIGINT handler\n");
289 }
290 (void)fflush(stdout);
291 if (status == -1) {
292 /*
293 * Error. Report it.
294 */
295 (void)fprintf(stderr, "%s: pcap_dispatch: %s\n",
296 program_name, pcap_geterr(pd));
297 }
298 pcap_close(pd);
299 if (cmdbuf != NULL) {
300 pcap_freecode(&fcode);
301 free(cmdbuf);
302 }
303 exit(status == -1 ? 1 : 0);
304 }
305
306 static void
307 usage(void)
308 {
309 (void)fprintf(stderr, "Usage: %s -L [ -i interface ] [ -s snaplen ] [ -w file ] [ -y dlt ] [expression]\n",
310 program_name);
311 exit(1);
312 }
313
314 /* VARARGS */
315 static void
316 error(const char *fmt, ...)
317 {
318 va_list ap;
319
320 (void)fprintf(stderr, "%s: ", program_name);
321 va_start(ap, fmt);
322 (void)vfprintf(stderr, fmt, ap);
323 va_end(ap);
324 if (*fmt) {
325 fmt += strlen(fmt);
326 if (fmt[-1] != '\n')
327 (void)fputc('\n', stderr);
328 }
329 exit(1);
330 /* NOTREACHED */
331 }
332
333 /* VARARGS */
334 static void
335 warning(const char *fmt, ...)
336 {
337 va_list ap;
338
339 (void)fprintf(stderr, "%s: WARNING: ", program_name);
340 va_start(ap, fmt);
341 (void)vfprintf(stderr, fmt, ap);
342 va_end(ap);
343 if (*fmt) {
344 fmt += strlen(fmt);
345 if (fmt[-1] != '\n')
346 (void)fputc('\n', stderr);
347 }
348 }
349
350 /*
351 * Copy arg vector into a new buffer, concatenating arguments with spaces.
352 */
353 static char *
354 copy_argv(register char **argv)
355 {
356 register char **p;
357 register size_t len = 0;
358 char *buf;
359 char *src, *dst;
360
361 p = argv;
362 if (*p == 0)
363 return 0;
364
365 while (*p)
366 len += strlen(*p++) + 1;
367
368 buf = (char *)malloc(len);
369 if (buf == NULL)
370 error("copy_argv: malloc");
371
372 p = argv;
373 dst = buf;
374 while ((src = *p++) != NULL) {
375 while ((*dst++ = *src++) != '\0')
376 ;
377 dst[-1] = ' ';
378 }
379 dst[-1] = '\0';
380
381 return buf;
382 }