diff options
author | Alexander Wuerstlein <arw@arw.name> | 2016-09-01 14:23:02 +0200 |
---|---|---|
committer | Michael Bestas <mikeioannina@gmail.com> | 2016-09-16 18:59:48 +0300 |
commit | 51e4e422f3f6eff1ce151265573534804452defc (patch) | |
tree | 691f7e3ae5a0e535a8f95de85b8d1561188003dc | |
parent | 3eb0163ff05744477b0aa2c0df332501d9264312 (diff) | |
download | android_frameworks_base-51e4e422f3f6eff1ce151265573534804452defc.tar.gz android_frameworks_base-51e4e422f3f6eff1ce151265573534804452defc.tar.bz2 android_frameworks_base-51e4e422f3f6eff1ce151265573534804452defc.zip |
connectivity-service: fix/improve unique hostname
ConnectivityService should set the net.hostname property to either
the current DEVICE_HOSTNAME or android-ANDROID_ID or leave net.hostname
unchanged if already set. However, the logic was flawed such that if
DEVICE_HOSTNAME was empty but net.hostname was not, net.hostname would
always be set to an empty string, leading to DHCP breakage later on.
The logic has been fixed, clarified and improved such that:
net.hostname will only be changed if it is empty. If net.hostname is
empty, it will be set to either (in order): the nonempty value of
DEVICE_HOSTNAME, android-ANDROID_ID or android-r-RANDOM_NUMBER. The last
option is an addition to have a sensible fallback in case both
DEVICE_HOSTNAME and ANDROID_ID are empty.
The random number is generated by java.util.Random, because I consider
cryptographically strong randomness unnecessary here and think possible
blocking in SecureRandom might be undesirable.
Thanks to stargo for pointing me to the right place to edit.
Thanks to Ethan Chen for his stylistic advice.
RM-290
Change-Id: I603ab4d6a265456bf4fa310939965cfa677fc881
-rw-r--r-- | services/core/java/com/android/server/ConnectivityService.java | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java index 2a474607836..1489fd8713a 100644 --- a/services/core/java/com/android/server/ConnectivityService.java +++ b/services/core/java/com/android/server/ConnectivityService.java @@ -159,6 +159,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.concurrent.atomic.AtomicInteger; +import java.util.Random; /** * @hide @@ -646,18 +647,23 @@ public class ConnectivityService extends IConnectivityManager.Stub mTrackerHandler = new NetworkStateTrackerHandler(mHandlerThread.getLooper()); // setup our unique device name - String hostname = CMSettings.Secure.getString(context.getContentResolver(), - CMSettings.Secure.DEVICE_HOSTNAME); - if (TextUtils.isEmpty(hostname) && - TextUtils.isEmpty(SystemProperties.get("net.hostname"))) { + // either to (in order): current net.hostname + // DEVICE_HOSTNAME + // android-ANDROID_ID + // android-r-RANDOM_NUMBER + if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) { + String hostname = CMSettings.Secure.getString(context.getContentResolver(), + CMSettings.Secure.DEVICE_HOSTNAME); String id = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); - if (id != null && id.length() > 0) { + if (!TextUtils.isEmpty(hostname)) { + SystemProperties.set("net.hostname", hostname); + } else if (!TextUtils.isEmpty(id)) { String name = new String("android-").concat(id); SystemProperties.set("net.hostname", name); + } else { + SystemProperties.set("net.hostname", "android-r-" + new Random().nextInt()); } - } else { - SystemProperties.set("net.hostname", hostname); } // read our default dns server ip |