From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49497) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZmwQq-00005r-EE for qemu-devel@nongnu.org; Fri, 16 Oct 2015 00:15:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZmwQl-0004U7-Rc for qemu-devel@nongnu.org; Fri, 16 Oct 2015 00:15:52 -0400 Received: from mx1.redhat.com ([209.132.183.28]:36517) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZmwQl-0004Tx-Iz for qemu-devel@nongnu.org; Fri, 16 Oct 2015 00:15:47 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (Postfix) with ESMTPS id 2108E7F for ; Fri, 16 Oct 2015 04:15:47 +0000 (UTC) From: Eric Blake Date: Thu, 15 Oct 2015 22:15:29 -0600 Message-Id: <1444968943-11254-5-git-send-email-eblake@redhat.com> In-Reply-To: <1444968943-11254-1-git-send-email-eblake@redhat.com> References: <1444968943-11254-1-git-send-email-eblake@redhat.com> Subject: [Qemu-devel] [PATCH v9 04/17] vnc: hoist allocation of VncBasicInfo to callers List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: armbru@redhat.com, Gerd Hoffmann 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. Add rudimentary Error handling for cases where the old code returned NULL; but as before, callers merely ignore errors for now. Signed-off-by: Eric Blake --- v9: (no v6-8): hoist from v5 33/46 --- ui/vnc.c | 52 ++++++++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/ui/vnc.c b/ui/vnc.c index d73966a..61af4ba 100644 --- a/ui/vnc.c +++ b/ui/vnc.c @@ -156,10 +156,11 @@ char *vnc_socket_remote_addr(const char *format, int fd) { return addr_to_string(format, &sa, salen); } -static VncBasicInfo *vnc_basic_info_get(struct sockaddr_storage *sa, - socklen_t salen) +static void vnc_basic_info_get(struct sockaddr_storage *sa, + socklen_t salen, + VncBasicInfo *info, + Error **errp) { - VncBasicInfo *info; char host[NI_MAXHOST]; char serv[NI_MAXSERV]; int err; @@ -168,42 +169,44 @@ static VncBasicInfo *vnc_basic_info_get(struct sockaddr_storage *sa, host, sizeof(host), serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV)) != 0) { - VNC_DEBUG("Cannot resolve address %d: %s\n", - err, gai_strerror(err)); - return NULL; + error_setg(errp, "Cannot resolve address %d: %s", + err, gai_strerror(err)); + return; } - info = g_malloc0(sizeof(VncBasicInfo)); info->host = g_strdup(host); info->service = g_strdup(serv); info->family = inet_netfamily(sa->ss_family); - return info; } -static VncBasicInfo *vnc_basic_info_get_from_server_addr(int fd) +static void vnc_basic_info_get_from_server_addr(int fd, VncBasicInfo *info, + Error **errp) { struct sockaddr_storage sa; socklen_t salen; salen = sizeof(sa); 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_basic_info_get(&sa, salen, info, errp); } -static VncBasicInfo *vnc_basic_info_get_from_remote_addr(int fd) +static void vnc_basic_info_get_from_remote_addr(int fd, VncBasicInfo *info, + Error **errp) { struct sockaddr_storage sa; socklen_t salen; salen = sizeof(sa); 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_basic_info_get(&sa, salen, info, errp); } static const char *vnc_auth_name(VncDisplay *vd) { @@ -256,13 +259,10 @@ static const char *vnc_auth_name(VncDisplay *vd) { static VncServerInfo *vnc_server_info_get(VncDisplay *vd) { VncServerInfo *info; - VncBasicInfo *bi = vnc_basic_info_get_from_server_addr(vd->lsock); - if (!bi) { - return NULL; - } info = g_malloc(sizeof(*info)); - info->base = bi; + info->base = g_malloc(sizeof(*info->base)); + vnc_basic_info_get_from_server_addr(vd->lsock, info->base, NULL); info->has_auth = true; info->auth = g_strdup(vnc_auth_name(vd)); return info; @@ -291,11 +291,15 @@ static void vnc_client_cache_auth(VncState *client) static void vnc_client_cache_addr(VncState *client) { - VncBasicInfo *bi = vnc_basic_info_get_from_remote_addr(client->csock); - - if (bi) { - client->info = g_malloc0(sizeof(*client->info)); - client->info->base = bi; + Error *err = NULL; + client->info = g_malloc0(sizeof(*client->info)); + client->info->base = g_malloc0(sizeof(*client->info->base)); + vnc_basic_info_get_from_remote_addr(client->csock, client->info->base, + &err); + if (err) { + qapi_free_VncClientInfo(client->info); + client->info = NULL; + error_free(err); } } -- 2.4.3