char: refactor parsing of socket address information

To prepare for handling more address types, refactor the parsing of
socket address information to make it more robust and extensible.

Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrange 2017-12-22 13:20:40 +00:00 committed by Daniel P. Berrangé
parent 1723d6b1cf
commit 9bb4060c99

View File

@ -1012,21 +1012,25 @@ static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
SocketAddressLegacy *addr; SocketAddressLegacy *addr;
ChardevSocket *sock; ChardevSocket *sock;
if ((!!path + !!host) != 1) {
error_setg(errp,
"Exactly one of 'path' or 'host' required");
return;
}
backend->type = CHARDEV_BACKEND_KIND_SOCKET; backend->type = CHARDEV_BACKEND_KIND_SOCKET;
if (!path) { if (path) {
if (!host) { if (tls_creds) {
error_setg(errp, "chardev: socket: no host given"); error_setg(errp, "TLS can only be used over TCP socket");
return; return;
} }
} else if (host) {
if (!port) { if (!port) {
error_setg(errp, "chardev: socket: no port given"); error_setg(errp, "chardev: socket: no port given");
return; return;
} }
} else { } else {
if (tls_creds) { g_assert_not_reached();
error_setg(errp, "TLS can only be used over TCP socket");
return;
}
} }
sock = backend->u.socket.data = g_new0(ChardevSocket, 1); sock = backend->u.socket.data = g_new0(ChardevSocket, 1);
@ -1052,7 +1056,7 @@ static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
addr->type = SOCKET_ADDRESS_LEGACY_KIND_UNIX; addr->type = SOCKET_ADDRESS_LEGACY_KIND_UNIX;
q_unix = addr->u.q_unix.data = g_new0(UnixSocketAddress, 1); q_unix = addr->u.q_unix.data = g_new0(UnixSocketAddress, 1);
q_unix->path = g_strdup(path); q_unix->path = g_strdup(path);
} else { } else if (host) {
addr->type = SOCKET_ADDRESS_LEGACY_KIND_INET; addr->type = SOCKET_ADDRESS_LEGACY_KIND_INET;
addr->u.inet.data = g_new(InetSocketAddress, 1); addr->u.inet.data = g_new(InetSocketAddress, 1);
*addr->u.inet.data = (InetSocketAddress) { *addr->u.inet.data = (InetSocketAddress) {
@ -1065,6 +1069,8 @@ static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
.has_ipv6 = qemu_opt_get(opts, "ipv6"), .has_ipv6 = qemu_opt_get(opts, "ipv6"),
.ipv6 = qemu_opt_get_bool(opts, "ipv6", 0), .ipv6 = qemu_opt_get_bool(opts, "ipv6", 0),
}; };
} else {
g_assert_not_reached();
} }
sock->addr = addr; sock->addr = addr;
} }