Version 12 of DNS

Updated 2003-01-23 16:55:51

[Domain Name System]

DNS is a distributed database that maps domain names to IP addresses so that we don't have to remember lots of numbers to navigate the internet. Domain lookups (also termed name resolution) is done using a DNS client library, the resolver, that is normally part of the system C library via the gethostbyname familly of calls. See resolver(5) and gethostbyname(3).

The architecture is described by RFC 1034 [L1 ] and the DNS protocol by RFC 1035 [L2 ]

One of the problems Tcl applications can encounter is that DNS resolution can be slow and processing is usually blocked while waiting for the DNS query to complete. This is because when you attempt to resolve a domain name, your local nameserver may need to ask another, who may need to ask another. DNS typically uses UDP so will retry queries a number of times using increasingly large delays between each retry. The net effect is that it can take up to 2 minutes to fail to resolve a domain name. In the meantime your application may be unresponsive the entire time as the thread of control is locked up in the resolver library and not running Tcl.

To deal with this issue it is possible to perform lookups using Tcl only and deal with the replies using Tcl's fileevent system. This should allow the application to remain responsive.


Jochen Loewer has written a pure-Tcl (client TCP) implementation.


Pat Thoyts has written a pure-Tcl (client TCP) implementation.

In March 2002 or so, tcllib acquired its own dns package.

Documentation for the Tcllib dns package can be found at http://tcllib.sourceforge.net/doc/dns.html

If you need to discuss the Tcllib dns implementation, a new page might be in order ...


Scotty knows DNS.

See also The DNS blocking problem.

[Much more to explain, as of February 2002.]


When using the dns package of Tcllib, you have to know which DNS server to use for host name resolutions. Your machine probably already knows that somewhere, it is just a matter of finding this information in a portable way. I have used the following script to perform this operation, it works on Windows and should on a decent UNIX system. For once, both architecture actually support the same binary name for the same service!! Feel free to adapt and use. EF

   set res [catch "exec nslookup localhost" lkup]
   if { $res == 0 } {
       set l [split $lkup]
       set nl ""
       foreach e $l {
           if { [string length $e] > 0 } {
               lappend nl $e
           }
       }

       set hostname ""
       set len [llength $nl]
       for { set i 0 } { $i < $len } { incr i } {
           set e [lindex $nl $i]
           if { [string match -nocase "*server*" $e] } {
               set hostname [lindex $nl [expr $i + 1]]
               break
           }
       }

       if { $hostname != "" } {
           puts "Primary DNS server is: $hostname"
       } else {
           puts "Could not find primary DNS server!"
       }
   } else {
     puts "Could not execute nslookup!"
   }

Category Internet