Use tcp_dissect_pdus().
[obnox/wireshark/wip.git] / capture_sync.c
index c07555df3c82529e1780033ff6fdd2474aaec131..098f9219598f66b635891926e082a63a5a245c1c 100644 (file)
@@ -81,6 +81,7 @@
 #include <epan/prefs.h>
 
 #include "globals.h"
+#include "file.h"
 
 #include "capture.h"
 #include "capture_sync.h"
@@ -95,8 +96,6 @@
 # include <io.h>
 #endif
 
-int fork_child = -1;       /* If not -1, in parent, process ID of child */
-
 #ifdef _WIN32
 #include <process.h>    /* For spawning child process */
 #endif
@@ -113,7 +112,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(gboolean always_report);
+static void sync_pipe_wait_for_child(int fork_child, gboolean always_report);
 
 /* Size of buffer to hold decimal representation of
    signed/unsigned 64-bit int */
@@ -217,7 +216,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
@@ -232,8 +231,7 @@ 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 */
 
-
-    fork_child = -1;
+    capture_opts->fork_child = -1;
 
     /* Allocate the string pointer array with enough space for the
        terminating NULL pointer. */
@@ -245,14 +243,14 @@ sync_pipe_do_capture(capture_options *capture_opts, gboolean is_tempfile) {
     argv = sync_pipe_add_arg(argv, &argc, CHILD_NAME);
 
     argv = sync_pipe_add_arg(argv, &argc, "-i");
-    argv = sync_pipe_add_arg(argv, &argc, cfile.iface);
+    argv = sync_pipe_add_arg(argv, &argc, capture_opts->iface);
 
     argv = sync_pipe_add_arg(argv, &argc, "-w");
-    argv = sync_pipe_add_arg(argv, &argc, cfile.save_file);
+    argv = sync_pipe_add_arg(argv, &argc, capture_opts->save_file);
 
     argv = sync_pipe_add_arg(argv, &argc, "-W");
-    sprintf(save_file_fd,"%d",cfile.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");
@@ -302,9 +300,9 @@ sync_pipe_do_capture(capture_options *capture_opts, gboolean is_tempfile) {
     if(_pipe(sync_pipe, 512, O_BINARY) < 0) {
       /* Couldn't create the pipe between parent and child. */
       error = errno;
-      unlink(cfile.save_file);
-      g_free(cfile.save_file);
-      cfile.save_file = NULL;
+      unlink(capture_opts->save_file);
+      g_free(capture_opts->save_file);
+      capture_opts->save_file = NULL;
       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Couldn't create sync pipe: %s",
                         strerror(error));
       return FALSE;
@@ -322,14 +320,14 @@ sync_pipe_do_capture(capture_options *capture_opts, gboolean is_tempfile) {
 
     /* Convert filter string to a quote delimited string and pass to child */
     filterstring = NULL;
-    if (cfile.cfilter != NULL && strlen(cfile.cfilter) != 0) {
+    if (capture_opts->cfilter != NULL && strlen(capture_opts->cfilter) != 0) {
       argv = sync_pipe_add_arg(argv, &argc, "-f");
-      filterstring = sync_pipe_quote_encapsulate(cfile.cfilter);
+      filterstring = sync_pipe_quote_encapsulate(capture_opts->cfilter);
       argv = sync_pipe_add_arg(argv, &argc, filterstring);
     }
 
     /* Spawn process */
-    fork_child = spawnvp(_P_NOWAIT, ethereal_path, argv);
+    capture_opts->fork_child = spawnvp(_P_NOWAIT, ethereal_path, argv);
     g_free(fontstring);
     if (filterstring) {
       g_free(filterstring);
@@ -338,9 +336,9 @@ sync_pipe_do_capture(capture_options *capture_opts, gboolean is_tempfile) {
     if (pipe(sync_pipe) < 0) {
       /* Couldn't create the pipe between parent and child. */
       error = errno;
-      unlink(cfile.save_file);
-      g_free(cfile.save_file);
-      cfile.save_file = NULL;
+      unlink(capture_opts->save_file);
+      g_free(capture_opts->save_file);
+      capture_opts->save_file = NULL;
       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Couldn't create sync pipe: %s",
                        strerror(error));
       return FALSE;
@@ -349,12 +347,12 @@ sync_pipe_do_capture(capture_options *capture_opts, gboolean is_tempfile) {
     argv = sync_pipe_add_arg(argv, &argc, "-m");
     argv = sync_pipe_add_arg(argv, &argc, prefs.PREFS_GUI_FONT_NAME);
 
-    if (cfile.cfilter != NULL && strlen(cfile.cfilter) != 0) {
+    if (capture_opts->cfilter != NULL && capture_opts->cfilter != 0) {
       argv = sync_pipe_add_arg(argv, &argc, "-f");
-      argv = sync_pipe_add_arg(argv, &argc, cfile.cfilter);
+      argv = sync_pipe_add_arg(argv, &argc, capture_opts->cfilter);
     }
 
-    if ((fork_child = fork()) == 0) {
+    if ((capture_opts->fork_child = fork()) == 0) {
       /*
        * Child process - run Ethereal with the right arguments to make
        * it just pop up the live capture dialog box and capture with
@@ -396,15 +394,15 @@ sync_pipe_do_capture(capture_options *capture_opts, gboolean is_tempfile) {
 
     /* Close the save file FD, as we won't be using it - we'll be opening
        it and reading the save file through Wiretap. */
-    close(cfile.save_file_fd);
+    close(capture_opts->save_file_fd);
 
-    if (fork_child == -1) {
+    if (capture_opts->fork_child == -1) {
       /* We couldn't even create the child process. */
       error = errno;
       close(sync_pipe[PIPE_READ]);
-      unlink(cfile.save_file);
-      g_free(cfile.save_file);
-      cfile.save_file = NULL;
+      unlink(capture_opts->save_file);
+      g_free(capture_opts->save_file);
+      capture_opts->save_file = NULL;
       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
                        "Couldn't create child process: %s", strerror(error));
       return FALSE;
@@ -423,10 +421,10 @@ sync_pipe_do_capture(capture_options *capture_opts, gboolean is_tempfile) {
           Close the read side of the sync pipe, remove the capture file,
           and report the failure. */
        close(sync_pipe[PIPE_READ]);
-       unlink(cfile.save_file);
-       g_free(cfile.save_file);
-       cfile.save_file = NULL;
-       sync_pipe_wait_for_child(TRUE);
+       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);
        return FALSE;
       }
       if (c == SP_CAPSTART || c == SP_ERROR_MSG)
@@ -436,9 +434,9 @@ sync_pipe_do_capture(capture_options *capture_opts, gboolean is_tempfile) {
           Close the read side of the sync pipe, remove the capture file,
           and report the failure. */
        close(sync_pipe[PIPE_READ]);
-       unlink(cfile.save_file);
-       g_free(cfile.save_file);
-       cfile.save_file = NULL;
+       unlink(capture_opts->save_file);
+       g_free(capture_opts->save_file);
+       capture_opts->save_file = NULL;
        simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
                        "Capture child process sent us a bad message");
        return FALSE;
@@ -467,7 +465,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(FALSE);
+           sync_pipe_wait_for_child(capture_opts->fork_child, FALSE);
          } else
            simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, msg);
          g_free(msg);
@@ -477,35 +475,41 @@ sync_pipe_do_capture(capture_options *capture_opts, gboolean is_tempfile) {
        close(sync_pipe[PIPE_READ]);
 
        /* Get rid of the save file - the capture never started. */
-       unlink(cfile.save_file);
-       g_free(cfile.save_file);
-       cfile.save_file = NULL;
+       unlink(capture_opts->save_file);
+       g_free(capture_opts->save_file);
+       capture_opts->save_file = NULL;
       }
       return FALSE;
     }
 
     /* The child process started a capture.
        Attempt to open the capture file and set up to read it. */
-    err = cf_start_tail(cfile.save_file, is_tempfile, &cfile);
-    if (err != 0) {
-      /* We weren't able to open the capture file; user has been
-        alerted. Close the sync pipe. */
+    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]);
+        close(sync_pipe[PIPE_READ]);
 
-      /* Don't unlink the save file - leave it around, for debugging
-        purposes. */
-      g_free(cfile.save_file);
-      cfile.save_file = NULL;
-      return FALSE;
+        /* 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;
     }
-    /* 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) &cfile, &fork_child, sync_pipe_input_cb);
 
-    return TRUE;
+    g_assert_not_reached();
+    return FALSE;
 }
 
 
@@ -515,7 +519,8 @@ sync_pipe_do_capture(capture_options *capture_opts, gboolean is_tempfile) {
 static gboolean 
 sync_pipe_input_cb(gint source, gpointer user_data)
 {
-  capture_file *cf = (capture_file *)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;
@@ -527,29 +532,29 @@ sync_pipe_input_cb(gint source, gpointer user_data)
     /* 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(FALSE);
+    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(cf, &err)) {
+    switch (cf_finish_tail(capture_opts->cf, &err)) {
 
-    case READ_SUCCESS:
-        if(cf->count == 0) {
+    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) ? "temporary " : "");
-          cf_close(cf);
+          cf_is_tempfile(capture_opts->cf) ? "temporary " : "");
+          cf_close(capture_opts->cf);
         }
         break;
-    case READ_ERROR:
+    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 READ_ABORTED:
+    case CF_READ_ABORTED:
       /* Exit by leaving the main loop, so that any quit functions
          we registered get called. */
       main_window_quit();
@@ -558,8 +563,8 @@ sync_pipe_input_cb(gint source, gpointer user_data)
 
     /* We're not doing a capture any more, so we don't have a save
        file. */
-    g_free(cf->save_file);
-    cf->save_file = NULL;
+    g_free(capture_opts->save_file);
+    capture_opts->save_file = NULL;
 
     return FALSE;
   }
@@ -576,8 +581,8 @@ sync_pipe_input_cb(gint source, gpointer user_data)
       nread--;
       break;
     case SP_DROPS :
-      cf->drops_known = TRUE;
-      cf->drops = atoi(p);
+      cf_set_drops_known(capture_opts->cf, TRUE);
+      cf_set_drops(capture_opts->cf, atoi(p));
       p = q + 1;
       q++;
       nread--;
@@ -622,10 +627,10 @@ 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(cf, to_read, &err)) {
+  switch (cf_continue_tail(capture_opts->cf, to_read, &err)) {
 
-  case READ_SUCCESS:
-  case READ_ERROR:
+  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.
@@ -633,10 +638,10 @@ sync_pipe_input_cb(gint source, gpointer user_data)
        XXX - abort on a read error? */
     break;
 
-  case READ_ABORTED:
+  case CF_READ_ABORTED:
     /* Kill the child capture process; the user wants to exit, and we
        shouldn't just leave it running. */
-    kill_capture_child(TRUE /* sync_mode */);
+    capture_kill_child(capture_opts);
     break;
   }
 
@@ -646,7 +651,7 @@ 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(gboolean always_report)
+sync_pipe_wait_for_child(int fork_child, gboolean always_report)
 {
   int  wstatus;
 
@@ -789,52 +794,60 @@ sync_pipe_signame(int sig)
 
 
 void
-sync_pipe_stop(void)
+sync_pipe_stop(capture_options *capture_opts)
 {
-  if (fork_child != -1) {
+  if (capture_opts->fork_child != -1 && capture_opts->fork_child != 0) {
 #ifndef _WIN32
-      kill(fork_child, SIGUSR1);
+      kill(capture_opts->fork_child, SIGUSR1);
 #else
       /* XXX: this is not the preferred method of closing a process!
        * the clean way would be getting the process id of the child process,
        * 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) fork_child, 0);
+      TerminateProcess((HANDLE) (capture_opts->fork_child), 0);
 #endif
   }
 }
 
 
 void
-sync_pipe_kill(void)
+sync_pipe_kill(capture_options *capture_opts)
 {
-  if (fork_child != -1)
+  if (capture_opts->fork_child != -1 && capture_opts->fork_child != 0) {
 #ifndef _WIN32
-      kill(fork_child, SIGTERM);       /* SIGTERM so it can clean up if necessary */
+      kill(capture_opts->fork_child, SIGTERM); /* SIGTERM so it can clean up if necessary */
 #else
       /* XXX: this is not the preferred method of closing a process!
        * the clean way would be getting the process id of the child process,
        * 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) fork_child, 0);
+      TerminateProcess((HANDLE) (capture_opts->fork_child), 0);
 #endif
+  }
 }
 
-
 #endif /* HAVE_LIBPCAP */