summaryrefslogtreecommitdiffstats
path: root/bsd-bindresvport.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>2000-01-17 09:52:46 +1100
committerDamien Miller <djm@mindrot.org>2000-01-17 09:52:46 +1100
commitb9b94a74e6770fdf4aaae47ac82d9a7dcf20611b (patch)
treefd621a2a90eb198383d964c133379fa291757630 /bsd-bindresvport.c
parent62a52ef08da1f8371132278cb4025eccf56671a2 (diff)
- Clean up bsd-bindresvport.c. Use arc4random() for picking initial
port, ignore EINVAL errors (Linux) when searching for free port.
Diffstat (limited to 'bsd-bindresvport.c')
-rw-r--r--bsd-bindresvport.c28
1 files changed, 9 insertions, 19 deletions
diff --git a/bsd-bindresvport.c b/bsd-bindresvport.c
index 3ea37ee5..0e91d265 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;
+
+ /* Terminate on errors, except "address already in use" */
+ if ((error < 0) && ((errno != EADDRINUSE) || (errno != EINVAL)))
break;
- (*portp)++;
- if (*portp < ENDPORT)
- *portp = STARTPORT;
+ *portp = (i % NPORTS) + STARTPORT;
}
return (error);