summaryrefslogtreecommitdiff
path: root/bsd-bindresvport.c
diff options
context:
space:
mode:
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 3ea37ee52..0e91d2658 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
47#define ENDPORT (IPPORT_RESERVED - 1) 47#define ENDPORT (IPPORT_RESERVED - 1)
48#define NPORTS (ENDPORT - STARTPORT + 1) 48#define NPORTS (ENDPORT - STARTPORT + 1)
49 49
50#if 0
51/*
52 * Bind a socket to a privileged IP port
53 */
54int
55bindresvport(sd, sin)
56 int sd;
57 struct sockaddr_in *sin;
58{
59 return bindresvport_af(sd, (struct sockaddr *)sin, AF_INET);
60}
61#endif
62
63/* 50/*
64 * Bind a socket to a privileged IP port 51 * Bind a socket to a privileged IP port
65 */ 52 */
@@ -97,17 +84,20 @@ bindresvport_af(sd, sa, af)
97 sa->sa_family = af; 84 sa->sa_family = af;
98 85
99 if (*portp == 0) 86 if (*portp == 0)
100 *portp = (getpid() % NPORTS) + STARTPORT; 87 *portp = (arc4random() % NPORTS) + STARTPORT;
101 88
102 for(i = 0; i < NPORTS; i++) { 89 for(i = 0; i < NPORTS; i++) {
103 error = bind(sd, sa, salen); 90 error = bind(sd, sa, salen);
104 91
105 if ((error == 0) || ((error < 0) && (errno != EADDRINUSE))) 92 /* Terminate on success */
93 if (error == 0)
94 break;
95
96 /* Terminate on errors, except "address already in use" */
97 if ((error < 0) && ((errno != EADDRINUSE) || (errno != EINVAL)))
106 break; 98 break;
107 99
108 (*portp)++; 100 *portp = (i % NPORTS) + STARTPORT;
109 if (*portp < ENDPORT)
110 *portp = STARTPORT;
111 } 101 }
112 102
113 return (error); 103 return (error);