11using Heijden . DNS ;
2- using NETworkManager . Helpers ;
32using System ;
4- using System . Linq ;
53using System . Net ;
6- using System . Net . NetworkInformation ;
7- using System . Net . Sockets ;
8- using System . Threading ;
94using System . Threading . Tasks ;
105using System . Windows ;
116
@@ -18,12 +13,19 @@ public class DNSLookup
1813 #endregion
1914
2015 #region Events
21- /* public event EventHandler<IPScannerHostFoundArgs > RecordReceived;
16+ public event EventHandler < DNSLookupRecordArgs > RecordReceived ;
2217
23- protected virtual void OnRecordReceived(IPScannerHostFoundArgs e)
24- {
25- RecordReceived?.Invoke(this, e);
26- } */
18+ protected virtual void OnRecordReceived ( DNSLookupRecordArgs e )
19+ {
20+ RecordReceived ? . Invoke ( this , e ) ;
21+ }
22+
23+ public event EventHandler < DNSLookupErrorArgs > LookupError ;
24+
25+ protected virtual void OnLookupError ( DNSLookupErrorArgs e )
26+ {
27+ LookupError ? . Invoke ( this , e ) ;
28+ }
2729
2830 public event EventHandler LookupComplete ;
2931
@@ -34,49 +36,69 @@ protected virtual void OnLookupComplete()
3436 #endregion
3537
3638 #region Methods
37- public void LookupAsync ( string hostnameOrIPAddress )
39+ public void LookupAsync ( string hostnameOrIPAddress , DNSLookupOptions dnsLookupOptions )
3840 {
3941 Task . Run ( ( ) =>
4042 {
41- dnsResolver . DnsServer = "2001:4860:4860::8888" ;
42- dnsResolver . TransportType = Heijden . DNS . TransportType . Udp ;
43- dnsResolver . Recursion = true ;
44- dnsResolver . TransportType = Heijden . DNS . TransportType . Tcp ;
45- Response dnsResponse = dnsResolver . Query ( hostnameOrIPAddress , QType . ANY , QClass . IN ) ;
43+ // This will convert the query to an Arpa request
44+ if ( dnsLookupOptions . Type == QType . PTR )
45+ {
46+ IPAddress ip ;
47+ if ( IPAddress . TryParse ( hostnameOrIPAddress , out ip ) )
48+ hostnameOrIPAddress = Resolver . GetArpaFromIp ( ip ) ;
49+ }
4650
47- if ( ! string . IsNullOrEmpty ( dnsResponse . Error ) )
48- MessageBox . Show ( dnsResponse . Error ) ;
51+ if ( dnsLookupOptions . Type == QType . NAPTR )
52+ hostnameOrIPAddress = Resolver . GetArpaFromEnum ( hostnameOrIPAddress ) ;
4953
50- foreach ( RecordA r in dnsResponse . RecordsA )
54+ if ( dnsLookupOptions . UseCustomDNSServer )
55+ dnsResolver . DnsServer = dnsLookupOptions . CustomDNSServer ;
56+
57+ dnsResolver . Recursion = dnsLookupOptions . Recursion ;
58+ dnsResolver . TransportType = dnsLookupOptions . TransportType ;
59+ dnsResolver . Retries = dnsLookupOptions . Attempts ;
60+ dnsResolver . TimeOut = dnsLookupOptions . Timeout ;
61+
62+ Response dnsResponse = dnsResolver . Query ( hostnameOrIPAddress , dnsLookupOptions . Type , dnsLookupOptions . Class ) ;
63+
64+ // If there was an error... return
65+ if ( ! string . IsNullOrEmpty ( dnsResponse . Error ) )
5166 {
52- MessageBox . Show ( r . Address . ToString ( ) ) ;
67+ OnLookupError ( new DNSLookupErrorArgs ( dnsResponse . Error ) ) ;
68+ return ;
5369 }
5470
71+ // A
72+ foreach ( RecordA r in dnsResponse . RecordsA )
73+ OnRecordReceived ( new DNSLookupRecordArgs ( r . RR , r . ToString ( ) ) ) ;
74+
75+ // AAAA
5576 foreach ( RecordAAAA r in dnsResponse . RecordsAAAA )
56- {
57- MessageBox . Show ( r . Address . ToString ( ) ) ;
58- }
77+ OnRecordReceived ( new DNSLookupRecordArgs ( r . RR , r . ToString ( ) ) ) ;
5978
79+ // CNAME
6080 foreach ( RecordCNAME r in dnsResponse . RecordsCNAME )
61- {
62- MessageBox . Show ( r . CNAME ) ;
63- }
64-
65- foreach ( RecordPTR r in dnsResponse . RecordsPTR )
66- {
67- MessageBox . Show ( r . PTRDNAME ) ;
68- }
81+ OnRecordReceived ( new DNSLookupRecordArgs ( r . RR , r . ToString ( ) ) ) ;
6982
83+ // MX
7084 foreach ( RecordMX r in dnsResponse . RecordsMX )
71- {
72- MessageBox . Show ( r . EXCHANGE ) ;
73- }
85+ OnRecordReceived ( new DNSLookupRecordArgs ( r . RR , r . ToString ( ) ) ) ;
7486
87+ // NS
7588 foreach ( RecordNS r in dnsResponse . RecordsNS )
76- {
77- MessageBox . Show ( r . NSDNAME ) ;
89+ OnRecordReceived ( new DNSLookupRecordArgs ( r . RR , r . ToString ( ) ) ) ;
7890
79- }
91+ // PTR
92+ foreach ( RecordPTR r in dnsResponse . RecordsPTR )
93+ OnRecordReceived ( new DNSLookupRecordArgs ( r . RR , r . ToString ( ) ) ) ;
94+
95+ // SOA
96+ foreach ( RecordSOA r in dnsResponse . RecordsSOA )
97+ OnRecordReceived ( new DNSLookupRecordArgs ( r . RR , r . ToString ( ) ) ) ;
98+
99+ // TXT
100+ foreach ( RecordTXT r in dnsResponse . RecordsTXT )
101+ OnRecordReceived ( new DNSLookupRecordArgs ( r . RR , r . ToString ( ) ) ) ;
80102
81103 OnLookupComplete ( ) ;
82104 } ) ;
0 commit comments