Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

DNS.NET Resolver (C#)

, 11 Mar 2013
A full implementation of a reusable DNS resolver component and a Dig.Net example application.
Article_Demo.zip
Article_Demo
DnsDig.exe
nunit.framework.dll
article_src-noexe.zip
DnsDig
DnsDig.suo
DnsDig
bin
Debug
Article_Demo.zip
DnsDig.vshost.exe.manifest
Dns
Records
NotUsed
Obsolete
Root
DnsDig.csproj.user
Doc
obj
Debug
DnsDig.AboutForm.resources
DnsDig.MainForm.resources
DnsDig.Properties.Resources.resources
Properties
Settings.settings
article_src.zip
DnsDig.suo
Article_Demo.zip
DnsDig.exe
DnsDig.pdb
DnsDig.vshost.exe
DnsDig.vshost.exe.manifest
nunit.framework.dll
Release
DnsDig.csproj.user
DesignTimeResolveAssemblyReferencesInput.cache
DnsDig.AboutForm.resources
DnsDig.csproj.GenerateResource.Cache
DnsDig.csprojResolveAssemblyReference.cache
DnsDig.exe
DnsDig.MainForm.resources
DnsDig.pdb
DnsDig.Properties.Resources.resources
TempPE
Properties.Resources.Designer.cs.dll
Settings.settings
article_src.zip.zip
DnsDig.suo
DnsDig.csproj.user
DnsExperiment.AboutForm.resources
DnsExperiment.csproj.GenerateResource.Cache
DnsExperiment.MainForm.resources
DnsExperiment.Properties.Resources.resources
Refactor
Properties.Resources.Designer.cs.dll
Settings.settings
using System;
using System.IO;
using System.Collections.Generic;
using System.Net;
using System.Text;

namespace Heijden.DNS
{
	#region Rfc 1034/1035
	/*
	4.1.2. Question section format

	The question section is used to carry the "question" in most queries,
	i.e., the parameters that define what is being asked.  The section
	contains QDCOUNT (usually 1) entries, each of the following format:

										1  1  1  1  1  1
		  0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
		+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
		|                                               |
		/                     QNAME                     /
		/                                               /
		+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
		|                     QTYPE                     |
		+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
		|                     QCLASS                    |
		+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+

	where:

	QNAME           a domain name represented as a sequence of labels, where
					each label consists of a length octet followed by that
					number of octets.  The domain name terminates with the
					zero length octet for the null label of the root.  Note
					that this field may be an odd number of octets; no
					padding is used.

	QTYPE           a two octet code which specifies the type of the query.
					The values for this field include all codes valid for a
					TYPE field, together with some more general codes which
					can match more than one type of RR.


	QCLASS          a two octet code that specifies the class of the query.
					For example, the QCLASS field is IN for the Internet.
	*/
	#endregion

	public class Question
	{
		private string m_QName;
		public string QName
		{
			get
			{
				return m_QName;
			}
			set
			{
				m_QName = value;
				if (!m_QName.EndsWith("."))
					m_QName += ".";
			}
		}
		public QType QType;
		public QClass QClass;

		public Question(string QName,QType QType,QClass QClass)
		{
			this.QName = QName;
			this.QType = QType;
			this.QClass = QClass;
		}

		public Question(RecordReader rr)
		{
			QName = rr.ReadDomainName();
			QType = (QType)rr.ReadUInt16();
			QClass = (QClass)rr.ReadUInt16();
		}

		private byte[] WriteName(string src)
		{
			if (!src.EndsWith("."))
				src += ".";

			if (src == ".")
				return new byte[1];

			StringBuilder sb = new StringBuilder();
			int intI, intJ, intLen = src.Length;
			sb.Append('\0');
			for (intI = 0, intJ = 0; intI < intLen; intI++, intJ++)
			{
				sb.Append(src[intI]);
				if (src[intI] == '.')
				{
					sb[intI - intJ] = (char)(intJ & 0xff);
					intJ = -1;
				}
			}
			sb[sb.Length - 1] = '\0';
			return System.Text.Encoding.ASCII.GetBytes(sb.ToString());
		}

		public byte[] Data
		{
			get
			{
				List<byte> data = new List<byte>();
				data.AddRange(WriteName(QName));
				data.AddRange(WriteShort((ushort)QType));
				data.AddRange(WriteShort((ushort)QClass));
				return data.ToArray();
			}
		}

		private byte[] WriteShort(ushort sValue)
		{
			return BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)sValue));
		}


		public override string ToString()
		{
			return string.Format("{0,-32}\t{1}\t{2}", QName, QClass, QType);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Alphons van der Heijden
Retired Van der Heijden Holding BV
Netherlands Netherlands
I'm Alphons van der Heijden, living in Lelystad, Netherlands, Europa, Earth. And currently I'm retiring from hard working ( ;- ), owning my own company. Because I'm full of energy, and a little to young to relax ...., I don't sit down, but create and recreate software solutions, that I like. Reinventing the wheel is my second nature. My interest is in the area of Internet technologies, .NET etc. I was there in 1992 when Mosaic came out, and from that point, my life changed dramatically, and so did the world, in fact. (Y)

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 11 Mar 2013
Article Copyright 2008 by Alphons van der Heijden
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid