3 * Routines for extcap external capture
4 * Copyright 2013, Mike Ryan <mikeryan@lacklustre.net>
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * 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.
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.
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.
36 /* Include for unlink */
43 #include <wsutil/file_util.h>
44 #include <wsutil/filesystem.h>
45 #include <wsutil/tempfile.h>
47 #include "capture_opts.h"
52 #include "extcap_parser.h"
55 static HANDLE pipe_h = NULL;
58 /* internal container, for all the extcap interfaces that have been found.
59 * will be resetted by every call to extcap_interface_list() and is being
60 * used in extcap_get_if_* as well as extcaps_init_initerfaces to ensure,
61 * that only extcap interfaces are being given to underlying extcap programs
63 static GHashTable *ifaces = NULL;
65 /* Callback definition for extcap_foreach */
66 typedef gboolean (*extcap_cb_t)(const gchar *extcap, gchar *output, void *data,
69 /* #define ARG_DEBUG */
71 static void extcap_debug_arguments ( extcap_arg *arg_iter );
75 extcap_if_exists(const char *ifname)
81 if ( g_hash_table_size(ifaces) > 0 )
83 if ( g_hash_table_lookup(ifaces, (const gchar *)ifname) != NULL )
94 extcap_if_exists_for_extcap(const char *ifname, const char *extcap)
98 if ( extcap_if_exists(ifname) )
100 if ( ( entry = (gchar *)g_hash_table_lookup(ifaces, (const gchar *)ifname) ) != NULL )
102 if ( strcmp(entry, extcap) == 0 )
111 extcap_if_executable(const char *ifname)
113 if ( extcap_if_exists(ifname) )
114 return (gchar *)g_hash_table_lookup(ifaces, (const gchar *)ifname);
116 return (gchar *)NULL;
120 extcap_if_cleanup(void)
122 if ( ifaces == NULL )
123 ifaces = g_hash_table_new(g_str_hash, g_str_equal);
125 g_hash_table_remove_all(ifaces);
129 extcap_if_add(gchar *ifname, gchar *extcap)
131 if ( g_hash_table_lookup(ifaces, ifname) == NULL )
132 g_hash_table_insert(ifaces, ifname, extcap);
135 static void extcap_foreach(gint argc, gchar **args, extcap_cb_t cb,
136 void *cb_data, char **err_str, const char * ifname _U_) {
137 const char *dirname = get_extcap_dir();
143 gchar **dll_search_envp;
149 argv = (gchar **) g_malloc0(sizeof(gchar *) * (argc + 2));
153 * Make sure executables can find dependent DLLs and that they're *our*
154 * DLLs: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682586.aspx
155 * Alternatively we could create a simple wrapper exe similar to Create
156 * Hidden Process (http://www.commandline.co.uk/chp/).
158 dll_search_envp = g_get_environ();
159 progfile_dir = g_strdup_printf("%s;%s", get_progfile_dir(), g_environ_getenv(dll_search_envp, "Path"));
160 dll_search_envp = g_environ_setenv(dll_search_envp, "Path", progfile_dir, TRUE);
161 g_free(progfile_dir);
164 if ((dir = g_dir_open(dirname, 0, NULL)) != NULL) {
166 dirname = g_strescape(dirname,NULL);
168 while (keep_going && (file = g_dir_read_name(dir)) != NULL ) {
169 GString *extcap_string = NULL;
170 gchar *extcap = NULL;
171 gchar *command_output = NULL;
172 gboolean status = FALSE;
174 gint exit_status = 0;
175 GError *error = NULL;
178 /* full path to extcap binary */
179 extcap_string = g_string_new("");
181 g_string_printf(extcap_string, "%s\\\\%s",dirname,file);
182 extcap = g_string_free(extcap_string, FALSE);
183 envp = dll_search_envp;
185 g_string_printf(extcap_string, "%s/%s", dirname, file);
186 extcap = g_string_free(extcap_string, FALSE);
188 if ( extcap_if_exists(ifname) && !extcap_if_exists_for_extcap(ifname, extcap ) )
192 for (i = 0; i < argc; ++i)
196 status = g_spawn_sync(dirname, argv, envp,
197 (GSpawnFlags) 0, NULL, NULL,
198 &command_output, NULL, &exit_status, &error);
200 if (status && exit_status == 0)
201 keep_going = cb(extcap, command_output, cb_data, err_str);
204 g_free(command_output);
211 g_strfreev(dll_search_envp);
216 static gboolean dlt_cb(const gchar *extcap _U_, gchar *output, void *data,
218 extcap_token_sentence *tokens;
219 extcap_dlt *dlts, *dlt_iter, *next;
220 if_capabilities_t *caps;
221 GList *linktype_list = NULL;
222 data_link_info_t *data_link_info;
224 tokens = extcap_tokenize_sentences(output);
225 extcap_parse_dlts(tokens, &dlts);
227 extcap_free_tokenized_sentence_list(tokens);
229 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap pipe %s ", extcap);
232 * Allocate the interface capabilities structure.
234 caps = (if_capabilities_t *) g_malloc(sizeof *caps);
235 caps->can_set_rfmon = FALSE;
238 while (dlt_iter != NULL ) {
239 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
240 " DLT %d name=\"%s\" display=\"%s\" ", dlt_iter->number,
241 dlt_iter->name, dlt_iter->display);
243 data_link_info = g_new(data_link_info_t, 1);
244 data_link_info->dlt = dlt_iter->number;
245 data_link_info->name = g_strdup(dlt_iter->name);
246 data_link_info->description = g_strdup(dlt_iter->display);
247 linktype_list = g_list_append(linktype_list, data_link_info);
248 dlt_iter = dlt_iter->next_dlt;
251 /* Check to see if we built a list */
252 if (linktype_list != NULL && data != NULL) {
253 caps->data_link_types = linktype_list;
254 *(if_capabilities_t **) data = caps;
257 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, " returned no DLTs");
258 *err_str = g_strdup("Extcap returned no DLTs");
264 while (dlt_iter != NULL ) {
265 next = dlt_iter->next_dlt;
266 extcap_free_dlt(dlt_iter);
274 extcap_get_if_dlts(const gchar *ifname, char **err_str) {
277 if_capabilities_t *caps = NULL;
279 if (ifname != NULL && err_str != NULL)
282 if ( extcap_if_exists(ifname) )
284 argv[0] = g_strdup(EXTCAP_ARGUMENT_LIST_DLTS);
285 argv[1] = g_strdup(EXTCAP_ARGUMENT_INTERFACE);
286 argv[2] = g_strdup(ifname);
290 extcap_foreach(3, argv, dlt_cb, &caps, err_str, ifname);
292 for (i = 0; i < 3; ++i)
299 static gboolean interfaces_cb(const gchar *extcap, gchar *output, void *data,
300 char **err_str _U_) {
301 GList **il = (GList **) data;
302 extcap_token_sentence *tokens;
303 extcap_interface *interfaces, *int_iter; /*, *next; */
306 tokens = extcap_tokenize_sentences(output);
307 extcap_parse_interfaces(tokens, &interfaces);
309 extcap_free_tokenized_sentence_list(tokens);
311 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap pipe %s ", extcap);
313 int_iter = interfaces;
314 while (int_iter != NULL ) {
315 if ( extcap_if_exists(int_iter->call) )
317 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_WARNING, "Extcap interface \"%s\" is already provided by \"%s\" ",
318 int_iter->call, (gchar *)extcap_if_executable(int_iter->call) );
319 int_iter = int_iter->next_interface;
323 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, " Interface [%s] \"%s\" ",
324 int_iter->call, int_iter->display);
326 if_info = g_new0(if_info_t, 1);
327 if_info->name = g_strdup(int_iter->call);
328 if_info->friendly_name = g_strdup(int_iter->display);
330 if_info->type = IF_EXTCAP;
332 if_info->extcap = g_strdup(extcap);
333 *il = g_list_append(*il, if_info);
335 extcap_if_add(g_strdup(int_iter->call), g_strdup(extcap) );
336 int_iter = int_iter->next_interface;
343 extcap_interface_list(char **err_str) {
353 argv = g_strdup(EXTCAP_ARGUMENT_LIST_INTERFACES);
357 extcap_foreach(1, &argv, interfaces_cb, &ret, err_str, NULL);
364 static void g_free_1(gpointer data, gpointer user_data _U_)
369 static void extcap_free_if_configuration(GList *list)
373 for (elem = g_list_first(list); elem; elem = elem->next)
376 if (elem->data == NULL)
381 arg_list = g_list_first((GList *)elem->data);
382 g_list_foreach(arg_list, g_free_1, NULL);
383 g_list_free(arg_list);
388 static gboolean search_cb(const gchar *extcap _U_, gchar *output, void *data,
389 char **err_str _U_) {
390 extcap_token_sentence *tokens = NULL;
391 GList *arguments = NULL;
392 GList **il = (GList **) data;
394 tokens = extcap_tokenize_sentences(output);
395 arguments = extcap_parse_args(tokens);
397 extcap_free_tokenized_sentence_list(tokens);
400 extcap_debug_arguments ( arguments );
403 *il = g_list_append(*il, arguments);
405 /* By returning false, extcap_foreach will break on first found */
410 extcap_get_if_configuration(const char * ifname) {
413 gchar **err_str = NULL;
415 if ( extcap_if_exists(ifname) )
417 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap path %s",
423 argv[0] = g_strdup(EXTCAP_ARGUMENT_CONFIG);
424 argv[1] = g_strdup(EXTCAP_ARGUMENT_INTERFACE);
425 argv[2] = g_strdup(ifname);
428 extcap_foreach(4, argv, search_cb, &ret, err_str, ifname);
434 void extcap_cleanup(capture_options * capture_opts) {
435 interface_options interface_opts;
438 for (icnt = 0; icnt < capture_opts->ifaces->len; icnt++) {
439 interface_opts = g_array_index(capture_opts->ifaces, interface_options,
442 /* skip native interfaces */
443 if (interface_opts.if_type != IF_EXTCAP)
446 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
447 "Extcap [%s] - Cleaning up fifo: %s; PID: %d", interface_opts.name,
448 interface_opts.extcap_fifo, interface_opts.extcap_pid);
452 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
453 "Extcap [%s] - Closing pipe", interface_opts.name);
454 FlushFileBuffers(pipe_h);
455 DisconnectNamedPipe(pipe_h);
459 if (interface_opts.extcap_fifo != NULL && file_exists(interface_opts.extcap_fifo))
461 /* the fifo will not be freed here, but with the other capture_opts in capture_sync */
462 ws_unlink(interface_opts.extcap_fifo);
463 interface_opts.extcap_fifo = NULL;
466 /* Maybe the client closed and removed fifo, but ws should check if
467 * pid should be closed */
468 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
469 "Extcap [%s] - Closing spawned PID: %d", interface_opts.name,
470 interface_opts.extcap_pid);
472 if (interface_opts.extcap_child_watch > 0)
474 g_source_remove(interface_opts.extcap_child_watch);
475 interface_opts.extcap_child_watch = 0;
478 if (interface_opts.extcap_pid != INVALID_EXTCAP_PID)
481 TerminateProcess(interface_opts.extcap_pid, 0);
483 g_spawn_close_pid(interface_opts.extcap_pid);
484 interface_opts.extcap_pid = INVALID_EXTCAP_PID;
487 /* Make sure modified interface_opts is saved in capture_opts. */
488 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, icnt);
489 g_array_insert_val(capture_opts->ifaces, icnt, interface_opts);
494 extcap_arg_cb(gpointer key, gpointer value, gpointer data) {
495 GPtrArray *args = (GPtrArray *)data;
499 g_ptr_array_add(args, g_strdup((const gchar*)key));
502 g_ptr_array_add(args, g_strdup((const gchar*)value));
506 static void extcap_child_watch_cb(GPid pid, gint status _U_, gpointer user_data)
509 interface_options interface_opts;
510 capture_options *capture_opts = (capture_options *)user_data;
512 /* Close handle to child process. */
513 g_spawn_close_pid(pid);
515 /* Update extcap_pid in interface options structure. */
516 for (i = 0; i < capture_opts->ifaces->len; i++)
518 interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
519 if (interface_opts.extcap_pid == pid)
521 interface_opts.extcap_pid = INVALID_EXTCAP_PID;
522 interface_opts.extcap_child_watch = 0;
523 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, i);
524 g_array_insert_val(capture_opts->ifaces, i, interface_opts);
530 /* call mkfifo for each extcap,
531 * returns FALSE if there's an error creating a FIFO */
533 extcaps_init_initerfaces(capture_options *capture_opts)
536 interface_options interface_opts;
538 for (i = 0; i < capture_opts->ifaces->len; i++)
540 GPtrArray *args = NULL;
541 GPid pid = INVALID_EXTCAP_PID;
545 interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
547 /* skip native interfaces */
548 if (interface_opts.if_type != IF_EXTCAP )
551 /* create pipe for fifo */
552 if ( ! extcap_create_pipe ( &interface_opts.extcap_fifo ) )
555 /* Create extcap call */
556 args = g_ptr_array_new();
557 #define add_arg(X) g_ptr_array_add(args, g_strdup(X))
559 add_arg(interface_opts.extcap);
560 add_arg(EXTCAP_ARGUMENT_RUN_CAPTURE);
561 add_arg(EXTCAP_ARGUMENT_INTERFACE);
562 add_arg(interface_opts.name);
563 add_arg(EXTCAP_ARGUMENT_RUN_PIPE);
564 add_arg(interface_opts.extcap_fifo);
565 if (interface_opts.extcap_args == NULL)
567 /* User did not perform interface configuration.
569 * Check if there are any boolean flags that are set by default
570 * and hence their argument should be added.
575 arglist = extcap_get_if_configuration(interface_opts.name);
576 for (elem = g_list_first(arglist); elem; elem = elem->next)
579 extcap_arg *arg_iter;
581 if (elem->data == NULL)
586 arg_list = g_list_first((GList *)elem->data);
587 while (arg_list != NULL)
589 /* In case of boolflags only first element in arg_list is relevant. */
590 arg_iter = (extcap_arg*) (arg_list->data);
592 if (arg_iter->arg_type == EXTCAP_ARG_BOOLFLAG)
594 if (arg_iter->default_complex != NULL
595 && extcap_complex_get_bool(arg_iter->default_complex))
597 add_arg(arg_iter->call);
601 arg_list = arg_list->next;
605 extcap_free_if_configuration(arglist);
609 g_hash_table_foreach(interface_opts.extcap_args, extcap_arg_cb, args);
614 /* Dump commandline parameters sent to extcap. */
615 for (tmp = (gchar **)args->pdata, tmp_i = 0; *tmp && **tmp; ++tmp_i, ++tmp)
617 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "argv[%d]: %s", tmp_i, *tmp);
620 /* Wireshark for windows crashes here sometimes *
621 * Access violation reading location 0x... */
622 g_spawn_async(NULL, (gchar **)args->pdata, NULL,
623 (GSpawnFlags) G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL,
626 g_ptr_array_foreach(args, (GFunc)g_free, NULL);
627 g_ptr_array_free(args, TRUE);
628 interface_opts.extcap_pid = pid;
629 interface_opts.extcap_child_watch =
630 g_child_watch_add(pid, extcap_child_watch_cb, (gpointer)capture_opts);
631 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, i);
632 g_array_insert_val(capture_opts->ifaces, i, interface_opts);
635 /* On Windows, wait for extcap to connect to named pipe.
636 * Some extcaps will present UAC screen to user.
637 * 30 second timeout should be reasonable timeout for extcap to
638 * connect to named pipe (including user interaction).
639 * Wait on multiple object in case of extcap termination
640 * without opening pipe.
642 * Minimum supported version of Windows: XP / Server 2003.
644 if (pid != INVALID_EXTCAP_PID)
650 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
652 ConnectNamedPipe(pipe_h, &ov);
653 handles[0] = ov.hEvent;
656 if (GetLastError() == ERROR_PIPE_CONNECTED)
658 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "extcap connected to pipe");
662 dw = WaitForMultipleObjects(2, handles, FALSE, 30000);
663 if (dw == WAIT_OBJECT_0)
665 /* ConnectNamedPipe finished. */
668 code = GetLastError();
669 if (code == ERROR_IO_PENDING)
672 if (!GetOverlappedResult(ov.hEvent, &ov, &dummy, TRUE))
674 code = GetLastError();
678 code = ERROR_SUCCESS;
682 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "ConnectNamedPipe code: %d", code);
684 else if (dw == (WAIT_OBJECT_0 + 1))
686 /* extcap process terminated. */
687 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "extcap terminated without connecting to pipe!");
689 else if (dw == WAIT_TIMEOUT)
691 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "extcap didn't connect to pipe within 30 seconds!");
695 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "WaitForMultipleObjects returned 0x%08X. Error %d", dw, GetLastError());
699 CloseHandle(ov.hEvent);
708 /* called by capture_sync to get the CreatNamedPipe handle*/
710 extcap_get_win32_handle()
716 gboolean extcap_create_pipe(char ** fifo)
719 gchar timestr[ 14+1 ];
722 gchar *pipename = NULL;
724 SECURITY_ATTRIBUTES security;
725 /* create pipename */
726 current_time = time(NULL);
727 strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S", localtime(¤t_time));
728 pipename = g_strconcat ( "\\\\.\\pipe\\", EXTCAP_PIPE_PREFIX, "_", timestr, NULL );
730 /* Security struct to enable Inheritable HANDLE */
731 memset(&security, 0, sizeof(SECURITY_ATTRIBUTES));
732 security.nLength = sizeof(SECURITY_ATTRIBUTES);
733 security.bInheritHandle = TRUE;
734 security.lpSecurityDescriptor = NULL;
736 /* create a namedPipe*/
737 pipe_h = CreateNamedPipe(
739 PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
740 PIPE_TYPE_MESSAGE| PIPE_READMODE_MESSAGE | PIPE_WAIT,
745 if (pipe_h == INVALID_HANDLE_VALUE)
747 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,"\nError creating pipe => (%d)", GetLastError());
752 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,"\nWireshark Created pipe =>(%s)",pipename);
753 *fifo = g_strdup(pipename);
756 gchar *temp_name = NULL;
759 if ( ( fd = create_tempfile ( &temp_name, EXTCAP_PIPE_PREFIX ) ) == 0 )
764 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
765 "Extcap - Creating fifo: %s", temp_name);
767 if ( file_exists(temp_name) )
768 ws_unlink(temp_name);
770 if (mkfifo(temp_name, 0600) == 0)
771 *fifo = g_strdup(temp_name);
778 void extcap_debug_arguments ( extcap_arg *arg_iter )
780 extcap_value *v = NULL;
781 GList *walker = NULL;
783 printf("debug - parser dump\n");
784 while (arg_iter != NULL) {
785 printf("ARG %d call=%s display=\"%s\" type=", arg_iter->arg_num, arg_iter->call, arg_iter->display);
787 switch (arg_iter->arg_type) {
788 case EXTCAP_ARG_INTEGER:
791 case EXTCAP_ARG_UNSIGNED:
792 printf("unsigned\n");
794 case EXTCAP_ARG_LONG:
797 case EXTCAP_ARG_DOUBLE:
800 case EXTCAP_ARG_BOOLEAN:
803 case EXTCAP_ARG_MENU:
806 case EXTCAP_ARG_RADIO:
809 case EXTCAP_ARG_SELECTOR:
812 case EXTCAP_ARG_STRING:
813 printf ( "string\n" );
815 case EXTCAP_ARG_MULTICHECK:
816 printf ( "unknown\n" );
818 case EXTCAP_ARG_UNKNOWN:
819 printf ( "unknown\n" );
823 if (arg_iter->range_start != NULL && arg_iter->range_end != NULL) {
825 extcap_printf_complex(arg_iter->range_start);
827 extcap_printf_complex(arg_iter->range_end);
831 for ( walker = g_list_first ( arg_iter->value_list ); walker; walker = walker->next )
833 v = (extcap_value *)walker->data;
834 if (v->is_default == TRUE)
836 printf("\tcall=\"%p\" display=\"%p\"\n", v->call, v->display);
837 printf("\tcall=\"%s\" display=\"%s\"\n", v->call, v->display);
840 arg_iter = arg_iter->next_arg;
847 * Editor modelines - http://www.wireshark.org/tools/modelines.html
852 * indent-tabs-mode: nil
855 * vi: set shiftwidth=4 tabstop=8 expandtab:
856 * :indentSize=4:tabSize=8:noTabs=true: