From patchwork Tue Mar 6 22:47:45 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amos Kong X-Patchwork-Id: 145047 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 5F85FB6F9F for ; Wed, 7 Mar 2012 09:48:26 +1100 (EST) Received: from localhost ([::1]:45246 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S53BA-0002RT-9X for incoming@patchwork.ozlabs.org; Tue, 06 Mar 2012 17:48:24 -0500 Received: from eggs.gnu.org ([208.118.235.92]:44398) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S53Ad-0001R4-DA for qemu-devel@nongnu.org; Tue, 06 Mar 2012 17:48:11 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1S53Ab-0005fR-Mk for qemu-devel@nongnu.org; Tue, 06 Mar 2012 17:47:50 -0500 Received: from mx1.redhat.com ([209.132.183.28]:54567) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S53Ab-0005f9-FC for qemu-devel@nongnu.org; Tue, 06 Mar 2012 17:47:49 -0500 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q26MllAZ031665 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 6 Mar 2012 17:47:48 -0500 Received: from dhcp-8-167.nay.redhat.com (dhcp-8-167.nay.redhat.com [10.66.8.167]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id q26MliN0008068; Tue, 6 Mar 2012 17:47:45 -0500 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 From: Amos Kong Date: Wed, 07 Mar 2012 06:47:45 +0800 Message-ID: <20120306224745.24264.19990.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> User-Agent: StGit/0.15 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 1/9] net: introduce tcp_server_start() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Introduce tcp_server_start() by moving original code in tcp_start_incoming_migration(). Signed-off-by: Amos Kong --- net.c | 28 ++++++++++++++++++++++++++++ qemu_socket.h | 2 ++ 2 files changed, 30 insertions(+), 0 deletions(-) diff --git a/net.c b/net.c index c34474f..e90ff23 100644 --- a/net.c +++ b/net.c @@ -99,6 +99,34 @@ static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) return 0; } +int tcp_server_start(const char *str, int *fd) +{ + int val, ret; + struct sockaddr_in saddr; + + if (parse_host_port(&saddr, str) < 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; + } + socket_set_nonblock(*fd); + + /* allow fast reuse */ + val = 1; + setsockopt(*fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); + + ret = bind(*fd, (struct sockaddr *)&saddr, sizeof(saddr)); + if (ret < 0) { + closesocket(*fd); + } + return ret; +} + int parse_host_port(struct sockaddr_in *saddr, const char *str) { char buf[512]; diff --git a/qemu_socket.h b/qemu_socket.h index fe4cf6c..d612793 100644 --- a/qemu_socket.h +++ b/qemu_socket.h @@ -54,6 +54,8 @@ int unix_listen(const char *path, char *ostr, int olen); int unix_connect_opts(QemuOpts *opts); int unix_connect(const char *path); +int tcp_server_start(const char *str, int *fd); + /* Old, ipv4 only bits. Don't use for new code. */ int parse_host_port(struct sockaddr_in *saddr, const char *str); int socket_init(void);