Click here to Skip to main content
Click here to Skip to main content

DNS Query MFC based Application

By , 8 Nov 2007
 

Introduction

When you connect to the server with the address 'www.codeproject.com', your browser needs its numeric representation 69.10.233.10 which it obtains by means of DNS queries. The same process is going on when your mail program needs to send email to a specific domain like webmaster@codeproject.com. Here, it needs to know the mail server for the codeproject domain which is obtained by means of a DNS query. There are other types of queries you may get with DNS on particular domain name. In this article, I provide a nice GUI MFC application with Document/View architecture for performing all types of DNS queries.

Background

The good way to start with DNS is to have a look at the Domain Name System topic on MSDN or SDK help under Networking/Network Protocols part. It describes the basics and provides you with the necessary RFCs to read, e.g. RFC 1034 (Domain Names-Concepts and Facilities-Implementation and Specification-Application and Support), RFC 1035 (Domain Names) and so on.

Using the Code

Make sure first that you are connected to the internet. Press CTRL+Q or go to File menu and click DnsQuery. You will get this dialog box.

DNS query setup

Enter the name of the server you want to query, like codeproject.com, choose the type of your query, options and optionally you may use your own default DNS servers or ask for a specific DNS server by typing it in the bottom edit box.

This example provides two queries performed of type A and NS to get numeric address for codeproject.com and its name servers.

DNS queries of type A and NS

In the DNS Query box in the previous image, the DNS server ns.dundas.com is specified to ask it for query type ANY to get all the information about the codeproject.com server.

DNS query of type ANY

You can find descriptions for other types of queries in the corresponding RFCs or in the windns.h file:

//

//  DNS Record Types

//

//  _TYPE_ defines are in host byte order.

//  _RTYPE_ defines are in net byte order.

//

//  Generally always deal with types in host byte order as we index

//  resource record functions by type.

//


#define DNS_TYPE_ZERO       0x0000

//  RFC 1034/1035

#define DNS_TYPE_A          0x0001      //  1

#define DNS_TYPE_NS         0x0002      //  2

#define DNS_TYPE_MD         0x0003      //  3

#define DNS_TYPE_MF         0x0004      //  4

#define DNS_TYPE_CNAME      0x0005      //  5

#define DNS_TYPE_SOA        0x0006      //  6

#define DNS_TYPE_MB         0x0007      //  7

#define DNS_TYPE_MG         0x0008      //  8

#define DNS_TYPE_MR         0x0009      //  9

#define DNS_TYPE_NULL       0x000a      //  10

#define DNS_TYPE_WKS        0x000b      //  11

#define DNS_TYPE_PTR        0x000c      //  12

#define DNS_TYPE_HINFO      0x000d      //  13

#define DNS_TYPE_MINFO      0x000e      //  14

#define DNS_TYPE_MX         0x000f      //  15

#define DNS_TYPE_TEXT       0x0010      //  16


//  RFC 1183

#define DNS_TYPE_RP         0x0011      //  17

#define DNS_TYPE_AFSDB      0x0012      //  18

#define DNS_TYPE_X25        0x0013      //  19

#define DNS_TYPE_ISDN       0x0014      //  20

#define DNS_TYPE_RT         0x0015      //  21


//  RFC 1348

#define DNS_TYPE_NSAP       0x0016      //  22

#define DNS_TYPE_NSAPPTR    0x0017      //  23


//  RFC 2065    (DNS security)

#define DNS_TYPE_SIG        0x0018      //  24

#define DNS_TYPE_KEY        0x0019      //  25


//  RFC 1664    (X.400 mail)

#define DNS_TYPE_PX         0x001a      //  26


//  RFC 1712    (Geographic position)

#define DNS_TYPE_GPOS       0x001b      //  27


//  RFC 1886    (IPv6 Address)

#define DNS_TYPE_AAAA       0x001c      //  28


//  RFC 1876    (Geographic location)

#define DNS_TYPE_LOC        0x001d      //  29


//  RFC 2065    (Secure negative response)

#define DNS_TYPE_NXT        0x001e      //  30


//  Patton      (Endpoint Identifier)

#define DNS_TYPE_EID        0x001f      //  31


//  Patton      (Nimrod Locator)

#define DNS_TYPE_NIMLOC     0x0020      //  32


//  RFC 2052    (Service location)

#define DNS_TYPE_SRV        0x0021      //  33


//  ATM Standard something-or-another (ATM Address)

#define DNS_TYPE_ATMA       0x0022      //  34


//  RFC 2168    (Naming Authority Pointer)

#define DNS_TYPE_NAPTR      0x0023      //  35


//  RFC 2230    (Key Exchanger)

#define DNS_TYPE_KX         0x0024      //  36


//  RFC 2538    (CERT)

#define DNS_TYPE_CERT       0x0025      //  37


//  A6 Draft    (A6)

#define DNS_TYPE_A6         0x0026      //  38


//  DNAME Draft (DNAME)

#define DNS_TYPE_DNAME      0x0027      //  39


//  Eastlake    (Kitchen Sink)

#define DNS_TYPE_SINK       0x0028      //  40


//  RFC 2671    (EDNS OPT)

#define DNS_TYPE_OPT        0x0029      //  41


//

//  IANA Reserved

//


#define DNS_TYPE_UINFO      0x0064      //  100

#define DNS_TYPE_UID        0x0065      //  101

#define DNS_TYPE_GID        0x0066      //  102

#define DNS_TYPE_UNSPEC     0x0067      //  103


//

//  Query only types (1035, 1995)

//      - Crawford      (ADDRS)

//      - TKEY draft    (TKEY)

//      - TSIG draft    (TSIG)

//      - RFC 1995      (IXFR)

//      - RFC 1035      (AXFR up)

//


#define DNS_TYPE_ADDRS      0x00f8      //  248

#define DNS_TYPE_TKEY       0x00f9      //  249

#define DNS_TYPE_TSIG       0x00fa      //  250

#define DNS_TYPE_IXFR       0x00fb      //  251

#define DNS_TYPE_AXFR       0x00fc      //  252

#define DNS_TYPE_MAILB      0x00fd      //  253

#define DNS_TYPE_MAILA      0x00fe      //  254

#define DNS_TYPE_ALL        0x00ff      //  255

#define DNS_TYPE_ANY        0x00ff      //  255


//

//  Temp Microsoft types -- use until get IANA approval for real type

//


#define DNS_TYPE_WINS       0xff01      //  64K - 255

#define DNS_TYPE_WINSR      0xff02      //  64K - 254

#define DNS_TYPE_NBSTAT     (DNS_TYPE_WINSR)

You can also find the description of the query I perform with the DnsQuery() function in MSDN help. I call it in the CDnsView class:

void CDnsView::OnFileDnsquery()
{
    PDNS_RECORD pdns;
    DNS_FREE_TYPE freetype = DnsFreeRecordList;
    in_addr inaddr;
    char digit[10];

    CDnsDialog dns(¶ms);
    if (dns.DoModal() == IDOK) {
            message.Format("Q? %s %s  %s \r\n", dns.m_name, 
                Type[params.type].title, params.servers);
            pEdit->SetSel(pEdit->GetWindowTextLength(), 
                pEdit->GetWindowTextLength());
            pEdit->ReplaceSel(message);

            if (res = DnsQuery(dns.m_name, dns.m_type, dns.m_options, 
                dns.m_ip4array, &pdns, 0)) {
                    LPVOID lpMsgBuf;
                    if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 
                        FORMAT_MESSAGE_FROM_SYSTEM | 
                        FORMAT_MESSAGE_IGNORE_INSERTS,
                            0, res, MAKELANGID(LANG_NEUTRAL, 
                        SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, 0)) {
                            message.Format("-ERR: %s \r\n", 
                    (LPCTSTR)lpMsgBuf);
                            pEdit->SetSel(pEdit->GetWindowTextLength(), 
                    pEdit->GetWindowTextLength());
                            pEdit->ReplaceSel(message);
                            LocalFree(lpMsgBuf);
                    } else {
                            pEdit->SetSel(pEdit->GetWindowTextLength(), 
                    pEdit->GetWindowTextLength());
                            pEdit->ReplaceSel
                    ("-ERR FormatMessage failed.?\r\n");
                    }
                    return;
           }

        do {
                switch (pdns->wType) {
                    case DNS_TYPE_A:
                            inaddr.S_un.S_addr = pdns->Data.A.IpAddress;
                            message.Format("   A %s [%s]", pdns->pName, 
                            inet_ntoa(inaddr));
                            break;

                    case DNS_TYPE_NS:
                            message.Format("   NS %s %s", pdns->pName, 
                                pdns->Data.NS.pNameHost);
                            break;

                    case DNS_TYPE_MD:
                            message.Format("   MD %s %s", pdns->pName, 
                                pdns->Data.MD.pNameHost);
                            break;

                    case DNS_TYPE_MF:
                            message.Format("   MF %s %s", pdns->pName, 
                                pdns->Data.MF.pNameHost);
                            break;

                    case DNS_TYPE_CNAME:
                            message.Format("   CNAME %s %s", pdns->pName, 
                                pdns->Data.CNAME.pNameHost);
                            break;

                    case DNS_TYPE_SOA:
                            message.Format("   SOA %s  nameserv: %s\r\n", 
                                pdns->pName, pdns->Data.SOA.pNamePrimaryServer);         
                            temp.Format("              admin: %s\r\n", 
                                pdns->Data.SOA.pNameAdministrator);
                            message += temp;
                            temp.Format("             serial: %u\r\n", 
                                pdns->Data.SOA.dwSerialNo);
                            message += temp;
                            temp.Format("            refresh: %u\r\n", 
                                pdns->Data.SOA.dwRefresh);
                            message += temp;
                            temp.Format("                ttl: %u\r\n", 
                                pdns->Data.SOA.dwDefaultTtl);
                            message += temp;
                            temp.Format("             expire: %u\r\n", 
                                pdns->Data.SOA.dwExpire);
                            message += temp;
                            temp.Format("              retry: %u", 
                                pdns->Data.SOA.dwRetry);
                            message += temp;
                            break;

                    case DNS_TYPE_MB:
                            message.Format("   MB %s %s", pdns->pName, 
                                pdns->Data.MB.pNameHost);
                            break;

                    case DNS_TYPE_MG:
                            message.Format("   MG %s %s", pdns->pName, 
                                pdns->Data.MG.pNameHost);
                            break;

                    case DNS_TYPE_MR:
                            message.Format("   MR %s %s", pdns->pName, 
                                pdns->Data.MR.pNameHost);
                            break;

                            //case DNS_TYPE_NULL:

                            //  message.Format("   MB %s %s",pdns->pName,

                                pdns->Data.Null.MR.pNameHost);
                            //break;


                    case DNS_TYPE_WKS:
                            inaddr.S_un.S_addr = pdns->Data.WKS.IpAddress;
                            message.Format("   WKS %s [%s] proto: 
                                %d mask: %d", pdns->pName, 
                                inet_ntoa(inaddr), 
                                pdns->Data.WKS.chProtocol, 
                                pdns->Data.WKS.BitMask);
                            break;

                    case DNS_TYPE_PTR:
                            message.Format("   PTR %s %s", pdns->pName, 
                                pdns->Data.PTR.pNameHost);
                            break;

                    case DNS_TYPE_HINFO:
                            message.Format("   HINFO %s", pdns->pName);
                            for (u_int i = 0; i < 
                                pdns->Data.HINFO.dwStringCount; i++) {
                                    message += "\r\n            ";
                                    message += pdns->
                        Data.HINFO.pStringArray[i];
                            }
                            break;

                    case DNS_TYPE_MINFO:
                            message.Format("   MINFO %s err: %s name: %s", 
                            pdns->pName, pdns->
                            Data.MINFO.pNameErrorsMailbox, 
                            pdns->Data.MINFO.pNameMailbox);
                            break;

                    case DNS_TYPE_MX:
                            message.Format("   MX %s %s pref: %d", 
                            pdns->pName, pdns->Data.MX.pNameExchange, 
                    pdns->Data.MX.wPreference);
                            break;

                    case DNS_TYPE_TEXT:
                            message.Format("   TEXT %s", pdns->pName);
                            for (u_int i = 0; i < pdns->
                        Data.TXT.dwStringCount; i++) {
                                    message += "\r\n            ";
                                    message += pdns->
                        Data.TXT.pStringArray[i];
                            }
                            break;

                    case DNS_TYPE_RP:
                            message.Format("   RP %s err: %s name: %s", 
                                   pdns->pName, 
                        pdns->Data.RP.pNameErrorsMailbox, 
                                 pdns->Data.RP.pNameMailbox);
                            break;

                    case DNS_TYPE_AFSDB:
                            message.Format("   AFSDB %s %s pref: %d", 
                                   pdns->pName, pdns->Data.AFSDB.pNameExchange, 
                                  pdns->Data.AFSDB.wPreference);
                            break;

                    case DNS_TYPE_X25:
                            message.Format("   X25 %s", pdns->pName);
                            for (u_int i = 0; i < 
                                 pdns->Data.X25.dwStringCount; i++) {
                                    message += "\r\n            ";
                                    message += pdns->Data.X25.pStringArray[i];
                            }
                            break;

                    case DNS_TYPE_ISDN:
                            message.Format("   ISDN %s", pdns->pName);
                            for (u_int i = 0; i < 
                                  pdns->Data.ISDN.dwStringCount; i++) {
                                    message += "\r\n            ";
                                    message += pdns->Data.ISDN.pStringArray[i];
                            }
                            break;

                    case DNS_TYPE_RT:
                            message.Format("   RT %s %s pref: %d", 
                                   pdns->pName, pdns->Data.RT.pNameExchange, 
                                 pdns->Data.RT.wPreference);
                            break;

                            //case DNS_TYPE_SIG:

                            //break;

                            //case DNS_TYPE_KEY:

                            //break;


                    case DNS_TYPE_AAAA:
                            message.Format("   AAAA %s %s pref: %d 
                                  [", pdns->pName);
                            for (int i = 0; i < 16; i++) {
                                sprintf(digit, "%d", 
                                      pdns->Data.AAAA.Ip6Address.IP6Byte[i]);
                                    message += digit;
                                    if (i != 15)
                                            message += ".";
                            }
                            message += "]";
                            break;

                    case DNS_TYPE_SRV:
                            message.Format("   SRV %s %s port:%d prior:%d 
                                   weight:%d", pdns->pName, 
                                   pdns->Data.SRV.pNameTarget, 
                                   pdns->Data.SRV.wPort, 
                                  pdns->Data.SRV.wPriority, 
                                pdns->Data.SRV.wWeight);
                            break;

                            //case DNS_TYPE_WINS:

                            //break;


                    case DNS_TYPE_WINSR:
                            message.Format("   NBSTAT %s %s", 
                                  pdns->pName, pdns->
                            Data.WINSR.pNameResultDomain);
                            break;

                    default:
                            message.Format(" unknown: %s type %d", 
                                  pdns->pName, pdns->wType);
                            break;
                    }

                    if (pdns->Flags.S.Section == DNSREC_AUTHORITY)
                            message += "   (authority)";
                    if (pdns->Flags.S.Section == DNSREC_ADDITIONAL)
                            message += "   (additional)";

                    pEdit->SetSel(pEdit->GetWindowTextLength(), 
                          pEdit->GetWindowTextLength());
                    pEdit->ReplaceSel(message + "\r\n");

                    pdns = pdns->pNext;
            } while (pdns);

            pEdit->SetSel(pEdit->GetWindowTextLength(), 
                  pEdit->GetWindowTextLength());
            pEdit->ReplaceSel("\r\n");

            DnsRecordListFree(pdns, freetype);
    }
}

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

About the Author

Chesnokov Yuriy
Engineer
Russian Federation Russian Federation
Member
Former Cambridge University postdoc (http://www-ucc-old.ch.cam.ac.uk/research/yc274-research.html), Department of Chemistry, Unilever Centre for Molecular Informatics, where I worked on the problem of complexity analysis of cardiac data.
 
As a subsidiary result we achieved 1st place in the annual PhysioNet/Computers in Cardiology Challenge 2006: QT Interval Measurement (http://physionet.org/challenge/2006/)
 
My research intrests are: digital signal processing in medicine, image and video processing, pattern recognition, AI, computer vision.
 
My recent publications are:
 
Complexity and spectral analysis of the heart rate variability dynamics for distant prediction of paroxysmal atrial fibrillation with artificial intelligence methods. Artificial Intelligence in Medicine. 2008. V43/2. PP. 151-165 (http://dx.doi.org/10.1016/j.artmed.2008.03.009)
 
Face Detection C++ Library with Skin and Motion Analysis. Biometrics AIA 2007 TTS. 22 November 2007, Moscow, Russia. (http://www.dancom.ru/rus/AIA/2007TTS/ProgramAIA2007TTS.html)
 
Screening Patients with Paroxysmal Atrial Fibrillation (PAF) from Non-PAF Heart Rhythm Using HRV Data Analysis. Computers in Cardiology 2007. V. 34. PP. 459–463 (http://www.cinc.org/archives/2007/pdf/0459.pdf)
 
Distant Prediction of Paroxysmal Atrial Fibrillation Using HRV Data Analysis. Computers in Cardiology 2007. V. 34. PP. 455-459 (http://www.cinc.org/archives/2007/pdf/0455.pdf)
 
Individually Adaptable Automatic QT Detector. Computers in Cardiology 2006. V. 33. PP. 337-341 http://www.cinc.org/archives/2006/pdf/0337.pdf)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralDNS_TYPE_NAPTRmemberaddick29 Feb '08 - 0:21 
Hi,
 
I'm trying to get WinDNS to query NAPTR records (using DNS_TYPE_NAPTR) and stumbled upon your sample.
 
It seems that NAPTR queries succeed in your app, but that you don't recognise the response (it falls through to the default case in the switch statement).
 
Do you know how I can interpret these results?
 
I have found other code samples that attempt to access pdns->Data.NAPTR, but this doesn't compile for me under VS2005 ('NAPTR' : is not a member of '_DnsRecordA::<unnamed-tag>'). It seems bizarre that Microsoft would include the search type in their headers but not provide structures to handle the response, so I'm guessing I've done something wrong.
 
Any ideas/suggestions?
 
Thanks.
AnswerRe: DNS_TYPE_NAPTRmvpChesnokov Yuriy29 Feb '08 - 0:51 
use manual DNS requests composing requests yourself.
may be .NET has solution
 
chesnokov

GeneralRe: DNS_TYPE_NAPTRmemberaddick29 Feb '08 - 0:55 
Do you know for sure that this can't be done through the DnsQuery API function?
AnswerRe: DNS_TYPE_NAPTRmvpChesnokov Yuriy29 Feb '08 - 4:51 
I never told you to use it in DnsQuery.
Read rfcs about raw requests. I've got Internet firewall article here you can use raw sockets from there.
 
chesnokov

GeneralRe: DNS_TYPE_NAPTRmemberaddick29 Feb '08 - 5:11 
You're right, you didn't. I was just asking whether you knew for sure that it wasn't possible.
 
Anyway, after much googling this afternoon I found this here...
 
"If the record is of a "Windows knows" type (e.g. SRV or A record), then
Windows "helpfully" converts the response into the appropriate data
structure, e.g. DNS_SRV_DATA, which is in the Data field of each record
returned by DnsQuery().
 
If the record is _NOT_ of a known type (and NAPTR is _not_ a type that
Windows understands, despite having a #define for the record type), then the
response is presented, untouched, in the Data field and the wDataLength
field indicates how many bytes are present."

 
So it looks like it can be done after all, if messily.
 
Thanks for your prompt responses anyway.
Generalgoodmemberrajantawate1(http//www.jhatak.com)11 Nov '07 - 16:32 
good
 
http://www.meetingflex.com
Blazing Fast Video Search.................................................Smile | :) -----------Smile | :)
GeneralMy Own assigned IP AddressmemberAlexEvans10 Nov '07 - 15:00 
Hello and thanks for sharing...
 
I am not sure that my question "Belongs" in this article, but I can't find where else can I ask it...
 
Whenever my Server is turned OFF, fo whatever reason, my ISP assigns it another IP address... They tell me that with ADSL it is possible to be assigned a fixed IP, but not so with Cable...
 
So, each time this happens, I can no longer connect t othe server from home... I want to create a program that will be started at start-up, that will interrogate the external IP address that was assigned by the ISP, if I can find it out (same as WHATISMYIP.COM, but autmatic...) then I can get the Server to "e-mail" this to my home...
 
Hope someone can help me
Thanks
 
Alex
GeneralRe: My Own assigned IP AddressmemberA. Riazi11 Nov '07 - 8:31 
You can use DynamicDNS.org applications.
GeneralRe: My Own assigned IP AddressmemberAlexEvans30 Nov '07 - 10:38 
Hello there,
 
Thanks for the tip, but it seems this is NOT a free utility...
 
Or am I missing something?
 
Cheers
Alex
GeneralRe: My Own assigned IP AddressmemberAmer Gerzic15 Jan '08 - 0:32 
It's free ... just look for free account! I have 5 dynamic addresses pointing to my server at home. Also, download DynDNS updater to automatically update the IP every time it changes.
 
Have a good one!
Amer Gerzic
www.amergerzic.com

Generalremove #definememberPanic2k39 Nov '07 - 8:39 
You should attempt to use enum or const in place of #define...
 
/// HACK: Don't PANIC ...
http://en.wikibooks.org/wiki/User:Panic2k4

GeneralRe: remove #define - Microsoft should! it is windns.hmemberChesnokov Yuriy9 Nov '07 - 20:55 
I'm using enums always and const. That is the excerpt from windns.h file provided by Visual Studio 2005Smile | :)
 
chesnokov

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 8 Nov 2007
Article Copyright 2007 by Chesnokov Yuriy
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid