diff --git a/docs/protocol.md b/docs/protocol.md index 2978e22..cf70ccc 100644 --- a/docs/protocol.md +++ b/docs/protocol.md @@ -608,3 +608,72 @@ sees them. One subsection per lane, appended, never rewritten. handshake pair, the heartbeat written on a fresh connection (`SocketStuff.cs:1451`), `Hi` and `ByeBye` — go out through `TcpSend` with no `SkSend` in front of them. + +### 10.3 control-channel (2026-09-16) + +1. **The 30-second connect budget is per socket-open, not per attempt.** + `connectTimeout = Common.GetTick() + (CONNECT_TIMEOUT / 2)` with + `CONNECT_TIMEOUT = 60000` is computed **once**, in the `SocketStuff` + constructor (`SocketStuff.cs:158, 176, 276`), and every client thread + compares its 1 s retry loop against that one deadline (`:1179-1193`). So the + cadence is "retry every second, but only during the first 30 seconds after + the sockets were opened" — a machine powered on a minute later is not dialled + at all until something reopens the sockets. +2. **Dialling is triggered, never polled.** `UpdateTCPClients` (`:797-837`) runs + only when something calls `MachineStuff.UpdateClientSockets`: after a + server-side handshake completes (`:1479`), when a peer is learned + (`MachineStuff.cs:775`), on the matrix's fourth packet (`:997`), on + `REOPEN_WHEN_WSAECONNRESET` (`frmScreen.cs:461`), once at tick 20 + (`frmScreen.cs:530-537`) and from `ReopenSockets` (`Common.cs:1260`). What + actually keeps a matrix alive after that window is the **reopen countdown**: + `PleaseReopenSocket` is set to 9 at init (`InitAndCleanup.cs:140`) and to 10 + on a network-availability change (`:273-276`), and the 100 ms timer reopens + *all* sockets when it is 10, or every 30 s (`count % 300`) while it is + positive and no socket is established (`frmScreen.cs:427-450`). The two + negative sentinels are separate paths: `REOPEN_WHEN_HOTKEY (-10055)` reopens + everything, `REOPEN_WHEN_WSAECONNRESET (-10054)` only re-runs the client loop + (`frmScreen.cs:452-461`, `InitAndCleanup.cs:28-29`). +3. **An id collision force-closes every socket, not just the offending one.** In + `UpdateTcpSockets`, `tobeRemovedSockets` starts as the *whole* socket list and + is narrowed to "other clients with the same machine name" only in the `else` + branch; the collision branch leaves it whole and sets `tcp = null` so the new + socket's status is never written (`SocketStuff.cs:1978-1998`). A new + `MachineId` is drawn and `PleaseReopenSocket = REOPEN_WHEN_HOTKEY` reopens + everything (`:1985-1990`). +4. **Duplicate clients are suppressed twice; duplicate servers never are.** The + newcomer closes itself just before it would become trusted + (`SocketStuff.cs:1421-1428`), and `UpdateTcpSockets` independently keeps the + first connected client and force-closes the rest (`:1998`). Both tests go + through `IsConnectedByAClientSocketTo`, which only looks at sockets with + `IsClient` set (`Common.cs:1142-1161`), so the inbound half of a pair is never + de-duplicated and a machine pair really does end up with two connections — + which is why one `Id` must reach both (§10.2 note 11, contracts C1). +5. **The per-endpoint failure budget is keyed by address *and port*, counts only + mismatched acks, and quits the app.** `FailedAttempt` is a static + `ConcurrentDictionary` keyed by `RemoteEndPoint.ToString()` + (`SocketStuff.cs:159, 1487-1497`), incremented only in the branch where the + `HandshakeAck` nonce does not match; the eleventh shows a message box and + calls `Quit`. The ">10 untrusted packets" branch (`:1389-1400`) never touches + it. Because the key carries the remote *port*, which is new on every TCP + connection, the counter in practice almost never reaches ten — it is a + same-connection guard, not a same-peer one. +6. **`Name2IP` is concatenated with a policy list, is used in addition to DNS, + and each group is filtered separately.** `Name2IpPolicyList + "\r\n" + + Name2IP` is split on CRLF and then on blanks (`:906-925`); the mapped + addresses get their own same-subnet check and are dialled **before** + `Dns.GetHostEntry` is called at all (`:930-941, 950`), while the DNS group + gets the reverse-lookup filter (`:989-1032`) and then its own same-subnet + check (`:1035-1044`). Reverse-lookup validation therefore never applies to a + user-supplied mapping. +7. **The socket options are per connection, set inside `MainTCPRoutine`.** + `SendBufferSize`/`ReceiveBufferSize = PACKAGE_SIZE * 10000` (320 000), + `NoDelay = true` and `SendTimeout = 500` are applied to the accepted or + connected socket, not to the listener (`SocketStuff.cs:1281-1286`). +8. **After the 48-byte preamble the stream is packet-aligned in both + directions.** Every write is a whole 32- or 64-byte packet through a + no-padding CBC stream (§2.3), and the preamble is exactly 48 bytes, so a + reader that takes 48 bytes and then exactly 32 at a time never straddles a + packet: 32 bytes of ciphertext in are always 32 bytes of plaintext out. That + makes a macOS stream read mean exactly what `ReadEx(buf, 0, PACKAGE_SIZE)` + means on Windows (`:574`), including "a short count is an `Invalid` package + and a zero count is an `Error` package" (`:574-578, 588-593`).