Skip to content

Commit 335cd0c

Browse files
committed
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
1 parent 61da29a commit 335cd0c

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

services/core/java/com/android/server/ConnectivityService.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@
159159
import java.util.Map;
160160
import java.util.Objects;
161161
import java.util.concurrent.atomic.AtomicInteger;
162+
import java.util.Random;
162163

163164
/**
164165
* @hide
@@ -646,18 +647,23 @@ public ConnectivityService(Context context, INetworkManagementService netManager
646647
mTrackerHandler = new NetworkStateTrackerHandler(mHandlerThread.getLooper());
647648

648649
// setup our unique device name
649-
String hostname = CMSettings.Secure.getString(context.getContentResolver(),
650-
CMSettings.Secure.DEVICE_HOSTNAME);
651-
if (TextUtils.isEmpty(hostname) &&
652-
TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
650+
// either to (in order): current net.hostname
651+
// DEVICE_HOSTNAME
652+
// android-ANDROID_ID
653+
// android-r-RANDOM_NUMBER
654+
if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) {
655+
String hostname = CMSettings.Secure.getString(context.getContentResolver(),
656+
CMSettings.Secure.DEVICE_HOSTNAME);
653657
String id = Settings.Secure.getString(context.getContentResolver(),
654658
Settings.Secure.ANDROID_ID);
655-
if (id != null && id.length() > 0) {
659+
if (!TextUtils.isEmpty(hostname)) {
660+
SystemProperties.set("net.hostname", hostname);
661+
} else if (!TextUtils.isEmpty(id)) {
656662
String name = new String("android-").concat(id);
657663
SystemProperties.set("net.hostname", name);
664+
} else {
665+
SystemProperties.set("net.hostname", "android-r-" + new Random().nextInt());
658666
}
659-
} else {
660-
SystemProperties.set("net.hostname", hostname);
661667
}
662668

663669
// read our default dns server ip

0 commit comments

Comments
 (0)