* FYI: InetAddress refactoring
@ 2006-09-01 13:35 Gary Benson
0 siblings, 0 replies; only message in thread
From: Gary Benson @ 2006-09-01 13:35 UTC (permalink / raw)
To: java-patches
[-- Attachment #1: Type: text/plain, Size: 311 bytes --]
Hi again,
This commit adds some logic to InetAddress.getByAddress() to make it
create an Inet4Address instead of Inet6Address when the byte array
it is passed represents an IPv4-mapped IPv6 address. It also makes
getByName() and getAllByName() defer to getByAddress() to take
advantage of this.
Cheers,
Gary
[-- Attachment #2: patch --]
[-- Type: text/plain, Size: 2570 bytes --]
Index: ChangeLog
===================================================================
--- ChangeLog (revision 116621)
+++ ChangeLog (working copy)
@@ -1,3 +1,10 @@
+2006-09-01 Gary Benson <gbenson@redhat.com>
+
+ * java/net/InetAddress.java (getByAddress): Create Inet4Address
+ objects when passed IPv4-mapped IPv6 addresses.
+ (getByName, getAllByName): Defer to the above to ensure that the
+ correct Inet*Address objects are returned.
+
2006-09-01 Gary Benson <gbenson@redhat.com>
* java/net/InetAddress.java (getByName, getAllByName):
Index: java/net/InetAddress.java
===================================================================
--- java/net/InetAddress.java (revision 116621)
+++ java/net/InetAddress.java (working copy)
@@ -536,7 +536,20 @@
return new Inet4Address(addr, host);
if (addr.length == 16)
- return new Inet6Address(addr, host);
+ {
+ for (int i = 0; i < 12; i++)
+ {
+ if (addr[i] != (i < 10 ? 0 : (byte) 0xFF))
+ return new Inet6Address(addr, host);
+ }
+
+ byte[] ip4addr = new byte[4];
+ ip4addr[0] = addr[12];
+ ip4addr[1] = addr[13];
+ ip4addr[2] = addr[14];
+ ip4addr[3] = addr[15];
+ return new Inet4Address(ip4addr, host);
+ }
throw new UnknownHostException("IP address has illegal length");
}
@@ -599,25 +612,7 @@
// Assume that the host string is an IP address
byte[] address = aton(hostname);
if (address != null)
- {
- if (address.length == 4)
- return new Inet4Address (address, null);
- else if (address.length == 16)
- {
- if ((address [10] == 0xFF) && (address [11] == 0xFF))
- {
- byte[] ip4addr = new byte [4];
- ip4addr [0] = address [12];
- ip4addr [1] = address [13];
- ip4addr [2] = address [14];
- ip4addr [3] = address [15];
- return new Inet4Address (ip4addr, null);
- }
- return new Inet6Address (address, null);
- }
- else
- throw new UnknownHostException ("Address has invalid length");
- }
+ return getByAddress(address);
// Perform security check before resolving
SecurityManager s = System.getSecurityManager();
@@ -658,11 +653,7 @@
// Check if hostname is an IP address
byte[] address = aton (hostname);
if (address != null)
- {
- InetAddress[] result = new InetAddress [1];
- result [0] = new InetAddress (address, null);
- return result;
- }
+ return new InetAddress[] {getByAddress(address)};
// Perform security check before resolving
SecurityManager s = System.getSecurityManager();
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2006-09-01 13:35 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-09-01 13:35 FYI: InetAddress refactoring Gary Benson
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).