From patchwork Thu May 19 15:30:17 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Mkrtchyan, Tigran" X-Patchwork-Id: 9127819 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9D39360221 for ; Thu, 19 May 2016 15:30:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8EFC7281BE for ; Thu, 19 May 2016 15:30:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 816DA281C5; Thu, 19 May 2016 15:30:30 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,HK_RANDOM_FROM, RCVD_IN_DNSWL_HI,UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 87221281BE for ; Thu, 19 May 2016 15:30:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932132AbcESPa2 (ORCPT ); Thu, 19 May 2016 11:30:28 -0400 Received: from smtp-o-1.desy.de ([131.169.56.154]:37077 "EHLO smtp-o-1.desy.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754284AbcESPa0 (ORCPT ); Thu, 19 May 2016 11:30:26 -0400 X-Clacks-Overhead: GNU Terry Pratchett Received: from smtp-map-1.desy.de (smtp-map-1.desy.de [131.169.56.66]) by smtp-o-1.desy.de (DESY-O-1) with ESMTP id 050E528092E for ; Thu, 19 May 2016 17:30:24 +0200 (CEST) Received: from ZITSWEEP2.win.desy.de (zitsweep2.win.desy.de [131.169.97.96]) by smtp-map-1.desy.de (DESY_MAP_1) with ESMTP id E2E5513E92 for ; Thu, 19 May 2016 17:30:23 +0200 (MEST) Received: from smtp-intra-3.desy.de (lb-40-26.desy.de) by ZITSWEEP2.win.desy.de (Clearswift SMTPRS 5.5.0) with ESMTP id ; Thu, 19 May 2016 17:30:24 +0200 Received: from anahit.desy.de (anahit.desy.de [131.169.185.68]) by smtp-intra-3.desy.de (DESY-INTRA-3) with ESMTP id 8BBD72CE06; Thu, 19 May 2016 17:30:23 +0200 (MEST) From: Tigran Mkrtchyan To: bfields@fieldses.org Cc: linux-nfs@vger.kernel.org, Gil Amsalem , Tigran Mkrtchyan Subject: [PATCH] Reduce the probability to get port collision when asking for secure port. port collision = same port allocated to 2 different clients trying to connect to the same address. Date: Thu, 19 May 2016 17:30:17 +0200 Message-Id: <1463671819-4933-2-git-send-email-tigran.mkrtchyan@desy.de> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1463671819-4933-1-git-send-email-tigran.mkrtchyan@desy.de> References: <1463671819-4933-1-git-send-email-tigran.mkrtchyan@desy.de> Sender: linux-nfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Gil Amsalem Signed-off-by: Tigran Mkrtchyan --- rpc/rpc.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/rpc/rpc.py b/rpc/rpc.py index 1a3ca38..8d88c62 100644 --- a/rpc/rpc.py +++ b/rpc/rpc.py @@ -824,23 +824,19 @@ class ConnectionHandler(object): defer.wait() return pipe - def bindsocket(self, s, port=1): + def bindsocket(self, s, start_port=1): """Scan up through ports, looking for one we can bind to""" # This is necessary when we need to use a 'secure' port - using = port - while 1: + valid_ports = range(start_port, 1024) + random.shuffle(valid_ports) + for port in valid_ports: try: - s.bind(('', using)) + s.bind(('', port)) return except socket.error, why: - if why[0] == errno.EADDRINUSE: - using += 1 - if port < 1024 <= using: - # If we ask for a secure port, make sure we don't - # silently bind to a non-secure one - raise - else: + if why[0] != errno.EADDRINUSE: raise + raise Exception('failed to find available secure port') def expose(self, address, af, safe=True):