HexDns
concepts
| This guide covers only the use of HexDns and assumes a working knowledge of
the Domain Name System. If you need additional information on DNS itself, see the Resources section for pointers. |
Though they often don't realize it, Internet users employ the Domain Name System every
day. It provides the essential mapping between domain names such as
"www.hexillion.com" and IP addresses such as "216.46.230.21".
Even savvy Netizens, however, often forget that the DNS holds more than just IP
addresses. In fact, it can contain just about any sort of information linked to a domain
name; more than 40 record types have already been defined.
HexDns provides easy programmatic access to all these records through a set of COM
objects. These objects are designed to be quite general and flexible, but they also make
common tasks easy through shortcuts and intelligent defaults.
A simple example
To introduce you to basic HexDns programming concepts, let's start with a short
ASP/VBScript example that gets mail server records for Yahoo:
Dim oConn, oMsg, oRec
Set oConn =
Server.CreateObject("HexDns.Connection")
Set oMsg =
oConn.Query("yahoo.com", hexDnsTypeMX)
For Each oRec in
oMsg.AnswerRecords
Response.Write oRec.Name & " " & oRec.Type
Response.Write "<br>" & vbCrLf
Next
These few lines demonstrate the basic steps you will need to take for all your HexDns
code:
- Create a Connection object
- Execute a query
- Examine the returned data
In this case, we create the Connection object and use it to query for
MX records. Behind the scenes, the Query
method fills in a DNS message and sends it to the default DNS server specified in your
machine's TCP/IP properties. It then returns the DNS response message it receives.
The Message object contains the
original question along with any records returned in three categories. Our example
iterates through the records of the AnswerRecords collection and prints
the domain name and type for each.
Object hierarchy
The diagram below lists all the objects that are part of the HexDns component. In
general, the Connection and Lookup
objects do most of the work, while the others serve as smart data structures.
The Lookup object provides several utility methods for
working with IP addresses, including two methods that implement basic query logic and
error checking for forward and reverse lookups. Lookup is very similar to
the HexLookup component.
The Message object contains three collections of Record objects, with each object
representing one of the resource records returned by a query. When HexDns recognizes the
type of a returned record, however, it places a type-specific record object in the
collection. These type-specific objects, which you see listed separately in the diagram,
expose interfaces derived from the Record object's interface and enhanced
with type-specific properties. For instance, RecordInA supports all the
properties of Record plus an additional property, Addr,
that returns the IP address from the record data.
VBScript example
Dim oConn, oMsg, oRec, oLkup
Set oConn =
Server.CreateObject("HexDns.Connection")
Set oLkup =
Server.CreateObject("HexDns.Lookup")
Set oMsg =
oConn.Query("yahoo.com", hexDnsTypeA)
For Each oRec in
oMsg.AnswerRecords
Response.Write oRec.Name & " " & oRec.Type &
" "
If hexDnsTypeA =
oRec.Type And _
hexDnsClassIN = oRec.Class Then
Response.Write oLkup.AddrToString(oRec.Addr)
End If
Response.Write "<br>" & vbCrLf
Next
The ASP/VBScript snippet above extends our first simple example (and it queries for A
records this time). While iterating through the answer records, it checks the type and
class of each record. If they equal hexDnsTypeA and hexDnsClassIn, respectively, it knows
that oRec actually points to a RecordInA object, which provides the Addr
property. Because VBScript does late binding, it can access Addr
immediately. One of the Lookup methods converts the numeric address in Addr
to a human-readable string.
VB example
Dim oConn As
HexDnsLib.Connection
Dim oLkup As
HexDnsLib.Lookup
Dim oMsg As
HexDnsLib.Message
Dim oRec As
HexDnsLib.Record
Dim oRecA As
HexDnsLib.RecordInA
Dim lIndex As
Long
Set oConn = New
HexDnsLib.Connection
Set oLkup = New
HexDnsLib.Lookup
Set oMsg =
oConn.Query("yahoo.com", hexDnsTypeA)
For lIndex = 1 To
oMsg.AnswerRecords.Count
Set oRec =
oMsg.AnswerRecords(lIndex)
Debug.Print oRec.Name
& " " & oRec.Type & " ";
If hexDnsTypeA =
oRec.Type And _
hexDnsClassIN = oRec.Class Then
Set
oRecA = oRec
Debug.Print
oLkup.AddrToString(oRecA.Addr);
End If
Debug.Print
Next
This VB code does the same thing as the VBScript code above, but with strongly-typed
early binding this time. (You'll need to tell VB about HexDns by selecting it in VB's
Project | References dialog.) In this case, you must explicitly declare a variable of type
RecordInA and assign the Record object to it before
accessing the Addr property. If you try to assign a non-type-A Record
to a RecordInA variable, you will get a runtime error.
Going from here
This brief introduction to HexDns demonstrates the basic concepts and gets you started
with some simple code. You can find more details in the documentation for each of the
objects. The HexDns sample programs will show you more "real world" code.
|