diff --git a/Sources/MWBNet/Resolver.swift b/Sources/MWBNet/Resolver.swift index 79c775f..791f1a7 100644 --- a/Sources/MWBNet/Resolver.swift +++ b/Sources/MWBNet/Resolver.swift @@ -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`. diff --git a/docs/features/control-channel/README.md b/docs/features/control-channel/README.md index 892ff2f..51768c1 100644 --- a/docs/features/control-channel/README.md +++ b/docs/features/control-channel/README.md @@ -47,6 +47,18 @@ No packet is interpreted here. Network.framework exposes them (`SocketStuff.cs:1283-1286`) — it exposes none, so only `NoDelay` is set and the bounded outbox is what limits queued bytes. +- **Name resolution filters each address group on its own** + (`SocketStuff.cs:928-944, 950, 989-1044`): `Resolver.addresses` runs the + same control flow `StartNewTcpClient` does, not a filter over the union. + The `Name2IP` mappings are checked against the same-subnet rule alone, and + when that check fails Windows returns from `StartNewTcpClient` **before** + `Dns.GetHostEntry` is called (`:934-937, 950`) — so the name is not dialled + at all that round and we return `[]`. The DNS group is filtered by the + reverse lookup and then gets its own same-subnet check (`:1037-1040`); + failing it drops the DNS addresses only, leaving the mapped ones, which + Windows has already handed to `StartNewTcpClientThread` by then. Reverse- + lookup validation never applies to a user-supplied mapping + (`docs/protocol.md` §10.3 note 6). - **Keep one client per peer** (`SocketStuff.cs:1980-2004`): when a second connection to the same peer name reaches `.connected`, the later *client* connection is closed; inbound (server-side) connections diff --git a/docs/protocol.md b/docs/protocol.md index cf70ccc..4d2d40b 100644 --- a/docs/protocol.md +++ b/docs/protocol.md @@ -658,13 +658,21 @@ sees them. One subsection per lane, appended, never rewritten. 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" + + and each group is filtered separately — and a mapped group that fails the + subnet check aborts the whole round.** `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 + `Dns.GetHostEntry` is called at all (`:928-944, 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. + user-supplied mapping. The two checks fail differently: the mapped group's + failure is a `return` from `StartNewTcpClient` (`:934-937`) that skips DNS + resolution entirely, so **nothing** is dialled for that name that round; the + DNS group's failure is a `return` reached after the mapped addresses already + went to `StartNewTcpClientThread` (`:941, 1037-1040`), so it drops only the + DNS addresses. Filtering the union of the two groups instead would both + dial out-of-subnet DNS addresses whenever one mapped address matched, and + dial DNS addresses Windows never resolves. 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