- Some more comments
[obnox/wireshark/wip.git] / capture.c
index 637711e8f85906c3d616b62928599aa6a7959011..b5556ce84cccccba34dc1449bbb1f05387610662 100644 (file)
--- a/capture.c
+++ b/capture.c
 
 #ifdef HAVE_LIBPCAP
 
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
 #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>
@@ -65,6 +64,8 @@
 #include "capture-wpcap.h"
 #endif
 #include "ui_util.h"
+#include "file_util.h"
+#include "log.h"
 
 
 
@@ -82,13 +83,31 @@ capture_start(capture_options *capture_opts)
   /* close the currently loaded capture file */
   cf_close(capture_opts->cf);
 
+  g_assert(capture_opts->state == CAPTURE_STOPPED);
+  capture_opts->state = CAPTURE_PREPARING;
+
+  g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Start ...");
+
   /* try to start the capture child process */
-  ret = sync_pipe_start(capture_opts, capture_opts->save_file == NULL);
+  ret = sync_pipe_start(capture_opts);
   if(!ret) {
       if(capture_opts->save_file != NULL) {
           g_free(capture_opts->save_file);
           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 */
+      /* (especially it will block, if no input coming from an input capture pipe (e.g. mkfifo) is coming in) */
+
+      /* 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;
@@ -98,13 +117,30 @@ capture_start(capture_options *capture_opts)
 void
 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);
 }
 
+
+void
+capture_restart(capture_options *capture_opts)
+{
+    g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Restart");
+
+    capture_opts->restart = TRUE;
+    capture_stop(capture_opts);
+}
+
+
 void
 capture_kill_child(capture_options *capture_opts)
 {
+  g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_INFO, "Capture Kill");
+
   /* kill the capture child */
   sync_pipe_kill(capture_opts);
 }
@@ -180,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) {
+  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);
@@ -200,11 +248,17 @@ capture_input_new_file(capture_options *capture_opts, gchar *new_file)
   int  err;
 
 
-  /*g_warning("New capture file: %s", new_file);*/
+  if(capture_opts->state == CAPTURE_PREPARING) {
+    g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture started!");
+  }
+  g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "File: \"%s\"", new_file);
+
+  g_assert(capture_opts->state == CAPTURE_PREPARING || capture_opts->state == CAPTURE_RUNNING);
 
   /* 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);
@@ -242,6 +296,10 @@ 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;
 }
@@ -254,10 +312,10 @@ capture_input_new_packets(capture_options *capture_opts, int to_read)
   int  err;
 
 
+  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:
@@ -267,6 +325,7 @@ capture_input_new_packets(capture_options *capture_opts, int to_read)
          file.
 
          XXX - abort on a read error? */
+         cf_callback_invoke(cf_cb_live_capture_update_continue, capture_opts->cf);
       break;
 
     case CF_READ_ABORTED:
@@ -275,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 */
 }
 
 
@@ -286,19 +385,50 @@ capture_input_closed(capture_options *capture_opts)
     int  err;
 
 
+    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, 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);
+        } else {
+            cf_callback_invoke(cf_cb_live_capture_fixed_started, 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) {
-              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);
@@ -320,15 +450,43 @@ capture_input_closed(capture_options *capture_opts)
         /* first of all, we are not doing a capture any more */
         cf_callback_invoke(cf_cb_live_capture_fixed_finished, capture_opts->cf);
 
-        /* this is a normal mode capture, read in the capture file data */
-        capture_input_read_all(capture_opts, cf_is_tempfile(capture_opts->cf), 
-            cf_get_drops_known(capture_opts->cf), cf_get_drops(capture_opts->cf));
+        /* this is a normal mode capture and if no error happened, read in the capture file data */
+        if(capture_opts->save_file != NULL) {
+            capture_input_read_all(capture_opts, cf_is_tempfile(capture_opts->cf), 
+                cf_get_drops_known(capture_opts->cf), cf_get_drops(capture_opts->cf));
+        }
     }
 
-    /* We're not doing a capture any more, so we don't have a save file. */
-    g_assert(capture_opts->save_file);
-    g_free(capture_opts->save_file);
-    capture_opts->save_file = NULL;
+    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 */
+    if(capture_opts->save_file == NULL) {
+        cf_close(capture_opts->cf);
+        return;
+    }
+
+    /* does the user wants to restart the current capture? */
+    if(capture_opts->restart) {
+        capture_opts->restart = FALSE;
+
+        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)) {
+            g_free(capture_opts->save_file);
+            capture_opts->save_file = NULL;
+        }
+
+        /* ... and start the capture again */
+        capture_start(capture_opts);
+    } else {
+        /* We're not doing a capture any more, so we don't have a save file. */
+        g_free(capture_opts->save_file);
+        capture_opts->save_file = NULL;
+    }
 }