control-channel: detect a machine-id collision on client sockets only

QA finding 2. The id check ran for every connection that finished its
handshake, so an inbound peer announcing our own id could force-close
every socket and make the app draw a new machine id. Windows runs the id
comparison and the keep-the-first-client rule as two branches of one
block gated by `status == Connected && tcp.IsClient`
(SocketStuff.cs:1980-1981): a server-accepted socket reaches neither.
Move the collision check inside the `isOutbound` gate the dedup half
already had, and say so in the README and docs/protocol.md §10.3 note 3.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
lucioandClaude Opus 5 committed 2026-09-16 01:40:55 -07:00
1 parent 43dc25ac71
commit c9ea89ac50
3 files changed
+35 -17

No files matched your search

+18 -10
View File
@@ -364,17 +364,25 @@ public final class ControlChannel: @unchecked Sendable {
/// The handshake completed. Two policies apply only here, because both are
/// keyed by things a single connection cannot see (`SocketStuff.cs:1964-2004`).
///
/// Both are client-only: `UpdateTcpSockets` runs the whole
/// collision-and-dedup block under `status == SocketStatus.Connected &&
/// tcp.IsClient` (`:1980-1981`), so neither the id check (`:1984-1992`) nor
/// the keep-the-first-client rule (`:1993-1997`) ever sees a
/// server-accepted socket.
private func accept(_ peer: PeerConnection, name: String, id: UInt32) {
if id == config.localID { // SocketStuff.cs:1984-1992
sink.channelDetectedIDCollision(self)
closeAll(.idCollision)
return
}
// Keep the first client that connected; a later one is redundant
// (SocketStuff.cs:1421-1428, 1998). Inbound connections are always kept.
if peer.isOutbound, hasConnectedClient(to: name, excluding: peer) {
peer.close(.duplicateClient)
return
if peer.isOutbound {
if id == config.localID { // SocketStuff.cs:1984-1992
sink.channelDetectedIDCollision(self)
closeAll(.idCollision)
return
}
// Keep the first client that connected; a later one is redundant
// (SocketStuff.cs:1421-1428, 1996-2004).
if hasConnectedClient(to: name, excluding: peer) {
peer.close(.duplicateClient)
return
}
}
dialers[name] = nil
sink.channel(self, connected: name, peerID: id)
+8 -4
View File
@@ -66,10 +66,14 @@ No packet is interpreted here.
and one inbound connection per peer, and forwards every app packet to
both — the receiver's replay ring collapses the pair (contracts C1).
- **Machine-id collision** (`SocketStuff.cs:1984-1992`): if a trusted
peer reports our own id, the channel reports `.idCollision` to the
sink; the app regenerates and calls `reopenAll()`. Windows force-closes
*every* socket in that branch, not just the offending one
(`docs/protocol.md` §10.3 note 3), and so do we.
peer reports our own id **over an outbound connection**, the channel
reports `.idCollision` to the sink; the app regenerates and calls
`reopenAll()`. Windows force-closes *every* socket in that branch, not
just the offending one (`docs/protocol.md` §10.3 note 3), and so do we.
Like the duplicate-client rule, this is client-only: `UpdateTcpSockets`
runs the whole block under `status == Connected && tcp.IsClient`
(`:1980-1981`), so an id reported over an inbound connection is
silently tolerated, here as there.
- **Failure budget per endpoint** (`SocketStuff.cs:1482-1499`): more
than 10 `.invalidKey` outcomes from the same remote address ⇒ report
`.peerRejected(address)` and stop dialling it until `reopenAll()`.
+9 -3
View File
@@ -633,13 +633,19 @@ sees them. One subsection per lane, appended, never rewritten.
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
3. **An id collision force-closes every socket, not just the offending one —
and it is only ever detected on a client socket.** 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`).
everything (`:1985-1990`). The id comparison and the keep-the-first-client
rule are two branches of one block gated by `status ==
SocketStatus.Connected && tcp.IsClient` (`:1980-1981`), so a server-accepted
socket reaches neither: a peer that announces our own `MachineId` over an
inbound connection is silently tolerated. Note 4's client-only observation
about the dedup half is the same gate.
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