qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: qemu-devel@nongnu.org
Cc: armbru@redhat.com, Gerd Hoffmann <kraxel@redhat.com>
Subject: [Qemu-devel] [PATCH RFC v3 02/20] vnc: hoist allocation of VncBasicInfo to callers
Date: Tue, 18 Aug 2015 07:19:44 -0700	[thread overview]
Message-ID: <1439907602-11414-3-git-send-email-eblake@redhat.com> (raw)
In-Reply-To: <1439907602-11414-1-git-send-email-eblake@redhat.com>

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 <eblake@redhat.com>
---
 ui/vnc.c | 52 ++++++++++++++++++++++++++++------------------------
 1 file changed, 28 insertions(+), 24 deletions(-)

diff --git a/ui/vnc.c b/ui/vnc.c
index e26973a..273161c 100644
--- a/ui/vnc.c
+++ b/ui/vnc.c
@@ -153,10 +153,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;
@@ -165,42 +166,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) {
@@ -257,13 +260,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;
@@ -293,11 +293,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

  parent reply	other threads:[~2015-08-18 14:20 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-18 14:19 [Qemu-devel] [PATCH RFC v3 00/20] post-introspection qapi cleanups Eric Blake
2015-08-18 14:19 ` [Qemu-devel] [PATCH RFC v3 01/20] qapi: use 'type' in generated C code to match QMP union wire form Eric Blake
2015-08-18 14:19 ` Eric Blake [this message]
2015-08-18 14:19 ` [Qemu-devel] [PATCH RFC v3 03/20] qapi: Unbox base members Eric Blake
2015-08-18 14:19 ` [Qemu-devel] [PATCH RFC v3 04/20] qapi-visit: Remove redundant functions for flat union base Eric Blake
2015-08-18 14:19 ` [Qemu-devel] [PATCH RFC v3 05/20] qapi: Test use of 'number' within alternates Eric Blake
2015-08-18 14:19 ` [Qemu-devel] [PATCH RFC v3 06/20] qapi: Simplify visiting of alternate types Eric Blake
2015-08-18 14:19 ` [Qemu-devel] [PATCH RFC v3 07/20] qapi: Fix alternates that accept 'number' but not 'int' Eric Blake
2015-08-18 14:19 ` [Qemu-devel] [PATCH RFC v3 08/20] qapi: Don't pass pre-existing error to later call Eric Blake
2015-08-18 14:19 ` [Qemu-devel] [PATCH RFC v3 09/20] qapi: Use consistent generated code patterns Eric Blake
2015-08-18 14:19 ` [Qemu-devel] [PATCH RFC v3 10/20] qapi: Add tests for empty unions Eric Blake
2015-08-18 14:19 ` [Qemu-devel] [PATCH RFC v3 11/20] qapi: Rework deallocation of partial struct Eric Blake
2015-08-18 16:05 ` [Qemu-devel] [PATCH RFC v3 12/20] qapi: Avoid use of 'data' member of qapi unions Eric Blake
2015-08-18 16:05 ` [Qemu-devel] [PATCH RFC v3 13/20] qapi: Forbid empty unions and useless alternates Eric Blake
2015-08-18 16:05 ` [Qemu-devel] [PATCH RFC v3 14/20] qapi: Drop useless 'data' member of unions Eric Blake
2015-08-18 16:05 ` [Qemu-devel] [PATCH RFC v3 15/20] qapi: Remove dead visitor code Eric Blake
2015-08-18 16:05 ` [Qemu-devel] [PATCH RFC v3 16/20] qapi: Document visitor interfaces Eric Blake
2015-08-18 16:05 ` [Qemu-devel] [PATCH RFC v3 17/20] qapi: Change visit_type_XXX() to no longer return partial objects Eric Blake
2015-08-18 16:05 ` [Qemu-devel] [PATCH RFC v3 18/20] qapi: Plumb in 'box' to qapi generator lower levels Eric Blake
2015-08-18 16:05 ` [Qemu-devel] [PATCH RFC v3 19/20] qapi: Implement boxed structs for commands/events Eric Blake
2015-08-18 16:05 ` [Qemu-devel] [PATCH RFC v3 20/20] qapi: Support boxed unions Eric Blake

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1439907602-11414-3-git-send-email-eblake@redhat.com \
    --to=eblake@redhat.com \
    --cc=armbru@redhat.com \
    --cc=kraxel@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).