From 274514e9855174db1d9d9def24816e4faac5f1c3 Mon Sep 17 00:00:00 2001 From: Milosz Derezynski Date: Mon, 23 May 2016 21:01:03 +0200 Subject: [PATCH 1/5] Try to access InetAddressHolder to set hostname --- .../android/AndroidNetworkAddressFactory.java | 30 +++++++++++++++---- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/fourthline/cling/android/AndroidNetworkAddressFactory.java b/core/src/main/java/org/fourthline/cling/android/AndroidNetworkAddressFactory.java index 107419967..85c80e273 100644 --- a/core/src/main/java/org/fourthline/cling/android/AndroidNetworkAddressFactory.java +++ b/core/src/main/java/org/fourthline/cling/android/AndroidNetworkAddressFactory.java @@ -15,9 +15,12 @@ package org.fourthline.cling.android; +import android.os.Build; + import org.fourthline.cling.transport.impl.NetworkAddressFactoryImpl; import org.fourthline.cling.transport.spi.InitializationException; +import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.net.Inet4Address; import java.net.Inet6Address; @@ -52,11 +55,26 @@ 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; + Object target; + + 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; + } + + field0.setAccessible(true); + field0.set(target, hostName); + + } catch (Exception ex) { log.log(Level.SEVERE, "Failed injecting hostName to work around Android InetAddress DNS bug: " + address, ex @@ -90,4 +108,4 @@ protected void discoverNetworkInterfaces() throws InitializationException { super.discoverNetworkInterfaces(); } } -} \ No newline at end of file +} From 4fdde0ffeb74af8a9631c90e4e5afeffee9ebc4d Mon Sep 17 00:00:00 2001 From: Milosz Derezynski Date: Mon, 23 May 2016 21:04:10 +0200 Subject: [PATCH 2/5] Cleanup --- .../fourthline/cling/android/AndroidNetworkAddressFactory.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/core/src/main/java/org/fourthline/cling/android/AndroidNetworkAddressFactory.java b/core/src/main/java/org/fourthline/cling/android/AndroidNetworkAddressFactory.java index 85c80e273..4edd5cf7a 100644 --- a/core/src/main/java/org/fourthline/cling/android/AndroidNetworkAddressFactory.java +++ b/core/src/main/java/org/fourthline/cling/android/AndroidNetworkAddressFactory.java @@ -15,12 +15,9 @@ package org.fourthline.cling.android; -import android.os.Build; - import org.fourthline.cling.transport.impl.NetworkAddressFactoryImpl; import org.fourthline.cling.transport.spi.InitializationException; -import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.net.Inet4Address; import java.net.Inet6Address; From f742cdbcaf3033d9108870e5a7e0cdc9060722e7 Mon Sep 17 00:00:00 2001 From: Milosz Derezynski Date: Mon, 23 May 2016 21:06:12 +0200 Subject: [PATCH 3/5] Check for target and field0 being null --- .../cling/android/AndroidNetworkAddressFactory.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/fourthline/cling/android/AndroidNetworkAddressFactory.java b/core/src/main/java/org/fourthline/cling/android/AndroidNetworkAddressFactory.java index 4edd5cf7a..31b227d73 100644 --- a/core/src/main/java/org/fourthline/cling/android/AndroidNetworkAddressFactory.java +++ b/core/src/main/java/org/fourthline/cling/android/AndroidNetworkAddressFactory.java @@ -52,8 +52,9 @@ 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(); - Field field0; - Object target; + + Field field0 = null; + Object target = null; try { @@ -68,8 +69,12 @@ protected boolean isUsableAddress(NetworkInterface networkInterface, InetAddress target = address; } - field0.setAccessible(true); - field0.set(target, hostName); + if (field != null && target != null && hostName != null) { + field0.setAccessible(true); + field0.set(target, hostName); + } else { + return false; + } } catch (Exception ex) { log.log(Level.SEVERE, From 877787df3f137e90c2d23050db31bdf75df6353d Mon Sep 17 00:00:00 2001 From: Milosz Derezynski Date: Mon, 23 May 2016 21:07:06 +0200 Subject: [PATCH 4/5] Fix typo --- .../fourthline/cling/android/AndroidNetworkAddressFactory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/org/fourthline/cling/android/AndroidNetworkAddressFactory.java b/core/src/main/java/org/fourthline/cling/android/AndroidNetworkAddressFactory.java index 31b227d73..c996c4565 100644 --- a/core/src/main/java/org/fourthline/cling/android/AndroidNetworkAddressFactory.java +++ b/core/src/main/java/org/fourthline/cling/android/AndroidNetworkAddressFactory.java @@ -69,7 +69,7 @@ protected boolean isUsableAddress(NetworkInterface networkInterface, InetAddress target = address; } - if (field != null && target != null && hostName != null) { + if (field0 != null && target != null && hostName != null) { field0.setAccessible(true); field0.set(target, hostName); } else { From 5e0d24a747eef8725f14489f3c668cd13101d53c Mon Sep 17 00:00:00 2001 From: Milosz Derezynski Date: Mon, 23 May 2016 21:07:50 +0200 Subject: [PATCH 5/5] Fix formatting --- .../android/AndroidNetworkAddressFactory.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/org/fourthline/cling/android/AndroidNetworkAddressFactory.java b/core/src/main/java/org/fourthline/cling/android/AndroidNetworkAddressFactory.java index c996c4565..74d8bd479 100644 --- a/core/src/main/java/org/fourthline/cling/android/AndroidNetworkAddressFactory.java +++ b/core/src/main/java/org/fourthline/cling/android/AndroidNetworkAddressFactory.java @@ -61,20 +61,20 @@ protected boolean isUsableAddress(NetworkInterface networkInterface, InetAddress try { field0 = InetAddress.class.getDeclaredField("holder"); field0.setAccessible(true); - target = field0.get(address); - field0 = target.getClass().getDeclaredField("hostName"); + target = field0.get(address); + field0 = target.getClass().getDeclaredField("hostName"); } catch( NoSuchFieldException e ) { // Let's try the non-OpenJDK variant - field0 = InetAddress.class.getDeclaredField("hostName"); + field0 = InetAddress.class.getDeclaredField("hostName"); target = address; } if (field0 != null && target != null && hostName != null) { - field0.setAccessible(true); - field0.set(target, hostName); - } else { - return false; - } + field0.setAccessible(true); + field0.set(target, hostName); + } else { + return false; + } } catch (Exception ex) { log.log(Level.SEVERE,