- Clean up bsd-bindresvport.c. Use arc4random() for picking initial

port, ignore EINVAL errors (Linux) when searching for free port.
This commit is contained in:
Damien Miller 2000-01-17 09:52:46 +11:00
parent 62a52ef08d
commit b9b94a74e6
2 changed files with 13 additions and 19 deletions

View File

@ -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

View File

@ -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);