My small .NET library which covers some network related protocols includes a DNS library. With this library you can query a DNS for i.e. the A record (IP address of a domain) or the corresponding mail server of a given domain (MX record). Here are three examples using the library.
All queries are working the same: you have to build a DnsRequest that includes the Question. The Resolve method of the DnsResolver will return a DnsResponse which contains the answer (or answers).
On a desktop .NET environment you can automatically load the DNS settings of the network adapter (for .NET Micro Framework this will be added next).
DnsResolver dns = new DnsResolver(); dns.LoadNetworkConfiguration();
Or by specifying the DNS server in the constructor (currently necessary in .NET MF):
DnsResolver dns = new DnsResolver(IPAddress.Parse("192.168.100.1"));
1) Query IP Address of a given Domain (A Record)
DnsResponse res = dns.Resolve(
new DnsRequest ( new Question ( www.ajaxpro.info, DnsType.A, DnsClass.IN ) )
);
You will now have one or more answers in your DnsResponse:
foreach(Answer a in res.Answers) {
Console.WriteLine(a.ToString());
}
2) Query MX Records (Mail Exchange)
Instead of DnsType.A in the above example we’re using DnsType.MX, now. The rest of the code to write is the same. The MX record contains a Preference and a Domain that will be responsible for receiving mails for the domain. This can be accessible in the Record property of the Answer instance:
foreach(Answer a in res.Answers) {
MXRecord mx = a.Record as MXRecord;
if(mx != null) {
Console.WriteLine(mx.Preference + " " + mx.Domain);
}
}
3) Reverse DNS Lookup (PTR Record)
For PTR records you need to specify the IP address instead of a domain name in the Question object.
string ip = "192.168.100.99";
Question q = new Question(ip, DnsType.PTR, DnsClass.IN);
DnsResponse res = dns.Resolve(new DnsRequest(q));