From b9b94a74e6770fdf4aaae47ac82d9a7dcf20611b Mon Sep 17 00:00:00 2001 From: Damien Miller Date: Mon, 17 Jan 2000 09:52:46 +1100 Subject: [PATCH] - Clean up bsd-bindresvport.c. Use arc4random() for picking initial port, ignore EINVAL errors (Linux) when searching for free port. --- ChangeLog | 4 ++++ bsd-bindresvport.c | 28 +++++++++------------------- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/ChangeLog b/ChangeLog index 89a3e7c28..8e23821e1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +20000117 + - Clean up bsd-bindresvport.c. Use arc4random() for picking initial + port, ignore EINVAL errors (Linux) when searching for free port. + 20000116 - Renamed --with-xauth-path to --with-xauth - Added --with-pid-dir option diff --git a/bsd-bindresvport.c b/bsd-bindresvport.c index 3ea37ee52..0e91d2658 100644 --- a/bsd-bindresvport.c +++ b/bsd-bindresvport.c @@ -47,19 +47,6 @@ static char *rcsid = "$OpenBSD: bindresvport.c,v 1.11 1999/12/17 19:22:08 deraad #define ENDPORT (IPPORT_RESERVED - 1) #define NPORTS (ENDPORT - STARTPORT + 1) -#if 0 -/* - * Bind a socket to a privileged IP port - */ -int -bindresvport(sd, sin) - int sd; - struct sockaddr_in *sin; -{ - return bindresvport_af(sd, (struct sockaddr *)sin, AF_INET); -} -#endif - /* * Bind a socket to a privileged IP port */ @@ -97,17 +84,20 @@ bindresvport_af(sd, sa, af) sa->sa_family = af; if (*portp == 0) - *portp = (getpid() % NPORTS) + STARTPORT; + *portp = (arc4random() % NPORTS) + STARTPORT; for(i = 0; i < NPORTS; i++) { error = bind(sd, sa, salen); - - if ((error == 0) || ((error < 0) && (errno != EADDRINUSE))) + + /* Terminate on success */ + if (error == 0) break; - (*portp)++; - if (*portp < ENDPORT) - *portp = STARTPORT; + /* Terminate on errors, except "address already in use" */ + if ((error < 0) && ((errno != EADDRINUSE) || (errno != EINVAL))) + break; + + *portp = (i % NPORTS) + STARTPORT; } return (error);