RFC: InetAddress.lookup() change (Win32 parts untested)
Gary Benson
gbenson@redhat.com
Tue Sep 5 10:48:00 GMT 2006
Hi again,
InetAddress.lookup() returns its result in differently depending
upon how it is called. If the result might be several addresses
it returns an array, but if the result is a single address it
modifies an object it was passed as an argument and returns NULL.
In the latter mode it is invoked like this:
InetAddress result = new InetAddress(null, null);
lookup (hostname, result, false);
return result;
Aside from being slightly odd this approach means that several of
InetAddress's public methods return InetAddress objects when they
should in fact be returning Inet4Address or Inet6Address objects.
The attached patch (which needs to be applied after my patch with
subject "RFC: Untested Win32 InetAddress tweak") changes lookup to
always return an array, and to create address objects using
InetAddress.getByAddress() which makes Inet4Address or Inet6Address
objects accordingly.
The change is to a part of InetAddress.lookup() that is identical
in both Posix and Win32 versions, but as before I don't have the
ability to test the Win32 part of this patch. Could someone please
test it for me?
Cheers,
Gary
-------------- next part --------------
Index: ChangeLog
===================================================================
--- ChangeLog (revision 116678)
+++ ChangeLog (working copy)
@@ -1,3 +1,12 @@
+2006-09-05 Gary Benson <gbenson@redhat.com>
+
+ * java/net/natInetAddressPosix.cc (lookup): Return arrays
+ of one item rather than modifying an object passed as an
+ argument.
+ * java/net/natInetAddressWin32.cc (lookup): Likewise.
+ * java/net/InetAddress.java (getHostName, getByName,
+ getLocalHost): Reflect the above.
+
2006-09-04 Gary Benson <gbenson@redhat.com>
* java/net/natInetAddressWin32.cc (lookup): Remove security
Index: java/net/natInetAddressPosix.cc
===================================================================
--- java/net/natInetAddressPosix.cc (revision 116678)
+++ java/net/natInetAddressPosix.cc (working copy)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2003 Free Software Foundation
+/* Copyright (C) 2003, 2006 Free Software Foundation
This file is part of libgcj.
@@ -181,13 +181,13 @@
if (len == 4)
{
val = chars;
- type = iaddr->family = AF_INET;
+ type = AF_INET;
}
#ifdef HAVE_INET6
else if (len == 16)
{
val = (char *) &chars;
- type = iaddr->family = AF_INET6;
+ type = AF_INET6;
}
#endif /* HAVE_INET6 */
else
@@ -225,21 +225,25 @@
hptr = gethostbyaddr (val, len, type);
#endif /* HAVE_GETHOSTBYADDR_R */
}
- if (hptr != NULL)
- {
- if (!all)
- host = JvNewStringUTF (hptr->h_name);
- }
+
if (hptr == NULL)
{
- if (iaddr != NULL && iaddr->addr != NULL)
- {
- iaddr->hostName = iaddr->getHostAddress();
- return NULL;
- }
- else
- throw new java::net::UnknownHostException(host);
+ if (iaddr == NULL)
+ throw new java::net::UnknownHostException (host);
+
+ JArray<java::net::InetAddress*> *result =
+ java::net::InetAddress::allocArray (1);
+ java::net::InetAddress** iaddrs = elements (result);
+
+ iaddrs[0] = java::net::InetAddress::getByAddress (
+ iaddr->getHostAddress (), iaddr->addr);
+
+ return result;
}
+
+ if (host == NULL)
+ host = JvNewStringUTF (hptr->h_name);
+
int count;
if (all)
{
@@ -249,32 +253,16 @@
}
else
count = 1;
- JArray<java::net::InetAddress*> *result;
- java::net::InetAddress** iaddrs;
- if (all)
- {
- result = java::net::InetAddress::allocArray (count);
- iaddrs = elements (result);
- }
- else
- {
- result = NULL;
- iaddrs = &iaddr;
- }
- for (int i = 0; i < count; i++)
- {
- if (iaddrs[i] == NULL)
- iaddrs[i] = new java::net::InetAddress (NULL, NULL);
- if (iaddrs[i]->hostName == NULL)
- iaddrs[i]->hostName = host;
- if (iaddrs[i]->addr == NULL)
- {
- char *bytes = hptr->h_addr_list[i];
- iaddrs[i]->addr = JvNewByteArray (hptr->h_length);
- iaddrs[i]->family = getFamily (iaddrs[i]->addr);
- memcpy (elements (iaddrs[i]->addr), bytes, hptr->h_length);
- }
+ JArray<java::net::InetAddress*> *result =
+ java::net::InetAddress::allocArray (count);
+ java::net::InetAddress** iaddrs = elements (result);
+
+ for (int i = 0; i < count; i++)
+ {
+ jbyteArray addr = JvNewByteArray (hptr->h_length);
+ memcpy (elements (addr), hptr->h_addr_list[i], hptr->h_length);
+ iaddrs[i] = java::net::InetAddress::getByAddress (host, addr);
}
return result;
}
Index: java/net/natInetAddressWin32.cc
===================================================================
--- java/net/natInetAddressWin32.cc (revision 116678)
+++ java/net/natInetAddressWin32.cc (working copy)
@@ -73,13 +73,13 @@
if (len == 4)
{
val = chars;
- type = iaddr->family = AF_INET;
+ type = AF_INET;
}
#ifdef HAVE_INET6
else if (len == 16)
{
val = (char *) &chars;
- type = iaddr->family = AF_INET6;
+ type = AF_INET6;
}
#endif /* HAVE_INET6 */
else
@@ -90,22 +90,25 @@
JvSynchronize sync (java::net::InetAddress::loopbackAddress);
hptr = gethostbyaddr (val, len, type);
}
- if (hptr != NULL)
- {
- if (!all)
- host = JvNewStringUTF (hptr->h_name);
- }
+
if (hptr == NULL)
{
- if (iaddr != NULL && iaddr->addr != NULL)
- {
- iaddr->hostName = iaddr->getHostAddress();
- return NULL;
- }
- else
- throw new java::net::UnknownHostException(host);
+ if (iaddr == NULL)
+ throw new java::net::UnknownHostException (host);
+
+ JArray<java::net::InetAddress*> *result =
+ java::net::InetAddress::allocArray (1);
+ java::net::InetAddress** iaddrs = elements (result);
+
+ iaddrs[0] = java::net::InetAddress::getByAddress (
+ iaddr->getHostAddress (), iaddr->addr);
+
+ return result;
}
+ if (host == NULL)
+ host = JvNewStringUTF (hptr->h_name);
+
int count;
if (all)
{
@@ -116,34 +119,16 @@
else
count = 1;
- JArray<java::net::InetAddress*> *result;
- java::net::InetAddress** iaddrs;
- if (all)
- {
- result = java::net::InetAddress::allocArray (count);
- iaddrs = elements (result);
- }
- else
- {
- result = NULL;
- iaddrs = &iaddr;
- }
+ JArray<java::net::InetAddress*> *result =
+ java::net::InetAddress::allocArray (count);
+ java::net::InetAddress** iaddrs = elements (result);
- for (int i = 0; i < count; i++)
+ for (int i = 0; i < count; i++)
{
- if (iaddrs[i] == NULL)
- iaddrs[i] = new java::net::InetAddress (NULL, NULL);
- if (iaddrs[i]->hostName == NULL)
- iaddrs[i]->hostName = host;
- if (iaddrs[i]->addr == NULL)
- {
- char *bytes = hptr->h_addr_list[i];
- iaddrs[i]->addr = JvNewByteArray (hptr->h_length);
- iaddrs[i]->family = getFamily (iaddrs[i]->addr);
- memcpy (elements (iaddrs[i]->addr), bytes, hptr->h_length);
- }
+ jbyteArray addr = JvNewByteArray (hptr->h_length);
+ memcpy (elements (addr), hptr->h_addr_list[i], hptr->h_length);
+ iaddrs[i] = java::net::InetAddress::getByAddress (host, addr);
}
-
return result;
}
Index: java/net/InetAddress.java
===================================================================
--- java/net/InetAddress.java (revision 116678)
+++ java/net/InetAddress.java (working copy)
@@ -294,12 +294,9 @@
*/
public String getHostName()
{
- if (hostName != null)
- return hostName;
+ if (hostName == null)
+ hostName = lookup (null, this, false)[0].getHostName();
- // Lookup hostname and set field.
- lookup (null, this, false);
-
return hostName;
}
@@ -602,9 +599,7 @@
s.checkConnect(hostname, -1);
// Try to resolve the host by DNS
- InetAddress result = new InetAddress(null, null);
- lookup (hostname, result, false);
- return result;
+ return lookup (hostname, null, false)[0];
}
/**
@@ -706,8 +701,7 @@
s.checkConnect (hostname, -1);
}
- localhost = new InetAddress (null, null);
- lookup (hostname, localhost, false);
+ localhost = lookup (hostname, null, false)[0];
}
catch (Exception ex)
{
More information about the Java-patches
mailing list