Skip to content

Commit

Permalink
Merge pull request #169 from md5555/feature/issue-164-android-openjdk
Browse files Browse the repository at this point in the history
Try to access InetAddressHolder to set hostname
  • Loading branch information
Christian Bauer authored Jul 6, 2016
2 parents 159fe18 + 5e0d24a commit 17862c4
Showing 1 changed file with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,31 @@ protected boolean isUsableAddress(NetworkInterface networkInterface, InetAddress
// TODO: Workaround Android DNS reverse lookup issue, still a problem on ICS+?
// http://4thline.org/projects/mailinglists.html#nabble-td3011461
String hostName = address.getHostAddress();
try {
Field field = InetAddress.class.getDeclaredField("hostName");
field.setAccessible(true);
field.set(address, hostName);
} catch (Exception ex) {

Field field0 = null;
Object target = null;

try {

try {
field0 = InetAddress.class.getDeclaredField("holder");
field0.setAccessible(true);
target = field0.get(address);
field0 = target.getClass().getDeclaredField("hostName");
} catch( NoSuchFieldException e ) {
// Let's try the non-OpenJDK variant
field0 = InetAddress.class.getDeclaredField("hostName");
target = address;
}

if (field0 != null && target != null && hostName != null) {
field0.setAccessible(true);
field0.set(target, hostName);
} else {
return false;
}

} catch (Exception ex) {
log.log(Level.SEVERE,
"Failed injecting hostName to work around Android InetAddress DNS bug: " + address,
ex
Expand Down Expand Up @@ -90,4 +110,4 @@ protected void discoverNetworkInterfaces() throws InitializationException {
super.discoverNetworkInterfaces();
}
}
}
}

0 comments on commit 17862c4

Please sign in to comment.