From ff51ff34125ef0dcfe67905aecd83de7ee9a615a Mon Sep 17 00:00:00 2001 From: Jacobus Martinus Kruithof Date: Sun, 29 Aug 2010 21:07:29 +0000 Subject: [PATCH] Bugzilla 49513, reported by jks/iname Availability of hostname is now taken into consideration when selecting a local address. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@990653 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 8 +++++ .../apache/tools/ant/taskdefs/HostInfo.java | 32 ++++++++++++++----- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index d865037bf..058658723 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -55,6 +55,14 @@ Changes that could break older environments: Fixed bugs: ----------- + * hostinfo now prefers addresses with a hostname over addresses without + a hostname, provided the addresses have the same scope. + For local lookup, no IP address will be put in NAME / DOMAIN anymore. + For remote lookup, if a host name was provided and only an IP address is + found, the IP address will no longer overwrite the host name provided to the + task. + Bugzilla Report 49513 + * mmap-based file copy problems under JDK 1.4 on Linux. Bugzilla Report 49430. diff --git a/src/main/org/apache/tools/ant/taskdefs/HostInfo.java b/src/main/org/apache/tools/ant/taskdefs/HostInfo.java index 1b1b03019..1a0deb8db 100644 --- a/src/main/org/apache/tools/ant/taskdefs/HostInfo.java +++ b/src/main/org/apache/tools/ant/taskdefs/HostInfo.java @@ -129,7 +129,7 @@ public class HostInfo extends Task { } selectAddresses(); - if (nameAddr != null) { + if (nameAddr != null && hasHostName(nameAddr)) { setDomainAndName(nameAddr.getCanonicalHostName()); } else { setProperty(DOMAIN, DEF_DOMAIN); @@ -153,6 +153,11 @@ public class HostInfo extends Task { setProperty(ADDR6, DEF_LOCAL_ADDR6); } } + + private boolean hasHostName(InetAddress addr) + { + return !addr.getHostAddress().equals(addr.getCanonicalHostName()); + } private void selectAddresses() { Iterator i = inetAddrs.iterator(); @@ -167,7 +172,7 @@ public class HostInfo extends Task { } } - nameAddr = selectBestAddress(best6, best4); + nameAddr = selectBestAddress(best4, best6); } private InetAddress selectBestAddress(InetAddress bestSoFar, @@ -177,7 +182,7 @@ public class HostInfo extends Task { // none selected so far, so this one is better. best = current; } else { - if (current.isLoopbackAddress()) { + if (current == null || current.isLoopbackAddress()) { // definitely not better than the previously selected address. } else if (current.isLinkLocalAddress()) { // link local considered better than loopback @@ -186,13 +191,24 @@ public class HostInfo extends Task { } } else if (current.isSiteLocalAddress()) { // site local considered better than link local (and loopback) - if (best.isLoopbackAddress() || best.isLinkLocalAddress()) { + // address with hostname resolved considered better than + // address without hostname + if (best.isLoopbackAddress() + || best.isLinkLocalAddress() + || !hasHostName(best)) { best = current; } } else { - // current is a global address, and therefore best (at least - // equally well) - best = current; + // current is a "Global address", considered better than + // site local (and better than link local, loopback) + // address with hostname resolved considered better than + // address without hostname + if (best.isLoopbackAddress() + || best.isLinkLocalAddress() + || best.isSiteLocalAddress() + || !hasHostName(best)) { + best = current; + } } } return best; @@ -204,7 +220,7 @@ public class HostInfo extends Task { selectAddresses(); - if (nameAddr != null) { + if (nameAddr != null && hasHostName(nameAddr)) { setDomainAndName(nameAddr.getCanonicalHostName()); } else { setDomainAndName(host);