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)