Regularize indentation a bit.
[obnox/wireshark/wip.git] / tshark.c
index 2350cca9946d3f18288ead14207a5f78975e7bbd..553388121908b4ea32e7dd0267d66b16107a2b90 100644 (file)
--- a/tshark.c
+++ b/tshark.c
@@ -77,6 +77,7 @@
 #include <epan/addr_resolv.h>
 #include "util.h"
 #include "clopts_common.h"
+#include "console_io.h"
 #include "cmdarg_err.h"
 #include "version_info.h"
 #include <epan/plugins.h>
@@ -89,6 +90,7 @@
 
 #ifdef HAVE_LIBPCAP
 #include "capture_ui_utils.h"
+#include "capture_ifinfo.h"
 #include "capture-pcap-util.h"
 #ifdef _WIN32
 #include "capture-wpcap.h"
@@ -232,7 +234,10 @@ print_usage(gboolean print_ver)
   fprintf(output, "  -f <capture filter>      packet filter in libpcap filter syntax\n");
   fprintf(output, "  -s <snaplen>             packet snapshot length (def: 65535)\n");
   fprintf(output, "  -p                       don't capture in promiscuous mode\n");
-#ifdef _WIN32
+#ifdef HAVE_PCAP_CREATE
+  fprintf(output, "  -I                       capture in monitor mode, if available\n");
+#endif
+#if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
   fprintf(output, "  -B <buffer size>         size of kernel buffer (def: 1MB)\n");
 #endif
   fprintf(output, "  -y <link type>           link layer type (def: first appropriate)\n");
@@ -752,6 +757,8 @@ main(int argc, char *argv[])
   gboolean             list_link_layer_types = FALSE;
   gboolean             start_capture = FALSE;
   int                  status;
+  GList               *if_list;
+  gchar               *err_str;
 #else
   gboolean             capture_option_specified = FALSE;
 #endif
@@ -767,18 +774,25 @@ main(int argc, char *argv[])
   GLogLevelFlags       log_flags;
   int                  optind_initial;
 
-#define OPTSTRING_INIT "a:b:c:C:d:De:E:f:F:G:hi:K:lLnN:o:pPqr:R:s:St:T:u:vVw:xX:y:z:"
 #ifdef HAVE_LIBPCAP
-#ifdef _WIN32
-#define OPTSTRING_WIN32 "B:"
+#if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
+#define OPTSTRING_B "B:"
 #else
-#define OPTSTRING_WIN32 ""
-#endif  /* _WIN32 */
-#else
-#define OPTSTRING_WIN32 ""
+#define OPTSTRING_B ""
+#endif  /* _WIN32 or HAVE_PCAP_CREATE */
+#else /* HAVE_LIBPCAP */
+#define OPTSTRING_B ""
 #endif  /* HAVE_LIBPCAP */
 
-  static const char    optstring[] = OPTSTRING_INIT OPTSTRING_WIN32;
+#ifdef HAVE_PCAP_CREATE
+#define OPTSTRING_I "I"
+#else
+#define OPTSTRING_I ""
+#endif
+
+#define OPTSTRING "a:b:" OPTSTRING_B "c:C:d:De:E:f:F:G:hi:" OPTSTRING_I "K:lLnN:o:pPqr:R:s:St:T:u:vVw:xX:y:z:"
+
+  static const char    optstring[] = OPTSTRING;
 
   /*
    * Get credential information for later use.
@@ -859,6 +873,7 @@ main(int argc, char *argv[])
 
   timestamp_set_type(TS_RELATIVE);
   timestamp_set_precision(TS_PREC_AUTO);
+  timestamp_set_seconds_type(TS_SECONDS_DEFAULT);
 
   /* Register all dissectors; we must do this before checking for the
      "-G" flag, as the "-G" flag dumps information registered by the
@@ -1000,12 +1015,15 @@ main(int argc, char *argv[])
       case 'f':        /* capture filter */
       case 'i':        /* Use interface x */
       case 'p':        /* Don't capture in promiscuous mode */
+#ifdef HAVE_PCAP_CREATE
+      case 'I':        /* Capture in monitor mode, if available */
+#endif
       case 's':        /* Set the snapshot (capture) length */
       case 'w':        /* Write to capture file x */
       case 'y':        /* Set the pcap data link type */
-#ifdef _WIN32
+#if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
       case 'B':        /* Buffer size */
-#endif /* _WIN32 */
+#endif /* _WIN32 or HAVE_PCAP_CREATE */
 #ifdef HAVE_LIBPCAP
         status = capture_opts_add_opt(&global_capture_opts, opt, optarg, &start_capture);
         if(status != 0) {
@@ -1030,8 +1048,23 @@ main(int argc, char *argv[])
 #endif
       case 'D':        /* Print a list of capture devices and exit */
 #ifdef HAVE_LIBPCAP
-        status = capture_opts_list_interfaces(FALSE);
-        exit(status);
+        if_list = capture_interface_list(&err, &err_str);
+        if (if_list == NULL) {
+          switch (err) {
+          case CANT_GET_INTERFACE_LIST:
+            cmdarg_err("%s", err_str);
+            g_free(err_str);
+            break;
+
+          case NO_INTERFACES_FOUND:
+            cmdarg_err("There are no interfaces on which a capture can be done");
+            break;
+          }
+          exit(2);
+        }
+        capture_opts_print_interfaces(if_list);
+        free_interface_list(if_list);
+        exit(0);
 #else
         capture_option_specified = TRUE;
         arg_error = TRUE;
@@ -1606,8 +1639,25 @@ main(int argc, char *argv[])
 
     /* if requested, list the link layer types and exit */
     if (list_link_layer_types) {
-        status = capture_opts_list_link_layer_types(&global_capture_opts, FALSE);
-        exit(status);
+        /* Get the list of link-layer types for the capture device. */
+        if_capabilities_t *caps;
+
+        caps = capture_get_if_capabilities(global_capture_opts.iface,
+                                           global_capture_opts.monitor_mode,
+                                           &err_str);
+        if (caps == NULL) {
+            cmdarg_err("%s", err_str);
+            g_free(err_str);
+            exit(2);
+        }
+        if (caps->data_link_types == NULL) {
+            cmdarg_err("The capture device \"%s\" has no data link types.", global_capture_opts.iface);
+            exit(2);
+        }
+        capture_opts_print_if_capabilities(caps,
+                                           global_capture_opts.monitor_mode);
+        free_if_capabilities(caps);
+        exit(0);
     }
 
     if (print_packet_info) {
@@ -3366,6 +3416,26 @@ write_failure_message(const char *filename, int err)
           filename, strerror(err));
 }
 
+/*
+ * Print to the standard error.  This is a command-line tool, so there's
+ * no need to pop up a console.
+ */
+void
+vfprintf_stderr(const char *fmt, va_list ap)
+{
+  vfprintf(stderr, fmt, ap);
+}
+
+void
+fprintf_stderr(const char *fmt, ...)
+{
+  va_list ap;
+
+  va_start(ap, fmt);
+  vfprintf_stderr(fmt, ap);
+  va_end(ap);
+}
+
 /*
  * Report an error in command-line arguments.
  */