]> The Tcpdump Group git mirrors - tcpdump/blobdiff - tcpdump.c
Remove "\n" in error() calls: It is added in the function
[tcpdump] / tcpdump.c
index 31a816707f1ef1c739ee123a4a153ba4d7bfbcf9..d5766712e45370d9a0b4a63330f3705ce826133d 100644 (file)
--- a/tcpdump.c
+++ b/tcpdump.c
@@ -34,6 +34,9 @@
  */
 
 #include <config.h>
+#ifndef TCPDUMP_CONFIG_H_
+#error "The included config.h header is not from the tcpdump build."
+#endif
 
 #include "netdissect-stdinc.h"
 
@@ -141,7 +144,6 @@ The Regents of the University of California.  All rights reserved.\n";
 #include "netdissect.h"
 #include "interface.h"
 #include "addrtoname.h"
-#include "pcap-missing.h"
 #include "ascii_strcasecmp.h"
 
 #include "print.h"
@@ -201,13 +203,15 @@ static int Qflag = -1;                    /* restrict captured packet by send/receive direction *
 static int Uflag;                      /* "unbuffered" output of dump files */
 static int Wflag;                      /* recycle output files after this number of files */
 static int WflagChars;
+#if defined(HAVE_FORK) || defined(HAVE_VFORK)
 static char *zflag = NULL;             /* compress each savefile using a specified command (like gzip or bzip2) */
+#endif
 static int timeout = 1000;             /* default timeout = 1000 ms = 1 s */
 #ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
 static int immediate_mode;
 #endif
 static int count_mode;
-static u_int packets_skipped;
+static u_int packets_to_skip;
 
 static int infodelay;
 static int infoprint;
@@ -223,7 +227,9 @@ static int64_t parse_int64(const char *argname, const char *string,
     char **endp, int64_t minval, int64_t maxval, int base);
 static void (*setsignal (int sig, void (*func)(int)))(int);
 static void cleanup(int);
+#if defined(HAVE_FORK) || defined(HAVE_VFORK)
 static void child_cleanup(int);
+#endif
 static void print_version(FILE *);
 static void print_usage(FILE *);
 
@@ -726,7 +732,7 @@ droproot(const char *username, const char *chroot_dir)
                {
                        int ret = capng_change_id(pw->pw_uid, pw->pw_gid, CAPNG_NO_FLAG);
                        if (ret < 0)
-                               error("capng_change_id(): return %d\n", ret);
+                               error("capng_change_id(): return %d", ret);
                        else
                                fprintf(stderr, "dropped privs to %s\n", username);
                }
@@ -1346,6 +1352,11 @@ open_interface(const char *device, netdissect_options *ndo, char *ebuf)
                } else if (status == PCAP_ERROR_PERM_DENIED && *cp != '\0')
                        error("%s: %s\n(%s)", device,
                            pcap_statustostr(status), cp);
+#ifdef PCAP_ERROR_CAPTURE_NOTSUP
+               else if (status == PCAP_ERROR_CAPTURE_NOTSUP && *cp != '\0')
+                       error("%s: %s\n(%s)", device,
+                           pcap_statustostr(status), cp);
+#endif
 #ifdef __FreeBSD__
                else if (status == PCAP_ERROR_RFMON_NOTSUP &&
                    strncmp(device, "wlan", 4) == 0) {
@@ -1363,7 +1374,7 @@ open_interface(const char *device, netdissect_options *ndo, char *ebuf)
                         * specific case would be an error message that looks a bit odd.
                         */
                        newdev[strlen(newdev)-1]++;
-                       error("%s is not a monitor mode VAP\n"
+                       error("%s is not a monitor mode VAP"
                            "To create a new monitor mode VAP use:\n"
                            "  ifconfig %s create wlandev %s wlanmode monitor\n"
                            "and use %s as the tcpdump interface",
@@ -2005,7 +2016,11 @@ main(int argc, char **argv)
                        break;
 #endif
                case 'z':
+#if defined(HAVE_FORK) || defined(HAVE_VFORK)
                        zflag = optarg;
+#else
+                       error("-z cannot be used. Fork subprocess not implemented.");
+#endif
                        break;
 
                case 'Z':
@@ -2055,7 +2070,7 @@ main(int argc, char **argv)
                        break;
 
                case OPTION_SKIP:
-                       packets_skipped = parse_u_int("packet skip count",
+                       packets_to_skip = parse_u_int("packet skip count",
                            optarg, NULL, 0, INT_MAX, 0);
                        break;
 
@@ -2091,17 +2106,25 @@ main(int argc, char **argv)
                }
 
        if (ndo->ndo_Aflag && ndo->ndo_xflag)
-               warning("-A and -x[x] are mutually exclusive. -A ignored.");
+               error("-A and -x[x] are mutually exclusive.");
        if (ndo->ndo_Aflag && ndo->ndo_Xflag)
-               warning("-A and -X[X] are mutually exclusive. -A ignored.");
+               error("-A and -X[X] are mutually exclusive.");
        if (ndo->ndo_xflag && ndo->ndo_Xflag)
-               warning("-x[x] and -X[X] are mutually exclusive. -x[x] ignored.");
+               error("-x[x] and -X[X] are mutually exclusive.");
+       if (Cflag != 0 && WFileName == NULL)
+               error("-C cannot be used without -w.");
+       if (Gflag != 0 && WFileName == NULL)
+               error("-G cannot be used without -w.");
+#if defined(HAVE_FORK) || defined(HAVE_VFORK)
+       if (zflag != NULL && (WFileName == NULL || (Cflag == 0 && Gflag == 0)))
+               error("-z cannot be used without -w and (-C or -G).");
+#endif
 
        if (cnt != -1)
-               if ((int)packets_skipped > (INT_MAX - cnt))
-                       // cnt + (int)packets_skipped used in pcap_loop() call
+               if ((int)packets_to_skip > (INT_MAX - cnt))
+                       // cnt + (int)packets_to_skip used in pcap_loop() call
                        error("Overflow (-c count) %d + (--skip count) %d", cnt,
-                             (int)packets_skipped);
+                             (int)packets_to_skip);
 
        if (Dflag)
                show_devices_and_exit();
@@ -2126,7 +2149,7 @@ main(int argc, char **argv)
        }
 
        if (ndo->ndo_fflag != 0 && (VFileName != NULL || RFileName != NULL))
-               error("-f can not be used with -V or -r");
+               error("-f cannot be used with -V or -r.");
 
        if (VFileName != NULL && RFileName != NULL)
                error("-V and -r are mutually exclusive.");
@@ -2194,11 +2217,11 @@ main(int argc, char **argv)
                                VFile = fopen(VFileName, "r");
 
                        if (VFile == NULL)
-                               error("Unable to open file: %s\n", pcap_strerror(errno));
+                               error("Unable to open file: %s", pcap_strerror(errno));
 
                        ret = get_next_file(VFile, VFileLine);
                        if (!ret)
-                               error("Nothing in %s\n", VFileName);
+                               error("Nothing in %s", VFileName);
                        RFileName = VFileLine;
                }
 
@@ -2370,7 +2393,11 @@ DIAG_ON_WARN_UNUSED_RESULT
 #ifdef HAVE_PCAP_SET_OPTIMIZER_DEBUG
        pcap_set_optimizer_debug(dflag);
 #endif
-       if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
+       /*
+        * netmask is in network byte order, pcap_compile() takes it
+        * in host byte order.
+        */
+       if (pcap_compile(pd, &fcode, cmdbuf, Oflag, ntohl(netmask)) < 0)
                error("%s", pcap_geterr(pd));
        if (dflag) {
                bpf_dump(&fcode, dflag);
@@ -2385,6 +2412,7 @@ DIAG_ON_WARN_UNUSED_RESULT
                capdns = capdns_setup();
 #endif /* HAVE_CASPER */
 
+       // Both localnet and netmask are in network byte order.
        init_print(ndo, localnet, netmask);
 
 #ifndef _WIN32
@@ -2699,7 +2727,7 @@ DIAG_ON_ASSIGN_ENUM
 
        do {
                status = pcap_loop(pd,
-                                  (cnt == -1 ? -1 : cnt + (int)packets_skipped),
+                                  (cnt == -1 ? -1 : cnt + (int)packets_to_skip),
                                   callback, pcap_userdata);
                if (WFileName == NULL) {
                        /*
@@ -2788,7 +2816,11 @@ DIAG_ON_ASSIGN_ENUM
                                        ndo->ndo_if_printer = get_if_printer(dlt);
                                        /* Free the old filter */
                                        pcap_freecode(&fcode);
-                                       if (pcap_compile(pd, &fcode, cmdbuf, Oflag, netmask) < 0)
+                                       /*
+                                        * netmask is in network byte order, pcap_compile() takes it
+                                        * in host byte order.
+                                        */
+                                       if (pcap_compile(pd, &fcode, cmdbuf, Oflag, ntohl(netmask)) < 0)
                                                error("%s", pcap_geterr(pd));
                                }
 
@@ -3088,8 +3120,8 @@ compress_savefile(const char *filename)
        child = fork_subprocess();
        if (child == -1) {
                fprintf(stderr,
-                       "compress_savefile: fork failed: %s\n",
-                       pcap_strerror(errno));
+                       "%s: fork failed: %s\n",
+                       __func__, pcap_strerror(errno));
                return;
        }
        if (child != 0) {
@@ -3108,24 +3140,15 @@ compress_savefile(const char *filename)
 #endif
        if (execlp(zflag, zflag, filename, (char *)NULL) == -1)
                fprintf(stderr,
-                       "compress_savefile: execlp(%s, %s) failed: %s\n",
-                       zflag,
-                       filename,
-                       pcap_strerror(errno));
+                       "%s: execlp(%s, %s) failed: %s\n",
+                       __func__, zflag, filename, pcap_strerror(errno));
 #ifdef HAVE_FORK
        exit(S_ERR_HOST_PROGRAM);
 #else
        _exit(S_ERR_HOST_PROGRAM);
 #endif
 }
-#else  /* HAVE_FORK && HAVE_VFORK */
-static void
-compress_savefile(const char *filename)
-{
-       fprintf(stderr,
-               "compress_savefile failed. Functionality not implemented under your system\n");
-}
-#endif /* HAVE_FORK && HAVE_VFORK */
+#endif /* HAVE_FORK || HAVE_VFORK */
 
 static void
 close_old_dump_file(struct dump_info *dump_info)
@@ -3135,11 +3158,13 @@ close_old_dump_file(struct dump_info *dump_info)
         */
        pcap_dump_close(dump_info->pdd);
 
+#if defined(HAVE_FORK) || defined(HAVE_VFORK)
        /*
         * Compress the file we just closed, if the user asked for it.
         */
        if (zflag != NULL)
                compress_savefile(dump_info->CurrentFileName);
+#endif
 }
 
 static void
@@ -3190,7 +3215,7 @@ dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *s
 
        dump_info = (struct dump_info *)user;
 
-       if (packets_captured <= packets_skipped)
+       if (packets_captured <= packets_to_skip)
                return;
 
        /*
@@ -3322,7 +3347,7 @@ dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
 
        dump_info = (struct dump_info *)user;
 
-       if (packets_captured <= packets_skipped)
+       if (packets_captured <= packets_to_skip)
                return;
 
        pcap_dump((u_char *)dump_info->pdd, h, sp);
@@ -3344,7 +3369,7 @@ print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
 
        ++infodelay;
 
-       if (!count_mode && packets_captured > packets_skipped)
+       if (!count_mode && packets_captured > packets_to_skip)
                pretty_print_packet((netdissect_options *)user, h, sp, packets_captured);
 
        --infodelay;