A .NET DNS Lookup Tool

Do you know what a DNS is?

Some of you might, and some may not, but it’s most likely something you use every single day without realizing. A DNS Lookup is the process of taking a domain name and getting its IP address, or taking an IP address and getting its domain name.

Whose Address?

I thought you might say that 🙂

Okay, so let me try to explain what an address is first. Like many people, you probably live in a house, flat, or apartment. Irrespective of what you do live in, that residence will have some kind of unique marker that identifies its location on planet Earth.

I, for example, live in a small market town in a sleepy little corner of a UK county called county Durham. The marker that identifies my residence might be something like the following:

123

A street

A Market Town

County Durham

United Kingdom

If you were in the UK, you could use this information, and find your way straight to my front door.

The Royal Mail in the UK also uses a number to identify my property. It’s called a UPRN (Universal Post Reference Number), which unfortunately I’m unable to tell you because I can’t read the barcode on the envelope I have in front of me. For arguments sake, however, let’s just imagine that this number is 1234567890.

The Royal Mail knows that this number correlates to my address above, but they don’t expect me to know or even be aware of that number because it’s just for their computers to sort the mail at the sorting office into the correct outgoing postman’s van. Likewise, when someone sends me a letter, it’s expected that the sender will use the full address because that’s easier for them to remember.

The Internet is very similar. Whenever you type an address into your browser, let’s say, for example, “www.codeguru.com”, that address is sent to a server whose job it is to translate that address to a number.

Number-based addresses on the Internet are a combination of four numbers from 1 to 255 joined by full stops (periods).

Codeguru’s numerical address, for example, is:

80.231.228.42

Once this translation has been done, your browser then makes a connection to the server that is present at this address and starts to request the web page you asked for from it.

Lookups are also done the other way, too. For example, 77.238.184.150, when looked up, equates to “dailyquiz.yahoo.net”, which is one of the servers responsible for serving “www.yahoo.co.uk”.

It may come as no surprise that .NET has a number of classes and methods built in to both help manage IP Addresses, and perform lookups between host names and IP Addresses.

Performing Lookups for DNS in .NET

Lookups are performed by using the System.Net.Dns Namespace, specifically the static “GetHostEntry” method call.

Taking one of our previous examples, we can easily do the following

IPHostEntry hostEntry =
   System.Net.Dns.GetHostEntry("77.238.184.150")

Which, when run through the Visual Studio Debugger, comes back with the following results:

Lookup1
Figure 1: Results of running the VS Debugger

You can clearly see from the debugger display, that the address in question actually has three other addresses connected to it, all of which translate to “dailyquiz.yahoo.net”. It can sometimes be fun to pick addresses at random, and then attempt to resolve them to see who or what they belong to, so to finish this post off, we’ll build a little Windows Forms tool to do just that.

Fire up Visual Studio and create a basic WinForms project. On your form, you need the following: two label controls, one textbox control, two buttons, and a list box.

Lay your form out so that it looks something like this:

Lookup2
Figure 2: Laying out the form

The code to handle the pick random IP button is as follows:

private void BtnResolveIpClick(object sender, EventArgs e)
{
   Random rand = new Random();
   var bytes = new byte[4];
   rand.NextBytes(bytes);
   string ipAddress = String.Format("{0}.{1}.{2}.{3}",
      bytes[0], bytes[1], bytes[2], bytes[3]);
   txtInputAddress.Text = ipAddress;
}

And the code to handle the resolve address button looks like this:

private void BtnResolveDomainClick(object sender, EventArgs e)
{
   bool validip;
   lsbResults.Items.Clear();
   lsbResults.Items.Add("Looking up "
      + txtInputAddress.Text
      + " Please wait...");
   IPHostEntry ipHost = new IPHostEntry();
   Application.DoEvents();

   try
   {
      ipHost = Dns.GetHostEntry(txtInputAddress.Text);
      validip = true;
   }
   catch (SocketException se)
   {
      var message = se.Message.ToLower();
      if(message.Equals("no such host is known"))
      {
         validip = false;
      }
      else
      {
         throw;
      }
   }

   if(validip)
   {
      foreach(IPAddress ip in ipHost.AddressList)
      {
         lsbResults.Items.Add(ip.AddressFamily.ToString());
         lsbResults.Items.Add(ip.ToString());
      }
      lsbResults.Items.Add("Host name is : " + ipHost.HostName);
   }
   else
   {
      lsbResults.Items.Add("Could not resolve "
         + txtInputAddress.Text + " unknown host.");
   }
}

As you can see, it’s wise to perform DNS resolution inside a try catch block because when a name cannot be looked up, it will cause a “SocketException” with a message of “No such host is known” to be thrown by the runtime.

Press F5 to run your program, try a few random IP addresses, and then see if you can resolve them. Eventually, you should get a hit.

Lookup3
Figure 3: Finding a random IP address

Going forward from here, it wouldn’t take much to create a fully fledged scanner, and do things such as resolve all addresses from xx.xx.xx.1 to xx.xx.xx.255 using a for loop.

Remember, too, that you can type an English name into the box in your program and resolve that too.

Lookup4
Figure 4: Finding a specific IP address

You could quite easily write a program to get a list of domain names from a database and resolve that list to a list of IP addresses.

Got a burning .NET question? Feel free to hunt me down on the interwebs and ask me. You can usually find me hanging around in the usual places such as Stack Overflow & Code Project under the name of Shawty, or on Twitter as @shawty_ds. I’ll see if I can incorporate your question in a future post.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read