From 53dfe2970af0655b568d1986d3fb36fed93c0e6c Mon Sep 17 00:00:00 2001 From: Bryce McKinlay Date: Fri, 11 Jun 1999 01:38:08 +0000 Subject: [PATCH] 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 --- libjava/ChangeLog | 8 ++++++ libjava/java/net/natInetAddress.cc | 41 +++++++++++++++++------------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/libjava/ChangeLog b/libjava/ChangeLog index b7b295df091..8a2eb837a8d 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,11 @@ +1999-06-10 Bryce McKinlay + * 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 * java/io/FileDescriptor.java (FileDescriptor(String, int)): diff --git a/libjava/java/net/natInetAddress.cc b/libjava/java/net/natInetAddress.cc index da3a1fbddf6..8da9102815a 100644 --- a/libjava/java/net/natInetAddress.cc +++ b/libjava/java/net/natInetAddress.cc @@ -64,14 +64,14 @@ java::net::InetAddress::aton (jstring host) if (inet_aton (hostname, &laddr)) { bytes = (char*) &laddr; - len = 4; + blen = 4; } #elif defined(HAVE_INET_ADDR) in_addr_t laddr = inet_addr (hostname); if (laddr != (in_addr_t)(-1)) { bytes = (char*) &laddr; - len = 4; + blen = 4; } #endif #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) { bytes = inet6_addr; - len = 16; + blen = 16; } #endif if (blen == 0) return NULL; - jbyteArray result = JvNewByteArray (len); + jbyteArray result = JvNewByteArray (blen); memcpy (elements (result), bytes, blen); return result; } @@ -97,10 +97,11 @@ java::net::InetAddress::lookup (jstring host, java::net::InetAddress* iaddr, struct hostent *hptr = NULL; #if defined (HAVE_GETHOSTBYNAME_R) || defined (HAVE_GETHOSTBYADDR_R) struct hostent hent_r; -#if defined (__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ == 0 - // glibc 2.0.7 has a bug where gethostbyname_r won't return an error - // if the buffer is too small. So in this case we size the buffer - // the same way that glibc does. This is fixed in glibc 2.1. +#if defined (__GLIBC__) + // FIXME: in glibc, gethostbyname_r returns NETDB_INTERNAL to herr and + // ERANGE to errno if the buffer size is too small, rather than what is + // expected here. We work around this by setting a bigger buffer size and + // hoping that it is big enough. char fixed_buffer[1024]; #else char fixed_buffer[200]; @@ -121,8 +122,8 @@ java::net::InetAddress::lookup (jstring host, java::net::InetAddress* iaddr, JvGetStringUTFRegion (host, 0, host->length(), hostname); buf[len] = '\0'; #ifdef HAVE_GETHOSTBYNAME_R - int herr = ERANGE; - while (hptr == NULL && herr == ERANGE) + int herr = 0; + while (true) { int ok; #ifdef GETHOSTBYNAME_R_RETURNS_INT @@ -137,6 +138,8 @@ java::net::InetAddress::lookup (jstring host, java::net::InetAddress* iaddr, size_r *= 2; buffer_r = (char *) _Jv_AllocBytesChecked (size_r); } + else + break; } #else // 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"); #ifdef HAVE_GETHOSTBYADDR_R - int herr = ERANGE; - while (hptr == NULL && herr == ERANGE) + int herr = 0; + while (true) { int ok; #ifdef GETHOSTBYADDR_R_RETURNS_INT @@ -185,6 +188,8 @@ java::net::InetAddress::lookup (jstring host, java::net::InetAddress* iaddr, size_r *= 2; buffer_r = (char *) _Jv_AllocBytesChecked (size_r); } + else + break; } #else /* HAVE_GETHOSTBYADDR_R */ // 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 */ } if (hptr != NULL) - { - host = JvNewStringUTF (hptr->h_name); + { + if (host == NULL) + host = JvNewStringUTF (hptr->h_name); java::lang::SecurityException *ex = checkConnect (host); if (ex != NULL) { @@ -240,13 +246,12 @@ java::net::InetAddress::lookup (jstring host, java::net::InetAddress* iaddr, { if (iaddrs[i] == NULL) iaddrs[i] = new java::net::InetAddress (NULL, NULL); - if (i == 0) - iaddrs[0]->hostname = host; + iaddrs[i]->hostname = host; if (iaddrs[i]->address == NULL) { char *bytes = hptr->h_addr_list[i]; - iaddr->address = JvNewByteArray (hptr->h_length); - memcpy (elements (iaddr->address), bytes, hptr->h_length); + iaddrs[i]->address = JvNewByteArray (hptr->h_length); + memcpy (elements (iaddrs[i]->address), bytes, hptr->h_length); } } return result;