nexmon – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 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 #include <epan/prefs-int.h>
52  
53 #include <wsutil/file_util.h>
54 #include <wsutil/filesystem.h>
55 #include <wsutil/tempfile.h>
56  
57 #include "capture_opts.h"
58  
59 #include "extcap.h"
60 #include "extcap_parser.h"
61 #include "extcap_spawn.h"
62  
63 #ifdef _WIN32
64 static HANDLE pipe_h = NULL;
65 #endif
66  
67 static void extcap_child_watch_cb(GPid pid, gint status, gpointer user_data);
68  
69 /* internal container, for all the extcap interfaces that have been found.
70 * will be resetted by every call to extcap_interface_list() and is being
71 * used in extcap_get_if_* as well as extcap_init_interfaces to ensure,
72 * that only extcap interfaces are being given to underlying extcap programs
73 */
74 static GHashTable *ifaces = NULL;
75  
76 /* internal container, for all the extcap executables that have been found.
77 * will be resetted by every call to extcap_interface_list() and is being
78 * used for printing information about all extcap interfaces found
79 */
80 static GHashTable *tools = NULL;
81  
82 /* internal container, to map preference names to pointers that hold preference
83 * values. These ensure that preferences can survive extcap if garbage
84 * collection, and does not lead to dangling pointers in the prefs subsystem.
85 */
86 static GHashTable *extcap_prefs_dynamic_vals = NULL;
87  
88 /* Callback definition for extcap_foreach */
89 typedef gboolean (*extcap_cb_t)(const gchar *extcap, const gchar *ifname, gchar *output, void *data,
90 gchar **err_str);
91  
92 /* #define ARG_DEBUG */
93 #if ARG_DEBUG
94 static void extcap_debug_arguments ( extcap_arg *arg_iter );
95 #endif
96  
97 static gboolean
98 extcap_if_exists(const gchar *ifname)
99 {
100 if ( !ifname || !ifaces )
101 return FALSE;
102  
103 if ( g_hash_table_lookup(ifaces, ifname) )
104 return TRUE;
105  
106 return FALSE;
107 }
108  
109 static gboolean
110 extcap_if_exists_for_extcap(const gchar *ifname, const gchar *extcap)
111 {
112 gchar *entry = (gchar *)g_hash_table_lookup(ifaces, ifname);
113  
114 if ( entry && strcmp(entry, extcap) == 0 )
115 return TRUE;
116  
117 return FALSE;
118 }
119  
120 static gchar *
121 extcap_if_executable(const gchar *ifname)
122 {
123 return (gchar *)g_hash_table_lookup(ifaces, ifname);
124 }
125  
126 static void
127 extcap_if_add(const gchar *ifname, const gchar *extcap)
128 {
129 if ( !g_hash_table_lookup(ifaces, ifname) )
130 g_hash_table_insert(ifaces, g_strdup(ifname), g_strdup(extcap));
131 }
132  
133 static void
134 extcap_free_info (gpointer data) {
135 extcap_info * info = (extcap_info *)data;
136  
137 g_free (info->basename);
138 g_free (info->full_path);
139 g_free (info->version);
140 g_free (info);
141 }
142  
143 static void
144 extcap_tool_add(const gchar *extcap, const extcap_interface *interface)
145 {
146 char *toolname;
147  
148 if ( !extcap || !interface )
149 return;
150  
151 toolname = g_path_get_basename(extcap);
152  
153 if ( !g_hash_table_lookup(tools, toolname) ) {
154 extcap_info * store = (extcap_info *)g_new0(extcap_info, 1);
155 store->version = g_strdup(interface->version);
156 store->full_path = g_strdup(extcap);
157 store->basename = g_strdup(toolname);
158  
159 g_hash_table_insert(tools, g_strdup(toolname), store);
160 }
161  
162 g_free(toolname);
163 }
164  
165 /* Note: args does not need to be NULL-terminated. */
166 static void extcap_foreach(gint argc, gchar **args, extcap_cb_t cb,
167 void *cb_data, char **err_str, const char * ifname _U_) {
168 const char *dirname = get_extcap_dir();
169 GDir *dir;
170 const gchar *file;
171 gboolean keep_going;
172  
173 keep_going = TRUE;
174  
175 if ((dir = g_dir_open(dirname, 0, NULL)) != NULL) {
176 GString *extcap_path = NULL;
177  
178 extcap_path = g_string_new("");
179 while (keep_going && (file = g_dir_read_name(dir)) != NULL ) {
180 gchar *command_output = NULL;
181  
182 /* full path to extcap binary */
183 #ifdef _WIN32
184 g_string_printf(extcap_path, "%s\\%s", dirname, file);
185 #else
186 g_string_printf(extcap_path, "%s/%s", dirname, file);
187 #endif
188 if ( extcap_if_exists(ifname) && !extcap_if_exists_for_extcap(ifname, extcap_path->str ) )
189 continue;
190  
191 if ( extcap_spawn_sync ( (gchar *) dirname, extcap_path->str, argc, args, &command_output ) )
192 keep_going = cb(extcap_path->str, ifname, command_output, cb_data, err_str);
193  
194 g_free(command_output);
195 }
196  
197 g_dir_close(dir);
198 g_string_free(extcap_path, TRUE);
199 }
200  
201 }
202  
203 static gboolean dlt_cb(const gchar *extcap _U_, const gchar *ifname _U_, gchar *output, void *data,
204 char **err_str) {
205 extcap_token_sentence *tokens;
206 extcap_dlt *dlts, *dlt_iter, *next;
207 if_capabilities_t *caps;
208 GList *linktype_list = NULL;
209 data_link_info_t *data_link_info;
210  
211 tokens = extcap_tokenize_sentences(output);
212 extcap_parse_dlts(tokens, &dlts);
213  
214 extcap_free_tokenized_sentence_list(tokens);
215  
216 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap pipe %s ", extcap);
217  
218 /*
219 * Allocate the interface capabilities structure.
220 */
221 caps = (if_capabilities_t *) g_malloc(sizeof *caps);
222 caps->can_set_rfmon = FALSE;
223  
224 dlt_iter = dlts;
225 while (dlt_iter != NULL ) {
226 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
227 " DLT %d name=\"%s\" display=\"%s\" ", dlt_iter->number,
228 dlt_iter->name, dlt_iter->display);
229  
230 data_link_info = g_new(data_link_info_t, 1);
231 data_link_info->dlt = dlt_iter->number;
232 data_link_info->name = g_strdup(dlt_iter->name);
233 data_link_info->description = g_strdup(dlt_iter->display);
234 linktype_list = g_list_append(linktype_list, data_link_info);
235 dlt_iter = dlt_iter->next_dlt;
236 }
237  
238 /* Check to see if we built a list */
239 if (linktype_list != NULL && data != NULL) {
240 caps->data_link_types = linktype_list;
241 *(if_capabilities_t **) data = caps;
242 } else {
243 if (err_str) {
244 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, " returned no DLTs");
245 *err_str = g_strdup("Extcap returned no DLTs");
246 }
247 g_free(caps);
248 }
249  
250 dlt_iter = dlts;
251 while (dlt_iter != NULL ) {
252 next = dlt_iter->next_dlt;
253 extcap_free_dlt(dlt_iter);
254 dlt_iter = next;
255 }
256  
257 return FALSE;
258 }
259  
260 if_capabilities_t *
261 extcap_get_if_dlts(const gchar *ifname, char **err_str) {
262 gchar *argv[3];
263 gint i;
264 if_capabilities_t *caps = NULL;
265  
266 if (err_str != NULL)
267 *err_str = NULL;
268  
269 if ( extcap_if_exists(ifname) )
270 {
271 argv[0] = g_strdup(EXTCAP_ARGUMENT_LIST_DLTS);
272 argv[1] = g_strdup(EXTCAP_ARGUMENT_INTERFACE);
273 argv[2] = g_strdup(ifname);
274  
275 extcap_foreach(3, argv, dlt_cb, &caps, err_str, ifname);
276  
277 for (i = 0; i < 3; ++i)
278 g_free(argv[i]);
279 }
280  
281 return caps;
282 }
283  
284 static gboolean interfaces_cb(const gchar *extcap, const gchar *ifname _U_, gchar *output, void *data,
285 char **err_str _U_) {
286 GList **il = (GList **) data;
287 extcap_token_sentence *tokens;
288 extcap_interface *interfaces, *int_iter; /*, *next; */
289 if_info_t *if_info;
290  
291 tokens = extcap_tokenize_sentences(output);
292 extcap_parse_interfaces(tokens, &interfaces);
293  
294 extcap_free_tokenized_sentence_list(tokens);
295  
296 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap pipe %s ", extcap);
297  
298 int_iter = interfaces;
299 while (int_iter != NULL ) {
300 if ( int_iter->if_type == EXTCAP_SENTENCE_INTERFACE && extcap_if_exists(int_iter->call) )
301 {
302 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_WARNING, "Extcap interface \"%s\" is already provided by \"%s\" ",
303 int_iter->call, (gchar *)extcap_if_executable(int_iter->call) );
304 int_iter = int_iter->next_interface;
305 continue;
306 }
307  
308 if ( int_iter->if_type == EXTCAP_SENTENCE_INTERFACE )
309 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, " Interface [%s] \"%s\" ",
310 int_iter->call, int_iter->display);
311 else if ( int_iter->if_type == EXTCAP_SENTENCE_EXTCAP )
312 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, " Extcap [%s] ", int_iter->call);
313  
314 if ( int_iter->if_type == EXTCAP_SENTENCE_INTERFACE ) {
315 if (il != NULL) {
316 if_info = g_new0(if_info_t, 1);
317 if_info->name = g_strdup(int_iter->call);
318 if_info->friendly_name = g_strdup(int_iter->display);
319  
320 if_info->type = IF_EXTCAP;
321  
322 if_info->extcap = g_strdup(extcap);
323 *il = g_list_append(*il, if_info);
324 }
325  
326 extcap_if_add(int_iter->call, extcap);
327 }
328  
329 /* Call for interfaces and tools alike. Multiple calls (because a tool has multiple
330 * interfaces) are handled internally */
331 extcap_tool_add(extcap, int_iter);
332  
333 int_iter = int_iter->next_interface;
334 }
335 extcap_free_interface(interfaces);
336  
337 return TRUE;
338 }
339  
340 static gint
341 if_info_compare(gconstpointer a, gconstpointer b)
342 {
343 gint comp = 0;
344 const if_info_t * if_a = (const if_info_t *)a;
345 const if_info_t * if_b = (const if_info_t *)b;
346  
347 if ( (comp = g_strcmp0(if_a->name, if_b->name)) == 0 )
348 return g_strcmp0(if_a->friendly_name, if_b->friendly_name);
349  
350 return comp;
351 }
352  
353 static void
354 extcap_reload_interface_list(GList **retp, char **err_str) {
355 gchar *argv;
356  
357 if (err_str != NULL)
358 *err_str = NULL;
359  
360 /* ifaces is used as cache, do not destroy its contents when
361 * returning or no extcap interfaces can be queried for options */
362 if (ifaces == NULL)
363 ifaces = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
364 else
365 g_hash_table_remove_all(ifaces);
366  
367 if (tools == NULL)
368 tools = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, extcap_free_info);
369 else
370 g_hash_table_remove_all(tools);
371  
372 argv = g_strdup(EXTCAP_ARGUMENT_LIST_INTERFACES);
373  
374 extcap_foreach(1, &argv, interfaces_cb, retp, err_str, NULL);
375  
376 g_free(argv);
377 }
378  
379 GHashTable *
380 extcap_tools_list(void) {
381 if ( tools == NULL || g_hash_table_size(tools) == 0 )
382 extcap_reload_interface_list(NULL, NULL);
383  
384 return tools;
385 }
386  
387 GList *
388 append_extcap_interface_list(GList *list, char **err_str) {
389 GList *ret = NULL;
390 GList *entry;
391 void *data;
392  
393 /* Update the extcap interfaces and get a list of their if_infos */
394 extcap_reload_interface_list(&ret, err_str);
395  
396 /* Sort that list */
397 ret = g_list_sort(ret, if_info_compare);
398  
399 /* Append the interfaces in that list to the list we're handed. */
400 while (ret != NULL) {
401 entry = g_list_first(ret);
402 data = entry->data;
403 ret = g_list_delete_link(ret, entry);
404 list = g_list_append(list, data);
405 }
406 return list;
407 }
408  
409 static void extcap_free_arg_elem(gpointer data, gpointer user_data _U_) {
410 extcap_free_arg((extcap_arg *) data);
411 g_free(data);
412 }
413  
414 void extcap_register_preferences(void)
415 {
416 GList * interfaces = NULL;
417  
418 module_t * dev_module = prefs_find_module("extcap");
419  
420 if ( !dev_module )
421 return;
422  
423 if ( ! ifaces || g_hash_table_size(ifaces) == 0 )
424 extcap_reload_interface_list(NULL, NULL);
425  
426 interfaces = g_hash_table_get_keys(ifaces);
427  
428 while ( interfaces ) {
429 extcap_get_if_configuration((gchar *)interfaces->data);
430  
431 interfaces = g_list_next(interfaces);
432 }
433 }
434  
435 /**
436 * Releases the dynamic preference value pointers. Must not be called before
437 * prefs_cleanup since these pointers could still be in use.
438 */
439 void extcap_cleanup(void)
440 {
441 if (extcap_prefs_dynamic_vals) {
442 g_hash_table_destroy(extcap_prefs_dynamic_vals);
443 }
444 }
445  
446 void extcap_pref_store(extcap_arg * arg, const char * newval)
447 {
448 if (arg && arg->pref_valptr != NULL)
449 {
450 g_free(*arg->pref_valptr);
451 *arg->pref_valptr = g_strdup(newval);
452 }
453  
454 }
455  
456 /**
457 * Obtains a pointer which can store a value for the given preference name.
458 *
459 * Extcap interfaces (and their preferences) are dynamic, they can be created
460 * and destroyed at will. Thus their data structures are insufficient to pass to
461 * the preferences APIs which require pointers which are valid until the
462 * preferences are removed (at exit).
463 */
464 static gchar ** extcap_prefs_dynamic_valptr(const char *name)
465 {
466 gchar **valp;
467 if (!extcap_prefs_dynamic_vals) {
468 /* Initialize table only as needed, most preferences are not dynamic */
469 extcap_prefs_dynamic_vals = g_hash_table_new_full(g_str_hash, g_str_equal,
470 g_free, g_free);
471 }
472 valp = (gchar **)g_hash_table_lookup(extcap_prefs_dynamic_vals, name);
473 if (!valp) {
474 /* New dynamic pref, allocate, initialize and store. */
475 valp = g_new0(gchar *, 1);
476 g_hash_table_insert(extcap_prefs_dynamic_vals, g_strdup(name), valp);
477 }
478 return valp;
479 }
480  
481 static void extcap_free_if_configuration(GList *list)
482 {
483 GList *elem, *sl;
484  
485 for (elem = g_list_first(list); elem; elem = elem->next)
486 {
487 if (elem->data != NULL) {
488 /* g_list_free_full() only exists since 2.28. */
489 sl = g_list_first((GList *)elem->data);
490 g_list_foreach(sl, (GFunc)extcap_free_arg_elem, NULL);
491 g_list_free(sl);
492 }
493 }
494 g_list_free(list);
495 }
496  
497 struct preference *
498 extcap_pref_for_argument(const gchar *ifname, struct _extcap_arg * arg) {
499 struct preference * pref = NULL;
500  
501 GRegex * regex_name = g_regex_new ("[-]+", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, NULL );
502 GRegex * regex_ifname = g_regex_new ("(?![a-zA-Z1-9_]).", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, NULL );
503 if (regex_name && regex_ifname) {
504 if ( prefs_find_module("extcap") ) {
505 gchar * pref_name = g_regex_replace(regex_name, arg->call, strlen(arg->call), 0, "", (GRegexMatchFlags) 0, NULL );
506 gchar * ifname_underscore = g_regex_replace(regex_ifname, ifname, strlen(ifname), 0, "_", (GRegexMatchFlags) 0, NULL );
507 gchar * ifname_lowercase = g_ascii_strdown(ifname_underscore, -1);
508 gchar * pref_ifname = g_strconcat(ifname_lowercase, ".", pref_name, NULL);
509  
510 pref = prefs_find_preference(prefs_find_module("extcap"), pref_ifname);
511  
512 g_free(pref_name);
513 g_free(ifname_underscore);
514 g_free(ifname_lowercase);
515 g_free(pref_ifname);
516 }
517 }
518 if (regex_name) {
519 g_regex_unref(regex_name);
520 }
521 if (regex_ifname) {
522 g_regex_unref(regex_ifname);
523 }
524  
525 return pref;
526 }
527  
528 static gboolean search_cb(const gchar *extcap _U_, const gchar *ifname _U_, gchar *output, void *data,
529 char **err_str _U_) {
530 extcap_token_sentence *tokens = NULL;
531 GList *arguments = NULL;
532 GList **il = (GList **) data;
533 module_t * dev_module = NULL;
534  
535 tokens = extcap_tokenize_sentences(output);
536 arguments = extcap_parse_args(tokens);
537  
538 extcap_free_tokenized_sentence_list(tokens);
539  
540 #if ARG_DEBUG
541 extcap_debug_arguments ( arguments );
542 #endif
543  
544 dev_module = prefs_find_module("extcap");
545  
546 if ( dev_module ) {
547 GList * walker = arguments;
548  
549 GRegex * regex_name = g_regex_new ("[-]+", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, NULL );
550 GRegex * regex_ifname = g_regex_new ("(?![a-zA-Z1-9_]).", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, NULL );
551 if (regex_name && regex_ifname) {
552 while ( walker != NULL ) {
553 extcap_arg * arg = (extcap_arg *)walker->data;
554 arg->device_name = g_strdup(ifname);
555  
556 if ( arg->save ) {
557 struct preference * pref = NULL;
558  
559 gchar * pref_name = g_regex_replace(regex_name, arg->call, strlen(arg->call), 0, "", (GRegexMatchFlags) 0, NULL );
560 gchar * ifname_underscore = g_regex_replace(regex_ifname, ifname, strlen(ifname), 0, "_", (GRegexMatchFlags) 0, NULL );
561 gchar * ifname_lowercase = g_ascii_strdown(ifname_underscore, -1);
562 gchar * pref_ifname = g_strconcat(ifname_lowercase, ".", pref_name, NULL);
563  
564 if ( ( pref = prefs_find_preference(dev_module, pref_ifname) ) == NULL ) {
565 arg->pref_valptr = extcap_prefs_dynamic_valptr(pref_ifname);
566 /* Set an initial value if any (the string will be copied at registration) */
567 if (arg->default_complex) {
568 *arg->pref_valptr = arg->default_complex->_val;
569 }
570  
571 prefs_register_string_preference(dev_module, g_strdup(pref_ifname),
572 arg->display, arg->display, (const char **)arg->pref_valptr);
573 } else {
574 /* Been here before, restore stored value */
575 if (! arg->pref_valptr && pref->varp.string && strlen(*pref->varp.string))
576 {
577 arg->pref_valptr = pref->varp.string;
578 }
579 }
580  
581 g_free(pref_name);
582 g_free(ifname_underscore);
583 g_free(ifname_lowercase);
584 g_free(pref_ifname);
585 }
586  
587 walker = g_list_next(walker);
588 }
589 }
590 if (regex_name) {
591 g_regex_unref(regex_name);
592 }
593 if (regex_ifname) {
594 g_regex_unref(regex_ifname);
595 }
596 }
597  
598 *il = g_list_append(*il, arguments);
599  
600 /* By returning false, extcap_foreach will break on first found */
601 return TRUE;
602 }
603  
604 GList *
605 extcap_get_if_configuration(const char * ifname) {
606 gchar *argv[3];
607 GList *ret = NULL;
608 gchar **err_str = NULL;
609 int i;
610  
611 if ( extcap_if_exists(ifname) )
612 {
613 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap path %s",
614 get_extcap_dir());
615  
616 argv[0] = g_strdup(EXTCAP_ARGUMENT_CONFIG);
617 argv[1] = g_strdup(EXTCAP_ARGUMENT_INTERFACE);
618 argv[2] = g_strdup(ifname);
619  
620 extcap_foreach(3, argv, search_cb, &ret, err_str, ifname);
621  
622 for (i = 0; i < 3; i++)
623 g_free(argv[i]);
624 }
625  
626 return ret;
627 }
628  
629 /**
630 * If is_required is FALSE: returns TRUE if the extcap interface has
631 * configurable options.
632 * If is_required is TRUE: returns TRUE when the extcap interface has
633 * configurable options that required modification. (For example, when an
634 * argument is required but empty.)
635 */
636 gboolean
637 extcap_has_configuration(const char * ifname, gboolean is_required) {
638 GList * arguments = 0;
639 GList * walker = 0, * item = 0;
640  
641 gboolean found = FALSE;
642  
643 arguments = extcap_get_if_configuration((const char *)( ifname ) );
644 walker = g_list_first(arguments);
645  
646 while ( walker != NULL && ! found ) {
647 item = g_list_first((GList *)(walker->data));
648 while ( item != NULL && ! found ) {
649 if ( (extcap_arg *)(item->data) != NULL ) {
650 extcap_arg * arg = (extcap_arg *)(item->data);
651 /* Should required options be present, or any kind of options */
652 if ( ! is_required )
653 found = TRUE;
654 else if ( arg->is_required ) {
655 const gchar * stored = NULL;
656 const gchar * defval = NULL;
657  
658 if (arg->pref_valptr != NULL)
659 stored = *arg->pref_valptr;
660  
661 if ( arg->default_complex != NULL && arg->default_complex->_val != NULL )
662 defval = arg->default_complex->_val;
663  
664 if ( arg->is_required ) {
665 /* If stored and defval is identical and the argument is required,
666 * configuration is needed */
667 if ( defval && stored && g_strcmp0(stored, defval) == 0 )
668 found = TRUE;
669 else if ( ! defval && (!stored || !*stored) )
670 found = TRUE;
671 }
672  
673 if ( arg->arg_type == EXTCAP_ARG_FILESELECT ) {
674 if ( arg->fileexists && ! ( file_exists(defval) || file_exists(stored) ) )
675 found = TRUE;
676 }
677 }
678 }
679  
680 item = item->next;
681 }
682 walker = walker->next;
683 }
684 extcap_free_if_configuration(arguments);
685  
686 return found;
687 }
688  
689 /* taken from capchild/capture_sync.c */
690 static gboolean pipe_data_available(int pipe_fd) {
691 #ifdef _WIN32 /* PeekNamedPipe */
692 HANDLE hPipe = (HANDLE) _get_osfhandle(pipe_fd);
693 DWORD bytes_avail;
694  
695 if (hPipe == INVALID_HANDLE_VALUE)
696 return FALSE;
697  
698 if (! PeekNamedPipe(hPipe, NULL, 0, NULL, &bytes_avail, NULL))
699 return FALSE;
700  
701 if (bytes_avail > 0)
702 return TRUE;
703 return FALSE;
704 #else /* select */
705 fd_set rfds;
706 struct timeval timeout;
707  
708 FD_ZERO(&rfds);
709 FD_SET(pipe_fd, &rfds);
710 timeout.tv_sec = 0;
711 timeout.tv_usec = 0;
712  
713 if (select(pipe_fd+1, &rfds, NULL, NULL, &timeout) > 0)
714 return TRUE;
715  
716 return FALSE;
717 #endif
718 }
719  
720 void extcap_if_cleanup(capture_options * capture_opts, gchar ** errormsg) {
721 interface_options interface_opts;
722 extcap_userdata * userdata;
723 guint icnt = 0;
724 gboolean overwrite_exitcode;
725 gchar * buffer;
726 #define STDERR_BUFFER_SIZE 1024
727  
728 for (icnt = 0; icnt < capture_opts->ifaces->len; icnt++) {
729 interface_opts = g_array_index(capture_opts->ifaces, interface_options,
730 icnt);
731  
732 /* skip native interfaces */
733 if (interface_opts.if_type != IF_EXTCAP)
734 continue;
735  
736 overwrite_exitcode = FALSE;
737  
738 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
739 "Extcap [%s] - Cleaning up fifo: %s; PID: %d", interface_opts.name,
740 interface_opts.extcap_fifo, interface_opts.extcap_pid);
741 #ifdef _WIN32
742 if (pipe_h)
743 {
744 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
745 "Extcap [%s] - Closing pipe", interface_opts.name);
746 FlushFileBuffers(pipe_h);
747 DisconnectNamedPipe(pipe_h);
748 CloseHandle(pipe_h);
749 }
750 #else
751 if (interface_opts.extcap_fifo != NULL && file_exists(interface_opts.extcap_fifo))
752 {
753 /* the fifo will not be freed here, but with the other capture_opts in capture_sync */
754 ws_unlink(interface_opts.extcap_fifo);
755 interface_opts.extcap_fifo = NULL;
756 }
757 #endif
758 /* Maybe the client closed and removed fifo, but ws should check if
759 * pid should be closed */
760 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
761 "Extcap [%s] - Closing spawned PID: %d", interface_opts.name,
762 interface_opts.extcap_pid);
763  
764 userdata = (extcap_userdata *) interface_opts.extcap_userdata;
765 if ( userdata )
766 {
767 if (userdata->extcap_stderr_rd > 0 && pipe_data_available(userdata->extcap_stderr_rd) )
768 {
769 buffer = (gchar * )g_malloc0(sizeof(gchar) * STDERR_BUFFER_SIZE + 1);
770 #ifdef _WIN32
771 win32_readfrompipe((HANDLE)_get_osfhandle(userdata->extcap_stderr_rd), STDERR_BUFFER_SIZE, buffer);
772 #else
773 if (read(userdata->extcap_stderr_rd, buffer, sizeof(gchar) * STDERR_BUFFER_SIZE) <= 0 )
774 buffer[0] = '\0';
775 #endif
776 if ( strlen ( buffer) > 0 )
777 {
778 userdata->extcap_stderr = g_strdup_printf("%s", buffer);
779 userdata->exitcode = 1;
780 }
781 g_free(buffer);
782 }
783  
784 #ifndef _WIN32
785 /* Final child watch may not have been called */
786 if ( interface_opts.extcap_child_watch != 0 )
787 {
788 extcap_child_watch_cb(userdata->pid, 0, capture_opts);
789 /* it will have changed in extcap_child_watch_cb */
790 interface_opts = g_array_index(capture_opts->ifaces, interface_options,
791 icnt);
792 }
793 #endif
794  
795 if ( userdata->extcap_stderr != NULL )
796 overwrite_exitcode = TRUE;
797  
798 if ( overwrite_exitcode || userdata->exitcode != 0 )
799 {
800 if ( userdata->extcap_stderr != 0 )
801 {
802 if ( *errormsg == NULL )
803 *errormsg = g_strdup_printf("Error by extcap pipe: %s", userdata->extcap_stderr);
804 else
805 {
806 gchar * temp = g_strconcat ( *errormsg, "\nError by extcap pipe: " ,userdata->extcap_stderr, NULL );
807 g_free(*errormsg);
808 *errormsg = temp;
809 }
810 g_free (userdata->extcap_stderr );
811 }
812  
813 userdata->extcap_stderr = NULL;
814 userdata->exitcode = 0;
815 }
816 }
817  
818 if (interface_opts.extcap_child_watch > 0)
819 {
820 g_source_remove(interface_opts.extcap_child_watch);
821 interface_opts.extcap_child_watch = 0;
822 }
823  
824 if (interface_opts.extcap_pid != INVALID_EXTCAP_PID)
825 {
826 #ifdef _WIN32
827 TerminateProcess(interface_opts.extcap_pid, 0);
828 #endif
829 g_spawn_close_pid(interface_opts.extcap_pid);
830 interface_opts.extcap_pid = INVALID_EXTCAP_PID;
831  
832 g_free(interface_opts.extcap_userdata);
833 interface_opts.extcap_userdata = NULL;
834 }
835  
836 /* Make sure modified interface_opts is saved in capture_opts. */
837 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, icnt);
838 g_array_insert_val(capture_opts->ifaces, icnt, interface_opts);
839 }
840 }
841  
842 static gboolean
843 extcap_add_arg_and_remove_cb(gpointer key, gpointer value, gpointer data) {
844 GPtrArray *args = (GPtrArray *)data;
845  
846 if ( key != NULL )
847 {
848 g_ptr_array_add(args, g_strdup((const gchar*)key));
849  
850 if ( value != NULL )
851 g_ptr_array_add(args, g_strdup((const gchar*)value));
852  
853 return TRUE;
854 }
855  
856 return FALSE;
857 }
858  
859 void extcap_child_watch_cb(GPid pid, gint status, gpointer user_data)
860 {
861 guint i;
862 interface_options interface_opts;
863 extcap_userdata * userdata = NULL;
864 capture_options * capture_opts = (capture_options *)(user_data);
865  
866 if ( capture_opts == NULL || capture_opts->ifaces == NULL || capture_opts->ifaces->len == 0 )
867 return;
868  
869 /* Close handle to child process. */
870 g_spawn_close_pid(pid);
871  
872 /* Update extcap_pid in interface options structure. */
873 for (i = 0; i < capture_opts->ifaces->len; i++)
874 {
875 interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
876 if (interface_opts.extcap_pid == pid)
877 {
878 userdata = (extcap_userdata *)interface_opts.extcap_userdata;
879 if ( userdata != NULL )
880 {
881 interface_opts.extcap_pid = INVALID_EXTCAP_PID;
882 userdata->exitcode = 0;
883 #ifndef _WIN32
884 if ( WIFEXITED(status) )
885 {
886 if ( WEXITSTATUS(status) != 0 )
887 userdata->exitcode = WEXITSTATUS(status);
888 }
889 else
890 userdata->exitcode = G_SPAWN_ERROR_FAILED;
891 #else
892 if (status != 0)
893 userdata->exitcode = status;
894 #endif
895 if ( status == 0 && userdata->extcap_stderr != NULL )
896 userdata->exitcode = 1;
897 }
898 g_source_remove(interface_opts.extcap_child_watch);
899 interface_opts.extcap_child_watch = 0;
900  
901 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, i);
902 g_array_insert_val(capture_opts->ifaces, i, interface_opts);
903 break;
904 }
905 }
906 }
907  
908 static
909 GPtrArray * extcap_prepare_arguments(interface_options interface_opts)
910 {
911 GPtrArray *result = NULL;
912 #if ARG_DEBUG
913 gchar **tmp;
914 int tmp_i;
915 #endif
916  
917 if (interface_opts.if_type == IF_EXTCAP )
918 {
919 result = g_ptr_array_new();
920  
921 #define add_arg(X) g_ptr_array_add(result, g_strdup(X))
922  
923 add_arg(interface_opts.extcap);
924 add_arg(EXTCAP_ARGUMENT_RUN_CAPTURE);
925 add_arg(EXTCAP_ARGUMENT_INTERFACE);
926 add_arg(interface_opts.name);
927 if (interface_opts.cfilter && strlen(interface_opts.cfilter) > 0) {
928 add_arg(EXTCAP_ARGUMENT_CAPTURE_FILTER);
929 add_arg(interface_opts.cfilter);
930 }
931 add_arg(EXTCAP_ARGUMENT_RUN_PIPE);
932 add_arg(interface_opts.extcap_fifo);
933 if (interface_opts.extcap_args == NULL || g_hash_table_size(interface_opts.extcap_args) == 0)
934 {
935 /* User did not perform interface configuration.
936 *
937 * Check if there are any boolean flags that are set by default
938 * and hence their argument should be added.
939 */
940 GList *arglist;
941 GList *elem;
942  
943 arglist = extcap_get_if_configuration(interface_opts.name);
944 for (elem = g_list_first(arglist); elem; elem = elem->next)
945 {
946 GList * arg_list;
947 extcap_arg *arg_iter;
948  
949 if (elem->data == NULL)
950 {
951 continue;
952 }
953  
954 arg_list = g_list_first((GList *)elem->data);
955 while (arg_list != NULL) {
956 const gchar * stored = NULL, * defval = NULL;
957 /* In case of boolflags only first element in arg_list is relevant. */
958 arg_iter = (extcap_arg*) (arg_list->data);
959 if (arg_iter->pref_valptr != NULL)
960 stored = *arg_iter->pref_valptr;
961  
962 if ( arg_iter->default_complex != NULL && arg_iter->default_complex->_val != NULL )
963 defval = arg_iter->default_complex->_val;
964  
965 /* Different data in storage then set for default */
966 if ( g_strcmp0(stored, defval) != 0 ) {
967 if ( arg_iter->arg_type == EXTCAP_ARG_BOOLFLAG ) {
968 if ( g_strcmp0(stored, "true") == 0 )
969 add_arg(arg_iter->call);
970 } else {
971 add_arg(arg_iter->call);
972 add_arg(stored);
973 }
974 } else if (arg_iter->arg_type == EXTCAP_ARG_BOOLFLAG) {
975 if (extcap_complex_get_bool(arg_iter->default_complex))
976 add_arg(arg_iter->call);
977 }
978  
979 arg_list = arg_list->next;
980 }
981 }
982  
983 extcap_free_if_configuration(arglist);
984 }
985 else
986 {
987 g_hash_table_foreach_remove(interface_opts.extcap_args, extcap_add_arg_and_remove_cb, result);
988 }
989 add_arg(NULL);
990 #undef add_arg
991  
992 #if ARG_DEBUG
993 /* Dump commandline parameters sent to extcap. */
994 for (tmp = (gchar **)result->pdata, tmp_i = 0; *tmp && **tmp; ++tmp_i, ++tmp)
995 {
996 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "argv[%d]: %s", tmp_i, *tmp);
997 }
998 #endif
999  
1000 }
1001  
1002 return result;
1003 }
1004  
1005 /* call mkfifo for each extcap,
1006 * returns FALSE if there's an error creating a FIFO */
1007 gboolean
1008 extcap_init_interfaces(capture_options *capture_opts)
1009 {
1010 guint i;
1011 interface_options interface_opts;
1012 extcap_userdata * userdata;
1013  
1014 for (i = 0; i < capture_opts->ifaces->len; i++)
1015 {
1016 GPtrArray *args = NULL;
1017 GPid pid = INVALID_EXTCAP_PID;
1018  
1019 interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
1020  
1021 /* skip native interfaces */
1022 if (interface_opts.if_type != IF_EXTCAP )
1023 continue;
1024  
1025 /* create pipe for fifo */
1026 if ( ! extcap_create_pipe ( &interface_opts.extcap_fifo ) )
1027 return FALSE;
1028  
1029 /* Create extcap call */
1030 args = extcap_prepare_arguments(interface_opts);
1031  
1032 userdata = g_new0(extcap_userdata, 1);
1033  
1034 pid = extcap_spawn_async(userdata, args );
1035  
1036 g_ptr_array_foreach(args, (GFunc)g_free, NULL);
1037 g_ptr_array_free(args, TRUE);
1038  
1039 if ( pid == INVALID_EXTCAP_PID )
1040 {
1041 g_free(userdata);
1042 continue;
1043 }
1044  
1045 interface_opts.extcap_pid = pid;
1046  
1047 interface_opts.extcap_child_watch =
1048 g_child_watch_add(pid, extcap_child_watch_cb, (gpointer)capture_opts);
1049  
1050 #ifdef _WIN32
1051 /* On Windows, wait for extcap to connect to named pipe.
1052 * Some extcaps will present UAC screen to user.
1053 * 30 second timeout should be reasonable timeout for extcap to
1054 * connect to named pipe (including user interaction).
1055 * Wait on multiple object in case of extcap termination
1056 * without opening pipe.
1057 *
1058 * Minimum supported version of Windows: XP / Server 2003.
1059 */
1060 if (pid != INVALID_EXTCAP_PID)
1061 {
1062 extcap_wait_for_pipe(pipe_h, pid);
1063 }
1064  
1065 #endif
1066  
1067 interface_opts.extcap_userdata = (gpointer) userdata;
1068  
1069 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, i);
1070 g_array_insert_val(capture_opts->ifaces, i, interface_opts);
1071 }
1072  
1073 return TRUE;
1074 }
1075  
1076 #ifdef _WIN32
1077 /* called by capture_sync to get the CreatNamedPipe handle*/
1078 HANDLE
1079 extcap_get_win32_handle()
1080 {
1081 return pipe_h;
1082 }
1083 #endif
1084  
1085 gboolean extcap_create_pipe(char ** fifo)
1086 {
1087 #ifdef _WIN32
1088 gchar timestr[ 14+1 ];
1089 time_t current_time;
1090  
1091 gchar *pipename = NULL;
1092  
1093 SECURITY_ATTRIBUTES security;
1094 /* create pipename */
1095 current_time = time(NULL);
1096 /*
1097 * XXX - we trust Windows not to return a time before the Epoch here,
1098 * so we won't get a null pointer back from localtime().
1099 */
1100 strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S", localtime(&current_time));
1101 pipename = g_strconcat ( "\\\\.\\pipe\\", EXTCAP_PIPE_PREFIX, "_", timestr, NULL );
1102  
1103 /* Security struct to enable Inheritable HANDLE */
1104 memset(&security, 0, sizeof(SECURITY_ATTRIBUTES));
1105 security.nLength = sizeof(SECURITY_ATTRIBUTES);
1106 security.bInheritHandle = TRUE;
1107 security.lpSecurityDescriptor = NULL;
1108  
1109 /* create a namedPipe*/
1110 pipe_h = CreateNamedPipe(
1111 utf_8to16(pipename),
1112 PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1113 PIPE_TYPE_MESSAGE| PIPE_READMODE_MESSAGE | PIPE_WAIT,
1114 5, 65536, 65536,
1115 300,
1116 &security);
1117  
1118 if (pipe_h == INVALID_HANDLE_VALUE)
1119 {
1120 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,"\nError creating pipe => (%d)", GetLastError());
1121 return FALSE;
1122 }
1123 else
1124 {
1125 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,"\nWireshark Created pipe =>(%s)",pipename);
1126 *fifo = g_strdup(pipename);
1127 }
1128 #else
1129 gchar *temp_name = NULL;
1130 int fd = 0;
1131  
1132 if ((fd = create_tempfile(&temp_name, EXTCAP_PIPE_PREFIX, NULL)) < 0 )
1133 return FALSE;
1134  
1135 ws_close(fd);
1136  
1137 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
1138 "Extcap - Creating fifo: %s", temp_name);
1139  
1140 if ( file_exists(temp_name) )
1141 ws_unlink(temp_name);
1142  
1143 if (mkfifo(temp_name, 0600) == 0)
1144 *fifo = g_strdup(temp_name);
1145 #endif
1146  
1147 return TRUE;
1148 }
1149  
1150 #if ARG_DEBUG
1151 void extcap_debug_arguments ( extcap_arg *arg_iter )
1152 {
1153 extcap_value *v = NULL;
1154 GList *walker = NULL;
1155  
1156 printf("debug - parser dump\n");
1157 while (arg_iter != NULL) {
1158 printf("ARG %d call=%s display=\"%s\" type=", arg_iter->arg_num, arg_iter->call, arg_iter->display);
1159  
1160 switch (arg_iter->arg_type) {
1161 case EXTCAP_ARG_INTEGER:
1162 printf("int\n");
1163 break;
1164 case EXTCAP_ARG_UNSIGNED:
1165 printf("unsigned\n");
1166 break;
1167 case EXTCAP_ARG_LONG:
1168 printf("long\n");
1169 break;
1170 case EXTCAP_ARG_DOUBLE:
1171 printf("double\n");
1172 break;
1173 case EXTCAP_ARG_BOOLEAN:
1174 printf("boolean\n");
1175 break;
1176 case EXTCAP_ARG_MENU:
1177 printf("menu\n");
1178 break;
1179 case EXTCAP_ARG_RADIO:
1180 printf("radio\n");
1181 break;
1182 case EXTCAP_ARG_SELECTOR:
1183 printf("selctor\n");
1184 break;
1185 case EXTCAP_ARG_STRING:
1186 printf ( "string\n" );
1187 break;
1188 case EXTCAP_ARG_PASSWORD:
1189 printf ( "PASSWORD\n" );
1190 break;
1191 case EXTCAP_ARG_MULTICHECK:
1192 printf ( "unknown\n" );
1193 break;
1194 case EXTCAP_ARG_UNKNOWN:
1195 printf ( "unknown\n" );
1196 break;
1197 }
1198  
1199 if (arg_iter->range_start != NULL && arg_iter->range_end != NULL) {
1200 printf("\tRange: ");
1201 extcap_printf_complex(arg_iter->range_start);
1202 printf(" - ");
1203 extcap_printf_complex(arg_iter->range_end);
1204 printf("\n");
1205 }
1206  
1207 for ( walker = g_list_first ( arg_iter->value_list ); walker; walker = walker->next )
1208 {
1209 v = (extcap_value *)walker->data;
1210 if (v->is_default)
1211 printf("*");
1212 printf("\tcall=\"%p\" display=\"%p\"\n", v->call, v->display);
1213 printf("\tcall=\"%s\" display=\"%s\"\n", v->call, v->display);
1214 }
1215  
1216 arg_iter = arg_iter->next_arg;
1217 }
1218 }
1219 #endif
1220  
1221 /*
1222 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1223 *
1224 * Local variables:
1225 * c-basic-offset: 4
1226 * tab-width: 8
1227 * indent-tabs-mode: nil
1228 * End:
1229 *
1230 * vi: set shiftwidth=4 tabstop=8 expandtab:
1231 * :indentSize=4:tabSize=8:noTabs=true:
1232 */