From e193bab90b075c24942aeac8fd10132868c9ee76 Mon Sep 17 00:00:00 2001 From: deraadt <> Date: Mon, 29 Jul 1996 05:48:39 +0000 Subject: rresvport() in terms of bind() & bindresvport() --- src/lib/libc/net/rcmd.3 | 3 +++ src/lib/libc/net/rcmd.c | 26 ++++++++++++-------------- 2 files changed, 15 insertions(+), 14 deletions(-) (limited to 'src/lib') diff --git a/src/lib/libc/net/rcmd.3 b/src/lib/libc/net/rcmd.3 index 8452e395db..add4faa314 100644 --- a/src/lib/libc/net/rcmd.3 +++ b/src/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/src/lib/libc/net/rcmd.c b/src/lib/libc/net/rcmd.c index 22a956cfb0..28420d20eb 100644 --- a/src/lib/libc/net/rcmd.c +++ b/src/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; -- cgit v1.2.3-55-g6feb