3 * Routines for extcap external capture
4 * Copyright 2013, Mike Ryan <mikeryan@lacklustre.net>
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * SPDX-License-Identifier: GPL-2.0+
24 /* Include for unlink */
28 #ifdef HAVE_SYS_TYPES_H
29 #include <sys/types.h>
31 #ifdef HAVE_SYS_WAIT_H
38 #include <epan/prefs.h>
40 #include "ui/iface_toolbar.h"
42 #include <wsutil/glib-compat.h>
43 #include <wsutil/file_util.h>
44 #include <wsutil/filesystem.h>
45 #include <wsutil/tempfile.h>
47 #include "capture_opts.h"
50 #include "extcap_parser.h"
51 #include "extcap_spawn.h"
54 static HANDLE pipe_h = INVALID_HANDLE_VALUE;
57 static void extcap_child_watch_cb(GPid pid, gint status, gpointer user_data);
59 /* internal container, for all the extcap executables that have been found.
60 * Will be resetted if extcap_clear_interfaces() is being explicitly called
61 * and is being used for printing information about all extcap interfaces found,
62 * as well as storing all sub-interfaces
64 static GHashTable * _loaded_interfaces = NULL;
66 /* Internal container, which maps each ifname to the tool providing it, for faster
67 * lookup. The key and string value are owned by this table.
69 static GHashTable * _tool_for_ifname = NULL;
71 /* internal container, for all the extcap executables that have been found
72 * and that provides a toolbar with controls to be added to a Interface Toolbar
74 static GHashTable *_toolbars = NULL;
76 /* internal container, to map preference names to pointers that hold preference
77 * values. These ensure that preferences can survive extcap if garbage
78 * collection, and does not lead to dangling pointers in the prefs subsystem.
80 static GHashTable *extcap_prefs_dynamic_vals = NULL;
82 typedef struct _extcap_callback_info_t
89 } extcap_callback_info_t;
91 /* Callback definition for extcap_foreach */
92 typedef gboolean(*extcap_cb_t)(extcap_callback_info_t info_structure);
94 static void extcap_load_interface_list(void);
97 extcap_loaded_interfaces(void)
99 if ( !_loaded_interfaces || g_hash_table_size(_loaded_interfaces) == 0 )
100 extcap_load_interface_list();
102 return _loaded_interfaces;
106 extcap_clear_interfaces(void)
108 if ( _loaded_interfaces )
109 g_hash_table_destroy(_loaded_interfaces);
110 _loaded_interfaces = NULL;
112 if ( _tool_for_ifname )
113 g_hash_table_destroy(_tool_for_ifname);
114 _tool_for_ifname = NULL;
117 guint extcap_count(void)
119 const char *dirname = get_extcap_dir();
125 if ((dir = g_dir_open(dirname, 0, NULL)) != NULL)
127 GString *extcap_path = NULL;
130 extcap_path = g_string_new("");
131 while ((file = g_dir_read_name(dir)) != NULL)
133 /* full path to extcap binary */
134 g_string_printf(extcap_path, "%s" G_DIR_SEPARATOR_S "%s", dirname, file);
135 /* treat anything executable as an extcap binary */
136 if (g_file_test(extcap_path->str, G_FILE_TEST_IS_REGULAR) &&
137 g_file_test(extcap_path->str, G_FILE_TEST_IS_EXECUTABLE))
144 g_string_free(extcap_path, TRUE);
150 extcap_if_exists(const gchar *ifname)
152 if (!ifname || !_tool_for_ifname)
157 if (g_hash_table_lookup(_tool_for_ifname, ifname))
165 static extcap_interface *
166 extcap_find_interface_for_ifname(const gchar *ifname)
168 extcap_interface * result = NULL;
170 if ( !ifname || ! _tool_for_ifname || ! _loaded_interfaces )
173 gchar * extcap_util = (gchar *)g_hash_table_lookup(_tool_for_ifname, ifname);
177 extcap_info * element = (extcap_info *)g_hash_table_lookup(_loaded_interfaces, extcap_util);
181 GList * walker = element->interfaces;
182 while ( walker && walker->data && ! result )
184 extcap_interface * interface = (extcap_interface *)walker->data;
185 if ( g_strcmp0(interface->call, ifname) == 0 )
191 walker = g_list_next ( walker );
198 extcap_free_toolbar_value(iface_toolbar_value *value)
205 g_free(value->value);
206 g_free(value->display);
211 extcap_free_toolbar_control(iface_toolbar_control *control)
218 g_free(control->display);
219 g_free(control->validation);
220 g_free(control->tooltip);
221 if (control->ctrl_type == INTERFACE_TYPE_STRING) {
222 g_free(control->default_value.string);
224 g_list_foreach(control->values, (GFunc)extcap_free_toolbar_value, NULL);
225 g_list_free(control->values);
230 extcap_free_toolbar(gpointer data)
237 iface_toolbar *toolbar = (iface_toolbar *)data;
239 g_free(toolbar->menu_title);
240 g_free(toolbar->help);
241 g_list_free_full(toolbar->ifnames, g_free);
242 g_list_foreach(toolbar->controls, (GFunc)extcap_free_toolbar_control, NULL);
243 g_list_free(toolbar->controls);
248 extcap_if_exists_for_extcap(const gchar *ifname, const gchar *extcap)
250 extcap_interface *entry = extcap_find_interface_for_ifname(ifname);
252 if (entry && strcmp(entry->extcap_path, extcap) == 0)
261 extcap_if_executable(const gchar *ifname)
263 extcap_interface *interface = extcap_find_interface_for_ifname(ifname);
264 return interface != NULL ? interface->extcap_path : NULL;
268 extcap_iface_toolbar_add(const gchar *extcap, iface_toolbar *toolbar_entry)
272 if (!extcap || !toolbar_entry)
277 toolname = g_path_get_basename(extcap);
279 if (!g_hash_table_lookup(_toolbars, toolname))
281 g_hash_table_insert(_toolbars, g_strdup(toolname), toolbar_entry);
287 /* Note: args does not need to be NULL-terminated. */
288 static gboolean extcap_foreach(gint argc, gchar **args,
289 extcap_cb_t cb, extcap_callback_info_t cb_info)
293 const char *dirname = get_extcap_dir();
297 if ((dir = g_dir_open(dirname, 0, NULL)) != NULL)
299 GString *extcap_path = NULL;
302 extcap_path = g_string_new("");
303 while (keep_going && (file = g_dir_read_name(dir)) != NULL)
305 gchar *command_output = NULL;
307 /* full path to extcap binary */
308 g_string_printf(extcap_path, "%s" G_DIR_SEPARATOR_S "%s", dirname, file);
309 /* treat anything executable as an extcap binary */
310 if (g_file_test(extcap_path->str, G_FILE_TEST_IS_REGULAR) &&
311 g_file_test(extcap_path->str, G_FILE_TEST_IS_EXECUTABLE))
313 if (extcap_if_exists(cb_info.ifname) && !extcap_if_exists_for_extcap(cb_info.ifname, extcap_path->str))
318 if (extcap_spawn_sync((gchar *) dirname, extcap_path->str, argc, args, &command_output))
320 cb_info.output = command_output;
321 cb_info.extcap = extcap_path->str;
323 keep_going = cb(cb_info);
326 g_free(command_output);
331 g_string_free(extcap_path, TRUE);
337 static void extcap_free_dlt(gpointer d, gpointer user_data _U_)
344 g_free(((extcap_dlt *)d)->name);
345 g_free(((extcap_dlt *)d)->display);
349 static void extcap_free_dlts(GList *dlts)
351 g_list_foreach(dlts, extcap_free_dlt, NULL);
355 static gboolean cb_dlt(extcap_callback_info_t cb_info)
357 GList *dlts = NULL, *temp = NULL;
359 if_capabilities_t *caps;
360 GList *linktype_list = NULL;
361 data_link_info_t *data_link_info;
362 extcap_dlt *dlt_item;
364 dlts = extcap_parse_dlts(cb_info.output);
367 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap pipe %s ", cb_info.extcap);
370 * Allocate the interface capabilities structure.
372 caps = (if_capabilities_t *) g_malloc(sizeof * caps);
373 caps->can_set_rfmon = FALSE;
374 caps->timestamp_types = NULL;
378 dlt_item = (extcap_dlt *)dlts->data;
381 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
382 " DLT %d name=\"%s\" display=\"%s\" ", dlt_item->number,
383 dlt_item->name, dlt_item->display);
385 data_link_info = g_new(data_link_info_t, 1);
386 data_link_info->dlt = dlt_item->number;
387 data_link_info->name = g_strdup(dlt_item->name);
388 data_link_info->description = g_strdup(dlt_item->display);
389 linktype_list = g_list_append(linktype_list, data_link_info);
392 dlts = g_list_next(dlts);
395 /* Check to see if we built a list */
396 if (linktype_list != NULL && cb_info.data != NULL)
398 caps->data_link_types = linktype_list;
399 *(if_capabilities_t **) cb_info.data = caps;
405 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, " returned no DLTs");
406 *(cb_info.err_str) = g_strdup("Extcap returned no DLTs");
411 extcap_free_dlts(temp);
417 extcap_get_if_dlts(const gchar *ifname, char **err_str)
421 if_capabilities_t *caps = NULL;
428 if (extcap_if_exists(ifname))
430 argv[0] = g_strdup(EXTCAP_ARGUMENT_LIST_DLTS);
431 argv[1] = g_strdup(EXTCAP_ARGUMENT_INTERFACE);
432 argv[2] = g_strdup(ifname);
434 extcap_callback_info_t cb_info;
435 cb_info.data = ∩︀
436 cb_info.err_str = err_str;
437 cb_info.ifname = ifname;
439 extcap_foreach(3, argv, cb_dlt, cb_info);
441 for (i = 0; i < 3; ++i)
450 static void extcap_free_interface(gpointer i)
453 extcap_interface *interface = (extcap_interface *)i;
460 g_free(interface->call);
461 g_free(interface->display);
462 g_free(interface->version);
463 g_free(interface->help);
464 g_free(interface->extcap_path);
468 static void extcap_free_interfaces(GList *interfaces)
470 if (interfaces == NULL)
475 g_list_foreach(interfaces, (GFunc)extcap_free_interface, NULL);
476 g_list_free(interfaces);
480 if_info_compare(gconstpointer a, gconstpointer b)
483 const if_info_t *if_a = (const if_info_t *)a;
484 const if_info_t *if_b = (const if_info_t *)b;
486 if ((comp = g_strcmp0(if_a->name, if_b->name)) == 0)
488 return g_strcmp0(if_a->friendly_name, if_b->friendly_name);
495 extcap_get_help_for_ifname(const char *ifname)
497 extcap_interface *interface = extcap_find_interface_for_ifname(ifname);
498 return interface != NULL ? interface->help : NULL;
502 append_extcap_interface_list(GList *list, char **err_str _U_)
504 GList *interface_list = NULL;
505 extcap_interface *data = NULL;
506 GList *ifutilkeys_head = NULL, *ifutilkeys = NULL;
508 /* Update the extcap interfaces and get a list of their if_infos */
509 if ( !_loaded_interfaces || g_hash_table_size(_loaded_interfaces) == 0 )
510 extcap_load_interface_list();
512 ifutilkeys_head = g_hash_table_get_keys(_loaded_interfaces);
513 ifutilkeys = ifutilkeys_head;
514 while ( ifutilkeys && ifutilkeys->data )
516 extcap_info * extinfo =
517 (extcap_info *) g_hash_table_lookup(_loaded_interfaces, (gchar *)ifutilkeys->data);
518 GList * walker = extinfo->interfaces;
519 while ( walker && walker->data )
521 interface_list = g_list_append(interface_list, walker->data);
522 walker = g_list_next(walker);
525 ifutilkeys = g_list_next(ifutilkeys);
527 g_list_free(ifutilkeys_head);
530 interface_list = g_list_sort(interface_list, if_info_compare);
532 /* Append the interfaces in that list to the list we're handed. */
533 while (interface_list != NULL)
535 GList *entry = g_list_first(interface_list);
536 data = (extcap_interface *)entry->data;
537 interface_list = g_list_delete_link(interface_list, entry);
539 if_info_t * if_info = g_new0(if_info_t, 1);
540 if_info->name = g_strdup(data->call);
541 if_info->friendly_name = g_strdup(data->display);
543 if_info->type = IF_EXTCAP;
545 if_info->extcap = g_strdup(data->extcap_path);
547 list = g_list_append(list, if_info);
554 extcap_register_preferences_callback(gpointer key, gpointer value _U_, gpointer user_data _U_)
558 arguments = extcap_get_if_configuration((gchar *)key);
559 /* Memory for prefs are external to an interface, they are part of
560 * extcap core, so the parsed arguments can be freed. */
561 extcap_free_if_configuration(arguments, TRUE);
564 void extcap_register_preferences(void)
566 module_t *dev_module = prefs_find_module("extcap");
573 if ( !_loaded_interfaces || g_hash_table_size(_loaded_interfaces) == 0 )
574 extcap_load_interface_list();
577 g_hash_table_foreach(_tool_for_ifname, extcap_register_preferences_callback, NULL);
581 * Releases the dynamic preference value pointers. Must not be called before
582 * prefs_cleanup since these pointers could still be in use.
584 void extcap_cleanup(void)
586 if (extcap_prefs_dynamic_vals)
587 g_hash_table_destroy(extcap_prefs_dynamic_vals);
589 if (_loaded_interfaces)
590 g_hash_table_destroy(_loaded_interfaces);
592 if (_tool_for_ifname)
593 g_hash_table_destroy(_tool_for_ifname);
597 * Obtains a pointer which can store a value for the given preference name.
598 * The preference name that can be passed to the prefs API is stored into
601 * Extcap interfaces (and their preferences) are dynamic, they can be created
602 * and destroyed at will. Thus their data structures are insufficient to pass to
603 * the preferences APIs which require pointers which are valid until the
604 * preferences are removed (at exit).
606 static gchar **extcap_prefs_dynamic_valptr(const char *name, char **pref_name)
609 if (!extcap_prefs_dynamic_vals)
611 /* Initialize table only as needed, most preferences are not dynamic */
612 extcap_prefs_dynamic_vals = g_hash_table_new_full(g_str_hash, g_str_equal,
615 if (!g_hash_table_lookup_extended(extcap_prefs_dynamic_vals, name,
616 (gpointer *)pref_name, (gpointer *)&valp))
618 /* New dynamic pref, allocate, initialize and store. */
619 valp = g_new0(gchar *, 1);
620 *pref_name = g_strdup(name);
621 g_hash_table_insert(extcap_prefs_dynamic_vals, *pref_name, valp);
626 void extcap_free_if_configuration(GList *list, gboolean free_args)
630 for (elem = g_list_first(list); elem; elem = elem->next)
632 if (elem->data != NULL)
634 sl = g_list_first((GList *)elem->data);
637 extcap_free_arg_list(sl);
649 extcap_pref_for_argument(const gchar *ifname, struct _extcap_arg *arg)
651 struct preference *pref = NULL;
653 GRegex *regex_name = g_regex_new("[-]+", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, NULL);
654 GRegex *regex_ifname = g_regex_new("(?![a-zA-Z1-9_]).", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, NULL);
655 if (regex_name && regex_ifname)
657 if (prefs_find_module("extcap"))
659 gchar *pref_name = g_regex_replace(regex_name, arg->call, strlen(arg->call), 0, "", (GRegexMatchFlags) 0, NULL);
660 gchar *ifname_underscore = g_regex_replace(regex_ifname, ifname, strlen(ifname), 0, "_", (GRegexMatchFlags) 0, NULL);
661 gchar *ifname_lowercase = g_ascii_strdown(ifname_underscore, -1);
662 gchar *pref_ifname = g_strconcat(ifname_lowercase, ".", pref_name, NULL);
664 pref = prefs_find_preference(prefs_find_module("extcap"), pref_ifname);
667 g_free(ifname_underscore);
668 g_free(ifname_lowercase);
674 g_regex_unref(regex_name);
678 g_regex_unref(regex_ifname);
684 static gboolean cb_preference(extcap_callback_info_t cb_info)
686 GList *arguments = NULL;
687 GList **il = (GList **) cb_info.data;
688 module_t *dev_module = NULL;
690 arguments = extcap_parse_args(cb_info.output);
692 dev_module = prefs_find_module("extcap");
696 GList *walker = arguments;
698 GRegex *regex_name = g_regex_new("[-]+", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, NULL);
699 GRegex *regex_ifname = g_regex_new("(?![a-zA-Z1-9_]).", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, NULL);
700 if (regex_name && regex_ifname)
702 while (walker != NULL)
704 extcap_arg *arg = (extcap_arg *)walker->data;
705 arg->device_name = g_strdup(cb_info.ifname);
709 struct preference *pref = NULL;
711 gchar *pref_name = g_regex_replace(regex_name, arg->call, strlen(arg->call), 0, "", (GRegexMatchFlags) 0, NULL);
712 gchar *ifname_underscore = g_regex_replace(regex_ifname, cb_info.ifname, strlen(cb_info.ifname), 0, "_", (GRegexMatchFlags) 0, NULL);
713 gchar *ifname_lowercase = g_ascii_strdown(ifname_underscore, -1);
714 gchar *pref_ifname = g_strconcat(ifname_lowercase, ".", pref_name, NULL);
716 if ((pref = prefs_find_preference(dev_module, pref_ifname)) == NULL)
718 char *pref_name_for_prefs;
719 char *pref_title = wmem_strdup(wmem_epan_scope(), arg->display);
721 arg->pref_valptr = extcap_prefs_dynamic_valptr(pref_ifname, &pref_name_for_prefs);
722 /* Set an initial value if any (the string will be copied at registration) */
723 if (arg->default_complex)
725 *arg->pref_valptr = arg->default_complex->_val;
728 prefs_register_string_preference(dev_module, pref_name_for_prefs,
729 pref_title, pref_title, (const char **)arg->pref_valptr);
733 /* Been here before, restore stored value */
734 if (arg->pref_valptr == NULL)
736 arg->pref_valptr = (gchar**)g_hash_table_lookup(extcap_prefs_dynamic_vals, pref_ifname);
741 g_free(ifname_underscore);
742 g_free(ifname_lowercase);
746 walker = g_list_next(walker);
751 g_regex_unref(regex_name);
755 g_regex_unref(regex_ifname);
759 *il = g_list_append(*il, arguments);
761 /* By returning false, extcap_foreach will break on first found */
766 extcap_get_if_configuration(const char *ifname)
770 gchar **err_str = NULL;
773 if (extcap_if_exists(ifname))
775 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap path %s",
778 argv[0] = g_strdup(EXTCAP_ARGUMENT_CONFIG);
779 argv[1] = g_strdup(EXTCAP_ARGUMENT_INTERFACE);
780 argv[2] = g_strdup(ifname);
782 extcap_callback_info_t cb_info;
784 cb_info.err_str = err_str;
785 cb_info.ifname = ifname;
787 extcap_foreach(3, argv, cb_preference, cb_info);
789 for (i = 0; i < 3; i++)
799 * If is_required is FALSE: returns TRUE if the extcap interface has
800 * configurable options.
801 * If is_required is TRUE: returns TRUE when the extcap interface has
802 * configurable options that required modification. (For example, when an
803 * argument is required but empty.)
806 extcap_has_configuration(const char *ifname, gboolean is_required)
808 GList *arguments = 0;
809 GList *walker = 0, * item = 0;
811 gboolean found = FALSE;
813 arguments = extcap_get_if_configuration((const char *)(ifname));
814 walker = g_list_first(arguments);
816 while (walker != NULL && !found)
818 item = g_list_first((GList *)(walker->data));
819 while (item != NULL && !found)
821 if ((extcap_arg *)(item->data) != NULL)
823 extcap_arg *arg = (extcap_arg *)(item->data);
824 /* Should required options be present, or any kind of options */
829 else if (arg->is_required)
831 const gchar *stored = NULL;
832 const gchar *defval = NULL;
834 if (arg->pref_valptr != NULL)
836 stored = *arg->pref_valptr;
839 if (arg->default_complex != NULL && arg->default_complex->_val != NULL)
841 defval = arg->default_complex->_val;
844 if (arg->is_required)
846 /* If stored and defval is identical and the argument is required,
847 * configuration is needed */
848 if (defval && stored && g_strcmp0(stored, defval) == 0)
852 else if (!defval && (!stored || !*stored))
858 if (arg->arg_type == EXTCAP_ARG_FILESELECT)
860 if (arg->fileexists && !(file_exists(defval) || file_exists(stored)))
870 walker = walker->next;
872 extcap_free_if_configuration(arguments, TRUE);
877 static gboolean cb_verify_filter(extcap_callback_info_t cb_info)
879 extcap_filter_status *status = (extcap_filter_status *)cb_info.data;
880 size_t output_size, i;
882 output_size = strlen(cb_info.output);
883 if (output_size == 0) {
884 *status = EXTCAP_FILTER_VALID;
886 *status = EXTCAP_FILTER_INVALID;
887 for (i = 0; i < output_size; i++) {
888 if (cb_info.output[i] == '\n' || cb_info.output[i] == '\r') {
889 cb_info.output[i] = '\0';
893 *cb_info.err_str = g_strdup(cb_info.output);
900 extcap_verify_capture_filter(const char *ifname, const char *filter, gchar **err_str)
903 extcap_filter_status status = EXTCAP_FILTER_UNKNOWN;
905 if (extcap_if_exists(ifname))
907 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap path %s",
910 argv[0] = EXTCAP_ARGUMENT_CAPTURE_FILTER;
911 argv[1] = (gchar*)filter;
912 argv[2] = EXTCAP_ARGUMENT_INTERFACE;
913 argv[3] = (gchar*)ifname;
915 extcap_callback_info_t cb_info;
916 cb_info.data = &status;
917 cb_info.err_str = err_str;
918 cb_info.ifname = ifname;
920 extcap_foreach(4, argv, cb_verify_filter, cb_info);
927 extcap_has_toolbar(const char *ifname)
929 if (!iface_toolbar_use())
934 GList *toolbar_list = g_hash_table_get_values (_toolbars);
935 for (GList *walker = toolbar_list; walker; walker = walker->next)
937 iface_toolbar *toolbar = (iface_toolbar *) walker->data;
938 if (g_list_find_custom(toolbar->ifnames, ifname, (GCompareFunc) strcmp))
947 /* taken from capchild/capture_sync.c */
948 static gboolean pipe_data_available(int pipe_fd)
950 #ifdef _WIN32 /* PeekNamedPipe */
951 HANDLE hPipe = (HANDLE) _get_osfhandle(pipe_fd);
954 if (hPipe == INVALID_HANDLE_VALUE)
959 if (! PeekNamedPipe(hPipe, NULL, 0, NULL, &bytes_avail, NULL))
971 struct timeval timeout;
974 FD_SET(pipe_fd, &rfds);
978 if (select(pipe_fd + 1, &rfds, NULL, NULL, &timeout) > 0)
987 void extcap_if_cleanup(capture_options *capture_opts, gchar **errormsg)
989 interface_options *interface_opts;
990 extcap_userdata *userdata;
992 gboolean overwrite_exitcode;
994 #define STDERR_BUFFER_SIZE 1024
996 for (icnt = 0; icnt < capture_opts->ifaces->len; icnt++)
998 interface_opts = &g_array_index(capture_opts->ifaces, interface_options,
1001 /* skip native interfaces */
1002 if (interface_opts->if_type != IF_EXTCAP)
1007 overwrite_exitcode = FALSE;
1009 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
1010 "Extcap [%s] - Cleaning up fifo: %s; PID: %d", interface_opts->name,
1011 interface_opts->extcap_fifo, interface_opts->extcap_pid);
1013 if (interface_opts->extcap_pipe_h != INVALID_HANDLE_VALUE)
1015 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
1016 "Extcap [%s] - Closing pipe", interface_opts->name);
1017 FlushFileBuffers(interface_opts->extcap_pipe_h);
1018 DisconnectNamedPipe(interface_opts->extcap_pipe_h);
1019 CloseHandle(interface_opts->extcap_pipe_h);
1020 interface_opts->extcap_pipe_h = INVALID_HANDLE_VALUE;
1022 if (interface_opts->extcap_control_in_h != INVALID_HANDLE_VALUE)
1024 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
1025 "Extcap [%s] - Closing control_in pipe", interface_opts->name);
1026 FlushFileBuffers(interface_opts->extcap_control_in_h);
1027 DisconnectNamedPipe(interface_opts->extcap_control_in_h);
1028 CloseHandle(interface_opts->extcap_control_in_h);
1029 interface_opts->extcap_control_in_h = INVALID_HANDLE_VALUE;
1031 if (interface_opts->extcap_control_out_h != INVALID_HANDLE_VALUE)
1033 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
1034 "Extcap [%s] - Closing control_out pipe", interface_opts->name);
1035 FlushFileBuffers(interface_opts->extcap_control_out_h);
1036 DisconnectNamedPipe(interface_opts->extcap_control_out_h);
1037 CloseHandle(interface_opts->extcap_control_out_h);
1038 interface_opts->extcap_control_out_h = INVALID_HANDLE_VALUE;
1041 if (interface_opts->extcap_fifo != NULL && file_exists(interface_opts->extcap_fifo))
1043 /* the fifo will not be freed here, but with the other capture_opts in capture_sync */
1044 ws_unlink(interface_opts->extcap_fifo);
1045 interface_opts->extcap_fifo = NULL;
1047 if (interface_opts->extcap_control_in && file_exists(interface_opts->extcap_control_in))
1049 ws_unlink(interface_opts->extcap_control_in);
1050 interface_opts->extcap_control_in = NULL;
1052 if (interface_opts->extcap_control_out && file_exists(interface_opts->extcap_control_out))
1054 ws_unlink(interface_opts->extcap_control_out);
1055 interface_opts->extcap_control_out = NULL;
1058 /* Maybe the client closed and removed fifo, but ws should check if
1059 * pid should be closed */
1060 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
1061 "Extcap [%s] - Closing spawned PID: %d", interface_opts->name,
1062 interface_opts->extcap_pid);
1064 userdata = (extcap_userdata *) interface_opts->extcap_userdata;
1067 if (userdata->extcap_stderr_rd > 0 && pipe_data_available(userdata->extcap_stderr_rd))
1069 buffer = (gchar *)g_malloc0(sizeof(gchar) * STDERR_BUFFER_SIZE + 1);
1071 win32_readfrompipe((HANDLE)_get_osfhandle(userdata->extcap_stderr_rd), STDERR_BUFFER_SIZE, buffer);
1074 buffer_len = read(userdata->extcap_stderr_rd, buffer, sizeof(gchar) * STDERR_BUFFER_SIZE);
1075 if (buffer_len <= 0)
1081 buffer[buffer_len] = '\0';
1084 if (strlen(buffer) > 0)
1086 userdata->extcap_stderr = g_strdup_printf("%s", buffer);
1087 userdata->exitcode = 1;
1093 /* Final child watch may not have been called */
1094 if (interface_opts->extcap_child_watch != 0)
1096 extcap_child_watch_cb(userdata->pid, 0, capture_opts);
1097 /* it will have changed in extcap_child_watch_cb */
1098 interface_opts = &g_array_index(capture_opts->ifaces, interface_options,
1103 if (userdata->extcap_stderr != NULL)
1105 overwrite_exitcode = TRUE;
1108 if (overwrite_exitcode || userdata->exitcode != 0)
1110 if (userdata->extcap_stderr != 0)
1112 if (*errormsg == NULL)
1114 *errormsg = g_strdup_printf("Error by extcap pipe: %s", userdata->extcap_stderr);
1118 gchar *temp = g_strconcat(*errormsg, "\nError by extcap pipe: " , userdata->extcap_stderr, NULL);
1122 g_free(userdata->extcap_stderr);
1125 userdata->extcap_stderr = NULL;
1126 userdata->exitcode = 0;
1130 if (interface_opts->extcap_child_watch > 0)
1132 g_source_remove(interface_opts->extcap_child_watch);
1133 interface_opts->extcap_child_watch = 0;
1136 if (interface_opts->extcap_pid != INVALID_EXTCAP_PID)
1139 TerminateProcess(interface_opts->extcap_pid, 0);
1141 g_spawn_close_pid(interface_opts->extcap_pid);
1142 interface_opts->extcap_pid = INVALID_EXTCAP_PID;
1144 g_free(interface_opts->extcap_userdata);
1145 interface_opts->extcap_userdata = NULL;
1151 extcap_add_arg_and_remove_cb(gpointer key, gpointer value, gpointer data)
1153 GPtrArray *args = (GPtrArray *)data;
1157 g_ptr_array_add(args, g_strdup((const gchar *)key));
1161 g_ptr_array_add(args, g_strdup((const gchar *)value));
1170 void extcap_child_watch_cb(GPid pid, gint status, gpointer user_data)
1173 interface_options *interface_opts;
1174 extcap_userdata *userdata = NULL;
1175 capture_options *capture_opts = (capture_options *)(user_data);
1177 if (capture_opts == NULL || capture_opts->ifaces == NULL || capture_opts->ifaces->len == 0)
1182 /* Close handle to child process. */
1183 g_spawn_close_pid(pid);
1185 /* Update extcap_pid in interface options structure. */
1186 for (i = 0; i < capture_opts->ifaces->len; i++)
1188 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, i);
1189 if (interface_opts->extcap_pid == pid)
1191 userdata = (extcap_userdata *)interface_opts->extcap_userdata;
1192 if (userdata != NULL)
1194 interface_opts->extcap_pid = INVALID_EXTCAP_PID;
1195 userdata->exitcode = 0;
1197 if (WIFEXITED(status))
1199 if (WEXITSTATUS(status) != 0)
1201 userdata->exitcode = WEXITSTATUS(status);
1206 userdata->exitcode = G_SPAWN_ERROR_FAILED;
1211 userdata->exitcode = status;
1214 if (status == 0 && userdata->extcap_stderr != NULL)
1216 userdata->exitcode = 1;
1219 g_source_remove(interface_opts->extcap_child_watch);
1220 interface_opts->extcap_child_watch = 0;
1227 GPtrArray *extcap_prepare_arguments(interface_options *interface_opts)
1229 GPtrArray *result = NULL;
1231 if (interface_opts->if_type == IF_EXTCAP)
1233 result = g_ptr_array_new();
1235 #define add_arg(X) g_ptr_array_add(result, g_strdup(X))
1237 add_arg(interface_opts->extcap);
1238 add_arg(EXTCAP_ARGUMENT_RUN_CAPTURE);
1239 add_arg(EXTCAP_ARGUMENT_INTERFACE);
1240 add_arg(interface_opts->name);
1241 if (interface_opts->cfilter && strlen(interface_opts->cfilter) > 0)
1243 add_arg(EXTCAP_ARGUMENT_CAPTURE_FILTER);
1244 add_arg(interface_opts->cfilter);
1246 add_arg(EXTCAP_ARGUMENT_RUN_PIPE);
1247 add_arg(interface_opts->extcap_fifo);
1248 if (interface_opts->extcap_control_in)
1250 add_arg(EXTCAP_ARGUMENT_CONTROL_OUT);
1251 add_arg(interface_opts->extcap_control_in);
1253 if (interface_opts->extcap_control_out)
1255 add_arg(EXTCAP_ARGUMENT_CONTROL_IN);
1256 add_arg(interface_opts->extcap_control_out);
1258 if (interface_opts->extcap_args == NULL || g_hash_table_size(interface_opts->extcap_args) == 0)
1260 /* User did not perform interface configuration.
1262 * Check if there are any boolean flags that are set by default
1263 * and hence their argument should be added.
1268 arglist = extcap_get_if_configuration(interface_opts->name);
1269 for (elem = g_list_first(arglist); elem; elem = elem->next)
1272 extcap_arg *arg_iter;
1274 if (elem->data == NULL)
1279 arg_list = g_list_first((GList *)elem->data);
1280 while (arg_list != NULL)
1282 const gchar *stored = NULL;
1283 /* In case of boolflags only first element in arg_list is relevant. */
1284 arg_iter = (extcap_arg *)(arg_list->data);
1285 if (arg_iter->pref_valptr != NULL)
1287 stored = *arg_iter->pref_valptr;
1290 if (arg_iter->arg_type == EXTCAP_ARG_BOOLFLAG)
1292 if (extcap_complex_get_bool(arg_iter->default_complex))
1294 add_arg(arg_iter->call);
1296 else if (g_strcmp0(stored, "true") == 0)
1298 add_arg(arg_iter->call);
1303 if (stored && strlen(stored) > 0) {
1304 add_arg(arg_iter->call);
1309 arg_list = arg_list->next;
1313 extcap_free_if_configuration(arglist, TRUE);
1317 g_hash_table_foreach_remove(interface_opts->extcap_args, extcap_add_arg_and_remove_cb, result);
1327 /* call mkfifo for each extcap,
1328 * returns FALSE if there's an error creating a FIFO */
1330 extcap_init_interfaces(capture_options *capture_opts)
1333 interface_options *interface_opts;
1334 extcap_userdata *userdata;
1336 for (i = 0; i < capture_opts->ifaces->len; i++)
1338 GPtrArray *args = NULL;
1339 GPid pid = INVALID_EXTCAP_PID;
1341 interface_opts = &g_array_index(capture_opts->ifaces, interface_options, i);
1343 /* skip native interfaces */
1344 if (interface_opts->if_type != IF_EXTCAP)
1349 /* create control pipes if having toolbar */
1350 if (extcap_has_toolbar(interface_opts->name))
1352 extcap_create_pipe(interface_opts->name, &interface_opts->extcap_control_in,
1353 EXTCAP_CONTROL_IN_PREFIX, FALSE);
1355 interface_opts->extcap_control_in_h = pipe_h;
1357 extcap_create_pipe(interface_opts->name, &interface_opts->extcap_control_out,
1358 EXTCAP_CONTROL_OUT_PREFIX, FALSE);
1360 interface_opts->extcap_control_out_h = pipe_h;
1364 /* create pipe for fifo */
1365 if (!extcap_create_pipe(interface_opts->name, &interface_opts->extcap_fifo,
1366 EXTCAP_PIPE_PREFIX, TRUE))
1371 interface_opts->extcap_pipe_h = pipe_h;
1374 /* Create extcap call */
1375 args = extcap_prepare_arguments(interface_opts);
1377 userdata = g_new0(extcap_userdata, 1);
1379 pid = extcap_spawn_async(userdata, args);
1381 g_ptr_array_foreach(args, (GFunc)g_free, NULL);
1382 g_ptr_array_free(args, TRUE);
1384 if (pid == INVALID_EXTCAP_PID)
1390 interface_opts->extcap_pid = pid;
1392 interface_opts->extcap_child_watch =
1393 g_child_watch_add(pid, extcap_child_watch_cb, (gpointer)capture_opts);
1396 /* On Windows, wait for extcap to connect to named pipe.
1397 * Some extcaps will present UAC screen to user.
1398 * 30 second timeout should be reasonable timeout for extcap to
1399 * connect to named pipe (including user interaction).
1400 * Wait on multiple object in case of extcap termination
1401 * without opening pipe.
1403 * Minimum supported version of Windows: XP / Server 2003.
1405 if (pid != INVALID_EXTCAP_PID)
1407 HANDLE pipe_handles[3];
1408 int num_pipe_handles = 1;
1409 pipe_handles[0] = interface_opts->extcap_pipe_h;
1411 if (extcap_has_toolbar(interface_opts->name))
1413 pipe_handles[1] = interface_opts->extcap_control_in_h;
1414 pipe_handles[2] = interface_opts->extcap_control_out_h;
1415 num_pipe_handles += 2;
1418 extcap_wait_for_pipe(pipe_handles, num_pipe_handles, pid);
1422 interface_opts->extcap_userdata = (gpointer) userdata;
1428 gboolean extcap_create_pipe(const gchar *ifname, gchar **fifo, const gchar *pipe_prefix, gboolean byte_mode _U_)
1431 gchar timestr[ 14 + 1 ];
1432 time_t current_time;
1433 gchar *pipename = NULL;
1434 SECURITY_ATTRIBUTES security;
1436 /* create pipename */
1437 current_time = time(NULL);
1439 * XXX - we trust Windows not to return a time before the Epoch here,
1440 * so we won't get a null pointer back from localtime().
1442 strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S", localtime(¤t_time));
1443 pipename = g_strconcat("\\\\.\\pipe\\", pipe_prefix, "_", ifname, "_", timestr, NULL);
1445 /* Security struct to enable Inheritable HANDLE */
1446 memset(&security, 0, sizeof(SECURITY_ATTRIBUTES));
1447 security.nLength = sizeof(SECURITY_ATTRIBUTES);
1448 security.bInheritHandle = TRUE;
1449 security.lpSecurityDescriptor = NULL;
1451 /* create a namedPipe */
1452 pipe_h = CreateNamedPipe(
1453 utf_8to16(pipename),
1454 PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1455 byte_mode ? PIPE_TYPE_BYTE : PIPE_TYPE_MESSAGE | byte_mode ? PIPE_READMODE_BYTE : PIPE_READMODE_MESSAGE | PIPE_WAIT,
1460 if (pipe_h == INVALID_HANDLE_VALUE)
1462 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "\nError creating pipe => (%d)", GetLastError());
1468 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "\nWireshark Created pipe =>(%s)", pipename);
1469 *fifo = g_strdup(pipename);
1472 gchar *temp_name = NULL;
1475 gchar *pfx = g_strconcat(pipe_prefix, "_", ifname, NULL);
1476 if ((fd = create_tempfile(&temp_name, pfx, NULL)) < 0)
1485 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
1486 "Extcap - Creating fifo: %s", temp_name);
1488 if (file_exists(temp_name))
1490 ws_unlink(temp_name);
1493 if (mkfifo(temp_name, 0600) == 0)
1495 *fifo = g_strdup(temp_name);
1502 /************* EXTCAP LOAD INTERFACE LIST ***************
1504 * The following code handles loading and reloading the interface list. It is explicitly
1505 * kept separate from the rest
1510 extcap_free_interface_info(gpointer data)
1512 extcap_info *info = (extcap_info *)data;
1514 g_free(info->basename);
1515 g_free(info->full_path);
1516 g_free(info->version);
1519 extcap_free_interfaces(info->interfaces);
1524 static extcap_info *
1525 extcap_ensure_interface(const gchar * toolname, gboolean create_if_nonexist)
1527 extcap_info * element = 0;
1532 if ( ! _loaded_interfaces )
1533 _loaded_interfaces = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, extcap_free_interface);
1535 element = (extcap_info *) g_hash_table_lookup(_loaded_interfaces, toolname );
1536 if ( ! element && create_if_nonexist )
1538 g_hash_table_insert(_loaded_interfaces, g_strdup(toolname), g_new0(extcap_info, 1));
1539 element = (extcap_info *) g_hash_table_lookup(_loaded_interfaces, toolname );
1546 extcap_get_tool_by_ifname(const gchar *ifname)
1548 if ( ifname && _tool_for_ifname )
1550 gchar * toolname = (gchar *)g_hash_table_lookup(_tool_for_ifname, ifname);
1552 return extcap_ensure_interface(toolname, FALSE);
1559 extcap_get_tool_info(const gchar * toolname)
1561 return extcap_ensure_interface(toolname, FALSE);
1564 static void remove_extcap_entry(gpointer entry, gpointer data _U_)
1566 extcap_interface *int_iter = (extcap_interface*)entry;
1568 if (int_iter->if_type == EXTCAP_SENTENCE_EXTCAP)
1569 extcap_free_interface(entry);
1572 static gboolean cb_load_interfaces(extcap_callback_info_t cb_info)
1574 GList * interfaces = NULL, * control_items = NULL, * walker = NULL;
1575 extcap_interface * int_iter = NULL;
1576 extcap_info * element = NULL;
1577 iface_toolbar * toolbar_entry = NULL;
1578 gchar * toolname = g_path_get_basename(cb_info.extcap);
1580 GList * interface_keys = g_hash_table_get_keys(_loaded_interfaces);
1582 /* Load interfaces from utility */
1583 interfaces = extcap_parse_interfaces(cb_info.output, &control_items);
1587 toolbar_entry = g_new0(iface_toolbar, 1);
1588 toolbar_entry->controls = control_items;
1591 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Loading interface list for %s ", cb_info.extcap);
1593 /* Seems, that there where no interfaces to be loaded */
1594 if ( ! interfaces || g_list_length(interfaces) == 0 )
1596 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Cannot load interfaces for %s", cb_info.extcap );
1597 /* Some utilities, androiddump for example, may actually don't present any interfaces, even
1598 * if the utility itself is present. In such a case, we return here, but do not return
1599 * FALSE, or otherwise further loading of other utilities will be stopped */
1600 g_list_free(interface_keys);
1605 /* Load or create the storage element for the tool */
1606 element = extcap_ensure_interface(toolname, TRUE);
1607 if ( element == NULL )
1609 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_ERROR, "Cannot store interface %s, maybe duplicate?", cb_info.extcap );
1610 g_list_foreach(interfaces, remove_extcap_entry, NULL);
1611 g_list_free(interfaces);
1612 g_list_free(interface_keys);
1617 walker = interfaces;
1619 while (walker != NULL)
1621 int_iter = (extcap_interface *)walker->data;
1623 if (int_iter->call != NULL)
1624 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Interface found %s\n", int_iter->call);
1626 /* Help is not necessarily stored with the interface, but rather with the version string.
1627 * As the version string allways comes in front of the interfaces, this ensures, that it get's
1628 * properly stored with the interface */
1629 if (int_iter->if_type == EXTCAP_SENTENCE_EXTCAP)
1631 if (int_iter->call != NULL)
1632 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, " Extcap [%s] ", int_iter->call);
1634 /* Only initialize values if none are set. Need to check only one element here */
1635 if ( ! element->version )
1637 element->version = g_strdup(int_iter->version);
1638 element->basename = g_strdup(toolname);
1639 element->full_path = g_strdup(cb_info.extcap);
1640 element->help = g_strdup(int_iter->help);
1643 help = int_iter->help;
1646 toolbar_entry->menu_title = g_strdup(int_iter->display);
1647 toolbar_entry->help = g_strdup(int_iter->help);
1650 walker = g_list_next(walker);
1654 /* Only interface definitions will be parsed here. help is already set by the extcap element,
1655 * which makes it necessary to have version in the list before the interfaces. This is normally
1656 * the case by design, but could be changed by separating the information in extcap-base. */
1657 if ( int_iter->if_type == EXTCAP_SENTENCE_INTERFACE )
1659 if ( g_list_find(interface_keys, int_iter->call) )
1661 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_WARNING, "Extcap interface \"%s\" is already provided by \"%s\" ",
1662 int_iter->call, (gchar *)extcap_if_executable(int_iter->call));
1663 walker = g_list_next(walker);
1667 if ((int_iter->call != NULL) && (int_iter->display))
1668 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, " Interface [%s] \"%s\" ", int_iter->call, int_iter->display);
1670 int_iter->extcap_path = g_strdup(cb_info.extcap);
1672 /* Only set the help, if it exists and no parsed help information is present */
1673 if ( ! int_iter->help && help )
1674 int_iter->help = g_strdup(help);
1676 element->interfaces = g_list_append(element->interfaces, int_iter);
1677 g_hash_table_insert(_tool_for_ifname, g_strdup(int_iter->call), g_strdup(toolname));
1681 if (!toolbar_entry->menu_title)
1683 toolbar_entry->menu_title = g_strdup(int_iter->display);
1685 toolbar_entry->ifnames = g_list_append(toolbar_entry->ifnames, g_strdup(int_iter->call));
1689 walker = g_list_next(walker);
1692 if (toolbar_entry && toolbar_entry->menu_title)
1694 iface_toolbar_add(toolbar_entry);
1695 extcap_iface_toolbar_add(cb_info.extcap, toolbar_entry);
1698 g_list_foreach(interfaces, remove_extcap_entry, NULL);
1699 g_list_free(interfaces);
1700 g_list_free(interface_keys);
1706 /* Handles loading of the interfaces.
1708 * A list of interfaces can be obtained by calling \ref extcap_loaded_interfaces
1711 extcap_load_interface_list(void)
1718 // Remove existing interface toolbars here instead of in extcap_clear_interfaces()
1719 // to avoid flicker in shown toolbars when refreshing interfaces.
1720 GList *toolbar_list = g_hash_table_get_values (_toolbars);
1721 for (GList *walker = toolbar_list; walker; walker = walker->next)
1723 iface_toolbar *toolbar = (iface_toolbar *) walker->data;
1724 iface_toolbar_remove(toolbar->menu_title);
1726 g_hash_table_remove_all(_toolbars);
1728 _toolbars = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, extcap_free_toolbar);
1731 if (_loaded_interfaces == NULL)
1733 _loaded_interfaces = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, extcap_free_interface_info);
1734 /* Cleanup lookup table */
1735 if ( _tool_for_ifname )
1737 g_hash_table_remove_all(_tool_for_ifname);
1738 _tool_for_ifname = 0;
1740 _tool_for_ifname = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
1743 argv = g_strdup(EXTCAP_ARGUMENT_LIST_INTERFACES);
1745 extcap_callback_info_t cb_info;
1746 cb_info.data = NULL;
1747 cb_info.ifname = NULL;
1748 cb_info.err_str = &error;
1750 extcap_foreach(1, &argv, cb_load_interfaces, cb_info);
1757 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1762 * indent-tabs-mode: nil
1765 * vi: set shiftwidth=4 tabstop=8 expandtab:
1766 * :indentSize=4:tabSize=8:noTabs=true: