Skip to content

A rootless Android app that boots Alpine Linux: run containers (Podman/Docker/LXC) and GUI desktop apps.

fix(status): show every reachable phone address, not just the default route

ExTve74936f37819
diff --git a/app/src/main/java/com/excp/podroid/ui/screens/status/StatusScreen.kt b/app/src/main/java/com/excp/podroid/ui/screens/status/StatusScreen.kt
index febf79a..b60abc2 100644
--- a/app/src/main/java/com/excp/podroid/ui/screens/status/StatusScreen.kt
+++ b/app/src/main/java/com/excp/podroid/ui/screens/status/StatusScreen.kt
@@ -125,6 +125,18 @@ fun StatusScreen(
value = ui.phoneIp,
mono = true,
)
+ // A phone can hold several addresses at once (mobile data plus a WiFi or
+ // USB tether). Forwards listen on all of them, so listing the rest saves
+ // the user guessing which one a given PC can actually reach.
+ ui.phoneAddresses
+ .filter { it.address != ui.phoneIp }
+ .forEach { extra ->
+ PodroidListRow(
+ label = stringResource(R.string.phone_ip_also, extra.iface),
+ value = extra.address,
+ mono = true,
+ )
+ }
Text(
text = stringResource(R.string.status_vm_load_graph_title),
diff --git a/app/src/main/java/com/excp/podroid/ui/screens/status/StatusViewModel.kt b/app/src/main/java/com/excp/podroid/ui/screens/status/StatusViewModel.kt
index 42234c0..e35c5a2 100644
--- a/app/src/main/java/com/excp/podroid/ui/screens/status/StatusViewModel.kt
+++ b/app/src/main/java/com/excp/podroid/ui/screens/status/StatusViewModel.kt
@@ -31,6 +31,8 @@ data class StatusUiState(
val engineSelection: EngineSelection = EngineSelection.AUTO,
val uptimeLabel: String? = null,
val phoneIp: String = "—",
+ /** Every reachable IPv4 address, so a peer on a tether or second network is not left guessing. */
+ val phoneAddresses: List<NetworkUtils.LocalAddress> = emptyList(),
val vmRamMb: Int = 512,
val vmCpus: Int = 2,
val storageSizeGb: Int = 8,
@@ -104,6 +106,7 @@ class StatusViewModel @Inject constructor(
engineSelection = b[1] as EngineSelection,
uptimeLabel = uptimeLabel(vmState, tick),
phoneIp = NetworkUtils.localIpv4(context),
+ phoneAddresses = NetworkUtils.allLocalIpv4(context),
vmRamMb = a[1] as Int,
vmCpus = a[2] as Int,
storageSizeGb = a[3] as Int,
diff --git a/app/src/main/java/com/excp/podroid/util/NetworkUtils.kt b/app/src/main/java/com/excp/podroid/util/NetworkUtils.kt
index 0c100cf..9078d0c 100644
--- a/app/src/main/java/com/excp/podroid/util/NetworkUtils.kt
+++ b/app/src/main/java/com/excp/podroid/util/NetworkUtils.kt
@@ -12,8 +12,12 @@ import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import java.net.Inet4Address
+import java.net.NetworkInterface
object NetworkUtils {
+
+ /** One reachable IPv4 address of this device, tagged with the interface carrying it. */
+ data class LocalAddress(val iface: String, val address: String)
/**
* The address users would `ssh root@<this> -p 9922` to, from another
* device on the same LAN.
@@ -29,6 +33,67 @@ object NetworkUtils {
cm?.let(::firstIpv4ByTransportPreference) ?: "unknown"
} catch (_: Exception) { "unknown" }
+ /**
+ * Every IPv4 address a peer could plausibly reach this device on, most useful first.
+ *
+ * [localIpv4] deliberately answers "the one address traffic leaves through", which is
+ * the wrong question when the phone is simultaneously on mobile data and acting as a
+ * WiFi or USB tether: the default route is cellular, so that is the address shown, and
+ * it is precisely the one a tethered PC cannot reach. Port forwards already listen on
+ * 0.0.0.0, so those peers can connect fine; they were just never told the right address.
+ *
+ * This enumerates interfaces directly rather than going through ConnectivityManager,
+ * because tethering interfaces (ap0, rndis0, swlan0) are downstreams where the phone is
+ * the gateway, not networks it is a client of, so they never appear in `allNetworks` at
+ * any transport preference.
+ */
+ fun allLocalIpv4(context: Context): List<LocalAddress> = try {
+ val primary = localIpv4(context).takeIf { it != "unknown" }
+ rank(enumerateIpv4(), primary)
+ } catch (_: Exception) {
+ emptyList()
+ }
+
+ private fun enumerateIpv4(): List<LocalAddress> = try {
+ NetworkInterface.getNetworkInterfaces()?.toList().orEmpty()
+ .filter { runCatching { it.isUp && !it.isLoopback }.getOrDefault(false) }
+ .flatMap { nif ->
+ nif.inetAddresses.toList()
+ .filterIsInstance<Inet4Address>()
+ .map { LocalAddress(nif.name, it.hostAddress.orEmpty()) }
+ }
+ .filter { it.address.isNotEmpty() && isPresentable(it.address) }
+ } catch (_: Exception) {
+ emptyList()
+ }
+
+ /**
+ * Drops addresses that are real but useless to hand to a peer: link-local autoconf, and
+ * the RFC 7335 464XLAT range carriers use for the CLAT interface, which shows up on
+ * IPv6-only mobile networks and is not reachable by anyone.
+ */
+ private fun isPresentable(addr: String): Boolean =
+ !addr.startsWith("169.254.") && !addr.startsWith("192.0.0.")
+
+ /**
+ * Primary first so the common case reads the same as before, then LAN-style private
+ * addresses, since those are what a nearby PC or a tethered client actually dials.
+ */
+ internal fun rank(found: List<LocalAddress>, primary: String?): List<LocalAddress> =
+ found.distinctBy { it.address }
+ .sortedWith(
+ compareBy(
+ { it.address != primary },
+ { !isPrivate(it.address) },
+ { it.iface },
+ ),
+ )
+
+ private fun isPrivate(addr: String): Boolean =
+ addr.startsWith("192.168.") || addr.startsWith("10.") ||
+ addr.startsWith("172.") && addr.substringAfter('.').substringBefore('.').toIntOrNull()
+ ?.let { it in 16..31 } == true
+
private val TRANSPORT_PREFERENCE = intArrayOf(
NetworkCapabilities.TRANSPORT_WIFI,
NetworkCapabilities.TRANSPORT_ETHERNET,
diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml
index 83035d5..7038b46 100644
--- a/app/src/main/res/values-zh/strings.xml
+++ b/app/src/main/res/values-zh/strings.xml
@@ -19,6 +19,7 @@
<!-- Network -->
<string name="network">网络</string>
<string name="phone_ip">手机 IP</string>
+ <string name="phone_ip_also">另一地址 (%1$s)</string>
<string name="ssh">SSH</string>
<string name="port_forwards">端口转发</string>
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 40321d8..0d332a6 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -19,6 +19,7 @@
<!-- Network -->
<string name="network">Network</string>
<string name="phone_ip">Phone IP</string>
+ <string name="phone_ip_also">Also on %1$s</string>
<string name="ssh">SSH</string>
<string name="port_forwards">Port forwards</string>
diff --git a/app/src/test/java/com/excp/podroid/util/NetworkUtilsRankTest.kt b/app/src/test/java/com/excp/podroid/util/NetworkUtilsRankTest.kt
new file mode 100644
index 0000000..0a031b0
--- /dev/null
+++ b/app/src/test/java/com/excp/podroid/util/NetworkUtilsRankTest.kt
@@ -0,0 +1,87 @@
+/*
+ * Podroid - Rootless Podman for Android
+ * Copyright (C) 2024-2026 Podroid contributors
+ *
+ * Ordering rules for the device's reachable IPv4 addresses.
+ */
+package com.excp.podroid.util
+
+import com.excp.podroid.util.NetworkUtils.LocalAddress
+import org.junit.Assert.assertEquals
+import org.junit.Test
+
+/**
+ * The interesting case is a phone on mobile data that is also tethering: the default
+ * route is the carrier's, so that is the "primary" address, yet the address a tethered
+ * PC must dial is the private one. Both need to be visible, and the useful ones first.
+ *
+ * Addresses here are illustrative only. 100.x is carrier-grade NAT space, which the
+ * ranking treats as non-private; the 192.168 and 10.x values stand in for a LAN.
+ */
+class NetworkUtilsRankTest {
+
+ private fun addrs(vararg pairs: Pair<String, String>) =
+ pairs.map { LocalAddress(it.first, it.second) }
+
+ @Test
+ fun `primary address is listed first even when it is not a private one`() {
+ val ranked = NetworkUtils.rank(
+ addrs(
+ "wlan0" to "192.168.45.25",
+ "rmnet0" to "100.82.14.7",
+ ),
+ primary = "100.82.14.7",
+ )
+ assertEquals("100.82.14.7", ranked.first().address)
+ }
+
+ @Test
+ fun `private LAN addresses outrank public ones when neither is primary`() {
+ val ranked = NetworkUtils.rank(
+ addrs(
+ "rmnet0" to "100.82.14.7",
+ "ap0" to "192.168.45.25",
+ ),
+ primary = null,
+ )
+ assertEquals("192.168.45.25", ranked.first().address)
+ }
+
+ @Test
+ fun `every address survives ranking so none is hidden from the user`() {
+ val ranked = NetworkUtils.rank(
+ addrs(
+ "wlan0" to "192.168.1.10",
+ "rmnet0" to "10.20.30.40",
+ "rndis0" to "192.168.35.25",
+ ),
+ primary = "192.168.1.10",
+ )
+ assertEquals(3, ranked.size)
+ assertEquals("192.168.1.10", ranked.first().address)
+ }
+
+ @Test
+ fun `the same address on two interfaces is reported once`() {
+ val ranked = NetworkUtils.rank(
+ addrs(
+ "wlan0" to "192.168.1.5",
+ "wlan1" to "192.168.1.5",
+ ),
+ primary = null,
+ )
+ assertEquals(1, ranked.size)
+ }
+
+ @Test
+ fun `172 addresses count as private only inside the reserved 16 to 31 block`() {
+ val ranked = NetworkUtils.rank(
+ addrs(
+ "eth0" to "172.32.0.4",
+ "eth1" to "172.20.0.4",
+ ),
+ primary = null,
+ )
+ assertEquals("172.20.0.4", ranked.first().address)
+ }
+}