ActiveSocket Toolkit Add network capabilities to any Windows or .NET application

Quicklinks


DNS NsLookup using Visual C# .NET

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.

ActiveSocket provides an easy-to-use development/scripting interface to use the same operations as NsLookup, through the DnsServer class. By using ActiveSocket, you can very easily create or enhance Windows applications/scripts with DNS lookup features.

ActiveSocket features the following: DNS, FTP, HTTP, HTTPs, ICMP Ping, IP-to-Country, MSN, NTP, RSH, SCP, SFTP, SNMP v1/v2c (Get, GetNext, Set), SNMP Traps, SNMP MIB, SSH, TCP, Telnet, TFTP, UDP, Telnet, Wake-On-LAN and more.

Step 1: Download and install the ActiveSocket Toolkit

Download the the ActiveSocket Toolkit from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.

Step 2: Create a new Visual C# .NET Project

Launch Microsoft Visual Studio (for instance 'Microsoft Visual Studio 2005') from the Start menu. Choose 'New' from the 'File' menu and click on 'Project'. In the 'New Project' dialog, select a Visual Studio template (for instance: 'Console Application'). Select a name for the application (for instance: 'DemoApp') and a name for the solution (for instance: 'DemoSolution'). Also, select the directory where you want to store the project (for instance: 'C:\MyProjects):

(Click on the picture to enlarge)

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

Now that a new project has been created, you must add a reference to the ActiveSocket Toolkit in the project to be able to use the ActiveSocket object. To do so, choose 'Add Reference...' from the 'Project' menu. In the 'Add Reference' dialog that pops up, select the 'COM' tab and select the 'ActiveSocket 3.1 Type Library' as shown in the following picture:

(Click on the picture to enlarge)

Click 'OK' to close the 'Add Reference' dialog.

On top of your code, type the following line to use the ActiveSocket namespace:

using ASOCKETLib;

In your Main function, declare and create the following object:

public DnsServer m_objDnsServer;
   
m_objDnsServer = new DnsServer();

Step 4: Perform a DNS lookup

You can now use the ActiveSocket SDK to do a DNS address lookup.

The following code shows how to create a DNS lookup utility using Microsoft Visual C# .NET:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using ASOCKETLib;

namespace Demo
{
   <span class="comment">/// <summary>
   /// Summary description for DnsForm.
   /// </summary></span>
   public class DnsForm : System.Windows.Forms.Form
   {
      private System.Windows.Forms.GroupBox groupBox1;
      private System.Windows.Forms.Label label1;
      private System.Windows.Forms.Label label2;
      private System.Windows.Forms.Label label3;
      private System.Windows.Forms.TextBox textServer;
      private System.Windows.Forms.TextBox textHost;
      private System.Windows.Forms.ComboBox comboType;
      private System.Windows.Forms.Button buttonLookup;
      private System.Windows.Forms.GroupBox groupBox2;
      private System.Windows.Forms.Label label4;
      private System.Windows.Forms.Label label5;
      private System.Windows.Forms.Label label6;
      private System.Windows.Forms.TextBox textResult;
      private System.Windows.Forms.TextBox textQueryResults;
      private System.Windows.Forms.TextBox textLogFile;
      private System.Windows.Forms.Button buttonView;
      <span class="comment">/// <summary>
      /// Required designer variable.
      /// </summary></span>
      private System.ComponentModel.Container components = null;

      public DnsForm()
      {
         <span class="comment">//
         // Required for Windows Form Designer support
         //</span>
         InitializeComponent();

         <span class="comment">//
         // TODO: Add any constructor code after InitializeComponent call
         //</span>
      }
<span class="comment">
      /// <summary>
      /// Clean up any resources being used.
      /// </summary></span>
      protected override void Dispose( bool disposing )
      {
         if( disposing )
         {
            if(components != null)
            {
               components.Dispose();
            }
         }
         base.Dispose( disposing );
      }

      #region Windows Form Designer generated code
      #endregion

      private DnsServer objDnsServer;
      private SocketConstants objConstants;
      private long GetResult ()
      {
         long  lResult = objDnsServer.LastError;

         if ( lResult == 22355 ) 
         {
            textResult.Text = "0 : Success";
         }
         else
         {
            textResult.Text = objDnsServer.LastError.ToString () + " : " + objDnsServer.GetErrorDescription ( (int ) lResult );
         }

         return lResult;
      }

      private void DnsForm_Load(object sender, System.EventArgs e)
      {
         objDnsServer = new DnsServer ();
         objConstants = new SocketConstants ();

         objDnsServer.Clear ();
         
         comboType.Items.Add ( "A Record" );
         comboType.Items.Add ( "NS Record" );
         comboType.Items.Add ( "CNAME Record");
         comboType.Items.Add ( "SOA Record" );
         comboType.Items.Add ( "PTR Record" );
         comboType.Items.Add ( "MX Record" );
         comboType.Items.Add ( "AAAA Record" );
         comboType.Items.Add ( "ANY Record" );

         comboType.SelectedIndex = 7;

         textLogFile.Text = System.IO.Path.GetTempPath() + "DnsLog.txt";
      }

      private void buttonLookup_Click(object sender, System.EventArgs e)
      {
         int   nType = objConstants.asDNS_TYPE_ANY;

         switch ( comboType.SelectedIndex )
         {
            case 0:  nType = objConstants.asDNS_TYPE_A;     break;
            case 1:  nType = objConstants.asDNS_TYPE_NS;    break;
            case 2:  nType = objConstants.asDNS_TYPE_CNAME; break;
            case 3:  nType = objConstants.asDNS_TYPE_SOA;   break;
            case 4:  nType = objConstants.asDNS_TYPE_PTR;   break;
            case 5:  nType = objConstants.asDNS_TYPE_MX;    break;
            case 6:  nType = objConstants.asDNS_TYPE_AAAA;  break;
            case 7:  nType = objConstants.asDNS_TYPE_ANY;   break;
         }

         objDnsServer.LogFile = textLogFile.Text;
         objDnsServer.Server = textServer.Text;

         objDnsServer.Lookup ( textHost.Text, nType );

         textQueryResults.Text = "";

         if ( GetResult () == 0 )
         {
            DnsRecord   objDnsRecord = ( DnsRecord ) objDnsServer.GetFirstRecord ();

            while ( GetResult () == 0 )
            {
               if ( objDnsRecord.Type == objConstants.asDNS_TYPE_A )
               {
                  textQueryResults.Text += "\r\nType: A\r\n";
                  textQueryResults.Text += "\r\n\tName: " + objDnsRecord.Name;
                  textQueryResults.Text += "\r\n\tAddress: " + objDnsRecord.Address;
               }
               
               if ( objDnsRecord.Type == objConstants.asDNS_TYPE_AAAA )
               {
                  textQueryResults.Text += "\r\nType: AAAA\r\n";
                  textQueryResults.Text += "\r\n\tName: " + objDnsRecord.Name;
                  textQueryResults.Text += "\r\n\tAddress: " + objDnsRecord.Address;
               }

               if ( objDnsRecord.Type == objConstants.asDNS_TYPE_CNAME )
               {
                  textQueryResults.Text += "\r\nType: CNAME\r\n";
                  textQueryResults.Text += "\r\n\tName: " + objDnsRecord.Name;
                  textQueryResults.Text += "\r\n\tAlias: " + objDnsRecord.Address;
               }

               if ( objDnsRecord.Type == objConstants.asDNS_TYPE_NS )
               {
                  textQueryResults.Text += "\r\nType: NS\r\n";
                  textQueryResults.Text += "\r\n\tName: " + objDnsRecord.Name;
                  textQueryResults.Text += "\r\n\tNameServer: " + objDnsRecord.NameServer;
               }

               if ( objDnsRecord.Type == objConstants.asDNS_TYPE_MX )
               {
                  textQueryResults.Text += "\r\nType: MX\r\n";
                  textQueryResults.Text += "\r\n\tName: " + objDnsRecord.Name;
                  textQueryResults.Text += "\r\n\tPreference: " + objDnsRecord.Preference;
                  textQueryResults.Text += "\r\n\tMailExchange: " + objDnsRecord.MailExchange;
               }

               if ( objDnsRecord.Type == objConstants.asDNS_TYPE_PTR )
               {
                  textQueryResults.Text += "\r\nType: PTR\r\n";
                  textQueryResults.Text += "\r\n\tName: " + objDnsRecord.Name;
                  textQueryResults.Text += "\r\n\tAddress: " + objDnsRecord.Address;
               }

               if ( objDnsRecord.Type == objConstants.asDNS_TYPE_SOA )
               {
                  textQueryResults.Text += "\r\nType: SOA\r\n";
                  textQueryResults.Text += "\r\n\tName: " + objDnsRecord.Name;
                  textQueryResults.Text += "\r\n\tName Server: " + objDnsRecord.NameServer;
                  textQueryResults.Text += "\r\n\tMailBox: " + objDnsRecord.MailBox;
                  textQueryResults.Text += "\r\n\tSerial: " + objDnsRecord.SerialNumber;
                  textQueryResults.Text += "\r\n\tRefresh: " + objDnsRecord.RefreshInterval;
                  textQueryResults.Text += "\r\n\tRetry Interval: " + objDnsRecord.RetryInterval;
                  textQueryResults.Text += "\r\n\tExpiration Limit: " + objDnsRecord.ExpirationLimit;
                  textQueryResults.Text += "\r\n\tMinimum TTL: " + objDnsRecord.MinimumTTL;
               }

               objDnsRecord = ( DnsRecord ) objDnsServer.GetNextRecord ();
            }
         }
      }
   }
}

There are many working samples included with the product. You can also find them on the ActiveXperts FTP site: ftp.activexperts-labs.com/samples/network-component.

NOTE: Demo Projects are created with Microsoft Visual Studio 2005

The ActiveSocket project ships with a set of Microsoft Visual Studio .NET samples, including samples for Microsoft Visual C# .NET. The projects are created with Microsoft Visual Studio 2005.

Users with a later version of Microsoft Visual Studio can open such a project. The Visual Studio Conversion Wizard will guide you through the process of converting the project to the version used.