Change offset to be 8, based on an educated guess from looking at the
[metze/wireshark/wip.git] / capture_sync.c
index fef2f94ccfa597169d5c52be6986965d3b066fe8..fded034adfb96058138f760b46356d5484c75afd 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
+#include "config.h"
 
 #ifdef HAVE_LIBPCAP
 
@@ -97,7 +95,9 @@
 #ifdef _WIN32
 #include "capture-wpcap.h"
 #endif
-#include "ui_util.h"
+
+#include "ui/ui_util.h"
+
 #include <wsutil/file_util.h>
 #include "log.h"
 
@@ -115,7 +115,7 @@ static const char *sync_pipe_signame(int);
 static gboolean sync_pipe_input_cb(gint source, gpointer user_data);
 static int sync_pipe_wait_for_child(int fork_child, gchar **msgp);
 static void pipe_convert_header(const guchar *header, int header_len, char *indicator, int *block_len);
-static int pipe_read_block(int pipe_fd, char *indicator, int len, char *msg,
+static ssize_t pipe_read_block(int pipe_fd, char *indicator, int len, char *msg,
                            char **err_msg);
 
 
@@ -129,7 +129,7 @@ sync_pipe_add_arg(const char **args, int *argc, const char *arg)
        pointers, *not* counting the NULL pointer at the end, so we have
        to add 2 in order to get the new size of the array, including the
        new pointer and the terminating NULL pointer. */
-    args = g_realloc( (gpointer) args, (*argc + 2) * sizeof (char *));
+    args = (const char **)g_realloc( (gpointer) args, (*argc + 2) * sizeof (char *));
 
     /* Stuff the pointer into the penultimate element of the array, which
        is the one at the index specified by "*argc". */
@@ -223,8 +223,8 @@ win32strerror(DWORD error)
     size_t errlen;
     char *p;
 
-    FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf,
-                   ERRBUF_SIZE, NULL);
+    FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
+                  NULL, error, 0, errbuf, ERRBUF_SIZE, NULL);
 
     /*
      * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
@@ -302,7 +302,7 @@ init_pipe_args(int *argc) {
     /* Allocate the string pointer array with enough space for the
        terminating NULL pointer. */
     *argc = 0;
-    argv = g_malloc(sizeof (char *));
+    argv = (const char **)g_malloc(sizeof (char *));
     *argv = NULL;
 
     /* take Wireshark's absolute program path and replace "Wireshark" with "dumpcap" */
@@ -311,6 +311,9 @@ init_pipe_args(int *argc) {
     /* Make that the first argument in the argument list (argv[0]). */
     argv = sync_pipe_add_arg(argv, argc, exename);
 
+    /* sync_pipe_add_arg strdupes exename, so we should free our copy */
+    g_free(exename);
+
     return argv;
 }
 
@@ -319,7 +322,6 @@ init_pipe_args(int *argc) {
 gboolean
 sync_pipe_start(capture_options *capture_opts) {
     char ssnap[ARGV_NUMBER_LEN];
-    char sdlt[ARGV_NUMBER_LEN];
     char scount[ARGV_NUMBER_LEN];
     char sfilesize[ARGV_NUMBER_LEN];
     char sfile_duration[ARGV_NUMBER_LEN];
@@ -427,6 +429,10 @@ sync_pipe_start(capture_options *capture_opts) {
         argv = sync_pipe_add_arg(argv, &argc, sautostop_duration);
     }
 
+    if (capture_opts->group_read_access) {
+        argv = sync_pipe_add_arg(argv, &argc, "-g");
+    }
+
     for (j = 0; j < capture_opts->ifaces->len; j++) {
         interface_opts = g_array_index(capture_opts->ifaces, interface_options, j);
 
@@ -445,8 +451,7 @@ sync_pipe_start(capture_options *capture_opts) {
 
         if (interface_opts.linktype != -1) {
             argv = sync_pipe_add_arg(argv, &argc, "-y");
-            g_snprintf(sdlt, ARGV_NUMBER_LEN, "%s", linktype_val_to_name(interface_opts.linktype));
-            argv = sync_pipe_add_arg(argv, &argc, sdlt);
+            argv = sync_pipe_add_arg(argv, &argc, linktype_val_to_name(interface_opts.linktype));
         }
 
         if (!interface_opts.promisc_mode) {
@@ -454,16 +459,18 @@ sync_pipe_start(capture_options *capture_opts) {
         }
 
 #if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
-        if (interface_opts.buffer_size != 1) {
+        if (interface_opts.buffer_size != DEFAULT_CAPTURE_BUFFER_SIZE) {
             argv = sync_pipe_add_arg(argv, &argc, "-B");
             g_snprintf(buffer_size, ARGV_NUMBER_LEN, "%d", interface_opts.buffer_size);
             argv = sync_pipe_add_arg(argv, &argc, buffer_size);
         }
 #endif
 
+#ifdef HAVE_PCAP_CREATE
         if (interface_opts.monitor_mode) {
             argv = sync_pipe_add_arg(argv, &argc, "-I");
         }
+#endif
 
 #ifdef HAVE_PCAP_REMOTE
         if (interface_opts.datatx_udp)
@@ -615,7 +622,7 @@ sync_pipe_start(capture_options *capture_opts) {
          */
         dup2(sync_pipe[PIPE_WRITE], 2);
         ws_close(sync_pipe[PIPE_READ]);
-        execv(argv[0], (gpointer)argv);
+        execv(argv[0], (char * const*)argv);
         g_snprintf(errmsg, sizeof errmsg, "Couldn't run %s in child process: %s",
                    argv[0], g_strerror(errno));
         sync_pipe_errmsg_to_parent(2, errmsg, "");
@@ -737,7 +744,7 @@ sync_pipe_open_command(const char** argv, int *data_read_fd,
         /* Couldn't create the message pipe between parent and child. */
         *msg = g_strdup_printf("Couldn't create sync pipe: %s",
                                win32strerror(GetLastError()));
-        for (i = 0; i < argc; i++) {
+        for (i = 0; argv[i] != NULL; i++) {
             g_free( (gpointer) argv[i]);
         }
         g_free( (gpointer) argv);
@@ -752,7 +759,7 @@ sync_pipe_open_command(const char** argv, int *data_read_fd,
                                win32strerror(GetLastError()));
         CloseHandle(sync_pipe[PIPE_READ]);
         CloseHandle(sync_pipe[PIPE_WRITE]);
-        for (i = 0; i < argc; i++) {
+        for (i = 0; argv[i] != NULL; i++) {
             g_free( (gpointer) argv[i]);
         }
         g_free( (gpointer) argv);
@@ -792,7 +799,7 @@ sync_pipe_open_command(const char** argv, int *data_read_fd,
         CloseHandle(data_pipe[PIPE_WRITE]);
         CloseHandle(sync_pipe[PIPE_READ]);
         CloseHandle(sync_pipe[PIPE_WRITE]);
-        for (i = 0; i < argc; i++) {
+        for (i = 0; argv[i] != NULL; i++) {
             g_free( (gpointer) argv[i]);
         }
         g_free( (gpointer) argv);
@@ -841,7 +848,7 @@ sync_pipe_open_command(const char** argv, int *data_read_fd,
         dup2(sync_pipe[PIPE_WRITE], 2);
         ws_close(sync_pipe[PIPE_READ]);
         ws_close(sync_pipe[PIPE_WRITE]);
-        execv(argv[0], (gpointer)argv);
+        execv(argv[0], (char * const*)argv);
         g_snprintf(errmsg, sizeof errmsg, "Couldn't run %s in child process: %s",
                    argv[0], g_strerror(errno));
         sync_pipe_errmsg_to_parent(2, errmsg, "");
@@ -894,14 +901,16 @@ sync_pipe_open_command(const char** argv, int *data_read_fd,
 }
 
 /*
- * Wait for dumpcap to finish.  On success, *msg is unchanged, and 0 is
- * returned.  On failure, *msg points to an error message for the
- * failure, and -1 is returned.  In the latter case, *msg must be
- * freed with g_free().
+ * Close the pipes we're using to read from dumpcap, and wait for it
+ * to exit.  On success, *msgp is unchanged, and the exit status of
+ * dumpcap is returned.  On failure (which includes "dumpcap exited
+ * due to being killed by a signal or an exception"), *msgp points
+ * to an error message for the failure, and -1 is returned.  In the
+ * latter case, *msgp must be freed with g_free().
  */
 static int
 sync_pipe_close_command(int *data_read_fd, int *message_read_fd,
-                        int *fork_child, gchar **msg)
+                        int *fork_child, gchar **msgp)
 {
     ws_close(*data_read_fd);
     if (message_read_fd != NULL)
@@ -912,7 +921,7 @@ sync_pipe_close_command(int *data_read_fd, int *message_read_fd,
     sync_pipe_kill(*fork_child);
 #endif
 
-    return sync_pipe_wait_for_child(*fork_child, msg);
+    return sync_pipe_wait_for_child(*fork_child, msgp);
 }
 
 /*
@@ -938,7 +947,7 @@ sync_pipe_run_command(const char** argv, gchar **data, gchar **primary_msg,
     int data_pipe_read_fd, sync_pipe_read_fd, fork_child, ret;
     char *wait_msg;
     gchar buffer[PIPE_BUF_SIZE+1];
-    int  nread;
+    ssize_t nread;
     char indicator;
     int  primary_msg_len;
     char *primary_msg_text;
@@ -946,7 +955,7 @@ sync_pipe_run_command(const char** argv, gchar **data, gchar **primary_msg,
     char *secondary_msg_text;
     char *combined_msg;
     GString *data_buf = NULL;
-    int count;
+    ssize_t count;
 
     ret = sync_pipe_open_command(argv, &data_pipe_read_fd, &sync_pipe_read_fd,
                                  &fork_child, &msg);
@@ -1010,10 +1019,10 @@ sync_pipe_run_command(const char** argv, gchar **data, gchar **primary_msg,
          */
 
         /* convert primary message */
-        pipe_convert_header(buffer, 4, &indicator, &primary_msg_len);
+        pipe_convert_header((guchar*)buffer, 4, &indicator, &primary_msg_len);
         primary_msg_text = buffer+4;
         /* convert secondary message */
-        pipe_convert_header(primary_msg_text + primary_msg_len, 4, &indicator,
+        pipe_convert_header((guchar*)primary_msg_text + primary_msg_len, 4, &indicator,
                             &secondary_msg_len);
         secondary_msg_text = primary_msg_text + primary_msg_len + 4;
         /* the capture child will close the sync_pipe, nothing to do */
@@ -1103,6 +1112,53 @@ sync_pipe_run_command(const char** argv, gchar **data, gchar **primary_msg,
     return ret;
 }
 
+int
+sync_interface_set_80211_chan(const gchar *iface, const char *freq, const gchar *type,
+                              gchar **data, gchar **primary_msg,
+                              gchar **secondary_msg)
+{
+    int argc, ret;
+    const char **argv;
+    gchar *opt;
+
+    argv = init_pipe_args(&argc);
+
+    if (!argv) {
+        *primary_msg = g_strdup("We don't know where to find dumpcap.");
+        *secondary_msg = NULL;
+        *data = NULL;
+        return -1;
+    }
+
+    argv = sync_pipe_add_arg(argv, &argc, "-i");
+    argv = sync_pipe_add_arg(argv, &argc, iface);
+
+    if (type)
+        opt = g_strdup_printf("%s,%s", freq, type);
+    else
+        opt = g_strdup_printf("%s", freq);
+
+    if (!opt) {
+        *primary_msg = g_strdup("Out of mem.");
+        *secondary_msg = NULL;
+        *data = NULL;
+        return -1;
+    }
+
+    argv = sync_pipe_add_arg(argv, &argc, "-k");
+    argv = sync_pipe_add_arg(argv, &argc, opt);
+
+#ifndef DEBUG_CHILD
+    /* Run dumpcap in capture child mode */
+    argv = sync_pipe_add_arg(argv, &argc, "-Z");
+    argv = sync_pipe_add_arg(argv, &argc, SIGNAL_PIPE_CTRL_ID_NONE);
+#endif
+
+    ret = sync_pipe_run_command(argv, data, primary_msg, secondary_msg);
+    g_free(opt);
+    return ret;
+}
+
 /*
  * Get the list of interfaces using dumpcap.
  *
@@ -1204,7 +1260,7 @@ sync_interface_stats_open(int *data_read_fd, int *fork_child, gchar **msg)
     int message_read_fd, ret;
     char *wait_msg;
     gchar buffer[PIPE_BUF_SIZE+1];
-    int  nread;
+    ssize_t nread;
     char indicator;
     int  primary_msg_len;
     char *primary_msg_text;
@@ -1285,10 +1341,10 @@ sync_interface_stats_open(int *data_read_fd, int *fork_child, gchar **msg)
          */
 
         /* convert primary message */
-        pipe_convert_header(buffer, 4, &indicator, &primary_msg_len);
+        pipe_convert_header((guchar*)buffer, 4, &indicator, &primary_msg_len);
         primary_msg_text = buffer+4;
         /* convert secondary message */
-        pipe_convert_header(primary_msg_text + primary_msg_len, 4, &indicator,
+        pipe_convert_header((guchar*)primary_msg_text + primary_msg_len, 4, &indicator,
                             &secondary_msg_len);
         /*secondary_msg_text = primary_msg_text + primary_msg_len + 4;*/
         /* the capture child will close the sync_pipe, nothing to do */
@@ -1346,16 +1402,23 @@ sync_interface_stats_open(int *data_read_fd, int *fork_child, gchar **msg)
 int
 sync_interface_stats_close(int *read_fd, int *fork_child, gchar **msg)
 {
+#ifndef _WIN32
+    /*
+     * Don't bother waiting for the child. sync_pipe_close_command
+     * does this for us on Windows.
+     */
+    sync_pipe_kill(*fork_child);
+#endif
     return sync_pipe_close_command(read_fd, NULL, fork_child, msg);
 }
 
 /* read a number of bytes from a pipe */
 /* (blocks until enough bytes read or an error occurs) */
-static int
+static ssize_t
 pipe_read_bytes(int pipe_fd, char *bytes, int required, char **msg)
 {
-    int newly;
-    int offset = 0;
+    ssize_t newly;
+    ssize_t offset = 0;
     int error;
 
     while(required) {
@@ -1378,7 +1441,7 @@ pipe_read_bytes(int pipe_fd, char *bytes, int required, char **msg)
             return newly;
         }
 
-        required -= newly;
+        required -= (int)newly;
         offset += newly;
     }
 
@@ -1419,7 +1482,7 @@ static gboolean pipe_data_available(int pipe_fd) {
 /* Read a line from a pipe, similar to fgets */
 int
 sync_pipe_gets_nonblock(int pipe_fd, char *bytes, int max) {
-    int newly;
+    ssize_t newly;
     int offset = -1;
 
     while(offset < max - 1) {
@@ -1430,11 +1493,11 @@ sync_pipe_gets_nonblock(int pipe_fd, char *bytes, int max) {
         if (newly == 0) {
             /* EOF - not necessarily an error */
             break;
-        } else if (newly < 0) {
+        } else if (newly == -1) {
             /* error */
             g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
                   "read from pipe %d: error(%u): %s", pipe_fd, errno, g_strerror(errno));
-            return newly;
+            return -1;
         } else if (bytes[offset] == '\n') {
             break;
         }
@@ -1455,19 +1518,19 @@ pipe_convert_header(const guchar *header, int header_len, char *indicator, int *
 
     /* convert header values */
     *indicator = header[0];
-    *block_len = header[1]<<16 | header[2]<<8 | header[3];
+    *block_len = (header[1]&0xFF)<<16 | (header[2]&0xFF)<<8 | (header[3]&0xFF);
 }
 
 /* read a message from the sending pipe in the standard format
    (1-byte message indicator, 3-byte message length (excluding length
    and indicator field), and the rest is the message) */
-static int
+static ssize_t
 pipe_read_block(int pipe_fd, char *indicator, int len, char *msg,
                 char **err_msg)
 {
     int required;
-    int newly;
-    guchar header[4];
+    ssize_t newly;
+    gchar header[4];
 
     /* read header (indicator and 3-byte length) */
     newly = pipe_read_bytes(pipe_fd, header, 4, err_msg);
@@ -1483,19 +1546,19 @@ pipe_read_block(int pipe_fd, char *indicator, int len, char *msg,
             return 0;
         }
         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
-              "read %d failed to read header: %u", pipe_fd, newly);
+              "read %d failed to read header: %lu", pipe_fd, (long)newly);
         if (newly != -1) {
             /*
              * Short read, but not an immediate EOF.
              */
-            *err_msg = g_strdup_printf("Premature EOF reading from sync pipe: got only %d bytes",
-                                       newly);
+            *err_msg = g_strdup_printf("Premature EOF reading from sync pipe: got only %ld bytes",
+                                       (long)newly);
         }
         return -1;
     }
 
     /* convert header values */
-    pipe_convert_header(header, 4, indicator, &required);
+    pipe_convert_header((guchar*)header, 4, indicator, &required);
 
     /* only indicator with no value? */
     if(required == 0) {
@@ -1507,12 +1570,17 @@ pipe_read_block(int pipe_fd, char *indicator, int len, char *msg,
     /* does the data fit into the given buffer? */
     if(required > len) {
         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
-              "read %d length error, required %d > len %d, indicator: %u",
-              pipe_fd, required, len, *indicator);
+              "read %d length error, required %d > len %d, header: 0x%02x 0x%02x 0x%02x 0x%02x",
+              pipe_fd, required, len,
+              header[0], header[1], header[2], header[3]);
 
         /* we have a problem here, try to read some more bytes from the pipe to debug where the problem really is */
         memcpy(msg, header, sizeof(header));
         newly = read(pipe_fd, &msg[sizeof(header)], len-sizeof(header));
+       if (newly < 0) { /* error */
+           g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
+                 "read from pipe %d: error(%u): %s", pipe_fd, errno, g_strerror(errno));
+       }
         *err_msg = g_strdup_printf("Unknown message from dumpcap, try to show it as a string: %s",
                                    msg);
         return -1;
@@ -1547,13 +1615,14 @@ sync_pipe_input_cb(gint source, gpointer user_data)
     capture_options *capture_opts = (capture_options *)user_data;
     int  ret;
     char buffer[SP_MAX_MSG_LEN+1];
-    int  nread;
+    ssize_t nread;
     char indicator;
     int  primary_len;
     char *primary_msg;
     int  secondary_len;
     char *secondary_msg;
     char *wait_msg, *combined_msg;
+    int npackets;
 
     nread = pipe_read_block(source, &indicator, SP_MAX_MSG_LEN, buffer,
                             &primary_msg);
@@ -1612,25 +1681,35 @@ sync_pipe_input_cb(gint source, gpointer user_data)
                alerted. Close the sync pipe. */
             ws_close(source);
 
-            /* the child has send us a filename which we couldn't open.
-               this probably means, the child is creating files faster than we can handle it.
-               this should only be the case for very fast file switches
-               we can't do much more than telling the child to stop
-               (this is the "emergency brake" if user e.g. wants to switch files every second) */
+            /* The child has sent us a filename which we couldn't open.
+
+               This could mean that the child is creating files faster
+              than we can handle it.  (XXX - why would that result in
+              a failure to open the file?)
+
+               That should only be the case for very fast file switches;
+               We can't do much more than telling the child to stop.
+               (This is the "emergency brake" if the user e.g. wants to
+              switch files every second).
+
+               This can also happen if the user specified "-", meaning
+               "standard output", as the capture file. */
             sync_pipe_stop(capture_opts);
+            capture_input_closed(capture_opts, NULL);
+            return FALSE;
         }
         break;
     case SP_PACKET_COUNT:
-        nread = atoi(buffer);
-        g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_input_cb: new packets %u", nread);
-        capture_input_new_packets(capture_opts, nread);
+        npackets = atoi(buffer);
+        g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_input_cb: new packets %u", npackets);
+        capture_input_new_packets(capture_opts, npackets);
         break;
     case SP_ERROR_MSG:
         /* convert primary message */
-        pipe_convert_header(buffer, 4, &indicator, &primary_len);
+        pipe_convert_header((guchar*)buffer, 4, &indicator, &primary_len);
         primary_msg = buffer+4;
         /* convert secondary message */
-        pipe_convert_header(primary_msg + primary_len, 4, &indicator, &secondary_len);
+        pipe_convert_header((guchar*)primary_msg + primary_len, 4, &indicator, &secondary_len);
         secondary_msg = primary_msg + primary_len + 4;
         /* message output */
         capture_input_error_message(capture_opts, primary_msg, secondary_msg);
@@ -1639,12 +1718,12 @@ sync_pipe_input_cb(gint source, gpointer user_data)
         break;
     case SP_BAD_FILTER: {
         char *ch;
-        int index;
+        int indx;
 
         ch = strtok(buffer, ":");
-        index = (int)strtol(ch, NULL, 10);
+        indx = (int)strtol(ch, NULL, 10);
         ch = strtok(NULL, ":");
-        capture_input_cfilter_error_message(capture_opts, index, ch);
+        capture_input_cfilter_error_message(capture_opts, indx, ch);
          /* the capture child will close the sync_pipe, nothing to do for now */
          break;
         }
@@ -1660,18 +1739,33 @@ sync_pipe_input_cb(gint source, gpointer user_data)
 
 
 
-/* the child process is going down, wait until it's completely terminated */
+/*
+ * dumpcap is exiting; wait for it to exit.  On success, *msgp is
+ * unchanged, and the exit status of dumpcap is returned.  On
+ * failure (which includes "dumpcap exited due to being killed by
+ * a signal or an exception"), *msgp points to an error message
+ * for the failure, and -1 is returned.  In the latter case, *msgp
+ * must be freed with g_free().
+ */
 static int
 sync_pipe_wait_for_child(int fork_child, gchar **msgp)
 {
     int fork_child_status;
     int ret;
+    GTimeVal start_time;
+    GTimeVal end_time;
+    float elapsed;
+
+    /*
+     * GLIB_CHECK_VERSION(2,28,0) adds g_get_real_time which could minimize or
+     * replace this
+     */
+    g_get_current_time(&start_time);
 
     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_wait_for_child: wait till child closed");
     g_assert(fork_child != -1);
 
     *msgp = NULL; /* assume no error */
-    ret = 0;
 #ifdef _WIN32
     if (_cwait(&fork_child_status, fork_child, _WAIT_CHILD) == -1) {
         *msgp = g_strdup_printf("Error from cwait(): %s", g_strerror(errno));
@@ -1721,7 +1815,10 @@ sync_pipe_wait_for_child(int fork_child, gchar **msgp)
     }
 #endif
 
-    g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_wait_for_child: capture child closed");
+    g_get_current_time(&end_time);
+    elapsed = (float) ((end_time.tv_sec - start_time.tv_sec) +
+                       ((end_time.tv_usec - start_time.tv_usec) / 1e6));
+    g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_wait_for_child: capture child closed after %.3fs", elapsed);
     return ret;
 }