Qt: Add interface toolbar support
[metze/wireshark/wip.git] / extcap.c
1 /* extcap.c
2  *
3  * Routines for extcap external capture
4  * Copyright 2013, Mike Ryan <mikeryan@lacklustre.net>
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #include <config.h>
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #ifdef _WIN32
32 #include <windows.h>
33 #include <process.h>
34 #include <time.h>
35 #else
36 /* Include for unlink */
37 #include <unistd.h>
38 #endif
39
40 #ifdef HAVE_SYS_TYPES_H
41 #include <sys/types.h>
42 #endif
43 #ifdef HAVE_SYS_WAIT_H
44 #include <sys/wait.h>
45 #endif
46
47 #include <glib.h>
48 #include <log.h>
49
50 #include <epan/prefs.h>
51
52 #include "ui/iface_toolbar.h"
53
54 #include <wsutil/file_util.h>
55 #include <wsutil/filesystem.h>
56 #include <wsutil/tempfile.h>
57
58 #include "capture_opts.h"
59
60 #include "extcap.h"
61 #include "extcap_parser.h"
62 #include "extcap_spawn.h"
63
64 #ifdef _WIN32
65 static HANDLE pipe_h = NULL;
66 #endif
67
68 static void extcap_child_watch_cb(GPid pid, gint status, gpointer user_data);
69
70 /* internal container, for all the extcap executables that have been found.
71  * Will be resetted if extcap_clear_interfaces() is being explicitly called
72  * and is being used for printing information about all extcap interfaces found,
73  * as well as storing all sub-interfaces
74  */
75 static GHashTable * _loaded_interfaces = NULL;
76
77 /* Internal container, which maps each ifname to the tool providing it, for faster
78  * lookup.
79  */
80 static GHashTable * _tool_for_ifname = NULL;
81
82 /* internal container, for all the extcap executables that have been found
83  * and that provides a toolbar with controls to be added to a Interface Toolbar
84  */
85 static GHashTable *_toolbars = NULL;
86
87 /* internal container, to map preference names to pointers that hold preference
88  * values. These ensure that preferences can survive extcap if garbage
89  * collection, and does not lead to dangling pointers in the prefs subsystem.
90  */
91 static GHashTable *extcap_prefs_dynamic_vals = NULL;
92
93 typedef struct _extcap_callback_info_t
94 {
95     const gchar * extcap;
96     const gchar * ifname;
97     gchar * output;
98     void * data;
99     gchar ** err_str;
100 } extcap_callback_info_t;
101
102 /* Callback definition for extcap_foreach */
103 typedef gboolean(*extcap_cb_t)(extcap_callback_info_t info_structure);
104
105 static void extcap_load_interface_list(void);
106
107 GHashTable *
108 extcap_loaded_interfaces(void)
109 {
110     if ( !_loaded_interfaces || g_hash_table_size(_loaded_interfaces) == 0 )
111         extcap_load_interface_list();
112
113     return _loaded_interfaces;
114 }
115
116 void
117 extcap_clear_interfaces(void)
118 {
119     if ( _loaded_interfaces )
120         g_hash_table_destroy(_loaded_interfaces);
121     _loaded_interfaces = NULL;
122
123     if ( _tool_for_ifname )
124         g_hash_table_destroy(_tool_for_ifname);
125     _tool_for_ifname = NULL;
126 }
127
128 guint extcap_count(void)
129 {
130     const char *dirname = get_extcap_dir();
131     GDir *dir;
132     guint count;
133
134     count = 0;
135
136     if ((dir = g_dir_open(dirname, 0, NULL)) != NULL)
137     {
138         GString *extcap_path = NULL;
139         const gchar *file;
140
141         extcap_path = g_string_new("");
142         while ((file = g_dir_read_name(dir)) != NULL)
143         {
144             /* full path to extcap binary */
145             g_string_printf(extcap_path, "%s" G_DIR_SEPARATOR_S "%s", dirname, file);
146             /* treat anything executable as an extcap binary */
147             if (g_file_test(extcap_path->str, G_FILE_TEST_IS_REGULAR) &&
148                 g_file_test(extcap_path->str, G_FILE_TEST_IS_EXECUTABLE))
149             {
150                 count++;
151             }
152         }
153
154         g_dir_close(dir);
155         g_string_free(extcap_path, TRUE);
156     }
157     return count;
158 }
159
160 static gboolean
161 extcap_if_exists(const gchar *ifname)
162 {
163     if (!ifname || !_tool_for_ifname)
164     {
165         return FALSE;
166     }
167
168     if (g_hash_table_lookup(_tool_for_ifname, ifname))
169     {
170         return TRUE;
171     }
172
173     return FALSE;
174 }
175
176 static extcap_interface *
177 extcap_find_interface_for_ifname(const gchar *ifname)
178 {
179     extcap_interface * result = NULL;
180
181     if ( !ifname || ! _tool_for_ifname || ! _loaded_interfaces )
182         return result;
183
184     gchar * extcap_util = (gchar *)g_hash_table_lookup(_tool_for_ifname, ifname);
185     if ( ! extcap_util )
186         return result;
187
188     extcap_info * element = (extcap_info *)g_hash_table_lookup(_loaded_interfaces, extcap_util);
189     if ( ! element )
190         return result;
191
192     GList * walker = element->interfaces;
193     while ( walker && walker->data && ! result )
194     {
195         extcap_interface * interface = (extcap_interface *)walker->data;
196         if ( g_strcmp0(interface->call, ifname) == 0 )
197         {
198             result = interface;
199             break;
200         }
201
202         walker = g_list_next ( walker );
203     }
204
205     return result;
206 }
207
208 static void
209 extcap_free_toolbar_value(iface_toolbar_value *value)
210 {
211     if (!value)
212     {
213         return;
214     }
215
216     g_free(value->value);
217     g_free(value->display);
218     g_free(value);
219 }
220
221 static void
222 extcap_free_toolbar_control(iface_toolbar_control *control)
223 {
224     if (!control)
225     {
226         return;
227     }
228
229     g_free(control->display);
230     g_free(control->validation);
231     g_free(control->tooltip);
232     if (control->ctrl_type == INTERFACE_TYPE_STRING) {
233         g_free(control->default_value.string);
234     }
235     g_list_foreach(control->values, (GFunc)extcap_free_toolbar_value, NULL);
236     g_list_free(control->values);
237     g_free(control);
238 }
239
240 static void
241 extcap_free_toolbar(gpointer data)
242 {
243     if (!data)
244     {
245         return;
246     }
247
248     iface_toolbar *toolbar = (iface_toolbar *)data;
249
250     g_free(toolbar->menu_title);
251     g_free(toolbar->help);
252     g_list_free_full(toolbar->ifnames, g_free);
253     g_list_foreach(toolbar->controls, (GFunc)extcap_free_toolbar_control, NULL);
254     g_list_free(toolbar->controls);
255     g_free(toolbar);
256 }
257
258 static gboolean
259 extcap_if_exists_for_extcap(const gchar *ifname, const gchar *extcap)
260 {
261     extcap_interface *entry = extcap_find_interface_for_ifname(ifname);
262
263     if (entry && strcmp(entry->extcap_path, extcap) == 0)
264     {
265         return TRUE;
266     }
267
268     return FALSE;
269 }
270
271 static gchar *
272 extcap_if_executable(const gchar *ifname)
273 {
274     extcap_interface *interface = extcap_find_interface_for_ifname(ifname);
275     return interface != NULL ? interface->extcap_path : NULL;
276 }
277
278 static void
279 extcap_iface_toolbar_add(const gchar *extcap, iface_toolbar *toolbar_entry)
280 {
281     char *toolname;
282
283     if (!extcap || !toolbar_entry)
284     {
285         return;
286     }
287
288     toolname = g_path_get_basename(extcap);
289
290     if (!g_hash_table_lookup(_toolbars, toolname))
291     {
292         g_hash_table_insert(_toolbars, g_strdup(toolname), toolbar_entry);
293     }
294
295     g_free(toolname);
296 }
297
298 /* Note: args does not need to be NULL-terminated. */
299 static gboolean extcap_foreach(gint argc, gchar **args,
300                                       extcap_cb_t cb, extcap_callback_info_t cb_info)
301 {
302     GDir *dir;
303     gboolean keep_going;
304     const char *dirname = get_extcap_dir();
305
306     keep_going = TRUE;
307
308     if ((dir = g_dir_open(dirname, 0, NULL)) != NULL)
309     {
310         GString *extcap_path = NULL;
311         const gchar *file;
312
313         extcap_path = g_string_new("");
314         while (keep_going && (file = g_dir_read_name(dir)) != NULL)
315         {
316             gchar *command_output = NULL;
317
318             /* full path to extcap binary */
319             g_string_printf(extcap_path, "%s" G_DIR_SEPARATOR_S "%s", dirname, file);
320             /* treat anything executable as an extcap binary */
321             if (g_file_test(extcap_path->str, G_FILE_TEST_IS_REGULAR) &&
322                 g_file_test(extcap_path->str, G_FILE_TEST_IS_EXECUTABLE))
323             {
324                 if (extcap_if_exists(cb_info.ifname) && !extcap_if_exists_for_extcap(cb_info.ifname, extcap_path->str))
325                 {
326                     continue;
327                 }
328
329                 if (extcap_spawn_sync((gchar *) dirname, extcap_path->str, argc, args, &command_output))
330                 {
331                     cb_info.output = command_output;
332                     cb_info.extcap = extcap_path->str;
333
334                     keep_going = cb(cb_info);
335                 }
336
337                 g_free(command_output);
338             }
339         }
340
341         g_dir_close(dir);
342         g_string_free(extcap_path, TRUE);
343     }
344
345     return keep_going;
346 }
347
348 static void extcap_free_dlt(gpointer d, gpointer user_data _U_)
349 {
350     if (d == NULL)
351     {
352         return;
353     }
354
355     g_free(((extcap_dlt *)d)->name);
356     g_free(((extcap_dlt *)d)->display);
357     g_free(d);
358 }
359
360 static void extcap_free_dlts(GList *dlts)
361 {
362     g_list_foreach(dlts, extcap_free_dlt, NULL);
363     g_list_free(dlts);
364 }
365
366 static gboolean cb_dlt(extcap_callback_info_t cb_info)
367 {
368     GList *dlts = NULL, *temp = NULL;
369
370     if_capabilities_t *caps;
371     GList *linktype_list = NULL;
372     data_link_info_t *data_link_info;
373     extcap_dlt *dlt_item;
374
375     dlts = extcap_parse_dlts(cb_info.output);
376     temp = dlts;
377
378     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap pipe %s ", cb_info.extcap);
379
380     /*
381      * Allocate the interface capabilities structure.
382      */
383     caps = (if_capabilities_t *) g_malloc(sizeof * caps);
384     caps->can_set_rfmon = FALSE;
385
386     while (dlts)
387     {
388         dlt_item = (extcap_dlt *)dlts->data;
389         if (dlt_item)
390         {
391             g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
392                   "  DLT %d name=\"%s\" display=\"%s\" ", dlt_item->number,
393                   dlt_item->name, dlt_item->display);
394
395             data_link_info = g_new(data_link_info_t, 1);
396             data_link_info->dlt = dlt_item->number;
397             data_link_info->name = g_strdup(dlt_item->name);
398             data_link_info->description = g_strdup(dlt_item->display);
399             linktype_list = g_list_append(linktype_list, data_link_info);
400         }
401
402         dlts = g_list_next(dlts);
403     }
404
405     /* Check to see if we built a list */
406     if (linktype_list != NULL && cb_info.data != NULL)
407     {
408         caps->data_link_types = linktype_list;
409         *(if_capabilities_t **) cb_info.data = caps;
410     }
411     else
412     {
413         if (cb_info.err_str)
414         {
415             g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "  returned no DLTs");
416             *(cb_info.err_str) = g_strdup("Extcap returned no DLTs");
417         }
418         g_free(caps);
419     }
420
421     extcap_free_dlts(temp);
422
423     return FALSE;
424 }
425
426 if_capabilities_t *
427 extcap_get_if_dlts(const gchar *ifname, char **err_str)
428 {
429     gchar *argv[3];
430     gint i;
431     if_capabilities_t *caps = NULL;
432
433     if (err_str != NULL)
434     {
435         *err_str = NULL;
436     }
437
438     if (extcap_if_exists(ifname))
439     {
440         argv[0] = g_strdup(EXTCAP_ARGUMENT_LIST_DLTS);
441         argv[1] = g_strdup(EXTCAP_ARGUMENT_INTERFACE);
442         argv[2] = g_strdup(ifname);
443
444         extcap_callback_info_t cb_info;
445         cb_info.data = &caps;
446         cb_info.err_str = err_str;
447         cb_info.ifname = ifname;
448
449         extcap_foreach(3, argv, cb_dlt, cb_info);
450
451         for (i = 0; i < 3; ++i)
452         {
453             g_free(argv[i]);
454         }
455     }
456
457     return caps;
458 }
459
460 static void extcap_free_interface(gpointer i)
461 {
462
463     extcap_interface *interface = (extcap_interface *)i;
464
465     if (i == NULL)
466     {
467         return;
468     }
469
470     g_free(interface->call);
471     g_free(interface->display);
472     g_free(interface->version);
473     g_free(interface->help);
474     g_free(interface->extcap_path);
475     g_free(interface);
476 }
477
478 static void extcap_free_interfaces(GList *interfaces)
479 {
480     if (interfaces == NULL)
481     {
482         return;
483     }
484
485     g_list_foreach(interfaces, (GFunc)extcap_free_interface, NULL);
486     g_list_free(interfaces);
487 }
488
489 static gint
490 if_info_compare(gconstpointer a, gconstpointer b)
491 {
492     gint comp = 0;
493     const if_info_t *if_a = (const if_info_t *)a;
494     const if_info_t *if_b = (const if_info_t *)b;
495
496     if ((comp = g_strcmp0(if_a->name, if_b->name)) == 0)
497     {
498         return g_strcmp0(if_a->friendly_name, if_b->friendly_name);
499     }
500
501     return comp;
502 }
503
504 gchar *
505 extcap_get_help_for_ifname(const char *ifname)
506 {
507     extcap_interface *interface = extcap_find_interface_for_ifname(ifname);
508     return interface != NULL ? interface->help : NULL;
509 }
510
511 GList *
512 append_extcap_interface_list(GList *list, char **err_str _U_)
513 {
514     GList *interface_list = NULL;
515     extcap_interface *data = NULL;
516     GList * ifutilkeys = NULL;
517
518     /* Update the extcap interfaces and get a list of their if_infos */
519     if ( !_loaded_interfaces || g_hash_table_size(_loaded_interfaces) == 0 )
520         extcap_load_interface_list();
521
522     ifutilkeys = g_hash_table_get_keys(_loaded_interfaces);
523     while ( ifutilkeys && ifutilkeys->data )
524     {
525         extcap_info * extinfo =
526                 (extcap_info *) g_hash_table_lookup(_loaded_interfaces, (gchar *)ifutilkeys->data);
527         GList * walker = extinfo->interfaces;
528         while ( walker && walker->data )
529         {
530             interface_list = g_list_append(interface_list, walker->data);
531             walker = g_list_next(walker);
532         }
533
534         ifutilkeys = g_list_next(ifutilkeys);
535     }
536
537     /* Sort that list */
538     interface_list = g_list_sort(interface_list, if_info_compare);
539
540     /* Append the interfaces in that list to the list we're handed. */
541     while (interface_list != NULL)
542     {
543         GList *entry = g_list_first(interface_list);
544         data = (extcap_interface *)entry->data;
545         interface_list = g_list_delete_link(interface_list, entry);
546
547         if_info_t * if_info = g_new0(if_info_t, 1);
548         if_info->name = g_strdup(data->call);
549         if_info->friendly_name = g_strdup(data->display);
550
551         if_info->type = IF_EXTCAP;
552
553         if_info->extcap = g_strdup(data->extcap_path);
554
555         list = g_list_append(list, if_info);
556     }
557
558     return list;
559 }
560
561 static void
562 extcap_register_preferences_callback(gpointer key, gpointer value _U_, gpointer user_data _U_)
563 {
564     GList *arguments;
565
566     arguments = extcap_get_if_configuration((gchar *)key);
567     /* Memory for prefs are external to an interface, they are part of
568      * extcap core, so the parsed arguments can be freed. */
569     extcap_free_if_configuration(arguments, TRUE);
570 }
571
572 void extcap_register_preferences(void)
573 {
574     module_t *dev_module = prefs_find_module("extcap");
575
576     if (!dev_module)
577     {
578         return;
579     }
580
581     if ( !_loaded_interfaces || g_hash_table_size(_loaded_interfaces) == 0 )
582         extcap_load_interface_list();
583
584
585     g_hash_table_foreach(_tool_for_ifname, extcap_register_preferences_callback, NULL);
586 }
587
588 /**
589  * Releases the dynamic preference value pointers. Must not be called before
590  * prefs_cleanup since these pointers could still be in use.
591  */
592 void extcap_cleanup(void)
593 {
594     if (extcap_prefs_dynamic_vals)
595         g_hash_table_destroy(extcap_prefs_dynamic_vals);
596
597     if (_loaded_interfaces)
598         g_hash_table_destroy(_loaded_interfaces);
599
600     if (_tool_for_ifname)
601         g_hash_table_destroy(_tool_for_ifname);
602 }
603
604 void extcap_pref_store(extcap_arg *arg, const char *newval)
605 {
606     if (arg && arg->pref_valptr != NULL)
607     {
608         g_free(*arg->pref_valptr);
609         *arg->pref_valptr = g_strdup(newval);
610     }
611
612 }
613
614 /**
615  * Obtains a pointer which can store a value for the given preference name.
616  * The preference name that can be passed to the prefs API is stored into
617  * 'prefs_name'.
618  *
619  * Extcap interfaces (and their preferences) are dynamic, they can be created
620  * and destroyed at will. Thus their data structures are insufficient to pass to
621  * the preferences APIs which require pointers which are valid until the
622  * preferences are removed (at exit).
623  */
624 static gchar **extcap_prefs_dynamic_valptr(const char *name, char **pref_name)
625 {
626     gchar **valp;
627     if (!extcap_prefs_dynamic_vals)
628     {
629         /* Initialize table only as needed, most preferences are not dynamic */
630         extcap_prefs_dynamic_vals = g_hash_table_new_full(g_str_hash, g_str_equal,
631                                     g_free, g_free);
632     }
633     if (!g_hash_table_lookup_extended(extcap_prefs_dynamic_vals, name,
634                                       (gpointer *)pref_name, (gpointer *)&valp))
635     {
636         /* New dynamic pref, allocate, initialize and store. */
637         valp = g_new0(gchar *, 1);
638         *pref_name = g_strdup(name);
639         g_hash_table_insert(extcap_prefs_dynamic_vals, *pref_name, valp);
640     }
641     return valp;
642 }
643
644 void extcap_free_if_configuration(GList *list, gboolean free_args)
645 {
646     GList *elem, *sl;
647
648     for (elem = g_list_first(list); elem; elem = elem->next)
649     {
650         if (elem->data != NULL)
651         {
652             sl = g_list_first((GList *)elem->data);
653             if (free_args)
654             {
655                 extcap_free_arg_list(sl);
656             }
657             else
658             {
659                 g_list_free(sl);
660             }
661         }
662     }
663     g_list_free(list);
664 }
665
666 struct preference *
667 extcap_pref_for_argument(const gchar *ifname, struct _extcap_arg *arg)
668 {
669     struct preference *pref = NULL;
670
671     GRegex *regex_name = g_regex_new("[-]+", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, NULL);
672     GRegex *regex_ifname = g_regex_new("(?![a-zA-Z1-9_]).", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, NULL);
673     if (regex_name && regex_ifname)
674     {
675         if (prefs_find_module("extcap"))
676         {
677             gchar *pref_name = g_regex_replace(regex_name, arg->call, strlen(arg->call), 0, "", (GRegexMatchFlags) 0, NULL);
678             gchar *ifname_underscore = g_regex_replace(regex_ifname, ifname, strlen(ifname), 0, "_", (GRegexMatchFlags) 0, NULL);
679             gchar *ifname_lowercase = g_ascii_strdown(ifname_underscore, -1);
680             gchar *pref_ifname = g_strconcat(ifname_lowercase, ".", pref_name, NULL);
681
682             pref = prefs_find_preference(prefs_find_module("extcap"), pref_ifname);
683
684             g_free(pref_name);
685             g_free(ifname_underscore);
686             g_free(ifname_lowercase);
687             g_free(pref_ifname);
688         }
689     }
690     if (regex_name)
691     {
692         g_regex_unref(regex_name);
693     }
694     if (regex_ifname)
695     {
696         g_regex_unref(regex_ifname);
697     }
698
699     return pref;
700 }
701
702 static gboolean cb_preference(extcap_callback_info_t cb_info)
703 {
704     GList *arguments = NULL;
705     GList **il = (GList **) cb_info.data;
706     module_t *dev_module = NULL;
707
708     arguments = extcap_parse_args(cb_info.output);
709
710     dev_module = prefs_find_module("extcap");
711
712     if (dev_module)
713     {
714         GList *walker = arguments;
715
716         GRegex *regex_name = g_regex_new("[-]+", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, NULL);
717         GRegex *regex_ifname = g_regex_new("(?![a-zA-Z1-9_]).", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, NULL);
718         if (regex_name && regex_ifname)
719         {
720             while (walker != NULL)
721             {
722                 extcap_arg *arg = (extcap_arg *)walker->data;
723                 arg->device_name = g_strdup(cb_info.ifname);
724
725                 if (arg->save)
726                 {
727                     struct preference *pref = NULL;
728
729                     gchar *pref_name = g_regex_replace(regex_name, arg->call, strlen(arg->call), 0, "", (GRegexMatchFlags) 0, NULL);
730                     gchar *ifname_underscore = g_regex_replace(regex_ifname, cb_info.ifname, strlen(cb_info.ifname), 0, "_", (GRegexMatchFlags) 0, NULL);
731                     gchar *ifname_lowercase = g_ascii_strdown(ifname_underscore, -1);
732                     gchar *pref_ifname = g_strconcat(ifname_lowercase, ".", pref_name, NULL);
733
734                     if ((pref = prefs_find_preference(dev_module, pref_ifname)) == NULL)
735                     {
736                         char *pref_name_for_prefs;
737                         char *pref_title = wmem_strdup(wmem_epan_scope(), arg->display);
738
739                         arg->pref_valptr = extcap_prefs_dynamic_valptr(pref_ifname, &pref_name_for_prefs);
740                         /* Set an initial value if any (the string will be copied at registration) */
741                         if (arg->default_complex)
742                         {
743                             *arg->pref_valptr = arg->default_complex->_val;
744                         }
745
746                         prefs_register_string_preference(dev_module, pref_name_for_prefs,
747                                                          pref_title, pref_title, (const char **)arg->pref_valptr);
748                     }
749                     else
750                     {
751                         /* Been here before, restore stored value */
752                         if (arg->pref_valptr == NULL)
753                         {
754                             arg->pref_valptr = (gchar**)g_hash_table_lookup(extcap_prefs_dynamic_vals, pref_ifname);
755                         }
756                     }
757
758                     g_free(pref_name);
759                     g_free(ifname_underscore);
760                     g_free(ifname_lowercase);
761                     g_free(pref_ifname);
762                 }
763
764                 walker = g_list_next(walker);
765             }
766         }
767         if (regex_name)
768         {
769             g_regex_unref(regex_name);
770         }
771         if (regex_ifname)
772         {
773             g_regex_unref(regex_ifname);
774         }
775     }
776
777     *il = g_list_append(*il, arguments);
778
779     /* By returning false, extcap_foreach will break on first found */
780     return TRUE;
781 }
782
783 GList *
784 extcap_get_if_configuration(const char *ifname)
785 {
786     gchar *argv[3];
787     GList *ret = NULL;
788     gchar **err_str = NULL;
789     int i;
790
791     if (extcap_if_exists(ifname))
792     {
793         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap path %s",
794               get_extcap_dir());
795
796         argv[0] = g_strdup(EXTCAP_ARGUMENT_CONFIG);
797         argv[1] = g_strdup(EXTCAP_ARGUMENT_INTERFACE);
798         argv[2] = g_strdup(ifname);
799
800         extcap_callback_info_t cb_info;
801         cb_info.data = &ret;
802         cb_info.err_str = err_str;
803         cb_info.ifname = ifname;
804
805         extcap_foreach(3, argv, cb_preference, cb_info);
806
807         for (i = 0; i < 3; i++)
808         {
809             g_free(argv[i]);
810         }
811     }
812
813     return ret;
814 }
815
816 /**
817  * If is_required is FALSE: returns TRUE if the extcap interface has
818  * configurable options.
819  * If is_required is TRUE: returns TRUE when the extcap interface has
820  * configurable options that required modification. (For example, when an
821  * argument is required but empty.)
822  */
823 gboolean
824 extcap_has_configuration(const char *ifname, gboolean is_required)
825 {
826     GList *arguments = 0;
827     GList *walker = 0, * item = 0;
828
829     gboolean found = FALSE;
830
831     arguments = extcap_get_if_configuration((const char *)(ifname));
832     walker = g_list_first(arguments);
833
834     while (walker != NULL && !found)
835     {
836         item = g_list_first((GList *)(walker->data));
837         while (item != NULL && !found)
838         {
839             if ((extcap_arg *)(item->data) != NULL)
840             {
841                 extcap_arg *arg = (extcap_arg *)(item->data);
842                 /* Should required options be present, or any kind of options */
843                 if (!is_required)
844                 {
845                     found = TRUE;
846                 }
847                 else if (arg->is_required)
848                 {
849                     const gchar *stored = NULL;
850                     const gchar *defval = NULL;
851
852                     if (arg->pref_valptr != NULL)
853                     {
854                         stored = *arg->pref_valptr;
855                     }
856
857                     if (arg->default_complex != NULL && arg->default_complex->_val != NULL)
858                     {
859                         defval = arg->default_complex->_val;
860                     }
861
862                     if (arg->is_required)
863                     {
864                         /* If stored and defval is identical and the argument is required,
865                          * configuration is needed */
866                         if (defval && stored && g_strcmp0(stored, defval) == 0)
867                         {
868                             found = TRUE;
869                         }
870                         else if (!defval && (!stored || !*stored))
871                         {
872                             found = TRUE;
873                         }
874                     }
875
876                     if (arg->arg_type == EXTCAP_ARG_FILESELECT)
877                     {
878                         if (arg->fileexists && !(file_exists(defval) || file_exists(stored)))
879                         {
880                             found = TRUE;
881                         }
882                     }
883                 }
884             }
885
886             item = item->next;
887         }
888         walker = walker->next;
889     }
890     extcap_free_if_configuration(arguments, TRUE);
891
892     return found;
893 }
894
895 gboolean
896 extcap_has_toolbar(const char *ifname)
897 {
898     if (!iface_toolbar_use())
899     {
900         return FALSE;
901     }
902
903     GList *toolbar_list = g_hash_table_get_values (_toolbars);
904     for (GList *walker = toolbar_list; walker; walker = walker->next)
905     {
906         iface_toolbar *toolbar = (iface_toolbar *) walker->data;
907         if (g_list_find_custom(toolbar->ifnames, ifname, (GCompareFunc) strcmp))
908         {
909             return TRUE;
910         }
911     }
912
913     return FALSE;
914 }
915
916 /* taken from capchild/capture_sync.c */
917 static gboolean pipe_data_available(int pipe_fd)
918 {
919 #ifdef _WIN32 /* PeekNamedPipe */
920     HANDLE hPipe = (HANDLE) _get_osfhandle(pipe_fd);
921     DWORD bytes_avail;
922
923     if (hPipe == INVALID_HANDLE_VALUE)
924     {
925         return FALSE;
926     }
927
928     if (! PeekNamedPipe(hPipe, NULL, 0, NULL, &bytes_avail, NULL))
929     {
930         return FALSE;
931     }
932
933     if (bytes_avail > 0)
934     {
935         return TRUE;
936     }
937     return FALSE;
938 #else /* select */
939     fd_set rfds;
940     struct timeval timeout;
941
942     FD_ZERO(&rfds);
943     FD_SET(pipe_fd, &rfds);
944     timeout.tv_sec = 0;
945     timeout.tv_usec = 0;
946
947     if (select(pipe_fd + 1, &rfds, NULL, NULL, &timeout) > 0)
948     {
949         return TRUE;
950     }
951
952     return FALSE;
953 #endif
954 }
955
956 void extcap_if_cleanup(capture_options *capture_opts, gchar **errormsg)
957 {
958     interface_options interface_opts;
959     extcap_userdata *userdata;
960     guint icnt = 0;
961     gboolean overwrite_exitcode;
962     gchar *buffer;
963 #define STDERR_BUFFER_SIZE 1024
964
965     for (icnt = 0; icnt < capture_opts->ifaces->len; icnt++)
966     {
967         interface_opts = g_array_index(capture_opts->ifaces, interface_options,
968                                        icnt);
969
970         /* skip native interfaces */
971         if (interface_opts.if_type != IF_EXTCAP)
972         {
973             continue;
974         }
975
976         overwrite_exitcode = FALSE;
977
978         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
979               "Extcap [%s] - Cleaning up fifo: %s; PID: %d", interface_opts.name,
980               interface_opts.extcap_fifo, interface_opts.extcap_pid);
981 #ifdef _WIN32
982         if (pipe_h)
983         {
984             g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
985                   "Extcap [%s] - Closing pipe", interface_opts.name);
986             FlushFileBuffers(pipe_h);
987             DisconnectNamedPipe(pipe_h);
988             CloseHandle(pipe_h);
989         }
990 #else
991         if (interface_opts.extcap_fifo != NULL && file_exists(interface_opts.extcap_fifo))
992         {
993             /* the fifo will not be freed here, but with the other capture_opts in capture_sync */
994             ws_unlink(interface_opts.extcap_fifo);
995             interface_opts.extcap_fifo = NULL;
996         }
997         if (interface_opts.extcap_control_in && file_exists(interface_opts.extcap_control_in))
998         {
999             ws_unlink(interface_opts.extcap_control_in);
1000             interface_opts.extcap_control_in = NULL;
1001         }
1002         if (interface_opts.extcap_control_out && file_exists(interface_opts.extcap_control_out))
1003         {
1004             ws_unlink(interface_opts.extcap_control_out);
1005             interface_opts.extcap_control_out = NULL;
1006         }
1007 #endif
1008         /* Maybe the client closed and removed fifo, but ws should check if
1009          * pid should be closed */
1010         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
1011               "Extcap [%s] - Closing spawned PID: %d", interface_opts.name,
1012               interface_opts.extcap_pid);
1013
1014         userdata = (extcap_userdata *) interface_opts.extcap_userdata;
1015         if (userdata)
1016         {
1017             if (userdata->extcap_stderr_rd > 0 && pipe_data_available(userdata->extcap_stderr_rd))
1018             {
1019                 buffer = (gchar *)g_malloc0(sizeof(gchar) * STDERR_BUFFER_SIZE + 1);
1020 #ifdef _WIN32
1021                 win32_readfrompipe((HANDLE)_get_osfhandle(userdata->extcap_stderr_rd), STDERR_BUFFER_SIZE, buffer);
1022 #else
1023                 if (read(userdata->extcap_stderr_rd, buffer, sizeof(gchar) * STDERR_BUFFER_SIZE) <= 0)
1024                 {
1025                     buffer[0] = '\0';
1026                 }
1027 #endif
1028                 if (strlen(buffer) > 0)
1029                 {
1030                     userdata->extcap_stderr = g_strdup_printf("%s", buffer);
1031                     userdata->exitcode = 1;
1032                 }
1033                 g_free(buffer);
1034             }
1035
1036 #ifndef _WIN32
1037             /* Final child watch may not have been called */
1038             if (interface_opts.extcap_child_watch != 0)
1039             {
1040                 extcap_child_watch_cb(userdata->pid, 0, capture_opts);
1041                 /* it will have changed in extcap_child_watch_cb */
1042                 interface_opts = g_array_index(capture_opts->ifaces, interface_options,
1043                                                icnt);
1044             }
1045 #endif
1046
1047             if (userdata->extcap_stderr != NULL)
1048             {
1049                 overwrite_exitcode = TRUE;
1050             }
1051
1052             if (overwrite_exitcode || userdata->exitcode != 0)
1053             {
1054                 if (userdata->extcap_stderr != 0)
1055                 {
1056                     if (*errormsg == NULL)
1057                     {
1058                         *errormsg = g_strdup_printf("Error by extcap pipe: %s", userdata->extcap_stderr);
1059                     }
1060                     else
1061                     {
1062                         gchar *temp = g_strconcat(*errormsg, "\nError by extcap pipe: " , userdata->extcap_stderr, NULL);
1063                         g_free(*errormsg);
1064                         *errormsg = temp;
1065                     }
1066                     g_free(userdata->extcap_stderr);
1067                 }
1068
1069                 userdata->extcap_stderr = NULL;
1070                 userdata->exitcode = 0;
1071             }
1072         }
1073
1074         if (interface_opts.extcap_child_watch > 0)
1075         {
1076             g_source_remove(interface_opts.extcap_child_watch);
1077             interface_opts.extcap_child_watch = 0;
1078         }
1079
1080         if (interface_opts.extcap_pid != INVALID_EXTCAP_PID)
1081         {
1082 #ifdef _WIN32
1083             TerminateProcess(interface_opts.extcap_pid, 0);
1084 #endif
1085             g_spawn_close_pid(interface_opts.extcap_pid);
1086             interface_opts.extcap_pid = INVALID_EXTCAP_PID;
1087
1088             g_free(interface_opts.extcap_userdata);
1089             interface_opts.extcap_userdata = NULL;
1090         }
1091
1092         /* Make sure modified interface_opts is saved in capture_opts. */
1093         capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, icnt);
1094         g_array_insert_val(capture_opts->ifaces, icnt, interface_opts);
1095     }
1096 }
1097
1098 static gboolean
1099 extcap_add_arg_and_remove_cb(gpointer key, gpointer value, gpointer data)
1100 {
1101     GPtrArray *args = (GPtrArray *)data;
1102
1103     if (key != NULL)
1104     {
1105         g_ptr_array_add(args, g_strdup((const gchar *)key));
1106
1107         if (value != NULL)
1108         {
1109             g_ptr_array_add(args, g_strdup((const gchar *)value));
1110         }
1111
1112         return TRUE;
1113     }
1114
1115     return FALSE;
1116 }
1117
1118 void extcap_child_watch_cb(GPid pid, gint status, gpointer user_data)
1119 {
1120     guint i;
1121     interface_options interface_opts;
1122     extcap_userdata *userdata = NULL;
1123     capture_options *capture_opts = (capture_options *)(user_data);
1124
1125     if (capture_opts == NULL || capture_opts->ifaces == NULL || capture_opts->ifaces->len == 0)
1126     {
1127         return;
1128     }
1129
1130     /* Close handle to child process. */
1131     g_spawn_close_pid(pid);
1132
1133     /* Update extcap_pid in interface options structure. */
1134     for (i = 0; i < capture_opts->ifaces->len; i++)
1135     {
1136         interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
1137         if (interface_opts.extcap_pid == pid)
1138         {
1139             userdata = (extcap_userdata *)interface_opts.extcap_userdata;
1140             if (userdata != NULL)
1141             {
1142                 interface_opts.extcap_pid = INVALID_EXTCAP_PID;
1143                 userdata->exitcode = 0;
1144 #ifndef _WIN32
1145                 if (WIFEXITED(status))
1146                 {
1147                     if (WEXITSTATUS(status) != 0)
1148                     {
1149                         userdata->exitcode = WEXITSTATUS(status);
1150                     }
1151                 }
1152                 else
1153                 {
1154                     userdata->exitcode = G_SPAWN_ERROR_FAILED;
1155                 }
1156 #else
1157                 if (status != 0)
1158                 {
1159                     userdata->exitcode = status;
1160                 }
1161 #endif
1162                 if (status == 0 && userdata->extcap_stderr != NULL)
1163                 {
1164                     userdata->exitcode = 1;
1165                 }
1166             }
1167             g_source_remove(interface_opts.extcap_child_watch);
1168             interface_opts.extcap_child_watch = 0;
1169
1170             capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, i);
1171             g_array_insert_val(capture_opts->ifaces, i, interface_opts);
1172             break;
1173         }
1174     }
1175 }
1176
1177 static
1178 GPtrArray *extcap_prepare_arguments(interface_options interface_opts)
1179 {
1180     GPtrArray *result = NULL;
1181
1182     if (interface_opts.if_type == IF_EXTCAP)
1183     {
1184         result = g_ptr_array_new();
1185
1186 #define add_arg(X) g_ptr_array_add(result, g_strdup(X))
1187
1188         add_arg(interface_opts.extcap);
1189         add_arg(EXTCAP_ARGUMENT_RUN_CAPTURE);
1190         add_arg(EXTCAP_ARGUMENT_INTERFACE);
1191         add_arg(interface_opts.name);
1192         if (interface_opts.cfilter && strlen(interface_opts.cfilter) > 0)
1193         {
1194             add_arg(EXTCAP_ARGUMENT_CAPTURE_FILTER);
1195             add_arg(interface_opts.cfilter);
1196         }
1197         add_arg(EXTCAP_ARGUMENT_RUN_PIPE);
1198         add_arg(interface_opts.extcap_fifo);
1199         if (interface_opts.extcap_control_in)
1200         {
1201             add_arg(EXTCAP_ARGUMENT_CONTROL_OUT);
1202             add_arg(interface_opts.extcap_control_in);
1203         }
1204         if (interface_opts.extcap_control_out)
1205         {
1206             add_arg(EXTCAP_ARGUMENT_CONTROL_IN);
1207             add_arg(interface_opts.extcap_control_out);
1208         }
1209         if (interface_opts.extcap_args == NULL || g_hash_table_size(interface_opts.extcap_args) == 0)
1210         {
1211             /* User did not perform interface configuration.
1212              *
1213              * Check if there are any boolean flags that are set by default
1214              * and hence their argument should be added.
1215              */
1216             GList *arglist;
1217             GList *elem;
1218
1219             arglist = extcap_get_if_configuration(interface_opts.name);
1220             for (elem = g_list_first(arglist); elem; elem = elem->next)
1221             {
1222                 GList *arg_list;
1223                 extcap_arg *arg_iter;
1224
1225                 if (elem->data == NULL)
1226                 {
1227                     continue;
1228                 }
1229
1230                 arg_list = g_list_first((GList *)elem->data);
1231                 while (arg_list != NULL)
1232                 {
1233                     const gchar *stored = NULL, * defval = NULL;
1234                     /* In case of boolflags only first element in arg_list is relevant. */
1235                     arg_iter = (extcap_arg *)(arg_list->data);
1236                     if (arg_iter->pref_valptr != NULL)
1237                     {
1238                         stored = *arg_iter->pref_valptr;
1239                     }
1240
1241                     if (arg_iter->default_complex != NULL && arg_iter->default_complex->_val != NULL)
1242                     {
1243                         defval = arg_iter->default_complex->_val;
1244                     }
1245
1246                     /* Different data in storage then set for default */
1247                     if (g_strcmp0(stored, defval) != 0)
1248                     {
1249                         if (arg_iter->arg_type == EXTCAP_ARG_BOOLFLAG)
1250                         {
1251                             if (g_strcmp0(stored, "true") == 0)
1252                             {
1253                                 add_arg(arg_iter->call);
1254                             }
1255                         }
1256                         else
1257                         {
1258                             add_arg(arg_iter->call);
1259                             add_arg(stored);
1260                         }
1261                     }
1262                     else if (arg_iter->arg_type == EXTCAP_ARG_BOOLFLAG)
1263                     {
1264                         if (extcap_complex_get_bool(arg_iter->default_complex))
1265                         {
1266                             add_arg(arg_iter->call);
1267                         }
1268                     }
1269
1270                     arg_list = arg_list->next;
1271                 }
1272             }
1273
1274             extcap_free_if_configuration(arglist, TRUE);
1275         }
1276         else
1277         {
1278             g_hash_table_foreach_remove(interface_opts.extcap_args, extcap_add_arg_and_remove_cb, result);
1279         }
1280         add_arg(NULL);
1281 #undef add_arg
1282
1283     }
1284
1285     return result;
1286 }
1287
1288 /* call mkfifo for each extcap,
1289  * returns FALSE if there's an error creating a FIFO */
1290 gboolean
1291 extcap_init_interfaces(capture_options *capture_opts)
1292 {
1293     guint i;
1294     interface_options interface_opts;
1295     extcap_userdata *userdata;
1296
1297     for (i = 0; i < capture_opts->ifaces->len; i++)
1298     {
1299         GPtrArray *args = NULL;
1300         GPid pid = INVALID_EXTCAP_PID;
1301
1302         interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
1303
1304         /* skip native interfaces */
1305         if (interface_opts.if_type != IF_EXTCAP)
1306         {
1307             continue;
1308         }
1309
1310         /* create control pipes if having toolbar */
1311         if (extcap_has_toolbar(interface_opts.name))
1312         {
1313             extcap_create_pipe(&interface_opts.extcap_control_in);
1314             extcap_create_pipe(&interface_opts.extcap_control_out);
1315         }
1316
1317         /* create pipe for fifo */
1318         if (!extcap_create_pipe(&interface_opts.extcap_fifo))
1319         {
1320             return FALSE;
1321         }
1322
1323         /* Create extcap call */
1324         args = extcap_prepare_arguments(interface_opts);
1325
1326         userdata = g_new0(extcap_userdata, 1);
1327
1328         pid = extcap_spawn_async(userdata, args);
1329
1330         g_ptr_array_foreach(args, (GFunc)g_free, NULL);
1331         g_ptr_array_free(args, TRUE);
1332
1333         if (pid == INVALID_EXTCAP_PID)
1334         {
1335             g_free(userdata);
1336             continue;
1337         }
1338
1339         interface_opts.extcap_pid = pid;
1340
1341         interface_opts.extcap_child_watch =
1342             g_child_watch_add(pid, extcap_child_watch_cb, (gpointer)capture_opts);
1343
1344 #ifdef _WIN32
1345         /* On Windows, wait for extcap to connect to named pipe.
1346          * Some extcaps will present UAC screen to user.
1347          * 30 second timeout should be reasonable timeout for extcap to
1348          * connect to named pipe (including user interaction).
1349          * Wait on multiple object in case of extcap termination
1350          * without opening pipe.
1351          *
1352          * Minimum supported version of Windows: XP / Server 2003.
1353          */
1354         if (pid != INVALID_EXTCAP_PID)
1355         {
1356             extcap_wait_for_pipe(pipe_h, pid);
1357         }
1358
1359 #endif
1360
1361         interface_opts.extcap_userdata = (gpointer) userdata;
1362
1363         capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, i);
1364         g_array_insert_val(capture_opts->ifaces, i, interface_opts);
1365     }
1366
1367     return TRUE;
1368 }
1369
1370 #ifdef _WIN32
1371 /* called by capture_sync to get the CreatNamedPipe handle*/
1372 HANDLE
1373 extcap_get_win32_handle()
1374 {
1375     return pipe_h;
1376 }
1377 #endif
1378
1379 gboolean extcap_create_pipe(char **fifo)
1380 {
1381 #ifdef _WIN32
1382     gchar timestr[ 14 + 1 ];
1383     time_t current_time;
1384
1385     gchar *pipename = NULL;
1386
1387     SECURITY_ATTRIBUTES security;
1388     /* create pipename */
1389     current_time = time(NULL);
1390     /*
1391      * XXX - we trust Windows not to return a time before the Epoch here,
1392      * so we won't get a null pointer back from localtime().
1393      */
1394     strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S", localtime(&current_time));
1395     pipename = g_strconcat("\\\\.\\pipe\\", EXTCAP_PIPE_PREFIX, "_", timestr, NULL);
1396
1397     /* Security struct to enable Inheritable HANDLE */
1398     memset(&security, 0, sizeof(SECURITY_ATTRIBUTES));
1399     security.nLength = sizeof(SECURITY_ATTRIBUTES);
1400     security.bInheritHandle = TRUE;
1401     security.lpSecurityDescriptor = NULL;
1402
1403     /* create a namedPipe*/
1404     pipe_h = CreateNamedPipe(
1405                  utf_8to16(pipename),
1406                  PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1407                  PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
1408                  5, 65536, 65536,
1409                  300,
1410                  &security);
1411
1412     if (pipe_h == INVALID_HANDLE_VALUE)
1413     {
1414         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "\nError creating pipe => (%d)", GetLastError());
1415         return FALSE;
1416     }
1417     else
1418     {
1419         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "\nWireshark Created pipe =>(%s)", pipename);
1420         *fifo = g_strdup(pipename);
1421     }
1422 #else
1423     gchar *temp_name = NULL;
1424     int fd = 0;
1425
1426     if ((fd = create_tempfile(&temp_name, EXTCAP_PIPE_PREFIX, NULL)) < 0)
1427     {
1428         return FALSE;
1429     }
1430
1431     ws_close(fd);
1432
1433     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
1434           "Extcap - Creating fifo: %s", temp_name);
1435
1436     if (file_exists(temp_name))
1437     {
1438         ws_unlink(temp_name);
1439     }
1440
1441     if (mkfifo(temp_name, 0600) == 0)
1442     {
1443         *fifo = g_strdup(temp_name);
1444     }
1445 #endif
1446
1447     return TRUE;
1448 }
1449
1450 /************* EXTCAP LOAD INTERFACE LIST ***************
1451  *
1452  * The following code handles loading and reloading the interface list. It is explicitly
1453  * kept separate from the rest
1454  */
1455
1456
1457 static void
1458 extcap_free_interface_info(gpointer data _U_)
1459 {
1460     extcap_info *info = (extcap_info *)data;
1461
1462     g_free(info->basename);
1463     g_free(info->full_path);
1464     g_free(info->version);
1465
1466     extcap_free_interfaces(info->interfaces);
1467
1468     g_free(info);
1469 }
1470
1471 static extcap_info *
1472 extcap_ensure_interface(const gchar * toolname, gboolean create_if_nonexist)
1473 {
1474     extcap_info * element = 0;
1475
1476     if ( ! toolname )
1477         return element;
1478
1479     if ( ! _loaded_interfaces )
1480         _loaded_interfaces = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, extcap_free_interface);
1481
1482     element = (extcap_info *) g_hash_table_lookup(_loaded_interfaces, toolname );
1483     if ( ! element && create_if_nonexist )
1484     {
1485         g_hash_table_insert(_loaded_interfaces, g_strdup(toolname), g_new0(extcap_info, 1));
1486         element = (extcap_info *) g_hash_table_lookup(_loaded_interfaces, toolname );
1487     }
1488
1489     return element;
1490 }
1491
1492 extcap_info *
1493 extcap_get_tool_by_ifname(const gchar *ifname)
1494 {
1495     if ( ifname && _tool_for_ifname )
1496     {
1497         gchar * toolname = (gchar *)g_hash_table_lookup(_tool_for_ifname, ifname);
1498         if ( toolname )
1499             return extcap_ensure_interface(toolname, FALSE);
1500     }
1501
1502     return NULL;
1503 }
1504
1505 extcap_info *
1506 extcap_get_tool_info(const gchar * toolname)
1507 {
1508     return extcap_ensure_interface(toolname, FALSE);
1509 }
1510
1511 static void remove_extcap_entry(gpointer entry, gpointer data _U_)
1512 {
1513     extcap_interface *int_iter = (extcap_interface*)entry;
1514
1515     if (int_iter->if_type == EXTCAP_SENTENCE_EXTCAP)
1516         extcap_free_interface(entry);
1517 }
1518
1519 static gboolean cb_load_interfaces(extcap_callback_info_t cb_info)
1520 {
1521     GList * interfaces = NULL, * control_items = NULL, * walker = NULL;
1522     extcap_interface * int_iter = NULL;
1523     extcap_info * element = NULL;
1524     iface_toolbar * toolbar_entry = NULL;
1525     gchar * toolname = g_path_get_basename(cb_info.extcap);
1526
1527     GList * interface_keys = g_hash_table_get_keys(_loaded_interfaces);
1528
1529     /* Load interfaces from utility */
1530     interfaces = extcap_parse_interfaces(cb_info.output, &control_items);
1531
1532     if (control_items)
1533     {
1534         toolbar_entry = g_new0(iface_toolbar, 1);
1535         toolbar_entry->controls = control_items;
1536     }
1537
1538     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Loading interface list for %s ", cb_info.extcap);
1539
1540     /* Seems, that there where no interfaces to be loaded */
1541     if ( ! interfaces || g_list_length(interfaces) == 0 )
1542     {
1543         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Cannot load interfaces for %s", cb_info.extcap );
1544         /* Some utilities, androiddump for example, may actually don't present any interfaces, even
1545          * if the utility itself is present. In such a case, we return here, but do not return
1546          * FALSE, or otherwise further loading of other utilities will be stopped */
1547         return TRUE;
1548     }
1549
1550     /* Load or create the storage element for the tool */
1551     element = extcap_ensure_interface(toolname, TRUE);
1552     if ( element == NULL )
1553     {
1554         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_ERROR, "Cannot store interface %s, maybe duplicate?", cb_info.extcap );
1555         return FALSE;
1556     }
1557
1558     walker = interfaces;
1559     gchar* help = NULL;
1560     while (walker != NULL)
1561     {
1562         int_iter = (extcap_interface *)walker->data;
1563
1564         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Interface found %s\n", int_iter->call);
1565
1566         /* Help is not necessarily stored with the interface, but rather with the version string.
1567          * As the version string allways comes in front of the interfaces, this ensures, that it get's
1568          * properly stored with the interface */
1569         if (int_iter->if_type == EXTCAP_SENTENCE_EXTCAP)
1570         {
1571             g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "  Extcap [%s] ", int_iter->call);
1572             /* Only initialize values if none are set. Need to check only one element here */
1573             if ( ! element->version )
1574             {
1575                 element->version = g_strdup(int_iter->version);
1576                 element->basename = g_strdup(toolname);
1577                 element->full_path = g_strdup(cb_info.extcap);
1578                 element->help = g_strdup(int_iter->help);
1579             }
1580
1581             help = int_iter->help;
1582             if (toolbar_entry)
1583             {
1584                 toolbar_entry->menu_title = g_strdup(int_iter->display);
1585                 toolbar_entry->help = g_strdup(int_iter->help);
1586             }
1587
1588             walker = g_list_next(walker);
1589             continue;
1590         }
1591
1592         /* Only interface definitions will be parsed here. help is already set by the extcap element,
1593          * which makes it necessary to have version in the list before the interfaces. This is normally
1594          * the case by design, but could be changed by separating the information in extcap-base. */
1595         if ( int_iter->if_type == EXTCAP_SENTENCE_INTERFACE )
1596         {
1597             if ( g_list_find(interface_keys, int_iter->call) )
1598             {
1599                 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_WARNING, "Extcap interface \"%s\" is already provided by \"%s\" ",
1600                       int_iter->call, (gchar *)extcap_if_executable(int_iter->call));
1601                 walker = g_list_next(walker);
1602                 continue;
1603             }
1604
1605             g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "  Interface [%s] \"%s\" ",
1606                   int_iter->call, int_iter->display);
1607
1608             int_iter->extcap_path = g_strdup(cb_info.extcap);
1609
1610             /* Only set the help, if it exists and no parsed help information is present */
1611             if ( ! int_iter->help && help )
1612                 int_iter->help = g_strdup(help);
1613
1614             element->interfaces = g_list_append(element->interfaces, int_iter);
1615             g_hash_table_insert(_tool_for_ifname, g_strdup(int_iter->call), g_strdup(toolname));
1616
1617             if (toolbar_entry)
1618             {
1619                 if (!toolbar_entry->menu_title)
1620                 {
1621                     toolbar_entry->menu_title = g_strdup(int_iter->display);
1622                 }
1623                 toolbar_entry->ifnames = g_list_append(toolbar_entry->ifnames, g_strdup(int_iter->call));
1624             }
1625         }
1626
1627         walker = g_list_next(walker);
1628     }
1629
1630     if (toolbar_entry && toolbar_entry->menu_title)
1631     {
1632         iface_toolbar_add(toolbar_entry);
1633         extcap_iface_toolbar_add(cb_info.extcap, toolbar_entry);
1634     }
1635
1636     g_list_foreach(interfaces, remove_extcap_entry, NULL);
1637     g_list_free(interfaces);
1638     g_list_free(interface_keys);
1639     g_free(toolname);
1640     return TRUE;
1641 }
1642
1643
1644 /* Handles loading of the interfaces.
1645  *
1646  * A list of interfaces can be obtained by calling \ref extcap_loaded_interfaces
1647  */
1648 static void
1649 extcap_load_interface_list(void)
1650 {
1651     gchar *argv;
1652     gchar *error;
1653
1654     if (_toolbars)
1655     {
1656         // Remove existing interface toolbars here instead of in extcap_clear_interfaces()
1657         // to avoid flicker in shown toolbars when refreshing interfaces.
1658         GList *toolbar_list = g_hash_table_get_values (_toolbars);
1659         for (GList *walker = toolbar_list; walker; walker = walker->next)
1660         {
1661             iface_toolbar *toolbar = (iface_toolbar *) walker->data;
1662             iface_toolbar_remove(toolbar->menu_title);
1663         }
1664         g_hash_table_remove_all(_toolbars);
1665     } else {
1666         _toolbars = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, extcap_free_toolbar);
1667     }
1668
1669     if (_loaded_interfaces == NULL)
1670     {
1671         _loaded_interfaces = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, extcap_free_interface_info);
1672         /* Cleanup lookup table */
1673         if ( _tool_for_ifname )
1674         {
1675             g_hash_table_remove_all(_tool_for_ifname);
1676             _tool_for_ifname = 0;
1677         }
1678         _tool_for_ifname = g_hash_table_new(g_str_hash, g_str_equal);
1679
1680         argv = g_strdup(EXTCAP_ARGUMENT_LIST_INTERFACES);
1681
1682         extcap_callback_info_t cb_info;
1683         cb_info.data = NULL;
1684         cb_info.ifname = NULL;
1685         cb_info.err_str = &error;
1686
1687         extcap_foreach(1, &argv, cb_load_interfaces, cb_info);
1688
1689         g_free(argv);
1690     }
1691 }
1692
1693 /*
1694  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
1695  *
1696  * Local variables:
1697  * c-basic-offset: 4
1698  * tab-width: 8
1699  * indent-tabs-mode: nil
1700  * End:
1701  *
1702  * vi: set shiftwidth=4 tabstop=8 expandtab:
1703  * :indentSize=4:tabSize=8:noTabs=true:
1704  */