mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-03-07 17:28:15 +00:00
remote: use enum helpers for parsing remote driver transport
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 <abologna@redhat.com> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
parent
55c8d1a95f
commit
8b21674932
@ -49,11 +49,35 @@
|
|||||||
#include "virauth.h"
|
#include "virauth.h"
|
||||||
#include "virauthconfig.h"
|
#include "virauthconfig.h"
|
||||||
#include "virstring.h"
|
#include "virstring.h"
|
||||||
|
#include "c-ctype.h"
|
||||||
|
|
||||||
#define VIR_FROM_THIS VIR_FROM_REMOTE
|
#define VIR_FROM_THIS VIR_FROM_REMOTE
|
||||||
|
|
||||||
VIR_LOG_INIT("remote.remote_driver");
|
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
|
#if SIZEOF_LONG < 8
|
||||||
# define HYPER_TO_TYPE(_type, _to, _from) \
|
# define HYPER_TO_TYPE(_type, _to, _from) \
|
||||||
do { \
|
do { \
|
||||||
@ -176,10 +200,17 @@ static int remoteSplitURIScheme(virURIPtr uri,
|
|||||||
if (VIR_STRNDUP(*driver, uri->scheme, p ? p - uri->scheme : -1) < 0)
|
if (VIR_STRNDUP(*driver, uri->scheme, p ? p - uri->scheme : -1) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (p &&
|
if (p) {
|
||||||
VIR_STRDUP(*transport, p + 1) < 0) {
|
if (VIR_STRDUP(*transport, p + 1) < 0) {
|
||||||
VIR_FREE(*driver);
|
VIR_FREE(*driver);
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
p = *transport;
|
||||||
|
while (*p) {
|
||||||
|
*p = c_tolower(*p);
|
||||||
|
p++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@ -778,15 +809,7 @@ doRemoteOpen(virConnectPtr conn,
|
|||||||
virConfPtr conf,
|
virConfPtr conf,
|
||||||
unsigned int flags)
|
unsigned int flags)
|
||||||
{
|
{
|
||||||
enum {
|
int transport;
|
||||||
trans_tls,
|
|
||||||
trans_unix,
|
|
||||||
trans_ssh,
|
|
||||||
trans_libssh2,
|
|
||||||
trans_ext,
|
|
||||||
trans_tcp,
|
|
||||||
trans_libssh,
|
|
||||||
} transport;
|
|
||||||
#ifndef WIN32
|
#ifndef WIN32
|
||||||
VIR_AUTOFREE(char *) daemonPath = NULL;
|
VIR_AUTOFREE(char *) daemonPath = NULL;
|
||||||
#endif
|
#endif
|
||||||
@ -815,52 +838,39 @@ doRemoteOpen(virConnectPtr conn,
|
|||||||
if (conn->uri) {
|
if (conn->uri) {
|
||||||
if (!transport_str) {
|
if (!transport_str) {
|
||||||
if (conn->uri->server)
|
if (conn->uri->server)
|
||||||
transport = trans_tls;
|
transport = REMOTE_DRIVER_TRANSPORT_TLS;
|
||||||
else
|
else
|
||||||
transport = trans_unix;
|
transport = REMOTE_DRIVER_TRANSPORT_UNIX;
|
||||||
} else {
|
} else {
|
||||||
if (STRCASEEQ(transport_str, "tls")) {
|
if ((transport = remoteDriverTransportTypeFromString(transport_str)) < 0) {
|
||||||
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 {
|
|
||||||
virReportError(VIR_ERR_INVALID_ARG, "%s",
|
virReportError(VIR_ERR_INVALID_ARG, "%s",
|
||||||
_("remote_open: transport in URL not recognised "
|
_("remote_open: transport in URL not recognised "
|
||||||
"(should be tls|unix|ssh|ext|tcp|libssh2|libssh)"));
|
"(should be tls|unix|ssh|ext|tcp|libssh2|libssh)"));
|
||||||
return VIR_DRV_OPEN_ERROR;
|
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 {
|
} else {
|
||||||
/* No URI, then must be probing so use UNIX socket */
|
/* 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. */
|
/* Remote server defaults to "localhost" if not specified. */
|
||||||
if (conn->uri && conn->uri->port != 0) {
|
if (conn->uri && conn->uri->port != 0) {
|
||||||
if (virAsprintf(&port, "%d", conn->uri->port) < 0)
|
if (virAsprintf(&port, "%d", conn->uri->port) < 0)
|
||||||
goto failed;
|
goto failed;
|
||||||
} else if (transport == trans_tls) {
|
} else if (transport == REMOTE_DRIVER_TRANSPORT_TLS) {
|
||||||
if (VIR_STRDUP(port, LIBVIRTD_TLS_PORT) < 0)
|
if (VIR_STRDUP(port, LIBVIRTD_TLS_PORT) < 0)
|
||||||
goto failed;
|
goto failed;
|
||||||
} else if (transport == trans_tcp) {
|
} else if (transport == REMOTE_DRIVER_TRANSPORT_TCP) {
|
||||||
if (VIR_STRDUP(port, LIBVIRTD_TCP_PORT) < 0)
|
if (VIR_STRDUP(port, LIBVIRTD_TCP_PORT) < 0)
|
||||||
goto failed;
|
goto failed;
|
||||||
} /* Port not used for unix, ext., default for ssh */
|
} /* Port not used for unix, ext., default for ssh */
|
||||||
@ -944,7 +954,7 @@ doRemoteOpen(virConnectPtr conn,
|
|||||||
VIR_DEBUG("proceeding with name = %s", name);
|
VIR_DEBUG("proceeding with name = %s", name);
|
||||||
|
|
||||||
/* For ext transport, command is required. */
|
/* For ext transport, command is required. */
|
||||||
if (transport == trans_ext && !command) {
|
if (transport == REMOTE_DRIVER_TRANSPORT_EXT && !command) {
|
||||||
virReportError(VIR_ERR_INVALID_ARG, "%s",
|
virReportError(VIR_ERR_INVALID_ARG, "%s",
|
||||||
_("remote_open: for 'ext' transport, command is required"));
|
_("remote_open: for 'ext' transport, command is required"));
|
||||||
goto failed;
|
goto failed;
|
||||||
@ -952,8 +962,8 @@ doRemoteOpen(virConnectPtr conn,
|
|||||||
|
|
||||||
VIR_DEBUG("Connecting with transport %d", transport);
|
VIR_DEBUG("Connecting with transport %d", transport);
|
||||||
/* Connect to the remote service. */
|
/* Connect to the remote service. */
|
||||||
switch (transport) {
|
switch ((remoteDriverTransport)transport) {
|
||||||
case trans_tls:
|
case REMOTE_DRIVER_TRANSPORT_TLS:
|
||||||
if (conf && !tls_priority &&
|
if (conf && !tls_priority &&
|
||||||
virConfGetValueString(conf, "tls_priority", &tls_priority) < 0)
|
virConfGetValueString(conf, "tls_priority", &tls_priority) < 0)
|
||||||
goto failed;
|
goto failed;
|
||||||
@ -976,7 +986,7 @@ doRemoteOpen(virConnectPtr conn,
|
|||||||
goto failed;
|
goto failed;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
case trans_tcp:
|
case REMOTE_DRIVER_TRANSPORT_TCP:
|
||||||
priv->client = virNetClientNewTCP(priv->hostname, port, AF_UNSPEC);
|
priv->client = virNetClientNewTCP(priv->hostname, port, AF_UNSPEC);
|
||||||
if (!priv->client)
|
if (!priv->client)
|
||||||
goto failed;
|
goto failed;
|
||||||
@ -991,7 +1001,7 @@ doRemoteOpen(virConnectPtr conn,
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case trans_libssh2:
|
case REMOTE_DRIVER_TRANSPORT_LIBSSH2:
|
||||||
if (!sockname) {
|
if (!sockname) {
|
||||||
/* Right now we don't support default session connections */
|
/* Right now we don't support default session connections */
|
||||||
if (flags & VIR_DRV_OPEN_REMOTE_USER) {
|
if (flags & VIR_DRV_OPEN_REMOTE_USER) {
|
||||||
@ -1026,7 +1036,7 @@ doRemoteOpen(virConnectPtr conn,
|
|||||||
priv->is_secure = 1;
|
priv->is_secure = 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case trans_libssh:
|
case REMOTE_DRIVER_TRANSPORT_LIBSSH:
|
||||||
if (!sockname) {
|
if (!sockname) {
|
||||||
/* Right now we don't support default session connections */
|
/* Right now we don't support default session connections */
|
||||||
if (flags & VIR_DRV_OPEN_REMOTE_USER) {
|
if (flags & VIR_DRV_OPEN_REMOTE_USER) {
|
||||||
@ -1062,7 +1072,7 @@ doRemoteOpen(virConnectPtr conn,
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
#ifndef WIN32
|
#ifndef WIN32
|
||||||
case trans_unix:
|
case REMOTE_DRIVER_TRANSPORT_UNIX:
|
||||||
if (!sockname) {
|
if (!sockname) {
|
||||||
if (flags & VIR_DRV_OPEN_REMOTE_USER)
|
if (flags & VIR_DRV_OPEN_REMOTE_USER)
|
||||||
sockname = remoteGetUNIXSocketNonRoot();
|
sockname = remoteGetUNIXSocketNonRoot();
|
||||||
@ -1088,7 +1098,7 @@ doRemoteOpen(virConnectPtr conn,
|
|||||||
priv->is_secure = 1;
|
priv->is_secure = 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case trans_ssh:
|
case REMOTE_DRIVER_TRANSPORT_SSH:
|
||||||
if (!command && VIR_STRDUP(command, "ssh") < 0)
|
if (!command && VIR_STRDUP(command, "ssh") < 0)
|
||||||
goto failed;
|
goto failed;
|
||||||
|
|
||||||
@ -1120,7 +1130,7 @@ doRemoteOpen(virConnectPtr conn,
|
|||||||
priv->is_secure = 1;
|
priv->is_secure = 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case trans_ext: {
|
case REMOTE_DRIVER_TRANSPORT_EXT: {
|
||||||
char const *cmd_argv[] = { command, NULL };
|
char const *cmd_argv[] = { command, NULL };
|
||||||
if (!(priv->client = virNetClientNewExternal(cmd_argv)))
|
if (!(priv->client = virNetClientNewExternal(cmd_argv)))
|
||||||
goto failed;
|
goto failed;
|
||||||
@ -1132,15 +1142,20 @@ doRemoteOpen(virConnectPtr conn,
|
|||||||
|
|
||||||
#else /* WIN32 */
|
#else /* WIN32 */
|
||||||
|
|
||||||
case trans_unix:
|
case REMOTE_DRIVER_TRANSPORT_UNIX:
|
||||||
case trans_ssh:
|
case REMOTE_DRIVER_TRANSPORT_SSH:
|
||||||
case trans_ext:
|
case REMOTE_DRIVER_TRANSPORT_EXT:
|
||||||
virReportError(VIR_ERR_INVALID_ARG, "%s",
|
virReportError(VIR_ERR_INVALID_ARG, "%s",
|
||||||
_("transport methods unix, ssh and ext are not supported "
|
_("transport methods unix, ssh and ext are not supported "
|
||||||
"under Windows"));
|
"under Windows"));
|
||||||
goto failed;
|
goto failed;
|
||||||
|
|
||||||
#endif /* WIN32 */
|
#endif /* WIN32 */
|
||||||
|
|
||||||
|
case REMOTE_DRIVER_TRANSPORT_LAST:
|
||||||
|
default:
|
||||||
|
virReportEnumRangeError(remoteDriverTransport, transport);
|
||||||
|
goto failed;
|
||||||
} /* switch (transport) */
|
} /* switch (transport) */
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user