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