Click here to Skip to main content
14,027,815 members
Click here to Skip to main content
Add your own
alternative version


20 bookmarked
Posted 14 Aug 2004

DNS Resolver RFC 1035

, 14 Aug 2004
Rate this:
Please Sign up or sign in to vote.
DNS resolving by RFC 1035; complete library for all versions of Windows.

Sample Image - dnsresolver.gif


I searched for a DNS Resolver for quite a long time. I got the solution, but it was too uncomfortable to use it. Thus, I made my own DNS Resolver; it's is very compact in size, and is free from the exceptions and errors we usually encounter with CSocket.

DNS Protocol Explanation (RFC 1035)

To query any type of domain name (for example, MX for mail exchange), the request is sent in the form of a binary record having a predefined bits and bytes pattern. Then, the server sends a response. If the server has the answer, it sends answers in the response records; otherwise, it sends other name servers that you then can query and get the answer from them. If other name servers don't have an answer, they in turn will again send other name servers. This process is endless, so we have to query recursively only up to a certain depth (I have chosen a depth of 10).

Library Source Code

The Dns.h file contains all DNS-related source code; it uses other libraries, such as SocketEx.h, SocketClient.h, and NeuroBuffer.h.

Library File NameDescription
CSocketExSocket functions wrapper class (CSocket is very heavy and unreliable if you don't have the exact idea of how it works). All the functions are of the same name as CSocket. You can use this class directly.
CSocketClientDerived from CSocketEx and throws proper exceptions with details of Winsock errors. It defines two operators, >> and <<, for easy send and receive. It also changes network to host and host to network order of bytes, if required.
CNeuroBufferBecause DNS response-request cycles are in binary form, a bytes buffer is used here.
CDnsBufferDNS responses contain domain names; these domain names are compressed with an LZW type of compression. It means that, rather than repeating names, it just gives a pointer to the previous name that occurred in the buffer. So, to extract, it's very important to follow this protocol. The CDnsBuffer class manages this decompression and gives you an expanded domain name.
** CDnsClientThis is the main class; you can either create an object of CDnsClient to query a single name server without any recursion, or you can call its static members Get and GetMX. It's better that you call static members only.
CDnsRRThis is the base class of response of the DNS request, which then is derived further for particular types.
CDnsRRMXThis is the response record of Type MX. It has the Exchange and Preference fields used for MX.
CDnsRRAResponse record for type A.
CDnsRRNSResponse record of type NS.
CDnsRRDefaultI have not programmed all classes of DNS here. I have programmed only particular classes, the rest of which are not programmed. You will find it in the CDnsRRDefault class; it stores the length of the buffer and the data buffer pointer.
CDnsRRPtrListWhen you interact with CDnsClient, the responses are returned in CDnsRRPtrList, which is a CList of the CDnsRR* pointer. You have to be careful while accessing the data of pointer. When CDnsRRPtrList gets destroyed, it automatically deletes all pointers!! So, you must keep a copy of the response record's data, which you need. Because these pointers you get are in the form of CDnsRR* only, you will need to cast according to Type. If you query for MX, you must cast CDnsRRMX * pMX = (CDnsRR*) pRR, and so forth. And then, access data from pMX. You can find the type in the Type field of the response record: "pRR->Type".

Note: I don't usually program in the form of different .h and .cpp files because using them somewhere else the next time is a big problem. You have to move both files here and there, so I put all of my code in the .h file only; I don't write to the .cpp file unless it's required. You need only to copy the SocketEx.h, SocketClient.h, NeuroBuffer.h, and Dns.h files into your project's directory, and add the line:

#include "<FONT color=#ff0000>dns.h</FONT>"

after your

#if !defined(.....

and so forth code of your Visual Studio-generated file. If you put it above this, you will get n number of errors.

Here's the main body of code:

  #include "Dns.h"

  void main(int nArgs,char * pcArgs[])
    CStringList MXs;
    CString Log;
    Log = CDnsClient::GetMX("",MXs);

    // Other Way
    // Additional Records sent by DNS Server usually contains
    // A records of all names, A records gives IP Addresses
    CDnsRRPtrList RRs,Additionals;

    // NSTried List, you must give this as an empty list.
    // All name servers have to be queried only once; this
    // maintains all name servers tried...
    CStringList NSTried;
    int nTries = 10;
    BOOL bResult =
                  NULL,            // Put null if you don't know
                                   // ther server's name
                  RRs,             // Answers
                  Additionals,     // Additional

    POSITION Pos = RRs.GetHeadPosition();
      CDnsRR * pRR = RRs.GetNext(Pos);
      CDnsRRMX * pRRMX = (CDnsRRMX*) pRR;

More about me.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Akash Kava
Web Developer
United States United States
Programmer with WILL

You may also be interested in...

Comments and Discussions

GeneralMX to ANY Pin
Dave_Nunes24-Jan-06 8:12
memberDave_Nunes24-Jan-06 8:12 
GeneralRe: MX to ANY Pin
spring3-Apr-06 17:45
memberspring3-Apr-06 17:45 
GeneralStill errors Pin
Andre Korenev8-Jun-05 6:15
sussAndre Korenev8-Jun-05 6:15 
GeneralTry the solution for our DNS MX Re: Still errors Pin
TongGao Tang27-Feb-07 4:05
memberTongGao Tang27-Feb-07 4:05 
GeneralWhoa. Lots of strings leaking Pin
Chris Meech29-Apr-05 4:42
memberChris Meech29-Apr-05 4:42 
Questionhow to work with it in LAN Pin
nice-hareesh1-Mar-05 5:15
membernice-hareesh1-Mar-05 5:15 
AnswerRe: how to work with it in LAN Pin
Akash Kava1-Mar-05 7:01
memberAkash Kava1-Mar-05 7:01 
Generalthank you, give me this Pin
nice-hareesh2-Mar-05 6:40
membernice-hareesh2-Mar-05 6:40 
QuestionWhat abt TXT resolver Pin
ThatsAlok26-Jan-05 18:12
memberThatsAlok26-Jan-05 18:12 
AnswerRe: What abt TXT resolver Pin
Akash Kava26-Jan-05 18:56
memberAkash Kava26-Jan-05 18:56 
GeneralRe: What abt TXT resolver Pin
ThatsAlok27-Jan-05 22:49
memberThatsAlok27-Jan-05 22:49 
GeneralBreaking the functionality into Headers and Source Files Pin
Koundinya13-Sep-04 23:30
memberKoundinya13-Sep-04 23:30 
GeneralMX Records for Mail Server Pin
Koundinya13-Sep-04 8:03
memberKoundinya13-Sep-04 8:03 
GeneralRe: MX Records for Mail Server Pin
Akash Kava13-Sep-04 19:19
memberAkash Kava13-Sep-04 19:19 
Yes it will, but it currently supports only TCP/IP based solution, not the UDP based.

-Akash Kava
GeneralRe: MX Records for Mail Server Pin
Koundinya13-Sep-04 23:20
memberKoundinya13-Sep-04 23:20 
GeneralMemory leaks Pin
789slashdot18-Aug-04 10:18
member789slashdot18-Aug-04 10:18 
GeneralMX lookups are implemented with TCP/IP, not UDP Pin
Anatoly18-Aug-04 6:32
memberAnatoly18-Aug-04 6:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190419.4 | Last Updated 15 Aug 2004
Article Copyright 2004 by Akash Kava
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid