Quicklinks
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.
Download the the ActiveSocket Toolkit from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.
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)
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 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:
Imports ASOCKETLib
In your Main function, declare and create the following object:
Public m_objDnsServer As DnsServer m_objDnsServer = New DnsServer()
When the required DNS object is created, you can implement the code to query a hostname on a DNS server:
Imports ASOCKETLib
Public Class DnsForm
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
#End Region
Private objDnsServer As IDnsServer
Private objConstants As ISocketConstants
Private Sub DnsForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
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"
End Sub
Private Sub buttonView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonView.Click
If (System.IO.File.Exists(textLogFile.Text.ToString())) Then
System.Diagnostics.Process.Start(textLogFile.Text)
End If
End Sub
Private Function GetResult()
GetResult = objDnsServer.LastError
If (objDnsServer.LastError = 22355) Then
textResult.Text = "0 : Success"
Else
textResult.Text = objDnsServer.LastError & " : " & objDnsServer.GetErrorDescription(objDnsServer.LastError)
End If
End Function
Private Sub buttonLookup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonLookup.Click
Dim nType As Integer = objConstants.asDNS_TYPE_ANY
Select Case (comboType.SelectedIndex)
Case 0
nType = objConstants.asDNS_TYPE_ANY
Case 1
nType = objConstants.asDNS_TYPE_NS
Case 2
nType = objConstants.asDNS_TYPE_CNAME
Case 3
nType = objConstants.asDNS_TYPE_SOA
Case 4
nType = objConstants.asDNS_TYPE_PTR
Case 5
nType = objConstants.asDNS_TYPE_MX
Case 6
nType = objConstants.asDNS_TYPE_AAAA
Case 7
nType = objConstants.asDNS_TYPE_ANY
End Select
objDnsServer.LogFile = textLogFile.Text
objDnsServer.Server = textServer.Text
objDnsServer.Lookup(textHost.Text, nType)
textQueryResults.Text = ""
If (GetResult() = 0) Then
Dim objDnsRecord As DnsRecord
objDnsRecord = objDnsServer.GetFirstRecord()
While (GetResult() = 0)
If (objDnsRecord.Type = objConstants.asDNS_TYPE_A) Then
textQueryResults.AppendText(vbCrLf & "Type: A")
textQueryResults.AppendText(vbCrLf & vbTab & "Name: " & objDnsRecord.Name)
textQueryResults.AppendText(vbCrLf & vbTab & "Address: " & objDnsRecord.Address)
End If
If (objDnsRecord.Type = objConstants.asDNS_TYPE_AAAA) Then
textQueryResults.AppendText(vbCrLf & "Type: AAAA")
textQueryResults.AppendText(vbCrLf & vbTab & "tName: " + objDnsRecord.Name)
textQueryResults.AppendText(vbCrLf & vbTab & "Address: " + objDnsRecord.Address)
End If
If (objDnsRecord.Type = objConstants.asDNS_TYPE_CNAME) Then
textQueryResults.AppendText(vbCrLf & "Type: CNAME")
textQueryResults.AppendText(vbCrLf & vbTab & "Name: " & objDnsRecord.Name)
textQueryResults.AppendText(vbCrLf & vbTab & "Alias: " & objDnsRecord.Address)
End If
If (objDnsRecord.Type = objConstants.asDNS_TYPE_NS) Then
textQueryResults.AppendText(vbCrLf & "Type: NS")
textQueryResults.AppendText(vbCrLf & vbTab & "Name: " & objDnsRecord.Name)
textQueryResults.AppendText(vbCrLf & vbTab & "NameServer: " & objDnsRecord.NameServer)
End If
If (objDnsRecord.Type = objConstants.asDNS_TYPE_MX) Then
textQueryResults.AppendText(vbCrLf & "Type: MX")
textQueryResults.AppendText(vbCrLf & vbTab & "Name: " & objDnsRecord.Name)
textQueryResults.AppendText(vbCrLf & vbTab & "Preference: " & objDnsRecord.Preference)
textQueryResults.AppendText(vbCrLf & vbTab & "MailExchange: " & objDnsRecord.MailExchange)
End If
If (objDnsRecord.Type = objConstants.asDNS_TYPE_PTR) Then
textQueryResults.AppendText(vbCrLf & "Type: PTR")
textQueryResults.AppendText(vbCrLf & vbTab & "Name: " & objDnsRecord.Name)
textQueryResults.AppendText(vbCrLf & vbTab & "Address: " & objDnsRecord.Address)
End If
If (objDnsRecord.Type = objConstants.asDNS_TYPE_SOA) Then
textQueryResults.AppendText(vbCrLf & "Type: SOA")
textQueryResults.AppendText(vbCrLf & vbTab & "Name: " & objDnsRecord.Name)
textQueryResults.AppendText(vbCrLf & vbTab & "Name Server: " & objDnsRecord.NameServer)
textQueryResults.AppendText(vbCrLf & vbTab & "MailBox: " & objDnsRecord.MailBox)
textQueryResults.AppendText(vbCrLf & vbTab & "Serial: " & objDnsRecord.SerialNumber)
textQueryResults.AppendText(vbCrLf & vbTab & "Refresh: " & objDnsRecord.RefreshInterval)
textQueryResults.AppendText(vbCrLf & vbTab & "Retry Interval: " & objDnsRecord.RetryInterval)
textQueryResults.AppendText(vbCrLf & vbTab & "Expiration Limit: " & objDnsRecord.ExpirationLimit)
textQueryResults.AppendText(vbCrLf & vbTab & "Minimum TTL: " & objDnsRecord.MinimumTTL)
End If
objDnsRecord = objDnsServer.GetNextRecord()
End While
End If
End Sub
End Class
You can download the complete sample from our ftp site ftp.activexperts-labs.com/samples/network-component. There are many other working ActiveSocket scripts on our site and shipped with the product.