diff options
Diffstat (limited to 'util/qemu-sockets.c')
-rw-r--r-- | util/qemu-sockets.c | 181 |
1 files changed, 87 insertions, 94 deletions
diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c index 8188d9a8d7..d8183f79d7 100644 --- a/util/qemu-sockets.c +++ b/util/qemu-sockets.c @@ -22,6 +22,7 @@ #endif /* CONFIG_AF_VSOCK */ #include "monitor/monitor.h" +#include "qapi/clone-visitor.h" #include "qapi/error.h" #include "qemu/sockets.h" #include "qemu/main-loop.h" @@ -578,16 +579,15 @@ err: } /* compatibility wrapper */ -InetSocketAddress *inet_parse(const char *str, Error **errp) +int inet_parse(InetSocketAddress *addr, const char *str, Error **errp) { - InetSocketAddress *addr; const char *optstr, *h; char host[65]; char port[33]; int to; int pos; - addr = g_new0(InetSocketAddress, 1); + memset(addr, 0, sizeof(*addr)); /* parse address */ if (str[0] == ':') { @@ -595,20 +595,20 @@ InetSocketAddress *inet_parse(const char *str, Error **errp) host[0] = '\0'; if (sscanf(str, ":%32[^,]%n", port, &pos) != 1) { error_setg(errp, "error parsing port in address '%s'", str); - goto fail; + return -1; } } else if (str[0] == '[') { /* IPv6 addr */ if (sscanf(str, "[%64[^]]]:%32[^,]%n", host, port, &pos) != 2) { error_setg(errp, "error parsing IPv6 address '%s'", str); - goto fail; + return -1; } addr->ipv6 = addr->has_ipv6 = true; } else { /* hostname or IPv4 addr */ if (sscanf(str, "%64[^:]:%32[^,]%n", host, port, &pos) != 2) { error_setg(errp, "error parsing address '%s'", str); - goto fail; + return -1; } if (host[strspn(host, "0123456789.")] == '\0') { addr->ipv4 = addr->has_ipv4 = true; @@ -626,7 +626,7 @@ InetSocketAddress *inet_parse(const char *str, Error **errp) if (sscanf(h, "%d%n", &to, &pos) != 1 || (h[pos] != '\0' && h[pos] != ',')) { error_setg(errp, "error parsing to= argument"); - goto fail; + return -1; } addr->has_to = true; addr->to = to; @@ -637,11 +637,7 @@ InetSocketAddress *inet_parse(const char *str, Error **errp) if (strstr(optstr, ",ipv6")) { addr->ipv6 = addr->has_ipv6 = true; } - return addr; - -fail: - qapi_free_InetSocketAddress(addr); - return NULL; + return 0; } @@ -656,13 +652,12 @@ fail: int inet_connect(const char *str, Error **errp) { int sock = -1; - InetSocketAddress *addr; + InetSocketAddress *addr = g_new(InetSocketAddress, 1); - addr = inet_parse(str, errp); - if (addr != NULL) { + if (!inet_parse(addr, str, errp)) { sock = inet_connect_saddr(addr, NULL, NULL, errp); - qapi_free_InetSocketAddress(addr); } + qapi_free_InetSocketAddress(addr); return sock; } @@ -793,26 +788,25 @@ static int vsock_listen_saddr(VsockSocketAddress *vaddr, return slisten; } -static VsockSocketAddress *vsock_parse(const char *str, Error **errp) +static int vsock_parse(VsockSocketAddress *addr, const char *str, + Error **errp) { - VsockSocketAddress *addr = NULL; char cid[33]; char port[33]; int n; if (sscanf(str, "%32[^:]:%32[^,]%n", cid, port, &n) != 2) { error_setg(errp, "error parsing address '%s'", str); - return NULL; + return -1; } if (str[n] != '\0') { error_setg(errp, "trailing characters in address '%s'", str); - return NULL; + return -1; } - addr = g_new0(VsockSocketAddress, 1); addr->cid = g_strdup(cid); addr->port = g_strdup(port); - return addr; + return 0; } #else static void vsock_unsupported(Error **errp) @@ -835,10 +829,11 @@ static int vsock_listen_saddr(VsockSocketAddress *vaddr, return -1; } -static VsockSocketAddress *vsock_parse(const char *str, Error **errp) +static int vsock_parse(VsockSocketAddress *addr, const char *str, + Error **errp) { vsock_unsupported(errp); - return NULL; + return -1; } #endif /* CONFIG_AF_VSOCK */ @@ -1045,29 +1040,25 @@ SocketAddress *socket_parse(const char *str, Error **errp) error_setg(errp, "invalid Unix socket address"); goto fail; } else { - addr->type = SOCKET_ADDRESS_KIND_UNIX; - addr->u.q_unix.data = g_new(UnixSocketAddress, 1); - addr->u.q_unix.data->path = g_strdup(str + 5); + addr->type = SOCKET_ADDRESS_TYPE_UNIX; + addr->u.q_unix.path = g_strdup(str + 5); } } else if (strstart(str, "fd:", NULL)) { if (str[3] == '\0') { error_setg(errp, "invalid file descriptor address"); goto fail; } else { - addr->type = SOCKET_ADDRESS_KIND_FD; - addr->u.fd.data = g_new(String, 1); - addr->u.fd.data->str = g_strdup(str + 3); + addr->type = SOCKET_ADDRESS_TYPE_FD; + addr->u.fd.str = g_strdup(str + 3); } } else if (strstart(str, "vsock:", NULL)) { - addr->type = SOCKET_ADDRESS_KIND_VSOCK; - addr->u.vsock.data = vsock_parse(str + strlen("vsock:"), errp); - if (addr->u.vsock.data == NULL) { + addr->type = SOCKET_ADDRESS_TYPE_VSOCK; + if (vsock_parse(&addr->u.vsock, str + strlen("vsock:"), errp)) { goto fail; } } else { - addr->type = SOCKET_ADDRESS_KIND_INET; - addr->u.inet.data = inet_parse(str, errp); - if (addr->u.inet.data == NULL) { + addr->type = SOCKET_ADDRESS_TYPE_INET; + if (inet_parse(&addr->u.inet, str, errp)) { goto fail; } } @@ -1084,24 +1075,24 @@ int socket_connect(SocketAddress *addr, NonBlockingConnectHandler *callback, int fd; switch (addr->type) { - case SOCKET_ADDRESS_KIND_INET: - fd = inet_connect_saddr(addr->u.inet.data, callback, opaque, errp); + case SOCKET_ADDRESS_TYPE_INET: + fd = inet_connect_saddr(&addr->u.inet, callback, opaque, errp); break; - case SOCKET_ADDRESS_KIND_UNIX: - fd = unix_connect_saddr(addr->u.q_unix.data, callback, opaque, errp); + case SOCKET_ADDRESS_TYPE_UNIX: + fd = unix_connect_saddr(&addr->u.q_unix, callback, opaque, errp); break; - case SOCKET_ADDRESS_KIND_FD: - fd = monitor_get_fd(cur_mon, addr->u.fd.data->str, errp); + case SOCKET_ADDRESS_TYPE_FD: + fd = monitor_get_fd(cur_mon, addr->u.fd.str, errp); if (fd >= 0 && callback) { qemu_set_nonblock(fd); callback(fd, NULL, opaque); } break; - case SOCKET_ADDRESS_KIND_VSOCK: - fd = vsock_connect_saddr(addr->u.vsock.data, callback, opaque, errp); + case SOCKET_ADDRESS_TYPE_VSOCK: + fd = vsock_connect_saddr(&addr->u.vsock, callback, opaque, errp); break; default: @@ -1115,20 +1106,20 @@ int socket_listen(SocketAddress *addr, Error **errp) int fd; switch (addr->type) { - case SOCKET_ADDRESS_KIND_INET: - fd = inet_listen_saddr(addr->u.inet.data, 0, false, errp); + case SOCKET_ADDRESS_TYPE_INET: + fd = inet_listen_saddr(&addr->u.inet, 0, false, errp); break; - case SOCKET_ADDRESS_KIND_UNIX: - fd = unix_listen_saddr(addr->u.q_unix.data, false, errp); + case SOCKET_ADDRESS_TYPE_UNIX: + fd = unix_listen_saddr(&addr->u.q_unix, false, errp); break; - case SOCKET_ADDRESS_KIND_FD: - fd = monitor_get_fd(cur_mon, addr->u.fd.data->str, errp); + case SOCKET_ADDRESS_TYPE_FD: + fd = monitor_get_fd(cur_mon, addr->u.fd.str, errp); break; - case SOCKET_ADDRESS_KIND_VSOCK: - fd = vsock_listen_saddr(addr->u.vsock.data, errp); + case SOCKET_ADDRESS_TYPE_VSOCK: + fd = vsock_listen_saddr(&addr->u.vsock, errp); break; default: @@ -1143,12 +1134,12 @@ void socket_listen_cleanup(int fd, Error **errp) addr = socket_local_address(fd, errp); - if (addr->type == SOCKET_ADDRESS_KIND_UNIX - && addr->u.q_unix.data->path) { - if (unlink(addr->u.q_unix.data->path) < 0 && errno != ENOENT) { + if (addr->type == SOCKET_ADDRESS_TYPE_UNIX + && addr->u.q_unix.path) { + if (unlink(addr->u.q_unix.path) < 0 && errno != ENOENT) { error_setg_errno(errp, errno, "Failed to unlink socket %s", - addr->u.q_unix.data->path); + addr->u.q_unix.path); } } @@ -1160,13 +1151,13 @@ int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp) int fd; /* - * TODO SOCKET_ADDRESS_KIND_FD when fd is AF_INET or AF_INET6 + * TODO SOCKET_ADDRESS_TYPE_FD when fd is AF_INET or AF_INET6 * (although other address families can do SOCK_DGRAM, too) */ switch (remote->type) { - case SOCKET_ADDRESS_KIND_INET: - fd = inet_dgram_saddr(remote->u.inet.data, - local ? local->u.inet.data : NULL, errp); + case SOCKET_ADDRESS_TYPE_INET: + fd = inet_dgram_saddr(&remote->u.inet, + local ? &local->u.inet : NULL, errp); break; default: @@ -1199,8 +1190,8 @@ socket_sockaddr_to_address_inet(struct sockaddr_storage *sa, } addr = g_new0(SocketAddress, 1); - addr->type = SOCKET_ADDRESS_KIND_INET; - inet = addr->u.inet.data = g_new0(InetSocketAddress, 1); + addr->type = SOCKET_ADDRESS_TYPE_INET; + inet = &addr->u.inet; inet->host = g_strdup(host); inet->port = g_strdup(serv); if (sa->ss_family == AF_INET) { @@ -1223,11 +1214,9 @@ socket_sockaddr_to_address_unix(struct sockaddr_storage *sa, struct sockaddr_un *su = (struct sockaddr_un *)sa; addr = g_new0(SocketAddress, 1); - addr->type = SOCKET_ADDRESS_KIND_UNIX; - addr->u.q_unix.data = g_new0(UnixSocketAddress, 1); + addr->type = SOCKET_ADDRESS_TYPE_UNIX; if (su->sun_path[0]) { - addr->u.q_unix.data->path = g_strndup(su->sun_path, - sizeof(su->sun_path)); + addr->u.q_unix.path = g_strndup(su->sun_path, sizeof(su->sun_path)); } return addr; @@ -1245,8 +1234,8 @@ socket_sockaddr_to_address_vsock(struct sockaddr_storage *sa, struct sockaddr_vm *svm = (struct sockaddr_vm *)sa; addr = g_new0(SocketAddress, 1); - addr->type = SOCKET_ADDRESS_KIND_VSOCK; - addr->u.vsock.data = vaddr = g_new0(VsockSocketAddress, 1); + addr->type = SOCKET_ADDRESS_TYPE_VSOCK; + vaddr = &addr->u.vsock; vaddr->cid = g_strdup_printf("%u", svm->svm_cid); vaddr->port = g_strdup_printf("%u", svm->svm_port); @@ -1318,8 +1307,8 @@ char *socket_address_to_string(struct SocketAddress *addr, Error **errp) InetSocketAddress *inet; switch (addr->type) { - case SOCKET_ADDRESS_KIND_INET: - inet = addr->u.inet.data; + case SOCKET_ADDRESS_TYPE_INET: + inet = &addr->u.inet; if (strchr(inet->host, ':') == NULL) { buf = g_strdup_printf("%s:%s", inet->host, inet->port); } else { @@ -1327,18 +1316,18 @@ char *socket_address_to_string(struct SocketAddress *addr, Error **errp) } break; - case SOCKET_ADDRESS_KIND_UNIX: - buf = g_strdup(addr->u.q_unix.data->path); + case SOCKET_ADDRESS_TYPE_UNIX: + buf = g_strdup(addr->u.q_unix.path); break; - case SOCKET_ADDRESS_KIND_FD: - buf = g_strdup(addr->u.fd.data->str); + case SOCKET_ADDRESS_TYPE_FD: + buf = g_strdup(addr->u.fd.str); break; - case SOCKET_ADDRESS_KIND_VSOCK: + case SOCKET_ADDRESS_TYPE_VSOCK: buf = g_strdup_printf("%s:%s", - addr->u.vsock.data->cid, - addr->u.vsock.data->port); + addr->u.vsock.cid, + addr->u.vsock.port); break; default: @@ -1347,29 +1336,33 @@ char *socket_address_to_string(struct SocketAddress *addr, Error **errp) return buf; } -SocketAddress *socket_address_crumple(SocketAddressFlat *addr_flat) +SocketAddress *socket_address_flatten(SocketAddressLegacy *addr_legacy) { SocketAddress *addr = g_new(SocketAddress, 1); - switch (addr_flat->type) { - case SOCKET_ADDRESS_FLAT_TYPE_INET: - addr->type = SOCKET_ADDRESS_KIND_INET; - addr->u.inet.data = QAPI_CLONE(InetSocketAddress, - &addr_flat->u.inet); + if (!addr_legacy) { + return NULL; + } + + switch (addr_legacy->type) { + case SOCKET_ADDRESS_LEGACY_KIND_INET: + addr->type = SOCKET_ADDRESS_TYPE_INET; + QAPI_CLONE_MEMBERS(InetSocketAddress, &addr->u.inet, + addr_legacy->u.inet.data); break; - case SOCKET_ADDRESS_FLAT_TYPE_UNIX: - addr->type = SOCKET_ADDRESS_KIND_UNIX; - addr->u.q_unix.data = QAPI_CLONE(UnixSocketAddress, - &addr_flat->u.q_unix); + case SOCKET_ADDRESS_LEGACY_KIND_UNIX: + addr->type = SOCKET_ADDRESS_TYPE_UNIX; + QAPI_CLONE_MEMBERS(UnixSocketAddress, &addr->u.q_unix, + addr_legacy->u.q_unix.data); break; - case SOCKET_ADDRESS_FLAT_TYPE_VSOCK: - addr->type = SOCKET_ADDRESS_KIND_VSOCK; - addr->u.vsock.data = QAPI_CLONE(VsockSocketAddress, - &addr_flat->u.vsock); + case SOCKET_ADDRESS_LEGACY_KIND_VSOCK: + addr->type = SOCKET_ADDRESS_TYPE_VSOCK; + QAPI_CLONE_MEMBERS(VsockSocketAddress, &addr->u.vsock, + addr_legacy->u.vsock.data); break; - case SOCKET_ADDRESS_FLAT_TYPE_FD: - addr->type = SOCKET_ADDRESS_KIND_FD; - addr->u.fd.data = QAPI_CLONE(String, &addr_flat->u.fd); + case SOCKET_ADDRESS_LEGACY_KIND_FD: + addr->type = SOCKET_ADDRESS_TYPE_FD; + QAPI_CLONE_MEMBERS(String, &addr->u.fd, addr_legacy->u.fd.data); break; default: abort(); |