Skip to content

Commit

Permalink
Fix: DNS server resolve is finished too early if queries are timed out (
Browse files Browse the repository at this point in the history
#2612)

* Fix: DNS server resolve is finished too early if queries are timed out

* Docs: #2612
  • Loading branch information
BornToBeRoot authored Jan 6, 2024
1 parent 293ce91 commit f50c982
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
39 changes: 27 additions & 12 deletions Source/NETworkManager.Models/Network/DNSLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Threading.Tasks;
using DnsClient;
using DnsClient.Protocol;
using NETworkManager.Utilities;

namespace NETworkManager.Models.Network;

Expand Down Expand Up @@ -90,7 +89,7 @@ private void OnLookupComplete()
/// <returns>List of DNS servers as <see cref="IPEndPoint" />.</returns>
private IEnumerable<IPEndPoint> GetDnsServer(IEnumerable<ServerConnectionInfo> dnsServers = null)
{
List<IPEndPoint> servers = new();
List<IPEndPoint> servers = [];

// Use windows dns servers
servers.AddRange(dnsServers == null
Expand Down Expand Up @@ -122,16 +121,9 @@ public void ResolveAsync(IEnumerable<string> 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)
{
Expand All @@ -143,8 +135,27 @@ public void ResolveAsync(IEnumerable<string> 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
Expand All @@ -159,15 +170,18 @@ public void ResolveAsync(IEnumerable<string> 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
}
Expand All @@ -191,6 +205,7 @@ public void ResolveAsync(IEnumerable<string> hosts)
/// </summary>
/// <param name="answers">List of DNS resource records.</param>
/// <param name="nameServer">DNS name server that answered the query.</param>
/// <param name="nameServerHostname">DNS name server hostname.</param>
private void ProcessDnsAnswers(IEnumerable<DnsResourceRecord> answers, NameServer nameServer,
string nameServerHostname = null)
{
Expand Down
1 change: 1 addition & 0 deletions Website/docs/changelog/next-release.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f50c982

Please sign in to comment.