/usr/share/tcltk/tcllib1.18/dns/resolv.tcl is in tcllib 1.18-dfsg-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | # resolv.tcl - Copyright (c) 2002 Emmanuel Frecon <emmanuel@sics.se>
#
# Original Author -- Emmanuel Frecon - emmanuel@sics.se
# Modified by Pat Thoyts <patthoyts@users.sourceforge.net>
#
# A super module on top of the dns module for host name resolution.
# There are two services provided on top of the regular Tcl library:
# Firstly, this module attempts to automatically discover the default
# DNS server that is setup on the machine that it is run on. This
# server will be used in all further host resolutions. Secondly, this
# module offers a rudimentary cache. The cache is rudimentary since it
# has no expiration on host name resolutions, but this is probably
# enough for short lived applications.
#
# -------------------------------------------------------------------------
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
# -------------------------------------------------------------------------
package require dns 1.0; # tcllib 1.3
namespace eval ::resolv {
namespace export resolve init ignore hostname
variable R
if {![info exists R]} {
array set R {
initdone 0
dns ""
dnsdefault ""
ourhost ""
search {}
}
}
}
# -------------------------------------------------------------------------
# Command Name -- ignore
# Original Author -- Emmanuel Frecon - emmanuel@sics.se
#
# Remove a host name resolution from the cache, if present, so that the
# next resolution will query the DNS server again.
#
# Arguments:
# hostname - Name of host to remove from the cache.
#
proc ::resolv::ignore { hostname } {
variable Cache
catch {unset Cache($hostname)}
return
}
# -------------------------------------------------------------------------
# Command Name -- init
# Original Author -- Emmanuel Frecon - emmanuel@sics.se
#
# Initialise this module with a known host name. This host (not mandatory)
# will become the default if the library was not able to find a DNS server.
# This command can be called several times, its effect is double: actively
# looking for the default DNS server setup on the running machine; and
# emptying the host name resolution cache.
#
# Arguments:
# defaultdns - Default DNS server
#
proc ::resolv::init { {defaultdns ""} {search {}}} {
variable R
variable Cache
# Clean the resolver cache
catch {unset Cache}
# Record the default DNS server and search list.
set R(dnsdefault) $defaultdns
set R(search) $search
# Now do some intelligent lookup. We do this on the current
# hostname to get a chance to get back some (full) information on
# ourselves. A previous version was using 127.0.0.1, not sure
# what is best.
set res [catch [list exec nslookup [info hostname]] lkup]
if { $res == 0 } {
set l [split $lkup]
set nl ""
foreach e $l {
if { [string length $e] > 0 } {
lappend nl $e
}
}
# Now, a lot of mixture to arrange so that hostname points at the
# DNS server that we should use for any further request. This
# code is complex, but was actually tested behind a firewall
# during the SITI Winter Conference 2003. There, strangly,
# nslookup returned an error but a DNS server was actually setup
# correctly...
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}]]
if { [string match -nocase "UnKnown" $hostname] } {
set hostname ""
}
break
}
}
if { $hostname != "" } {
set R(dns) $hostname
} else {
for { set i 0 } { $i < $len } { incr i } {
set e [lindex $nl $i]
if { [string match -nocase "*address*" $e] } {
set hostname [lindex $nl [expr {$i + 1}]]
break
}
}
if { $hostname != "" } {
set R(dns) $hostname
}
}
}
if {$R(dns) == ""} {
set R(dns) $R(dnsdefault)
}
# Start again to find our full name
set ourhost ""
if {$res == 0} {
set dot [string first "." [info hostname]]
if { $dot < 0 } {
for { set i 0 } { $i < $len } { incr i } {
set e [lindex $nl $i]
if { [string match -nocase "*name*" $e] } {
set ourhost [lindex $nl [expr {$i + 1}]]
break
}
}
if { $ourhost == "" } {
if { ! [regexp {\d+\.\d+\.\d+\.\d+} $hostname] } {
set dot [string first "." $hostname]
set ourhost [format "%s%s" [info hostname] \
[string range $hostname $dot end]]
}
}
} else {
set ourhost [info hostname]
}
}
if {$ourhost == ""} {
set R(ourhost) [info hostname]
} else {
set R(ourhost) $ourhost
}
set R(initdone) 1
return $R(dns)
}
# -------------------------------------------------------------------------
# Command Name -- resolve
# Original Author -- Emmanuel Frecon - emmanuel@sics.se
#
# Resolve a host name to an IP address. This is a wrapping procedure around
# the basic services of the dns library.
#
# Arguments:
# hostname - Name of host
#
proc ::resolv::resolve { hostname } {
variable R
variable Cache
# Initialise if not already done. Auto initialisation cannot take
# any known DNS server (known to the caller)
if { ! $R(initdone) } { init }
# Check whether this is not simply a raw IP address. What about
# IPv6 ??
# - We don't have sockets in Tcl for IPv6 protocols - [PT]
#
if { [regexp {\d+\.\d+\.\d+\.\d+} $hostname] } {
return $hostname
}
# Look for hostname in the cache, if found return.
if { [array names ::resolv::Cache $hostname] != "" } {
return $::resolv::Cache($hostname)
}
# Scream if we don't have any DNS server setup, since we cannot do
# anything in that case.
if { $R(dns) == "" } {
return -code error "No dns server provided"
}
set R(retries) 0
set ip [Resolve $hostname]
# And store the result of resolution in our cache for further use.
set Cache($hostname) $ip
return $ip
}
# Description:
# Attempt to resolve hostname via DNS. If the name cannot be resolved then
# iterate through the search list appending each domain in turn until we
# get one that succeeds.
#
proc ::resolv::Resolve {hostname} {
variable R
set t [::dns::resolve $hostname -server $R(dns)]
::dns::wait $t; # wait with event processing
set status [dns::status $t]
if {$status == "ok"} {
set ip [lindex [::dns::address $t] 0]
::dns::cleanup $t
} elseif {$status == "error"
&& [::dns::errorcode $t] == 3
&& $R(retries) < [llength $R(search)]} {
::dns::cleanup $t
set suffix [lindex $R(search) $R(retries)]
incr R(retries)
set new [lindex [split $hostname .] 0].[string trim $suffix .]
set ip [Resolve $new]
} else {
set err [dns::error $t]
::dns::cleanup $t
return -code error "dns error: $err"
}
return $ip
}
# -------------------------------------------------------------------------
package provide resolv 1.0.3
# -------------------------------------------------------------------------
# Local Variables:
# indent-tabs-mode: nil
# End:
|