dcavalca / rpms / qemu

Forked from rpms/qemu a year ago
Clone

Blame 0232-Refactor-inet_connect_opts-function.patch

5544c1
From 2f56ee52ed70bed32de06ba1c9ef22e4d4d504f0 Mon Sep 17 00:00:00 2001
5544c1
From: "Michael S. Tsirkin" <mst@redhat.com>
5544c1
Date: Mon, 24 Sep 2012 13:11:07 +0200
5544c1
Subject: [PATCH] Refactor inet_connect_opts function
5544c1
5544c1
refactor address resolution code to fix nonblocking connect
5544c1
remove getnameinfo call
5544c1
5544c1
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
5544c1
Signed-off-by: Amos Kong <akong@redhat.com>
5544c1
Signed-off-by: Orit Wasserman <owasserm@redhat.com>
5544c1
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
5544c1
(cherry picked from commit 05bc1d8a4b2f77df8cc9880a552047e30c16f1f8)
5544c1
5544c1
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
5544c1
---
5544c1
 qemu-sockets.c | 148 +++++++++++++++++++++++++++++++++------------------------
5544c1
 1 file changed, 85 insertions(+), 63 deletions(-)
5544c1
5544c1
diff --git a/qemu-sockets.c b/qemu-sockets.c
5544c1
index 037775b..22797bf 100644
5544c1
--- a/qemu-sockets.c
5544c1
+++ b/qemu-sockets.c
5544c1
@@ -209,95 +209,117 @@ listen:
5544c1
     return slisten;
5544c1
 }
5544c1
 
5544c1
-int inet_connect_opts(QemuOpts *opts, bool *in_progress, Error **errp)
5544c1
+#ifdef _WIN32
5544c1
+#define QEMU_SOCKET_RC_INPROGRESS(rc) \
5544c1
+    ((rc) == -EINPROGRESS || (rc) == -EWOULDBLOCK || (rc) == -WSAEALREADY)
5544c1
+#else
5544c1
+#define QEMU_SOCKET_RC_INPROGRESS(rc) \
5544c1
+    ((rc) == -EINPROGRESS)
5544c1
+#endif
5544c1
+
5544c1
+static int inet_connect_addr(struct addrinfo *addr, bool block,
5544c1
+                             bool *in_progress)
5544c1
 {
5544c1
-    struct addrinfo ai,*res,*e;
5544c1
+    int sock, rc;
5544c1
+
5544c1
+    if (in_progress) {
5544c1
+        *in_progress = false;
5544c1
+    }
5544c1
+
5544c1
+    sock = qemu_socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
5544c1
+    if (sock < 0) {
5544c1
+        fprintf(stderr, "%s: socket(%s): %s\n", __func__,
5544c1
+                inet_strfamily(addr->ai_family), strerror(errno));
5544c1
+        return -1;
5544c1
+    }
5544c1
+    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
5544c1
+    if (!block) {
5544c1
+        socket_set_nonblock(sock);
5544c1
+    }
5544c1
+    /* connect to peer */
5544c1
+    do {
5544c1
+        rc = 0;
5544c1
+        if (connect(sock, addr->ai_addr, addr->ai_addrlen) < 0) {
5544c1
+            rc = -socket_error();
5544c1
+        }
5544c1
+    } while (rc == -EINTR);
5544c1
+
5544c1
+    if (!block && QEMU_SOCKET_RC_INPROGRESS(rc)) {
5544c1
+        if (in_progress) {
5544c1
+            *in_progress = true;
5544c1
+        }
5544c1
+    } else if (rc < 0) {
5544c1
+        closesocket(sock);
5544c1
+        return -1;
5544c1
+    }
5544c1
+    return sock;
5544c1
+}
5544c1
+
5544c1
+static struct addrinfo *inet_parse_connect_opts(QemuOpts *opts, Error **errp)
5544c1
+{
5544c1
+    struct addrinfo ai, *res;
5544c1
+    int rc;
5544c1
     const char *addr;
5544c1
     const char *port;
5544c1
-    char uaddr[INET6_ADDRSTRLEN+1];
5544c1
-    char uport[33];
5544c1
-    int sock,rc;
5544c1
-    bool block;
5544c1
 
5544c1
-    memset(&ai,0, sizeof(ai));
5544c1
+    memset(&ai, 0, sizeof(ai));
5544c1
     ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
5544c1
     ai.ai_family = PF_UNSPEC;
5544c1
     ai.ai_socktype = SOCK_STREAM;
5544c1
 
5544c1
-    if (in_progress) {
5544c1
-        *in_progress = false;
5544c1
-    }
5544c1
-
5544c1
     addr = qemu_opt_get(opts, "host");
5544c1
     port = qemu_opt_get(opts, "port");
5544c1
-    block = qemu_opt_get_bool(opts, "block", 0);
5544c1
     if (addr == NULL || port == NULL) {
5544c1
-        fprintf(stderr, "inet_connect: host and/or port not specified\n");
5544c1
+        fprintf(stderr,
5544c1
+                "inet_parse_connect_opts: host and/or port not specified\n");
5544c1
         error_set(errp, QERR_SOCKET_CREATE_FAILED);
5544c1
-        return -1;
5544c1
+        return NULL;
5544c1
     }
5544c1
 
5544c1
-    if (qemu_opt_get_bool(opts, "ipv4", 0))
5544c1
+    if (qemu_opt_get_bool(opts, "ipv4", 0)) {
5544c1
         ai.ai_family = PF_INET;
5544c1
-    if (qemu_opt_get_bool(opts, "ipv6", 0))
5544c1
+    }
5544c1
+    if (qemu_opt_get_bool(opts, "ipv6", 0)) {
5544c1
         ai.ai_family = PF_INET6;
5544c1
+    }
5544c1
 
5544c1
     /* lookup */
5544c1
-    if (0 != (rc = getaddrinfo(addr, port, &ai, &res))) {
5544c1
-        fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
5544c1
+    rc = getaddrinfo(addr, port, &ai, &res;;
5544c1
+    if (rc != 0) {
5544c1
+        fprintf(stderr, "getaddrinfo(%s,%s): %s\n", addr, port,
5544c1
                 gai_strerror(rc));
5544c1
         error_set(errp, QERR_SOCKET_CREATE_FAILED);
5544c1
-	return -1;
5544c1
+        return NULL;
5544c1
+    }
5544c1
+    return res;
5544c1
+}
5544c1
+
5544c1
+int inet_connect_opts(QemuOpts *opts, bool *in_progress, Error **errp)
5544c1
+{
5544c1
+    struct addrinfo *res, *e;
5544c1
+    int sock = -1;
5544c1
+    bool block = qemu_opt_get_bool(opts, "block", 0);
5544c1
+
5544c1
+    res = inet_parse_connect_opts(opts, errp);
5544c1
+    if (!res) {
5544c1
+        return -1;
5544c1
+    }
5544c1
+
5544c1
+    if (in_progress) {
5544c1
+        *in_progress = false;
5544c1
     }
5544c1
 
5544c1
     for (e = res; e != NULL; e = e->ai_next) {
5544c1
-        if (getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
5544c1
-                            uaddr,INET6_ADDRSTRLEN,uport,32,
5544c1
-                            NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
5544c1
-            fprintf(stderr,"%s: getnameinfo: oops\n", __FUNCTION__);
5544c1
-            continue;
5544c1
-        }
5544c1
-        sock = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol);
5544c1
-        if (sock < 0) {
5544c1
-            fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
5544c1
-            inet_strfamily(e->ai_family), strerror(errno));
5544c1
-            continue;
5544c1
-        }
5544c1
-        setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
5544c1
-        if (!block) {
5544c1
-            socket_set_nonblock(sock);
5544c1
-        }
5544c1
-        /* connect to peer */
5544c1
-        do {
5544c1
-            rc = 0;
5544c1
-            if (connect(sock, e->ai_addr, e->ai_addrlen) < 0) {
5544c1
-                rc = -socket_error();
5544c1
-            }
5544c1
-        } while (rc == -EINTR);
5544c1
-
5544c1
-  #ifdef _WIN32
5544c1
-        if (!block && (rc == -EINPROGRESS || rc == -EWOULDBLOCK
5544c1
-                       || rc == -WSAEALREADY)) {
5544c1
-  #else
5544c1
-        if (!block && (rc == -EINPROGRESS)) {
5544c1
-  #endif
5544c1
-            if (in_progress) {
5544c1
-                *in_progress = true;
5544c1
-            }
5544c1
-        } else if (rc < 0) {
5544c1
-            if (NULL == e->ai_next)
5544c1
-                fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__,
5544c1
-                        inet_strfamily(e->ai_family),
5544c1
-                        e->ai_canonname, uaddr, uport, strerror(errno));
5544c1
-            closesocket(sock);
5544c1
-            continue;
5544c1
+        sock = inet_connect_addr(e, block, in_progress);
5544c1
+        if (sock >= 0) {
5544c1
+            break;
5544c1
         }
5544c1
-        freeaddrinfo(res);
5544c1
-        return sock;
5544c1
     }
5544c1
-    error_set(errp, QERR_SOCKET_CONNECT_FAILED);
5544c1
+    if (sock < 0) {
5544c1
+        error_set(errp, QERR_SOCKET_CONNECT_FAILED);
5544c1
+    }
5544c1
     freeaddrinfo(res);
5544c1
-    return -1;
5544c1
+    return sock;
5544c1
 }
5544c1
 
5544c1
 int inet_dgram_opts(QemuOpts *opts)
5544c1
-- 
5544c1
1.7.12.1
5544c1