vnc: Hoist allocation of VncBasicInfo to callers

A future qapi patch will rework generated structs with a base
class to be unboxed.  In preparation for that, change the code
that allocates then populates an info struct to instead merely
populate the fields of an info field passed in as a parameter
(renaming vnc_basic_info_get* to vnc_init_basic_info*). Add
rudimentary Error handling at the lowest levels for cases
where the old code returned NULL; but rather than plumb Error
all the way through the stack, the callers drop the error and
return NULL as before.

Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <1445898903-12082-7-git-send-email-eblake@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
This commit is contained in:
Eric Blake 2015-10-26 16:34:45 -06:00 committed by Markus Armbruster
parent 9fb081e0b9
commit 98481bfcd6

View File

@ -156,10 +156,11 @@ char *vnc_socket_remote_addr(const char *format, int fd) {
return addr_to_string(format, &sa, salen); return addr_to_string(format, &sa, salen);
} }
static VncBasicInfo *vnc_basic_info_get(struct sockaddr_storage *sa, static void vnc_init_basic_info(struct sockaddr_storage *sa,
socklen_t salen) socklen_t salen,
VncBasicInfo *info,
Error **errp)
{ {
VncBasicInfo *info;
char host[NI_MAXHOST]; char host[NI_MAXHOST];
char serv[NI_MAXSERV]; char serv[NI_MAXSERV];
int err; int err;
@ -168,42 +169,44 @@ static VncBasicInfo *vnc_basic_info_get(struct sockaddr_storage *sa,
host, sizeof(host), host, sizeof(host),
serv, sizeof(serv), serv, sizeof(serv),
NI_NUMERICHOST | NI_NUMERICSERV)) != 0) { NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
VNC_DEBUG("Cannot resolve address %d: %s\n", error_setg(errp, "Cannot resolve address: %s",
err, gai_strerror(err)); gai_strerror(err));
return NULL; return;
} }
info = g_malloc0(sizeof(VncBasicInfo));
info->host = g_strdup(host); info->host = g_strdup(host);
info->service = g_strdup(serv); info->service = g_strdup(serv);
info->family = inet_netfamily(sa->ss_family); info->family = inet_netfamily(sa->ss_family);
return info;
} }
static VncBasicInfo *vnc_basic_info_get_from_server_addr(int fd) static void vnc_init_basic_info_from_server_addr(int fd, VncBasicInfo *info,
Error **errp)
{ {
struct sockaddr_storage sa; struct sockaddr_storage sa;
socklen_t salen; socklen_t salen;
salen = sizeof(sa); salen = sizeof(sa);
if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0) { if (getsockname(fd, (struct sockaddr*)&sa, &salen) < 0) {
return NULL; error_setg_errno(errp, errno, "getsockname failed");
return;
} }
return vnc_basic_info_get(&sa, salen); vnc_init_basic_info(&sa, salen, info, errp);
} }
static VncBasicInfo *vnc_basic_info_get_from_remote_addr(int fd) static void vnc_init_basic_info_from_remote_addr(int fd, VncBasicInfo *info,
Error **errp)
{ {
struct sockaddr_storage sa; struct sockaddr_storage sa;
socklen_t salen; socklen_t salen;
salen = sizeof(sa); salen = sizeof(sa);
if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0) { if (getpeername(fd, (struct sockaddr*)&sa, &salen) < 0) {
return NULL; error_setg_errno(errp, errno, "getpeername failed");
return;
} }
return vnc_basic_info_get(&sa, salen); vnc_init_basic_info(&sa, salen, info, errp);
} }
static const char *vnc_auth_name(VncDisplay *vd) { static const char *vnc_auth_name(VncDisplay *vd) {
@ -256,15 +259,18 @@ static const char *vnc_auth_name(VncDisplay *vd) {
static VncServerInfo *vnc_server_info_get(VncDisplay *vd) static VncServerInfo *vnc_server_info_get(VncDisplay *vd)
{ {
VncServerInfo *info; VncServerInfo *info;
VncBasicInfo *bi = vnc_basic_info_get_from_server_addr(vd->lsock); Error *err = NULL;
if (!bi) {
return NULL;
}
info = g_malloc(sizeof(*info)); info = g_malloc(sizeof(*info));
info->base = bi; info->base = g_malloc(sizeof(*info->base));
vnc_init_basic_info_from_server_addr(vd->lsock, info->base, &err);
info->has_auth = true; info->has_auth = true;
info->auth = g_strdup(vnc_auth_name(vd)); info->auth = g_strdup(vnc_auth_name(vd));
if (err) {
qapi_free_VncServerInfo(info);
info = NULL;
error_free(err);
}
return info; return info;
} }
@ -291,11 +297,16 @@ static void vnc_client_cache_auth(VncState *client)
static void vnc_client_cache_addr(VncState *client) static void vnc_client_cache_addr(VncState *client)
{ {
VncBasicInfo *bi = vnc_basic_info_get_from_remote_addr(client->csock); Error *err = NULL;
if (bi) { client->info = g_malloc0(sizeof(*client->info));
client->info = g_malloc0(sizeof(*client->info)); client->info->base = g_malloc0(sizeof(*client->info->base));
client->info->base = bi; vnc_init_basic_info_from_remote_addr(client->csock, client->info->base,
&err);
if (err) {
qapi_free_VncClientInfo(client->info);
client->info = NULL;
error_free(err);
} }
} }