From 8b2167493295fe227f93747c422aa984bc13db42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Date: Tue, 9 Jul 2019 14:45:07 +0100 Subject: [PATCH] remote: use enum helpers for parsing remote driver transport MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of open-coding a string -> enum conversion, use the enum helpers for the remote driver transport. The old code uses STRCASEEQ, so we must force the URI transport to lowercase for sake of back-compatibility. Reviewed-by: Andrea Bolognani Signed-off-by: Daniel P. Berrangé --- src/remote/remote_driver.c | 121 +++++++++++++++++++++---------------- 1 file changed, 68 insertions(+), 53 deletions(-) diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index ef240052f1..1b36ef3f05 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -49,11 +49,35 @@ #include "virauth.h" #include "virauthconfig.h" #include "virstring.h" +#include "c-ctype.h" #define VIR_FROM_THIS VIR_FROM_REMOTE VIR_LOG_INIT("remote.remote_driver"); +typedef enum { + REMOTE_DRIVER_TRANSPORT_TLS, + REMOTE_DRIVER_TRANSPORT_UNIX, + REMOTE_DRIVER_TRANSPORT_SSH, + REMOTE_DRIVER_TRANSPORT_LIBSSH2, + REMOTE_DRIVER_TRANSPORT_EXT, + REMOTE_DRIVER_TRANSPORT_TCP, + REMOTE_DRIVER_TRANSPORT_LIBSSH, + + REMOTE_DRIVER_TRANSPORT_LAST, +} remoteDriverTransport; + +VIR_ENUM_DECL(remoteDriverTransport); +VIR_ENUM_IMPL(remoteDriverTransport, + REMOTE_DRIVER_TRANSPORT_LAST, + "tls", + "unix", + "ssh", + "libssh2", + "ext", + "tcp", + "libssh"); + #if SIZEOF_LONG < 8 # define HYPER_TO_TYPE(_type, _to, _from) \ do { \ @@ -176,10 +200,17 @@ static int remoteSplitURIScheme(virURIPtr uri, if (VIR_STRNDUP(*driver, uri->scheme, p ? p - uri->scheme : -1) < 0) return -1; - if (p && - VIR_STRDUP(*transport, p + 1) < 0) { - VIR_FREE(*driver); - return -1; + if (p) { + if (VIR_STRDUP(*transport, p + 1) < 0) { + VIR_FREE(*driver); + return -1; + } + + p = *transport; + while (*p) { + *p = c_tolower(*p); + p++; + } } return 0; @@ -778,15 +809,7 @@ doRemoteOpen(virConnectPtr conn, virConfPtr conf, unsigned int flags) { - enum { - trans_tls, - trans_unix, - trans_ssh, - trans_libssh2, - trans_ext, - trans_tcp, - trans_libssh, - } transport; + int transport; #ifndef WIN32 VIR_AUTOFREE(char *) daemonPath = NULL; #endif @@ -815,52 +838,39 @@ doRemoteOpen(virConnectPtr conn, if (conn->uri) { if (!transport_str) { if (conn->uri->server) - transport = trans_tls; + transport = REMOTE_DRIVER_TRANSPORT_TLS; else - transport = trans_unix; + transport = REMOTE_DRIVER_TRANSPORT_UNIX; } else { - if (STRCASEEQ(transport_str, "tls")) { - transport = trans_tls; - } else if (STRCASEEQ(transport_str, "unix")) { - if (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 { - transport = trans_unix; - } - } else if (STRCASEEQ(transport_str, "ssh")) { - transport = trans_ssh; - } else if (STRCASEEQ(transport_str, "libssh2")) { - transport = trans_libssh2; - } else if (STRCASEEQ(transport_str, "ext")) { - transport = trans_ext; - } else if (STRCASEEQ(transport_str, "tcp")) { - transport = trans_tcp; - } else if (STRCASEEQ(transport_str, "libssh")) { - transport = trans_libssh; - } 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 = trans_unix; + transport = REMOTE_DRIVER_TRANSPORT_UNIX; } /* Remote server defaults to "localhost" if not specified. */ if (conn->uri && conn->uri->port != 0) { if (virAsprintf(&port, "%d", conn->uri->port) < 0) goto failed; - } else if (transport == trans_tls) { + } else if (transport == REMOTE_DRIVER_TRANSPORT_TLS) { if (VIR_STRDUP(port, LIBVIRTD_TLS_PORT) < 0) goto failed; - } else if (transport == trans_tcp) { + } else if (transport == REMOTE_DRIVER_TRANSPORT_TCP) { if (VIR_STRDUP(port, LIBVIRTD_TCP_PORT) < 0) goto failed; } /* Port not used for unix, ext., default for ssh */ @@ -944,7 +954,7 @@ doRemoteOpen(virConnectPtr conn, VIR_DEBUG("proceeding with name = %s", name); /* For ext transport, command is required. */ - if (transport == trans_ext && !command) { + if (transport == REMOTE_DRIVER_TRANSPORT_EXT && !command) { virReportError(VIR_ERR_INVALID_ARG, "%s", _("remote_open: for 'ext' transport, command is required")); goto failed; @@ -952,8 +962,8 @@ doRemoteOpen(virConnectPtr conn, VIR_DEBUG("Connecting with transport %d", transport); /* Connect to the remote service. */ - switch (transport) { - case trans_tls: + switch ((remoteDriverTransport)transport) { + case REMOTE_DRIVER_TRANSPORT_TLS: if (conf && !tls_priority && virConfGetValueString(conf, "tls_priority", &tls_priority) < 0) goto failed; @@ -976,7 +986,7 @@ doRemoteOpen(virConnectPtr conn, goto failed; #endif - case trans_tcp: + case REMOTE_DRIVER_TRANSPORT_TCP: priv->client = virNetClientNewTCP(priv->hostname, port, AF_UNSPEC); if (!priv->client) goto failed; @@ -991,7 +1001,7 @@ doRemoteOpen(virConnectPtr conn, break; - case trans_libssh2: + case REMOTE_DRIVER_TRANSPORT_LIBSSH2: if (!sockname) { /* Right now we don't support default session connections */ if (flags & VIR_DRV_OPEN_REMOTE_USER) { @@ -1026,7 +1036,7 @@ doRemoteOpen(virConnectPtr conn, priv->is_secure = 1; break; - case trans_libssh: + case REMOTE_DRIVER_TRANSPORT_LIBSSH: if (!sockname) { /* Right now we don't support default session connections */ if (flags & VIR_DRV_OPEN_REMOTE_USER) { @@ -1062,7 +1072,7 @@ doRemoteOpen(virConnectPtr conn, break; #ifndef WIN32 - case trans_unix: + case REMOTE_DRIVER_TRANSPORT_UNIX: if (!sockname) { if (flags & VIR_DRV_OPEN_REMOTE_USER) sockname = remoteGetUNIXSocketNonRoot(); @@ -1088,7 +1098,7 @@ doRemoteOpen(virConnectPtr conn, priv->is_secure = 1; break; - case trans_ssh: + case REMOTE_DRIVER_TRANSPORT_SSH: if (!command && VIR_STRDUP(command, "ssh") < 0) goto failed; @@ -1120,7 +1130,7 @@ doRemoteOpen(virConnectPtr conn, priv->is_secure = 1; break; - case trans_ext: { + case REMOTE_DRIVER_TRANSPORT_EXT: { char const *cmd_argv[] = { command, NULL }; if (!(priv->client = virNetClientNewExternal(cmd_argv))) goto failed; @@ -1132,15 +1142,20 @@ doRemoteOpen(virConnectPtr conn, #else /* WIN32 */ - case trans_unix: - case trans_ssh: - case trans_ext: + case REMOTE_DRIVER_TRANSPORT_UNIX: + case REMOTE_DRIVER_TRANSPORT_SSH: + case REMOTE_DRIVER_TRANSPORT_EXT: virReportError(VIR_ERR_INVALID_ARG, "%s", _("transport methods unix, ssh and ext are not supported " "under Windows")); goto failed; #endif /* WIN32 */ + + case REMOTE_DRIVER_TRANSPORT_LAST: + default: + virReportEnumRangeError(remoteDriverTransport, transport); + goto failed; } /* switch (transport) */