From owner-freebsd-net@FreeBSD.ORG Wed Feb 27 21:14:58 2008 Return-Path: Delivered-To: freebsd-net@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 9045E1065670 for ; Wed, 27 Feb 2008 21:14:58 +0000 (UTC) (envelope-from rpaulo@gmail.com) Received: from ag-out-0708.google.com (ag-out-0708.google.com [72.14.246.248]) by mx1.freebsd.org (Postfix) with ESMTP id 4E5D58FC2C for ; Wed, 27 Feb 2008 21:14:58 +0000 (UTC) (envelope-from rpaulo@gmail.com) Received: by ag-out-0708.google.com with SMTP id 5so8040491agb.7 for ; Wed, 27 Feb 2008 13:14:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:sender:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references:x-google-sender-auth; bh=jNMFaCHW4r1ws3PPY8VO8jGAs9Su3CcAMnMj39/18U0=; b=InAVBvibldoJyjRiBHhSZ+Btvf3NhuGLamBOzAwztTSsN7rUl4AsyqRea8scI7ojxSPit67ip0H22GWE34hNzoV6dHqEEpXYuNKzpZMUEC1rYyPphH/ygxWhgU4sVGk18MhCtRk0KLwhRH3col11wQtih8PVKXfVpQizEIkrP58= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:sender:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references:x-google-sender-auth; b=OWEG60d1icpbrISamuRQIImn3kTtqrxwq8s9GO0ALoD6mpsO/VSwTOBhvoUwqOxCozY5KgaslGp/GW1RHKJE/UFfzGf06+PBF6lQ4xJ/2Ev5LpPfeh5wJQZnLPmIfgsoVXSY5nn+G5Cr3arL6Dbbvy46uF9zPYnAc63wmTezalY= Received: by 10.100.123.4 with SMTP id v4mr13131451anc.4.1204146897115; Wed, 27 Feb 2008 13:14:57 -0800 (PST) Received: by 10.100.153.10 with HTTP; Wed, 27 Feb 2008 13:14:56 -0800 (PST) Message-ID: Date: Wed, 27 Feb 2008 21:14:56 +0000 From: "Rui Paulo" Sender: rpaulo@gmail.com To: "Fernando Gont" In-Reply-To: <200802250817.m1P8HoPb024302@venus.xmundo.net> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <200802250817.m1P8HoPb024302@venus.xmundo.net> X-Google-Sender-Auth: 761c1aba43dc9ef4 Cc: freebsd-net@freebsd.org Subject: Re: Ephemeral port selection (patch) X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Networking and TCP/IP with FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 27 Feb 2008 21:14:58 -0000 On Mon, Feb 25, 2008 at 8:16 AM, Fernando Gont wrote: > Folks, > > This patch simply eliminates duplicated code in the in_pcb_bind() function. > > Index: in_pcb.c > =================================================================== > RCS file: /home/ncvs/src/sys/netinet/in_pcb.c,v > retrieving revision 1.198 > diff -u -r1.198 in_pcb.c > --- in_pcb.c 22 Dec 2007 10:06:11 -0000 1.198 > +++ in_pcb.c 25 Feb 2008 06:10:04 -0000 > @@ -393,7 +393,7 @@ > if (*lportp != 0) > lport = *lportp; > if (lport == 0) { > - u_short first, last; > + u_short first, last, aux; > int count; > > if (laddr.s_addr != INADDR_ANY) > @@ -440,47 +440,28 @@ > /* > * Simple check to ensure all ports are not used up causing > * a deadlock here. > - * > - * We split the two cases (up and down) so that the direction > - * is not being tested on each round of the loop. > */ > if (first > last) { > - /* > - * counting down > - */ > - if (dorandom) > - *lastport = first - > - (arc4random() % (first - last)); > - count = first - last; > - > - do { > - if (count-- < 0) /* completely used? */ > - return (EADDRNOTAVAIL); > - --*lastport; > - if (*lastport > first || *lastport < last) > - *lastport = first; > - lport = htons(*lastport); > - } while (in_pcblookup_local(pcbinfo, laddr, lport, > - wild)); > - } else { > - /* > - * counting up > - */ > - if (dorandom) > - *lastport = first + > - (arc4random() % (last - first)); > - count = last - first; > - > - do { > - if (count-- < 0) /* completely used? */ > - return (EADDRNOTAVAIL); > - ++*lastport; > - if (*lastport < first || *lastport > last) > - *lastport = first; > - lport = htons(*lastport); > - } while (in_pcblookup_local(pcbinfo, laddr, lport, > - wild)); > + aux = first; > + first = last; > + last = aux; > } > + > + if (dorandom) > + *lastport = first + > + (arc4random() % (last - first)); > + > + count = last - first; > + > + do { > + if (count-- < 0) /* completely used? */ > + return (EADDRNOTAVAIL); > + ++*lastport; > + if (*lastport < first || *lastport > last) > + *lastport = first; > + lport = htons(*lastport); > + } while (in_pcblookup_local(pcbinfo, laddr, lport, > + wild)); > } > if (prison_ip(cred, 0, &laddr.s_addr)) > return (EINVAL); > > > -- > Fernando Gont > e-mail: fernando@gont.com.ar || fgont@acm.org > PGP Fingerprint: 7809 84F5 322E 45C7 F1C9 3945 96EE A9EF D076 FFF1 > > Yeah, you're right. I'll try to commit this. Regards. -- Rui Paulo