Note realtick's invalidity for ETH_CAPTYPE_OTHERPOD in the comment for
[obnox/wireshark/wip.git] / dumpcap.c
index 287f8927fd5c8ceacc4567d711bbe4ec727e5a84..bf0eea77646f1b5cbdd9da6fce751bf382a46c86 100644 (file)
--- a/dumpcap.c
+++ b/dumpcap.c
@@ -25,6 +25,7 @@
 # include "config.h"
 #endif
 
+#include <stdlib.h> /* for exit() */
 #include <glib.h>
 
 #include <string.h>
@@ -97,7 +98,7 @@ print_usage(gboolean print_ver) {
         "Dumpcap " VERSION "%s\n"
         "Capture network packets and dump them into a libpcap file.\n"
         "See http://www.wireshark.org for more information.\n",
-        svnversion);
+        wireshark_svnversion);
   } else {
     output = stderr;
   }
@@ -148,7 +149,7 @@ show_version(GString *comp_info_str, GString *runtime_info_str)
         "%s\n"
         "%s\n"
         "See http://www.wireshark.org for more information.\n",
-        svnversion, get_copyright_info() ,comp_info_str->str, runtime_info_str->str);
+        wireshark_svnversion, get_copyright_info() ,comp_info_str->str, runtime_info_str->str);
 }
 
 /*
@@ -193,13 +194,17 @@ cmdarg_err_cont(const char *fmt, ...)
 BOOL WINAPI ConsoleCtrlHandlerRoutine(DWORD dwCtrlType)
 {
     g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_INFO,
-        "Console: Ctrl+C");
+        "Console: Control signal");
     g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG,
-        "Console: Ctrl+C CtrlType: %u", dwCtrlType);
+        "Console: Control signal, CtrlType: %u", dwCtrlType);
 
-    capture_loop_stop();
-
-    return TRUE;
+    /* Keep capture running if we're a service and a user logs off */
+    if (capture_child || (dwCtrlType != CTRL_LOGOFF_EVENT)) {
+        capture_loop_stop();
+        return TRUE;
+    } else {
+        return FALSE;
+    }
 }
 #endif
 
@@ -228,8 +233,6 @@ main(int argc, char *argv[])
   int                  opt;
   extern char         *optarg;
   gboolean             arg_error = FALSE;
-  GString             *comp_info_str;
-  GString             *runtime_info_str;
 
 #ifdef _WIN32
   WSADATA              wsaData;
@@ -268,14 +271,6 @@ main(int argc, char *argv[])
   SetConsoleCtrlHandler(&ConsoleCtrlHandlerRoutine, TRUE);
 #endif  /* _WIN32 */
 
-  /* Assemble the compile-time version information string */
-  comp_info_str = g_string_new("Compiled ");
-  g_string_append(comp_info_str, "with ");
-  get_compiled_version_info(comp_info_str);
-
-  /* Assemble the run-time version information string */
-  runtime_info_str = g_string_new("Running ");
-  get_runtime_version_info(runtime_info_str);
 
   /* the default_log_handler will use stdout, which makes trouble in */
   /* capture child mode, as it uses stdout for it's sync_pipe */
@@ -322,9 +317,22 @@ main(int argc, char *argv[])
         exit_main(0);
         break;
       case 'v':        /* Show version and exit */
+      {
+        GString             *comp_info_str;
+        GString             *runtime_info_str;
+        /* Assemble the compile-time version information string */
+        comp_info_str = g_string_new("Compiled with ");
+        get_compiled_version_info(comp_info_str, NULL);
+
+        /* Assemble the run-time version information string */
+        runtime_info_str = g_string_new("Running ");
+        get_runtime_version_info(runtime_info_str, NULL);              
         show_version(comp_info_str, runtime_info_str);
+        g_string_free(comp_info_str, TRUE);
+        g_string_free(runtime_info_str, TRUE);
         exit_main(0);
         break;
+      }
       /*** capture option specific ***/
       case 'a':        /* autostop criteria */
       case 'b':        /* Ringbuffer option */
@@ -379,6 +387,7 @@ main(int argc, char *argv[])
   if (argc != 0) {
     /*
      * Extra command line arguments were specified; complain.
+     * XXX - interpret as capture filter, as tcpdump and tshark do?
      */
     cmdarg_err("Invalid argument: %s", argv[0]);
     arg_error = TRUE;
@@ -517,7 +526,6 @@ report_packet_count(int packet_count)
     char tmp[SP_DECISIZE+1+1];
     static int count = 0;
 
-
     if(capture_child) {
         g_snprintf(tmp, sizeof(tmp), "%d", packet_count);
         g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Packets: %s", tmp);
@@ -533,36 +541,43 @@ report_packet_count(int packet_count)
 void
 report_new_capture_file(const char *filename)
 {
-
-    g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "File: %s", filename);
-
     if(capture_child) {
+        g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "File: %s", filename);
         pipe_write_block(1, SP_FILE, filename);
+    } else {
+        fprintf(stderr, "File: %s\n", filename);
+        /* stderr could be line buffered */
+        fflush(stderr);
     }
 }
 
 void
-report_cfilter_error(const char *cfilter _U_, const char *errmsg)
+report_cfilter_error(const char *cfilter, const char *errmsg)
 {
-
-    g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Capture filter error: %s", errmsg);
-
     if (capture_child) {
+        g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Capture filter error: %s", errmsg);
         pipe_write_block(1, SP_BAD_FILTER, errmsg);
+    } else {
+        fprintf(stderr, 
+          "Invalid capture filter: \"%s\"!\n"
+          "\n"
+          "That string isn't a valid capture filter (%s).\n"
+          "See the User's Guide for a description of the capture filter syntax.\n",
+          cfilter, errmsg);
     }
 }
 
 void
 report_capture_error(const char *error_msg, const char *secondary_error_msg)
 {
-
-    g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, 
-        "Primary Error: %s", error_msg);
-    g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, 
-        "Secondary Error: %s", secondary_error_msg);
-
     if(capture_child) {
+        g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, 
+            "Primary Error: %s", error_msg);
+        g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, 
+            "Secondary Error: %s", secondary_error_msg);
        sync_pipe_errmsg_to_parent(error_msg, secondary_error_msg);
+    } else {
+        fprintf(stderr, "%s\n%s\n", error_msg, secondary_error_msg);
     }
 }
 
@@ -571,12 +586,15 @@ report_packet_drops(int drops)
 {
     char tmp[SP_DECISIZE+1+1];
 
-
     g_snprintf(tmp, sizeof(tmp), "%d", drops);
-    g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Packets dropped: %s", tmp);
 
     if(capture_child) {
+        g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Packets dropped: %s", tmp);
         pipe_write_block(1, SP_DROPS, tmp);
+    } else {
+        fprintf(stderr, "Packets dropped: %s\n", tmp);
+        /* stderr could be line buffered */
+        fflush(stderr);
     }
 }
 
@@ -625,49 +643,3 @@ signal_pipe_check_running(void)
 
 const char *netsnmp_get_version(void) { return ""; }
 
-
-#ifdef _WIN32
-
-/* Convert from UTF-16 to UTF-8. */
-/* XXX - copied from epan/strutil.c */
-#define        INITIAL_FMTBUF_SIZE     128
-
-gchar * utf_16to8(const wchar_t *utf16str) {
-  static gchar *utf8buf[3];
-  static int utf8buf_len[3];
-  static int idx;
-
-  if (utf16str == NULL)
-    return NULL;
-
-  idx = (idx + 1) % 3;
-
-  /*
-   * Allocate the buffer if it's not already allocated.
-   */
-  if (utf8buf[idx] == NULL) {
-    utf8buf_len[idx] = INITIAL_FMTBUF_SIZE;
-    utf8buf[idx] = g_malloc(utf8buf_len[idx]);
-  }
-
-  while (WideCharToMultiByte(CP_UTF8, 0, utf16str, -1,
-      NULL, 0, NULL, NULL) >= utf8buf_len[idx]) {
-    /*
-     * Double the buffer's size if it's not big enough.
-     * The size of the buffer starts at 128, so doubling its size
-     * adds at least another 128 bytes, which is more than enough
-     * for one more character plus a terminating '\0'.
-     */
-    utf8buf_len[idx] *= 2;
-    utf8buf[idx] = g_realloc(utf8buf[idx], utf8buf_len[idx]);
-  }
-
-  if (WideCharToMultiByte(CP_UTF8, 0, utf16str, -1,
-      utf8buf[idx], utf8buf_len[idx], NULL, NULL) == 0)
-    return NULL;
-
-  return utf8buf[idx];
-}
-
-#endif /* _WIN32 */
-