1- using Heijden . DNS ;
1+ using DnsClient ;
22using System ;
33using System . Collections . Generic ;
44using System . Linq ;
@@ -13,21 +13,18 @@ public class DNSLookup
1313 {
1414 #region Variables
1515 public bool UseCustomDNSServer = false ;
16- public List < string > CustomDNSServers = new List < string > { "1.1.1.1" , "1.0.0.1" } ;
17- public int Port = 53 ;
16+ public DNSServerInfo CustomDNSServer ;
1817 public bool AddDNSSuffix = true ;
1918 public bool UseCustomDNSSuffix = false ;
2019 public string CustomDNSSuffix ;
21- public QClass Class = QClass . IN ;
22- public QType Type = QType . ANY ;
20+ public QueryClass QueryClass = QueryClass . IN ;
21+ public QueryType QueryType = QueryType . ANY ;
22+
23+ public bool UseCache = false ;
2324 public bool Recursion = true ;
24- public bool UseResolverCache = false ;
25- public TransportType TransportType = TransportType . Udp ;
26- public int Attempts = 3 ;
27- public int Timeout = 2000 ;
28- public bool ResolveCNAME = true ;
29-
30- private readonly Resolver _dnsResolver = new Resolver ( ) ;
25+ public bool UseTCPOnly = false ;
26+ public int Retries = 3 ;
27+ public TimeSpan Timeout = new TimeSpan ( 2000 ) ;
3128 #endregion
3229
3330 #region Events
@@ -54,71 +51,28 @@ protected virtual void OnLookupComplete()
5451 #endregion
5552
5653 #region Methods
57- public Task < Tuple < IPAddress , List < string > > > ResolvePTRAsync ( IPAddress host )
54+ private List < IPEndPoint > GetDnsServer ( )
5855 {
59- return Task . Run ( ( ) => ResolvePTR ( host ) ) ;
60- }
61-
62- public Tuple < IPAddress , List < string > > ResolvePTR ( IPAddress host )
63- {
64- // DNS server list
65- var dnsServers = new List < string > ( ) ;
56+ List < IPEndPoint > dnsServers = new List < IPEndPoint > ( ) ;
6657
6758 if ( UseCustomDNSServer )
68- CustomDNSServers . ForEach ( x => dnsServers . Add ( x ) ) ;
69- else // Use windows default dns server, but filter/remove IPv6 site local addresses (fec0:0:0:0:ffff::)
70- _dnsResolver . DnsServers . Where ( x => ! x . Address . ToString ( ) . StartsWith ( @"fec0" ) ) . Select ( y => y . Address . ToString ( ) ) . ToList ( ) . ForEach ( x => dnsServers . Add ( x ) ) ;
71-
72- // PTR
73- var name = Resolver . GetArpaFromIp ( host ) ;
74-
75- var port = UseCustomDNSServer ? Port : Resolver . DefaultPort ;
76-
77- var dnsServerIPAddress = string . Empty ;
78- var ptrResults = new List < string > ( ) ;
79-
80- foreach ( var dnsServer in dnsServers )
8159 {
82- dnsServerIPAddress = dnsServer ;
83-
84- // Create a new for each request
85- var resolver = new Resolver ( dnsServer , port )
86- {
87- Recursion = Recursion ,
88- TransportType = TransportType ,
89- UseCache = UseResolverCache ,
90- Retries = Attempts ,
91- TimeOut = Timeout
92- } ;
93-
94- var dnsResponse = resolver . Query ( name , QType . PTR ) ;
95-
96- if ( ! string . IsNullOrEmpty ( dnsResponse . Error ) ) // On error... try next dns server...
97- continue ;
98-
99- // PTR
100- ptrResults . AddRange ( dnsResponse . RecordsPTR . Select ( r => r . PTRDNAME ) ) ;
101-
102- // If we got results, break... else --> check next dns server
103- if ( ptrResults . Count > 0 )
104- break ;
60+ foreach ( var dnsServer in CustomDNSServer . Servers )
61+ dnsServers . Add ( new IPEndPoint ( IPAddress . Parse ( dnsServer ) , CustomDNSServer . Port ) ) ;
62+ }
63+ else
64+ {
65+ foreach ( var dnsServer in NameServer . ResolveNameServers ( true , false ) )
66+ dnsServers . Add ( dnsServer ) ;
10567 }
10668
107- return new Tuple < IPAddress , List < string > > ( IPAddress . Parse ( dnsServerIPAddress ) , ptrResults ) ;
69+ return dnsServers ;
10870 }
10971
11072 public void ResolveAsync ( List < string > hosts )
11173 {
11274 Task . Run ( ( ) =>
11375 {
114- // DNS server list
115- var dnsServers = new List < string > ( ) ;
116-
117- if ( UseCustomDNSServer )
118- CustomDNSServers . ForEach ( x => dnsServers . Add ( x ) ) ;
119- else // Use windows default dns server, but filter/remove IPv6 site local addresses (fec0:0:0:0:ffff::)
120- _dnsResolver . DnsServers . Where ( x => ! x . Address . ToString ( ) . StartsWith ( @"fec0" ) ) . Select ( y => y . Address . ToString ( ) ) . ToList ( ) . ForEach ( x => dnsServers . Add ( x ) ) ;
121-
12276 // Foreach host
12377 foreach ( var host in hosts )
12478 {
@@ -130,113 +84,79 @@ public void ResolveAsync(List<string> hosts)
13084 if ( name . IndexOf ( "." , StringComparison . OrdinalIgnoreCase ) == - 1 )
13185 {
13286 if ( AddDNSSuffix )
133- {
13487 dnsSuffix = UseCustomDNSSuffix ? CustomDNSSuffix : IPGlobalProperties . GetIPGlobalProperties ( ) . DomainName ;
135- }
13688 }
13789
13890 // Append dns suffix to hostname
13991 if ( ! string . IsNullOrEmpty ( dnsSuffix ) )
14092 name += $ ".{ dnsSuffix } ";
14193
142- switch ( Type )
94+ Parallel . ForEach ( GetDnsServer ( ) , dnsServer =>
14395 {
144- // PTR
145- case QType . PTR :
146- if ( IPAddress . TryParse ( name , out IPAddress ip ) )
147- name = Resolver . GetArpaFromIp ( ip ) ;
148- break ;
149- // NAPTR
150- case QType . NAPTR :
151- name = Resolver . GetArpaFromEnum ( name ) ;
152- break ;
153- }
96+ LookupClient dnsLookupClient = new LookupClient ( dnsServer ) ;
97+ dnsLookupClient . UseTcpOnly = UseTCPOnly ;
98+ dnsLookupClient . UseCache = UseCache ;
99+ dnsLookupClient . Recursion = Recursion ;
100+ dnsLookupClient . Timeout = Timeout ;
101+ dnsLookupClient . Retries = Retries ;
154102
155- var port = UseCustomDNSServer ? Port : Resolver . DefaultPort ;
156-
157- Parallel . ForEach ( dnsServers , dnsServer =>
158- {
159- // Create a new for each request
160- var resolver = new Resolver ( dnsServer , port )
161- {
162- Recursion = Recursion ,
163- TransportType = TransportType ,
164- UseCache = UseResolverCache ,
165- Retries = Attempts ,
166- TimeOut = Timeout
167- } ;
168-
169- var dnsResponse = resolver . Query ( name , Type , Class ) ;
103+ // PTR vs A, AAAA, CNAME etc.
104+ var dnsResponse = QueryType == QueryType . PTR ? dnsLookupClient . QueryReverse ( IPAddress . Parse ( host ) ) : dnsLookupClient . Query ( host , QueryType , QueryClass ) ;
170105
171106 // If there was an error... return
172- if ( ! string . IsNullOrEmpty ( dnsResponse . Error ) )
107+ if ( dnsResponse . HasError )
173108 {
174- OnLookupError ( new DNSLookupErrorArgs ( dnsResponse . Error , resolver . DnsServer ) ) ;
109+ OnLookupError ( new DNSLookupErrorArgs ( dnsResponse . ErrorMessage , dnsResponse . NameServer . Endpoint ) ) ;
175110 return ;
176111 }
177112
178113 // Process the results...
179- ProcessResponse ( dnsResponse ) ;
180-
181- // If we get a CNAME back (from an ANY result), do a second request and try to get the A, AAAA etc...
182- if ( ! ResolveCNAME || Type != QType . ANY )
183- return ;
184-
185- foreach ( var record in dnsResponse . RecordsCNAME )
186- {
187- var dnsResponse2 = resolver . Query ( record . CNAME , Type , Class ) ;
188-
189- if ( ! string . IsNullOrEmpty ( dnsResponse2 . Error ) )
190- {
191- OnLookupError ( new DNSLookupErrorArgs ( dnsResponse2 . Error , resolver . DnsServer ) ) ;
192- continue ;
193- }
194-
195- ProcessResponse ( dnsResponse2 ) ;
196- }
114+ ProcessDnsQueryResponse ( dnsResponse ) ;
197115 } ) ;
198116 }
199117
200118 OnLookupComplete ( ) ;
201119 } ) ;
202120 }
203121
204- private void ProcessResponse ( Response dnsResponse )
122+ private void ProcessDnsQueryResponse ( IDnsQueryResponse dnsQueryResponse )
205123 {
206- var dnsServer = dnsResponse . Server . Address . ToString ( ) ;
207- var port = dnsResponse . Server . Port ;
124+ var dnsServer = dnsQueryResponse . NameServer . Endpoint ;
208125
209126 // A
210- foreach ( var r in dnsResponse . RecordsA )
211- OnRecordReceived ( new DNSLookupRecordArgs ( r . RR , r , dnsServer , port ) ) ;
127+ foreach ( var record in dnsQueryResponse . Answers . ARecords ( ) )
128+ OnRecordReceived ( new DNSLookupRecordArgs ( record . DomainName , record . TimeToLive , record . RecordClass , record . RecordType , record . Address . ToString ( ) , dnsServer ) ) ;
212129
213130 // AAAA
214- foreach ( var r in dnsResponse . RecordsAAAA )
215- OnRecordReceived ( new DNSLookupRecordArgs ( r . RR , r , dnsServer , port ) ) ;
131+ foreach ( var record in dnsQueryResponse . Answers . AaaaRecords ( ) )
132+ OnRecordReceived ( new DNSLookupRecordArgs ( record . DomainName , record . TimeToLive , record . RecordClass , record . RecordType , record . Address . ToString ( ) , dnsServer ) ) ;
216133
217134 // CNAME
218- foreach ( var r in dnsResponse . RecordsCNAME )
219- OnRecordReceived ( new DNSLookupRecordArgs ( r . RR , r , dnsServer , port ) ) ;
135+ foreach ( var record in dnsQueryResponse . Answers . CnameRecords ( ) )
136+ OnRecordReceived ( new DNSLookupRecordArgs ( record . DomainName , record . TimeToLive , record . RecordClass , record . RecordType , record . CanonicalName , dnsServer ) ) ;
220137
221138 // MX
222- foreach ( var r in dnsResponse . RecordsMX )
223- OnRecordReceived ( new DNSLookupRecordArgs ( r . RR , r , dnsServer , port ) ) ;
139+ foreach ( var record in dnsQueryResponse . Answers . MxRecords ( ) )
140+ OnRecordReceived ( new DNSLookupRecordArgs ( record . DomainName , record . TimeToLive , record . RecordClass , record . RecordType , record . Exchange , dnsServer ) ) ;
224141
225142 // NS
226- foreach ( var r in dnsResponse . RecordsNS )
227- OnRecordReceived ( new DNSLookupRecordArgs ( r . RR , r , dnsServer , port ) ) ;
143+ foreach ( var record in dnsQueryResponse . Answers . NsRecords ( ) )
144+ OnRecordReceived ( new DNSLookupRecordArgs ( record . DomainName , record . TimeToLive , record . RecordClass , record . RecordType , record . NSDName , dnsServer ) ) ;
228145
229146 // PTR
230- foreach ( var r in dnsResponse . RecordsPTR )
231- OnRecordReceived ( new DNSLookupRecordArgs ( r . RR , r , dnsServer , port ) ) ;
147+ foreach ( var record in dnsQueryResponse . Answers . PtrRecords ( ) )
148+ OnRecordReceived ( new DNSLookupRecordArgs ( record . DomainName , record . TimeToLive , record . RecordClass , record . RecordType , record . PtrDomainName , dnsServer ) ) ;
232149
233150 // SOA
234- foreach ( var r in dnsResponse . RecordsSOA )
235- OnRecordReceived ( new DNSLookupRecordArgs ( r . RR , r , dnsServer , port ) ) ;
151+ foreach ( var record in dnsQueryResponse . Answers . SoaRecords ( ) )
152+ OnRecordReceived ( new DNSLookupRecordArgs ( record . DomainName , record . TimeToLive , record . RecordClass , record . RecordType , record . MName + " " + record . RName , dnsServer ) ) ;
236153
237154 // TXT
238- foreach ( var r in dnsResponse . RecordsTXT )
239- OnRecordReceived ( new DNSLookupRecordArgs ( r . RR , r , dnsServer , port ) ) ;
155+ foreach ( var record in dnsQueryResponse . Answers . TxtRecords ( ) )
156+ OnRecordReceived ( new DNSLookupRecordArgs ( record . DomainName , record . TimeToLive , record . RecordClass , record . RecordType , record . Text . ToString ( ) , dnsServer ) ) ;
157+
158+ // ToDo: implement more
159+
240160 }
241161 #endregion
242162 }
0 commit comments