extcap: Create unique pipe names for each interface
authorStig Bjørlykke <stig@bjorlykke.org>
Sat, 19 Aug 2017 21:39:21 +0000 (23:39 +0200)
committerAnders Broman <a.broman58@gmail.com>
Wed, 23 Aug 2017 07:26:18 +0000 (07:26 +0000)
On Windows the pipe names does not get random characters appended.
Add the interface name and pipe type to make it unique.

This partly fixes the issue with capturing from multiple extcap
interfaces on Windows.

Ping-Bug: 13653
Ping-Bug: 13833
Change-Id: I4290b37cf789bf77608993682a803aca29513d28
Reviewed-on: https://code.wireshark.org/review/23158
Petri-Dish: Stig Bjørlykke <stig@bjorlykke.org>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Anders Broman <a.broman58@gmail.com>
capchild/capture_sync.c
capture_opts.c
capture_opts.h
extcap.c
extcap.h

index 9f3d0ca0b98277ad635ba502644501d93803ea93..33e730ed66e335462445c4e4f56041498fcf9fc6 100644 (file)
@@ -526,19 +526,12 @@ sync_pipe_start(capture_options *capture_opts, capture_session *cap_session, inf
 #else
     si.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
     si.wShowWindow  = SW_HIDE;  /* this hides the console window */
-#if defined(_WIN32)
-    /* needs first a check if NULL *
-     * otherwise wouldn't work with non extcap interfaces */
-    if(interface_opts.extcap_fifo != NULL)
-    {
-       if(strncmp(interface_opts.extcap_fifo,"\\\\.\\pipe\\",9)== 0)
-       {
-         si.hStdInput = extcap_get_win32_handle();
-       }
-    }
+#ifdef HAVE_EXTCAP
+    if(interface_opts.extcap_pipe_h != INVALID_HANDLE_VALUE)
+        si.hStdInput = interface_opts.extcap_pipe_h;
     else
 #endif
-       si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
+        si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
 
     si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
     si.hStdError = sync_pipe_write;
index 29bb484b20599a2254ac18b833df85e8477155ea..0f7a88ee3f403efd916397077687687b4daafe92 100644 (file)
@@ -68,6 +68,9 @@ capture_opts_init(capture_options *capture_opts)
     capture_opts->default_options.extcap_args     = NULL;
     capture_opts->default_options.extcap_userdata = NULL;
     capture_opts->default_options.extcap_pid      = INVALID_EXTCAP_PID;
+#ifdef _WIN32
+    capture_opts->default_options.extcap_pipe_h   = INVALID_HANDLE_VALUE;
+#endif
     capture_opts->default_options.extcap_control_in  = NULL;
     capture_opts->default_options.extcap_control_out = NULL;
 #endif
@@ -713,6 +716,9 @@ capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str
     interface_opts.extcap_args = NULL;
     interface_opts.extcap_pid = INVALID_EXTCAP_PID;
     interface_opts.extcap_userdata = NULL;
+#ifdef _WIN32
+    interface_opts.extcap_pipe_h = INVALID_HANDLE_VALUE;
+#endif
     interface_opts.extcap_control_in = g_strdup(capture_opts->default_options.extcap_control_in);
     interface_opts.extcap_control_out = g_strdup(capture_opts->default_options.extcap_control_out);
 #endif
@@ -1226,6 +1232,9 @@ collect_ifaces(capture_options *capture_opts)
             if (interface_opts.extcap_args)
                 g_hash_table_ref(interface_opts.extcap_args);
             interface_opts.extcap_userdata = NULL;
+#ifdef _WIN32
+            interface_opts.extcap_pipe_h = INVALID_HANDLE_VALUE;
+#endif
             interface_opts.extcap_control_in = NULL;
             interface_opts.extcap_control_out = NULL;
 #endif
index 8e1cb8de2a2121227229e7710f121117d3e4aef7..22d03c84728ac91e7e814eef2fcfd5ee76b2d4d2 100644 (file)
 
 #include <caputils/capture_ifinfo.h>
 
+#ifdef _WIN32
+#include <windows.h>
+#endif
+
 #ifdef __cplusplus
 extern "C" {
 #endif /* __cplusplus */
@@ -232,6 +236,9 @@ typedef struct interface_options_tag {
     GPid              extcap_pid;           /* pid of running process or INVALID_EXTCAP_PID */
     gpointer          extcap_userdata;
     guint             extcap_child_watch;
+#ifdef _WIN32
+    HANDLE            extcap_pipe_h;
+#endif
     gchar            *extcap_control_in;
     gchar            *extcap_control_out;
 #endif
index 9bb36c178bb2000e70b78e6cf6992821c64cf7d1..d025c9fe256e58d589366d6ebee26b119c5366ca 100644 (file)
--- a/extcap.c
+++ b/extcap.c
@@ -63,7 +63,7 @@
 #include "extcap_spawn.h"
 
 #ifdef _WIN32
-static HANDLE pipe_h = NULL;
+static HANDLE pipe_h = INVALID_HANDLE_VALUE;
 #endif
 
 static void extcap_child_watch_cb(GPid pid, gint status, gpointer user_data);
@@ -1032,13 +1032,14 @@ void extcap_if_cleanup(capture_options *capture_opts, gchar **errormsg)
               "Extcap [%s] - Cleaning up fifo: %s; PID: %d", interface_opts.name,
               interface_opts.extcap_fifo, interface_opts.extcap_pid);
 #ifdef _WIN32
-        if (pipe_h)
+        if (interface_opts.extcap_pipe_h != INVALID_HANDLE_VALUE)
         {
             g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG,
                   "Extcap [%s] - Closing pipe", interface_opts.name);
-            FlushFileBuffers(pipe_h);
-            DisconnectNamedPipe(pipe_h);
-            CloseHandle(pipe_h);
+            FlushFileBuffers(interface_opts.extcap_pipe_h);
+            DisconnectNamedPipe(interface_opts.extcap_pipe_h);
+            CloseHandle(interface_opts.extcap_pipe_h);
+            interface_opts.extcap_pipe_h = INVALID_HANDLE_VALUE;
         }
 #else
         if (interface_opts.extcap_fifo != NULL && file_exists(interface_opts.extcap_fifo))
@@ -1353,15 +1354,21 @@ extcap_init_interfaces(capture_options *capture_opts)
         /* create control pipes if having toolbar */
         if (extcap_has_toolbar(interface_opts.name))
         {
-            extcap_create_pipe(&interface_opts.extcap_control_in);
-            extcap_create_pipe(&interface_opts.extcap_control_out);
+            extcap_create_pipe(interface_opts.name, &interface_opts.extcap_control_in,
+                               EXTCAP_CONTROL_IN_PREFIX);
+            extcap_create_pipe(interface_opts.name, &interface_opts.extcap_control_out,
+                               EXTCAP_CONTROL_OUT_PREFIX);
         }
 
         /* create pipe for fifo */
-        if (!extcap_create_pipe(&interface_opts.extcap_fifo))
+        if (!extcap_create_pipe(interface_opts.name, &interface_opts.extcap_fifo,
+                                EXTCAP_PIPE_PREFIX))
         {
             return FALSE;
         }
+#ifdef _WIN32
+        interface_opts.extcap_pipe_h = pipe_h;
+#endif
 
         /* Create extcap call */
         args = extcap_prepare_arguments(interface_opts);
@@ -1396,9 +1403,8 @@ extcap_init_interfaces(capture_options *capture_opts)
          */
         if (pid != INVALID_EXTCAP_PID)
         {
-            extcap_wait_for_pipe(pipe_h, pid);
+            extcap_wait_for_pipe(interface_opts.extcap_pipe_h, pid);
         }
-
 #endif
 
         interface_opts.extcap_userdata = (gpointer) userdata;
@@ -1410,16 +1416,7 @@ extcap_init_interfaces(capture_options *capture_opts)
     return TRUE;
 }
 
-#ifdef _WIN32
-/* called by capture_sync to get the CreatNamedPipe handle*/
-HANDLE
-extcap_get_win32_handle()
-{
-    return pipe_h;
-}
-#endif
-
-gboolean extcap_create_pipe(char **fifo)
+gboolean extcap_create_pipe(const gchar *ifname, gchar **fifo, const gchar *pipe_prefix)
 {
 #ifdef _WIN32
     gchar timestr[ 14 + 1 ];
@@ -1435,7 +1432,7 @@ gboolean extcap_create_pipe(char **fifo)
      * so we won't get a null pointer back from localtime().
      */
     strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S", localtime(&current_time));
-    pipename = g_strconcat("\\\\.\\pipe\\", EXTCAP_PIPE_PREFIX, "_", timestr, NULL);
+    pipename = g_strconcat("\\\\.\\pipe\\", pipe_prefix, "_", ifname, "_", timestr, NULL);
 
     /* Security struct to enable Inheritable HANDLE */
     memset(&security, 0, sizeof(SECURITY_ATTRIBUTES));
@@ -1466,10 +1463,13 @@ gboolean extcap_create_pipe(char **fifo)
     gchar *temp_name = NULL;
     int fd = 0;
 
-    if ((fd = create_tempfile(&temp_name, EXTCAP_PIPE_PREFIX, NULL)) < 0)
+    gchar *pfx = g_strconcat(pipe_prefix, "_", ifname, NULL);
+    if ((fd = create_tempfile(&temp_name, pfx, NULL)) < 0)
     {
+        g_free(pfx);
         return FALSE;
     }
+    g_free(pfx);
 
     ws_close(fd);
 
index addf30fad7f55d650d63b84f50ed84e61d281e04..2e6739fa00d69f12f19e3b5eae1837d32ee7f337 100644 (file)
--- a/extcap.h
+++ b/extcap.h
@@ -29,7 +29,6 @@
 #include <glib.h>
 
 #ifdef _WIN32
-#include <windows.h>
 #include <wsutil/unicode-utils.h>
 #endif
 
@@ -41,6 +40,8 @@
 
 /* Prefix for the pipe interfaces */
 #define EXTCAP_PIPE_PREFIX "wireshark_extcap"
+#define EXTCAP_CONTROL_IN_PREFIX  "wireshark_control_in"
+#define EXTCAP_CONTROL_OUT_PREFIX "wireshark_control_out"
 
 #define EXTCAP_ARGUMENT_CONFIG                  "--extcap-config"
 #define EXTCAP_ARGUMENT_LIST_INTERFACES         "--extcap-interfaces"
@@ -138,16 +139,11 @@ extcap_has_configuration(const char * ifname, gboolean is_required);
 gboolean
 extcap_has_toolbar(const char *ifname);
 
-#ifdef WIN32
-HANDLE
-extcap_get_win32_handle();
-#endif
-
 gboolean
 extcap_init_interfaces(capture_options * capture_opts);
 
 gboolean
-extcap_create_pipe(char ** fifo);
+extcap_create_pipe(const gchar *ifname, gchar **fifo, const gchar *pipe_prefix);
 
 /* Clean up all if related stuff */
 void