wmem: allow wmem_destroy_list to ignore a NULL list.
[metze/wireshark/wip.git] / extcap.c
index 2c4444b12a37736e31f68e090b10a3731ad99122..cdd8e0f70e02cb6792f90e8baa5f2d6077fe504f 100644 (file)
--- a/extcap.c
+++ b/extcap.c
@@ -1,4 +1,4 @@
-/* extcap.h
+/* extcap.c
  *
  * Routines for extcap external capture
  * Copyright 2013, Mike Ryan <mikeryan@lacklustre.net>
@@ -7,19 +7,7 @@
  * By Gerald Combs <gerald@wireshark.org>
  * Copyright 1998 Gerald Combs
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
 #include <config.h>
 #include <unistd.h>
 #endif
 
+#include <sys/types.h>
+#ifdef HAVE_SYS_WAIT_H
+#include <sys/wait.h>
+#endif
+
 #include <glib.h>
 #include <log.h>
 
 #include <epan/prefs.h>
-#include <epan/prefs-int.h>
+
+#include "ui/iface_toolbar.h"
 
 #include <wsutil/file_util.h>
 #include <wsutil/filesystem.h>
+#include <wsutil/ws_pipe.h>
 #include <wsutil/tempfile.h>
 
 #include "capture_opts.h"
 
-#ifdef HAVE_EXTCAP
-
 #include "extcap.h"
 #include "extcap_parser.h"
 
+#include "version_info.h"
+
 #ifdef _WIN32
-static HANDLE pipe_h = NULL;
+static HANDLE pipe_h = INVALID_HANDLE_VALUE;
 #endif
 
-/* internal container, for all the extcap interfaces that have been found.
- * will be resetted by every call to extcap_interface_list() and is being
- * used in extcap_get_if_* as well as extcap_init_interfaces to ensure,
- * that only extcap interfaces are being given to underlying extcap programs
- */
-static GHashTable *ifaces = NULL;
+static void extcap_child_watch_cb(GPid pid, gint status, gpointer user_data);
 
 /* internal container, for all the extcap executables that have been found.
- * will be resetted by every call to extcap_interface_list() and is being
- * used for printing information about all extcap interfaces found
+ * Will be resetted if extcap_clear_interfaces() is being explicitly called
+ * and is being used for printing information about all extcap interfaces found,
+ * as well as storing all sub-interfaces
  */
-static GHashTable *tools = NULL;
+static GHashTable * _loaded_interfaces = NULL;
+
+/* Internal container, which maps each ifname to the tool providing it, for faster
+ * lookup. The key and string value are owned by this table.
+ */
+static GHashTable * _tool_for_ifname = NULL;
+
+/* internal container, for all the extcap executables that have been found
+ * and that provides a toolbar with controls to be added to a Interface Toolbar
+ */
+static GHashTable *_toolbars = NULL;
+
+/* internal container, to map preference names to pointers that hold preference
+ * values. These ensure that preferences can survive extcap if garbage
+ * collection, and does not lead to dangling pointers in the prefs subsystem.
+ */
+static GHashTable *extcap_prefs_dynamic_vals = NULL;
+
+typedef struct _extcap_callback_info_t
+{
+    const gchar * extcap;
+    const gchar * ifname;
+    gchar * output;
+    void * data;
+    gchar ** err_str;
+} extcap_callback_info_t;
 
 /* Callback definition for extcap_foreach */
-typedef gboolean (*extcap_cb_t)(const gchar *extcap, const gchar *ifname, gchar *output, void *data,
-        gchar **err_str);
+typedef gboolean(*extcap_cb_t)(extcap_callback_info_t info_structure);
+
+/** GThreadPool does not support pushing new work from a thread while waiting
+ * for the thread pool to finish. This data structure tracks ongoing work.
+ * See https://gitlab.gnome.org/GNOME/glib/issues/1598 */
+typedef struct thread_pool {
+    GThreadPool    *pool;
+    gint            count;  /**< Number of tasks that have not finished. */
+    GCond           cond;
+    GMutex          data_mutex;
+} thread_pool_t;
+
+/**
+ * Callback definition for extcap_run_all, invoked with a thread pool (to
+ * schedule new tasks), an opaque data parameter, and the output from last task
+ * (or NULL if it failed). The output must be freed by the callback function.
+ * The implementation MUST be thread-safe.
+ */
+typedef void (*extcap_run_cb_t)(thread_pool_t *pool, void *data, char *output);
 
-/* #define ARG_DEBUG */
-#if ARG_DEBUG
-static void extcap_debug_arguments ( extcap_arg *arg_iter );
-#endif
+typedef struct extcap_run_task {
+    const char     *extcap_path;
+    char          **argv;       /**< NULL-terminated arguments list, freed when the task is completed. */
+    extcap_run_cb_t output_cb;
+    void           *data;       /** Parameter to be passed to output_cb. */
+} extcap_run_task_t;
+
+typedef struct extcap_iface_info {
+    char *ifname;                       /**< Interface name. */
+    char *output;                       /**< Output of --extcap-config. */
+} extcap_iface_info_t;
+
+typedef struct extcap_run_extcaps_info {
+    char    *extcap_path;               /**< Extcap program path, MUST be the first member.  */
+    char    *output;                    /**< Output of --extcap-interfaces. */
+    guint   num_interfaces;             /**< Number of discovered interfaces. */
+    extcap_iface_info_t *iface_infos;   /**< Per-interface information. */
+} extcap_run_extcaps_info_t;
+
+
+static void extcap_load_interface_list(void);
 
 static gboolean
-extcap_if_exists(const gchar *ifname)
+thread_pool_push(thread_pool_t *pool, gpointer data, GError **error)
 {
-    if ( !ifname || !ifaces )
-        return FALSE;
+    g_mutex_lock(&pool->data_mutex);
+    ++pool->count;
+    g_mutex_unlock(&pool->data_mutex);
+    return g_thread_pool_push(pool->pool, data, error);
+}
 
-    if ( g_hash_table_lookup(ifaces, ifname) )
-        return TRUE;
+static void
+thread_pool_wait(thread_pool_t *pool)
+{
+    g_mutex_lock(&pool->data_mutex);
+    while (pool->count != 0) {
+        g_cond_wait(&pool->cond, &pool->data_mutex);
+    }
+    g_cond_clear(&pool->cond);
+    g_mutex_unlock(&pool->data_mutex);
+    g_mutex_clear(&pool->data_mutex);
+}
 
-    return FALSE;
+GHashTable *
+extcap_loaded_interfaces(void)
+{
+    if (prefs.capture_no_extcap)
+        return NULL;
+
+    if ( !_loaded_interfaces || g_hash_table_size(_loaded_interfaces) == 0 )
+        extcap_load_interface_list();
+
+    return _loaded_interfaces;
 }
 
-static gboolean
-extcap_if_exists_for_extcap(const gchar *ifname, const gchar *extcap)
+void
+extcap_clear_interfaces(void)
 {
-    gchar *entry = (gchar *)g_hash_table_lookup(ifaces, ifname);
+    if ( _loaded_interfaces )
+        g_hash_table_destroy(_loaded_interfaces);
+    _loaded_interfaces = NULL;
 
-    if ( entry && strcmp(entry, extcap) == 0 )
-        return TRUE;
+    if ( _tool_for_ifname )
+        g_hash_table_destroy(_tool_for_ifname);
+    _tool_for_ifname = NULL;
+}
 
-    return FALSE;
+/**
+ * Obtains a list of extcap program paths. Use g_slist_free_full(paths, g_free)
+ * to destroy the list.
+ */
+static GSList *
+extcap_get_extcap_paths(void)
+{
+    GDir *dir;
+    const char *dirname = get_extcap_dir();
+    const gchar *file;
+    GSList *paths = NULL;
+
+    if ((dir = g_dir_open(dirname, 0, NULL)) != NULL) {
+        while ((file = g_dir_read_name(dir)) != NULL) {
+            /* full path to extcap binary */
+            gchar *extcap_path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", dirname, file);
+            /* treat anything executable as an extcap binary */
+            if (g_file_test(extcap_path, G_FILE_TEST_IS_REGULAR) &&
+                g_file_test(extcap_path, G_FILE_TEST_IS_EXECUTABLE)) {
+                paths = g_slist_append(paths, extcap_path);
+            } else {
+                g_free(extcap_path);
+            }
+
+        }
+        g_dir_close(dir);
+    }
+
+    return paths;
 }
 
-static gchar *
-extcap_if_executable(const gchar *ifname)
+static extcap_interface *
+extcap_find_interface_for_ifname(const gchar *ifname)
+{
+    extcap_interface * result = NULL;
+
+    if ( !ifname || ! _tool_for_ifname || ! _loaded_interfaces )
+        return result;
+
+    gchar * extcap_util = (gchar *)g_hash_table_lookup(_tool_for_ifname, ifname);
+    if ( ! extcap_util )
+        return result;
+
+    extcap_info * element = (extcap_info *)g_hash_table_lookup(_loaded_interfaces, extcap_util);
+    if ( ! element )
+        return result;
+
+    GList * walker = element->interfaces;
+    while ( walker && walker->data && ! result )
+    {
+        extcap_interface * interface = (extcap_interface *)walker->data;
+        if ( g_strcmp0(interface->call, ifname) == 0 )
+        {
+            result = interface;
+            break;
+        }
+
+        walker = g_list_next ( walker );
+    }
+
+    return result;
+}
+
+static void
+extcap_free_toolbar_value(iface_toolbar_value *value)
+{
+    if (!value)
+    {
+        return;
+    }
+
+    g_free(value->value);
+    g_free(value->display);
+    g_free(value);
+}
+
+static void
+extcap_free_toolbar_control(iface_toolbar_control *control)
 {
-    return (gchar *)g_hash_table_lookup(ifaces, ifname);
+    if (!control)
+    {
+        return;
+    }
+
+    g_free(control->display);
+    g_free(control->validation);
+    g_free(control->tooltip);
+    if (control->ctrl_type == INTERFACE_TYPE_STRING) {
+        g_free(control->default_value.string);
+    }
+    g_list_free_full(control->values, (GDestroyNotify)extcap_free_toolbar_value);
+    g_free(control);
 }
 
 static void
-extcap_if_add(const gchar *ifname, const gchar *extcap)
+extcap_free_toolbar(gpointer data)
+{
+    if (!data)
+    {
+        return;
+    }
+
+    iface_toolbar *toolbar = (iface_toolbar *)data;
+
+    g_free(toolbar->menu_title);
+    g_free(toolbar->help);
+    g_list_free_full(toolbar->ifnames, g_free);
+    g_list_free_full(toolbar->controls, (GDestroyNotify)extcap_free_toolbar_control);
+    g_free(toolbar);
+}
+
+static gchar *
+extcap_if_executable(const gchar *ifname)
 {
-    if ( !g_hash_table_lookup(ifaces, ifname) )
-        g_hash_table_insert(ifaces, g_strdup(ifname), g_strdup(extcap));
+    extcap_interface *interface = extcap_find_interface_for_ifname(ifname);
+    return interface != NULL ? interface->extcap_path : NULL;
 }
 
 static void
-extcap_tool_add(const gchar *extcap, const extcap_interface *interface)
+extcap_iface_toolbar_add(const gchar *extcap, iface_toolbar *toolbar_entry)
 {
     char *toolname;
 
-    if ( !extcap || !interface )
+    if (!extcap || !toolbar_entry)
+    {
         return;
+    }
 
     toolname = g_path_get_basename(extcap);
 
-    if ( !g_hash_table_lookup(tools, toolname) ) {
-        extcap_info * store = (extcap_info *)g_new0(extcap_info, 1);
-        store->version = g_strdup(interface->version);
-        store->full_path = g_strdup(extcap);
-        store->basename = g_strdup(toolname);
-
-        g_hash_table_insert(tools, g_strdup(toolname), store);
+    if (!g_hash_table_lookup(_toolbars, toolname))
+    {
+        g_hash_table_insert(_toolbars, g_strdup(toolname), toolbar_entry);
     }
 
     g_free(toolname);
 }
 
-/* Note: args does not need to be NULL-terminated. */
-static void extcap_foreach(gint argc, gchar **args, extcap_cb_t cb,
-        void *cb_data, char **err_str, const char * ifname _U_) {
-    const char *dirname = get_extcap_dir();
-    GDir *dir;
-    const gchar *file;
-    gboolean keep_going;
-    gint i;
-    gchar **argv;
-#ifdef _WIN32
-    gchar **dll_search_envp;
-    gchar *progfile_dir;
-#endif
+static gchar **
+extcap_convert_arguments_to_array(GList * arguments)
+{
+    gchar ** result = NULL;
+    if ( arguments )
+    {
+        GList * walker = g_list_first(arguments);
+        int cnt = 0;
 
-    keep_going = TRUE;
+        result = (gchar **) g_malloc0(sizeof(gchar *) * (g_list_length(arguments)));
 
-    argv = (gchar **) g_malloc0(sizeof(gchar *) * (argc + 2));
+        while(walker)
+        {
+            result[cnt] = g_strdup((const gchar *)walker->data);
+            walker = g_list_next(walker);
+            cnt++;
+        }
+    }
+    return result;
+}
 
-#ifdef _WIN32
-    /*
-     * Make sure executables can find dependent DLLs and that they're *our*
-     * DLLs: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682586.aspx
-     * Alternatively we could create a simple wrapper exe similar to Create
-     * Hidden Process (http://www.commandline.co.uk/chp/).
-     */
-    dll_search_envp = g_get_environ();
-    progfile_dir = g_strdup_printf("%s;%s", get_progfile_dir(), g_environ_getenv(dll_search_envp, "Path"));
-    dll_search_envp = g_environ_setenv(dll_search_envp, "Path", progfile_dir, TRUE);
-    g_free(progfile_dir);
-#endif
+static void extcap_free_array(gchar ** args, int argc)
+{
+    int cnt = 0;
 
-    if ((dir = g_dir_open(dirname, 0, NULL)) != NULL) {
-        GString *extcap_path = NULL;
+    for ( cnt = 0; cnt < argc; cnt++ )
+        g_free(args[cnt]);
+    g_free(args);
+}
 
-        extcap_path = g_string_new("");
-        while (keep_going && (file = g_dir_read_name(dir)) != NULL ) {
-            gchar *command_output = NULL;
-            gboolean status = FALSE;
-            gint exit_status = 0;
-            gchar **envp = NULL;
+static void
+extcap_free_extcaps_info_array(extcap_run_extcaps_info_t *infos, guint count)
+{
+    for (guint i = 0; i < count; i++) {
+        g_free(infos[i].extcap_path);
+        g_free(infos[i].output);
+        for (guint j = 0; j < infos[i].num_interfaces; j++) {
+            extcap_iface_info_t *iface_info = &infos[i].iface_infos[j];
+            g_free(iface_info->ifname);
+            g_free(iface_info->output);
+        }
+        g_free(infos[i].iface_infos);
+    }
+    g_free(infos);
+}
 
-            /* full path to extcap binary */
-#ifdef _WIN32
-            g_string_printf(extcap_path, "%s\\%s", dirname, file);
-            envp = dll_search_envp;
-#else
-            g_string_printf(extcap_path, "%s/%s", dirname, file);
-#endif
-            if ( extcap_if_exists(ifname) && !extcap_if_exists_for_extcap(ifname, extcap_path->str ) )
-                continue;
+static void
+extcap_run_one(const extcap_interface *interface, GList *arguments, extcap_cb_t cb, void *user_data, char **err_str)
+{
+    const char *dirname = get_extcap_dir();
+    gchar **args = extcap_convert_arguments_to_array(arguments);
+    int cnt = g_list_length(arguments);
+    gchar *command_output;
+    if (ws_pipe_spawn_sync(dirname, interface->extcap_path, cnt, args, &command_output)) {
+        extcap_callback_info_t cb_info = {
+            .ifname = interface->call,
+            .extcap = interface->extcap_path,
+            .output = command_output,
+            .data = user_data,
+            .err_str = err_str,
+        };
+        cb(cb_info);
+        g_free(command_output);
+    }
+    extcap_free_array(args, cnt);
+}
 
-#ifdef _WIN32
-            argv[0] = g_strescape(extcap_path->str, NULL);
+/** Thread callback to run an extcap program and pass its output. */
+static void
+extcap_thread_callback(gpointer data, gpointer user_data)
+{
+    extcap_run_task_t *task = (extcap_run_task_t *)data;
+    thread_pool_t *pool = (thread_pool_t *)user_data;
+    const char *dirname = get_extcap_dir();
+
+    char *command_output;
+    if (ws_pipe_spawn_sync(dirname, task->extcap_path, g_strv_length(task->argv), task->argv, &command_output)) {
+        task->output_cb(pool, task->data, command_output);
+    } else {
+        task->output_cb(pool, task->data, NULL);
+    }
+    g_strfreev(task->argv);
+    g_free(task);
+
+    // Notify when all tasks are completed and no new subtasks were created.
+    g_mutex_lock(&pool->data_mutex);
+    if (--pool->count == 0) {
+        g_cond_signal(&pool->cond);
+    }
+    g_mutex_unlock(&pool->data_mutex);
+}
+
+/*
+ * Run all extcap programs with the given arguments list, invoke the callback to
+ * do some processing and return the results.
+ *
+ * @param [IN] argv NULL-terminated arguments list.
+ * @param [IN] output_cb Thread callback function that receives the output.
+ * @param [IN] data_size Size of the per-program information that will be returned.
+ * @param [OUT] count Size of the returned array.
+ * @return Array of information or NULL if there are none. The first member of
+ * each element (char *extcap_path) must be freed.
+ */
+static gpointer
+extcap_run_all(const char *argv[], extcap_run_cb_t output_cb, gsize data_size, guint *count)
+{
+    /* Need enough space for at least 'extcap_path'. */
+    g_assert(data_size >= sizeof(char *));
+
+    GSList *paths = extcap_get_extcap_paths();
+    int i = 0;
+#if GLIB_CHECK_VERSION(2,36,0)
+    int max_threads = (int)g_get_num_processors();
 #else
-            argv[0] = g_strdup(extcap_path->str);
+    // If the number of processors is unavailable, just use some sane maximum.
+    // extcap should not be CPU bound, so -1 could also be used for unlimited.
+    int max_threads = 8;
 #endif
-            for (i = 0; i < argc; ++i)
-                argv[i+1] = args[i];
-            argv[argc+1] = NULL;
 
-            status = g_spawn_sync(dirname, argv, envp,
-                (GSpawnFlags) 0, NULL, NULL,
-                    &command_output, NULL, &exit_status, NULL);
+    if (!paths) {
+        *count = 0;
+        return NULL;
+    }
 
-            if (status && exit_status == 0)
-            keep_going = cb(extcap_path->str, ifname, command_output, cb_data, err_str);
+    guint64 start_time = g_get_monotonic_time();
+    guint paths_count = g_slist_length(paths);
+    /* GSList is not thread-safe, so pre-allocate an array instead. */
+    gpointer infos = g_malloc0_n(paths_count, data_size);
 
-            g_free(argv[0]);
-            g_free(command_output);
-        }
+    thread_pool_t pool;
+    pool.pool = g_thread_pool_new(extcap_thread_callback, &pool, max_threads, FALSE, NULL);
+    pool.count = 0;
+    g_cond_init(&pool.cond);
+    g_mutex_init(&pool.data_mutex);
 
-        g_dir_close(dir);
-        g_string_free(extcap_path, TRUE);
+    for (GSList *path = paths; path; path = g_slist_next(path), i++) {
+        extcap_run_task_t *task = g_new0(extcap_run_task_t, 1);
+
+        task->extcap_path = (char *)path->data;
+        task->argv = g_strdupv((char **)argv);
+        task->output_cb = output_cb;
+        task->data = ((char *)infos) + (i * data_size);
+        *((char **)task->data) = (char *)path->data;
+
+        thread_pool_push(&pool, task, NULL);
     }
+    g_slist_free(paths);    /* Note: the contents are transferred to 'infos'. */
 
-#ifdef _WIN32
-    g_strfreev(dll_search_envp);
-#endif
-    g_free(argv);
+    /* Wait for all (sub)tasks to complete. */
+    thread_pool_wait(&pool);
+    g_thread_pool_free(pool.pool, FALSE, TRUE);
+
+    g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "extcap: completed discovery of %d tools in %.3fms",
+            paths_count, (g_get_monotonic_time() - start_time) / 1000.0);
+    *count = paths_count;
+    return infos;
+}
+
+static void extcap_free_dlt(gpointer d, gpointer user_data _U_)
+{
+    if (d == NULL)
+    {
+        return;
+    }
+
+    g_free(((extcap_dlt *)d)->name);
+    g_free(((extcap_dlt *)d)->display);
+    g_free(d);
 }
 
-static gboolean dlt_cb(const gchar *extcap _U_, const gchar *ifname _U_, gchar *output, void *data,
-        char **err_str) {
-    extcap_token_sentence *tokens;
-    extcap_dlt *dlts, *dlt_iter, *next;
+static void extcap_free_dlts(GList *dlts)
+{
+    g_list_foreach(dlts, extcap_free_dlt, NULL);
+    g_list_free(dlts);
+}
+
+static gboolean cb_dlt(extcap_callback_info_t cb_info)
+{
+    GList *dlts = NULL, *temp = NULL;
+
     if_capabilities_t *caps;
     GList *linktype_list = NULL;
     data_link_info_t *data_link_info;
+    extcap_dlt *dlt_item;
 
-    tokens = extcap_tokenize_sentences(output);
-    extcap_parse_dlts(tokens, &dlts);
-
-    extcap_free_tokenized_sentence_list(tokens);
+    dlts = extcap_parse_dlts(cb_info.output);
+    temp = dlts;
 
-    g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap pipe %s ", extcap);
+    g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap pipe %s ", cb_info.extcap);
 
     /*
      * Allocate the interface capabilities structure.
      */
-    caps = (if_capabilities_t *) g_malloc(sizeof *caps);
+    caps = (if_capabilities_t *) g_malloc(sizeof * caps);
     caps->can_set_rfmon = FALSE;
+    caps->timestamp_types = NULL;
 
-    dlt_iter = dlts;
-    while (dlt_iter != NULL ) {
-        g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
-                "  DLT %d name=\"%s\" display=\"%s\" ", dlt_iter->number,
-                dlt_iter->name, dlt_iter->display);
+    while (dlts)
+    {
+        dlt_item = (extcap_dlt *)dlts->data;
+        if (dlt_item)
+        {
+            g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
+                  "  DLT %d name=\"%s\" display=\"%s\" ", dlt_item->number,
+                  dlt_item->name, dlt_item->display);
+
+            data_link_info = g_new(data_link_info_t, 1);
+            data_link_info->dlt = dlt_item->number;
+            data_link_info->name = g_strdup(dlt_item->name);
+            data_link_info->description = g_strdup(dlt_item->display);
+            linktype_list = g_list_append(linktype_list, data_link_info);
+        }
 
-        data_link_info = g_new(data_link_info_t, 1);
-        data_link_info->dlt = dlt_iter->number;
-        data_link_info->name = g_strdup(dlt_iter->name);
-        data_link_info->description = g_strdup(dlt_iter->display);
-        linktype_list = g_list_append(linktype_list, data_link_info);
-        dlt_iter = dlt_iter->next_dlt;
+        dlts = g_list_next(dlts);
     }
 
     /* Check to see if we built a list */
-    if (linktype_list != NULL && data != NULL) {
+    if (linktype_list != NULL && cb_info.data != NULL)
+    {
         caps->data_link_types = linktype_list;
-        *(if_capabilities_t **) data = caps;
-    } else {
-        if (err_str) {
+        *(if_capabilities_t **) cb_info.data = caps;
+    }
+    else
+    {
+        if (cb_info.err_str)
+        {
             g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "  returned no DLTs");
-            *err_str = g_strdup("Extcap returned no DLTs");
+            *(cb_info.err_str) = g_strdup("Extcap returned no DLTs");
         }
         g_free(caps);
     }
 
-    dlt_iter = dlts;
-    while (dlt_iter != NULL ) {
-        next = dlt_iter->next_dlt;
-        extcap_free_dlt(dlt_iter);
-        dlt_iter = next;
-    }
+    extcap_free_dlts(temp);
 
     return FALSE;
 }
 
 if_capabilities_t *
-extcap_get_if_dlts(const gchar *ifname, char **err_str) {
-    gchar *argv[3];
-    gint i;
+extcap_get_if_dlts(const gchar *ifname, char **err_str)
+{
+    GList * arguments = NULL;
     if_capabilities_t *caps = NULL;
 
     if (err_str != NULL)
+    {
         *err_str = NULL;
+    }
 
-    if ( extcap_if_exists(ifname) )
+    extcap_interface *interface = extcap_find_interface_for_ifname(ifname);
+    if (interface)
     {
-        argv[0] = g_strdup(EXTCAP_ARGUMENT_LIST_DLTS);
-        argv[1] = g_strdup(EXTCAP_ARGUMENT_INTERFACE);
-        argv[2] = g_strdup(ifname);
+        arguments = g_list_append(arguments, g_strdup(EXTCAP_ARGUMENT_LIST_DLTS));
+        arguments = g_list_append(arguments, g_strdup(EXTCAP_ARGUMENT_INTERFACE));
+        arguments = g_list_append(arguments, g_strdup(ifname));
 
-        extcap_foreach(3, argv, dlt_cb, &caps, err_str, ifname);
+        extcap_run_one(interface, arguments, cb_dlt, &caps, err_str);
 
-        for (i = 0; i < 3; ++i)
-            g_free(argv[i]);
+        g_list_free_full(arguments, g_free);
     }
 
     return caps;
 }
 
-static gboolean interfaces_cb(const gchar *extcap, const gchar *ifname _U_, gchar *output, void *data,
-        char **err_str _U_) {
-    GList **il = (GList **) data;
-    extcap_token_sentence *tokens;
-    extcap_interface *interfaces, *int_iter; /*, *next; */
-    if_info_t *if_info;
-
-    tokens = extcap_tokenize_sentences(output);
-    extcap_parse_interfaces(tokens, &interfaces);
-
-    extcap_free_tokenized_sentence_list(tokens);
-
-    g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap pipe %s ", extcap);
-
-    int_iter = interfaces;
-    while (int_iter != NULL ) {
-        if ( int_iter->if_type == EXTCAP_SENTENCE_INTERFACE && extcap_if_exists(int_iter->call) )
-        {
-            g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_WARNING, "Extcap interface \"%s\" is already provided by \"%s\" ",
-                    int_iter->call, (gchar *)extcap_if_executable(int_iter->call) );
-            int_iter = int_iter->next_interface;
-            continue;
-        }
-
-        if ( int_iter->if_type == EXTCAP_SENTENCE_INTERFACE )
-            g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "  Interface [%s] \"%s\" ",
-                    int_iter->call, int_iter->display);
-        else if ( int_iter->if_type == EXTCAP_SENTENCE_EXTCAP )
-            g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "  Extcap [%s] ", int_iter->call);
-
-        if ( int_iter->if_type == EXTCAP_SENTENCE_INTERFACE ) {
-            if (il != NULL) {
-                if_info = g_new0(if_info_t, 1);
-                if_info->name = g_strdup(int_iter->call);
-                if_info->friendly_name = g_strdup(int_iter->display);
-
-                if_info->type = IF_EXTCAP;
+static void extcap_free_interface(gpointer i)
+{
 
-                if_info->extcap = g_strdup(extcap);
-                *il = g_list_append(*il, if_info);
-            }
+    extcap_interface *interface = (extcap_interface *)i;
 
-            extcap_if_add(int_iter->call, extcap);
-        }
+    if (i == NULL)
+    {
+        return;
+    }
 
-        /* Call for interfaces and tools alike. Multiple calls (because a tool has multiple
-         * interfaces) are handled internally */
-        extcap_tool_add(extcap, int_iter);
+    g_free(interface->call);
+    g_free(interface->display);
+    g_free(interface->version);
+    g_free(interface->help);
+    g_free(interface->extcap_path);
+    g_free(interface);
+}
 
-        int_iter = int_iter->next_interface;
+static void extcap_free_interfaces(GList *interfaces)
+{
+    if (interfaces == NULL)
+    {
+        return;
     }
-    extcap_free_interface(interfaces);
 
-    return TRUE;
+    g_list_free_full(interfaces, extcap_free_interface);
 }
 
 static gint
 if_info_compare(gconstpointer a, gconstpointer b)
 {
     gint comp = 0;
-    if_info_t * if_a = (if_info_t *)a;
-    if_info_t * if_b = (if_info_t *)b;
+    const if_info_t *if_a = (const if_info_t *)a;
+    const if_info_t *if_b = (const if_info_t *)b;
 
-    if ( (comp = g_strcmp0(if_a->name, if_b->name)) == 0 )
+    if ((comp = g_strcmp0(if_a->name, if_b->name)) == 0)
+    {
         return g_strcmp0(if_a->friendly_name, if_b->friendly_name);
+    }
 
     return comp;
 }
 
-GHashTable *
-extcap_tools_list(void) {
-    if ( tools == NULL || g_hash_table_size(tools) == 0 )
-        extcap_interface_list(NULL, NULL);
-
-    return tools;
+gchar *
+extcap_get_help_for_ifname(const char *ifname)
+{
+    extcap_interface *interface = extcap_find_interface_for_ifname(ifname);
+    return interface != NULL ? interface->help : NULL;
 }
 
-static void
-extcap_free_info (gpointer data) {
-    extcap_info * info = (extcap_info *)data;
+GList *
+append_extcap_interface_list(GList *list, char **err_str _U_)
+{
+    GList *interface_list = NULL;
+    extcap_interface *data = NULL;
+    GList *ifutilkeys_head = NULL, *ifutilkeys = NULL;
 
-    g_free (info->basename);
-    g_free (info->full_path);
-    g_free (info->version);
-    g_free (info);
-}
+    if (prefs.capture_no_extcap)
+        return list;
 
-void
-extcap_interface_list(GList **listp, char **err_str) {
-    gchar *argv;
-    /* gint i; */
-    GList *ret = NULL;
-    GList *entry;
-    void *data;
+    /* Update the extcap interfaces and get a list of their if_infos */
+    if ( !_loaded_interfaces || g_hash_table_size(_loaded_interfaces) == 0 )
+        extcap_load_interface_list();
 
-    if (err_str != NULL)
-        *err_str = NULL;
+    ifutilkeys_head = g_hash_table_get_keys(_loaded_interfaces);
+    ifutilkeys = ifutilkeys_head;
+    while ( ifutilkeys && ifutilkeys->data )
+    {
+        extcap_info * extinfo =
+                (extcap_info *) g_hash_table_lookup(_loaded_interfaces, (gchar *)ifutilkeys->data);
+        GList * walker = extinfo->interfaces;
+        while ( walker && walker->data )
+        {
+            interface_list = g_list_append(interface_list, walker->data);
+            walker = g_list_next(walker);
+        }
 
-    /* ifaces is used as cache, do not destroy its contents when
-     * returning or no extcap interfaces can be queried for options */
-    if (ifaces == NULL)
-        ifaces = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
-    else
-        g_hash_table_remove_all(ifaces);
+        ifutilkeys = g_list_next(ifutilkeys);
+    }
+    g_list_free(ifutilkeys_head);
 
-    if (tools == NULL)
-        tools = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, extcap_free_info);
-    else
-        g_hash_table_remove_all(tools);
+    /* Sort that list */
+    interface_list = g_list_sort(interface_list, if_info_compare);
 
-    argv = g_strdup(EXTCAP_ARGUMENT_LIST_INTERFACES);
+    /* Append the interfaces in that list to the list we're handed. */
+    while (interface_list != NULL)
+    {
+        GList *entry = g_list_first(interface_list);
+        data = (extcap_interface *)entry->data;
+        interface_list = g_list_delete_link(interface_list, entry);
 
-    extcap_foreach(1, &argv, interfaces_cb, (listp != NULL) ? &ret : NULL, err_str, NULL);
+        if_info_t * if_info = g_new0(if_info_t, 1);
+        if_info->name = g_strdup(data->call);
+        if_info->friendly_name = g_strdup(data->display);
 
-    g_free(argv);
+        if_info->type = IF_EXTCAP;
 
-    if (listp != NULL) {
-        /* Sort the list */
-        ret = g_list_sort(ret, if_info_compare);
+        if_info->extcap = g_strdup(data->extcap_path);
 
-        /* Append the interfaces to the list. */
-        while (ret != NULL) {
-            entry = g_list_first(ret);
-            data = entry->data;
-            ret = g_list_delete_link(ret, entry);
-            *listp = g_list_append(*listp, data);
-        }
+        list = g_list_append(list, if_info);
     }
-}
 
-static void extcap_free_arg_elem(gpointer data, gpointer user_data _U_) {
-    extcap_free_arg((extcap_arg *) data);
-    g_free(data);
+    return list;
 }
 
 void extcap_register_preferences(void)
 {
-    GList * interfaces = NULL;
+    if (prefs.capture_no_extcap)
+        return;
 
-    module_t * dev_module = prefs_find_module("extcap");
+    module_t *dev_module = prefs_find_module("extcap");
 
-    if ( !dev_module )
+    if (!dev_module)
+    {
         return;
+    }
+
+    // Will load information about extcaps and their supported config.
+    extcap_load_interface_list();
+}
 
-    if ( ! ifaces || g_hash_table_size(ifaces) == 0 )
-        extcap_interface_list(NULL, NULL);
+/**
+ * Releases the dynamic preference value pointers. Must not be called before
+ * prefs_cleanup since these pointers could still be in use.
+ */
+void extcap_cleanup(void)
+{
+    if (extcap_prefs_dynamic_vals)
+        g_hash_table_destroy(extcap_prefs_dynamic_vals);
 
-    interfaces = g_hash_table_get_keys(ifaces);
+    if (_loaded_interfaces)
+        g_hash_table_destroy(_loaded_interfaces);
 
-    while ( interfaces ) {
-        extcap_get_if_configuration((gchar *)interfaces->data);
+    if (_tool_for_ifname)
+        g_hash_table_destroy(_tool_for_ifname);
+}
 
-        interfaces = g_list_next(interfaces);
+/**
+ * Obtains a pointer which can store a value for the given preference name.
+ * The preference name that can be passed to the prefs API is stored into
+ * 'prefs_name'.
+ *
+ * Extcap interfaces (and their preferences) are dynamic, they can be created
+ * and destroyed at will. Thus their data structures are insufficient to pass to
+ * the preferences APIs which require pointers which are valid until the
+ * preferences are removed (at exit).
+ */
+static gchar **extcap_prefs_dynamic_valptr(const char *name, char **pref_name)
+{
+    gchar **valp;
+    if (!extcap_prefs_dynamic_vals)
+    {
+        /* Initialize table only as needed, most preferences are not dynamic */
+        extcap_prefs_dynamic_vals = g_hash_table_new_full(g_str_hash, g_str_equal,
+                                    g_free, g_free);
+    }
+    if (!g_hash_table_lookup_extended(extcap_prefs_dynamic_vals, name,
+                                      (gpointer *)pref_name, (gpointer *)&valp))
+    {
+        /* New dynamic pref, allocate, initialize and store. */
+        valp = g_new0(gchar *, 1);
+        *pref_name = g_strdup(name);
+        g_hash_table_insert(extcap_prefs_dynamic_vals, *pref_name, valp);
     }
+    return valp;
 }
 
-static void extcap_free_if_configuration(GList *list)
+void extcap_free_if_configuration(GList *list, gboolean free_args)
 {
     GList *elem, *sl;
 
     for (elem = g_list_first(list); elem; elem = elem->next)
     {
-        if (elem->data != NULL) {
-            /* g_list_free_full() only exists since 2.28. */
+        if (elem->data != NULL)
+        {
             sl = g_list_first((GList *)elem->data);
-            g_list_foreach(sl, (GFunc)extcap_free_arg_elem, NULL);
-            g_list_free(sl);
+            if (free_args)
+            {
+                extcap_free_arg_list(sl);
+            }
+            else
+            {
+                g_list_free(sl);
+            }
         }
     }
     g_list_free(list);
 }
 
-gchar * extcap_settings_key(const gchar * ifname, const gchar * setting)
+struct preference *
+extcap_pref_for_argument(const gchar *ifname, struct _extcap_arg *arg)
 {
-    gchar * setting_nohyphen;
-    gchar * ifname_underscore;
-    gchar * ifname_lower;
-    gchar * key;
-    GRegex * regex = g_regex_new ("(?![a-zA-Z1-9_]).", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, NULL );
+    struct preference *pref = NULL;
 
-    if (!regex)
-        return NULL;
+    GRegex *regex_name = g_regex_new("[-]+", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, NULL);
+    GRegex *regex_ifname = g_regex_new("(?![a-zA-Z0-9_]).", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, NULL);
+    if (regex_name && regex_ifname)
+    {
+        if (prefs_find_module("extcap"))
+        {
+            gchar *pref_name = g_regex_replace(regex_name, arg->call, strlen(arg->call), 0, "", (GRegexMatchFlags) 0, NULL);
+            gchar *ifname_underscore = g_regex_replace(regex_ifname, ifname, strlen(ifname), 0, "_", (GRegexMatchFlags) 0, NULL);
+            gchar *ifname_lowercase = g_ascii_strdown(ifname_underscore, -1);
+            gchar *pref_ifname = g_strconcat(ifname_lowercase, ".", pref_name, NULL);
 
-    setting_nohyphen =
-        g_regex_replace_literal(regex, setting, strlen(setting), 0,
-            "", (GRegexMatchFlags) 0, NULL );
-    ifname_underscore =
-        g_regex_replace_literal(regex, ifname, strlen(ifname), 0,
-            "_", (GRegexMatchFlags) 0, NULL );
-    ifname_lower = g_utf8_strdown(ifname_underscore, -1);
-    key = g_strconcat(ifname_lower, ".", setting_nohyphen, NULL);
-
-    g_free(setting_nohyphen);
-    g_free(ifname_underscore);
-    g_free(ifname_lower);
-    g_regex_unref(regex);
-
-    return key;
-}
+            pref = prefs_find_preference(prefs_find_module("extcap"), pref_ifname);
 
-static gboolean search_cb(const gchar *extcap _U_, const gchar *ifname _U_, gchar *output, void *data,
-        char **err_str _U_) {
-    extcap_token_sentence *tokens = NULL;
-    GList *arguments = NULL;
-    GList **il = (GList **) data;
-    module_t * dev_module = NULL;
+            g_free(pref_name);
+            g_free(ifname_underscore);
+            g_free(ifname_lowercase);
+            g_free(pref_ifname);
+        }
+    }
+    if (regex_name)
+    {
+        g_regex_unref(regex_name);
+    }
+    if (regex_ifname)
+    {
+        g_regex_unref(regex_ifname);
+    }
 
-    tokens = extcap_tokenize_sentences(output);
-    arguments = extcap_parse_args(tokens);
+    return pref;
+}
 
-    extcap_free_tokenized_sentence_list(tokens);
+static gboolean cb_preference(extcap_callback_info_t cb_info)
+{
+    GList *arguments = NULL;
+    GList **il = (GList **) cb_info.data;
+    module_t *dev_module = NULL;
 
-#if ARG_DEBUG
-    extcap_debug_arguments ( arguments );
-#endif
+    arguments = extcap_parse_args(cb_info.output);
 
     dev_module = prefs_find_module("extcap");
 
-    if ( dev_module ) {
-        GList * walker = arguments;
+    if (dev_module)
+    {
+        GList *walker = arguments;
+
+        GRegex *regex_name = g_regex_new("[-]+", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, NULL);
+        GRegex *regex_ifname = g_regex_new("(?![a-zA-Z0-9_]).", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, NULL);
+        if (regex_name && regex_ifname)
+        {
+            while (walker != NULL)
+            {
+                extcap_arg *arg = (extcap_arg *)walker->data;
+                arg->device_name = g_strdup(cb_info.ifname);
+
+                if (arg->save)
+                {
+                    struct preference *pref = NULL;
 
-        while ( walker != NULL ) {
-            extcap_arg * arg = (extcap_arg *)walker->data;
+                    gchar *pref_name = g_regex_replace(regex_name, arg->call, strlen(arg->call), 0, "", (GRegexMatchFlags) 0, NULL);
+                    gchar *ifname_underscore = g_regex_replace(regex_ifname, cb_info.ifname, strlen(cb_info.ifname), 0, "_", (GRegexMatchFlags) 0, NULL);
+                    gchar *ifname_lowercase = g_ascii_strdown(ifname_underscore, -1);
+                    gchar *pref_ifname = g_strconcat(ifname_lowercase, ".", pref_name, NULL);
 
-            if ( arg->save ) {
-                struct preference * pref = NULL;
-                gchar * pref_ifname = extcap_settings_key(ifname, arg->call);
+                    if ((pref = prefs_find_preference(dev_module, pref_ifname)) == NULL)
+                    {
+                        char *pref_name_for_prefs;
+                        char *pref_title = wmem_strdup(wmem_epan_scope(), arg->display);
 
-                if ( ( pref = prefs_find_preference(dev_module, pref_ifname) ) == NULL ) {
-                    /* Set an initial value */
-                    if ( ! arg->storeval && arg->default_complex )
-                        arg->storeval = g_strdup(arg->default_complex->_val);
+                        arg->pref_valptr = extcap_prefs_dynamic_valptr(pref_ifname, &pref_name_for_prefs);
+                        /* Set an initial value if any (the string will be copied at registration) */
+                        if (arg->default_complex)
+                        {
+                            *arg->pref_valptr = arg->default_complex->_val;
+                        }
 
-                    prefs_register_string_preference(dev_module, g_strdup(pref_ifname),
-                            arg->display, arg->display, (const gchar **)&(arg->storeval));
-                } else {
-                    /* Been here before, restore stored value */
-                    if (! arg->storeval && pref->varp.string)
-                        arg->storeval = g_strdup(*(pref->varp.string));
+                        prefs_register_string_preference(dev_module, pref_name_for_prefs,
+                                                         pref_title, pref_title, (const char **)arg->pref_valptr);
+                    }
+                    else
+                    {
+                        /* Been here before, restore stored value */
+                        if (arg->pref_valptr == NULL)
+                        {
+                            arg->pref_valptr = (gchar**)g_hash_table_lookup(extcap_prefs_dynamic_vals, pref_ifname);
+                        }
                     }
-                g_free(pref_ifname);
-            }
 
-            walker = g_list_next(walker);
+                    g_free(pref_name);
+                    g_free(ifname_underscore);
+                    g_free(ifname_lowercase);
+                    g_free(pref_ifname);
+                }
+
+                walker = g_list_next(walker);
+            }
+        }
+        if (regex_name)
+        {
+            g_regex_unref(regex_name);
+        }
+        if (regex_ifname)
+        {
+            g_regex_unref(regex_ifname);
         }
     }
 
@@ -554,70 +883,153 @@ static gboolean search_cb(const gchar *extcap _U_, const gchar *ifname _U_, gcha
 }
 
 GList *
-extcap_get_if_configuration(const char * ifname) {
-    gchar *argv[3];
+extcap_get_if_configuration(const char *ifname)
+{
+    GList * arguments = NULL;
     GList *ret = NULL;
-    gchar **err_str = NULL;
-    int i;
 
-    if ( extcap_if_exists(ifname) )
+    extcap_interface *interface = extcap_find_interface_for_ifname(ifname);
+    if (interface)
     {
         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap path %s",
-                get_extcap_dir());
+              get_extcap_dir());
 
-        argv[0] = g_strdup(EXTCAP_ARGUMENT_CONFIG);
-        argv[1] = g_strdup(EXTCAP_ARGUMENT_INTERFACE);
-        argv[2] = g_strdup(ifname);
+        arguments = g_list_append(arguments, g_strdup(EXTCAP_ARGUMENT_CONFIG));
+        arguments = g_list_append(arguments, g_strdup(EXTCAP_ARGUMENT_INTERFACE));
+        arguments = g_list_append(arguments, g_strdup(ifname));
 
-        extcap_foreach(3, argv, search_cb, &ret, err_str, ifname);
+        extcap_run_one(interface, arguments, cb_preference, &ret, NULL);
 
-        for (i = 0; i < 3; i++)
-            g_free(argv[i]);
+        g_list_free_full(arguments, g_free);
     }
 
     return ret;
 }
 
+static gboolean cb_reload_preference(extcap_callback_info_t cb_info)
+{
+    GList *arguments = NULL, * walker = NULL;
+    GList **il = (GList **) cb_info.data;
+
+    arguments = extcap_parse_values(cb_info.output);
+
+    walker = g_list_first(arguments);
+    while (walker != NULL)
+    {
+        extcap_value * val = (extcap_value *)walker->data;
+        *il = g_list_append(*il, val);
+        walker = g_list_next(walker);
+    }
+    g_list_free(arguments);
+
+    /* By returning false, extcap_foreach will break on first found */
+    return FALSE;
+}
+
+GList *
+extcap_get_if_configuration_values(const char * ifname, const char * argname, GHashTable *arguments)
+{
+    GList * args = NULL;
+    GList *ret = NULL;
+
+    extcap_interface *interface = extcap_find_interface_for_ifname(ifname);
+    if (interface)
+    {
+        g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap path %s",
+              get_extcap_dir());
+
+        args = g_list_append(args, g_strdup(EXTCAP_ARGUMENT_CONFIG));
+        args = g_list_append(args, g_strdup(EXTCAP_ARGUMENT_INTERFACE));
+        args = g_list_append(args, g_strdup(ifname));
+        args = g_list_append(args, g_strdup(EXTCAP_ARGUMENT_RELOAD_OPTION));
+        args = g_list_append(args, g_strdup(argname));
+
+        if ( arguments )
+        {
+            GList * keys = g_hash_table_get_keys(arguments);
+            while ( keys )
+            {
+                const gchar * key_data = (const gchar *)keys->data;
+                args = g_list_append(args, g_strdup(key_data));
+                args = g_list_append(args, g_strdup((const gchar *)g_hash_table_lookup(arguments, key_data)));
+                keys = g_list_next(keys);
+            }
+        }
+
+        extcap_run_one(interface, args, cb_reload_preference, &ret, NULL);
+
+        g_list_free_full(args, g_free);
+    }
+
+    return ret;
+}
+
+/**
+ * If is_required is FALSE: returns TRUE if the extcap interface has
+ * configurable options.
+ * If is_required is TRUE: returns TRUE when the extcap interface has
+ * configurable options that required modification. (For example, when an
+ * argument is required but empty.)
+ */
 gboolean
-extcap_has_configuration(const char * ifname, gboolean is_required) {
-    GList * arguments = 0;
-    GList * walker = 0, * item = 0;
+extcap_has_configuration(const char *ifname, gboolean is_required)
+{
+    GList *arguments = 0;
+    GList *walker = 0, * item = 0;
 
     gboolean found = FALSE;
 
-    arguments = extcap_get_if_configuration((const char *)( ifname ) );
+    arguments = extcap_get_if_configuration(ifname);
     walker = g_list_first(arguments);
 
-    while ( walker != NULL && ! found ) {
+    while (walker != NULL && !found)
+    {
         item = g_list_first((GList *)(walker->data));
-        while ( item != NULL && ! found ) {
-            if ( (extcap_arg *)(item->data) != NULL ) {
-                extcap_arg * arg = (extcap_arg *)(item->data);
+        while (item != NULL && !found)
+        {
+            if ((extcap_arg *)(item->data) != NULL)
+            {
+                extcap_arg *arg = (extcap_arg *)(item->data);
                 /* Should required options be present, or any kind of options */
-                if ( ! is_required )
+                if (!is_required)
+                {
                     found = TRUE;
-                else if ( arg->is_required ) {
-                    gchar * stored = NULL;
-                    gchar * defval = NULL;
+                }
+                else if (arg->is_required)
+                {
+                    const gchar *stored = NULL;
+                    const gchar *defval = NULL;
 
-                    if ( arg->storeval != NULL )
-                        stored = arg->storeval;
+                    if (arg->pref_valptr != NULL)
+                    {
+                        stored = *arg->pref_valptr;
+                    }
 
-                    if ( arg->default_complex != NULL && arg->default_complex->_val != NULL )
+                    if (arg->default_complex != NULL && arg->default_complex->_val != NULL)
+                    {
                         defval = arg->default_complex->_val;
+                    }
 
-                    if ( arg->is_required ) {
+                    if (arg->is_required)
+                    {
                         /* If stored and defval is identical and the argument is required,
                          * configuration is needed */
-                        if ( defval && stored && g_strcmp0(stored, defval) == 0 )
+                        if (defval && stored && g_strcmp0(stored, defval) == 0)
+                        {
                             found = TRUE;
-                        else if ( ! defval && (!stored || strlen(g_strchomp(stored)) <= (size_t)0) )
+                        }
+                        else if (!defval && (!stored || !*stored))
+                        {
                             found = TRUE;
+                        }
                     }
 
-                    if ( arg->arg_type == EXTCAP_ARG_FILESELECT ) {
-                        if ( arg->fileexists && ! ( file_exists(defval) || file_exists(stored) ) )
+                    if (arg->arg_type == EXTCAP_ARG_FILESELECT)
+                    {
+                        if (arg->fileexists && !(file_exists(defval) || file_exists(stored)))
+                        {
                             found = TRUE;
+                        }
                     }
                 }
             }
@@ -626,74 +1038,250 @@ extcap_has_configuration(const char * ifname, gboolean is_required) {
         }
         walker = walker->next;
     }
-    extcap_free_if_configuration(arguments);
+    extcap_free_if_configuration(arguments, TRUE);
 
     return found;
 }
 
-void extcap_cleanup(capture_options * capture_opts) {
-    interface_options interface_opts;
+static gboolean cb_verify_filter(extcap_callback_info_t cb_info)
+{
+    extcap_filter_status *status = (extcap_filter_status *)cb_info.data;
+    size_t output_size, i;
+
+    output_size = strlen(cb_info.output);
+    if (output_size == 0) {
+        *status = EXTCAP_FILTER_VALID;
+    } else {
+        *status = EXTCAP_FILTER_INVALID;
+        for (i = 0; i < output_size; i++) {
+            if (cb_info.output[i] == '\n' || cb_info.output[i] == '\r') {
+                cb_info.output[i] = '\0';
+                break;
+            }
+        }
+        *cb_info.err_str = g_strdup(cb_info.output);
+    }
+
+    return TRUE;
+}
+
+extcap_filter_status
+extcap_verify_capture_filter(const char *ifname, const char *filter, gchar **err_str)
+{
+    GList * arguments = NULL;
+    extcap_filter_status status = EXTCAP_FILTER_UNKNOWN;
+
+    extcap_interface *interface = extcap_find_interface_for_ifname(ifname);
+    if (interface)
+    {
+        g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap path %s",
+              get_extcap_dir());
+
+        arguments = g_list_append(arguments, g_strdup(EXTCAP_ARGUMENT_CAPTURE_FILTER));
+        arguments = g_list_append(arguments, g_strdup(filter));
+        arguments = g_list_append(arguments, g_strdup(EXTCAP_ARGUMENT_INTERFACE));
+        arguments = g_list_append(arguments, g_strdup(ifname));
+
+        extcap_run_one(interface, arguments, cb_verify_filter, &status, err_str);
+        g_list_free_full(arguments, g_free);
+    }
+
+    return status;
+}
+
+gboolean
+extcap_has_toolbar(const char *ifname)
+{
+    if (!iface_toolbar_use())
+    {
+        return FALSE;
+    }
+
+    GList *toolbar_list = g_hash_table_get_values (_toolbars);
+    for (GList *walker = toolbar_list; walker; walker = walker->next)
+    {
+        iface_toolbar *toolbar = (iface_toolbar *) walker->data;
+        if (g_list_find_custom(toolbar->ifnames, ifname, (GCompareFunc) strcmp))
+        {
+            return TRUE;
+        }
+    }
+
+    return FALSE;
+}
+
+void extcap_if_cleanup(capture_options *capture_opts, gchar **errormsg)
+{
+    interface_options *interface_opts;
+    ws_pipe_t *pipedata;
     guint icnt = 0;
+    gboolean overwrite_exitcode;
+    gchar *buffer;
+#define STDERR_BUFFER_SIZE 1024
 
-    for (icnt = 0; icnt < capture_opts->ifaces->len; icnt++) {
-        interface_opts = g_array_index(capture_opts->ifaces, interface_options,
-                icnt);
+    for (icnt = 0; icnt < capture_opts->ifaces->len; icnt++)
+    {
+        interface_opts = &g_array_index(capture_opts->ifaces, interface_options,
+                                       icnt);
 
         /* skip native interfaces */
-        if (interface_opts.if_type != IF_EXTCAP)
-        continue;
+        if (interface_opts->if_type != IF_EXTCAP)
+        {
+            continue;
+        }
+
+        overwrite_exitcode = FALSE;
 
         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
-                "Extcap [%s] - Cleaning up fifo: %s; PID: %d", interface_opts.name,
-                interface_opts.extcap_fifo, interface_opts.extcap_pid);
+              "Extcap [%s] - Cleaning up fifo: %s; PID: %d", interface_opts->name,
+              interface_opts->extcap_fifo, interface_opts->extcap_pid);
 #ifdef _WIN32
-        if (pipe_h)
+        if (interface_opts->extcap_pipe_h != INVALID_HANDLE_VALUE)
         {
             g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
-                "Extcap [%s] - Closing pipe", interface_opts.name);
-            FlushFileBuffers(pipe_h);
-            DisconnectNamedPipe(pipe_h);
-            CloseHandle(pipe_h);
+                  "Extcap [%s] - Closing pipe", interface_opts->name);
+            FlushFileBuffers(interface_opts->extcap_pipe_h);
+            DisconnectNamedPipe(interface_opts->extcap_pipe_h);
+            CloseHandle(interface_opts->extcap_pipe_h);
+            interface_opts->extcap_pipe_h = INVALID_HANDLE_VALUE;
+        }
+        if (interface_opts->extcap_control_in_h != INVALID_HANDLE_VALUE)
+        {
+            g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
+                  "Extcap [%s] - Closing control_in pipe", interface_opts->name);
+            FlushFileBuffers(interface_opts->extcap_control_in_h);
+            DisconnectNamedPipe(interface_opts->extcap_control_in_h);
+            CloseHandle(interface_opts->extcap_control_in_h);
+            interface_opts->extcap_control_in_h = INVALID_HANDLE_VALUE;
+        }
+        if (interface_opts->extcap_control_out_h != INVALID_HANDLE_VALUE)
+        {
+            g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
+                  "Extcap [%s] - Closing control_out pipe", interface_opts->name);
+            FlushFileBuffers(interface_opts->extcap_control_out_h);
+            DisconnectNamedPipe(interface_opts->extcap_control_out_h);
+            CloseHandle(interface_opts->extcap_control_out_h);
+            interface_opts->extcap_control_out_h = INVALID_HANDLE_VALUE;
         }
 #else
-        if (interface_opts.extcap_fifo != NULL && file_exists(interface_opts.extcap_fifo))
+        if (interface_opts->extcap_fifo != NULL && file_exists(interface_opts->extcap_fifo))
         {
             /* the fifo will not be freed here, but with the other capture_opts in capture_sync */
-            ws_unlink(interface_opts.extcap_fifo);
-            interface_opts.extcap_fifo = NULL;
+            ws_unlink(interface_opts->extcap_fifo);
+            interface_opts->extcap_fifo = NULL;
+        }
+        if (interface_opts->extcap_control_in && file_exists(interface_opts->extcap_control_in))
+        {
+            ws_unlink(interface_opts->extcap_control_in);
+            interface_opts->extcap_control_in = NULL;
+        }
+        if (interface_opts->extcap_control_out && file_exists(interface_opts->extcap_control_out))
+        {
+            ws_unlink(interface_opts->extcap_control_out);
+            interface_opts->extcap_control_out = NULL;
         }
 #endif
         /* Maybe the client closed and removed fifo, but ws should check if
          * pid should be closed */
         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
-                "Extcap [%s] - Closing spawned PID: %d", interface_opts.name,
-                interface_opts.extcap_pid);
+              "Extcap [%s] - Closing spawned PID: %d", interface_opts->name,
+              interface_opts->extcap_pid);
 
-        if (interface_opts.extcap_pid != INVALID_EXTCAP_PID)
+        pipedata = (ws_pipe_t *) interface_opts->extcap_pipedata;
+        if (pipedata)
         {
-#ifdef _WIN32
-            TerminateProcess(interface_opts.extcap_pid, 0);
+            if (pipedata->stderr_fd > 0)
+            {
+                buffer = (gchar *)g_malloc0(STDERR_BUFFER_SIZE + 1);
+                ws_read_string_from_pipe(ws_get_pipe_handle(pipedata->stderr_fd), buffer, STDERR_BUFFER_SIZE + 1);
+                if (strlen(buffer) > 0)
+                {
+                    pipedata->stderr_msg = g_strdup(buffer);
+                    pipedata->exitcode = 1;
+                }
+                g_free(buffer);
+            }
+
+#ifndef _WIN32
+            /* Final child watch may not have been called */
+            if (interface_opts->extcap_child_watch > 0)
+            {
+                extcap_child_watch_cb(pipedata->pid, 0, capture_opts);
+                /* it will have changed in extcap_child_watch_cb */
+                interface_opts = &g_array_index(capture_opts->ifaces, interface_options,
+                                               icnt);
+            }
 #endif
-            g_spawn_close_pid(interface_opts.extcap_pid);
-            interface_opts.extcap_pid = INVALID_EXTCAP_PID;
+
+            if (pipedata->stderr_msg != NULL)
+            {
+                overwrite_exitcode = TRUE;
+            }
+
+            if (overwrite_exitcode || pipedata->exitcode != 0)
+            {
+                if (pipedata->stderr_msg != NULL)
+                {
+                    if (*errormsg == NULL)
+                    {
+                        *errormsg = g_strdup_printf("Error by extcap pipe: %s", pipedata->stderr_msg);
+                    }
+                    else
+                    {
+                        gchar *temp = g_strconcat(*errormsg, "\nError by extcap pipe: " , pipedata->stderr_msg, NULL);
+                        g_free(*errormsg);
+                        *errormsg = temp;
+                    }
+                    g_free(pipedata->stderr_msg);
+                }
+
+                pipedata->stderr_msg = NULL;
+                pipedata->exitcode = 0;
+            }
         }
 
-        /* Make sure modified interface_opts is saved in capture_opts. */
-        capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, icnt);
-        g_array_insert_val(capture_opts->ifaces, icnt, interface_opts);
+        if (interface_opts->extcap_child_watch > 0)
+        {
+            g_source_remove(interface_opts->extcap_child_watch);
+            interface_opts->extcap_child_watch = 0;
+        }
+
+        if (pipedata) {
+            if (pipedata->stdout_fd > 0)
+            {
+                ws_close(pipedata->stdout_fd);
+            }
+
+            if (pipedata->stderr_fd > 0)
+            {
+                ws_close(pipedata->stderr_fd);
+            }
+
+            if (interface_opts->extcap_pid != WS_INVALID_PID)
+            {
+                ws_pipe_close(pipedata);
+                interface_opts->extcap_pid = WS_INVALID_PID;
+
+                g_free(pipedata);
+                interface_opts->extcap_pipedata = NULL;
+            }
+        }
     }
 }
 
 static gboolean
-extcap_add_arg_and_remove_cb(gpointer key, gpointer value, gpointer data) {
+extcap_add_arg_and_remove_cb(gpointer key, gpointer value, gpointer data)
+{
     GPtrArray *args = (GPtrArray *)data;
 
-    if ( key != NULL )
+    if (key != NULL)
     {
-        g_ptr_array_add(args, g_strdup((const gchar*)key));
+        g_ptr_array_add(args, g_strdup((const gchar *)key));
 
-        if ( value != NULL )
-            g_ptr_array_add(args, g_strdup((const gchar*)value));
+        if (value != NULL)
+        {
+            g_ptr_array_add(args, g_strdup((const gchar *)value));
+        }
 
         return TRUE;
     }
@@ -701,11 +1289,17 @@ extcap_add_arg_and_remove_cb(gpointer key, gpointer value, gpointer data) {
     return FALSE;
 }
 
-static void extcap_child_watch_cb(GPid pid, gint status _U_, gpointer user_data)
+void extcap_child_watch_cb(GPid pid, gint status, gpointer user_data)
 {
     guint i;
-    interface_options interface_opts;
-    capture_options *capture_opts = (capture_options *)user_data;
+    interface_options *interface_opts;
+    ws_pipe_t *pipedata = NULL;
+    capture_options *capture_opts = (capture_options *)(user_data);
+
+    if (capture_opts == NULL || capture_opts->ifaces == NULL || capture_opts->ifaces->len == 0)
+    {
+        return;
+    }
 
     /* Close handle to child process. */
     g_spawn_close_pid(pid);
@@ -713,60 +1307,77 @@ static void extcap_child_watch_cb(GPid pid, gint status _U_, gpointer user_data)
     /* Update extcap_pid in interface options structure. */
     for (i = 0; i < capture_opts->ifaces->len; i++)
     {
-        interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
-        if (interface_opts.extcap_pid == pid)
+        interface_opts = &g_array_index(capture_opts->ifaces, interface_options, i);
+        if (interface_opts->extcap_pid == pid)
         {
-            interface_opts.extcap_pid = INVALID_EXTCAP_PID;
-            g_source_remove(interface_opts.extcap_child_watch);
-            interface_opts.extcap_child_watch = 0;
-
-            capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, i);
-            g_array_insert_val(capture_opts->ifaces, i, interface_opts);
+            pipedata = (ws_pipe_t *)interface_opts->extcap_pipedata;
+            if (pipedata != NULL)
+            {
+                interface_opts->extcap_pid = WS_INVALID_PID;
+                pipedata->exitcode = 0;
+#ifndef _WIN32
+                if (WIFEXITED(status))
+                {
+                    if (WEXITSTATUS(status) != 0)
+                    {
+                        pipedata->exitcode = WEXITSTATUS(status);
+                    }
+                }
+                else
+                {
+                    pipedata->exitcode = G_SPAWN_ERROR_FAILED;
+                }
+#else
+                if (status != 0)
+                {
+                    pipedata->exitcode = status;
+                }
+#endif
+                if (status == 0 && pipedata->stderr_msg != NULL)
+                {
+                    pipedata->exitcode = 1;
+                }
+            }
+            g_source_remove(interface_opts->extcap_child_watch);
+            interface_opts->extcap_child_watch = 0;
             break;
         }
     }
 }
 
-/* call mkfifo for each extcap,
- * returns FALSE if there's an error creating a FIFO */
-gboolean
-extcap_init_interfaces(capture_options *capture_opts)
+static
+GPtrArray *extcap_prepare_arguments(interface_options *interface_opts)
 {
-    guint i;
-    interface_options interface_opts;
+    GPtrArray *result = NULL;
 
-    for (i = 0; i < capture_opts->ifaces->len; i++)
+    if (interface_opts->if_type == IF_EXTCAP)
     {
-        GPtrArray *args = NULL;
-        GPid pid = INVALID_EXTCAP_PID;
-        gchar **tmp;
-        int tmp_i;
-
-        interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
+        result = g_ptr_array_new();
 
-        /* skip native interfaces */
-        if (interface_opts.if_type != IF_EXTCAP )
-            continue;
-
-        /* create pipe for fifo */
-        if ( ! extcap_create_pipe ( &interface_opts.extcap_fifo ) )
-            return FALSE;
-
-        /* Create extcap call */
-        args = g_ptr_array_new();
-#define add_arg(X) g_ptr_array_add(args, g_strdup(X))
+#define add_arg(X) g_ptr_array_add(result, g_strdup(X))
 
-        add_arg(interface_opts.extcap);
+        add_arg(interface_opts->extcap);
         add_arg(EXTCAP_ARGUMENT_RUN_CAPTURE);
         add_arg(EXTCAP_ARGUMENT_INTERFACE);
-        add_arg(interface_opts.name);
-        if (interface_opts.cfilter && strlen(interface_opts.cfilter) > 0) {
+        add_arg(interface_opts->name);
+        if (interface_opts->cfilter && strlen(interface_opts->cfilter) > 0)
+        {
             add_arg(EXTCAP_ARGUMENT_CAPTURE_FILTER);
-            add_arg(interface_opts.cfilter);
+            add_arg(interface_opts->cfilter);
         }
         add_arg(EXTCAP_ARGUMENT_RUN_PIPE);
-        add_arg(interface_opts.extcap_fifo);
-        if (interface_opts.extcap_args == NULL || g_hash_table_size(interface_opts.extcap_args) == 0)
+        add_arg(interface_opts->extcap_fifo);
+        if (interface_opts->extcap_control_in)
+        {
+            add_arg(EXTCAP_ARGUMENT_CONTROL_OUT);
+            add_arg(interface_opts->extcap_control_in);
+        }
+        if (interface_opts->extcap_control_out)
+        {
+            add_arg(EXTCAP_ARGUMENT_CONTROL_IN);
+            add_arg(interface_opts->extcap_control_out);
+        }
+        if (interface_opts->extcap_args == NULL || g_hash_table_size(interface_opts->extcap_args) == 0)
         {
             /* User did not perform interface configuration.
              *
@@ -776,10 +1387,10 @@ extcap_init_interfaces(capture_options *capture_opts)
             GList *arglist;
             GList *elem;
 
-            arglist = extcap_get_if_configuration(interface_opts.name);
+            arglist = extcap_get_if_configuration(interface_opts->name);
             for (elem = g_list_first(arglist); elem; elem = elem->next)
             {
-                GList * arg_list;
+                GList *arg_list;
                 extcap_arg *arg_iter;
 
                 if (elem->data == NULL)
@@ -788,63 +1399,126 @@ extcap_init_interfaces(capture_options *capture_opts)
                 }
 
                 arg_list = g_list_first((GList *)elem->data);
-                while (arg_list != NULL) {
-                    gchar * stored = NULL, * defval = NULL;
+                while (arg_list != NULL)
+                {
+                    const gchar *stored = NULL;
                     /* In case of boolflags only first element in arg_list is relevant. */
-                    arg_iter = (extcap_arg*) (arg_list->data);
-                    if ( arg_iter->storeval != NULL )
-                        stored = arg_iter->storeval;
-
-                    if ( arg_iter->default_complex != NULL && arg_iter->default_complex->_val != NULL )
-                        defval = arg_iter->default_complex->_val;
-
-                    /* Different data in storage then set for default */
-                    if ( g_strcmp0(stored, defval) != 0 ) {
-                        if ( arg_iter->arg_type == EXTCAP_ARG_BOOLFLAG ) {
-                            if ( g_strcmp0(stored, "true") == 0 )
-                                add_arg(arg_iter->call);
-                        } else {
-                            gchar * call = g_strconcat(arg_iter->call, " ", stored, NULL);
-                            add_arg(call);
-                            g_free(call);
-                        }
-                    } else if  (arg_iter->arg_type == EXTCAP_ARG_BOOLFLAG) {
+                    arg_iter = (extcap_arg *)(arg_list->data);
+                    if (arg_iter->pref_valptr != NULL)
+                    {
+                        stored = *arg_iter->pref_valptr;
+                    }
+
+                    if (arg_iter->arg_type == EXTCAP_ARG_BOOLFLAG)
+                    {
                         if (extcap_complex_get_bool(arg_iter->default_complex))
+                        {
+                            add_arg(arg_iter->call);
+                        }
+                        else if (g_strcmp0(stored, "true") == 0)
+                        {
                             add_arg(arg_iter->call);
+                        }
+                    }
+                    else
+                    {
+                        if (stored && strlen(stored) > 0) {
+                            add_arg(arg_iter->call);
+                            add_arg(stored);
+                        }
                     }
 
                     arg_list = arg_list->next;
                 }
             }
 
-            extcap_free_if_configuration(arglist);
+            extcap_free_if_configuration(arglist, TRUE);
         }
         else
         {
-            g_hash_table_foreach_remove(interface_opts.extcap_args, extcap_add_arg_and_remove_cb, args);
+            g_hash_table_foreach_remove(interface_opts->extcap_args, extcap_add_arg_and_remove_cb, result);
         }
         add_arg(NULL);
 #undef add_arg
 
-        /* Dump commandline parameters sent to extcap. */
-        for (tmp = (gchar **)args->pdata, tmp_i = 0; *tmp && **tmp; ++tmp_i, ++tmp)
+    }
+
+    return result;
+}
+
+static void ptr_array_free(gpointer data, gpointer user_data _U_)
+{
+    g_free(data);
+}
+
+/* call mkfifo for each extcap,
+ * returns FALSE if there's an error creating a FIFO */
+gboolean
+extcap_init_interfaces(capture_options *capture_opts)
+{
+    guint i;
+    interface_options *interface_opts;
+    ws_pipe_t *pipedata;
+
+    for (i = 0; i < capture_opts->ifaces->len; i++)
+    {
+        GPtrArray *args = NULL;
+        GPid pid = WS_INVALID_PID;
+
+        interface_opts = &g_array_index(capture_opts->ifaces, interface_options, i);
+
+        /* skip native interfaces */
+        if (interface_opts->if_type != IF_EXTCAP)
+        {
+            continue;
+        }
+
+        /* create control pipes if having toolbar */
+        if (extcap_has_toolbar(interface_opts->name))
         {
-            g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "argv[%d]: %s", tmp_i, *tmp);
+            extcap_create_pipe(interface_opts->name, &interface_opts->extcap_control_in,
+                               EXTCAP_CONTROL_IN_PREFIX);
+#ifdef _WIN32
+            interface_opts->extcap_control_in_h = pipe_h;
+#endif
+            extcap_create_pipe(interface_opts->name, &interface_opts->extcap_control_out,
+                               EXTCAP_CONTROL_OUT_PREFIX);
+#ifdef _WIN32
+            interface_opts->extcap_control_out_h = pipe_h;
+#endif
         }
 
-        /* Wireshark for windows crashes here sometimes *
-         * Access violation reading location 0x...      */
-        g_spawn_async(NULL, (gchar **)args->pdata, NULL,
-                    (GSpawnFlags) G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL,
-                    &pid,NULL);
+        /* create pipe for fifo */
+        if (!extcap_create_pipe(interface_opts->name, &interface_opts->extcap_fifo,
+                                EXTCAP_PIPE_PREFIX))
+        {
+            return FALSE;
+        }
+#ifdef _WIN32
+        interface_opts->extcap_pipe_h = pipe_h;
+#endif
+
+        /* Create extcap call */
+        args = extcap_prepare_arguments(interface_opts);
+
+        pipedata = g_new0(ws_pipe_t, 1);
+
+        pid = ws_pipe_spawn_async(pipedata, args);
 
-        g_ptr_array_foreach(args, (GFunc)g_free, NULL);
+        g_ptr_array_foreach(args, ptr_array_free, NULL);
         g_ptr_array_free(args, TRUE);
-        interface_opts.extcap_pid = pid;
-        interface_opts.extcap_child_watch =
+
+        if (pid == WS_INVALID_PID)
+        {
+            g_free(pipedata);
+            continue;
+        }
+
+        ws_close(pipedata->stdin_fd);
+        interface_opts->extcap_pid = pid;
+
+        interface_opts->extcap_child_watch =
             g_child_watch_add(pid, extcap_child_watch_cb, (gpointer)capture_opts);
-        capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, i);
-        g_array_insert_val(capture_opts->ifaces, i, interface_opts);
 
 #ifdef _WIN32
         /* On Windows, wait for extcap to connect to named pipe.
@@ -853,94 +1527,46 @@ extcap_init_interfaces(capture_options *capture_opts)
          * connect to named pipe (including user interaction).
          * Wait on multiple object in case of extcap termination
          * without opening pipe.
-         *
-         * Minimum supported version of Windows: XP / Server 2003.
          */
-        if (pid != INVALID_EXTCAP_PID)
+        if (pid != WS_INVALID_PID)
         {
-            DWORD dw;
-            HANDLE handles[2];
-            OVERLAPPED ov;
-            ov.Pointer = 0;
-            ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
+            HANDLE pipe_handles[3];
+            int num_pipe_handles = 1;
+            pipe_handles[0] = interface_opts->extcap_pipe_h;
 
-            ConnectNamedPipe(pipe_h, &ov);
-            handles[0] = ov.hEvent;
-            handles[1] = pid;
-
-            if (GetLastError() == ERROR_PIPE_CONNECTED)
+            if (extcap_has_toolbar(interface_opts->name))
             {
-                g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "extcap connected to pipe");
-            }
-            else
-            {
-                dw = WaitForMultipleObjects(2, handles, FALSE, 30000);
-                if (dw == WAIT_OBJECT_0)
-                {
-                    /* ConnectNamedPipe finished. */
-                    DWORD code;
+                pipe_handles[1] = interface_opts->extcap_control_in_h;
+                pipe_handles[2] = interface_opts->extcap_control_out_h;
+                num_pipe_handles += 2;
+             }
 
-                    code = GetLastError();
-                    if (code == ERROR_IO_PENDING)
-                    {
-                        DWORD dummy;
-                        if (!GetOverlappedResult(ov.hEvent, &ov, &dummy, TRUE))
-                        {
-                            code = GetLastError();
-                        }
-                        else
-                        {
-                            code = ERROR_SUCCESS;
-                        }
-                    }
-
-                    g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "ConnectNamedPipe code: %d", code);
-                }
-                else if (dw == (WAIT_OBJECT_0 + 1))
-                {
-                    /* extcap process terminated. */
-                    g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "extcap terminated without connecting to pipe!");
-                }
-                else if (dw == WAIT_TIMEOUT)
-                {
-                    g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "extcap didn't connect to pipe within 30 seconds!");
-                }
-                else
-                {
-                    g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "WaitForMultipleObjects returned 0x%08X. Error %d", dw, GetLastError());
-                }
-            }
-
-            CloseHandle(ov.hEvent);
+            ws_pipe_wait_for_pipe(pipe_handles, num_pipe_handles, pid);
         }
 #endif
+
+        interface_opts->extcap_pipedata = (gpointer) pipedata;
     }
 
     return TRUE;
 }
 
 #ifdef _WIN32
-/* called by capture_sync to get the CreatNamedPipe handle*/
-HANDLE
-extcap_get_win32_handle()
+gboolean extcap_create_pipe(const gchar *ifname, gchar **fifo, const gchar *pipe_prefix)
 {
-    return pipe_h;
-}
-#endif
-
-gboolean extcap_create_pipe(char ** fifo)
-{
-#ifdef _WIN32
-    gchar timestr[ 14+1 ];
+    gchar timestr[ 14 + 1 ];
     time_t current_time;
-
     gchar *pipename = NULL;
-
     SECURITY_ATTRIBUTES security;
+
     /* create pipename */
     current_time = time(NULL);
+    /*
+     * XXX - we trust Windows not to return a time before the Epoch here,
+     * so we won't get a null pointer back from localtime().
+     */
     strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S", localtime(&current_time));
-    pipename = g_strconcat ( "\\\\.\\pipe\\", EXTCAP_PIPE_PREFIX, "_", timestr, NULL );
+    pipename = g_strconcat("\\\\.\\pipe\\", pipe_prefix, "_", ifname, "_", timestr, NULL);
 
     /* Security struct to enable Inheritable HANDLE */
     memset(&security, 0, sizeof(SECURITY_ATTRIBUTES));
@@ -948,118 +1574,450 @@ gboolean extcap_create_pipe(char ** fifo)
     security.bInheritHandle = TRUE;
     security.lpSecurityDescriptor = NULL;
 
-    /* create a namedPipe*/
+    /* create a namedPipe */
     pipe_h = CreateNamedPipe(
-                utf_8to16(pipename),
-                PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
-                PIPE_TYPE_MESSAGE| PIPE_READMODE_MESSAGE | PIPE_WAIT,
-                5, 65536, 65536,
-                300,
-                &security);
+                 utf_8to16(pipename),
+                 PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
+                 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
+                 1, 65536, 65536,
+                 300,
+                 &security);
 
     if (pipe_h == INVALID_HANDLE_VALUE)
     {
-        g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,"\nError creating pipe => (%d)", GetLastError());
+        g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "\nError creating pipe => (%d)", GetLastError());
+        g_free (pipename);
         return FALSE;
     }
     else
     {
-        g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,"\nWireshark Created pipe =>(%s)",pipename);
+        g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "\nWireshark Created pipe =>(%s)", pipename);
         *fifo = g_strdup(pipename);
     }
+
+    return TRUE;
+}
 #else
+gboolean extcap_create_pipe(const gchar *ifname, gchar **fifo, const gchar *pipe_prefix)
+{
     gchar *temp_name = NULL;
     int fd = 0;
 
-    if ((fd = create_tempfile(&temp_name, EXTCAP_PIPE_PREFIX)) < 0 )
+    gchar *pfx = g_strconcat(pipe_prefix, "_", ifname, NULL);
+    if ((fd = create_tempfile(&temp_name, pfx, NULL)) < 0)
+    {
+        g_free(pfx);
         return FALSE;
+    }
+    g_free(pfx);
 
     ws_close(fd);
 
     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
-            "Extcap - Creating fifo: %s", temp_name);
+          "Extcap - Creating fifo: %s", temp_name);
 
-    if ( file_exists(temp_name) )
+    if (file_exists(temp_name))
+    {
         ws_unlink(temp_name);
+    }
 
     if (mkfifo(temp_name, 0600) == 0)
+    {
         *fifo = g_strdup(temp_name);
-#endif
+    }
 
     return TRUE;
 }
+#endif
 
-#if ARG_DEBUG
-void extcap_debug_arguments ( extcap_arg *arg_iter )
+/************* EXTCAP LOAD INTERFACE LIST ***************
+ *
+ * The following code handles loading and reloading the interface list. It is explicitly
+ * kept separate from the rest
+ */
+
+
+static void
+extcap_free_interface_info(gpointer data)
 {
-    extcap_value *v = NULL;
-    GList *walker = NULL;
+    extcap_info *info = (extcap_info *)data;
 
-    printf("debug - parser dump\n");
-    while (arg_iter != NULL) {
-        printf("ARG %d call=%s display=\"%s\" type=", arg_iter->arg_num, arg_iter->call, arg_iter->display);
+    g_free(info->basename);
+    g_free(info->full_path);
+    g_free(info->version);
+    g_free(info->help);
 
-        switch (arg_iter->arg_type) {
-            case EXTCAP_ARG_INTEGER:
-            printf("int\n");
-            break;
-            case EXTCAP_ARG_UNSIGNED:
-            printf("unsigned\n");
-            break;
-            case EXTCAP_ARG_LONG:
-            printf("long\n");
-            break;
-            case EXTCAP_ARG_DOUBLE:
-            printf("double\n");
-            break;
-            case EXTCAP_ARG_BOOLEAN:
-            printf("boolean\n");
-            break;
-            case EXTCAP_ARG_MENU:
-            printf("menu\n");
-            break;
-            case EXTCAP_ARG_RADIO:
-            printf("radio\n");
-            break;
-            case EXTCAP_ARG_SELECTOR:
-            printf("selctor\n");
-            break;
-            case EXTCAP_ARG_STRING:
-            printf ( "string\n" );
-            break;
-            case EXTCAP_ARG_PASSWORD:
-            printf ( "PASSWORD\n" );
-            break;
-            case EXTCAP_ARG_MULTICHECK:
-            printf ( "unknown\n" );
-            break;
-            case EXTCAP_ARG_UNKNOWN:
-            printf ( "unknown\n" );
-            break;
+    extcap_free_interfaces(info->interfaces);
+
+    g_free(info);
+}
+
+static extcap_info *
+extcap_ensure_interface(const gchar * toolname, gboolean create_if_nonexist)
+{
+    extcap_info * element = 0;
+
+    if ( prefs.capture_no_extcap )
+        return NULL;
+
+    if ( ! toolname )
+        return element;
+
+    if ( ! _loaded_interfaces )
+        _loaded_interfaces = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, extcap_free_interface);
+
+    element = (extcap_info *) g_hash_table_lookup(_loaded_interfaces, toolname );
+    if ( ! element && create_if_nonexist )
+    {
+        g_hash_table_insert(_loaded_interfaces, g_strdup(toolname), g_new0(extcap_info, 1));
+        element = (extcap_info *) g_hash_table_lookup(_loaded_interfaces, toolname );
+    }
+
+    return element;
+}
+
+extcap_info *
+extcap_get_tool_by_ifname(const gchar *ifname)
+{
+    if ( ifname && _tool_for_ifname )
+    {
+        gchar * toolname = (gchar *)g_hash_table_lookup(_tool_for_ifname, ifname);
+        if ( toolname )
+            return extcap_ensure_interface(toolname, FALSE);
+    }
+
+    return NULL;
+}
+
+extcap_info *
+extcap_get_tool_info(const gchar * toolname)
+{
+    return extcap_ensure_interface(toolname, FALSE);
+}
+
+static void remove_extcap_entry(gpointer entry, gpointer data _U_)
+{
+    extcap_interface *int_iter = (extcap_interface*)entry;
+
+    if (int_iter->if_type == EXTCAP_SENTENCE_EXTCAP)
+        extcap_free_interface(entry);
+}
+
+static void
+process_new_extcap(const char *extcap, char *output)
+{
+    GList * interfaces = NULL, * control_items = NULL, * walker = NULL;
+    extcap_interface * int_iter = NULL;
+    extcap_info * element = NULL;
+    iface_toolbar * toolbar_entry = NULL;
+    gchar * toolname = g_path_get_basename(extcap);
+
+    GList * interface_keys = g_hash_table_get_keys(_loaded_interfaces);
+
+    /* Load interfaces from utility */
+    interfaces = extcap_parse_interfaces(output, &control_items);
+
+    g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Loading interface list for %s ", extcap);
+
+    /* Seems, that there where no interfaces to be loaded */
+    if ( ! interfaces || g_list_length(interfaces) == 0 )
+    {
+        g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Cannot load interfaces for %s", extcap );
+        g_list_free(interface_keys);
+        g_free(toolname);
+        return;
+    }
+
+    /* Load or create the storage element for the tool */
+    element = extcap_ensure_interface(toolname, TRUE);
+    if ( element == NULL )
+    {
+        g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_ERROR, "Cannot store interface %s, maybe duplicate?", extcap );
+        g_list_foreach(interfaces, remove_extcap_entry, NULL);
+        g_list_free(interfaces);
+        g_list_free(interface_keys);
+        g_free(toolname);
+        return;
+    }
+
+    if (control_items)
+    {
+        toolbar_entry = g_new0(iface_toolbar, 1);
+        toolbar_entry->controls = control_items;
+    }
+
+    walker = interfaces;
+    gchar* help = NULL;
+    while (walker != NULL)
+    {
+        int_iter = (extcap_interface *)walker->data;
+
+        if (int_iter->call != NULL)
+            g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Interface found %s\n", int_iter->call);
+
+        /* Help is not necessarily stored with the interface, but rather with the version string.
+         * As the version string allways comes in front of the interfaces, this ensures, that it get's
+         * properly stored with the interface */
+        if (int_iter->if_type == EXTCAP_SENTENCE_EXTCAP)
+        {
+            if (int_iter->call != NULL)
+                g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "  Extcap [%s] ", int_iter->call);
+
+            /* Only initialize values if none are set. Need to check only one element here */
+            if ( ! element->version )
+            {
+                element->version = g_strdup(int_iter->version);
+                element->basename = g_strdup(toolname);
+                element->full_path = g_strdup(extcap);
+                element->help = g_strdup(int_iter->help);
+            }
+
+            help = int_iter->help;
+            if (toolbar_entry)
+            {
+                toolbar_entry->menu_title = g_strdup(int_iter->display);
+                toolbar_entry->help = g_strdup(int_iter->help);
+            }
+
+            walker = g_list_next(walker);
+            continue;
         }
 
-        if (arg_iter->range_start != NULL && arg_iter->range_end != NULL) {
-            printf("\tRange: ");
-            extcap_printf_complex(arg_iter->range_start);
-            printf(" - ");
-            extcap_printf_complex(arg_iter->range_end);
-            printf("\n");
+        /* Only interface definitions will be parsed here. help is already set by the extcap element,
+         * which makes it necessary to have version in the list before the interfaces. This is normally
+         * the case by design, but could be changed by separating the information in extcap-base. */
+        if ( int_iter->if_type == EXTCAP_SENTENCE_INTERFACE )
+        {
+            if ( g_list_find(interface_keys, int_iter->call) )
+            {
+                g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_WARNING, "Extcap interface \"%s\" is already provided by \"%s\" ",
+                      int_iter->call, extcap_if_executable(int_iter->call));
+                walker = g_list_next(walker);
+                continue;
+            }
+
+            if ((int_iter->call != NULL) && (int_iter->display))
+                g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "  Interface [%s] \"%s\" ", int_iter->call, int_iter->display);
+
+            int_iter->extcap_path = g_strdup(extcap);
+
+            /* Only set the help, if it exists and no parsed help information is present */
+            if ( ! int_iter->help && help )
+                int_iter->help = g_strdup(help);
+
+            element->interfaces = g_list_append(element->interfaces, int_iter);
+            g_hash_table_insert(_tool_for_ifname, g_strdup(int_iter->call), g_strdup(toolname));
+
+            if (toolbar_entry)
+            {
+                if (!toolbar_entry->menu_title)
+                {
+                    toolbar_entry->menu_title = g_strdup(int_iter->display);
+                }
+                toolbar_entry->ifnames = g_list_append(toolbar_entry->ifnames, g_strdup(int_iter->call));
+            }
         }
 
-        for ( walker = g_list_first ( arg_iter->value_list ); walker; walker = walker->next )
+        walker = g_list_next(walker);
+    }
+
+    if (toolbar_entry && toolbar_entry->menu_title)
+    {
+        iface_toolbar_add(toolbar_entry);
+        extcap_iface_toolbar_add(extcap, toolbar_entry);
+    }
+
+    g_list_foreach(interfaces, remove_extcap_entry, NULL);
+    g_list_free(interfaces);
+    g_list_free(interface_keys);
+    g_free(toolname);
+}
+
+
+/** Thread callback to save the output of a --extcap-config call. */
+static void
+extcap_process_config_cb(thread_pool_t *pool _U_, void *data, char *output)
+{
+    extcap_iface_info_t *iface_info = (extcap_iface_info_t *)data;
+    iface_info->output = output;
+}
+
+/**
+ * Thread callback to process discovered interfaces, scheduling more tasks to
+ * retrieve the configuration for each interface. Called once for every extcap
+ * program.
+ */
+static void
+extcap_process_interfaces_cb(thread_pool_t *pool, void *data, char *output)
+{
+    extcap_run_extcaps_info_t *info = (extcap_run_extcaps_info_t *)data;
+    guint i = 0;
+    guint num_interfaces = 0;
+
+    if (!output) {
+        // No interfaces available, nothing to do.
+        return;
+    }
+
+    // Save output for process_new_extcap.
+    info->output = output;
+
+    // Are there any interfaces to query information from?
+    GList *interfaces = extcap_parse_interfaces(output, NULL);
+    for (GList *iface = interfaces; iface; iface = g_list_next(iface)) {
+        extcap_interface *intf = (extcap_interface *)iface->data;
+        if (intf->if_type == EXTCAP_SENTENCE_INTERFACE) {
+            ++num_interfaces;
+        }
+    }
+    if (num_interfaces == 0) {
+        // nothing to do.
+        g_list_free_full(interfaces, extcap_free_interface);
+        return;
+    }
+
+    /* GSList is not thread-safe, so pre-allocate an array instead. */
+    info->iface_infos = g_new0(extcap_iface_info_t, num_interfaces);
+    info->num_interfaces = num_interfaces;
+
+    // Schedule new commands to retrieve the configuration.
+    for (GList *iface = interfaces; iface; iface = g_list_next(iface)) {
+        extcap_interface *intf = (extcap_interface *)iface->data;
+        if (intf->if_type != EXTCAP_SENTENCE_INTERFACE) {
+            continue;
+        }
+
+        const char *argv[] = {
+            EXTCAP_ARGUMENT_CONFIG,
+            EXTCAP_ARGUMENT_INTERFACE,
+            intf->call,
+            NULL
+        };
+        extcap_run_task_t *task = g_new0(extcap_run_task_t, 1);
+        extcap_iface_info_t *iface_info = &info->iface_infos[i++];
+
+        task->extcap_path = info->extcap_path;
+        task->argv = g_strdupv((char **)argv);
+        task->output_cb = extcap_process_config_cb;
+        task->data = iface_info;
+        iface_info->ifname = g_strdup(intf->call);
+
+        thread_pool_push(pool, task, NULL);
+    }
+    g_list_free_full(interfaces, extcap_free_interface);
+}
+
+/**
+ * Thread callback to check whether the new-style --list-interfaces call with an
+ * explicit function succeeded. If not, schedule a call without the new version
+ * argument.
+ */
+static void
+extcap_list_interfaces_cb(thread_pool_t *pool, void *data, char *output)
+{
+    extcap_run_extcaps_info_t *info = (extcap_run_extcaps_info_t *)data;
+
+    if (!output) {
+        /* No output available, schedule a fallback query. */
+        const char *argv[] = {
+            EXTCAP_ARGUMENT_LIST_INTERFACES,
+            NULL
+        };
+        extcap_run_task_t *task = g_new0(extcap_run_task_t, 1);
+
+        task->extcap_path = info->extcap_path;
+        task->argv = g_strdupv((char **)argv);
+        task->output_cb = extcap_process_interfaces_cb;
+        task->data = info;
+
+        thread_pool_push(pool, task, NULL);
+    } else {
+        extcap_process_interfaces_cb(pool, info, output);
+    }
+}
+
+
+/* Handles loading of the interfaces.
+ *
+ * A list of interfaces can be obtained by calling \ref extcap_loaded_interfaces
+ */
+static void
+extcap_load_interface_list(void)
+{
+    if (prefs.capture_no_extcap)
+        return;
+
+    if (_toolbars)
+    {
+        // Remove existing interface toolbars here instead of in extcap_clear_interfaces()
+        // to avoid flicker in shown toolbars when refreshing interfaces.
+        GList *toolbar_list = g_hash_table_get_values (_toolbars);
+        for (GList *walker = toolbar_list; walker; walker = walker->next)
         {
-            v = (extcap_value *)walker->data;
-            if (v->is_default)
-            printf("*");
-            printf("\tcall=\"%p\" display=\"%p\"\n", v->call, v->display);
-            printf("\tcall=\"%s\" display=\"%s\"\n", v->call, v->display);
+            iface_toolbar *toolbar = (iface_toolbar *) walker->data;
+            iface_toolbar_remove(toolbar->menu_title);
         }
+        g_hash_table_remove_all(_toolbars);
+    } else {
+        _toolbars = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, extcap_free_toolbar);
+    }
 
-        arg_iter = arg_iter->next_arg;
+    if (_loaded_interfaces == NULL)
+    {
+        int major = 0;
+        int minor = 0;
+        guint count = 0;
+        extcap_run_extcaps_info_t *infos;
+        GList *unused_arguments = NULL;
+
+        _loaded_interfaces = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, extcap_free_interface_info);
+        /* Cleanup lookup table */
+        if ( _tool_for_ifname )
+        {
+            g_hash_table_remove_all(_tool_for_ifname);
+            _tool_for_ifname = 0;
+        } else {
+            _tool_for_ifname = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
+        }
+
+        get_ws_version_number(&major, &minor, NULL);
+        char *arg_version = g_strdup_printf("%s=%d.%d", EXTCAP_ARGUMENT_VERSION, major, minor);
+        const char *argv[] = {
+            EXTCAP_ARGUMENT_LIST_INTERFACES,
+            arg_version,
+            NULL
+        };
+        infos = (extcap_run_extcaps_info_t *)extcap_run_all(argv,
+                extcap_list_interfaces_cb, sizeof(extcap_run_extcaps_info_t),
+                &count);
+        for (guint i = 0; i < count; i++) {
+            if (!infos[i].output) {
+                continue;
+            }
+
+            // Save new extcap and each discovered interface.
+            process_new_extcap(infos[i].extcap_path, infos[i].output);
+            for (guint j = 0; j < infos[i].num_interfaces; j++) {
+                extcap_iface_info_t *iface_info = &infos[i].iface_infos[j];
+
+                if (!iface_info->output) {
+                    continue;
+                }
+
+                extcap_callback_info_t cb_info = {
+                    .ifname = iface_info->ifname,
+                    .output = iface_info->output,
+                    .data = &unused_arguments,
+                };
+                cb_preference(cb_info);
+            }
+        }
+        /* XXX rework cb_preference such that this unused list can be removed. */
+        extcap_free_if_configuration(unused_arguments, TRUE);
+        extcap_free_extcaps_info_array(infos, count);
+        g_free(arg_version);
     }
 }
-#endif
-#endif
 
 /*
  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html