about summary refs log tree commit diff
path: root/sunrpc/bindrsvprt.c
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2007-07-12 18:26:36 +0000
committerJakub Jelinek <jakub@redhat.com>2007-07-12 18:26:36 +0000
commit0ecb606cb6cf65de1d9fc8a919bceb4be476c602 (patch)
tree2ea1f8305970753e4a657acb2ccc15ca3eec8e2c /sunrpc/bindrsvprt.c
parent7d58530341304d403a6626d7f7a1913165fe2f32 (diff)
downloadglibc-0ecb606cb6cf65de1d9fc8a919bceb4be476c602.tar.gz
glibc-0ecb606cb6cf65de1d9fc8a919bceb4be476c602.tar.xz
glibc-0ecb606cb6cf65de1d9fc8a919bceb4be476c602.zip
2.5-18.1
Diffstat (limited to 'sunrpc/bindrsvprt.c')
-rw-r--r--sunrpc/bindrsvprt.c30
1 files changed, 22 insertions, 8 deletions
diff --git a/sunrpc/bindrsvprt.c b/sunrpc/bindrsvprt.c
index 374518716e..023ae1723b 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 512
 #define ENDPORT (IPPORT_RESERVED - 1)
 #define NPORTS	(ENDPORT - STARTPORT + 1)
+  static short startport = STARTPORT;
 
   if (sin == (struct sockaddr_in *) 0)
     {
@@ -68,17 +69,30 @@ 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;
+  int endport = ENDPORT;
+ again:
+  for (i = 0; i < nports; ++i)
     {
       sin->sin_port = htons (port++);
-      if (port > ENDPORT)
-	{
-	  port = STARTPORT;
-	}
+      if (port > endport)
+	port = startport;
       res = __bind (sd, sin, sizeof (struct sockaddr_in));
+      if (res >= 0 || errno != EADDRINUSE)
+	break;
+    }
+
+  if (i == nports && startport != LOWPORT)
+    {
+      startport = LOWPORT;
+      endport = STARTPORT - 1;
+      nports = STARTPORT - LOWPORT;
+      port = LOWPORT + port % (STARTPORT - LOWPORT);
+      goto again;
     }
 
   return res;