From f50c982fb39ae99c71afe5c0719399f5162891b7 Mon Sep 17 00:00:00 2001 From: BornToBeRoot <16019165+BornToBeRoot@users.noreply.github.com> Date: Sat, 6 Jan 2024 23:55:41 +0100 Subject: [PATCH] Fix: DNS server resolve is finished too early if queries are timed out (#2612) * Fix: DNS server resolve is finished too early if queries are timed out * Docs: #2612 --- .../Network/DNSLookup.cs | 39 +++++++++++++------ Website/docs/changelog/next-release.md | 1 + 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/Source/NETworkManager.Models/Network/DNSLookup.cs b/Source/NETworkManager.Models/Network/DNSLookup.cs index 069031be64..226ff290a9 100644 --- a/Source/NETworkManager.Models/Network/DNSLookup.cs +++ b/Source/NETworkManager.Models/Network/DNSLookup.cs @@ -6,7 +6,6 @@ using System.Threading.Tasks; using DnsClient; using DnsClient.Protocol; -using NETworkManager.Utilities; namespace NETworkManager.Models.Network; @@ -90,7 +89,7 @@ private void OnLookupComplete() /// List of DNS servers as . private IEnumerable GetDnsServer(IEnumerable dnsServers = null) { - List servers = new(); + List servers = []; // Use windows dns servers servers.AddRange(dnsServers == null @@ -122,16 +121,9 @@ public void ResolveAsync(IEnumerable hosts) // Append dns suffix to hostname, if option is set, otherwise just copy the list var queries = _addSuffix && _settings.QueryType != QueryType.PTR ? GetHostWithSuffix(hosts) : hosts; - // Foreach dns server - Parallel.ForEach(_servers, async dnsServer => + // For each dns server + Parallel.ForEach(_servers, dnsServer => { - // Get the dns server hostname for some additional information - var dnsServerHostName = string.Empty; - - var dnsResult = await DNSClient.GetInstance().ResolvePtrAsync(dnsServer.Address); - if (!dnsResult.HasError) - dnsServerHostName = dnsResult.Value; - // Init each dns server once LookupClientOptions lookupClientOptions = new(dnsServer) { @@ -143,8 +135,27 @@ public void ResolveAsync(IEnumerable hosts) }; LookupClient lookupClient = new(lookupClientOptions); + + // Get the dns server hostname for some additional information + var dnsServerHostName = string.Empty; + + try + { + var result = lookupClient.QueryReverse(dnsServer.Address); + + if (!result.HasError) + { + var record = result.Answers.PtrRecords().FirstOrDefault(); + + if (record != null) + dnsServerHostName = record.PtrDomainName; + } + } + catch { + // ignored + } - // Foreach host + // For each host Parallel.ForEach(queries, query => { try @@ -159,15 +170,18 @@ public void ResolveAsync(IEnumerable hosts) { OnLookupError(new DNSLookupErrorArgs(query, $"{dnsServer.Address}", $"{dnsServer.Address}:{dnsServer.Port}", dnsResponse.ErrorMessage)); + return; // continue } if (dnsResponse.Answers.Count == 0) { var digAdditionalCommand = _settings.QueryType == QueryType.PTR ? " -x " : " "; + OnLookupError(new DNSLookupErrorArgs(query, $"{dnsServer.Address}", $"{dnsServer.Address}:{dnsServer.Port}", $"No DNS resource records received for query \"{query}\" (Query type: \"{_settings.QueryType}\") and the DNS server did not return an error. Try to check your DNS server with: dig @{dnsServer.Address}{digAdditionalCommand}{query}")); + return; // continue } @@ -191,6 +205,7 @@ public void ResolveAsync(IEnumerable hosts) /// /// List of DNS resource records. /// DNS name server that answered the query. + /// DNS name server hostname. private void ProcessDnsAnswers(IEnumerable answers, NameServer nameServer, string nameServerHostname = null) { diff --git a/Website/docs/changelog/next-release.md b/Website/docs/changelog/next-release.md index e09f52aa35..8dbd8dd837 100644 --- a/Website/docs/changelog/next-release.md +++ b/Website/docs/changelog/next-release.md @@ -43,6 +43,7 @@ Release date: **xx.xx.2023** - Scan is no longer aborted if the IP of a single host in a series of hosts cannot be resolved (the host is skipped and an error is displayed) [#2573](https://github.com/BornToBeRoot/NETworkManager/pull/2573) - DNS Lookup - Hostname of the nameserver added to group [#2573](https://github.com/BornToBeRoot/NETworkManager/pull/2573) + - Resolve was finished too early if a request timed [#2612](https://github.com/BornToBeRoot/NETworkManager/pull/2612) - Sort improved [#2583](https://github.com/BornToBeRoot/NETworkManager/pull/2583) - SNMP OID profile column sort fixed [#2583](https://github.com/BornToBeRoot/NETworkManager/pull/2583)