maauto: bind IPv4 loopback so adb forward can reach the server
- Commit
73badab5a2209276d6246b8ef942299b091a67c7- Author
- vayun-mathur <[email protected]>
- When
- 2026-09-13 04:23 UTC (2 months ago)
- Parents
839c5fc
Diff
auto/src/main/java/com/vayunmathur/auto/network/HeadUnitServer.kt
diff --git a/auto/src/main/java/com/vayunmathur/auto/network/HeadUnitServer.kt b/auto/src/main/java/com/vayunmathur/auto/network/HeadUnitServer.ktindex 4100b25950..6594aba51d 100644--- a/auto/src/main/java/com/vayunmathur/auto/network/HeadUnitServer.kt+++ b/auto/src/main/java/com/vayunmathur/auto/network/HeadUnitServer.kt@@ -23,7 +23,15 @@ class HeadUnitServer(private val port: Int = DHU_PORT) : Closeable { /** Blocks until a head unit connects. */ fun accept(): Socket {- val socket = server ?: ServerSocket(port, BACKLOG, InetAddress.getLoopbackAddress())+ // `getLoopbackAddress()` returns the IPv6 `::1` on dual-stack ART, which+ // leaves the socket unreachable from an `adb forward` that dials IPv4+ // 127.0.0.1 on-device. Bind IPv4 explicitly and fall back to dual-stack+ // `::` only if the bind fails: adb can reach either, and the wildcard+ // would expose the socket only to the phone itself either way.+ val socket = server ?: tryIpv4Loopback() ?: ServerSocket(port, BACKLOG)+ .also {+ Log.w(TAG, "IPv4 loopback bind failed; listening dual-stack instead")+ } .also { server = it Log.i(TAG, "listening for a head unit on ${it.inetAddress.hostAddress}:$port")@@ -41,11 +49,18 @@ class HeadUnitServer(private val port: Int = DHU_PORT) : Closeable { server = null } + private fun tryIpv4Loopback(): ServerSocket? = runCatching {+ ServerSocket(port, BACKLOG, InetAddress.getByName(IPV4_LOOPBACK))+ }.getOrNull()+ companion object { /** The port the Desktop Head Unit expects to be forwarded to. */ const val DHU_PORT = 5277 private const val TAG = "MaAuto.Server" private const val BACKLOG = 1++ /** Explicit IPv4 loopback: `getLoopbackAddress()` yields IPv6 `::1` on ART. */+ private const val IPV4_LOOPBACK = "127.0.0.1" } }