-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDNS.cs
More file actions
289 lines (238 loc) · 9.92 KB
/
DNS.cs
File metadata and controls
289 lines (238 loc) · 9.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace WindowsAPI
{
public class DNS
{
/// <summary>
/// See https://msdn.microsoft.com/en-us/library/windows/desktop/ms682082(v=vs.85).aspx
/// </summary>
#region DLL Imports
[DllImport("dnsapi", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern int DnsQuery_W([MarshalAs(UnmanagedType.LPWStr)] string pszName, DnsRecordType wType, QueryOption options, ref IP4_ARRAY dnsServerIpArray, ref IntPtr ppQueryResults, int pReserved);
[DllImport("dnsapi", CharSet = CharSet.Auto, SetLastError = true)]
static extern void DnsRecordListFree(IntPtr pRecordList, int FreeType);
enum QueryOption
{
DNS_QUERY_STANDARD = 0,
DNS_QUERY_ACCEPT_TRUNCATED_RESPONSE = 1,
DNS_QUERY_USE_TCP_ONLY = 2,
DNS_QUERY_NO_RECURSION = 4,
DNS_QUERY_BYPASS_CACHE = 8,
DNS_QUERY_DONT_RESET_TTL_VALUES = 0x100000,
DNS_QUERY_NO_HOSTS_FILE = 0x40,
DNS_QUERY_NO_LOCAL_NAME = 0x20,
DNS_QUERY_NO_NETBT = 0x80,
DNS_QUERY_NO_WIRE_QUERY = 0x10,
DNS_QUERY_RESERVED = -16777216,
DNS_QUERY_RETURN_MESSAGE = 0x200,
DNS_QUERY_TREAT_AS_FQDN = 0x1000,
DNS_QUERY_WIRE_ONLY = 0x100
}
enum DnsRecordType : short
{
DNS_TYPE_A = 1,
DNS_TYPE_NS = 2,
DNS_TYPE_CNAME = 5,
DNS_TYPE_SOA = 6,
DNS_TYPE_PTR = 12,
DNS_TYPE_HINFO = 13,
DNS_TYPE_MX = 15,
DNS_TYPE_TXT = 16,
DNS_TYPE_AAAA = 28
}
[StructLayout(LayoutKind.Sequential)]
private struct CNAMERecord
{
// Generic DNS record structure
public IntPtr pNext;
public string pName;
public DnsRecordType wType;
public short wDataLength;
public int flags;
public int dwTtl;
public int dwReserved;
// CANME record specific
public IntPtr pNameHost;
}
[StructLayout(LayoutKind.Sequential)]
private struct MXRecord
{
// Generic DNS record structure
public IntPtr pNext;
public string pName;
public DnsRecordType wType;
public short wDataLength;
public int flags;
public int dwTtl;
public int dwReserved;
// MX record specific
public IntPtr pNameExchange;
public short wPreference;
public short Pad;
}
[StructLayout(LayoutKind.Sequential)]
private struct TXTRecord
{
// Generic DNS record structure
public IntPtr pNext;
public string pName;
public DnsRecordType wType;
public short wDataLength;
public int flags;
public int dwTtl;
public int dwReserved;
// MX record specific
public int dwStringCount;
public IntPtr pStringArray;
}
public struct IP4_ARRAY
{
public UInt32 AddrCount;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1, ArraySubType = UnmanagedType.U4)] public UInt32[] AddrArray;
}
#endregion
public static string GetCNAMERecord(string domain, string[] serverIP = null)
{
IntPtr recordsArray = IntPtr.Zero;
IntPtr dnsRecord = IntPtr.Zero;
CNAMERecord cnameRecord;
IP4_ARRAY dnsServerArray = new IP4_ARRAY();
if (serverIP != null && serverIP.Length > 0)
{
dnsServerArray.AddrCount = (uint)(serverIP.Length);
dnsServerArray.AddrArray = new uint[serverIP.Length];
for (int C = 0; C < serverIP.Length; C++)
{
IPAddress iPAddress;
if (IPAddress.TryParse(serverIP[C], out iPAddress))
{
dnsServerArray.AddrCount++;
uint address = BitConverter.ToUInt32(iPAddress.GetAddressBytes(), 0);
dnsServerArray.AddrArray[C] = address;
}
}
}
// Interop calls will only work on Windows platform (no mono c#)
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
{
throw new NotSupportedException();
}
try
{
int queryResult = DnsQuery_W(domain, DnsRecordType.DNS_TYPE_CNAME, QueryOption.DNS_QUERY_BYPASS_CACHE, ref dnsServerArray, ref recordsArray, 0);
// Check for error
if (queryResult != 0)
{
throw new Win32Exception(queryResult);
}
// Loop through the result record list
for (dnsRecord = recordsArray; !dnsRecord.Equals(IntPtr.Zero); dnsRecord = cnameRecord.pNext)
{
cnameRecord = (CNAMERecord)Marshal.PtrToStructure(dnsRecord, typeof(CNAMERecord));
if (cnameRecord.wType == DnsRecordType.DNS_TYPE_CNAME)
{
string txt = Marshal.PtrToStringAuto(cnameRecord.pNameHost);
return txt==null?"":txt;
}
}
}
finally
{
DnsRecordListFree(recordsArray, 0);
}
return "";
}
public static List<string> GetMXRecords(string domain, string[] serverIP = null)
{
IntPtr ppResults = IntPtr.Zero;
MXRecord mxRecord;
IP4_ARRAY dnsServerArray = new IP4_ARRAY();
if (serverIP != null && serverIP.Length>0)
{
dnsServerArray.AddrCount = (uint)(serverIP.Length);
dnsServerArray.AddrArray = new uint[serverIP.Length];
for (int C = 0; C < serverIP.Length; C++)
{
IPAddress iPAddress;
if (IPAddress.TryParse(serverIP[C], out iPAddress))
{
dnsServerArray.AddrCount++;
uint address = BitConverter.ToUInt32(iPAddress.GetAddressBytes(), 0);
dnsServerArray.AddrArray[C] = address;
}
}
}
var results = new List<Tuple<short, string>>();
int error = DnsQuery_W(domain, DnsRecordType.DNS_TYPE_MX, QueryOption.DNS_QUERY_BYPASS_CACHE, ref dnsServerArray, ref ppResults, 0);
if (error != 0)
throw new Win32Exception(error);
for (var pRecord = ppResults; pRecord != IntPtr.Zero; pRecord = mxRecord.pNext)
{
mxRecord = (MXRecord)Marshal.PtrToStructure(pRecord, typeof(MXRecord));
if (mxRecord.wType == DnsRecordType.DNS_TYPE_MX)
{
string name = Marshal.PtrToStringAuto(mxRecord.pNameExchange);
results.Add(new Tuple<short, string>(mxRecord.wPreference, name));
}
}
DnsRecordListFree(ppResults, 0);
return results.OrderBy(x => x.Item1).Select(x => x.Item2).ToList();
}
public static List<string> GetTXTRecords(string domain, string[] serverIP = null)
{
IntPtr recordsArray = IntPtr.Zero;
IntPtr dnsRecord = IntPtr.Zero;
TXTRecord txtRecord;
IP4_ARRAY dnsServerArray = new IP4_ARRAY();
if (serverIP != null && serverIP.Length>0)
{
dnsServerArray.AddrCount = 0;
dnsServerArray.AddrArray = new uint[serverIP.Length];
for(int C=0; C<serverIP.Length;C++)
{
IPAddress iPAddress;
if(IPAddress.TryParse(serverIP[C],out iPAddress))
{
dnsServerArray.AddrCount++;
uint address = BitConverter.ToUInt32(iPAddress.GetAddressBytes(), 0);
dnsServerArray.AddrArray[C] = address;
}
}
}
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
{
throw new NotSupportedException();
}
var results = new List<string>();
try
{
int queryResult = DnsQuery_W(domain, DnsRecordType.DNS_TYPE_TXT, QueryOption.DNS_QUERY_BYPASS_CACHE,ref dnsServerArray, ref recordsArray, 0);
if (queryResult != 0)
{
throw new Win32Exception(queryResult);
}
for (dnsRecord = recordsArray; !dnsRecord.Equals(IntPtr.Zero); dnsRecord = txtRecord.pNext)
{
txtRecord = (TXTRecord)Marshal.PtrToStructure(dnsRecord, typeof(TXTRecord));
if (txtRecord.wType == DnsRecordType.DNS_TYPE_TXT)
{
string txt = Marshal.PtrToStringAuto(txtRecord.pStringArray);
results.Add(txt);
}
}
}
finally
{
DnsRecordListFree(recordsArray, 0);
}
return results;
}
}
}