extcap: fix a crash introduced in g622603b
[metze/wireshark/wip.git] / extcap.c
1 /* extcap.h
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 #include <glib.h>
41 #include <log.h>
42
43 #include <wsutil/file_util.h>
44 #include <wsutil/filesystem.h>
45 #include <wsutil/tempfile.h>
46
47 #include "capture_opts.h"
48
49 #ifdef HAVE_EXTCAP
50
51 #include "extcap.h"
52 #include "extcap_parser.h"
53
54 #ifdef _WIN32
55 static HANDLE pipe_h = NULL;
56 #endif
57
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
62  */
63 static GHashTable *ifaces = NULL;
64
65 /* Callback definition for extcap_foreach */
66 typedef gboolean (*extcap_cb_t)(const gchar *extcap, gchar *output, void *data,
67         gchar **err_str);
68
69 /* #define ARG_DEBUG */
70 #if ARG_DEBUG
71 static void extcap_debug_arguments ( extcap_arg *arg_iter );
72 #endif
73
74 static gboolean
75 extcap_if_exists(const char *ifname)
76 {
77     if ( ifname != NULL )
78     {
79         if ( ifaces != NULL )
80         {
81             if ( g_hash_table_size(ifaces) > 0 )
82             {
83                 if ( g_hash_table_lookup(ifaces, (const gchar *)ifname) != NULL )
84                 {
85                     return TRUE;
86                 }
87             }
88         }
89     }
90     return FALSE;
91 }
92
93 static gboolean
94 extcap_if_exists_for_extcap(const char *ifname, const char *extcap)
95 {
96     gchar * entry = NULL;
97
98     if ( extcap_if_exists(ifname) )
99     {
100         if ( ( entry = (gchar *)g_hash_table_lookup(ifaces, (const gchar *)ifname) ) != NULL )
101         {
102             if ( strcmp(entry, extcap) == 0 )
103                 return TRUE;
104         }
105     }
106
107     return FALSE;
108 }
109
110 static gchar *
111 extcap_if_executable(const char *ifname)
112 {
113     if ( extcap_if_exists(ifname) )
114         return (gchar *)g_hash_table_lookup(ifaces, (const gchar *)ifname);
115
116     return (gchar *)NULL;
117 }
118
119 static void
120 extcap_if_cleanup(void)
121 {
122     if ( ifaces == NULL )
123         ifaces = g_hash_table_new(g_str_hash, g_str_equal);
124
125     g_hash_table_remove_all(ifaces);
126 }
127
128 static void
129 extcap_if_add(gchar *ifname, gchar *extcap)
130 {
131     if ( g_hash_table_lookup(ifaces, ifname) == NULL )
132         g_hash_table_insert(ifaces, ifname, extcap);
133 }
134
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();
138     GDir *dir;
139     const gchar *file;
140     gboolean keep_going;
141     gchar **argv;
142 #ifdef _WIN32
143     gchar **dll_search_envp;
144     gchar *progfile_dir;
145 #endif
146
147     keep_going = TRUE;
148
149     argv = (gchar **) g_malloc0(sizeof(gchar *) * (argc + 2));
150
151 #ifdef _WIN32
152     /*
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/).
157      */
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);
162 #endif
163
164     if ((dir = g_dir_open(dirname, 0, NULL)) != NULL) {
165 #ifdef _WIN32
166         dirname = g_strescape(dirname,NULL);
167 #endif
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;
173             gint i;
174             gint exit_status = 0;
175             GError *error = NULL;
176             gchar **envp = NULL;
177
178             /* full path to extcap binary */
179             extcap_string = g_string_new("");
180 #ifdef _WIN32
181             g_string_printf(extcap_string, "%s\\\\%s",dirname,file);
182             extcap = g_string_free(extcap_string, FALSE);
183             envp = dll_search_envp;
184 #else
185             g_string_printf(extcap_string, "%s/%s", dirname, file);
186             extcap = g_string_free(extcap_string, FALSE);
187 #endif
188             if ( extcap_if_exists(ifname) && !extcap_if_exists_for_extcap(ifname, extcap ) )
189                 continue;
190
191             argv[0] = extcap;
192             for (i = 0; i < argc; ++i)
193                 argv[i+1] = args[i];
194             argv[argc+1] = NULL;
195
196             status = g_spawn_sync(dirname, argv, envp,
197                 (GSpawnFlags) 0, NULL, NULL,
198                     &command_output, NULL, &exit_status, &error);
199
200             if (status && exit_status == 0)
201             keep_going = cb(extcap, command_output, cb_data, err_str);
202
203             g_free(extcap);
204             g_free(command_output);
205         }
206
207         g_dir_close(dir);
208     }
209
210 #ifdef _WIN32
211     g_strfreev(dll_search_envp);
212 #endif
213     g_free(argv);
214 }
215
216 static gboolean dlt_cb(const gchar *extcap _U_, gchar *output, void *data,
217         char **err_str) {
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;
223
224     tokens = extcap_tokenize_sentences(output);
225     extcap_parse_dlts(tokens, &dlts);
226
227     extcap_free_tokenized_sentence_list(tokens);
228
229     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap pipe %s ", extcap);
230
231     /*
232      * Allocate the interface capabilities structure.
233      */
234     caps = (if_capabilities_t *) g_malloc(sizeof *caps);
235     caps->can_set_rfmon = FALSE;
236
237     dlt_iter = dlts;
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);
242
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;
249     }
250
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;
255     } else {
256         if (err_str) {
257             g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "  returned no DLTs");
258             *err_str = g_strdup("Extcap returned no DLTs");
259         }
260         g_free(caps);
261     }
262
263     dlt_iter = dlts;
264     while (dlt_iter != NULL ) {
265         next = dlt_iter->next_dlt;
266         extcap_free_dlt(dlt_iter);
267         dlt_iter = next;
268     }
269
270     return FALSE;
271 }
272
273 if_capabilities_t *
274 extcap_get_if_dlts(const gchar *ifname, char **err_str) {
275     gchar *argv[3];
276     gint i;
277     if_capabilities_t *caps = NULL;
278
279     if (ifname != NULL && err_str != NULL)
280         *err_str = NULL;
281
282     if ( extcap_if_exists(ifname) )
283     {
284         argv[0] = g_strdup(EXTCAP_ARGUMENT_LIST_DLTS);
285         argv[1] = g_strdup(EXTCAP_ARGUMENT_INTERFACE);
286         argv[2] = g_strdup(ifname);
287
288         if (err_str)
289             *err_str = NULL;
290         extcap_foreach(3, argv, dlt_cb, &caps, err_str, ifname);
291
292         for (i = 0; i < 3; ++i)
293             g_free(argv[i]);
294     }
295
296     return caps;
297 }
298
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; */
304     if_info_t *if_info;
305
306     tokens = extcap_tokenize_sentences(output);
307     extcap_parse_interfaces(tokens, &interfaces);
308
309     extcap_free_tokenized_sentence_list(tokens);
310
311     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap pipe %s ", extcap);
312
313     int_iter = interfaces;
314     while (int_iter != NULL ) {
315         if ( extcap_if_exists(int_iter->call) )
316         {
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;
320             continue;
321         }
322
323         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "  Interface [%s] \"%s\" ",
324                 int_iter->call, int_iter->display);
325
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);
329
330         if_info->type = IF_EXTCAP;
331
332         if_info->extcap = g_strdup(extcap);
333         *il = g_list_append(*il, if_info);
334
335         extcap_if_add(g_strdup(int_iter->call), g_strdup(extcap) );
336         int_iter = int_iter->next_interface;
337     }
338
339     return TRUE;
340 }
341
342 GList *
343 extcap_interface_list(char **err_str) {
344     gchar *argv;
345     /* gint i; */
346     GList *ret = NULL;
347
348     if (err_str != NULL)
349     *err_str = NULL;
350
351     extcap_if_cleanup();
352
353     argv = g_strdup(EXTCAP_ARGUMENT_LIST_INTERFACES);
354
355     if (err_str)
356     *err_str = NULL;
357     extcap_foreach(1, &argv, interfaces_cb, &ret, err_str, NULL);
358
359     g_free(argv);
360
361     return ret;
362 }
363
364 static void g_free_1(gpointer data, gpointer user_data _U_)
365 {
366     g_free(data);
367 }
368
369 static void extcap_free_if_configuration(GList *list)
370 {
371     GList *elem;
372
373     for (elem = g_list_first(list); elem; elem = elem->next)
374     {
375         GList *arg_list;
376         if (elem->data == NULL)
377         {
378             continue;
379         }
380
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);
384     }
385     g_list_free(list);
386 }
387
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;
393
394     tokens = extcap_tokenize_sentences(output);
395     arguments = extcap_parse_args(tokens);
396
397     extcap_free_tokenized_sentence_list(tokens);
398
399 #if ARG_DEBUG
400     extcap_debug_arguments ( arguments );
401 #endif
402
403     *il = g_list_append(*il, arguments);
404
405     /* By returning false, extcap_foreach will break on first found */
406     return TRUE;
407 }
408
409 GList *
410 extcap_get_if_configuration(const char * ifname) {
411     gchar *argv[4];
412     GList *ret = NULL;
413     gchar **err_str = NULL;
414
415     if ( extcap_if_exists(ifname) )
416     {
417         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Extcap path %s",
418                 get_extcap_dir());
419
420         argv[0] = g_strdup(EXTCAP_ARGUMENT_CONFIG);
421         argv[1] = g_strdup(EXTCAP_ARGUMENT_INTERFACE);
422         argv[2] = g_strdup(ifname);
423         argv[3] = NULL;
424
425         extcap_foreach(4, argv, search_cb, &ret, err_str, ifname);
426     }
427
428     return ret;
429 }
430
431 gboolean
432 extcap_has_configuration(const char * ifname) {
433     GList * arguments = 0;
434     GList * walker = 0, * item = 0;
435
436     gboolean found = FALSE;
437
438     arguments = extcap_get_if_configuration((const char *)( ifname ) );
439     walker = g_list_first(arguments);
440
441     while ( walker != NULL && ! found )
442     {
443         item = g_list_first((GList *)(walker->data));
444         while ( item != NULL && ! found )
445         {
446             if ( (extcap_arg *)(item->data) != NULL )
447                 found = TRUE;
448
449             item = item->next;
450         }
451         walker = walker->next;
452     }
453
454     return found;
455 }
456
457 void extcap_cleanup(capture_options * capture_opts) {
458     interface_options interface_opts;
459     guint icnt = 0;
460
461     for (icnt = 0; icnt < capture_opts->ifaces->len; icnt++) {
462         interface_opts = g_array_index(capture_opts->ifaces, interface_options,
463                 icnt);
464
465         /* skip native interfaces */
466         if (interface_opts.if_type != IF_EXTCAP)
467         continue;
468
469         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
470                 "Extcap [%s] - Cleaning up fifo: %s; PID: %d", interface_opts.name,
471                 interface_opts.extcap_fifo, interface_opts.extcap_pid);
472 #ifdef _WIN32
473         if (pipe_h)
474         {
475             g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
476                 "Extcap [%s] - Closing pipe", interface_opts.name);
477             FlushFileBuffers(pipe_h);
478             DisconnectNamedPipe(pipe_h);
479             CloseHandle(pipe_h);
480         }
481 #else
482         if (interface_opts.extcap_fifo != NULL && file_exists(interface_opts.extcap_fifo))
483         {
484             /* the fifo will not be freed here, but with the other capture_opts in capture_sync */
485             ws_unlink(interface_opts.extcap_fifo);
486             interface_opts.extcap_fifo = NULL;
487         }
488 #endif
489         /* Maybe the client closed and removed fifo, but ws should check if
490          * pid should be closed */
491         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
492                 "Extcap [%s] - Closing spawned PID: %d", interface_opts.name,
493                 interface_opts.extcap_pid);
494
495         if (interface_opts.extcap_child_watch > 0)
496         {
497             g_source_remove(interface_opts.extcap_child_watch);
498             interface_opts.extcap_child_watch = 0;
499         }
500
501         if (interface_opts.extcap_pid != INVALID_EXTCAP_PID)
502         {
503 #ifdef _WIN32
504             TerminateProcess(interface_opts.extcap_pid, 0);
505 #endif
506             g_spawn_close_pid(interface_opts.extcap_pid);
507             interface_opts.extcap_pid = INVALID_EXTCAP_PID;
508         }
509
510         /* Make sure modified interface_opts is saved in capture_opts. */
511         capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, icnt);
512         g_array_insert_val(capture_opts->ifaces, icnt, interface_opts);
513     }
514 }
515
516 static void
517 extcap_arg_cb(gpointer key, gpointer value, gpointer data) {
518     GPtrArray *args = (GPtrArray *)data;
519
520     if ( key != NULL )
521     {
522         g_ptr_array_add(args, g_strdup((const gchar*)key));
523
524         if ( value != NULL )
525             g_ptr_array_add(args, g_strdup((const gchar*)value));
526     }
527 }
528
529 static void extcap_child_watch_cb(GPid pid, gint status _U_, gpointer user_data)
530 {
531     guint i;
532     interface_options interface_opts;
533     capture_options *capture_opts = (capture_options *)user_data;
534
535     /* Close handle to child process. */
536     g_spawn_close_pid(pid);
537
538     /* Update extcap_pid in interface options structure. */
539     for (i = 0; i < capture_opts->ifaces->len; i++)
540     {
541         interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
542         if (interface_opts.extcap_pid == pid)
543         {
544             interface_opts.extcap_pid = INVALID_EXTCAP_PID;
545             interface_opts.extcap_child_watch = 0;
546             capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, i);
547             g_array_insert_val(capture_opts->ifaces, i, interface_opts);
548             break;
549         }
550     }
551 }
552
553 /* call mkfifo for each extcap,
554  * returns FALSE if there's an error creating a FIFO */
555 gboolean
556 extcaps_init_initerfaces(capture_options *capture_opts)
557 {
558     guint i;
559     interface_options interface_opts;
560
561     for (i = 0; i < capture_opts->ifaces->len; i++)
562     {
563         GPtrArray *args = NULL;
564         GPid pid = INVALID_EXTCAP_PID;
565         gchar **tmp;
566         int tmp_i;
567
568         interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
569
570         /* skip native interfaces */
571         if (interface_opts.if_type != IF_EXTCAP )
572             continue;
573
574         /* create pipe for fifo */
575         if ( ! extcap_create_pipe ( &interface_opts.extcap_fifo ) )
576             return FALSE;
577
578         /* Create extcap call */
579         args = g_ptr_array_new();
580 #define add_arg(X) g_ptr_array_add(args, g_strdup(X))
581
582         add_arg(interface_opts.extcap);
583         add_arg(EXTCAP_ARGUMENT_RUN_CAPTURE);
584         add_arg(EXTCAP_ARGUMENT_INTERFACE);
585         add_arg(interface_opts.name);
586         if (interface_opts.cfilter && strlen(interface_opts.cfilter) > 0) {
587             add_arg(EXTCAP_ARGUMENT_CAPTURE_FILTER);
588             add_arg(interface_opts.cfilter);
589         }
590         add_arg(EXTCAP_ARGUMENT_RUN_PIPE);
591         add_arg(interface_opts.extcap_fifo);
592         if (interface_opts.extcap_args == NULL)
593         {
594             /* User did not perform interface configuration.
595              *
596              * Check if there are any boolean flags that are set by default
597              * and hence their argument should be added.
598              */
599             GList *arglist;
600             GList *elem;
601
602             arglist = extcap_get_if_configuration(interface_opts.name);
603             for (elem = g_list_first(arglist); elem; elem = elem->next)
604             {
605                 GList * arg_list;
606                 extcap_arg *arg_iter;
607
608                 if (elem->data == NULL)
609                 {
610                     continue;
611                 }
612
613                 arg_list = g_list_first((GList *)elem->data);
614                 while (arg_list != NULL)
615                 {
616                     /* In case of boolflags only first element in arg_list is relevant. */
617                     arg_iter = (extcap_arg*) (arg_list->data);
618
619                     if  (arg_iter->arg_type == EXTCAP_ARG_BOOLFLAG)
620                     {
621                         if (arg_iter->default_complex != NULL
622                             && extcap_complex_get_bool(arg_iter->default_complex))
623                         {
624                             add_arg(arg_iter->call);
625                         }
626                     }
627
628                     arg_list = arg_list->next;
629                 }
630             }
631
632             extcap_free_if_configuration(arglist);
633         }
634         else
635         {
636             g_hash_table_foreach(interface_opts.extcap_args, extcap_arg_cb, args);
637         }
638         add_arg(NULL);
639 #undef add_arg
640
641         /* Dump commandline parameters sent to extcap. */
642         for (tmp = (gchar **)args->pdata, tmp_i = 0; *tmp && **tmp; ++tmp_i, ++tmp)
643         {
644             g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "argv[%d]: %s", tmp_i, *tmp);
645         }
646
647         /* Wireshark for windows crashes here sometimes *
648          * Access violation reading location 0x...      */
649         g_spawn_async(NULL, (gchar **)args->pdata, NULL,
650                     (GSpawnFlags) G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL,
651                     &pid,NULL);
652
653         g_ptr_array_foreach(args, (GFunc)g_free, NULL);
654         g_ptr_array_free(args, TRUE);
655         interface_opts.extcap_pid = pid;
656         interface_opts.extcap_child_watch =
657             g_child_watch_add(pid, extcap_child_watch_cb, (gpointer)capture_opts);
658         capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, i);
659         g_array_insert_val(capture_opts->ifaces, i, interface_opts);
660
661 #ifdef _WIN32
662         /* On Windows, wait for extcap to connect to named pipe.
663          * Some extcaps will present UAC screen to user.
664          * 30 second timeout should be reasonable timeout for extcap to
665          * connect to named pipe (including user interaction).
666          * Wait on multiple object in case of extcap termination
667          * without opening pipe.
668          *
669          * Minimum supported version of Windows: XP / Server 2003.
670          */
671         if (pid != INVALID_EXTCAP_PID)
672         {
673             DWORD dw;
674             HANDLE handles[2];
675             OVERLAPPED ov;
676             ov.Pointer = 0;
677             ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
678
679             ConnectNamedPipe(pipe_h, &ov);
680             handles[0] = ov.hEvent;
681             handles[1] = pid;
682
683             if (GetLastError() == ERROR_PIPE_CONNECTED)
684             {
685                 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "extcap connected to pipe");
686             }
687             else
688             {
689                 dw = WaitForMultipleObjects(2, handles, FALSE, 30000);
690                 if (dw == WAIT_OBJECT_0)
691                 {
692                     /* ConnectNamedPipe finished. */
693                     DWORD code;
694
695                     code = GetLastError();
696                     if (code == ERROR_IO_PENDING)
697                     {
698                         DWORD dummy;
699                         if (!GetOverlappedResult(ov.hEvent, &ov, &dummy, TRUE))
700                         {
701                             code = GetLastError();
702                         }
703                         else
704                         {
705                             code = ERROR_SUCCESS;
706                         }
707                     }
708
709                     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "ConnectNamedPipe code: %d", code);
710                 }
711                 else if (dw == (WAIT_OBJECT_0 + 1))
712                 {
713                     /* extcap process terminated. */
714                     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "extcap terminated without connecting to pipe!");
715                 }
716                 else if (dw == WAIT_TIMEOUT)
717                 {
718                     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "extcap didn't connect to pipe within 30 seconds!");
719                 }
720                 else
721                 {
722                     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "WaitForMultipleObjects returned 0x%08X. Error %d", dw, GetLastError());
723                 }
724             }
725
726             CloseHandle(ov.hEvent);
727         }
728 #endif
729     }
730
731     return TRUE;
732 }
733
734 #ifdef _WIN32
735 /* called by capture_sync to get the CreatNamedPipe handle*/
736 HANDLE
737 extcap_get_win32_handle()
738 {
739     return pipe_h;
740 }
741 #endif
742
743 gboolean extcap_create_pipe(char ** fifo)
744 {
745 #ifdef _WIN32
746     gchar timestr[ 14+1 ];
747     time_t current_time;
748
749     gchar *pipename = NULL;
750
751     SECURITY_ATTRIBUTES security;
752     /* create pipename */
753     current_time = time(NULL);
754     strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S", localtime(&current_time));
755     pipename = g_strconcat ( "\\\\.\\pipe\\", EXTCAP_PIPE_PREFIX, "_", timestr, NULL );
756
757     /* Security struct to enable Inheritable HANDLE */
758     memset(&security, 0, sizeof(SECURITY_ATTRIBUTES));
759     security.nLength = sizeof(SECURITY_ATTRIBUTES);
760     security.bInheritHandle = TRUE;
761     security.lpSecurityDescriptor = NULL;
762
763     /* create a namedPipe*/
764     pipe_h = CreateNamedPipe(
765                 utf_8to16(pipename),
766                 PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
767                 PIPE_TYPE_MESSAGE| PIPE_READMODE_MESSAGE | PIPE_WAIT,
768                 5, 65536, 65536,
769                 300,
770                 &security);
771
772     if (pipe_h == INVALID_HANDLE_VALUE)
773     {
774         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,"\nError creating pipe => (%d)", GetLastError());
775         return FALSE;
776     }
777     else
778     {
779         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,"\nWireshark Created pipe =>(%s)",pipename);
780         *fifo = g_strdup(pipename);
781     }
782 #else
783     gchar *temp_name = NULL;
784     int fd = 0;
785
786     if ((fd = create_tempfile(&temp_name, EXTCAP_PIPE_PREFIX)) < 0 )
787         return FALSE;
788
789     ws_close(fd);
790
791     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
792             "Extcap - Creating fifo: %s", temp_name);
793
794     if ( file_exists(temp_name) )
795         ws_unlink(temp_name);
796
797     if (mkfifo(temp_name, 0600) == 0)
798         *fifo = g_strdup(temp_name);
799 #endif
800
801     return TRUE;
802 }
803
804 #if ARG_DEBUG
805 void extcap_debug_arguments ( extcap_arg *arg_iter )
806 {
807     extcap_value *v = NULL;
808     GList *walker = NULL;
809
810     printf("debug - parser dump\n");
811     while (arg_iter != NULL) {
812         printf("ARG %d call=%s display=\"%s\" type=", arg_iter->arg_num, arg_iter->call, arg_iter->display);
813
814         switch (arg_iter->arg_type) {
815             case EXTCAP_ARG_INTEGER:
816             printf("int\n");
817             break;
818             case EXTCAP_ARG_UNSIGNED:
819             printf("unsigned\n");
820             break;
821             case EXTCAP_ARG_LONG:
822             printf("long\n");
823             break;
824             case EXTCAP_ARG_DOUBLE:
825             printf("double\n");
826             break;
827             case EXTCAP_ARG_BOOLEAN:
828             printf("boolean\n");
829             break;
830             case EXTCAP_ARG_MENU:
831             printf("menu\n");
832             break;
833             case EXTCAP_ARG_RADIO:
834             printf("radio\n");
835             break;
836             case EXTCAP_ARG_SELECTOR:
837             printf("selctor\n");
838             break;
839             case EXTCAP_ARG_STRING:
840             printf ( "string\n" );
841             break;
842             case EXTCAP_ARG_MULTICHECK:
843             printf ( "unknown\n" );
844             break;
845             case EXTCAP_ARG_UNKNOWN:
846             printf ( "unknown\n" );
847             break;
848         }
849
850         if (arg_iter->range_start != NULL && arg_iter->range_end != NULL) {
851             printf("\tRange: ");
852             extcap_printf_complex(arg_iter->range_start);
853             printf(" - ");
854             extcap_printf_complex(arg_iter->range_end);
855             printf("\n");
856         }
857
858         for ( walker = g_list_first ( arg_iter->value_list ); walker; walker = walker->next )
859         {
860             v = (extcap_value *)walker->data;
861             if (v->is_default == TRUE)
862             printf("*");
863             printf("\tcall=\"%p\" display=\"%p\"\n", v->call, v->display);
864             printf("\tcall=\"%s\" display=\"%s\"\n", v->call, v->display);
865         }
866
867         arg_iter = arg_iter->next_arg;
868     }
869 }
870 #endif
871 #endif
872
873 /*
874  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
875  *
876  * Local variables:
877  * c-basic-offset: 4
878  * tab-width: 8
879  * indent-tabs-mode: nil
880  * End:
881  *
882  * vi: set shiftwidth=4 tabstop=8 expandtab:
883  * :indentSize=4:tabSize=8:noTabs=true:
884  */