Add a #define for the wildcard pattern that matches all files.
[metze/wireshark/wip.git] / capture_opts.c
index ee9d401e332b3d57eeb4f08db3231d62e7e6cb84..38c39b304b73018d029f188d96e3ce994a71a73a 100644 (file)
@@ -1,8 +1,6 @@
 /* capture_opts.c
  * Routines for capture options setting
  *
- * $Id$
- *
  * Wireshark - Network traffic analyzer
  * By Gerald Combs <gerald@wireshark.org>
  * Copyright 1998 Gerald Combs
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#include "config.h"
+#include <config.h>
 
 #include <stdio.h>
+#include <stdlib.h>
 
 #ifdef HAVE_LIBPCAP
 
 #include <string.h>
-#include <ctype.h>
 
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
+#include <errno.h>
 
 #include <glib.h>
 
-#include <epan/packet.h>
-#include <epan/prefs.h>
-#include "capture_ui_utils.h"
-
 #include "capture_opts.h"
 #include "ringbuffer.h"
-#include "clopts_common.h"
-#include "cmdarg_err.h"
 
-#include "capture_ifinfo.h"
-#include "capture-pcap-util.h"
+#include <wsutil/clopts_common.h>
+#include <wsutil/cmdarg_err.h>
 #include <wsutil/file_util.h>
 
+#include "caputils/capture_ifinfo.h"
+#include "caputils/capture-pcap-util.h"
+
+#include "filter_files.h"
+
 static gboolean capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe);
 
 
 void
 capture_opts_init(capture_options *capture_opts)
 {
-  capture_opts->ifaces                          = g_array_new(FALSE, FALSE, sizeof(interface_options));
-  capture_opts->all_ifaces                      = g_array_new(FALSE, FALSE, sizeof(interface_t));
-  capture_opts->num_selected                    = 0;
-  capture_opts->default_options.name            = NULL;
-  capture_opts->default_options.descr           = NULL;
-  capture_opts->default_options.cfilter         = NULL;
-  capture_opts->default_options.has_snaplen     = FALSE;
-  capture_opts->default_options.snaplen         = WTAP_MAX_PACKET_SIZE;
-  capture_opts->default_options.linktype        = -1;
-  capture_opts->default_options.promisc_mode    = TRUE;
-#if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
-  capture_opts->default_options.buffer_size     = DEFAULT_CAPTURE_BUFFER_SIZE;
+    capture_opts->ifaces                          = g_array_new(FALSE, FALSE, sizeof(interface_options));
+    capture_opts->all_ifaces                      = g_array_new(FALSE, FALSE, sizeof(interface_t));
+    capture_opts->num_selected                    = 0;
+    capture_opts->default_options.name            = NULL;
+    capture_opts->default_options.descr           = NULL;
+    capture_opts->default_options.cfilter         = NULL;
+    capture_opts->default_options.has_snaplen     = FALSE;
+    capture_opts->default_options.snaplen         = WTAP_MAX_PACKET_SIZE;
+    capture_opts->default_options.linktype        = -1; /* use interface default */
+    capture_opts->default_options.promisc_mode    = TRUE;
+    capture_opts->default_options.if_type         = IF_WIRED;
+#ifdef HAVE_EXTCAP
+    capture_opts->default_options.extcap          = NULL;
+    capture_opts->default_options.extcap_fifo     = NULL;
+    capture_opts->default_options.extcap_args     = NULL;
+    capture_opts->default_options.extcap_pid      = INVALID_EXTCAP_PID;
 #endif
-  capture_opts->default_options.monitor_mode    = FALSE;
+#ifdef CAN_SET_CAPTURE_BUFFER_SIZE
+    capture_opts->default_options.buffer_size     = DEFAULT_CAPTURE_BUFFER_SIZE;
+#endif
+    capture_opts->default_options.monitor_mode    = FALSE;
 #ifdef HAVE_PCAP_REMOTE
-  capture_opts->default_options.src_type        = CAPTURE_IFLOCAL;
-  capture_opts->default_options.remote_host     = NULL;
-  capture_opts->default_options.remote_port     = NULL;
-  capture_opts->default_options.auth_type       = CAPTURE_AUTH_NULL;
-  capture_opts->default_options.auth_username   = NULL;
-  capture_opts->default_options.auth_password   = NULL;
-  capture_opts->default_options.datatx_udp      = FALSE;
-  capture_opts->default_options.nocap_rpcap     = TRUE;
-  capture_opts->default_options.nocap_local     = FALSE;
+    capture_opts->default_options.src_type        = CAPTURE_IFLOCAL;
+    capture_opts->default_options.remote_host     = NULL;
+    capture_opts->default_options.remote_port     = NULL;
+    capture_opts->default_options.auth_type       = CAPTURE_AUTH_NULL;
+    capture_opts->default_options.auth_username   = NULL;
+    capture_opts->default_options.auth_password   = NULL;
+    capture_opts->default_options.datatx_udp      = FALSE;
+    capture_opts->default_options.nocap_rpcap     = TRUE;
+    capture_opts->default_options.nocap_local     = FALSE;
 #endif
 #ifdef HAVE_PCAP_SETSAMPLING
-  capture_opts->default_options.sampling_method = CAPTURE_SAMP_NONE;
-  capture_opts->default_options.sampling_param  = 0;
+    capture_opts->default_options.sampling_method = CAPTURE_SAMP_NONE;
+    capture_opts->default_options.sampling_param  = 0;
 #endif
-  capture_opts->saving_to_file                  = FALSE;
-  capture_opts->save_file                       = NULL;
-  capture_opts->group_read_access               = FALSE;
+    capture_opts->saving_to_file                  = FALSE;
+    capture_opts->save_file                       = NULL;
+    capture_opts->group_read_access               = FALSE;
 #ifdef PCAP_NG_DEFAULT
-  capture_opts->use_pcapng                      = TRUE;             /* Save as pcap-ng by default */
+    capture_opts->use_pcapng                      = TRUE;             /* Save as pcap-ng by default */
 #else
-  capture_opts->use_pcapng                      = FALSE;            /* Save as pcap by default */
+    capture_opts->use_pcapng                      = FALSE;            /* Save as pcap by default */
 #endif
-  capture_opts->real_time_mode                  = TRUE;
-  capture_opts->show_info                       = TRUE;
-  capture_opts->quit_after_cap                  = getenv("WIRESHARK_QUIT_AFTER_CAPTURE") ? TRUE : FALSE;
-  capture_opts->restart                         = FALSE;
-
-  capture_opts->multi_files_on                  = FALSE;
-  capture_opts->has_file_duration               = FALSE;
-  capture_opts->file_duration                   = 60;               /* 1 min */
-  capture_opts->has_ring_num_files              = FALSE;
-  capture_opts->ring_num_files                  = RINGBUFFER_MIN_NUM_FILES;
-
-  capture_opts->has_autostop_files              = FALSE;
-  capture_opts->autostop_files                  = 1;
-  capture_opts->has_autostop_packets            = FALSE;
-  capture_opts->autostop_packets                = 0;
-  capture_opts->has_autostop_filesize           = FALSE;
-  capture_opts->autostop_filesize               = 1024;             /* 1 MB */
-  capture_opts->has_autostop_duration           = FALSE;
-  capture_opts->autostop_duration               = 60;               /* 1 min */
-  capture_opts->capture_comment                 = NULL;
-
-  capture_opts->output_to_pipe                  = FALSE;
-  capture_opts->capture_child                   = FALSE;
+    capture_opts->real_time_mode                  = TRUE;
+    capture_opts->show_info                       = TRUE;
+    capture_opts->quit_after_cap                  = getenv("WIRESHARK_QUIT_AFTER_CAPTURE") ? TRUE : FALSE;
+    capture_opts->restart                         = FALSE;
+    capture_opts->orig_save_file                  = NULL;
+
+    capture_opts->multi_files_on                  = FALSE;
+    capture_opts->has_file_duration               = FALSE;
+    capture_opts->file_duration                   = 60;               /* 1 min */
+    capture_opts->has_ring_num_files              = FALSE;
+    capture_opts->ring_num_files                  = RINGBUFFER_MIN_NUM_FILES;
+
+    capture_opts->has_autostop_files              = FALSE;
+    capture_opts->autostop_files                  = 1;
+    capture_opts->has_autostop_packets            = FALSE;
+    capture_opts->autostop_packets                = 0;
+    capture_opts->has_autostop_filesize           = FALSE;
+    capture_opts->autostop_filesize               = 1000;             /* 1 MB */
+    capture_opts->has_autostop_duration           = FALSE;
+    capture_opts->autostop_duration               = 60;               /* 1 min */
+    capture_opts->capture_comment                 = NULL;
+
+    capture_opts->output_to_pipe                  = FALSE;
+    capture_opts->capture_child                   = FALSE;
 }
 
 
@@ -137,7 +140,12 @@ capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_optio
         g_log(log_domain, log_level, "Snap length[%02d] (%u) : %d", i, interface_opts.has_snaplen, interface_opts.snaplen);
         g_log(log_domain, log_level, "Link Type[%02d]       : %d", i, interface_opts.linktype);
         g_log(log_domain, log_level, "Promiscuous Mode[%02d]: %s", i, interface_opts.promisc_mode?"TRUE":"FALSE");
-#if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
+#ifdef HAVE_EXTCAP
+        g_log(log_domain, log_level, "Extcap[%02d]          : %s", i, interface_opts.extcap ? interface_opts.extcap : "(unspecified)");
+        g_log(log_domain, log_level, "Extcap FIFO[%02d]     : %s", i, interface_opts.extcap_fifo ? interface_opts.extcap_fifo : "(unspecified)");
+        g_log(log_domain, log_level, "Extcap PID[%02d]      : %d", i, interface_opts.extcap_pid);
+#endif
+#ifdef CAN_SET_CAPTURE_BUFFER_SIZE
         g_log(log_domain, log_level, "Buffer size[%02d]     : %d (MB)", i, interface_opts.buffer_size);
 #endif
         g_log(log_domain, log_level, "Monitor Mode[%02d]    : %s", i, interface_opts.monitor_mode?"TRUE":"FALSE");
@@ -173,7 +181,11 @@ capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_optio
     g_log(log_domain, log_level, "Snap length[df] (%u) : %d", capture_opts->default_options.has_snaplen, capture_opts->default_options.snaplen);
     g_log(log_domain, log_level, "Link Type[df]       : %d", capture_opts->default_options.linktype);
     g_log(log_domain, log_level, "Promiscuous Mode[df]: %s", capture_opts->default_options.promisc_mode?"TRUE":"FALSE");
-#if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
+#ifdef HAVE_EXTCAP
+    g_log(log_domain, log_level, "Extcap[df]          : %s", capture_opts->default_options.extcap ? capture_opts->default_options.extcap : "(unspecified)");
+    g_log(log_domain, log_level, "Extcap FIFO[df]     : %s", capture_opts->default_options.extcap_fifo ? capture_opts->default_options.extcap_fifo : "(unspecified)");
+#endif
+#ifdef CAN_SET_CAPTURE_BUFFER_SIZE
     g_log(log_domain, log_level, "Buffer size[df]     : %d (MB)", capture_opts->default_options.buffer_size);
 #endif
     g_log(log_domain, log_level, "Monitor Mode[df]    : %s", capture_opts->default_options.monitor_mode?"TRUE":"FALSE");
@@ -229,46 +241,100 @@ capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_optio
 static gboolean
 set_autostop_criterion(capture_options *capture_opts, const char *autostoparg)
 {
-  gchar *p, *colonp;
-
-  colonp = strchr(autostoparg, ':');
-  if (colonp == NULL)
-    return FALSE;
-
-  p = colonp;
-  *p++ = '\0';
-
-  /*
-   * Skip over any white space (there probably won't be any, but
-   * as we allow it in the preferences file, we might as well
-   * allow it here).
-   */
-  while (isspace((guchar)*p))
-    p++;
-  if (*p == '\0') {
+    gchar *p, *colonp;
+
+    colonp = strchr(autostoparg, ':');
+    if (colonp == NULL)
+        return FALSE;
+
+    p = colonp;
+    *p++ = '\0';
+
     /*
-     * Put the colon back, so if our caller uses, in an
-     * error message, the string they passed us, the message
-     * looks correct.
+     * Skip over any white space (there probably won't be any, but
+     * as we allow it in the preferences file, we might as well
+     * allow it here).
      */
-    *colonp = ':';
-    return FALSE;
-  }
-  if (strcmp(autostoparg,"duration") == 0) {
-    capture_opts->has_autostop_duration = TRUE;
-    capture_opts->autostop_duration = get_positive_int(p,"autostop duration");
-  } else if (strcmp(autostoparg,"filesize") == 0) {
-    capture_opts->has_autostop_filesize = TRUE;
-    capture_opts->autostop_filesize = get_positive_int(p,"autostop filesize");
-  } else if (strcmp(autostoparg,"files") == 0) {
-    capture_opts->multi_files_on = TRUE;
-    capture_opts->has_autostop_files = TRUE;
-    capture_opts->autostop_files = get_positive_int(p,"autostop files");
-  } else {
-    return FALSE;
-  }
-  *colonp = ':'; /* put the colon back */
-  return TRUE;
+    while (g_ascii_isspace(*p))
+        p++;
+    if (*p == '\0') {
+        /*
+         * Put the colon back, so if our caller uses, in an
+         * error message, the string they passed us, the message
+         * looks correct.
+         */
+        *colonp = ':';
+        return FALSE;
+    }
+    if (strcmp(autostoparg,"duration") == 0) {
+        capture_opts->has_autostop_duration = TRUE;
+        capture_opts->autostop_duration = get_positive_int(p,"autostop duration");
+    } else if (strcmp(autostoparg,"filesize") == 0) {
+        capture_opts->has_autostop_filesize = TRUE;
+        capture_opts->autostop_filesize = get_positive_int(p,"autostop filesize");
+    } else if (strcmp(autostoparg,"files") == 0) {
+        capture_opts->multi_files_on = TRUE;
+        capture_opts->has_autostop_files = TRUE;
+        capture_opts->autostop_files = get_positive_int(p,"autostop files");
+    } else {
+        return FALSE;
+    }
+    *colonp = ':'; /* put the colon back */
+    return TRUE;
+}
+
+static gboolean get_filter_arguments(capture_options* capture_opts, const char* arg)
+{
+    char* colonp;
+    char* val;
+    char* filter_exp = NULL;
+
+    colonp = strchr(arg, ':');
+    if (colonp) {
+        val = colonp;
+        *val = '\0';
+        val++;
+        if (strcmp(arg, "predef") == 0) {
+            GList* filterItem;
+
+            filterItem = get_filter_list_first(CFILTER_LIST);
+            while (filterItem != NULL) {
+                filter_def *filterDef;
+
+                filterDef = (filter_def*)filterItem->data;
+                if (strcmp(val, filterDef->name) == 0) {
+                    filter_exp = g_strdup(filterDef->strval);
+                    break;
+                }
+                filterItem = filterItem->next;
+            }
+        }
+    }
+
+    if (filter_exp == NULL) {
+        /* No filter expression found yet; fallback to previous implemention
+           and assume the arg contains a filter expression */
+        if (colonp) {
+            *colonp = ':';      /* restore colon */
+        }
+        filter_exp = g_strdup(arg);
+    }
+
+    if (capture_opts->ifaces->len > 0) {
+        interface_options interface_opts;
+
+        interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
+        capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
+        g_free(interface_opts.cfilter);
+        interface_opts.cfilter = filter_exp;
+        g_array_append_val(capture_opts->ifaces, interface_opts);
+        return TRUE;
+    }
+    else {
+        g_free(capture_opts->default_options.cfilter);
+        capture_opts->default_options.cfilter = filter_exp;
+        return TRUE;
+    }
 }
 
 /*
@@ -280,45 +346,45 @@ set_autostop_criterion(capture_options *capture_opts, const char *autostoparg)
 static gboolean
 get_ring_arguments(capture_options *capture_opts, const char *arg)
 {
-  gchar *p = NULL, *colonp;
-
-  colonp = strchr(arg, ':');
-  if (colonp == NULL)
-    return FALSE;
-
-  p = colonp;
-  *p++ = '\0';
-
-  /*
-   * Skip over any white space (there probably won't be any, but
-   * as we allow it in the preferences file, we might as well
-   * allow it here).
-   */
-  while (isspace((guchar)*p))
-    p++;
-  if (*p == '\0') {
+    gchar *p = NULL, *colonp;
+
+    colonp = strchr(arg, ':');
+    if (colonp == NULL)
+        return FALSE;
+
+    p = colonp;
+    *p++ = '\0';
+
     /*
-     * Put the colon back, so if our caller uses, in an
-     * error message, the string they passed us, the message
-     * looks correct.
+     * Skip over any white space (there probably won't be any, but
+     * as we allow it in the preferences file, we might as well
+     * allow it here).
      */
-    *colonp = ':';
-    return FALSE;
-  }
-
-  if (strcmp(arg,"files") == 0) {
-    capture_opts->has_ring_num_files = TRUE;
-    capture_opts->ring_num_files = get_positive_int(p, "number of ring buffer files");
-  } else if (strcmp(arg,"filesize") == 0) {
-    capture_opts->has_autostop_filesize = TRUE;
-    capture_opts->autostop_filesize = get_positive_int(p, "ring buffer filesize");
-  } else if (strcmp(arg,"duration") == 0) {
-    capture_opts->has_file_duration = TRUE;
-    capture_opts->file_duration = get_positive_int(p, "ring buffer duration");
-  }
-
-  *colonp = ':';    /* put the colon back */
-  return TRUE;
+    while (g_ascii_isspace(*p))
+        p++;
+    if (*p == '\0') {
+        /*
+         * Put the colon back, so if our caller uses, in an
+         * error message, the string they passed us, the message
+         * looks correct.
+         */
+        *colonp = ':';
+        return FALSE;
+    }
+
+    if (strcmp(arg,"files") == 0) {
+        capture_opts->has_ring_num_files = TRUE;
+        capture_opts->ring_num_files = get_positive_int(p, "number of ring buffer files");
+    } else if (strcmp(arg,"filesize") == 0) {
+        capture_opts->has_autostop_filesize = TRUE;
+        capture_opts->autostop_filesize = get_positive_int(p, "ring buffer filesize");
+    } else if (strcmp(arg,"duration") == 0) {
+        capture_opts->has_file_duration = TRUE;
+        capture_opts->file_duration = get_positive_int(p, "ring buffer duration");
+    }
+
+    *colonp = ':';    /* put the colon back */
+    return TRUE;
 }
 
 #ifdef HAVE_PCAP_SETSAMPLING
@@ -340,7 +406,7 @@ get_sampling_arguments(capture_options *capture_opts, const char *arg)
     p = colonp;
     *p++ = '\0';
 
-    while (isspace((guchar)*p))
+    while (g_ascii_isspace(*p))
         p++;
     if (*p == '\0') {
         *colonp = ':';
@@ -398,7 +464,7 @@ get_auth_arguments(capture_options *capture_opts, const char *arg)
     p = colonp;
     *p++ = '\0';
 
-    while (isspace((guchar)*p))
+    while (g_ascii_isspace(*p))
         p++;
 
     if (capture_opts->ifaces->len > 0) {
@@ -456,17 +522,11 @@ capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str
         }
         if_list = capture_interface_list(&err, &err_str, NULL);
         if (if_list == NULL) {
-            switch (err) {
-
-            case CANT_GET_INTERFACE_LIST:
-            case DONT_HAVE_PCAP:
+            if (err == 0)
+                cmdarg_err("There are no interfaces on which a capture can be done");
+            else {
                 cmdarg_err("%s", err_str);
                 g_free(err_str);
-                break;
-
-            case NO_INTERFACES_FOUND:
-                cmdarg_err("There are no interfaces on which a capture can be done");
-                break;
             }
             return 2;
         }
@@ -489,12 +549,20 @@ capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str
             /* fallback to the interface name */
             interface_opts.console_display_name = g_strdup(if_info->name);
         }
+        interface_opts.if_type = if_info->type;
+#ifdef HAVE_EXTCAP
+        interface_opts.extcap = g_strdup(if_info->extcap);
+#endif
         free_interface_list(if_list);
     } else if (capture_opts->capture_child) {
         /* In Wireshark capture child mode, thus proper device name is supplied. */
         /* No need for trying to match it for friendly names. */
         interface_opts.name = g_strdup(optarg_str_p);
         interface_opts.console_display_name = g_strdup(optarg_str_p);
+        interface_opts.if_type = capture_opts->default_options.if_type;
+#ifdef HAVE_EXTCAP
+        interface_opts.extcap = g_strdup(capture_opts->default_options.extcap);
+#endif
     } else {
         /*
          * Retrieve the interface list so that we can search for the
@@ -532,6 +600,10 @@ capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str
                     } else {
                         interface_opts.console_display_name = g_strdup(if_info->name);
                     }
+                    interface_opts.if_type = if_info->type;
+#ifdef HAVE_EXTCAP
+                    interface_opts.extcap = g_strdup(if_info->extcap);
+#endif
                     matched = TRUE;
                     break;
                 }
@@ -542,6 +614,10 @@ capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str
                     /* exact match - use the friendly name for display */
                     interface_opts.name = g_strdup(if_info->name);
                     interface_opts.console_display_name = g_strdup(if_info->friendly_name);
+                    interface_opts.if_type = if_info->type;
+#ifdef HAVE_EXTCAP
+                    interface_opts.extcap = g_strdup(if_info->extcap);
+#endif
                     matched = TRUE;
                     break;
                 }
@@ -562,6 +638,10 @@ capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str
                         /* prefix match - use the friendly name for display */
                         interface_opts.name = g_strdup(if_info->name);
                         interface_opts.console_display_name = g_strdup(if_info->friendly_name);
+                        interface_opts.if_type = if_info->type;
+#ifdef HAVE_EXTCAP
+                        interface_opts.extcap = g_strdup(if_info->extcap);
+#endif
                         matched = TRUE;
                         break;
                     }
@@ -577,11 +657,19 @@ capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str
                  */
                 interface_opts.name = g_strdup(optarg_str_p);
                 interface_opts.console_display_name = g_strdup(optarg_str_p);
+                interface_opts.if_type = capture_opts->default_options.if_type;
+#ifdef HAVE_EXTCAP
+                interface_opts.extcap = g_strdup(capture_opts->default_options.extcap);
+#endif
             }
             free_interface_list(if_list);
         } else {
             interface_opts.name = g_strdup(optarg_str_p);
             interface_opts.console_display_name = g_strdup(optarg_str_p);
+            interface_opts.if_type = capture_opts->default_options.if_type;
+#ifdef HAVE_EXTCAP
+            interface_opts.extcap = g_strdup(capture_opts->default_options.extcap);
+#endif
         }
     }
 
@@ -595,7 +683,12 @@ capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str
     interface_opts.has_snaplen = capture_opts->default_options.has_snaplen;
     interface_opts.linktype = capture_opts->default_options.linktype;
     interface_opts.promisc_mode = capture_opts->default_options.promisc_mode;
-#if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
+#ifdef HAVE_EXTCAP
+    interface_opts.extcap_fifo = g_strdup(capture_opts->default_options.extcap_fifo);
+    interface_opts.extcap_args = NULL;
+    interface_opts.extcap_pid = INVALID_EXTCAP_PID;
+#endif
+#ifdef CAN_SET_CAPTURE_BUFFER_SIZE
     interface_opts.buffer_size = capture_opts->default_options.buffer_size;
 #endif
     interface_opts.monitor_mode = capture_opts->default_options.monitor_mode;
@@ -628,6 +721,10 @@ capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg_
 
     switch(opt) {
     case LONGOPT_NUM_CAP_COMMENT:  /* capture comment */
+        if (capture_opts->capture_comment) {
+            cmdarg_err("--capture-comment can be set only once per file");
+            return 1;
+        }
         capture_opts->capture_comment = g_strdup(optarg_str_p);
         break;
     case 'a':        /* autostop criteria */
@@ -651,7 +748,7 @@ capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg_
             return 1;
         }
         break;
-#if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
+#ifdef CAN_SET_CAPTURE_BUFFER_SIZE
     case 'B':        /* Buffer size */
         if (capture_opts->ifaces->len > 0) {
             interface_options interface_opts;
@@ -670,18 +767,7 @@ capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg_
         capture_opts->autostop_packets = get_positive_int(optarg_str_p, "packet count");
         break;
     case 'f':        /* capture filter */
-        if (capture_opts->ifaces->len > 0) {
-            interface_options interface_opts;
-
-            interface_opts = g_array_index(capture_opts->ifaces, interface_options, capture_opts->ifaces->len - 1);
-            capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, capture_opts->ifaces->len - 1);
-            g_free(interface_opts.cfilter);
-            interface_opts.cfilter = g_strdup(optarg_str_p);
-            g_array_append_val(capture_opts->ifaces, interface_opts);
-        } else {
-            g_free(capture_opts->default_options.cfilter);
-            capture_opts->default_options.cfilter = g_strdup(optarg_str_p);
-        }
+        get_filter_arguments(capture_opts, optarg_str_p);
         break;
     case 'g':        /* enable group read access on the capture file(s) */
         capture_opts->group_read_access = TRUE;
@@ -956,126 +1042,211 @@ capture_opts_default_iface_if_necessary(capture_options *capture_opts,
 static int
 capture_opts_test_for_fifo(const char *path)
 {
-  ws_statb64 statb;
+    ws_statb64 statb;
 
-  if (ws_stat64(path, &statb) < 0)
-    return errno;
+    if (ws_stat64(path, &statb) < 0)
+        return errno;
 
-  if (S_ISFIFO(statb.st_mode))
-    return ESPIPE;
-  else
-    return 0;
+    if (S_ISFIFO(statb.st_mode))
+        return ESPIPE;
+    else
+        return 0;
 }
 
 static gboolean
 capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe)
 {
-  int err;
-
-  *is_pipe = FALSE;
-
-  if (save_file != NULL) {
-    /* We're writing to a capture file. */
-    if (strcmp(save_file, "-") == 0) {
-      /* Writing to stdout. */
-      /* XXX - should we check whether it's a pipe?  It's arguably
-         silly to do "-w - >output_file" rather than "-w output_file",
-         but by not checking we might be violating the Principle Of
-         Least Astonishment. */
-      *is_pipe = TRUE;
-    } else {
-      /* not writing to stdout, test for a FIFO (aka named pipe) */
-      err = capture_opts_test_for_fifo(save_file);
-      switch (err) {
+    int err;
+
+    *is_pipe = FALSE;
+
+    if (save_file != NULL) {
+        /* We're writing to a capture file. */
+        if (strcmp(save_file, "-") == 0) {
+            /* Writing to stdout. */
+            /* XXX - should we check whether it's a pipe?  It's arguably
+               silly to do "-w - >output_file" rather than "-w output_file",
+               but by not checking we might be violating the Principle Of
+               Least Astonishment. */
+            *is_pipe = TRUE;
+        } else {
+            /* not writing to stdout, test for a FIFO (aka named pipe) */
+            err = capture_opts_test_for_fifo(save_file);
+            switch (err) {
 
-      case ENOENT:      /* it doesn't exist, so we'll be creating it,
-                           and it won't be a FIFO */
-      case 0:           /* found it, but it's not a FIFO */
-        break;
+            case ENOENT:      /* it doesn't exist, so we'll be creating it,
+                                 and it won't be a FIFO */
+            case 0:           /* found it, but it's not a FIFO */
+                break;
 
-      case ESPIPE:      /* it is a FIFO */
-        *is_pipe = TRUE;
-        break;
+            case ESPIPE:      /* it is a FIFO */
+                *is_pipe = TRUE;
+                break;
 
-      default:          /* couldn't stat it              */
-        break;          /* ignore: later attempt to open */
-                        /*  will generate a nice msg     */
-      }
+            default:          /* couldn't stat it              */
+                break;          /* ignore: later attempt to open */
+                /*  will generate a nice msg     */
+            }
+        }
     }
-  }
 
-  return 0;
+    return 0;
 }
 
-/*
- * Add all non-hidden selected interfaces in the "all interfaces" list
- * to the list of interfaces for the capture.
- */
 void
-collect_ifaces(capture_options *capture_opts)
+capture_opts_del_iface(capture_options *capture_opts, guint if_index)
 {
-  guint i;
-  interface_t device;
-  interface_options interface_opts;
+    interface_options interface_opts;
+
+    interface_opts = g_array_index(capture_opts->ifaces, interface_options, if_index);
+    /* XXX - check if found? */
 
-  /* Empty out the existing list of interfaces. */
-  for (i = capture_opts->ifaces->len; i != 0; i--) {
-    interface_opts = g_array_index(capture_opts->ifaces, interface_options, i - 1);
     g_free(interface_opts.name);
     g_free(interface_opts.descr);
     if (interface_opts.console_display_name != NULL)
         g_free(interface_opts.console_display_name);
     g_free(interface_opts.cfilter);
+#ifdef HAVE_EXTCAP
+    g_free(interface_opts.extcap);
+    g_free(interface_opts.extcap_fifo);
+    if (interface_opts.extcap_args)
+        g_hash_table_unref(interface_opts.extcap_args);
+    if (interface_opts.extcap_pid != INVALID_EXTCAP_PID)
+        g_spawn_close_pid(interface_opts.extcap_pid);
+#endif
 #ifdef HAVE_PCAP_REMOTE
     if (interface_opts.src_type == CAPTURE_IFREMOTE) {
-      g_free(interface_opts.remote_host);
-      g_free(interface_opts.remote_port);
-      g_free(interface_opts.auth_username);
-      g_free(interface_opts.auth_password);
+        g_free(interface_opts.remote_host);
+        g_free(interface_opts.remote_port);
+        g_free(interface_opts.auth_username);
+        g_free(interface_opts.auth_password);
     }
 #endif
-    capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, i - 1);
-  }
-
-  /* Now fill the list up again. */
-  for (i = 0; i < capture_opts->all_ifaces->len; i++) {
-    device = g_array_index(capture_opts->all_ifaces, interface_t, i);
-    if (!device.hidden && device.selected) {
-      interface_opts.name = g_strdup(device.name);
-      interface_opts.descr = g_strdup(device.display_name);
-      interface_opts.console_display_name = g_strdup(device.name);
-      interface_opts.linktype = device.active_dlt;
-      interface_opts.cfilter = g_strdup(device.cfilter);
-      interface_opts.snaplen = device.snaplen;
-      interface_opts.has_snaplen = device.has_snaplen;
-      interface_opts.promisc_mode = device.pmode;
-#if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
-      interface_opts.buffer_size =  device.buffer;
+    capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, if_index);
+}
+
+
+
+/*
+ * Add all non-hidden selected interfaces in the "all interfaces" list
+ * to the list of interfaces for the capture.
+ */
+void
+collect_ifaces(capture_options *capture_opts)
+{
+    guint i;
+    interface_t device;
+    interface_options interface_opts;
+
+    /* Empty out the existing list of interfaces. */
+    for (i = capture_opts->ifaces->len; i != 0; i--)
+        capture_opts_del_iface(capture_opts, i-1);
+
+    /* Now fill the list up again. */
+    for (i = 0; i < capture_opts->all_ifaces->len; i++) {
+        device = g_array_index(capture_opts->all_ifaces, interface_t, i);
+        if (!device.hidden && device.selected) {
+            interface_opts.name = g_strdup(device.name);
+            interface_opts.descr = g_strdup(device.display_name);
+            interface_opts.console_display_name = g_strdup(device.name);
+            interface_opts.linktype = device.active_dlt;
+            interface_opts.cfilter = g_strdup(device.cfilter);
+            interface_opts.snaplen = device.snaplen;
+            interface_opts.has_snaplen = device.has_snaplen;
+            interface_opts.promisc_mode = device.pmode;
+            interface_opts.if_type = device.if_info.type;
+#ifdef HAVE_EXTCAP
+            interface_opts.extcap = g_strdup(device.if_info.extcap);
+            interface_opts.extcap_fifo = NULL;
+            interface_opts.extcap_args = device.external_cap_args_settings;
+            interface_opts.extcap_pid = INVALID_EXTCAP_PID;
+            if (interface_opts.extcap_args)
+                g_hash_table_ref(interface_opts.extcap_args);
+#endif
+#ifdef CAN_SET_CAPTURE_BUFFER_SIZE
+            interface_opts.buffer_size =  device.buffer;
 #endif
 #ifdef HAVE_PCAP_CREATE
-      interface_opts.monitor_mode = device.monitor_mode_enabled;
+            interface_opts.monitor_mode = device.monitor_mode_enabled;
 #endif
 #ifdef HAVE_PCAP_REMOTE
-      interface_opts.src_type = CAPTURE_IFREMOTE;
-      interface_opts.remote_host = g_strdup(device.remote_opts.remote_host_opts.remote_host);
-      interface_opts.remote_port = g_strdup(device.remote_opts.remote_host_opts.remote_port);
-      interface_opts.auth_type = device.remote_opts.remote_host_opts.auth_type;
-      interface_opts.auth_username = g_strdup(device.remote_opts.remote_host_opts.auth_username);
-      interface_opts.auth_password = g_strdup(device.remote_opts.remote_host_opts.auth_password);
-      interface_opts.datatx_udp = device.remote_opts.remote_host_opts.datatx_udp;
-      interface_opts.nocap_rpcap = device.remote_opts.remote_host_opts.nocap_rpcap;
-      interface_opts.nocap_local = device.remote_opts.remote_host_opts.nocap_local;
+            interface_opts.src_type = CAPTURE_IFREMOTE;
+            interface_opts.remote_host = g_strdup(device.remote_opts.remote_host_opts.remote_host);
+            interface_opts.remote_port = g_strdup(device.remote_opts.remote_host_opts.remote_port);
+            interface_opts.auth_type = device.remote_opts.remote_host_opts.auth_type;
+            interface_opts.auth_username = g_strdup(device.remote_opts.remote_host_opts.auth_username);
+            interface_opts.auth_password = g_strdup(device.remote_opts.remote_host_opts.auth_password);
+            interface_opts.datatx_udp = device.remote_opts.remote_host_opts.datatx_udp;
+            interface_opts.nocap_rpcap = device.remote_opts.remote_host_opts.nocap_rpcap;
+            interface_opts.nocap_local = device.remote_opts.remote_host_opts.nocap_local;
 #endif
 #ifdef HAVE_PCAP_SETSAMPLING
-      interface_opts.sampling_method = device.remote_opts.sampling_method;
-      interface_opts.sampling_param  = device.remote_opts.sampling_param;
+            interface_opts.sampling_method = device.remote_opts.sampling_method;
+            interface_opts.sampling_param  = device.remote_opts.sampling_param;
 #endif
-      g_array_append_val(capture_opts->ifaces, interface_opts);
-    } else {
-      continue;
+            g_array_append_val(capture_opts->ifaces, interface_opts);
+        } else {
+            continue;
+        }
     }
-  }
 }
 
+static void
+capture_opts_free_interface_t_links(gpointer elem, gpointer unused _U_)
+{
+    link_row* e = (link_row*)elem;
+    if (e != NULL)
+        g_free(e->name);
+    g_free(elem);
+}
+
+static void
+capture_opts_free_interface_t_addrs(gpointer elem, gpointer unused _U_)
+{
+    g_free(elem);
+}
+
+void
+capture_opts_free_interface_t(interface_t *device)
+{
+    if (device != NULL) {
+        g_free(device->name);
+        g_free(device->display_name);
+        g_free(device->friendly_name);
+        g_free(device->addresses);
+        g_free(device->cfilter);
+        g_list_foreach(device->links,
+                       capture_opts_free_interface_t_links, NULL);
+        g_list_free(device->links);
+#ifdef HAVE_PCAP_REMOTE
+        g_free(device->remote_opts.remote_host_opts.remote_host);
+        g_free(device->remote_opts.remote_host_opts.remote_port);
+        g_free(device->remote_opts.remote_host_opts.auth_username);
+        g_free(device->remote_opts.remote_host_opts.auth_password);
+#endif
+        g_free(device->if_info.name);
+        g_free(device->if_info.friendly_name);
+        g_free(device->if_info.vendor_description);
+        g_slist_foreach(device->if_info.addrs,
+                        capture_opts_free_interface_t_addrs, NULL);
+        g_slist_free(device->if_info.addrs);
+#ifdef HAVE_EXTCAP
+        g_free(device->if_info.extcap);
+#endif
+    }
+}
 
 #endif /* HAVE_LIBPCAP */
+
+/*
+ * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
+ *
+ * Local variables:
+ * c-basic-offset: 4
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ *
+ * vi: set shiftwidth=4 tabstop=8 expandtab:
+ * :indentSize=4:tabSize=8:noTabs=true:
+ */