Open Source DNS Lookup Library using C#

This example contains an open source DNS Lookup library and an example of using it to look up Txt Records.


using System;
using System.Collections.Generic;
using Heijden.DNS;

namespace DnsTestConsoleApp
{
    public class DnsTest
    {
        private readonly Resolver _resolver;

        public DnsTest()
        {
            _resolver = new Resolver();
            _resolver.Recursion = true;
            _resolver.UseCache = true;
            _resolver.DnsServer = "8.8.8.8"; // Google Public DNS

            _resolver.TimeOut = 1000;
            _resolver.Retries = 3;
            _resolver.TransportType = TransportType.Udp;
        }

        public IList TxtRecords(string name)
        {
            IList records = new List();
            const QType qType = QType.TXT;
            const QClass qClass = QClass.IN;

            Response response = _resolver.Query(name, qType, qClass);
            
            foreach (RecordTXT record in response.RecordsTXT)
            {
                records.Add(record.ToString());
            }

            return records;
        }
    }
}

This is called by a simple console app:


using System;
using System.Collections.Generic;

namespace DnsTestConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            DnsTest dnsTest = new DnsTest();

            Console.WriteLine("TXT Records for: robertsindall.co.uk");
            WriteList(dnsTest.TxtRecords("robertsindall.co.uk"));

            Console.ReadKey();
        }

        private static void WriteList(IEnumerable list)
        {
            foreach (string item in list)
            {
                Console.WriteLine(item);
            }
        }
    }
}

It has these available QTypes:

A
NS
MD
MF
CNAME
SOA
MB
MG
MR
NULL
WKS
PTR
HINFO
MINFO
MX
TXT
RP
AFSDB
X25
ISDN
RT
NSAP
NSAP_PTR
SIG
KEY
PX
GPOS
AAAA
LOC
NXT
EID
NIMLOC
SRV
ATMA
NAPTR
KX
CERT
A6
DNAME
SINK
OPT
APL
DS
SSHFP
IPSECKEY
RRSIG
NSEC
DNSKEY
DHCID
NSEC3
NSEC3PARAM
HIP
SPF
UINFO
UID
GID
UNSPEC
TKEY
TSIG
IXFR
AXFR
MAILB
MAILA
ANY
TA
DLV

It has these available QClasses:

IN
CS
CH
HS
ANY

You can download the Open Source DNS Lookup Library using C# solution here.



Comments

  1. Carlos Perez October 3rd

    Comment Arrow

    Just tried using it in PowerShell and it works great, until I try to do a AXFR

    $assembly = [System.Reflection.Assembly]::LoadFile(‘c:\Heijden.Dns.dll’)
    $resolver = new-object Heijden.DNS.Resolver
    $resolver.DnsServer = ’69.64.68.41′
    $resolver.TimeOut = 2000
    $resolver.Recursion =$True
    $resolver.TransportType = [Heijden.DNS.TransportType]::TCP
    $target = ‘zonetransfer.me’
    $resolver.Query($target,[Heijden.DNS.QType]::AXFR,[Heijden.DNS.QClass]::IN)

    Any clues why it fails only for AXFR?


Add Yours

  • Author Avatar

    YOU


Comment Arrow




About Author

Robert

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning hands down.