• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
Keine Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

frameworks/base


Commit MetaInfo

Revision95b0c191099d1e33d5e47bb2db13b504807d57bc (tree)
Zeit2016-10-05 18:04:59
AutorAlexander Wuerstlein <arw@arw....>
CommiterSteve Kondik

Log Message

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.

Change-Id: I603ab4d6a265456bf4fa310939965cfa677fc881

Ändern Zusammenfassung

Diff

--- a/services/core/java/com/android/server/ConnectivityService.java
+++ b/services/core/java/com/android/server/ConnectivityService.java
@@ -166,6 +166,7 @@ import java.util.Map;
166166 import java.util.Objects;
167167 import java.util.SortedSet;
168168 import java.util.TreeSet;
169+import java.util.Random;
169170
170171 /**
171172 * @hide
@@ -656,15 +657,22 @@ public class ConnectivityService extends IConnectivityManager.Stub
656657 mTrackerHandler = new NetworkStateTrackerHandler(mHandlerThread.getLooper());
657658
658659 // setup our unique device name
659- String hostname = CMSettings.Secure.getString(context.getContentResolver(),
660- CMSettings.Secure.DEVICE_HOSTNAME);
661- if (TextUtils.isEmpty(hostname) &&
662- TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
660+ // either to (in order): current net.hostname
661+ // DEVICE_HOSTNAME
662+ // android-ANDROID_ID
663+ // android-r-RANDOM_NUMBER
664+ if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
665+ String hostname = CMSettings.Secure.getString(context.getContentResolver(),
666+ CMSettings.Secure.DEVICE_HOSTNAME);
663667 String id = Settings.Secure.getString(context.getContentResolver(),
664668 Settings.Secure.ANDROID_ID);
665- if (id != null && id.length() > 0) {
669+ if (!TextUtils.isEmpty(hostname)) {
670+ SystemProperties.set("net.hostname", hostname);
671+ } else if (!TextUtils.isEmpty(id)) {
666672 String name = new String("android-").concat(id);
667673 SystemProperties.set("net.hostname", name);
674+ } else {
675+ SystemProperties.set("net.hostname", "android-r-" + new Random().nextInt());
668676 }
669677 }
670678