From afb337c6ff67cd49309c3e414995db57f8be5824 Mon Sep 17 00:00:00 2001 From: Theo de Raadt Date: Mon, 29 Jul 1996 05:48:40 +0000 Subject: rresvport() in terms of bind() & bindresvport() --- lib/libc/net/rcmd.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) (limited to 'lib/libc/net/rcmd.c') 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; -- cgit v1.2.3