From Jaap Keuter: update ethereal_gen.py to generate new-style plugin
[obnox/wireshark/wip.git] / capture_sync.c
index f589b099b33793707b31202e8a2aa3bfa91623ca..092cce08d55a1d615d45e867e4205a22af7171ba 100644 (file)
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  */
 
-/* With MSVC and a libethereal.dll this file needs to import some variables 
-   in a special way. Therefore _NEED_VAR_IMPORT_ is defined. */
-#define _NEED_VAR_IMPORT_
-
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
@@ -112,7 +108,7 @@ static char *sync_pipe_signame(int);
 
 
 static gboolean sync_pipe_input_cb(gint source, gpointer user_data);
-static void sync_pipe_wait_for_child(int fork_child, gboolean always_report);
+static void sync_pipe_wait_for_child(capture_options *capture_opts, gboolean always_report);
 
 /* Size of buffer to hold decimal representation of
    signed/unsigned 64-bit int */
@@ -125,6 +121,7 @@ static void sync_pipe_wait_for_child(int fork_child, gboolean always_report);
 #define SP_PACKET_COUNT        '*'     /* followed by count of packets captured since last message */
 #define SP_ERROR_MSG   '!'     /* followed by length of error message that follows */
 #define SP_DROPS       '#'         /* followed by count of packets dropped in capture */
+#define SP_FILE            ':'     /* followed by length of the name of the last opened file that follows */
 
 
 
@@ -144,6 +141,17 @@ sync_pipe_packet_count_to_parent(int packet_count)
     write(1, tmp, strlen(tmp));
 }
 
+void
+sync_pipe_filename_to_parent(const char *filename)
+{
+    int msglen = strlen(filename);
+    char lenbuf[SP_DECISIZE+1+1];
+
+    sprintf(lenbuf, "%u%c", msglen, SP_FILE);
+    write(1, lenbuf, strlen(lenbuf));
+    write(1, filename, msglen);
+}
+
 void
 sync_pipe_errmsg_to_parent(const char *errmsg)
 {
@@ -216,7 +224,7 @@ sync_pipe_do_capture(capture_options *capture_opts, gboolean is_tempfile) {
     char scount[24];                   /* need a constant for len of numbers */
     char sautostop_filesize[24];       /* need a constant for len of numbers */
     char sautostop_duration[24];       /* need a constant for len of numbers */
-    char save_file_fd[24];
+    char save_file_fd_str[24];
 #ifndef _WIN32
     char errmsg[1024+1];
 #endif
@@ -231,7 +239,6 @@ sync_pipe_do_capture(capture_options *capture_opts, gboolean is_tempfile) {
     enum PIPES { PIPE_READ, PIPE_WRITE };   /* Constants 0 and 1 for PIPE_READ and PIPE_WRITE */
     int sync_pipe[2];                       /* pipes used to sync between instances */
 
-
     capture_opts->fork_child = -1;
 
     /* Allocate the string pointer array with enough space for the
@@ -250,8 +257,8 @@ sync_pipe_do_capture(capture_options *capture_opts, gboolean is_tempfile) {
     argv = sync_pipe_add_arg(argv, &argc, capture_opts->save_file);
 
     argv = sync_pipe_add_arg(argv, &argc, "-W");
-    sprintf(save_file_fd,"%d",capture_opts->save_file_fd);     /* in lieu of itoa */
-    argv = sync_pipe_add_arg(argv, &argc, save_file_fd);
+    sprintf(save_file_fd_str,"%d",capture_opts->save_file_fd); /* in lieu of itoa */
+    argv = sync_pipe_add_arg(argv, &argc, save_file_fd_str);
 
     if (capture_opts->has_autostop_packets) {
       argv = sync_pipe_add_arg(argv, &argc, "-c");
@@ -425,7 +432,7 @@ sync_pipe_do_capture(capture_options *capture_opts, gboolean is_tempfile) {
        unlink(capture_opts->save_file);
        g_free(capture_opts->save_file);
        capture_opts->save_file = NULL;
-       sync_pipe_wait_for_child(capture_opts->fork_child, TRUE);
+       sync_pipe_wait_for_child(capture_opts, TRUE);
        return FALSE;
       }
       if (c == SP_CAPSTART || c == SP_ERROR_MSG)
@@ -466,7 +473,7 @@ sync_pipe_do_capture(capture_options *capture_opts, gboolean is_tempfile) {
          } else if (i == 0) {
            simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
                  "Capture child process failed: EOF reading its error message.");
-           sync_pipe_wait_for_child(capture_opts->fork_child, FALSE);
+           sync_pipe_wait_for_child(capture_opts, FALSE);
          } else
            simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, msg);
          g_free(msg);
@@ -483,34 +490,88 @@ sync_pipe_do_capture(capture_options *capture_opts, gboolean is_tempfile) {
       return FALSE;
     }
 
-    /* The child process started a capture.
-       Attempt to open the capture file and set up to read it. */
-    switch(cf_start_tail(capture_opts->cf, capture_opts->save_file, is_tempfile, &err)) {
-    case CF_OK:
-        /* We were able to open and set up to read the capture file;
-           arrange that our callback be called whenever it's possible
-           to read from the sync pipe, so that it's called when
-           the child process wants to tell us something. */
-        pipe_input_set_handler(sync_pipe[PIPE_READ], (gpointer) capture_opts, &capture_opts->fork_child, sync_pipe_input_cb);
+    if(capture_opts->sync_mode) {
+        /* The child process started a capture.
+           Attempt to open the capture file and set up to read it. */
+        switch(cf_start_tail(capture_opts->cf, capture_opts->save_file, is_tempfile, &err)) {
+        case CF_OK:
+            /* We were able to open and set up to read the capture file;
+               arrange that our callback be called whenever it's possible
+               to read from the sync pipe, so that it's called when
+               the child process wants to tell us something. */
+            pipe_input_set_handler(sync_pipe[PIPE_READ], (gpointer) capture_opts, &capture_opts->fork_child, sync_pipe_input_cb);
+
+            return TRUE;
+            break;
+        case CF_ERROR:
+            /* We weren't able to open the capture file; user has been
+            alerted. Close the sync pipe. */
 
+            close(sync_pipe[PIPE_READ]);
+
+            /* Don't unlink the save file - leave it around, for debugging
+            purposes. */
+            g_free(capture_opts->save_file);
+            capture_opts->save_file = NULL;
+            return FALSE;
+            break;
+        }
+
+        g_assert_not_reached();
+        return FALSE;
+    } else {
+        pipe_input_set_handler(sync_pipe[PIPE_READ], (gpointer) capture_opts, &capture_opts->fork_child, sync_pipe_input_cb);
         return TRUE;
-        break;
-    case CF_ERROR:
-        /* We weren't able to open the capture file; user has been
-        alerted. Close the sync pipe. */
+    }
+}
+
 
-        close(sync_pipe[PIPE_READ]);
+static void
+sync_pipe_closed(capture_options *capture_opts)
+{
+    int  err;
 
-        /* Don't unlink the save file - leave it around, for debugging
-        purposes. */
+
+    /* The child has closed the sync pipe, meaning it's not going to be
+       capturing any more packets.  Pick up its exit status, and
+       complain if it did anything other than exit with status 0. */
+    sync_pipe_wait_for_child(capture_opts, FALSE);
+
+    if(capture_opts->sync_mode) {
+        /* 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)) {
+
+        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!",
+              simple_dialog_primary_start(), simple_dialog_primary_end(),
+              cf_is_tempfile(capture_opts->cf) ? "temporary " : "");
+              cf_close(capture_opts->cf);
+            }
+            break;
+        case CF_READ_ERROR:
+          /* Just because we got an error, that doesn't mean we were unable
+             to read any of the file; we handle what we could get from the
+             file. */
+          break;
+
+        case CF_READ_ABORTED:
+          /* Exit by leaving the main loop, so that any quit functions
+             we registered get called. */
+          main_window_quit();
+        }
+
+        /* 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;
-        return FALSE;
-        break;
+    } else {
+        /* this is a normal mode capture, read in the capture file data */
+        capture_read(capture_opts, cf_is_tempfile(capture_opts->cf), cf_get_drops_known(capture_opts->cf), cf_get_drops(capture_opts->cf));
     }
-
-    g_assert_not_reached();
-    return FALSE;
 }
 
 
@@ -521,7 +582,6 @@ static gboolean
 sync_pipe_input_cb(gint source, gpointer user_data)
 {
   capture_options *capture_opts = (capture_options *)user_data;
-  gint fork_child = capture_opts->fork_child;
 #define BUFSIZE        4096
   char buffer[BUFSIZE+1], *p = buffer, *q = buffer, *msg, *r;
   int  nread, msglen, chars_to_copy;
@@ -529,44 +589,14 @@ sync_pipe_input_cb(gint source, gpointer user_data)
   int  err;
 
 
+  /* we are a capture parent */
+  g_assert(!capture_opts->capture_child);
+
   if ((nread = read(source, buffer, BUFSIZE)) <= 0) {
     /* The child has closed the sync pipe, meaning it's not going to be
        capturing any more packets.  Pick up its exit status, and
        complain if it did anything other than exit with status 0. */
-    sync_pipe_wait_for_child(fork_child, FALSE);
-
-    /* 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)) {
-
-    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!",
-          simple_dialog_primary_start(), simple_dialog_primary_end(),
-          cf_is_tempfile(capture_opts->cf) ? "temporary " : "");
-          cf_close(capture_opts->cf);
-        }
-        break;
-    case CF_READ_ERROR:
-      /* Just because we got an error, that doesn't mean we were unable
-         to read any of the file; we handle what we could get from the
-         file. */
-      break;
-
-    case CF_READ_ABORTED:
-      /* Exit by leaving the main loop, so that any quit functions
-         we registered get called. */
-      main_window_quit();
-      return FALSE;
-    }
-
-    /* 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;
-
+    sync_pipe_closed(capture_opts);
     return FALSE;
   }
 
@@ -618,6 +648,45 @@ sync_pipe_input_cb(gint source, gpointer user_data)
       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, msg);
       g_free(msg);
       break;
+    case SP_FILE :
+      msglen = atoi(p);
+      p = q + 1;
+      q++;
+      nread--;
+
+      /* Read the entire message.
+         XXX - if the child hasn't sent it all yet, this could cause us
+         to hang until they do. */
+      msg = g_malloc(msglen + 1);
+      r = msg;
+      while (msglen != 0) {
+       if (nread == 0) {
+         /* Read more. */
+          if ((nread = read(source, buffer, BUFSIZE)) <= 0)
+            break;
+          p = buffer;
+          q = buffer;
+        }
+       chars_to_copy = MIN(msglen, nread);
+        memcpy(r, q, chars_to_copy);
+        r += chars_to_copy;
+        q += chars_to_copy;
+        nread -= chars_to_copy;
+        msglen -= chars_to_copy;
+      }
+      *r = '\0';
+
+      /* currently, both filenames must be equal */
+      /* (this will change, when multiple files together with sync_mode are captured) */
+      g_assert(strcmp(capture_opts->save_file, msg) == 0);
+
+      /* save the new filename */
+      if(capture_opts->save_file != NULL) {
+        g_free(capture_opts->save_file);
+      }
+      capture_opts->save_file = g_strdup(msg);
+      g_free(msg);
+      break;
     default :
       q++;
       nread--;
@@ -625,25 +694,27 @@ sync_pipe_input_cb(gint source, gpointer user_data)
     }
   }
 
-  /* Read from the capture file the number of records the child told us
-     it added.
-     XXX - do something if this fails? */
-  switch (cf_continue_tail(capture_opts->cf, to_read, &err)) {
+  if(capture_opts->sync_mode) {
+      /* Read from the capture file the number of records the child told us
+         it added.
+         XXX - do something if this fails? */
+      switch (cf_continue_tail(capture_opts->cf, to_read, &err)) {
 
-  case CF_READ_OK:
-  case CF_READ_ERROR:
-    /* Just because we got an error, that doesn't mean we were unable
-       to read any of the file; we handle what we could get from the
-       file.
+      case CF_READ_OK:
+      case CF_READ_ERROR:
+        /* Just because we got an error, that doesn't mean we were unable
+           to read any of the file; we handle what we could get from the
+           file.
 
-       XXX - abort on a read error? */
-    break;
+           XXX - abort on a read error? */
+        break;
 
-  case CF_READ_ABORTED:
-    /* Kill the child capture process; the user wants to exit, and we
-       shouldn't just leave it running. */
-    capture_kill_child(capture_opts);
-    break;
+      case CF_READ_ABORTED:
+        /* Kill the child capture process; the user wants to exit, and we
+           shouldn't just leave it running. */
+        capture_kill_child(capture_opts);
+        break;
+      }
   }
 
   return TRUE;
@@ -652,15 +723,18 @@ sync_pipe_input_cb(gint source, gpointer user_data)
 
 /* the child process is going down, wait until it's completely terminated */
 static void
-sync_pipe_wait_for_child(int fork_child, gboolean always_report)
+sync_pipe_wait_for_child(capture_options *capture_opts, gboolean always_report)
 {
   int  wstatus;
 
+
+  g_assert(capture_opts->fork_child != -1);
+
 #ifdef _WIN32
   /* XXX - analyze the wait status and display more information
      in the dialog box?
      XXX - set "fork_child" to -1 if we find it exited? */
-  if (_cwait(&wstatus, fork_child, _WAIT_CHILD) == -1) {
+  if (_cwait(&wstatus, capture_opts->fork_child, _WAIT_CHILD) == -1) {
     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
                "Child capture process stopped unexpectedly");
   }
@@ -694,7 +768,7 @@ sync_pipe_wait_for_child(int fork_child, gboolean always_report)
   }
 
   /* No more child process. */
-  fork_child = -1;
+  capture_opts->fork_child = -1;
 #endif
 }
 
@@ -797,7 +871,8 @@ sync_pipe_signame(int sig)
 void
 sync_pipe_stop(capture_options *capture_opts)
 {
-  if (capture_opts->fork_child != -1) {
+  /* XXX - in which cases this will be 0? */
+  if (capture_opts->fork_child != -1 && capture_opts->fork_child != 0) {
 #ifndef _WIN32
       kill(capture_opts->fork_child, SIGUSR1);
 #else
@@ -806,12 +881,16 @@ sync_pipe_stop(capture_options *capture_opts)
        * then getting window handle hWnd of that process (using EnumChildWindows),
        * and then do a SendMessage(hWnd, WM_CLOSE, 0, 0) 
        *
-       * Unfortunately, I don't know how to get the process id from the handle */
-      /* Hint: OpenProcess will get an handle from the id, not vice versa :-(
+       * Unfortunately, I don't know how to get the process id from the
+       * handle.  OpenProcess will get an handle (not a window handle)
+       * from the process ID; it will not get a window handle from the
+       * process ID.  (How could it?  A process can have more than one
+       * window.)
        *
-       * Hint: GenerateConsoleCtrlEvent() will only work, if both processes are 
-       * running in the same console, I don't know if that is true for our case.
-       * And this also will require to have the process id
+       * Hint: GenerateConsoleCtrlEvent() will only work if both processes are 
+       * running in the same console; that's not necessarily the case for
+       * us, as we might not be running in a console.
+       * And this also will require to have the process id.
        */
       TerminateProcess((HANDLE) (capture_opts->fork_child), 0);
 #endif
@@ -822,7 +901,8 @@ sync_pipe_stop(capture_options *capture_opts)
 void
 sync_pipe_kill(capture_options *capture_opts)
 {
-  if (capture_opts->fork_child != -1)
+  /* XXX - in which cases this will be 0? */
+  if (capture_opts->fork_child != -1 && capture_opts->fork_child != 0) {
 #ifndef _WIN32
       kill(capture_opts->fork_child, SIGTERM); /* SIGTERM so it can clean up if necessary */
 #else
@@ -831,16 +911,20 @@ sync_pipe_kill(capture_options *capture_opts)
        * then getting window handle hWnd of that process (using EnumChildWindows),
        * and then do a SendMessage(hWnd, WM_CLOSE, 0, 0) 
        *
-       * Unfortunately, I don't know how to get the process id from the handle */
-      /* Hint: OpenProcess will get an handle from the id, not vice versa :-(
+       * Unfortunately, I don't know how to get the process id from the
+       * handle.  OpenProcess will get an handle (not a window handle)
+       * from the process ID; it will not get a window handle from the
+       * process ID.  (How could it?  A process can have more than one
+       * window.)
        *
-       * Hint: GenerateConsoleCtrlEvent() will only work, if both processes are 
-       * running in the same console, I don't know if that is true for our case.
-       * And this also will require to have the process id
+       * Hint: GenerateConsoleCtrlEvent() will only work if both processes are 
+       * running in the same console; that's not necessarily the case for
+       * us, as we might not be running in a console.
+       * And this also will require to have the process id.
        */
       TerminateProcess((HANDLE) (capture_opts->fork_child), 0);
 #endif
+  }
 }
 
-
 #endif /* HAVE_LIBPCAP */