#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 $";
struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
u_int16_t *portp;
+ u_int16_t port;
int salen;
int i;
}
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);
}
-#endif /* HAVE_BINRESVPORT_AF */
+#endif /* HAVE_BINDRESVPORT_AF */