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

DNS Query MFC based Application

, 8 Nov 2007 GPL3
Rate this:
Please Sign up or sign in to vote.
This article demonstrates DNS query for specific domain address with MFC GUI interface for convenient usage

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)

Share

About the Author

Chesnokov Yuriy
Engineer
Russian Federation Russian Federation
No Biography provided

Comments and Discussions

 
GeneralDNS_TYPE_NAPTR Pinmemberaddick29-Feb-08 1:21 
AnswerRe: DNS_TYPE_NAPTR PinmvpChesnokov Yuriy29-Feb-08 1:51 
GeneralRe: DNS_TYPE_NAPTR Pinmemberaddick29-Feb-08 1:55 
Do you know for sure that this can't be done through the DnsQuery API function?
AnswerRe: DNS_TYPE_NAPTR PinmvpChesnokov Yuriy29-Feb-08 5:51 
GeneralRe: DNS_TYPE_NAPTR Pinmemberaddick29-Feb-08 6:11 
Generalgood Pinmemberrajantawate1(http//www.jhatak.com)11-Nov-07 17:32 
GeneralMy Own assigned IP Address PinmemberAlexEvans10-Nov-07 16:00 
GeneralRe: My Own assigned IP Address PinmemberA. Riazi11-Nov-07 9:31 
GeneralRe: My Own assigned IP Address PinmemberAlexEvans30-Nov-07 11:38 
GeneralRe: My Own assigned IP Address PinmemberAmer Gerzic15-Jan-08 1:32 
Generalremove #define PinmemberPanic2k39-Nov-07 9:39 
GeneralRe: remove #define - Microsoft should! it is windns.h PinmemberChesnokov Yuriy9-Nov-07 21:55 

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

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

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