Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 27 Feb 2008 21:14:56 +0000
From:      "Rui Paulo" <rpaulo@fnop.net>
To:        "Fernando Gont" <fernando@gont.com.ar>
Cc:        freebsd-net@freebsd.org
Subject:   Re: Ephemeral port selection (patch)
Message-ID:  <e1309ba60802271314v1a11c86bw3d04d98a0f166a1@mail.gmail.com>
In-Reply-To: <200802250817.m1P8HoPb024302@venus.xmundo.net>
References:  <200802250817.m1P8HoPb024302@venus.xmundo.net>

next in thread | previous in thread | raw e-mail | index | archive | help
On Mon, Feb 25, 2008 at 8:16 AM, Fernando Gont <fernando@gont.com.ar> 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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?e1309ba60802271314v1a11c86bw3d04d98a0f166a1>