Monday, February 23, 2009

3 Examples using MSchwarz.Net.Dns

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(""));


1) Query IP Address of a given Domain (A Record)

DnsResponse res = dns.Resolve(
    new DnsRequest ( new Question (, DnsType.A, DnsClass.IN ) )

You will now have one or more answers in your DnsResponse:

foreach(Answer a in res.Answers) {

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 = "";
Question q = new Question(ip, DnsType.PTR, DnsClass.IN);

DnsResponse res = dns.Resolve(new DnsRequest(q));


Celiker BAHCECI said...


Anna said...

microDNS.dll includes a reference to System.Net.IPAddress that is not available in micro .net project hence a micro .net project refrencing this dll will not compile. Am I missing something?