qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Corentin Chary <corentin.chary@gmail.com>
To: Jan Kiszka <jan.kiszka@siemens.com>
Cc: kvm@vger.kernel.org, Peter Lieven <pl@dlh.net>,
	qemu-devel <qemu-devel@nongnu.org>,
	Anthony Liguori <aliguori@linux.vnet.ibm.com>,
	Corentin Chary <corentin.chary@gmail.com>,
	Paolo Bonzini <pbonzini@redhat.com>
Subject: [Qemu-devel] [PATCH 1/2] sockets: add qemu_socketpair()
Date: Thu, 10 Mar 2011 13:59:38 +0100	[thread overview]
Message-ID: <1299761979-15197-1-git-send-email-corentin.chary@gmail.com> (raw)
In-Reply-To: <4D7767C0.6060609@siemens.com>

Signed-off-by: Corentin Chary <corentin.chary@gmail.com>
---
 osdep.c       |   83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qemu_socket.h |    1 +
 2 files changed, 84 insertions(+), 0 deletions(-)

diff --git a/osdep.c b/osdep.c
index 327583b..93bfbe0 100644
--- a/osdep.c
+++ b/osdep.c
@@ -147,6 +147,89 @@ int qemu_socket(int domain, int type, int protocol)
     return ret;
 }
 
+#ifdef _WIN32
+int qemu_socketpair(int domain, int type, int protocol, int socks[2])
+{
+    union {
+       struct sockaddr_in inaddr;
+       struct sockaddr addr;
+    } a;
+    int listener;
+    socklen_t addrlen = sizeof(a.inaddr);
+    int reuse = 1;
+
+    if (domain == AF_UNIX) {
+        domain = AF_INET;
+    }
+
+    if (socks == 0) {
+        return EINVAL;
+    }
+
+    listener = qemu_socket(domain, type, protocol);
+    if (listener < 0) {
+        return listener;
+    }
+
+    memset(&a, 0, sizeof(a));
+    a.inaddr.sin_family = AF_INET;
+    a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+    a.inaddr.sin_port = 0;
+
+    socks[0] = socks[1] = -1;
+
+    if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR,
+                   (char*) &reuse, (socklen_t) sizeof(reuse)) == -1) {
+        goto error;
+    }
+    if  (bind(listener, &a.addr, sizeof(a.inaddr)) < 0) {
+        goto error;
+    }
+
+    memset(&a, 0, sizeof(a));
+    if  (getsockname(listener, &a.addr, &addrlen) < 0) {
+        goto error;
+    }
+
+    a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+    a.inaddr.sin_family = AF_INET;
+
+    if (listen(listener, 1) < 0) {
+        goto error;
+    }
+
+    socks[0] = qemu_socket(AF_INET, SOCK_STREAM, 0);
+    if (socks[0] < 0) {
+        goto error;
+    }
+    if (connect(socks[0], &a.addr, sizeof(a.inaddr)) < 0) {
+        goto error;
+    }
+
+    socks[1] = qemu_accept(listener, NULL, NULL);
+    if (socks[1] < 0) {
+        goto error;
+    }
+
+    closesocket(listener);
+    return 0;
+
+error:
+    if (listener != -1)
+        closesocket(listener);
+    if (socks[0] != -1)
+        closesocket(socks[0]);
+    if (socks[1] != -1)
+        closesocket(socks[1]);
+    return -1;
+}
+#else
+int qemu_socketpair(int domain, int type, int protocol, int socks[2])
+{
+    return socketpair(domain, type, protocol, socks);
+}
+#endif
+
 /*
  * Accept a connection and set FD_CLOEXEC
  */
diff --git a/qemu_socket.h b/qemu_socket.h
index 180e4db..d7eb9a5 100644
--- a/qemu_socket.h
+++ b/qemu_socket.h
@@ -34,6 +34,7 @@ int inet_aton(const char *cp, struct in_addr *ia);
 
 /* misc helpers */
 int qemu_socket(int domain, int type, int protocol);
+int qemu_socketpair(int domain, int type, int protocol, int socks[2]);
 int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen);
 void socket_set_nonblock(int fd);
 int send_all(int fd, const void *buf, int len1);
-- 
1.7.3.4

  parent reply	other threads:[~2011-03-10 12:59 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-08 22:53 [Qemu-devel] segmentation fault in qemu-kvm-0.14.0 Peter Lieven
2011-03-09  7:13 ` Corentin Chary
2011-03-09  7:26 ` Stefan Weil
2011-03-09  7:39   ` Michael Tokarev
2011-03-09  9:22     ` Stefan Weil
2011-03-09 10:00   ` Peter Lieven
2011-03-15 12:53   ` Peter Lieven
2011-03-15 18:52     ` Stefan Weil
2011-03-09  7:37 ` [Qemu-devel] " Jan Kiszka
2011-03-09  8:50   ` Corentin Chary
2011-03-09  9:04     ` Jan Kiszka
2011-03-09  9:54       ` Corentin Chary
2011-03-09  9:58         ` Jan Kiszka
2011-03-09 10:02           ` Jan Kiszka
2011-03-09 10:06             ` Corentin Chary
2011-03-09 10:12               ` Jan Kiszka
2011-03-09 10:14                 ` Corentin Chary
2011-03-09 10:17                   ` Jan Kiszka
2011-03-09 10:41                     ` [Qemu-devel] [PATCH] vnc: threaded server depends on io-thread Corentin Chary
2011-03-09 10:50                       ` [Qemu-devel] " Peter Lieven
2011-03-09 10:57                         ` Corentin Chary
2011-03-09 11:05                           ` Stefan Hajnoczi
2011-03-09 11:25                             ` Jan Kiszka
2011-03-09 11:32                               ` Peter Lieven
2011-03-09 11:33                                 ` Jan Kiszka
2011-03-09 11:42                       ` Jan Kiszka
2011-03-09 12:50                         ` Peter Lieven
2011-03-09 13:21                         ` [Qemu-devel] [PATCH v2] " Corentin Chary
2011-03-09 13:42                           ` [Qemu-devel] " Corentin Chary
2011-03-09 13:51                           ` Paolo Bonzini
2011-03-09 13:59                             ` Corentin Chary
2011-03-10 12:59                         ` Corentin Chary [this message]
2011-03-10 12:59                         ` [Qemu-devel] [PATCH 2/2] vnc: don't mess up with iohandlers in the vnc thread Corentin Chary
2011-03-10 13:06                           ` [Qemu-devel] " Paolo Bonzini
2011-03-10 13:45                             ` Anthony Liguori
2011-03-10 13:54                               ` Corentin Chary
2011-03-10 13:58                                 ` Paolo Bonzini
2011-03-10 13:56                               ` Paolo Bonzini
2011-03-10 13:47                           ` Peter Lieven
2011-03-10 15:13                         ` [Qemu-devel] [PATCH v5] " Corentin Chary
2011-03-14  9:19                           ` [Qemu-devel] " Corentin Chary
2011-03-14  9:55                             ` Peter Lieven
2011-03-15 16:55                             ` Peter Lieven
2011-03-15 18:07                               ` Peter Lieven
2011-03-09 10:02   ` [Qemu-devel] Re: segmentation fault in qemu-kvm-0.14.0 Peter Lieven
2011-03-09 10:16   ` Peter Lieven
2011-03-09 10:20     ` Jan Kiszka
2011-03-09 10:31       ` Peter Lieven
2011-03-09 11:20   ` Paolo Bonzini
2011-03-09 11:44     ` Jan Kiszka

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=1299761979-15197-1-git-send-email-corentin.chary@gmail.com \
    --to=corentin.chary@gmail.com \
    --cc=aliguori@linux.vnet.ibm.com \
    --cc=jan.kiszka@siemens.com \
    --cc=kvm@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=pl@dlh.net \
    --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).