]>
The Tcpdump Group git mirrors - libpcap/blob - testprogs/capturetest.c
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.
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
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.
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";
44 #include <sys/types.h>
48 #include "pcap/funcattrs.h"
51 #include "portability.h"
54 static char *program_name
;
57 static void countme(u_char
*, const struct pcap_pkthdr
*, const u_char
*);
58 static void PCAP_NORETURN
usage(void);
59 static void PCAP_NORETURN
error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
60 static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
61 static char *copy_argv(char **);
65 static int breaksigint
= 0;
70 sigint_handler(int signum _U_
)
79 * We don't have UN*X-style signals, so we don't have anything to test.
86 * We do have UN*X-style signals (we assume that "not Windows" means "UN*X").
93 #define COMMAND_OPTIONS B_OPTION "i:mn" R_OPTION S_OPTION "t:"
94 #define USAGE_OPTIONS "-" B_OPTION "mn" R_OPTION S_OPTION
97 main(int argc
, char **argv
)
100 register char *cp
, *cmdbuf
, *device
;
111 bpf_u_int32 localnet
, netmask
;
112 struct bpf_program fcode
;
113 char ebuf
[PCAP_ERRBUF_SIZE
];
118 if ((cp
= strrchr(argv
[0], '/')) != NULL
)
119 program_name
= cp
+ 1;
121 program_name
= argv
[0];
124 while ((op
= getopt(argc
, argv
, COMMAND_OPTIONS
)) != -1) {
156 longarg
= strtol(optarg
, &p
, 10);
157 if (p
== optarg
|| *p
!= '\0') {
158 error("Timeout value \"%s\" is not a number",
163 error("Timeout value %ld is negative", longarg
);
166 if (longarg
> INT_MAX
) {
167 error("Timeout value %ld is too large (> %d)",
171 timeout
= (int)longarg
;
180 if (device
== NULL
) {
181 if (pcap_findalldevs(&devlist
, ebuf
) == -1)
184 error("no interfaces available for capture");
185 device
= strdup(devlist
->name
);
186 pcap_freealldevs(devlist
);
192 * If we were told to catch SIGINT, do so.
195 struct sigaction action
;
197 action
.sa_handler
= sigint_handler
;
198 sigemptyset(&action
.sa_mask
);
201 * Should SIGINT interrupt, or restart, system calls?
203 action
.sa_flags
= sigrestart
? SA_RESTART
: 0;
205 if (sigaction(SIGINT
, &action
, NULL
) == -1)
206 error("Can't catch SIGINT: %s\n",
211 pd
= pcap_create(device
, ebuf
);
214 status
= pcap_set_snaplen(pd
, 65535);
216 error("%s: pcap_set_snaplen failed: %s",
217 device
, pcap_statustostr(status
));
219 status
= pcap_set_immediate_mode(pd
, 1);
221 error("%s: pcap_set_immediate_mode failed: %s",
222 device
, pcap_statustostr(status
));
224 status
= pcap_set_timeout(pd
, timeout
);
226 error("%s: pcap_set_timeout failed: %s",
227 device
, pcap_statustostr(status
));
228 status
= pcap_activate(pd
);
231 * pcap_activate() failed.
233 error("%s: %s\n(%s)", device
,
234 pcap_statustostr(status
), pcap_geterr(pd
));
235 } else if (status
> 0) {
237 * pcap_activate() succeeded, but it's warning us
238 * of a problem it had.
240 warning("%s: %s\n(%s)", device
,
241 pcap_statustostr(status
), pcap_geterr(pd
));
243 if (pcap_lookupnet(device
, &localnet
, &netmask
, ebuf
) < 0) {
248 cmdbuf
= copy_argv(&argv
[optind
]);
250 if (pcap_compile(pd
, &fcode
, cmdbuf
, 1, netmask
) < 0)
251 error("%s", pcap_geterr(pd
));
253 if (pcap_setfilter(pd
, &fcode
) < 0)
254 error("%s", pcap_geterr(pd
));
255 if (pcap_setnonblock(pd
, nonblock
, ebuf
) == -1)
256 error("pcap_setnonblock failed: %s", ebuf
);
257 printf("Listening on %s\n", device
);
260 status
= pcap_dispatch(pd
, -1, countme
,
261 (u_char
*)&packet_count
);
265 printf("%d packets seen, %d packets counted after pcap_dispatch returns\n",
266 status
, packet_count
);
268 if (pcap_stats(pd
, &ps
) < 0) {
269 (void)fprintf(stderr
, "pcap_stats: %s\n",
272 printf("%d ps_recv, %d ps_drop, %d ps_ifdrop\n",
273 ps
.ps_recv
, ps
.ps_drop
, ps
.ps_ifdrop
);
279 * We got interrupted, so perhaps we didn't
280 * manage to finish a line we were printing.
281 * Print an extra newline, just in case.
284 printf("Broken out of loop from SIGINT handler\n");
286 (void)fflush(stdout
);
291 (void)fprintf(stderr
, "%s: pcap_dispatch: %s\n",
292 program_name
, pcap_geterr(pd
));
295 pcap_freecode(&fcode
);
297 exit(status
== -1 ? 1 : 0);
301 countme(u_char
*user
, const struct pcap_pkthdr
*h _U_
, const u_char
*sp _U_
)
303 int *counterp
= (int *)user
;
311 (void)fprintf(stderr
, "Usage: %s [ " USAGE_OPTIONS
" ] [ -i interface ] [ -t timeout] [expression]\n",
318 error(const char *fmt
, ...)
322 (void)fprintf(stderr
, "%s: ", program_name
);
324 (void)vfprintf(stderr
, fmt
, ap
);
329 (void)fputc('\n', stderr
);
337 warning(const char *fmt
, ...)
341 (void)fprintf(stderr
, "%s: WARNING: ", program_name
);
343 (void)vfprintf(stderr
, fmt
, ap
);
348 (void)fputc('\n', stderr
);
353 * Copy arg vector into a new buffer, concatenating arguments with spaces.
356 copy_argv(register char **argv
)
359 register size_t len
= 0;
368 len
+= strlen(*p
++) + 1;
370 buf
= (char *)malloc(len
);
372 error("copy_argv: malloc");
376 while ((src
= *p
++) != NULL
) {
377 while ((*dst
++ = *src
++) != '\0')