diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md index dc929eac..6d01b055 100644 --- a/THIRD_PARTY_NOTICES.md +++ b/THIRD_PARTY_NOTICES.md @@ -28,7 +28,6 @@ Code copied or adapted from another project is listed here with its source, the | MacNativeGames block-linear deswizzler (address function; copy loop split out as `tex_bl_copy`) | https://github.com/ly05010419/MacNativeGames @ `b75be8cfa704df3701e86130c8ced268e79d6b11` (`third_party/d2r_gfx/gfx/tex/nvn_deswizzle.c`), Copyright (c) 2026 jhaizhou | MIT | `runtime/tex_deswizzle.c`, `runtime/tex.h` | | SDL 3.4.18, dynamic link, `SDL3.dll` copied next to `nx2navi.exe`. Developer builds use the MSYS2 build (`mingw-w64-ucrt-x86_64-sdl3`, which imports `libiconv-2.dll`, LGPL-2.1, unmodified, copied beside it); the installer release ships the upstream release `SDL3-3.4.18-win32-x64.zip`, which imports no libiconv | https://libsdl.org, Copyright (c) 1997-2026 Sam Lantinga | zlib | `runtime/input_win.c` (gamepad input, rumble, gyro and accelerometer) | | libopus (MSYS2 `mingw-w64-ucrt-x86_64-opus`), dynamic link, `libopus-0.dll` copied next to `nx2navi.exe` | https://opus-codec.org, Copyright (c) 2001-2023 Xiph.Org, Skype Limited, Octasic, Jean-Marc Valin, Timothy B. Terriberry, CSIRO, Gregory Maxwell, Mark Borgerding, Erik de Castro Lopo, Mozilla, Amazon | BSD-3-Clause | `runtime/ov_media.c` (nn::codec hardware Opus decoder) | -| Monocypher 4.0.2 (`monocypher.c`, `monocypher.h`, unmodified; licence text in `runtime/third_party/monocypher/LICENCE.md`) | https://monocypher.org/download/monocypher-4.0.2.tar.gz (SHA-512 `bf275d4c53ff94af6cdc723a4e002e9f080f4d1436c86c76bb37870b34807f1d7b32331d8ff8a1aeb369e946f3769021e03e63efac25b82efc5abf54dc084714`), Copyright (c) 2017-2023 Loup Vaillant, Michael Savage, Fabio Scotoni | BSD-2-Clause (dual-licensed with CC0-1.0; the BSD licence is the one used here) | `runtime/third_party/monocypher/` (compiled into `nx2navi.exe` for co-op: key derivation with BLAKE2b and the room's messages sealed with XChaCha20-Poly1305, `runtime/coop_proto.c`) | | Dear ImGui 1.91.9 (core and Vulkan back end, unmodified), as vendored by LAS Recomp | https://github.com/ocornut/imgui, via https://github.com/peiikai/LASRecomp @ `da35b83a9611d046a9a681aae668d7085954d37f` (`third_party/imgui/`), Copyright (c) 2014-2025 Omar Cornut | MIT | `runtime/third_party/imgui/` (compiled into `nx2navi.exe` for the in-game menu, `runtime/ui/`) | | SavageWindRecomp settings menu: two-pane option rows (row look, left / right stepping, hold speed-up, description pane), the menu style and tab bar | https://github.com/Revivedx/SavageWindRecomp @ `575ad88e02f9a89a9a885d90039d48af33c982f4` (`runtime/ui/option_rows.mm`, `runtime/ui/menu.mm`), Copyright (c) 2026 Revivedx and the Savage Wind Recomp contributors, Copyright (c) 2026 Peiikai | MIT | `runtime/ui/option_rows.cpp`, `runtime/ui/option_rows.h`, `runtime/ui/menu.cpp` | | SavageWindRecomp performance overlay: the frame ring and its one-second summary, the NVML calls, the sampling loop, the full panel's layout | https://github.com/Revivedx/SavageWindRecomp @ `575ad88e02f9a89a9a885d90039d48af33c982f4` (`runtime/perfmon.c`, `runtime/perfmon.h`, `perf_overlay_draw` in `runtime/ui/menu.mm`), Copyright (c) 2026 Revivedx and the Savage Wind Recomp contributors, Copyright (c) 2026 Peiikai | MIT | `runtime/perfmon.c`, `runtime/perfmon.h`, `runtime/ui/menu.cpp` | diff --git a/docs/architecture.md b/docs/architecture.md index 23c3f05a..cf0d2a4e 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -45,8 +45,6 @@ from other projects, with its license, is listed in [THIRD_PARTY_NOTICES.md](../ order it logged them: a line waits for the queue's lock, and one that finds the queue full is written after everything queued before it. A fatal error, the crash report and the end of the process write the queue out first (`rt_log_sync`). `NX2NAVI_LOG_ASYNC=0` writes each line where it is logged. -- Co-op (`coop.c`, `coop_proto.c`, [coop.md](coop.md)): a host thread with a UDP socket to a relay (`tools/coop_relay.py`), - rooms of up to 8 players and a channel for messages between them; it never holds a virtual core. - OS calls the host makes go through `runtime/platform/` (`platform.h`, `win32.c`). #### Threads and cores diff --git a/docs/building.md b/docs/building.md index c6c0e9d8..2160889b 100644 --- a/docs/building.md +++ b/docs/building.md @@ -379,8 +379,7 @@ instead of timing one. No game files. A change to the button prompts (`tools/make_romfs_patches.py --prompts`, `tools/button_icons.py`) passes `.venv-py314\Scripts\python tests\romfs_prompts_test.py`, which runs the tool on a RomFS it makes itself (no game -files); a change to the settings passes `nx2navi.exe --probe-cfg`; a change to the co-op layer (`runtime/coop*.c`, -`tools/coop_relay.py`) passes `python tests\coop_relay_test.py` and `nx2navi.exe --probe-coop` ([coop.md](coop.md)). +files); a change to the settings passes `nx2navi.exe --probe-cfg`. A change to the action prompt's diamond of face buttons (`runtime/action_prompt.c`) or to the SARC and zstd dictionary helpers (`runtime/sarc.c`) passes `python tests\action_prompt_check.py`. It builds them alone, with the diff --git a/docs/coop.md b/docs/coop.md deleted file mode 100644 index a8767ed4..00000000 --- a/docs/coop.md +++ /dev/null @@ -1,621 +0,0 @@ -# Co-op - -Each player runs their own game and save; the co-op layer connects the games of up to 8 players through a relay. The -game has no multiplayer of its own and its `nn::socket` stays offline: the layer is a host thread of the runtime -(`runtime/coop.c`) with its own UDP socket. - -What exists so far: rooms, the relay, a sealed channel for messages between the players, progress sync (below) and the -menu's Co-op tab (below), which makes, joins, accepts and leaves a room. The other players are not shown in your -world, and where your Link is never leaves your PC ("Other players in the world"). - -**Co-op is a beta in 0.8.0.** The menu shows its Co-op tab only with `coop_beta = 1` under `[coop]` in `nx2navi.ini`; -no menu row sets it. Everything below is as it is with the tab shown. - -## What joining a room means for your save - -Read this before you join a room. - -- **Nothing from a room reaches your game until you accept it.** When you join, the menu's Co-op tab and the log name - the other players (each by the name and the key fingerprint their game signed), the room's rules and the key that - signs them; the tab asks you to accept, and the log gives the room's offer a number: `put coop_accept = 1a2b3c4d - into nx2navi.ini [coop] while the game runs`. Accept on the tab or do that. Until then your game takes nothing from - the room. You accept players by - their keys, not their names: a new player, a player whose game restarted, a removed player who comes back, or a - second player with a name you know is a new offer, and so are a key you accepted that now carries another name (it - is accepted under the name it had when you accepted it), other rules, rules signed by another key, and another - save loaded in the room; nothing more is taken until you accept that one too. A key you accepted that comes back - under its own name as another member number (the relay numbers players as they come) is no new offer; the log says - so, and it says when a member presents another key than the one it was identified by. A player in the room whose game never - says who it is keeps the room from being accepted at all. The number is random for each start of the game, so nobody - can put the right one into a file in advance. Accepting every room without asking (`NX2NAVI_COOP_ACCEPT=always`) - works only from the environment, never from `nx2navi.ini`. -- **Your game takes values only from players you accepted.** What a player you did not accept sends is not kept, not - passed on and never applied, also after that player leaves or is removed. Every message that can change your save is - signed by the player who sent it, so this holds also against someone who knows the room's secret and runs the relay, - or sends from its address on your network: they can make no message in another player's name that changes your save. - For story flags and items acceptance is passed along: a player you accepted hands on what they took from the players - *they* accepted, so a stranger someone else let in reaches you through them. Pools are not passed along: your game - counts in a pool only what you and the players you accepted brought, picked up and spent, and joins the pools only - once you accepted the room (another save loaded in it joins only once you accepted that one too). What remains with - the maker: a player uses the pools at all only once the maker's game admitted them, which it does for every player - the maker accepted; a player who left before you accepted is not counted in your pools; and while a player you - accepted counts a stranger in their pools, what they claimed when they joined a pool counts that stranger too, until - the maker removes the stranger. -- **Your save is copied first.** The moment you accept a room, the game copies your whole save (every slot) to - `coop-backup/` in the profile folder, and takes nothing from the room until the copy is made. The first copy for a - room in each start of the game is `first-` and is kept until you delete it; later copies (another save - loaded in the room, say) are ``, of which the newest eight are kept. A room the relay lost and that the - game reaches again is still accepted and copied. "Going back" below says how to restore a copy. A game that has - not saved yet has no save on the disk to copy: it takes nothing from the room until it has saved once (the log says - so), and a copy that cannot be written keeps the room closed as well. -- **What the room can change in your save** is what its rules share (progress, items, songs, quest items; small keys, - rupees and ammunition in a pool): every value is checked against your game's own table of flags before it is - stored, passed on or applied. A flag can only be set to "done", an item count only up to 999, small keys only up to - 15, a pooled counter only within its capacity. Anything else is dropped, counted in the log, and a player who sends a - few dozen of those is ignored for as long as they stay in the room. -- **What a player you let in can still do.** A player in the room plays with you: with progress shared, what they do - in their world (open a chest, beat a boss, set a story flag) happens in yours, and a player who wants to can send - such flags without playing for them. With small keys in a pool, a player you accepted can spend the keys you share: - their spends count at most up to what entered the pool (what the players brought and picked up), so they can empty a - dungeon's pool, and the keys picked up after that, but no more than the dungeon has, and a record that claims less - than nothing counts as nothing. The maker's removal of that player, also after they left, or a pool reset after they - left, gives back what they took, in every game, and each game makes its claim to the pool again as if that player - had never been in it, so keys you brought while their gains were in the pool stay yours, and a claim made while - their spends were in it does not leave the pool fuller than what the others brought. A pool reset while that player - is still in the room carries what they took into the new pools, and removing them afterwards does not give it back: - remove first, then reset (the reset's log line, the maker's hint and the Co-op tab's Reset the pools say so). Keys a - removed player's gains handed out stay as doors already opened, and in the claim of a player who saved with them and - joined the pools again with that save before the removal. With rupees or ammunition in a pool (off by default) the - same holds for them. - Flags stay set when a player is removed. None of this can be undone inside the game otherwise: go back to the copy - of your save. Invite only people you trust with your save, as you would hand them your controller. -- **Who can get in.** Only someone who knows the room's secret: 18 characters (85 bits) that the game makes when a room - is made and that the maker passes on to the others (by voice, a chat message). The relay never learns it. Someone who - sees your traffic on the network, or who runs the relay, cannot read the messages, change them, make new ones or - guess the secret from them. Someone who also knows the secret can read the room's messages, but can still make none - in a player's name: each player signs what they send with their own key. -- **Removing a player.** The room's maker can remove a player by their key's fingerprint, which the log and the Co-op - tab show beside each member (Remove on the player's row of the tab, or `coop_remove = ` in `nx2navi.ini` - while the game runs; while they are in the room or after they left): the maker's game signs the removal, every game drops that player's key and keeps it - dropped (their messages are ignored, their pool records dropped, so what they took from a pool comes back), and the - relay drops them and refuses them again (the session the maker's game listed for them at that moment, so a player - given the number since is not the one dropped). A member number in the file is not taken, since numbers pass to other - players as members come and go, and each value acts once, so a line left in the file or read again after a moment in - which the file could not be read removes nobody else. Someone who knows the secret can still come back under a new - key (everyone then gets a new offer to accept or not); to be sure, make a new room, which has a new secret. The maker - can also reset the pools (`coop_reset_pools = 1`, then 2 the next time, or 0 and then 1 again): every game joins them - again with what they held for the players in the room, which recovers a pool that a player who left had emptied, - also when the maker resets twice before the games joined again. What a game held at the reset is its claim from then - on, also while that player is at the title screen, so a key another player picks up after the reset comes on top of - it. A player who is not in the room at the reset, or whom the maker has not admitted to the pools yet, is carried - for nobody: what it brought and picked up leaves the other games' pools until it plays again in the room, when its - game joins them with what it holds. If the maker resets again before such a player's game joined, that game keeps - the larger of its two carries, so keys the others used in between can come back (more keys, never fewer). Remove a - player who emptied a pool and is still in the room first (both lines in one save of `nx2navi.ini` act - in that order). The key the maker's game signs the room's rules and - removals with is kept in the profile folder (`coop-keys/`, readable by your account only: the folder 0700, the file - 0600 on Linux; on Windows an access list of its own with your account alone), so a maker whose game restarted is still the room's maker. That file, together with the room's - secret, lets whoever has both act as the room's maker: never hand out a copy of your profile folder. A game that sees - another member present its own key says so in the log (that member stays not identified, so nobody can accept the - room while it is there), and a key file that is damaged or cannot be read is logged too. Deleting the file ends your - say over rooms you made before. - -### Who can see what - -| Who | Sees | Cannot | -|---|---|---| -| The relay's operator, and anyone on the network path | that a room exists (a random id and the locator, a value derived from the secret that does not give the secret back), the players' names, their IP addresses, when and how much they send | read, change or make any message between the players; learn the secret; change anyone's save or the room's rules (moving the relay's maker mark changes nothing for a player who accepted). It can drop, delay and send again what a player sent; a player takes one again only as "Replay" below says. | -| Someone who knows the secret and runs the relay, or sends from its address on your network | all of the above, and every message of the room | make a section, identity, pool record or set of rules in another player's name that a game acts on (each is signed by its maker); change your save other than through a player you accepted, or the rules of a player who accepted. It can also: send a member's messages again (see "Replay": within the last 64 they are refused, but four older ones in a row let it send the member's whole present session again); seal a message in a member's name with a counter far ahead, which costs a player that member's next three messages, each time it does so (one after every three of the member's keeps them out while it keeps sending); send rosters, or identities of another key for a member's session, that keep players from being identified or the room from being accepted while it keeps sending and up to 10 s after; send sections and digests in a member's name that use up what a player checks and takes from that member (128 signatures and 48 KB a second), which delays that member's own while it keeps sending; send digests in a member's name with sums of its choosing (see the sync messages); and, to a player who has not accepted the room yet, make every member's digest carry rules of a key of its own (a member does not sign its digest, only the rules in it are signed, by whoever made them): with no maker in the room, or one it controls, that player's game shows those rules and their key in the offer, so compare that key with the maker's before you accept (step 3 below). None of that changes a save, and none of it lasts longer than said once it stops | -| The other players in the room | your name, your progress (what the rules share), your messages | your IP address (the relay forwards everything), your account or profile (the id the relay knows you by is random for each start of the game and never shown to them) | -| Your log and "Copy system info" | a three-character label hashed from the room's secret (no part of the secret) and the offer's number; system info shows that `coop_room`, `coop_server`, `coop_name` and `coop_accept` are set, not their values | the secret, the relay's address (it appears only with `NX2NAVI_TRACE=coop`) | - -### Going back to your save from before a session - -1. Close the game. -2. In the profile folder, open `coop-backup` and pick the copy to go back to: `first-` is the save as it - was before you first accepted that room, `` a later acceptance (the time is UTC). -3. Delete the folder `save-tx` (it may hold a save that was being written; it would otherwise be finished over the copy - at the next start). -4. Move the folder `save/` aside (keep it until you are sure), and copy the contents of the backup folder into - a new `save/` of the same name. -5. Start the game: every slot is as it was when you accepted the room. Progress made after that, in any slot, is in the - folder you moved aside, not in the copy. - -## The menu's Co-op tab - -The menu (F1, or the pad's combo) has a Co-op tab, usable with the keyboard, the mouse and a pad. - -- **Not in a room:** your name and the relay's address (each row opens a field to type in; with a pad, A on the field - brings up the system's on-screen keyboard where it has one), Make a room, Join a room (a field for the secret, in any - case, with or without dashes, and a Paste button that takes the clipboard's text), a line that says what the layer is - doing or why it stopped (no answer from the relay, no room with that secret, refused), the rules of a room you make, - and the co-op switch. Make and Join turn co-op on when it is off. -- **In a room:** while the room waits for your answer, who is in it (a player whose game has not said who it is, or - whose session two keys present at once, is marked "not identified"; a player whose key you accepted under another - name is marked "a key you accepted as ", and that is a new offer), what its rules share, the fingerprint - of the key that signs the rules, to compare with the one the maker reads out to you by voice or chat, and Accept or - not; while any player is not identified the tab says so and why the room cannot be accepted, and Accept is greyed. - Then the players, with who made the room, how long ago each was last heard and each one's key (and the name you - accepted it as, when it now signs another); the maker sees its own key to read out, and Remove on each other - player's row, which removes by the key (a player who is not identified has no key and is removed by member number, - only while the same session still holds it). The maker also has a Pools group: every key that took more from the - pools than it brought, in the room or not, with what it took and a Remove of that key, and Reset the pools, which - says to remove such a player first. What it took can change, up or down, when the maker removes a player or a - player accepts more keys, also while that player's game is at its title screen. Then the - room's secret, hidden until you press its row, and a row that copies it to the clipboard; the rules (the room's - creator changes them, everyone else reads them; a change is a new offer for everyone); Leave the room. The status - line says when the relay lost the room and the game is getting back into it, and when the relay takes no more joins - from your network and the game tries again in a minute. -- **Nothing is accepted, removed, reset or left by accident.** Accept or not, Remove, Reset the pools and Leave each - open a window whose cursor starts on the safe button (Not now, Cancel, Stay), whenever it opens; only Right reaches - the acting button, and pressing the button that opened the window again, holding it, or a double tap lands on the - safe one. A new player or other rules while the Accept window is open replace what it shows and put the cursor back - on Not now: Accept takes only the offer you read. The window says that your whole save is copied to `coop-backup` - first, and before your first Accept in each start of the game it shows the three warnings of "Before you join a room - with strangers" below. -- **While the menu is closed** a short note at the top of the screen says when a player joined or left the room and - when the room waits for your answer. -- The name, the relay, the rules and the switch are settings (written to `nx2navi.ini` like every row); the secret is - not a setting, is not written to the log, and is shown only when you ask. - -## Running a relay - -``` -python tools/coop_relay.py [--host 0.0.0.0] [--port 24680] [--log-addrs] [--log-joins] [--max-rooms 1000] [--rooms-per-network 8] -``` - -Python 3, standard library only. A player who can reach it by UDP (forward the port on the router, open it in the -firewall) is all it takes; the others put its address into `coop_server`. The relay forwards the sealed datagrams of a -room and can read none of what the players send each other. It keeps only who is in which room, writes nothing to -disk, and loses its rooms when it stops: the room's maker makes it again under the same secret. Its log has one line -per room made, join, leave and removal, with names, member ids and a random room id (never the secret or the locator); -addresses only with `--log-addrs`; a line when a network's joins ran out (at most every 10 s); and with `--log-joins`, -where each network's joins went. - -- A room holds up to 8 players, is closed 10 minutes after the last one left (a minute, if nobody but its maker was ever - in it), and a player the relay has not heard from for 15 s is dropped (the member id is kept for a minute so that the - same game, with its rejoin token, joins again as the same member). -- The roster (who is in the room, each one's session, which one made it) goes out at a change, again 0.25 s and 0.75 s - later, and every 5 s: a datagram is lost now and then, and nothing is acknowledged. -- Limits: a source address answers a cookie challenge before the relay sends it anything larger than it sent; each - member is limited to 200 datagrams and 64 KB per second, a datagram forwarded to seven counted seven times; each - network (an IPv4 /24, an IPv6 /48) to 20 challenges a second, a join every 3 s after a burst of 20, and 8 open rooms; - each room that forwarded to someone in the last 2 s to an equal share of the forwarding (rooms that send nothing, or - send to nobody, take nothing); the relay as a whole to 5000 challenges a second and 8 MB/s of forwarding. A join - attempt costs its network one join whatever the answer, and the same attempt sent again (its answer was lost, or it - asks again for a room that is not there yet) is answered from memory without a charge while the answer is the same - refusal; the game asks for a room that is not there at most every 10 s and, while it keeps its room, waits a minute - when the relay refuses it for its network's joins instead of ending the session, so players behind one address who - wait for a room do not use up each other's joins. The relay keeps the budgets of at most 4096 networks and forgets a - budget that is full again, and checks its own challenge budget first, so a flood from spoofed addresses leaves no - state behind. A member that rejoins with its token from its own address and in its own session needs no challenge; - from another address, or with a new session (every rejoin draws one), it answers one first, so someone who saw its - CONNECT cannot take its place. The challenges for a CONNECT with a member's valid token come from that member's own - challenges (2 a second, 8 at once, kept apart for its own network and for the others), never from its packets, and - from a share set aside for rejoins: 1000 a second split equally among the open rooms for CONNECTs from other networks - than the member's, and 1000 a second split among the rooms and then among the eight member numbers of each for - CONNECTs from the member's own network (each 8 at once at least, so a member can rejoin eight times at once even on - a relay with its full 1000 rooms, and then once every 8 s). So the tokens of rooms someone made to flood with take - only those rooms' shares, and a flood of CONNECTs with a member's token, someone else's or seen on the way, from - another network neither drops the member's messages nor keeps its rejoin from its own network out of the room. What - such a flood can take is the room's share for other networks: a member that comes back from another network (a - phone that changed networks) may wait for as long as it lasts, and so may one whose own network someone sends from - with its token. A game whose roster shows - another session for it joins again, at most once - every 15 s. A session that was in the room before is refused (the last 4096 of each room are kept). A flood from many - networks can still use up the relay's 5000 challenges a second for newcomers: a relay has finite capacity, and its - operator's firewall is the place for more. -- `--host ::` listens for IPv6 as well as IPv4. The game's own socket is IPv4 and IPv6 in one; Windows may show its firewall - prompt the first time the game opens it. - -## Rooms and secrets - -A room is its secret: 18 characters of `23456789BCDFGHJKMNPRSTVWXYZ` (no vowels, no 0/1/O/I/L), typed in any case, -with or without `-` and spaces, shown `XXXXXX-XXXXXX-XXXXXX`. The game makes a new random one for each room it makes; -`coop_room` names one for `coop_auto` (tests, LAN parties). A create of a secret whose room is open on the relay is -refused (it is not a join), so nobody can be put into a room they did not mean to join. - -## Settings - -`nx2navi.ini`, section `[coop]` (`nx2navi.ini.example` has the text); the environment variable is the key with `NX2NAVI_`. - -| Key | Default | | -|---|---|---| -| `coop` | `0` | on or off, at once; off: no socket and nothing running | -| `coop_name` | empty | at most 24 bytes; empty: `Player-` and four random hex digits | -| `coop_server` | none | `host`, `host:port` or `[v6]:port`, default port 24680; with none set the layer stays idle and logs why | -| `coop_port` | `0` | local UDP port, 0 = any; read at start | -| `coop_accept` | `ask` | `ask`: nothing from a room is applied until its offer is accepted; a number (the eight hex digits the log shows, random for each start of the game): accepts that offer, set while the game runs (the file's line as it is now counts before the one read at start); `always`, from the environment only (`NX2NAVI_COOP_ACCEPT=always`; an `nx2navi.ini` line is ignored): every room is accepted (the save is still copied first) | -| `coop_auto` | `off` | `create` or `join` starts a session at launch (a join needs `coop_room`); no menu row | -| `coop_room` | empty | the room secret of `coop_auto`; no menu row | -| `coop_remove` | empty | the room's maker removes a player by its key's fingerprint (in the room or after it left; the relay drops it too while it is in the room); each value acts once; a member number is not taken from the file; the Co-op tab's Remove does the same | -| `coop_reset_pools` | empty | the room's maker resets the pools when the value changes to a new one other than `0` (1, then 2; or 0, then 1 again); the Co-op tab's Reset the pools does the same | -| `coop_drop` | `0` | test: percent of received datagrams thrown away; no menu row | -| `coop_test_set` | empty | test, environment only (`NX2NAVI_COOP_TEST_SET`): `hash=value@frame,...` sets those flags (hash in hex) at those frames of play; an `nx2navi.ini` line is ignored | -| `coop_progress` ... `coop_ammo` | see below | the rules of a room this player makes (Progress sync) | - -With `coop_auto` a room the relay lost (it restarted) is made again under the same secret by its maker, or waited for, -for as long as the session runs (asked for every 10 s; a refusal for the network's joins waits a minute); a maker who -finds it made again by someone else joins it, and the rules stay the ones its key signed. A player who leaves ends the -session. An `nx2navi.ini` someone else gave you can name a room and join it at launch (`coop_auto`, `coop_room`), but -it cannot accept the room for you: that needs the number of this start of the game, or the environment. The game reads -`coop_accept`, `coop_remove` and `coop_reset_pools` from the file as it is now once a second while it is in a room -(never out of one), as it reads the file at start (a BOM, a repeated key's later line, quotes), and skips a reading -when the file cannot be read or is empty for a moment, as while an editor saves it; values in the file at start, and -at the first reading after the start, act on nothing. - -## Progress sync - -When one player gains progress, the others' games gain it too, while they play; a player who joins later catches up -with everything the room has, and the room with what the joiner brings. Each player's own save keeps what it got: the -game saves it as its own progress. - -The game keeps its progress as flags (a database keyed by 32-bit hashes of flag names). Between two frames the runtime -reads the flags that changed, and sets the ones the room has and this game lacks through the game's own set routines, -as if the player had done it. Nothing is read or set while the game is on its title screen, loading a save or changing -scene, and not while its clock stands (the pause menu): what arrives meanwhile is set afterwards. A chest or a door -another player opened shows open the next time the area loads; an item shows in the menu the next time it opens. - -What one room merged belongs to that room and that save: leaving the room, joining another, or loading another save -forgets it. A save loaded while in a room is a new offer: nothing reaches it before you accept again, and the save is -copied again first. Once accepted, it gets the room's progress, and that holds what every save played in the room -brought, the last one's too (the others hold it); no way exists to tell one save's flags from another's in a merged -state, so the new offer and the copy are the answer. - -### What is shared - -| Category | Rule key | Modes (default first) | What | -|---|---|---|---| -| progress | `coop_progress` | `shared`, `own` | story, world and dungeon flags: events, talks, chests, doors, switches, bosses, map and compass | -| items | `coop_items` | `shared`, `own` | equipment, key items, bottles owned, wallet, bag and quiver sizes, magic meter size | -| songs | `coop_songs` | `shared`, `own` | | -| quest | `coop_quest` | `shared`, `own` | medallions, spiritual stones, the stone of agony, the gerudo card | -| keys | `coop_keys` | `pool`, `own` | small keys, per dungeon | -| rupees | `coop_rupees` | `own`, `pool` | | -| ammo | `coop_ammo` | `own`, `pool` | bombs, bombchus, arrows, deku nuts, seeds and sticks, magic beans | - -- `shared`: a flag set by anyone stays set for everyone; of a count (an item, an upgrade) the highest wins. -- `own`: not synced; switching to it stops the merge and keeps what was merged. -- `pool`: one count that every player adds to and spends from. Each player's gains and spends are counted apart (in a - record their game signs) and add up, so two players spending at once both pay; a pool that two spends at once took - below zero shows 0 and the next gain pays the debt first. A player's spends count only up to what that player gained - plus the counter's limit (999 rupees or ammunition), and never beyond what entered the pool (the highest count a - player brought, at least 0, plus every gain): nobody takes more than the pool really had, so a dungeon's key pool - loses at most the keys that dungeon has, and in a rupee pool a player who spends more than 999 rupees beyond what they - gained is not charged for the rest; a count brought below zero counts as zero, so no record turns the others' keys - into a debt. A joiner whose count is higher raises the pool to it; a game that restarts and rejoins does not add its - count again; a game that joins a pool in debt (as a drain leaves it) pays the debt with its count, so the count comes - back with the drainer's removal. A pool above a counter's capacity shows the capacity. Each game counts in its pools - only the records of the players its player accepted, and its own; it stores and passes on every admitted record, so - the games hold the same records, and games whose players accepted the same players show the same pools. A game joins - the pools once its player accepted the room and the maker's game admitted its key, which it does for each player the - maker accepted (up to 24 keys per pool reset); until then its counts stay its own. -- Small keys in the pool: a key anyone picks up goes into the dungeon's pool, a door anyone opens takes one out, and the - pickup and the door are progress, so they are gone and open for everyone. Two players who each used the last key - on a different door at the same moment open one door more than the keys paid for. -- Items, songs and quest items can be `own` only with `coop_progress = own`: in a shared world a chest one player - opened is empty for the others. With progress `own`, keys are `own` too (each world has its own doors). A room's maker - whose settings break this gets the nearest valid set, and the log says which. - -Never shared, by design or because no reliable way exists yet: - -- Where Link is, his health (the game republishes it from the player several times a second, so it cannot be set - through the flags), the magic in the meter (likewise outside the flags), the time of day (the clock runs in the - game's world manager, not in a flag), bottle contents and the equipment worn. -- Heart containers and pieces and gold skulltula tokens: most of their pickups are flags of single objects in the world - that this layer does not write, so a shared count would pay one piece or token twice; the reward flags the game names - for them stay with each player too, so everyone can collect them. -- Items the game takes away again (the trade sequence, rented masks, the knife that breaks, amiibo items): a shared - highest count would hand them back. -- Flags that the game itself resets at a scene change, a new day or a load, and counters that are not progress (puzzle - positions, minigame records, enemies defeated). - -### The room's rules - -The rules belong to the room: every game in it merges the same way. They are the settings of the player who made the -room, signed with that player's key: a game takes rules only signed by the key its player accepted (pinned at the -first acceptance in the room). Before that, a game that made the room itself takes its own; otherwise it takes the -key of the player the relay marks as the maker, but only while no other player carries rules signed by another key -(the relay's mark cannot outvote the rules the players' digests carry, and a mark the relay puts on your own name does -not make you the maker); with no maker in the room it takes the rules every member's digest carries. A member does -not sign its digest, so before you accept, someone who knows the secret and runs the relay can make every member's -digest to you carry rules of a key of its own, which your game then takes (with no maker in the room, or a maker it -controls): the offer shows the key that signs the rules, and comparing it with the maker's is what tells you (step 3 -of "Before you join a room with strangers"). When the members' rules disagree it takes -none, applies nothing, and the log and the offer say so: ask the maker which key fingerprint is theirs. No other -player's settings are used, and a member the relay marks later (after a restart, or a relay that lies) changes -nothing for a player who accepted; the offer then says that the relay names another maker. The signed rules travel -with the sync messages, so a joiner and a game that rejoins after a relay restart get them from anyone. If the maker -has left, the players keep the rules they have. The maker can change a rule during the session: switched to shared or -pool, the category merges from then on (after each player accepts the new offer); switched to own, it stops, and -nothing already merged is taken back. A maker whose game restarted keeps the room's key (from `coop-keys/` in the -profile), takes its own rules back from the others and can change them, remove players and reset the pools again. - -### Messages - -Sync rides in the room's sealed messages. Each game sends a digest (a sum per section of its state, and the rules) -every 2 s and at once to a player who joins; a section whose sum differs is sent to that player, so lost messages are -repaired and a late joiner catches up within a few seconds. A change goes out at once and again 80 and 250 ms later. -Sending is paced at 32 KB/s, under the relay's limit per member, with the players who asked for sections served in -turn (one that asks for everything all the time gets its share, not all of it), and each game takes at most 48 KB/s -from any one player. Games merge only with the same schema: this layer's version, the game's version, and the whole table of what -is shared and up to which value, so two builds that classify a flag differently do not merge. - -## The room's secret and the wire - -For a reviewer: what is derived from what, what each datagram carries and what a receiver checks. The code is -`runtime/coop_proto.c` (C) and `tools/coop_relay.py` (the relay's side, Python); `tests/coop_vectors.txt` holds byte -vectors of every message that both are checked against. - -**Library.** Monocypher 4.0.2 (`runtime/third_party/monocypher/`, two files, unmodified, BSD-2-Clause, dual-licensed -with CC0) for BLAKE2b and XChaCha20-Poly1305. It is small, has no dependencies, builds the same on Windows and Linux, -and its primitives are standard (RFC 7693, and the XChaCha20-Poly1305 construction of the IETF draft). It is used only -in `coop_proto.c`, behind four functions (`coop_room_keys`, `coop_seal`, `coop_open`, and BLAKE2b inside the first), so -another library can replace it there; the sync layer's signatures (EdDSA over Curve25519 with BLAKE2b, the library's -`crypto_eddsa`) go through `coop_sign_keypair`, `coop_sign`, `coop_sign_check` and `coop_blake2b` there too. The relay -needs no library: it uses Python's own BLAKE2b and HMAC. - -**The secret.** 18 symbols, each drawn uniformly from 27 (bytes from the operating system's random source, a byte -below 243 taken modulo 27): log2(27^18) = 85.6 bits. It is made by the client that creates the room and never sent to -the relay or written to a log; players pass it on themselves. - -**Derivation.** With `S` the 18 canonical ASCII characters as the key of BLAKE2b: - -- locator `L` = BLAKE2b-128(key `S`, "nx2navi-coop/2 room"): the relay's name for the room, and the key of the tags of - datagrams between a client and the relay. The relay and anyone who sees a CONNECT learn `L`; getting `S` from it is a - search over 2^85.6 secrets. -- message key `K` = BLAKE2b-256(key `S`, "nx2navi-coop/2 messages"): seals every message between the players. Only - holders of `S` have it. - -**The client and relay datagrams.** A 24-byte header (magic, version 2, type, the relay's room id, src, dst, length, -sequence) and an 8-byte tag, HMAC-SHA256(`L`, header[0:16] ‖ payload)[0:8]. CONNECT carries `L`, a client id (random -for each start of the game), a session (random for each join), a nonce (random for each attempt), the name, the -relay's cookie and the rejoin token. CHALLENGE, JOINED and ERROR echo the nonce and carry `L`'s tag; the client takes -only answers with its own nonce and its own `L`, so a datagram someone else sends from the relay's address neither -ends an attempt nor puts the player into another room (an untagged "protocol version" error is taken only as the -first answer of an attempt). JOINED carries a rejoin token, random per member: the relay gives a member's id (and the -maker's mark) back only to a CONNECT with it. The ROSTER lists each member's id, flags (away, maker), session and -name. KICK (maker to relay) names a member and its session. These datagrams are authenticated against outsiders who -do not know `L` only; the relay and an observer of a CONNECT can make them, which lets them disturb the session -(drop, reorder, invent members that cannot speak), not change a save. So a roster is taken only up to 2^20 ahead of -the last one's number (the relay's counter moves with its own datagrams), and after the silence time and 4 s without -a roster taken, the next one counts whatever its number, so one forged far ahead does not cut a player off; no roster -ends a session for progress sync (a session ends only when its member's identity in a later one names it, below), and -neither does a relay that says the room is gone; a roster that lists another session for this player makes it join -again at most once per 15 s, and its game keeps signing for the session it joined with. A relay or observer that keeps -sending rosters changes the roster for as long as it sends, as it can drop datagrams for as long as it likes. - -**The player message envelope** (the payload of DATA, version byte 1): - -``` -u8 envelope version (1) | u64 sender's session | u32 counter | XChaCha20-Poly1305 ciphertext of (u8 kind, message) | u8[16] tag -nonce (24 bytes) = session (8, LE) | counter (4, LE) | sender's member id (1) | 11 zero bytes -associated data = destination member id, envelope version -``` - -A session is drawn for every join and the counter starts at 1, so a nonce never repeats under `K` (two sessions -collide with probability about n²/2^65). The kind byte and the message are inside the ciphertext: a new kind of message -(a pose, say) needs only a number, and a later envelope version can change the outer fields without touching the -messages. A message is at most `COOP_MAX_MESSAGE` = 1146 bytes after its kind. - -**What a receiver checks, in order:** the sender is in the roster and is not itself, the destination is it or -everyone, the envelope's session is the one the roster lists for that sender, the counter is new within a window of -the last 64 of that session, the tag opens with `K` for that sender and destination; only then is the counter recorded -and the message delivered. A counter older than the window is refused, unless it opens and is the fourth of that -sender's messages in a row below the window, each later than the one before: then the window starts again from it and -it is delivered. `K` is the whole room's, so whoever knows the secret can seal one message in a member's name with a -counter far ahead, which leaves that member's later messages below the window; the member's next four start it again, -and the first three of them are lost. That is per forged message: one sent again after every three of the member's -keeps that member's messages from a player for as long as it keeps sending. - -**Replay.** A message recorded before a player joined again carries the old session, which the roster no longer -lists: refused. Within a session the window refuses only what it still remembers: a message sent again within the -last 64 it took is refused, and an older one alone is refused too. But four older ones in a row, each later than the -one before, start the window again (that is how it recovers from a counter far ahead), and from then on the window -has forgotten what it took: anyone who recorded a member's messages of its present session (the relay too, without -the secret) can have every one of them delivered again, in order. Someone who knows the secret can also seal a -recorded message again under a new counter. So the envelope does not make a message arrive once; a layer for which a -repeat matters checks a counter of its own. A sync section carries its sender's own counter inside its signature for -that: a section is taken once, and only within the last 64 of its sender's session; a digest taken again repeats what -it said (until its sender's next one, 2 s later). A relay that lists an old session in a forged roster could replay -that session's messages to a player whose game never saw the session end; they are messages the sender really signed, -and sync messages only raise values, so this brings nothing new into a save. - -**The sync messages (version 4).** `runtime/coop_state.c` has the byte layout. Each game makes a signing key for each -room and start of the game (EdDSA, from a random value of the start and the room's secret, so nothing ties a player -across rooms; the maker's from 32 random bytes kept in the profile for that room, mixed with the secret, so that it -outlives a restart) and sends in every DIGEST its identity: the key, its name, the session it held in the room before -this one (0 none) and a signature over the room's context (BLAKE2b-128 keyed with the secret), its member id, the -session it joined with and that session before. A game takes values only from a member whose identity checks out; an -identity belongs to one session, a session a member's later identity names as the one before is never taken again -(the latest 1024 of each key the player accepted are kept apart, where no other member's churn removes them; the -others share the last 65536), and a key another member holds is refused, so an identity cannot be copied to another -member or slot; the relay also refuses a session that was in the room before. An identity of another key for a -session that is identified already (only one of them is the member's) makes the game take that member as neither -while both are presented; once one of them has not been presented for 10 s, the other takes the place. Only the key's -holder ends its sessions -that way: a forged roster, a relay that says the room is gone, or a replayed message or identity ends none. A game -announces its identity at once when its session changes. A name is checked as signed and shown cleaned (control and -reordering characters replaced). Every SECTION (a push of a change or an answer to a digest) ends with its sender's -counter (8 bytes) and its signature (64 bytes) over "nx2navi-coop/4 section", the room's context, the sender's -member id and session and the section's bytes, checked against the identified key before anything of the section is -stored, applied or passed on: 72 bytes and one signature per section whatever it carries, about 35 µs to sign and -100 µs to check one (measured on the runtime's -O2 build of Monocypher on an x64 desktop). Flags are taken only from a -member whose key the player accepted. The DIGEST also carries the -maker's charter: the rules, the keys admitted to the pools (8-byte key ids), the last 16 keys removed and the pools' -epoch, with an epoch the maker raises at each change (saturating at 2^32−1), signed by the maker's key; each game -keeps every removal it saw in the room. A pool record (one per contributor and pool) carries the contributor's key, -the pools' epoch, a sequence number, its base, gains and spends, and its signature; a game keeps the record with the -highest sequence of each key (its own too: a room that comes back hands it its own newest record) and stores only -records of admitted keys of the current pools' epoch, checking the signature before it stores or passes one on; its -pools count only the records of keys its player accepted, and its own. A game remembers (in memory) its last 8 joins -of each pool in the epoch, each with what it joined with and the records it saw then; its base is the highest of -their claims, each made as if the game had counted then the keys it counts now, and made again whenever that changes -(a removal, or its player accepting more). An older join keeps the claim it had when the ninth was made, so after -nine saves loaded in one pools' epoch (a game over and continue counts) a removal leaves that claim as it was until -that save is loaded again. A reset carries this game's own claim as it would be with only the players the reset -admits, and the game signs it at once as its record in the new epoch (a game the reset does not admit claims it when -it joins). A digest's sums are not signed: a forged one can make a game send its sections or join a pool before it -heard every record, as dropping messages can. - -**Rules and the maker.** A game takes a charter signed by the key its player accepted; before any acceptance, its own -for a room this session made; else by the key of the member the relay marks as the maker while no identified member -carries a charter of another key; else, with no maker in the room, by the one key every identified member's charter -carries (each member's first digest is waited for, up to 5 s, so the quickest does not win). A charter of the same key -counts only with a later epoch. - -**Per-member limits in each game.** Every value is checked against the local table before it is stored, passed on or -applied; at most 48 KB/s is taken from one member; 32 refused flags, or pool records whose contributor's signature -fails, in sections that member signed, and the member is ignored while it stays. Nothing that fails inside a digest -counts against the member it names (an identity, a charter in the maker's name), and neither does a section whose -signature fails: anyone with the secret can send either in its name. A pool record this game cannot count is dropped -without counting against its sender, since an honest member may hold a newer charter for a moment. A signature check -that failed is not made again for the same signature, key and signed bytes (a changed copy of a genuine message fails -on its own and leaves the genuine one to be checked), and each member gets at most 128 signature checks a second (256 -at once), sections' included; -the register index is keyed (SipHash-2-4 with a random key of the process); a pool stores at most 24 records (the -admitted keys), a record's base, gains and spends are at most 2^20 either way, and nobody but a record's key holder -can make or change it. Without the system's random source (no client id, session, nonce or key a stranger cannot -guess) co-op stays off. - -## Before you join a room with strangers - -1. **Keep a save you can go back to.** Join only with a save you are ready to restore from - `coop-backup/first-`. Anyone you accept can set story flags in your save, and so can anyone a player you - accepted let in. A player in your pools can empty a dungeon's small keys while they play; the maker's removal of - that player, or a pool reset after they were removed or left, gives back what they took, but no removal takes a - flag back, and a reset while they are still in the room keeps their drain: remove first, then reset. -2. **Your acceptance is yours, the room is not.** A player you did not accept reaches neither your flags nor, by - themselves, your pools, also when they know the secret and run the relay. But the players you accept pass on the - flags of the players they accepted, a player you accepted who counts a stranger in their pools claims against a - pool with that stranger in it until the maker removes them, and the maker alone decides who may use the pools at - all. If anyone you do not know is in the room, even one you declined, do not accept: leave, and ask the maker for a - new room (a new secret) for the people you know. -3. **Check the keys, and keep yours to yourself.** Before you accept, compare the fingerprint of the key that signs - the room's rules with the maker's, and each player's with that player's, by voice or chat: a name is what a member - says it is, and someone who knows the secret and runs the relay can hold back a player's own identity and show its - own key under that player's name and member number until that player's arrives. Never start the game with - `NX2NAVI_COOP_ACCEPT=always`, - or with a script or `nx2navi.ini` someone hands you. Never hand out a copy of your profile folder: its `coop-keys` - folder makes whoever has it, with a room's secret, the maker of that room (your game logs a member that presents its - key). - -What nothing can prevent: everyone who knows the secret can join and read the room; a member or the relay can disturb -it (leave messages out, delay them, send one again as "Replay" says, flood within its limits, keep the rules from -being agreed, change the offer so that nothing is applied until you accept again, keep the room unaccepted by never -saying who it is, send rosters or identities that keep a player unidentified for as long as it keeps sending, or seal -messages with a counter far ahead, each of which costs a player three of a member's messages); and the relay sees who plays -with whom and when, with names and addresses. None of that changes a save. - -## Other players in the world - -Not in this build. The protocol has a message for it (POS in `runtime/coop_proto.h`: the area, a position, a facing -and what Link's animation slots play), and its codec and its counter against replayed poses are in the build and -checked by `--probe-coop`, but nothing sends one and nothing draws another player: a POS that arrives is dropped. So -no position leaves your PC, whatever the settings. - -## In the log - -State changes only: `coop: connecting to the relay`, `the relay answered in N ms`, `room XXX… created`, `joined room -XXX… as member N`, ` (member N) joined the room` and `left the room`, `relay lost; rejoining`, `left room ()`. -A cause that repeats (a message that does not open, a replay, no server) logs once per 30 s; a counters line follows -every minute in a room. Peer addresses are never logged; the relay's only with `NX2NAVI_TRACE=coop`, which also adds a -line per datagram. - -Progress sync: `coop: sync: playing a save: N progress flags, ...` when a save is being played (and `... nothing is read -or written until a save is played` when it stops), `a room to accept; in the room: ...; rules: ...` followed by `put -coop_accept = into nx2navi.ini [coop]` (or, while a member is not identified, `N member(s) in the room are -not identified`; when the members carry rules of different keys, a line that says so), `room accepted`, `the save -as it was before this room is in `, `the key this game signs the room's rules with is kept in ` (the -maker), `the room's rules: progress shared, ...`, `took N values from the room (progress a, items b)` at most every -5 s, `N values from the room were refused`, `member N sent values this game does not take; nothing more is taken from -it`, for the maker `key K (member N, name | not in the room) took N keys ... from the pools beyond what it brought; -coop_remove = K removes it`, `key K removed`, `coop_remove = N in nx2navi.ini names a member number` (not taken), `the -pools are reset`, `member N presents this game's own key`, `member N presents another key than the one it was identified -by in its session`, `member N (name, key K) presents a key you accepted as member M | under the name ...: under this -name it is a new offer`, and `the room's key file ... is damaged | cannot be read`. -`NX2NAVI_TRACE=coop` adds `sent`, -`received` and `applied` lines by category, hash and value. With `coop = 0` nothing of this is logged and the game's -frames do no more than check the setting. - -## The interface for the layers above - -`runtime/coop.h`: `coop_send(kind, bytes, n)` and `coop_send_to(member, ...)` send a sealed message to the others (it is -a datagram: it may be lost, arrive out of order or, recorded, arrive again: see "Replay"), `coop_on_message(kind, fn, user)` receives -them on the coop thread, `coop_on_view(fn, user)` reports the state and the roster (with each member's maker mark and -whether its messages opened), `coop_view_get(&view)` copies them, `coop_command(COOP_CMD_CREATE | COOP_CMD_JOIN | -COOP_CMD_LEAVE, secret)` drives the room, `coop_remove_member(id)` is the maker's removal (progress sync signs it -through `coop_on_remove`). `runtime/coop_offer.h` holds the menu's calls, none of which waits for the sync engine (what -they read is a copy the coop thread publishes at each step, what they ask it carries out at its next step): -`coop_game_offer(&offer)` and `coop_game_accept(number)` show a room and accept it (`CoopOffer` in -`runtime/coop_state.h`: each member's signed name, key fingerprint, whether it is identified or removed, how many -members never identified (the room cannot be accepted while there are any), whether the members' rules disagree, the -rules and the key that signs them, whether another save is played and whether the copy is made), -`coop_game_reset_pools()` is the maker's pool reset, `coop_game_remove_key(fingerprint)` the maker's removal of a key -(also of a player who left; a member in the room with that key is removed at the relay too), and -`coop_game_contributors()` lists the pools' contributors with what each took beyond what it brought, per pool and in -the room or not (a player who used keys others found is among them). `CoopView.made` is this session's own creation of -the room, `CoopView.name` the player's own name. The wire format is `runtime/coop_proto.h`. Progress sync uses -`coop_on_tick(fn, user)` for its timers. - -## Checks - -- `python tests/coop_relay_test.py`: the relay (rooms, rejoin with the token, timeouts, limits per member, per network - and in all, removal by the maker, bad tags, a stolen client id, challenge and room floods, fan-out, garbage from a - member, the log without secrets, a join attempt asked again without a charge, two players behind one address who - wait ten minutes for a room, a rejoin during a challenge flood, rooms that send to nobody, a rejoin while the members - of four other rooms flood with their tokens, and a rejoin on a relay with its full 1000 rooms while someone floods - with every token of the room and a member of it with its own) on a fake clock and over - UDP, and the codec against the vectors. With `NX2NAVI_EXE=/ocarina` it also runs the probe against a relay it - starts (with `--log-joins`, whose log a failure shows), with and without 30 % loss, and restarts that relay in the - middle. -- `ocarina --probe-coop`: the sockets, the codec and its cryptography (known answers, a sealed message that does not - open when changed or sent by another), a fuzz of every parser, the POS message's counter (a pose sent again, one - recorded 35000 poses earlier, another session in the same member number, one forged far ahead), which members count - as shown (only those accepted and identified), and two sessions in - one process that make and join a - room, exchange messages, take garbage and forgeries from a hostile relay (a changed message, one it sealed itself, - a replay after a rejoin, lies while a player connects), lose the relay and find each other again, and in which the - maker removes a player. Needs no window, GPU or game files. It runs its own small relay; - `NX2NAVI_COOP_RELAY=host:port` uses a real one, `NX2NAVI_COOP_DROP=30` drops that share of what the sessions - receive, `NX2NAVI_COOP_VECTORS=tests/coop_vectors.txt` checks the byte vectors. Its sync part runs the merge engine - between fake games on a fake network: merging, late joiners, loss, the rules and pools, nothing carried from one room - or save to the next, a hostile member (refused values, pool records it did not sign or that nobody admitted, its own - records against a pool, removal), identities (a removed player back, a copied identity, a second name, the accepted - list), the offer's number, rules that wait for acceptance, another save in the room, the maker's pinned key, an - honest base above the limit, fair answers, the third review's cases: flags only from accepted keys (through - another member, after a declined member left or was removed, from a member that never identified), a key drain with - the maker's game restarted and the absent drainer removed, a pool reset, an identity replayed after 64 sessions or a - room the relay lost, the relay's maker mark against the members' rules, signed names, failed signatures, a game's own - pool record after the room came back, and a removal 17 removals ago; and the fourth review's: a record with the - lowest base, pools that follow the player's own acceptance, two resets in a row, a forged roster's sessions, what a - contributor took per pool, an accepted key's ended session after 65600 others; and the fifth review's: sections in - another member's name made with the secret alone (and a flood of them), a member's session after a forged roster - with a replayed message, a roster without it, a relay that says the room is gone, and a replayed identity of a - session the game never saw end, claims made again after a removal (keys brought while a removed player's gains - counted, a claim made while its spends counted, honest games that agree, a game at its title screen), another save - that joins only once accepted; and the sixth review's: digests in a member's name whose charter does not check out, - a changed copy of a member's digest and of the maker's charter sent first, another key's identity for a member's - session sent first, and a reset's carry with a player who comes after it and with a removal in the same charter; - and the seventh review's: a key the player accepted in another member's place under that member's name, and a relay - restart that hands the players other numbers; - and a randomised run (a few seeds by default; `NX2NAVI_COOP_ADVERSARY=first:count` runs those seeds alone, - `NX2NAVI_COOP_ADVERSARY_TRACE=1` prints its events and the engines' lines) of hostile members who hold the secret, - admitted or not, and of an attacker with the secret and no accepted key who forges senders and rosters, replays - messages and identities, sends changed copies of digests and sections and identities of its own key in a member's - place, against invariants of flags, acceptance, identification, ignored members, the maker's charter and a ledger - of the honest players' joins, carries, pickups and uses. The session part adds rosters forged - far and a little ahead, rosters that list another session again and again, a message in a member's name sealed with - the room's key 2^31 − 1 ahead, a refusal for the network's joins, and a - room that is not there. `ocarina --probe-fs` checks that the first copy - of a save outlasts the rotation. -- `python tests/coop_page_check.py`: the menu's Co-op tab without the game, driven with the keyboard's and the pad's - events: typing a name, a relay and a secret (Q and E are letters there, not tab keys), Make, Join with a wrong secret - and a pasted one, the offer accepted and declined, the maker's key to compare, the three warnings before the first - Accept only, a player not identified (Accept greyed, removed by member number only while the same session holds - it), a key accepted under another name (said in the offer, the row and the Accept window), Remove by key, a pool - contributor's key removed, Reset the pools, Leave, the relay's join limit and a lost room, - the maker's rules and the others' read-only ones; Accept, Remove, Reset and Leave never act on a second press, a held - button or a double tap, and a new offer under an open Accept window puts the cursor back on Not now. It then runs - again with a deliberate fault (each such window opens with the cursor moved onto its acting button) and passes only - when that run fails. Needs a C++ compiler only. diff --git a/docs/releases/v0.9.3.md b/docs/releases/v0.9.3.md index bbb568f3..74f0055a 100644 --- a/docs/releases/v0.9.3.md +++ b/docs/releases/v0.9.3.md @@ -27,3 +27,7 @@ - **The other experimental performance settings have rows there too**: *Helpers work ahead* (`replay_pipe`, 0 to 4), *Helpers take small jobs* (`replay_small`, 0 to 64) and *Start next frame early* (`tail_early`). A row whose setting does nothing without another one is greyed out and says which it needs. +- **The hidden co-op beta is gone.** The co-op of 0.8.0 (shown only with `coop_beta = 1`) shared story progress + through a relay a player had to run and never showed the other players. Its code, its settings (the `[coop]` section + of nx2navi.ini), its relay script and its menu tab are removed; lines of it left in an nx2navi.ini do nothing, and a + bug report still keeps their values out. A new co-op is being built in its place. diff --git a/launcher/saves.py b/launcher/saves.py index 5bf97074..f5c08d6b 100644 --- a/launcher/saves.py +++ b/launcher/saves.py @@ -393,8 +393,8 @@ def _fresh_file(folder, stem, suffix): NO_SAVE = 'the game has no save yet: play until it saves, then export' -# Settings whose values stay on the player's PC (runtime/cfg_private.h has the same list): a co-op room's secret and -# the offer number made for it, the relay's address, the player's name, the key a maker removes, the menu's test hook. +# Settings whose values stay on the player's PC (runtime/cfg_private.h has the same list): the menu's test hook, and +# what the co-op of versions up to 0.9.2 kept in nx2navi.ini, which a player's file may still hold. PRIVATE_SETTINGS = ('coop_room', 'coop_server', 'coop_name', 'coop_accept', 'coop_remove', 'menu_test_set') PRIVATE_SHOWN = b'(set, not shown)' diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 547cf14d..c4385f08 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -637,71 +637,6 @@ # The key of the application's uploaded art asset (Rich Presence > Art Assets) shown as the status's large image. # discord_image = nx2navi_512x512 -[coop] -# Co-op: your own game and save, with the other players' progress shared through a relay (docs/coop.md). Positions are -# not shared yet: you do not see the other players in your world. -# The menu's Co-op tab sets the name, the relay and the rules below, and makes, joins, accepts and leaves a room. -# 0 = off: no socket is opened, no name is looked up and nothing is sent. Takes effect at once. -# coop = 0 -# Co-op is a beta: the menu shows its Co-op tab only with coop_beta = 1, which no menu row sets. Takes effect at once. -# coop_beta = 0 -# Your name for the other players, at most 24 bytes. Default: Player- and four random hex digits. -# coop_name = -# The relay, host or host:port (default port 24680): run one with `python tools/coop_relay.py` and forward its UDP port, or -# use one a friend runs. No default: with none set the session stays idle and the log says why. -# coop_server = -# The local UDP port, 0 = any. For a firewall rule or a port forward when testing on a LAN. Takes effect at the next start. -# coop_port = 0 -# For tests and LAN parties, no menu row: coop_auto = create or join starts a session at launch (a join needs coop_room, the -# room's secret of 18 characters from 23456789BCDFGHJKMNPRSTVWXYZ, XXXXXX-XXXXXX-XXXXXX; a create without it makes a new -# one). While the session runs, a room the relay lost (a restart) is made again or waited for (asked for every 10 s; a -# refusal for too many joins from your network waits a minute), and a create that finds the room open joins it. Joining -# is not accepting: nothing from the room is applied before you accept it (coop_accept). -# off = no session at launch. -# coop_auto = off -# coop_room = -# Test: percent of received co-op datagrams thrown away, 0 to 100. -# coop_drop = 0 -# The rules of a room you make (a room you join uses its maker's; the log names them). Take effect at once. -# Story, world and dungeon progress: shared or own. -# coop_progress = shared -# Equipment, key items, upgrades; songs; medallions, stones and the other quest items: shared or own. Own only with -# coop_progress = own: in a shared world a chest one player opened is empty for the others. -# coop_items = shared -# coop_songs = shared -# coop_quest = shared -# Small keys: pool = every key anyone picks up goes into the dungeon's pool and every door anyone opens takes one out of it; -# own = keys stay with the player who picked them up (doors still open for everyone). Own with coop_progress = own. -# coop_keys = pool -# Rupees and ammunition: own, or pool = one count every player adds to and spends from. -# coop_rupees = own -# coop_ammo = own -# Nothing from a room is applied to your game before you accept who is in it (by their keys) and its rules, and before -# your save is copied to coop-backup in the profile folder (docs/coop.md: going back). ask = the log shows each room's -# offer with a number (eight hex digits, new at each start of the game); accept it on the menu's Co-op tab, or put that -# number here while the game runs to accept that offer (this line as the file is now counts, before the one read at -# start). A new member, other rules or another save loaded make a new offer with a new number (another save joins the -# pools only once you accept it). A member who never says who it is keeps the room from being accepted. Your key pools -# count only you and the players you accepted, among those the room's maker lets use them. Every message that can -# change your save is signed by the player who sent it, so someone who knows the room's secret and runs the relay (or -# sends from its address on your network) can read and disturb the room but not set flags in a player's name. -# Accepting every room (always) is taken from the environment only (NX2NAVI_COOP_ACCEPT=always), never from this file, -# so an ini someone hands you cannot accept a room for you. -# coop_accept = ask -# The room's maker (the Co-op tab's Remove and Reset the pools do the same): coop_remove = a player's key fingerprint -# (xxxx-xxxx-xxxx-xxxx, as the log and the tab show it beside each member; without dashes it may be all digits), in the -# room or after it left; every game drops that key and what it took from the pools, and keys others brought while it -# was in a pool stay theirs. A member number here is not taken: numbers pass to other players as members come and go. -# coop_reset_pools = a new value (1, then 2, or 0 and then 1 again): every game joins the pools again with what they -# held for the players in the room, and what each held counts from the reset on. A player who drained a pool and is -# still in the room when you reset keeps what it took: remove it first, then reset (both in one save of this file act -# in that order). -# Both are read from this file once a second while you are in a room, and each value acts once. The key your game signs -# the rules of a room you made with is kept in the profile folder (coop-keys, readable by your account only), so a -# restart keeps you the room's maker; never hand out a copy of the profile folder. -# coop_remove = -# coop_reset_pools = - [audio] # none mixes without opening an audio device. # audio = default @@ -833,7 +768,7 @@ # cheat_0123abcd = 1 [diagnostics] -# Extra lines in the log, comma separated: svc, host, ipc, fs, gfx, perf, sched, audio, present, coop or all. The menu's +# Extra lines in the log, comma separated: svc, host, ipc, fs, gfx, perf, sched, audio, present or all. The menu's # Performance log sets perf (frame timings). Default: none. # trace = perf # A game thread that waits this many seconds for a lock, or for another thread to end, is logged once with the thread diff --git a/runtime/boot.c b/runtime/boot.c index 76f7b984..15ed6cd4 100644 --- a/runtime/boot.c +++ b/runtime/boot.c @@ -16,7 +16,6 @@ #include "cfg.h" #include "cheats.h" -#include "coop.h" #include "fs.h" #include "kernel.h" #include "ipc.h" @@ -324,7 +323,6 @@ void rt_frame_presented(const tex_t *t, uint64_t n) { stats_line(); fflush(stdout); fflush(stderr); - coop_shutdown(); // LEAVE and BYE to the room, before the process ends fs_save_flush(); vk_pipeline_prewarm_marker_clear(); // an orderly end, not a driver crash in the boot's builds (vk_pipeline.c pm_skipped) vk_pipeline_save_exit(); // no-op without the Vulkan back end diff --git a/runtime/cfg.c b/runtime/cfg.c index a05450b4..a08defaf 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -13,7 +13,6 @@ #include "cfg.h" #include "cfg_private.h" -#include "coop_app.h" #include "discord_app.h" #include "host.h" #include "platform/platform.h" @@ -81,12 +80,11 @@ static const char *const known[] = { "TEXUP_LOG_MS", "DYN_RES", "DYN_RES_TARGET", "DYN_RES_STEP", "TEXTURE_DETAIL", "RECORDER_CORE", "RECORDER_PRIO", "REPLAY_TIME", "LOCK_TIME", "POOL_CREATE_BUDGET", "POOL_CREATE_MS", "POOL_FINI_IDLE", "POOL_LOAD_BACKLOG", "POOL_LOAD_MS", "VMA_BLOCK_MB", "SYNC_WAKE_ALL", "LOG_ASYNC", "DEVICE_LOST_AT", "STAGING_FAIL_AT", "VRAM_BALLAST_MB", - "COOP", "COOP_BETA", "COOP_NAME", "COOP_SERVER", "COOP_PORT", "COOP_ROOM", "COOP_AUTO", "COOP_DROP", // co-op (coop.c); the last three have no menu row - "COOP_PROGRESS", "COOP_ITEMS", "COOP_SONGS", "COOP_QUEST", "COOP_KEYS", "COOP_RUPEES", "COOP_AMMO", // the room's rules (coop_game.c) - "COOP_TEST_SET", // test: sets flags by hash at given frames (coop_game.c); no menu row - "COOP_ACCEPT", // accepts a co-op room's members and rules before anything of it is applied (coop_game.c) - "COOP_REMOVE", // the room's maker removes a member: its id (coop.c), or a key's fingerprint (coop_game.c); no row (the Co-op tab's Remove) - "COOP_RESET_POOLS", // the room's maker resets the pools when it changes to a value other than 0 (coop_game.c); no row (the tab's Reset the pools) + // retired: the settings of the co-op of versions up to 0.9.2; read by nothing, listed so that a file which still + // sets them logs no unknown key + "COOP", "COOP_BETA", "COOP_NAME", "COOP_SERVER", "COOP_PORT", "COOP_ROOM", "COOP_AUTO", "COOP_DROP", + "COOP_PROGRESS", "COOP_ITEMS", "COOP_SONGS", "COOP_QUEST", "COOP_KEYS", "COOP_RUPEES", "COOP_AMMO", + "COOP_TEST_SET", "COOP_ACCEPT", "COOP_REMOVE", "COOP_RESET_POOLS", }; // The nx2navi.ini section a key cfg_set may write goes to when the file lacks it (nx2navi.ini.example's sections). @@ -112,9 +110,6 @@ static const struct { const char *key, *section; } sections[] = { {"FRAMEGEN", "display"}, {"FRAMEGEN_MULTIPLIER", "display"}, {"FG_MULT", "display"}, {"DEVICE_PROFILE", "display"}, {"ASSUME_FOCUS", "display"}, {"DISCORD_RPC", "discord"}, {"DISCORD_APP_ID", "discord"}, {"DISCORD_IMAGE", "discord"}, - {"COOP", "coop"}, {"COOP_BETA", "coop"}, {"COOP_NAME", "coop"}, {"COOP_SERVER", "coop"}, {"COOP_PORT", "coop"}, - {"COOP_PROGRESS", "coop"}, {"COOP_ITEMS", "coop"}, {"COOP_SONGS", "coop"}, {"COOP_QUEST", "coop"}, {"COOP_KEYS", "coop"}, - {"COOP_RUPEES", "coop"}, {"COOP_AMMO", "coop"}, {"COOP_ACCEPT", "coop"}, {"SHADOW_SCALE", "display"}, {"SHADOW_RANGE", "display"}, {"CONTACT_SHADOWS", "display"}, {"PCSS", "display"}, {"MODS_DIR", "mods"}, {"MODS_ORDER", "mods"}, {"MODS_DISABLED", "mods"}, {"MODS_REBUILD", "mods"}, @@ -245,18 +240,6 @@ static const CfgMeta metas[] = { {"DISCORD_RPC", "1", "0|1", LIVE}, {"DISCORD_APP_ID", NX2NAVI_DISCORD_APP_ID_DEFAULT, NULL, LIVE}, {"DISCORD_IMAGE", NX2NAVI_DISCORD_IMAGE_DEFAULT, NULL, LIVE}, - // Co-op (coop.c): off until the player turns it on. The server is host[:port] of a relay (tools/coop_relay.py), and has no - // default until coop_app.h names one; the name is at most 24 bytes, empty for Player-xxxx; the port is the local UDP port - // (0: any). COOP_ROOM, COOP_AUTO, COOP_DROP and COOP_TEST_SET are read with cfg_get, no row. - {"COOP", "0", "0|1", LIVE}, {"COOP_BETA", "0", "0|1", LIVE}, {"COOP_NAME", "", NULL, LIVE}, - {"COOP_SERVER", NX2NAVI_COOP_SERVER_DEFAULT, NULL, LIVE}, {"COOP_PORT", "0", "0..65535", RESTART}, - // The rules a room made by this player syncs by (coop_state.h, docs/coop.md); a joiner's own are not used in the room. - {"COOP_PROGRESS", "shared", "shared|own", LIVE}, {"COOP_ITEMS", "shared", "shared|own", LIVE}, - {"COOP_SONGS", "shared", "shared|own", LIVE}, {"COOP_QUEST", "shared", "shared|own", LIVE}, - {"COOP_KEYS", "pool", "pool|own", LIVE}, {"COOP_RUPEES", "own", "own|pool", LIVE}, {"COOP_AMMO", "own", "own|pool", LIVE}, - // Nothing from a room is applied before its members and rules are accepted (coop_game.c): ask (the log names the - // offer's number, which this setting or the menu accepts), always (every room, the save still copied first), or a number. - {"COOP_ACCEPT", "ask", NULL, LIVE}, // Advanced: scheduling {"ASYNC_PREEMPT", "1", "0|1|2", RESTART}, {"REPLAY_ASYNC", "0", "0|1", RESTART}, {"REPLAY_WORKERS", "0", "0..16", RESTART}, {"REPLAY_PIPE", "0", "0..4", RESTART}, @@ -1105,7 +1088,7 @@ static void log_file_settings(void) { char key[64]; snprintf(key, sizeof key, "%s", entries[i].key); for (char *p = key; *p; p++) *p = (char)tolower((unsigned char)*p); - // a co-op room's secret, the relay, the player's name: only that they are set (cfg_private.h) + // a private setting (cfg_private.h): only that it is set const char *value = cfg_private_key(entries[i].key) && entries[i].value[0] ? CFG_PRIVATE_SHOWN : entries[i].value; size_t vl = strlen(value); if (vl > 200) vl = 200; // a long value (a list of mods) is cut: the line shows that the key is set @@ -1137,43 +1120,6 @@ static void load_ini(void) { } } -int cfg_ini_read(const char *path, const char *key, char *out, size_t cap) { - if (cap) out[0] = 0; - uint64_t size = 0; - uint8_t *data = plat_file_read(path, &size); - if (!data || !size) { // gone or empty for a moment (an editor saving it), or locked: no reading, not "no value" - free(data); - return -1; - } - int found = 0; - char *p = (char *)data; - if (size >= 3 && !memcmp(p, "\xEF\xBB\xBF", 3)) p += 3; // as cfg_load: editors add a BOM - for (;;) { - char *eol = strchr(p, '\n'); - if (eol) *eol = 0; - char *s = trim(p), *eq = strchr(s, '='); - if (*s && *s != '#' && *s != ';' && *s != '[' && eq) { - *eq = 0; - const char *k = trim(s); - size_t i = 0; - while (k[i] && key[i] && tolower((unsigned char)k[i]) == tolower((unsigned char)key[i])) i++; - if (!k[i] && !key[i]) { // as cfg_load: a repeated key's later line wins, quotes go, "key =" sets nothing - char *v = trim(eq + 1); - size_t vl = strlen(v); - if (vl >= 2 && v[0] == '"' && v[vl - 1] == '"') v[vl - 1] = 0, v++; - snprintf(out, cap, "%s", v); - found = *v != 0; - } - } - if (!eol) break; - p = eol + 1; - } - free(data); - return found; -} - -int cfg_ini_now(const char *key, char *out, size_t cap) { return find_ini() ? cfg_ini_read(ini_path, key, out, cap) : 0; } - void cfg_init(void) { load_ini(); __atomic_store_n(&lowend, -1, __ATOMIC_RELEASE); // asked before the file was read: decided again (lowend_of) diff --git a/runtime/cfg.h b/runtime/cfg.h index 75fa8db8..741d625e 100644 --- a/runtime/cfg.h +++ b/runtime/cfg.h @@ -24,13 +24,6 @@ unsigned cfg_version(void); int cfg_setenv(const char *name, const char *value); // 1 when the environment sets name: cfg_set cannot change what cfg_get returns (the menu greys the setting out). int cfg_from_env(const char *name); -// The value of key, as nx2navi.ini spells it ("coop_accept"), in nx2navi.ini as the file is now: the file is read again, -// for the few settings that act while the game runs without the menu (co-op's acceptance and the room maker's -// remedies; everything else is read at start). Read as at start: a BOM is skipped, a repeated key's later line wins, -// quotes go and "key =" sets nothing. 1 and the value in out when the file sets it, 0 when it does not, -1 when the -// file cannot be read now or is empty (an editor saving it, a lock): the caller skips that reading. -int cfg_ini_now(const char *key, char *out, size_t cap); -int cfg_ini_read(const char *path, const char *key, char *out, size_t cap); // the same, of the file at path // 1 when the player set name (the environment or nx2navi.ini), 0 when cfg_get gives a device profile's default or NULL. int cfg_is_set(const char *name); int cfg_low_end_active(void); // the low-end GPU mode was on when the game started (cfg.c LOWEND) diff --git a/runtime/cfg_private.h b/runtime/cfg_private.h index 6da440cb..a6bb5b7d 100644 --- a/runtime/cfg_private.h +++ b/runtime/cfg_private.h @@ -1,5 +1,6 @@ -// Settings whose values stay on the player's PC: a co-op room's secret and the offer number made for it, the relay's -// address, the player's name, the key a maker removes, and the menu's test hook (which can carry a secret). The log's +// Settings whose values stay on the player's PC: the menu's test hook, and what the co-op of versions up to 0.9.2 kept +// in nx2navi.ini, which a player's file may still hold (a room's secret and the offer number made for it, the relay's +// address, the player's name, the key a maker removed). The log's // line of what nx2navi.ini sets (cfg.c log_file_settings), "Copy system info" (sysinfo.c) and the copy of nx2navi.ini a // bug report takes along (save_share.c; launcher/saves.py has the same list) show only that such a key is set. // Header only, so that the tests that build one of those files alone need nothing else. diff --git a/runtime/coop.c b/runtime/coop.c deleted file mode 100644 index 092f7d32..00000000 --- a/runtime/coop.c +++ /dev/null @@ -1,1354 +0,0 @@ -// Co-op session layer (coop.h): the state machine of one player's connection to a relay, and the thread that runs it. -// -// States: OFF (the COOP setting is off), LEFT (on, in no room), CONNECTING (reaching the relay to create or join a room), -// JOINED, RECONNECTING (the relay went silent; joining the same room again as the same member). -// -// A room is its secret (coop_proto.h): the relay knows it by the locator derived from it, and the players seal their -// messages with the message key derived from it, so the relay forwards what it cannot read, change or make. -// -// Reaching the relay is a cookie exchange (tools/coop_relay.py): a CONNECT is answered by a CHALLENGE with a cookie, a -// CONNECT that carries the cookie is answered by JOINED or an ERROR. Every answer carries the nonce of the attempt and -// the locator's tag, so a datagram someone else sends from the relay's address does not end or redirect the attempt. -// A burst of four CONNECTs 250 ms apart carries the exchange over a lossy path; a burst without an answer is followed by -// a wait of 1, 2, 4, 8, then 15 s. A session that keeps its room (retry) asks for a room that is not there at most every -// 10 s, and waits a minute when the relay refuses it for its network's joins, instead of ending. In a room a PING every -// 2 s keeps the NAT mapping open and measures the round trip, and 8 s without any datagram from the relay is -// RECONNECTING. JOINED hands out a rejoin token, which a later CONNECT of the same member carries: the relay gives a -// member's id back only to it. -// -// Every join is a new session: a random number the CONNECT tells the relay, which lists it in the roster. A player's -// messages carry its session and a counter from 1 in the sealed envelope; the others take a message only of the session -// the roster names for its sender and once within a window of 64 counters, so a message recorded before a rejoin, or -// sent again within the window, is refused. Four older ones in a row start the window again (window_below), after which -// a recorded message of that session can come again: a layer that must take a message once checks a counter of its own. -// -// One thread owns the socket, the DNS lookup and the state. Other threads reach it through a mailbox (commands, settings), an -// outbox (messages to send) and a copy of the state they may read; all three sit behind one lock that the thread holds only -// to copy. The thread never holds a virtual core (kernel.c: host work runs without one), as discord_rpc.c's. -#include -#include -#include -#include -#include - -#include "cfg.h" -#include "coop.h" -#include "coop_app.h" -#include "host.h" -#include "platform/platform.h" - -enum { - PING_MS = 2000, SILENCE_MS = 8000, BURST_GAP_MS = 250, BACKOFF_FIRST_MS = 1000, BACKOFF_MAX_MS = 15000, - BURST_TRIES = 4, // CONNECTs per burst - NO_ROOM_WAIT_MS = 10000, // a room that is not there is asked for this often at most (each asks the relay for a join) - RATE_WAIT_MS = 60000, // the wait after the relay refused a join for the network's budget - ROSTER_AHEAD = 1 << 20, // a roster's number is taken at most this far ahead of the last one's - ROSTER_STALE_MS = 4000, // after the silence time and this long without a roster taken, any roster is taken - STATS_MS = 60000, // the periodic counters line, while in a room - LOG_EVERY_MS = 30000, // a repeated cause logs this often - RENAME_MS = 1000, // a name change joins the room again after this much quiet - OUT_SLOTS = 64, // messages waiting to be sent - DEFAULT_PORT = 24680, - VIEW_HOOKS = 8, - TICK_HOOKS = 4, -}; - -typedef struct { uint8_t dst, kind; uint16_t len; uint8_t data[COOP_MAX_MESSAGE]; } OutMsg; -typedef struct { CoopMsgFn fn; void *user; } MsgHandler; -typedef struct { CoopViewFn fn; void *user; } ViewHook; -typedef struct { CoopTickFn fn; void *user; } TickHook; - -// A cause that logs at most once per LOG_EVERY_MS. -enum { L_MALFORMED, L_SENDER, L_TAG, L_REPLAY, L_OUTBOX, L_NO_SERVER, L_RESOLVE, L_SOCKET, L_CAUSES }; - -// The counters of one sender's session seen last: a counter at most 64 below the highest is taken once, an older one -// is not, unless the sender's own messages keep arriving below the window (window_below). A new session (the roster -// names it) starts a new window. low and low_n: the last counter that opened below it, and how many did in a row. -typedef struct { uint64_t session; uint32_t high, low; uint64_t bits; int valid, low_n; } Window; -enum { WINDOW_AGAIN = 4 }; - -struct CoopSess { - // ---- settings, set by coop_sess_new and coop_sess_configure - char tag[16]; - uint32_t ping_ms, silence_ms, burst_gap_ms, backoff_first_ms, backoff_max_ms; - // what the stepping thread works with now - char server[160], name[COOP_NAME_MAX + 1], host[128]; - uint16_t port, local_port; - uint64_t client_id; - unsigned drop_pct; - int retry; - - // ---- shared, behind lock - PlatMutex lock; - PlatEvent off_done; - int cmd; // a command waiting (COOP_CMD_*), 0 none; a later one replaces it - char cmd_secret[COOP_SECRET_LEN + 1]; - int cmd_remove; // a member the maker removes, 0 none, and its session in the view then - uint64_t cmd_remove_session; - int cfg_pending; // pending holds settings the thread has not applied - struct { char server[160], name[COOP_NAME_MAX + 1]; uint64_t client_id; uint16_t local_port; unsigned drop_pct; int retry, on; } pending; - OutMsg *out; - unsigned out_head, out_tail; - CoopView view; - uint64_t heard_at[COOP_MAX_MEMBERS + 1]; // last message of each member that opened, by id; 0 never - MsgHandler msg[256]; - ViewHook hooks[VIEW_HOOKS]; - int n_hooks; - TickHook ticks[TICK_HOOKS]; - int n_ticks; - int pub_state; // state, readable without the lock - int pub_joined; - - // ---- the stepping thread's - CoopState state; - PlatUdp *sock; - PlatAddr relay; - int relay_known; - char relay_text[64]; // the relay's resolved address, for the trace - int intent, made_room; // CONNECT's intent; this session's own CREATE made the room - uint64_t room_session; // the session the room lists for this player since its last JOINED - char want[COOP_SECRET_LEN + 1]; // the secret of the room being joined or made - uint8_t locator[COOP_KEY_LEN]; // of want: the relay's name of the room and the tags' key - uint8_t msg_key[COOP_MSG_KEY_LEN]; // of want: seals the players' messages - uint64_t session, nonce; // this join's session (in the roster); this attempt's nonce - uint8_t cookie[COOP_COOKIE_LEN], token[COOP_COOKIE_LEN]; - int have_cookie, have_token, said_answer, heard_relay; - uint32_t connect_seq, out_seq, out_counter; - uint64_t next_tx, last_connect_tx, last_rx, next_ping, next_stats, rename_at; - uint32_t ping_salt; // added to the clock in a PING's token, new for every episode: the token alone would tell - // the relay how long this PC has been up - uint64_t roster_at, roster_rejoin_at; // when a roster was last taken; the last rejoin a roster asked for - int own_taken; // the roster taken last lists another session for this player - uint32_t backoff; - int burst_left; - uint32_t room; - int self; - char secret[COOP_SECRET_LEN + 1]; // of the room we are in - CoopRoster roster; - uint32_t roster_seq; - Window rw[COOP_MAX_MEMBERS + 1]; - char status[96]; - int dirty; - int off_applied; // a stop was applied: off_done is set once the view saying so is published - uint64_t rng; - uint64_t log_at[L_CAUSES]; - uint32_t n_tx, n_rx, n_lost, n_malformed, n_bad_tag, n_sender, n_dup, n_test_drop, n_outbox_full; - uint32_t stats_seen; - uint8_t rx[2048]; -}; - -// ---- logging - -#define TRACE(s, ...) do { if (rt_trace & RT_TRACE_COOP) say(s, __VA_ARGS__); } while (0) -static void say(const CoopSess *s, const char *fmt, ...) __attribute__((format(printf, 2, 3))); - -static void say(const CoopSess *s, const char *fmt, ...) { - char msg[256]; - va_list ap; - va_start(ap, fmt); - vsnprintf(msg, sizeof msg, fmt, ap); - va_end(ap); - if (s->tag[0]) rt_log("coop[%s]: %s", s->tag, msg); - else rt_log("coop: %s", msg); -} - -// 1 when cause may log now: the first time, then once per LOG_EVERY_MS (the periodic counters line has the totals). -static int log_ready(CoopSess *s, int cause, uint64_t now) { - if (s->log_at[cause] && now - s->log_at[cause] < LOG_EVERY_MS) return 0; - s->log_at[cause] = now ? now : 1; - return 1; -} -#define SAY_LIMITED(s, cause, now, ...) do { if (log_ready(s, cause, now)) say(s, __VA_ARGS__); } while (0) - -static const char *type_name(unsigned t) { - switch (t) { - case COOP_T_CONNECT: return "CONNECT"; - case COOP_T_CHALLENGE: return "CHALLENGE"; - case COOP_T_JOINED: return "JOINED"; - case COOP_T_ERROR: return "ERROR"; - case COOP_T_ROSTER: return "ROSTER"; - case COOP_T_PING: return "PING"; - case COOP_T_PONG: return "PONG"; - case COOP_T_LEAVE: return "LEAVE"; - case COOP_T_KICK: return "KICK"; - case COOP_T_DATA: return "DATA"; - default: return "?"; - } -} - -// ---- small things - -static uint32_t rnd(CoopSess *s) { // xorshift64*: for the test drop and nothing else - s->rng ^= s->rng >> 12, s->rng ^= s->rng << 25, s->rng ^= s->rng >> 27; - return (uint32_t)((s->rng * 0x2545F4914F6CDD1Dull) >> 32); -} - -// 64 random bits from the system's source (sessions, nonces), never 0; 0 when the source failed: there is no fallback, -// since a session or nonce someone could guess would let them answer for the relay or take this player's place. -static uint64_t random64(void) { - uint64_t v = 0; - if (!plat_random(&v, sizeof v)) return 0; - return v ? v : 1; -} - -static void copy_str(char *dst, size_t cap, const char *src) { - snprintf(dst, cap, "%s", src ? src : ""); -} - -// "XXX…" for a secret in the log (coop_secret_short: a label hashed from it): no part of the secret goes there. -static const char *label_of(const char *secret, char out[8]) { - if (secret[0]) coop_secret_short(secret, out); - else copy_str(out, 8, ""); - return out; -} - -static void set_status(CoopSess *s, const char *fmt, ...) __attribute__((format(printf, 2, 3))); - -static void set_status(CoopSess *s, const char *fmt, ...) { - char text[sizeof s->status]; - va_list ap; - va_start(ap, fmt); - vsnprintf(text, sizeof text, fmt, ap); - va_end(ap); - if (strcmp(text, s->status)) memcpy(s->status, text, sizeof text), s->dirty = 1; -} - -static void set_state(CoopSess *s, CoopState st) { - if (s->state != st) s->dirty = 1; - s->state = st; - __atomic_store_n(&s->pub_state, (int)st, __ATOMIC_RELEASE); - __atomic_store_n(&s->pub_joined, st == COOP_JOINED, __ATOMIC_RELEASE); -} - -int coop_server_parse(const char *text, char *host, size_t cap, uint16_t *port) { - while (*text == ' ') text++; - size_t n = strlen(text); - while (n && text[n - 1] == ' ') n--; - if (!n) return 0; - const char *h = text, *colon = NULL; - size_t hn = n; - if (*text == '[') { // [v6] or [v6]:port - const char *close = memchr(text, ']', n); - if (!close) return 0; - h = text + 1, hn = (size_t)(close - h); - if (close + 1 < text + n) { - if (close[1] != ':') return 0; - colon = close + 1; - } - } else { - size_t colons = 0; - for (size_t i = 0; i < n; i++) - if (text[i] == ':') colons++, colon = text + i; - if (colons > 1) colon = NULL; // a bare IPv6 address - else if (colon) hn = (size_t)(colon - text); - } - uint32_t p = DEFAULT_PORT; - if (colon) { - const char *d = colon + 1; - size_t dn = (size_t)(text + n - d); - if (!dn || dn > 5) return 0; - p = 0; - for (size_t i = 0; i < dn; i++) { - if (d[i] < '0' || d[i] > '9') return 0; - p = p * 10 + (uint32_t)(d[i] - '0'); - } - if (!p || p > 65535) return 0; - } - if (!hn || hn >= cap) return 0; - memcpy(host, h, hn); - host[hn] = 0; - *port = (uint16_t)p; - return 1; -} - -// ---- sending - -// One datagram to the relay, tagged with the locator. -static void send_dgram(CoopSess *s, uint8_t type, uint32_t room, uint8_t src, uint8_t dst, uint32_t seq, - const void *payload, size_t n) { - uint8_t d[COOP_MAX_DGRAM]; - CoopHdr h = {.ver = COOP_PROTO, .type = type, .src = src, .dst = dst, .room = room, .seq = seq}; - size_t k = coop_dgram_pack(d, sizeof d, &h, payload, n, s->locator); - if (!k || !s->sock || !s->relay_known) return; - plat_udp_send(s->sock, &s->relay, d, k); - s->n_tx++; - TRACE(s, "tx %s to %u, %zu bytes", type_name(type), dst, k); -} - -// A datagram of the room we are in. -static void send_room(CoopSess *s, uint8_t type, uint8_t dst, const void *payload, size_t n) { - send_dgram(s, type, s->room, (uint8_t)s->self, dst, s->out_seq++, payload, n); -} - -// A player message, sealed with the room's message key under this session and the next counter. -static void send_sealed(CoopSess *s, uint8_t dst, uint8_t kind, const void *bytes, size_t n) { - uint8_t payload[COOP_MAX_PAYLOAD]; - size_t k = coop_seal(payload, sizeof payload, s->msg_key, (uint8_t)s->self, dst, s->session, ++s->out_counter, kind, bytes, n); - if (k) send_room(s, COOP_T_DATA, dst, payload, k); -} - -static void send_connect(CoopSess *s, uint64_t now) { - CoopConnect c = {.intent = (uint8_t)s->intent, .client_id = s->client_id, .session = s->session, .nonce = s->nonce}; - uint8_t payload[128]; - memcpy(c.locator, s->locator, COOP_KEY_LEN); - copy_str(c.name, sizeof c.name, s->name); - if (s->have_cookie) memcpy(c.cookie, s->cookie, COOP_COOKIE_LEN); - if (s->have_token) memcpy(c.token, s->token, COOP_COOKIE_LEN); - size_t n = coop_connect_pack(payload, sizeof payload, &c); - send_dgram(s, COOP_T_CONNECT, 0, 0, 0, s->connect_seq, payload, n); - if (!s->have_cookie) s->last_connect_tx = now; -} - -// ---- leaving - -static void close_socket(CoopSess *s) { - if (s->sock) plat_udp_close(s->sock); - s->sock = NULL; -} - -// Tells the others and the relay, twice (a datagram may be lost): BYE to every member, then LEAVE. -static void say_goodbye(CoopSess *s) { - if (!s->sock || !s->relay_known || !s->room) return; - for (int i = 0; i < 2; i++) { - if (s->state == COOP_JOINED) send_sealed(s, COOP_DST_ALL, COOP_K_BYE, NULL, 0); - send_room(s, COOP_T_LEAVE, 0, NULL, 0); - } -} - -// Out of the room (or never in): closes the socket and shows why. -static void go_left(CoopSess *s, const char *why, int log_it) { - int was_in = s->state == COOP_JOINED || s->state == COOP_RECONNECTING; - if (log_it) { - if (was_in) say(s, "left room (%s)", why); - else say(s, "no room: %s", why); - } - close_socket(s); - s->room = 0, s->self = 0, s->made_room = 0; - s->roster.n = 0; - s->have_token = 0; - memset(s->rw, 0, sizeof s->rw); - set_state(s, COOP_LEFT); - set_status(s, "%s", why); - s->dirty = 1; -} - -// ---- reaching the relay - -// A socket and the relay's address, when there are none yet. 0 and a status when they cannot be had. -static int ensure_socket(CoopSess *s, uint64_t now) { - if (!s->sock) { - s->sock = plat_udp_open(s->local_port, 1); - if (!s->sock) { - set_status(s, "cannot open a UDP socket"); - SAY_LIMITED(s, L_SOCKET, now, "cannot open a UDP socket"); - return 0; - } - } - if (!s->relay_known) { - say(s, "connecting to the relay"); - TRACE(s, "the relay is %s", s->server); - if (!plat_net_resolve(s->host, s->port, &s->relay)) { - set_status(s, "cannot find the relay's address"); - SAY_LIMITED(s, L_RESOLVE, now, "cannot resolve the relay's name"); - return 0; - } - plat_addr_str(&s->relay, s->relay_text, sizeof s->relay_text); - s->relay_known = 1; - } - return 1; -} - -static void schedule_backoff(CoopSess *s, uint64_t now) { - s->next_tx = now + s->backoff; - if (!s->heard_relay && s->sock && s->relay_known) set_status(s, "no answer from the relay; trying again in %u s", (s->backoff + 500) / 1000); - s->backoff = s->backoff * 2 > s->backoff_max_ms ? s->backoff_max_ms : s->backoff * 2; -} - -// A new attempt to get into a room: intent, the room's secret, and whether it is a rejoin of the room we were in (which -// keeps the rejoin token). Every attempt is a new session and a new nonce; 0 (and no room) when the system's random -// source cannot give them. -static int begin_episode(CoopSess *s, int intent, const char *secret, int rejoin, uint64_t now) { - uint64_t session = random64(), nonce = random64(); - if (!session || !nonce) { - go_left(s, "the system's random source failed: co-op needs it", 1); - return 0; - } - s->intent = intent; - if (secret != s->want) copy_str(s->want, sizeof s->want, secret); // a rejoin passes the secret it already holds - coop_room_keys(s->want, s->locator, s->msg_key); - s->session = session, s->nonce = nonce, s->out_counter = 0; - s->ping_salt = (uint32_t)(nonce >> 32) ^ (uint32_t)session; - s->connect_seq = s->out_seq++; - s->have_cookie = s->said_answer = s->heard_relay = 0; - if (!rejoin) s->have_token = 0; - s->burst_left = 0; - s->next_tx = now; - s->backoff = s->backoff_first_ms; - s->relay_known = 0; // looked up again: the name may point elsewhere by now - if (!rejoin) { - s->room = 0, s->self = 0, s->made_room = 0; - s->roster.n = 0; - } - set_state(s, rejoin ? COOP_RECONNECTING : COOP_CONNECTING); - if (rejoin) set_status(s, "reconnecting to the relay"); - else set_status(s, "%s", intent == COOP_INTENT_CREATE ? "creating a room" : "joining the room"); - s->dirty = 1; - return 1; -} - -static void connect_timers(CoopSess *s, uint64_t now) { - if (now < s->next_tx) return; - if (s->burst_left == 0) { // a new burst: the socket and the relay's address first - if (!ensure_socket(s, now)) { - s->relay_known = 0; - schedule_backoff(s, now); - return; - } - s->burst_left = BURST_TRIES; - } - send_connect(s, now); - if (--s->burst_left) s->next_tx = now + s->burst_gap_ms; - else schedule_backoff(s, now); -} - -// ---- the roster - -// 1 when every id of r is 1..8 and none repeats: what the rest of the code indexes by. -static int roster_valid(const CoopRoster *r) { - unsigned seen = 0; - for (int i = 0; i < r->n; i++) { - unsigned id = r->m[i].id; - if (id < 1 || id > COOP_MAX_MEMBERS || (seen & 1u << id)) return 0; - seen |= 1u << id; - } - return 1; -} - -static const CoopMemberInfo *member_of(const CoopSess *s, int id) { - for (int i = 0; i < s->roster.n; i++) - if (s->roster.m[i].id == id) return &s->roster.m[i]; - return NULL; -} - -// The roster the relay sent replaces ours; the log tells who came, went, changed name or went away. 1 when this -// player's own line lists another session than its own: someone took its place at the relay (a CONNECT with its -// token, seen on the way), and the session joins again to take it back (roster_rejoin). -static int apply_roster(CoopSess *s, const CoopRoster *r) { - int taken = 0; - for (int i = 0; i < r->n; i++) { - const CoopMemberInfo *m = &r->m[i], *old = member_of(s, m->id); - if (m->id == s->self) { // the relay's maker mark is not taken for this session's own creation (on_joined) - taken = m->session != s->session; - continue; - } - if (!old) say(s, "%s (member %u) joined the room", m->name, m->id); - else if (strcmp(old->name, m->name)) say(s, "member %u is now %s", m->id, m->name); - else if ((old->flags ^ m->flags) & COOP_FLAG_AWAY) - say(s, "%s (member %u) %s", m->name, m->id, m->flags & COOP_FLAG_AWAY ? "is away" : "is back"); - // a new member, or a new session of it: nothing heard from it yet. Its replay window stays: a window belongs to - // one session (window_check takes another session as new), so the same session, listed again after this player - // joined again, keeps refusing what it already delivered - if (!old || old->session != m->session) { - plat_mutex_lock(&s->lock); - s->heard_at[m->id] = 0; - plat_mutex_unlock(&s->lock); - } - } - for (int j = 0; j < s->roster.n; j++) { - int id = s->roster.m[j].id, found = 0; - for (int i = 0; i < r->n; i++) found |= r->m[i].id == id; - if (found) continue; - say(s, "%s (member %u) left the room", s->roster.m[j].name, id); - plat_mutex_lock(&s->lock); - s->heard_at[id] = 0; - plat_mutex_unlock(&s->lock); - } - s->roster = *r; - s->dirty = 1; - return taken; -} - -// ---- receiving - -// 1 when counter of session is new in the window, 0 when it was taken before, -1 when it is too old to tell (64 or more -// below the highest); nothing changes (commit after the message opened). -static int window_check(const Window *w, uint64_t session, uint32_t counter) { - if (!w->valid || w->session != session) return 1; - int32_t d = (int32_t)(counter - w->high); - if (d > 0) return 1; - if (d <= -64) return -1; - return !(w->bits >> (unsigned)-d & 1); -} - -static void window_commit(Window *w, uint64_t session, uint32_t counter) { - if (!w->valid || w->session != session) { - *w = (Window){.session = session, .high = counter, .bits = 1, .valid = 1}; - return; - } - int32_t d = (int32_t)(counter - w->high); - if (d > 0) { - w->bits = d >= 64 ? 1 : w->bits << d | 1; - w->high = counter; - w->low_n = 0; - } else { - w->bits |= 1ull << (unsigned)-d; - } -} - -// A message that opened with the room's message key but is too old for the window. That key is the whole room's, so -// whoever holds it can seal one message in a member's name with a counter far ahead and leave every later message of -// that member below the window for the rest of its session. The sender's own next messages then keep arriving below -// it, each later than the one before: at the WINDOW_AGAIN-th in a row the window starts again from it, which takes this -// one. One that is not later than the last starts the row again (a copy of one counts once, and one sealed above the -// sender's own cannot hold the row there). The window forgets what it took: recorded messages of the session above the -// new start, and its last 64, can then be taken again (coop.h says what a handler does about it). 1 when the window -// started again. -static int window_below(Window *w, uint32_t counter) { - if (w->low_n && (int32_t)(counter - w->low) <= 0) w->low_n = 0; - w->low = counter; - if (++w->low_n < WINDOW_AGAIN) return 0; - *w = (Window){.session = w->session, .high = counter, .bits = 1, .valid = 1}; - return 1; -} - -// The answers to a CONNECT: the attempt's nonce and the locator's tag. -static int answer_ok(CoopSess *s, const uint8_t *d, size_t n, uint64_t nonce) { - if (nonce != s->nonce || !coop_dgram_verify(d, n, s->locator)) { - s->n_bad_tag++, s->n_lost++; - return 0; - } - return 1; -} - -// The on_* handlers return 1 for a datagram that passed its checks and made sense: only those show the relay is there. -static int on_challenge(CoopSess *s, const uint8_t *d, size_t n, const CoopHdr *h, const uint8_t *p, uint64_t now) { - uint64_t nonce; - uint8_t cookie[COOP_COOKIE_LEN]; - if ((s->state != COOP_CONNECTING && s->state != COOP_RECONNECTING) || !coop_challenge_parse(p, h->len, &nonce, cookie)) return 0; - if (!answer_ok(s, d, n, nonce)) return 0; - memcpy(s->cookie, cookie, COOP_COOKIE_LEN); - s->have_cookie = 1; - s->heard_relay = 1; - if (!s->said_answer) { - s->said_answer = 1; - say(s, "the relay answered in %u ms", (unsigned)(now - s->last_connect_tx)); - TRACE(s, "the relay is at %s", s->relay_text); - } - send_connect(s, now); // the CONNECT with the cookie, at once - return 1; -} - -static int on_joined(CoopSess *s, const uint8_t *d, size_t n, const CoopHdr *h, const uint8_t *p, uint64_t now) { - if (s->state != COOP_CONNECTING && s->state != COOP_RECONNECTING) return 0; - CoopJoined j; - if (!coop_joined_parse(p, h->len, &j) || !h->room || j.member < 1 || j.member > COOP_MAX_MEMBERS || !roster_valid(&j.roster)) { - s->n_malformed++, s->n_lost++; - return 0; - } - if (!answer_ok(s, d, n, j.nonce)) return 0; // the room asked for (our locator), this attempt (our nonce) - int rejoined = s->state == COOP_RECONNECTING; - s->room = h->room; - s->self = j.member; - memcpy(s->secret, s->want, sizeof s->secret); - memcpy(s->token, j.token, COOP_COOKIE_LEN); - s->have_token = 1; - if (!rejoined) s->made_room = s->intent == COOP_INTENT_CREATE; // this session's own creation, not the relay's mark - s->room_session = s->session; - apply_roster(s, &j.roster); // after a rejoin: against the roster before it, so a member whose session is the same stays as it was - s->roster_seq = h->seq; - s->roster_at = now, s->own_taken = 0; - s->last_rx = now; - s->next_ping = now + s->ping_ms; - s->next_stats = now + STATS_MS; - char label[8]; - label_of(s->secret, label); - if (rejoined) say(s, "rejoined room %s as member %u", label, s->self); - else if (s->intent == COOP_INTENT_CREATE) say(s, "room %s created (member %u)", label, s->self); - else say(s, "joined room %s as member %u", label, s->self); - set_state(s, COOP_JOINED); - set_status(s, "%s", ""); - s->dirty = 1; - return 1; -} - -static void refused(CoopSess *s, uint8_t reason, uint64_t now) { - char label[8]; - label_of(s->want, label); - switch (reason) { - case COOP_E_NO_ROOM: - if (s->state == COOP_RECONNECTING) { // the relay lost the room: a restart - say(s, "room %s is gone from the relay", label); - if (!s->retry) go_left(s, "the room is closed", 0); - else if (s->made_room) { - say(s, "creating room %s again", label); - if (begin_episode(s, COOP_INTENT_CREATE, s->want, 0, now)) set_status(s, "the room was lost; creating it again"); - } else if (begin_episode(s, COOP_INTENT_JOIN, s->want, 0, now)) { - set_status(s, "the room was lost; waiting for it"); - } - } else if (s->retry) { // not made yet, perhaps: wait and ask again - s->heard_relay = 1; - set_status(s, "room %s not found; waiting for it", label); - if (s->burst_left || now >= s->next_tx) { // the first answer of a burst ends it; a CONNECT sent twice is answered twice - s->burst_left = 0; - schedule_backoff(s, now); - // each question asks the relay for one of the network's joins: at most one every NO_ROOM_WAIT_MS, so - // that players behind one address who wait for a room leave joins for the others (the same attempt - // asked again costs nothing at the relay) - if (s->next_tx < now + NO_ROOM_WAIT_MS) s->next_tx = now + NO_ROOM_WAIT_MS; - } - } else { - go_left(s, "no such room", 1); - } - break; - case COOP_E_ROOM_FULL: go_left(s, "the room is full", 1); break; - case COOP_E_PROTO: go_left(s, "update required: the relay speaks another protocol version", 1); break; - case COOP_E_RATE: - if (s->retry) { // a session that keeps its room waits until the network's joins are back, and stays - if (s->next_tx < now + RATE_WAIT_MS) { - say(s, "the relay refused: too many joins from this network; trying again in a minute"); - s->next_tx = now + RATE_WAIT_MS; - } - s->heard_relay = 1, s->burst_left = 0; - set_status(s, "too many joins from this network; trying again in a minute"); - } else { - go_left(s, "too many joins from this network; try again in a minute", 1); - } - break; - case COOP_E_ROOM_EXISTS: - if (s->retry && s->intent == COOP_INTENT_CREATE) { - // a session that keeps its room (coop_auto): someone made the room again first after the relay lost it, or - // it is open already. Joining it keeps the player in the room they meant; the rules stay the ones the - // players accepted, since a game takes them only signed by the maker it pinned (coop_state.h) - say(s, "room %s is open already; joining it", label); - if (begin_episode(s, COOP_INTENT_JOIN, s->want, 0, now)) set_status(s, "the room is open already; joining it"); - } else { - go_left(s, "a room with this secret is already open on the relay: join it", 1); - } - break; - case COOP_E_REMOVED: go_left(s, "the room's maker removed this player", 1); break; - case COOP_E_SESSION_USED: - if (s->retry) { // someone sent this attempt's CONNECT first (seen on the way): a new attempt, a new session - say(s, "the relay saw this session in the room before (a replay on the way?); trying again with a new one"); - int rejoin = s->state == COOP_RECONNECTING; - if (begin_episode(s, s->intent, s->want, rejoin, now)) s->next_tx = now + NO_ROOM_WAIT_MS; - } else { - go_left(s, "the relay saw this session in the room before (a replay on the way?)", 1); - } - break; - default: go_left(s, "the relay refused", 1); break; - } -} - -static int on_error(CoopSess *s, const uint8_t *d, size_t n, const CoopHdr *h, const uint8_t *p, uint64_t now) { - int connecting = s->state == COOP_CONNECTING || s->state == COOP_RECONNECTING; - CoopError e; - if (!coop_error_parse(p, h->len, &e)) { - s->n_malformed++, s->n_lost++; - return 0; - } - // an answer to this attempt; in a room, only a removal (nonce 0, the room's tag). A removal in the room never - // arrives (it carries nonce 0, never this attempt's), and must not be taken by accepting nonce 0: anyone who saw a - // CONNECT can tag a datagram with its locator. The maker's removal shows as the member leaving the roster instead. - if (connecting ? !answer_ok(s, d, n, e.nonce) : s->state != COOP_JOINED || e.reason != COOP_E_REMOVED || h->room != s->room || - !answer_ok(s, d, n, 0)) - return 0; - refused(s, e.reason, now); - return 1; -} - -// This player's own line lists another session: join again to take the place back, at most once per backoff_max_ms, -// so that rosters from someone on the path cannot use up the network's joins; a later one waits for its turn (timers). -static void roster_rejoin(CoopSess *s, uint64_t now) { - if (s->roster_rejoin_at && now - s->roster_rejoin_at < s->backoff_max_ms) return; - s->roster_rejoin_at = now ? now : 1; - say(s, "the relay lists another session for this player; joining the room again"); - begin_episode(s, COOP_INTENT_JOIN, s->want, 1, now); -} - -static int on_roster(CoopSess *s, const uint8_t *d, size_t n, const CoopHdr *h, const uint8_t *p, uint64_t now) { - CoopRoster r; - if (s->state != COOP_JOINED || h->room != s->room) return 0; - if (!coop_dgram_verify(d, n, s->locator)) { - s->n_bad_tag++, s->n_lost++; - return 0; - } - if (!coop_roster_parse(p, h->len, &r, NULL) || !roster_valid(&r)) { - s->n_malformed++, s->n_lost++; - return 0; - } - // a later roster, up to ROSTER_AHEAD past the last one: anyone who saw a CONNECT can tag one, and a number far - // ahead would make every roster of the relay after it look old. An older one arriving late is dropped, unless no - // roster was taken for a while (the relay sends one every few seconds): then the last one's number was not the - // relay's - int32_t ahead = (int32_t)(h->seq - s->roster_seq); - int stale = now - s->roster_at >= (uint64_t)s->silence_ms + ROSTER_STALE_MS; - if ((ahead > 0 && ahead <= ROSTER_AHEAD) || (stale && ahead)) { - s->roster_seq = h->seq, s->roster_at = now; - s->own_taken = apply_roster(s, &r); - if (s->own_taken) roster_rejoin(s, now); - } - return 1; -} - -static int on_pong(CoopSess *s, const uint8_t *d, size_t n, const CoopHdr *h, const uint8_t *p, uint64_t now) { - uint32_t token; - if (s->state != COOP_JOINED || h->room != s->room || !coop_u32_parse(p, h->len, &token)) return 0; - if (!coop_dgram_verify(d, n, s->locator)) { - s->n_bad_tag++, s->n_lost++; - return 0; - } - uint32_t rtt = (uint32_t)now + s->ping_salt - token; - if (rtt <= 60000) { // a token of ours - plat_mutex_lock(&s->lock); - s->view.ping_ms = rtt ? rtt : 1; - plat_mutex_unlock(&s->lock); - } - return 1; -} - -static int on_data(CoopSess *s, const uint8_t *d, size_t n, const CoopHdr *h, const uint8_t *p, uint64_t now) { - (void)d, (void)n; - if (s->state != COOP_JOINED || h->room != s->room) return 0; - const CoopMemberInfo *m = h->src != s->self ? member_of(s, h->src) : NULL; - uint64_t session; - uint32_t counter; - if (!m || (h->dst != COOP_DST_ALL && h->dst != s->self)) { - s->n_sender++, s->n_lost++; - SAY_LIMITED(s, L_SENDER, now, "a message from member %u to %u was thrown away: not a member the roster names", h->src, h->dst); - return 0; - } - if (!coop_envelope_peek(p, h->len, &session, &counter)) { - s->n_malformed++, s->n_lost++; - return 0; - } - int fresh = session == m->session ? window_check(&s->rw[h->src], session, counter) : 0; - if (!fresh) { // an earlier session's, or seen - s->n_dup++, s->n_lost++; - SAY_LIMITED(s, L_REPLAY, now, "a message of member %u that is not of its present session, or seen before, was thrown away", h->src); - return 0; - } - uint8_t kind, bytes[COOP_MAX_PAYLOAD]; - size_t nb; - if (!coop_open(p, h->len, s->msg_key, h->src, h->dst, &kind, bytes, sizeof bytes, &nb)) { - s->n_bad_tag++, s->n_lost++; - SAY_LIMITED(s, L_TAG, now, "a message that does not open with the room's key was thrown away"); - return 0; - } - if (fresh < 0 && !window_below(&s->rw[h->src], counter)) { - s->n_dup++, s->n_lost++; - SAY_LIMITED(s, L_REPLAY, now, "a message of member %u older than its last 64 was thrown away", h->src); - return 0; - } - if (fresh > 0) window_commit(&s->rw[h->src], session, counter); - plat_mutex_lock(&s->lock); - s->heard_at[h->src] = now; - plat_mutex_unlock(&s->lock); - TRACE(s, "rx message kind %u from member %u, %zu bytes", kind, h->src, nb); - CoopMsgFn fn = __atomic_load_n(&s->msg[kind].fn, __ATOMIC_ACQUIRE); - if (fn) fn(h->src, bytes, nb, s->msg[kind].user); - return 1; -} - -static void handle_datagram(CoopSess *s, size_t n, const PlatAddr *from, uint64_t now) { - if (s->drop_pct && rnd(s) % 100 < s->drop_pct) { - s->n_test_drop++; - return; - } - CoopHdr h; - const uint8_t *p; - if (!coop_dgram_parse(s->rx, n, &h, &p)) { - s->n_malformed++, s->n_lost++; - SAY_LIMITED(s, L_MALFORMED, now, "a datagram that is not a co-op datagram was thrown away"); - return; - } - if (!s->relay_known || !plat_addr_eq(from, &s->relay)) { // only the relay talks to us - s->n_sender++, s->n_lost++; - SAY_LIMITED(s, L_SENDER, now, "a datagram from an address that is not the relay was thrown away"); - return; - } - if (h.ver != COOP_PROTO) { - // a relay of another version answers a CONNECT with an untagged ERROR it cannot word in our format: taken only - // as the first answer of an attempt, so a stray one does not end a session - if (h.type == COOP_T_ERROR && coop_dgram_tag_zero(s->rx) && !s->heard_relay && - (s->state == COOP_CONNECTING || s->state == COOP_RECONNECTING)) { - s->n_rx++; - refused(s, COOP_E_PROTO, now); - } - return; - } - TRACE(s, "rx %s, %zu bytes", type_name(h.type), n); - int ok = 0; - switch (h.type) { - case COOP_T_CHALLENGE: ok = on_challenge(s, s->rx, n, &h, p, now); break; - case COOP_T_JOINED: ok = on_joined(s, s->rx, n, &h, p, now); break; - case COOP_T_ERROR: ok = on_error(s, s->rx, n, &h, p, now); break; - case COOP_T_ROSTER: ok = on_roster(s, s->rx, n, &h, p, now); break; - case COOP_T_PONG: ok = on_pong(s, s->rx, n, &h, p, now); break; - case COOP_T_DATA: ok = on_data(s, s->rx, n, &h, p, now); break; - default: break; - } - if (ok) { - s->n_rx++; - s->last_rx = now; // a datagram the relay made: it is there - } -} - -// ---- the outbox - -static void flush_outbox(CoopSess *s) { - for (int i = 0; i < OUT_SLOTS; i++) { - OutMsg m; - plat_mutex_lock(&s->lock); - if (s->out_head == s->out_tail) { - plat_mutex_unlock(&s->lock); - return; - } - OutMsg *slot = &s->out[s->out_tail]; - m.dst = slot->dst, m.kind = slot->kind, m.len = slot->len; - memcpy(m.data, slot->data, slot->len); - s->out_tail = (s->out_tail + 1) % OUT_SLOTS; - plat_mutex_unlock(&s->lock); - if (s->state == COOP_JOINED) send_sealed(s, m.dst, m.kind, m.data, m.len); // anywhere else it is dropped - } -} - -int coop_sess_send(CoopSess *s, int member, uint8_t kind, const void *bytes, size_t n) { - if (!__atomic_load_n(&s->pub_joined, __ATOMIC_ACQUIRE) || n > COOP_MAX_MESSAGE || member < 0 || - (member > COOP_MAX_MEMBERS && member != COOP_DST_ALL)) - return 0; - plat_mutex_lock(&s->lock); - unsigned next = (s->out_head + 1) % OUT_SLOTS; - if (next == s->out_tail) { - plat_mutex_unlock(&s->lock); - __atomic_add_fetch(&s->n_outbox_full, 1, __ATOMIC_RELAXED); - return 0; - } - OutMsg *slot = &s->out[s->out_head]; - slot->dst = member ? (uint8_t)member : COOP_DST_ALL; - slot->kind = kind; - slot->len = (uint16_t)n; - if (n) memcpy(slot->data, bytes, n); - s->out_head = next; - plat_mutex_unlock(&s->lock); - return 1; -} - -// ---- commands and settings - -// The settings coop_sess_configure queued, on the stepping thread. -static void apply_settings(CoopSess *s, uint64_t now) { - plat_mutex_lock(&s->lock); - if (!s->cfg_pending) { - plat_mutex_unlock(&s->lock); - return; - } - s->cfg_pending = 0; - char server[sizeof s->server], name[sizeof s->name]; - copy_str(server, sizeof server, s->pending.server); - copy_str(name, sizeof name, s->pending.name); - s->client_id = s->pending.client_id; - s->local_port = s->pending.local_port; - s->drop_pct = s->pending.drop_pct; - s->retry = s->pending.retry; - int on = s->pending.on; - plat_mutex_unlock(&s->lock); - if (strcmp(server, s->server)) { - copy_str(s->server, sizeof s->server, server); - s->relay_known = 0; - s->host[0] = 0; - if (s->server[0] && !coop_server_parse(s->server, s->host, sizeof s->host, &s->port)) - say(s, "the coop_server setting is not host or host:port"); - if (s->state == COOP_JOINED || s->state == COOP_RECONNECTING) { - say_goodbye(s); - go_left(s, "the server changed", 1); - } else if (s->state == COOP_CONNECTING) { - go_left(s, "the server changed", 0); - } - } - if (strcmp(name, s->name)) { - copy_str(s->name, sizeof s->name, name); - if (s->state == COOP_JOINED) s->rename_at = now + RENAME_MS; // joins again once the typing has stopped - } - if (on && s->state == COOP_OFF) { - set_state(s, COOP_LEFT); - set_status(s, "%s", ""); - } else if (!on) { - if (s->state == COOP_JOINED || s->state == COOP_RECONNECTING) { - say_goodbye(s); - say(s, "left room (co-op turned off)"); - } - close_socket(s); - s->room = 0, s->self = 0, s->made_room = 0, s->roster.n = 0; - s->rename_at = 0; - set_state(s, COOP_OFF); - set_status(s, "%s", ""); - s->dirty = 1; - s->off_applied = 1; - } -} - -static void do_command(CoopSess *s, int cmd, const char *secret, uint64_t now) { - if (s->state == COOP_OFF) return; - switch (cmd) { - case COOP_CMD_LEAVE: - if (s->state == COOP_LEFT) break; - if (s->state == COOP_JOINED || s->state == COOP_RECONNECTING) { - say_goodbye(s); - say(s, "left room (leave requested)"); - } else { - say(s, "stopped trying to get into a room"); - } - go_left(s, "you left the room", 0); - break; - case COOP_CMD_CREATE: - case COOP_CMD_JOIN: - if (!s->host[0]) { - SAY_LIMITED(s, L_NO_SERVER, now, "no server configured (coop_server / coop_app.h)"); - go_left(s, "no server configured", 0); - break; - } - if (s->state == COOP_JOINED || s->state == COOP_RECONNECTING) say_goodbye(s); - begin_episode(s, cmd == COOP_CMD_CREATE ? COOP_INTENT_CREATE : COOP_INTENT_JOIN, secret, 0, now); - break; - } -} - -// The maker's KICK of a member, with the member's session so that a KICK sent again later cannot remove whoever has the -// id then: the session the published view listed when the maker removed it (progress sync removed that member's key at -// that moment), so that a member that rejoined since, or another player given the id, is not the one kicked. -static void do_remove(CoopSess *s, int member, uint64_t session) { - const CoopMemberInfo *m = member_of(s, member); - uint8_t payload[16]; - if (s->state != COOP_JOINED || !s->made_room || !m || member == s->self) { - say(s, "only the room's maker can remove a member, and only one that is in the room"); - return; - } - size_t k = coop_kick_pack(payload, sizeof payload, (uint8_t)member, session ? session : m->session); - for (int i = 0; i < 2; i++) send_room(s, COOP_T_KICK, 0, payload, k); - say(s, "removing %s (member %d) from the room", m->name, member); -} - -// ---- the step - -static void lost_relay(CoopSess *s, uint64_t now) { - say(s, "relay lost; rejoining"); - begin_episode(s, COOP_INTENT_JOIN, s->want, 1, now); -} - -static void timers(CoopSess *s, uint64_t now) { - switch (s->state) { - case COOP_CONNECTING: - case COOP_RECONNECTING: - connect_timers(s, now); - break; - case COOP_JOINED: - if (now - s->last_rx >= s->silence_ms) { - lost_relay(s, now); - break; - } - if (s->rename_at && now >= s->rename_at) { // the name changed: the relay learns it with a new CONNECT - s->rename_at = 0; - say(s, "joining the room again as %s", s->name); - begin_episode(s, COOP_INTENT_JOIN, s->want, 1, now); - break; - } - if (s->own_taken && now - s->roster_rejoin_at >= s->backoff_max_ms) { // a roster's rejoin that waited for its turn - roster_rejoin(s, now); - break; - } - if (now >= s->next_ping) { - uint8_t token[4]; - coop_u32_pack(token, sizeof token, (uint32_t)now + s->ping_salt); - send_room(s, COOP_T_PING, 0, token, sizeof token); - s->next_ping = now + s->ping_ms; - } - if (now >= s->next_stats) { - s->next_stats = now + STATS_MS; - uint32_t seen = s->n_rx + s->n_tx + s->n_lost + s->n_test_drop; - if (seen != s->stats_seen) { - s->stats_seen = seen; - say(s, "stats: sent %u, accepted %u, thrown away %u (tag %u, sender %u, not co-op %u, replay %u), dropped by the test %u, outbox full %u", - s->n_tx, s->n_rx, s->n_lost, s->n_bad_tag, s->n_sender, s->n_malformed, s->n_dup, s->n_test_drop, - __atomic_load_n(&s->n_outbox_full, __ATOMIC_RELAXED)); - } - } - break; - default: - break; - } -} - -static void view_rebuild(CoopSess *s) { - plat_mutex_lock(&s->lock); - CoopView *v = &s->view; - int in_room = s->state == COOP_JOINED || s->state == COOP_RECONNECTING; - const char *secret = in_room ? s->secret : s->state == COOP_CONNECTING ? s->want : ""; - v->state = s->state; - if (secret[0]) coop_secret_format(secret, v->code); - else v->code[0] = 0; - label_of(secret, v->label); - v->self = in_room ? s->self : 0; - v->made = in_room && s->made_room; - v->session = in_room ? s->room_session : 0; - copy_str(v->name, sizeof v->name, s->name); - v->maker = 0; - v->n = in_room ? s->roster.n : 0; - for (int i = 0; i < v->n; i++) { - v->member[i].id = s->roster.m[i].id; - v->member[i].away = s->roster.m[i].flags & COOP_FLAG_AWAY ? 1 : 0; - v->member[i].maker = s->roster.m[i].flags & COOP_FLAG_MAKER ? 1 : 0; - if (v->member[i].maker) v->maker = v->member[i].id; - v->member[i].session = s->roster.m[i].session; - copy_str(v->member[i].name, sizeof v->member[i].name, s->roster.m[i].name); - } - copy_str(v->status, sizeof v->status, s->status); - if (s->state != COOP_JOINED) v->ping_ms = 0; - v->version++; - plat_mutex_unlock(&s->lock); -} - -static void publish(CoopSess *s) { - if (!s->dirty) return; - s->dirty = 0; - view_rebuild(s); - CoopView copy; - ViewHook hooks[VIEW_HOOKS]; - plat_mutex_lock(&s->lock); - copy = s->view; - int n = s->n_hooks; - memcpy(hooks, s->hooks, sizeof hooks); - plat_mutex_unlock(&s->lock); - for (int i = 0; i < n; i++) hooks[i].fn(©, hooks[i].user); - // A stop is signalled only now: whoever it wakes (coop_shutdown's caller, a page that switched co-op off) reads the - // view that says the session is off, and every view hook has been told of it. - if (s->off_applied) { - s->off_applied = 0; - plat_event_set(&s->off_done); - } -} - -void coop_sess_step(CoopSess *s) { - uint64_t now = plat_now_ms(); - plat_mutex_lock(&s->lock); - int cmd = s->cmd, remove = s->cmd_remove; - uint64_t remove_session = s->cmd_remove_session; - char secret[sizeof s->cmd_secret]; - memcpy(secret, s->cmd_secret, sizeof secret); - s->cmd = 0, s->cmd_remove = 0; - plat_mutex_unlock(&s->lock); - apply_settings(s, now); - if (cmd) do_command(s, cmd, secret, now); - if (remove) do_remove(s, remove, remove_session); - flush_outbox(s); - publish(s); // what a command or a setting changed shows before this thread waits for a datagram - uint32_t wait = s->state == COOP_JOINED ? 8 : s->state == COOP_CONNECTING || s->state == COOP_RECONNECTING ? 50 : 250; - if (s->sock) { - PlatAddr from; - int n = plat_udp_recv(s->sock, s->rx, sizeof s->rx, &from, wait); - for (int i = 0; n > 0 && i < 64; i++) { - handle_datagram(s, (size_t)n, &from, plat_now_ms()); - if (!s->sock) break; // a handler ended it - n = plat_udp_recv(s->sock, s->rx, sizeof s->rx, &from, 0); - } - if (n < 0 && s->sock) { - SAY_LIMITED(s, L_SOCKET, now, "the socket failed"); - close_socket(s); - if (s->state == COOP_JOINED) lost_relay(s, plat_now_ms()); - } - } else { - plat_sleep_ms(wait); - } - timers(s, plat_now_ms()); - publish(s); - TickHook ticks[TICK_HOOKS]; - plat_mutex_lock(&s->lock); - int nt = s->n_ticks; - memcpy(ticks, s->ticks, sizeof ticks); - plat_mutex_unlock(&s->lock); - for (int i = 0; i < nt; i++) ticks[i].fn(plat_now_ms(), ticks[i].user); -} - -// ---- the session's interface - -CoopSess *coop_sess_new(const CoopParams *p) { - CoopSess *s = calloc(1, sizeof *s); - if (!s) return NULL; - s->out = calloc(OUT_SLOTS, sizeof *s->out); - if (!s->out) { - free(s); - return NULL; - } - copy_str(s->tag, sizeof s->tag, p->tag); - s->ping_ms = p->ping_ms ? p->ping_ms : PING_MS; - s->silence_ms = p->silence_ms ? p->silence_ms : SILENCE_MS; - s->burst_gap_ms = p->burst_gap_ms ? p->burst_gap_ms : BURST_GAP_MS; - s->backoff_first_ms = p->backoff_first_ms ? p->backoff_first_ms : BACKOFF_FIRST_MS; - s->backoff_max_ms = p->backoff_max_ms ? p->backoff_max_ms : BACKOFF_MAX_MS; - plat_mutex_init(&s->lock); - plat_event_init(&s->off_done, 1, 0); - uint64_t seed = 0; - if (!plat_random(&seed, sizeof seed) || !seed) seed = plat_now_ns() | 1; - s->rng = seed; - s->state = COOP_OFF; - s->view.state = COOP_OFF; - coop_sess_configure(s, p); - apply_settings(s, plat_now_ms()); // nothing else runs yet: the first step need not wait for it - publish(s); - return s; -} - -void coop_sess_free(CoopSess *s) { - if (!s) return; - close_socket(s); - free(s->out); - free(s); -} - -void coop_sess_configure(CoopSess *s, const CoopParams *p) { - plat_mutex_lock(&s->lock); - copy_str(s->pending.server, sizeof s->pending.server, p->server); - char clean[COOP_NAME_MAX + 1]; - coop_name_clean(clean, sizeof clean, (const uint8_t *)(p->name ? p->name : ""), p->name ? strlen(p->name) : 0); - copy_str(s->pending.name, sizeof s->pending.name, clean); - s->pending.client_id = p->client_id; - s->pending.local_port = p->local_port; - s->pending.drop_pct = p->drop_pct > 100 ? 100 : p->drop_pct; - s->pending.retry = p->retry; - s->pending.on = p->on; - if (!p->on) plat_event_reset(&s->off_done); - s->cfg_pending = 1; - plat_mutex_unlock(&s->lock); -} - -void coop_sess_stop(CoopSess *s) { - plat_mutex_lock(&s->lock); - s->pending.on = 0; - plat_event_reset(&s->off_done); - s->cfg_pending = 1; - plat_mutex_unlock(&s->lock); -} - -int coop_sess_command(CoopSess *s, CoopCmd cmd, const char *secret) { - char canon[COOP_SECRET_LEN + 1] = ""; - if (cmd != COOP_CMD_CREATE && cmd != COOP_CMD_JOIN && cmd != COOP_CMD_LEAVE) return 0; - if (cmd == COOP_CMD_JOIN || (cmd == COOP_CMD_CREATE && secret && *secret)) { - if (!secret || !coop_secret_normalize(secret, canon)) return 0; - } else if (cmd == COOP_CMD_CREATE) { // a new room: a new secret, which only this player knows until it passes it on - uint8_t r[32]; - if (!plat_random(r, sizeof r) || !coop_secret_from_random(r, canon)) return 0; - } - plat_mutex_lock(&s->lock); - s->cmd = (int)cmd; - copy_str(s->cmd_secret, sizeof s->cmd_secret, canon); - plat_mutex_unlock(&s->lock); - return 1; -} - -int coop_sess_remove(CoopSess *s, int member) { - if (member < 1 || member > COOP_MAX_MEMBERS || !__atomic_load_n(&s->pub_joined, __ATOMIC_ACQUIRE)) return 0; - plat_mutex_lock(&s->lock); - s->cmd_remove = member, s->cmd_remove_session = 0; - for (int i = 0; i < s->view.n; i++) - if (s->view.member[i].id == member) s->cmd_remove_session = s->view.member[i].session; - plat_mutex_unlock(&s->lock); - return 1; -} - -int coop_sess_wait_off(CoopSess *s, uint32_t ms) { return plat_event_wait_ns(&s->off_done, (uint64_t)ms * 1000000u); } - -int coop_sess_on_message(CoopSess *s, uint8_t kind, CoopMsgFn fn, void *user) { - s->msg[kind].user = user; - __atomic_store_n(&s->msg[kind].fn, fn, __ATOMIC_RELEASE); - return 1; -} - -int coop_sess_on_view(CoopSess *s, CoopViewFn fn, void *user) { - plat_mutex_lock(&s->lock); - int ok = s->n_hooks < VIEW_HOOKS; - if (ok) s->hooks[s->n_hooks++] = (ViewHook){fn, user}; - plat_mutex_unlock(&s->lock); - return ok; -} - -int coop_sess_on_tick(CoopSess *s, CoopTickFn fn, void *user) { - plat_mutex_lock(&s->lock); - int ok = s->n_ticks < TICK_HOOKS; - if (ok) s->ticks[s->n_ticks++] = (TickHook){fn, user}; - plat_mutex_unlock(&s->lock); - return ok; -} - -void coop_sess_view(CoopSess *s, CoopView *out) { - uint64_t now = plat_now_ms(); - plat_mutex_lock(&s->lock); - *out = s->view; - for (int i = 0; i < out->n; i++) { - uint64_t at = s->heard_at[out->member[i].id]; - out->member[i].heard_ms = out->member[i].id == out->self ? 0 : at ? (uint32_t)(now - at) : UINT32_MAX; - } - out->tx = __atomic_load_n(&s->n_tx, __ATOMIC_RELAXED); // counted by the stepping thread without the lock - out->rx = __atomic_load_n(&s->n_rx, __ATOMIC_RELAXED); - out->lost = __atomic_load_n(&s->n_lost, __ATOMIC_RELAXED); - plat_mutex_unlock(&s->lock); -} - -// ---- the process's session - -static CoopSess *g_sess; -static PlatOnce g_once = PLAT_ONCE_INIT; -static int g_started, g_final; - -static void make_session(void *arg) { - (void)arg; - CoopParams p = {0}; - g_sess = coop_sess_new(&p); - if (!g_sess) rt_log("coop: out of memory"); -} - -static CoopSess *process_session(void) { - plat_once(&g_once, make_session, NULL); - return g_sess; -} - -// The player's id towards the relay: random for each start of the game, so that nothing ties one session to another, -// and never sent to the other players. The default name's digits are random too, not taken from it. 0 when the system's -// random source failed: an id others could guess would let them take this player's place, so co-op stays off. -static uint64_t g_client_id; -static unsigned g_name_digits; -static PlatOnce g_client_once = PLAT_ONCE_INIT; -static void make_client_id(void *arg) { - (void)arg; - uint64_t r[2] = {0, 0}; - if (!plat_random(r, sizeof r)) return; - g_client_id = r[0] ? r[0] : 1; - g_name_digits = (unsigned)(r[1] & 0xFFFF); -} - -uint64_t coop_client_id(void) { - plat_once(&g_client_once, make_client_id, NULL); - return g_client_id; -} - -static void coop_main(void *arg) { - (void)arg; - CoopSess *s = process_session(); - unsigned seen = ~0u; - int on = 0, said_random = 0; - uint64_t client_id = 0; - char name[COOP_NAME_MAX + 1] = "", server[160] = "", removed[24] = ""; - copy_str(removed, sizeof removed, cfg_get("NX2NAVI_COOP_REMOVE")); // the value at start is only noted: it acts on nothing - for (;;) { - unsigned v = cfg_version(); - if (v != seen && !__atomic_load_n(&g_final, __ATOMIC_ACQUIRE)) { // the settings changed (the menu writes them) - seen = v; - int want = cfg_bool("NX2NAVI_COOP"); - const char *auto_mode = cfg_get("NX2NAVI_COOP_AUTO"), *drop = cfg_get("NX2NAVI_COOP_DROP"); - int automatic = auto_mode && (!strcmp(auto_mode, "create") || !strcmp(auto_mode, "join")); - if (want) { - if (!client_id) client_id = coop_client_id(); - if (!client_id) { - if (!said_random) rt_log("coop: the system's random source failed; co-op stays off"); - said_random = 1, want = 0; - } - const char *n = cfg_value("NX2NAVI_COOP_NAME"); - coop_name_clean(name, sizeof name, (const uint8_t *)n, strlen(n)); - if (!name[0]) snprintf(name, sizeof name, "Player-%04x", g_name_digits); - copy_str(server, sizeof server, cfg_value("NX2NAVI_COOP_SERVER")); - } - CoopParams p = {.server = server, .name = name, .client_id = client_id, .on = want, .retry = automatic, - .local_port = (uint16_t)cfg_int("NX2NAVI_COOP_PORT"), .drop_pct = drop ? (unsigned)atoi(drop) : 0}; - coop_sess_configure(s, &p); - if (want && !on && auto_mode && *auto_mode && strcmp(auto_mode, "off") && !automatic) - rt_log("coop: COOP_AUTO must be off, create or join"); - if (want && !on && automatic) { // a session at launch, for tests and LAN parties - const char *room = cfg_get("NX2NAVI_COOP_ROOM"); - char secret[COOP_SECRET_LEN + 1] = ""; - int has = room && *room && coop_secret_normalize(room, secret); - int join = !strcmp(auto_mode, "join"); - if (room && *room && !has) rt_log("coop: COOP_ROOM is not a room secret (18 characters, XXXXXX-XXXXXX-XXXXXX)"); - if (join && !has) rt_log("coop: COOP_AUTO=join needs COOP_ROOM"); - else coop_sess_command(s, join ? COOP_CMD_JOIN : COOP_CMD_CREATE, secret); - } - // the maker removes a member: COOP_REMOVE = its member id (until the menu has a row for it); a key's - // fingerprint there is progress sync's (coop_game.c), for a member that left - const char *rm = cfg_get("NX2NAVI_COOP_REMOVE"); - if (want && rm && *rm && strcmp(rm, removed) && strlen(rm) <= 2 && strspn(rm, "0123456789") == strlen(rm)) coop_remove_member(atoi(rm)); - copy_str(removed, sizeof removed, rm); - on = want; - } - coop_sess_step(s); - } -} - -void coop_init(void) { - if (!process_session() || g_started) return; - g_started = 1; - if (!plat_thread_spawn(coop_main, NULL, "coop", 0)) rt_log("coop: cannot start the co-op thread"); -} - -void coop_shutdown(void) { - if (!g_started) return; - CoopSess *s = process_session(); - __atomic_store_n(&g_final, 1, __ATOMIC_RELEASE); - int st = __atomic_load_n(&s->pub_state, __ATOMIC_ACQUIRE); - if (st != COOP_JOINED && st != COOP_RECONNECTING) return; // no room to say goodbye to - coop_sess_stop(s); - coop_sess_wait_off(s, 400); -} - -int coop_command(CoopCmd cmd, const char *secret) { - CoopSess *s = process_session(); - if (!s || __atomic_load_n(&s->pub_state, __ATOMIC_ACQUIRE) == COOP_OFF) return 0; - return coop_sess_command(s, cmd, secret); -} - -static CoopRemoveFn g_remove_fn; -static void *g_remove_user; - -int coop_on_remove(CoopRemoveFn fn, void *user) { - g_remove_user = user; - __atomic_store_n(&g_remove_fn, fn, __ATOMIC_RELEASE); - return 1; -} - -int coop_remove_member(int member) { - CoopSess *s = process_session(); - if (!s) return 0; - CoopRemoveFn fn = __atomic_load_n(&g_remove_fn, __ATOMIC_ACQUIRE); - int signed_out = fn && fn(member, g_remove_user); - CoopView v; - coop_sess_view(s, &v); - int kicked = v.made && coop_sess_remove(s, member); // the relay takes a removal only from the member it marks - return signed_out || kicked; -} - -void coop_view_get(CoopView *out) { - CoopSess *s = process_session(); - if (s) coop_sess_view(s, out); - else memset(out, 0, sizeof *out); -} - -int coop_send(uint8_t kind, const void *bytes, size_t n) { - CoopSess *s = process_session(); - return s && coop_sess_send(s, 0, kind, bytes, n); -} - -int coop_send_to(int member, uint8_t kind, const void *bytes, size_t n) { - CoopSess *s = process_session(); - return s && member > 0 && coop_sess_send(s, member, kind, bytes, n); -} - -int coop_on_message(uint8_t kind, CoopMsgFn fn, void *user) { - CoopSess *s = process_session(); - return s && coop_sess_on_message(s, kind, fn, user); -} - -int coop_on_view(CoopViewFn fn, void *user) { - CoopSess *s = process_session(); - return s && coop_sess_on_view(s, fn, user); -} - -int coop_on_tick(CoopTickFn fn, void *user) { - CoopSess *s = process_session(); - return s && coop_sess_on_tick(s, fn, user); -} diff --git a/runtime/coop.h b/runtime/coop.h deleted file mode 100644 index 1f454ee6..00000000 --- a/runtime/coop.h +++ /dev/null @@ -1,143 +0,0 @@ -// Co-op session layer: one host thread, "coop", owns a UDP socket to a relay (tools/coop_relay.py), keeps the player in a -// room of up to 8 and carries the messages the players send each other (coop_proto.h: the wire format). It never holds a -// virtual core and never touches game state: progress sync is a layer above this one, which send -// with coop_send and receive with coop_on_message. The game's own nn::socket stays offline. -// -// Threads. Everything below the "process" heading may be called from any thread; the callbacks run on the coop thread -// (keep them short, they hold up the socket) and must not call coop_command. coop_send copies the message into an outbox the -// coop thread flushes within about 8 ms; coop_view_get and coop_command take a lock the coop thread holds only to copy. -// -// The session is off until the COOP setting (nx2navi.ini [coop], the menu) is on; docs/coop.md has the settings. -#pragma once -#include -#include - -#include "coop_proto.h" - -typedef enum { - COOP_OFF, // the COOP setting is off: no socket, no thread work - COOP_LEFT, // on, in no room (the status says why after a failure) - COOP_CONNECTING, // reaching the relay to create or join a room - COOP_JOINED, // in a room - COOP_RECONNECTING, // the relay went silent; joining the same room again as the same member -} CoopState; - -typedef enum { COOP_CMD_CREATE = 1, COOP_CMD_JOIN, COOP_CMD_LEAVE } CoopCmd; - -typedef struct { - uint8_t id; // member id 1..8 - uint8_t away; // the relay has not heard from the member for a while - uint8_t maker; // made the room (the relay says so); the rules are the ones its key signs (coop_state.h) - char name[COOP_NAME_MAX + 1]; - uint64_t session; // its session in the room, as the relay lists it (progress sync binds its identity to it) - uint32_t heard_ms; // milliseconds since the last message of this member that opened with the room's key; - // UINT32_MAX never (a member the relay lists but that has not shown it knows the secret) -} CoopPeer; - -typedef struct { - CoopState state; - unsigned version; // changes with the state, the room, the roster and the status text - char code[COOP_SECRET_TEXT + 1]; // the room's secret, XXXXXX-XXXXXX-XXXXXX, in a room or while joining one; else "" - char label[8]; // its first characters, for logs ("XXX…") - int self; // this player's member id; 0 outside a room - int maker; // the member id of the room's maker, 0 when the maker is not in the room - int made; // this player's session made the room (its own CREATE, not the relay's mark: its - // settings are the room's rules, coop_state.h) - uint64_t session; // this player's session in the room, the one it joined with (0 outside a room); a roster - // may list another for it, which progress sync does not sign for - char name[COOP_NAME_MAX + 1]; // this player's own name (COOP_NAME, cleaned), whatever the relay's roster says - int n; // members of the room, this player included, by id - CoopPeer member[COOP_MAX_MEMBERS]; - uint32_t ping_ms; // round trip to the relay; 0 until measured - char status[96]; // what is going on or what went wrong, in words - uint32_t tx, rx, lost; // datagrams sent, accepted, and received but not accepted (bad tag, wrong sender, replay) -} CoopView; - -// ---- the process's session (the one the settings drive) - -// Starts the coop thread. It re-reads the settings when cfg_version changes and does nothing while COOP is off. -void coop_init(void); -// Leaves the room and says BYE to the others, best effort, for the three ways the process ends (boot.c, svc.c, -// gfx_present.c, before fs_save_flush): waits at most about 0.4 s for the coop thread, then returns whatever happened. -// A session still connecting is left running (the process ends next; the relay drops a member it never hears from). -void coop_shutdown(void); -// Create, join or leave a room. secret (as typed: any case, '-' and spaces allowed) is required by JOIN; CREATE makes a -// room of that secret, or of a new random one when it is NULL or empty (the view shows it: the player passes it on to -// the others out of band). 1 when queued, 0 when the secret is not one or co-op is off. The outcome shows in coop_view_get. -int coop_command(CoopCmd cmd, const char *secret); -// The room's maker removes a member (its id): every game drops its identity (progress sync signs the removal with the -// maker's key, coop_state.h), and when the relay names this player the maker, the relay drops the member and refuses -// it again under that identity. 1 when either was done. -int coop_remove_member(int member); -// fn(member, user) when coop_remove_member is called, on the caller's thread, before the relay is asked: the layer that -// signs removals (progress sync) registers here; 1 when it removed the member. One hook. -typedef int (*CoopRemoveFn)(int member, void *user); -int coop_on_remove(CoopRemoveFn fn, void *user); -void coop_view_get(CoopView *out); -// This player's id towards the relay: random for each start of the game, never sent to the other players. -uint64_t coop_client_id(void); - -// Peer messages. kind is COOP_K_*; bytes follow it in the datagram. coop_send goes to every other member, coop_send_to to -// one (a member id). 1 when queued, 0 when not in a room, the message is too long (COOP_MAX_MESSAGE bytes) or the -// outbox is full: a message is a datagram and may be lost anyway. Every message is sealed with the room's message key. -int coop_send(uint8_t kind, const void *bytes, size_t n); -int coop_send_to(int member, uint8_t kind, const void *bytes, size_t n); -// fn(from member id, the message bytes after the kind, their count, user) for every message of that kind that opened -// with the room's key and came from the session the roster names for its sender. One handler per kind; register before -// the message can arrive (at start-up). Not once only: the window refuses a repeat within the last 64 it took, but -// four older messages in a row start it again, after which a recorded message of that session can arrive again (a -// relay without the secret can do that), and anyone with the secret can seal one again under a new counter. A handler -// for which a repeat or an old message matters keeps a counter of its own per sender and session inside its messages -// and takes only one above the last it took (a position message, say, so that an old pose does not move a player -// back); progress sync signs its counter, which only the sender can then move. -typedef void (*CoopMsgFn)(int from, const uint8_t *bytes, size_t n, void *user); -int coop_on_message(uint8_t kind, CoopMsgFn fn, void *user); -// fn(view, user) after the state, the room, the roster or the status changed; up to 8 hooks. A layer that sends a HELLO to -// every new member listens here. -typedef void (*CoopViewFn)(const CoopView *view, void *user); -int coop_on_view(CoopViewFn fn, void *user); -// fn(now in ms, user) at every step of the coop thread (every 8 ms or less in a room, 250 ms idle); up to 4 hooks. A layer -// with timers of its own (digests, repeated sends) runs them here. -typedef void (*CoopTickFn)(uint64_t now_ms, void *user); -int coop_on_tick(CoopTickFn fn, void *user); - -// "host", "host:port", "[v6]:port" or a bare IPv6 address into host (cap bytes) and port (default 24680). 0 when text is -// empty or does not fit. -int coop_server_parse(const char *text, char *host, size_t cap, uint16_t *port); - -// ---- a session of its own (--probe-coop runs two in one process; the process's session is one of these driven by the settings) -typedef struct CoopSess CoopSess; -typedef struct { - const char *tag; // names the session in log lines ("coop[A]: ..."); NULL for none - const char *server; // "host[:port]" - const char *name; // the player's name - uint64_t client_id; // the same one on every reconnect keeps the member id for a minute (with the rejoin token) - int on; // 0: off (COOP_OFF), the COOP setting; 1: on, in no room until a command says - uint16_t local_port; // 0: any - unsigned drop_pct; // percent of received datagrams thrown away, for tests - int retry; // a room the relay lost (a restart) is made or waited for again instead of ending the session - // timings in ms, 0 for the defaults (2000, 8000, 250, 1000, 15000); tests shorten them - uint32_t ping_ms, silence_ms, burst_gap_ms, backoff_first_ms, backoff_max_ms; -} CoopParams; -CoopSess *coop_sess_new(const CoopParams *p); // p's strings are copied -void coop_sess_free(CoopSess *s); // the stepping thread must be gone -// The stepping thread calls this in a loop: handles commands, the outbox, one batch of datagrams and the timers, waiting at -// most 8 ms (in a room), 50 ms (reaching the relay) or 250 ms (idle) for the next datagram. -void coop_sess_step(CoopSess *s); -int coop_sess_command(CoopSess *s, CoopCmd cmd, const char *secret); // as coop_command, for this session -int coop_sess_remove(CoopSess *s, int member); // as coop_remove_member -int coop_sess_send(CoopSess *s, int member, uint8_t kind, const void *bytes, size_t n); // member 0 or COOP_DST_ALL: all -int coop_sess_on_message(CoopSess *s, uint8_t kind, CoopMsgFn fn, void *user); -int coop_sess_on_view(CoopSess *s, CoopViewFn fn, void *user); -int coop_sess_on_tick(CoopSess *s, CoopTickFn fn, void *user); -void coop_sess_view(CoopSess *s, CoopView *out); -// Replaces the settings the session reads with those of p (the timings stay as they were made); the stepping thread -// applies them, on or off included. A new server ends the room; a new name, once the room is joined, joins it again under the new name a -// second after the last change, so typing a name does not rejoin at every letter. -void coop_sess_configure(CoopSess *s, const CoopParams *p); -// Ends the session as coop_sess_configure with on = 0 would: LEAVE and BYE, socket closed, state COOP_OFF. -void coop_sess_stop(CoopSess *s); -// Waits at most ms for the stepping thread to have carried out a stop (or a configure with on = 0); 1 when it did. -int coop_sess_wait_off(CoopSess *s, uint32_t ms); - -int probe_coop(void); // coop_probe.c: --probe-coop diff --git a/runtime/coop_app.h b/runtime/coop_app.h deleted file mode 100644 index 8b09134a..00000000 --- a/runtime/coop_app.h +++ /dev/null @@ -1,7 +0,0 @@ -// The relay NX2navi's co-op (coop.c) joins when coop_server is not set: the one place that default lives, like -// discord_app.h for the Rich Presence. Empty until the project hosts a relay: with no server the session stays idle and -// logs why, rather than sending a player's room code to a server nobody chose. Anyone can run tools/coop_relay.py and set -// coop_server in nx2navi.ini (docs/coop.md). -#pragma once - -#define NX2NAVI_COOP_SERVER_DEFAULT "" // "host" or "host:port"; the default port is 24680 diff --git a/runtime/coop_game.c b/runtime/coop_game.c deleted file mode 100644 index 594f3bdf..00000000 --- a/runtime/coop_game.c +++ /dev/null @@ -1,1228 +0,0 @@ -// Co-op progress sync, the game side (coop_game.h), for Ocarina 1.1.1. -// -// The flag database. The game keeps its progress in one manager, *(u64 *)(main+0x4e842a8), as typed containers of 0x60 -// bytes from +8: Bool at +0x008, Int at +0x0c8, UInt at +0x788. A container holds its entry count (+0x08), entries -// (+0x10), an open-addressing table of {u32 hash, u32 entry} slots probed from hash % capacity (+0x18, capacity +0x24) and a -// ring of pending sets (+0x28 capacity, +0x30 slots, +0x38 counter, its low 20 bits the count). A Bool entry is 0x18 bytes -// (+0x08 the reset kinds, +0x10 the save file, -1 none, +0x15 default, +0x16 current value), an Int or UInt entry 0x28 -// (+0x08, +0x10 the same, +0x18 default, +0x1c current). Structs group flags: the struct table (+0xc10 count, +0xc18 -// entries of 0x20, +0xc20 slots, +0xc2c capacity) maps a struct's hash to an entry whose member table (+0x08, capacity +0x14) -// holds {u32 member hash, u32 flag or struct hash} slots. Flags are named by MurmurHash3 of their names; the names stay out -// of this file, the hashes it needs are numbers below. -// -// Writes go through the game's own set routines, SetBool (main+0x682b20) and SetInt (main+0x163b70), with x0 the manager, -// x1 the value and x2 the hash: they queue the change on the ring and the game's per-frame drain (main+0xb99980) writes the -// current and saved values and its own bookkeeping, as for a change the game made. Reads are of the entries' current values. -// -// What is shared (docs/coop.md has the reasons): -// - progress: every Bool that goes to progress.sav (save file 0) and is reset only by a new game or a load (reset kinds 8, -// 16 and 32; flags with kind 1 or 256 revert at scene changes, 2 at a new day, 4 are options), except the songs, the -// first-get markers and the heart rewards below. Its register is 1 once the flag differs from its default (63 of them -// start true and count down). Story, world and dungeon flags are one category: the game's flags carry no mark of which -// is which, and their names do not reach the runtime. -// - items: the item counts of the pouch struct (except the lists below), the capacity struct (wallet, bag and quiver -// sizes), the magic meter's size, and the first-get struct's markers; the highest count wins. -// - songs: 13 first-get markers. quest: 11 pouch counts (medallions, stones, the stone of agony, the gerudo card). -// - keys: the small-key count of each dungeon (the dungeon struct's member); rupees: the wallet's count; ammunition: 7 -// pouch counts. These three are counters the game spends: shared, they are pools (coop_state.h), else per player. -// - per player always: the trade sequence, rented masks, the knife that breaks, amiibo items and one unknown seed item -// (a highest-count merge would hand back what the game took away); heart pieces and gold skulltula tokens, and the 11 -// heart reward flags (their pickups are mostly per-object flags this layer does not write, so a shared count would pay a -// piece twice); life, the magic in the meter, bottle contents, equipment worn, the time of day, Link's place. -#include -#include -#include -#include - -#include "cfg.h" -#include "coop.h" -#include "coop_game.h" -#include "coop_state.h" -#include "fs.h" -#include "platform/platform.h" - -#define MGR_PTR 0x7004e842a8ull -#define SET_BOOL_FN 0x7000682b20ull -#define SET_INT_FN 0x7000163b70ull - -enum { - C_BOOL = 0x008, C_INT = 0x0c8, C_UINT = 0x788, - CN_COUNT = 0x08, CN_ENTRIES = 0x10, CN_TABLE = 0x18, CN_CAP = 0x24, CN_RING_CAP = 0x28, CN_RING = 0x30, CN_RING_CTR = 0x38, - BOOL_SIZE = 0x18, INT_SIZE = 0x28, - E_RESET = 0x08, E_FILE = 0x10, B_DEFAULT = 0x15, B_CURRENT = 0x16, I_CURRENT = 0x1c, - ST_COUNT = 0xc10, ST_ENTRIES = 0xc18, ST_TABLE = 0xc20, ST_CAP = 0xc2c, ST_SIZE = 0x20, STE_MEMBERS = 0x08, STE_MCAP = 0x14, - MGR_SIZE = 0xd80, - RESET_NEW_GAME = 8 | 16 | 32, RESET_TEMPORARY = 1 | 2 | 4 | 256, - SLICES = 4, // a quarter of the flags is read each frame: every flag every 4 frames - APPLY_PER_FRAME = 16, - INT_MAX_ITEM = 999, // the highest item count or counter the room may set: the game's counters show three digits - KEY_MAX = 15, // the most small keys of one dungeon the room may set (the most any dungeon has is 9) - CLOCK_MS = 2500, // the game clock advanced this recently: the game is being played -}; - -// Struct and member hashes (MurmurHash3 of the names the game uses). -#define S_POUCH 0x3da094e3u // item counts, one struct per item -#define M_STOCK 0x22344481u // an item's count -#define S_FIRST_GET 0xade0ef78u // "got it once" markers, one per item -#define S_DUNGEON 0xf5a32cdcu // one struct per dungeon: map, compass, boss key, small keys -#define M_SMALL_KEYS 0x1af87831u -#define S_CAPACITY 0xf80f8077u // wallet, bomb bag, quiver, seed bag, stick and nut capacities -#define F_RUPEES 0xa77921d7u -#define F_MAGIC_SIZE 0x12d63a97u -#define F_PLAY_CLOCK 0xe53eb6d1u // UInt: seconds played, advanced while the game runs (on the title screen too) -#define F_LOADING 0xf5deef0au // Bool: a save is being loaded -#define PLAYER_PTR 0x7004e371d0ull // [main+0x4e371d0] + 0x480: the Player actor, none before the first load - -// Scene changes in progress (Bool, not saved): nothing is read or written while one is set. -static const uint32_t transition_flags[] = {0x9a9506e7, 0xf8633c7f, 0xf96989b6, 0xe19dfdd6}; -static const uint32_t song_flags[] = {0x01c20002, 0xb492ac33, 0xd3f8bfcc, 0x165dd1ad, 0x03b3450e, 0x2b39dd4f, 0xdc0a0b54, - 0x49041ab9, 0x3fa63986, 0xc2fb95f4, 0x802a36a8, 0xd6320ba4, 0x5e86fb4d}; -static const uint32_t heart_reward_flags[] = {0x6b8fc751, 0x1d1793e9, 0xb5127e75, 0xe41e1597, 0x98ca7aab, 0xf1891f6e, - 0x3f43245b, 0x1657d7d8, 0xa666baa8, 0x5ff7aa30, 0xf2fab510}; -static const uint32_t quest_counts[] = {0x692d57cb, 0x5ce254a6, 0x0151a68f, 0x0c7a5fd0, 0x204d2063, 0x235c7d9a, - 0xcbd1e58c, 0x7d69ad47, 0xb8e3bf45, 0xf3b14ce2, 0xfeb4d56b}; -static const uint32_t own_counts[] = {0x07f10aaf, 0x0165e7ca, 0x51d89ef0, 0x07de3e6f, 0x58c9bc78, 0xa862d276, 0x2815c82a, - 0x897cf7eb, 0x919ea324, 0x5c41a6b1, 0xded8e8d3, 0x24596e8e, 0xb6ca6093, 0x6be5c3b0, - 0x7e607033, 0x0e94a8df, 0xfc508da7, 0xb5c10899, 0x47b2d775, 0xf046629e, 0x96459cd3, - 0x924e9a46, 0xf5e647da, 0x486c59a4, 0x49d1c374, 0x20820479, 0x396a5c02, 0x8d02f5ea, - 0x600e29eb, 0x51ec97b3, 0x04735978}; -// Ammunition counts and the capacity member that caps each (0: none known). -static const struct { uint32_t count, cap_member; } ammo[] = { - {0xdb1bcd4f, 0xc0f40041}, {0xdf7c517a, 0}, {0x518ec74b, 0x59663556}, {0xf30d9f54, 0x83d2e406}, - {0x5fadf7fc, 0x483d0f84}, {0xb9494411, 0xf9e54315}, {0xb003124d, 0}, -}; -#define CAP_RUPEES 0x3b58ca14u // the capacity member of the wallet - -static int in_list(const uint32_t *list, size_t n, uint32_t h) { - for (size_t i = 0; i < n; i++) - if (list[i] == h) return 1; - return 0; -} -#define IN(list, h) in_list(list, sizeof list / sizeof list[0], h) - -typedef struct { - uint32_t hash; - uint8_t is_int, cat, inv; - uint32_t max; // the highest register value the room may set: 1 for a Bool, KEY_MAX or INT_MAX_ITEM for an Int - int32_t last; // the raw value last reported - uint64_t addr; // the entry - uint64_t cap_addr; // a pool counter's capacity entry (Int), 0 none -} GFlag; - -// A set of hashes (open addressing, 0 empty), for the struct memberships while the table is built. -typedef struct { uint32_t *h; unsigned cap; } HSet; -static void hset_add(HSet *s, uint32_t h) { - if (!h || !s->h) return; - unsigned i = h & (s->cap - 1); - while (s->h[i] && s->h[i] != h) i = (i + 1) & (s->cap - 1); - s->h[i] = h; -} -static int hset_has(const HSet *s, uint32_t h) { - if (!h || !s->h) return 0; - unsigned i = h & (s->cap - 1); - while (s->h[i]) { - if (s->h[i] == h) return 1; - i = (i + 1) & (s->cap - 1); - } - return 0; -} - -typedef struct { uint32_t hash; int32_t value; uint32_t frame; int done; } TestSet; -enum { PUB_CONTRIBUTORS = 24 }; // the pools' contributors the menu is shown (the maker's log hint takes as many) - -static struct { - // settings, re-read when cfg_version changes (the game thread's copy and the coop thread's) - unsigned cfg_seen, cfg_seen_net; - int on, said_version; - TestSet test[8]; // NX2NAVI_COOP_TEST_SET - int ntest; - - PlatMutex lock; // around eng - CoopSync *eng; - uint64_t self_id; - - // the game thread's - uint64_t mgr, bool_entries, int_entries; - GFlag *flags; // flags first, then the pool counters - int *by_hash; // indices of flags in hash order - int nflags, npools, cap; - unsigned count[COOP_CAT_N]; - int bound, gate, slice; - int loaded; // a save was loaded since the title screen last started (its clock runs too) - int titles; // title_starts when last seen - int player; // the Player actor stands in the world (player_in_world) - uint32_t clock; - uint64_t clock_at, clock_entry, loading_entry, transition_entry[4]; - uint32_t frames; // frames played since the save was bound - unsigned applied[COOP_CAT_N], applied_total; - uint64_t applied_log_at; - unsigned rules_seen; - uint32_t schema; // of the table: games merge only with the same - unsigned rejected_seen, ignored_seen; - uint64_t checked_at; - - // the gate (G.lock; open is read by the game thread without it): nothing from a room is applied before the player - // accepted what the room is (the engine's offer: identities, rules and their key, the save) and a copy of the save - // exists to go back to; coop_sync_take checks it again under the lock - CoopView view; // the last view (the relay's names of members not identified yet) - char first_rooms[8][COOP_SECRET_TEXT + 1]; // rooms whose first copy of the save was made in this start - unsigned nfirst; - uint32_t logged; // the offer last logged - int logged_unid, said_disagree; // and its unidentified members; the members' rules differed (logged) - int said_always; - int open; // the current offer is accepted and the copy exists: the game thread applies - - // the maker's key for a room (the coop thread's): the room it was looked up for, and whether one is kept - char key_code[COOP_SECRET_TEXT + 1]; - uint8_t room_key[32]; - int has_room_key, said_random; - // the maker's remedies by setting (coop_remove = a key's fingerprint, coop_reset_pools): the values last acted on - // (the first ones only noted: a value left from before acts on nothing) - char remove_seen[32], reset_seen[32]; - int cfg_primed; - uint64_t spent_seen, spent_at; // the contributors' spends last logged for the maker, and when - unsigned own_key_seen; // digests carrying this game's own key, as last logged - uint64_t own_key_said; - unsigned key_changes_seen, key_moves_seen; // a member's key that changed within its session, an accepted key - // under another number or name: as last logged - // nx2navi.ini as it is now (cfg reads it at start only), once a second in a room: coop_accept, and the maker's - // coop_remove and coop_reset_pools as last acted on (the first reading is only noted) - uint64_t ini_at; - int ini_primed; - char ini_accept[32], ini_remove[32], ini_reset[32]; - - // what the menu reads and asks (coop_offer.h) without waiting for G.lock: copies the coop thread publishes at each - // step under pub_lock, which is held only for a copy, and requests the coop thread carries out at its next step - PlatMutex pub_lock; - CoopOffer pub; // the offer as offer_of gave it last (and its return value: pub_has) - int pub_has, pub_maker; // pub_maker: this game holds the key that signs the rules (coop_sync_is_maker) - CoopContributor pub_contrib[PUB_CONTRIBUTORS]; - int pub_ncontrib; - uint64_t pub_contrib_at; // when the contributors were published (at most every 500 ms; 0: at the next step) - uint32_t want_accept; // an offer number the menu accepted (atomic) - uint32_t pub_shown; // the members whose puppets are shown (coop_offer_shown; atomic) - // the maker's remedies the menu asked for, each with when (ms; 0 none): a request the coop thread did not take - // within 2 s (co-op went off meanwhile) is dropped, so that it never acts in a later room - uint64_t want_reset; // atomic - char want_remove[24]; // a key fingerprint, "" none (pub_lock, as want_remove_at) - uint64_t want_remove_at; -} G; - -#define TRACE_ON (rt_trace & RT_TRACE_COOP) - -// ---- reading the database - -static int rd_ok(uint64_t a, uint64_t n) { return a >= 0x1000 && mem_readable(a, n); } - -// The entry of hash in the container at base (stride bytes per entry), 0 when absent. -static uint64_t entry_of(uint64_t mgr, unsigned base, unsigned stride, uint32_t hash) { - uint64_t c = mgr + base, table = LD64(c + CN_TABLE), entries = LD64(c + CN_ENTRIES); - uint32_t cap = LD32(c + CN_CAP), count = LD32(c + CN_COUNT); - if (!hash || !cap || !table || !entries) return 0; - for (uint32_t i = hash % cap, k = 0; k < cap; k++, i = i + 1 == cap ? 0 : i + 1) { - uint32_t h = LD32(table + 8ull * i); - if (h == hash) { - uint32_t idx = LD32(table + 8ull * i + 4); - return entries + (uint64_t)(idx < count ? idx : 0) * stride; - } - if (!h) return 0; - } - return 0; -} - -static uint64_t struct_of(uint64_t mgr, uint32_t hash) { - uint64_t table = LD64(mgr + ST_TABLE), entries = LD64(mgr + ST_ENTRIES); - uint32_t cap = LD32(mgr + ST_CAP), count = LD32(mgr + ST_COUNT); - if (!cap || !table || !entries || !rd_ok(table, 8ull * cap)) return 0; - for (uint32_t i = hash % cap, k = 0; k < cap; k++, i = i + 1 == cap ? 0 : i + 1) { - uint32_t h = LD32(table + 8ull * i); - if (h == hash) { - uint32_t idx = LD32(table + 8ull * i + 4); - uint64_t e = entries + (uint64_t)(idx < count ? idx : 0) * ST_SIZE; - return rd_ok(e, ST_SIZE) ? e : 0; - } - if (!h) return 0; - } - return 0; -} - -// fn(member hash, flag or struct hash, user) for each member of struct entry e. -static void members(uint64_t e, void (*fn)(uint32_t m, uint32_t v, void *u), void *u) { - uint64_t t = LD64(e + STE_MEMBERS); - uint32_t cap = LD32(e + STE_MCAP); - if (!t || !cap || cap > 65536 || !rd_ok(t, 8ull * cap)) return; - for (uint32_t i = 0; i < cap; i++) { - uint32_t m = LD32(t + 8ull * i); - if (m) fn(m, LD32(t + 8ull * i + 4), u); - } -} - -typedef struct { uint64_t mgr; HSet *set; uint32_t want; } Walk; -static void add_member(uint32_t m, uint32_t v, void *u) { (void)m, hset_add(((Walk *)u)->set, v); } -static void pick(uint32_t m, uint32_t v, void *u) { - Walk *w = u; - if (m == w->want) hset_add(w->set, v); -} -// A struct of structs: the flag of member want in each. -static void add_nested(uint32_t m, uint32_t v, void *u) { - (void)m; - Walk *w = u; - uint64_t e = struct_of(w->mgr, v); - if (e) members(e, pick, w); -} - -typedef struct { uint64_t mgr; uint32_t member; uint64_t flag; } FindMember; -static void find_member(uint32_t m, uint32_t v, void *u) { - FindMember *f = u; - if (m == f->member) f->flag = entry_of(f->mgr, C_INT, INT_SIZE, v); -} -// The Int entry a struct member names, 0 when absent. -static uint64_t member_entry(uint64_t mgr, uint32_t s, uint32_t member) { - uint64_t e = struct_of(mgr, s); - FindMember f = {mgr, member, 0}; - if (e) members(e, find_member, &f); - return f.flag; -} - -static GFlag *add_flag(void) { - if (G.nflags == G.cap) { - int nc = G.cap ? G.cap * 2 : 4096; - GFlag *nf = realloc(G.flags, (size_t)nc * sizeof *nf); - if (!nf) return NULL; - G.flags = nf, G.cap = nc; - } - GFlag *f = &G.flags[G.nflags++]; - memset(f, 0, sizeof *f); - return f; -} - -static int cmp_pool_last(const void *a, const void *b) { - return coop_cat_is_pool(((const GFlag *)a)->cat) - coop_cat_is_pool(((const GFlag *)b)->cat); -} - -static int cmp_by_hash(const void *a, const void *b) { - uint32_t x = G.flags[*(const int *)a].hash, y = G.flags[*(const int *)b].hash; - return x < y ? -1 : x > y; -} - -// The classified table of the manager's flags (the header comment's rules). 0 when the database is not readable. -static int build_table(uint64_t mgr) { - G.nflags = G.npools = 0; - memset(G.count, 0, sizeof G.count); - HSet first_get = {0}, pouch = {0}, keys = {0}, capacity = {0}; - HSet *sets[] = {&first_get, &pouch, &keys, &capacity}; - for (int i = 0; i < 4; i++) { - sets[i]->cap = 1024; - sets[i]->h = calloc(sets[i]->cap, sizeof *sets[i]->h); - } - uint64_t e; - Walk w = {mgr, &first_get, 0}; - if ((e = struct_of(mgr, S_FIRST_GET))) members(e, add_member, &w); - w.set = &pouch, w.want = M_STOCK; - if ((e = struct_of(mgr, S_POUCH))) members(e, add_nested, &w); - w.set = &keys, w.want = M_SMALL_KEYS; - if ((e = struct_of(mgr, S_DUNGEON))) members(e, add_nested, &w); - w.set = &capacity; - if ((e = struct_of(mgr, S_CAPACITY))) members(e, add_member, &w); - - for (int type = 0; type < 2; type++) { - unsigned base = type ? C_INT : C_BOOL, stride = type ? INT_SIZE : BOOL_SIZE; - uint64_t c = mgr + base, table = LD64(c + CN_TABLE), entries = LD64(c + CN_ENTRIES); - uint32_t cap = LD32(c + CN_CAP), count = LD32(c + CN_COUNT); - if (!cap || !count || count > 1000000 || !rd_ok(table, 8ull * cap) || !rd_ok(entries, (uint64_t)count * stride)) continue; - for (uint32_t i = 0; i < cap; i++) { - uint32_t h = LD32(table + 8ull * i), idx = LD32(table + 8ull * i + 4); - if (!h || idx >= count) continue; - uint64_t a = entries + (uint64_t)idx * stride; - uint32_t reset = LD32(a + E_RESET); - int32_t file = (int32_t)LD32(a + E_FILE); - int cat = -1; - uint8_t inv = 0; - uint64_t cap_addr = 0; - if (!type) { - if (file != 0 || reset & RESET_TEMPORARY || !(reset & RESET_NEW_GAME) || IN(heart_reward_flags, h)) continue; - cat = IN(song_flags, h) ? COOP_CAT_SONGS : hset_has(&first_get, h) ? COOP_CAT_ITEMS : COOP_CAT_PROGRESS; - inv = LD8(a + B_DEFAULT) != 0; - } else if (h == F_RUPEES) { - cat = COOP_CAT_RUPEES; - cap_addr = member_entry(mgr, S_CAPACITY, CAP_RUPEES); - } else if (hset_has(&keys, h)) { - cat = COOP_CAT_KEYS; - } else if (hset_has(&pouch, h)) { - if (IN(own_counts, h)) continue; - cat = IN(quest_counts, h) ? COOP_CAT_QUEST : COOP_CAT_ITEMS; - for (size_t k = 0; k < sizeof ammo / sizeof ammo[0]; k++) - if (ammo[k].count == h) { - cat = COOP_CAT_AMMO; - if (ammo[k].cap_member) cap_addr = member_entry(mgr, S_CAPACITY, ammo[k].cap_member); - } - } else if (hset_has(&capacity, h) || h == F_MAGIC_SIZE) { - cat = COOP_CAT_ITEMS; - } - if (cat < 0) continue; - GFlag *f = add_flag(); - if (!f) break; - f->hash = h, f->is_int = (uint8_t)type, f->cat = (uint8_t)cat, f->inv = inv, f->addr = a, f->cap_addr = cap_addr; - f->max = !type ? 1 : cat == COOP_CAT_KEYS ? KEY_MAX : INT_MAX_ITEM; - G.count[cat]++; - } - } - for (int i = 0; i < 4; i++) free(sets[i]->h); - qsort(G.flags, (size_t)G.nflags, sizeof *G.flags, cmp_pool_last); // the scan reads flags in slices, the pools every frame - for (int i = 0; i < G.nflags; i++) G.npools += coop_cat_is_pool(G.flags[i].cat); - free(G.by_hash); - G.by_hash = malloc((size_t)G.nflags * sizeof *G.by_hash); - if (!G.by_hash) return 0; - for (int i = 0; i < G.nflags; i++) G.by_hash[i] = i; - qsort(G.by_hash, (size_t)G.nflags, sizeof *G.by_hash, cmp_by_hash); - // the schema: this layer's version, the game's, and the table (every hash with its category, type and limit), so - // that two builds that classify or limit differently do not merge - uint64_t h = 0xcbf29ce484222325ull; - for (const char *p = "nx2navi-coop-sync/2"; *p; p++) h = (h ^ (uint8_t)*p) * 0x100000001b3ull; - h = (h ^ (uint64_t)rt_game_version) * 0x100000001b3ull; - for (int i = 0; i < G.nflags; i++) { - const GFlag *f = &G.flags[G.by_hash[i]]; - uint64_t row[2] = {f->hash | (uint64_t)f->cat << 32 | (uint64_t)f->is_int << 40, f->max}; - for (int k = 0; k < 16; k++) h = (h ^ (uint8_t)(row[k / 8] >> 8 * (k % 8))) * 0x100000001b3ull; - } - G.schema = (uint32_t)(h ^ h >> 32); - return G.nflags > 0; -} - -static int32_t raw_of(const GFlag *f) { return f->is_int ? (int32_t)LD32(f->addr + I_CURRENT) : LD8(f->addr + B_CURRENT); } -static uint32_t reg_of(const GFlag *f, int32_t raw) { - if (!f->is_int) return (raw != 0) != f->inv; // 1: differs from its default - return raw > 0 ? (uint32_t)raw : 0; -} - -static GFlag *flag_by_hash(uint32_t hash) { - int lo = 0, hi = G.nflags - 1; - while (lo <= hi) { - int mid = (lo + hi) / 2; - GFlag *f = &G.flags[G.by_hash[mid]]; - if (f->hash == hash) return f; - if (f->hash < hash) lo = mid + 1; - else hi = mid - 1; - } - return NULL; -} - -// A set of this hash waits on the Int ring (the game changed it this frame): a pool write now would overwrite it. -static int int_pending(uint64_t mgr, uint32_t hash) { - uint64_t c = mgr + C_INT, ring = LD64(c + CN_RING); - uint32_t n = LD32(c + CN_RING_CTR) & 0xFFFFF, cap = LD32(c + CN_RING_CAP); - if (!ring || n > cap || !rd_ok(ring, 12ull * n)) return 1; - for (uint32_t i = 0; i < n; i++) - if (LD32(ring + 12ull * i + 4) == hash) return 1; - return 0; -} - -// ---- settings - -static int mode_of(const char *key, int pool) { - const char *v = cfg_value(key); - if (!strcmp(v, "own")) return COOP_MODE_OWN; - return pool ? (!strcmp(v, "pool") ? COOP_MODE_POOL : COOP_MODE_OWN) : (!strcmp(v, "shared") ? COOP_MODE_SHARED : COOP_MODE_OWN); -} - -static void read_rules(CoopRules *r) { - static const char *const keys[COOP_CAT_N] = {"NX2NAVI_COOP_PROGRESS", "NX2NAVI_COOP_ITEMS", "NX2NAVI_COOP_SONGS", "NX2NAVI_COOP_QUEST", - "NX2NAVI_COOP_KEYS", "NX2NAVI_COOP_RUPEES", "NX2NAVI_COOP_AMMO"}; - coop_rules_default(r); - for (int c = 0; c < COOP_CAT_N; c++) r->mode[c] = (uint8_t)mode_of(keys[c], coop_cat_is_pool(c)); - CoopRules as_set = *r; - if (coop_rules_normalize(r)) { - char a[200], b[200]; - coop_rules_text(&as_set, a, sizeof a); - coop_rules_text(r, b, sizeof b); - rt_log("coop: sync: the settings ask for %s; a room made with them uses %s", a, b); - } -} - -// "hash=value@frame" items separated by ',' or ';' (hash in hex): set that flag at that frame of play, for tests. Read -// from the environment only, never from nx2navi.ini: an ini a player was handed cannot set flags in their save. -static void read_test_aid(void) { - G.ntest = 0; - const char *t = cfg_get("NX2NAVI_COOP_TEST_SET"); - if (t && *t && !cfg_from_env("NX2NAVI_COOP_TEST_SET")) { - rt_log("coop: test: COOP_TEST_SET is taken from the environment only; the nx2navi.ini line is ignored"); - return; - } - while (t && *t && G.ntest < (int)(sizeof G.test / sizeof G.test[0])) { - char *end; - unsigned long h = strtoul(t, &end, 16); - if (*end != '=') break; - long v = strtol(end + 1, &end, 0); - if (*end != '@') break; - unsigned long fr = strtoul(end + 1, &end, 0); - G.test[G.ntest++] = (TestSet){(uint32_t)h, (int32_t)v, (uint32_t)fr, 0}; - while (*end == ',' || *end == ';' || *end == ' ') end++; - t = end; - } - if (t && *t) rt_log("coop: test: COOP_TEST_SET is \"hash=value@frame,...\" (hex hash); stopped at \"%s\"", t); -} - -// ---- the engine's side of the coop thread - -static void trace_line(void *user, const char *line) { (void)user, rt_log("coop: sync: %s", line); } - -// The engine's limits (coop_sync_limits), with G.lock held: a flag or counter of the table in that category. -static uint32_t limit_fn(int cat, uint32_t hash, void *user) { - (void)user; - GFlag *f = flag_by_hash(hash); - return f && f->cat == cat ? f->max : 0; -} - -static int ensure_engine(void) { - if (G.eng) return 1; - G.self_id = coop_client_id(); - CoopSync *e = coop_sync_new(G.self_id, 0); // the schema is the table's, once a save is played (bind) - if (!e) return 0; - uint8_t key[16 + 32]; // the index's key, and the seed of this start's identities and offer numbers - if (!plat_random(key, sizeof key)) { // a seed others could guess would let them sign as this player: no co-op - coop_sync_free(e); - if (!G.said_random) rt_log("coop: sync: the system's random source failed; progress sync stays off"); - G.said_random = 1; - return 0; - } - coop_sync_index_key(e, key); - coop_sync_seed(e, key + 16); - coop_sync_limits(e, limit_fn, NULL); - if (TRACE_ON) coop_sync_trace(e, trace_line, NULL); - plat_mutex_lock(&G.lock); - G.eng = e; - plat_mutex_unlock(&G.lock); - return 1; -} - -static int send_fn(int member, uint8_t kind, const uint8_t *bytes, size_t n, void *user) { - (void)user; - return member ? coop_send_to(member, kind, bytes, n) : coop_send(kind, bytes, n); -} - -static void on_message(int from, const uint8_t *bytes, size_t n, void *user) { - uint8_t kind = (uint8_t)(uintptr_t)user; - plat_mutex_lock(&G.lock); - if (G.eng) coop_sync_receive(G.eng, from, kind, bytes, n, plat_now_ms()); - plat_mutex_unlock(&G.lock); -} - -// The maker's key for the room of code (32 random bytes) in /coop-keys, so that a maker whose game started -// again signs the room's rules and removals with the same key. The file is named by a hash of the room's secret, and -// the key is mixed with the secret before use (coop_state.c), so the file alone signs nothing; with the secret, whoever -// can read it acts as the room's maker, so the folder is this account's alone (0700, the file 0600). make: a room this -// session made gets one when it has none. 1 and the key in key when there is one. -static int room_key(const char *code, int make, uint8_t key[32]) { - uint8_t h[8]; - coop_blake2b(h, sizeof h, NULL, 0, code, strlen(code)); - char rel[64], path[1200], dir[1200]; - snprintf(rel, sizeof rel, "coop-keys/%02x%02x%02x%02x%02x%02x%02x%02x.key", h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7]); - if (!fs_profile_path(rel, path, sizeof path) || !fs_profile_path("coop-keys", dir, sizeof dir)) return 0; - uint64_t n = 0; - uint8_t *b = plat_file_read(path, &n); - int ok = b && n == 32, read = b != NULL; - if (ok) memcpy(key, b, 32); - free(b); - if (ok) { - plat_file_private(dir), plat_file_private(path); // a folder an earlier release made readable to others - return 1; - } - if (plat_file_exists(path)) - rt_log("coop: sync: the room's key file in %s %s; %s", dir, read ? "is damaged (not 32 bytes)" : "cannot be read", - make ? "a new key replaces it, so this game no longer signs the rules the room accepted from it" : "this game does not sign the room's rules"); - if (!make || !plat_random(key, 32)) return 0; - plat_file_mkdir_p(dir); - plat_file_private(dir); - if (!plat_file_write_atomic(path, key, 32)) { - rt_log("coop: sync: the room's key could not be kept in %s; a restart of this game ends its say over the room's rules", dir); - return 1; - } - plat_file_private(path); - rt_log("coop: sync: the key this game signs the room's rules with is kept in %s (with the room's secret it acts as the room's maker: " - "never hand out a copy of this folder or of the profile)", dir); - return 1; -} - -static void on_view(const CoopView *v, void *user) { - (void)user; - int ids[COOP_MAX_MEMBERS], n = 0; - uint64_t sessions[COOP_MAX_MEMBERS]; - int in_room = v->state == COOP_JOINED || v->state == COOP_RECONNECTING; - for (int i = 0; in_room && i < v->n; i++) { - // this player's own session is the one it joined with (the engine signs for it), whatever a roster lists - sessions[n] = v->member[i].id == v->self && v->session ? v->session : v->member[i].session; - ids[n++] = v->member[i].id; - } - if (in_room && (strcmp(G.key_code, v->code) || (v->made && !G.has_room_key))) { // the file once per room, outside the lock - snprintf(G.key_code, sizeof G.key_code, "%s", v->code); - G.has_room_key = room_key(v->code, v->made, G.room_key); - } - plat_mutex_lock(&G.lock); - G.view = *v; - if (G.eng) { - coop_sync_name(G.eng, v->name); // the player's own name, signed: the relay's roster may say another - if (in_room && G.has_room_key) coop_sync_room_seed(G.eng, v->code, G.room_key); - coop_sync_room(G.eng, in_room ? v->code : "", v->self, ids, sessions, n, in_room ? v->maker : 0, plat_now_ms()); - CoopRules mine; - read_rules(&mine); - coop_sync_settings(G.eng, &mine, in_room && v->made); - } - plat_mutex_unlock(&G.lock); -} - -// ---- the gate - -// coop_accept: "always" accepts every offer, but only from the environment, so an nx2navi.ini someone hands a player -// cannot make their game take a room without asking. A number (hex) accepts that offer from any source: it is keyed -// with a random value of this start of the game, so no file can hold it in advance. nx2navi.ini's line counts as the -// file is now (poll_ini), before the line read at start (which may say always, and that counts for nothing), so the -// player puts the number the log shows there while the game runs. 1 for always, else 0 and the number (0 none) in -// *number. -static int accept_setting(uint32_t *number) { - const char *v = cfg_value("NX2NAVI_COOP_ACCEPT"); - int env = cfg_from_env("NX2NAVI_COOP_ACCEPT"); - *number = 0; - if (env && !strcmp(v, "always")) return 1; - if (!env && G.ini_accept[0]) v = G.ini_accept; - if (!strcmp(v, "always")) { - if (!G.said_always) - rt_log("coop: sync: coop_accept = always is taken from the environment only (NX2NAVI_COOP_ACCEPT); the nx2navi.ini line is ignored"); - G.said_always = 1; - return 0; - } - char *end; - unsigned long n = strtoul(v, &end, 16); - if (*v && !*end) *number = (uint32_t)n; - return 0; -} - -// The engine's offer, with the relay's names for members not identified yet (G.lock held). -static int offer_of(CoopOffer *o, uint64_t now) { - int has = coop_sync_offer(G.eng, o, now); - for (int i = 0; i < o->n; i++) - for (int k = 0; !o->member[i].name[0] && k < G.view.n; k++) - if (G.view.member[k].id == o->member[i].id) snprintf(o->member[i].name, sizeof o->member[i].name, "%s", G.view.member[k].name); - return has; -} - -static void say_accepted(const CoopOffer *o, const char *how) { - rt_log("coop: sync: room accepted (%s); %s", how, coop_sync_copy_wanted(G.eng) ? "the save is copied first" : "values from it are applied"); - (void)o; -} - -// A key this player accepted (coop_offer_shown's question), G.lock held. -static int key_accepted(const char *key, void *eng) { - uint64_t kid; - return coop_sync_key_parse(key, &kid) && coop_sync_accepted_key(eng, kid); -} - -// What the menu and the puppets read (G.lock held): the offer offer_update settled on, whether this game is the maker, -// the members whose puppets are shown, and the contributors at most every 500 ms. -static void publish(const CoopOffer *o, int has, uint64_t now) { - int maker = coop_sync_is_maker(G.eng), fresh = !G.pub_contrib_at || now - G.pub_contrib_at >= 500; - CoopContributor c[PUB_CONTRIBUTORS]; - int n = fresh ? coop_sync_contributors(G.eng, c, PUB_CONTRIBUTORS) : 0; - __atomic_store_n(&G.pub_shown, coop_offer_shown(o, key_accepted, G.eng), __ATOMIC_RELEASE); - plat_mutex_lock(&G.pub_lock); - G.pub = *o, G.pub_has = has, G.pub_maker = maker; - if (fresh) memcpy(G.pub_contrib, c, sizeof *c * (size_t)n), G.pub_ncontrib = n, G.pub_contrib_at = now; - plat_mutex_unlock(&G.pub_lock); -} - -uint32_t coop_game_shown(void) { return __atomic_load_n(&G.pub_shown, __ATOMIC_ACQUIRE); } - -// No engine (co-op off, or between rooms): nothing to read. -static void publish_none(void) { - __atomic_store_n(&G.pub_shown, 0, __ATOMIC_RELEASE); - plat_mutex_lock(&G.pub_lock); - if (G.pub_has || G.pub.n || G.pub_ncontrib || G.pub_maker) - memset(&G.pub, 0, sizeof G.pub), G.pub_has = G.pub_maker = G.pub_ncontrib = 0, G.pub_contrib_at = 0; - plat_mutex_unlock(&G.pub_lock); -} - -// The offer of the room (the coop thread, G.lock held): accepted by the menu or the setting, logged when it changed and -// is not accepted, and published for the menu. -static void offer_update(uint64_t now) { - CoopOffer o; - uint32_t number; - int always = accept_setting(&number); - // the menu's accept, of the offer it showed: taken only while that offer is still the room's - uint32_t menu = __atomic_exchange_n(&G.want_accept, 0, __ATOMIC_ACQ_REL); - int has = offer_of(&o, now); - const char *how = !has || o.accepted ? NULL - : menu == o.token ? "in the menu" - : always ? "coop_accept = always" - : number == o.token ? "coop_accept names its number" - : NULL; - if (how && coop_sync_accept(G.eng, o.token)) { - say_accepted(&o, how); - has = offer_of(&o, now); - } - if (o.token && !o.accepted && (o.token != G.logged || o.unidentified != G.logged_unid)) { - int again = G.logged != 0; - G.logged = o.token, G.logged_unid = o.unidentified; - char who[COOP_MAX_MEMBERS * 160] = "", rules[200]; - size_t at = 0; - for (int i = 0; i < o.n && at < sizeof who; i++) { - const CoopOfferMember *m = &o.member[i]; - at += (size_t)snprintf(who + at, sizeof who - at, "%s%s (member %d, %s%s%s%s%s%s)", i ? ", " : "", m->name, m->id, m->heard ? "key " : "not identified", - m->key, m->accepted_as[0] ? ", a key you accepted as " : "", m->accepted_as, - m->maker ? ", signs the rules" : "", m->removed ? ", removed by the maker: ignored" : ""); - } - coop_rules_text(&o.rules, rules, sizeof rules); - rt_log("coop: sync: %s; in the room: %s; rules: %s, signed by key %s", - o.new_save ? "another save is played in the room: it gets the room's progress, which holds what the last save brought" - : again ? "the room changed (a member, the rules or the key that signs them)" : "a room to accept", - o.n ? who : "nobody else", rules, o.maker_key); - if (o.maker_moved) rt_log("coop: sync: the relay names another member as the maker than the one whose key signs the rules; the signed rules stay"); - if (o.disagree) - rt_log("coop: sync: members carry rules signed by different keys: before accepting, compare the key that signs these rules with the " - "maker's, by voice or chat"); - if (o.unidentified) - rt_log("coop: sync: %d member(s) in the room are not identified (never said who they are, or two keys present themselves for " - "one): the room cannot be accepted while they are in it (the maker can remove them, or everyone moves to a new room)", - o.unidentified); - else - rt_log("coop: sync: nothing from the room is applied to this game until you accept it: put coop_accept = %08x into nx2navi.ini " - "[coop] while the game runs (the number is new at each start of the game and changes with the room); the save is copied " - "to coop-backup in the profile folder first. Your key pools count only the players you accept, among those the maker lets " - "use the pools at all; your story flags take what the players you accept took from the players they accepted", o.token); - } - if (!o.token && o.disagree && !G.said_disagree) - rt_log("coop: sync: the members carry rules signed by different keys, so this game takes no rules and applies nothing; ask the " - "room's maker which key is theirs"); - G.said_disagree = !o.token && o.disagree; - __atomic_store_n(&G.open, coop_sync_open(G.eng), __ATOMIC_RELEASE); - publish(&o, has, now); -} - -int coop_game_offer(CoopOffer *out) { - plat_mutex_lock(&G.pub_lock); - *out = G.pub; - int has = G.pub_has; - plat_mutex_unlock(&G.pub_lock); - return has; -} - -int coop_game_accept(uint32_t token) { - plat_mutex_lock(&G.pub_lock); - int same = token && G.pub_has && token == G.pub.token, done = same && G.pub.accepted; - int ok = same && !G.pub.accepted && !G.pub.unidentified; - plat_mutex_unlock(&G.pub_lock); - if (ok) __atomic_store_n(&G.want_accept, token, __ATOMIC_RELEASE); - return ok || done; -} - -// The maker's pool reset (G.lock held), with the order that gives a drain back: a player still in the room when the -// pools are reset is admitted to the new ones with what it took already taken, so it is removed first. -static int reset_pools(void) { - int ok = G.eng && coop_sync_reset_pools(G.eng); - G.pub_contrib_at = 0; - if (ok) - rt_log("coop: sync: the pools are reset: every game joins them again with what they held for the players in the room; a player " - "who drained a pool and is still in the room keeps what it took, so remove it first (coop_remove), then reset"); - else rt_log("coop: sync: coop_reset_pools: only the room's maker resets the pools"); - return ok; -} - -// The same on the coop thread without G.lock: nx2navi.ini's value, and a reset the menu queued. -static int lock_and_reset_pools(void) { - plat_mutex_lock(&G.lock); - int ok = reset_pools(); - plat_mutex_unlock(&G.lock); - return ok; -} - -int coop_game_reset_pools(void) { - plat_mutex_lock(&G.pub_lock); - int maker = G.pub_maker; - plat_mutex_unlock(&G.pub_lock); - if (maker) __atomic_store_n(&G.want_reset, plat_now_ms(), __ATOMIC_RELEASE); - else rt_log("coop: sync: coop_reset_pools: only the room's maker resets the pools"); - return maker; -} - -// The maker removes a key (G.lock held): a member that left, or one in the room. -static int remove_key(const char *fingerprint) { - uint64_t kid; - int ok = G.eng && coop_sync_key_parse(fingerprint, &kid) && coop_sync_remove_key(G.eng, kid); - if (ok) rt_log("coop: sync: key %s removed: every game drops it and its pool records, and ignores it from now on", fingerprint); - else rt_log("coop: sync: key %s was not removed: only the room's maker removes, and only a key the room knows", fingerprint); - G.pub_contrib_at = 0; - return ok; -} - -int coop_game_remove_key(const char *fingerprint) { - uint64_t kid; - if (!fingerprint || !coop_sync_key_parse(fingerprint, &kid)) return 0; - plat_mutex_lock(&G.pub_lock); - int maker = G.pub_maker; - if (maker) snprintf(G.want_remove, sizeof G.want_remove, "%s", fingerprint), G.want_remove_at = plat_now_ms(); - plat_mutex_unlock(&G.pub_lock); - if (!maker) rt_log("coop: sync: key %s was not removed: only the room's maker removes", fingerprint); - return maker; -} - -int coop_game_contributors(CoopContributor *out, int max) { - plat_mutex_lock(&G.pub_lock); - int n = G.pub_ncontrib < max ? G.pub_ncontrib : max; - if (n > 0) memcpy(out, G.pub_contrib, sizeof *out * (size_t)n); - plat_mutex_unlock(&G.pub_lock); - return n > 0 ? n : 0; -} - -int coop_game_is_maker(void) { - plat_mutex_lock(&G.pub_lock); - int maker = G.pub_maker; - plat_mutex_unlock(&G.pub_lock); - return maker; -} - -// The maker's removal of a key by its fingerprint (the coop thread, without G.lock: a removal of a member in the room -// goes through coop_remove_member's hook), from nx2navi.ini's coop_remove or the menu. A member in the room that holds -// the key is removed at the relay too. -static void remove_by_fingerprint(const char *fp) { - uint64_t kid = 0, k; - int member = 0; - CoopOffer o; - plat_mutex_lock(&G.lock); - if (G.eng && coop_sync_key_parse(fp, &kid)) coop_sync_offer(G.eng, &o, plat_now_ms()); - else o.n = 0; - for (int i = 0; i < o.n; i++) - if (o.member[i].heard && coop_sync_key_parse(o.member[i].key, &k) && k == kid) member = o.member[i].id; - plat_mutex_unlock(&G.lock); - if (member && coop_remove_member(member)) return; // in the room: the relay removes it too - plat_mutex_lock(&G.lock); - remove_key(fp); - plat_mutex_unlock(&G.lock); -} - -// The maker's coop_remove (a key's fingerprint, which the log shows beside each member) and coop_reset_pools, and -// coop_accept, from nx2navi.ini as it is now, once a second while in a room (on the coop thread, without G.lock). A -// reading that fails (an editor saving the file, a lock) is skipped, not read as empty. A value acts once: when it comes -// back after a missed reading, or stays in the file, it acts on nothing again (coop_reset_pools = 0 lets the next value -// act). A member number is not taken from the file: numbers pass to other players as members come and go. - -static void poll_ini(uint64_t now) { - if (G.view.state != COOP_JOINED && G.view.state != COOP_RECONNECTING) return; // no room: nothing to act on, nothing read - if (G.ini_at && now - G.ini_at < 1000) return; - G.ini_at = now; - char acc[32], rm[32], rp[32]; - if (cfg_ini_now("coop_accept", acc, sizeof acc) < 0 || cfg_ini_now("coop_remove", rm, sizeof rm) < 0 || - cfg_ini_now("coop_reset_pools", rp, sizeof rp) < 0) - return; - snprintf(G.ini_accept, sizeof G.ini_accept, "%s", acc); - int primed = G.ini_primed; - G.ini_primed = 1; - if (rm[0] && strcmp(rm, G.ini_remove)) { - snprintf(G.ini_remove, sizeof G.ini_remove, "%s", rm); - uint64_t kid; - if (!primed) { - } else if (!coop_sync_key_parse(rm, &kid) && strspn(rm, "0123456789") == strlen(rm)) { - // a fingerprint is 16 digits (dashes aside), so one that is all decimal digits is still a fingerprint - rt_log("coop: sync: coop_remove = %s in nx2navi.ini names a member number, which another player may have by now: put the " - "member's key fingerprint there (the log shows it beside each member)", rm); - } else { - remove_by_fingerprint(rm); - } - } - if (!strcmp(rp, "0")) snprintf(G.ini_reset, sizeof G.ini_reset, "0"); - else if (rp[0] && strcmp(rp, G.ini_reset)) { - snprintf(G.ini_reset, sizeof G.ini_reset, "%s", rp); - if (primed) lock_and_reset_pools(); - } -} - -// coop_remove_member's hook (coop.h): the maker's removal, signed, so that every game drops that identity. -static int on_remove(int member, void *user) { - (void)user; - plat_mutex_lock(&G.lock); - int ok = G.eng && coop_sync_remove(G.eng, member); - plat_mutex_unlock(&G.lock); - if (ok) rt_log("coop: sync: member %d removed: every game drops its identity and its pool records", member); - return ok; -} - -// For the maker: the pool contributors that took more from the pools than they brought (per pool: -// coop_sync_contributors), in the room or not, when that changed (at most every 10 s), with the setting that removes -// each (G.lock held). A player who used keys others found is among them too; a drainer is the one nobody expected. -static void say_spenders(uint64_t now) { - if (now - G.spent_at < 10000 || !coop_sync_is_maker(G.eng)) return; - CoopContributor c[24]; - int n = coop_sync_contributors(G.eng, c, 24); - uint64_t h = 0; - for (int i = 0; i < n; i++) - if (!c[i].self && !c[i].removed && (c[i].spent[0] > 0 || c[i].spent[1] > 0 || c[i].spent[2] > 0)) - for (int k = 0; k < 3; k++) h = (h ^ (uint64_t)c[i].spent[k] ^ (uint64_t)c[i].key[k * 5] ^ (uint64_t)c[i].member << 40) * 0x100000001b3ull; - if (h == G.spent_seen) return; - G.spent_seen = h, G.spent_at = now; - for (int i = 0; h && i < n; i++) { - if (c[i].self || c[i].removed || !(c[i].spent[0] > 0 || c[i].spent[1] > 0 || c[i].spent[2] > 0)) continue; - char who[COOP_NAME_MAX + 40]; - if (c[i].member) snprintf(who, sizeof who, "member %d, %s", c[i].member, c[i].name); - else snprintf(who, sizeof who, "not in the room"); - rt_log("coop: sync: key %s (%s) took %lld keys, %lld rupees and %lld ammunition from the pools beyond what it brought; " - "coop_remove = %s removes it and gives back what it took (remove it before a pool reset: a reset while it is in the " - "room keeps what it took)", c[i].key, who, (long long)c[i].spent[0], (long long)c[i].spent[1], (long long)c[i].spent[2], c[i].key); - } -} - -// The copy of the save an accepted room waits for, on the coop thread without G.lock (it takes a while). The first -// copy for a room in this start of the game is kept out of the rotation ("first-