Repository navigation
Protocol Library Store
A store is a folder that holds a console-shaped CEC folder, plus ctrpass's own state beside it: the identity seed, which titles receive, where each message came from, and a book of the consoles met. Exchanges read the outbox from it and file arrivals into its inboxes.
Implemented in: store, store::peers, store::snapshot, store::disk, identity.
flowchart TD
root["store root/"] --> cec["CEC/ (console layout)"]
root --> state["state.json"]
root --> seed["seed"]
root --> peers["peers.json"]
root --> lock["lock"]
root --> damaged["damaged/"]
cec --> list["MBoxList____"]
cec --> title["TITLE/ with MBoxInfo____, MBoxData.010, InBox___/, OutBox__/"]
The CEC folder uses the file formats on CEC Message Boxes.
| File | Contents |
|---|---|
CEC/ |
Titles, their names and signing keys, and the messages in each box |
state.json |
Titles with a real key, titles set to receive, service records set for receive-only titles, and the origin of each message |
seed |
32 bytes in hex; every identity field is derived from it |
peers.json |
The peer book |
lock |
Held with an exclusive lock while the store is open |
damaged/ |
Files that could not be read, moved aside with a timestamp prefix |
| Limit | Value |
|---|---|
| Titles | 24 |
| Inbox bytes per title | 1 MiB |
| Inbox messages per title | 24 |
Opening a store:
- Creates the folders if missing, with mode
0700. - Takes the lock. A second process gets a "busy" error.
- Removes leftover temporary files from an interrupted write.
- Reads
MBoxList____. If it is unreadable, moves it aside and rebuilds it from the title folders. - Reads
state.json. If it is unreadable, moves it aside and rebuilds which titles have a key from theMBoxInfo____files. - Checks every title's
MBoxInfo____and rewrites it if it is missing or has no magic. - Reads every message file. A file that is not a well-formed container, belongs to another title, or whose name does not match its message ID is moved to
damaged/. - Creates a seed if there is none.
Every write goes to a private temporary file, is synced, and is renamed over the target. A crash leaves either the old or the new contents.
| Operation | Behaviour |
|---|---|
| Add a message | Checks the signature against the given key or the title's key, registers the title if needed, writes the file, and for an inbox drops the oldest other messages until the inbox is within its limits. |
| Replace outbox | Makes one message the only outbox message of its title. |
| File inbound | Used for arrivals from a console. Checks each signature. Files all of one title's arrivals or none, and never drops existing messages to make room. Records the peer MAC as the origin. |
| Remove, clear | Removes one message or empties a box across all titles. |
| Verify | Whether a message matches its title's key, or unknown without one. |
Each message has an origin: peer with the console's MAC, or local with a note such as where it was imported from.
| Setting | Stored in |
|---|---|
| Registered titles | MBoxList____ |
| Name | MBoxData.010 |
| Signing key |
MBoxInfo____ at 0x10, and the title is marked keyed in state.json (an all-zero key is also valid, so the mark is what says a key was set) |
| Receive on or off | state.json |
| Service record for a receive-only title |
state.json, as hex |
Giving a different key for a title that already has one is a key conflict unless replacing is asked for.
The seed decides how the store presents itself:
| Field | Derived from |
|---|---|
| MAC | Seed bytes 0-5, with the locally administered bit set and the multicast bit cleared |
| Creator MAC (in the Mii) | Seed bytes 6-11, with the two low bits of the first byte cleared, like a factory address |
| Mii ID | Low byte 0x90 | (seed[12] & 0x0f), then seed bytes 13-15 |
| Face shape | seed[17] % 12 |
| Hat |
seed[16] % 76, or 0 if it is one of 69, 75, 168, 169 |
| Dream | seed[18] % 20 |
| Hobby | seed[19] % 27 |
| Gender | seed[20] & 1 |
After a MAC rotation (below), the rotated MAC replaces the seed's MAC.
peers.json tracks every console MAC seen in an exchange.
| Item | Meaning |
|---|---|
| Peer record | First and last exchange time, exchange count, attempt count, last failed attempt, message IDs sent to it, message IDs received from it |
| Filter | MACs that finished an exchange. They are declined until removed or pushed out. Holds up to 255, oldest out first. |
| Blocked | MACs never engaged, with a note and time |
| MAC | The MAC chosen at the last rotation |
| Rotated | Time of the last rotation |
stateDiagram-v2
[*] --> Unknown
Unknown --> Due
Due --> BackingOff: exchange did not finish
BackingOff --> Due: 60 s passed
Due --> Filtered: exchange finished
Filtered --> Due: MAC rotation or unfilter
Due --> Blocked: block
Blocked --> Due: unblock
Consoles change their MAC and clear their filter from time to time. ctrpass does the same, by default every 8 hours. A rotation:
- picks a new random locally administered MAC;
- clears the filter;
- forgets what was sent to each console, so consoles met before get a full exchange again;
- records the time.
The first check only starts the clock. A radio must be opened again with the new MAC after a rotation.
A snapshot is the whole store as one JSON value: the seed, each title's name, key, receive flag and service record, every message in hex with its box, and the peer book. A snapshot can be restored into an empty store.
Snapshots let a process exchange from a private copy of a store it may not open itself. The Linux helper uses this.
How to set up
- Overview and requirements
- Choosing a Wi-Fi adapter
- Building and installing
- Making the key file
- Desktop app
- Command line
- Messages and emulators
- Troubleshooting
Plugins
Protocol
- Overview
- Discovery
- Link setup
- Frames and CCMP
- YY transport
- Exchange flow
- Exchange objects
- CEC messages
- CEC message boxes
- Mii records
- Mii Plaza
- Keys
ctrpass library
Linux