summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTheo de Raadt <deraadt@cvs.openbsd.org>1996-07-29 05:48:40 +0000
committerTheo de Raadt <deraadt@cvs.openbsd.org>1996-07-29 05:48:40 +0000
commitafb337c6ff67cd49309c3e414995db57f8be5824 (patch)
tree69102e6b39b8d4273f26e0050c484558307a3781
parent089f0b9d36e1645042d7d82329d7e14ac207491f (diff)
rresvport() in terms of bind() & bindresvport()
-rw-r--r--lib/libc/net/rcmd.33
-rw-r--r--lib/libc/net/rcmd.c26
2 files changed, 15 insertions, 14 deletions
diff --git a/lib/libc/net/rcmd.3 b/lib/libc/net/rcmd.3
index 8452e395dba..add4faa3141 100644
--- a/lib/libc/net/rcmd.3
+++ b/lib/libc/net/rcmd.3
@@ -134,6 +134,9 @@ by
and several other functions. Privileged Internet ports are those
in the range 0 to 1023. Only the super-user
is allowed to bind an address of this sort to a socket.
+.Fn rresvport
+needs to be seeded with a port number; if that port
+is not available it will find another.
.Pp
The
.Fn iruserok
diff --git a/lib/libc/net/rcmd.c b/lib/libc/net/rcmd.c
index 22a956cfb05..28420d20ebe 100644
--- a/lib/libc/net/rcmd.c
+++ b/lib/libc/net/rcmd.c
@@ -228,21 +228,19 @@ rresvport(alport)
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0)
return (-1);
- for (;;) {
- sin.sin_port = htons((u_short)*alport);
- if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
- return (s);
- if (errno != EADDRINUSE) {
- (void)close(s);
- return (-1);
- }
- (*alport)--;
- if (*alport == IPPORT_RESERVED/2) {
- (void)close(s);
- errno = EAGAIN; /* close */
- return (-1);
- }
+ sin.sin_port = htons((u_short)*alport);
+ if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
+ return (s);
+ if (errno != EADDRINUSE) {
+ (void)close(s);
+ return (-1);
}
+ if (bindresvport(s, &sin) == -1) {
+ (void)close(s);
+ return (-1);
+ }
+ *alport = (int)ntohs(sin.sin_port);
+ return (s);
}
int __check_rhosts_file = 1;