control-channel: Resolver filters the Name2IP and DNS groups separately

QA finding 1. `Resolver.addresses` applied one same-subnet check to the
union of the Name2IP mappings and the DNS results. `StartNewTcpClient`
checks each group on its own: the mapped group's failure is a `return`
that skips `Dns.GetHostEntry` entirely (SocketStuff.cs:934-937, 950), so
nothing is dialled for that name that round; the DNS group's failure
(:1037-1040) drops only the DNS addresses, because the mapped ones have
already been dialled (:941). Port that control flow, and document it in
the feature README's Assumptions and docs/protocol.md §10.3 note 6.

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

No files matched your search

+23 -17
View File
@@ -1,7 +1,7 @@
// Name → addresses, the way Windows does it: the user's `Name2IP` mappings
// first, then DNS, then the two optional filters (reverse-lookup validation
// and the same-subnet check). There is no discovery in this protocol
// (`docs/protocol.md` §7).
// first, then DNS, with each group filtered on its own by the two optional
// filters (reverse-lookup validation and the same-subnet check). There is no
// discovery in this protocol (`docs/protocol.md` §7).
//
// Ported from App/Class/SocketStuff.cs StartNewTcpClient (904-1046),
// CheckForSameSubNet (1065-1107) and GetMyIPv4Addresses (1109-1127).
@@ -18,26 +18,32 @@ public enum Resolver {
/// the `Name2IP` mappings first, then whatever DNS returns, with
/// duplicates removed (`SocketStuff.cs:904-1046`).
///
/// Returns an empty array when nothing resolves or when the same-subnet
/// check rejects the peer, which is Windows returning from
/// `ClientThread` without starting a socket (`:1035-1044`).
/// The two groups are **filtered separately**, which is the control flow
/// `StartNewTcpClient` has: the mapped addresses get their own same-subnet
/// check and, when it fails, Windows `return`s before `Dns.GetHostEntry` is
/// ever called, so that name is not dialled at all this round
/// (`:928-944, 950`); the DNS group is filtered by the reverse lookup and
/// then gets its own same-subnet check, which on failure drops only the DNS
/// addresses (`:989-1044`). Reverse-lookup validation never applies to a
/// user-supplied mapping.
///
/// Returns an empty array when nothing resolves or when every group was
/// rejected — Windows returning from `StartNewTcpClient` without starting a
/// socket.
public static func addresses(for name: String, config: ChannelConfig) -> [String] {
var found: [String] = []
for ip in mappedAddresses(for: name, in: config.name2IP) where !found.contains(ip) {
found.append(ip)
let mapped = mappedAddresses(for: name, in: config.name2IP)
if !mapped.isEmpty, config.sameSubnetOnly, !isSameSubnet(mapped) {
return [] // `:934-937` — the early return that also skips DNS.
}
// Only DNS results go through the reverse-lookup filter: Windows dials
// the Name2IP addresses before it ever looks the name up (`:924-941`).
var resolved = lookup(name).filter { !found.contains($0) }
var resolved = lookup(name).filter { !mapped.contains($0) }
if config.validateRemoteIP {
resolved = resolved.filter { validates(address: $0, as: name) }
}
found.append(contentsOf: resolved)
guard !found.isEmpty else { return [] }
guard !config.sameSubnetOnly || isSameSubnet(found) else { return [] }
return found
if !resolved.isEmpty, config.sameSubnetOnly, !isSameSubnet(resolved) {
resolved = [] // `:1037-1040` — drops this group only.
}
return mapped + resolved
}
/// The `Name2IP` addresses mapped to `name`.