Add the (still unfinished) tethereal side of the funnel.
[metze/wireshark/wip.git] / capture.c
index 2a8e5b43ac915e085774361c4a2728ef68e3805f..b5556ce84cccccba34dc1449bbb1f05387610662 100644 (file)
--- a/capture.c
+++ b/capture.c
 #include <fcntl.h>
 #endif
 
-#ifdef HAVE_IO_H
-# include <io.h>
-#endif
-
 #include <signal.h>
 #include <errno.h>
 
-#include <pcap.h>
-
 #include <glib.h>
 
 #include <epan/packet.h>
 #include "file.h"
 #include "capture.h"
 #include "capture_sync.h"
+#include "capture_info.h"
 #include "capture_ui_utils.h"
 #include "util.h"
-#include "pcap-util.h"
+#include "capture-pcap-util.h"
 #include "alert_box.h"
 #include "simple_dialog.h"
 #include <epan/prefs.h>
@@ -69,6 +64,7 @@
 #include "capture-wpcap.h"
 #endif
 #include "ui_util.h"
+#include "file_util.h"
 #include "log.h"
 
 
@@ -100,6 +96,7 @@ capture_start(capture_options *capture_opts)
           capture_opts->save_file = NULL;
       }
 
+      g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Start failed!");
       capture_opts->state = CAPTURE_STOPPED;
   } else {
       /* the capture child might not respond shortly after bringing it up */
@@ -108,6 +105,9 @@ capture_start(capture_options *capture_opts)
       /* to prevent problems, bring the main GUI into "capture mode" right after successfully */
       /* spawn/exec the capture child, without waiting for any response from it */
       cf_callback_invoke(cf_cb_live_capture_prepared, capture_opts);
+
+      if(capture_opts->show_info)
+        capture_info_open(capture_opts->iface);
   }
 
   return ret;
@@ -119,6 +119,8 @@ capture_stop(capture_options *capture_opts)
 {
   g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Stop ...");
 
+  cf_callback_invoke(cf_cb_live_capture_stopping, capture_opts);
+
   /* stop the capture child gracefully */
   sync_pipe_stop(capture_opts);
 }
@@ -137,7 +139,7 @@ capture_restart(capture_options *capture_opts)
 void
 capture_kill_child(capture_options *capture_opts)
 {
-  g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Kill");
+  g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_INFO, "Capture Kill");
 
   /* kill the capture child */
   sync_pipe_kill(capture_opts);
@@ -214,10 +216,22 @@ guint32 drops)
   }
 
   /* if we didn't captured even a single packet, close the file again */
-  if(cf_packet_count(capture_opts->cf) == 0 && !capture_opts->restart) {
+  if(cf_get_packet_count(capture_opts->cf) == 0 && !capture_opts->restart) {
     simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK, 
-    "%sNo packets captured!%s\n\n"
-    "As no data was captured, closing the %scapture file!",
+"%sNo packets captured!%s\n"
+"\n"
+"As no data was captured, closing the %scapture file!\n"
+"\n"
+"\n"
+"Help about capturing can be found at:\n"
+"\n"
+"       http://wiki.ethereal.com/CaptureSetup"
+#ifdef _WIN32
+"\n\n"
+"Wireless (Wi-Fi/WLAN):\n"
+"Try to switch off promiscuous mode in the Capture Options!"
+#endif
+"",
     simple_dialog_primary_start(), simple_dialog_primary_end(),
     (cf_is_tempfile(capture_opts->cf)) ? "temporary " : "");
     cf_close(capture_opts->cf);
@@ -244,6 +258,7 @@ capture_input_new_file(capture_options *capture_opts, gchar *new_file)
   /* free the old filename */
   if(capture_opts->save_file != NULL) {
     /* we start a new capture file, close the old one (if we had one before) */
+    /* (we can only have an open capture file in real_time_mode!) */
     if( ((capture_file *) capture_opts->cf)->state != FILE_CLOSED) {
         cf_callback_invoke(cf_cb_live_capture_update_finished, capture_opts->cf);
         cf_finish_tail(capture_opts->cf, &err);
@@ -281,6 +296,9 @@ capture_input_new_file(capture_options *capture_opts, gchar *new_file)
     cf_callback_invoke(cf_cb_live_capture_fixed_started, capture_opts);
   }
 
+  if(capture_opts->show_info)
+    capture_info_new_file(new_file);
+
   capture_opts->state = CAPTURE_RUNNING;
 
   return TRUE;
@@ -297,9 +315,7 @@ capture_input_new_packets(capture_options *capture_opts, int to_read)
   g_assert(capture_opts->save_file);
 
   if(capture_opts->real_time_mode) {
-    /* Read from the capture file the number of records the child told us
-       it added.
-       XXX - do something if this fails? */
+    /* Read from the capture file the number of records the child told us it added. */
     switch (cf_continue_tail(capture_opts->cf, to_read, &err)) {
 
     case CF_READ_OK:
@@ -310,7 +326,6 @@ capture_input_new_packets(capture_options *capture_opts, int to_read)
 
          XXX - abort on a read error? */
          cf_callback_invoke(cf_cb_live_capture_update_continue, capture_opts->cf);
-         main_window_update();
       break;
 
     case CF_READ_ABORTED:
@@ -319,7 +334,47 @@ capture_input_new_packets(capture_options *capture_opts, int to_read)
       capture_kill_child(capture_opts);
       break;
     }
+  } else {
+    /* increase capture file packet counter by the number or incoming packets */
+    cf_set_packet_count(capture_opts->cf, 
+        cf_get_packet_count(capture_opts->cf) + to_read);
+
+    cf_callback_invoke(cf_cb_live_capture_fixed_continue, capture_opts->cf);
   }
+
+  /* update the main window, so we get events (e.g. from the stop toolbar button) */
+  main_window_update();
+
+  if(capture_opts->show_info)
+    capture_info_new_packets(to_read);
+}
+
+
+/* Capture child told us, how many dropped packets it counted.
+ */
+void
+capture_input_drops(capture_options *capture_opts, int dropped)
+{
+  g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_INFO, "%d packet%s dropped", dropped, plurality(dropped, "", "s"));
+
+  g_assert(capture_opts->state == CAPTURE_RUNNING);
+
+  cf_set_drops_known(capture_opts->cf, TRUE);
+  cf_set_drops(capture_opts->cf, dropped);
+}
+
+
+/* Capture child told us, that an error has occurred while starting/running the capture. */
+void
+capture_input_error_message(capture_options *capture_opts, char *error_message)
+{
+  g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Error message from child: \"%s\"", error_message);
+
+  g_assert(capture_opts->state == CAPTURE_PREPARING || capture_opts->state == CAPTURE_RUNNING);
+
+  simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_message);
+
+  /* the capture child will close the sync_pipe if required, nothing to do for now */
 }
 
 
@@ -333,7 +388,8 @@ capture_input_closed(capture_options *capture_opts)
     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture stopped!");
     g_assert(capture_opts->state == CAPTURE_PREPARING || capture_opts->state == CAPTURE_RUNNING);
 
-    /* if we didn't started the capture (happens if an error occured), do a fake start */
+    /* if we didn't started the capture, do a fake start */
+    /* (happens if we got an error message - we won't get a filename then) */
     if(capture_opts->state == CAPTURE_PREPARING) {
         if(capture_opts->real_time_mode) {
             cf_callback_invoke(cf_cb_live_capture_update_started, capture_opts);
@@ -343,18 +399,36 @@ capture_input_closed(capture_options *capture_opts)
     }
 
     if(capture_opts->real_time_mode) {
-        /* first of all, we are not doing a capture any more */
+               cf_read_status_t status;
+
+        /* Read what remains of the capture file. */
+        status = cf_finish_tail(capture_opts->cf, &err);
+
+        /* Tell the GUI, we are not doing a capture any more.
+                  Must be done after the cf_finish_tail(), so file lengths are displayed 
+                  correct. */
         cf_callback_invoke(cf_cb_live_capture_update_finished, capture_opts->cf);
 
-        /* Read what remains of the capture file, and finish the capture.
-           XXX - do something if this fails? */
-        switch (cf_finish_tail(capture_opts->cf, &err)) {
+        /* Finish the capture. */
+        switch (status) {
 
         case CF_READ_OK:
-            if(cf_packet_count(capture_opts->cf) == 0 && !capture_opts->restart) {
-              simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK, 
-              "%sNo packets captured!%s\n\n"
-              "As no data was captured, closing the %scapture file!",
+            if(cf_get_packet_count(capture_opts->cf) == 0 && !capture_opts->restart) {
+                simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK, 
+"%sNo packets captured!%s\n"
+"\n"
+"As no data was captured, closing the %scapture file!\n"
+"\n"
+"\n"
+"Help about capturing can be found at:\n"
+"\n"
+"       http://wiki.ethereal.com/CaptureSetup"
+#ifdef _WIN32
+"\n\n"
+"Wireless (Wi-Fi/WLAN):\n"
+"Try to switch off promiscuous mode in the Capture Options!"
+#endif
+"",
               simple_dialog_primary_start(), simple_dialog_primary_end(),
               cf_is_tempfile(capture_opts->cf) ? "temporary " : "");
               cf_close(capture_opts->cf);
@@ -383,6 +457,9 @@ capture_input_closed(capture_options *capture_opts)
         }
     }
 
+    if(capture_opts->show_info)
+      capture_info_close();
+
     capture_opts->state = CAPTURE_STOPPED;
 
     /* if we couldn't open a capture file, there's nothing more for us to do */
@@ -395,7 +472,7 @@ capture_input_closed(capture_options *capture_opts)
     if(capture_opts->restart) {
         capture_opts->restart = FALSE;
 
-        unlink(capture_opts->save_file);
+        eth_unlink(capture_opts->save_file);
 
         /* if it was a tempfile, throw away the old filename (so it will become a tempfile again) */
         if(cf_is_tempfile(capture_opts->cf)) {