summaryrefslogtreecommitdiff
path: root/sunrpc/bindrsvprt.c
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2005-05-23 17:53:19 +0000
committerJakub Jelinek <jakub@redhat.com>2005-05-23 17:53:19 +0000
commit7cfcf20b10c97faaf764f5c7784c4f2fa7922c5f (patch)
tree149a19f5f50b1433df714885fd986cdaefbee919 /sunrpc/bindrsvprt.c
parente975f7ef2955c34c381d84b2494b47018c3461ab (diff)
2005-05-23 Ulrich Drepper <drepper@redhat.com>
* sunrpc/pm_getport.c (__get_socket): New function. (pmap_getport): Use it to open a non-reserved socket to the portmapper for TCP. * include/rpc/pmap_clnt.h (__get_socket): Declare. * sunrpc/pm_getmaps.c (pmap_getmaps): Use __get_socket to get an non-reserved socket for the portmapper. Based on a patch by Steve Dickson <steved@redhat.com>. * sunrpc/bindrsvprt.c (bindresvport): Try harder to find a port. If we tried looking at the usual range without success extend the range to even lower ports.
Diffstat (limited to 'sunrpc/bindrsvprt.c')
-rw-r--r--sunrpc/bindrsvprt.c23
1 files changed, 18 insertions, 5 deletions
diff --git a/sunrpc/bindrsvprt.c b/sunrpc/bindrsvprt.c
index 374518716e..39c1e8063d 100644
--- a/sunrpc/bindrsvprt.c
+++ b/sunrpc/bindrsvprt.c
@@ -43,14 +43,15 @@
int
bindresvport (int sd, struct sockaddr_in *sin)
{
- int res;
static short port;
struct sockaddr_in myaddr;
int i;
#define STARTPORT 600
+#define LOWPORT 200
#define ENDPORT (IPPORT_RESERVED - 1)
#define NPORTS (ENDPORT - STARTPORT + 1)
+ static short startport = STARTPORT;
if (sin == (struct sockaddr_in *) 0)
{
@@ -68,17 +69,29 @@ bindresvport (int sd, struct sockaddr_in *sin)
{
port = (__getpid () % NPORTS) + STARTPORT;
}
- res = -1;
- __set_errno (EADDRINUSE);
- for (i = 0; i < NPORTS && res < 0 && errno == EADDRINUSE; ++i)
+ /* Initialize to make gcc happy. */
+ int res = -1;
+
+ int nports = ENDPORT - startport + 1;
+ again:
+ for (i = 0; i < nports; ++i)
{
sin->sin_port = htons (port++);
if (port > ENDPORT)
{
- port = STARTPORT;
+ port = startport;
}
res = __bind (sd, sin, sizeof (struct sockaddr_in));
+ if (res >= 0 || errno != EADDRINUSE)
+ break;
+ }
+
+ if (i == nports && startport != LOWPORT)
+ {
+ startport = LOWPORT;
+ nports = STARTPORT - LOWPORT;
+ goto again;
}
return res;