Skip to content

Commit f50c982

Browse files
authored
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
1 parent 293ce91 commit f50c982

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

Source/NETworkManager.Models/Network/DNSLookup.cs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Threading.Tasks;
77
using DnsClient;
88
using DnsClient.Protocol;
9-
using NETworkManager.Utilities;
109

1110
namespace NETworkManager.Models.Network;
1211

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

9594
// Use windows dns servers
9695
servers.AddRange(dnsServers == null
@@ -122,16 +121,9 @@ public void ResolveAsync(IEnumerable<string> hosts)
122121
// Append dns suffix to hostname, if option is set, otherwise just copy the list
123122
var queries = _addSuffix && _settings.QueryType != QueryType.PTR ? GetHostWithSuffix(hosts) : hosts;
124123

125-
// Foreach dns server
126-
Parallel.ForEach(_servers, async dnsServer =>
124+
// For each dns server
125+
Parallel.ForEach(_servers, dnsServer =>
127126
{
128-
// Get the dns server hostname for some additional information
129-
var dnsServerHostName = string.Empty;
130-
131-
var dnsResult = await DNSClient.GetInstance().ResolvePtrAsync(dnsServer.Address);
132-
if (!dnsResult.HasError)
133-
dnsServerHostName = dnsResult.Value;
134-
135127
// Init each dns server once
136128
LookupClientOptions lookupClientOptions = new(dnsServer)
137129
{
@@ -143,8 +135,27 @@ public void ResolveAsync(IEnumerable<string> hosts)
143135
};
144136

145137
LookupClient lookupClient = new(lookupClientOptions);
138+
139+
// Get the dns server hostname for some additional information
140+
var dnsServerHostName = string.Empty;
141+
142+
try
143+
{
144+
var result = lookupClient.QueryReverse(dnsServer.Address);
145+
146+
if (!result.HasError)
147+
{
148+
var record = result.Answers.PtrRecords().FirstOrDefault();
149+
150+
if (record != null)
151+
dnsServerHostName = record.PtrDomainName;
152+
}
153+
}
154+
catch {
155+
// ignored
156+
}
146157

147-
// Foreach host
158+
// For each host
148159
Parallel.ForEach(queries, query =>
149160
{
150161
try
@@ -159,15 +170,18 @@ public void ResolveAsync(IEnumerable<string> hosts)
159170
{
160171
OnLookupError(new DNSLookupErrorArgs(query, $"{dnsServer.Address}",
161172
$"{dnsServer.Address}:{dnsServer.Port}", dnsResponse.ErrorMessage));
173+
162174
return; // continue
163175
}
164176

165177
if (dnsResponse.Answers.Count == 0)
166178
{
167179
var digAdditionalCommand = _settings.QueryType == QueryType.PTR ? " -x " : " ";
180+
168181
OnLookupError(new DNSLookupErrorArgs(query, $"{dnsServer.Address}",
169182
$"{dnsServer.Address}:{dnsServer.Port}",
170183
$"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}"));
184+
171185
return; // continue
172186
}
173187

@@ -191,6 +205,7 @@ public void ResolveAsync(IEnumerable<string> hosts)
191205
/// </summary>
192206
/// <param name="answers">List of DNS resource records.</param>
193207
/// <param name="nameServer">DNS name server that answered the query.</param>
208+
/// <param name="nameServerHostname">DNS name server hostname.</param>
194209
private void ProcessDnsAnswers(IEnumerable<DnsResourceRecord> answers, NameServer nameServer,
195210
string nameServerHostname = null)
196211
{

Website/docs/changelog/next-release.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Release date: **xx.xx.2023**
4343
- 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)
4444
- DNS Lookup
4545
- Hostname of the nameserver added to group [#2573](https://github.com/BornToBeRoot/NETworkManager/pull/2573)
46+
- Resolve was finished too early if a request timed [#2612](https://github.com/BornToBeRoot/NETworkManager/pull/2612)
4647
- Sort improved [#2583](https://github.com/BornToBeRoot/NETworkManager/pull/2583)
4748
- SNMP
4849
OID profile column sort fixed [#2583](https://github.com/BornToBeRoot/NETworkManager/pull/2583)

0 commit comments

Comments
 (0)