1- using NETworkManager . Models . Lookup ;
1+ using DnsClient ;
2+ using NETworkManager . Models . Lookup ;
23using System ;
3- using System . Collections . Generic ;
4- using System . Diagnostics ;
54using System . Linq ;
65using System . Net ;
76using System . Net . NetworkInformation ;
87using System . Threading ;
98using System . Threading . Tasks ;
10- using TransportType = Heijden . DNS . TransportType ;
119
1210namespace NETworkManager . Models . Network
1311{
@@ -21,16 +19,23 @@ public class IPScanner
2119 public byte [ ] ICMPBuffer = new byte [ 32 ] ;
2220 public int ICMPAttempts = 2 ;
2321 public bool ResolveHostname = true ;
22+
23+ // ToDo:
2424 public bool UseCustomDNSServer = false ;
25- public List < string > CustomDNSServer = new List < string > ( ) ;
26- public int DNSPort = 53 ;
27- public int DNSAttempts = 2 ;
28- public int DNSTimeout = 2000 ;
25+ public IPAddress CustomDNSServer ;
26+ public int CustomDNSPort = 53 ;
27+ public bool DNSTCPOnly = false ;
28+ public bool DNSUseCache = true ;
29+ public bool DNSRecursion = true ;
30+ public TimeSpan DNSTimeout = new TimeSpan ( 2000 ) ;
31+ public int DNSRetries = 3 ;
32+
2933 public bool ResolveMACAddress = false ;
3034 public bool ShowScanResultForAllIPAddresses = false ;
31- public TransportType DNSTransportType = TransportType . Udp ;
32- public bool DNSUseResolverCache = false ;
33- public bool DNSRecursion = false ;
35+
36+ // ToDo - End
37+
38+ private LookupClient DnsLookupClient ;
3439 #endregion
3540
3641 #region Events
@@ -71,6 +76,18 @@ public void ScanAsync(IPAddress[] ipAddresses, CancellationToken cancellationTok
7176 {
7277 _progressValue = 0 ;
7378
79+ // Create dns client and set options
80+
81+ if ( ResolveHostname )
82+ {
83+ DnsLookupClient = UseCustomDNSServer ? new LookupClient ( new IPEndPoint ( CustomDNSServer , CustomDNSPort ) ) : new LookupClient ( ) ;
84+ DnsLookupClient . UseCache = DNSUseCache ;
85+ DnsLookupClient . Recursion = DNSRecursion ;
86+ DnsLookupClient . Timeout = DNSTimeout ;
87+ DnsLookupClient . Retries = DNSRetries ;
88+ DnsLookupClient . UseTcpOnly = DNSTCPOnly ;
89+ }
90+
7491 // Modify the ThreadPool for better performance
7592 ThreadPool . GetMinThreads ( out var workerThreads , out var completionPortThreads ) ;
7693 ThreadPool . SetMinThreads ( workerThreads + Threads , completionPortThreads + Threads ) ;
@@ -109,9 +126,7 @@ public void ScanAsync(IPAddress[] ipAddresses, CancellationToken cancellationTok
109126 pingInfo = new PingInfo ( ipAddress , pingReply . Status ) ;
110127 }
111128 catch ( PingException )
112- {
113-
114- }
129+ { }
115130
116131 // Don't scan again, if the user has canceled (when more than 1 attempt)
117132 if ( cancellationToken . IsCancellationRequested )
@@ -126,27 +141,13 @@ public void ScanAsync(IPAddress[] ipAddresses, CancellationToken cancellationTok
126141
127142 if ( ResolveHostname )
128143 {
129- var dnsLookup = new DNSLookup
130- {
131- UseCustomDNSServer = UseCustomDNSServer ,
132- CustomDNSServers = CustomDNSServer ,
133- Port = DNSPort ,
134- Attempts = DNSAttempts ,
135- Timeout = DNSTimeout ,
136- TransportType = DNSTransportType ,
137- UseResolverCache = DNSUseResolverCache ,
138- Recursion = DNSRecursion
139- } ;
144+ var dnsQueryResponse = DnsLookupClient . QueryReverse ( ipAddress ) ;
140145
141- try
142- {
143- hostname = dnsLookup . ResolvePTR ( ipAddress ) . Item2 . FirstOrDefault ( ) ;
144- }
145- catch
146- {
147- // Could not resolve hostname... e.g. no dns server is configured
148- }
149- }
146+ if ( ! dnsQueryResponse . HasError )
147+ hostname = dnsQueryResponse . Answers . PtrRecords ( ) . FirstOrDefault ( ) ? . PtrDomainName ;
148+ else
149+ hostname = $ "{ Resources . Localization . Strings . Error } : { dnsQueryResponse . ErrorMessage } ";
150+ }
150151
151152 // ARP
152153 PhysicalAddress macAddress = null ;
0 commit comments