X-Git-Url: http://andersk.mit.edu/gitweb/openssh.git/blobdiff_plain/f914c7fb22b265b9795a76a5be9119a0d08dda2f..3dc1102ec55da4c4f412db1224270bd3e60962cc:/bsd-bindresvport.c diff --git a/bsd-bindresvport.c b/bsd-bindresvport.c index 0e91d265..15bb667d 100644 --- a/bsd-bindresvport.c +++ b/bsd-bindresvport.c @@ -61,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; @@ -83,21 +84,26 @@ bindresvport_af(sd, sa, af) } sa->sa_family = af; - if (*portp == 0) - *portp = (arc4random() % 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); - + /* Terminate on success */ if (error == 0) break; /* Terminate on errors, except "address already in use" */ - if ((error < 0) && ((errno != EADDRINUSE) || (errno != EINVAL))) + if ((error < 0) && !((errno == EADDRINUSE) || (errno == EINVAL))) break; - *portp = (i % NPORTS) + STARTPORT; + port++; + if (port > ENDPORT) + port = STARTPORT; } return (error);