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