Skip to content

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

x11: VncClient handshake (RFB 3.8, None auth)

ExTv71fdd07c7306
diff --git a/app/src/main/java/com/excp/podroid/x11/VncClient.kt b/app/src/main/java/com/excp/podroid/x11/VncClient.kt
new file mode 100644
index 0000000..58d3e17
--- /dev/null
+++ b/app/src/main/java/com/excp/podroid/x11/VncClient.kt
@@ -0,0 +1,67 @@
+/*
+ * Podroid - Rootless Podman for Android
+ * Copyright (C) 2024-2026 Podroid contributors
+ *
+ * Minimal RFB 3.8 client. Supports SecurityType None, Raw + CopyRect +
+ * Cursor pseudo encodings. Designed for loopback (SLIRP) so we don't
+ * bother with Tight / ZRLE / TLS.
+ */
+package com.excp.podroid.x11
+
+import java.io.DataInputStream
+import java.io.InputStream
+import java.io.OutputStream
+
+data class VncServerInfo(val width: Int, val height: Int, val name: String)
+
+object VncClient {
+ private const val PROTOCOL_VERSION = "RFB 003.008\n"
+ private const val SEC_TYPE_NONE: Byte = 1
+
+ /**
+ * Performs the RFB 3.8 handshake. Reads the server greeting from `inp`,
+ * writes our responses to `out`, and returns the framebuffer dimensions
+ * (the only ServerInit fields we need for v1 — pixel format is fixed
+ * 32-bit BGRA via SetPixelFormat sent later by the caller).
+ *
+ * Throws IOException on protocol mismatch.
+ */
+ fun handshake(inp: InputStream, out: OutputStream): VncServerInfo {
+ val din = DataInputStream(inp)
+
+ // 1. Read 12-byte version "RFB xxx.yyy\n"
+ val serverVersion = ByteArray(12).also { din.readFully(it) }
+ require(serverVersion[0] == 'R'.code.toByte()) { "not RFB greeting" }
+
+ // 2. Send our version (always 003.008)
+ out.write(PROTOCOL_VERSION.toByteArray())
+ out.flush()
+
+ // 3. Read security types. 0 => failure (not handled here)
+ val numTypes = din.readUnsignedByte()
+ require(numTypes > 0) { "server reported zero security types" }
+ val types = ByteArray(numTypes).also { din.readFully(it) }
+ require(types.any { it == SEC_TYPE_NONE }) { "server has no None auth" }
+
+ // 4. Choose None
+ out.write(byteArrayOf(SEC_TYPE_NONE))
+ out.flush()
+
+ // 5. Read SecurityResult (4 bytes; 0 = OK)
+ val secResult = din.readInt()
+ require(secResult == 0) { "security result $secResult" }
+
+ // 6. Send ClientInit (1 byte: shared = 1)
+ out.write(byteArrayOf(1))
+ out.flush()
+
+ // 7. Read ServerInit
+ val w = din.readUnsignedShort()
+ val h = din.readUnsignedShort()
+ din.skipBytes(16) // pixel format we'll override
+ val nameLen = din.readInt()
+ val name = ByteArray(nameLen).also { din.readFully(it) }.toString(Charsets.UTF_8)
+
+ return VncServerInfo(w, h, name)
+ }
+}
diff --git a/app/src/test/java/com/excp/podroid/x11/VncClientTest.kt b/app/src/test/java/com/excp/podroid/x11/VncClientTest.kt
new file mode 100644
index 0000000..2eaba19
--- /dev/null
+++ b/app/src/test/java/com/excp/podroid/x11/VncClientTest.kt
@@ -0,0 +1,51 @@
+/*
+ * Podroid - Rootless Podman for Android
+ * Copyright (C) 2024-2026 Podroid contributors
+ */
+package com.excp.podroid.x11
+
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertArrayEquals
+import org.junit.Test
+import java.io.ByteArrayInputStream
+import java.io.ByteArrayOutputStream
+
+class VncClientTest {
+
+ @Test
+ fun `handshake replies with RFB 003 008 and selects None auth`() {
+ // Server greeting: "RFB 003.008\n" then 1 security type + [None=1]
+ val serverBytes = byteArrayOf(
+ // 12-byte version greeting
+ 'R'.code.toByte(), 'F'.code.toByte(), 'B'.code.toByte(), ' '.code.toByte(),
+ '0'.code.toByte(), '0'.code.toByte(), '3'.code.toByte(), '.'.code.toByte(),
+ '0'.code.toByte(), '0'.code.toByte(), '8'.code.toByte(), '\n'.code.toByte(),
+ // Security types: count=1, [None=1]
+ 0x01, 0x01,
+ // SecurityResult: OK=0
+ 0x00, 0x00, 0x00, 0x00,
+ // ServerInit: width=1280, height=720, pixel-format (16 bytes), name-len=0
+ 0x05, 0x00, // width 1280
+ 0x02, 0xD0.toByte(), // height 720
+ 32, 24, 0, 1, // bpp, depth, big-endian, true-color
+ 0x00, 0xFF.toByte(), 0x00, 0xFF.toByte(), 0x00, 0xFF.toByte(), // max RGB
+ 16, 8, 0, // shifts (ARGB)
+ 0, 0, 0, // padding
+ 0, 0, 0, 0, // name length 0
+ )
+ val out = ByteArrayOutputStream()
+
+ val info = VncClient.handshake(ByteArrayInputStream(serverBytes), out)
+
+ assertEquals(1280, info.width)
+ assertEquals(720, info.height)
+ // Client must have sent: version "RFB 003.008\n", then sec-type 1, then shared=1
+ val sent = out.toByteArray()
+ // First 12 bytes: client version
+ assertArrayEquals("RFB 003.008\n".toByteArray(), sent.copyOfRange(0, 12))
+ // Byte 12: chosen security type = 1 (None)
+ assertEquals(1, sent[12].toInt())
+ // Byte 13: ClientInit shared flag = 1
+ assertEquals(1, sent[13].toInt())
+ }
+}