natInetAddress.cc (aton): Fix typos.

* java/net/natInetAddress.cc (aton): Fix typos.
        (lookup): Use a bigger buffer size for gethostbyname_r on all
        versions of glibc. Updated FIXME comment explaining this.
        Modified while loops to not set herr = ERANGE to work around glibc
        problems. Use user specified hostname in InetAddress result when
        available (consistent with JDK).

From-SVN: r27484
This commit is contained in:
Bryce McKinlay 1999-06-11 01:38:08 +00:00 committed by Bryce McKinlay
parent b312276f5b
commit 53dfe2970a
2 changed files with 31 additions and 18 deletions

View file

@ -1,3 +1,11 @@
1999-06-10 Bryce McKinlay <bryce@albatross.co.nz>
* java/net/natInetAddress.cc (aton): Fix typos.
(lookup): Use a bigger buffer size for gethostbyname_r on all
versions of glibc. Updated FIXME comment explaining this.
Modified while loops to not set herr = ERANGE to work around glibc
problems. Use user specified hostname in InetAddress result when
available (consistent with JDK).
1999-06-10 Warren Levy <warrenl@cygnus.com> 1999-06-10 Warren Levy <warrenl@cygnus.com>
* java/io/FileDescriptor.java (FileDescriptor(String, int)): * java/io/FileDescriptor.java (FileDescriptor(String, int)):

View file

@ -64,14 +64,14 @@ java::net::InetAddress::aton (jstring host)
if (inet_aton (hostname, &laddr)) if (inet_aton (hostname, &laddr))
{ {
bytes = (char*) &laddr; bytes = (char*) &laddr;
len = 4; blen = 4;
} }
#elif defined(HAVE_INET_ADDR) #elif defined(HAVE_INET_ADDR)
in_addr_t laddr = inet_addr (hostname); in_addr_t laddr = inet_addr (hostname);
if (laddr != (in_addr_t)(-1)) if (laddr != (in_addr_t)(-1))
{ {
bytes = (char*) &laddr; bytes = (char*) &laddr;
len = 4; blen = 4;
} }
#endif #endif
#ifdef HAVE_INET_PTON #ifdef HAVE_INET_PTON
@ -79,12 +79,12 @@ java::net::InetAddress::aton (jstring host)
if (len == 0 && inet_pton (AF_INET6, hostname, inet6_addr) > 0) if (len == 0 && inet_pton (AF_INET6, hostname, inet6_addr) > 0)
{ {
bytes = inet6_addr; bytes = inet6_addr;
len = 16; blen = 16;
} }
#endif #endif
if (blen == 0) if (blen == 0)
return NULL; return NULL;
jbyteArray result = JvNewByteArray (len); jbyteArray result = JvNewByteArray (blen);
memcpy (elements (result), bytes, blen); memcpy (elements (result), bytes, blen);
return result; return result;
} }
@ -97,10 +97,11 @@ java::net::InetAddress::lookup (jstring host, java::net::InetAddress* iaddr,
struct hostent *hptr = NULL; struct hostent *hptr = NULL;
#if defined (HAVE_GETHOSTBYNAME_R) || defined (HAVE_GETHOSTBYADDR_R) #if defined (HAVE_GETHOSTBYNAME_R) || defined (HAVE_GETHOSTBYADDR_R)
struct hostent hent_r; struct hostent hent_r;
#if defined (__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ == 0 #if defined (__GLIBC__)
// glibc 2.0.7 has a bug where gethostbyname_r won't return an error // FIXME: in glibc, gethostbyname_r returns NETDB_INTERNAL to herr and
// if the buffer is too small. So in this case we size the buffer // ERANGE to errno if the buffer size is too small, rather than what is
// the same way that glibc does. This is fixed in glibc 2.1. // expected here. We work around this by setting a bigger buffer size and
// hoping that it is big enough.
char fixed_buffer[1024]; char fixed_buffer[1024];
#else #else
char fixed_buffer[200]; char fixed_buffer[200];
@ -121,8 +122,8 @@ java::net::InetAddress::lookup (jstring host, java::net::InetAddress* iaddr,
JvGetStringUTFRegion (host, 0, host->length(), hostname); JvGetStringUTFRegion (host, 0, host->length(), hostname);
buf[len] = '\0'; buf[len] = '\0';
#ifdef HAVE_GETHOSTBYNAME_R #ifdef HAVE_GETHOSTBYNAME_R
int herr = ERANGE; int herr = 0;
while (hptr == NULL && herr == ERANGE) while (true)
{ {
int ok; int ok;
#ifdef GETHOSTBYNAME_R_RETURNS_INT #ifdef GETHOSTBYNAME_R_RETURNS_INT
@ -137,6 +138,8 @@ java::net::InetAddress::lookup (jstring host, java::net::InetAddress* iaddr,
size_r *= 2; size_r *= 2;
buffer_r = (char *) _Jv_AllocBytesChecked (size_r); buffer_r = (char *) _Jv_AllocBytesChecked (size_r);
} }
else
break;
} }
#else #else
// FIXME: this is insufficient if some other piece of code calls // FIXME: this is insufficient if some other piece of code calls
@ -168,8 +171,8 @@ java::net::InetAddress::lookup (jstring host, java::net::InetAddress* iaddr,
JvFail ("unrecognized size"); JvFail ("unrecognized size");
#ifdef HAVE_GETHOSTBYADDR_R #ifdef HAVE_GETHOSTBYADDR_R
int herr = ERANGE; int herr = 0;
while (hptr == NULL && herr == ERANGE) while (true)
{ {
int ok; int ok;
#ifdef GETHOSTBYADDR_R_RETURNS_INT #ifdef GETHOSTBYADDR_R_RETURNS_INT
@ -185,6 +188,8 @@ java::net::InetAddress::lookup (jstring host, java::net::InetAddress* iaddr,
size_r *= 2; size_r *= 2;
buffer_r = (char *) _Jv_AllocBytesChecked (size_r); buffer_r = (char *) _Jv_AllocBytesChecked (size_r);
} }
else
break;
} }
#else /* HAVE_GETHOSTBYADDR_R */ #else /* HAVE_GETHOSTBYADDR_R */
// FIXME: this is insufficient if some other piece of code calls // FIXME: this is insufficient if some other piece of code calls
@ -194,8 +199,9 @@ java::net::InetAddress::lookup (jstring host, java::net::InetAddress* iaddr,
#endif /* HAVE_GETHOSTBYADDR_R */ #endif /* HAVE_GETHOSTBYADDR_R */
} }
if (hptr != NULL) if (hptr != NULL)
{ {
host = JvNewStringUTF (hptr->h_name); if (host == NULL)
host = JvNewStringUTF (hptr->h_name);
java::lang::SecurityException *ex = checkConnect (host); java::lang::SecurityException *ex = checkConnect (host);
if (ex != NULL) if (ex != NULL)
{ {
@ -240,13 +246,12 @@ java::net::InetAddress::lookup (jstring host, java::net::InetAddress* iaddr,
{ {
if (iaddrs[i] == NULL) if (iaddrs[i] == NULL)
iaddrs[i] = new java::net::InetAddress (NULL, NULL); iaddrs[i] = new java::net::InetAddress (NULL, NULL);
if (i == 0) iaddrs[i]->hostname = host;
iaddrs[0]->hostname = host;
if (iaddrs[i]->address == NULL) if (iaddrs[i]->address == NULL)
{ {
char *bytes = hptr->h_addr_list[i]; char *bytes = hptr->h_addr_list[i];
iaddr->address = JvNewByteArray (hptr->h_length); iaddrs[i]->address = JvNewByteArray (hptr->h_length);
memcpy (elements (iaddr->address), bytes, hptr->h_length); memcpy (elements (iaddrs[i]->address), bytes, hptr->h_length);
} }
} }
return result; return result;