How to do a DNS Name Lookup

Example of a DNS lookup of www.google.com using the DnDns Library
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;

using DnDns.Enums;
using DnDns.Query;
using DnDns.Records;

namespace DnDnsExamples
{
    class Program
    {
        static void Main(string[] args)
        {
            string dnsServer = "192.168.0.1";

            DnsQueryRequest request = new DnsQueryRequest();
            DnsQueryResponse response = request.Resolve(dnsServer, "www.google.com", NsType.A, NsClass.INET, ProtocolType.Udp);

            Console.WriteLine("Bytes received: " + response.BytesReceived);

            // Enumerate the Answer Records
            Console.WriteLine("Answers:");
            foreach (IDnsRecord record in response.Answers)
            {
                Console.WriteLine(record.Answer);
                Console.WriteLine("  |--- RDATA Field Length: " + record.DnsHeader.DataLength);
                Console.WriteLine("  |--- Name: " + record.DnsHeader.Name);
                Console.WriteLine("  |--- NS Class: " + record.DnsHeader.NsClass);
                Console.WriteLine("  |--- NS Type: " + record.DnsHeader.NsType);
                Console.WriteLine("  |--- TTL: " + record.DnsHeader.TimeToLive);
                Console.WriteLine();
            }

            Console.ReadLine();
        }
    }
}


Here's the output from the sample above:
Bytes received: 100
Answers:
Host: www.l.google.com.
  |--- RDATA Field Length: 8
  |--- Name: www.google.com.
  |--- NS Class: INET
  |--- NS Type: CNAME
  |--- TTL: 58849

Address: 64.233.167.104
  |--- RDATA Field Length: 4
  |--- Name: www.l.google.com.
  |--- NS Class: INET
  |--- NS Type: A
  |--- TTL: 300

Address: 64.233.167.147
  |--- RDATA Field Length: 4
  |--- Name: www.l.google.com.
  |--- NS Class: INET
  |--- NS Type: A
  |--- TTL: 300

Address: 64.233.167.99
  |--- RDATA Field Length: 4
  |--- Name: www.l.google.com.
  |--- NS Class: INET
  |--- NS Type: A
  |--- TTL: 300

Last edited Feb 5, 2008 at 1:53 PM by jmonty, version 2

Comments

No comments yet.