#include <netdissect-stdinc.h>
+#include <sys/stat.h>
+
+#ifdef HAVE_FCNTL_H
+#include <fcntl.h>
+#endif
+
#ifdef USE_LIBSMI
#include <smi.h>
#endif
#include <sys/capability.h>
#include <sys/ioccom.h>
#include <net/bpf.h>
-#include <fcntl.h>
#include <libgen.h>
#endif /* HAVE_CAPSICUM */
#include <pcap.h>
#include <signal.h>
#include <stdio.h>
+#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
char *program_name;
/* Forwards */
+static void error(const char *, ...)
+ __attribute__((noreturn))
+#ifdef __ATTRIBUTE___FORMAT_OK
+ __attribute__((format (printf, 1, 2)))
+#endif /* __ATTRIBUTE___FORMAT_OK */
+ ;
+static void warning(const char *, ...)
+#ifdef __ATTRIBUTE___FORMAT_OK
+ __attribute__((format (printf, 1, 2)))
+#endif /* __ATTRIBUTE___FORMAT_OK */
+ ;
+static void exit_tcpdump(int) __attribute__((noreturn));
static RETSIGTYPE cleanup(int);
static RETSIGTYPE child_cleanup(int);
static void print_version(void);
void pcap_set_optimizer_debug(int);
#endif
+/* VARARGS */
+static void
+error(const char *fmt, ...)
+{
+ va_list ap;
+
+ (void)fprintf(stderr, "%s: ", program_name);
+ va_start(ap, fmt);
+ (void)vfprintf(stderr, fmt, ap);
+ va_end(ap);
+ if (*fmt) {
+ fmt += strlen(fmt);
+ if (fmt[-1] != '\n')
+ (void)fputc('\n', stderr);
+ }
+ exit_tcpdump(1);
+ /* NOTREACHED */
+}
+
+/* VARARGS */
+static void
+warning(const char *fmt, ...)
+{
+ va_list ap;
+
+ (void)fprintf(stderr, "%s: WARNING: ", program_name);
+ va_start(ap, fmt);
+ (void)vfprintf(stderr, fmt, ap);
+ va_end(ap);
+ if (*fmt) {
+ fmt += strlen(fmt);
+ if (fmt[-1] != '\n')
+ (void)fputc('\n', stderr);
+ }
+}
+
+static void
+exit_tcpdump(int status)
+{
+ nd_cleanup();
+ exit(status);
+}
+
#ifdef HAVE_PCAP_SET_TSTAMP_TYPE
static void
show_tstamp_types_and_exit(const char *device)
if (n_tstamp_types == 0) {
fprintf(stderr, "Time stamp type cannot be set for %s\n",
device);
- exit(0);
+ exit_tcpdump(0);
}
fprintf(stderr, "Time stamp types for %s (use option -j to set):\n",
device);
}
}
pcap_free_tstamp_types(tstamp_types);
- exit(0);
+ exit_tcpdump(0);
}
#endif
#ifdef HAVE_PCAP_FREE_DATALINKS
pcap_free_datalinks(dlts);
#endif
- exit(0);
+ exit_tcpdump(0);
}
#ifdef HAVE_PCAP_FINDALLDEVS
printf("\n");
}
pcap_freealldevs(devlist);
- exit(0);
+ exit_tcpdump(0);
}
#endif /* HAVE_PCAP_FINDALLDEVS */
if (chroot_dir && !username) {
fprintf(stderr, "%s: Chroot without dropping root is insecure\n",
program_name);
- exit(1);
+ exit_tcpdump(1);
}
pw = getpwnam(username);
if (chroot(chroot_dir) != 0 || chdir ("/") != 0) {
fprintf(stderr, "%s: Couldn't chroot/chdir to '%.64s': %s\n",
program_name, chroot_dir, pcap_strerror(errno));
- exit(1);
+ exit_tcpdump(1);
}
}
#ifdef HAVE_LIBCAP_NG
(unsigned long)pw->pw_uid,
(unsigned long)pw->pw_gid,
pcap_strerror(errno));
- exit(1);
+ exit_tcpdump(1);
}
else {
fprintf(stderr, "dropped privs to %s\n", username);
else {
fprintf(stderr, "%s: Couldn't find user '%.32s'\n",
program_name, username);
- exit(1);
+ exit_tcpdump(1);
}
#ifdef HAVE_LIBCAP_NG
/* We don't need CAP_SETUID and CAP_SETGID any more. */
}
#endif
+/*
+ * Copy arg vector into a new buffer, concatenating arguments with spaces.
+ */
+static char *
+copy_argv(register char **argv)
+{
+ register char **p;
+ register u_int len = 0;
+ char *buf;
+ char *src, *dst;
+
+ p = argv;
+ if (*p == 0)
+ return 0;
+
+ while (*p)
+ len += strlen(*p++) + 1;
+
+ buf = (char *)malloc(len);
+ if (buf == NULL)
+ error("copy_argv: malloc");
+
+ p = argv;
+ dst = buf;
+ while ((src = *p++) != NULL) {
+ while ((*dst++ = *src++) != '\0')
+ ;
+ dst[-1] = ' ';
+ }
+ dst[-1] = '\0';
+
+ return buf;
+}
+
+/*
+ * On Windows, we need to open the file in binary mode, so that
+ * we get all the bytes specified by the size we get from "fstat()".
+ * On UNIX, that's not necessary. O_BINARY is defined on Windows;
+ * we define it as 0 if it's not defined, so it does nothing.
+ */
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+
+static char *
+read_infile(char *fname)
+{
+ register int i, fd, cc;
+ register char *cp;
+ struct stat buf;
+
+ fd = open(fname, O_RDONLY|O_BINARY);
+ if (fd < 0)
+ error("can't open %s: %s", fname, pcap_strerror(errno));
+
+ if (fstat(fd, &buf) < 0)
+ error("can't stat %s: %s", fname, pcap_strerror(errno));
+
+ cp = malloc((u_int)buf.st_size + 1);
+ if (cp == NULL)
+ error("malloc(%d) for %s: %s", (u_int)buf.st_size + 1,
+ fname, pcap_strerror(errno));
+ cc = read(fd, cp, (u_int)buf.st_size);
+ if (cc < 0)
+ error("read %s: %s", fname, pcap_strerror(errno));
+ if (cc != buf.st_size)
+ error("short read %s (%d != %d)", fname, cc, (int)buf.st_size);
+
+ close(fd);
+ /* replace "# comment" with spaces */
+ for (i = 0; i < cc; i++) {
+ if (cp[i] == '#')
+ while (i < cc && cp[i] != '\n')
+ cp[i++] = ' ';
+ }
+ cp[cc] = '\0';
+ return (cp);
+}
+
int
main(int argc, char **argv)
{
netdissect_options *ndo = &Ndo;
int immediate_mode = 0;
+ /*
+ * Initialize the netdissect code.
+ */
+ if (nd_init(ebuf, sizeof ebuf) == -1)
+ error("%s", ebuf);
+
memset(ndo, 0, sizeof(*ndo));
ndo_set_function_pointers(ndo);
ndo->ndo_snaplen = DEFAULT_SNAPLEN;
if (abort_on_misalignment(ebuf, sizeof(ebuf)) < 0)
error("%s", ebuf);
-#ifdef USE_LIBSMI
- smiInit("tcpdump");
-#endif
-
while (
(op = getopt_long(argc, argv, SHORTOPTS, longopts, NULL)) != -1)
switch (op) {
case 'h':
print_usage();
- exit(0);
+ exit_tcpdump(0);
break;
case 'H':
case OPTION_VERSION:
print_version();
- exit(0);
+ exit_tcpdump(0);
break;
#ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
default:
print_usage();
- exit(1);
+ exit_tcpdump(1);
/* NOTREACHED */
}
pcap_close(pd);
free(cmdbuf);
pcap_freecode(&fcode);
- exit(0);
+ exit_tcpdump(0);
}
init_print(ndo, localnet, netmask, timezone_offset);
free(cmdbuf);
pcap_freecode(&fcode);
- exit(status == -1 ? 1 : 0);
+ exit_tcpdump(status == -1 ? 1 : 0);
}
/* make a clean exit on interrupts */
(void)fflush(stdout);
info(1);
}
- exit(0);
+ exit_tcpdump(0);
#endif
}
if (Cflag == 0 && Wflag > 0 && Gflag_count >= Wflag) {
(void)fprintf(stderr, "Maximum file limit reached: %d\n",
Wflag);
- exit(0);
+ exit_tcpdump(0);
/* NOTREACHED */
}
if (dump_info->CurrentFileName != NULL)