]> The Tcpdump Group git mirrors - tcpdump/blobdiff - tcpdump.c
DCCP: Fix printing "Timestamp" and "Timestamp Echo" options
[tcpdump] / tcpdump.c
index 638ba596e8656770d8e0f8b4ca90ecfdea581da5..8ea4925148bcae0853bcfdc5604dda004282de9a 100644 (file)
--- a/tcpdump.c
+++ b/tcpdump.c
@@ -142,12 +142,16 @@ The Regents of the University of California.  All rights reserved.\n";
 #define PATH_MAX 1024
 #endif
 
-#ifdef SIGINFO
+#if defined(SIGINFO)
 #define SIGNAL_REQ_INFO SIGINFO
-#elif SIGUSR1
+#elif defined(SIGUSR1)
 #define SIGNAL_REQ_INFO SIGUSR1
 #endif
 
+#if defined(HAVE_PCAP_DUMP_FLUSH) && defined(SIGUSR2)
+#define SIGNAL_FLUSH_PCAP SIGUSR2
+#endif
+
 static int Bflag;                      /* buffer size */
 #ifdef HAVE_PCAP_DUMP_FTELL64
 static int64_t Cflag;                  /* rotate dump files after this many bytes */
@@ -192,7 +196,9 @@ static int Uflag;                   /* "unbuffered" output of dump files */
 static int Wflag;                      /* recycle output files after this number of files */
 static int WflagChars;
 static char *zflag = NULL;             /* compress each savefile using a specified command (like gzip or bzip2) */
+#ifdef HAVE_PCAP_SET_IMMEDIATE_MODE
 static int immediate_mode;
+#endif
 
 static int infodelay;
 static int infoprint;
@@ -227,7 +233,11 @@ static void dump_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
 static void droproot(const char *, const char *);
 
 #ifdef SIGNAL_REQ_INFO
-void requestinfo(int);
+static void requestinfo(int);
+#endif
+
+#ifdef SIGNAL_FLUSH_PCAP
+static void flushpcap(int);
 #endif
 
 #ifdef _WIN32
@@ -249,11 +259,15 @@ static const struct tok status_flags[] = {
        { PCAP_IF_RUNNING,  "Running"  },
 #endif
        { PCAP_IF_LOOPBACK, "Loopback" },
+#ifdef PCAP_IF_WIRELESS
+       { PCAP_IF_WIRELESS, "Wireless" },
+#endif
        { 0, NULL }
 };
 #endif
 
 static pcap_t *pd;
+static pcap_dumper_t *pdd = NULL;
 
 static int supports_monitor_mode;
 
@@ -265,7 +279,7 @@ struct dump_info {
        char    *WFileName;
        char    *CurrentFileName;
        pcap_t  *pd;
-       pcap_dumper_t *p;
+       pcap_dumper_t *pdd;
        netdissect_options *ndo;
 #ifdef HAVE_CAPSICUM
        int     dirfd;
@@ -466,8 +480,50 @@ show_devices_and_exit(void)
                printf("%d.%s", i+1, dev->name);
                if (dev->description != NULL)
                        printf(" (%s)", dev->description);
-               if (dev->flags != 0)
-                       printf(" [%s]", bittok2str(status_flags, "none", dev->flags));
+               if (dev->flags != 0) {
+                       printf(" [");
+                       printf("%s", bittok2str(status_flags, "none", dev->flags));
+#ifdef PCAP_IF_WIRELESS
+                       if (dev->flags & PCAP_IF_WIRELESS) {
+                               switch (dev->flags & PCAP_IF_CONNECTION_STATUS) {
+
+                               case PCAP_IF_CONNECTION_STATUS_UNKNOWN:
+                                       printf(", Association status unknown");
+                                       break;
+
+                               case PCAP_IF_CONNECTION_STATUS_CONNECTED:
+                                       printf(", Associated");
+                                       break;
+
+                               case PCAP_IF_CONNECTION_STATUS_DISCONNECTED:
+                                       printf(", Not associated");
+                                       break;
+
+                               case PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE:
+                                       break;
+                               }
+                       } else {
+                               switch (dev->flags & PCAP_IF_CONNECTION_STATUS) {
+
+                               case PCAP_IF_CONNECTION_STATUS_UNKNOWN:
+                                       printf(", Connection status unknown");
+                                       break;
+
+                               case PCAP_IF_CONNECTION_STATUS_CONNECTED:
+                                       printf(", Connected");
+                                       break;
+
+                               case PCAP_IF_CONNECTION_STATUS_DISCONNECTED:
+                                       printf(", Disconnected");
+                                       break;
+
+                               case PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE:
+                                       break;
+                               }
+                       }
+#endif
+                       printf("]");
+               }
                printf("\n");
        }
        pcap_freealldevs(devlist);
@@ -2176,7 +2232,6 @@ DIAG_ON_CLANG(assign-enum)
        }
 #endif
        if (WFileName) {
-               pcap_dumper_t *p;
                /* Do not exceed the default PATH_MAX for files. */
                dumpinfo.CurrentFileName = (char *)malloc(PATH_MAX + 1);
 
@@ -2189,7 +2244,7 @@ DIAG_ON_CLANG(assign-enum)
                else
                  MakeFilename(dumpinfo.CurrentFileName, WFileName, 0, 0);
 
-               p = pcap_dump_open(pd, dumpinfo.CurrentFileName);
+               pdd = pcap_dump_open(pd, dumpinfo.CurrentFileName);
 #ifdef HAVE_LIBCAP_NG
                /* Give up CAP_DAC_OVERRIDE capability.
                 * Only allow it to be restored if the -C or -G flag have been
@@ -2203,7 +2258,7 @@ DIAG_ON_CLANG(assign-enum)
                        );
                capng_apply(CAPNG_SELECT_BOTH);
 #endif /* HAVE_LIBCAP_NG */
-               if (p == NULL)
+               if (pdd == NULL)
                        error("%s", pcap_geterr(pd));
 #ifdef HAVE_CAPSICUM
                set_dumper_capsicum_rights(p);
@@ -2236,13 +2291,13 @@ DIAG_ON_CLANG(assign-enum)
 #endif
                        callback = dump_packet_and_trunc;
                        dumpinfo.pd = pd;
-                       dumpinfo.p = p;
+                       dumpinfo.pdd = pdd;
                        pcap_userdata = (u_char *)&dumpinfo;
                } else {
                        callback = dump_packet;
                        dumpinfo.WFileName = WFileName;
                        dumpinfo.pd = pd;
-                       dumpinfo.p = p;
+                       dumpinfo.pdd = pdd;
                        pcap_userdata = (u_char *)&dumpinfo;
                }
                if (print) {
@@ -2254,7 +2309,7 @@ DIAG_ON_CLANG(assign-enum)
 
 #ifdef HAVE_PCAP_DUMP_FLUSH
                if (Uflag)
-                       pcap_dump_flush(p);
+                       pcap_dump_flush(pdd);
 #endif
        } else {
                dlt = pcap_datalink(pd);
@@ -2271,6 +2326,9 @@ DIAG_ON_CLANG(assign-enum)
        if (RFileName == NULL)
                (void)setsignal(SIGNAL_REQ_INFO, requestinfo);
 #endif
+#ifdef SIGNAL_FLUSH_PCAP
+       (void)setsignal(SIGNAL_FLUSH_PCAP, flushpcap);
+#endif
 
        if (ndo->ndo_vflag > 0 && WFileName && !print) {
                /*
@@ -2686,7 +2744,7 @@ dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *s
                        /*
                         * Close the current file and open a new one.
                         */
-                       pcap_dump_close(dump_info->p);
+                       pcap_dump_close(dump_info->pdd);
 
                        /*
                         * Compress the file we just closed, if the user asked for it
@@ -2746,18 +2804,18 @@ dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *s
                                error("unable to fdopen file %s",
                                    dump_info->CurrentFileName);
                        }
-                       dump_info->p = pcap_dump_fopen(dump_info->pd, fp);
+                       dump_info->pdd = pcap_dump_fopen(dump_info->pd, fp);
 #else  /* !HAVE_CAPSICUM */
-                       dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName);
+                       dump_info->pdd = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName);
 #endif
 #ifdef HAVE_LIBCAP_NG
                        capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
                        capng_apply(CAPNG_SELECT_BOTH);
 #endif /* HAVE_LIBCAP_NG */
-                       if (dump_info->p == NULL)
+                       if (dump_info->pdd == NULL)
                                error("%s", pcap_geterr(pd));
 #ifdef HAVE_CAPSICUM
-                       set_dumper_capsicum_rights(dump_info->p);
+                       set_dumper_capsicum_rights(dump_info->pdd);
 #endif
                }
        }
@@ -2769,7 +2827,7 @@ dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *s
         */
        if (Cflag != 0) {
 #ifdef HAVE_PCAP_DUMP_FTELL64
-               int64_t size = pcap_dump_ftell64(dump_info->p);
+               int64_t size = pcap_dump_ftell64(dump_info->pdd);
 #else
                /*
                 * XXX - this only handles a Cflag value > 2^31-1 on
@@ -2777,7 +2835,7 @@ dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *s
                 * Windows) or LLP64 (64-bit Windows) would require
                 * a version of libpcap with pcap_dump_ftell64().
                 */
-               long size = pcap_dump_ftell(dump_info->p);
+               long size = pcap_dump_ftell(dump_info->pdd);
 #endif
 
                if (size == -1)
@@ -2791,7 +2849,7 @@ dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *s
                        /*
                         * Close the current file and open a new one.
                         */
-                       pcap_dump_close(dump_info->p);
+                       pcap_dump_close(dump_info->pdd);
 
                        /*
                         * Compress the file we just closed, if the user
@@ -2827,26 +2885,26 @@ dump_packet_and_trunc(u_char *user, const struct pcap_pkthdr *h, const u_char *s
                                error("unable to fdopen file %s",
                                    dump_info->CurrentFileName);
                        }
-                       dump_info->p = pcap_dump_fopen(dump_info->pd, fp);
+                       dump_info->pdd = pcap_dump_fopen(dump_info->pd, fp);
 #else  /* !HAVE_CAPSICUM */
-                       dump_info->p = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName);
+                       dump_info->pdd = pcap_dump_open(dump_info->pd, dump_info->CurrentFileName);
 #endif
 #ifdef HAVE_LIBCAP_NG
                        capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_DAC_OVERRIDE);
                        capng_apply(CAPNG_SELECT_BOTH);
 #endif /* HAVE_LIBCAP_NG */
-                       if (dump_info->p == NULL)
+                       if (dump_info->pdd == NULL)
                                error("%s", pcap_geterr(pd));
 #ifdef HAVE_CAPSICUM
-                       set_dumper_capsicum_rights(dump_info->p);
+                       set_dumper_capsicum_rights(dump_info->pdd);
 #endif
                }
        }
 
-       pcap_dump((u_char *)dump_info->p, h, sp);
+       pcap_dump((u_char *)dump_info->pdd, h, sp);
 #ifdef HAVE_PCAP_DUMP_FLUSH
        if (Uflag)
-               pcap_dump_flush(dump_info->p);
+               pcap_dump_flush(dump_info->pdd);
 #endif
 
        if (dump_info->ndo != NULL)
@@ -2868,10 +2926,10 @@ dump_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
 
        dump_info = (struct dump_info *)user;
 
-       pcap_dump((u_char *)dump_info->p, h, sp);
+       pcap_dump((u_char *)dump_info->pdd, h, sp);
 #ifdef HAVE_PCAP_DUMP_FLUSH
        if (Uflag)
-               pcap_dump_flush(dump_info->p);
+               pcap_dump_flush(dump_info->pdd);
 #endif
 
        if (dump_info->ndo != NULL)
@@ -2897,7 +2955,8 @@ print_packet(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
 }
 
 #ifdef SIGNAL_REQ_INFO
-void requestinfo(int signo _U_)
+static void
+requestinfo(int signo _U_)
 {
        if (infodelay)
                ++infoprint;
@@ -2906,6 +2965,15 @@ void requestinfo(int signo _U_)
 }
 #endif
 
+#ifdef SIGNAL_FLUSH_PCAP
+static void
+flushpcap(int signo _U_)
+{
+       if (pdd != NULL)
+               pcap_dump_flush(pdd);
+}
+#endif
+
 static void
 print_packets_captured (void)
 {