Skip to content

Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.

docs: the lan-play protocol, and an honest list of what is half-done

LNPANDY20b28124c7d2
diff --git a/docs/MULTIPLAYER.md b/docs/MULTIPLAYER.md
new file mode 100644
index 00000000..39558eb5
--- /dev/null
+++ b/docs/MULTIPLAYER.md
@@ -0,0 +1,139 @@
+# Local-network and lan-play multiplayer: what the protocol is, and what we would need
+
+Written after reading [c22dev/mush8](https://github.com/c22dev/mush8), an independent native
+port of Mario Kart 8 Deluxe for Apple platforms (Rust, `nxhost`/`nxpatch`/`nxshader` crates —
+a different implementation of the same idea as nxnative). Nothing here is copied code: the
+sections below describe the **public protocol** and the shape of a host-side implementation.
+
+**We have none of this today.** `dm/host/hle_offline.c` line 1 says so outright: online play,
+local wireless, friends and SSL are "out of scope … all made unavailable, the game takes its
+offline branch", and there is no `hle_bsd` — every socket call returns -1 / `ENETDOWN`.
+
+## Three modes, one virtual network
+
+mush8 offers LAN, LanPlay and Wireless. All three are the same shape: a virtual network stack
+in the host, with one pluggable transport carrying raw IP packets.
+
+ game → nn::socket (bind/sendto/recvfrom/poll)
+ → virtual network: own address 10.13.x.y, mask 255.255.0.0, gateway 10.13.37.1
+ → Transport: LAN (real broadcast) | LanPlay (UDP to a server) | Wireless (peer to peer)
+
+The game's own code is untouched: it sees a normal BSD socket API, and everything below is host
+side. That is the property that makes this cheap to bolt onto a host that already has the
+services stubbed out.
+
+### The virtual network layer
+
+- **Address**: random `10.13.<1..254>.<1..254>`, never the gateway, chosen once per process.
+- **Mask** `255.255.0.0`, **gateway** `10.13.37.1`, **MTU** 1500.
+- **Broadcast**: `255.255.255.255` and the subnet's own broadcast address both deliver to every
+ local socket bound to that port — this is how discovery works, and why no server is needed on
+ a LAN.
+- **Per-socket state**: local bind address, optional connected peer, non-blocking flag, timeout,
+ a receive queue and a "changed" epoch so `poll`/`select` can wake when a packet arrives.
+- **Socket registry per port**: a socket can be found by the port a packet arrives on, which is
+ what makes broadcast delivery work without a real routing table.
+
+### lan-play (the public server protocol)
+
+Port **11451** by default, plain UDP, one byte of type followed by the payload.
+
+| Byte | Type | Direction | Payload |
+|---|---|---|---|
+| `0x00` | keepalive | client → server | none |
+| `0x01` | IPv4 | both | a whole IP packet |
+| `0x02` | ping | client → server | 4 random bytes (echoed back) |
+| `0x03` | IPv4 fragment | both | 16-byte header + data (below) |
+| `0x04` | auth | server → client | the server asks for a login; implementations without it log and continue |
+| `0x10` | info | server → client | a UTF-8 message to show the player |
+
+The high bit (`type & 0x7F`) is masked off on receive, so `0x81` is accepted as IPv4.
+
+**Fragment header** (16 bytes, all multi-byte fields big-endian):
+
+ offset size field
+ 0 4 source IPv4 address
+ 4 4 (reserved in the observed implementation)
+ 8 2 fragment id
+ 10 1 part index (0-based)
+ 11 1 total parts
+ 12 2 data length in this packet
+ 14 2 path MTU, i.e. the data length of every part except the last
+
+Reassembly keeps a table keyed by `(source, fragment id)`, drops entries older than 5 s, rejects
+mismatched totals or MTUs, and concatenates parts in index order.
+
+**Client behaviour**: keepalive + ping every 10 s, consider the server lost after 30 s of
+silence, reconnect every 5 s on failure. The client picks its virtual address itself; the server
+maps it to the connection.
+
+A real Switch can join a lan-play server, which is the whole point: this is how Switch players
+and a ported build end up in the same lobby. For LAN mode the README of mush8 notes that the
+**game update must match the Switch's** — version mismatch is the usual cause of "we cannot see
+each other".
+
+### LAN mode
+
+Real broadcast on the physical interface instead of an encapsulation: packets go out as UDP
+broadcasts on the local subnet so other Switches (and other hosts running the same build) see
+them. Simpler than lan-play, but only works when everyone shares one subnet.
+
+### Wireless mode
+
+Peer discovery without a router, using the platform's peer-to-peer mechanism (mush8 uses mDNS /
+DNS-SD to advertise and browse, `dnssd.rs`: `DNSServiceRegister` / `DNSServiceBrowse` /
+`DNSServiceResolve` / `DNSServiceGetAddrInfo`). Each player advertises a service and browses for
+the others; the first contact becomes the transport.
+
+## What the game needs above the transport
+
+Transport alone is not enough: the game asks Horizon whether a wireless/LAN session exists and
+what the room looks like. That is `nn::ldn` (local wireless), `nn::nifm` (network interface) and
+in some titles `nn::vnet` (virtual network sockets). Each game uses a subset differently:
+
+- which `ldn` calls it makes (create/join/scan, and in what order),
+- how it encodes room name, password, player list and game mode in the advertise/join payloads,
+- whether it opens sockets through `nn::socket` or `nn::vnet`,
+- what it does when a scan returns nothing.
+
+This is why **mush8 supports exactly one game**: the `ldn` payload layer is per-title. A general
+implementation would expose the virtual network and the `ldn` primitives, and then each game
+needs its own mapping from "the bytes in the room struct" to something meaningful.
+
+## What an implementation in our host would need
+
+Roughly four layers, in order:
+
+| Layer | Content | Where it would live |
+|---|---|---|
+| 1. BSD sockets | `socket`/`bind`/`sendto`/`recvfrom`/`poll`/`select` over the virtual network, with the errno conventions the SDK expects | new `dm/host/hle_bsd.c` (replaces the offline stubs in `hle_offline.c`) |
+| 2. Virtual network | address/MASK/gateway, per-port socket registry, broadcast rules, receive queues, wake-on-packet for `poll` | new `dm/host/dm_vnet.c` |
+| 3. Transports | lan-play framing + reassembly; LAN broadcast; wireless via mDNS | one file per transport |
+| 4. `ldn` / `nifm` | make the game believe a session can exist; per-game payload mapping | `hle_ldn.c`, and per-game code |
+
+Layers 1–3 are game independent and reusable. Layer 4 is where a new title costs work again.
+
+**Cost estimate**: layers 1–3 for one game (Mario Kart 8 Deluxe, since its protocol is the one
+documented above and it is the title mush8 proved) ≈ 2–3 weeks. Each further title that uses
+`ldn` differently: days to weeks.
+
+## Open questions before committing to this
+
+- **Does our Metal host survive packet injection at frame rate?** Untested. The transport runs
+ on its own thread, but `poll` must wake a blocked guest thread promptly.
+- **iOS local network permission**: iOS 14+ shows a prompt and the entitlement
+ `com.apple.developer.networking.multicast` may be needed for broadcast; a sideloaded unsigned
+ IPA cannot carry it, which could make LAN mode impossible on iOS while lan-play (unicast UDP)
+ still works.
+- **Matching the game update to the Switch's**, per mush8's note — our profiles already pin an
+ update version per game, so this is checkable, but users must know.
+- **mush8 is Rust and we are C/ObjC**: no code can be taken, only the protocol. Anything we build
+ is a fresh implementation with its own bugs, and it cannot be verified without two clients —
+ which means you need a real Switch, a second iPhone, or two Macs on one network to test.
+
+## Sources
+
+- [c22dev/mush8](https://github.com/c22dev/mush8) — `crates/nxhost/src/services/{vnet,lanplay,lan,wireless}.rs`,
+ `crates/nxhost/src/dnssd.rs` (read at commit 0.1).
+- lan-play itself is a public protocol with several open implementations (switch-lan-play and
+ clients for it); the constant table above matches what mush8 implements.
diff --git a/docs/WORK-IN-PROGRESS.md b/docs/WORK-IN-PROGRESS.md
new file mode 100644
index 00000000..3f8f65ec
--- /dev/null
+++ b/docs/WORK-IN-PROGRESS.md
@@ -0,0 +1,88 @@
+# Work in progress
+
+Everything below is either finished-but-unmerged, half-done, or researched-only. Ordered by
+what blocks the most other work. Keep this file current: an item moves to "Done" only when it
+is on main and verified.
+
+Last updated: 2026-10-26.
+
+## Done and on main (for reference)
+
+| Item | Commit |
+|---|---|
+| SwiftUI app: v0.4.3, self-update, repo download/pull, 58→69 games | `v0.4.3` |
+| Save-load deadlock (issue #4), ported upstream as PR #141 | `569a415`, upstream `b87a0cc` |
+| NuGet / DOTNET_ROOT / xattr environment fixes | `1cd32ed`, `ed06721`, `9741197` |
+| Sync guards: unit tests + patcher-consistency check in the sync | `f074175`-1 |
+| Daily health-check workflow | `.github/workflows/daily-health.yml` |
+| Qt front end (Windows + macOS), dark card UI, high-DPI, ~80 MB package | `9112dda`, `ba92be2`+ |
+| Windows build capability (4,100 lines: toolchain, shims, cross-compile) | `f074175` |
+| Touch-pad geometry + `DM_TOUCH_PREVIEW` (layer 1 of touch support) | `9112dda` |
+| Roadmap + multiplayer protocol notes | `docs/ROADMAP.md`, `docs/MULTIPLAYER.md` |
+
+## A. Needs your test on Windows (blocking my next step)
+
+**The Windows build path has never run on Windows.** The Qt front end runs (you opened it) and
+the build capability is written and unit-tested on macOS, but nobody has clicked Build on a
+Windows machine.
+
+- [ ] Download the latest `NS-Port-Builder-qt-windows-x64.zip`, run it, and report:
+ does the dependency panel find a .NET SDK, and what does it say about LLVM/MinGW,
+ iPhoneOS SDK and Metal tools (`python -m nspb_qt.win.doctor` gives a full report).
+- Blocked by this: everything in section C.
+
+## B. Half-done, finish without your input
+
+- [ ] **BC texture decode** (from d2r-mobile: `bcdec.h` + 58-line `nvn_bcdec.c` glue). The code
+ is identified and read; **not yet copied into the tree**. Plan: `dm/third_party/bcdec/`,
+ `dm/gfx/tex/nvn_bcdec.{c,h}`, hook in `nvn_fmt_mtl.c`, verified by decoding a known BC
+ block and comparing bytes. Default off, enabled per profile, so nobody is affected until
+ it is proven on an A15/A16 device (which neither of us has).
+- [ ] **Launcher architecture A**: `Documents/games/<titleID>/{romfs,msl,save,temp}` layout,
+ `main.m romfs_locate` extended to look there, `DM_ROMFS`/`DM_MSL_DIR`/`DM_SAVE` set from the
+ chosen game. This is what makes an external drive work. The runtime-profile part
+ (`hle_profile.c`) was proven in a worktree but is **not merged**.
+- [ ] Commit the runtime-profile work from the investigation
+ (`/tmp/nx-mg`, branch `mg-profile-runtime`) if we adopt architecture A or B — currently the
+ experimental patches live only in that worktree.
+- [ ] v0.4.4 release once B has something user-visible (external drive support is the best
+ candidate).
+
+## C. Researched only, not started
+
+Ordered by value per week. See `docs/ROADMAP.md` for the reasoning.
+
+| # | Item | Effort | Why not started |
+|---|---|---|---|
+| C1 | **Relative touch look** (`hle_look.c` idea) | 1–2 wks | touch-the-wild's version depends on per-game observation hooks and float-exact provenance; author says unstable. Layer 1 (geometry) is already in |
+| C2 | **Native aspect ratio** (`hle_aspect.c` idea) | 3–5 d per game | Needs per-game projection offsets; layer 1 came first |
+| C3 | **Atmosphère cheats** | 0 if upstream merges PR #268 | Waiting on upstream; falls back to ~1 wk of our own if it stalls |
+| C4 | **Settings capability gating** (`requires` + `defaultAtFps` from AnySwitch) | 1–2 wks | Polish; needs `settings.tsv` schema change |
+| C5 | **Universal launcher architecture B** (4–8 games per IPA) | 3–4 wks | Blocked on: NRO-style module packing for non-NRO modules (`loader.c map_image`), and on deciding whether 4–8 games is worth it |
+| C6 | **Multiplayer (LAN / lan-play / wireless)** | 2–3 wks for one game | Protocol documented in `docs/MULTIPLAYER.md`; needs BSD socket layer + virtual network + per-game `ldn` mapping. Untested: iOS multicast entitlement on a sideloaded IPA |
+| C7 | **Launcher architecture C** (`dlopen` per-game framework) | large | Needs a `dlopen` feasibility experiment first, per the investigation |
+| C8 | **Pikmin 4 / Yoshi / other fork profiles** | days each | Cherriable from forks once their PRs land upstream |
+
+## D. Blocked on upstream
+
+- [ ] PR #268 Atmosphère cheats — watch for merge; our daily sync will bring it in.
+- [ ] seedds/nxnative#205 (we filed): ARM64 branch range on a 185 MB text. Without it,
+ EA Sports FC 27 and similar large titles cannot be rewritten.
+- [ ] Upstream PRs from forks worth cherry-picking: #218/#216 (Yoshi sparse textures, texraw),
+ #261 (CPU deswizzle, already merged), #269 (Pokémon Violet + BLE).
+
+## E. Small things I noticed but have not done
+
+- [ ] The Qt front end has no reproducible "check for app updates" (the SwiftUI one has it).
+- [ ] Qt front end: per-stage percentage display (currently an indeterminate bar for the
+ running stage; the SwiftUI app shows real percentages from the script's own output).
+- [ ] `frontend-qt/README.md` says the Windows path is untested — update once section A passes.
+- [ ] The games table in both READMEs is hand-inserted; `refresh_badges()` already counts the
+ folders, so the table could be generated too.
+- [ ] `docs/ROADMAP.md` predates the BC/multiplayer findings; fold them in.
+
+## How to use this file
+
+- Pick from A first if you are the one with the hardware; otherwise B.
+- C is a menu, not a queue: each entry names its own blocker.
+- Anything finished goes to the top table with its commit, so the list stays short.