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>
diff --git a/chardev/char-socket.c b/chardev/char-socket.c
index 1a371b9..a916ef4 100644
--- a/chardev/char-socket.c
+++ b/chardev/char-socket.c
@@ -1012,21 +1012,25 @@
SocketAddressLegacy *addr;
ChardevSocket *sock;
+ if ((!!path + !!host) != 1) {
+ error_setg(errp,
+ "Exactly one of 'path' or 'host' required");
+ return;
+ }
+
backend->type = CHARDEV_BACKEND_KIND_SOCKET;
- if (!path) {
- if (!host) {
- error_setg(errp, "chardev: socket: no host given");
+ if (path) {
+ if (tls_creds) {
+ error_setg(errp, "TLS can only be used over TCP socket");
return;
}
+ } else if (host) {
if (!port) {
error_setg(errp, "chardev: socket: no port given");
return;
}
} else {
- if (tls_creds) {
- error_setg(errp, "TLS can only be used over TCP socket");
- return;
- }
+ g_assert_not_reached();
}
sock = backend->u.socket.data = g_new0(ChardevSocket, 1);
@@ -1052,7 +1056,7 @@
addr->type = SOCKET_ADDRESS_LEGACY_KIND_UNIX;
q_unix = addr->u.q_unix.data = g_new0(UnixSocketAddress, 1);
q_unix->path = g_strdup(path);
- } else {
+ } else if (host) {
addr->type = SOCKET_ADDRESS_LEGACY_KIND_INET;
addr->u.inet.data = g_new(InetSocketAddress, 1);
*addr->u.inet.data = (InetSocketAddress) {
@@ -1065,6 +1069,8 @@
.has_ipv6 = qemu_opt_get(opts, "ipv6"),
.ipv6 = qemu_opt_get_bool(opts, "ipv6", 0),
};
+ } else {
+ g_assert_not_reached();
}
sock->addr = addr;
}