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