Domain Name System DNS component for C++ for Windows


ActiveSocket is a Network Communication component for Windows Developers. It runs on any 32 bit and 64 bit Windows Platform, incl. Windows 7, Windows 2008, Windows 2003, Windows 2000, Windows Vista and Windows XP.
It features many IP protocols, incl.: SSH (Secure Shell), RSH (Remote Shell), HTTP(s), FTP, ICMP Ping, NTP, SNMP v1/v2c (Get,GetNext,Set), SNMP MIB translation, SNMP Trap Sender, SNMP Trap Receiver, Telnet, DNS, TCP, UDP, IP-to-Country, Wake-On-LAN and more. Samples are included many popular development platforms, incl. Visual C# .NET, Visual Basic .NET, ASP .NET, Visual Basic, Visual C/C++, ASP, Java, Javascript, PHP, Borland Delphi, Borland C++ Builder, ColdFusion.

The Domain Name System (DNS) is the method by which Internet addresses in mnemonic form - such as www.activexperts.com - are converted into the equivalent numeric IP address such as 212.97.55.136. To the user and application process this translation is a service provided either by the local host or from a remote host via the Internet. The DNS server (or resolver) may communicate with other Internet DNS servers if it cannot translate the address itself.

DNS names are constructed hierarchically. The highest level of the hierarchy is the last component or label of the DNS address. Labels can be up to 63 characters long and are not case sensitive. A maximum length of 255 characters is allowed. Labels must start with a letter and can only consist of letters, digits and hyphens.

Nslookup is a popular program for UNIX, LINUX and Windows to query Internet domain name servers. It allows the user to query name servers for information about various hosts and domains or to print a list of hosts in a domain.

Step 2: Create a new Visual C++ project

Launch 'Microsoft Visual C++' from the Start menu, and choose 'New' from the 'File Menu'. The 'New' dialog appears.

Select the type of project (for instance: 'Win32 Console Application'), enter a 'Project name' and select the 'Location':

(Click on the picture to enlarge)

Select the kind of project, for instance a 'Hello, world!' application and click 'Finish':

(Click on the picture to enlarge)

Step 3: Refer to the ActiveSocket Library and declare the objects

A new Project is created now.

Before you can use ActiveSocket, you need to refer to the ActiveSocket library. The actually reference files are shipped with the product and are located in the following directory:

    C:\Program Files\ActiveXperts\ActiveSocket\Examples\Visual C++\Include

Copy all files in the above directory ('ASocket.h', 'ASocket_i.c' and 'ASocketConstants.h') to your project directory.

On top of your code, declare the following object:

    IDnsServer	*pDnsServer = NULL;

Step 4: Create the objects

Since the ActiveComport Toolkit is a COM object, you must initialize the COM library before they can call COM library functions (e.g. ActiveComport functions):

   CoInitialize(NULL);

Create the object in the following way:

   CoCreateInstance(CLSID_DnsServer, NULL, CLSCTX_INPROC_SERVER, IID_IDnsServer, (void**) &pDnsServer );

Source code: query a DNS server for all DNS records associates with a specific domain name

#include <comdef.h>
#include <atlbase.h>
#include <windows.h>
#include <stdio.h>

#include "..\include\ASocketConstants.h"
#include "..\include\ASocket_i.c"
#include "..\include\ASocket.h"

////////////////////////////////////////////////////////////////////////////////

VOID print( IDnsRecord *pDnsRecord );

////////////////////////////////////////////////////////////////////////////////

int main(int argc, char* argv[])
{
    IDnsServer	*pDnsServer      = NULL;
    IDnsRecord	*pDnsRecord      = NULL;
    
    LONG		lLastError;
    HRESULT		hr;
    _bstr_t		bstrDnsServer    = "ns1.interstroom.nl";
    _bstr_t		bstrHost         = "activexperts.com";
    
    VARIANT		vtVar;
    
    // Initialize COM
    CoInitialize(NULL);
    
    // Initialize Variant
    VariantInit	( &vtVar );
    
    hr = CoCreateInstance(CLSID_DnsServer, NULL, CLSCTX_INPROC_SERVER, IID_IDnsServer, (void**) &pDnsServer );
    if( ! SUCCEEDED( hr ) )
    {       
        pDnsServer = NULL;
        printf( "Unable to create instance of the object.\n" );
        goto _EndMain;
    }
    
    // Set DNS server
    pDnsServer->put_Server( bstrDnsServer );
    
    // Lookup
    pDnsServer->Lookup( bstrHost, asDNS_TYPE_ANY );
    pDnsServer->get_LastError( &lLastError );
    printf( "Lookup[%s], result: %d\n", ( char * ) bstrHost, lLastError );
    if( lLastError ) goto _EndMain;
    
    
    // Iterate over all objects
    pDnsServer->GetFirstRecord( &vtVar );
    pDnsServer->get_LastError( &lLastError );
    while( lLastError == 0L )
    {     
        if( vtVar.vt == VT_DISPATCH )
        {
            pDnsRecord = ( IDnsRecord * ) vtVar.pdispVal;
    
            print( pDnsRecord );
    
            VariantClear ( &vtVar );
        }
    
        pDnsServer->GetNextRecord( &vtVar );
        pDnsServer->get_LastError( &lLastError );
    }
    
_EndMain:
    
    if( pDnsServer != NULL )
    {      
        pDnsServer->Release();
        pDnsServer = NULL;
    }
    
    printf( "Ready.\n" );
    
    return 0;
}

VOID print( IDnsRecord *pDnsRecord )
{
    BSTR    bstrName = NULL, bstrAddress = NULL, bstrMailExchange = NULL, bstrNameServer = NULL;
    BSTR    bstrMailbox = NULL, bstrSerial = NULL;
    LONG    lType = 0L, lPreference = 0L, lRefreshInterval = 0L, lRetryInterval = 0L; 
    LONG    lExpLimit = 0L, lMinimumTTL = 0L;
    
    if( pDnsRecord == NULL )
        return;
    
    // Get: Type
    pDnsRecord->get_Type( &lType );
    // Get: Preference
    pDnsRecord->get_Preference( &lPreference );
    // Get: RefreshInterval
    pDnsRecord->get_RefreshInterval( &lRefreshInterval );
    // Get: RetryInterval
    pDnsRecord->get_RetryInterval( &lRetryInterval );
    // Get: ExpLimit
    pDnsRecord->get_ExpirationLimit( &lExpLimit );
    // Get: MinimumTTL
    pDnsRecord->get_MinimumTTL( &lMinimumTTL );
    
    // Get: Name
    pDnsRecord->get_Name( &bstrName );
    // Get: Address
    pDnsRecord->get_Address( &bstrAddress );
    // Get: NameServer
    pDnsRecord->get_NameServer( &bstrNameServer );
    // Get: MailExchange
    pDnsRecord->get_MailExchange( &bstrMailExchange );
    // Get: Mailbox
    pDnsRecord->get_MailBox( &bstrMailbox );
    // Get: Serial
    pDnsRecord->get_SerialNumber( &bstrSerial );
    
    switch( lType )
    {
        case asDNS_TYPE_A:
            printf( "Type: A\n" );
            printf( "  Name:         %ls\n", bstrName );
            printf( "  Address:      %ls\n", bstrAddress );
            break;
    case asDNS_TYPE_AAAA:
            printf( "Type: AAAA\n" );
            printf( "  Name:         %ls\n", bstrName );
            printf( "  Address:      %ls\n", bstrAddress );
            break;
    case asDNS_TYPE_CNAME:
            printf( "Type: CNAME\n" );
            printf( "  Name:         %ls\n", bstrName );
            printf( "  Alias:        %ls\n", bstrAddress );
            break;
    case asDNS_TYPE_MX:
            printf( "Type: MX\n" );
            printf( "  Name:         %ls\n", bstrName );
            printf( "  Preference:   %ld\n", lPreference );
            printf( "  MailExchange: %ls\n", bstrMailExchange);
            break;
    case asDNS_TYPE_NS:
            printf( "Type: NS\n" );
            printf( "  Name:         %ls\n", bstrName );
            printf( "  NameServer:   %ls\n", bstrNameServer);
            break;
    case asDNS_TYPE_PTR:
            printf( "Type: PTR\n" );
            printf( "  Name:         %ls\n", bstrName );
            printf( "  Host:         %ls\n", bstrAddress);
            break;
    case asDNS_TYPE_SOA:
            printf( "Type: PTR\n" );
            printf( "  Name:         %ls\n", bstrName );
            printf( "  NameServer:   %ls\n", bstrNameServer);
            printf( "  Mailbox:      %ls\n", bstrMailbox);
            printf( "  Serial:       %ls\n", bstrSerial);
            printf( "  Refresh:      %ld\n", lRefreshInterval);
            printf( "  Retry:        %ld\n", lRetryInterval);
            printf( "  Exp. Limit:   %ld\n", lExpLimit);
            printf( "  MinimumTTL:   %ld\n", lMinimumTTL);
            break;
    default:
            printf( "Type: UNKNOWN\n" );
            break;
    }
    printf( "\n" );
    
    if( bstrName )         SysFreeString( bstrName );
    if( bstrAddress )      SysFreeString( bstrAddress );
    if( bstrMailExchange ) SysFreeString( bstrName );
    if( bstrNameServer )   SysFreeString( bstrNameServer );
    if( bstrMailbox )      SysFreeString( bstrMailbox );
    if( bstrSerial )       SysFreeString( bstrSerial );
}