aboutsummaryrefslogtreecommitdiff
path: root/testprogs/capturetest.c
diff options
context:
space:
mode:
authorGuy Harris <guy@alum.mit.edu>2020-02-03 11:46:26 -0800
committerGuy Harris <guy@alum.mit.edu>2020-02-03 11:46:26 -0800
commit972577eef695926b096efb93857b3fc527bc6fb5 (patch)
treeb360918a82a6678b4be1c6563db674144f2e6d43 /testprogs/capturetest.c
parent658585a2027a77f880364ed16d1c0de27069f39c (diff)
On UN*X, add options to test signals during a capture.
Diffstat (limited to 'testprogs/capturetest.c')
-rw-r--r--testprogs/capturetest.c79
1 files changed, 77 insertions, 2 deletions
diff --git a/testprogs/capturetest.c b/testprogs/capturetest.c
index ec7ba8cd..75e06620 100644
--- a/testprogs/capturetest.c
+++ b/testprogs/capturetest.c
@@ -38,6 +38,9 @@ The Regents of the University of California. All rights reserved.\n";
#include <unistd.h>
#endif
#include <errno.h>
+#ifndef _WIN32
+ #include <signal.h>
+#endif
#include <sys/types.h>
#include <pcap.h>
@@ -58,6 +61,37 @@ static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
static char *copy_argv(char **);
static pcap_t *pd;
+#ifndef _WIN32
+static int breaksigint = 0;
+#endif
+
+#ifndef _WIN32
+static void
+sigint_handler(int signum _U_)
+{
+ if (breaksigint)
+ pcap_breakloop(pd);
+}
+#endif
+
+#ifdef _WIN32
+/*
+ * We don't have UN*X-style signals, so we don't have anything to test.
+ */
+#define B_OPTION ""
+#define R_OPTION ""
+#define S_OPTION ""
+#else
+/*
+ * We do have UN*X-style signals (we assume that "not Windows" means "UN*X").
+ */
+#define B_OPTION "b"
+#define R_OPTION "r"
+#define S_OPTION "s"
+#endif
+
+#define COMMAND_OPTIONS B_OPTION "i:mn" R_OPTION S_OPTION "t:"
+#define USAGE_OPTIONS "-" B_OPTION "mn" R_OPTION S_OPTION
int
main(int argc, char **argv)
@@ -69,6 +103,8 @@ main(int argc, char **argv)
int timeout = 1000;
int immediate = 0;
int nonblock = 0;
+ int sigrestart = 0;
+ int catchsigint = 0;
pcap_if_t *devlist;
bpf_u_int32 localnet, netmask;
struct bpf_program fcode;
@@ -83,9 +119,15 @@ main(int argc, char **argv)
program_name = argv[0];
opterr = 0;
- while ((op = getopt(argc, argv, "i:mnt:")) != -1) {
+ while ((op = getopt(argc, argv, COMMAND_OPTIONS)) != -1) {
switch (op) {
+#ifndef _WIN32
+ case 'b':
+ breaksigint = 1;
+ break;
+#endif
+
case 'i':
device = optarg;
break;
@@ -98,6 +140,16 @@ main(int argc, char **argv)
nonblock = 1;
break;
+#ifndef _WIN32
+ case 'r':
+ sigrestart = 1;
+ break;
+
+ case 's':
+ catchsigint = 1;
+ break;
+#endif
+
case 't':
longarg = strtol(optarg, &p, 10);
if (p == optarg || *p != '\0') {
@@ -132,6 +184,28 @@ main(int argc, char **argv)
pcap_freealldevs(devlist);
}
*ebuf = '\0';
+
+#ifndef _WIN32
+ /*
+ * If we were told to catch SIGINT, do so.
+ */
+ if (catchsigint) {
+ struct sigaction action;
+
+ action.sa_handler = sigint_handler;
+ action.sa_mask = 0;
+
+ /*
+ * Should SIGINT interrrupt, or restart, system calls?
+ */
+ action.sa_flags = sigrestart ? SA_RESTART : 0;
+
+ if (sigaction(SIGINT, &action, NULL) == -1)
+ error("Can't catch SIGINT: %s\n",
+ strerror(errno));
+ }
+#endif
+
pd = pcap_create(device, ebuf);
if (pd == NULL)
error("%s", ebuf);
@@ -201,6 +275,7 @@ main(int argc, char **argv)
* Print an extra newline, just in case.
*/
putchar('\n');
+ printf("Broken out of loop from SIGINT handler\n");
}
(void)fflush(stdout);
if (status == -1) {
@@ -227,7 +302,7 @@ countme(u_char *user, const struct pcap_pkthdr *h _U_, const u_char *sp _U_)
static void
usage(void)
{
- (void)fprintf(stderr, "Usage: %s [ -mn ] [ -i interface ] [ -t timeout] [expression]\n",
+ (void)fprintf(stderr, "Usage: %s [ " USAGE_OPTIONS " ] [ -i interface ] [ -t timeout] [expression]\n",
program_name);
exit(1);
}