[libvirt PATCH 4/9] remote: parse the remote transport string earlier

Daniel P. Berrangé berrange at redhat.com
Thu Jul 9 18:36:41 UTC 2020


We delay converting the remote transport string to enum form until
fairly late. As a result we're doing string comparisons when we
could be just doing enum comparisons.

Signed-off-by: Daniel P. Berrangé <berrange at redhat.com>
---
 po/POTFILES.in              |  1 +
 src/remote/remote_driver.c  | 51 ++++++++++---------------------------
 src/remote/remote_sockets.c | 35 +++++++++++++++++++++----
 src/remote/remote_sockets.h |  2 +-
 4 files changed, 45 insertions(+), 44 deletions(-)

diff --git a/po/POTFILES.in b/po/POTFILES.in
index af52054aa4..8fd391a63a 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -180,6 +180,7 @@
 @SRCDIR@/src/remote/remote_daemon_dispatch.c
 @SRCDIR@/src/remote/remote_daemon_stream.c
 @SRCDIR@/src/remote/remote_driver.c
+ at SRCDIR@/src/remote/remote_sockets.c
 @SRCDIR@/src/rpc/virkeepalive.c
 @SRCDIR@/src/rpc/virnetclient.c
 @SRCDIR@/src/rpc/virnetclientprogram.c
diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c
index b84b72522a..c39085951e 100644
--- a/src/remote/remote_driver.c
+++ b/src/remote/remote_driver.c
@@ -863,12 +863,11 @@ static int
 doRemoteOpen(virConnectPtr conn,
              struct private_data *priv,
              const char *driver_str,
-             const char *transport_str,
+             remoteDriverTransport transport,
              virConnectAuthPtr auth G_GNUC_UNUSED,
              virConfPtr conf,
              unsigned int flags)
 {
-    int transport;
 #ifndef WIN32
     g_autofree char *daemonPath = NULL;
 #endif
@@ -903,34 +902,6 @@ doRemoteOpen(virConnectPtr conn,
     /* We handle *ALL* URIs here. The caller has rejected any
      * URIs we don't care about */
 
-    if (conn->uri) {
-        if (!transport_str) {
-            if (conn->uri->server)
-                transport = REMOTE_DRIVER_TRANSPORT_TLS;
-            else
-                transport = REMOTE_DRIVER_TRANSPORT_UNIX;
-        } else {
-            if ((transport = remoteDriverTransportTypeFromString(transport_str)) < 0) {
-                virReportError(VIR_ERR_INVALID_ARG, "%s",
-                               _("remote_open: transport in URL not recognised "
-                                 "(should be tls|unix|ssh|ext|tcp|libssh2|libssh)"));
-                return VIR_DRV_OPEN_ERROR;
-            }
-
-            if (transport == REMOTE_DRIVER_TRANSPORT_UNIX &&
-                conn->uri->server) {
-                virReportError(VIR_ERR_INVALID_ARG,
-                               _("using unix socket and remote "
-                                 "server '%s' is not supported."),
-                               conn->uri->server);
-                return VIR_DRV_OPEN_ERROR;
-            }
-        }
-    } else {
-        /* No URI, then must be probing so use UNIX socket */
-        transport = REMOTE_DRIVER_TRANSPORT_UNIX;
-    }
-
     /* Remote server defaults to "localhost" if not specified. */
     if (conn->uri && conn->uri->port != 0) {
         port = g_strdup_printf("%d", conn->uri->port);
@@ -1352,11 +1323,16 @@ remoteConnectOpen(virConnectPtr conn,
     int rflags = 0;
     const char *autostart = getenv("LIBVIRT_AUTOSTART");
     char *driver = NULL;
-    char *transport = NULL;
+    remoteDriverTransport transport;
+
+    if (conn->uri) {
+        if (remoteSplitURIScheme(conn->uri, &driver, &transport) < 0)
+            goto cleanup;
+    } else {
+        /* No URI, then must be probing so use UNIX socket */
+        transport = REMOTE_DRIVER_TRANSPORT_UNIX;
+    }
 
-    if (conn->uri &&
-        remoteSplitURIScheme(conn->uri, &driver, &transport) < 0)
-        goto cleanup;
 
     if (inside_daemon) {
         if (!conn->uri) {
@@ -1398,12 +1374,12 @@ remoteConnectOpen(virConnectPtr conn,
         rflags |= VIR_DRV_OPEN_REMOTE_USER;
 
         /*
-         * Furthermore if no servername is given, and no +XXX
-         * transport is listed, or transport is unix,
+         * Furthermore if no servername is given,
+         * and the transport is unix,
          * and uid is unprivileged then auto-spawn a daemon.
          */
         if (!conn->uri->server &&
-            (transport == NULL || STREQ(transport, "unix")) &&
+            (transport == REMOTE_DRIVER_TRANSPORT_UNIX) &&
             (!autostart ||
              STRNEQ(autostart, "0"))) {
             VIR_DEBUG("Try daemon autostart");
@@ -1438,7 +1414,6 @@ remoteConnectOpen(virConnectPtr conn,
 
  cleanup:
     VIR_FREE(driver);
-    VIR_FREE(transport);
     return ret;
 }
 
diff --git a/src/remote/remote_sockets.c b/src/remote/remote_sockets.c
index 976124d0ed..cdc0a00293 100644
--- a/src/remote/remote_sockets.c
+++ b/src/remote/remote_sockets.c
@@ -21,6 +21,9 @@
 #include <config.h>
 
 #include "remote_sockets.h"
+#include "virerror.h"
+
+#define VIR_FROM_THIS VIR_FROM_REMOTE
 
 VIR_ENUM_IMPL(remoteDriverTransport,
               REMOTE_DRIVER_TRANSPORT_LAST,
@@ -42,25 +45,47 @@ VIR_ENUM_IMPL(remoteDriverMode,
 int
 remoteSplitURIScheme(virURIPtr uri,
                      char **driver,
-                     char **transport)
+                     remoteDriverTransport *transport)
 {
     char *p = strchr(uri->scheme, '+');
 
-    *driver = *transport = NULL;
-
     if (p)
         *driver = g_strndup(uri->scheme, p - uri->scheme);
     else
         *driver = g_strdup(uri->scheme);
 
     if (p) {
-        *transport = g_strdup(p + 1);
+        g_autofree char *tmp = g_strdup(p + 1);
+        int val;
 
-        p = *transport;
+        p = tmp;
         while (*p) {
             *p = g_ascii_tolower(*p);
             p++;
         }
+
+        if ((val = remoteDriverTransportTypeFromString(tmp)) < 0) {
+            virReportError(VIR_ERR_INVALID_ARG, "%s",
+                           _("remote_open: transport in URL not recognised "
+                             "(should be tls|unix|ssh|ext|tcp|libssh2|libssh)"));
+            return -1;
+        }
+
+        if (val == REMOTE_DRIVER_TRANSPORT_UNIX &&
+            uri->server) {
+            virReportError(VIR_ERR_INVALID_ARG,
+                           _("using unix socket and remote "
+                             "server '%s' is not supported."),
+                           uri->server);
+            return -1;
+        }
+
+        *transport = val;
+    } else {
+        if (uri->server)
+            *transport = REMOTE_DRIVER_TRANSPORT_TLS;
+        else
+            *transport = REMOTE_DRIVER_TRANSPORT_UNIX;
     }
 
     return 0;
diff --git a/src/remote/remote_sockets.h b/src/remote/remote_sockets.h
index bef3cdada9..ade3feab88 100644
--- a/src/remote/remote_sockets.h
+++ b/src/remote/remote_sockets.h
@@ -53,4 +53,4 @@ VIR_ENUM_DECL(remoteDriverMode);
 int
 remoteSplitURIScheme(virURIPtr uri,
                      char **driver,
-                     char **transport);
+                     remoteDriverTransport *transport);
-- 
2.26.2




More information about the libvir-list mailing list