From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:43745) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S53BV-00049q-DF for qemu-devel@nongnu.org; Tue, 06 Mar 2012 17:48:46 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1S53BT-0005nu-Mx for qemu-devel@nongnu.org; Tue, 06 Mar 2012 17:48:44 -0500 Received: from mx1.redhat.com ([209.132.183.28]:4030) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S53BT-0005mq-EL for qemu-devel@nongnu.org; Tue, 06 Mar 2012 17:48:43 -0500 From: Amos Kong Date: Wed, 07 Mar 2012 06:48:39 +0800 Message-ID: <20120306224839.24264.35771.stgit@dhcp-8-167.nay.redhat.com> In-Reply-To: <20120306224330.24264.9494.stgit@dhcp-8-167.nay.redhat.com> References: <20120306224330.24264.9494.stgit@dhcp-8-167.nay.redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Subject: [Qemu-devel] [PATCH v3 7/9] net: introduce parse_host_port_info() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , 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 int parse_host_port(struct sockaddr_in *saddr, const char *str) Parsed address info will be restored into 'saddr', it only support ipv4. This function is used by net_socket_mcast_init() and net_socket_udp_init(). int parse_host_port_info(struct addrinfo *result, const char *str) Parsed address info will be restored into 'result', it's an address list. It can be used to parse IPv6 address/port. Signed-off-by: Amos Kong --- net.c | 26 ++++++++++++++++++++------ 1 files changed, 20 insertions(+), 6 deletions(-) diff --git a/net.c b/net.c index de1db8c..2518e5f 100644 --- a/net.c +++ b/net.c @@ -130,18 +130,15 @@ static int tcp_client_connect(int fd, struct addrinfo *rp) return ret; } -static int tcp_start_common(const char *str, int *fd, bool server) +static int parse_host_port_info(struct addrinfo **result, const char *str, + 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; - *fd = -1; service = str; if (get_str_sep(hostname, sizeof(hostname), &service, ':') < 0) { @@ -164,12 +161,29 @@ static int tcp_start_common(const char *str, int *fd, bool server) hints.ai_flags = AI_PASSIVE; } - s = getaddrinfo(name, service, &hints, &result); + s = getaddrinfo(name, service, &hints, result); if (s != 0) { error_report("qemu: getaddrinfo: %s", gai_strerror(s)); return -EINVAL; } + return 0; +} + +static int tcp_start_common(const char *str, int *fd, bool server) +{ + struct addrinfo *rp; + int sfd; + int ret = -EINVAL; + struct addrinfo *result; + + *fd = -1; + + ret = parse_host_port_info(&result, str, server); + if (ret < 0) { + 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