qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Amos Kong <akong@redhat.com>
To: aliguori@us.ibm.com, kvm@vger.kernel.org, quintela@redhat.com,
	jasowang@redhat.com, qemu-devel@nongnu.org, owasserm@redhat.com,
	laine@redhat.com
Subject: [Qemu-devel] [PATCH v3 6/9] net: use getaddrinfo() in tcp_start_common
Date: Wed, 07 Mar 2012 06:48:30 +0800	[thread overview]
Message-ID: <20120306224830.24264.44283.stgit@dhcp-8-167.nay.redhat.com> (raw)
In-Reply-To: <20120306224330.24264.9494.stgit@dhcp-8-167.nay.redhat.com>

Migrating with IPv6 address exists problem, gethostbyname()/inet_aton()
could not translate IPv6 address/port simply, so use getaddrinfo()
in tcp_start_common to translate network address and service.
We can get an address list by getaddrinfo().

Userlevel IPv6 Programming Introduction:
http://www.akkadia.org/drepper/userapi-ipv6.html

Reference RFC 3493, Basic Socket Interface Extensions for IPv6

Signed-off-by: Amos Kong <akong@redhat.com>
---
 net.c |   79 ++++++++++++++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 58 insertions(+), 21 deletions(-)

diff --git a/net.c b/net.c
index b05c881..de1db8c 100644
--- a/net.c
+++ b/net.c
@@ -99,7 +99,7 @@ static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
     return 0;
 }
 
-static int tcp_server_bind(int fd, struct sockaddr_in *saddr)
+static int tcp_server_bind(int fd, struct addrinfo *rp)
 {
     int ret;
     int val = 1;
@@ -107,7 +107,7 @@ static int tcp_server_bind(int fd, struct sockaddr_in *saddr)
     /* allow fast reuse */
     setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
 
-    ret = bind(fd, (struct sockaddr *)saddr, sizeof(*saddr));
+    ret = bind(fd, rp->ai_addr, rp->ai_addrlen);
 
     if (ret == -1) {
         ret = -socket_error();
@@ -116,12 +116,12 @@ static int tcp_server_bind(int fd, struct sockaddr_in *saddr)
 
 }
 
-static int tcp_client_connect(int fd, struct sockaddr_in *saddr)
+static int tcp_client_connect(int fd, struct addrinfo *rp)
 {
     int ret;
 
     do {
-        ret = connect(fd, (struct sockaddr *)saddr, sizeof(*saddr));
+        ret = connect(fd, rp->ai_addr, rp->ai_addrlen);
         if (ret == -1) {
             ret = -socket_error();
         }
@@ -132,38 +132,75 @@ static int tcp_client_connect(int fd, struct sockaddr_in *saddr)
 
 static int tcp_start_common(const char *str, int *fd, bool server)
 {
+    char hostname[512];
+    const char *service;
+    const char *name;
+    struct addrinfo hints;
+    struct addrinfo *result, *rp;
+    int s;
+    int sfd;
     int ret = -EINVAL;
-    struct sockaddr_in saddr;
 
     *fd = -1;
-    if (parse_host_port(&saddr, str) < 0) {
+    service = str;
+
+    if (get_str_sep(hostname, sizeof(hostname), &service, ':') < 0) {
         error_report("invalid host/port combination: %s", str);
         return -EINVAL;
     }
-
-    *fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
-    if (fd < 0) {
-        perror("socket");
-        return -1;
+    if (server && strlen(hostname) == 0) {
+        name = NULL;
+    } else {
+        name = hostname;
     }
-    socket_set_nonblock(*fd);
+
+    /* Obtain address(es) matching host/port */
+
+    memset(&hints, 0, sizeof(struct addrinfo));
+    hints.ai_family = AF_UNSPEC;     /* Allow IPv4 or IPv6 */
+    hints.ai_socktype = SOCK_STREAM; /* Datagram socket */
 
     if (server) {
-        ret = tcp_server_bind(*fd, &saddr);
-    } else {
-        ret = tcp_client_connect(*fd, &saddr);
+        hints.ai_flags = AI_PASSIVE;
     }
 
-#ifdef _WIN32
-    if (ret == -WSAEALREADY || ret == -WSAEINVAL) {
-        return ret;                  /* Success */
+    s = getaddrinfo(name, service, &hints, &result);
+    if (s != 0) {
+        error_report("qemu: getaddrinfo: %s", gai_strerror(s));
+        return -EINVAL;
     }
+
+    /* getaddrinfo() returns a list of address structures.
+       Try each address until we successfully bind/connect).
+       If socket(2) (or bind/connect(2)) fails, we (close the socket
+       and) try the next address. */
+
+    for (rp = result; rp != NULL; rp = rp->ai_next) {
+        sfd = qemu_socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
+        if (sfd == -1) {
+            ret = -errno;
+            continue;
+        }
+        socket_set_nonblock(sfd);
+        if (server) {
+            ret = tcp_server_bind(sfd, rp);
+        } else {
+            ret = tcp_client_connect(sfd, rp);
+        }
+#ifdef _WIN32
+        if (ret == -WSAEALREADY || ret == -WSAEINVAL) {
+            *fd = sfd;
+            break;                  /* Success */
+        }
 #endif
-    if (ret >= 0 || ret == -EINPROGRESS || ret == -EWOULDBLOCK) {
-        return ret;                  /* Success */
+        if (ret >= 0 || ret == -EINPROGRESS || ret == -EWOULDBLOCK) {
+            *fd = sfd;
+            break;                  /* Success */
+        }
+        closesocket(sfd);
     }
 
-    closesocket(*fd);
+    freeaddrinfo(result);
     return ret;
 }
 

  parent reply	other threads:[~2012-03-06 22:48 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-06 22:47 [Qemu-devel] [PATCH v3 0/9] support to migrate with IPv6 address Amos Kong
2012-03-06 22:47 ` [Qemu-devel] [PATCH v3 1/9] net: introduce tcp_server_start() Amos Kong
2012-03-13 16:39   ` Michael Roth
2012-03-14  8:33     ` Amos Kong
2012-03-14 14:58       ` Michael Roth
2012-03-16 10:47         ` Amos Kong
2012-03-14  7:14   ` Orit Wasserman
2012-03-14  7:27     ` Paolo Bonzini
2012-03-14  7:51       ` Amos Kong
2012-03-14  8:28         ` Paolo Bonzini
2012-03-14 10:03         ` Orit Wasserman
2012-03-14 11:39         ` Kevin Wolf
2012-03-06 22:47 ` [Qemu-devel] [PATCH v3 2/9] net: use tcp_server_start() for tcp server creation Amos Kong
2012-03-06 22:48 ` [Qemu-devel] [PATCH v3 3/9] net: introduce tcp_client_start() Amos Kong
2012-03-13 18:35   ` Michael Roth
2012-03-14 10:19     ` Amos Kong
2012-03-14 15:30       ` Michael Roth
2012-03-14  7:31   ` Orit Wasserman
2012-03-06 22:48 ` [Qemu-devel] [PATCH v3 4/9] net: use tcp_client_start for tcp client creation Amos Kong
2012-03-06 22:48 ` [Qemu-devel] [PATCH v3 5/9] net: refector tcp_*_start functions Amos Kong
2012-03-06 22:48 ` Amos Kong [this message]
2012-03-06 22:48 ` [Qemu-devel] [PATCH v3 7/9] net: introduce parse_host_port_info() Amos Kong
2012-03-06 22:48 ` [Qemu-devel] [PATCH v3 8/9] net: split hostname and service by last colon Amos Kong
2012-03-13 19:34   ` Michael Roth
2012-03-06 22:48 ` [Qemu-devel] [PATCH v3 9/9] net: support to include ipv6 address by brackets Amos Kong
2012-03-13 19:47   ` Michael Roth
2012-03-14  9:58     ` Amos Kong
2012-03-14 15:38       ` Michael Roth

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=20120306224830.24264.44283.stgit@dhcp-8-167.nay.redhat.com \
    --to=akong@redhat.com \
    --cc=aliguori@us.ibm.com \
    --cc=jasowang@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=laine@redhat.com \
    --cc=owasserm@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=quintela@redhat.com \
    /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).