]> andersk Git - openssh.git/blobdiff - bsd-bindresvport.c
Added in Linux's select issue to TODO. Which may have to do with the
[openssh.git] / bsd-bindresvport.c
index 3ea37ee5256b14ac365438dbaf20f7628a875c42..fb3f6f2b85767a7a61faa71c0c170dc1356ffe46 100644 (file)
@@ -29,7 +29,7 @@
 
 #include "config.h"
 
-#ifndef HAVE_BINRESVPORT_AF
+#ifndef HAVE_BINDRESVPORT_AF
 
 #if defined(LIBC_SCCS) && !defined(lint)
 static char *rcsid = "$OpenBSD: bindresvport.c,v 1.11 1999/12/17 19:22:08 deraadt Exp $";
@@ -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
  */
@@ -74,6 +61,7 @@ bindresvport_af(sd, sa, af)
        struct sockaddr_in *sin;
        struct sockaddr_in6 *sin6;
        u_int16_t *portp;
+       u_int16_t port;
        int salen;
        int i;
 
@@ -96,21 +84,29 @@ bindresvport_af(sd, sa, af)
        }
        sa->sa_family = af;
 
-       if (*portp == 0)
-               *portp = (getpid() % NPORTS) + STARTPORT;
+       port = ntohs(*portp);
+       if (port == 0)
+               port = (arc4random() % NPORTS) + STARTPORT;
 
        for(i = 0; i < NPORTS; i++) {
+               *portp = htons(port);
+               
                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;
+               port++;
+               if (port > ENDPORT)
+                       port = STARTPORT;
        }
 
        return (error);
 }
 
-#endif /* HAVE_BINRESVPORT_AF */
+#endif /* HAVE_BINDRESVPORT_AF */
This page took 0.045679 seconds and 4 git commands to generate.