]> andersk Git - openssh.git/blobdiff - bsd-bindresvport.c
- Big manpage and config file cleanup from Andre Lucas
[openssh.git] / bsd-bindresvport.c
index 3ea37ee5256b14ac365438dbaf20f7628a875c42..04780673b7087b38ee77859d939817c5fe414f89 100644 (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 = (u_int16_t)(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);
This page took 0.199631 seconds and 4 git commands to generate.