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 /* Prefix for the pipe interfaces */
66 #define EXTCAP_PIPE_PREFIX "wireshark_extcap"
68 /* Callback definition for extcap_foreach */
69 typedef gboolean (*extcap_cb_t)(const gchar *extcap, gchar *output, void *data,
72 /* #define ARG_DEBUG */
74 static void extcap_debug_arguments ( extcap_arg *arg_iter );
78 extcap_if_exists(const char *ifname)
84 if ( g_hash_table_size(ifaces) > 0 )
86 if ( g_hash_table_lookup(ifaces, (const gchar *)ifname) != NULL )
97 extcap_if_exists_for_extcap(const char *ifname, const char *extcap)
101 if ( extcap_if_exists(ifname) )
103 if ( ( entry = (gchar *)g_hash_table_lookup(ifaces, (const gchar *)ifname) ) != NULL )
105 if ( strcmp(entry, extcap) == 0 )
114 extcap_if_executable(const char *ifname)
116 if ( extcap_if_exists(ifname) )
117 return (gchar *)g_hash_table_lookup(ifaces, (const gchar *)ifname);
119 return (gchar *)NULL;
123 extcap_if_cleanup(void)
125 if ( ifaces == NULL )
126 ifaces = g_hash_table_new(g_str_hash, g_str_equal);
128 g_hash_table_remove_all(ifaces);
132 extcap_if_add(gchar *ifname, gchar *extcap)
134 if ( g_hash_table_lookup(ifaces, ifname) == NULL )
135 g_hash_table_insert(ifaces, ifname, extcap);
138 static void extcap_foreach(gint argc, gchar **args, extcap_cb_t cb,
139 void *cb_data, char **err_str, const char * ifname _U_) {
140 const char *dirname = get_extcap_dir();
148 argv = (gchar **) g_malloc0(sizeof(gchar *) * (argc + 2));
150 if ((dir = g_dir_open(dirname, 0, NULL)) != NULL) {
152 dirname = g_strescape(dirname,NULL);
154 while (keep_going && (file = g_dir_read_name(dir)) != NULL ) {
155 GString *extcap_string = NULL;
156 gchar *extcap = NULL;
157 gchar *command_output = NULL;
158 gboolean status = FALSE;
160 gint exit_status = 0;
161 GError *error = NULL;
163 /* full path to extcap binary */
164 extcap_string = g_string_new("");
166 g_string_printf(extcap_string, "%s\\\\%s",dirname,file);
167 extcap = g_string_free(extcap_string, FALSE);
169 g_string_printf(extcap_string, "%s/%s", dirname, file);
170 extcap = g_string_free(extcap_string, FALSE);
172 if ( extcap_if_exists(ifname) && !extcap_if_exists_for_extcap(ifname, extcap ) )
176 for (i = 0; i < argc; ++i)
180 status = g_spawn_sync(dirname, argv, NULL,
181 (GSpawnFlags) 0, NULL, NULL,
182 &command_output, NULL, &exit_status, &error);
184 if (status && exit_status == 0)
185 keep_going = cb(extcap, command_output, cb_data, err_str);
188 g_free(command_output);
197 static gboolean dlt_cb(const gchar *extcap _U_, gchar *output, void *data,
199 extcap_token_sentence *tokens;
200 extcap_dlt *dlts, *dlt_iter, *next;
201 if_capabilities_t *caps;
202 GList *linktype_list = NULL;
203 data_link_info_t *data_link_info;
205 tokens = extcap_tokenize_sentences(output);
206 extcap_parse_dlts(tokens, &dlts);
208 extcap_free_tokenized_sentence_list(tokens);
210 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap pipe %s ", extcap);
213 * Allocate the interface capabilities structure.
215 caps = (if_capabilities_t *) g_malloc(sizeof *caps);
216 caps->can_set_rfmon = FALSE;
219 while (dlt_iter != NULL ) {
220 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
221 " DLT %d name=\"%s\" display=\"%s\" ", dlt_iter->number,
222 dlt_iter->name, dlt_iter->display);
224 data_link_info = g_new(data_link_info_t, 1);
225 data_link_info->dlt = dlt_iter->number;
226 data_link_info->name = g_strdup(dlt_iter->name);
227 data_link_info->description = g_strdup(dlt_iter->display);
228 linktype_list = g_list_append(linktype_list, data_link_info);
229 dlt_iter = dlt_iter->next_dlt;
232 /* Check to see if we built a list */
233 if (linktype_list != NULL && data != NULL) {
234 caps->data_link_types = linktype_list;
235 *(if_capabilities_t **) data = caps;
238 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, " returned no DLTs");
239 *err_str = g_strdup("Extcap returned no DLTs");
245 while (dlt_iter != NULL ) {
246 next = dlt_iter->next_dlt;
247 extcap_free_dlt(dlt_iter);
255 extcap_get_if_dlts(const gchar *ifname, char **err_str) {
258 if_capabilities_t *caps = NULL;
260 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, " returned no DLTs");
262 if (ifname != NULL && err_str != NULL)
265 if ( extcap_if_exists(ifname) )
267 argv[0] = g_strdup(EXTCAP_ARGUMENT_LIST_DLTS);
268 argv[1] = g_strdup(EXTCAP_ARGUMENT_INTERFACE);
269 argv[2] = g_strdup(ifname);
273 extcap_foreach(3, argv, dlt_cb, &caps, err_str, ifname);
275 for (i = 0; i < 3; ++i)
282 static gboolean interfaces_cb(const gchar *extcap, gchar *output, void *data,
283 char **err_str _U_) {
284 GList **il = (GList **) data;
285 extcap_token_sentence *tokens;
286 extcap_interface *interfaces, *int_iter; /*, *next; */
289 tokens = extcap_tokenize_sentences(output);
290 extcap_parse_interfaces(tokens, &interfaces);
292 extcap_free_tokenized_sentence_list(tokens);
294 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap pipe %s ", extcap);
296 int_iter = interfaces;
297 while (int_iter != NULL ) {
298 if ( extcap_if_exists(int_iter->call) )
300 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_WARNING, "Extcap interface \"%s\" is already provided by \"%s\" ",
301 int_iter->call, (gchar *)extcap_if_executable(int_iter->call) );
302 int_iter = int_iter->next_interface;
306 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, " Interface [%s] \"%s\" ",
307 int_iter->call, int_iter->display);
309 if_info = g_new0(if_info_t, 1);
310 if_info->name = g_strdup(int_iter->call);
311 if_info->friendly_name = g_strdup(int_iter->display);
313 if_info->type = IF_EXTCAP;
315 if_info->extcap = g_strdup(extcap);
316 *il = g_list_append(*il, if_info);
318 extcap_if_add(g_strdup(int_iter->call), g_strdup(extcap) );
319 int_iter = int_iter->next_interface;
326 extcap_interface_list(char **err_str) {
336 argv = g_strdup(EXTCAP_ARGUMENT_LIST_INTERFACES);
340 extcap_foreach(1, &argv, interfaces_cb, &ret, err_str, NULL);
347 static gboolean search_cb(const gchar *extcap _U_, gchar *output, void *data,
348 char **err_str _U_) {
349 extcap_token_sentence *tokens = NULL;
350 GList *arguments = NULL;
351 GList **il = (GList **) data;
353 tokens = extcap_tokenize_sentences(output);
354 arguments = extcap_parse_args(tokens);
356 extcap_free_tokenized_sentence_list(tokens);
359 extcap_debug_arguments ( arguments );
362 *il = g_list_append(*il, arguments);
364 /* By returning false, extcap_foreach will break on first found */
369 extcap_get_if_configuration(const char * ifname) {
372 gchar **err_str = NULL;
374 if ( extcap_if_exists(ifname) )
376 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap path %s",
382 argv[0] = g_strdup(EXTCAP_ARGUMENT_CONFIG);
383 argv[1] = g_strdup(EXTCAP_ARGUMENT_INTERFACE);
384 argv[2] = g_strdup(ifname);
387 extcap_foreach(4, argv, search_cb, &ret, err_str, ifname);
393 void extcap_cleanup(capture_options * capture_opts) {
394 interface_options interface_opts;
397 for (icnt = 0; icnt < capture_opts->ifaces->len; icnt++) {
398 interface_opts = g_array_index(capture_opts->ifaces, interface_options,
401 /* skip native interfaces */
402 if (interface_opts.if_type != IF_EXTCAP)
405 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
406 "Extcap [%s] - Cleaning up fifo: %s; PID: %d", interface_opts.name,
407 interface_opts.extcap_fifo, interface_opts.extcap_pid);
411 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
412 "Extcap [%s] - Closing pipe", interface_opts.name);
413 FlushFileBuffers(pipe_h);
414 DisconnectNamedPipe(pipe_h);
418 if (interface_opts.extcap_fifo != NULL && file_exists(interface_opts.extcap_fifo))
420 /* the fifo will not be freed here, but with the other capture_opts in capture_sync */
421 ws_unlink(interface_opts.extcap_fifo);
422 interface_opts.extcap_fifo = NULL;
425 /* Maybe the client closed and removed fifo, but ws should check if
426 * pid should be closed */
427 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
428 "Extcap [%s] - Closing spawned PID: %d", interface_opts.name,
429 interface_opts.extcap_pid);
431 if (interface_opts.extcap_child_watch > 0)
433 g_source_remove(interface_opts.extcap_child_watch);
434 interface_opts.extcap_child_watch = 0;
437 if (interface_opts.extcap_pid != INVALID_EXTCAP_PID)
440 TerminateProcess(interface_opts.extcap_pid, 0);
442 g_spawn_close_pid(interface_opts.extcap_pid);
443 interface_opts.extcap_pid = INVALID_EXTCAP_PID;
446 /* Make sure modified interface_opts is saved in capture_opts. */
447 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, icnt);
448 g_array_insert_val(capture_opts->ifaces, icnt, interface_opts);
453 extcap_arg_cb(gpointer key, gpointer value, gpointer data) {
454 GPtrArray *args = (GPtrArray *)data;
458 g_ptr_array_add(args, key);
461 g_ptr_array_add(args, value);
465 static void extcap_child_watch_cb(GPid pid, gint status _U_, gpointer user_data)
468 interface_options interface_opts;
469 capture_options *capture_opts = (capture_options *)user_data;
471 /* Close handle to child process. */
472 g_spawn_close_pid(pid);
474 /* Update extcap_pid in interface options structure. */
475 for (i = 0; i < capture_opts->ifaces->len; i++)
477 interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
478 if (interface_opts.extcap_pid == pid)
480 interface_opts.extcap_pid = INVALID_EXTCAP_PID;
481 interface_opts.extcap_child_watch = 0;
482 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, i);
483 g_array_insert_val(capture_opts->ifaces, i, interface_opts);
489 /* call mkfifo for each extcap,
490 * returns FALSE if there's an error creating a FIFO */
492 extcaps_init_initerfaces(capture_options *capture_opts)
495 interface_options interface_opts;
497 for (i = 0; i < capture_opts->ifaces->len; i++)
499 GPtrArray *args = NULL;
500 GPid pid = INVALID_EXTCAP_PID;
504 interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
506 /* skip native interfaces */
507 if (interface_opts.if_type != IF_EXTCAP )
510 /* create pipe for fifo */
511 if ( ! extcap_create_pipe ( &interface_opts.extcap_fifo ) )
514 /* Create extcap call */
515 args = g_ptr_array_new();
516 #define add_arg(X) g_ptr_array_add(args, g_strdup(X))
518 add_arg(interface_opts.extcap);
519 add_arg(EXTCAP_ARGUMENT_RUN_CAPTURE);
520 add_arg(EXTCAP_ARGUMENT_INTERFACE);
521 add_arg(interface_opts.name);
522 add_arg(EXTCAP_ARGUMENT_RUN_PIPE);
523 add_arg(interface_opts.extcap_fifo);
524 if (interface_opts.extcap_args != NULL)
525 g_hash_table_foreach(interface_opts.extcap_args, extcap_arg_cb, args);
529 /* Dump commandline parameters sent to extcap. */
530 for (tmp = (gchar **)args->pdata, tmp_i = 0; *tmp && **tmp; ++tmp_i, ++tmp)
532 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "argv[%d]: %s", tmp_i, *tmp);
535 /* Wireshark for windows crashes here sometimes *
536 * Access violation reading location 0x... */
537 g_spawn_async(NULL, (gchar **)args->pdata, NULL,
538 (GSpawnFlags) G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL,
541 g_ptr_array_foreach(args, (GFunc)g_free, NULL);
542 g_ptr_array_free(args, TRUE);
543 interface_opts.extcap_pid = pid;
544 interface_opts.extcap_child_watch =
545 g_child_watch_add(pid, extcap_child_watch_cb, (gpointer)capture_opts);
546 capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, i);
547 g_array_insert_val(capture_opts->ifaces, i, interface_opts);
550 /* On Windows, wait for extcap to connect to named pipe.
551 * Some extcaps will present UAC screen to user.
552 * 30 second timeout should be reasonable timeout for extcap to
553 * connect to named pipe (including user interaction).
554 * Wait on multiple object in case of extcap termination
555 * without opening pipe.
557 * Minimum supported version of Windows: XP / Server 2003.
559 if (pid != INVALID_EXTCAP_PID)
565 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
567 ConnectNamedPipe(pipe_h, &ov);
568 handles[0] = ov.hEvent;
571 if (GetLastError() == ERROR_PIPE_CONNECTED)
573 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "extcap connected to pipe");
577 dw = WaitForMultipleObjects(2, handles, FALSE, 30000);
578 if (dw == WAIT_OBJECT_0)
580 /* ConnectNamedPipe finished. */
583 code = GetLastError();
584 if (code == ERROR_IO_PENDING)
587 if (!GetOverlappedResult(ov.hEvent, &ov, &dummy, TRUE))
589 code = GetLastError();
593 code = ERROR_SUCCESS;
597 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "ConnectNamedPipe code: %d", code);
599 else if (dw == (WAIT_OBJECT_0 + 1))
601 /* extcap process terminated. */
602 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "extcap terminated without connecting to pipe!");
604 else if (dw == WAIT_TIMEOUT)
606 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "extcap didn't connect to pipe within 30 seconds!");
610 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "WaitForMultipleObjects returned 0x%08X. Error %d", dw, GetLastError());
614 CloseHandle(ov.hEvent);
623 /* called by capture_sync to get the CreatNamedPipe handle*/
625 extcap_get_win32_handle()
631 gboolean extcap_create_pipe(char ** fifo)
634 gchar timestr[ 14+1 ];
637 gchar *pipename = NULL;
639 SECURITY_ATTRIBUTES security;
640 /* create pipename */
641 current_time = time(NULL);
642 strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S", localtime(¤t_time));
643 pipename = g_strconcat ( "\\\\.\\pipe\\", EXTCAP_PIPE_PREFIX, "_", timestr, NULL );
645 /* Security struct to enable Inheritable HANDLE */
646 memset(&security, 0, sizeof(SECURITY_ATTRIBUTES));
647 security.nLength = sizeof(SECURITY_ATTRIBUTES);
648 security.bInheritHandle = TRUE;
649 security.lpSecurityDescriptor = NULL;
651 /* create a namedPipe*/
652 pipe_h = CreateNamedPipe(
654 PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
655 PIPE_TYPE_MESSAGE| PIPE_READMODE_MESSAGE | PIPE_WAIT,
660 if (pipe_h == INVALID_HANDLE_VALUE)
662 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,"\nError creating pipe => (%d)", GetLastError());
667 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,"\nWireshark Created pipe =>(%s)",pipename);
668 *fifo = g_strdup(pipename);
671 gchar *temp_name = NULL;
674 if ( ( fd = create_tempfile ( &temp_name, EXTCAP_PIPE_PREFIX ) ) == 0 )
679 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
680 "Extcap - Creating fifo: %s", temp_name);
682 if ( file_exists(temp_name) )
683 ws_unlink(temp_name);
685 if (mkfifo(temp_name, 0600) == 0)
686 *fifo = g_strdup(temp_name);
693 void extcap_debug_arguments ( extcap_arg *arg_iter )
695 extcap_value *v = NULL;
696 GList *walker = NULL;
698 printf("debug - parser dump\n");
699 while (arg_iter != NULL) {
700 printf("ARG %d call=%s display=\"%s\" type=", arg_iter->arg_num, arg_iter->call, arg_iter->display);
702 switch (arg_iter->arg_type) {
703 case EXTCAP_ARG_INTEGER:
706 case EXTCAP_ARG_UNSIGNED:
707 printf("unsigned\n");
709 case EXTCAP_ARG_LONG:
712 case EXTCAP_ARG_DOUBLE:
715 case EXTCAP_ARG_BOOLEAN:
718 case EXTCAP_ARG_MENU:
721 case EXTCAP_ARG_RADIO:
724 case EXTCAP_ARG_SELECTOR:
727 case EXTCAP_ARG_STRING:
728 printf ( "string\n" );
730 case EXTCAP_ARG_MULTICHECK:
731 printf ( "unknown\n" );
733 case EXTCAP_ARG_UNKNOWN:
734 printf ( "unknown\n" );
738 if (arg_iter->range_start != NULL && arg_iter->range_end != NULL) {
740 extcap_printf_complex(arg_iter->range_start);
742 extcap_printf_complex(arg_iter->range_end);
746 for ( walker = g_list_first ( arg_iter->value_list ); walker; walker = walker->next )
748 v = (extcap_value *)walker->data;
749 if (v->is_default == TRUE)
751 printf("\tcall=\"%p\" display=\"%p\"\n", v->call, v->display);
752 printf("\tcall=\"%s\" display=\"%s\"\n", v->call, v->display);
755 arg_iter = arg_iter->next_arg;
762 * Editor modelines - http://www.wireshark.org/tools/modelines.html
767 * indent-tabs-mode: t
770 * vi: set shiftwidth=4 tabstop=4 noexpandtab:
771 * :indentSize=4:tabSize=4:noTabs=false: