From patchwork Tue Feb 7 14:09:15 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Markus Armbruster X-Patchwork-Id: 139971 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 9643C1007D3 for ; Wed, 8 Feb 2012 03:26:30 +1100 (EST) Received: from localhost ([::1]:33805 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Rulkj-0002dk-6L for incoming@patchwork.ozlabs.org; Tue, 07 Feb 2012 09:10:37 -0500 Received: from eggs.gnu.org ([140.186.70.92]:53902) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ruljn-0001Ut-UE for qemu-devel@nongnu.org; Tue, 07 Feb 2012 09:09:50 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Ruljf-0005fJ-HB for qemu-devel@nongnu.org; Tue, 07 Feb 2012 09:09:39 -0500 Received: from oxygen.pond.sub.org ([78.46.104.156]:39187) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ruljf-0005eV-CR for qemu-devel@nongnu.org; Tue, 07 Feb 2012 09:09:31 -0500 Received: from blackfin.pond.sub.org (p5B32D218.dip.t-dialin.net [91.50.210.24]) by oxygen.pond.sub.org (Postfix) with ESMTPA id E0031A43F0; Tue, 7 Feb 2012 15:09:29 +0100 (CET) Received: by blackfin.pond.sub.org (Postfix, from userid 500) id 3408B6007E; Tue, 7 Feb 2012 15:09:27 +0100 (CET) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Tue, 7 Feb 2012 15:09:15 +0100 Message-Id: <1328623766-12287-9-git-send-email-armbru@redhat.com> X-Mailer: git-send-email 1.7.6.5 In-Reply-To: <1328623766-12287-1-git-send-email-armbru@redhat.com> References: <1328623766-12287-1-git-send-email-armbru@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 78.46.104.156 Cc: kwolf@redhat.com, aliguori@us.ibm.com Subject: [Qemu-devel] [PATCH 08/19] sockets: Clean up inet_listen_opts()'s convoluted bind() loop 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 Signed-off-by: Markus Armbruster --- qemu-sockets.c | 14 ++++++-------- 1 files changed, 6 insertions(+), 8 deletions(-) diff --git a/qemu-sockets.c b/qemu-sockets.c index e6e6c72..6bcb8e3 100644 --- a/qemu-sockets.c +++ b/qemu-sockets.c @@ -107,7 +107,7 @@ int inet_listen_opts(QemuOpts *opts, int port_offset) char port[33]; char uaddr[INET6_ADDRSTRLEN+1]; char uport[33]; - int slisten,rc,to,try_next; + int slisten, rc, to, port_min, port_max, p; memset(&ai,0, sizeof(ai)); ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; @@ -159,20 +159,18 @@ int inet_listen_opts(QemuOpts *opts, int port_offset) } #endif - for (;;) { + port_min = inet_getport(e); + port_max = to ? to + port_offset : port_min; + for (p = port_min; p <= port_max; p++) { + inet_setport(e, p); if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) { goto listen; } - try_next = to && (inet_getport(e) <= to + port_offset); - if (!try_next) + if (p == port_max) { fprintf(stderr,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__, inet_strfamily(e->ai_family), uaddr, inet_getport(e), strerror(errno)); - if (try_next) { - inet_setport(e, inet_getport(e) + 1); - continue; } - break; } closesocket(slisten); }